Skip to content

Commit e1d9f47

Browse files
committed
Add initial object pools tests
1 parent ba0e2bc commit e1d9f47

3 files changed

Lines changed: 289 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 Analyzers.Helpers.ObjectPools;
8+
using Xunit;
9+
10+
public class ObjectPoolTests
11+
{
12+
[Fact]
13+
public void TestDefaultConstructor()
14+
{
15+
Func<object> factory = () => new object();
16+
var pool = new ObjectPool<object>(factory);
17+
Assert.IsType<object>(pool.Allocate());
18+
}
19+
20+
[Fact]
21+
public void TestAllocateFree()
22+
{
23+
Func<object> factory = () => new object();
24+
var pool = new ObjectPool<object>(factory);
25+
26+
// Covers the case where no item is in the pool
27+
Assert.IsType<object>(pool.Allocate());
28+
29+
// Covers the case where the item is the first in the pool
30+
var obj = new object();
31+
pool.Free(obj);
32+
Assert.Same(obj, pool.Allocate());
33+
34+
// Covers the case where the item is not the first in the pool
35+
var obj2 = new object();
36+
pool.Free(obj);
37+
pool.Free(obj2);
38+
Assert.Same(obj, pool.Allocate());
39+
Assert.Same(obj2, pool.Allocate());
40+
41+
// Covers the case (in AllocateSlow and FreeSlow) where the item is not the first or second in the pool
42+
var obj3 = new object();
43+
pool.Free(obj);
44+
pool.Free(obj2);
45+
pool.Free(obj3);
46+
Assert.Same(obj, pool.Allocate());
47+
Assert.Same(obj2, pool.Allocate());
48+
Assert.Same(obj3, pool.Allocate());
49+
}
50+
51+
[Fact]
52+
public void TestObjectCanBeDropped()
53+
{
54+
Func<object> factory = () => new object();
55+
var pool = new ObjectPool<object>(factory, 1);
56+
57+
var obj = new object();
58+
pool.Free(obj);
59+
var obj2 = new object();
60+
pool.Free(obj2);
61+
62+
Assert.Same(obj, pool.Allocate());
63+
Assert.NotSame(obj2, pool.Allocate());
64+
}
65+
}
66+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180
<Compile Include="Helpers\UseCultureAttribute.cs" />
181181
<Compile Include="HelperTests\AccessLevelHelperTests.cs" />
182182
<Compile Include="HelperTests\IndentationHelperTests.cs" />
183+
<Compile Include="HelperTests\ObjectPools\ObjectPoolTests.cs" />
184+
<Compile Include="HelperTests\ObjectPools\SharedPoolsTests.cs" />
183185
<Compile Include="HelperTests\RequiresTests.cs" />
184186
<Compile Include="HelperTests\TokenHelperTests.cs" />
185187
<Compile Include="HelperTests\TriviaHelperTests.cs" />

0 commit comments

Comments
 (0)