-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_test.go
80 lines (69 loc) · 1.24 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
package purify
import (
"testing"
)
func TestAddWord(t *testing.T) {
tests := []struct {
in string
want error
}{
{"smart", nil},
{"mandarin", nil},
{"mand", nil},
{"artquake", nil},
{"smart", ErrDuplicateWord},
}
trie := NewTrie()
for _, tt := range tests {
got := trie.AddWord(tt.in)
if got != tt.want {
t.Errorf("AddWord(...): got %s, wanted %s", got, tt.want)
}
}
}
func TestFindWord(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"smart", false},
{"mandarin", true},
{"mand", false},
{"artquake", false},
{"smart", true},
}
trie := NewTrie()
trie.AddWord("mandarin")
for i, tt := range tests {
if i == 4 {
trie.AddWord("smart")
}
got := trie.Find(tt.in)
if got != tt.want {
t.Errorf("FindWord(...): got %v, wanted %v", got, tt.want)
}
}
}
func BenchmarkAddWord(b *testing.B) {
trie := NewTrie()
for i := 0; i < b.N; i++ {
trie.AddWord("word")
}
}
func BenchmarkFind_NotExist(b *testing.B) {
trie := NewTrie()
for i := 0; i < b.N; i++ {
trie.Find("word")
}
}
func BenchmarkFind_ItExists(b *testing.B) {
trie := NewTrie()
b.StopTimer()
for i := 0; i < b.N; i++ {
trie.AddWord("word")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
trie.Find("word")
}
}