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