-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
54 lines (45 loc) · 2.01 KB
/
Copy pathmain.c
File metadata and controls
54 lines (45 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "regex.h"
#include <stdbool.h>
#include <stdio.h>
#define TEST(pattern_str, text_str, expected) \
do { \
void *re = compile(pattern_str); \
bool res = match(text_str, re); \
if (res == (expected)) { \
printf(" [PASS] pattern: \"%s\" | text: \"%s\"\n", pattern_str, \
text_str); \
passed++; \
} else { \
printf( \
" [FAIL] pattern: \"%s\" | text: \"%s\" (expected %s, got %s)\n", \
pattern_str, text_str, (expected) ? "match" : "no match", \
res ? "match" : "no match"); \
failed++; \
} \
} while (0)
int main() {
int passed = 0;
int failed = 0;
printf("=== Regex Tests ===\n\n");
printf(" Literals\n");
TEST("abc", "abc", true);
TEST("abc", "abd", false);
TEST("abc", "ab", false);
printf("\n Kleene Star (*)\n");
TEST("a*", "", true);
TEST("a*", "a", true);
TEST("a*", "aaaa", true);
TEST("ab*c", "ac", true);
TEST("ab*c", "abbc", true);
printf("\n Wildcard (.)\n");
TEST("a.c", "abc", true);
TEST("a.c", "axc", true);
TEST("a.c", "ac", false);
printf("\n Or (|)\n");
TEST("(cat)|(bat)", "cat", true);
TEST("(cat)|(bat)", "bat", true);
TEST("(cat)|(bat)", "rat", false);
printf("\n==================================\n");
printf("Results: %d passed, %d failed\n", passed, failed);
return failed > 0 ? 1 : 0;
}