summaryrefslogtreecommitdiff
path: root/src/pkg/regexp/onepass_test.go
blob: 7b2beea67ffb2a60f8c8e7debc358a3dea4ca500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2014 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.

package regexp

import (
	"reflect"
	"regexp/syntax"
	"testing"
)

var runeMergeTests = []struct {
	left, right, merged []rune
	next                []uint32
	leftPC, rightPC     uint32
}{
	{
		// empty rhs
		[]rune{69, 69},
		[]rune{},
		[]rune{69, 69},
		[]uint32{1},
		1, 2,
	},
	{
		// identical runes, identical targets
		[]rune{69, 69},
		[]rune{69, 69},
		[]rune{},
		[]uint32{mergeFailed},
		1, 1,
	},
	{
		// identical runes, different targets
		[]rune{69, 69},
		[]rune{69, 69},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// append right-first
		[]rune{69, 69},
		[]rune{71, 71},
		[]rune{69, 69, 71, 71},
		[]uint32{1, 2},
		1, 2,
	},
	{
		// append, left-first
		[]rune{71, 71},
		[]rune{69, 69},
		[]rune{69, 69, 71, 71},
		[]uint32{2, 1},
		1, 2,
	},
	{
		// successful interleave
		[]rune{60, 60, 71, 71, 101, 101},
		[]rune{69, 69, 88, 88},
		[]rune{60, 60, 69, 69, 71, 71, 88, 88, 101, 101},
		[]uint32{1, 2, 1, 2, 1},
		1, 2,
	},
	{
		// left surrounds right
		[]rune{69, 74},
		[]rune{71, 71},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// right surrounds left
		[]rune{69, 74},
		[]rune{68, 75},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// overlap at interval begin
		[]rune{69, 74},
		[]rune{74, 75},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// overlap ar interval end
		[]rune{69, 74},
		[]rune{65, 69},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// overlap from above
		[]rune{69, 74},
		[]rune{71, 74},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// overlap from below
		[]rune{69, 74},
		[]rune{65, 71},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
	{
		// out of order []rune
		[]rune{69, 74, 60, 65},
		[]rune{66, 67},
		[]rune{},
		[]uint32{mergeFailed},
		1, 2,
	},
}

func TestMergeRuneSet(t *testing.T) {
	for ix, test := range runeMergeTests {
		merged, next := mergeRuneSets(&test.left, &test.right, test.leftPC, test.rightPC)
		if !reflect.DeepEqual(merged, test.merged) {
			t.Errorf("mergeRuneSet :%d (%v, %v) merged\n have\n%v\nwant\n%v", ix, test.left, test.right, merged, test.merged)
		}
		if !reflect.DeepEqual(next, test.next) {
			t.Errorf("mergeRuneSet :%d(%v, %v) next\n have\n%v\nwant\n%v", ix, test.left, test.right, next, test.next)
		}
	}
}

const noStr = `!`

var onePass = &onePassProg{}

var onePassTests = []struct {
	re      string
	onePass *onePassProg
	prog    string
}{
	{`^(?:a|(?:a*))$`, notOnePass, noStr},
	{`^(?:(a)|(?:a*))$`, notOnePass, noStr},
	{`^(?:(?:(?:.(?:$))?))$`, onePass, `a`},
	{`^abcd$`, onePass, `abcd`},
	{`^abcd$`, onePass, `abcde`},
	{`^(?:(?:a{0,})*?)$`, onePass, `a`},
	{`^(?:(?:a+)*)$`, onePass, ``},
	{`^(?:(?:a|(?:aa)))$`, onePass, ``},
	{`^(?:[^\s\S])$`, onePass, ``},
	{`^(?:(?:a{3,4}){0,})$`, notOnePass, `aaaaaa`},
	{`^(?:(?:a+)*)$`, onePass, `a`},
	{`^(?:(?:(?:a*)+))$`, onePass, noStr},
	{`^(?:(?:a+)*)$`, onePass, ``},
	{`^[a-c]+$`, onePass, `abc`},
	{`^[a-c]*$`, onePass, `abcdabc`},
	{`^(?:a*)$`, onePass, `aaaaaaa`},
	{`^(?:(?:aa)|a)$`, onePass, `a`},
	{`^[a-c]*`, notOnePass, `abcdabc`},
	{`^[a-c]*$`, onePass, `abc`},
	{`^...$`, onePass, ``},
	{`^(?:a|(?:aa))$`, onePass, `a`},
	{`^[a-c]*`, notOnePass, `abcabc`},
	{`^a((b))c$`, onePass, noStr},
	{`^a.[l-nA-Cg-j]?e$`, onePass, noStr},
	{`^a((b))$`, onePass, noStr},
	{`^a(?:(b)|(c))c$`, onePass, noStr},
	{`^a(?:(b*)|(c))c$`, notOnePass, noStr},
	{`^a(?:b|c)$`, onePass, noStr},
	{`^a(?:b?|c)$`, onePass, noStr},
	{`^a(?:b?|c?)$`, notOnePass, noStr},
	{`^a(?:b?|c+)$`, onePass, noStr},
	{`^a(?:b+|(bc))d$`, notOnePass, noStr},
	{`^a(?:bc)+$`, onePass, noStr},
	{`^a(?:[bcd])+$`, onePass, noStr},
	{`^a((?:[bcd])+)$`, onePass, noStr},
	{`^a(:?b|c)*d$`, onePass, `abbbccbbcbbd"`},
	{`^.bc(d|e)*$`, onePass, `abcddddddeeeededd`},
	{`^(?:(?:aa)|.)$`, notOnePass, `a`},
	{`^(?:(?:a{1,2}){1,2})$`, notOnePass, `aaaa`},
}

func TestCompileOnePass(t *testing.T) {
	var (
		p   *syntax.Prog
		re  *syntax.Regexp
		err error
	)
	for _, test := range onePassTests {
		if re, err = syntax.Parse(test.re, syntax.Perl); err != nil {
			t.Errorf("Parse(%q) got err:%s, want success", test.re, err)
			continue
		}
		// needs to be done before compile...
		re = re.Simplify()
		if p, err = syntax.Compile(re); err != nil {
			t.Errorf("Compile(%q) got err:%s, want success", test.re, err)
			continue
		}
		onePass = compileOnePass(p)
		if (onePass == notOnePass) != (test.onePass == notOnePass) {
			t.Errorf("CompileOnePass(%q) got %v, expected %v", test.re, onePass, test.onePass)
		}
	}
}