Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / wildtest.c
1 /*
2 **  wildmatch test suite.
3 */
4
5 /*#define COMPARE_WITH_FNMATCH*/
6
7 #define WILD_TEST_ITERATIONS
8 #include "lib/wildmatch.c"
9
10 #include <popt.h>
11
12 #ifdef COMPARE_WITH_FNMATCH
13 #include <fnmatch.h>
14
15 int fnmatch_errors = 0;
16 #endif
17
18 int wildmatch_errors = 0;
19
20 typedef char bool;
21
22 int output_iterations = 0;
23 int explode_mod = 0;
24 int empties_mod = 0;
25 int empty_at_start = 0;
26 int empty_at_end = 0;
27
28 static struct poptOption long_options[] = {
29   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
30   {"iterations",     'i', POPT_ARG_NONE,   &output_iterations, 0, 0, 0},
31   {"empties",        'e', POPT_ARG_STRING, 0, 'e', 0, 0},
32   {"explode",        'x', POPT_ARG_INT,    &explode_mod, 0, 0, 0},
33   {0,0,0,0, 0, 0, 0}
34 };
35
36 /* match just at the start of string (anchored tests) */
37 static void
38 run_test(int line, bool matches, bool same_as_fnmatch,
39          const char *text, const char *pattern)
40 {
41     bool matched;
42 #ifdef COMPARE_WITH_FNMATCH
43     bool fn_matched;
44     int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
45 #else
46     same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
47 #endif
48
49     if (explode_mod) {
50         char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
51         int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
52
53         if (empty_at_start)
54             texts[ndx++] = "";
55         /* An empty string must turn into at least one empty array item. */
56         while (1) {
57             texts[ndx] = buf + ndx * (explode_mod + 1);
58             strlcpy(texts[ndx++], text + pos, explode_mod + 1);
59             if (pos + explode_mod >= len)
60                 break;
61             pos += explode_mod;
62             if (!(++cnt % empties_mod))
63                 texts[ndx++] = "";
64         }
65         if (empty_at_end)
66             texts[ndx++] = "";
67         texts[ndx] = NULL;
68         matched = wildmatch_array(pattern, (const char**)texts, 0);
69     } else
70         matched = wildmatch(pattern, text);
71 #ifdef COMPARE_WITH_FNMATCH
72     fn_matched = !fnmatch(pattern, text, flags);
73 #endif
74     if (matched != matches) {
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++;
78     }
79 #ifdef COMPARE_WITH_FNMATCH
80     if (fn_matched != (matches ^ !same_as_fnmatch)) {
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++;
84     }
85 #endif
86     if (output_iterations) {
87         printf("%d: \"%s\" iterations = %d\n", line, pattern,
88                wildmatch_iteration_count);
89     }
90 }
91
92 int
93 main(int argc, char **argv)
94 {
95     char buf[2048], *s, *string[2], *end[2];
96     const char *arg;
97     FILE *fp;
98     int opt, line, i, flag[2];
99     poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
100                                     long_options, 0);
101
102     while ((opt = poptGetNextOpt(pc)) != -1) {
103         switch (opt) {
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;
114           default:
115             fprintf(stderr, "%s: %s\n",
116                     poptBadOption(pc, POPT_BADOPTION_NOALIAS),
117                     poptStrerror(opt));
118             exit(1);
119         }
120     }
121
122     if (explode_mod && !empties_mod)
123         empties_mod = 1024;
124
125     argv = (char**)poptGetArgs(pc);
126     if (!argv || argv[1]) {
127         fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
128         exit(1);
129     }
130
131     if ((fp = fopen(*argv, "r")) == NULL) {
132         fprintf(stderr, "Unable to open %s\n", *argv);
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) {
151                 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
152                         line, *argv, buf);
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) {
163                     fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
164                             line, *argv, buf);
165                     exit(1);
166                 }
167                 end[i] = s;
168             }
169             else {
170                 if (!*s || *s == '\n') {
171                     fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
172                             line, *argv, buf);
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)
186         fputs("No", stdout);
187     else
188         printf("%d", wildmatch_errors);
189     printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
190
191 #ifdef COMPARE_WITH_FNMATCH
192     if (!fnmatch_errors)
193         fputs("No", stdout);
194     else
195         printf("%d", fnmatch_errors);
196     printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
197
198 #endif
199
200     return 0;
201 }