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