1 #include "config_ast.h"  // IWYU pragma: keep
2 
3 #include <string.h>
4 
5 #include "ast.h"
6 #include "terror.h"
7 
8 struct ShellPatternMatch {
9     const char *input;
10     const char *pattern;
11     int expected_result;
12 };
13 
test_matching_patterns()14 void test_matching_patterns() {
15     int actual_result, expected_result;
16     struct ShellPatternMatch tests[] = {{"foo", "f*", 1},        {"foo", "f??", 1},
17                                         {"foo1", "foo[1-9]", 1}, {"foobar", "foo[a-c]ar", 1},
18                                         {"f**", "f\\*\\*", 1},   {"f??", "f\\?\\?", 1},
19                                         {NULL, NULL, 0}};
20 
21     for (int i = 0; tests[i].input; ++i) {
22         actual_result = strmatch(tests[i].input, tests[i].pattern);
23         expected_result = tests[i].expected_result;
24 
25         if (actual_result != expected_result) {
26             terror("strmatch() failed :: '%s' failed to match shell pattern '%s'", tests[i].input,
27                    tests[i].pattern);
28         }
29     }
30 
31     for (int i = 0; tests[i].input; ++i) {
32         actual_result = strgrpmatch(tests[i].input, tests[i].pattern, NULL, 0,
33                                     STR_MAXIMAL | STR_LEFT | STR_RIGHT);
34         expected_result = tests[i].expected_result;
35 
36         if (actual_result != expected_result) {
37             terror("strgrpmatch() failed :: '%s' failed to match shell pattern '%s'",
38                    tests[i].input, tests[i].pattern);
39         }
40     }
41 }
42 
test_unmatching_patterns()43 void test_unmatching_patterns() {
44     int actual_result, expected_result;
45     struct ShellPatternMatch tests[] = {{"foo", "f\\*\\*", 0},   {"foo", "f\\?\\?", 0},
46                                         {"foo1", "foo[2-9]", 0}, {"foobar", "foo[c-z]ar", 0},
47                                         {"f**", "f\\?\\?", 0},   {"f??", "f\\*\\*", 0},
48                                         {NULL, NULL, 0}};
49 
50     for (int i = 0; tests[i].input; ++i) {
51         actual_result = strmatch(tests[i].input, tests[i].pattern);
52         expected_result = tests[i].expected_result;
53 
54         if (actual_result != expected_result) {
55             terror("strmatch() failed :: '%s' matches unmatching shell pattern '%s'",
56                    tests[i].input, tests[i].pattern);
57         }
58     }
59 
60     for (int i = 0; tests[i].input; ++i) {
61         actual_result = strgrpmatch(tests[i].input, tests[i].pattern, NULL, 0,
62                                     STR_MAXIMAL | STR_LEFT | STR_RIGHT);
63         expected_result = tests[i].expected_result;
64 
65         if (actual_result != expected_result) {
66             terror("strgrpmatch() failed :: '%s' matches unmatching shell pattern '%s'",
67                    tests[i].input, tests[i].pattern);
68         }
69     }
70 }
71 
tmain()72 tmain() {
73     UNUSED(argc);
74     UNUSED(argv);
75 
76     test_matching_patterns();
77     test_unmatching_patterns();
78 
79     texit(0);
80 }
81