Add() public method

public Add ( string name, Foo value ) : void
name string
value Foo
return void
Esempio n. 1
0
        public void Keys_CopyTo_NullKeys()
        {
            MyNameObjectCollection nameObjectCollection = new MyNameObjectCollection();

            nameObjectCollection.Add(null, new Foo("1"));
            nameObjectCollection.Add(null, new Foo("2"));
            nameObjectCollection.Add(null, null);
            nameObjectCollection.Add(null, new Foo("3"));

            Keys_CopyTo_Helper(nameObjectCollection, 0);
        }
Esempio n. 2
0
        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));
                }
            }
        }
Esempio n. 3
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            NameObjectCollectionBase.KeysCollection keys;

            // Set up initial collection
            for (int i = 0; i < 20; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Get the KeysCollection
            _strErr = "Err_001, ";
            keys = noc.Keys;

            // Check Count
            if (keys.Count != noc.Count)
            {
                Assert.False(true, string.Format(_strErr + "keys.Count is wrong.  expected {0}, got {1}", noc.Count, keys.Count));
            }

            // Compare - test Get, Item
            for (int i = 0; i < noc.Count; i++)
            {
                if (keys.Get(i) != "key_" + i.ToString())
                {
                    Assert.False(true, string.Format(_strErr + "keys.Get({0}) is wrong.  expected {1}, got {2}", i, "key_" + i.ToString(), keys.Get(i)));
                }
                if (keys[i] != "key_" + i.ToString())
                {
                    Assert.False(true, string.Format(_strErr + "keys[{0}] is wrong.  expected {1}, got {2}", i, "key_" + i.ToString(), keys[i]));
                }
            }

            // Get enumerator - it's the same enumerator as the original collection, so don't
            // need to test it again here.
            IEnumerator en = keys.GetEnumerator();

            // Get SyncRoot - just a cursory test
            if (((ICollection)keys).SyncRoot != ((ICollection)noc).SyncRoot)
            {
                Assert.False(true, _strErr + "keys.SyncRoot was not the same as noc.SyncRoot");
            }

            // Get IsSynchronized
            if (((ICollection)keys).IsSynchronized)
            {
                Assert.False(true, _strErr + "keys.SyncRoot was not the same as noc.SyncRoot");
            }

            // Check empty collection
            noc = new MyNameObjectCollection();
            keys = noc.Keys;

            // Check Count
            if (keys.Count != 0)
            {
                Assert.False(true, string.Format(_strErr + "keys.Count is wrong.  expected {0}, got {1}", 0, keys.Count));
            }
        }
        public static void GetAllValues_Invalid()
        {
            MyNameObjectCollection nameObjectCollection = new MyNameObjectCollection();
            Assert.Throws<ArgumentNullException>("type", () => nameObjectCollection.GetAllValues(null));

            nameObjectCollection.Add("name", new Foo("value"));
            Assert.Throws<ArrayTypeMismatchException>(() => nameObjectCollection.GetAllValues(typeof(string)));
        }
        public static void GetAllValues_Invalid()
        {
            MyNameObjectCollection nameObjectCollection = new MyNameObjectCollection();

            AssertExtensions.Throws <ArgumentNullException>("type", () => nameObjectCollection.GetAllValues(null));

            nameObjectCollection.Add("name", new Foo("value"));
            Assert.Throws <ArrayTypeMismatchException>(() => nameObjectCollection.GetAllValues(typeof(string)));
        }
Esempio n. 6
0
        public void Test01()
        {
            // [] Get SyncRoot, same instance returns same object
            MyNameObjectCollection noc1 = new MyNameObjectCollection();
            MyNameObjectCollection noc2;

            noc1.Add("key1", new Foo());
            noc1.Add("key2", new Foo());

            if (((ICollection)noc1).SyncRoot == null)
            {
                Assert.False(true, _strErr + "SyncRoot object is null");
            }

            noc2 = noc1;
            if (((ICollection)noc1).SyncRoot != ((ICollection)noc2).SyncRoot)
            {
                Assert.False(true, _strErr + "Different SyncRoot objects for same collection");
            }

            // [] Different instances return different SyncRoot objects
            noc1 = new MyNameObjectCollection();
            noc2 = new MyNameObjectCollection();

            if (((ICollection)noc1).SyncRoot == ((ICollection)noc2).SyncRoot)
            {
                Assert.False(true, _strErr + "SyncRoot for 2 different collections is the same");
            }

            // [] IsSynchronized returns false
            noc1 = new MyNameObjectCollection();

            if (((ICollection)noc1).IsSynchronized != false)
            {
                Assert.False(true, _strErr + "IsSynchronized should be false!");
            }

            // [] SyncRoot is of type Object
            noc1 = new MyNameObjectCollection();
            if (((ICollection)noc1).SyncRoot.GetType() != typeof(Object))
            {
                Assert.False(true, _strErr + "SyncRoot is not of type Object");
            }
        }
