// 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); }
public EnumerableValuesCollection(ReadOnlyMultiDictionaryBase <TKey, TValue> myDictionary) { _myDictionary = myDictionary; }
public KeyValuePairsCollection(ReadOnlyMultiDictionaryBase <TKey, TValue> myDictionary) { _myDictionary = myDictionary; }
/// <summary> /// Constructor. Initializes this collection. /// </summary> /// <param name="myDictionary">Dictionary we're using.</param> /// <param name="key">The key we're looking at.</param> public ValuesForKeyCollection(ReadOnlyMultiDictionaryBase <TKey, TValue> myDictionary, TKey key) { _myDictionary = myDictionary; _key = key; }