-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
146 lines (138 loc) · 2.97 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
package gohoa
import (
"reflect"
"testing"
)
func TestTrie_TextSuggestions(t *testing.T) {
trie := InitTrie(TEXT_TRIE)
trie.Insert("apple")
trie.Insert("banana")
trie.Insert("orange")
trie.Insert("mackey")
trie.Insert("maclace")
trie.Insert("macbig")
trie.Insert("muadib")
trie.Insert("mallory")
tests := []struct {
name string
key string
wantResult []string
}{
{
name: "Test suggestions for 'a'",
key: "a",
wantResult: []string{"apple"},
},
{
name: "Test suggestions for 'b'",
key: "b",
wantResult: []string{"banana"},
},
{
name: "Test suggestions for 'o'",
key: "o",
wantResult: []string{"orange"},
},
{
name: "Test suggestions for 'ap'",
key: "ap",
wantResult: []string{"apple"},
},
{
name: "Test suggestions for 'ban'",
key: "ban",
wantResult: []string{"banana"},
},
{
name: "Test suggestions for 'ora'",
key: "ora",
wantResult: []string{"orange"},
},
{
name: "Test suggestions for 'xyz'",
key: "xyz",
wantResult: nil,
},
{
name: "Test suggestions for 'mac'",
key: "mac",
wantResult: []string{"macbig", "mackey", "maclace"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult := trie.Suggestions(tt.key)
if !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("Trie.Suggestions() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func TestTrie_NumberSuggestions(t *testing.T) {
trie := InitTrie(NUM_TRIE)
trie.Insert("123")
trie.Insert("456")
trie.Insert("789")
trie.Insert("5698")
trie.Insert("5611")
trie.Insert("5612")
tests := []struct {
name string
key string
wantResult []string
}{
{
name: "Test suggestions for '1'",
key: "1",
wantResult: []string{"123"},
},
{
name: "Test suggestions for '4'",
key: "4",
wantResult: []string{"456"},
},
{
name: "Test suggestions for '7'",
key: "7",
wantResult: []string{"789"},
},
{
name: "Test suggestions for '12'",
key: "12",
wantResult: []string{"123"},
},
{
name: "Test suggestions for '45'",
key: "45",
wantResult: []string{"456"},
},
{
name: "Test suggestions for '78'",
key: "78",
wantResult: []string{"789"},
},
{
name: "Test suggestions for '999'",
key: "999",
wantResult: nil,
},
{
name: "Test suggestions for '56'",
key: "56",
wantResult: []string{"5611", "5612", "5698"},
},
{
name: "Test suggestions for '561'",
key: "561",
wantResult: []string{"5611", "5612"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult := trie.Suggestions(tt.key)
if !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("Trie.Suggestions() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}