#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 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
using System;
using System.Collections;
using System.Collections.Generic;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Collections {
/// Unit Test for the read only collection wrapper
[TestFixture]
public class ReadOnlyCollectionTest {
///
/// Verifies that the copy constructor of the read only collection works
///
[Test]
public void TestCopyConstructor() {
int[] integers = new int[] { 12, 34, 56, 78 };
ReadOnlyCollection testCollection = new ReadOnlyCollection(integers);
CollectionAssert.AreEqual(integers, testCollection);
}
/// Verifies that the IsReadOnly property returns true
[Test]
public void TestIsReadOnly() {
ReadOnlyCollection testCollection = new ReadOnlyCollection(new int[0]);
Assert.IsTrue(testCollection.IsReadOnly);
}
///
/// Verifies that the CopyTo() of the read only collection works
///
[Test]
public void TestCopyToArray() {
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
ReadOnlyCollection testCollection = new ReadOnlyCollection(inputIntegers);
int[] outputIntegers = new int[testCollection.Count];
testCollection.CopyTo(outputIntegers, 0);
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
}
///
/// Checks whether the Contains() method of the read only collection is able to
/// determine if the collection contains an item
///
[Test]
public void TestContains() {
int[] integers = new int[] { 1234, 6789 };
ReadOnlyCollection testCollection = new ReadOnlyCollection(integers);
Assert.IsTrue(testCollection.Contains(1234));
Assert.IsFalse(testCollection.Contains(4321));
}
///
/// Ensures that the Add() method of the read only collection throws an exception
///
[Test]
public void TestThrowOnAdd() {
ReadOnlyCollection testCollection = new ReadOnlyCollection(new int[0]);
Assert.Throws(
delegate() { (testCollection as ICollection).Add(123); }
);
}
///
/// Ensures that the Remove() method of the read only collection throws an exception
///
[Test]
public void TestThrowOnRemove() {
ReadOnlyCollection testCollection = new ReadOnlyCollection(new int[0]);
Assert.Throws(
delegate() { (testCollection as ICollection).Remove(123); }
);
}
///
/// Ensures that the Clear() method of the read only collection throws an exception
///
[Test]
public void TestThrowOnClear() {
ReadOnlyCollection testCollection = new ReadOnlyCollection(new int[0]);
Assert.Throws(
delegate() { (testCollection as ICollection).Clear(); }
);
}
///
/// Tests whether the typesafe enumerator of the read only collection is working
///
[Test]
public void TestTypesafeEnumerator() {
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
ReadOnlyCollection testCollection = new ReadOnlyCollection(inputIntegers);
List outputIntegers = new List();
foreach(int value in testCollection) {
outputIntegers.Add(value);
}
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
}
///
/// Verifies that the CopyTo() of the read only collection works if invoked via
/// the ICollection interface
///
[Test]
public void TestCopyToArrayViaICollection() {
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
ReadOnlyCollection testCollection = new ReadOnlyCollection(inputIntegers);
int[] outputIntegers = new int[testCollection.Count];
(testCollection as ICollection).CopyTo(outputIntegers, 0);
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
}
///
/// Verifies that the IsSynchronized property and the SyncRoot property are working
///
[Test]
public void TestSynchronization() {
ReadOnlyCollection testCollection = new ReadOnlyCollection(new int[0]);
if(!(testCollection as ICollection).IsSynchronized) {
lock((testCollection as ICollection).SyncRoot) {
Assert.AreEqual(0, testCollection.Count);
}
}
}
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST