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