2 * Test suite for the wildmatch code.
4 * Copyright (C) 2003-2007 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 version 3 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, visit the http://fsf.org website.
19 /*#define COMPARE_WITH_FNMATCH*/
21 #define WILD_TEST_ITERATIONS
22 #include "lib/wildmatch.c"
26 #ifdef COMPARE_WITH_FNMATCH
29 int fnmatch_errors = 0;
32 int wildmatch_errors = 0;
36 int output_iterations = 0;
39 int empty_at_start = 0;
42 static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
45 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
46 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
50 /* match just at the start of string (anchored tests) */
52 run_test(int line, bool matches, bool same_as_fnmatch,
53 const char *text, const char *pattern)
56 #ifdef COMPARE_WITH_FNMATCH
58 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
60 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
64 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
65 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
69 /* An empty string must turn into at least one empty array item. */
71 texts[ndx] = buf + ndx * (explode_mod + 1);
72 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
73 if (pos + explode_mod >= len)
76 if (!(++cnt % empties_mod))
82 matched = wildmatch_array(pattern, (const char**)texts, 0);
84 matched = wildmatch(pattern, text);
85 #ifdef COMPARE_WITH_FNMATCH
86 fn_matched = !fnmatch(pattern, text, flags);
88 if (matched != matches) {
89 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
90 line, text, pattern, matches? "a" : "NO");
93 #ifdef COMPARE_WITH_FNMATCH
94 if (fn_matched != (matches ^ !same_as_fnmatch)) {
95 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
96 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
100 if (output_iterations) {
101 printf("%d: \"%s\" iterations = %d\n", line, pattern,
102 wildmatch_iteration_count);
107 main(int argc, char **argv)
109 char buf[2048], *s, *string[2], *end[2];
112 int opt, line, i, flag[2];
113 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
116 while ((opt = poptGetNextOpt(pc)) != -1) {
119 arg = poptGetOptArg(pc);
120 empties_mod = atoi(arg);
121 if (strchr(arg, 's'))
123 if (strchr(arg, 'e'))
129 fprintf(stderr, "%s: %s\n",
130 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
136 if (explode_mod && !empties_mod)
139 argv = (char**)poptGetArgs(pc);
140 if (!argv || argv[1]) {
141 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
145 if ((fp = fopen(*argv, "r")) == NULL) {
146 fprintf(stderr, "Unable to open %s\n", *argv);
151 while (fgets(buf, sizeof buf, fp)) {
153 if (*buf == '#' || *buf == '\n')
155 for (s = buf, i = 0; i <= 1; i++) {
162 if (*++s != ' ' && *s != '\t')
165 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
169 while (*++s == ' ' || *s == '\t') {}
171 for (i = 0; i <= 1; i++) {
172 if (*s == '\'' || *s == '"' || *s == '`') {
175 while (*s && *s != quote) s++;
177 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
184 if (!*s || *s == '\n') {
185 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
190 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
193 while (*++s == ' ' || *s == '\t') {}
195 *end[0] = *end[1] = '\0';
196 run_test(line, flag[0], flag[1], string[0], string[1]);
199 if (!wildmatch_errors)
202 printf("%d", wildmatch_errors);
203 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
205 #ifdef COMPARE_WITH_FNMATCH
209 printf("%d", fnmatch_errors);
210 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");