示例#1
0
        public void Remove_FirstTimeReturnsTrueNextTimeReturnsFalse()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("z"));

            Assert.True(trie.Remove(new Word("z")));
            Assert.False(trie.Remove(new Word("z")));
            Assert.False(trie.Remove(new Word("z")));

            trie.Add(new Word("zz"));

            Assert.True(trie.Remove(new Word("zz")));
            Assert.False(trie.Remove(new Word("zz")));
            Assert.False(trie.Remove(new Word("zz")));

            trie.Add(new Word("zzx"));

            Assert.True(trie.Remove(new Word("zzx")));
            Assert.False(trie.Remove(new Word("zzx")));
            Assert.False(trie.Remove(new Word("zzx")));

            trie.Add(new Word("zwxy"));

            Assert.True(trie.Remove(new Word("zwxy")));
            Assert.False(trie.Remove(new Word("zwxy")));
            Assert.False(trie.Remove(new Word("zwxy")));

            trie.Add(new Word("vwxy"));

            Assert.True(trie.Remove(new Word("vwxy")));
            Assert.False(trie.Remove(new Word("vwxy")));
            Assert.False(trie.Remove(new Word("vwxy")));
        }
示例#2
0
        public void Match_FirstAndSecondLevelTwoMatches_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abc"));
            trie.Add(new Word("abd"));

            List <string> words = new List <string>();

            trie.Match(new Word("ab"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "ABC", "ABD" }, words.ToArray());
        }
示例#3
0
        public void Match_FirstLevelThreeDeepMatches_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("adefghijkl"));
            trie.Add(new Word("acdefghi"));
            trie.Add(new Word("abcdefg"));

            List <string> words = new List <string>();

            trie.Match(new Word("a"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "ABCDEFG", "ACDEFGHI", "ADEFGHIJKL" }, words.ToArray());
        }
示例#4
0
        public void Match_OneCharFirstLevelWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abc"));
            trie.Add(new Word("xbc"));
            trie.Add(new Word("zbc"));

            List <string> words = new List <string>();

            trie.Match(new Word("?"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "ABC", "XBC", "ZBC" }, words.ToArray());
        }
示例#5
0
        public void Match_ThreeCharsSecondLevelWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abc"));
            trie.Add(new Word("acc"));
            trie.Add(new Word("axa"));
            trie.Add(new Word("axc"));

            List <string> words = new List <string>();

            trie.Match(new Word("a?c"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "ABC", "ACC", "AXC" }, words.ToArray());
        }
示例#6
0
        public void MatchAnagram_Length2_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("aa"));
            trie.Add(new Word("aab"));
            trie.Add(new Word("bbb"));
            trie.Add(new Word("bc"));

            List <string> words = new List <string>();

            trie.MatchAnagram(new Word("aa"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "AA" }, words.ToArray());
        }
示例#7
0
        public void MatchAnagram_Length3OneWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("dfg"));
            trie.Add(new Word("f"));
            trie.Add(new Word("fed"));
            trie.Add(new Word("feds"));

            List <string> words = new List <string>();

            trie.MatchAnagram(new Word("d?f"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "DFG", "FED" }, words.ToArray());
        }
示例#8
0
        public void Match_TwoCharsFirstLevelWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abc"));
            trie.Add(new Word("bbb"));
            trie.Add(new Word("bbxx"));
            trie.Add(new Word("ccccc"));

            List <string> words = new List <string>();

            trie.Match(new Word("?b"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "ABC", "BBB", "BBXX" }, words.ToArray());
        }
示例#9
0
        public void MatchMaxLength_ThreeCharsAllWildMaxLength4_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("a"));
            trie.Add(new Word("bb"));
            trie.Add(new Word("ccc"));
            trie.Add(new Word("dddd"));
            trie.Add(new Word("eeeee"));

            List <string> words = new List <string>();

            trie.Match(new Word("???"), 4, w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "CCC", "DDDD" }, words.ToArray());
        }
示例#10
0
        public void Match_FirstCharNoMatch_DoesNothing()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("baaa"));

            int count = 0;

            trie.Match(new Word("aaaa"), w => ++ count);

            Assert.Equal(0, count);
        }
示例#11
0
        public void Match_FirstCharMatchButNoOtherMatch_DoesNothing()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abbb"));

            int count = 0;

            trie.Match(new Word("ac"), w => ++ count);

            Assert.Equal(0, count);
        }
示例#12
0
        public void Match_FirstLevelOneMatch_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("ab"));

            List <string> words = new List <string>();

            trie.Match(new Word("a"), w => words.Add(w.ToString()));

            Assert.Equal(new string[] { "AB" }, words.ToArray());
        }
