Switching to GPL 3.
[rsync/rsync.git] / wildtest.c
... / ...
CommitLineData
1/*
2 * Test suite for the wildmatch code.
3 *
4 * Copyright (C) 2003-2007 Wayne Davison
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3 as
8 * published by the Free Software Foundation.
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 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, visit the http://fsf.org website.
17 */
18
19/*#define COMPARE_WITH_FNMATCH*/
20
21#define WILD_TEST_ITERATIONS
22#include "lib/wildmatch.c"
23
24#include <popt.h>
25
26#ifdef COMPARE_WITH_FNMATCH
27#include <fnmatch.h>
28
29int fnmatch_errors = 0;
30#endif
31
32int wildmatch_errors = 0;
33
34typedef char bool;
35
36int output_iterations = 0;
37int explode_mod = 0;
38int empties_mod = 0;
39int empty_at_start = 0;
40int empty_at_end = 0;
41
42static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
45 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
46 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
47 {0,0,0,0, 0, 0, 0}
48};
49
50/* match just at the start of string (anchored tests) */
51static void
52run_test(int line, bool matches, bool same_as_fnmatch,
53 const char *text, const char *pattern)
54{
55 bool matched;
56#ifdef COMPARE_WITH_FNMATCH
57 bool fn_matched;
58 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
59#else
60 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
61#endif
62
63 if (explode_mod) {
64 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
65 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
66
67 if (empty_at_start)
68 texts[ndx++] = "";
69 /* An empty string must turn into at least one empty array item. */
70 while (1) {
71 texts[ndx] = buf + ndx * (explode_mod + 1);
72 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
73 if (pos + explode_mod >= len)
74 break;
75 pos += explode_mod;
76 if (!(++cnt % empties_mod))
77 texts[ndx++] = "";
78 }
79 if (empty_at_end)
80 texts[ndx++] = "";
81 texts[ndx] = NULL;
82 matched = wildmatch_array(pattern, (const char**)texts, 0);
83 } else
84 matched = wildmatch(pattern, text);
85#ifdef COMPARE_WITH_FNMATCH
86 fn_matched = !fnmatch(pattern, text, flags);
87#endif
88 if (matched != matches) {
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++;
92 }
93#ifdef COMPARE_WITH_FNMATCH
94 if (fn_matched != (matches ^ !same_as_fnmatch)) {
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++;
98 }
99#endif
100 if (output_iterations) {
101 printf("%d: \"%s\" iterations = %d\n", line, pattern,
102 wildmatch_iteration_count);
103 }
104}
105
106int
107main(int argc, char **argv)
108{
109 char buf[2048], *s, *string[2], *end[2];
110 const char *arg;
111 FILE *fp;
112 int opt, line, i, flag[2];
113 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
114 long_options, 0);
115
116 while ((opt = poptGetNextOpt(pc)) != -1) {
117 switch (opt) {
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;
128 default:
129 fprintf(stderr, "%s: %s\n",
130 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
131 poptStrerror(opt));
132 exit(1);
133 }
134 }
135
136 if (explode_mod && !empties_mod)
137 empties_mod = 1024;
138
139 argv = (char**)poptGetArgs(pc);
140 if (!argv || argv[1]) {
141 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
142 exit(1);
143 }
144
145 if ((fp = fopen(*argv, "r")) == NULL) {
146 fprintf(stderr, "Unable to open %s\n", *argv);
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) {
165 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
166 line, *argv, buf);
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) {
177 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
178 line, *argv, buf);
179 exit(1);
180 }
181 end[i] = s;
182 }
183 else {
184 if (!*s || *s == '\n') {
185 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
186 line, *argv, buf);
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)
200 fputs("No", stdout);
201 else
202 printf("%d", wildmatch_errors);
203 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
204
205#ifdef COMPARE_WITH_FNMATCH
206 if (!fnmatch_errors)
207 fputs("No", stdout);
208 else
209 printf("%d", fnmatch_errors);
210 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
211
212#endif
213
214 return 0;
215}