#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 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] internal 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] public void TestThrowOnInsertViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); Assert.Throws( delegate() { (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] public void TestThrowOnRemoveViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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] public void TestThrowOnReplaceByIndexerViaGenericIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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] public void TestThrowOnAddViaGenericICollection() { ReadOnlyList testList = new ReadOnlyList(new int[0]); Assert.Throws( delegate() { (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] public void TestThrowOnClearViaGenericICollection() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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] public void TestThrowOnRemoveViaGenericICollection() { int[] integers = new int[] { 12, 34, 67, 89 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.Throws( delegate() { (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] public void TestThrowOnClearViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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] public void TestThrowOnAddViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); Assert.Throws( delegate() { (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] public void TestThrowOnInsertViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[0]); Assert.Throws( delegate() { (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] public void TestThrowOnRemoveViaIList() { int[] integers = new int[] { 1234, 6789 }; ReadOnlyList testList = new ReadOnlyList(integers); Assert.Throws( delegate() { (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] public void TestThrowOnRemoveAtViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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] public void TestThrowOnReplaceByIndexerViaIList() { ReadOnlyList testList = new ReadOnlyList(new int[1]); Assert.Throws( delegate() { (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) { Assert.AreEqual(0, testList.Count); } } } } } // namespace Nuclex.Support.Collections #endif // UNITTEST