Esempio n. 7
0
        public void Test01()
        {
            // [] Get SyncRoot, same instance returns same object
            MyNameObjectCollection noc1 = new MyNameObjectCollection();
            MyNameObjectCollection noc2;
            noc1.Add("key1", new Foo());
            noc1.Add("key2", new Foo());

            if (((ICollection)noc1).SyncRoot == null)
            {
                Assert.False(true, _strErr + "SyncRoot object is null");
            }

            noc2 = noc1;
            if (((ICollection)noc1).SyncRoot != ((ICollection)noc2).SyncRoot)
            {
                Assert.False(true, _strErr + "Different SyncRoot objects for same collection");
            }

            // [] Different instances return different SyncRoot objects
            noc1 = new MyNameObjectCollection();
            noc2 = new MyNameObjectCollection();

            if (((ICollection)noc1).SyncRoot == ((ICollection)noc2).SyncRoot)
            {
                Assert.False(true, _strErr + "SyncRoot for 2 different collections is the same");
            }

            // [] IsSynchronized returns false
            noc1 = new MyNameObjectCollection();

            if (((ICollection)noc1).IsSynchronized != false)
            {
                Assert.False(true, _strErr + "IsSynchronized should be false!");
            }

            // [] SyncRoot is of type Object
            noc1 = new MyNameObjectCollection();
            if (((ICollection)noc1).SyncRoot.GetType() != typeof(Object))
            {
                Assert.False(true, _strErr + "SyncRoot is not of type Object");
            }
        }
        public void Constructor_Provider_Comparer()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            MyNameObjectCollection coll = new MyNameObjectCollection(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
#pragma warning restore CS0618 // Type or member is obsolete
            coll.Add(null, null);
            Assert.Equal(1, coll.Count);
            coll.Remove(null);
            Assert.Equal(0, coll.Count);
        }
        public void Constructor_Provider_Comparer()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            MyNameObjectCollection coll = new MyNameObjectCollection(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
#pragma warning restore CS0618 // Type or member is obsolete
            coll.Add(null, null);
            Assert.Equal(1, coll.Count);
            coll.Remove(null);
            Assert.Equal(0, coll.Count);
        }
Esempio n. 10
0
        public void Set_ObjectAtIndex_ModifiesCollection()
        {
            var noc = new MyNameObjectCollection();
            for (int i = 0; i < 10; i++)
            {
                var foo1 = new Foo();
                noc.Add("Name_" + i, foo1);

                var foo2 = new Foo();
                noc[i] = foo2;
                Assert.Equal(foo2, noc[i]);
            }
        }
        public void Constructor_Int_Provider_Comparer()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            MyNameObjectCollection coll = new MyNameObjectCollection(5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
#pragma warning restore CS0618 // Type or member is obsolete
            coll.Add("a", new Foo("1"));
            int         i = 0;
            IEnumerator e = coll.GetEnumerator();
            while (e.MoveNext())
            {
                i++;
            }
            Assert.Equal(1, i);
        }
        public void Constructor_Int_Provider_Comparer()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            MyNameObjectCollection coll = new MyNameObjectCollection(5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
#pragma warning restore CS0618 // Type or member is obsolete
            coll.Add("a", new Foo("1"));
            int i = 0;
            IEnumerator e = coll.GetEnumerator();
            while (e.MoveNext())
            {
                i++;
            }
            Assert.Equal(1, i);
        }
Esempio n. 13
0
        public void Set_ObjectAtIndex_ModifiesCollection()
        {
            var noc = new MyNameObjectCollection();

            for (int i = 0; i < 10; i++)
            {
                var foo1 = new Foo("Value_1");
                noc.Add("Name_" + i, foo1);

                var foo2 = new Foo("Value_2");
                noc[i] = foo2;
                Assert.Same(foo2, noc[i]);
            }
        }
