Exemple #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);
        }
Exemple #2
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);
                }
            }
        }