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.
15 var nstateTests
= []struct {
19 {[]uint32{1, 2, 3}, 1},
22 {[]uint32{1, 2, 8}, 0x10FFF},
25 func TestNstateEnc(t
*testing
.T
) {
29 for _
, tt
:= range nstateTests
{
31 n1
.partial
= tt
.partial
32 for _
, id
:= range tt
.q
{
37 if n2
.partial
!= n1
.partial ||
!reflect
.DeepEqual(n1
.q
.Dense(), n2
.q
.Dense()) {
38 t
.Errorf("%v.enc.dec = %v", &n1
, &n2
)
43 var matchTests
= []struct {
48 // Adapted from go/src/pkg/regexp/find_test.go.
49 {`a+`, "abc\ndef\nghi\n", []int{1}},
51 {`^abcdefg`, "abcdefg", []int{1}},
52 {`a+`, "baaab", []int{1}},
53 {"abcd..", "abcdef", []int{1}},
56 {`b`, "abc", []int{1}},
58 {`.*`, "abcdef", []int{1}},
59 {`^`, "abcde", []int{1}},
60 {`$`, "abcde", []int{1}},
61 {`^abcd$`, "abcd", []int{1}},
62 {`^bcd'`, "abcdef", nil},
63 {`^abcd$`, "abcde", nil},
64 {`a+`, "baaab", []int{1}},
65 {`a*`, "baaab", []int{1}},
66 {`[a-z]+`, "abcd", []int{1}},
67 {`[^a-z]+`, "ab1234cd", []int{1}},
68 {`[a\-\]z]+`, "az]-bcz", []int{1}},
69 {`[^\n]+`, "abcd\n", []int{1}},
70 {`[日本語]+`, "日本語日本語", []int{1}},
71 {`日本語+`, "日本語", []int{1}},
72 {`日本語+`, "日本語語語語", []int{1}},
74 {`(a)`, "a", []int{1}},
75 {`(.)(.)`, "日a", []int{1}},
76 {`(.*)`, "", []int{1}},
77 {`(.*)`, "abcd", []int{1}},
78 {`(..)(..)`, "abcd", []int{1}},
79 {`(([^xyz]*)(d))`, "abcd", []int{1}},
80 {`((a|b|c)*(d))`, "abcd", []int{1}},
81 {`(((a|b|c)*)(d))`, "abcd", []int{1}},
82 {`\a\f\r\t\v`, "\a\f\r\t\v", []int{1}},
83 {`[\a\f\n\r\t\v]+`, "\a\f\r\t\v", []int{1}},
85 {`a*(|(b))c*`, "aacc", []int{1}},
86 {`(.*).*`, "ab", []int{1}},
87 {`[.]`, ".", []int{1}},
88 {`/$`, "/abc/", []int{1}},
92 {`.`, "abc", []int{1}},
93 {`(.)`, "abc", []int{1}},
94 {`.(.)`, "abcd", []int{1}},
95 {`ab*`, "abbaab", []int{1}},
96 {`a(b*)`, "abbaab", []int{1}},
99 {`ab$`, "cab", []int{1}},
100 {`axxb$`, "axxcb", nil},
101 {`data`, "daXY data", []int{1}},
102 {`da(.)a$`, "daXY data", []int{1}},
103 {`zx+`, "zzx", []int{1}},
104 {`ab$`, "abcab", []int{1}},
105 {`(aa)*$`, "a", []int{1}},
106 {`(?:.|(?:.a))`, "", nil},
107 {`(?:A(?:A|a))`, "Aa", []int{1}},
108 {`(?:A|(?:A|a))`, "a", []int{1}},
109 {`(a){0}`, "", []int{1}},
110 // {`(?-s)(?:(?:^).)`, "\n", nil},
111 // {`(?s)(?:(?:^).)`, "\n", []int{1}},
112 // {`(?:(?:^).)`, "\n", nil},
113 {`\b`, "x", []int{1}},
114 {`\b`, "xx", []int{1}},
115 {`\b`, "x y", []int{1}},
116 {`\b`, "xx yy", []int{1}},
118 {`\B`, "xx", []int{1}},
120 {`\B`, "xx yy", []int{1}},
121 {`(?im)^[abc]+$`, "abcABC", []int{1}},
122 {`(?im)^[α]+$`, "αΑ", []int{1}},
123 {`[Aa]BC`, "abc", nil},
124 {`[Aa]bc`, "abc", []int{1}},
127 {`[^\S\s]`, "abcd", nil},
128 {`[^\S[:space:]]`, "abcd", nil},
129 {`[^\D\d]`, "abcd", nil},
130 {`[^\D[:digit:]]`, "abcd", nil},
131 {`(?i)\W`, "x", nil},
132 {`(?i)\W`, "k", nil},
133 {`(?i)\W`, "s", nil},
135 // can backslash-escape any punctuation
136 {`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
137 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
138 {`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,
139 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, []int{1}},
140 {"\\`", "`", []int{1}},
141 {"[\\`]+", "`", []int{1}},
143 // long set of matches (longer than startSize)
146 "qwertyuiopasdfghjklzxcvbnm1234567890",
151 func TestMatch(t
*testing
.T
) {
152 for _
, tt
:= range matchTests
{
153 re
, err
:= Compile("(?m)" + tt
.re
)
155 t
.Errorf("Compile(%#q): %v", tt
.re
, err
)
160 if !reflect
.DeepEqual(lines
, tt
.m
) {
161 t
.Errorf("grep(%#q, %q) = %v, want %v", tt
.re
, tt
.s
, lines
, tt
.m
)
166 func grep(re
*Regexp
, b
[]byte) []int {
170 i
:= re
.Match(b
, true, true)
174 start
:= bytes
.LastIndex(b
[:i
], nl
) + 1
179 lineno
+= bytes
.Count(b
[:start
], nl
)
180 m
= append(m
, lineno
)
181 if start
< end
&& b
[end
-1] == '\n' {
192 var grepTests
= []struct {
199 {re
: `a+`, s
: "abc\ndef\nghalloo\n", out
: "input:abc\ninput:ghalloo\n"},
200 {re
: `x.*y`, s
: "xay\nxa\ny\n", out
: "input:xay\n"},
203 func TestGrep(t
*testing
.T
) {
204 for i
, tt
:= range grepTests
{
205 re
, err
:= Compile("(?m)" + tt
.re
)
207 t
.Errorf("Compile(%#q): %v", tt
.re
, err
)
212 var out
, errb bytes
.Buffer
215 matches
:= g
.Reader(strings
.NewReader(tt
.s
), "input")
217 for _
, match
:= range matches
{
218 mstr
= mstr
+ fmt
.Sprintf("%s:%s\n", match
.Path
, match
.Context
)
220 if mstr
!= tt
.out || errb
.String() != tt
.err
{
221 t
.Errorf("#%d: grep(%#q, %q) = %q, %q, want %q, %q", i
, tt
.re
, tt
.s
, out
.String(), errb
.String(), tt
.out
, tt
.err
)