Esempio n. 14
0
        public void Keys_GetEnumerator_Invalid(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            NameObjectCollectionBase.KeysCollection keys = nameObjectCollection.Keys;
            IEnumerator enumerator = keys.GetEnumerator();

            // Has not started enumerating
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Has finished enumerating
            while (enumerator.MoveNext())
            {
                ;
            }
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Has reset enumerating
            enumerator.Reset();
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Modify collection
            enumerator.MoveNext();
            nameObjectCollection.Add("new-name", new Foo("new-value"));
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
            if (count > 0)
            {
                Assert.NotNull(enumerator.Current);
            }

            // Modified read only collection still throws
            nameObjectCollection.IsReadOnly = true;
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());

            // Clear collection
            nameObjectCollection.IsReadOnly = false;
            enumerator = keys.GetEnumerator();
            enumerator.MoveNext();
            nameObjectCollection.Clear();
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
        }
        public void Test01()
        {
            MyNameObjectCollection noc;

            Object[] objarray;
            Foo[]    fooarray;


            // [] GetAllValues() on empty collection
            noc      = new MyNameObjectCollection();
            objarray = noc.GetAllValues();
            CheckObjArray(objarray, noc);
            // [] GetAllValues() on non-empty collection
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }
            objarray = noc.GetAllValues();
            CheckObjArray(objarray, noc);

            // [] GetAllValues(Type) on empty collection
            _strErr  = "Err_003, ";
            noc      = new MyNameObjectCollection();
            fooarray = (Foo[])noc.GetAllValues(typeof(Foo));
            CheckFooArray(fooarray, noc);

            // [] GetAllValues(Type) on non-empty collection
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }
            fooarray = (Foo[])noc.GetAllValues(typeof(Foo));
            CheckFooArray(fooarray, noc);

            // [] GetAllValues(Type) with incompatible type
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }

            Assert.Throws <ArrayTypeMismatchException>(() => { Array array = noc.GetAllValues(typeof(String)); });
        }
Esempio n. 16
0
        public void Test01()
        {
            MyNameObjectCollection noc;
            Object[] objarray;
            Foo[] fooarray;


            // [] GetAllValues() on empty collection
            noc = new MyNameObjectCollection();
            objarray = noc.GetAllValues();
            CheckObjArray(objarray, noc);
            // [] GetAllValues() on non-empty collection
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }
            objarray = noc.GetAllValues();
            CheckObjArray(objarray, noc);

            // [] GetAllValues(Type) on empty collection
            _strErr = "Err_003, ";
            noc = new MyNameObjectCollection();
            fooarray = (Foo[])noc.GetAllValues(typeof(Foo));
            CheckFooArray(fooarray, noc);

            // [] GetAllValues(Type) on non-empty collection
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }
            fooarray = (Foo[])noc.GetAllValues(typeof(Foo));
            CheckFooArray(fooarray, noc);

            // [] GetAllValues(Type) with incompatible type
            noc = new MyNameObjectCollection();
            for (int i = 0; i < 15; i++)
            {
                noc.Add("str_" + i.ToString(), new Foo());
            }

            Assert.Throws<ArrayTypeMismatchException>(() => { Array array = noc.GetAllValues(typeof(String)); });
        }
        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]);
        }
Esempio n. 18
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            ArrayList keys             = new ArrayList();
            ArrayList values           = new ArrayList();


            // [] Set up collection, including some null keys and values
            for (int i = 0; i < 10; i++)
            {
                String k = "key_" + i.ToString();
                Foo    v = new Foo();
                noc.Add(k, v);
                keys.Add(k);
                values.Add(v);
            }
            Foo val = new Foo();

            noc.Add(null, val);
            keys.Add(null);
            values.Add(val);
            noc.Add(null, null);
            keys.Add(null);
            values.Add(null);
            noc.Add("repeatedkey", null);
            keys.Add("repeatedkey");
            values.Add(null);
            noc.Add("repeatedkey", val);
            keys.Add("repeatedkey");
            values.Add(val);
            CheckCollection(noc, keys, values);

            // [] Remove from middle of collection
            noc.RemoveAt(3);
            keys.RemoveAt(3);
            values.RemoveAt(3);
            CheckCollection(noc, keys, values);

            // [] Remove first element
            noc.RemoveAt(0);
            keys.RemoveAt(0);
            values.RemoveAt(0);
            CheckCollection(noc, keys, values);

            // [] Remove last element
            noc.RemoveAt(noc.Count - 1);
            keys.RemoveAt(keys.Count - 1);
            values.RemoveAt(values.Count - 1);
            CheckCollection(noc, keys, values);

            // [] Index < 0
            Assert.Throws <ArgumentOutOfRangeException>(() => { noc.RemoveAt(-1); });

            // [] Index = Count
            Assert.Throws <ArgumentOutOfRangeException>(() => { noc.RemoveAt(noc.Count); });

            // [] Remove all elements
            int n = noc.Count;

            for (int i = 0; i < n; i++)
            {
                noc.RemoveAt(0);
                keys.RemoveAt(0);
                values.RemoveAt(0);
                CheckCollection(noc, keys, values);
            }

            // [] RemoveAt on an empty collection
            Assert.Throws <ArgumentOutOfRangeException>(() => { noc.RemoveAt(0); });

            // [] RemoveAt on a ReadOnly collection
            noc.Add("key", new Foo());
            noc.IsReadOnly = true;
            Assert.Throws <NotSupportedException>(() => { noc.RemoveAt(0); });
        }
