summaryrefslogtreecommitdiff
path: root/lib/regex/test
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
committerOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
commit2bc852bda98762d3bc01548bf972e3f1b137fbfb (patch)
tree74831deed3c9057c5fe0cbb8790d220e855bc792 /lib/regex/test
parent3de952510eb2a23350d24ed926f19c0cf72a12f2 (diff)
downloadmc-2bc852bda98762d3bc01548bf972e3f1b137fbfb.tar.gz
Move Myrddin libs to lib/ subdirectory.
Diffstat (limited to 'lib/regex/test')
-rw-r--r--lib/regex/test/basic.myr39
-rw-r--r--lib/regex/test/bld.sub7
-rw-r--r--lib/regex/test/boundaries.myr36
-rw-r--r--lib/regex/test/capture.myr17
-rw-r--r--lib/regex/test/class.myr67
-rw-r--r--lib/regex/test/failmatch.myr7
-rw-r--r--lib/regex/test/negclass.myr72
-rwxr-xr-xlib/regex/test/runtest.sh124
-rw-r--r--lib/regex/test/testmatch.myr58
-rw-r--r--lib/regex/test/tests29
-rw-r--r--lib/regex/test/unicode.myr19
11 files changed, 475 insertions, 0 deletions
diff --git a/lib/regex/test/basic.myr b/lib/regex/test/basic.myr
new file mode 100644
index 0000000..168b547
--- /dev/null
+++ b/lib/regex/test/basic.myr
@@ -0,0 +1,39 @@
+use std
+
+use "testmatch.use"
+
+const main = {
+ var s : byte[:]
+
+ s = std.strjoin([
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ ][:], "")
+ testmatch(".*bc", "Abc", `std.Some [][:])
+ testmatch("(a*)*", "a", `std.Some ["a"][:])
+ testmatch("(aa|aab?)*", s, `std.Some ["aa"][:])
+ /* greedy matches */
+ testmatch("(<.*>).*", "<a foo> blah <bar>", `std.Some [
+ "<a foo> blah <bar>",
+ ][:])
+ testmatch("(<.+>).*", "<a foo> blah <bar>", `std.Some [
+ "<a foo> blah <bar>",
+ ][:])
+ /* reluctant matches */
+ testmatch("(<.*?>).*", "<a foo> blah <bar>", `std.Some [
+ "<a foo>",
+ ][:])
+ testmatch("(<.+?>).*", "<a foo> blah <bar>", `std.Some [
+ "<a foo>",
+ ][:])
+}
diff --git a/lib/regex/test/bld.sub b/lib/regex/test/bld.sub
new file mode 100644
index 0000000..12f8056
--- /dev/null
+++ b/lib/regex/test/bld.sub
@@ -0,0 +1,7 @@
+test basic {inc=../libstd,inc=..} = basic.myr testmatch.myr;;
+test boundaries {inc=../libstd,inc=..} = boundaries.myr testmatch.myr;;
+test capture {inc=../libstd,inc=..} = capture.myr testmatch.myr;;
+test class {inc=../libstd,inc=..} = class.myr testmatch.myr;;
+test failmatch {inc=../libstd,inc=..} = failmatch.myr testmatch.myr;;
+test negclass {inc=../libstd,inc=..} = negclass.myr testmatch.myr;;
+test unicode {inc=../libstd,inc=..} = unicode.myr testmatch.myr;;
diff --git a/lib/regex/test/boundaries.myr b/lib/regex/test/boundaries.myr
new file mode 100644
index 0000000..196d197
--- /dev/null
+++ b/lib/regex/test/boundaries.myr
@@ -0,0 +1,36 @@
+use std
+use "testmatch.use"
+
+const main = {
+ /* expected matches */
+ testmatch("\\<([a-z]*)\\>", "abcdef", `std.Some [
+ "abcdef",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "!m!", `std.Some [ /* single char word in midstring */
+ "m",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "!m", `std.Some [ /* single char word at end of string */
+ "m",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "m!", `std.Some [ /* single char word at start of string */
+ "m",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "!@#!!matches!!%!", `std.Some [ /* word in midstring */
+ "matches",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "matches!!%!", `std.Some [ /* word at start of string */
+ "matches",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "!@#!!matches", `std.Some [ /* word at end of string */
+ "matches",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "!@#!!matches!!%!foo", `std.Some [ /* matches last word in string */
+ "foo",
+ ][:])
+ testmatch(".*(\\<.*\\>).*", "123", `std.Some [ /* numbers are also word bounds */
+ "123",
+ ][:])
+
+ /* nonmatches */
+ testmatch("\\<([a-z]*)\\>foo", "abcdefoo", `std.None) /* word boundary needed in midstring */
+}
diff --git a/lib/regex/test/capture.myr b/lib/regex/test/capture.myr
new file mode 100644
index 0000000..f270428
--- /dev/null
+++ b/lib/regex/test/capture.myr
@@ -0,0 +1,17 @@
+use std
+use "testmatch.use"
+
+const main = {
+ testmatch("A(.*)", "Abc", `std.Some ["bc"][:])
+ testmatch("A(.*)e", "Abcde", `std.Some ["bcd"][:])
+ testmatch("(a|b)+", "abab", `std.Some ["b"][:])
+ testmatch("A(b(.*)d)e", "Abcde", `std.Some [
+ "bcd",
+ "c"
+ ][:])
+ testmatch("(a?)(a*)(a?)", "aaaa", `std.Some [
+ "a",
+ "aaa",
+ ""
+ ][:])
+}
diff --git a/lib/regex/test/class.myr b/lib/regex/test/class.myr
new file mode 100644
index 0000000..284f440
--- /dev/null
+++ b/lib/regex/test/class.myr
@@ -0,0 +1,67 @@
+use std
+
+use "testmatch.use"
+
+const main = {
+ asciiclass()
+ set()
+ /*
+ unicodeclass()
+ negasciiclass()
+ negasciirange()
+ negset()
+ */
+}
+
+const asciiclass = {
+ /* \d success */
+ testmatch("\\d", "1", `std.Some [][:])
+ testmatch("\\d\\d", "13", `std.Some [][:])
+ testmatch("\\d+", "13688", `std.Some [][:])
+ /* \d fail */
+ testmatch("\\d", "x", `std.None)
+ testmatch("\\d\\d", "x3", `std.None)
+ testmatch("\\d+", "1368f", `std.None)
+
+ /* \x success */
+ testmatch("\\x", "a", `std.Some [][:])
+ testmatch("\\x\\x", "1F", `std.Some [][:])
+ testmatch("\\x+", "13b8cDEf", `std.Some [][:])
+ /* \x fail */
+ testmatch("\\x", "Z", `std.None)
+ testmatch("\\x\\x", "fg", `std.None)
+ testmatch("\\x+", "13b8cg", `std.None)
+
+ /* \s success */
+ testmatch("\\s", " ", `std.Some [][:])
+ testmatch("\\s\\s", "\t\n", `std.Some [][:])
+ testmatch("\\s+", "\t\n\r \t", `std.Some [][:])
+ /* \s fail */
+ testmatch("\\s", "a", `std.None)
+ testmatch("\\s\\s", "i\n", `std.None)
+ testmatch("\\s+", "\t\n\r.\t", `std.None)
+
+ /* word success */
+ testmatch("\\w+", "abcABC0123_", `std.Some [][:])
+ /* word fail */
+ testmatch("\\w+", "abcABC0123_.", `std.None)
+
+ /* \h success */
+ testmatch("\\h", " ", `std.Some [][:])
+ testmatch("\\h\\h", "\t ", `std.Some [][:])
+ testmatch("\\h+", "\t \t ", `std.Some [][:])
+ /* \h fail */
+ testmatch("\\h", "\n", `std.None)
+ testmatch("\\h\\h", "\t\r", `std.None)
+ testmatch("\\h+", "\t \t.", `std.None)
+}
+
+const set = {
+ /* ranges */
+ testmatch("[a-z]*", "abcd", `std.Some [][:])
+ testmatch("[a-zA-Z]*", "abCD", `std.Some [][:])
+ testmatch("[a-zA-Z0-9_]*", "_abCD018", `std.Some [][:])
+
+ testmatch("[abc]*", "abba", `std.Some [][:])
+ testmatch("[a-zABC]*", "abBa", `std.Some [][:])
+}
diff --git a/lib/regex/test/failmatch.myr b/lib/regex/test/failmatch.myr
new file mode 100644
index 0000000..c501d00
--- /dev/null
+++ b/lib/regex/test/failmatch.myr
@@ -0,0 +1,7 @@
+use std
+
+use "testmatch.use"
+
+const main = {
+ testmatch(".*bcd", "Abc", `std.None)
+}
diff --git a/lib/regex/test/negclass.myr b/lib/regex/test/negclass.myr
new file mode 100644
index 0000000..19404fa
--- /dev/null
+++ b/lib/regex/test/negclass.myr
@@ -0,0 +1,72 @@
+use std
+
+use "testmatch.use"
+
+const main = {
+ asciiclass()
+ set()
+ /*
+ unicodeclass()
+ negasciiclass()
+ negasciirange()
+ negset()
+ */
+}
+
+const asciiclass = {
+ /* \D success */
+ testmatch("\\D", "x", `std.Some [][:])
+ testmatch("\\D+", "xa!#^cs", `std.Some [][:])
+
+ /* \D fail: end of ranges chars */
+ testmatch("\\D", "0", `std.None)
+ testmatch("\\D", "9", `std.None)
+ testmatch("\\D+", "a35x", `std.None)
+ testmatch("\\D+", "13688", `std.None)
+
+ /* \X success */
+ testmatch("\\X", "Z", `std.Some [][:])
+ testmatch("\\X\\X", "gg", `std.Some [][:])
+ /* \X fail */
+ testmatch("\\X", "a", `std.None)
+ testmatch("\\X+", "zz13b8cDEf", `std.None)
+
+ /* \S success */
+ testmatch("\\S", "a", `std.Some [][:])
+ testmatch("\\S\\S", "i%", `std.Some [][:])
+ testmatch("\\S+", "alskd690!#!!", `std.Some [][:])
+
+ /* \S fail */
+ testmatch("\\S", " ", `std.None)
+ testmatch("\\S\\S", "\t\n", `std.None)
+ testmatch("\\S+", "\t \nkait", `std.None)
+
+ /* word success */
+ testmatch("\\W+", "!%!^^@@!^", `std.Some [][:])
+ /* word fail */
+ testmatch("\\W+", "a^#$bcABC0123_", `std.None)
+
+ /* \H success */
+ testmatch("\\H", "\n", `std.Some [][:])
+ testmatch("\\H\\H", "\n\r", `std.Some [][:])
+ /* \H fail */
+ testmatch("\\H+", "\t \t.", `std.None)
+ testmatch("\\H\\H", "\t ", `std.None)
+ testmatch("\\H+", "\ta35 \t ", `std.None)
+}
+
+const set = {
+ /* ranges: should succeed */
+ testmatch("[^a-z]*", "ABCD", `std.Some [][:])
+ testmatch("[^a-zA-Z]*", "1234", `std.Some [][:])
+ testmatch("[^a-zA-Z0-9_]*", "-^^-", `std.Some [][:])
+ testmatch("[^abc]*", "d6d", `std.Some [][:])
+ testmatch("[^a-zABC]*", "!^!!))#", `std.Some [][:])
+
+ /* ranges: should fail */
+ testmatch("[^a-z]*", "abcd", `std.None)
+ testmatch("[^a-zA-Z]*", "abCD", `std.None)
+ testmatch("[^a-zA-Z0-9_]*", "_abCD018", `std.None)
+ testmatch("[^abc]*", "abba", `std.None)
+ testmatch("[^a-zABC]*", "abBa", `std.None)
+}
diff --git a/lib/regex/test/runtest.sh b/lib/regex/test/runtest.sh
new file mode 100755
index 0000000..95f548f
--- /dev/null
+++ b/lib/regex/test/runtest.sh
@@ -0,0 +1,124 @@
+#!/bin/bash
+NFAILURES=0
+NPASSES=0
+
+function build {
+ rm -f $1 $1.o $1.s $1.use
+ myrbuild $FLAGS -b $1 $1.myr $EXTRA_SRC
+}
+
+function pass {
+ PASSED="$PASSED $1"
+ NPASSED=$[$NPASSED + 1]
+}
+
+function fail {
+ echo "FAIL: $1"
+ FAILED="$FAILED $1"
+ NFAILED=$[$NFAILED + 1]
+}
+
+function expectstatus {
+ ./$1 $3
+ if [ $? -eq $2 ]; then
+ pass $1
+ return
+ else
+ fail $1
+ fi
+}
+
+function expectprint {
+ if [ "`./$1 $3`" != "$2" ]; then
+ fail $1
+ else
+ pass $1
+ fi
+}
+
+
+function expectcompare {
+ if [ x"" != x"$TMPDIR" ]; then
+ t=$TMPDIR/myrtest-$1-$RANDOM
+ else
+ t=/tmp/myrtest-$1-$RANDOM
+ fi
+ ./$1 $3 > $t
+ if cmp $t data/$1-expected; then
+ pass $1
+ else
+ fail $1
+ fi
+ rm -f $t
+}
+
+function expectfcompare {
+ ./$1 $3
+ if cmp data/$1-expected $2; then
+ pass $1
+ else
+ fail $1
+ fi
+}
+
+function shouldskip {
+ if [ -z $ARGS ]; then
+ return 1
+ fi
+
+ for i in $ARGS; do
+ if [ $i = $1 ]; then
+ return 1
+ fi
+ done
+ return 0
+}
+
+
+# Should build and run
+function B {
+ if shouldskip $1; then
+ return
+ fi
+
+ test="$1"; shift
+ type="$1"; shift
+ res="$1"; shift
+ if [ $# > 0 ]; then
+ args="$1"; shift
+ fi
+ build $test
+ case $type in
+ "E") expectstatus "$test" "$res" "$input";;
+ "P") expectprint "$test" "$res" "$input";;
+ "C") expectcompare "$test" "$res" "$input";;
+ "F") expectfcompare "$test" "$res" "$args";;
+ esac
+}
+
+# Should fail
+function F {
+ if shouldskip $1; then
+ return
+ fi
+ (build $1) > /dev/null
+ if [ $? -eq '1' ]; then
+ pass $1
+ else
+ fail $1
+ fi
+}
+
+# Should generate a usefile
+function U {
+ return
+}
+
+source tests
+
+echo "PASSED ($NPASSED): $PASSED"
+if [ -z "$NFAILED" ]; then
+ echo "SUCCESS"
+else
+ echo "FAILURES ($NFAILED): $FAILED"
+fi
diff --git a/lib/regex/test/testmatch.myr b/lib/regex/test/testmatch.myr
new file mode 100644
index 0000000..4600410
--- /dev/null
+++ b/lib/regex/test/testmatch.myr
@@ -0,0 +1,58 @@
+use std
+use regex
+
+pkg =
+ const testmatch : (pat : byte[:], text : byte[:], expected : std.option(byte[:][:]) -> void)
+ const dbgmatch : (pat : byte[:], text : byte[:], expected : std.option(byte[:][:]) -> void)
+;;
+
+const testmatch = {pat, text, expected
+ run(regex.compile(pat), pat, text, expected)
+}
+
+const dbgmatch = {pat, text, expected
+ run(regex.dbgcompile(pat), pat, text, expected)
+}
+
+const run = {regex, pat, text, expected
+ var i, re
+
+ re = std.try(regex)
+ match regex.exec(re, text)
+ | `std.Some res:
+ match expected
+ | `std.None:
+ std.fatal("expected no match, got:")
+ for i = 0; i < res.len; i++
+ std.put("\t{}: {}\n", i, res[i])
+ ;;
+ | `std.Some exp:
+ if !std.sleq(res[0], text)
+ std.put("whole match does not match text!\n")
+ std.fatal("failed matching {} over {}\n", pat, text)
+ ;;
+ res = res[1:]
+ if res.len != exp.len
+ std.put("mismatch: expected {} matches, got {}\n", exp.len, res.len)
+ std.fatal("failed matching {} over {}\n", pat, text)
+ ;;
+ for i = 0; i < exp.len; i++
+ if !std.sleq(res[i], exp[i])
+ std.put("mismatch on {}: expected {}, got {}\n", i, exp[i], res[i])
+ std.fatal("failed matching {} over {}\n", pat, text)
+ ;;
+ ;;
+ ;;
+ | `std.None:
+ match expected
+ | `std.None: /* : expected failure */
+ | `std.Some matches:
+ std.put("expected matches:\n")
+ for i = 0; i < matches.len; i++
+ std.put("\t{}: {}\n", i, matches[i])
+ ;;
+ std.fatal("no match found\n")
+ ;;
+ ;;
+ regex.free(re)
+}
diff --git a/lib/regex/test/tests b/lib/regex/test/tests
new file mode 100644
index 0000000..a5f70f7
--- /dev/null
+++ b/lib/regex/test/tests
@@ -0,0 +1,29 @@
+FLAGS=-I../
+EXTRA_SRC=testmatch.myr
+# Format:
+# [B|F] testname [E|P] result
+# [B|F]: Compiler outcome.
+# B: Expect that this test will build.
+# F: Expect that this test will not build.
+# testname: Test case
+# The test that will run. We will try to
+# compile 'testname.myr' to 'testname',
+# and then execute it, verifying the result
+# [E|P|C]: Result type
+# E tells us that the result is an exit status
+# P tells us that the result is on stdout,
+# and should be compared to the value on the
+# line
+# C tells us that the result is on stdout,
+# and should be compared to the contents of
+# the file passed on the line.
+# result: Result value
+# What we compare with. This should be self-
+# evident.
+B basic C
+B boundaries C
+B capture C
+B class C
+B failmatch C
+B negclass C
+B unicode C
diff --git a/lib/regex/test/unicode.myr b/lib/regex/test/unicode.myr
new file mode 100644
index 0000000..5916a1c
--- /dev/null
+++ b/lib/regex/test/unicode.myr
@@ -0,0 +1,19 @@
+use std
+use regex
+
+use "testmatch.use"
+
+const main = {
+ testmatch(".*bæc", "Abæc", `std.Some [][:])
+ testmatch("(\\p{L}*)bæc\\P{L}*", \
+ "Aabæc%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", \
+ `std.Some ["Aa"][:])
+ /* test various syntaxen */
+ testmatch("(\\pL*)bæc\\PL*", \
+ "Aabæc%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", \
+ `std.Some ["Aa"][:])
+ testmatch("(\\p{Letter}*)bæc\\P{Uppercase_Letter}*", \
+ "Aabæc%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", \
+ `std.Some ["Aa"][:])
+ testmatch(".", "æ", `std.Some [][:])
+}