This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
all_test.go
655 lines (594 loc) · 15.6 KB
/
all_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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rubex
import (
"errors"
"runtime"
"strings"
"testing"
)
var good_re = []string{
``,
`.`,
`^.$`,
`a`,
`a*`,
`a+`,
`a?`,
`a|b`,
`a*|b*`,
`(a*|b)(c*|d)`,
`[a-z]`,
`[a-abc-c\-\]\[]`,
`[a-z]+`,
//`[]`, //this is not considered as good by ruby/javascript regex
`[abc]`,
`[^1234]`,
`[^\n]`,
`\!\\`,
}
type stringError struct {
re string
err error
}
var bad_re = []stringError{
{`*`, errors.New("target of repeat operator is not specified")},
{`+`, errors.New("target of repeat operator is not specified")},
{`?`, errors.New("target of repeat operator is not specified")},
{`(abc`, errors.New("end pattern with unmatched parenthesis")},
{`abc)`, errors.New("unmatched close parenthesis")},
{`x[a-z`, errors.New("premature end of char-class")},
//{`abc]`, Err}, //this is not considered as bad by ruby/javascript regex; nor are the following commented out regex patterns
{`abc[`, errors.New("premature end of char-class")},
{`[z-a]`, errors.New("empty range in char class")},
{`abc\`, errors.New("end pattern at escape")},
//{`a**`, Err},
//{`a*+`, Err},
//{`a??`, Err},
//{`\x`, Err},
}
func runParallel(testFunc func(chan bool), concurrency int) {
runtime.GOMAXPROCS(4)
done := make(chan bool, concurrency)
for i := 0; i < concurrency; i++ {
go testFunc(done)
}
for i := 0; i < concurrency; i++ {
<-done
<-done
}
runtime.GOMAXPROCS(1)
}
const numConcurrentRuns = 200
func compileTest(t *testing.T, expr string, error error) *Regexp {
re, err := Compile(expr)
if (error == nil && err != error) || (error != nil && err.Error() != error.Error()) {
t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
}
return re
}
func TestGoodCompile(t *testing.T) {
testFunc := func(done chan bool) {
done <- false
for i := 0; i < len(good_re); i++ {
compileTest(t, good_re[i], nil)
}
done <- true
}
runParallel(testFunc, numConcurrentRuns)
}
func TestBadCompile(t *testing.T) {
for i := 0; i < len(bad_re); i++ {
compileTest(t, bad_re[i].re, bad_re[i].err)
}
}
func matchTest(t *testing.T, test *FindTest) {
re := compileTest(t, test.pat, nil)
if re == nil {
return
}
m := re.MatchString(test.text)
if m != (len(test.matches) > 0) {
t.Errorf("MatchString failure on %s: %t should be %t", test.pat, m, len(test.matches) > 0)
}
// now try bytes
m = re.Match([]byte(test.text))
if m != (len(test.matches) > 0) {
t.Errorf("Match failure on %s: %t should be %t", test.pat, m, len(test.matches) > 0)
}
}
func TestMatch(t *testing.T) {
for _, test := range findTests {
matchTest(t, &test)
}
}
func matchFunctionTest(t *testing.T, test *FindTest) {
m, err := MatchString(test.pat, test.text)
if err == nil {
return
}
if m != (len(test.matches) > 0) {
t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
}
}
func TestMatchFunction(t *testing.T) {
for _, test := range findTests {
matchFunctionTest(t, &test)
}
}
type ReplaceTest struct {
pattern, replacement, input, output string
}
var replaceTests = []ReplaceTest{
// Test empty input and/or replacement, with pattern that matches the empty string.
{"", "", "", ""},
{"", "x", "", "x"},
{"", "", "abc", "abc"},
{"", "x", "abc", "xaxbxcx"},
// Test empty input and/or replacement, with pattern that does not match the empty string.
{"b", "", "", ""},
{"b", "x", "", ""},
{"b", "", "abc", "ac"},
{"b", "x", "abc", "axc"},
{"y", "", "", ""},
{"y", "x", "", ""},
{"y", "", "abc", "abc"},
{"y", "x", "abc", "abc"},
// Multibyte characters -- verify that we don't try to match in the middle
// of a character.
{"[a-c]*", "x", "\u65e5", "x\u65e5x"},
{"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
// Start and end of a string.
{"^[a-c]*", "x", "abcdabc", "xdabc"},
{"[a-c]*$", "x", "abcdabc", "abcdxx"},
{"^[a-c]*$", "x", "abcdabc", "abcdabc"},
{"^[a-c]*", "x", "abc", "x"},
{"[a-c]*$", "x", "abc", "xx"},
{"^[a-c]*$", "x", "abc", "x"},
{"^[a-c]*", "x", "dabce", "xdabce"},
{"[a-c]*$", "x", "dabce", "dabcex"},
{"^[a-c]*$", "x", "dabce", "dabce"},
{"^[a-c]*", "x", "", "x"},
{"[a-c]*$", "x", "", "x"},
{"^[a-c]*$", "x", "", "x"},
{"^[a-c]+", "x", "abcdabc", "xdabc"},
{"[a-c]+$", "x", "abcdabc", "abcdx"},
{"^[a-c]+$", "x", "abcdabc", "abcdabc"},
{"^[a-c]+", "x", "abc", "x"},
{"[a-c]+$", "x", "abc", "x"},
{"^[a-c]+$", "x", "abc", "x"},
{"^[a-c]+", "x", "dabce", "dabce"},
{"[a-c]+$", "x", "dabce", "dabce"},
{"^[a-c]+$", "x", "dabce", "dabce"},
{"^[a-c]+", "x", "", ""},
{"[a-c]+$", "x", "", ""},
{"^[a-c]+$", "x", "", ""},
// Other cases.
{"abc", "def", "abcdefg", "defdefg"},
{"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
{"abc", "", "abcdabc", "d"},
{"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
{"abc", "d", "", ""},
{"abc", "d", "abc", "d"},
{".+", "x", "abc", "x"},
{"[a-c]*", "x", "def", "xdxexfx"},
{"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
{"[a-c]*", "x", "abcbcdcdedef", "xxdxxdxexdxexfx"},
{"(foo)*bar(s)", "\\1", "bars", ""},
}
type ReplaceFuncTest struct {
pattern string
replacement func(string) string
input, output string
}
var replaceFuncTests = []ReplaceFuncTest{
{"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
{"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
{"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcyxydxyexyfxy"},
}
func TestReplaceAll(t *testing.T) {
for _, tc := range replaceTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.ReplaceAllString(tc.input, tc.replacement)
if actual != tc.output {
t.Errorf("%q.Replace(%q,%q) = %q; want %q",
tc.pattern, tc.input, tc.replacement, actual, tc.output)
}
// now try bytes
actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
if actual != tc.output {
t.Errorf("%q.Replace(%q,%q) = %q; want %q",
tc.pattern, tc.input, tc.replacement, actual, tc.output)
}
}
}
func TestReplaceAllFunc(t *testing.T) {
for _, tc := range replaceFuncTests {
re, err := Compile(tc.pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
continue
}
actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
if actual != tc.output {
t.Errorf("%q.ReplaceFunc(%q,%q) = %q; want %q",
tc.pattern, tc.input, tc.replacement, actual, tc.output)
}
// now try bytes
actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
if actual != tc.output {
t.Errorf("%q.ReplaceFunc(%q,%q) = %q; want %q",
tc.pattern, tc.input, tc.replacement, actual, tc.output)
}
}
}
/*
* "hallo".gsub(/h(.*)llo/, "e")
*/
func TestGsub1(t *testing.T) {
input := "hallo"
pattern := "h(.*)llo"
expected := "e"
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.Gsub(input, "e")
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* "hallo".gsub(/h(?<foo>.*)llo/, "\\k<foo>")
*/
func TestGsubNamedCapture1(t *testing.T) {
input := "hallo"
pattern := "h(?<foo>.*)llo"
expected := "a"
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.Gsub(input, "\\k<foo>")
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* "hallo".gsub(/h(?<foo>.*)ll(?<bar>.*)/, "\\k<foo>\\k<bar>\\k<foo>")
*/
func TestGsubNamedCapture2(t *testing.T) {
input := "hallo"
pattern := "h(?<foo>.*)ll(?<bar>.*)"
expected := "aoa"
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.Gsub(input, "\\k<foo>\\k<bar>\\k<foo>")
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* "hallo".gsub(/h(?<foo>.*)(l*)(?<bar>.*)/, "\\k<foo>\\k<bar>\\k<foo>\\1")
*/
func TestGsubNamedCapture3(t *testing.T) {
input := "hallo"
pattern := "h(?<foo>.*)(l*)(?<bar>.*)"
expected := "alloallo"
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.Gsub(input, "\\k<foo>\\k<bar>\\k<foo>\\1")
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* "hallo".gsub(/h(?<foo>.*)(l*)(?<bar>.*)/, "\\k<foo>\\k<bar>\\k<foo>\\1")
*/
func TestGsubNamedCapture4(t *testing.T) {
input := "The lamb was sure to go."
pattern := "(?<word>[^\\s\\.]+)(?<white_space>\\s)"
expected := "They lamby wasy surey toy go."
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.GsubFunc(input, func(_ string, captures map[string]string) string {
return captures["word"] + "y" + captures["white_space"]
})
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* "hallo".gsub(/h(.*)llo/) { |match|
* "e"
* }
*/
func TestGsubFunc1(t *testing.T) {
input := "hallo"
pattern := "h(.*)llo"
expected := "e"
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.GsubFunc(input, func(match string, captures map[string]string) string {
return "e"
})
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/*
* @env = {}
* "hallo".gsub(/h(.*)llo/) { |match|
* $~.captures.each_with_index do |arg, index|
* @env["#{index + 1}"] = arg
* "abcd".gsub(/(d)/) do
* env["1"]
* end
* end
* }
*/
func TestGsubFunc2(t *testing.T) {
input := "hallo"
pattern := "h(.*)llo"
expected := "abca"
env := make(map[string]string)
re, err := Compile(pattern)
if err != nil {
t.Errorf("Unexpected error compiling %q: %v", pattern, err)
return
}
actual := re.GsubFunc(input, func(_ string, captures map[string]string) string {
for name, capture := range captures {
env[name] = capture
}
re1 := MustCompile("(d)")
return re1.GsubFunc("abcd", func(_ string, captures2 map[string]string) string {
return env["1"]
})
})
if actual != expected {
t.Errorf("expected %q, actual %q\n", expected, actual)
}
}
/* how to match $ as itself */
func TestPattern1(t *testing.T) {
re := MustCompile(`b\$a`)
if !re.MatchString("b$a") {
t.Errorf("expect to match\n")
}
re = MustCompile("b\\$a")
if !re.MatchString("b$a") {
t.Errorf("expect to match 2\n")
}
}
/* how to use $ as the end of line */
func TestPattern2(t *testing.T) {
re := MustCompile("a$")
if !re.MatchString("a") {
t.Errorf("expect to match\n")
}
if re.MatchString("ab") {
t.Errorf("expect to mismatch\n")
}
}
func TestCompileWithOption(t *testing.T) {
re := MustCompileWithOption("a$", ONIG_OPTION_IGNORECASE)
if !re.MatchString("A") {
t.Errorf("expect to match\n")
}
re = MustCompile("a$")
if re.MatchString("A") {
t.Errorf("expect to mismatch\n")
}
}
type MetaTest struct {
pattern, output, literal string
isLiteral bool
}
var metaTests = []MetaTest{
{``, ``, ``, true},
{`foo`, `foo`, `foo`, true},
{`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
{`foo.\$`, `foo\.\\\$`, `foo`, false}, // has escaped operators and real operators
{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[{\]}\\\|,<\.>/\?~`, `!@#`, false},
}
func TestQuoteMeta(t *testing.T) {
for _, tc := range metaTests {
// Verify that QuoteMeta returns the expected string.
quoted := QuoteMeta(tc.pattern)
if quoted != tc.output {
t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
tc.pattern, quoted, tc.output)
continue
}
// Verify that the quoted string is in fact treated as expected
// by Compile -- i.e. that it matches the original, unquoted string.
if tc.pattern != "" {
re, err := Compile(quoted)
if err != nil {
t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
continue
}
src := "abc" + tc.pattern + "def"
repl := "xyz"
replaced := re.ReplaceAllString(src, repl)
expected := "abcxyzdef"
if replaced != expected {
t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
tc.pattern, src, repl, replaced, expected)
}
}
}
}
/*
* LiteralPrefix is not supported by rubex
*
//LiteralPrefix
func TestLiteralPrefix(t *testing.T) {
for _, tc := range metaTests {
// Literal method needs to scan the pattern.
re := MustCompile(tc.pattern)
str, complete := re.LiteralPrefix()
if complete != tc.isLiteral {
t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
}
if str != tc.literal {
t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
}
}
}
*/
type numSubexpCase struct {
input string
expected int
}
var numSubexpCases = []numSubexpCase{
{``, 0},
{`.*`, 0},
{`abba`, 0},
{`ab(b)a`, 1},
{`ab(.*)a`, 1},
{`(.*)ab(.*)a`, 2},
{`(.*)(ab)(.*)a`, 3},
{`(.*)((a)b)(.*)a`, 4},
{`(.*)(\(ab)(.*)a`, 3},
{`(.*)(\(a\)b)(.*)a`, 3},
}
func TestNumSubexp(t *testing.T) {
for _, c := range numSubexpCases {
re := MustCompile(c.input)
n := re.NumSubexp()
if n != c.expected {
t.Errorf("NumSubexp for %q returned %d, expected %d", c.input, n, c.expected)
}
}
}
func BenchmarkLiteral(b *testing.B) {
x := strings.Repeat("x", 50) + "y"
b.StopTimer()
re := MustCompile("y")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!")
break
}
}
}
func BenchmarkNotLiteral(b *testing.B) {
x := strings.Repeat("x", 50) + "y"
b.StopTimer()
re := MustCompile(".y")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!")
break
}
}
}
func BenchmarkMatchClass(b *testing.B) {
b.StopTimer()
x := strings.Repeat("xxxx", 20) + "w"
re := MustCompile("[abcdw]")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!")
break
}
}
}
func BenchmarkMatchClass_InRange(b *testing.B) {
b.StopTimer()
// 'b' is between 'a' and 'c', so the charclass
// range checking is no help here.
x := strings.Repeat("bbbb", 20) + "c"
re := MustCompile("[ac]")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!")
break
}
}
}
func BenchmarkReplaceAll(b *testing.B) {
x := "abcdefghijklmnopqrstuvwxyz"
b.StopTimer()
re := MustCompile("[cjrw]")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.ReplaceAllString(x, "")
}
}
func BenchmarkFindAllStringSubmatchIndex(b *testing.B) {
x := "abcdefghijklmnopqrstuvwxyz"
b.StopTimer()
re := MustCompile("[cjrw]")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.FindAllStringSubmatchIndex(x, 0)
}
}
func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
b.StopTimer()
x := []byte("abcdefghijklmnopqrstuvwxyz")
re := MustCompile("^zbc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
b.StopTimer()
x := []byte("abcdefghijklmnopqrstuvwxyz")
for i := 0; i < 15; i++ {
x = append(x, x...)
}
re := MustCompile("^zbc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredShortMatch(b *testing.B) {
b.StopTimer()
x := []byte("abcdefghijklmnopqrstuvwxyz")
re := MustCompile("^.bc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredLongMatch(b *testing.B) {
b.StopTimer()
x := []byte("abcdefghijklmnopqrstuvwxyz")
for i := 0; i < 15; i++ {
x = append(x, x...)
}
re := MustCompile("^.bc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}