-
Notifications
You must be signed in to change notification settings - Fork 35
/
builder_test.go
256 lines (227 loc) · 5.02 KB
/
builder_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
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2017 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vellum
import (
"bufio"
"io/ioutil"
"math/rand"
"os"
"sort"
"testing"
)
func init() {
thousandTestWords, _ = loadWords("data/words-1000.txt")
}
// this simple test case only has a shared final state
// it also tests out of order insert
func TestBuilderSimple(t *testing.T) {
b, err := New(ioutil.Discard, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
// add our first string
err = b.Insert([]byte("jul"), 0)
if err != nil {
t.Errorf("got error inserting string: %v", err)
}
// expect len to be 1
if b.len != 1 {
t.Errorf("expected node count to be 1, got %v", b.len)
}
// try to add a value out of order (not allowed)
err = b.Insert([]byte("abc"), 0)
if err == nil {
t.Errorf("expected err, got nil")
}
// add a second string
err = b.Insert([]byte("mar"), 0)
if err != nil {
t.Errorf("got error inserting string: %v", err)
}
// expect len to grow by 1
if b.len != 2 {
t.Errorf("expected node count to be 2, got %v", b.len)
}
// now close the builder
err = b.Close()
if err != nil {
t.Errorf("got error closing set builder: %v", err)
}
}
func TestBuilderSharedPrefix(t *testing.T) {
b, err := New(ioutil.Discard, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
// add our first string
err = b.Insert([]byte("car"), 0)
if err != nil {
t.Errorf("got error inserting string: %v", err)
}
// expect len to be 1
if b.len != 1 {
t.Errorf("expected node count to be 1, got %v", b.len)
}
// add a second string
err = b.Insert([]byte("cat"), 0)
if err != nil {
t.Errorf("got error inserting string: %v", err)
}
// expect len to be 2
if b.len != 2 {
t.Errorf("expected node count to be 2, got %v", b.len)
}
// now close the builder
err = b.Close()
if err != nil {
t.Errorf("got error closing set builder: %v", err)
}
}
func randomValues(list []string) []uint64 {
rv := make([]uint64, len(list))
for i := range list {
rv[i] = uint64(rand.Uint64())
}
return rv
}
func insertStrings(b *Builder, list []string, vals []uint64) error {
for i, item := range list {
err := b.Insert([]byte(item), vals[i])
if err != nil {
return err
}
}
return nil
}
var smallSample = map[string]uint64{
"mon": 2,
"tues": 3,
"thurs": 5,
"tye": 99,
}
func insertStringMap(b *Builder, m map[string]uint64) error {
// make list of keys
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
// sort it
sort.Strings(keys)
// insert in sorted order
for _, k := range keys {
err := b.Insert([]byte(k), m[k])
if err != nil {
return err
}
}
return nil
}
func TestBuilderNodeEquiv(t *testing.T) {
tests := []struct {
desc string
a *builderNode
b *builderNode
want bool
}{
{
"both states final",
&builderNode{
final: true,
},
&builderNode{
final: true,
},
true,
},
{
"both states final, different final val",
&builderNode{
final: true,
finalOutput: 7,
},
&builderNode{
final: true,
finalOutput: 9,
},
false,
},
{
"both states final, same transitions, but different trans val",
&builderNode{
final: true,
trans: []transition{
{in: 'a', out: 7},
},
},
&builderNode{
final: true,
trans: []transition{
{in: 'a', out: 9},
},
},
false,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
got := test.a.equiv(test.b)
if got != test.want {
t.Errorf("wanted: %t, got: %t", test.want, got)
}
})
}
}
func loadWords(path string) ([]string, error) {
var rv []string
file, err := os.Open(path)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
word := append([]byte(nil), scanner.Bytes()...)
rv = append(rv, string(word))
if err != nil {
return nil, err
}
}
if err = scanner.Err(); err != nil {
return nil, err
}
err = file.Close()
if err != nil {
return nil, err
}
return rv, nil
}
var thousandTestWords []string
func BenchmarkBuilder(b *testing.B) {
dataset := thousandTestWords
randomThousandVals := randomValues(dataset)
b.ResetTimer()
for i := 0; i < b.N; i++ {
builder, err := New(ioutil.Discard, nil)
if err != nil {
b.Fatalf("error creating builder: %v", err)
}
err = insertStrings(builder, dataset, randomThousandVals)
if err != nil {
b.Fatalf("error inserting thousand words: %v", err)
}
err = builder.Close()
if err != nil {
b.Fatalf("error closing builder: %v", err)
}
}
}