|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.HelperTests.ObjectPools |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Linq; |
| 9 | + using System.Text; |
| 10 | + using StyleCop.Analyzers.Helpers.ObjectPools; |
| 11 | + using Xunit; |
| 12 | + |
| 13 | + public class SharedPoolsTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void TestBigDefault() |
| 17 | + { |
| 18 | + var listPool = SharedPools.BigDefault<List<int>>(); |
| 19 | + Assert.IsAssignableFrom<ObjectPool<List<int>>>(listPool); |
| 20 | + |
| 21 | + var stackPool = SharedPools.BigDefault<Stack<int>>(); |
| 22 | + Assert.IsAssignableFrom<ObjectPool<Stack<int>>>(stackPool); |
| 23 | + |
| 24 | + Assert.NotSame(listPool, stackPool); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void TestDefault() |
| 29 | + { |
| 30 | + var listPool = SharedPools.BigDefault<List<int>>(); |
| 31 | + Assert.IsAssignableFrom<ObjectPool<List<int>>>(listPool); |
| 32 | + |
| 33 | + var stackPool = SharedPools.BigDefault<Stack<int>>(); |
| 34 | + Assert.IsAssignableFrom<ObjectPool<Stack<int>>>(stackPool); |
| 35 | + |
| 36 | + Assert.NotSame(listPool, stackPool); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void TestStringBuilderPool() |
| 41 | + { |
| 42 | + StringBuilder builder = null; |
| 43 | + using (var obj = SharedPools.Default<StringBuilder>().GetPooledObject()) |
| 44 | + { |
| 45 | + Assert.NotNull(obj.Object); |
| 46 | + Assert.True(obj.Object.Length == 0); |
| 47 | + |
| 48 | + obj.Object.Append("testing"); |
| 49 | + |
| 50 | + // Keep a copy to verify its length is reset |
| 51 | + builder = obj.Object; |
| 52 | + Assert.NotEqual(0, builder.Length); |
| 53 | + } |
| 54 | + |
| 55 | + Assert.Equal(0, builder.Length); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void TestStackPool() |
| 60 | + { |
| 61 | + Stack<int> collection = null; |
| 62 | + using (var obj = SharedPools.Default<Stack<int>>().GetPooledObject()) |
| 63 | + { |
| 64 | + Assert.NotNull(obj.Object); |
| 65 | + Assert.True(obj.Object.Count == 0); |
| 66 | + |
| 67 | + obj.Object.Push(1); |
| 68 | + |
| 69 | + // Keep a copy to verify its length is reset |
| 70 | + collection = obj.Object; |
| 71 | + Assert.NotEqual(0, collection.Count); |
| 72 | + } |
| 73 | + |
| 74 | + Assert.Equal(0, collection.Count); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void TestQueuePool() |
| 79 | + { |
| 80 | + Queue<int> collection = null; |
| 81 | + using (var obj = SharedPools.Default<Queue<int>>().GetPooledObject()) |
| 82 | + { |
| 83 | + Assert.NotNull(obj.Object); |
| 84 | + Assert.True(obj.Object.Count == 0); |
| 85 | + |
| 86 | + obj.Object.Enqueue(1); |
| 87 | + |
| 88 | + // Keep a copy to verify its length is reset |
| 89 | + collection = obj.Object; |
| 90 | + Assert.NotEqual(0, collection.Count); |
| 91 | + } |
| 92 | + |
| 93 | + Assert.Equal(0, collection.Count); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void TestHashSetPool() |
| 98 | + { |
| 99 | + HashSet<int> collection = null; |
| 100 | + using (var obj = SharedPools.Default<HashSet<int>>().GetPooledObject()) |
| 101 | + { |
| 102 | + Assert.NotNull(obj.Object); |
| 103 | + Assert.True(obj.Object.Count == 0); |
| 104 | + |
| 105 | + obj.Object.Add(1); |
| 106 | + |
| 107 | + // Keep a copy to verify its length is reset |
| 108 | + collection = obj.Object; |
| 109 | + Assert.NotEqual(0, collection.Count); |
| 110 | + } |
| 111 | + |
| 112 | + Assert.Equal(0, collection.Count); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void TestDictionaryPool() |
| 117 | + { |
| 118 | + Dictionary<int, int> collection = null; |
| 119 | + using (var obj = SharedPools.Default<Dictionary<int, int>>().GetPooledObject()) |
| 120 | + { |
| 121 | + Assert.NotNull(obj.Object); |
| 122 | + Assert.True(obj.Object.Count == 0); |
| 123 | + |
| 124 | + obj.Object.Add(1, 1); |
| 125 | + |
| 126 | + // Keep a copy to verify its length is reset |
| 127 | + collection = obj.Object; |
| 128 | + Assert.NotEqual(0, collection.Count); |
| 129 | + } |
| 130 | + |
| 131 | + Assert.Equal(0, collection.Count); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void TestListPool() |
| 136 | + { |
| 137 | + List<int> collection = null; |
| 138 | + using (var obj = SharedPools.Default<List<int>>().GetPooledObject()) |
| 139 | + { |
| 140 | + Assert.NotNull(obj.Object); |
| 141 | + Assert.True(obj.Object.Count == 0); |
| 142 | + |
| 143 | + obj.Object.Add(1); |
| 144 | + |
| 145 | + // Keep a copy to verify its length is reset |
| 146 | + collection = obj.Object; |
| 147 | + Assert.NotEqual(0, collection.Count); |
| 148 | + } |
| 149 | + |
| 150 | + Assert.Equal(0, collection.Count); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void TestExceptionPool() |
| 155 | + { |
| 156 | + using (var obj = SharedPools.Default<Exception>().GetPooledObject()) |
| 157 | + { |
| 158 | + Assert.NotNull(obj.Object); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void TestClearAndFreeNull() |
| 164 | + { |
| 165 | + SharedPools.Default<StringBuilder>().ClearAndFree(null); |
| 166 | + SharedPools.Default<HashSet<int>>().ClearAndFree(null); |
| 167 | + SharedPools.Default<Stack<int>>().ClearAndFree(null); |
| 168 | + SharedPools.Default<Queue<int>>().ClearAndFree(null); |
| 169 | + SharedPools.Default<Dictionary<int, int>>().ClearAndFree(null); |
| 170 | + SharedPools.Default<List<int>>().ClearAndFree(null); |
| 171 | + } |
| 172 | + |
| 173 | + [Fact] |
| 174 | + public void TestClearAndFreeLarge() |
| 175 | + { |
| 176 | + // StringBuilder |
| 177 | + var builder = new StringBuilder("text", 1024); |
| 178 | + SharedPools.Default<StringBuilder>().ClearAndFree(builder); |
| 179 | + Assert.Equal(0, builder.Length); |
| 180 | + Assert.True(builder.Capacity < 1024); |
| 181 | + |
| 182 | + // HashSet<int> |
| 183 | + var set = new HashSet<int>(Enumerable.Range(0, 1024)); |
| 184 | + SharedPools.Default<HashSet<int>>().ClearAndFree(set); |
| 185 | + Assert.Equal(0, set.Count); |
| 186 | + |
| 187 | + // Stack<int> |
| 188 | + var stack = new Stack<int>(Enumerable.Range(0, 1024)); |
| 189 | + SharedPools.Default<Stack<int>>().ClearAndFree(stack); |
| 190 | + Assert.Equal(0, stack.Count); |
| 191 | + |
| 192 | + // Queue<int> |
| 193 | + var queue = new Queue<int>(Enumerable.Range(0, 1024)); |
| 194 | + SharedPools.Default<Queue<int>>().ClearAndFree(queue); |
| 195 | + Assert.Equal(0, queue.Count); |
| 196 | + |
| 197 | + // Dictionary<int, int> **This one doesn't go back in the pool!** |
| 198 | + var dictionary = Enumerable.Range(0, 1024).ToDictionary(i => i); |
| 199 | + SharedPools.Default<Dictionary<int, int>>().ClearAndFree(dictionary); |
| 200 | + Assert.Equal(1024, dictionary.Count); |
| 201 | + |
| 202 | + // List<int> |
| 203 | + var list = new List<int>(Enumerable.Range(0, 1024)); |
| 204 | + Assert.True(list.Capacity >= 1024); |
| 205 | + SharedPools.Default<List<int>>().ClearAndFree(list); |
| 206 | + Assert.Equal(0, list.Count); |
| 207 | + Assert.True(list.Capacity < 1024); |
| 208 | + } |
| 209 | + |
| 210 | + [Fact] |
| 211 | + public void TestPooledObjectHandlesNullAllocation() |
| 212 | + { |
| 213 | + Func<ObjectPool<object>, object> allocator = pool => null; |
| 214 | + Action<ObjectPool<object>, object> releaser = (pool, obj) => { }; |
| 215 | + using (var obj = new PooledObject<object>(SharedPools.Default<object>(), allocator, releaser)) |
| 216 | + { |
| 217 | + Assert.Null(obj.Object); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments