-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
79 lines (65 loc) · 1.57 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
package algo
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewTrie(t *testing.T) {
newTrie := NewTrie()
if newTrie.root.children == nil {
t.Fatalf("exp non-nil children map")
}
if len(newTrie.root.children) > 0 {
t.Fatalf("exp empty initialized map, got %d", len(newTrie.root.children))
}
}
func TestTrie_Insert(t *testing.T) {
newTrie := NewTrie()
newTrie.Insert("cat")
if len(newTrie.root.children) != 1 {
t.Fatalf("exp map len %d, got %d", 1, len(newTrie.root.children))
}
newTrie.Insert("bat")
if len(newTrie.root.children) != 2 {
t.Fatalf("exp map len %d, got %d", 2, len(newTrie.root.children))
}
}
func TestTrie_AutoComplete(t *testing.T) {
newTrie := NewTrie()
newTrie.Insert("cat")
newTrie.Insert("cab")
newTrie.Insert("bat")
exp := []string{"at", "ab"}
res := newTrie.AutoComplete("c")
if diff := cmp.Diff(exp, res); diff != "" {
t.Errorf("exp %v, got %v; diff: %v", exp, res, diff)
}
}
func TestTrie_Autocorrect(t *testing.T) {
newTrie := NewTrie()
newTrie.Insert("ace")
newTrie.Insert("act")
newTrie.Insert("bad")
newTrie.Insert("bake")
newTrie.Insert("bat")
newTrie.Insert("batter")
newTrie.Insert("cat")
newTrie.Insert("catnip")
newTrie.Insert("catnap")
testCases := []struct {
input string
exp string
}{
{"catnar", "catnap"},
{"catnip", "catnip"},
{"caxasfdij", "cat"},
{"bakp", "bake"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s", tc.input), func(t *testing.T) {
if res := newTrie.Autocorrect(tc.input); res != tc.exp {
t.Errorf("exp %s, got %s", tc.exp, res)
}
})
}
}