Esempio n. 19
0
        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 Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();

            NameObjectCollectionBase.KeysCollection keys;

            // Set up initial collection
            for (int i = 0; i < 20; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Get the KeysCollection
            _strErr = "Err_001, ";
            keys    = noc.Keys;

            // Check Count
            if (keys.Count != noc.Count)
            {
                Assert.False(true, string.Format(_strErr + "keys.Count is wrong.  expected {0}, got {1}", noc.Count, keys.Count));
            }

            // Compare - test Get, Item
            for (int i = 0; i < noc.Count; i++)
            {
                if (keys.Get(i) != "key_" + i.ToString())
                {
                    Assert.False(true, string.Format(_strErr + "keys.Get({0}) is wrong.  expected {1}, got {2}", i, "key_" + i.ToString(), keys.Get(i)));
                }
                if (keys[i] != "key_" + i.ToString())
                {
                    Assert.False(true, string.Format(_strErr + "keys[{0}] is wrong.  expected {1}, got {2}", i, "key_" + i.ToString(), keys[i]));
                }
            }

            // Get enumerator - it's the same enumerator as the original collection, so don't
            // need to test it again here.
            IEnumerator en = keys.GetEnumerator();

            // Get SyncRoot - just a cursory test
            if (((ICollection)keys).SyncRoot != ((ICollection)noc).SyncRoot)
            {
                Assert.False(true, _strErr + "keys.SyncRoot was not the same as noc.SyncRoot");
            }

            // Get IsSynchronized
            if (((ICollection)keys).IsSynchronized)
            {
                Assert.False(true, _strErr + "keys.SyncRoot was not the same as noc.SyncRoot");
            }

            // Check empty collection
            noc  = new MyNameObjectCollection();
            keys = noc.Keys;

            // Check Count
            if (keys.Count != 0)
            {
                Assert.False(true, string.Format(_strErr + "keys.Count is wrong.  expected {0}, got {1}", 0, keys.Count));
            }
        }
Esempio n. 21
0
        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); });
        }
Esempio n. 22
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            ArrayList keys = new ArrayList();
            ArrayList values = new ArrayList();


            // [] Set up collection, including some null keys and values
            for (int i = 0; i < 10; i++)
            {
                String k = "key_" + i.ToString();
                Foo v = new Foo();
                noc.Add(k, v);
                keys.Add(k);
                values.Add(v);
            }
            Foo val = new Foo();
            noc.Add(null, val);
            keys.Add(null);
            values.Add(val);
            noc.Add(null, null);
            keys.Add(null);
            values.Add(null);
            noc.Add("repeatedkey", null);
            keys.Add("repeatedkey");
            values.Add(null);
            noc.Add("repeatedkey", val);
            keys.Add("repeatedkey");
            values.Add(val);
            CheckCollection(noc, keys, values);

            // [] Remove from middle of collection
            noc.RemoveAt(3);
            keys.RemoveAt(3);
            values.RemoveAt(3);
            CheckCollection(noc, keys, values);

            // [] Remove first element
            noc.RemoveAt(0);
            keys.RemoveAt(0);
            values.RemoveAt(0);
            CheckCollection(noc, keys, values);

            // [] Remove last element
            noc.RemoveAt(noc.Count - 1);
            keys.RemoveAt(keys.Count - 1);
            values.RemoveAt(values.Count - 1);
            CheckCollection(noc, keys, values);

            // [] Index < 0
            Assert.Throws<ArgumentOutOfRangeException>(() => { noc.RemoveAt(-1); });

            // [] Index = Count
            Assert.Throws<ArgumentOutOfRangeException>(() => { noc.RemoveAt(noc.Count); });

            // [] Remove all elements
            int n = noc.Count;
            for (int i = 0; i < n; i++)
            {
                noc.RemoveAt(0);
                keys.RemoveAt(0);
                values.RemoveAt(0);
                CheckCollection(noc, keys, values);
            }

            // [] RemoveAt on an empty collection
            Assert.Throws<ArgumentOutOfRangeException>(() => { noc.RemoveAt(0); });

            // [] RemoveAt on a ReadOnly collection
            noc.Add("key", new Foo());
            noc.IsReadOnly = true;
            Assert.Throws<NotSupportedException>(() => { noc.RemoveAt(0); });
        }
        public void Keys_CopyTo_NullKeys()
        {
            MyNameObjectCollection nameObjectCollection = new MyNameObjectCollection();
            nameObjectCollection.Add(null, new Foo("1"));
            nameObjectCollection.Add(null, new Foo("2"));
            nameObjectCollection.Add(null, null);
            nameObjectCollection.Add(null, new Foo("3"));

            Keys_CopyTo(nameObjectCollection, 0);
        }
