-
Notifications
You must be signed in to change notification settings - Fork 19
/
ahocorasick_test.go
91 lines (74 loc) · 1.61 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
package ahocorasick
import (
"math/rand"
"testing"
)
func Test1(t *testing.T) {
ac := NewMatcher()
dictionary := []string{"she", "he", "say", "shr", "her"}
ac.Build(dictionary)
expected := []*Term{
{Index: 0, EndPosition: 5},
{Index: 1, EndPosition: 5},
{Index: 4, EndPosition: 6},
}
s := "yasherhs"
ret := ac.Match(s)
if len(expected) != len(ret) {
t.Fatal()
}
for i, _ := range ret {
if ret[i].Index != expected[i].Index || ret[i].EndPosition != expected[i].EndPosition {
t.Fatal()
}
original := dictionary[ret[i].Index]
matched := s[ret[i].EndPosition-len(original) : ret[i].EndPosition]
if original != matched {
t.Fatal()
}
}
}
func Test2(t *testing.T) {
ac := NewMatcher()
dictionary := []string{"中国人民", "国人", "中国人", "hello世界", "hello"}
ac.Build(dictionary)
if len(ac.Match("中国人")) != 2 {
t.Fatal()
}
if len(ac.Match("世界")) != 0 {
t.Fatal()
}
s := "hello世界"
ret := ac.Match(s)
if len(ret) != 2 {
t.Fatal()
}
for i, _ := range ret {
original := dictionary[ret[i].Index]
matched := s[ret[i].EndPosition-len(original) : ret[i].EndPosition]
if original != matched {
t.Fatal()
}
}
}
func Benchmark(b *testing.B) {
ac := NewMatcher()
dictionary := make([]string, 0)
for i := 0; i < 200000; i++ {
dictionary = append(dictionary, randWord(2, 6))
}
ac.Build(dictionary)
for i := 0; i < b.N; i++ {
ac.Match(randWord(5000, 10000))
}
}
func randWord(m, n int) string {
num := rand.Intn(n-m) + m
var s string
var a rune = 'a'
for i := 0; i < num; i++ {
c := a + rune(rand.Intn(26))
s = s + string(c)
}
return s
}