public void CopyToTest() { var list = new MyCollection <string> { "A", "B", "C" }; var biggerArray = new string[] { "0", "1", "2", "a", "b", "c", "d" }; var biggerExpected = new string[] { "0", "1", "2", "A", "B", "C", "d" }; list.CopyTo(biggerArray, 3); Assert.Equal(biggerArray, biggerExpected); var equalArray = new string[] { "0", "1", "2", "a", "b", "c" }; var equalExpected = new string[] { "0", "1", "2", "A", "B", "C" }; list.CopyTo(equalArray, 3); Assert.Equal(equalArray, equalExpected); var smallerArray = new string[] { "0", "1", "2", "a", "b" }; try { list.CopyTo(smallerArray, 3); } catch (Exception e) { Assert.True(e is ArgumentException); } }
public void CopyToTest() { var list = new MyCollection <char> { 'A', 'B', 'C' }; var biggerArray = new char[] { '0', '1', '2', 'a', 'b', 'c', 'd' }; var biggerExpected = new char[] { '0', '1', '2', 'A', 'B', 'C', 'd' }; list.CopyTo(biggerArray, 3); Assert.Equal(biggerArray, biggerExpected); var equalArray = new char[] { '0', '1', '2', 'a', 'b', 'c' }; var equalExpected = new char[] { '0', '1', '2', 'A', 'B', 'C' }; list.CopyTo(equalArray, 3); Assert.Equal(equalArray, equalExpected); var smallerArray = new char[] { '0', '1', '2', 'a', 'b' }; try { list.CopyTo(smallerArray, 3); } catch (Exception e) { Assert.True(e is ArgumentException); } }
public static void TestCopyTo_Invalid() { MyCollection collBase = CreateCollection(100); var fooArr = new Foo[100]; Assert.Throws <ArgumentOutOfRangeException>(() => collBase.CopyTo(fooArr, -1)); // Index < 0 Assert.Throws <ArgumentException>(() => collBase.CopyTo(fooArr, 50)); // Index + fooArray.Length > collBase.Count }
public void CopyTo_CorrectArguments_DestinationArrayShouldContainsFullCollection(int index) { // Arrange var random = new Random(); var arrayLength = 30; var collection = new MyCollection <string>(); var destinationArray = new string[100]; for (var i = 0; i < arrayLength; i++) { collection.Add(random.Next(10000).ToString()); } for (var i = 0; i < index; i++) { destinationArray[i] = random.Next(10000).ToString(); } // Act collection.CopyTo(destinationArray, index); // Assert for (int i = 0; i < arrayLength; i++) { Assert.Equal(collection[i], destinationArray[i + index]); } }
public void ClassImplementingICollectionCastToICollectionCopyToWorks() { ICollection <string> l = new MyCollection(new[] { "x", "y" }); var a1 = new string[2]; l.CopyTo(a1, 0); Assert.AreEqual("x", a1[0], "1.Element 0"); Assert.AreEqual("y", a1[1], "1.Element 1"); var a2 = new string[4]; l.CopyTo(a2, 1); Assert.AreEqual(null, a2[0], "2.Element 0"); Assert.AreEqual("x", a2[1], "2.Element 1"); Assert.AreEqual("y", a2[2], "2.Element 2"); Assert.AreEqual(null, a2[3], "2.Element 3"); Assert.Throws <ArgumentNullException>(() => { l.CopyTo(null, 0); }, "null"); var a3 = new string[1]; Assert.Throws <ArgumentException>(() => { l.CopyTo(a3, 0); }, "Short array"); var a4 = new string[2]; Assert.Throws <ArgumentException>(() => { l.CopyTo(a4, 1); }, "Start index 1"); Assert.Throws <ArgumentOutOfRangeException>(() => { l.CopyTo(a4, -1); }, "Negative start index"); Assert.Throws <ArgumentException>(() => { l.CopyTo(a4, 3); }, "Start index 3"); }
public void CopyToTest() { // Notation litérale "Collection initializer" // Autorisée car : // - on implémente IEnumerable via IEnumerator // - on a une méthode Add via IList var list = new MyCollection <string> { "A", "B", "C" }; var biggerArray = new string[] { "0", "1", "2", "a", "b", "c", "d" }; var biggerExpected = new string[] { "0", "1", "2", "A", "B", "C", "d" }; list.CopyTo(biggerArray, 3); Assert.Equal(biggerArray, biggerExpected); var equalArray = new string[] { "0", "1", "2", "a", "b", "c" }; var equalExpected = new string[] { "0", "1", "2", "A", "B", "C" }; list.CopyTo(equalArray, 3); Assert.Equal(equalArray, equalExpected); var smallerArray = new string[] { "0", "1", "2", "a", "b" }; try { list.CopyTo(smallerArray, 3); // Si on atteint cette ligne, // c'est que la ligne du dessus // n'a pas planté alors qu'elle // devait Assert.True(false); } catch (Exception e) { Assert.True(e is ArgumentException); } }
public static void TestCopyTo() { MyCollection collBase = CreateCollection(100); // Basic var fooArr = new Foo[100]; collBase.CopyTo(fooArr, 0); Assert.Equal(collBase.Count, fooArr.Length); for (int i = 0; i < fooArr.Length; i++) { Assert.Equal(collBase[i], fooArr.GetValue(i)); } // With index fooArr = new Foo[collBase.Count * 2]; collBase.CopyTo(fooArr, collBase.Count); for (int i = collBase.Count; i < fooArr.Length; i++) { Assert.Equal(collBase[i - collBase.Count], fooArr.GetValue(i)); } }
public void CopyTo_CollectionBiggerThanSpaceInArray_ThrowsArgumentException(int index) { // Arrange var random = new Random(); var arrayLength = 30; var collection = new MyCollection <string>(); var destinationArray = new string[100]; for (var i = 0; i < arrayLength; i++) { collection.Add(random.Next(10000).ToString()); } // Act and Assert Assert.Throws <ArgumentException>(() => collection.CopyTo(destinationArray, index)); }
public void CopyTo_NullArgument_ThrowsArgumentNullException() { // Arrange var random = new Random(); var count = 30; var collection = new MyCollection <int>(); for (var i = 0; i < count; i++) { collection.Add(random.Next(50, 10000)); } int[] array = null; // Act and Assert Assert.Throws <ArgumentNullException>(() => collection.CopyTo(array, 0)); }
public static void CopyTo_Invalid() { MyCollection collBase = CreateCollection(100); var fooArr = new Foo[100]; // Index < 0 AssertExtensions.Throws <ArgumentOutOfRangeException>("destinationIndex", "dstIndex", () => collBase.CopyTo(fooArr, -1)); // Index + fooArray.Length > collBase.Count AssertExtensions.Throws <ArgumentException>("destinationArray", string.Empty, () => collBase.CopyTo(fooArr, 50)); }