OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [regexp/] [syntax/] [simplify_test.go] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package syntax_test
6
 
7
import . "regexp/syntax"
8
import "testing"
9
 
10
var simplifyTests = []struct {
11
        Regexp string
12
        Simple string
13
}{
14
        // Already-simple constructs
15
        {`a`, `a`},
16
        {`ab`, `ab`},
17
        {`a|b`, `[a-b]`},
18
        {`ab|cd`, `ab|cd`},
19
        {`(ab)*`, `(ab)*`},
20
        {`(ab)+`, `(ab)+`},
21
        {`(ab)?`, `(ab)?`},
22
        {`.`, `(?s:.)`},
23
        {`^`, `^`},
24
        {`$`, `$`},
25
        {`[ac]`, `[ac]`},
26
        {`[^ac]`, `[^ac]`},
27
 
28
        // Posix character classes
29
        {`[[:alnum:]]`, `[0-9A-Za-z]`},
30
        {`[[:alpha:]]`, `[A-Za-z]`},
31
        {`[[:blank:]]`, `[\t ]`},
32
        {`[[:cntrl:]]`, `[\x00-\x1f\x7f]`},
33
        {`[[:digit:]]`, `[0-9]`},
34
        {`[[:graph:]]`, `[!-~]`},
35
        {`[[:lower:]]`, `[a-z]`},
36
        {`[[:print:]]`, `[ -~]`},
37
        {`[[:punct:]]`, "[!-/:-@\\[-`\\{-~]"},
38
        {`[[:space:]]`, `[\t-\r ]`},
39
        {`[[:upper:]]`, `[A-Z]`},
40
        {`[[:xdigit:]]`, `[0-9A-Fa-f]`},
41
 
42
        // Perl character classes
43
        {`\d`, `[0-9]`},
44
        {`\s`, `[\t-\n\f-\r ]`},
45
        {`\w`, `[0-9A-Z_a-z]`},
46
        {`\D`, `[^0-9]`},
47
        {`\S`, `[^\t-\n\f-\r ]`},
48
        {`\W`, `[^0-9A-Z_a-z]`},
49
        {`[\d]`, `[0-9]`},
50
        {`[\s]`, `[\t-\n\f-\r ]`},
51
        {`[\w]`, `[0-9A-Z_a-z]`},
52
        {`[\D]`, `[^0-9]`},
53
        {`[\S]`, `[^\t-\n\f-\r ]`},
54
        {`[\W]`, `[^0-9A-Z_a-z]`},
55
 
56
        // Posix repetitions
57
        {`a{1}`, `a`},
58
        {`a{2}`, `aa`},
59
        {`a{5}`, `aaaaa`},
60
        {`a{0,1}`, `a?`},
61
        // The next three are illegible because Simplify inserts (?:)
62
        // parens instead of () parens to avoid creating extra
63
        // captured subexpressions.  The comments show a version with fewer parens.
64
        {`(a){0,2}`, `(?:(a)(a)?)?`},                       //       (aa?)?
65
        {`(a){0,4}`, `(?:(a)(?:(a)(?:(a)(a)?)?)?)?`},       //   (a(a(aa?)?)?)?
66
        {`(a){2,6}`, `(a)(a)(?:(a)(?:(a)(?:(a)(a)?)?)?)?`}, // aa(a(a(aa?)?)?)?
67
        {`a{0,2}`, `(?:aa?)?`},                             //       (aa?)?
68
        {`a{0,4}`, `(?:a(?:a(?:aa?)?)?)?`},                 //   (a(a(aa?)?)?)?
69
        {`a{2,6}`, `aa(?:a(?:a(?:aa?)?)?)?`},               // aa(a(a(aa?)?)?)?
70
        {`a{0,}`, `a*`},
71
        {`a{1,}`, `a+`},
72
        {`a{2,}`, `aa+`},
73
        {`a{5,}`, `aaaaa+`},
74
 
75
        // Test that operators simplify their arguments.
76
        {`(?:a{1,}){1,}`, `a+`},
77
        {`(a{1,}b{1,})`, `(a+b+)`},
78
        {`a{1,}|b{1,}`, `a+|b+`},
79
        {`(?:a{1,})*`, `(?:a+)*`},
80
        {`(?:a{1,})+`, `a+`},
81
        {`(?:a{1,})?`, `(?:a+)?`},
82
        {``, `(?:)`},
83
        {`a{0}`, `(?:)`},
84
 
85
        // Character class simplification
86
        {`[ab]`, `[a-b]`},
87
        {`[a-za-za-z]`, `[a-z]`},
88
        {`[A-Za-zA-Za-z]`, `[A-Za-z]`},
89
        {`[ABCDEFGH]`, `[A-H]`},
90
        {`[AB-CD-EF-GH]`, `[A-H]`},
91
        {`[W-ZP-XE-R]`, `[E-Z]`},
92
        {`[a-ee-gg-m]`, `[a-m]`},
93
        {`[a-ea-ha-m]`, `[a-m]`},
94
        {`[a-ma-ha-e]`, `[a-m]`},
95
        {`[a-zA-Z0-9 -~]`, `[ -~]`},
96
 
97
        // Empty character classes
98
        {`[^[:cntrl:][:^cntrl:]]`, `[^\x00-\x{10FFFF}]`},
99
 
100
        // Full character classes
101
        {`[[:cntrl:][:^cntrl:]]`, `(?s:.)`},
102
 
103
        // Unicode case folding.
104
        {`(?i)A`, `(?i:A)`},
105
        {`(?i)a`, `(?i:A)`},
106
        {`(?i)[A]`, `(?i:A)`},
107
        {`(?i)[a]`, `(?i:A)`},
108
        {`(?i)K`, `(?i:K)`},
109
        {`(?i)k`, `(?i:K)`},
110
        {`(?i)\x{212a}`, "(?i:K)"},
111
        {`(?i)[K]`, "[Kk\u212A]"},
112
        {`(?i)[k]`, "[Kk\u212A]"},
113
        {`(?i)[\x{212a}]`, "[Kk\u212A]"},
114
        {`(?i)[a-z]`, "[A-Za-z\u017F\u212A]"},
115
        {`(?i)[\x00-\x{FFFD}]`, "[\\x00-\uFFFD]"},
116
        {`(?i)[\x00-\x{10FFFF}]`, `(?s:.)`},
117
 
118
        // Empty string as a regular expression.
119
        // The empty string must be preserved inside parens in order
120
        // to make submatches work right, so these tests are less
121
        // interesting than they might otherwise be.  String inserts
122
        // explicit (?:) in place of non-parenthesized empty strings,
123
        // to make them easier to spot for other parsers.
124
        {`(a|b|)`, `([a-b]|(?:))`},
125
        {`(|)`, `()`},
126
        {`a()`, `a()`},
127
        {`(()|())`, `(()|())`},
128
        {`(a|)`, `(a|(?:))`},
129
        {`ab()cd()`, `ab()cd()`},
130
        {`()`, `()`},
131
        {`()*`, `()*`},
132
        {`()+`, `()+`},
133
        {`()?`, `()?`},
134
        {`(){0}`, `(?:)`},
135
        {`(){1}`, `()`},
136
        {`(){1,}`, `()+`},
137
        {`(){0,2}`, `(?:()()?)?`},
138
}
139
 
140
func TestSimplify(t *testing.T) {
141
        for _, tt := range simplifyTests {
142
                re, err := Parse(tt.Regexp, MatchNL|Perl&^OneLine)
143
                if err != nil {
144
                        t.Errorf("Parse(%#q) = error %v", tt.Regexp, err)
145
                        continue
146
                }
147
                s := re.Simplify().String()
148
                if s != tt.Simple {
149
                        t.Errorf("Simplify(%#q) = %#q, want %#q", tt.Regexp, s, tt.Simple)
150
                }
151
        }
152
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.