コード例 #1
0
		public void TestObjectPool()
		{
			IPoolable r = null;
			var tt11 = this.InitlizeTestTarget1();
			var tt12 = this.InitlizeTestTarget1();
			var tt13 = this.InitlizeTestTarget1();
			var tt21 = this.InitlizeTestTarget2();
			var tt22 = this.InitlizeTestTarget2();
			var tt23 = this.InitlizeTestTarget2();
			string type1 = typeof(TestTarget1).Name;
			string type2 = typeof(TestTarget2).Name;

			///
			/// new
			///
			ObjectPool pool = new ObjectPool(null);
			r = pool.Get(tt11.GetType().Name);
			Assert.IsNull(r);
			Assert.AreEqual(pool.Count, 0);

			///
			/// Set & Get & Count
			///
			pool.Set(tt11, type1);
			Assert.IsNull(pool.Get(type2));
			Assert.AreEqual(pool.Count, 1);

			pool.Set(tt12, type1);
			Assert.IsNull(pool.Get(type2));
			Assert.AreEqual(pool.Count, 1);

			pool.Set(tt21, type2);
			Assert.AreSame(pool.Get(type2), tt21);
			Assert.AreEqual(pool.Count, 2);

			pool.Set(tt22, type2);
			Assert.AreSame(pool.Get(type1), tt11);
			Assert.AreEqual(pool.Count, 2);

			Assert.IsNull(pool.Get("unknown"));
			Assert.IsNull(pool.Get(null));
			Assert.AreSame(pool.Get(type1), tt12);
			Assert.IsNull(pool.Get(type1));
			Assert.AreSame(pool.Get(type2), tt22);
			Assert.IsNull(pool.Get(type2));

			///
			/// Remove
			///
			pool.Set(tt11, type1);
			Assert.AreEqual(pool.CountSet(type1), 1);
			Assert.AreEqual(pool.CountSet(type2), 0);

			pool.Set(tt12, type1);
			Assert.AreEqual(pool.CountSet(type1), 2);
			Assert.AreEqual(pool.CountSet(type2), 0);

			pool.Set(tt21, type2);
			Assert.AreEqual(pool.CountSet(type1), 2);
			Assert.AreEqual(pool.CountSet(type2), 1);

			pool.Set(tt22, type2);
			Assert.AreEqual(pool.CountSet(type1), 2);
			Assert.AreEqual(pool.CountSet(type2), 2);

			pool.Remove(tt12, type1);
			Assert.AreEqual(pool.CountSet(type1), 1);
			Assert.AreEqual(pool.CountSet(type2), 2);

			pool.Remove(tt12, type1);
			Assert.AreEqual(pool.CountSet(type1), 1);
			Assert.AreEqual(pool.CountSet(type2), 2);

			pool.Remove(tt11, type1);
			Assert.AreEqual(pool.CountSet(type1), 0);
			Assert.AreEqual(pool.CountSet(type2), 2);

			pool.Remove(tt11, type1);
			Assert.AreEqual(pool.CountSet(type1), 0);
			Assert.AreEqual(pool.CountSet(type2), 2);

			pool.Remove(type2);
			Assert.AreEqual(pool.CountSet(type1), 0);
			Assert.AreEqual(pool.CountSet(type2), 0);
		}