-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
184 lines (144 loc) · 4.59 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gotri
import (
"testing"
)
var testData = []struct {
word string
value string
expected string
}{
{"ants", "পিপীলিকা", "পিপীলিকা"},
{"acting", "to behave in the stated way", "to behave in the stated way"},
{"abc", "ad", "ad"},
{"and", "এবং", "এবং"},
{"abandon", "ত্যাগ করা", "ত্যাগ করা"},
{"abandoned", "পরিত্যক্ত", "পরিত্যক্ত"},
{"abyss", "অতল গহ্বর", "অতল গহ্বর"},
{"babble", "", ""},
{"baboon", "", ""},
{"babbl", "", ""},
{"book", "", ""},
{"books", "", ""},
{"café", "a restaurant where simple and usually quite cheap meals are served", "a restaurant where simple and usually quite cheap meals are served"},
{"get up", "to stand up", "to stand up"}, //test with space character
//TODO add more test data
}
var testSearchData = []struct {
word string
value string
expected string
found bool
}{
{"ants", "পিপীলিকা", "পিপীলিকা", true},
{"acting", "to behave in the stated way", "to behave in the stated way", true},
{"abc", "ad", "ad", true},
{"and", "এবং", "এবং", true},
{"abandon", "ত্যাগ করা", "ত্যাগ করা", true},
{"abandoned", "পরিত্যক্ত", "পরিত্যক্ত", true},
{"abyss", "অতল গহ্বর", "অতল গহ্বর", true},
{"babble", "", "", true},
{"baboon", "", "", true},
{"babbl", "", "", true},
{"book", "", "", true},
{"books", "", "", true},
{"okkk", "", "", false},
{"café", "a restaurant where simple and usually quite cheap meals are served", "a restaurant where simple and usually quite cheap meals are served", true},
}
var testSuggestionData = []struct {
searchCharacter string
total int
expectedWordList []string
expectedCount int
}{
{"café", 1, []string{"café"}, 1},
{"okkk", 1, []string{}, 0}, //test for item that doesn't exist in the tree
{"a", 1, []string{"abandon"}, 1},
{"a", 2, []string{"abandon", "abandoned"}, 2},
{"b", 2, []string{"babbl", "babble"}, 2},
{"a", 4, []string{"abandon", "abandoned", "abc", "abyss"}, 4},
{"a", 5, []string{"abandon", "abandoned", "abc", "abyss", "acting"}, 5},
{"book", 2, []string{"book", "books"}, 2}, //test for a complete word in the suggestion
{"books", 1, []string{"books"}, 1}, //test for only one word in the tree
{"get", 1, []string{"get up"}, 1},
}
var tr = New()
func TestAdd(t *testing.T) {
for _, v := range testData {
tr.Add(v.word, v.value)
val, ok := tr.Search(v.word)
if !ok {
t.Errorf("Expected %v, got %v", true, ok)
}
if v.expected != val {
t.Errorf("Expected %v, got %v", v.expected, val)
}
}
}
func TestSearch(t *testing.T) {
for _, v := range testSearchData {
val, ok := tr.Search(v.word)
if v.found != ok {
t.Errorf("Expected %v, got %v", v.found, ok)
}
if v.expected != val {
t.Errorf("Expected %v, got %v", v.expected, val)
}
}
//test for empty/nil tree
var tr *Trie
v, ok := tr.Search("keyword")
if ok != false {
t.Errorf("Expected %v got %v", false, ok)
}
if v != "" {
t.Errorf("Expected %v got %v", "", v)
}
}
func TestSuggestion(t *testing.T) {
for _, val := range testSuggestionData {
// //move to next position node from the searching character
for i := 0; i < len(val.searchCharacter); i++ {
index := val.searchCharacter[i]
if tr.Children[index] == nil {
return
}
tr = tr.Children[index]
}
wordList := []string{}
_, resultArr := Suggestion(tr, val.searchCharacter, wordList, val.total)
if len(resultArr) != val.expectedCount {
// fmt.Println(val.expectedWordList, resultArr)
t.Errorf("Expected %v, got %v", val.expectedCount, len(resultArr))
}
for k, v := range resultArr {
if v != val.expectedWordList[k] {
t.Errorf("Expected %v, got %v", val.expectedWordList, resultArr)
}
}
}
}
func TestGetSuggestion(t *testing.T) {
tr = New()
//add the data to the tree and start from root node
for _, v := range testData {
tr.Add(v.word, v.value)
val, ok := tr.Search(v.word)
if !ok {
t.Errorf("Expected %v, got %v", true, ok)
}
if v.expected != val {
t.Errorf("Expected %v, got %v", v.expected, val)
}
}
for _, val := range testSuggestionData {
resultArr := tr.GetSuggestion(val.searchCharacter, val.total)
if len(resultArr) != val.expectedCount {
t.Errorf("Expected %v, got %v", val.expectedCount, len(resultArr))
}
for k, v := range resultArr {
if v != val.expectedWordList[k] {
t.Errorf("Expected %v, got %v", val.expectedWordList, resultArr)
}
}
}
}