Debian Codegraph @

wildtest.c

raw symbols SCIP
1 /*
2 * Test suite for the wildmatch code.
3 *
4 * Copyright (C) 2003-2019 Wayne Davison
5 *
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.
10 *
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.
15 *
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.
18 */
19
20 /*#define COMPARE_WITH_FNMATCH*/
21
22 #define WILD_TEST_ITERATIONS
23 #include "lib/wildmatch.c"
24
25 #include <popt.h>
26 #include <stdbool.h>
27
28 #ifdef COMPARE_WITH_FNMATCH
29 #include <fnmatch.h>
30
31 int fnmatch_errors = 0;
32 #endif
33
34 int wildmatch_errors = 0;
35
36 int output_iterations = 0;
37 int explode_mod = 0;
38 int empties_mod = 0;
39 int empty_at_start = 0;
40 int empty_at_end = 0;
41
42 static const 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},
47 {0,0,0,0, 0, 0, 0}
48 };
49
50 /* match just at the start of string (anchored tests) */
51 static void
52 run_test(int line, bool matches,
53 #ifdef COMPARE_WITH_FNMATCH
54 bool same_as_fnmatch,
55 #endif
56 const char *text, const char *pattern)
57 {
58 bool matched;
59 #ifdef COMPARE_WITH_FNMATCH
60 bool fn_matched;
61 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
62 #endif
63
64 if (explode_mod) {
65 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
66 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
67
68 if (empty_at_start)
69 texts[ndx++] = "";
70 /* An empty string must turn into at least one empty array item. */
71 while (1) {
72 texts[ndx] = buf + ndx * (explode_mod + 1);
73 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
74 if (pos + explode_mod >= len)
75 break;
76 pos += explode_mod;
77 if (!(++cnt % empties_mod))
78 texts[ndx++] = "";
79 }
80 if (empty_at_end)
81 texts[ndx++] = "";
82 texts[ndx] = NULL;
83 matched = wildmatch_array(pattern, (const char**)texts, 0);
84 } else
85 matched = wildmatch(pattern, text);
86 #ifdef COMPARE_WITH_FNMATCH
87 fn_matched = !fnmatch(pattern, text, flags);
88 #endif
89 if (matched != matches) {
90 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
91 line, text, pattern, matches? "a" : "NO");
92 wildmatch_errors++;
93 }
94 #ifdef COMPARE_WITH_FNMATCH
95 if (fn_matched != (matches ^ !same_as_fnmatch)) {
96 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
97 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
98 fnmatch_errors++;
99 }
100 #endif
101 if (output_iterations) {
102 printf("%d: \"%s\" iterations = %d\n", line, pattern,
103 wildmatch_iteration_count);
104 }
105 }
106
107 int
108 main(int argc, char **argv)
109 {
110 char buf[2048], *s, *string[2], *end[2];
111 const char *arg;
112 FILE *fp;
113 int opt, line, i, flag[2];
114 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
115 long_options, 0);
116
117 while ((opt = poptGetNextOpt(pc)) != -1) {
118 switch (opt) {
119 case 'e':
120 arg = poptGetOptArg(pc);
121 empties_mod = atoi(arg);
122 if (strchr(arg, 's'))
123 empty_at_start = 1;
124 if (strchr(arg, 'e'))
125 empty_at_end = 1;
126 if (!explode_mod)
127 explode_mod = 1024;
128 break;
129 default:
130 fprintf(stderr, "%s: %s\n",
131 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
132 poptStrerror(opt));
133 exit(1);
134 }
135 }
136
137 if (explode_mod && !empties_mod)
138 empties_mod = 1024;
139
140 argv = (char**)poptGetArgs(pc);
141 if (!argv || argv[1]) {
142 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
143 exit(1);
144 }
145
146 if ((fp = fopen(*argv, "r")) == NULL) {
147 fprintf(stderr, "Unable to open %s\n", *argv);
148 exit(1);
149 }
150
151 line = 0;
152 while (fgets(buf, sizeof buf, fp)) {
153 line++;
154 if (*buf == '#' || *buf == '\n')
155 continue;
156 for (s = buf, i = 0; i <= 1; i++) {
157 if (*s == '1')
158 flag[i] = 1;
159 else if (*s == '0')
160 flag[i] = 0;
161 else
162 flag[i] = -1;
163 if (!*s || (*++s != ' ' && *s != '\t'))
164 flag[i] = -1;
165 if (flag[i] < 0) {
166 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
167 line, *argv, buf);
168 exit(1);
169 }
170 if (*s)
171 s++;
172 while (*s == ' ' || *s == '\t')
173 s++;
174 }
175 for (i = 0; i <= 1; i++) {
176 if (*s == '\'' || *s == '"' || *s == '`') {
177 char quote = *s++;
178 string[i] = s;
179 while (*s && *s != quote) s++;
180 if (!*s) {
181 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
182 line, *argv, buf);
183 exit(1);
184 }
185 end[i] = s;
186 }
187 else {
188 if (!*s || *s == '\n') {
189 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
190 line, *argv, buf);
191 exit(1);
192 }
193 string[i] = s;
194 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
195 end[i] = s;
196 }
197 if (*s)
198 s++;
199 while (*s == ' ' || *s == '\t')
200 s++;
201 }
202 *end[0] = *end[1] = '\0';
203 run_test(line, flag[0],
204 #ifdef COMPARE_WITH_FNMATCH
205 flag[1],
206 #endif
207 string[0], string[1]);
208 }
209
210 fclose(fp);
211
212 if (!wildmatch_errors)
213 fputs("No", stdout);
214 else
215 printf("%d", wildmatch_errors);
216 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
217
218 #ifdef COMPARE_WITH_FNMATCH
219 if (!fnmatch_errors)
220 fputs("No", stdout);
221 else
222 printf("%d", fnmatch_errors);
223 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
224
225 #endif
226
227 return 0;
228 }