Switch to using an input file so that backslashes are specified
[rsync/rsync.git] / wildtest.c
CommitLineData
5de45bca
WD
1/*
2** wildmatch test suite.
3*/
4
6cd50960 5/*#define COMPARE_WITH_FNMATCH*/
5de45bca 6
d5c973cc 7#define WILD_TEST_ITERATIONS
076f60ee
WD
8#include "lib/wildmatch.c"
9
10#include "popt.h"
11
5de45bca
WD
12#ifdef COMPARE_WITH_FNMATCH
13#include <fnmatch.h>
97d53f8c
WD
14
15int fnmatch_errors = 0;
5de45bca
WD
16#endif
17
97d53f8c
WD
18int wildmatch_errors = 0;
19
5de45bca
WD
20typedef char bool;
21
22#define false 0
23#define true 1
24
d5c973cc 25int output_iterations = 0;
076f60ee
WD
26
27static struct poptOption long_options[] = {
28 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9a17dddb 29 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
076f60ee
WD
30 {0,0,0,0, 0, 0, 0}
31};
32
5de45bca
WD
33/* match just at the start of string (anchored tests) */
34static void
97d53f8c
WD
35run_test(int line, bool matches, bool same_as_fnmatch,
36 const char *text, const char *pattern)
5de45bca
WD
37{
38 bool matched;
39#ifdef COMPARE_WITH_FNMATCH
40 bool fn_matched;
41 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
88085892
WD
42#else
43 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
5de45bca
WD
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) {
97d53f8c
WD
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++;
5de45bca
WD
54 }
55#ifdef COMPARE_WITH_FNMATCH
56 if (fn_matched != (matches ^ !same_as_fnmatch)) {
97d53f8c
WD
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++;
5de45bca
WD
60 }
61#endif
d5c973cc
WD
62 if (output_iterations)
63 printf("[%s] iterations = %d\n", pattern, wildmatch_iteration_count);
5de45bca
WD
64}
65
66int
67main(int argc, char **argv)
68{
97d53f8c
WD
69 char buf[2048], *s, *string[2], *end[2];
70 FILE *fp;
71 int opt, line, i, flag[2];
076f60ee
WD
72 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
73 long_options, 0);
74
75 while ((opt = poptGetNextOpt(pc)) != -1) {
76 switch (opt) {
77 default:
9a17dddb
WD
78 fprintf(stderr, "%s: %s\n",
79 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
80 poptStrerror(opt));
076f60ee
WD
81 exit(1);
82 }
83 }
5de45bca 84
97d53f8c
WD
85 if ((fp = fopen("wildtest.txt", "r")) == NULL) {
86 fprintf(stderr, "Unable to open wildtest.txt.\n");
87 exit(1);
88 }
89
90 line = 0;
91 while (fgets(buf, sizeof buf, fp)) {
92 line++;
93 if (*buf == '#' || *buf == '\n')
94 continue;
95 for (s = buf, i = 0; i <= 1; i++) {
96 if (*s == '1')
97 flag[i] = 1;
98 else if (*s == '0')
99 flag[i] = 0;
100 else
101 flag[i] = -1;
102 if (*++s != ' ' && *s != '\t')
103 flag[i] = -1;
104 if (flag[i] < 0) {
105 fprintf(stderr, "Invalid flag syntax on line %d of wildtest.txt:%s\n",
106 line, buf);
107 exit(1);
108 }
109 while (*++s == ' ' || *s == '\t') {}
110 }
111 for (i = 0; i <= 1; i++) {
112 if (*s == '\'' || *s == '"' || *s == '`') {
113 char quote = *s++;
114 string[i] = s;
115 while (*s && *s != quote) s++;
116 if (!*s) {
117 fprintf(stderr, "Unmatched quote on line %d of wildtest.txt:%s\n",
118 line, buf);
119 exit(1);
120 }
121 end[i] = s;
122 }
123 else {
124 if (!*s || *s == '\n') {
125 fprintf(stderr, "Not enough strings on line %d of wildtest.txt:%s\n",
126 line, buf);
127 exit(1);
128 }
129 string[i] = s;
130 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
131 end[i] = s;
132 }
133 while (*++s == ' ' || *s == '\t') {}
134 }
135 *end[0] = *end[1] = '\0';
136 run_test(line, flag[0], flag[1], string[0], string[1]);
137 }
138
139 if (!wildmatch_errors)
140 printf("No wildmatch errors found.\n");
141 else
142 printf("Found %d wildmatch errors.\n", wildmatch_errors);
143
144#ifdef COMPARE_WITH_FNMATCH
145 if (!fnmatch_errors)
146 printf("No fnmatch errors found.\n");
147 else
148 printf("Found %d fnmatch errors.\n", fnmatch_errors);
149
150#endif
5de45bca
WD
151
152 return 0;
153}