summaryrefslogtreecommitdiff
path: root/src/lib/regexp/all_test.go
blob: a9f275893ad95054a9f81799d43474479f80fc7d (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2009 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 (
	"os";
	"regexp";
	"testing";
)

var good_re = []string{
	``,
	`.`,
	`^.$`,
	`a`,
	`a*`,
	`a+`,
	`a?`,
	`a|b`,
	`a*|b*`,
	`(a*|b)(c*|d)`,
	`[a-z]`,
	`[a-abc-c\-\]\[]`,
	`[a-z]+`,
	`[]`,
	`[abc]`,
	`[^1234]`,
}

// TODO: nice to do this with a map
type stringError struct {
	re	string;
	err	os.Error;
}
var bad_re = []stringError{
	stringError{ `*`,	 	regexp.ErrBareClosure },
	stringError{ `(abc`,	regexp.ErrUnmatchedLpar },
	stringError{ `abc)`,	regexp.ErrUnmatchedRpar },
	stringError{ `x[a-z`,	regexp.ErrUnmatchedLbkt },
	stringError{ `abc]`,	regexp.ErrUnmatchedRbkt },
	stringError{ `[z-a]`,	regexp.ErrBadRange },
	stringError{ `abc\`,	regexp.ErrExtraneousBackslash },
	stringError{ `a**`,	regexp.ErrBadClosure },
	stringError{ `a*+`,	regexp.ErrBadClosure },
	stringError{ `a??`,	regexp.ErrBadClosure },
	stringError{ `*`,	 	regexp.ErrBareClosure },
	stringError{ `\x`,	regexp.ErrBadBackslash },
}

type vec []int;

type tester struct {
	re	string;
	text	string;
	match	vec;
}

var matches = []tester {
	tester{ ``,	"",	vec{0,0} },
	tester{ `a`,	"a",	vec{0,1} },
	tester{ `x`,	"y",	vec{} },
	tester{ `b`,	"abc",	vec{1,2} },
	tester{ `.`,	"a",	vec{0,1} },
	tester{ `.*`,	"abcdef",	vec{0,6} },
	tester{ `^abcd$`,	"abcd",	vec{0,4} },
	tester{ `^bcd'`,	"abcdef",	vec{} },
	tester{ `^abcd$`,	"abcde",	vec{} },
	tester{ `a+`,	"baaab",	vec{1,4} },
	tester{ `a*`,	"baaab",	vec{0,0} },
	tester{ `[a-z]+`,	"abcd",	vec{0,4} },
	tester{ `[^a-z]+`,	"ab1234cd",	vec{2,6} },
	tester{ `[a\-\]z]+`,	"az]-bcz",	vec{0,4} },
	tester{ `[日本語]+`,	"日本語日本語",	vec{0,18} },
	tester{ `()`,	"",	vec{0,0, 0,0} },
	tester{ `(a)`,	"a",	vec{0,1, 0,1} },
	tester{ `(.)(.)`,	"日a",	vec{0,4, 0,3, 3,4} },
	tester{ `(.*)`,	"",	vec{0,0, 0,0} },
	tester{ `(.*)`,	"abcd",	vec{0,4, 0,4} },
	tester{ `(..)(..)`,	"abcd",	vec{0,4, 0,2, 2,4} },
	tester{ `(([^xyz]*)(d))`,	"abcd",	vec{0,4, 0,4, 0,3, 3,4} },
	tester{ `((a|b|c)*(d))`,	"abcd",	vec{0,4, 0,4, 2,3, 3,4} },
	tester{ `(((a|b|c)*)(d))`,	"abcd",	vec{0,4, 0,4, 0,3, 2,3, 3,4} },
	tester{ `a*(|(b))c*`,	"aacc",	vec{0,4, 2,2, -1,-1} },
}

func compileTest(t *testing.T, expr string, error os.Error) *regexp.Regexp {
	re, err := regexp.Compile(expr);
	if err != error {
		t.Error("compiling `", expr, "`; unexpected error: ", err.String());
	}
	return re
}

func printVec(t *testing.T, m []int) {
	l := len(m);
	if l == 0 {
		t.Log("\t<no match>");
	} else {
		for i := 0; i < l; i = i+2 {
			t.Log("\t", m[i], ",", m[i+1])
		}
	}
}

func printStrings(t *testing.T, m []string) {
	l := len(m);
	if l == 0 {
		t.Log("\t<no match>");
	} else {
		for i := 0; i < l; i = i+2 {
			t.Logf("\t%q", m[i])
		}
	}
}

func equal(m1, m2 []int) bool {
	l := len(m1);
	if l != len(m2) {
		return false
	}
	for i := 0; i < l; i++ {
		if m1[i] != m2[i] {
			return false
		}
	}
	return true
}

func equalStrings(m1, m2 []string) bool {
	l := len(m1);
	if l != len(m2) {
		return false
	}
	for i := 0; i < l; i++ {
		if m1[i] != m2[i] {
			return false
		}
	}
	return true
}

func executeTest(t *testing.T, expr string, str string, match []int) {
	re := compileTest(t, expr, nil);
	if re == nil {
		return
	}
	m := re.Execute(str);
	if !equal(m, match) {
		t.Error("Execute failure on `", expr, "` matching `", str, "`:");
		printVec(t, m);
		t.Log("should be:");
		printVec(t, match);
	}
}

func TestGoodCompile(t *testing.T) {
	for i := 0; i < len(good_re); i++ {
		compileTest(t, good_re[i], nil);
	}
}

func TestBadCompile(t *testing.T) {
	for i := 0; i < len(bad_re); i++ {
		compileTest(t, bad_re[i].re, bad_re[i].err)
	}
}

func TestExecute(t *testing.T) {
	for i := 0; i < len(matches); i++ {
		test := &matches[i];
		executeTest(t, test.re, test.text, test.match)
	}
}

func matchTest(t *testing.T, expr string, str string, match []int) {
	re := compileTest(t, expr, nil);
	if re == nil {
		return
	}
	m := re.Match(str);
	if m != (len(match) > 0) {
		t.Error("Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0);
	}
}

func TestMatch(t *testing.T) {
	for i := 0; i < len(matches); i++ {
		test := &matches[i];
		matchTest(t, test.re, test.text, test.match)
	}
}

func matchStringsTest(t *testing.T, expr string, str string, match []int) {
	re := compileTest(t, expr, nil);
	if re == nil {
		return
	}
	strs := make([]string, len(match)/2);
	for i := 0; i < len(match); i++ {
		strs[i/2] = str[match[i] : match[i+1]]
	}
	m := re.MatchStrings(str);
	if !equalStrings(m, strs) {
		t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:");
		printStrings(t, m);
		t.Log("should be:");
		printStrings(t, strs);
	}
}

func TestMatchStrings(t *testing.T) {
	for i := 0; i < len(matches); i++ {
		test := &matches[i];
		matchTest(t, test.re, test.text, test.match)
	}
}

func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
	m, err := Match(expr, str);
	if err == nil {
		return
	}
	if m != (len(match) > 0) {
		t.Error("function Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0);
	}
}

func TestMatchFunction(t *testing.T) {
	for i := 0; i < len(matches); i++ {
		test := &matches[i];
		matchFunctionTest(t, test.re, test.text, test.match)
	}
}