Esempio n. 24
0
        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)));
            }
        }
Esempio n. 25
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            IEnumerator            en  = null;
            bool res;


            // [] Enumerator for empty collection
            // Get enumerator
            en = noc.GetEnumerator();

            // MoveNext should return false
            res = en.MoveNext();
            if (res)
            {
                Assert.False(true, _strErr + "MoveNext returned true");
            }

            //  Attempt to get Current should result in exception
            Assert.Throws <InvalidOperationException>(() => { String curr = (String)en.Current; });

            // [] Enumerator for non-empty collection
            // Add items
            for (int i = 0; i < 10; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Get enumerator
            en = noc.GetEnumerator();

            //  Attempt to get Current should result in exception
            Assert.Throws <InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Iterate over collection
            for (int i = 0; i < noc.Count; i++)
            {
                // MoveNext should return true
                res = en.MoveNext();
                if (!res)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, MoveNext returned false", i));
                }

                // Check current
                String curr = (String)en.Current;
                if (noc[curr] == null)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, Current={1}, key not found in collection", i, curr));
                }

                // Check current again
                String current1 = (String)en.Current;
                if (current1 != curr)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, Value of Current changed!  Was {1}, now {2}", i, curr, current1));
                }
            }

            // next MoveNext should bring us outside of the collection, return false
            res = en.MoveNext();
            if (res)
            {
                Assert.False(true, _strErr + "MoveNext returned true");
            }

            // Attempt to get Current should result in exception
            Assert.Throws <InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Reset
            en.Reset();

            // Attempt to get Current should result in exception
            Assert.Throws <InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Modify collection and then try MoveNext, Current, Reset
            // new collection
            noc = new MyNameObjectCollection();
            noc.Add("key1", new Foo());
            noc.Add("key2", new Foo());
            noc.Add("key3", new Foo());
            en = noc.GetEnumerator();

            // MoveNext
            if (!en.MoveNext())
            {
                Assert.False(true, _strErr + "MoveNext returned false");
            }

            // Current
            String current = (String)en.Current;

            // Modify collection
            noc.RemoveAt(0);
            if (noc.Count != 2)
            {
                Assert.False(true, string.Format(_strErr + "Collection Count wrong.  Expected {0}, got {1}", 2, noc.Count));
            }

            //  Current should not throw, but no guarantee is made on the return value
            string curr1 = (String)en.Current;

            //  MoveNext should throw exception
            Assert.Throws <InvalidOperationException>(() => { en.MoveNext(); });

            //  Reset should throw exception
            Assert.Throws <InvalidOperationException>(() => { en.Reset(); });

            //  Current should not throw, but no guarantee is made on the return value
            curr1 = (String)en.Current;

            //  MoveNext should still throw exception if collection is ReadOnly
            noc.IsReadOnly = true;
            Assert.Throws <InvalidOperationException>(() => { en.MoveNext(); });

            // Clear collection and then try MoveNext, Current, Reset
            // new collection
            noc = new MyNameObjectCollection();
            noc.Add("key1", new Foo());
            noc.Add("key2", new Foo());
            noc.Add("key3", new Foo());
            en = noc.GetEnumerator();

            // MoveNext
            if (!en.MoveNext())
            {
                Assert.False(true, _strErr + "MoveNext returned false");
            }

            // Current
            current = (String)en.Current;

            // Modify collection
            noc.Clear();
            if (noc.Count != 0)
            {
                Assert.False(true, string.Format(_strErr + "Collection Count wrong.  Expected {0}, got {1}", 2, noc.Count));
            }

            //  Current throws.  Should it throw here?!
            Assert.Throws <InvalidOperationException>(() => { String curr = (String)en.Current; });

            //  MoveNext should throw exception
            Assert.Throws <InvalidOperationException>(() => { en.MoveNext(); });

            //  Reset should throw exception
            Assert.Throws <InvalidOperationException>(() => { en.Reset(); });
        }
        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");
                }
            }
        }
