Exemplo n.º 1
0
        public void AsReadOnly()
        {
            int[] elements = new int[400];
            for (int i = 0; i < 400; ++i)
            {
                elements[i] = i;
            }

            ReadWriteTestCollection <int> coll1 = new ReadWriteTestCollection <int>(elements);
            ICollection <int>             coll2 = coll1.AsReadOnly();

            InterfaceTests.TestReadonlyCollectionGeneric <int>(coll2, elements, true, null);

            coll1.Add(27);
            coll1.Add(199);

            elements = new int[402];
            coll2    = coll1.AsReadOnly();

            for (int i = 0; i < 400; ++i)
            {
                elements[i] = i;
            }

            elements[400] = 27;
            elements[401] = 199;

            InterfaceTests.TestReadonlyCollectionGeneric <int>(coll2, elements, true, null);

            coll1 = new ReadWriteTestCollection <int>(new int[0]);
            coll2 = coll1.AsReadOnly();
            InterfaceTests.TestReadonlyCollectionGeneric <int>(coll2, new int[0], true, null);
            coll1.Add(4);
            InterfaceTests.TestReadonlyCollectionGeneric <int>(coll2, new int[] { 4 }, true, null);
        }
        public void ReadOnlyValueCollection()
        {
            ReadOnlyTestMultiDictionary <string, int> dict = CreateTestReadOnlyDictionary();
            ICollection <int> valueColl = dict["Eric"];

            InterfaceTests.TestReadonlyCollectionGeneric(valueColl, new int[] { 1, 9, 11 }, true, null);
        }
Exemplo n.º 3
0
        public void ReadOnlyCollection()
        {
            string[] s = { "Hello", "Goodbye", "Eric", "Clapton", "Rules" };

            ReadOnlyTestCollection <string> coll = new ReadOnlyTestCollection <string>(s);

            InterfaceTests.TestCollection <string>((ICollection)coll, s, true);
            InterfaceTests.TestReadonlyCollectionGeneric <string>((ICollection <string>)coll, s, true, "ReadOnlyTestCollection");
        }
Exemplo n.º 4
0
        public void CheckList()
        {
            string[] s = { "Hello", "Goodbye", "Eric", "Clapton", "Rules" };

            List <string> coll = new List <string>(s);

            InterfaceTests.TestCollection <string>((ICollection)coll, s, true);
            InterfaceTests.TestReadWriteCollectionGeneric <string>((ICollection <string>)coll, s, true);

            IList <string> ro = new List <string>(s).AsReadOnly();

            InterfaceTests.TestReadonlyCollectionGeneric <string>(ro, s, true, null);
        }
