Clear() public method

public Clear ( ) : void
return void
        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);
        }
Example #2
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);
        }
Example #3
0
        public void Keys_GetEnumerator_Invalid(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

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

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

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

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

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

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

            // Clear collection
            nameObjectCollection.IsReadOnly = false;
            enumerator = keys.GetEnumerator();
            enumerator.MoveNext();
            nameObjectCollection.Clear();
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
        }
        public void 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]);
        }
Example #5
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); });
        }
Example #6
0
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            IEnumerator en = null;
            bool res;


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

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

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

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

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

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

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

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

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

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

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

            // Reset
            en.Reset();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            //  Reset should throw exception
            Assert.Throws<InvalidOperationException>(() => { en.Reset(); });
        }
        public void Test01()
        {
            MyNameObjectCollection noc = new MyNameObjectCollection();
            Array array = null;
            ArrayList ArrayValues = new ArrayList();
            Random rand = new Random(-55);
            int n = 0;
            String key1 = "key1";
            String key2 = "key2";
            Foo val1 = new Foo();
            Foo val2 = new Foo();

            // [] Copy a collection to middle of target array.
            // Set up initial collection
            n = rand.Next(10, 1000);
            for (int i = 0; i < n; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Set up initial array
            n = noc.Count + rand.Next(20, 100);
            array = Array.CreateInstance(typeof(String), n);
            ArrayValues.Clear();
            for (int i = 0; i < n; i++)
            {
                String v = "arrayvalue_" + i.ToString();
                array.SetValue(v, i);
                ArrayValues.Add(v);
            }

            // Copy the collection
            int offset = 10;
            ((ICollection)noc.Keys).CopyTo(array, offset);
            for (int i = 0; i < noc.Count; i++)
            {
                ArrayValues[i + offset] = noc.GetKey(i);
            }

            // Check array
            CheckArray(array, ArrayValues);

            // [] Verify copy is distinct from original collection.
            // Clear initial collection
            noc.Clear();

            // Verify copy is not cleared
            CheckArray(array, ArrayValues);

            // [] Fill whole array (index=0)
            // Set up initial collection
            noc = new MyNameObjectCollection();
            n = rand.Next(10, 1000);
            for (int i = 0; i < n; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Set up initial array
            n = noc.Count;
            array = Array.CreateInstance(typeof(String), n);
            ArrayValues.Clear();
            for (int i = 0; i < n; i++)
            {
                String v = "arrayvalue_" + i.ToString();
                array.SetValue(v, i);
                ArrayValues.Add(v);
            }

            // Copy the collection
            ((ICollection)noc.Keys).CopyTo(array, 0);
            for (int i = 0; i < noc.Count; i++)
            {
                ArrayValues[i] = noc.GetKey(i);
            }

            // Check array
            CheckArray(array, ArrayValues);

            // [] index = max index in array
            // Set up initial collection
            noc.Clear();
            noc.Add("key1", new Foo());

            // Set up initial array
            n = noc.Count + rand.Next(1, 100);
            array = Array.CreateInstance(typeof(String), n);
            ArrayValues.Clear();
            for (int i = 0; i < n; i++)
            {
                String v = "arrayvalue_" + i.ToString();
                array.SetValue(v, i);
                ArrayValues.Add(v);
            }

            // Copy the collection
            offset = ArrayValues.Count - 1;
            ((ICollection)noc.Keys).CopyTo(array, offset);
            ArrayValues[offset] = noc.GetKey(0);

            // Retrieve values
            CheckArray(array, ArrayValues);

            // [] Target array is zero-length.
            array = Array.CreateInstance(typeof(String), 0);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 0); });

            // [] Call on an empty collection (to zero-length array).
            noc = new MyNameObjectCollection();
            array = Array.CreateInstance(typeof(String), 0);

            // Copy the collection
            ((ICollection)noc.Keys).CopyTo(array, 0);

            // [] Call on an empty collection.
            noc = new MyNameObjectCollection();
            array = Array.CreateInstance(typeof(String), 16);

            // Copy the collection
            ((ICollection)noc.Keys).CopyTo(array, 0);

            // Retrieve elements
            foreach (String v in array)
            {
                if (v != null)
                {
                    Assert.False(true, _strErr + "Value is incorrect.  array should be null");
                }
            }

            // [] Call with array = null
            noc = new MyNameObjectCollection();
            Assert.Throws<ArgumentNullException>(() => { ((ICollection)noc.Keys).CopyTo(null, 0); });

            // [] Target array is multidimensional.
            array = new string[20, 2];
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 16); });

            // [] Target array is of incompatible type.
            array = Array.CreateInstance(typeof(Foo), 10);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<InvalidCastException>(() => { ((ICollection)noc.Keys).CopyTo(array, 1); });

            // [] index = array length
            n = rand.Next(10, 100);
            array = Array.CreateInstance(typeof(String), n);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, n); });

            // [] index > array length
            n = rand.Next(10, 100);
            array = Array.CreateInstance(typeof(String), n);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, n + 1); });

            // [] index = Int32.MaxValue
            array = Array.CreateInstance(typeof(String), 10);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, Int32.MaxValue); });

            // [] index < 0
            array = Array.CreateInstance(typeof(String), 10);
            noc = new MyNameObjectCollection();
            noc.Add(key1, val1);
            noc.Add(key2, val2);
            Assert.Throws<ArgumentOutOfRangeException>(() => { ((ICollection)noc.Keys).CopyTo(array, -1); });

            // [] index is valid but collection doesn't fit in available space
            noc = new MyNameObjectCollection();
            // Set up initial collection
            n = rand.Next(10, 1000);
            for (int i = 0; i < n; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Set up initial array
            n = noc.Count + 20;
            array = Array.CreateInstance(typeof(Foo), n);

            Assert.Throws<ArgumentException>(() => { ((ICollection)noc.Keys).CopyTo(array, 30); });  // array is only 20 bigger than collection

            // [] index is negative
            noc = new MyNameObjectCollection();
            // Set up initial collection
            n = rand.Next(10, 1000);
            for (int i = 0; i < n; i++)
            {
                noc.Add("key_" + i.ToString(), new Foo());
            }

            // Set up initial array
            n = noc.Count + 20;
            array = Array.CreateInstance(typeof(Foo), n);

            Assert.Throws<ArgumentOutOfRangeException>(() => { ((ICollection)noc.Keys).CopyTo(array, -1); });

            // [] All keys are null
            noc = new MyNameObjectCollection();
            noc.Add(null, new Foo());
            noc.Add(null, new Foo());
            noc.Add(null, null);
            noc.Add(null, new Foo());
            if (noc.Count != 4)
            {
                Assert.False(true, _strErr + "Elements were not added correctly");
            }

            array = Array.CreateInstance(typeof(String), 16);

            // Copy the collection
            ((ICollection)noc.Keys).CopyTo(array, 0);

            // Retrieve elements
            foreach (String v in array)
            {
                if (v != null)
                {
                    Assert.False(true, _strErr + "Value is incorrect.  array should be null");
                }
            }
        }
Example #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)));
            }
        }
        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);
        }
Example #10
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(); });
        }
Example #11
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)));
            }
        }