Beispiel #1
0
        public void BasicEnumeratorTests()
        {
            CPStringTrie <string> .Enumerator e, e0;

            CPStringTrie <string> emptyTrie = new CPStringTrie <string>();

            Assert.IsNotNull(emptyTrie.FindAtLeast(""));
            Assert.That(!emptyTrie.FindAtLeast("foo").IsValid);
            Assert.IsNull(emptyTrie.FindExact(""));
            Assert.That(!emptyTrie.Find("?", out e) && e != null && !e.IsValid);

            CPStringTrie <string> trie = new CPStringTrie <string>();

            foreach (string key in BasicTestStrings())
            {
                trie.Add(key, key);
            }

            Assert.That(trie.Find("An", out e));
            Assert.That(!trie.Find("Am", out e0));
            Assert.AreEqual(e0.CurrentKey, e.CurrentValue);

            Assert.That(e.MovePrev());
            Assert.AreEqual("A", e.CurrentKey);
            Assert.AreEqual("A", e.CurrentValue);
            Assert.AreEqual("An", e0.CurrentKey);
            Assert.That(e0.MoveNext());
            Assert.AreEqual("Ant", e0.CurrentKey);

            Assert.That(!trie.Find("2Noté", out e));
            Assert.AreEqual("2Noté thé Ŭnicodé.", e.CurrentKey);
            Assert.IsNull(trie.FindExact("2Noté"));
            Assert.IsNotNull(trie.FindExact("2An"));
            Assert.AreEqual("16single", trie.FindAtLeast("16simgle").CurrentKey);

            Assert.That(!trie.Find("32zzz", out e));
            Assert.AreEqual("4", e.CurrentKey);
            Assert.That(e.MoveNext());
            Assert.AreEqual("4 ", e.CurrentKey);
            Assert.That(e.MovePrev());
            Assert.AreEqual("4", e.CurrentKey);
            Assert.That(e.MovePrev());
            Assert.AreEqual("32z", e.CurrentKey);

            Assert.That(!trie.Find("zzz", out e));
            Assert.That(!e.IsValid);
            Assert.That(e.MovePrev());
            Assert.AreEqual("z", e.CurrentValue);

            TestFind(trie, 500);
        }
Beispiel #2
0
        public void BasicTests()
        {
            CPStringTrie <string> trie = new CPStringTrie <string>();
            int count = 0;

            List <string> testStrings = new List <string>(BasicTestStrings());

            // Test insertion
            foreach (string key in testStrings)
            {
                trie.Add(key, key);
                Assert.AreEqual(++count, trie.Count);
            }

            Assert.That(trie.Contains(new KeyValuePair <string, string>("a", "a")));
            Assert.That(!trie.Contains(new KeyValuePair <string, string>("a", "")));

            // Test forward enumeration
            count = 0;
            string last = null;

            foreach (KeyValuePair <string, string> p in trie)
            {
                Assert.That(p.Key == p.Value);
                Assert.That(last == null || string.Compare(last, p.Value, StringComparison.Ordinal) < 0);
                last = p.Value;
                count++;
            }
            Assert.AreEqual(count, trie.Count);

            // Test deletion
            Debug.Assert(testStrings.Count % 16 == 0);
            for (int i = 0; i < testStrings.Count; i++)
            {
                // Remove keys in a different order than they were added
                string key = testStrings[i ^ 0xF];
                Assert.That(trie.Contains(new KeyValuePair <string, string>(key, key)));
                Assert.That(trie[key] == key);
                trie[key] = "!";
                Assert.That(trie[key] == "!");
                Assert.That(trie.Remove(key));
                Assert.That(!trie.ContainsKey(key));
            }
            Assert.AreEqual(0, trie.Count);
        }
Beispiel #3
0
        private void TestFind(CPStringTrie <string> trie, int iterations)
        {
            Random rand = new Random();

            CPStringTrie <string> .Enumerator e;

            // Search for random character pairs and make sure the returned
            // enumerator points to the right place.
            for (int i = 0; i < iterations; i++)
            {
                char   c0 = (char)rand.Next((int)' ', (int)'z' + 1);
                char   c1 = (char)rand.Next((int)' ', (int)'z' + 1);
                string s  = c0.ToString() + c1.ToString();

                bool   found  = trie.Find(s, out e);
                string curKey = e.IsValid ? e.CurrentKey : null;

                bool havePrev = e.MovePrev();
                if (found)
                {
                    Assert.AreEqual(s, curKey);
                }
                else if (curKey != null)
                {
                    Assert.That(string.CompareOrdinal(s, curKey) < 0);
                }
                else
                {
                    Assert.That(havePrev);
                }

                Assert.That(havePrev == e.IsValid);
                if (havePrev)
                {
                    curKey = e.CurrentKey;
                    Assert.That(string.CompareOrdinal(s, curKey) > 0);
                }
            }
        }
Beispiel #4
0
 public CPStringTrie(CPStringTrie <TValue> clone) : base(clone)
 {
 }