public static void CopyTo() { MyDictionary dictBase = CreateDictionary(100); // Basic var entries = new DictionaryEntry[dictBase.Count]; dictBase.CopyTo(entries, 0); Assert.Equal(dictBase.Count, entries.Length); for (int i = 0; i < entries.Length; i++) { DictionaryEntry entry = entries[i]; Assert.Equal(CreateKey(entries.Length - i - 1), entry.Key); Assert.Equal(CreateValue(entries.Length - i - 1), entry.Value); } // With index entries = new DictionaryEntry[dictBase.Count * 2]; dictBase.CopyTo(entries, dictBase.Count); Assert.Equal(dictBase.Count * 2, entries.Length); for (int i = dictBase.Count; i < entries.Length; i++) { DictionaryEntry entry = entries[i]; Assert.Equal(CreateKey(entries.Length - i - 1), entry.Key); Assert.Equal(CreateValue(entries.Length - i - 1), entry.Value); } }
public static void TestCopyTo_Invalid() { MyDictionary dictBase = CreateDictionary(100); Assert.Throws <ArgumentNullException>(() => dictBase.CopyTo(null, 0)); // Array is null Assert.Throws <ArgumentOutOfRangeException>(() => dictBase.CopyTo(new DictionaryEntry[100], -1)); // Index < 0 Assert.Throws <ArgumentException>(() => dictBase.CopyTo(new DictionaryEntry[100], 100)); // Index >= count Assert.Throws <ArgumentException>(() => dictBase.CopyTo(new DictionaryEntry[100], 50)); // Index + array.Count >= count }
public static void CopyTo_Invalid() { MyDictionary dictBase = CreateDictionary(100); AssertExtensions.Throws <ArgumentNullException>("array", () => dictBase.CopyTo(null, 0)); // Array is null AssertExtensions.Throws <ArgumentException>("array", null, () => dictBase.CopyTo(new object[100, 100], 0)); // Array is multidimensional AssertExtensions.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictBase.CopyTo(new DictionaryEntry[100], -1)); // Index < 0 AssertExtensions.Throws <ArgumentException>(null, () => dictBase.CopyTo(new DictionaryEntry[100], 100)); // Index >= count AssertExtensions.Throws <ArgumentException>(null, () => dictBase.CopyTo(new DictionaryEntry[100], 50)); // Index + array.Count >= count }
public void MyDictionaryCopyToTest() { KeyValuePair <int, string>[] check = { new KeyValuePair <int, string>(50, "Vasya"), new KeyValuePair <int, string>(38, "Stepan"), new KeyValuePair <int, string>(13, "Stich"), new KeyValuePair <int, string>(77, "Lilo"), new KeyValuePair <int, string>(45, "IceFrog"), new KeyValuePair <int, string>(20, "Alladin"), new KeyValuePair <int, string>(90, "Gaben"), }; KeyValuePair <int, string>[] pairs = { new KeyValuePair <int, string>(50, "Vasya"), new KeyValuePair <int, string>(38, "Stepan"), new KeyValuePair <int, string>(77, "Lilo"), new KeyValuePair <int, string>(13, "Stich"), new KeyValuePair <int, string>(20, "Alladin"), new KeyValuePair <int, string>(90, "Gaben"), new KeyValuePair <int, string>(45, "IceFrog") }; MyDictionary <int, string> dict = new MyDictionary <int, string>(pairs); KeyValuePair <int, string>[] pair = new KeyValuePair <int, string> [7]; dict.CopyTo(pair, 0); foreach (var x in pair) { Assert.AreEqual(check, x); } }
public void CopyTo_Test() { dict.Add(0, 00); dict.Add(1, 11); KeyValuePair <int, int>[] pair = new KeyValuePair <int, int> [dict.Count]; dict.CopyTo(pair, 0); Assert.AreEqual(new KeyValuePair <int, int>(0, 00), pair[0]); Assert.AreEqual(new KeyValuePair <int, int>(1, 11), pair[1]); }
public void CopyToTest() { KeyValuePair <int, int>[] array = new KeyValuePair <int, int> [10]; MyDictionary <int, int> dictionary = new MyDictionary <int, int>(); dictionary.Add(10, 25); dictionary.Add(15, 50); dictionary.Add(20, 99); dictionary.CopyTo(array, 5); Assert.AreEqual(array[5], new KeyValuePair <int, int>(10, 25)); Assert.AreEqual(array[6], new KeyValuePair <int, int>(15, 50)); Assert.AreEqual(array[7], new KeyValuePair <int, int>(20, 99)); }