public static void CopyToTest() { string[] anArray = new string[] { "one", "two", "three", "four" }; ObservableCollection<string> collection = new ObservableCollection<string>((IEnumerable<string>)anArray); string[] aCopy = new string[collection.Count]; collection.CopyTo(aCopy, 0); for (int i = 0; i < anArray.Length; ++i) Assert.Equal(anArray[i], aCopy[i]); // copy observable collection starting in middle, where array is larger than source. aCopy = new string[collection.Count + 2]; int offsetIndex = 1; collection.CopyTo(aCopy, offsetIndex); for (int i = 0; i < aCopy.Length; i++) { string value = aCopy[i]; if (i == 0) Assert.True(null == value, "Should not have a value since we did not start copying there."); else if (i == (aCopy.Length - 1)) Assert.True(null == value, "Should not have a value since the collection is shorter than the copy array.."); else { int indexInCollection = i - offsetIndex; Assert.Equal(collection[indexInCollection], aCopy[i]); } } }
public static void CopyToTest_Negative() { string[] anArray = new string[] { "one", "two", "three", "four" }; ObservableCollection<string> collection = new ObservableCollection<string>(anArray); int[] iArrInvalidValues = new Int32[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, Int32.MinValue }; foreach (var index in iArrInvalidValues) { string[] aCopy = new string[collection.Count]; Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(aCopy, index)); } int[] iArrLargeValues = new Int32[] { collection.Count, Int32.MaxValue, Int32.MaxValue / 2, Int32.MaxValue / 10 }; foreach (var index in iArrLargeValues) { string[] aCopy = new string[collection.Count]; Assert.Throws<ArgumentException>(() => collection.CopyTo(aCopy, index)); } Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 1)); string[] copy = new string[collection.Count - 1]; Assert.Throws<ArgumentException>(() => collection.CopyTo(copy, 0)); copy = new string[0]; Assert.Throws<ArgumentException>(() => collection.CopyTo(copy, 0)); }
public void Test_ObservableCollection_CopyTo() { var list = new ObservableCollection<int>() { 6, 5, 8 }; var array = new int[5]; list.CopyTo(array, 1); Assert.Equal(0, array[0]); Assert.Equal(6, array[1]); Assert.Equal(5, array[2]); Assert.Equal(8, array[3]); Assert.Equal(0, array[4]); }