From e90aa90faf9f061a0c51b95aaad19de40aea8449 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Fri, 3 Sep 2021 16:47:23 +0300 Subject: [PATCH] added some more tests --- src/Jamfile | 1 + src/sretests.c | 70 ++++++++++++++++++++++++++++++++ src/testregexps.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 src/sretests.c create mode 100644 src/testregexps.c diff --git a/src/Jamfile b/src/Jamfile index 0bdc924..59aefab 100644 --- a/src/Jamfile +++ b/src/Jamfile @@ -14,6 +14,7 @@ Re9Main test3 : test3.c ; #Re9Main test4 : test4.c ; Re9Main test5 : test5.c ; Re9Main test6 : test6.c ; +Re9Main sretests : sretests.c ; SubInclude TOP src libre9 ; diff --git a/src/sretests.c b/src/sretests.c new file mode 100644 index 0000000..e8fa0ec --- /dev/null +++ b/src/sretests.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include "libre9/re9.h" + +#include "testregexps.c" + + +int main (int argc, char *argv[]) { + int c; + re9_sub_t subs[RE9_SUBEXP_MAX]; + char matches[5][512]; + int fail; + const TestItem *ti = tests; + + int showAll = 1; + if (argc > 1 && strcmp(argv[1], "fail") == 0) showAll = 0; + + for (; ti->re; ti++) { + const char *errmsg; + re9_prog_t *prog = re9_compile(ti->re, RE9_FLAG_NONUTF8, &errmsg); + if (!prog) { + printf("[%s]: error %s\n", ti->re, errmsg); + return 1; + } + for (c = 0; c < 5; c++) matches[c][0] = '\0'; + if (showAll) printf("%s : %s : ", ti->re, ti->str); fflush(stdout); + memset(subs, 0, sizeof(subs)); + if (re9_execute(prog, RE9_FLAG_NONUTF8, ti->str, subs, RE9_SUBEXP_MAX)) { + /* save matched strings */ + int mn; + for (mn = 0; mn < re9_nsub(prog); mn++) { + if (subs[mn].sp && subs[mn].ep && subs[mn].sp != subs[mn].ep) { + int len = (int)(ptrdiff_t)(subs[mn].ep-subs[mn].sp); + memcpy(matches[mn], subs[mn].sp, len); + matches[mn][len] = '\0'; + } + } + } + /* check */ + fail = 0; + for (c = 0; !fail && c < 5; c++) { + if (ti->matches[c]) { + if (strcmp(ti->matches[c], matches[c])) fail = 1; + } + } + if (fail) { + printf("---------------\n"); + if (!showAll) printf("%s : %s : ", ti->re, ti->str); + printf("FAIL!\n"); + for (c = 0; c < 5; c++) { + printf(" %d", c); + if (ti->matches[c] && strcmp(ti->matches[c], matches[c])) { + printf("\n must: '%s'\n", ti->matches[c]); + printf(" got : '%s'\n", matches[c]); + printf(" (%s)\n", strcmp(ti->matches[c], matches[c])?"BAD":"OK"); + } else { + printf(": OK\n"); + } + } + } else { + if (showAll) printf("OK\n"); + } + re9_free(prog); + } + + return 0; +} diff --git a/src/testregexps.c b/src/testregexps.c new file mode 100644 index 0000000..54dd82e --- /dev/null +++ b/src/testregexps.c @@ -0,0 +1,116 @@ +typedef struct { + const char *re; + const char *str; + const char *matches[5]; +} TestItem; +static const TestItem tests[] = { +/* simple */ +{"a(b)c", "abc", {"abc", "b", "", "", ""}}, +{"a((b)(c))", "abc", {"abc", "bc", "b", "c", ""}}, +{"a((((b))))c", "abc", {"abc", "b", "b", "b", "b"}}, +{"a((((b))))c", "a(b)c", {"", "", "", "", ""}}, +{"a\\(", "a(", {"a(", "", "", "", ""}}, +{"a()b", "ab", {"ab", "", "", "", ""}}, +{"a()()b", "ab", {"ab", "", "", "", ""}}, +{"(we|wee|week|frob)(knights|night|day)", "weeknights", {"weeknights", "wee", "knights", "", ""}}, +{"aa|(bb)|cc", "aabb", {"aa", "", "", "", ""}}, +{"aa|(bb)|cc", "abbaa", {"bb", "bb", "", "", ""}}, +{"aa|(bb)|cc", "bccaa", {"cc", "", "", "", ""}}, +{"aa|a(b)|cc", "abaab", {"ab", "b", "", "", ""}}, +{"aa|a(b)", "abaab", {"ab", "b", "", "", ""}}, +{"aa|(a(b))|cc", "abaabcc", {"ab", "ab", "b", "", ""}}, +{"(ab)|ac", "aaaabcc", {"ab", "ab", "", "", ""}}, +{"(a(b))|ac", "abaabcc", {"ab", "ab", "b", "", ""}}, +{"ab|(ac)", "aaaabcc", {"ab", "", "", "", ""}}, +{"ab|(ac)", "aaaacbc", {"ac", "ac", "", "", ""}}, +{"aa|(ab|(ac))|ad", "cac", {"ac", "ac", "ac", "", ""}}, +{"(aa|(a(b)|a(c))|ad)", "cac", {"ac", "ac", "ac", "", "c"}}, +{"(.)*", "abc", {"abc", "c", "", "", ""}}, +{"b|()|a", "cac", {"", "", "", "", ""}}, +/* simple meta */ +{"a.c", "abc", {"abc", "", "", "", ""}}, +{"a..", "abc", {"abc", "", "", "", ""}}, +{"a..", "ab", {"", "", "", "", ""}}, +{"...", "ab", {"", "", "", "", ""}}, +{".", "abc", {"a", "", "", "", ""}}, +{".", "", {"", "", "", "", ""}}, +/* anchors */ +{"^abc", "abcd", {"abc", "", "", "", ""}}, +{"^abc", "aabcd", {"", "", "", "", ""}}, +{"^abc|def", "abc", {"abc", "", "", "", ""}}, +{"^abc|def", "zabc", {"", "", "", "", ""}}, +{"^abc|def", "zabcdef", {"def", "", "", "", ""}}, +{"abc|^def", "defabc", {"def", "", "", "", ""}}, +{"abc|^def", "abcdef", {"abc", "", "", "", ""}}, +{"abc|^def", "defabbc", {"def", "", "", "", ""}}, +{"abc|^def", "adefbc", {"", "", "", "", ""}}, +{"^(abc|def)", "abc", {"abc", "abc", "", "", ""}}, +{"^(abc|def)", "aabc", {"", "", "", "", ""}}, +{"(^abc|def)", "abcdef", {"abc", "abc", "", "", ""}}, +{"(^abc|def)", "^abcdef", {"def", "def", "", "", ""}}, +{"^", "hoge", {"", "", "", "", ""}}, +{"$", "hoge", {"", "", "", "", ""}}, +{"abc$", "bcabc", {"abc", "", "", "", ""}}, +{"abc$", "abcab", {"", "", "", "", ""}}, +{"^abc$", "abc", {"abc", "", "", "", ""}}, +{"^$", "", {"", "", "", "", ""}}, +{"^$", "a", {"", "", "", "", ""}}, +{"abc$|def", "abc", {"abc", "", "", "", ""}}, +{"abc$|def", "defabc", {"def", "", "", "", ""}}, +{"^abc|def$", "abcdef", {"abc", "", "", "", ""}}, +{"^abc|def$", "defabc", {"", "", "", "", ""}}, +{"^abc|def$", "defabc", {"", "", "", "", ""}}, +{"(^abc|def$)", "aaadef", {"def", "def", "", "", ""}}, +{"ab\\$", "ab$cd", {"ab$", "", "", "", ""}}, +/* backslash escape */ +{"a\\*c", "a*c", {"a*c", "", "", "", ""}}, +{"a\\.c", "a.c", {"a.c", "", "", "", ""}}, +{"a\\.c", "abc", {"", "", "", "", ""}}, +{"a\\\\b", "a\\b", {"a\\b", "", "", "", ""}}, +{"a\\\\\\*b", "a\\*b", {"a\\*b", "", "", "", ""}}, +{"a\\\\bc", "a\\bc", {"a\\bc", "", "", "", ""}}, +{"a\\[b", "a[b", {"a[b", "", "", "", ""}}, +/* repetitions */ +{"ab*c", "abc", {"abc", "", "", "", ""}}, +{"ab*c", "ac", {"ac", "", "", "", ""}}, +{"ab*c", "abbbc", {"abbbc", "", "", "", ""}}, +{"ab*c", "abbabaabbc", {"abbc", "", "", "", ""}}, +{"ab+c", "abc", {"abc", "", "", "", ""}}, +{"ab+c", "abbc", {"abbc", "", "", "", ""}}, +{"ab+c", "abbabaabbc", {"abbc", "", "", "", ""}}, +{"ab?c", "abc", {"abc", "", "", "", ""}}, +{"ab?c", "abbaac", {"ac", "", "", "", ""}}, +{"a.*c", "abc", {"abc", "", "", "", ""}}, +{"a.*c", "zaabcabcabcabcczab", {"aabcabcabcabcc", "", "", "", ""}}, +{"a(b*|c)d", "abbd", {"abbd", "bb", "", "", ""}}, +{"a(b*|c)d", "ad", {"ad", "", "", "", ""}}, +{"a(b*|c)d", "acd", {"acd", "c", "", "", ""}}, +{"a(b*|c)d", "abcd", {"", "", "", "", ""}}, +{"a.*c", "bacbababbbbadbaba", {"ac", "", "", "", ""}}, +{"a.*c", "abaaaabababbadbabdba", {"", "", "", "", ""}}, +/* repetitions (non-greedy) */ +{"ab*?.", "abc", {"ab", "", "", "", ""}}, +{"ab*?.", "ac", {"ac", "", "", "", ""}}, +{"a.*?c", "abbbc", {"abbbc", "", "", "", ""}}, +{"a.*?a", "abbabaabbc", {"abba", "", "", "", ""}}, +{"<.*?>", "", {"", "", "", "", ""}}, +{"ab+?.", "abc", {"abc", "", "", "", ""}}, +{"ab+?.", "abbc", {"abb", "", "", "", ""}}, +{"a.+?a", "abbabaabbc", {"abba", "", "", "", ""}}, +{"<.+?>", " <>", {"<>", "", "", "", ""}}, +{"ab??c", "abc", {"abc", "", "", "", ""}}, +{"ab??c", "abbaac", {"ac", "", "", "", ""}}, +{"ab??.", "abbaac", {"ab", "", "", "", ""}}, +{"a(hoge)??hoge", "ahogehoge", {"ahoge", "", "", "", ""}}, +{"(foo)??bar", "foobar", {"foobar", "foo", "", "", ""}}, +{"(foo)??bar", "foofoobar", {"foobar", "foo", "", "", ""}}, +{"(foo)*?bar", "foofoobar", {"foofoobar", "foo", "", "", ""}}, +/* uncapturing group */ +{"a(?:b)*c(d)", "abcdbcdefg", {NULL, "d", NULL, NULL, NULL}}, +{"a(?:bcd)*e(f)", "abcdbcdefg", {NULL, "f", NULL, NULL, NULL}}, +{"a(?:bcd)*e(f)", "aefg", {NULL, "f", NULL, NULL, NULL}}, +{"a(?:bcd)+e(f)", "aefg", {NULL, "", NULL, NULL, NULL}}, +{"a(?:bc(de(?:fg)?hi)jk)?l", "abcdefghijkl", {NULL, "defghi", NULL, NULL, NULL}}, +{"a(?:bc(de(?:fg)?hi)jk)?l", "abcdehijkl", {NULL, "dehi", NULL, NULL, NULL}}, +{NULL, NULL, {NULL, NULL, NULL, NULL, NULL}} +}; -- 2.11.4.GIT