/// <summary>
        /// Test dictionary enumeration.
        /// </summary>
        /// <typeparam name="TKey">Key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">Value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to test.</param>
        /// <param name="key">Key that is present in the dictionary.</param>
        /// <param name="value">Value associated with the key in the dictionary.</param>
        private static void TestDictionaryEnumeration <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var         kvp        = new KeyValuePair <TKey, TValue>(key, value);
            IEnumerable enumerator = dictionary.Where(x => true);

            foreach (object o in enumerator)
            {
                Assert.AreEqual(o, kvp, "Dictionary query enumeration");
            }

            enumerator = dictionary.Keys.Where(x => true);
            foreach (object o in enumerator)
            {
                Assert.AreEqual(o, kvp.Key, "Dictionary query enumeration");
            }

            foreach (KeyValuePair <TKey, TValue> o in dictionary.Where(x => true).Reverse())
            {
                Assert.AreEqual(o, kvp, "Dictionary query enumeration");
            }

            foreach (object o in (IEnumerable)dictionary)
            {
                Assert.AreEqual(o, kvp, "Dictionary enumeration");
            }

            foreach (KeyValuePair <TKey, TValue> a in dictionary.Reverse())
            {
                Assert.AreEqual(a, kvp, "Reverse dictionary enumeration");
            }

            foreach (object o in (IEnumerable)dictionary.Keys)
            {
                Assert.AreEqual(o, key, "Key enumeration");
            }

            foreach (TKey k in dictionary.Keys.Reverse())
            {
                Assert.AreEqual(k, key, "Reverse key enumeration");
            }

            foreach (TValue v in dictionary.Values.Reverse())
            {
                Assert.AreEqual(v, value, "Value enumeration");
            }

            foreach (TKey k in ((IDictionary <TKey, TValue>)dictionary).Keys)
            {
                Assert.AreEqual(k, key, "Key enumeration");
            }

            foreach (TValue v in ((IDictionary <TKey, TValue>)dictionary).Values)
            {
                Assert.AreEqual(v, value, "Value enumeration");
            }
        }
Пример #2
0
 public void VerifyWhereThrowsExceptionWhenExpressionIsNull()
 {
     using (var persistentDictionary = new PersistentDictionary <Guid, Guid>(DictionaryLocation))
     {
         persistentDictionary.Where(null);
     }
 }
Пример #3
0
        /// <summary>
        /// Test LINQ queries on the dictionary.
        /// </summary>
        /// <typeparam name="TKey">Key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">Value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to test.</param>
        /// <param name="key">Key that is present in the dictionary.</param>
        /// <param name="value">Value associated with the key in the dictionary.</param>
        private static void TestDictionaryLinq <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var kvp = new KeyValuePair <TKey, TValue>(key, value);

            Assert.IsTrue(dictionary.Any(x => 0 == x.Key.CompareTo(key)), "Any == should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => 0 <= x.Key.CompareTo(key)), "Any <= should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => 0 >= x.Key.CompareTo(key)), "Any >= should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => !(0 != x.Key.CompareTo(key))), "Any !(!=) should have found {0}", key);

            var query = from x in dictionary where x.Key.CompareTo(key) == 0 select x.Value;

            Assert.AreEqual(value, query.Single(), "Where == failed");
            query = from x in dictionary where x.Key.CompareTo(key) == 0 select x.Value;
            Assert.AreEqual(value, query.Reverse().Single(), "Where == failed (reversed)");
            query = from x in dictionary where x.Key.CompareTo(key) <= 0 select x.Value;
            Assert.AreEqual(value, query.Single(), "Where <= failed");
            query = from x in dictionary where x.Key.CompareTo(key) >= 0 select x.Value;
            Assert.AreEqual(value, query.Single(), "Where >= failed");
            query = from x in dictionary where !(x.Key.CompareTo(key) != 0) select x.Value;
            Assert.AreEqual(value, query.Single(), "Where !(!=) failed");
            Assert.AreEqual(kvp, dictionary.Where(x => x.Key.CompareTo(key) >= 0).Reverse().Last(), "Where.Reverse.Last failed");

            Assert.AreEqual(kvp, dictionary.First(), "First");
            Assert.AreEqual(kvp, dictionary.First(x => x.Key.CompareTo(key) == 0), "First");
            Assert.AreEqual(kvp, dictionary.FirstOrDefault(), "FirstOrDefault");
            Assert.AreEqual(kvp, dictionary.FirstOrDefault(x => x.Key.CompareTo(key) == 0), "FirstOrDefault");
            Assert.AreEqual(kvp, dictionary.Last(), "Last");
            Assert.AreEqual(kvp, dictionary.Last(x => x.Key.CompareTo(key) == 0), "Last");
            Assert.AreEqual(kvp, dictionary.LastOrDefault(), "LastOrDefault");
            Assert.AreEqual(kvp, dictionary.LastOrDefault(x => x.Key.CompareTo(key) == 0), "LastOrDefault");

            Assert.AreEqual(1, dictionary.Count(x => x.Key.CompareTo(key) <= 0), "Count failed");
        }