#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2017 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace Nuclex.Support.Collections {
/// Unit tests for the Pool class
[TestFixture]
internal class PoolTest {
#region class TestClass
/// Used to test the pool
private class TestClass : IRecyclable {
/// Returns the object to its initial state
public void Recycle() {
this.Recycled = true;
}
/// Whether the instance has been recycled
public bool Recycled;
}
#endregion // class TestClass
#region class NoDefaultConstructor
/// Used to test the pool
private class NoDefaultConstructor {
/// Private constructor so no instances can be created
private NoDefaultConstructor() { }
}
#endregion // class NoDefaultConstructor
///
/// Verifies that the pool can return newly constructed objects
///
[Test]
public void NewInstancesCanBeObtained() {
Pool pool = new Pool();
Assert.IsNotNull(pool.Get());
}
///
/// Verifies that an exception is thrown if the pool's default instance creator is used
/// on a type that doesn't have a default constructor
///
[Test]
public void UsingDefaultInstanceCreatorRequiresDefaultConstructor() {
Assert.Throws(
delegate() { new Pool(); }
);
}
///
/// Tests whether the pool can redeem objects that are no longer used
///
[Test]
public void InstancesCanBeRedeemed() {
Pool pool = new Pool();
pool.Redeem(new TestClass());
}
///
/// Tests whether the Recycle() method is called at the appropriate time
///
[Test]
public void RedeemedItemsWillBeRecycled() {
Pool pool = new Pool();
TestClass x = new TestClass();
Assert.IsFalse(x.Recycled);
pool.Redeem(x);
Assert.IsTrue(x.Recycled);
}
/// Verifies that the pool's Capacity is applied correctly
[Test]
public void PoolCapacityCanBeAdjusted() {
Pool pool = new Pool(123);
Assert.AreEqual(123, pool.Capacity);
pool.Capacity = 321;
Assert.AreEqual(321, pool.Capacity);
}
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST