() = evalfile ("inc.sl"); testing_feature ("regexp"); % Tests go here.... private define test_regexp (pat, str, val, ans) { variable val1 = string_match (str, pat); % form 1 if (val != val1) failed ("string_match(%S,%S) ==> %S, not %S", str, pat, val1, val); if (val == 0) return; variable pos, len; (pos, len) = string_match_nth (0); variable ans1 = str[[pos:pos+len-1]]; if (ans1 != ans) failed ("string_match(%S,%S) ==> %S, not %S", str, pat, ans1, ans); } test_regexp (`.d`, "ffdoodfoo fob", 2, "fd"); test_regexp (`.*d`, "ffdoodfoo fob", 1, "ffdood"); test_regexp (`.?d`, "ffdoodfoo fob", 2, "fd"); test_regexp (`.+d`, "ffdoodfoo fob", 1, "ffdood"); % Looping test the RE cache loop (7) { test_regexp (`.\{1,3\}d`, "ffdoodfoo fob", 1, "ffd"); loop (5) { test_regexp (`.\{1,\}d`, "ffdoodfoo fob", 1, "ffdood"); loop (3) { test_regexp (`.\{4,\}d`, "ffdoodfoo fob", 1, "ffdood"); test_regexp (`.\{3,4\}d`, "ffdoodfoo fob", 2, "fdood"); } } } test_regexp (`.\{3\}d`, "ffdoodfoo fob", 3, "dood"); test_regexp(`\s+fo\so`, "fo o fo od", 5, " fo o"); test_regexp(`\s+\S+\s?d`, "fo o fo od", 11, " od"); test_regexp(`\s+\S+\s?o`, "fo o fo od", 5, " fo o"); test_regexp (`\c[A-Z]+`, "fooFOO4", 4, "FOO"); test_regexp (`\C[A-Z]+`, "fooFOO4", 1, "fooFOO"); test_regexp (`\`, "ffoodfoo fob", 0, NULL); test_regexp (`fo+\>`, "ffoodfoo fob", 6, "foo"); test_regexp (`fo?\>`, "ffoodfoo fob", 0, NULL); test_regexp (`fo?.\>`, "ffoodfoo fob", 6, "foo"); test_regexp (`\ %S, not %S", str, pat, val1, val); if (val == 0) return; variable ofs = 1+strbytelen (str); str = strcat (str, str); val1 = string_match (str, pat, ofs);% form 2 if (val1) val += ofs-1; if (val1 != val) failed ("string_match($str,$pat,$ofs) ==> $val1, not $val"$); if (ans == NULL) return; variable matches = string_matches (str, pat); if ((length (matches) < 2) || (ans != matches[1])) failed ("string_matches (%s, %s)", str, pat); } test_regexp_match ("B.*[1-5]", "0xAB123G", 4, NULL); test_regexp_match ("\([1-5]+\)G\1"R, "0xAB123G12F", 0, NULL); test_regexp_match ("\([1-5]+\)G\1"R, "0xAB123G123F", 5, "123"); test_regexp_match ("[1-5]", "0xAB123G", 5, NULL); test_regexp_match ("\([1-5]2\)"R, "0xAB123G", 5, "12"); test_regexp_match ("G", "0xAB123G", 8, NULL); test_regexp_match ("\(G\)"R, "0xAB123G", 8, "G"); test_regexp_match (`=\(\d\)`, "L=1X", 2, "1"); test_regexp_match (`=\(\d?\)`, "L=1X", 2, "1"); test_regexp_match (`=\(\d?\)`, "L=12X", 2, "1"); test_regexp_match (`=\(\d+\)`, "L=1X", 2, "1"); test_regexp_match (`=\(\d+\)`, "L=12X", 2, "12"); test_regexp_match (`=\(\d*\)`, "L=1X", 2, "1"); test_regexp_match (`=\(\d*\)`, "L=X", 2, ""); test_regexp_match (`\D+\d+\D?\(\d+\)`, "L=12X13", 1, "13"); static define test_globbing (glob, re) { variable pat = glob_to_regexp (glob); if (re != pat) failed ("glob_to_regexp (%s) produced %s, expected %s", glob, pat, re); } #iffalse test_globbing ("*.c", "^[^.].*\\.c$"); #else test_globbing ("*.c", "^.*\\.c$"); #endif test_globbing ("[*].c", "^[*]\\.c$"); test_globbing ("x+??$.$", "^x\\+..\\$\\.\\$$"); test_globbing ("x+[file$", "^x\\+\\[file\\$$"); test_globbing ("x+[^$", "^x\\+\\[^\\$$"); test_globbing ("x+[^]$", "^x\\+\\[^]\\$$"); test_globbing ("x+[^]]$", "^x\\+[^]]\\$$"); test_globbing ("[", "^\\[$"); test_globbing ("x[", "^x\\[$"); test_globbing ("x[]", "^x\\[]$"); test_globbing ("x[]]", "^x[]]$"); test_globbing ("x\\[]]", "^x\\\\[]]$"); print ("Ok\n"); exit (0);