-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
99 lines (83 loc) · 2 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 (
"reflect"
"testing"
)
func prepareTrie() *Trie {
t := NewTrie()
t.Insert("hello", "hell", "hamster", "harris", "harrier", "harmore", "harmless", "he")
return t
}
func TestTrie_HasPrefix(t *testing.T) {
tree := prepareTrie()
if tree.HasPrefix("he") != true {
t.Fatal("tree.HasPrefix(\"he\") should be true")
}
if tree.HasPrefix("") != true {
t.Fatal("tree.HasPrefix(\"\") should be true")
}
if tree.HasPrefix("hem") != false {
t.Fatal("tree.HasPrefix(\"\") should be false")
}
}
func TestTrie_HasWord(t *testing.T) {
tree := prepareTrie()
if tree.HasWord("hello") != true {
t.Fatal("tree.HasWord(\"hello\") should be true")
}
if tree.HasWord("hamster") != true {
t.Fatal("tree.HasWord(\"hamster\") should be true")
}
if tree.HasWord("hem") != false {
t.Fatal("tree.HasWord(\"hem\") should be false")
}
if tree.HasWord("hel") != false {
t.Fatal("tree.HasWord(\"hel\") should be false")
}
if tree.HasWord("") != false {
t.Fatal("tree.HasWord(\"\") should be false")
}
}
func TestTrie_Insert(t *testing.T) {
tree := prepareTrie()
tree.Insert("inserted")
if tree.HasWord("inserted") != true {
t.Fatal("tree.HasWord(\"inserted\") should be true")
}
}
func TestTrie_WordsByPrefix(t *testing.T) {
tree := prepareTrie()
words := tree.WordsByPrefix("he")
expected := []string{"he", "hell", "hello"}
if !reflect.DeepEqual(words, expected) {
t.Fatalf("words should be equal to %v", expected)
}
words = tree.WordsByPrefix("gag")
if len(words) > 0 {
t.Fatal("There are no words with prefix gag")
}
}
func BenchmarkTrie_HasWord(b *testing.B) {
t := prepareTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
t.HasWord("harris")
}
}
func BenchmarkTrie_HasPrefix(b *testing.B) {
t := prepareTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
t.HasPrefix("harm")
}
}
func BenchmarkTrie_WordsByPrefix(b *testing.B) {
t := prepareTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
t.WordsByPrefix("ha")
}
}