-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpex_test.go
536 lines (492 loc) · 13.8 KB
/
simpex_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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package simpex_test
import (
"fmt"
"reflect"
"regexp"
"testing"
"github.com/tobiassjosten/go-simpex"
)
func TestCompile(t *testing.T) {
tcs := map[string]struct {
pattern []byte
sx []byte
error bool
}{
"escape and handle start/end capture symbols": {
pattern: []byte("{{{{{Lorem}}} ipsum {{dolor}}}} sit amet."),
sx: []byte("\x02{{Lorem}\x03 ipsum {dolor}} sit amet."),
},
"separate escape and capture": {
pattern: []byte("{{{Lorem}} ipsum} dolor sit amet."),
sx: []byte("\x02{Lorem} ipsum\x03 dolor sit amet."),
},
"handle unopened capture symbols": {
pattern: []byte("Lorem} ipsum dolor sit amet."),
error: true,
},
"handle unclosed capture symbols": {
pattern: []byte("{Lorem ipsum dolor sit amet."),
error: true,
},
"handle nested capture symbols": {
pattern: []byte("{Lorem {ipsum} dolor} sit amet."),
error: true,
},
"escape and handle phrase symbols": {
pattern: []byte("Lorem * ** ***."),
sx: []byte("Lorem \x1d * *\x1d."),
},
"escape and handle word symbols": {
pattern: []byte("Lorem ^ ^^ ^^^."),
sx: []byte("Lorem \x1e ^ ^\x1e."),
},
"escape and handle captured word symbols": {
pattern: []byte("{^^}"),
sx: []byte("\x02^\x03"),
},
"escape and handle character symbols": {
pattern: []byte("Lorem ip_um d______r s___t amet."),
sx: []byte("Lorem ip\x1fum d___r s_\x1ft amet."),
},
"escape and handle everything": {
pattern: []byte("{{{{{}}} {*} {**} {***} {^} {^^} {^^^} {_} {__} {___}"),
sx: []byte("\x02{{}\x03 \x02\x1d\x03 \x02*\x03 \x02*\x1d\x03 \x02\x1e\x03 \x02^\x03 \x02^\x1e\x03 \x02\x1f\x03 \x02_\x03 \x02_\x1f\x03"),
},
"disallow character word combination": {
pattern: []byte("_^"),
error: true,
},
"disallow character phrase combination": {
pattern: []byte("_*"),
error: true,
},
"disallow word character combination": {
pattern: []byte("^_"),
error: true,
},
"disallow word phrase combination": {
pattern: []byte("^*"),
error: true,
},
"disallow phrase character combination": {
pattern: []byte("*_"),
error: true,
},
"disallow phrase word combination": {
pattern: []byte("*^"),
error: true,
},
"reserved capture start symbol": {
pattern: []byte("\x02"),
error: true,
},
"reserved capture end symbol": {
pattern: []byte("\x03"),
error: true,
},
"reserved character symbol": {
pattern: []byte("\x1f"),
error: true,
},
"reserved word symbol": {
pattern: []byte("\x1e"),
error: true,
},
"reserved phrase symbol": {
pattern: []byte("\x1d"),
error: true,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
sx, err := simpex.Compile(tc.pattern)
if tc.error && (err == nil) {
t.Fatalf("Compile(%q) missing error", tc.pattern)
} else if !tc.error && (err != nil) {
t.Fatalf("Compile(%q) unexpected error '%s'", tc.pattern, err)
}
if string(tc.sx) != string(sx) {
t.Fatalf("Compile(%q)\ngot %q\nwant %q", tc.pattern, sx, tc.sx)
}
})
}
}
func TestMatch(t *testing.T) {
tcs := map[string]struct {
pattern []byte
text []byte
matches [][]byte
error bool
}{
"mismatch longer pattern": {
pattern: []byte("Lorem ipsum dolor sit amet."),
text: []byte("Lorem ipsum"),
},
"mismatch longer text": {
pattern: []byte("Lorem ipsum"),
text: []byte("Lorem ipsum dolor sit amet."),
},
"exact match simple": {
pattern: []byte("Lorem ipsum dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"exact match capture": {
pattern: []byte("{Lorem} ipsum dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("Lorem")},
},
"exact match escaped capture simple": {
pattern: []byte("{{Lorem}} ipsum dolor sit amet."),
text: []byte("{Lorem} ipsum dolor sit amet."),
matches: [][]byte{},
},
"exact match escaped capture capture one": {
pattern: []byte("{{{Lorem}}} ipsum dolor sit amet."),
text: []byte("{Lorem} ipsum dolor sit amet."),
matches: [][]byte{[]byte("{Lorem}")},
},
"exact match escaped capture capture two": {
pattern: []byte("{{{Lorem}} ipsum} dolor sit amet."),
text: []byte("{Lorem} ipsum dolor sit amet."),
matches: [][]byte{[]byte("{Lorem} ipsum")},
},
"character match empty": {
pattern: []byte("_"),
text: []byte(""),
},
"character match single": {
pattern: []byte("_"),
text: []byte("a"),
matches: [][]byte{},
},
"character match capture single": {
pattern: []byte("{_}"),
text: []byte("a"),
matches: [][]byte{[]byte("a")},
},
"character match simple": {
pattern: []byte("Lorem ipsum do_or sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"character match capture": {
pattern: []byte("Lorem ipsum do{_}or sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{{'l'}},
},
"character match escaped one": {
pattern: []byte("Lorem ipsum do__or sit amet."),
text: []byte("Lorem ipsum do_or sit amet."),
matches: [][]byte{},
},
"character match escaped two": {
pattern: []byte("Lorem ipsum do___or sit amet."),
text: []byte("Lorem ipsum do_lor sit amet."),
matches: [][]byte{},
},
"word match empty": {
pattern: []byte("^"),
text: []byte(""),
},
"word match single": {
pattern: []byte("^"),
text: []byte("asdf"),
matches: [][]byte{},
},
"word match capture single": {
pattern: []byte("{^}"),
text: []byte("asdf"),
matches: [][]byte{[]byte("asdf")},
},
"word match prefix": {
pattern: []byte("^df"),
text: []byte("asdf"),
matches: [][]byte{},
},
"word match non-match prefix": {
pattern: []byte("^df"),
text: []byte("asdd"),
},
"word match simple": {
pattern: []byte("Lorem ^ dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"word match capture": {
pattern: []byte("Lorem {^} dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("ipsum")},
},
"word match mid prefix": {
pattern: []byte("Lorem ^sum dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"word match capture prefix": {
pattern: []byte("Lorem {^sum} dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("ipsum")},
},
"word match suffix": {
pattern: []byte("Lorem ip^ dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"word match capture suffix": {
pattern: []byte("Lorem {ip^} dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("ipsum")},
},
"word match escaped one": {
pattern: []byte("Lorem ^^ dolor sit amet."),
text: []byte("Lorem ^ dolor sit amet."),
matches: [][]byte{},
},
"word match escaped two": {
pattern: []byte("Lorem ^^^ dolor sit amet."),
text: []byte("Lorem ^ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match empty": {
pattern: []byte("*"),
text: []byte(""),
},
"phrase match single": {
pattern: []byte("*"),
text: []byte("asdf"),
matches: [][]byte{},
},
"phrase match capture single": {
pattern: []byte("{*}"),
text: []byte("asdf"),
matches: [][]byte{[]byte("asdf")},
},
"phrase match all": {
pattern: []byte("*"),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match capture all": {
pattern: []byte("{*}"),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("Lorem ipsum dolor sit amet.")},
},
"phrase match beginning": {
pattern: []byte("* dolor sit amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match middle": {
pattern: []byte("Lorem * amet."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match end": {
pattern: []byte("Lorem ipsum dolor *."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match simple two": {
pattern: []byte("Lorem ipsum dolor * lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{},
},
"phrase match capture one": {
pattern: []byte("Lorem ipsum dolor {*}."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match capture two": {
pattern: []byte("Lorem ipsum dolor {*} lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match prefix one": {
pattern: []byte("Lorem ipsum dolor *et."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match prefix two": {
pattern: []byte("Lorem ipsum dolor *et lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{},
},
"phrase match capture prefix one": {
pattern: []byte("Lorem ipsum dolor {*et}."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match capture prefix two": {
pattern: []byte("Lorem ipsum dolor {*et} lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match suffix one": {
pattern: []byte("Lorem ipsum dolor si*."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"phrase match suffix two": {
pattern: []byte("Lorem ipsum dolor si* lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{},
},
"phrase match capture suffix one": {
pattern: []byte("Lorem ipsum dolor {si*}."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match capture suffix two": {
pattern: []byte("Lorem ipsum dolor {si*} lol."),
text: []byte("Lorem ipsum dolor sit amet lol."),
matches: [][]byte{[]byte("sit amet")},
},
"phrase match escaped one": {
pattern: []byte("Lorem ipsum dolor **."),
text: []byte("Lorem ipsum dolor *."),
matches: [][]byte{},
},
"phrase match escaped two": {
pattern: []byte("Lorem ipsum dolor ***."),
text: []byte("Lorem ipsum dolor *sit amet."),
matches: [][]byte{},
},
"phrase non-matching following": {
pattern: []byte("* amet"),
text: []byte("asdf"),
},
"combination match simple": {
pattern: []byte("Lorem ^ do_or *."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{},
},
"combination match escaped": {
pattern: []byte("Lorem ^^ do__or **."),
text: []byte("Lorem ^ do_or *."),
matches: [][]byte{},
},
"combination match capture": {
pattern: []byte("{Lorem} {^} do{_}or {*}."),
text: []byte("Lorem ipsum dolor sit amet."),
matches: [][]byte{
[]byte("Lorem"),
[]byte("ipsum"),
{'l'},
[]byte("sit amet"),
},
},
"combination match capture escaped": {
pattern: []byte("{{{Lorem}}} {^^} do{__}or {**}."),
text: []byte("{Lorem} ^ do_or *."),
matches: [][]byte{
[]byte("{Lorem}"),
[]byte("^"),
{'_'},
[]byte("*"),
},
},
"unclosed phrase capture": {
pattern: []byte("{*"),
text: []byte("0"),
error: true,
},
"unopened phrase capture": {
pattern: []byte("*}"),
text: []byte("0"),
error: true,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
matches, err := simpex.Match(tc.pattern, tc.text)
if tc.error && (err == nil) {
t.Fatalf(
"Match(%q, %q) missing error",
tc.pattern, tc.text,
)
} else if !tc.error && (err != nil) {
t.Fatalf(
"Match(%q, %q) unexpected error '%s'",
tc.pattern, tc.text, err,
)
}
if tc.matches != nil && matches == nil {
t.Fatalf(
"Match(%q, %q) = nil, want %q",
tc.pattern, tc.text, tc.matches,
)
} else if tc.matches == nil && matches != nil {
t.Fatalf(
"Match(%q, %q) = %q, want nil",
tc.pattern, tc.text, matches,
)
} else if !reflect.DeepEqual(tc.matches, matches) {
t.Fatalf(
"Match(%q, %q) = %q, want %q",
tc.pattern, tc.text, matches, tc.matches,
)
}
})
}
}
func FuzzMatch(f *testing.F) {
f.Add(
[]byte("{Lorem} {^} do{_}or {*}."),
[]byte("Lorem ipsum dolor sit amet."),
)
f.Fuzz(func(t *testing.T, pattern, text []byte) {
_, _ = simpex.Match(pattern, text)
})
}
var (
benchresult1 [][]byte
benchresult2 [][][]byte
benchmarks = map[string][][]byte{
"exact match": {
[]byte("Lorem ipsum dolor sit amet."),
[]byte("Lorem ipsum dolor sit amet."),
[]byte("Lorem ipsum dolor sit amet."),
},
"character match": {
[]byte("Lorem ipsum dolor sit amet."),
[]byte("Lorem ipsum do_or sit amet."),
[]byte("Lorem ipsum do.or sit amet."),
},
"word match": {
[]byte("Lorem ipsum dolor sit amet."),
[]byte("Lorem ^ dolor sit amet."),
[]byte("Lorem [a-zA-Z0-9]+ dolor sit amet."),
},
"phrase match": {
[]byte("Lorem ipsum dolor sit amet."),
[]byte("Lorem ipsum dolor * amet."),
[]byte("Lorem ipsum dolor .+ amet."),
},
"all specials": {
[]byte("Lorem ipsum dolor sit amet."),
[]byte("{Lorem} {^} do{_}or {*}."),
[]byte("(Lorem) ([a-zA-Z0-9]+) do(.)or (.+)."),
},
}
)
func BenchmarkMatch(b *testing.B) {
var r1 [][]byte
var r2 [][][]byte
for name, benchmark := range benchmarks {
b.Run(fmt.Sprintf("%s simpex", name), func(b *testing.B) {
sx, _ := simpex.Compile(benchmark[1])
for i := 0; i < b.N; i++ {
r1 = sx.Match(benchmark[0])
}
})
b.Run(fmt.Sprintf("%s regexp", name), func(b *testing.B) {
pattern, _ := regexp.Compile(string(benchmark[2]))
for i := 0; i < b.N; i++ {
r2 = pattern.FindAllSubmatch(benchmark[0], -1)
}
})
}
benchresult1 = r1
benchresult2 = r2
}