public static void CopyTo() { var collection = new ReadOnlyList <int>(s_intArray); const int targetIndex = 3; int[] intArray = new int[s_intArray.Length + targetIndex]; Assert.Throws <ArgumentNullException>(() => collection.CopyTo(null, 0)); AssertExtensions.Throws <ArgumentException>(null, () => ((ICollection)collection).CopyTo(new int[s_intArray.Length, s_intArray.Length], 0)); Assert.Throws <ArgumentOutOfRangeException>(() => collection.CopyTo(intArray, -1)); AssertExtensions.Throws <ArgumentException>("destinationArray", "", () => collection.CopyTo(intArray, s_intArray.Length - 1)); collection.CopyTo(intArray, targetIndex); for (int i = targetIndex; i < intArray.Length; i++) { Assert.Equal(collection[i - targetIndex], intArray[i]); } object[] objectArray = new object[s_intArray.Length + targetIndex]; ((ICollection)collection).CopyTo(intArray, targetIndex); for (int i = targetIndex; i < intArray.Length; i++) { Assert.Equal(collection[i - targetIndex], intArray[i]); } }
public static void MembersForwardedToUnderlyingIList() { var expectedApiCalls = IListApi.Count | IListApi.IndexerGet | IListApi.Contains | IListApi.CopyTo | IListApi.GetEnumeratorGeneric | IListApi.IndexOf | IListApi.GetEnumerator; var list = new CallTrackingIList <int>(expectedApiCalls); var collection = new ReadOnlyList <int>(list); int count = collection.Count; bool readOnly = ((IList)collection).IsReadOnly; int x = collection[0]; collection.Contains(x); collection.CopyTo(s_intArray, 0); collection.GetEnumerator(); collection.IndexOf(x); ((IEnumerable)collection).GetEnumerator(); list.AssertAllMembersCalled(); }
public void Test() { List <string> strings = new List <string>(new string[] { "a", "b", "c" }); ReadOnlyList <String> read = new ReadOnlyList <string>(strings); strings.Add("d"); Assert.AreEqual(3, read.Count); Assert.IsTrue(read.Contains("a")); Assert.AreEqual(0, read.IndexOf("a")); Assert.IsTrue(read.Contains("b")); Assert.AreEqual(1, read.IndexOf("b")); Assert.IsTrue(read.Contains("c")); Assert.AreEqual(2, read.IndexOf("c")); Assert.IsFalse(read.Contains("d")); Assert.AreEqual(-1, read.IndexOf("d")); Assert.AreEqual("a,b,c", String.Join(",", read.ToArray())); Assert.AreEqual("a,b,c", String.Join(",", new List <String>(read).ToArray())); string[] arcopy = new string[3]; read.CopyTo(arcopy, 0); Assert.AreEqual("a,b,c", String.Join(",", arcopy)); System.Collections.IEnumerator en = ((System.Collections.IEnumerable)read).GetEnumerator(); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("a", en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("b", en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("c", en.Current); Assert.IsFalse(en.MoveNext()); }
public void Test() { List<string> strings = new List<string>(new string[] { "a", "b", "c" }); ReadOnlyList<String> read = new ReadOnlyList<string>(strings); strings.Add("d"); Assert.AreEqual(3, read.Count); Assert.IsTrue(read.Contains("a")); Assert.AreEqual(0, read.IndexOf("a")); Assert.IsTrue(read.Contains("b")); Assert.AreEqual(1, read.IndexOf("b")); Assert.IsTrue(read.Contains("c")); Assert.AreEqual(2, read.IndexOf("c")); Assert.IsFalse(read.Contains("d")); Assert.AreEqual(-1, read.IndexOf("d")); Assert.AreEqual("a,b,c", String.Join(",", read.ToArray())); Assert.AreEqual("a,b,c", String.Join(",", new List<String>(read).ToArray())); string[] arcopy = new string[3]; read.CopyTo(arcopy, 0); Assert.AreEqual("a,b,c", String.Join(",", arcopy)); System.Collections.IEnumerator en = ((System.Collections.IEnumerable)read).GetEnumerator(); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("a", en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("b", en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual("c", en.Current); Assert.IsFalse(en.MoveNext()); }
public void TestCopyToArray() { int[] inputIntegers = new int[] { 12, 34, 56, 78 }; ReadOnlyList<int> testList = new ReadOnlyList<int>(inputIntegers); int[] outputIntegers = new int[testList.Count]; testList.CopyTo(outputIntegers, 0); CollectionAssert.AreEqual(inputIntegers, outputIntegers); }
public void TestICollection() { ICollection read = new ReadOnlyList <int>((IEnumerable <int>) new int[] { 5, 10, 15 }); Assert.AreEqual(3, read.Count); Assert.IsFalse(read.IsSynchronized); Assert.IsTrue(Object.ReferenceEquals(read, read.SyncRoot)); long[] lary = new long[3]; read.CopyTo(lary, 0); Assert.AreEqual(5L, lary[0]); Assert.AreEqual(10L, lary[1]); Assert.AreEqual(15L, lary[2]); ICollection copy = (ICollection)((ICloneable)read).Clone(); Assert.AreEqual(3, copy.Count); }
public virtual void CopyTo <T>(ReadOnlyList <T> list, Array array, int index) { list.CopyTo(array, index); }
public void TestICollection() { ICollection read = new ReadOnlyList<int>((IEnumerable<int>)new int[] { 5, 10, 15 }); Assert.AreEqual(3, read.Count); Assert.IsFalse(read.IsSynchronized); Assert.IsTrue(Object.ReferenceEquals(read, read.SyncRoot)); long[] lary = new long[3]; read.CopyTo(lary, 0); Assert.AreEqual(5L, lary[0]); Assert.AreEqual(10L, lary[1]); Assert.AreEqual(15L, lary[2]); ICollection copy = (ICollection)((ICloneable)read).Clone(); Assert.AreEqual(3, copy.Count); }