/
bypass_test.go
105 lines (86 loc) · 2.92 KB
/
bypass_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
// Copyright 2013 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 regexp
import (
"encoding/csv"
"os"
"reflect"
"regexp"
"strconv"
"testing"
)
var compileByPassTests = []struct {
pat string
isByPass bool
}{
{`a`, true},
{`[a]`, true},
{`[^a]`, true},
{`.`, true},
{`.+`, false},
{`a.`, true},
{`^a.`, true},
{`a{2}`, true},
{`(a)`, false},
{`x.[^z]yz$`, true},
{`^(?:(?:a(?:a.)))$`, true},
{`(?:a(?:a.))`, true},
{`\A(?:(?:a(?:a.)))\z`, true},
{`^aa.*`, true},
}
func TestByPassCompile(t *testing.T) {
for _, test := range compileByPassTests {
re := MustCompile(test.pat)
if (re.bypass != nil) != test.isByPass {
t.Errorf("pat: %s should have been bypassed=%t", test.pat, test.isByPass)
}
}
}
func TestByPassGithubRegexps(t *testing.T) {
var total, linearUnanchored, linearAnchored, alt, firstpass, supported, unsupported, invalid, unmatchable int
f, _ := os.Open("testdata/github-regexp.csv")
defer f.Close()
lines, _ := csv.NewReader(f).ReadAll()
for _, line := range lines {
n, _ := strconv.Atoi(line[1])
total += n
_, err := regexp.Compile(line[0])
if err != nil {
invalid += n
continue
}
re, errb := Compile(line[0])
if errb != nil {
t.Errorf("Failed to compile in regexp-bypass but not in regexp: %s", line[0])
continue
}
if re.bypass == nil {
unsupported += n
continue
}
supported += n
switch reflect.TypeOf(re.bypass).String() {
case "*regexp.byPassProgAnchored":
linearAnchored += n
case "*regexp.byPassProgUnanchored":
linearUnanchored += n
case "*regexp.byPassProgAlternate":
alt += n
case "*regexp.byPassProgFirstPass":
firstpass += n
case "*regexp.byPassProgUnmatchable":
unmatchable += n
}
}
t.Logf("\nStats on %d unique regexps from GitHub for bypass matcher:", len(lines))
t.Logf("Total occurences %d", total)
t.Logf("Invalid %d (%0.2f%%)", invalid, float64(invalid*100)/float64(total))
t.Logf("Unsupported %d (%0.2f%%)", unsupported, float64(unsupported*100)/float64(total))
t.Logf("Supported total %d (%0.2f%%)", supported, float64(supported*100)/float64(total))
t.Logf(" Supported with byPassProgAnchored %d (%0.2f%%)", linearAnchored, float64(linearAnchored*100)/float64(total))
t.Logf(" Supported with byPassProgUnanchored %d (%0.2f%%)", linearUnanchored, float64(linearUnanchored*100)/float64(total))
t.Logf(" Supported with byPassProgAlternate %d (%0.2f%%)", alt, float64(alt*100)/float64(total))
t.Logf(" Supported with byPassProgFirstPass %d (%0.2f%%)", firstpass, float64(firstpass*100)/float64(total))
t.Logf(" Supported with byPassProgUnmatchable %d (%0.2f%%)", unmatchable, float64(unmatchable*100)/float64(total))
}