RemoveAt() public method

public RemoveAt ( int index ) : void
index int
return void
Esempio n. 1
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));
        }
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 RemoveAt_ReadOnly_ThrowsNotSupportedException()
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(1);

            nameObjectCollection.IsReadOnly = true;
            Assert.Throws <NotSupportedException>(() => nameObjectCollection.RemoveAt(0));
        }
        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. 5
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. 6
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. 7
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(); });
        }
Esempio n. 8
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. 9
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(); });
        }
Esempio n. 10
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)));
            }
        }