/// <summary>
        /// Test dictionary insert/replace/delete.
        /// </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 TestBasicOperations <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var kvp = new KeyValuePair <TKey, TValue>(key, value);

            // Add a record
            dictionary.Add(key, value);

            // Test PersistentDictionary.Add error handling
            try
            {
                dictionary.Add(key, value);
                Assert.Fail("Expected ArgumentException from Add");
            }
            catch (ArgumentException)
            {
                // Expected
            }

            // Overwrite a value
            dictionary[key] = value;

            // Retrieve a value
            Assert.AreEqual(value, dictionary[key], "Retrieve with [] failed");
            TValue t;

            Assert.IsTrue(dictionary.TryGetValue(key, out t), "TryGetValue({0}) failed", key);
            Assert.AreEqual(value, t, "TryGetValue({0}) returned the wrong value", key);

            // Clear and re-insert
            dictionary.Clear();
            Assert.AreEqual(0, dictionary.Count, "Dictionary is empty. Count is wrong");
            dictionary[key] = value;
            Assert.AreEqual(1, dictionary.Count, "Item was just inserted. Count is wrong");

            // Get the keys and values
            Assert.AreEqual(dictionary.Keys.First(), key, "Keys collection");
            Assert.AreEqual(dictionary.Values.First(), value, "Values collection");

            // Test PersistentDictionary.Contains (true)
            Assert.IsTrue(dictionary.ContainsKey(key), "Dictionary should have contained key {0}", key);
            Assert.IsTrue(dictionary.ContainsValue(value), "Dictionary should have contained value {0}", value);
            Assert.IsTrue(dictionary.Contains(kvp), "Dictionary should have contained <{0},{1}>", key, value);

            // Test PersistentDictionary.Remove
            Assert.IsTrue(dictionary.Remove(key), "Key {0} should exist, but removal failed", key);
            Assert.IsFalse(dictionary.Remove(key), "Key {0} doesn't exist, but removal succeeded", key);

            dictionary.Add(kvp);
            Assert.IsTrue(dictionary.Remove(kvp), "KeyValuePair <{0},{1}> should exist, but removal failed", kvp.Key, kvp.Value);
            Assert.IsFalse(dictionary.Remove(kvp), "KeyValuePair <{0},{1}> doesn't exist, but removal succeeded", kvp.Key, kvp.Value);

            // Test PersistentDictionary.Contains (false)
            Assert.IsFalse(dictionary.ContainsKey(key), "Dictionary should have contained key {0}", key);
            Assert.IsFalse(dictionary.ContainsValue(value), "Dictionary should have contained value {0}", value);
            Assert.IsFalse(dictionary.Contains(kvp), "Dictionary should have contained <{0},{1}>", key, value);
        }
예제 #2
0
        public void PersistentDictionary_create()
        {
            using (var dic = new PersistentDictionary <string, int>())
            {
                int max = 10;
                for (int i = 0; i < max; i++)
                {
                    dic[i.ToString()] = i;
                }

                Assert.AreEqual(dic.Count, max);
                dic.Clear();
                Assert.AreEqual(dic.Count, 0);
            }
        }
예제 #3
0
        public void PersistentDictionary_typedCreated()
        {
            using (var dic = new PersistentDictionary <string, object>())
            {
                int max = 10;
                for (int i = 0; i < max; i++)
                {
                    dic[i.ToString()] = i;
                }

                Assert.AreEqual(dic.Count, max);
                CollectionAssert.AllItemsAreInstancesOfType(dic.Values.ToArray(), typeof(int));

                dic.Clear();
                Assert.AreEqual(dic.Count, 0);
            }
        }
        /// <summary>
        /// Create and modify a generic dictionary.
        /// </summary>
        /// <typeparam name="TKey">The key type for the dictionary.</typeparam>
        /// <typeparam name="TValue">The value type for the dictionary.</typeparam>
        private static void TestNullableGenericDictionary <TKey, TValue>() where TKey : IComparable <TKey> where TValue : struct
        {
            // Test the version with a nullable value. Use both null and non-null values.
            using (var dictionary = new PersistentDictionary <TKey, TValue?>(DictionaryPath))
            {
                RunDictionaryTests(dictionary, default(TKey), default(TValue));
                dictionary.Clear();
                RunDictionaryTests(dictionary, default(TKey), default(TValue?));
            }

            PersistentDictionaryFile.DeleteFiles(DictionaryPath);

            using (var dictionary = new PersistentDictionary <TKey, TValue>(DictionaryPath))
            {
                TKey   key   = default(TKey);
                TValue value = default(TValue);
                RunDictionaryTests(dictionary, key, value);
            }

            // Reopen the database
            Dictionary <TKey, TValue> temp;

            using (var dictionary = new PersistentDictionary <TKey, TValue>(DictionaryPath))
            {
                temp = new Dictionary <TKey, TValue>(dictionary);
            }

            // Delete the database
            Assert.IsTrue(PersistentDictionaryFile.Exists(DictionaryPath), "Dictionary should exist");
            PersistentDictionaryFile.DeleteFiles(DictionaryPath);
            Assert.IsFalse(PersistentDictionaryFile.Exists(DictionaryPath), "Dictionary should have been deleted");

            // Recreate the database
            using (var dictionary = new PersistentDictionary <TKey, TValue>(temp, DictionaryPath))
            {
                DictionaryAssert.AreEqual(temp, dictionary);
            }

            PersistentDictionaryFile.DeleteFiles(DictionaryPath);
        }
예제 #5
0
 /// <inheritdoc />
 public override void Clear()
 {
     index.Clear();
 }
예제 #6
0
        public Esent()
        {
            dictionary = new PersistentDictionary <int, string>("Names");

            dictionary.Clear();
        }
예제 #7
0
        public Esent()
        {
            dictionary = new PersistentDictionary<int, string>("Names");

            dictionary.Clear();
        }