Exemplo n.º 5
0
        public void CheckDictionaryKeyValues()
        {
            Dictionary <string, int> dict = new Dictionary <string, int>();

            dict["Eric"]     = 3;
            dict["Clapton"]  = 1;
            dict["Rules"]    = 4;
            dict["The"]      = 1;
            dict["Universe"] = 5;

            InterfaceTests.TestCollection <string>(dict.Keys, new string[] { "Eric", "Clapton", "Rules", "The", "Universe" }, false);
            InterfaceTests.TestReadonlyCollectionGeneric <string>(dict.Keys, new string[] { "Eric", "Clapton", "Rules", "The", "Universe" }, false, null);
            InterfaceTests.TestCollection <int>(dict.Values, new int[] { 1, 1, 3, 4, 5 }, false);
            InterfaceTests.TestReadonlyCollectionGeneric <int>(dict.Values, new int[] { 1, 1, 3, 4, 5 }, false, null);
        }
        // Check the contents of a ReadOnly Multi-Dictionary non-destructively. Keys and Values must be in order.
        internal static void CheckOrderedReadOnlyMultiDictionaryContents <TKey, TValue>(ReadOnlyMultiDictionaryBase <TKey, TValue> dict,
                                                                                        TKey[] keys, TValue[][] values, TKey nonKey,
                                                                                        TValue nonValue, string name,
                                                                                        Func <TKey, TKey, bool> keyEquals,
                                                                                        Func <TValue, TValue, bool> valueEquals)
        {
            int iKey, iValue;
            ICollection <TValue> getValues;

            if (keyEquals == null)
            {
                keyEquals = delegate(TKey x, TKey y) { return(object.Equals(x, y)); }
            }
            ;
            if (valueEquals == null)
            {
                valueEquals = delegate(TValue x, TValue y) { return(object.Equals(x, y)); }
            }
            ;

            // Check Count.
            Assert.AreEqual(keys.Length, dict.Count);

            // Check indexer, ContainsKey, Contains, TryGetValue for each key.
            for (iKey = 0; iKey < keys.Length; ++iKey)
            {
                Assert.IsTrue(dict.ContainsKey(keys[iKey]));
                Assert.IsTrue(dict.Contains(new KeyValuePair <TKey, ICollection <TValue> >(keys[iKey], values[iKey])));

                bool b = ((IDictionary <TKey, ICollection <TValue> >)dict).TryGetValue(keys[iKey], out getValues);
                Assert.IsTrue(b);
                iValue = 0;
                foreach (TValue val in getValues)
                {
                    Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                    ++iValue;
                }

                iValue = 0;
                foreach (TValue val in values[iKey])
                {
                    Assert.IsTrue(dict.Contains(keys[iKey], val));
                    ++iValue;
                }

                iValue = 0;
                foreach (TValue val in dict[keys[iKey]])
                {
                    Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                    ++iValue;
                }
                Assert.IsTrue(iValue == values[iKey].Length);
            }

            // Check Keys collection.
            iKey = 0;
            foreach (TKey key in dict.Keys)
            {
                Assert.IsTrue(keyEquals(keys[iKey], key));
                ++iKey;
            }
            Assert.IsTrue(iKey == keys.Length);
            InterfaceTests.TestReadonlyCollectionGeneric <TKey>(dict.Keys, keys, true, null);

            // Check Values collection
            iKey   = 0;
            iValue = 0;
            int valueCount = 0;

            foreach (TValue val in dict.Values)
            {
                Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                ++iValue;
                if (iValue == values[iKey].Length)
                {
                    iValue = 0;
                    ++iKey;
                }
                ++valueCount;
            }
            Assert.IsTrue(iKey == keys.Length);

            int a = 0;

            TValue[] vals = new TValue[valueCount];
            for (iKey = 0; iKey < keys.Length; ++iKey)
            {
                for (iValue = 0; iValue < values[iKey].Length; ++iValue)
                {
                    vals[a++] = values[iKey][iValue];
                }
            }
            InterfaceTests.TestReadonlyCollectionGeneric <TValue>(dict.Values, vals, true, null);

            // Check KeyValuePairs collection.
            iKey       = 0;
            iValue     = 0;
            valueCount = 0;
            foreach (KeyValuePair <TKey, TValue> pair in dict.KeyValuePairs)
            {
                Assert.IsTrue(keyEquals(keys[iKey], pair.Key));
                Assert.IsTrue(valueEquals(values[iKey][iValue], pair.Value));
                ++iValue;
                if (iValue == values[iKey].Length)
                {
                    iValue = 0;
                    ++iKey;
                }
                ++valueCount;
            }
            Assert.IsTrue(iKey == keys.Length);

            a = 0;
            KeyValuePair <TKey, TValue>[] pairs = new KeyValuePair <TKey, TValue> [valueCount];
            for (iKey = 0; iKey < keys.Length; ++iKey)
            {
                for (iValue = 0; iValue < values[iKey].Length; ++iValue)
                {
                    pairs[a++] = new KeyValuePair <TKey, TValue>(keys[iKey], values[iKey][iValue]);
                }
            }
            InterfaceTests.TestReadonlyCollectionGeneric <KeyValuePair <TKey, TValue> >(dict.KeyValuePairs, pairs, true, null);

            // Tests Contains, ContainsKey, TryGetValue for wrong values.
            Assert.IsFalse(dict.ContainsKey(nonKey));
            Assert.IsFalse(((IDictionary <TKey, ICollection <TValue> >)dict).TryGetValue(nonKey, out getValues));
            for (iKey = 0; iKey < keys.Length; ++iKey)
            {
                Assert.IsFalse(dict.Contains(keys[iKey], nonValue));
                Assert.IsFalse(dict.Contains(new KeyValuePair <TKey, ICollection <TValue> >(keys[iKey], new TValue[1] {
                    nonValue
                })));
            }

            // Test IDictionary<TKey,IEnumerable<TValue>> implementation
            InterfaceTests.TestReadOnlyMultiDictionaryGeneric <TKey, TValue>(dict, keys, values, nonKey, nonValue, true, name, null, null);
        }