Commits

smack0007 committed 7a39cf4

Added some basic unit tests for ObjectPool.

Comments (0)

Files changed (2)

Snowball.Tests/Collections/ObjectPoolTests.cs

+using System;
+using NUnit.Framework;
+using Snowball.Collections;
+
+namespace Snowball.Tests.Collections
+{
+	[TestFixture]
+	public class ObjectPoolTests
+	{
+		class ObjectPoolTestObject
+		{
+			public static int ConstructorCalled = 0;
+
+			public ObjectPoolTestObject()
+			{
+				ConstructorCalled++;
+			}
+		}
+
+		[SetUp]
+		public void SetUp()
+		{
+			ObjectPoolTestObject.ConstructorCalled = 0;
+		}
+
+		[Test]
+		public void ConstructorAllocatesTheNumberOfObjectSpecifiedInCapacity()
+		{
+			ObjectPool<ObjectPoolTestObject> pool = new ObjectPool<ObjectPoolTestObject>(() => { return new ObjectPoolTestObject(); }, 2);
+			Assert.AreEqual(2, ObjectPoolTestObject.ConstructorCalled);
+		}
+
+		[Test]
+		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);
+		}
+
+		[Test]
+		public void NoNewObjectsAllocatedAndNullIsReturnedByNextWhenAutoGrowIsFalse()
+		{
+			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);
+		}
+	}
+}

Snowball.Tests/Snowball.Tests.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Collections\ObjectPoolTests.cs" />
     <Compile Include="CollisionHelperTests.cs" />
     <Compile Include="Graphics\ColorTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />