-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
67 lines (47 loc) · 1.22 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
package autocomplete_test
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/thenativeweb/codingcircle/autocomplete"
)
func getTrie() *autocomplete.Trie {
var t = autocomplete.NewTrie()
t.Add("the")
t.Add("native")
t.Add("web")
t.Add("website")
return t
}
func TestTrie(t *testing.T) {
t.Run("prefix exists", func(t *testing.T) {
words := getTrie()
result := words.Search("t")
sort.Strings(result)
assert.Equal(t, []string{"the"}, result)
})
t.Run("prefix exists with multiple words", func(t *testing.T) {
words := getTrie()
result := words.Search("w")
sort.Strings(result)
assert.Equal(t, []string{"web", "website"}, result)
})
t.Run("prefix does not exist", func(t *testing.T) {
words := getTrie()
result := words.Search("x")
sort.Strings(result)
assert.Equal(t, []string{}, result)
})
t.Run("prefix is a word", func(t *testing.T) {
words := getTrie()
result := words.Search("native")
sort.Strings(result)
assert.Equal(t, []string{"native"}, result)
})
t.Run("prefix is empty", func(t *testing.T) {
words := getTrie()
result := words.Search("")
sort.Strings(result)
assert.Equal(t, []string{"native", "the", "web", "website"}, result)
})
}