Ejemplo n.º 1
0
        public void ReadOnlyDictionary()
        {
            string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World" };
            int[][]  i_array = new int[][] { new int[] { 1, 9, 11 },
                                             new int[] { 6, 10 },
                                             new int[] { 4 },
                                             new int[] { 1, 2, 3, 4, 5, 6 },
                                             new int[] { 8 } };

            ReadOnlyTestMultiDictionary <string, int> dict = CreateTestReadOnlyDictionary();

            CheckOrderedReadOnlyMultiDictionaryContents(dict, s_array, i_array, "foo", 113, "ReadOnlyTestMultiDictionary", null, null);

            InterfaceTests.TestReadOnlyMultiDictionaryGeneric <string, int>(dict, s_array, i_array, "foo", 113, true, "ReadOnlyTestMultiDictionary", null, null);
            InterfaceTests.TestReadOnlyMultiDictionaryGeneric <string, int>(dict, s_array, i_array, "foo", 113, false, "ReadOnlyTestMultiDictionary", null, 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);
        }