Commit | Line | Data |
---|---|---|
5de45bca WD |
1 | /* |
2 | ** wildmatch test suite. | |
3 | */ | |
4 | ||
6cd50960 | 5 | /*#define COMPARE_WITH_FNMATCH*/ |
5de45bca | 6 | |
d5c973cc | 7 | #define WILD_TEST_ITERATIONS |
076f60ee WD |
8 | #include "lib/wildmatch.c" |
9 | ||
10 | #include "popt.h" | |
11 | ||
5de45bca WD |
12 | #ifdef COMPARE_WITH_FNMATCH |
13 | #include <fnmatch.h> | |
97d53f8c WD |
14 | |
15 | int fnmatch_errors = 0; | |
5de45bca WD |
16 | #endif |
17 | ||
97d53f8c WD |
18 | int wildmatch_errors = 0; |
19 | ||
5de45bca WD |
20 | typedef char bool; |
21 | ||
22 | #define false 0 | |
23 | #define true 1 | |
24 | ||
d5c973cc | 25 | int output_iterations = 0; |
076f60ee WD |
26 | |
27 | static struct poptOption long_options[] = { | |
28 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
9a17dddb | 29 | {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0}, |
076f60ee WD |
30 | {0,0,0,0, 0, 0, 0} |
31 | }; | |
32 | ||
5de45bca WD |
33 | /* match just at the start of string (anchored tests) */ |
34 | static void | |
97d53f8c WD |
35 | run_test(int line, bool matches, bool same_as_fnmatch, |
36 | const char *text, const char *pattern) | |
5de45bca WD |
37 | { |
38 | bool matched; | |
39 | #ifdef COMPARE_WITH_FNMATCH | |
40 | bool fn_matched; | |
41 | int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME; | |
88085892 WD |
42 | #else |
43 | same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */ | |
5de45bca WD |
44 | #endif |
45 | ||
46 | matched = wildmatch(pattern, text); | |
47 | #ifdef COMPARE_WITH_FNMATCH | |
48 | fn_matched = !fnmatch(pattern, text, flags); | |
49 | #endif | |
50 | if (matched != matches) { | |
97d53f8c WD |
51 | printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n", |
52 | line, text, pattern, matches? "a" : "NO"); | |
53 | wildmatch_errors++; | |
5de45bca WD |
54 | } |
55 | #ifdef COMPARE_WITH_FNMATCH | |
56 | if (fn_matched != (matches ^ !same_as_fnmatch)) { | |
97d53f8c WD |
57 | printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n", |
58 | line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO"); | |
59 | fnmatch_errors++; | |
5de45bca WD |
60 | } |
61 | #endif | |
b6b42c89 WD |
62 | if (output_iterations) { |
63 | printf("%d: \"%s\" iterations = %d\n", line, pattern, | |
64 | wildmatch_iteration_count); | |
65 | } | |
5de45bca WD |
66 | } |
67 | ||
68 | int | |
69 | main(int argc, char **argv) | |
70 | { | |
97d53f8c WD |
71 | char buf[2048], *s, *string[2], *end[2]; |
72 | FILE *fp; | |
73 | int opt, line, i, flag[2]; | |
076f60ee WD |
74 | poptContext pc = poptGetContext("wildtest", argc, (const char**)argv, |
75 | long_options, 0); | |
76 | ||
77 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
78 | switch (opt) { | |
79 | default: | |
9a17dddb WD |
80 | fprintf(stderr, "%s: %s\n", |
81 | poptBadOption(pc, POPT_BADOPTION_NOALIAS), | |
82 | poptStrerror(opt)); | |
076f60ee WD |
83 | exit(1); |
84 | } | |
85 | } | |
5de45bca | 86 | |
c4cd2ca6 WD |
87 | argv = (char**)poptGetArgs(pc); |
88 | if (!argv || argv[1]) { | |
89 | fprintf(stderr, "Usage: wildtest TESTFILE\n"); | |
90 | exit(1); | |
91 | } | |
92 | ||
93 | if ((fp = fopen(*argv, "r")) == NULL) { | |
94 | fprintf(stderr, "Unable to open %s\n", *argv); | |
97d53f8c WD |
95 | exit(1); |
96 | } | |
97 | ||
98 | line = 0; | |
99 | while (fgets(buf, sizeof buf, fp)) { | |
100 | line++; | |
101 | if (*buf == '#' || *buf == '\n') | |
102 | continue; | |
103 | for (s = buf, i = 0; i <= 1; i++) { | |
104 | if (*s == '1') | |
105 | flag[i] = 1; | |
106 | else if (*s == '0') | |
107 | flag[i] = 0; | |
108 | else | |
109 | flag[i] = -1; | |
110 | if (*++s != ' ' && *s != '\t') | |
111 | flag[i] = -1; | |
112 | if (flag[i] < 0) { | |
c4cd2ca6 WD |
113 | fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s", |
114 | line, *argv, buf); | |
97d53f8c WD |
115 | exit(1); |
116 | } | |
117 | while (*++s == ' ' || *s == '\t') {} | |
118 | } | |
119 | for (i = 0; i <= 1; i++) { | |
120 | if (*s == '\'' || *s == '"' || *s == '`') { | |
121 | char quote = *s++; | |
122 | string[i] = s; | |
123 | while (*s && *s != quote) s++; | |
124 | if (!*s) { | |
c4cd2ca6 WD |
125 | fprintf(stderr, "Unmatched quote on line %d of %s:\n%s", |
126 | line, *argv, buf); | |
97d53f8c WD |
127 | exit(1); |
128 | } | |
129 | end[i] = s; | |
130 | } | |
131 | else { | |
132 | if (!*s || *s == '\n') { | |
c4cd2ca6 WD |
133 | fprintf(stderr, "Not enough strings on line %d of %s:\n%s", |
134 | line, *argv, buf); | |
97d53f8c WD |
135 | exit(1); |
136 | } | |
137 | string[i] = s; | |
138 | while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {} | |
139 | end[i] = s; | |
140 | } | |
141 | while (*++s == ' ' || *s == '\t') {} | |
142 | } | |
143 | *end[0] = *end[1] = '\0'; | |
144 | run_test(line, flag[0], flag[1], string[0], string[1]); | |
145 | } | |
146 | ||
147 | if (!wildmatch_errors) | |
16859cdb | 148 | fputs("No", stdout); |
97d53f8c | 149 | else |
16859cdb WD |
150 | printf("%d", wildmatch_errors); |
151 | printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s"); | |
97d53f8c WD |
152 | |
153 | #ifdef COMPARE_WITH_FNMATCH | |
154 | if (!fnmatch_errors) | |
16859cdb | 155 | fputs("No", stdout); |
97d53f8c | 156 | else |
16859cdb WD |
157 | printf("%d", fnmatch_errors); |
158 | printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s"); | |
97d53f8c WD |
159 | |
160 | #endif | |
5de45bca WD |
161 | |
162 | return 0; | |
163 | } |