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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [regexp/] [all_test.go] - Blame information for rev 774

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

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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 regexp
6
 
7
import (
8
        "strings"
9
        "testing"
10
)
11
 
12
var good_re = []string{
13
        ``,
14
        `.`,
15
        `^.$`,
16
        `a`,
17
        `a*`,
18
        `a+`,
19
        `a?`,
20
        `a|b`,
21
        `a*|b*`,
22
        `(a*|b)(c*|d)`,
23
        `[a-z]`,
24
        `[a-abc-c\-\]\[]`,
25
        `[a-z]+`,
26
        `[abc]`,
27
        `[^1234]`,
28
        `[^\n]`,
29
        `\!\\`,
30
}
31
 
32
/*
33
type stringError struct {
34
        re  string
35
        err error
36
}
37
 
38
var bad_re = []stringError{
39
        {`*`, ErrBareClosure},
40
        {`+`, ErrBareClosure},
41
        {`?`, ErrBareClosure},
42
        {`(abc`, ErrUnmatchedLpar},
43
        {`abc)`, ErrUnmatchedRpar},
44
        {`x[a-z`, ErrUnmatchedLbkt},
45
        {`abc]`, ErrUnmatchedRbkt},
46
        {`[z-a]`, ErrBadRange},
47
        {`abc\`, ErrExtraneousBackslash},
48
        {`a**`, ErrBadClosure},
49
        {`a*+`, ErrBadClosure},
50
        {`a??`, ErrBadClosure},
51
        {`\x`, ErrBadBackslash},
52
}
53
*/
54
 
55
func compileTest(t *testing.T, expr string, error error) *Regexp {
56
        re, err := Compile(expr)
57
        if err != error {
58
                t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
59
        }
60
        return re
61
}
62
 
63
func TestGoodCompile(t *testing.T) {
64
        for i := 0; i < len(good_re); i++ {
65
                compileTest(t, good_re[i], nil)
66
        }
67
}
68
 
69
/*
70
func TestBadCompile(t *testing.T) {
71
        for i := 0; i < len(bad_re); i++ {
72
                compileTest(t, bad_re[i].re, bad_re[i].err)
73
        }
74
}
75
*/
76
 
77
func matchTest(t *testing.T, test *FindTest) {
78
        re := compileTest(t, test.pat, nil)
79
        if re == nil {
80
                return
81
        }
82
        m := re.MatchString(test.text)
83
        if m != (len(test.matches) > 0) {
84
                t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
85
        }
86
        // now try bytes
87
        m = re.Match([]byte(test.text))
88
        if m != (len(test.matches) > 0) {
89
                t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
90
        }
91
}
92
 
93
func TestMatch(t *testing.T) {
94
        for _, test := range findTests {
95
                matchTest(t, &test)
96
        }
97
}
98
 
99
func matchFunctionTest(t *testing.T, test *FindTest) {
100
        m, err := MatchString(test.pat, test.text)
101
        if err == nil {
102
                return
103
        }
104
        if m != (len(test.matches) > 0) {
105
                t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
106
        }
107
}
108
 
109
func TestMatchFunction(t *testing.T) {
110
        for _, test := range findTests {
111
                matchFunctionTest(t, &test)
112
        }
113
}
114
 
115
type ReplaceTest struct {
116
        pattern, replacement, input, output string
117
}
118
 