Esempio n. 27
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            IEnumerator en = null;
            bool res;


            // [] Enumerator for empty collection
            // Get enumerator
            en = noc.GetEnumerator();

            // MoveNext should return false
            res = en.MoveNext();
            if (res)
            {
                Assert.False(true, _strErr + "MoveNext returned true");
            }

            //  Attempt to get Current should result in exception
            Assert.Throws<InvalidOperationException>(() => { String curr = (String)en.Current; });

            // [] Enumerator for non-empty collection
            // Add items
            for (int i = 0; i < 10; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Get enumerator
            en = noc.GetEnumerator();

            //  Attempt to get Current should result in exception
            Assert.Throws<InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Iterate over collection
            for (int i = 0; i < noc.Count; i++)
            {
                // MoveNext should return true
                res = en.MoveNext();
                if (!res)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, MoveNext returned false", i));
                }

                // Check current
                String curr = (String)en.Current;
                if (noc[curr] == null)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, Current={1}, key not found in collection", i, curr));
                }

                // Check current again
                String current1 = (String)en.Current;
                if (current1 != curr)
                {
                    Assert.False(true, string.Format(_strErr + "#{0}, Value of Current changed!  Was {1}, now {2}", i, curr, current1));
                }
            }

            // next MoveNext should bring us outside of the collection, return false
            res = en.MoveNext();
            if (res)
            {
                Assert.False(true, _strErr + "MoveNext returned true");
            }

            // Attempt to get Current should result in exception
            Assert.Throws<InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Reset
            en.Reset();

            // Attempt to get Current should result in exception
            Assert.Throws<InvalidOperationException>(() => { String curr = (String)en.Current; });

            // Modify collection and then try MoveNext, Current, Reset
            // new collection
            noc = new MyNameObjectCollection();
            noc.Add("key1", new Foo());
            noc.Add("key2", new Foo());
            noc.Add("key3", new Foo());
            en = noc.GetEnumerator();

            // MoveNext
            if (!en.MoveNext())
            {
                Assert.False(true, _strErr + "MoveNext returned false");
            }

            // Current
            String current = (String)en.Current;

            // Modify collection
            noc.RemoveAt(0);
            if (noc.Count != 2)
            {
                Assert.False(true, string.Format(_strErr + "Collection Count wrong.  Expected {0}, got {1}", 2, noc.Count));
            }

            //  Current should not throw, but no guarantee is made on the return value
            string curr1 = (String)en.Current;

            //  MoveNext should throw exception
            Assert.Throws<InvalidOperationException>(() => { en.MoveNext(); });

            //  Reset should throw exception
            Assert.Throws<InvalidOperationException>(() => { en.Reset(); });

            //  Current should not throw, but no guarantee is made on the return value
            curr1 = (String)en.Current;

            //  MoveNext should still throw exception if collection is ReadOnly
            noc.IsReadOnly = true;
            Assert.Throws<InvalidOperationException>(() => { en.MoveNext(); });

            // Clear collection and then try MoveNext, Current, Reset
            // new collection
            noc = new MyNameObjectCollection();
            noc.Add("key1", new Foo());
            noc.Add("key2", new Foo());
            noc.Add("key3", new Foo());
            en = noc.GetEnumerator();

            // MoveNext
            if (!en.MoveNext())
            {
                Assert.False(true, _strErr + "MoveNext returned false");
            }

            // Current
            current = (String)en.Current;

            // Modify collection
            noc.Clear();
            if (noc.Count != 0)
            {
                Assert.False(true, string.Format(_strErr + "Collection Count wrong.  Expected {0}, got {1}", 2, noc.Count));
            }

            //  Current throws.  Should it throw here?!
            Assert.Throws<InvalidOperationException>(() => { String curr = (String)en.Current; });

            //  MoveNext should throw exception
            Assert.Throws<InvalidOperationException>(() => { en.MoveNext(); });

            //  Reset should throw exception
            Assert.Throws<InvalidOperationException>(() => { en.Reset(); });
        }