-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie_test.go
98 lines (89 loc) · 1.59 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
package LevenshteinTrie
import (
"bufio"
"io"
"os"
"testing"
)
var tree *TrieNode
func getfile() (io.ReadCloser, error) {
filename := "./w1_fixed.txt"
file, err := os.Open(filename)
return file, err
}
func TestInsert(t *testing.T) {
tree = NewTrie()
file, err := getfile()
defer file.Close()
if err != nil {
t.Fatal(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if err != nil {
break
}
tree.InsertText(line)
}
}
func TestPrefixSearch(t *testing.T) {
expected := []string{
"zygodactyl",
"zygoma",
"zygomatic",
"zygomaticus",
"zygomycetes",
"zygon",
"zygosity",
"zygote",
"zygote-specific",
"zygotes",
"zygourakis",
}
query := "zygo"
words := tree.Suffix(query)
for _, e := range expected {
if !contains(words, e) {
t.Errorf("Missing word: %s", e)
} else {
t.Logf("Found: %s\n", e)
}
}
}
func contains(words []string, word string) bool {
for _, w := range words {
if w == word {
return true
}
}
return false
}
func TestLevenshteinSearch(t *testing.T) {
expected := []struct {
query string
result []QueryResult
distance int
}{
{"accidens", []QueryResult{
{"accidens", 0},
{"accident", 1},
}, 1},
}
for _, e := range expected {
results := tree.Levenshtein(e.query, e.distance)
if !containsq(results, e.query) {
t.Errorf("Missing term: %s", e.query)
} else {
t.Logf("Looking for: %s, Found: %s\n", e.query, results)
}
}
}
func containsq(results []QueryResult, word string) bool {
for _, r := range results {
if r.Val == word {
return true
}
}
return false
}