From eb7aaf6a6fde6ed1682ec12094c0b428a5ee0b6b Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Tue, 13 Apr 2021 10:04:44 -0600 Subject: [PATCH] add test for issue 69 I've added a test for https://github.com/kokke/tiny-regex-c/issues/69. In order to do this I enhanced the new test3.c file to support patterns that match in the middle of text. --- tests/test3.c | 65 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/tests/test3.c b/tests/test3.c index b78591d..33e7d71 100644 --- a/tests/test3.c +++ b/tests/test3.c @@ -5,39 +5,65 @@ #include "re.h" #ifdef RE_CSTRINGS + #define RE_MATCH re_match_cstr #define RE_MATCH_START_ONLY re_match_start_only_cstr #define text_args(t) t #else + #define RE_MATCH re_match #define RE_MATCH_START_ONLY re_match_start_only #define text_args(t) t, t + strlen(t) #endif -void test(const char *regex, const char *text, int expected_match_len) +static void checkMatch(struct re_context *context, int match_result, re_length_t match_start, re_length_t expected_match_start, int expected_match_len) { - printf("Pattern '%s' Text '%s'\n", regex, text); - struct re_context context = {0}; - - if (RE_MATCH_START_ONLY(&context, regex, text_args(text))) { - if (expected_match_len == -1) { - printf("fail: expected not to match\n"); - exit(1); - } - if ((re_length_t)expected_match_len != context.match_length) { - printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context.match_length); - exit(1); + if (match_result) { + if (expected_match_start != match_start) { + printf("fail: expected not to match to start at %llu but started at %llu\n", + (unsigned long long)expected_match_start, (unsigned long long)match_start); + exit(1); + } + if (expected_match_len == -1) { + printf("fail: expected not to match\n"); + exit(1); + } + if ((re_length_t)expected_match_len != context->match_length) { + printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context->match_length); + exit(1); + } + } else { + if (expected_match_len != -1) { + printf("fail: expected a match\n"); + exit(1); + } } - } else { - if (expected_match_len != -1) { - printf("fail: expected a match\n"); + if (context->error) { + printf("fail: unexpected error %s\n", re_error_name_table[context->error]); exit(1); } +} + +void testExt(const char *regex, const char *text, re_length_t expected_match_start, int expected_match_len) +{ + printf("Pattern '%s' Text '%s'\n", regex, text); + { + struct re_context context = {0}; + re_length_t match_start; + int match_result = RE_MATCH(&context, regex, text_args(text), &match_start); + checkMatch(&context, match_result, match_start, expected_match_start, expected_match_len); } - if (context.error) { - printf("fail: unexpected error %s\n", re_error_name_table[context.error]); - exit(1); + + if (expected_match_start == 0) { + struct re_context context = {0}; + int match_result = RE_MATCH_START_ONLY(&context, regex, text_args(text)); + checkMatch(&context, match_result, 0, 0, expected_match_len); } } +void test(const char *regex, const char *text, int expected_match_len) +{ + testExt(regex, text, 0, expected_match_len); +} + void testbad(const char *regex, const char *text, enum re_error error) { printf("BadPattern '%s' Text '%s'\n", regex, text); @@ -194,4 +220,7 @@ int main() // https://github.com/kokke/tiny-regex-c/issues/53 test(".*a.*a", "aa", 2); + + // https://github.com/kokke/tiny-regex-c/issues/69 + testExt("{\\w+}", "{{FW}_TEST", 1, 4); }