-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahocorasick_test.go
425 lines (385 loc) · 11 KB
/
ahocorasick_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package ahocorasick
import (
"fmt"
"testing"
)
func TestStateSingleState(t *testing.T) {
s := newState(0)
if 1 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateSingleStateSparse(t *testing.T) {
s := newState(10)
if 1 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtend(t *testing.T) {
s := newState(0)
s2 := s.extend(23)
if s == s2 || s2 == nil || 2 != s.size() {
t.Logf("edge = %v\n", s.edges.get(23))
t.Logf("keys = %v\n", s.edges.keys())
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendSparse(t *testing.T) {
s := newState(10)
s2 := s.extend(23)
if s == s2 || s2 == nil || 2 != s.size() {
t.Logf("edge = %v\n", s.edges.get(23))
t.Logf("keys = %v\n", s.edges.keys())
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendAll(t *testing.T) {
s := newState(0)
s2 := s.extendAll([]byte("hello world!"))
if s == s2 || s2 == nil || 13 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendAllTwice(t *testing.T) {
s := newState(0)
s2 := s.extendAll([]byte("hello world!"))
s3 := s.extendAll([]byte("hello world!"))
if s == s2 || s2 == nil || s2 != s3 || 13 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendAllTwiceSparse(t *testing.T) {
s := newState(10)
s2 := s.extendAll([]byte("hello world!"))
s3 := s.extendAll([]byte("hello world!"))
if s == s2 || s2 == nil || s2 != s3 || 13 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendMany(t *testing.T) {
s := newState(0)
for i := 0; i < 256; i++ {
s.extend(byte(i))
}
if 257 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestStateExtendManySparse(t *testing.T) {
s := newState(10)
for i := 0; i < 256; i++ {
s.extend(byte(i))
}
if 257 != s.size() {
t.Logf("size = %d\n", s.size())
t.Fail()
}
}
func TestTree1(t *testing.T) {
tree := New()
tree.Add("hello")
tree.Add("hi")
tree.prepare()
s0 := tree.root
s1 := s0.edges.get('h')
s2 := s1.edges.get('e')
s3 := s2.edges.get('l')
s4 := s3.edges.get('l')
s5 := s4.edges.get('o')
s6 := s1.edges.get('i')
if s6 == nil {
t.Log("s6 is nil")
t.Fail()
}
if s0 != s1.fail || s0 != s2.fail || s0 != s3.fail || s0 != s4.fail || s0 != s5.fail || s0 != s6.fail {
t.Logf("s0 %v", s0)
t.Logf("s1 fail %v", s1.fail)
t.Logf("s2 fail %v", s2.fail)
t.Logf("s3 fail %v", s3.fail)
t.Logf("s4 fail %v", s4.fail)
t.Logf("s5 fail %v", s5.fail)
t.Logf("s6 fail %v", s6.fail)
t.Fail()
}
if l0 := s0.out.Len(); 0 != l0 {
t.Logf("s0 out len %v", l0)
t.Fail()
}
if l1 := s1.out.Len(); 0 != l1 {
t.Logf("s1 out len %v", l1)
t.Fail()
}
if l2 := s2.out.Len(); 0 != l2 {
t.Logf("s2 out len %v", l2)
t.Fail()
}
if l3 := s3.out.Len(); 0 != l3 {
t.Logf("s3 out len %v", l3)
t.Fail()
}
if l4 := s4.out.Len(); 0 != l4 {
t.Logf("s4 out len %v", l4)
t.Fail()
}
if l5 := s5.out.Len(); 1 != l5 {
t.Logf("s5 out len %v", l5)
t.Fail()
}
if l6 := s6.out.Len(); 1 != l6 {
t.Logf("s out len %v", l6)
t.Fail()
}
}
func TestTree2(t *testing.T) {
tree := New()
tree.Add("he")
tree.Add("she")
tree.Add("his")
tree.Add("hers")
if s := tree.root.size(); 10 != s {
t.Logf("tree size %f", s)
t.Fail()
}
tree.prepare()
s0 := tree.root
s1 := s0.edges.get('h')
s2 := s1.edges.get('e')
s3 := s0.edges.get('s')
s4 := s3.edges.get('h')
s5 := s4.edges.get('e')
s6 := s1.edges.get('i')
s7 := s6.edges.get('s')
s8 := s2.edges.get('r')
s9 := s8.edges.get('s')
if s6 == nil {
t.Log("s6 is nil")
t.Fail()
}
if s0 != s1.fail || s0 != s2.fail || s0 != s3.fail || s0 != s6.fail || s0 != s8.fail ||
s1 != s4.fail || s2 != s5.fail || s3 != s7.fail || s3 != s9.fail {
t.Logf("s0 %v", s0)
t.Logf("s1 %v", s1)
t.Logf("s2 %v", s2)
t.Logf("s3 %v", s3)
t.Logf("s1 fail %v", s1.fail)
t.Logf("s2 fail %v", s2.fail)
t.Logf("s3 fail %v", s3.fail)
t.Logf("s4 fail %v", s4.fail)
t.Logf("s5 fail %v", s5.fail)
t.Logf("s6 fail %v", s6.fail)
t.Logf("s7 fail %v", s7.fail)
t.Logf("s8 fail %v", s8.fail)
t.Logf("s9 fail %v", s9.fail)
t.Fail()
}
if l0 := s0.out.Len(); 0 != l0 {
t.Logf("s0 out len %v", l0)
t.Fail()
}
if l1 := s1.out.Len(); 0 != l1 {
t.Logf("s1 out len %v", l1)
t.Fail()
}
if l2 := s2.out.Len(); 1 != l2 {
t.Logf("s2 out len %v", l2)
t.Fail()
}
if l3 := s3.out.Len(); 0 != l3 {
t.Logf("s3 out len %v", l3)
t.Fail()
}
if l4 := s4.out.Len(); 0 != l4 {
t.Logf("s4 out len %v", l4)
t.Fail()
}
if l5 := s5.out.Len(); 2 != l5 {
t.Logf("s5 out len %v", l5)
t.Fail()
}
if l6 := s6.out.Len(); 0 != l6 {
t.Logf("s6 out len %v", l6)
t.Fail()
}
if l7 := s7.out.Len(); 1 != l7 {
t.Logf("s7 out len %v", l7)
t.Fail()
}
if l8 := s8.out.Len(); 0 != l8 {
t.Logf("s8 out len %v", l8)
t.Fail()
}
if l9 := s9.out.Len(); 1 != l9 {
t.Logf("s9 out len %v", l9)
t.Fail()
}
}
func TestSearchSingle(t *testing.T) {
tree := New()
tree.Add("apple")
res := tree.startSearch("washington cut the apple tree")
if l := res.lastMatchedState.out.Len(); 1 != l {
t.Logf("search result out len %v", l)
t.Fail()
}
if out := res.out().Front().Value.(string); "apple" != out {
t.Logf("search result out %v", out)
t.Fail()
}
if 24 != res.lastIndex {
t.Logf("search result last index %v", res.lastIndex)
t.Fail()
}
if cont := res.continueSearch(); nil != cont {
t.Logf("search continued %v", cont)
t.Fail()
}
}
func TestSearchAdjacent(t *testing.T) {
tree := New()
tree.Add("john")
tree.Add("jane")
res := tree.startSearch("johnjane")
cont := res.continueSearch()
if cont2 := cont.continueSearch(); nil != cont2 {
t.Logf("search continued %v", cont2)
t.Fail()
}
}
func TestSearchEmpty(t *testing.T) {
tree := New()
tree.Add("zip")
tree.Add("zap")
res := tree.startSearch("")
if nil != res {
t.Logf("search results %v", res)
t.Fail()
}
}
func TestMultipleOutputs(t *testing.T) {
tree := New()
tree.Add("z")
tree.Add("zz")
tree.Add("zzz")
res := tree.startSearch("zzz")
if 1 != res.lastIndex {
t.Logf("search result last index %v", res.lastIndex)
t.Fail()
}
if out := res.out().Front().Value.(string); "z" != out {
t.Logf("search result out %v", out)
t.Fail()
}
res = res.continueSearch()
if 2 != res.lastIndex {
t.Logf("search result last index %v", res.lastIndex)
t.Fail()
}
if out := res.out().Front().Value.(string); "zz" != out {
t.Logf("search result out %v", out)
t.Fail()
}
res = res.continueSearch()
if 3 != res.lastIndex {
t.Logf("search result last index %v", res.lastIndex)
t.Fail()
}
if out := res.out().Front().Value.(string); "zzz" != out {
t.Logf("search result out %v", out)
t.Fail()
}
res = res.continueSearch()
if nil != res {
t.Logf("search results %v", res)
t.Fail()
}
}
func TestChannel(t *testing.T) {
tree := New()
tree.Add("moo")
tree.Add("one")
tree.Add("on")
tree.Add("ne")
ch := tree.Search("one moon ago")
if on := <-ch; "on" != on {
t.Logf("expected 'on' got '%v'", on)
t.Fail()
}
if one := <-ch; "one" != one {
t.Logf("expected 'one' got '%v'", one)
t.Fail()
}
if ne := <-ch; "ne" != ne {
t.Logf("expected 'ne' got '%v'", ne)
t.Fail()
}
if moo := <-ch; "moo" != moo {
t.Logf("expected 'moo' got '%v'", moo)
t.Fail()
}
if on2 := <-ch; "on" != on2 {
t.Logf("expected 'on' got '%v'", on2)
t.Fail()
}
if x, ok := <-ch; ok {
t.Logf("expected nothing got '%v'", x)
t.Fail()
}
}
func TestText(t *testing.T) {
text := "The ga3 mutant of Arabidopsis is a gibberellin-responsive dwarf. We present data showing that the ga3-1 mutant is deficient in ent-kaurene oxidase activity, the first cytochrome P450-mediated step in the gibberellin biosynthetic pathway. By using a combination of conventional map-based cloning and random sequencing we identified a putative cytochrome P450 gene mapping to the same location as GA3. Relative to the progenitor line, two ga3 mutant alleles contained single base changes generating in-frame stop codons in the predicted amino acid sequence of the P450. A genomic clone spanning the P450 locus complemented the ga3-2 mutant. The deduced GA3 protein defines an additional class of cytochrome P450 enzymes. The GA3 gene was expressed in all tissues examined, RNA abundance being highest in inflorescence tissue."
terms := []string{"microsome",
"cytochrome",
"cytochrome P450 activity",
"gibberellic acid biosynthesis",
"GA3",
"cytochrome P450",
"oxygen binding",
"AT5G25900.1",
"protein",
"RNA",
"gibberellin",
"Arabidopsis",
"ent-kaurene oxidase activity",
"inflorescence",
"tissue"}
tree := New()
for _, term := range terms {
tree.Add(term)
}
found := make(map[string]int)
for f := range tree.Search(text) {
found[f] = 1
}
if 10 != len(found) {
t.Log("more found than expected:\n")
for f := range found {
t.Logf("=> '%v'", f)
}
t.Fail()
}
}
func TestUnicode(t *testing.T) {
tree := New()
tree.Add("iPhone")
tree.Add("youtube.com/watch")
tree.Add("profil")
tree.Add("Заходи, сюда")
p1 := <-tree.Search("What type of iPhone do you have?")
p2 := <-tree.Search("Hi, take a look here: https://www.youtube.com/watch?v=RC_6skf1-t")
p3 := <-tree.Search("Salut est-ce que tu peux aimer ma photo de profil?")
p4 := <-tree.Search("Привет! У нас сегодня акция. Заходи, сюда узнаешь больше!")
p5 := <-tree.Search("Yanıtını Beğen Hediye Yolluyor Dene Gör :)")
expected := "p1: iPhone p2: youtube.com/watch p3: profil p4: Заходи, сюда p5: "
if s := fmt.Sprintf("p1: %v p2: %v p3: %v p4: %v p5: %v", p1, p2, p3, p4, p5); s != expected {
t.Logf("expected:\n => '%v'\ngot:\n => '%v'", expected, s)
t.Fail()
}
}