-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathregexp_test.go
219 lines (204 loc) · 5.08 KB
/
regexp_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
// Copyright 2011 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 (
"bytes"
"reflect"
"strings"
"testing"
)
var nstateTests = []struct {
q []uint32
partial rune
}{
{[]uint32{1, 2, 3}, 1},
{[]uint32{1}, 1},
{[]uint32{}, 0},
{[]uint32{1, 2, 8}, 0x10FFF},
}
func TestNstateEnc(t *testing.T) {
var n1, n2 nstate
n1.q.Init(10)
n2.q.Init(10)
for _, tt := range nstateTests {
n1.q.Reset()
n1.partial = tt.partial
for _, id := range tt.q {
n1.q.Add(id)
}
enc := n1.enc()
n2.dec(enc)
if n2.partial != n1.partial || !reflect.DeepEqual(n1.q.Dense(), n2.q.Dense()) {
t.Errorf("%v.enc.dec = %v", &n1, &n2)
}
}
}
var matchTests = []struct {
re string
s string
m []int
}{
// Adapted from go/src/pkg/regexp/find_test.go.
{`a+`, "abc\ndef\nghi\n", []int{1}},
{``, ``, []int{1}},
{`^abcdefg`, "abcdefg", []int{1}},
{`a+`, "baaab", []int{1}},
{"abcd..", "abcdef", []int{1}},
{`a`, "a", []int{1}},
{`x`, "y", nil},
{`b`, "abc", []int{1}},
{`.`, "a", []int{1}},
{`.*`, "abcdef", []int{1}},
{`^`, "abcde", []int{1}},
{`$`, "abcde", []int{1}},
{`^abcd$`, "abcd", []int{1}},
{`^bcd'`, "abcdef", nil},
{`^abcd$`, "abcde", nil},
{`a+`, "baaab", []int{1}},
{`a*`, "baaab", []int{1}},
{`[a-z]+`, "abcd", []int{1}},
{`[^a-z]+`, "ab1234cd", []int{1}},
{`[a\-\]z]+`, "az]-bcz", []int{1}},
{`[^\n]+`, "abcd\n", []int{1}},
{`[日本語]+`, "日本語日本語", []int{1}},
{`日本語+`, "日本語", []int{1}},
{`日本語+`, "日本語語語語", []int{1}},
{`()`, "", []int{1}},
{`(a)`, "a", []int{1}},
{`(.)(.)`, "日a", []int{1}},
{`(.*)`, "", []int{1}},
{`(.*)`, "abcd", []int{1}},
{`(..)(..)`, "abcd", []int{1}},
{`(([^xyz]*)(d))`, "abcd", []int{1}},
{`((a|b|c)*(d))`, "abcd", []int{1}},
{`(((a|b|c)*)(d))`, "abcd", []int{1}},
{`\a\f\r\t\v`, "\a\f\r\t\v", []int{1}},
{`[\a\f\n\r\t\v]+`, "\a\f\r\t\v", []int{1}},
{`a*(|(b))c*`, "aacc", []int{1}},
{`(.*).*`, "ab", []int{1}},
{`[.]`, ".", []int{1}},
{`/$`, "/abc/", []int{1}},
{`/$`, "/abc", nil},
// multiple matches
{`.`, "abc", []int{1}},
{`(.)`, "abc", []int{1}},
{`.(.)`, "abcd", []int{1}},
{`ab*`, "abbaab", []int{1}},
{`a(b*)`, "abbaab", []int{1}},
// fixed bugs
{`ab$`, "cab", []int{1}},
{`axxb$`, "axxcb", nil},
{`data`, "daXY data", []int{1}},
{`da(.)a$`, "daXY data", []int{1}},
{`zx+`, "zzx", []int{1}},
{`ab$`, "abcab", []int{1}},
{`(aa)*$`, "a", []int{1}},
{`(?:.|(?:.a))`, "", nil},
{`(?:A(?:A|a))`, "Aa", []int{1}},
{`(?:A|(?:A|a))`, "a", []int{1}},
{`(a){0}`, "", []int{1}},
// {`(?-s)(?:(?:^).)`, "\n", nil},
// {`(?s)(?:(?:^).)`, "\n", []int{1}},
// {`(?:(?:^).)`, "\n", nil},
{`\b`, "x", []int{1}},
{`\b`, "xx", []int{1}},
{`\b`, "x y", []int{1}},
{`\b`, "xx yy", []int{1}},
{`\B`, "x", nil},
{`\B`, "xx", []int{1}},
{`\B`, "x y", nil},
{`\B`, "xx yy", []int{1}},
{`(?im)^[abc]+$`, "abcABC", []int{1}},
{`(?im)^[α]+$`, "αΑ", []int{1}},
{`[Aa]BC`, "abc", nil},
{`[Aa]bc`, "abc", []int{1}},
// RE2 tests
{`[^\S\s]`, "abcd", nil},
{`[^\S[:space:]]`, "abcd", nil},
{`[^\D\d]`, "abcd", nil},
{`[^\D[:digit:]]`, "abcd", nil},
{`(?i)\W`, "x", nil},
{`(?i)\W`, "k", nil},
{`(?i)\W`, "s", nil},
// can backslash-escape any punctuation
{`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
`!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
{`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,
`!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
{"\\`", "`", []int{1}},
{"[\\`]+", "`", []int{1}},
// long set of matches (longer than startSize)
{
".",
"qwertyuiopasdfghjklzxcvbnm1234567890",
[]int{1},
},
}
func TestMatch(t *testing.T) {
for _, tt := range matchTests {
re, err := Compile("(?m)" + tt.re)
if err != nil {
t.Errorf("Compile(%#q): %v", tt.re, err)
continue
}
b := []byte(tt.s)
lines := grep(re, b)
if !reflect.DeepEqual(lines, tt.m) {
t.Errorf("grep(%#q, %q) = %v, want %v", tt.re, tt.s, lines, tt.m)
}
}
}
func grep(re *Regexp, b []byte) []int {
var m []int
lineno := 1
for {
i := re.Match(b, true, true)
if i < 0 {
break
}
start := bytes.LastIndex(b[:i], nl) + 1
end := i + 1
if end > len(b) {
end = len(b)
}
lineno += bytes.Count(b[:start], nl)
m = append(m, lineno)
if start < end && b[end-1] == '\n' {
lineno++
}
b = b[end:]
if len(b) == 0 {
break
}
}
return m
}
var grepTests = []struct {
re string
s string
out string
err string
g Grep
}{
{re: `a+`, s: "abc\ndef\nghalloo\n", out: "input:abc\ninput:ghalloo\n"},
{re: `x.*y`, s: "xay\nxa\ny\n", out: "input:xay\n"},
}
func TestGrep(t *testing.T) {
for i, tt := range grepTests {
re, err := Compile("(?m)" + tt.re)
if err != nil {
t.Errorf("Compile(%#q): %v", tt.re, err)
continue
}
g := tt.g
g.Regexp = re
var out, errb bytes.Buffer
g.Stdout = &out
g.Stderr = &errb
g.Reader(strings.NewReader(tt.s), "input")
if out.String() != tt.out || errb.String() != tt.err {
t.Errorf("#%d: grep(%#q, %q) = %q, %q, want %q, %q", i, tt.re, tt.s, out.String(), errb.String(), tt.out, tt.err)
}
}
}