Exemple #1
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 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); });
        }