Ejemplo n.º 1
0
 public Repository(
     OrderedSet<PhoneBookEntry> sortedEntries,
     IDictionary<string, PhoneBookEntry> entriesByName,
     MultiDictionaryBase<string, PhoneBookEntry> entriesByPhone)
 {
     this.sortedEntries = sortedEntries;
     this.entriesByName = entriesByName;
     this.entriesByPhone = entriesByPhone;
 }
Ejemplo n.º 2
0
 public PhonebookRepositoryFast(IDictionary <string, IPhoneEntry> entriesByName, MultiDictionaryBase <string, IPhoneEntry> entriesByPhone)
 {
     this.sortedEntries  = new OrderedSet <IPhoneEntry>();
     this.entriesByName  = entriesByName;
     this.entriesByPhone = entriesByPhone;
 }
 public PhonebookRepositoryFast(IDictionary<string, IPhoneEntry> entriesByName, MultiDictionaryBase<string, IPhoneEntry> entriesByPhone)
 {
     this.sortedEntries = new OrderedSet<IPhoneEntry>();
     this.entriesByName = entriesByName;
     this.entriesByPhone = entriesByPhone;
 }
Ejemplo n.º 4
0
        // Check the contents of a Multi-Dictionary non-destructively. Keys and Values must be in order.
        internal static void CheckOrderedMultiDictionaryContents <TKey, TValue>(MultiDictionaryBase <TKey, TValue> dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, BinaryPredicate <TKey> keyEquals, BinaryPredicate <TValue> 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,ICollection<TValue>> implementation
            InterfaceTests.TestMultiDictionaryGeneric <TKey, TValue>(dict, keys, values, nonKey, nonValue, true, null, null);
        }