Commit be7e0f81 authored by Rob Pike's avatar Rob Pike

gotestify regexp

R=rsc
DELTA=101  (53 added, 25 deleted, 23 changed)
OCL=19635
CL=19637
parent 9195c22e
...@@ -2,28 +2,54 @@ ...@@ -2,28 +2,54 @@
# Use of this source code is governed by a BSD-style # Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
A=6 # DO NOT EDIT. Automatically generated by gobuild.
G=$(A)g # gobuild -m >Makefile
L=$(A)l O=6
PKG=$(GOROOT)/pkg/regexp.$A GC=$(O)g
CC=$(O)c -w
AS=$(O)a
AR=$(O)ar
test: main.$A test.$A default: packages
$L -o test test.$A
./test
install: regexp.$A clean:
cp regexp.$A $(PKG) rm -f *.$O *.a $O.out
main: main.$A test: packages
$L -o main main.$A gotest
main.$A: regexp.$A coverage: packages
gotest
6cov -g `pwd` | grep -v '^test.*\.go:'
clean: %.$O: %.go
rm -f *.6 test $(GC) $*.go
%.$O: %.c
$(CC) $*.c
%.$O: %.s
$(AS) $*.s
O1=\
regexp.$O\
regexp.a: a1
a1: $(O1)
$(AR) grc regexp.a regexp.$O
rm -f $(O1)
newpkg: clean
$(AR) grc regexp.a
$(O1): newpkg
nuke: clean nuke: clean
rm -f $(PKG) rm -f $(GOROOT)/pkg/regexp.a
packages: regexp.a
install: packages
cp regexp.a $(GOROOT)/pkg/regexp.a
%.6: %.go
$G $<
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package regexp
import ( import (
"os"; "os";
"regexp"; "regexp";
"testing";
) )
var good_re = []string{ var good_re = []string{
...@@ -85,11 +86,10 @@ var matches = []Tester { ...@@ -85,11 +86,10 @@ var matches = []Tester {
Tester{ `a*(|(b))c*`, "aacc", Vec{0,4, 2,2, -1,-1, END} }, Tester{ `a*(|(b))c*`, "aacc", Vec{0,4, 2,2, -1,-1, END} },
} }
func Compile(expr string, error *os.Error) regexp.Regexp { func CompileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
re, err := regexp.Compile(expr); re, err := regexp.Compile(expr);
if err != error { if err != error {
print("compiling `", expr, "`; unexpected error: ", err.String(), "\n"); t.Error("compiling `", expr, "`; unexpected error: ", err.String());
sys.exit(1);
} }
return re return re
} }
...@@ -104,13 +104,13 @@ func MarkedLen(m *[] int) int { ...@@ -104,13 +104,13 @@ func MarkedLen(m *[] int) int {
return i return i
} }
func PrintVec(m *[] int) { func PrintVec(t *testing.T, m *[] int) {
l := MarkedLen(m); l := MarkedLen(m);
if l == 0 { if l == 0 {
print("<no match>"); t.Log("\t<no match>");
} else { } else {
for i := 0; i < l && m[i] != END; i = i+2 { for i := 0; i < l && m[i] != END; i = i+2 {
print(m[i], ",", m[i+1], " ") t.Log("\t", m[i], ",", m[i+1])
} }
} }
} }
...@@ -128,33 +128,35 @@ func Equal(m1, m2 *[]int) bool { ...@@ -128,33 +128,35 @@ func Equal(m1, m2 *[]int) bool {
return true return true
} }
func Match(expr string, str string, match *[]int) { func MatchTest(t *testing.T, expr string, str string, match *[]int) {
re := Compile(expr, nil); re := CompileTest(t, expr, nil);
if re == nil {
return
}
m := re.Execute(str); m := re.Execute(str);
if !Equal(m, match) { if !Equal(m, match) {
print("failure on `", expr, "` matching `", str, "`:\n"); t.Error("failure on `", expr, "` matching `", str, "`:");
PrintVec(m); PrintVec(t, m);
print("\nshould be:\n"); t.Log("should be:");
PrintVec(match); PrintVec(t, match);
print("\n");
sys.exit(1);
} }
} }
func main() { export func TestGoodCompile(t *testing.T) {
//regexp.debug = true;
if sys.argc() > 1 {
Compile(sys.argv(1), nil);
sys.exit(0);
}
for i := 0; i < len(good_re); i++ { for i := 0; i < len(good_re); i++ {
Compile(good_re[i], nil); CompileTest(t, good_re[i], nil);
} }
}
export func TestBadCompile(t *testing.T) {
for i := 0; i < len(bad_re); i++ { for i := 0; i < len(bad_re); i++ {
Compile(bad_re[i].re, bad_re[i].err) CompileTest(t, bad_re[i].re, bad_re[i].err)
} }
}
export func TestMatch(t *testing.T) {
for i := 0; i < len(matches); i++ { for i := 0; i < len(matches); i++ {
t := &matches[i]; test := &matches[i];
Match(t.re, t.text, &t.match) MatchTest(t, test.re, test.text, &test.match)
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment