github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/regexp/syntax/prog_test.go (about) 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. 4 5 package syntax 6 7 import ( 8 "testing" 9 ) 10 11 var compileTests = []struct { 12 Regexp string 13 Prog string 14 }{ 15 {"a", ` 0 fail 16 1* rune1 "a" -> 2 17 2 match 18 `}, 19 {"[A-M][n-z]", ` 0 fail 20 1* rune "AM" -> 2 21 2 rune "nz" -> 3 22 3 match 23 `}, 24 {"", ` 0 fail 25 1* nop -> 2 26 2 match 27 `}, 28 {"a?", ` 0 fail 29 1 rune1 "a" -> 3 30 2* alt -> 1, 3 31 3 match 32 `}, 33 {"a??", ` 0 fail 34 1 rune1 "a" -> 3 35 2* alt -> 3, 1 36 3 match 37 `}, 38 {"a+", ` 0 fail 39 1* rune1 "a" -> 2 40 2 alt -> 1, 3 41 3 match 42 `}, 43 {"a+?", ` 0 fail 44 1* rune1 "a" -> 2 45 2 alt -> 3, 1 46 3 match 47 `}, 48 {"a*", ` 0 fail 49 1 rune1 "a" -> 2 50 2* alt -> 1, 3 51 3 match 52 `}, 53 {"a*?", ` 0 fail 54 1 rune1 "a" -> 2 55 2* alt -> 3, 1 56 3 match 57 `}, 58 {"a+b+", ` 0 fail 59 1* rune1 "a" -> 2 60 2 alt -> 1, 3 61 3 rune1 "b" -> 4 62 4 alt -> 3, 5 63 5 match 64 `}, 65 {"(a+)(b+)", ` 0 fail 66 1* cap 2 -> 2 67 2 rune1 "a" -> 3 68 3 alt -> 2, 4 69 4 cap 3 -> 5 70 5 cap 4 -> 6 71 6 rune1 "b" -> 7 72 7 alt -> 6, 8 73 8 cap 5 -> 9 74 9 match 75 `}, 76 {"a+|b+", ` 0 fail 77 1 rune1 "a" -> 2 78 2 alt -> 1, 6 79 3 rune1 "b" -> 4 80 4 alt -> 3, 6 81 5* alt -> 1, 3 82 6 match 83 `}, 84 {"A[Aa]", ` 0 fail 85 1* rune1 "A" -> 2 86 2 rune "A"/i -> 3 87 3 match 88 `}, 89 {"(?:(?:^).)", ` 0 fail 90 1* empty 4 -> 2 91 2 anynotnl -> 3 92 3 match 93 `}, 94 } 95 96 func TestCompile(t *testing.T) { 97 for _, tt := range compileTests { 98 re, _ := Parse(tt.Regexp, Perl) 99 p, _ := Compile(re) 100 s := p.String() 101 if s != tt.Prog { 102 t.Errorf("compiled %#q:\n--- have\n%s---\n--- want\n%s---", tt.Regexp, s, tt.Prog) 103 } 104 } 105 } 106 107 func BenchmarkEmptyOpContext(b *testing.B) { 108 for i := 0; i < b.N; i++ { 109 var r1 rune = -1 110 for _, r2 := range "foo, bar, baz\nsome input text.\n" { 111 EmptyOpContext(r1, r2) 112 r1 = r2 113 } 114 EmptyOpContext(r1, -1) 115 } 116 }