-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathac_test.go
262 lines (246 loc) · 6.52 KB
/
ac_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
package ac
import (
"fmt"
"reflect"
"testing"
)
var cases = []struct {
name string // matches original test name from cloudflare/ahocorasick
dict []string
input string
matches []string
}{
{
"TestNoPatterns",
[]string{},
"",
nil,
},
{
"TestNoData",
[]string{"foo", "baz", "bar"},
"",
nil,
},
{
"TestSuffixes",
[]string{"Superman", "uperman", "perman", "erman"},
"The Man Of Steel: Superman",
[]string{"Superman", "uperman", "perman", "erman"},
},
{
"TestPrefixes",
[]string{"Superman", "Superma", "Superm", "Super"},
"The Man Of Steel: Superman",
[]string{"Super", "Superm", "Superma", "Superman"},
},
{
"TestInterior",
[]string{"Steel", "tee", "e"},
"The Man Of Steel: Superman",
[]string{"e", "tee", "Steel"},
},
{
"TestMatchAtStart",
[]string{"The", "Th", "he"},
"The Man Of Steel: Superman",
[]string{"Th", "The", "he"},
},
{
"TestMatchAtEnd",
[]string{"teel", "eel", "el"},
"The Man Of Steel",
[]string{"teel", "eel", "el"},
},
{
"TestOverlappingPatterns",
[]string{"Man ", "n Of", "Of S"},
"The Man Of Steel",
[]string{"Man ", "n Of", "Of S"},
},
{
"TestMultipleMatches",
[]string{"The", "Man", "an"},
"A Man A Plan A Canal: Panama, which Man Planned The Canal",
[]string{"Man", "an", "The"},
},
{
"TestSingleCharacterMatches",
[]string{"a", "M", "z"},
"A Man A Plan A Canal: Panama, which Man Planned The Canal",
[]string{"M", "a"}},
{
"TestNothingMatches",
[]string{"baz", "bar", "foo"},
"A Man A Plan A Canal: Panama, which Man Planned The Canal",
nil,
},
{
"Wikipedia1",
[]string{"a", "ab", "bc", "bca", "c", "caa"},
"abccab",
[]string{"a", "ab", "bc", "c"},
},
{
"Wikipedia2",
[]string{"a", "ab", "bc", "bca", "c", "caa"},
"bccab",
[]string{"bc", "c", "a", "ab"},
},
{
"Wikipedia3",
[]string{"a", "ab", "bc", "bca", "c", "caa"},
"bccb",
[]string{"bc", "c"},
},
{
"Browser1",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"},
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
[]string{"Mozilla", "Mac", "Macintosh", "Safari"},
},
{
"Browser2",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"},
"Mozilla/5.0 (Mac; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
[]string{"Mozilla", "Mac", "Safari"},
},
{
"Browser3",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"},
"Mozilla/5.0 (Moc; Intel Computer OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
[]string{"Mozilla", "Safari"},
},
{
"Browser4",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"},
"Mozilla/5.0 (Moc; Intel Computer OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Sofari/537.36",
[]string{"Mozilla"},
},
{
"Browser5",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"},
"Mazilla/5.0 (Moc; Intel Computer OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Sofari/537.36",
nil,
},
{
// this is to make sure backtracking works. We get a partial
// match of "Superwoman" with "Superman". Then we need to make
// sure that we restart the search and find "per". Some implementations
// had bugs that didn't backtrack (really start over) and didn't match
// "per"
"Backtrack",
[]string{"Superwoman", "per"},
"The Man Of Steel: Superman",
[]string{"per"},
},
{
"NotAsciiInput",
[]string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage", "Gecko"},
"Mazilla/5.0 \u0000 (Moc; Intel Computer OS X 10_7_5) AppleWebKit/537.36 \uFFFF (KHTML, like Gecko) Chrome/30.0.1599.101 Sofari/537.36",
[]string{"Gecko"},
},
}
func TestAC(t *testing.T) {
for _, tt := range cases {
m, err := CompileString(tt.dict)
if err != nil {
t.Fatalf("%s:unable to compile %s", tt.name, err)
}
//
matches := m.FindAllString(tt.input)
if !reflect.DeepEqual(matches, tt.matches) {
t.Errorf("%s: FindAllString want %v, got %v", tt.name, tt.matches, matches)
}
//
contains := m.MatchString(tt.input)
if contains {
if len(tt.matches) == 0 {
t.Errorf("%s: MatchString want false, but got true", tt.name)
}
} else {
// does not contain, but got matches
if len(tt.matches) != 0 {
t.Errorf("%s: MatchString want true, but got false", tt.name)
}
}
}
}
func TestACBlices(t *testing.T) {
for _, tt := range cases {
var dict [][]byte
for _, d := range tt.dict {
dict = append(dict, []byte(d))
}
m := MustCompile(dict)
matches := m.FindAll([]byte(tt.input))
var mb [][]byte
for _, m := range matches {
mb = append(mb, []byte(m))
}
if !reflect.DeepEqual(matches, mb) {
t.Errorf("%s: FindAll = %v, want %v", tt.name, mb, matches)
}
contains := m.Match([]byte(tt.input))
if contains {
if len(tt.matches) == 0 {
t.Errorf("%s: MatchString = true, want false", tt.name)
}
} else {
// does not contain, but got matches
if len(tt.matches) != 0 {
t.Errorf("%s: Match = false, want true", tt.name)
}
}
}
}
func TestNonASCIIDictionary(t *testing.T) {
dict := []string{"hello world", "こんにちは世界"}
_, err := CompileString(dict)
if err != nil {
t.Errorf("error compiling matcher: %s", err)
}
}
var (
source1 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
source1b = []byte(source1)
dict1 = []string{"Mozilla", "Mac", "Macintosh", "Safari", "Sausage"}
dict2 = []string{"Googlebot", "bingbot", "msnbot", "Yandex", "Baiduspider"}
re1 = MustCompileString(dict1)
re2 = MustCompileString(dict2)
)
// this is to prevent optimizer tricks
var result1 bool
func BenchmarkAC1(b *testing.B) {
var result bool
for i := 0; i < b.N; i++ {
result = re1.MatchString(source1)
}
result1 = result
}
func ExampleMatcher_FindAllString() {
m := MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
matches := m.FindAllString("The Man Of Steel: Superman")
fmt.Println(matches)
// Output: [Superman uperman perman erman]
}
func ExampleMatcher_MatchString() {
m := MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
contains := m.MatchString("The Man Of Steel: Superman")
fmt.Println(contains)
// Output: true
}
func BenchmarkAC2(b *testing.B) {
var result bool
for i := 0; i < b.N; i++ {
result = re2.MatchString(source1)
}
result1 = result
}
func BenchmarkAC2Byte(b *testing.B) {
var result bool
for i := 0; i < b.N; i++ {
result = re2.Match(source1b)
}
result1 = result
}