2 ** wildmatch test suite.
5 /*#define COMPARE_WITH_FNMATCH*/
7 #define WILD_TEST_ITERATIONS
8 #include "lib/wildmatch.c"
12 #ifdef COMPARE_WITH_FNMATCH
15 int fnmatch_errors = 0;
18 int wildmatch_errors = 0;
22 int output_iterations = 0;
25 int empty_at_start = 0;
28 static struct poptOption long_options[] = {
29 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
30 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
31 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
32 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
36 /* match just at the start of string (anchored tests) */
38 run_test(int line, bool matches, bool same_as_fnmatch,
39 const char *text, const char *pattern)
42 #ifdef COMPARE_WITH_FNMATCH
44 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
46 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
51 char *texts[MAXPATHLEN], *hold;
52 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
56 if (!(buf = calloc(MAXPATHLEN * 2, 1)))
58 for (j = (MAXPATHLEN-1) / explode_mod; j >= 0; j--)
59 texts[j] = buf + j * (explode_mod + 1);
63 texts[ndx++][0] = '\0';
64 /* An empty string must turn into at least one empty array item. */
66 strncpy(texts[ndx++], text + pos, explode_mod);
67 if (pos + explode_mod >= len) {
68 texts[ndx-1][len - pos] = '\0';
72 if (!(++cnt % empties_mod))
73 texts[ndx++][0] = '\0';
76 texts[ndx++][0] = '\0';
79 matched = wildmatch_array(pattern, (const char**)texts, 0);
82 matched = wildmatch(pattern, text);
83 #ifdef COMPARE_WITH_FNMATCH
84 fn_matched = !fnmatch(pattern, text, flags);
86 if (matched != matches) {
87 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
88 line, text, pattern, matches? "a" : "NO");
91 #ifdef COMPARE_WITH_FNMATCH
92 if (fn_matched != (matches ^ !same_as_fnmatch)) {
93 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
94 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
98 if (output_iterations) {
99 printf("%d: \"%s\" iterations = %d\n", line, pattern,
100 wildmatch_iteration_count);
105 main(int argc, char **argv)
107 char buf[2048], *s, *string[2], *end[2];
110 int opt, line, i, flag[2];
111 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
114 while ((opt = poptGetNextOpt(pc)) != -1) {
117 arg = poptGetOptArg(pc);
118 empties_mod = atoi(arg);
119 if (strchr(arg, 's'))
121 if (strchr(arg, 'e'))
127 fprintf(stderr, "%s: %s\n",
128 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
134 if (explode_mod && !empties_mod)
137 argv = (char**)poptGetArgs(pc);
138 if (!argv || argv[1]) {
139 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
143 if ((fp = fopen(*argv, "r")) == NULL) {
144 fprintf(stderr, "Unable to open %s\n", *argv);
149 while (fgets(buf, sizeof buf, fp)) {
151 if (*buf == '#' || *buf == '\n')
153 for (s = buf, i = 0; i <= 1; i++) {
160 if (*++s != ' ' && *s != '\t')
163 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
167 while (*++s == ' ' || *s == '\t') {}
169 for (i = 0; i <= 1; i++) {
170 if (*s == '\'' || *s == '"' || *s == '`') {
173 while (*s && *s != quote) s++;
175 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
182 if (!*s || *s == '\n') {
183 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
188 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
191 while (*++s == ' ' || *s == '\t') {}
193 *end[0] = *end[1] = '\0';
194 run_test(line, flag[0], flag[1], string[0], string[1]);
197 if (!wildmatch_errors)
200 printf("%d", wildmatch_errors);
201 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
203 #ifdef COMPARE_WITH_FNMATCH
207 printf("%d", fnmatch_errors);
208 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");