void CheckCollection(MyNameObjectCollection noc, ArrayList keys, ArrayList values) { // Check counts if ((noc.Count != keys.Count) || (noc.Count != values.Count)) { Assert.False(true, _strErr + "Wrong number of elements, or mismatched keys and values."); Assert.False(true, string.Format(_strErr + "noc.Count={0}, keys.Count={1}, values.Count={2}", noc.Count, keys.Count, values.Count)); } // Check keys/values for (int i = 0; i < noc.Count; i++) { // keys if (noc.GetKey(i) != (String)keys[i]) { Assert.False(true, string.Format(_strErr + "key #{0} not as expected. noc.GetKey({0}) = {1}, should be {2}", i, noc.GetKey(i), keys[i])); } // values if (values[i] == null) { if (noc[i] != null) { Assert.False(true, string.Format(_strErr + "value #{0} not as expected. noc[{0}] = {1}, should be {2}", i, noc[i], values[i])); } } else { if ((noc[i] == null) || ((Foo)noc[i] != (Foo)values[i])) { Assert.False(true, string.Format(_strErr + "value #{0} not as expected. noc[{0}] = {1}, should be {2}", i, noc[i], values[i])); } } } }
public void CopyTo(int count, int index) { MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count); ICollection collection = nameObjectCollection; string[] copyArray = new string[index + collection.Count + index]; collection.CopyTo(copyArray, index); for (int i = 0; i < index; i++) { Assert.Null(copyArray[i]); } for (int i = 0; i < count; i++) { Assert.Equal(nameObjectCollection.GetKey(i), copyArray[i + index]); } for (int i = index + collection.Count; i < copyArray.Length; i++) { Assert.Null(copyArray[i]); } // Clearing the nameObjectCollection should not affect the keys copy int previousCount = copyArray.Length; nameObjectCollection.Clear(); Assert.Equal(previousCount, copyArray.Length); }
private static void Keys_CopyTo_Helper(MyNameObjectCollection nameObjectCollection, int index) { ICollection keys = nameObjectCollection.Keys; string[] keysArray = new string[index + keys.Count + index]; keys.CopyTo(keysArray, index); for (int i = 0; i < index; i++) { Assert.Null(keysArray[i]); } for (int i = 0; i < keys.Count; i++) { Assert.Equal(nameObjectCollection.GetKey(i), keysArray[i + index]); } for (int i = index + keys.Count; i < keysArray.Length; i++) { Assert.Null(keysArray[i]); } // Clearing the nameObjectCollection should not affect the keys copy int previousCount = keysArray.Length; nameObjectCollection.Clear(); Assert.Equal(previousCount, keysArray.Length); }
public void RemoveAt() { MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(10); nameObjectCollection.Add(null, new Foo("null-value")); nameObjectCollection.Add(null, null); nameObjectCollection.Add("repeated-name", null); nameObjectCollection.Add("repeated-name", new Foo("repeated-name-value")); // Remove from the middle nameObjectCollection.RemoveAt(3); Assert.Equal(13, nameObjectCollection.Count); Assert.Null(nameObjectCollection["Name_3"]); Assert.Equal("Name_4", nameObjectCollection.GetKey(3)); // Remove the first nameObjectCollection.RemoveAt(0); Assert.Equal(12, nameObjectCollection.Count); Assert.Null(nameObjectCollection["Name_0"]); Assert.Equal("Name_1", nameObjectCollection.GetKey(0)); // Remove the last nameObjectCollection.RemoveAt(nameObjectCollection.Count - 1); Assert.Equal(11, nameObjectCollection.Count); Assert.Equal("repeated-name", nameObjectCollection.GetKey(nameObjectCollection.Count - 1)); Assert.Null(nameObjectCollection["repeated-name"]); // Remove all int count = nameObjectCollection.Count; for (int i = 0; i < count; i++) { int newCount = 11 - i - 1; string key = nameObjectCollection.GetKey(0); string nextKey = null; if (newCount != 0) { nextKey = nameObjectCollection.GetKey(1); } nameObjectCollection.RemoveAt(0); Assert.Equal(newCount, nameObjectCollection.Count); Assert.Null(nameObjectCollection[key]); if (newCount != 0) { Assert.Equal(nextKey, nameObjectCollection.GetKey(0)); } } }
public void GetEnumerator(int count) { MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count); Assert.NotSame(nameObjectCollection.GetEnumerator(), nameObjectCollection.GetEnumerator()); IEnumerator enumerator = nameObjectCollection.GetEnumerator(); for (int i = 0; i < 2; i++) { int counter = 0; while (enumerator.MoveNext()) { Assert.Equal(nameObjectCollection.GetKey(counter), enumerator.Current); counter++; } Assert.Equal(count, nameObjectCollection.Count); enumerator.Reset(); } }
public void IsReadOnly_Set() { MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(10); Assert.False(nameObjectCollection.IsReadOnly); nameObjectCollection.IsReadOnly = true; Assert.True(nameObjectCollection.IsReadOnly); Assert.Throws <NotSupportedException>(() => nameObjectCollection.Add("name", null)); Assert.Throws <NotSupportedException>(() => nameObjectCollection["name"] = null); Assert.Throws <NotSupportedException>(() => nameObjectCollection[0] = null); Assert.Throws <NotSupportedException>(() => nameObjectCollection.Remove("name")); Assert.Throws <NotSupportedException>(() => nameObjectCollection.RemoveAt(0)); Assert.Throws <NotSupportedException>(() => nameObjectCollection.Clear()); Assert.Equal("Name_0", nameObjectCollection.GetKey(0)); Assert.Equal(new Foo("Value_0"), nameObjectCollection["Name_0"]); Assert.Equal(new Foo("Value_0"), nameObjectCollection[0]); }
void CheckCollection(MyNameObjectCollection noc, ArrayList keys, ArrayList values) { // Check counts if ((noc.Count != keys.Count) || (noc.Count != values.Count)) { Assert.False(true, _strErr + "Wrong number of elements, or mismatched keys and values."); Assert.False(true, string.Format(_strErr + "noc.Count={0}, keys.Count={1}, values.Count={2}", noc.Count, keys.Count, values.Count)); } // Check keys/values for (int i = 0; i < noc.Count; i++) { // keys if (noc.GetKey(i) != (String)keys[i]) { Assert.False(true, string.Format(_strErr + "key #{0} not as expected. noc.GetKey({0}) = {1}, should be {2}", i, noc.GetKey(i), keys[i])); } // values if (values[i] == null) { if (noc[i] != null) { Assert.False(true, string.Format(_strErr + "value #{0} not as expected. noc[{0}] = {1}, should be {2}", i, noc[i], values[i])); } } else { if ((noc[i] == null) || ((Foo)noc[i] != (Foo)values[i])) { Assert.False(true, string.Format(_strErr + "value #{0} not as expected. noc[{0}] = {1}, should be {2}", i, noc[i], values[i])); } } } }
public void Test01() { MyNameObjectCollection noc = new MyNameObjectCollection(); Array array = null; ArrayList ArrayValues = new ArrayList(); Random rand = new Random(-55); int n = 0; String key1 = "key1"; String key2 = "key2"; Foo val1 = new Foo(); Foo val2 = new Foo(); // [] Copy a collection to middle of target array. // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + rand.Next(20, 100); array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection int offset = 10; ((ICollection)noc).CopyTo(array, offset); for (int i = 0; i < noc.Count; i++) { ArrayValues[i + offset] = noc.GetKey(i); } // Check array CheckArray(array, ArrayValues); // [] Verify copy is distinct from original collection. // Clear initial collection noc.Clear(); // Verify copy is not cleared CheckArray(array, ArrayValues); // [] Fill whole array (index=0) // Set up initial collection noc = new MyNameObjectCollection(); n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count; array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection ((ICollection)noc).CopyTo(array, 0); for (int i = 0; i < noc.Count; i++) { ArrayValues[i] = noc.GetKey(i); } // Check array CheckArray(array, ArrayValues); // [] index = max index in array // Set up initial collection noc.Clear(); noc.Add("key1", new Foo()); // Set up initial array n = noc.Count + rand.Next(1, 100); array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection offset = ArrayValues.Count - 1; ((ICollection)noc).CopyTo(array, offset); ArrayValues[offset] = noc.GetKey(0); // Retrieve values CheckArray(array, ArrayValues); // [] Target array is zero-length. array = Array.CreateInstance(typeof(String), 0); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, 0); }); // [] Call on an empty collection (to zero-length array). noc = new MyNameObjectCollection(); array = Array.CreateInstance(typeof(String), 0); // Copy the collection ((ICollection)noc).CopyTo(array, 0); // [] Call on an empty collection. noc = new MyNameObjectCollection(); array = Array.CreateInstance(typeof(String), 16); // Copy the collection ((ICollection)noc).CopyTo(array, 0); // Retrieve elements foreach (String v in array) { if (v != null) { Assert.False(true, _strErr + "Value is incorrect. array should be null"); } } // [] Call with array = null noc = new MyNameObjectCollection(); Assert.Throws <ArgumentNullException>(() => { ((ICollection)noc).CopyTo(null, 0); }); // [] Target array is multidimensional. array = new string[20, 2]; noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, 16); }); // [] Target array is of incompatible type. array = Array.CreateInstance(typeof(Foo), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <InvalidCastException>(() => { ((ICollection)noc).CopyTo(array, 1); }); // [] index = array length n = rand.Next(10, 100); array = Array.CreateInstance(typeof(String), n); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, n); }); // [] index > array length n = rand.Next(10, 100); array = Array.CreateInstance(typeof(String), n); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, n + 1); }); // [] index = Int32.MaxValue array = Array.CreateInstance(typeof(String), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, Int32.MaxValue); }); // [] index < 0 array = Array.CreateInstance(typeof(String), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws <ArgumentOutOfRangeException>(() => { ((ICollection)noc).CopyTo(array, -1); }); // [] index is valid but collection doesn't fit in available space noc = new MyNameObjectCollection(); // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + 20; array = Array.CreateInstance(typeof(Foo), n); Assert.Throws <ArgumentException>(() => { ((ICollection)noc).CopyTo(array, 30); }); // array is only 20 bigger than collection // [] index is negative noc = new MyNameObjectCollection(); // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + 20; array = Array.CreateInstance(typeof(Foo), n); Assert.Throws <ArgumentOutOfRangeException>(() => { ((ICollection)noc).CopyTo(array, -1); }); }
public void Test01() { MyNameObjectCollection noc = new MyNameObjectCollection(); Array array = null; ArrayList ArrayValues = new ArrayList(); Random rand = new Random(-55); int n = 0; String key1 = "key1"; String key2 = "key2"; Foo val1 = new Foo(); Foo val2 = new Foo(); // [] Copy a collection to middle of target array. // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + rand.Next(20, 100); array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection int offset = 10; ((ICollection)noc.Keys).CopyTo(array, offset); for (int i = 0; i < noc.Count; i++) { ArrayValues[i + offset] = noc.GetKey(i); } // Check array CheckArray(array, ArrayValues); // [] Verify copy is distinct from original collection. // Clear initial collection noc.Clear(); // Verify copy is not cleared CheckArray(array, ArrayValues); // [] Fill whole array (index=0) // Set up initial collection noc = new MyNameObjectCollection(); n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count; array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection ((ICollection)noc.Keys).CopyTo(array, 0); for (int i = 0; i < noc.Count; i++) { ArrayValues[i] = noc.GetKey(i); } // Check array CheckArray(array, ArrayValues); // [] index = max index in array // Set up initial collection noc.Clear(); noc.Add("key1", new Foo()); // Set up initial array n = noc.Count + rand.Next(1, 100); array = Array.CreateInstance(typeof(String), n); ArrayValues.Clear(); for (int i = 0; i < n; i++) { String v = "arrayvalue_" + i.ToString(); array.SetValue(v, i); ArrayValues.Add(v); } // Copy the collection offset = ArrayValues.Count - 1; ((ICollection)noc.Keys).CopyTo(array, offset); ArrayValues[offset] = noc.GetKey(0); // Retrieve values CheckArray(array, ArrayValues); // [] Target array is zero-length. array = Array.CreateInstance(typeof(String), 0); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 0); }); // [] Call on an empty collection (to zero-length array). noc = new MyNameObjectCollection(); array = Array.CreateInstance(typeof(String), 0); // Copy the collection ((ICollection)noc.Keys).CopyTo(array, 0); // [] Call on an empty collection. noc = new MyNameObjectCollection(); array = Array.CreateInstance(typeof(String), 16); // Copy the collection ((ICollection)noc.Keys).CopyTo(array, 0); // Retrieve elements foreach (String v in array) { if (v != null) { Assert.False(true, _strErr + "Value is incorrect. array should be null"); } } // [] Call with array = null noc = new MyNameObjectCollection(); Assert.Throws<ArgumentNullException>(() => { ((ICollection)noc.Keys).CopyTo(null, 0); }); // [] Target array is multidimensional. array = new string[20, 2]; noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 16); }); // [] Target array is of incompatible type. array = Array.CreateInstance(typeof(Foo), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<InvalidCastException>(() => { ((ICollection)noc.Keys).CopyTo(array, 1); }); // [] index = array length n = rand.Next(10, 100); array = Array.CreateInstance(typeof(String), n); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, n); }); // [] index > array length n = rand.Next(10, 100); array = Array.CreateInstance(typeof(String), n); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, n + 1); }); // [] index = Int32.MaxValue array = Array.CreateInstance(typeof(String), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, Int32.MaxValue); }); // [] index < 0 array = Array.CreateInstance(typeof(String), 10); noc = new MyNameObjectCollection(); noc.Add(key1, val1); noc.Add(key2, val2); Assert.Throws<ArgumentOutOfRangeException>(() => { ((ICollection)noc.Keys).CopyTo(array, -1); }); // [] index is valid but collection doesn't fit in available space noc = new MyNameObjectCollection(); // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + 20; array = Array.CreateInstance(typeof(Foo), n); Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 30); }); // array is only 20 bigger than collection // [] index is negative noc = new MyNameObjectCollection(); // Set up initial collection n = rand.Next(10, 1000); for (int i = 0; i < n; i++) { noc.Add("key_" + i.ToString(), new Foo()); } // Set up initial array n = noc.Count + 20; array = Array.CreateInstance(typeof(Foo), n); Assert.Throws<ArgumentOutOfRangeException>(() => { ((ICollection)noc.Keys).CopyTo(array, -1); }); // [] All keys are null noc = new MyNameObjectCollection(); noc.Add(null, new Foo()); noc.Add(null, new Foo()); noc.Add(null, null); noc.Add(null, new Foo()); if (noc.Count != 4) { Assert.False(true, _strErr + "Elements were not added correctly"); } array = Array.CreateInstance(typeof(String), 16); // Copy the collection ((ICollection)noc.Keys).CopyTo(array, 0); // Retrieve elements foreach (String v in array) { if (v != null) { Assert.False(true, _strErr + "Value is incorrect. array should be null"); } } }
public void Test01() { MyNameObjectCollection noc = new MyNameObjectCollection(); String key = "key1"; Foo value = new Foo(); // [] IsReadOnly is initially false if (noc.IsReadOnly != false) { Assert.False(true, _strErr + "IsReadOnly should be false initially"); } // [] Set IsReadOnly to true noc.IsReadOnly = true; if (noc.IsReadOnly != true) { Assert.False(true, _strErr + "IsReadOnly should be true"); } // Now we are going to verify that methods that change the collection throw an exception // if IsReadOnly is true. // [] Set IsReadOnly to false and add an element noc.IsReadOnly = false; if (noc.IsReadOnly != false) { Assert.False(true, _strErr + "IsReadOnly should be false"); } noc.Add(key, value); noc.IsReadOnly = true; // [] Add fails Assert.Throws <NotSupportedException>(() => { noc.Add("new key", new Foo()); }); // [] Remove fails Assert.Throws <NotSupportedException>(() => { noc.Remove(key); }); // [] RemoveAt fails Assert.Throws <NotSupportedException>(() => { noc.RemoveAt(0); }); // [] Clear fails Assert.Throws <NotSupportedException>(() => { noc.Clear(); }); // [] Get by key succeeds if (noc[key] != value) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", value, noc[key])); } // [] Set by key fails Assert.Throws <NotSupportedException>(() => { noc[key] = new Foo(); }); // [] Get by index succeeds if (noc[0] != value) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", value, noc[0])); } // [] Set by index fails Assert.Throws <NotSupportedException>(() => { noc[0] = new Foo(); }); // [] GetKey succeeds if (noc.GetKey(0) != key) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", key, noc.GetKey(0))); } }
public void Keys_CopyTo(MyNameObjectCollection nameObjectCollection, int index) { ICollection keys = nameObjectCollection.Keys; string[] keysArray = new string[index + keys.Count + index]; keys.CopyTo(keysArray, index); for (int i = 0; i < index; i++) { Assert.Null(keysArray[i]); } for (int i = 0; i < keys.Count; i++) { Assert.Equal(nameObjectCollection.GetKey(i), keysArray[i + index]); } for (int i = index + keys.Count; i < keysArray.Length; i++) { Assert.Null(keysArray[i]); } // Clearing the nameObjectCollection should not affect the keys copy int previousCount = keysArray.Length; nameObjectCollection.Clear(); Assert.Equal(previousCount, keysArray.Length); }
public void Test01() { MyNameObjectCollection noc = new MyNameObjectCollection(); String key = "key1"; Foo value = new Foo(); // [] IsReadOnly is initially false if (noc.IsReadOnly != false) { Assert.False(true, _strErr + "IsReadOnly should be false initially"); } // [] Set IsReadOnly to true noc.IsReadOnly = true; if (noc.IsReadOnly != true) { Assert.False(true, _strErr + "IsReadOnly should be true"); } // Now we are going to verify that methods that change the collection throw an exception // if IsReadOnly is true. // [] Set IsReadOnly to false and add an element noc.IsReadOnly = false; if (noc.IsReadOnly != false) { Assert.False(true, _strErr + "IsReadOnly should be false"); } noc.Add(key, value); noc.IsReadOnly = true; // [] Add fails Assert.Throws<NotSupportedException>(() => { noc.Add("new key", new Foo()); }); // [] Remove fails Assert.Throws<NotSupportedException>(() => { noc.Remove(key); }); // [] RemoveAt fails Assert.Throws<NotSupportedException>(() => { noc.RemoveAt(0); }); // [] Clear fails Assert.Throws<NotSupportedException>(() => { noc.Clear(); }); // [] Get by key succeeds if (noc[key] != value) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", value, noc[key])); } // [] Set by key fails Assert.Throws<NotSupportedException>(() => { noc[key] = new Foo(); }); // [] Get by index succeeds if (noc[0] != value) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", value, noc[0])); } // [] Set by index fails Assert.Throws<NotSupportedException>(() => { noc[0] = new Foo(); }); // [] GetKey succeeds if (noc.GetKey(0) != key) { Assert.False(true, string.Format(_strErr + "Wrong value returned. Expected {0}, got {1}", key, noc.GetKey(0))); } }