-
Notifications
You must be signed in to change notification settings - Fork 9
/
dawg_test.go
243 lines (202 loc) · 4.33 KB
/
dawg_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package dawg_test
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"testing"
"github.com/smhanov/dawg"
)
func testsWords() []string {
return []string{
"hello",
"jellow",
}
}
func createDawg(words []string) dawg.Finder {
dawg := dawg.New()
for _, word := range words {
dawg.Add(word)
}
return dawg.Finish()
}
func testDawg(t *testing.T, dawg dawg.Finder, words []string) {
added := dawg.NumAdded()
if added != len(words) {
t.Errorf("NumWords() returned %d, expected %d", added, len(words))
}
for i, word := range words {
index := dawg.IndexOf(word)
if index != i {
log.Panicf("Index returned should be %v, not %v", i, index)
}
wordFound, _ := dawg.AtIndex(i)
if wordFound != word {
log.Panicf("AtIndex(%d) should be %s, not %s", i, word, wordFound)
}
}
}
func runTest(t *testing.T, words []string) dawg.Finder {
finder := createDawg(words)
//finder.Print()
testDawg(t, finder, words)
// Now try the disk version
_, err := finder.Save("test.dawg")
if err != nil {
log.Panic(err)
}
//f, err := os.Open("test.dawg")
//dawg.DumpFile(f)
//f.Close()
saved, err := dawg.Load("test.dawg")
if err != nil {
log.Panic(err)
}
testDawg(t, saved, words)
return finder
}
func TestZeroLengthWord(t *testing.T) {
runTest(t, []string{
"",
})
}
func TestSingleEntry(t *testing.T) {
runTest(t, []string{
"a",
})
}
func TestHelloJello(t *testing.T) {
runTest(t, []string{
"hello",
"jello",
})
}
func Test5Words(t *testing.T) {
runTest(t, []string{
"",
"blip",
"cat",
"catnip",
"cats",
})
}
func testPrefixes(t *testing.T, words []string, word string, shouldbe []dawg.FindResult) {
finder := createDawg(words)
results := finder.FindAllPrefixesOf(word)
if len(results) != len(shouldbe) {
t.Errorf("Got %v but should be %v", results, shouldbe)
}
for i, result := range results {
if result != shouldbe[i] {
t.Errorf("Got %v but should be %v", results, shouldbe)
break
}
}
}
func TestPrefixes(t *testing.T) {
words := []string{
"",
"blip",
"cat",
"catnip",
"cats",
}
testPrefixes(t, words, "catsup", []dawg.FindResult{
{Word: "", Index: 0},
{Word: "cat", Index: 2},
{Word: "cats", Index: 4},
})
}
func readDictWords(t *testing.T) []string {
dict := "/usr/share/dict/words"
if _, err := os.Stat(dict); os.IsNotExist(err) {
t.Logf("Skipping full dictionary test; can't find %s", dict)
return nil
}
file, err := os.Open(dict)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var words []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
words = append(words, scanner.Text())
}
sort.Slice(words, func(i, j int) bool {
return words[i] < words[j]
})
var unique []string
unique = append(unique, words[0])
for i := 1; i < len(words); i++ {
if words[i] != words[i-1] {
unique = append(unique, words[i])
}
}
return unique
}
func TestFullDict(t *testing.T) {
words := readDictWords(t)
dawg := runTest(t, words)
t.Logf("DAWG has %v words, %v nodes, %v edges",
dawg.NumAdded(), dawg.NumNodes(), dawg.NumEdges())
}
func TestEnumerate(t *testing.T) {
words := []string{
"",
"blip",
"cat",
"catnip",
"cats",
"zzz",
}
finder := createDawg(words)
finder.Print()
total := 0
// test: when we get to catn, avoid descending
// when we get to cats, stop altogether.
finder.Enumerate(func(index int, word []rune, final bool) int {
if final {
total += 1
}
switch string(word) {
case "":
if index != 0 || !final {
t.Errorf("Bad index at %v %v %v", index, string(word), final)
}
case "blip":
if index != 1 || !final {
t.Errorf("Bad index at %v %v %v", index, string(word), final)
}
case "catn":
return dawg.Skip
case "catni":
fallthrough
case "catnip":
t.Error("Should not have got to catni")
case "cats":
return dawg.Stop
case "zzz":
t.Error("Stop had no effect.")
}
return dawg.Continue
})
if total != 4 {
t.Errorf("Did not enumerate expected number of words; only got %d", total)
}
}
func ExampleNew() {
dawg := dawg.New()
dawg.Add("blip") // index 0
dawg.Add("cat") // index 1
dawg.Add("catnip") // index 2
dawg.Add("cats") // index 3
finder := dawg.Finish()
for _, result := range finder.FindAllPrefixesOf("catsup") {
fmt.Printf("Found prefix %s, index %d\n", result.Word, result.Index)
}
// Output:
// Found prefix cat, index 1
// Found prefix cats, index 3
}