2 * Test suite for the wildmatch code.
4 * Copyright (C) 2003-2009 Wayne Davison
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, visit the http://fsf.org website.
20 /*#define COMPARE_WITH_FNMATCH*/
22 #define WILD_TEST_ITERATIONS
23 #include "lib/wildmatch.c"
27 #ifdef COMPARE_WITH_FNMATCH
30 int fnmatch_errors = 0;
33 int wildmatch_errors = 0;
34 char number_separator = ',';
38 int output_iterations = 0;
41 int empty_at_start = 0;
44 static struct poptOption long_options[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
46 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
47 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
48 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
52 /* match just at the start of string (anchored tests) */
54 run_test(int line, bool matches,
55 #ifdef COMPARE_WITH_FNMATCH
58 const char *text, const char *pattern)
61 #ifdef COMPARE_WITH_FNMATCH
63 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
67 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
68 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
72 /* An empty string must turn into at least one empty array item. */
74 texts[ndx] = buf + ndx * (explode_mod + 1);
75 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
76 if (pos + explode_mod >= len)
79 if (!(++cnt % empties_mod))
85 matched = wildmatch_array(pattern, (const char**)texts, 0);
87 matched = wildmatch(pattern, text);
88 #ifdef COMPARE_WITH_FNMATCH
89 fn_matched = !fnmatch(pattern, text, flags);
91 if (matched != matches) {
92 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
93 line, text, pattern, matches? "a" : "NO");
96 #ifdef COMPARE_WITH_FNMATCH
97 if (fn_matched != (matches ^ !same_as_fnmatch)) {
98 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
99 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
103 if (output_iterations) {
104 printf("%d: \"%s\" iterations = %d\n", line, pattern,
105 wildmatch_iteration_count);
110 main(int argc, char **argv)
112 char buf[2048], *s, *string[2], *end[2];
115 int opt, line, i, flag[2];
116 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
119 while ((opt = poptGetNextOpt(pc)) != -1) {
122 arg = poptGetOptArg(pc);
123 empties_mod = atoi(arg);
124 if (strchr(arg, 's'))
126 if (strchr(arg, 'e'))
132 fprintf(stderr, "%s: %s\n",
133 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
139 if (explode_mod && !empties_mod)
142 argv = (char**)poptGetArgs(pc);
143 if (!argv || argv[1]) {
144 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
148 if ((fp = fopen(*argv, "r")) == NULL) {
149 fprintf(stderr, "Unable to open %s\n", *argv);
154 while (fgets(buf, sizeof buf, fp)) {
156 if (*buf == '#' || *buf == '\n')
158 for (s = buf, i = 0; i <= 1; i++) {
165 if (*++s != ' ' && *s != '\t')
168 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
172 while (*++s == ' ' || *s == '\t') {}
174 for (i = 0; i <= 1; i++) {
175 if (*s == '\'' || *s == '"' || *s == '`') {
178 while (*s && *s != quote) s++;
180 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
187 if (!*s || *s == '\n') {
188 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
193 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
196 while (*++s == ' ' || *s == '\t') {}
198 *end[0] = *end[1] = '\0';
199 run_test(line, flag[0],
200 #ifdef COMPARE_WITH_FNMATCH
203 string[0], string[1]);
206 if (!wildmatch_errors)
209 printf("%d", wildmatch_errors);
210 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
212 #ifdef COMPARE_WITH_FNMATCH
216 printf("%d", fnmatch_errors);
217 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");