2 ** wildmatch test suite.
5 /*#define COMPARE_WITH_FNMATCH*/
7 #define WILD_TEST_ITERATIONS
8 #include "lib/wildmatch.c"
12 #ifdef COMPARE_WITH_FNMATCH
15 int fnmatch_errors
= 0;
18 int wildmatch_errors
= 0;
22 int output_iterations
= 0;
25 int empty_at_start
= 0;
28 static struct poptOption long_options
[] = {
29 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
30 {"iterations", 'i', POPT_ARG_NONE
, &output_iterations
, 0, 0, 0},
31 {"empties", 'e', POPT_ARG_STRING
, 0, 'e', 0, 0},
32 {"explode", 'x', POPT_ARG_INT
, &explode_mod
, 0, 0, 0},
36 /* match just at the start of string (anchored tests) */
38 run_test(int line
, bool matches
, bool same_as_fnmatch
,
39 const char *text
, const char *pattern
)
42 #ifdef COMPARE_WITH_FNMATCH
44 int flags
= strstr(pattern
, "**")? 0 : FNM_PATHNAME
;
46 same_as_fnmatch
= 0; /* Get rid of unused-variable compiler warning. */
50 char buf
[MAXPATHLEN
*2], *texts
[MAXPATHLEN
];
51 int pos
= 0, cnt
= 0, ndx
= 0, len
= strlen(text
);
55 /* An empty string must turn into at least one empty array item. */
57 texts
[ndx
] = buf
+ ndx
* (explode_mod
+ 1);
58 strlcpy(texts
[ndx
++], text
+ pos
, explode_mod
+ 1);
59 if (pos
+ explode_mod
>= len
)
62 if (!(++cnt
% empties_mod
))
68 matched
= wildmatch_array(pattern
, (const char**)texts
, 0);
70 matched
= wildmatch(pattern
, text
);
71 #ifdef COMPARE_WITH_FNMATCH
72 fn_matched
= !fnmatch(pattern
, text
, flags
);
74 if (matched
!= matches
) {
75 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
76 line
, text
, pattern
, matches
? "a" : "NO");
79 #ifdef COMPARE_WITH_FNMATCH
80 if (fn_matched
!= (matches
^ !same_as_fnmatch
)) {
81 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");
86 if (output_iterations
) {
87 printf("%d: \"%s\" iterations = %d\n", line
, pattern
,
88 wildmatch_iteration_count
);
93 main(int argc
, char **argv
)
95 char buf
[2048], *s
, *string
[2], *end
[2];
98 int opt
, line
, i
, flag
[2];
99 poptContext pc
= poptGetContext("wildtest", argc
, (const char**)argv
,
102 while ((opt
= poptGetNextOpt(pc
)) != -1) {
105 arg
= poptGetOptArg(pc
);
106 empties_mod
= atoi(arg
);
107 if (strchr(arg
, 's'))
109 if (strchr(arg
, 'e'))
115 fprintf(stderr
, "%s: %s\n",
116 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
122 if (explode_mod
&& !empties_mod
)
125 argv
= (char**)poptGetArgs(pc
);
126 if (!argv
|| argv
[1]) {
127 fprintf(stderr
, "Usage: wildtest [OPTIONS] TESTFILE\n");
131 if ((fp
= fopen(*argv
, "r")) == NULL
) {
132 fprintf(stderr
, "Unable to open %s\n", *argv
);
137 while (fgets(buf
, sizeof buf
, fp
)) {
139 if (*buf
== '#' || *buf
== '\n')
141 for (s
= buf
, i
= 0; i
<= 1; i
++) {
148 if (*++s
!= ' ' && *s
!= '\t')
151 fprintf(stderr
, "Invalid flag syntax on line %d of %s:\n%s",
155 while (*++s
== ' ' || *s
== '\t') {}
157 for (i
= 0; i
<= 1; i
++) {
158 if (*s
== '\'' || *s
== '"' || *s
== '`') {
161 while (*s
&& *s
!= quote
) s
++;
163 fprintf(stderr
, "Unmatched quote on line %d of %s:\n%s",
170 if (!*s
|| *s
== '\n') {
171 fprintf(stderr
, "Not enough strings on line %d of %s:\n%s",
176 while (*++s
&& *s
!= ' ' && *s
!= '\t' && *s
!= '\n') {}
179 while (*++s
== ' ' || *s
== '\t') {}
181 *end
[0] = *end
[1] = '\0';
182 run_test(line
, flag
[0], flag
[1], string
[0], string
[1]);
185 if (!wildmatch_errors
)
188 printf("%d", wildmatch_errors
);
189 printf(" wildmatch error%s found.\n", wildmatch_errors
== 1? "" : "s");
191 #ifdef COMPARE_WITH_FNMATCH
195 printf("%d", fnmatch_errors
);
196 printf(" fnmatch error%s found.\n", fnmatch_errors
== 1? "" : "s");