blob: 6189f1f662a307062b5687dce93b07396610f5ed [file] [log] [blame]
Wayne Davison5de45bc2003-07-05 00:05:02 +00001/*
2** wildmatch test suite.
3*/
4
Wayne Davison6cd50962003-07-05 06:53:41 +00005/*#define COMPARE_WITH_FNMATCH*/
Wayne Davison5de45bc2003-07-05 00:05:02 +00006
Wayne Davisond5c973c2003-07-05 19:03:42 +00007#define WILD_TEST_ITERATIONS
Wayne Davison076f60e2003-07-05 18:49:38 +00008#include "lib/wildmatch.c"
9
Wayne Davisona3869e92006-01-26 22:32:59 +000010#include <popt.h>
Wayne Davison076f60e2003-07-05 18:49:38 +000011
Wayne Davison5de45bc2003-07-05 00:05:02 +000012#ifdef COMPARE_WITH_FNMATCH
13#include <fnmatch.h>
Wayne Davison97d53f82003-07-06 17:41:01 +000014
15int fnmatch_errors = 0;
Wayne Davison5de45bc2003-07-05 00:05:02 +000016#endif
17
Wayne Davison97d53f82003-07-06 17:41:01 +000018int wildmatch_errors = 0;
19
Wayne Davison5de45bc2003-07-05 00:05:02 +000020typedef char bool;
21
Wayne Davisond5c973c2003-07-05 19:03:42 +000022int output_iterations = 0;
Wayne Davisonaec75b02006-01-02 17:46:21 +000023int explode_mod = 0;
24int empties_mod = 0;
25int empty_at_start = 0;
26int empty_at_end = 0;
Wayne Davison076f60e2003-07-05 18:49:38 +000027
28static struct poptOption long_options[] = {
29 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
Wayne Davison9a17ddd2003-07-05 19:21:41 +000030 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
Wayne Davisonaec75b02006-01-02 17:46:21 +000031 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
32 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
Wayne Davison076f60e2003-07-05 18:49:38 +000033 {0,0,0,0, 0, 0, 0}
34};
35
Wayne Davison5de45bc2003-07-05 00:05:02 +000036/* match just at the start of string (anchored tests) */
37static void
Wayne Davison97d53f82003-07-06 17:41:01 +000038run_test(int line, bool matches, bool same_as_fnmatch,
39 const char *text, const char *pattern)
Wayne Davison5de45bc2003-07-05 00:05:02 +000040{
41 bool matched;
42#ifdef COMPARE_WITH_FNMATCH
43 bool fn_matched;
44 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
Wayne Davison88085892003-07-05 06:56:16 +000045#else
46 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
Wayne Davison5de45bc2003-07-05 00:05:02 +000047#endif
48
Wayne Davisonaec75b02006-01-02 17:46:21 +000049 if (explode_mod) {
Wayne Davison75430202006-01-13 21:40:26 +000050 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
Wayne Davisonaec75b02006-01-02 17:46:21 +000051 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
52
Wayne Davisonaec75b02006-01-02 17:46:21 +000053 if (empty_at_start)
Wayne Davison75430202006-01-13 21:40:26 +000054 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000055 /* An empty string must turn into at least one empty array item. */
56 while (1) {
Wayne Davison75430202006-01-13 21:40:26 +000057 texts[ndx] = buf + ndx * (explode_mod + 1);
58 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
59 if (pos + explode_mod >= len)
Wayne Davisonaec75b02006-01-02 17:46:21 +000060 break;
Wayne Davisonaec75b02006-01-02 17:46:21 +000061 pos += explode_mod;
62 if (!(++cnt % empties_mod))
Wayne Davison75430202006-01-13 21:40:26 +000063 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000064 }
65 if (empty_at_end)
Wayne Davison75430202006-01-13 21:40:26 +000066 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000067 texts[ndx] = NULL;
68 matched = wildmatch_array(pattern, (const char**)texts, 0);
Wayne Davisonaec75b02006-01-02 17:46:21 +000069 } else
70 matched = wildmatch(pattern, text);
Wayne Davison5de45bc2003-07-05 00:05:02 +000071#ifdef COMPARE_WITH_FNMATCH
72 fn_matched = !fnmatch(pattern, text, flags);
73#endif
74 if (matched != matches) {
Wayne Davison97d53f82003-07-06 17:41:01 +000075 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++;
Wayne Davison5de45bc2003-07-05 00:05:02 +000078 }
79#ifdef COMPARE_WITH_FNMATCH
80 if (fn_matched != (matches ^ !same_as_fnmatch)) {
Wayne Davison97d53f82003-07-06 17:41:01 +000081 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++;
Wayne Davison5de45bc2003-07-05 00:05:02 +000084 }
85#endif
Wayne Davisonb6b42c82003-07-06 18:29:24 +000086 if (output_iterations) {
87 printf("%d: \"%s\" iterations = %d\n", line, pattern,
88 wildmatch_iteration_count);
89 }
Wayne Davison5de45bc2003-07-05 00:05:02 +000090}
91
92int
93main(int argc, char **argv)
94{
Wayne Davison97d53f82003-07-06 17:41:01 +000095 char buf[2048], *s, *string[2], *end[2];
Wayne Davisonaec75b02006-01-02 17:46:21 +000096 const char *arg;
Wayne Davison97d53f82003-07-06 17:41:01 +000097 FILE *fp;
98 int opt, line, i, flag[2];
Wayne Davison076f60e2003-07-05 18:49:38 +000099 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
100 long_options, 0);
101
102 while ((opt = poptGetNextOpt(pc)) != -1) {
103 switch (opt) {
Wayne Davisonaec75b02006-01-02 17:46:21 +0000104 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;
Wayne Davison076f60e2003-07-05 18:49:38 +0000114 default:
Wayne Davison9a17ddd2003-07-05 19:21:41 +0000115 fprintf(stderr, "%s: %s\n",
116 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
117 poptStrerror(opt));
Wayne Davison076f60e2003-07-05 18:49:38 +0000118 exit(1);
119 }
120 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000121
Wayne Davisonaec75b02006-01-02 17:46:21 +0000122 if (explode_mod && !empties_mod)
123 empties_mod = 1024;
124
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000125 argv = (char**)poptGetArgs(pc);
126 if (!argv || argv[1]) {
Wayne Davisonaec75b02006-01-02 17:46:21 +0000127 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000128 exit(1);
129 }
130
131 if ((fp = fopen(*argv, "r")) == NULL) {
132 fprintf(stderr, "Unable to open %s\n", *argv);
Wayne Davison97d53f82003-07-06 17:41:01 +0000133 exit(1);
134 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000135
Wayne Davison97d53f82003-07-06 17:41:01 +0000136 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) {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000151 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
152 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000153 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) {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000163 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
164 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000165 exit(1);
166 }
167 end[i] = s;
168 }
169 else {
170 if (!*s || *s == '\n') {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000171 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
172 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000173 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 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000184
Wayne Davison97d53f82003-07-06 17:41:01 +0000185 if (!wildmatch_errors)
Wayne Davison16859cd2003-07-07 07:08:24 +0000186 fputs("No", stdout);
Wayne Davison97d53f82003-07-06 17:41:01 +0000187 else
Wayne Davison16859cd2003-07-07 07:08:24 +0000188 printf("%d", wildmatch_errors);
189 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
Wayne Davison5de45bc2003-07-05 00:05:02 +0000190
Wayne Davison97d53f82003-07-06 17:41:01 +0000191#ifdef COMPARE_WITH_FNMATCH
192 if (!fnmatch_errors)
Wayne Davison16859cd2003-07-07 07:08:24 +0000193 fputs("No", stdout);
Wayne Davison97d53f82003-07-06 17:41:01 +0000194 else
Wayne Davison16859cd2003-07-07 07:08:24 +0000195 printf("%d", fnmatch_errors);
196 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
Wayne Davisonc21eeef2003-07-06 04:34:19 +0000197
Wayne Davison97d53f82003-07-06 17:41:01 +0000198#endif
Wayne Davison5de45bc2003-07-05 00:05:02 +0000199
200 return 0;
201}