示例#13
0
        public void Contains_ReturnsTrueIfFoundFalseOtherwise()
        {
            WordTrie trie = new WordTrie();

            Assert.False(trie.Contains(new Word("a")));

            trie.Add(new Word("a"));

            Assert.True(trie.Contains(new Word("a")));
            Assert.False(trie.Contains(new Word("ab")));

            trie.Add(new Word("abc"));

            Assert.False(trie.Contains(new Word("ab")));
            Assert.True(trie.Contains(new Word("abc")));

            trie.Remove(new Word("abc"));

            Assert.False(trie.Contains(new Word("ab")));
            Assert.False(trie.Contains(new Word("abc")));

            trie.Add(new Word("abcdefghijklmno"));

            Assert.False(trie.Contains(new Word("ab")));
            Assert.False(trie.Contains(new Word("abc")));
            Assert.False(trie.Contains(new Word("abcd")));
            Assert.False(trie.Contains(new Word("abcde")));
            Assert.False(trie.Contains(new Word("abcdef")));
            Assert.False(trie.Contains(new Word("abcdefg")));
            Assert.False(trie.Contains(new Word("abcdefgh")));
            Assert.False(trie.Contains(new Word("abcdefghi")));
            Assert.False(trie.Contains(new Word("abcdefghij")));
            Assert.False(trie.Contains(new Word("abcdefghijk")));
            Assert.False(trie.Contains(new Word("abcdefghijkl")));
            Assert.False(trie.Contains(new Word("abcdefghijklm")));
            Assert.False(trie.Contains(new Word("abcdefghijklmn")));
            Assert.True(trie.Contains(new Word("abcdefghijklmno")));
        }
示例#14
0
        public void MatchAnagram_MinLength1OneWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("a"));
            trie.Add(new Word("ab"));
            trie.Add(new Word("b"));
            trie.Add(new Word("bc"));
            trie.Add(new Word("bcd"));
            trie.Add(new Word("c"));
            trie.Add(new Word("cab"));
            trie.Add(new Word("d"));

            List <string> words = new List <string>();

            trie.MatchAnagram(new Word("ab?"), 1, w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "A", "A", "AB", "AB", "AB", "B", "B", "BC", "C", "CAB", "D" }, words.ToArray());
        }
示例#15
0
        public void MatchMaxLength_TwoCharsMaxLength3_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("a"));
            trie.Add(new Word("bb"));
            trie.Add(new Word("cc"));
            trie.Add(new Word("ccc"));
            trie.Add(new Word("ccd"));
            trie.Add(new Word("cccc"));
            trie.Add(new Word("dddd"));
            trie.Add(new Word("eeeee"));

            List <string> words = new List <string>();

            trie.Match(new Word("cc"), 3, w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "CC", "CCC", "CCD", }, words.ToArray());
        }
示例#16
0
        public void MatchAnagram_MinLength3ThreeWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("area"));
            trie.Add(new Word("best"));
            trie.Add(new Word("ear"));
            trie.Add(new Word("ears"));
            trie.Add(new Word("east"));
            trie.Add(new Word("eat"));
            trie.Add(new Word("raster"));
            trie.Add(new Word("rates"));
            trie.Add(new Word("steer"));
            trie.Add(new Word("tear"));
            trie.Add(new Word("treat"));

            List <string> words = new List <string>();

            trie.MatchAnagram(new Word("??ae?s"), 3, w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "AREA", "AREA", "AREA", "AREA", "AREA", "BEST", "BEST", "BEST", "EAR", "EAR", "EAR", "EAR", "EARS", "EARS", "EARS", "EARS", "EARS", "EARS", "EARS", "EAST", "EAST", "EAST", "EAST", "EAST", "EAST", "EAST", "EAT", "EAT", "EAT", "EAT", "RASTER", "RATES", "RATES", "RATES", "RATES", "STEER", "STEER", "TEAR", "TEAR", "TEAR", "TREAT" }, words.ToArray());
        }
示例#17
0
        public void MatchAnagram_Length6ThreeWild_InvokesOnAllMatches()
        {
            WordTrie trie = new WordTrie();

            trie.Add(new Word("abc"));
            trie.Add(new Word("bcd"));
            trie.Add(new Word("east"));
            trie.Add(new Word("easter"));
            trie.Add(new Word("eaters"));
            trie.Add(new Word("era"));
            trie.Add(new Word("erase"));
            trie.Add(new Word("eraser"));
            trie.Add(new Word("extra"));
            trie.Add(new Word("seat"));
            trie.Add(new Word("seater"));
            trie.Add(new Word("tease"));
            trie.Add(new Word("teaser"));

            List <string> words = new List <string>();

            trie.MatchAnagram(new Word("ee???a"), w => words.Add(w.ToString()));

            words.Sort();
            Assert.Equal(new string[] { "EASTER", "EATERS", "ERASER", "SEATER", "TEASER" }, words.ToArray());
        }
示例#18
0
        public void Add_FirstTimeReturnsTrueNextTimeReturnsFalse()
        {
            WordTrie trie = new WordTrie();

            Assert.True(trie.Add(new Word("z")));
            Assert.False(trie.Add(new Word("z")));
            Assert.False(trie.Add(new Word("z")));

            Assert.True(trie.Add(new Word("zz")));
            Assert.False(trie.Add(new Word("zz")));
            Assert.False(trie.Add(new Word("zz")));

            Assert.True(trie.Add(new Word("zzx")));
            Assert.False(trie.Add(new Word("zzx")));
            Assert.False(trie.Add(new Word("zzx")));

            Assert.True(trie.Add(new Word("azxy")));
            Assert.False(trie.Add(new Word("azxy")));
            Assert.False(trie.Add(new Word("azxy")));

            Assert.True(trie.Add(new Word("azx")));
            Assert.False(trie.Add(new Word("azx")));
            Assert.False(trie.Add(new Word("azx")));

            Assert.True(trie.Add(new Word("zxzxzxzxzxzxzxz")));
            Assert.False(trie.Add(new Word("zxzxzxzxzxzxzxz")));
            Assert.False(trie.Add(new Word("zxzxzxzxzxzxzxz")));
        }