Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for issue 69 #1

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions tests/test3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}