Commit | Line | Data |
---|---|---|
5de45bca | 1 | /* |
0f78b815 WD |
2 | * Test suite for the wildmatch code. |
3 | * | |
ba2133d6 | 4 | * Copyright (C) 2003-2007 Wayne Davison |
0f78b815 WD |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
4fd842f9 | 7 | * it under the terms of the GNU General Public License version 3 as |
ba2133d6 | 8 | * published by the Free Software Foundation. |
0f78b815 WD |
9 | * |
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. | |
14 | * | |
e7c67065 | 15 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 16 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 17 | */ |
5de45bca | 18 | |
6cd50960 | 19 | /*#define COMPARE_WITH_FNMATCH*/ |
5de45bca | 20 | |
d5c973cc | 21 | #define WILD_TEST_ITERATIONS |
076f60ee WD |
22 | #include "lib/wildmatch.c" |
23 | ||
a3869e92 | 24 | #include <popt.h> |
076f60ee | 25 | |
5de45bca WD |
26 | #ifdef COMPARE_WITH_FNMATCH |
27 | #include <fnmatch.h> | |
97d53f8c WD |
28 | |
29 | int fnmatch_errors = 0; | |
5de45bca WD |
30 | #endif |
31 | ||
97d53f8c WD |
32 | int wildmatch_errors = 0; |
33 | ||
5de45bca WD |
34 | typedef char bool; |
35 | ||
d5c973cc | 36 | int output_iterations = 0; |
aec75b03 WD |
37 | int explode_mod = 0; |
38 | int empties_mod = 0; | |
39 | int empty_at_start = 0; | |
40 | int empty_at_end = 0; | |
076f60ee WD |
41 | |
42 | static struct poptOption long_options[] = { | |
43 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
9a17dddb | 44 | {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0}, |
aec75b03 WD |
45 | {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0}, |
46 | {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0}, | |
076f60ee WD |
47 | {0,0,0,0, 0, 0, 0} |
48 | }; | |
49 | ||
5de45bca WD |
50 | /* match just at the start of string (anchored tests) */ |
51 | static void | |
97d53f8c WD |
52 | run_test(int line, bool matches, bool same_as_fnmatch, |
53 | const char *text, const char *pattern) | |
5de45bca WD |
54 | { |
55 | bool matched; | |
56 | #ifdef COMPARE_WITH_FNMATCH | |
57 | bool fn_matched; | |
58 | int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME; | |
88085892 WD |
59 | #else |
60 | same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */ | |
5de45bca WD |
61 | #endif |
62 | ||
aec75b03 | 63 | if (explode_mod) { |
75430208 | 64 | char buf[MAXPATHLEN*2], *texts[MAXPATHLEN]; |
aec75b03 WD |
65 | int pos = 0, cnt = 0, ndx = 0, len = strlen(text); |
66 | ||
aec75b03 | 67 | if (empty_at_start) |
75430208 | 68 | texts[ndx++] = ""; |
aec75b03 WD |
69 | /* An empty string must turn into at least one empty array item. */ |
70 | while (1) { | |
75430208 WD |
71 | texts[ndx] = buf + ndx * (explode_mod + 1); |
72 | strlcpy(texts[ndx++], text + pos, explode_mod + 1); | |
73 | if (pos + explode_mod >= len) | |
aec75b03 | 74 | break; |
aec75b03 WD |
75 | pos += explode_mod; |
76 | if (!(++cnt % empties_mod)) | |
75430208 | 77 | texts[ndx++] = ""; |
aec75b03 WD |
78 | } |
79 | if (empty_at_end) | |
75430208 | 80 | texts[ndx++] = ""; |
aec75b03 WD |
81 | texts[ndx] = NULL; |
82 | matched = wildmatch_array(pattern, (const char**)texts, 0); | |
aec75b03 WD |
83 | } else |
84 | matched = wildmatch(pattern, text); | |
5de45bca WD |
85 | #ifdef COMPARE_WITH_FNMATCH |
86 | fn_matched = !fnmatch(pattern, text, flags); | |
87 | #endif | |
88 | if (matched != matches) { | |
97d53f8c WD |
89 | printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n", |
90 | line, text, pattern, matches? "a" : "NO"); | |
91 | wildmatch_errors++; | |
5de45bca WD |
92 | } |
93 | #ifdef COMPARE_WITH_FNMATCH | |
94 | if (fn_matched != (matches ^ !same_as_fnmatch)) { | |
97d53f8c WD |
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"); | |
97 | fnmatch_errors++; | |
5de45bca WD |
98 | } |
99 | #endif | |
b6b42c89 WD |
100 | if (output_iterations) { |
101 | printf("%d: \"%s\" iterations = %d\n", line, pattern, | |
102 | wildmatch_iteration_count); | |
103 | } | |
5de45bca WD |
104 | } |
105 | ||
106 | int | |
107 | main(int argc, char **argv) | |
108 | { | |
97d53f8c | 109 | char buf[2048], *s, *string[2], *end[2]; |
aec75b03 | 110 | const char *arg; |
97d53f8c WD |
111 | FILE *fp; |
112 | int opt, line, i, flag[2]; | |
076f60ee WD |
113 | poptContext pc = poptGetContext("wildtest", argc, (const char**)argv, |
114 | long_options, 0); | |
115 | ||
116 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
117 | switch (opt) { | |
aec75b03 WD |
118 | case 'e': |
119 | arg = poptGetOptArg(pc); | |
120 | empties_mod = atoi(arg); | |
121 | if (strchr(arg, 's')) | |
122 | empty_at_start = 1; | |
123 | if (strchr(arg, 'e')) | |
124 | empty_at_end = 1; | |
125 | if (!explode_mod) | |
126 | explode_mod = 1024; | |
127 | break; | |
076f60ee | 128 | default: |
9a17dddb WD |
129 | fprintf(stderr, "%s: %s\n", |
130 | poptBadOption(pc, POPT_BADOPTION_NOALIAS), | |
131 | poptStrerror(opt)); | |
076f60ee WD |
132 | exit(1); |
133 | } | |
134 | } | |
5de45bca | 135 | |
aec75b03 WD |
136 | if (explode_mod && !empties_mod) |
137 | empties_mod = 1024; | |
138 | ||
c4cd2ca6 WD |
139 | argv = (char**)poptGetArgs(pc); |
140 | if (!argv || argv[1]) { | |
aec75b03 | 141 | fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n"); |
c4cd2ca6 WD |
142 | exit(1); |
143 | } | |
144 | ||
145 | if ((fp = fopen(*argv, "r")) == NULL) { | |
146 | fprintf(stderr, "Unable to open %s\n", *argv); | |
97d53f8c WD |
147 | exit(1); |
148 | } | |
149 | ||
150 | line = 0; | |
151 | while (fgets(buf, sizeof buf, fp)) { | |
152 | line++; | |
153 | if (*buf == '#' || *buf == '\n') | |
154 | continue; | |
155 | for (s = buf, i = 0; i <= 1; i++) { | |
156 | if (*s == '1') | |
157 | flag[i] = 1; | |
158 | else if (*s == '0') | |
159 | flag[i] = 0; | |
160 | else | |
161 | flag[i] = -1; | |
162 | if (*++s != ' ' && *s != '\t') | |
163 | flag[i] = -1; | |
164 | if (flag[i] < 0) { | |
c4cd2ca6 WD |
165 | fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s", |
166 | line, *argv, buf); | |
97d53f8c WD |
167 | exit(1); |
168 | } | |
169 | while (*++s == ' ' || *s == '\t') {} | |
170 | } | |
171 | for (i = 0; i <= 1; i++) { | |
172 | if (*s == '\'' || *s == '"' || *s == '`') { | |
173 | char quote = *s++; | |
174 | string[i] = s; | |
175 | while (*s && *s != quote) s++; | |
176 | if (!*s) { | |
c4cd2ca6 WD |
177 | fprintf(stderr, "Unmatched quote on line %d of %s:\n%s", |
178 | line, *argv, buf); | |
97d53f8c WD |
179 | exit(1); |
180 | } | |
181 | end[i] = s; | |
182 | } | |
183 | else { | |
184 | if (!*s || *s == '\n') { | |
c4cd2ca6 WD |
185 | fprintf(stderr, "Not enough strings on line %d of %s:\n%s", |
186 | line, *argv, buf); | |
97d53f8c WD |
187 | exit(1); |
188 | } | |
189 | string[i] = s; | |
190 | while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {} | |
191 | end[i] = s; | |
192 | } | |
193 | while (*++s == ' ' || *s == '\t') {} | |
194 | } | |
195 | *end[0] = *end[1] = '\0'; | |
196 | run_test(line, flag[0], flag[1], string[0], string[1]); | |
197 | } | |
198 | ||
199 | if (!wildmatch_errors) | |
16859cdb | 200 | fputs("No", stdout); |
97d53f8c | 201 | else |
16859cdb WD |
202 | printf("%d", wildmatch_errors); |
203 | printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s"); | |
97d53f8c WD |
204 | |
205 | #ifdef COMPARE_WITH_FNMATCH | |
206 | if (!fnmatch_errors) | |
16859cdb | 207 | fputs("No", stdout); |
97d53f8c | 208 | else |
16859cdb WD |
209 | printf("%d", fnmatch_errors); |
210 | printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s"); | |
97d53f8c WD |
211 | |
212 | #endif | |
5de45bca WD |
213 | |
214 | return 0; | |
215 | } |