#region CPL License /* Nuclex Framework Copyright (C) 2002-2008 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 list wrapper [TestFixture] public class ReadOnlyListTest { /// /// Verifies that the copy constructor of the read only list works /// [Test] public void TestCopyConstructor() { int[] integers = new int[] { 12, 34, 56, 78 }; ReadOnlyList testList = new ReadOnlyList(integers); CollectionAssert.AreEqual(integers, testList); } /// Verifies that the IsReadOnly property returns true [Test] public void TestIsReadOnly() { ReadOnlyList testList = new ReadOnlyList(new int[0]); Assert.IsTrue(testList.IsReadOnly); } /// /// Verifies that the CopyTo() of the read only list works /// [Test] public void TestCopyToArray() { int[] inputIntegers = new int[] { 12, 34, 56, 78 }; ReadOnlyList testList = new ReadOnlyList(inputIntegers); int[] outputIntegers = new int[testList.Count]; testList.CopyTo(outputIntegers, 0); CollectionAssert.AreEqual(inputIntegers, outputIntegers); } /// /// Checks whether the Contains() method of the read only list is able to /// determine if the list contains an item /// [Test] public void TestContains() { int[] integers = new int[] { 1234, 6789 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.IsTrue(testList.Contains(1234)); Assert.IsFalse(testList.Contains(4321)); } /// /// Checks whether the IndexOf() method of the read only list is able to /// determine if the index of an item in the list /// [Test] public void TestIndexOf() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.AreEqual(0, testList.IndexOf(12)); Assert.AreEqual(1, testList.IndexOf(34)); Assert.AreEqual(2, testList.IndexOf(67)); Assert.AreEqual(3, testList.IndexOf(89)); } /// /// Checks whether the indexer method of the read only list is able to /// retrieve items from the list /// [Test] public void TestRetrieveByIndexer() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.AreEqual(12, testList[0]); Assert.AreEqual(34, testList[1]); Assert.AreEqual(67, testList[2]); Assert.AreEqual(89, testList[3]); } /// /// Checks whether the read only list will throw an exception if its Insert() method /// is called via the generic IList<> interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnInsertViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); (testList as IList).Insert(0, 12345); } /// /// Checks whether the read only list will throw an exception if its RemoveAt() method /// is called via the generic IList<> interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnRemoveViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as IList).RemoveAt(0); } /// /// Checks whether the indexer method of the read only list will throw an exception /// if it is attempted to be used for replacing an item /// [Test] public void TestRetrieveByIndexerViaGenericIList() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.AreEqual(12, (testList as IList)[0]); Assert.AreEqual(34, (testList as IList)[1]); Assert.AreEqual(67, (testList as IList)[2]); Assert.AreEqual(89, (testList as IList)[3]); } /// /// Checks whether the indexer method of the read only list will throw an exception /// if it is attempted to be used for replacing an item /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnReplaceByIndexerViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as IList)[0] = 12345; } /// /// Checks whether the read only list will throw an exception if its Add() method /// is called via the generic ICollection<> interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnAddViaGenericICollection() { ReadOnlyList testList = new ReadOnlyList(new int[0]); (testList as ICollection).Add(12345); } /// /// Checks whether the read only list will throw an exception if its Clear() method /// is called via the generic ICollection<> interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnClearViaGenericICollection() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as ICollection).Clear(); } /// /// Checks whether the read only list will throw an exception if its Remove() method /// is called via the generic ICollection<> interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnRemoveViaGenericICollection() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); (testList as ICollection).Remove(89); } /// /// Tests whether the typesafe enumerator of the read only list is working /// [Test] public void TestTypesafeEnumerator() { int[] inputIntegers = new int[] { 12, 34, 56, 78 }; ReadOnlyList testList = new ReadOnlyList(inputIntegers); List outputIntegers = new List(); foreach(int value in testList) { outputIntegers.Add(value); } CollectionAssert.AreEqual(inputIntegers, outputIntegers); } /// /// Checks whether the read only list will throw an exception if its Clear() method /// is called via the IList interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnClearViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as IList).Clear(); } /// /// Checks whether the read only list will throw an exception if its Add() method /// is called via the IList interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnAddViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); (testList as IList).Add(12345); } /// /// Checks whether the Contains() method of the read only list is able to /// determine if the list contains an item /// [Test] public void TestContainsViaIList() { int[] integers = new int[] { 1234, 6789 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.IsTrue((testList as IList).Contains(1234)); Assert.IsFalse((testList as IList).Contains(4321)); } /// /// Checks whether the IndexOf() method of the read only list is able to /// determine if the index of an item in the list /// [Test] public void TestIndexOfViaIList() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.AreEqual(0, (testList as IList).IndexOf(12)); Assert.AreEqual(1, (testList as IList).IndexOf(34)); Assert.AreEqual(2, (testList as IList).IndexOf(67)); Assert.AreEqual(3, (testList as IList).IndexOf(89)); } /// /// Checks whether the read only list will throw an exception if its Insert() method /// is called via the IList interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnInsertViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); (testList as IList).Insert(0, 12345); } /// /// Checks whether the IsFixedSize property of the read only list returns the /// expected result for a read only list based on a fixed array /// [Test] public void TestIsFixedSizeViaIList() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.IsTrue((testList as IList).IsFixedSize); } /// /// Checks whether the read only list will throw an exception if its Remove() method /// is called via the IList interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnRemoveViaIList() { int[] integers = new int[] { 1234, 6789 }; ReadOnlyList testList = new ReadOnlyList(integers); (testList as IList).Remove(6789); } /// /// Checks whether the read only list will throw an exception if its Remove() method /// is called via the IList interface /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnRemoveAtViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as IList).RemoveAt(0); } /// /// Checks whether the indexer method of the read only list will throw an exception /// if it is attempted to be used for replacing an item /// [Test] public void TestRetrieveByIndexerViaIList() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.AreEqual(12, (testList as IList)[0]); Assert.AreEqual(34, (testList as IList)[1]); Assert.AreEqual(67, (testList as IList)[2]); Assert.AreEqual(89, (testList as IList)[3]); } /// /// Checks whether the indexer method of the read only list will throw an exception /// if it is attempted to be used for replacing an item /// [Test, ExpectedException(typeof(NotSupportedException))] public void TestThrowOnReplaceByIndexerViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); (testList as IList)[0] = 12345; } /// /// Verifies that the CopyTo() of the read only list works if invoked via /// the ICollection interface /// [Test] public void TestCopyToArrayViaICollection() { int[] inputIntegers = new int[] { 12, 34, 56, 78 }; ReadOnlyList testList = new ReadOnlyList(inputIntegers); int[] outputIntegers = new int[testList.Count]; (testList as ICollection).CopyTo(outputIntegers, 0); CollectionAssert.AreEqual(inputIntegers, outputIntegers); } /// /// Verifies that the IsSynchronized property and the SyncRoot property are working /// [Test] public void TestSynchronization() { ReadOnlyList testList = new ReadOnlyList(new int[0]); if(!(testList as ICollection).IsSynchronized) { lock((testList as ICollection).SyncRoot) { int count = testList.Count; } } } } } // namespace Nuclex.Support.Collections #endif // UNITTEST