Fixed two typos.
[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
24 static struct poptOption long_options[] = {
25   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
26   {"iterations",     'i', POPT_ARG_NONE,   &output_iterations, 0, 0, 0},
27   {0,0,0,0, 0, 0, 0}
28 };
29
30 /* match just at the start of string (anchored tests) */
31 static void
32 run_test(int line, bool matches, bool same_as_fnmatch,
33          const char *text, const char *pattern)
34 {
35     bool matched;
36 #ifdef COMPARE_WITH_FNMATCH
37     bool fn_matched;
38     int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
39 #else
40     same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
41 #endif
42
43     matched = wildmatch(pattern, text);
44 #ifdef COMPARE_WITH_FNMATCH
45     fn_matched = !fnmatch(pattern, text, flags);
46 #endif
47     if (matched != matches) {
48         printf("wildmatch failure on line %d:\n  %s\n  %s\n  expected %s match\n",
49                line, text, pattern, matches? "a" : "NO");
50         wildmatch_errors++;
51     }
52 #ifdef COMPARE_WITH_FNMATCH
53     if (fn_matched != (matches ^ !same_as_fnmatch)) {
54         printf("fnmatch disagreement on line %d:\n  %s\n  %s\n  expected %s match\n",
55                line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
56         fnmatch_errors++;
57     }
58 #endif
59     if (output_iterations) {
60         printf("%d: \"%s\" iterations = %d\n", line, pattern,
61                wildmatch_iteration_count);
62     }
63 }
64
65 int
66 main(int argc, char **argv)
67 {
68     char buf[2048], *s, *string[2], *end[2];
69     FILE *fp;
70     int opt, line, i, flag[2];
71     poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
72                                     long_options, 0);
73
74     while ((opt = poptGetNextOpt(pc)) != -1) {
75         switch (opt) {
76           default:
77             fprintf(stderr, "%s: %s\n",
78                     poptBadOption(pc, POPT_BADOPTION_NOALIAS),
79                     poptStrerror(opt));
80             exit(1);
81         }
82     }
83
84     argv = (char**)poptGetArgs(pc);
85     if (!argv || argv[1]) {
86         fprintf(stderr, "Usage: wildtest TESTFILE\n");
87         exit(1);
88     }
89
90     if ((fp = fopen(*argv, "r")) == NULL) {
91         fprintf(stderr, "Unable to open %s\n", *argv);
92         exit(1);
93     }
94
95     line = 0;
96     while (fgets(buf, sizeof buf, fp)) {
97         line++;
98         if (*buf == '#' || *buf == '\n')
99             continue;
100         for (s = buf, i = 0; i <= 1; i++) {
101             if (*s == '1')
102                 flag[i] = 1;
103             else if (*s == '0')
104                 flag[i] = 0;
105             else
106                 flag[i] = -1;
107             if (*++s != ' ' && *s != '\t')
108                 flag[i] = -1;
109             if (flag[i] < 0) {
110                 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
111                         line, *argv, buf);
112                 exit(1);
113             }
114             while (*++s == ' ' || *s == '\t') {}
115         }
116         for (i = 0; i <= 1; i++) {
117             if (*s == '\'' || *s == '"' || *s == '`') {
118                 char quote = *s++;
119                 string[i] = s;
120                 while (*s && *s != quote) s++;
121                 if (!*s) {
122                     fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
123                             line, *argv, buf);
124                     exit(1);
125                 }
126                 end[i] = s;
127             }
128             else {
129                 if (!*s || *s == '\n') {
130                     fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
131                             line, *argv, buf);
132                     exit(1);
133                 }
134                 string[i] = s;
135                 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
136                 end[i] = s;
137             }
138             while (*++s == ' ' || *s == '\t') {}
139         }
140         *end[0] = *end[1] = '\0';
141         run_test(line, flag[0], flag[1], string[0], string[1]);
142     }
143
144     if (!wildmatch_errors)
145         fputs("No", stdout);
146     else
147         printf("%d", wildmatch_errors);
148     printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
149
150 #ifdef COMPARE_WITH_FNMATCH
151     if (!fnmatch_errors)
152         fputs("No", stdout);
153     else
154         printf("%d", fnmatch_errors);
155     printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
156
157 #endif
158
159     return 0;
160 }