-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes_test.go
317 lines (262 loc) · 7.51 KB
/
indexes_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package xim
import (
"fmt"
"reflect"
"testing"
"time"
)
func TestAddIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.Add("label1", "abc dあいbCh", "sample")
idx.Add("label2", "abc debch iJあdeN", "sample")
built := idx.MustBuild()
assertBuiltIndex(t, built, map[string]bool{
"label1 abc dあいbCh": true,
"label1 sample": true,
"label2 abc debch iJあdeN": true,
"label2 sample": true,
})
}
func TestAddBigrams(t *testing.T) {
idx := NewIndexes(nil)
idx.AddBigrams("label1", "abc dあいbCh")
idx.AddBigrams("label2", "abc debch iJあdeN")
expected := make(map[string]bool)
for _, s := range Bigrams("abc dあいbCh") {
expected["label1 "+s] = true
}
for _, s := range Bigrams("abc debch iJあdeN") {
expected["label2 "+s] = true
}
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestAddBiunigramsIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.AddBiunigrams("label1", "abc dあいbCh")
idx.AddBiunigrams("label2", "abc debch iJあdeN")
expected := make(map[string]bool)
for _, s := range Biunigrams("abc dあいbCh") {
expected["label1 "+s] = true
}
for _, s := range Biunigrams("abc debch iJあdeN") {
expected["label2 "+s] = true
}
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestAddPrefixesIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.AddPrefixes("label1", "abc dあいbCh")
idx.AddPrefixes("label2", "abc debch iJあdeN")
expected := make(map[string]bool)
for _, s := range Prefixes("abc dあいbCh") {
expected["label1 "+s] = true
}
for _, s := range Prefixes("abc debch iJあdeN") {
expected["label2 "+s] = true
}
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestAddSuffixesIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.AddSuffixes("label1", "abc dあいbCh")
idx.AddSuffixes("label2", "abc debch iJあdeN")
expected := make(map[string]bool)
for _, s := range Suffixes("abc dあいbCh") {
expected["label1 "+s] = true
}
for _, s := range Suffixes("abc debch iJあdeN") {
expected["label2 "+s] = true
}
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestAddSomethingIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.AddSomething("label1", []string{"abc dあいbCh", "abc debch iJあdeN"})
idx.AddSomething("label2", 123)
now := time.Now()
idx.AddSomething("label3", now)
built := idx.MustBuild()
assertBuiltIndex(t, built, map[string]bool{
"label1 abc dあいbCh": true,
"label1 abc debch iJあdeN": true,
"label2 123": true,
fmt.Sprintf("label3 %d", now.UnixNano()): true,
})
}
func TestAddAllIndex(t *testing.T) {
idx := NewIndexes(nil)
idx.Add("label1", "abc dあいbCh", "sample")
idx.AddBigrams("label2", "abc dあいbCh")
idx.AddBiunigrams("label3", "abc dあいbCh")
idx.AddPrefixes("label4", "abc dあいbCh")
idx.AddSomething("label5", []string{"abc dあいbCh", "AbcdeF"})
expected := make(map[string]bool)
// Add
expected["label1 abc dあいbCh"] = true
expected["label1 sample"] = true
// AddBigrams
for _, s := range Bigrams("abc dあいbCh") {
expected["label2 "+s] = true
}
// AddBiunigrams
for _, s := range Biunigrams("abc dあいbCh") {
expected["label3 "+s] = true
}
// AddPrefixes
for _, s := range Prefixes("abc dあいbCh") {
expected["label4 "+s] = true
}
// AddSomething
expected["label5 abc dあいbCh"] = true
expected["label5 AbcdeF"] = true
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestBuildIndex(t *testing.T) {
t.Run("Success", func(tr *testing.T) {
idx := NewIndexes(nil)
for i := 0; i < MaxIndexesSize; i++ {
idx.Add(fmt.Sprintf("label%d", i), "abc")
}
built, err := idx.Build()
if err != nil {
tr.Errorf("error = %s, wants = nil", err)
}
if built == nil {
tr.Error("built = nil, wants != nil")
}
})
t.Run("Config.CompositeIdxLabels > MaxCompositeIndexLabels", func(tr *testing.T) {
labels := make([]string, MaxCompositeIndexLabels+1)
for i := 0; i < len(labels); i++ {
labels[i] = string(rune('a' + i))
}
idx := NewIndexes(&Config{CompositeIdxLabels: labels})
if _, err := idx.Build(); err == nil {
tr.Error("error = nil, wants != nil")
}
})
t.Run("Number of Build() results > MaxIndexesSize", func(tr *testing.T) {
idx := NewIndexes(nil)
for i := 0; i < MaxIndexesSize+1; i++ {
idx.Add(fmt.Sprintf("label%d", i), "abc")
}
if _, err := idx.Build(); err == nil {
tr.Error("error = nil, wants != nil")
}
})
}
func TestMustBuildIndex(t *testing.T) {
t.Run("Success", func(tr *testing.T) {
defer func() {
if rec := recover(); rec != nil {
tr.Errorf("expected: not panic, was: panic = [%v]\n", rec)
}
}()
idx := NewIndexes(nil)
for i := 0; i < MaxIndexesSize; i++ {
idx.Add(fmt.Sprintf("label%d", i), "abc")
}
built := idx.MustBuild()
if built == nil {
tr.Error("built = nil, wants != nil")
}
})
t.Run("Config.CompositeIdxLabels > MaxCompositeIndexLabels", func(tr *testing.T) {
defer func() {
if r := recover(); r == nil {
tr.Error("expected: panic, was: not panic\n")
}
}()
labels := make([]string, MaxCompositeIndexLabels+1)
for i := 0; i < len(labels); i++ {
labels[i] = string(rune('a' + i))
}
idx := NewIndexes(&Config{CompositeIdxLabels: labels})
idx.MustBuild()
})
t.Run("Number of MustBuild() results > MaxIndexesSize", func(tr *testing.T) {
defer func() {
if r := recover(); r == nil {
tr.Error("expected: panic, was: not panic\n")
}
}()
idx := NewIndexes(nil)
for i := 0; i < MaxIndexesSize+1; i++ {
idx.Add(fmt.Sprintf("label%d", i), "abc")
}
idx.MustBuild()
})
}
func TestIndexConfigCompositeIdxLabels(t *testing.T) {
idx := NewIndexes(&Config{CompositeIdxLabels: []string{"label1", "label2", "label3"}})
idx.Add("label1", "a")
idx.Add("label2", "b")
idx.Add("label3", "c")
idx.Add("label4", "d")
built := idx.MustBuild()
// c b a
// -----
//3 0 1 1
//5 1 0 1
//6 1 1 0
//7 1 1 1
assertBuiltIndex(t, built, map[string]bool{
"label1 a": true,
"label2 b": true,
"label3 c": true,
"label4 d": true,
"3 a;b": true,
"5 a;c": true,
"6 b;c": true,
"7 a;b;c": true,
})
}
func TestIndexConfigIgnoreCase(t *testing.T) {
idx := NewIndexes(&Config{IgnoreCase: true})
idx.Add("label1", "abc dあいbCh", "saMPle")
idx.AddBigrams("label2", "abc dあいbCh")
idx.AddBiunigrams("label3", "abc dあいbCh")
idx.AddPrefixes("label4", "abc dあいbCh")
idx.AddSomething("label5", []string{"abc dあいbCh", "AbcdeF"})
expected := make(map[string]bool)
// Add
expected["label1 abc dあいbch"] = true
expected["label1 sample"] = true
// AddBigrams
for _, s := range Bigrams("abc dあいbch") {
expected["label2 "+s] = true
}
// AddBiunigrams
for _, s := range Biunigrams("abc dあいbch") {
expected["label3 "+s] = true
}
// AddPrefixes
for _, s := range Prefixes("abc dあいbch") {
expected["label4 "+s] = true
}
// AddSomething
expected["label5 abc dあいbch"] = true
expected["label5 abcdef"] = true
built := idx.MustBuild()
assertBuiltIndex(t, built, expected)
}
func TestIndexConfigSaveNoFiltersIndex(t *testing.T) {
idx := NewIndexes(&Config{SaveNoFiltersIndex: true})
idx.Add("label1", "a")
built := idx.MustBuild()
assertBuiltIndex(t, built, map[string]bool{
"label1 a": true,
IndexNoFilters: true,
})
}
func assertBuiltIndex(t *testing.T, actual, expected map[string]bool) {
t.Helper()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("unexpected, actual: `%v`, expected: `%v`", actual, expected)
}
}