prog_test.go 1.83 KB
Newer Older
1 2 3 4
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5
package syntax
6 7 8 9 10 11 12 13 14 15

import (
	"testing"
)

var compileTests = []struct {
	Regexp string
	Prog   string
}{
	{"a", `  0	fail
16
  1*	rune1 "a" -> 2
17 18 19 20 21 22 23 24 25 26 27 28
  2	match
`},
	{"[A-M][n-z]", `  0	fail
  1*	rune "AM" -> 2
  2	rune "nz" -> 3
  3	match
`},
	{"", `  0	fail
  1*	nop -> 2
  2	match
`},
	{"a?", `  0	fail
29
  1	rune1 "a" -> 3
30 31 32 33
  2*	alt -> 1, 3
  3	match
`},
	{"a??", `  0	fail
34
  1	rune1 "a" -> 3
35 36 37 38
  2*	alt -> 3, 1
  3	match
`},
	{"a+", `  0	fail
39
  1*	rune1 "a" -> 2
40 41 42 43
  2	alt -> 1, 3
  3	match
`},
	{"a+?", `  0	fail
44
  1*	rune1 "a" -> 2
45 46 47 48
  2	alt -> 3, 1
  3	match
`},
	{"a*", `  0	fail
49
  1	rune1 "a" -> 2
50 51 52 53
  2*	alt -> 1, 3
  3	match
`},
	{"a*?", `  0	fail
54
  1	rune1 "a" -> 2
55 56 57 58
  2*	alt -> 3, 1
  3	match
`},
	{"a+b+", `  0	fail
59
  1*	rune1 "a" -> 2
60
  2	alt -> 1, 3
61
  3	rune1 "b" -> 4
62 63 64 65 66
  4	alt -> 3, 5
  5	match
`},
	{"(a+)(b+)", `  0	fail
  1*	cap 2 -> 2
67
  2	rune1 "a" -> 3
68 69 70
  3	alt -> 2, 4
  4	cap 3 -> 5
  5	cap 4 -> 6
71
  6	rune1 "b" -> 7
72 73 74 75 76
  7	alt -> 6, 8
  8	cap 5 -> 9
  9	match
`},
	{"a+|b+", `  0	fail
77
  1	rune1 "a" -> 2
78
  2	alt -> 1, 6
79
  3	rune1 "b" -> 4
80 81 82 83
  4	alt -> 3, 6
  5*	alt -> 1, 3
  6	match
`},
84 85 86 87 88 89 90 91 92 93
	{"A[Aa]", `  0	fail
  1*	rune1 "A" -> 2
  2	rune "A"/i -> 3
  3	match
`},
	{"(?:(?:^).)", `  0	fail
  1*	empty 4 -> 2
  2	anynotnl -> 3
  3	match
`},
94 95 96 97 98 99 100 101 102 103 104 105
}

func TestCompile(t *testing.T) {
	for _, tt := range compileTests {
		re, _ := Parse(tt.Regexp, Perl)
		p, _ := Compile(re)
		s := p.String()
		if s != tt.Prog {
			t.Errorf("compiled %#q:\n--- have\n%s---\n--- want\n%s---", tt.Regexp, s, tt.Prog)
		}
	}
}
106 107 108 109 110 111 112 113 114 115 116

func BenchmarkEmptyOpContext(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var r1 rune = -1
		for _, r2 := range "foo, bar, baz\nsome input text.\n" {
			EmptyOpContext(r1, r2)
			r1 = r2
		}
		EmptyOpContext(r1, -1)
	}
}