-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
99 lines (88 loc) · 2.05 KB
/
trie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package trie
import "testing"
func TestTrie(t *testing.T) {
words := []string{"apple", "app", "apricot", "banana"}
trie := Constructor()
for _, word := range words {
trie.Insert(word)
}
testCase := []struct {
word string
want bool
}{
{"apple", true},
{"app", true},
{"apricot", true},
{"banana", true},
{"ap", false},
{"application", false},
{"orange", false},
}
for _, tc := range testCase {
t.Run("Search for ", func(t *testing.T) {
got := trie.Search(tc.word)
if got != tc.want {
t.Errorf("Search(%q) = %v, want %v", tc.word, got, tc.want)
}
})
}
testPrefix := []struct {
prefix string
want bool
}{
{"app", true},
{"apr", true},
{"ban", true},
{"bana", true},
{"or", false},
{"ba", true},
{"c", false},
}
for _, tc := range testPrefix {
t.Run("Stars with ", func(t *testing.T) {
got := trie.StartsWith(tc.prefix)
if got != tc.want {
t.Errorf("got %v, want %v", got, tc.want)
}
})
}
}
// so a trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
// so it intialize the trie Constructor
// and then inserts all the word inside it
// then we return true if the word is in the trie, nd false otherwise
func TestEndOfDictionary(t *testing.T) {
dict := Init()
testCases := []struct {
addWords []string
searchWords map[string]bool
}{
{
addWords: []string{"bad", "dad", "mad"},
searchWords: map[string]bool{
"pad": false,
".ad": true,
"b..": true,
"b.d": true,
"b.": false,
"...": true,
"m.d": true,
"mad": true,
"ma.": true,
},
},
}
for _, tc := range testCases {
for _, word := range tc.addWords {
dict.AddWord(word)
}
for word, expected := range tc.searchWords {
t.Run("Search_"+word, func(t *testing.T) {
result := dict.Search(word)
if result != expected {
t.Errorf("Search(%q) = %v, want %v", word, result, expected)
}
})
}
}
}