Debian Codegraph @

find_test.go

raw symbols SCIP
1 package re2
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 // For each pattern/text pair, what is the expected output of each function?
9 // We can derive the textual results from the indexed results, the non-submatch
10 // results from the submatched results, the single results from the 'all' results,
11 // and the byte results from the string results. Therefore the table includes
12 // only the FindAllStringSubmatchIndex result.
13 type FindTest struct {
14 pat string
15 text string
16 matches [][]int
17 }
18
19 func (t FindTest) String() string {
20 return fmt.Sprintf("pat: %#q text: %#q", t.pat, t.text)
21 }
22
23 var findTests = []FindTest{
24 {``, ``, build(1, 0, 0)},
25 {`^abcdefg`, "abcdefg", build(1, 0, 7)},
26 {`a+`, "baaab", build(1, 1, 4)},
27 {"abcd..", "abcdef", build(1, 0, 6)},
28 {`a`, "a", build(1, 0, 1)},
29 {`x`, "y", nil},
30 {`b`, "abc", build(1, 1, 2)},
31 {`.`, "a", build(1, 0, 1)},
32 {`.*`, "abcdef", build(1, 0, 6)},
33 {`^`, "abcde", build(1, 0, 0)},
34 {`$`, "abcde", build(1, 5, 5)},
35 {`^abcd$`, "abcd", build(1, 0, 4)},
36 {`^bcd'`, "abcdef", nil},
37 {`^abcd$`, "abcde", nil},
38 {`a+`, "baaab", build(1, 1, 4)},
39 {`a*`, "baaab", build(3, 0, 0, 1, 4, 5, 5)},
40 {`[a-z]+`, "abcd", build(1, 0, 4)},
41 {`[^a-z]+`, "ab1234cd", build(1, 2, 6)},
42 {`[a\-\]z]+`, "az]-bcz", build(2, 0, 4, 6, 7)},
43 {`[^\n]+`, "abcd\n", build(1, 0, 4)},
44 {`[日本語]+`, "日本語日本語", build(1, 0, 18)},
45 {`日本語+`, "日本語", build(1, 0, 9)},
46 {`日本語+`, "日本語語語語", build(1, 0, 18)},
47 {`()`, "", build(1, 0, 0, 0, 0)},
48 {`(a)`, "a", build(1, 0, 1, 0, 1)},
49 {`(.)(.)`, "日a", build(1, 0, 4, 0, 3, 3, 4)},
50 {`(.*)`, "", build(1, 0, 0, 0, 0)},
51 {`(.*)`, "abcd", build(1, 0, 4, 0, 4)},
52 {`(..)(..)`, "abcd", build(1, 0, 4, 0, 2, 2, 4)},
53 {`(([^xyz]*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 3, 4)},
54 {`((a|b|c)*(d))`, "abcd", build(1, 0, 4, 0, 4, 2, 3, 3, 4)},
55 {`(((a|b|c)*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 2, 3, 3, 4)},
56 {`\a\f\n\r\t\v`, "\a\f\n\r\t\v", build(1, 0, 6)},
57 {`[\a\f\n\r\t\v]+`, "\a\f\n\r\t\v", build(1, 0, 6)},
58
59 {`a*(|(b))c*`, "aacc", build(1, 0, 4, 2, 2, -1, -1)},
60 {`(.*).*`, "ab", build(1, 0, 2, 0, 2)},
61 {`[.]`, ".", build(1, 0, 1)},
62 {`/$`, "/abc/", build(1, 4, 5)},
63 {`/$`, "/abc", nil},
64
65 // multiple matches
66 {`.`, "abc", build(3, 0, 1, 1, 2, 2, 3)},
67 {`(.)`, "abc", build(3, 0, 1, 0, 1, 1, 2, 1, 2, 2, 3, 2, 3)},
68 {`.(.)`, "abcd", build(2, 0, 2, 1, 2, 2, 4, 3, 4)},
69 {`ab*`, "abbaab", build(3, 0, 3, 3, 4, 4, 6)},
70 {`a(b*)`, "abbaab", build(3, 0, 3, 1, 3, 3, 4, 4, 4, 4, 6, 5, 6)},
71
72 // fixed bugs
73 {`ab$`, "cab", build(1, 1, 3)},
74 {`axxb$`, "axxcb", nil},
75 {`data`, "daXY data", build(1, 5, 9)},
76 {`da(.)a$`, "daXY data", build(1, 5, 9, 7, 8)},
77 {`zx+`, "zzx", build(1, 1, 3)},
78 {`ab$`, "abcab", build(1, 3, 5)},
79 {`(aa)*$`, "a", build(1, 1, 1, -1, -1)},
80 {`(?:.|(?:.a))`, "", nil},
81 {`(?:A(?:A|a))`, "Aa", build(1, 0, 2)},
82 {`(?:A|(?:A|a))`, "a", build(1, 0, 1)},
83 {`(a){0}`, "", build(1, 0, 0, -1, -1)},
84 {`(?-s)(?:(?:^).)`, "\n", nil},
85 {`(?s)(?:(?:^).)`, "\n", build(1, 0, 1)},
86 {`(?:(?:^).)`, "\n", nil},
87 {`\b`, "x", build(2, 0, 0, 1, 1)},
88 {`\b`, "xx", build(2, 0, 0, 2, 2)},
89 {`\b`, "x y", build(4, 0, 0, 1, 1, 2, 2, 3, 3)},
90 {`\b`, "xx yy", build(4, 0, 0, 2, 2, 3, 3, 5, 5)},
91 {`\B`, "x", nil},
92 {`\B`, "xx", build(1, 1, 1)},
93 {`\B`, "x y", nil},
94 {`\B`, "xx yy", build(2, 1, 1, 4, 4)},
95 {`(|a)*`, "aa", build(3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2)},
96
97 // RE2 tests
98 {`[^\S\s]`, "abcd", nil},
99 {`[^\S[:space:]]`, "abcd", nil},
100 {`[^\D\d]`, "abcd", nil},
101 {`[^\D[:digit:]]`, "abcd", nil},
102 {`(?i)\W`, "x", nil},
103 {`(?i)\W`, "k", nil},
104 {`(?i)\W`, "s", nil},
105
106 // Multibyte characters -- verify that we don't try to match in the middle
107 // of a character.
108 {"[a-c]*", "\u65e5", build(2, 0, 0, 3, 3)},
109 {"[^\u65e5]", "abc\u65e5def", build(6, 0, 1, 1, 2, 2, 3, 6, 7, 7, 8, 8, 9)},
110
111 // can backslash-escape any punctuation
112 {
113 `\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
114 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31),
115 },
116 {
117 `[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,
118 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31),
119 },
120 {"\\`", "`", build(1, 0, 1)},
121 {"[\\`]+", "`", build(1, 0, 1)},
122
123 // GAP - re2 discards non-utf8 bytes in input strings, so they can never be matched.
124 // {"\ufffd", "\xff", build(1, 0, 1)},
125 // {"\ufffd", "hello\xffworld", build(1, 5, 6)},
126 // {`.*`, "hello\xffworld", build(1, 0, 11)},
127 // {`\x{fffd}`, "\xc2\x00", build(1, 0, 1)},
128 // {"[\ufffd]", "\xff", build(1, 0, 1)},
129 // {`[\x{fffd}]`, "\xc2\x00", build(1, 0, 1)},
130
131 // long set of matches (longer than startSize)
132 {
133 ".",
134 "qwertyuiopasdfghjklzxcvbnm1234567890",
135 build(36, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
136 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,
137 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
138 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36),
139 },
140 }
141
142 // build is a helper to construct a [][]int by extracting n sequences from x.
143 // This represents n matches with len(x)/n submatches each.
144 func build(n int, x ...int) [][]int {
145 ret := make([][]int, n)
146 runLength := len(x) / n
147 j := 0
148 for i := range ret {
149 ret[i] = make([]int, runLength)
150 copy(ret[i], x[j:])
151 j += runLength
152 if j > len(x) {
153 panic("invalid build entry")
154 }
155 }
156 return ret
157 }
158
159 // First the simple cases.
160
161 func TestFind(t *testing.T) {
162 for _, test := range findTests {
163 re := MustCompile(test.pat)
164 if re.String() != test.pat {
165 t.Errorf("String() = `%s`; should be `%s`", re.String(), test.pat)
166 }
167 result := re.Find([]byte(test.text))
168 switch {
169 case len(test.matches) == 0 && len(result) == 0:
170 // ok
171 case test.matches == nil && result != nil:
172 t.Errorf("expected no match; got one: %s", test)
173 case test.matches != nil && result == nil:
174 t.Errorf("expected match; got none: %s", test)
175 case test.matches != nil && result != nil:
176 expect := test.text[test.matches[0][0]:test.matches[0][1]]
177 if len(result) != cap(result) {
178 t.Errorf("expected capacity %d got %d: %s", len(result), cap(result), test)
179 }
180 if expect != string(result) {
181 t.Errorf("expected %q got %q: %s", expect, result, test)
182 }
183 }
184 }
185 }
186
187 func TestFindString(t *testing.T) {
188 for _, test := range findTests {
189 result := MustCompile(test.pat).FindString(test.text)
190 switch {
191 case len(test.matches) == 0 && len(result) == 0:
192 // ok
193 case test.matches == nil && result != "":
194 t.Errorf("expected no match; got one: %s", test)
195 case test.matches != nil && result == "":
196 // Tricky because an empty result has two meanings: no match or empty match.
197 if test.matches[0][0] != test.matches[0][1] {
198 t.Errorf("expected match; got none: %s", test)
199 }
200 case test.matches != nil && result != "":
201 expect := test.text[test.matches[0][0]:test.matches[0][1]]
202 if expect != result {
203 t.Errorf("expected %q got %q: %s", expect, result, test)
204 }
205 }
206 }
207 }
208
209 func testFindIndex(test *FindTest, result []int, t *testing.T) {
210 switch {
211 case len(test.matches) == 0 && len(result) == 0:
212 // ok
213 case test.matches == nil && result != nil:
214 t.Errorf("expected no match; got one: %s", test)
215 case test.matches != nil && result == nil:
216 t.Errorf("expected match; got none: %s", test)
217 case test.matches != nil && result != nil:
218 expect := test.matches[0]
219 if expect[0] != result[0] || expect[1] != result[1] {
220 t.Errorf("expected %v got %v: %s", expect, result, test)
221 }
222 }
223 }
224
225 func TestFindIndex(t *testing.T) {
226 for _, test := range findTests {
227 testFindIndex(&test, MustCompile(test.pat).FindIndex([]byte(test.text)), t)
228 }
229 }
230
231 func TestFindStringIndex(t *testing.T) {
232 for _, test := range findTests {
233 testFindIndex(&test, MustCompile(test.pat).FindStringIndex(test.text), t)
234 }
235 }
236
237 // Now come the simple All cases.
238
239 func TestFindAll(t *testing.T) {
240 for _, test := range findTests {
241 result := MustCompile(test.pat).FindAll([]byte(test.text), -1)
242 switch {
243 case test.matches == nil && result == nil:
244 // ok
245 case test.matches == nil && result != nil:
246 t.Errorf("expected no match; got one: %s", test)
247 case test.matches != nil && result == nil:
248 t.Fatalf("expected match; got none: %s", test)
249 case test.matches != nil && result != nil:
250 if len(test.matches) != len(result) {
251 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
252 continue
253 }
254 for k, e := range test.matches {
255 got := result[k]
256 if len(got) != cap(got) {
257 t.Errorf("match %d: expected capacity %d got %d: %s", k, len(got), cap(got), test)
258 }
259 expect := test.text[e[0]:e[1]]
260 if expect != string(got) {
261 t.Errorf("match %d: expected %q got %q: %s", k, expect, got, test)
262 }
263 }
264 }
265 }
266 }
267
268 func TestFindAllString(t *testing.T) {
269 for _, test := range findTests {
270 result := MustCompile(test.pat).FindAllString(test.text, -1)
271 switch {
272 case test.matches == nil && result == nil:
273 // ok
274 case test.matches == nil && result != nil:
275 t.Errorf("expected no match; got one: %s", test)
276 case test.matches != nil && result == nil:
277 t.Errorf("expected match; got none: %s", test)
278 case test.matches != nil && result != nil:
279 if len(test.matches) != len(result) {
280 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
281 continue
282 }
283 for k, e := range test.matches {
284 expect := test.text[e[0]:e[1]]
285 if expect != result[k] {
286 t.Errorf("expected %q got %q: %s", expect, result, test)
287 }
288 }
289 }
290 }
291 }
292
293 func testFindAllIndex(test *FindTest, result [][]int, t *testing.T) {
294 switch {
295 case test.matches == nil && result == nil:
296 // ok
297 case test.matches == nil && result != nil:
298 t.Errorf("expected no match; got one: %s", test)
299 case test.matches != nil && result == nil:
300 t.Errorf("expected match; got none: %s", test)
301 case test.matches != nil && result != nil:
302 if len(test.matches) != len(result) {
303 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
304 return
305 }
306 for k, e := range test.matches {
307 if e[0] != result[k][0] || e[1] != result[k][1] {
308 t.Errorf("match %d: expected %v got %v: %s", k, e, result[k], test)
309 }
310 }
311 }
312 }
313
314 func TestFindAllIndex(t *testing.T) {
315 for _, test := range findTests {
316 testFindAllIndex(&test, MustCompile(test.pat).FindAllIndex([]byte(test.text), -1), t)
317 }
318 }
319
320 func TestFindAllStringIndex(t *testing.T) {
321 for _, test := range findTests {
322 testFindAllIndex(&test, MustCompile(test.pat).FindAllStringIndex(test.text, -1), t)
323 }
324 }
325
326 // Now come the Submatch cases.
327
328 func testSubmatchBytes(test *FindTest, n int, submatches []int, result [][]byte, t *testing.T) {
329 if len(submatches) != len(result)*2 {
330 t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test)
331 return
332 }
333 for k := 0; k < len(submatches); k += 2 {
334 if submatches[k] == -1 {
335 if result[k/2] != nil {
336 t.Errorf("match %d: expected nil got %q: %s", n, result, test)
337 }
338 continue
339 }
340 got := result[k/2]
341 if len(got) != cap(got) {
342 t.Errorf("match %d: expected capacity %d got %d: %s", n, len(got), cap(got), test)
343 return
344 }
345 expect := test.text[submatches[k]:submatches[k+1]]
346 if expect != string(got) {
347 t.Errorf("match %d: expected %q got %q: %s", n, expect, got, test)
348 return
349 }
350 }
351 }
352
353 func TestFindSubmatch(t *testing.T) {
354 for _, test := range findTests {
355 result := MustCompile(test.pat).FindSubmatch([]byte(test.text))
356 switch {
357 case test.matches == nil && result == nil:
358 // ok
359 case test.matches == nil && result != nil:
360 t.Errorf("expected no match; got one: %s", test)
361 case test.matches != nil && result == nil:
362 t.Errorf("expected match; got none: %s", test)
363 case test.matches != nil && result != nil:
364 testSubmatchBytes(&test, 0, test.matches[0], result, t)
365 }
366 }
367 }
368
369 func testSubmatchString(test *FindTest, n int, submatches []int, result []string, t *testing.T) {
370 if len(submatches) != len(result)*2 {
371 t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test)
372 return
373 }
374 for k := 0; k < len(submatches); k += 2 {
375 if submatches[k] == -1 {
376 if result[k/2] != "" {
377 t.Errorf("match %d: expected nil got %q: %s", n, result, test)
378 }
379 continue
380 }
381 expect := test.text[submatches[k]:submatches[k+1]]
382 if expect != result[k/2] {
383 t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test)
384 return
385 }
386 }
387 }
388
389 func TestFindStringSubmatch(t *testing.T) {
390 for _, test := range findTests {
391 result := MustCompile(test.pat).FindStringSubmatch(test.text)
392 switch {
393 case test.matches == nil && result == nil:
394 // ok
395 case test.matches == nil && result != nil:
396 t.Errorf("expected no match; got one: %s", test)
397 case test.matches != nil && result == nil:
398 t.Errorf("expected match; got none: %s", test)
399 case test.matches != nil && result != nil:
400 testSubmatchString(&test, 0, test.matches[0], result, t)
401 }
402 }
403 }
404
405 func testSubmatchIndices(test *FindTest, n int, expect, result []int, t *testing.T) {
406 if len(expect) != len(result) {
407 t.Errorf("match %d: expected %d matches; got %d: %s", n, len(expect)/2, len(result)/2, test)
408 return
409 }
410 for k, e := range expect {
411 if e != result[k] {
412 t.Errorf("match %d: submatch error: expected %v got %v: %s", n, expect, result, test)
413 }
414 }
415 }
416
417 func testFindSubmatchIndex(test *FindTest, result []int, t *testing.T) {
418 switch {
419 case test.matches == nil && result == nil:
420 // ok
421 case test.matches == nil && result != nil:
422 t.Errorf("expected no match; got one: %s", test)
423 case test.matches != nil && result == nil:
424 t.Errorf("expected match; got none: %s", test)
425 case test.matches != nil && result != nil:
426 testSubmatchIndices(test, 0, test.matches[0], result, t)
427 }
428 }
429
430 func TestFindSubmatchIndex(t *testing.T) {
431 for _, test := range findTests {
432 testFindSubmatchIndex(&test, MustCompile(test.pat).FindSubmatchIndex([]byte(test.text)), t)
433 }
434 }
435
436 func TestFindStringSubmatchIndex(t *testing.T) {
437 for _, test := range findTests {
438 testFindSubmatchIndex(&test, MustCompile(test.pat).FindStringSubmatchIndex(test.text), t)
439 }
440 }
441
442 // Now come the monster AllSubmatch cases.
443
444 func TestFindAllSubmatch(t *testing.T) {
445 for _, test := range findTests {
446 result := MustCompile(test.pat).FindAllSubmatch([]byte(test.text), -1)
447 switch {
448 case test.matches == nil && result == nil:
449 // ok
450 case test.matches == nil && result != nil:
451 t.Errorf("expected no match; got one: %s", test)
452 case test.matches != nil && result == nil:
453 t.Errorf("expected match; got none: %s", test)
454 case len(test.matches) != len(result):
455 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
456 case test.matches != nil && result != nil:
457 for k, match := range test.matches {
458 testSubmatchBytes(&test, k, match, result[k], t)
459 }
460 }
461 }
462 }
463
464 func TestFindAllStringSubmatch(t *testing.T) {
465 for _, test := range findTests {
466 result := MustCompile(test.pat).FindAllStringSubmatch(test.text, -1)
467 switch {
468 case test.matches == nil && result == nil:
469 // ok
470 case test.matches == nil && result != nil:
471 t.Errorf("expected no match; got one: %s", test)
472 case test.matches != nil && result == nil:
473 t.Errorf("expected match; got none: %s", test)
474 case len(test.matches) != len(result):
475 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
476 case test.matches != nil && result != nil:
477 for k, match := range test.matches {
478 testSubmatchString(&test, k, match, result[k], t)
479 }
480 }
481 }
482 }
483
484 func testFindAllSubmatchIndex(test *FindTest, result [][]int, t *testing.T) {
485 switch {
486 case test.matches == nil && result == nil:
487 // ok
488 case test.matches == nil && result != nil:
489 t.Errorf("expected no match; got one: %s", test)
490 case test.matches != nil && result == nil:
491 t.Errorf("expected match; got none: %s", test)
492 case len(test.matches) != len(result):
493 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)
494 case test.matches != nil && result != nil:
495 for k, match := range test.matches {
496 testSubmatchIndices(test, k, match, result[k], t)
497 }
498 }
499 }
500
501 func TestFindAllSubmatchIndex(t *testing.T) {
502 for _, test := range findTests {
503 testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllSubmatchIndex([]byte(test.text), -1), t)
504 }
505 }
506
507 func TestFindAllStringSubmatchIndex(t *testing.T) {
508 for _, test := range findTests {
509 testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllStringSubmatchIndex(test.text, -1), t)
510 }
511 }