1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var nstateTests
= []struct {
18 {[]uint32{1, 2, 3}, 1},
21 {[]uint32{1, 2, 8}, 0x10FFF},
24 func TestNstateEnc(t
*testing
.T
) {
28 for _
, tt
:= range nstateTests
{
30 n1
.partial
= tt
.partial
31 for _
, id
:= range tt
.q
{
36 if n2
.partial
!= n1
.partial ||
!reflect
.DeepEqual(n1
.q
.Dense(), n2
.q
.Dense()) {
37 t
.Errorf("%v.enc.dec = %v", &n1
, &n2
)
42 var matchTests
= []struct {
47 // Adapted from go/src/pkg/regexp/find_test.go.
48 {`a+`, "abc\ndef\nghi\n", []int{1}},
50 {`^abcdefg`, "abcdefg", []int{1}},
51 {`a+`, "baaab", []int{1}},
52 {"abcd..", "abcdef", []int{1}},
55 {`b`, "abc", []int{1}},
57 {`.*`, "abcdef", []int{1}},
58 {`^`, "abcde", []int{1}},
59 {`$`, "abcde", []int{1}},
60 {`^abcd$`, "abcd", []int{1}},
61 {`^bcd'`, "abcdef", nil},
62 {`^abcd$`, "abcde", nil},
63 {`a+`, "baaab", []int{1}},
64 {`a*`, "baaab", []int{1}},
65 {`[a-z]+`, "abcd", []int{1}},
66 {`[^a-z]+`, "ab1234cd", []int{1}},
67 {`[a\-\]z]+`, "az]-bcz", []int{1}},
68 {`[^\n]+`, "abcd\n", []int{1}},
69 {`[日本語]+`, "日本語日本語", []int{1}},
70 {`日本語+`, "日本語", []int{1}},
71 {`日本語+`, "日本語語語語", []int{1}},
73 {`(a)`, "a", []int{1}},
74 {`(.)(.)`, "日a", []int{1}},
75 {`(.*)`, "", []int{1}},
76 {`(.*)`, "abcd", []int{1}},
77 {`(..)(..)`, "abcd", []int{1}},
78 {`(([^xyz]*)(d))`, "abcd", []int{1}},
79 {`((a|b|c)*(d))`, "abcd", []int{1}},
80 {`(((a|b|c)*)(d))`, "abcd", []int{1}},
81 {`\a\f\r\t\v`, "\a\f\r\t\v", []int{1}},
82 {`[\a\f\n\r\t\v]+`, "\a\f\r\t\v", []int{1}},
84 {`a*(|(b))c*`, "aacc", []int{1}},
85 {`(.*).*`, "ab", []int{1}},
86 {`[.]`, ".", []int{1}},
87 {`/$`, "/abc/", []int{1}},
91 {`.`, "abc", []int{1}},
92 {`(.)`, "abc", []int{1}},
93 {`.(.)`, "abcd", []int{1}},
94 {`ab*`, "abbaab", []int{1}},
95 {`a(b*)`, "abbaab", []int{1}},
98 {`ab$`, "cab", []int{1}},
99 {`axxb$`, "axxcb", nil},
100 {`data`, "daXY data", []int{1}},
101 {`da(.)a$`, "daXY data", []int{1}},
102 {`zx+`, "zzx", []int{1}},
103 {`ab$`, "abcab", []int{1}},
104 {`(aa)*$`, "a", []int{1}},
105 {`(?:.|(?:.a))`, "", nil},
106 {`(?:A(?:A|a))`, "Aa", []int{1}},
107 {`(?:A|(?:A|a))`, "a", []int{1}},
108 {`(a){0}`, "", []int{1}},
109 // {`(?-s)(?:(?:^).)`, "\n", nil},
110 // {`(?s)(?:(?:^).)`, "\n", []int{1}},
111 // {`(?:(?:^).)`, "\n", nil},
112 {`\b`, "x", []int{1}},
113 {`\b`, "xx", []int{1}},
114 {`\b`, "x y", []int{1}},
115 {`\b`, "xx yy", []int{1}},
117 {`\B`, "xx", []int{1}},
119 {`\B`, "xx yy", []int{1}},
120 {`(?im)^[abc]+$`, "abcABC", []int{1}},
121 {`(?im)^[α]+$`, "αΑ", []int{1}},
122 {`[Aa]BC`, "abc", nil},
123 {`[Aa]bc`, "abc", []int{1}},
126 {`[^\S\s]`, "abcd", nil},
127 {`[^\S[:space:]]`, "abcd", nil},
128 {`[^\D\d]`, "abcd", nil},
129 {`[^\D[:digit:]]`, "abcd", nil},
130 {`(?i)\W`, "x", nil},
131 {`(?i)\W`, "k", nil},
132 {`(?i)\W`, "s", nil},
134 // can backslash-escape any punctuation
135 {`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
136 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
137 {`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,
138 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
139 {"\\`", "`", []int{1}},
140 {"[\\`]+", "`", []int{1}},
142 // long set of matches (longer than startSize)
145 "qwertyuiopasdfghjklzxcvbnm1234567890",
150 func TestMatch(t
*testing
.T
) {
151 for _
, tt
:= range matchTests
{
152 re
, err
:= Compile("(?m)" + tt
.re
)
154 t
.Errorf("Compile(%#q): %v", tt
.re
, err
)
159 if !reflect
.DeepEqual(lines
, tt
.m
) {
160 t
.Errorf("grep(%#q, %q) = %v, want %v", tt
.re
, tt
.s
, lines
, tt
.m
)
165 func grep(re
*Regexp
, b
[]byte) []int {
169 i
:= re
.Match(b
, true, true)
173 start
:= bytes
.LastIndex(b
[:i
], nl
) + 1
178 lineno
+= bytes
.Count(b
[:start
], nl
)
179 m
= append(m
, lineno
)
180 if start
< end
&& b
[end
-1] == '\n' {
191 var grepTests
= []struct {
198 {re
: `a+`, s
: "abc\ndef\nghalloo\n", out
: "input:abc\ninput:ghalloo\n"},
199 {re
: `x.*y`, s
: "xay\nxa\ny\n", out
: "input:xay\n"},
202 func TestGrep(t
*testing
.T
) {
203 for i
, tt
:= range grepTests
{
204 re
, err
:= Compile("(?m)" + tt
.re
)
206 t
.Errorf("Compile(%#q): %v", tt
.re
, err
)
211 var out
, errb bytes
.Buffer
214 g
.Reader(strings
.NewReader(tt
.s
), "input")
215 if out
.String() != tt
.out || errb
.String() != tt
.err
{
216 t
.Errorf("#%d: grep(%#q, %q) = %q, %q, want %q, %q", i
, tt
.re
, tt
.s
, out
.String(), errb
.String(), tt
.out
, tt
.err
)