Наследование: System.Collections.Specialized.NameObjectCollectionBase
Пример #1
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));
            }
        }
Пример #2
0
        public void RemoveAt_InvalidIndex_ThrowsArgumentOutOfRangeException(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => nameObjectCollection.RemoveAt(-1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => nameObjectCollection.RemoveAt(count));
        }
        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);
        }
Пример #4
0
        public void RemoveAt_ReadOnly_ThrowsNotSupportedException()
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(1);

            nameObjectCollection.IsReadOnly = true;
            Assert.Throws <NotSupportedException>(() => nameObjectCollection.RemoveAt(0));
        }
Пример #5
0
        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);
        }
Пример #6
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 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)));
        }
Пример #8
0
        public void Keys_Properties(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);
            ICollection            keysCollection       = nameObjectCollection.Keys;

            Assert.Same(((ICollection)nameObjectCollection).SyncRoot, keysCollection.SyncRoot);
            Assert.False(keysCollection.IsSynchronized);
        }
        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)));
        }
        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);
        }
Пример #11
0
        public void SyncRoot()
        {
            ICollection nameObjectCollection1 = new MyNameObjectCollection();
            ICollection nameObjectCollection2 = new MyNameObjectCollection();

            Assert.False(nameObjectCollection1.IsSynchronized);

            Assert.Same(nameObjectCollection1.SyncRoot, nameObjectCollection1.SyncRoot);
            Assert.NotSame(nameObjectCollection1.SyncRoot, nameObjectCollection2.SyncRoot);
        }
Пример #12
0
        public void SyncRoot()
        {
            ICollection nameObjectCollection1 = new MyNameObjectCollection();
            ICollection nameObjectCollection2 = new MyNameObjectCollection();

            Assert.False(nameObjectCollection1.IsSynchronized);

            Assert.Same(nameObjectCollection1.SyncRoot, nameObjectCollection1.SyncRoot);
            Assert.NotEqual(nameObjectCollection1.SyncRoot, nameObjectCollection2.SyncRoot);
        }
        public void GetAllValues(int count, Type type)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            if (type == typeof(object))
            {
                VerifyGetAllValues(nameObjectCollection, nameObjectCollection.GetAllValues());
            }
            VerifyGetAllValues(nameObjectCollection, nameObjectCollection.GetAllValues(type));
        }
        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);
        }
Пример #15
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);
        }
Пример #16
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));
                }
            }
        }
Пример #17
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]);
            }
        }
Пример #18
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]);
            }
        }
        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);
        }
Пример #20
0
 void CheckFooArray(Foo[] array, MyNameObjectCollection expected)
 {
     if (array.Length != expected.Count)
     {
         Assert.False(true, string.Format(_strErr + "Array length != {0}.  Length == {1}", expected.Count, array.Length));
     }
     for (int i = 0; i < expected.Count; i++)
     {
         if ((array[i]) != expected[i])
         {
             Assert.False(true, string.Format("Value {0} is incorrect.  array[{0}]={1}, should be {2}", i, (array[i]), expected[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);
        }
Пример #22
0
 void CheckFooArray(Foo[] array, MyNameObjectCollection expected)
 {
     if (array.Length != expected.Count)
     {
         Assert.False(true, string.Format(_strErr + "Array length != {0}.  Length == {1}", expected.Count, array.Length));
     }
     for (int i = 0; i < expected.Count; i++)
     {
         if ((array[i]) != expected[i])
         {
             Assert.False(true, string.Format("Value {0} is incorrect.  array[{0}]={1}, should be {2}", i, (array[i]), expected[i]));
         }
     }
 }
Пример #23
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)); });
        }
Пример #24
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());
        }
Пример #25
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)); });
        }
Пример #26
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");
            }
        }
Пример #27
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 CopyTo_Invalid(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);
            ICollection            collection           = nameObjectCollection;

            Assert.Throws <ArgumentNullException>("array", () => collection.CopyTo(null, 0));
            Assert.Throws <ArgumentException>("array", () => collection.CopyTo(new string[count, count], 0));

            if (count > 0)
            {
                Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new string[0], 0));
                Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new string[count - 1], 0));

                Assert.Throws <InvalidCastException>(() => collection.CopyTo(new Foo[count], 0));
            }

            Assert.Throws <ArgumentOutOfRangeException>("index", () => collection.CopyTo(new string[count], -1));
            Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new string[count], 1));
            Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new string[count], count + 1));
        }
        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]);
        }
Пример #31
0
        public void Keys_GetEnumerator(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            NameObjectCollectionBase.KeysCollection keys = nameObjectCollection.Keys;
            Assert.NotSame(keys.GetEnumerator(), keys.GetEnumerator());

            IEnumerator enumerator = keys.GetEnumerator();

            for (int i = 0; i < 2; i++)
            {
                int counter = 0;
                while (enumerator.MoveNext())
                {
                    Assert.Equal(keys[counter], enumerator.Current);
                    counter++;
                }
                Assert.Equal(count, keys.Count);
                enumerator.Reset();
            }
        }
Пример #32
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); });
        }
Пример #33
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)));
            }
        }
Пример #34
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)));
            }
        }
Пример #35
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.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");
                }
            }
        }
Пример #36
0
        public void Keys_PreservesInstance(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            Assert.Same(nameObjectCollection.Keys, nameObjectCollection.Keys);
        }
Пример #37
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);
        }
Пример #38
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(); });
        }
Пример #39
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); });
        }
Пример #40
0
        public static void TestGetAllValues_NullType_Throws()
        {
            var nameObjectCollection = new MyNameObjectCollection();

            Assert.Throws <ArgumentNullException>("type", () => nameObjectCollection.GetAllValues(null));
        }
Пример #41
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); });
        }
Пример #42
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]));
                    }
                }
            }
        }
Пример #43
0
        public void Keys_CopyTo(int count, int index)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            Keys_CopyTo_Helper(nameObjectCollection, index);
        }
        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));
            }
        }
Пример #45
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);
        }
Пример #46
0
 public static void TestGetAllValues_NullType_Throws()
 {
     var nameObjectCollection = new MyNameObjectCollection();
     Assert.Throws<ArgumentNullException>("type", () => nameObjectCollection.GetAllValues(null));
 }
Пример #47
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(); });
        }