blob: bea4cebbe6a33e2dd21d1a1e1728f4bc2eaa15a2 [file] [log] [blame]
Wayne Davison5de45bc2003-07-05 00:05:02 +00001/*
Wayne Davison0f78b812006-04-25 20:23:34 +00002 * Test suite for the wildmatch code.
3 *
Wayne Davison3e2e4b52019-03-16 09:15:49 -07004 * Copyright (C) 2003-2019 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00005 *
6 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Wayne Davisone7c67062006-04-25 23:51:12 +000016 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000017 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000018 */
Wayne Davison5de45bc2003-07-05 00:05:02 +000019
Wayne Davison6cd50962003-07-05 06:53:41 +000020/*#define COMPARE_WITH_FNMATCH*/
Wayne Davison5de45bc2003-07-05 00:05:02 +000021
Wayne Davisond5c973c2003-07-05 19:03:42 +000022#define WILD_TEST_ITERATIONS
Wayne Davison076f60e2003-07-05 18:49:38 +000023#include "lib/wildmatch.c"
24
Wayne Davisona3869e92006-01-26 22:32:59 +000025#include <popt.h>
Wayne Davison076f60e2003-07-05 18:49:38 +000026
Wayne Davison5de45bc2003-07-05 00:05:02 +000027#ifdef COMPARE_WITH_FNMATCH
28#include <fnmatch.h>
Wayne Davison97d53f82003-07-06 17:41:01 +000029
30int fnmatch_errors = 0;
Wayne Davison5de45bc2003-07-05 00:05:02 +000031#endif
32
Wayne Davison97d53f82003-07-06 17:41:01 +000033int wildmatch_errors = 0;
34
Wayne Davison5de45bc2003-07-05 00:05:02 +000035typedef char bool;
36
Wayne Davisond5c973c2003-07-05 19:03:42 +000037int output_iterations = 0;
Wayne Davisonaec75b02006-01-02 17:46:21 +000038int explode_mod = 0;
39int empties_mod = 0;
40int empty_at_start = 0;
41int empty_at_end = 0;
Wayne Davison076f60e2003-07-05 18:49:38 +000042
43static struct poptOption long_options[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
Wayne Davison9a17ddd2003-07-05 19:21:41 +000045 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
Wayne Davisonaec75b02006-01-02 17:46:21 +000046 {"empties", 'e', POPT_ARG_STRING, 0, 'e', 0, 0},
47 {"explode", 'x', POPT_ARG_INT, &explode_mod, 0, 0, 0},
Wayne Davison076f60e2003-07-05 18:49:38 +000048 {0,0,0,0, 0, 0, 0}
49};
50
Wayne Davison5de45bc2003-07-05 00:05:02 +000051/* match just at the start of string (anchored tests) */
52static void
Wayne Davisonfd91c3b2011-09-22 23:37:07 -070053run_test(int line, bool matches,
54#ifdef COMPARE_WITH_FNMATCH
55 bool same_as_fnmatch,
56#endif
Wayne Davison97d53f82003-07-06 17:41:01 +000057 const char *text, const char *pattern)
Wayne Davison5de45bc2003-07-05 00:05:02 +000058{
59 bool matched;
60#ifdef COMPARE_WITH_FNMATCH
61 bool fn_matched;
62 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
63#endif
64
Wayne Davisonaec75b02006-01-02 17:46:21 +000065 if (explode_mod) {
Wayne Davison75430202006-01-13 21:40:26 +000066 char buf[MAXPATHLEN*2], *texts[MAXPATHLEN];
Wayne Davisonaec75b02006-01-02 17:46:21 +000067 int pos = 0, cnt = 0, ndx = 0, len = strlen(text);
68
Wayne Davisonaec75b02006-01-02 17:46:21 +000069 if (empty_at_start)
Wayne Davison75430202006-01-13 21:40:26 +000070 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000071 /* An empty string must turn into at least one empty array item. */
72 while (1) {
Wayne Davison75430202006-01-13 21:40:26 +000073 texts[ndx] = buf + ndx * (explode_mod + 1);
74 strlcpy(texts[ndx++], text + pos, explode_mod + 1);
75 if (pos + explode_mod >= len)
Wayne Davisonaec75b02006-01-02 17:46:21 +000076 break;
Wayne Davisonaec75b02006-01-02 17:46:21 +000077 pos += explode_mod;
78 if (!(++cnt % empties_mod))
Wayne Davison75430202006-01-13 21:40:26 +000079 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000080 }
81 if (empty_at_end)
Wayne Davison75430202006-01-13 21:40:26 +000082 texts[ndx++] = "";
Wayne Davisonaec75b02006-01-02 17:46:21 +000083 texts[ndx] = NULL;
84 matched = wildmatch_array(pattern, (const char**)texts, 0);
Wayne Davisonaec75b02006-01-02 17:46:21 +000085 } else
86 matched = wildmatch(pattern, text);
Wayne Davison5de45bc2003-07-05 00:05:02 +000087#ifdef COMPARE_WITH_FNMATCH
88 fn_matched = !fnmatch(pattern, text, flags);
89#endif
90 if (matched != matches) {
Wayne Davison97d53f82003-07-06 17:41:01 +000091 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
92 line, text, pattern, matches? "a" : "NO");
93 wildmatch_errors++;
Wayne Davison5de45bc2003-07-05 00:05:02 +000094 }
95#ifdef COMPARE_WITH_FNMATCH
96 if (fn_matched != (matches ^ !same_as_fnmatch)) {
Wayne Davison97d53f82003-07-06 17:41:01 +000097 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
98 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
99 fnmatch_errors++;
Wayne Davison5de45bc2003-07-05 00:05:02 +0000100 }
101#endif
Wayne Davisonb6b42c82003-07-06 18:29:24 +0000102 if (output_iterations) {
103 printf("%d: \"%s\" iterations = %d\n", line, pattern,
104 wildmatch_iteration_count);
105 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000106}
107
108int
109main(int argc, char **argv)
110{
Wayne Davison97d53f82003-07-06 17:41:01 +0000111 char buf[2048], *s, *string[2], *end[2];
Wayne Davisonaec75b02006-01-02 17:46:21 +0000112 const char *arg;
Wayne Davison97d53f82003-07-06 17:41:01 +0000113 FILE *fp;
114 int opt, line, i, flag[2];
Wayne Davison076f60e2003-07-05 18:49:38 +0000115 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
116 long_options, 0);
117
118 while ((opt = poptGetNextOpt(pc)) != -1) {
119 switch (opt) {
Wayne Davisonaec75b02006-01-02 17:46:21 +0000120 case 'e':
121 arg = poptGetOptArg(pc);
122 empties_mod = atoi(arg);
123 if (strchr(arg, 's'))
124 empty_at_start = 1;
125 if (strchr(arg, 'e'))
126 empty_at_end = 1;
127 if (!explode_mod)
128 explode_mod = 1024;
129 break;
Wayne Davison076f60e2003-07-05 18:49:38 +0000130 default:
Wayne Davison9a17ddd2003-07-05 19:21:41 +0000131 fprintf(stderr, "%s: %s\n",
132 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
133 poptStrerror(opt));
Wayne Davison076f60e2003-07-05 18:49:38 +0000134 exit(1);
135 }
136 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000137
Wayne Davisonaec75b02006-01-02 17:46:21 +0000138 if (explode_mod && !empties_mod)
139 empties_mod = 1024;
140
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000141 argv = (char**)poptGetArgs(pc);
142 if (!argv || argv[1]) {
Wayne Davisonaec75b02006-01-02 17:46:21 +0000143 fprintf(stderr, "Usage: wildtest [OPTIONS] TESTFILE\n");
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000144 exit(1);
145 }
146
147 if ((fp = fopen(*argv, "r")) == NULL) {
148 fprintf(stderr, "Unable to open %s\n", *argv);
Wayne Davison97d53f82003-07-06 17:41:01 +0000149 exit(1);
150 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000151
Wayne Davison97d53f82003-07-06 17:41:01 +0000152 line = 0;
153 while (fgets(buf, sizeof buf, fp)) {
154 line++;
155 if (*buf == '#' || *buf == '\n')
156 continue;
157 for (s = buf, i = 0; i <= 1; i++) {
158 if (*s == '1')
159 flag[i] = 1;
160 else if (*s == '0')
161 flag[i] = 0;
162 else
163 flag[i] = -1;
164 if (*++s != ' ' && *s != '\t')
165 flag[i] = -1;
166 if (flag[i] < 0) {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000167 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
168 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000169 exit(1);
170 }
171 while (*++s == ' ' || *s == '\t') {}
172 }
173 for (i = 0; i <= 1; i++) {
174 if (*s == '\'' || *s == '"' || *s == '`') {
175 char quote = *s++;
176 string[i] = s;
177 while (*s && *s != quote) s++;
178 if (!*s) {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000179 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
180 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000181 exit(1);
182 }
183 end[i] = s;
184 }
185 else {
186 if (!*s || *s == '\n') {
Wayne Davisonc4cd2ca2004-01-08 08:59:52 +0000187 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
188 line, *argv, buf);
Wayne Davison97d53f82003-07-06 17:41:01 +0000189 exit(1);
190 }
191 string[i] = s;
192 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
193 end[i] = s;
194 }
195 while (*++s == ' ' || *s == '\t') {}
196 }
197 *end[0] = *end[1] = '\0';
Wayne Davisonfd91c3b2011-09-22 23:37:07 -0700198 run_test(line, flag[0],
199#ifdef COMPARE_WITH_FNMATCH
200 flag[1],
201#endif
202 string[0], string[1]);
Wayne Davison97d53f82003-07-06 17:41:01 +0000203 }
Wayne Davison5de45bc2003-07-05 00:05:02 +0000204
Wayne Davison97d53f82003-07-06 17:41:01 +0000205 if (!wildmatch_errors)
Wayne Davison16859cd2003-07-07 07:08:24 +0000206 fputs("No", stdout);
Wayne Davison97d53f82003-07-06 17:41:01 +0000207 else
Wayne Davison16859cd2003-07-07 07:08:24 +0000208 printf("%d", wildmatch_errors);
209 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
Wayne Davison5de45bc2003-07-05 00:05:02 +0000210
Wayne Davison97d53f82003-07-06 17:41:01 +0000211#ifdef COMPARE_WITH_FNMATCH
212 if (!fnmatch_errors)
Wayne Davison16859cd2003-07-07 07:08:24 +0000213 fputs("No", stdout);
Wayne Davison97d53f82003-07-06 17:41:01 +0000214 else
Wayne Davison16859cd2003-07-07 07:08:24 +0000215 printf("%d", fnmatch_errors);
216 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
Wayne Davisonc21eeef2003-07-06 04:34:19 +0000217
Wayne Davison97d53f82003-07-06 17:41:01 +0000218#endif
Wayne Davison5de45bc2003-07-05 00:05:02 +0000219
220 return 0;
221}