show: make highlight legible
[debiancodesearch.git] / regexp / regexp_test.go
blob1f72c586553aee9820b696e936e25f3513b3dba1
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.
5 package regexp
7 import (
8 "bytes"
9 "fmt"
10 "reflect"
11 "strings"
12 "testing"
15 var nstateTests = []struct {
16 q []uint32
17 partial rune
19 {[]uint32{1, 2, 3}, 1},
20 {[]uint32{1}, 1},
21 {[]uint32{}, 0},
22 {[]uint32{1, 2, 8}, 0x10FFF},
25 func TestNstateEnc(t *testing.T) {
26 var n1, n2 nstate
27 n1.q.Init(10)
28 n2.q.Init(10)
29 for _, tt := range nstateTests {
30 n1.q.Reset()
31 n1.partial = tt.partial
32 for _, id := range tt.q {
33 n1.q.Add(id)
35 enc := n1.enc()
36 n2.dec(enc)
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 {
44 re string
45 s string
46 m []int
48 // Adapted from go/src/pkg/regexp/find_test.go.
49 {`a+`, "abc\ndef\nghi\n", []int{1}},
50 {``, ``, []int{1}},
51 {`^abcdefg`, "abcdefg", []int{1}},
52 {`a+`, "baaab", []int{1}},
53 {"abcd..", "abcdef", []int{1}},
54 {`a`, "a", []int{1}},
55 {`x`, "y", nil},
56 {`b`, "abc", []int{1}},
57 {`.`, "a", []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}},
73 {`()`, "", []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}},
89 {`/$`, "/abc", nil},
91 // multiple matches
92 {`.`, "abc", []int{1}},
93 {`(.)`, "abc", []int{1}},
94 {`.(.)`, "abcd", []int{1}},
95 {`ab*`, "abbaab", []int{1}},
96 {`a(b*)`, "abbaab", []int{1}},
98 // fixed bugs
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}},
117 {`\B`, "x", nil},
118 {`\B`, "xx", []int{1}},
119 {`\B`, "x y", nil},
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}},
126 // RE2 tests
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)
145 ".",
146 "qwertyuiopasdfghjklzxcvbnm1234567890",
147 []int{1},
151 func TestMatch(t *testing.T) {
152 for _, tt := range matchTests {
153 re, err := Compile("(?m)" + tt.re)
154 if err != nil {
155 t.Errorf("Compile(%#q): %v", tt.re, err)
156 continue
158 b := []byte(tt.s)
159 lines := grep(re, b)
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 {
167 var m []int
168 lineno := 1
169 for {
170 i := re.Match(b, true, true)
171 if i < 0 {
172 break
174 start := bytes.LastIndex(b[:i], nl) + 1
175 end := i + 1
176 if end > len(b) {
177 end = len(b)
179 lineno += bytes.Count(b[:start], nl)
180 m = append(m, lineno)
181 if start < end && b[end-1] == '\n' {
182 lineno++
184 b = b[end:]
185 if len(b) == 0 {
186 break
189 return m
192 var grepTests = []struct {
193 re string
194 s string
195 out string
196 err string
197 g Grep
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)
206 if err != nil {
207 t.Errorf("Compile(%#q): %v", tt.re, err)
208 continue
210 g := tt.g
211 g.Regexp = re
212 var out, errb bytes.Buffer
213 g.Stdout = &out
214 g.Stderr = &errb
215 matches := g.Reader(strings.NewReader(tt.s), "input")
216 var mstr string
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)