119
var replaceTests = []ReplaceTest{
120
        // Test empty input and/or replacement, with pattern that matches the empty string.
121
        {"", "", "", ""},
122
        {"", "x", "", "x"},
123
        {"", "", "abc", "abc"},
124
        {"", "x", "abc", "xaxbxcx"},
125
 
126
        // Test empty input and/or replacement, with pattern that does not match the empty string.
127
        {"b", "", "", ""},
128
        {"b", "x", "", ""},
129
        {"b", "", "abc", "ac"},
130
        {"b", "x", "abc", "axc"},
131
        {"y", "", "", ""},
132
        {"y", "x", "", ""},
133
        {"y", "", "abc", "abc"},
134
        {"y", "x", "abc", "abc"},
135
 
136
        // Multibyte characters -- verify that we don't try to match in the middle
137
        // of a character.
138
        {"[a-c]*", "x", "\u65e5", "x\u65e5x"},
139
        {"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
140
 
141
        // Start and end of a string.
142
        {"^[a-c]*", "x", "abcdabc", "xdabc"},
143
        {"[a-c]*$", "x", "abcdabc", "abcdx"},
144
        {"^[a-c]*$", "x", "abcdabc", "abcdabc"},
145
        {"^[a-c]*", "x", "abc", "x"},
146
        {"[a-c]*$", "x", "abc", "x"},
147
        {"^[a-c]*$", "x", "abc", "x"},
148
        {"^[a-c]*", "x", "dabce", "xdabce"},
149
        {"[a-c]*$", "x", "dabce", "dabcex"},
150
        {"^[a-c]*$", "x", "dabce", "dabce"},
151
        {"^[a-c]*", "x", "", "x"},
152
        {"[a-c]*$", "x", "", "x"},
153
        {"^[a-c]*$", "x", "", "x"},
154
 
155
        {"^[a-c]+", "x", "abcdabc", "xdabc"},
156
        {"[a-c]+$", "x", "abcdabc", "abcdx"},
157
        {"^[a-c]+$", "x", "abcdabc", "abcdabc"},
158
        {"^[a-c]+", "x", "abc", "x"},
159
        {"[a-c]+$", "x", "abc", "x"},
160
        {"^[a-c]+$", "x", "abc", "x"},
161
        {"^[a-c]+", "x", "dabce", "dabce"},
162
        {"[a-c]+$", "x", "dabce", "dabce"},
163
        {"^[a-c]+$", "x", "dabce", "dabce"},
164
        {"^[a-c]+", "x", "", ""},
165
        {"[a-c]+$", "x", "", ""},
166
        {"^[a-c]+$", "x", "", ""},
167
 
168
        // Other cases.
169
        {"abc", "def", "abcdefg", "defdefg"},
170
        {"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
171
        {"abc", "", "abcdabc", "d"},
172
        {"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
173
        {"abc", "d", "", ""},
174
        {"abc", "d", "abc", "d"},
175
        {".+", "x", "abc", "x"},
176
        {"[a-c]*", "x", "def", "xdxexfx"},
177
        {"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
178
        {"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
179
}
180
 
181
type ReplaceFuncTest struct {
182
        pattern       string
183
        replacement   func(string) string
184
        input, output string
185
}
186
 
187
var replaceFuncTests = []ReplaceFuncTest{
188
        {"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
189
        {"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
190
        {"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
191
}
192
 
193
func TestReplaceAll(t *testing.T) {
194
        for _, tc := range replaceTests {
195
                re, err := Compile(tc.pattern)
196
                if err != nil {
197
                        t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
198
                        continue
199
                }
200
                actual := re.ReplaceAllString(tc.input, tc.replacement)
201
                if actual != tc.output {
202
                        t.Errorf("%q.Replace(%q,%q) = %q; want %q",
203
                                tc.pattern, tc.input, tc.replacement, actual, tc.output)
204
                }
205
                // now try bytes
206
                actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
207
                if actual != tc.output {
208
                        t.Errorf("%q.Replace(%q,%q) = %q; want %q",
209
                                tc.pattern, tc.input, tc.replacement, actual, tc.output)
210
                }
211
        }
212
}
213
 
214
func TestReplaceAllFunc(t *testing.T) {
215
        for _, tc := range replaceFuncTests {
216
                re, err := Compile(tc.pattern)
217
                if err != nil {
218
                        t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
219
                        continue
220
                }
221
                actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
222
                if actual != tc.output {
223
                        t.Errorf("%q.ReplaceFunc(%q,%q) = %q; want %q",
224
                                tc.pattern, tc.input, tc.replacement, actual, tc.output)
225
                }
226
                // now try bytes
227
                actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
228
                if actual != tc.output {
229
                        t.Errorf("%q.ReplaceFunc(%q,%q) = %q; want %q",
230
                                tc.pattern, tc.input, tc.replacement, actual, tc.output)
231
                }
232
        }
233
}
234
 
235
type MetaTest struct {
236
        pattern, output, literal string
237
        isLiteral                bool
238
}
239
 
240
var metaTests = []MetaTest{
241
        {``, ``, ``, true},
242
        {`foo`, `foo`, `foo`, true},
243
        {`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
244
        {`foo.\$`, `foo\.\\\$`, `foo`, false},     // has escaped operators and real operators
245
        {`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
246
}
247
 
248
func TestQuoteMeta(t *testing.T) {
249
        for _, tc := range metaTests {
250
                // Verify that QuoteMeta returns the expected string.
251
                quoted := QuoteMeta(tc.pattern)
252
                if quoted != tc.output {
253
                        t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
254
                                tc.pattern, quoted, tc.output)
255
                        continue
256
                }
257
 
258
                // Verify that the quoted string is in fact treated as expected
259
                // by Compile -- i.e. that it matches the original, unquoted string.
260
                if tc.pattern != "" {
261
                        re, err := Compile(quoted)
262
                        if err != nil {
263
                                t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
264
                                continue
265
                        }
266
                        src := "abc" + tc.pattern + "def"
267
                        repl := "xyz"
268
                        replaced := re.ReplaceAllString(src, repl)
269
                        expected := "abcxyzdef"
270
                        if replaced != expected {
271
                                t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
272
                                        tc.pattern, src, repl, replaced, expected)
273
                        }
274
                }
275
        }
276
}
277
 
278
func TestLiteralPrefix(t *testing.T) {
279
        for _, tc := range metaTests {
280
                // Literal method needs to scan the pattern.
281
                re := MustCompile(tc.pattern)
282
                str, complete := re.LiteralPrefix()
283
                if complete != tc.isLiteral {
284
                        t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
285
                }
286
                if str != tc.literal {
287
                        t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
288
                }
289
        }
290
}
291
 
292
type subexpCase struct {
293
        input string
294
        num   int
295
        names []string
296
}
297
 
298
var subexpCases = []subexpCase{
299
        {``, 0, nil},
300
        {`.*`, 0, nil},
301
        {`abba`, 0, nil},
302
        {`ab(b)a`, 1, []string{"", ""}},
303
        {`ab(.*)a`, 1, []string{"", ""}},
304
        {`(.*)ab(.*)a`, 2, []string{"", "", ""}},
305
        {`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
306
        {`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
307
        {`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
308
        {`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
309
        {`(?P.*)(?P(a)b)(?P.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
310
}
311
 
312
func TestSubexp(t *testing.T) {
313
        for _, c := range subexpCases {
314
                re := MustCompile(c.input)
315
                n := re.NumSubexp()
316
                if n != c.num {
317
                        t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
318
                        continue
319
                }
320
                names := re.SubexpNames()
321
                if len(names) != 1+n {
322
                        t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
323
                        continue
324
                }
325
                if c.names != nil {
326
                        for i := 0; i < 1+n; i++ {
327
                                if names[i] != c.names[i] {
328
                                        t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
329
                                }
330
                        }
331
                }
332
        }
333
}
334
 
335
func BenchmarkLiteral(b *testing.B) {
336
        x := strings.Repeat("x", 50) + "y"
337
        b.StopTimer()
338
        re := MustCompile("y")
339
        b.StartTimer()
340
        for i := 0; i < b.N; i++ {
341
                if !re.MatchString(x) {
342
                        b.Fatalf("no match!")
343
                }
344
        }
345
}
346
 
347
func BenchmarkNotLiteral(b *testing.B) {
348
        x := strings.Repeat("x", 50) + "y"
349
        b.StopTimer()
350
        re := MustCompile(".y")
351
        b.StartTimer()
352
        for i := 0; i < b.N; i++ {
353
                if !re.MatchString(x) {
354
                        b.Fatalf("no match!")
355
                }
356
        }
357
}
358
 
359
func BenchmarkMatchClass(b *testing.B) {
360
        b.StopTimer()
361
        x := strings.Repeat("xxxx", 20) + "w"
362
        re := MustCompile("[abcdw]")
363
        b.StartTimer()
364
        for i := 0; i < b.N; i++ {
365
                if !re.MatchString(x) {
366
                        b.Fatalf("no match!")
367
                }
368
        }
369
}
370
 
371
func BenchmarkMatchClass_InRange(b *testing.B) {
372
        b.StopTimer()
373
        // 'b' is between 'a' and 'c', so the charclass
374
        // range checking is no help here.
375
        x := strings.Repeat("bbbb", 20) + "c"
376
        re := MustCompile("[ac]")
377
        b.StartTimer()
378
        for i := 0; i < b.N; i++ {
379
                if !re.MatchString(x) {
380
                        b.Fatalf("no match!")
381
                }
382
        }
383
}
384
 
385
func BenchmarkReplaceAll(b *testing.B) {
386
        x := "abcdefghijklmnopqrstuvwxyz"
387
        b.StopTimer()
388
        re := MustCompile("[cjrw]")
389
        b.StartTimer()
390
        for i := 0; i < b.N; i++ {
391
                re.ReplaceAllString(x, "")
392
        }
393
}
394
 
395
func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
396
        b.StopTimer()
397
        x := []byte("abcdefghijklmnopqrstuvwxyz")
398
        re := MustCompile("^zbc(d|e)")
399
        b.StartTimer()
400
        for i := 0; i < b.N; i++ {
401
                re.Match(x)
402
        }
403
}
404
 
405
func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
406
        b.StopTimer()
407
        x := []byte("abcdefghijklmnopqrstuvwxyz")
408
        for i := 0; i < 15; i++ {
409
                x = append(x, x...)
410
        }
411
        re := MustCompile("^zbc(d|e)")
412
        b.StartTimer()
413
        for i := 0; i < b.N; i++ {
414
                re.Match(x)
415
        }
416
}
417
 
418
func BenchmarkAnchoredShortMatch(b *testing.B) {
419
        b.StopTimer()
420
        x := []byte("abcdefghijklmnopqrstuvwxyz")
421
        re := MustCompile("^.bc(d|e)")
422
        b.StartTimer()
423
        for i := 0; i < b.N; i++ {
424
                re.Match(x)
425
        }
426
}
427
 
428
func BenchmarkAnchoredLongMatch(b *testing.B) {
429
        b.StopTimer()
430
        x := []byte("abcdefghijklmnopqrstuvwxyz")
431
        for i := 0; i < 15; i++ {
432
                x = append(x, x...)
433
        }
434
        re := MustCompile("^.bc(d|e)")
435
        b.StartTimer()
436
        for i := 0; i < b.N; i++ {
437
                re.Match(x)
438
        }
439
}

powered by: WebSVN 2.1.0

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