Commits

smack0007 committed 2bbec47

More unit tests for ObjectPool.

Comments (0)

Files changed (1)

Snowball.Tests/Collections/ObjectPoolTests.cs

 		public void NoNewObjectsAllocatedWhenCapacityIsNotExceeded()
 		{
 			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+			
 			var foo = pool.Next();
 			var bar = pool.Next();
+			
 			Assert.AreEqual(2, ObjectPoolTestObject.ConstructorCalled);
 		}
 
 		{
 			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
 			pool.AutoGrow = false;
+			
 			var foo = pool.Next();
 			var bar = pool.Next();
 			var baz = pool.Next();
 			Assert.AreEqual(2, ObjectPoolTestObject.ConstructorCalled);
 			Assert.IsNull(baz);
 		}
+
+		[Test]
+		public void AutoGrowFactorIndicatesTheNumberOfNewObjectsAllocatedByAnAutoGrow()
+		{
+			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+			pool.AutoGrow = true;
+			pool.AutoGrowFactor = 2;
+
+			var foo = pool.Next();
+			var bar = pool.Next();
+			var baz = pool.Next();
+
+			Assert.AreEqual(4, ObjectPoolTestObject.ConstructorCalled);
+			Assert.AreEqual(4, pool.Capactiy);
+		}
+
+		[Test]
+		public void ActiveCountIncrementsWithEachCallToNext()
+		{
+			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+
+			var foo = pool.Next();
+			Assert.AreEqual(1, pool.ActiveCount);
+
+			var bar = pool.Next();
+			Assert.AreEqual(2, pool.ActiveCount);
+		}
+
+		[Test]
+		public void ReturningObjectCausesActiveCountToDecrement()
+		{
+			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+
+			var foo = pool.Next();
+			pool.Return(foo);
+
+			Assert.AreEqual(0, pool.ActiveCount);
+		}
+
+		[Test]
+		public void ReturningAnObjectTwiceCausesActiveCountToOnlyDecrementOnce()
+		{
+			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+
+			var foo = pool.Next();
+
+			var bar = pool.Next();
+			pool.Return(bar);
+			pool.Return(bar);
+
+			Assert.AreEqual(1, pool.ActiveCount);
+		}
 	}
 }