using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; using Developpez.Dotnet.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class TrieDictionaryTests { #region Standard dictionary tests [Test] public void Test_AddAndGetIndexer() { var dic1 = new TrieDictionary(); dic1.Add("key1", "value1"); Assert.AreEqual("value1", dic1["key1"]); Assert.AreEqual(1, dic1.Count); var dic2 = new TrieDictionary(); dic2.Add("2", 2); dic2.Add("3", 3); dic2.Add("4", 4); Assert.AreEqual(2, dic2["2"]); Assert.AreEqual(3, dic2["3"]); Assert.AreEqual(4, dic2["4"]); Assert.AreEqual(3, dic2.Count); } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_AddNullKey() { var dic = new TrieDictionary(); dic.Add(null, 42); } [Test, ExpectedException(typeof(ArgumentException))] public void Test_AddDuplicate() { var dic = new TrieDictionary(); dic.Add("foo", 42); dic.Add("foo", 99); } [Test, ExpectedException(typeof(KeyNotFoundException))] public void Test_GetIndexerNonExisting() { var dic = new TrieDictionary(); int foo = dic["foo"]; } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_GetIndexerNullKey() { var dic = new TrieDictionary(); int s = dic[null]; } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_SetIndexerNullKey() { var dic = new TrieDictionary(); dic[null] = 42; } [Test] public void Test_SetIndexerExistingKey() { var dic = new TrieDictionary(); dic.Add("key1", 33); dic["key1"] = 42; Assert.AreEqual(1, dic.Count); Assert.AreEqual(42, dic["key1"]); } [Test] public void Test_SetIndexerNonExistingKey() { var dic = new TrieDictionary(); dic["key1"] = 42; Assert.AreEqual(1, dic.Count); Assert.AreEqual(42, dic["key1"]); } [Test] public void Test_Remove() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); Assert.IsTrue(dic.Remove("key3")); Assert.IsFalse(dic.Remove("foo")); Assert.AreEqual(3, dic.Count); Assert.IsFalse(dic.ContainsKey("key3")); } [Test] public void Test_RemoveKeepsNoReference() { object value = new object(); WeakReference wrValue = new WeakReference(value); var dictionary = new TrieDictionary< object>(); dictionary.Add("test", value); dictionary.Remove("test"); value = null; GC.Collect(); Thread.Sleep(200); Assert.IsNull(wrValue.Target); } [Test] public void Test_Clear() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); dic.Clear(); Assert.AreEqual(0, dic.Count, "Clear method failed!"); Assert.IsFalse(dic.ContainsKey("key1")); Assert.IsFalse(dic.ContainsKey("key2")); Assert.IsFalse(dic.ContainsKey("key3")); Assert.IsFalse(dic.ContainsKey("key4")); } [Test] public void Test_ClearKeepsNoReference() { object value = new object(); WeakReference wrValue = new WeakReference(value); var dictionary = new TrieDictionary(); dictionary.Add("test", value); dictionary.Clear(); value = null; GC.Collect(); Thread.Sleep(200); Assert.IsNull(wrValue.Target); } [Test] public void Test_ContainsKey() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); bool contains = dic.ContainsKey("key4"); Assert.IsTrue(contains, "ContainsKey does not return correct value!"); contains = dic.ContainsKey("key5"); Assert.IsFalse(contains, "ContainsKey for non existant does not return correct value!"); } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_ContainsKeyNullKey() { var dic = new TrieDictionary(); dic.ContainsKey(null); } [Test] public void Test_TryGetValue() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); string value; bool retrieved = dic.TryGetValue("key4", out value); Assert.IsTrue(retrieved); Assert.AreEqual("value4", value, "TryGetValue does not return value!"); retrieved = dic.TryGetValue("key7", out value); Assert.IsFalse(retrieved); Assert.IsNull(value, "value for non existant value should be null!"); } [Test] public void Test_ForEach() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); int i = 0; foreach (KeyValuePair entry in dic) i++; Assert.AreEqual(4, i, "fail1: foreach entry failed!"); i = 0; foreach (KeyValuePair entry in ((IEnumerable)dic)) i++; Assert.AreEqual(4, i, "fail2: foreach entry failed!"); } [Test] public void Test_Keys() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); ICollection keys = ((IDictionary)dic).Keys; Assert.AreEqual(4, keys.Count); int i = 0; foreach (string key in keys) { i++; } Assert.AreEqual(4, i); } [Test] public void Test_Values() { var dic = new TrieDictionary(); dic.Add("key1", "value1"); dic.Add("key2", "value2"); dic.Add("key3", "value3"); dic.Add("key4", "value4"); ICollection values = ((IDictionary)dic).Values; Assert.AreEqual(4, values.Count); int i = 0; foreach (string key in values) { i++; } Assert.AreEqual(4, i); } [Test] public void Test_CopyTo() { var dic = new TrieDictionary(); dic.Add("foo", 42); dic.Add("bar", 12); var array = new KeyValuePair[2]; var collection = (ICollection>)dic; collection.CopyTo(array, 0); Assert.AreEqual(array[0].Key, "bar"); Assert.AreEqual(array[0].Value, 12); Assert.AreEqual(array[1].Key, "foo"); Assert.AreEqual(array[1].Value, 42); } [Test] public void Test_ICollectionOfKeyValuePairContains() { var dictionary = new TrieDictionary(); dictionary.Add("foo", 42); dictionary.Add("bar", 12); var collection = dictionary as ICollection>; Assert.AreEqual(2, collection.Count); Assert.IsFalse(collection.Contains(new KeyValuePair("baz", 13))); Assert.IsFalse(collection.Contains(new KeyValuePair("foo", 13))); Assert.IsTrue(collection.Contains(new KeyValuePair("foo", 42))); } [Test] public void Test_ICollectionOfKeyValuePairRemove() { var dictionary = new TrieDictionary(); dictionary.Add("foo", 42); dictionary.Add("bar", 12); var collection = dictionary as ICollection>; Assert.AreEqual(2, collection.Count); Assert.IsFalse(collection.Remove(new KeyValuePair("baz", 13))); Assert.IsFalse(collection.Remove(new KeyValuePair("foo", 13))); Assert.IsTrue(collection.Remove(new KeyValuePair("foo", 42))); Assert.AreEqual(12, dictionary["bar"]); Assert.IsFalse(dictionary.ContainsKey("foo")); } [Test] public void Test_ICollectionCopyToKeyValuePairArray() { var dictionary = new TrieDictionary(); dictionary.Add("foo", 42); var collection = dictionary as ICollection>; Assert.AreEqual(1, collection.Count); var pairs = new KeyValuePair[1]; collection.CopyTo(pairs, 0); Assert.AreEqual("foo", pairs[0].Key); Assert.AreEqual(42, pairs[0].Value); } #endregion #region TrieDictionary specific tests [Test] public void Test_ContainsPrefixMissingKey() { var dic = new TrieDictionary(); bool result = dic.ContainsPrefix("baz"); Assert.IsFalse(result); dic.Add("foobar", 42); result = dic.ContainsPrefix("baz"); Assert.IsFalse(result); } [Test] public void Test_ContainsPrefixExistingKey() { var dic = new TrieDictionary(); dic.Add("foo", 42); bool result = dic.ContainsPrefix("foo"); Assert.IsTrue(result); dic.Clear(); dic.Add("foobar", 33); result = dic.ContainsPrefix("foo"); Assert.IsTrue(result); } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_ContainsPrefixNullKey() { var dic = new TrieDictionary(); dic.Add("foobar", 42); dic.ContainsPrefix(null); } [Test] public void Test_FindPrefixMissingKey() { var dic = new TrieDictionary(); var result = dic.FindPrefix("baz"); Assert.IsFalse(result.Any()); dic.Add("foobar", 42); result = dic.FindPrefix("baz"); Assert.IsFalse(result.Any()); } [Test] public void Test_FindPrefixExistingKey() { var dic = new TrieDictionary(); dic.Add("foo", 42); var result = dic.FindPrefix("foo").ToArray(); Assert.AreEqual(1, result.Length); Assert.AreEqual("foo", result[0].Key); Assert.AreEqual(42, result[0].Value); dic.Clear(); dic.Add("foobar", 33); result = dic.FindPrefix("foo").ToArray(); Assert.AreEqual(1, result.Length); Assert.AreEqual("foobar", result[0].Key); Assert.AreEqual(33, result[0].Value); } [Test, ExpectedException(typeof(ArgumentNullException))] public void Test_FindPrefixNullKey() { var dic = new TrieDictionary(); dic.Add("foobar", 42); dic.FindPrefix(null); } #endregion } }