OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package regexp | 5 package regexp |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | |
9 "strings" | 8 "strings" |
10 "testing" | 9 "testing" |
11 ) | 10 ) |
12 | 11 |
13 var good_re = []string{ | 12 var good_re = []string{ |
14 ``, | 13 ``, |
15 `.`, | 14 `.`, |
16 `^.$`, | 15 `^.$`, |
17 `a`, | 16 `a`, |
18 `a*`, | 17 `a*`, |
19 `a+`, | 18 `a+`, |
20 `a?`, | 19 `a?`, |
21 `a|b`, | 20 `a|b`, |
22 `a*|b*`, | 21 `a*|b*`, |
23 `(a*|b)(c*|d)`, | 22 `(a*|b)(c*|d)`, |
24 `[a-z]`, | 23 `[a-z]`, |
25 `[a-abc-c\-\]\[]`, | 24 `[a-abc-c\-\]\[]`, |
26 `[a-z]+`, | 25 `[a-z]+`, |
27 `[]`, | 26 `[]`, |
28 `[abc]`, | 27 `[abc]`, |
29 `[^1234]`, | 28 `[^1234]`, |
30 `[^\n]`, | 29 `[^\n]`, |
31 `\!\\`, | 30 `\!\\`, |
32 } | 31 } |
33 | 32 |
34 type stringError struct { | 33 type stringError struct { |
35 re string | 34 re string |
36 » err os.Error | 35 » err error |
37 } | 36 } |
38 | 37 |
39 var bad_re = []stringError{ | 38 var bad_re = []stringError{ |
40 {`*`, ErrBareClosure}, | 39 {`*`, ErrBareClosure}, |
41 {`+`, ErrBareClosure}, | 40 {`+`, ErrBareClosure}, |
42 {`?`, ErrBareClosure}, | 41 {`?`, ErrBareClosure}, |
43 {`(abc`, ErrUnmatchedLpar}, | 42 {`(abc`, ErrUnmatchedLpar}, |
44 {`abc)`, ErrUnmatchedRpar}, | 43 {`abc)`, ErrUnmatchedRpar}, |
45 {`x[a-z`, ErrUnmatchedLbkt}, | 44 {`x[a-z`, ErrUnmatchedLbkt}, |
46 {`abc]`, ErrUnmatchedRbkt}, | 45 {`abc]`, ErrUnmatchedRbkt}, |
47 {`[z-a]`, ErrBadRange}, | 46 {`[z-a]`, ErrBadRange}, |
48 {`abc\`, ErrExtraneousBackslash}, | 47 {`abc\`, ErrExtraneousBackslash}, |
49 {`a**`, ErrBadClosure}, | 48 {`a**`, ErrBadClosure}, |
50 {`a*+`, ErrBadClosure}, | 49 {`a*+`, ErrBadClosure}, |
51 {`a??`, ErrBadClosure}, | 50 {`a??`, ErrBadClosure}, |
52 {`\x`, ErrBadBackslash}, | 51 {`\x`, ErrBadBackslash}, |
53 } | 52 } |
54 | 53 |
55 func compileTest(t *testing.T, expr string, error os.Error) *Regexp { | 54 func compileTest(t *testing.T, expr string, error error) *Regexp { |
56 re, err := Compile(expr) | 55 re, err := Compile(expr) |
57 if err != error { | 56 if err != error { |
58 » » t.Error("compiling `", expr, "`; unexpected error: ", err.String
()) | 57 » » t.Error("compiling `", expr, "`; unexpected error: ", err.Error(
)) |
59 } | 58 } |
60 return re | 59 return re |
61 } | 60 } |
62 | 61 |
63 func TestGoodCompile(t *testing.T) { | 62 func TestGoodCompile(t *testing.T) { |
64 for i := 0; i < len(good_re); i++ { | 63 for i := 0; i < len(good_re); i++ { |
65 compileTest(t, good_re[i], nil) | 64 compileTest(t, good_re[i], nil) |
66 } | 65 } |
67 } | 66 } |
68 | 67 |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 x := []byte("abcdefghijklmnopqrstuvwxyz") | 416 x := []byte("abcdefghijklmnopqrstuvwxyz") |
418 for i := 0; i < 15; i++ { | 417 for i := 0; i < 15; i++ { |
419 x = append(x, x...) | 418 x = append(x, x...) |
420 } | 419 } |
421 re := MustCompile("^.bc(d|e)") | 420 re := MustCompile("^.bc(d|e)") |
422 b.StartTimer() | 421 b.StartTimer() |
423 for i := 0; i < b.N; i++ { | 422 for i := 0; i < b.N; i++ { |
424 re.Match(x) | 423 re.Match(x) |
425 } | 424 } |
426 } | 425 } |
OLD | NEW |