Added some portability code for the ctype functions.
[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>
14#endif
15
16typedef char bool;
17
18#define false 0
19#define true 1
20
d5c973cc 21int output_iterations = 0;
076f60ee
WD
22
23static struct poptOption long_options[] = {
24 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9a17dddb 25 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
076f60ee
WD
26 {0,0,0,0, 0, 0, 0}
27};
28
5de45bca
WD
29/* match just at the start of string (anchored tests) */
30static void
076f60ee 31ok(int n, const char *text, const char *pattern, bool matches, bool same_as_fnmatch)
5de45bca
WD
32{
33 bool matched;
34#ifdef COMPARE_WITH_FNMATCH
35 bool fn_matched;
36 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
88085892
WD
37#else
38 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
5de45bca
WD
39#endif
40
41 matched = wildmatch(pattern, text);
42#ifdef COMPARE_WITH_FNMATCH
43 fn_matched = !fnmatch(pattern, text, flags);
44#endif
45 if (matched != matches) {
c21eeef5
WD
46 printf("wildmatch failure on #%d:\n %s\n %s\n expected %s match\n",
47 n, text, pattern, matches? "a" : "NO");
5de45bca
WD
48 }
49#ifdef COMPARE_WITH_FNMATCH
50 if (fn_matched != (matches ^ !same_as_fnmatch)) {
c21eeef5
WD
51 printf("fnmatch disagreement on #%d:\n %s\n %s\n expected %s match\n",
52 n, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
5de45bca
WD
53 }
54#endif
d5c973cc
WD
55 if (output_iterations)
56 printf("[%s] iterations = %d\n", pattern, wildmatch_iteration_count);
5de45bca
WD
57}
58
59int
60main(int argc, char **argv)
61{
076f60ee
WD
62 int opt;
63 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
64 long_options, 0);
65
66 while ((opt = poptGetNextOpt(pc)) != -1) {
67 switch (opt) {
68 default:
9a17dddb
WD
69 fprintf(stderr, "%s: %s\n",
70 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
71 poptStrerror(opt));
076f60ee
WD
72 exit(1);
73 }
74 }
5de45bca
WD
75
76 /* Basic wildmat features. */
77 /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */
076f60ee
WD
78 ok(100, "foo", "foo", true, true);
79 ok(101, "foo", "bar", false, true);
80 ok(102, "", "", true, true);
81 ok(103, "foo", "???", true, true);
82 ok(104, "foo", "??", false, true);
83 ok(105, "foo", "*", true, true);
84 ok(106, "foo", "f*", true, true);
85 ok(107, "foo", "*f", false, true);
86 ok(108, "foo", "*foo*", true, true);
87 ok(109, "foobar", "*ob*a*r*", true, true);
88 ok(110, "aaaaaaabababab", "*ab", true, true);
89 ok(111, "foo*", "foo\\*", true, true);
90 ok(112, "foobar", "foo\\*bar", false, true);
91 ok(113, "f\\oo", "f\\\\oo", true, true);
92 ok(114, "ball", "*[al]?", true, true);
93 ok(115, "ten", "[ten]", false, true);
94 ok(116, "ten", "**[!te]", true, true);
95 ok(117, "ten", "**[!ten]", false, true);
96 ok(118, "ten", "t[a-g]n", true, true);
97 ok(119, "ten", "t[!a-g]n", false, true);
98 ok(120, "ton", "t[!a-g]n", true, true);
c21eeef5 99 ok(121, "ton", "t[^a-g]n", true, true);
076f60ee
WD
100 ok(122, "a]b", "a[]]b", true, true);
101 ok(123, "a-b", "a[]-]b", true, true);
102 ok(124, "a]b", "a[]-]b", true, true);
103 ok(125, "aab", "a[]-]b", false, true);
104 ok(126, "aab", "a[]a-]b", true, true);
c21eeef5 105 ok(127, "]", "]", true, true);
5de45bca
WD
106
107 /* Extended slash-matching features */
108 /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */
076f60ee
WD
109 ok(200, "foo/baz/bar", "foo*bar", false, true);
110 ok(201, "foo/baz/bar", "foo**bar", true, true);
111 ok(202, "foo/bar", "foo?bar", false, true);
112 ok(203, "foo/bar", "foo[/]bar", true, false);
113 ok(204, "foo", "**/foo", false, true);
114 ok(205, "/foo", "**/foo", true, true);
115 ok(206, "bar/baz/foo", "**/foo", true, true);
116 ok(207, "bar/baz/foo", "*/foo", false, true);
117 ok(208, "foo/bar/baz", "**/bar*", false, false);
118 ok(209, "foo/bar/baz", "**/bar**", true, true);
5de45bca
WD
119
120 /* Various additional tests. */
121 /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */
076f60ee
WD
122 ok(300, "acrt", "a[c-c]st", false, true);
123 ok(301, "]", "[!]-]", false, true);
124 ok(302, "a", "[!]-]", true, true);
125 ok(303, "", "\\", false, true);
126 ok(304, "\\", "\\", false, true);
127 ok(305, "foo", "foo", true, true);
128 ok(306, "@foo", "@foo", true, true);
129 ok(307, "foo", "@foo", false, true);
130 ok(308, "[ab]", "\\[ab]", true, true);
131 ok(309, "?a?b", "\\??\\?b", true, true);
132 ok(310, "abc", "\\a\\b\\c", true, true);
133 ok(311, "foo", "", false, true);
134 ok(312, "foo/bar/baz/to", "**/t[o]", true, true);
5de45bca 135
c21eeef5
WD
136 /* Character class tests. */
137 /* TEST, "txt","pattern", MATCH?, SAME-AS-FNMATCH? */
138 ok(400, "a1B", "[[:alpha:]][[:digit:]][[:upper:]]", true, true);
139 ok(401, "a", "[[:digit:][:upper:][:space:]]", false, true);
140 ok(402, "A", "[[:digit:][:upper:][:space:]]", true, true);
141 ok(403, "1", "[[:digit:][:upper:][:space:]]", true, true);
142 ok(404, " ", "[[:digit:][:upper:][:space:]]", true, true);
143 ok(405, ".", "[[:digit:][:upper:][:space:]]", false, true);
144 ok(406, "5", "[[:xdigit:]]", true, true);
145 ok(407, "f", "[[:xdigit:]]", true, true);
146 ok(408, "D", "[[:xdigit:]]", true, true);
147
5de45bca
WD
148 /* Additional tests, including some malformed wildmats. */
149 /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */
c21eeef5
WD
150 ok(500, "]", "[\\\\-^]", true, true);
151 ok(501, "[", "[\\\\-^]", false, true);
152 ok(502, "-", "[\\-_]", true, true);
153 ok(503, "]", "[\\]]", true, true);
154 ok(504, "\\]", "[\\]]", false, true);
076f60ee
WD
155 ok(505, "\\", "[\\]]", false, true);
156 ok(506, "ab", "a[]b", false, true);
157 ok(507, "a[]b", "a[]b", false, true);
158 ok(508, "ab[", "ab[", false, true);
159 ok(509, "ab", "[!", false, true);
160 ok(510, "ab", "[-", false, true);
161 ok(511, "-", "[-]", true, true);
162 ok(512, "-", "[a-", false, true);
163 ok(513, "-", "[!a-", false, true);
164 ok(514, "-", "[--A]", true, true);
165 ok(515, "5", "[--A]", true, true);
166 ok(516, "\303\206", "[--A]", false, true);
167 ok(517, " ", "[ --]", true, true);
168 ok(518, "$", "[ --]", true, true);
169 ok(519, "-", "[ --]", true, true);
170 ok(520, "0", "[ --]", false, true);
171 ok(521, "-", "[---]", true, true);
172 ok(522, "-", "[------]", true, true);
173 ok(523, "j", "[a-e-n]", false, true);
174 ok(524, "-", "[a-e-n]", true, true);
175 ok(525, "a", "[!------]", true, true);
176 ok(526, "[", "[]-a]", false, true);
177 ok(527, "^", "[]-a]", true, true);
178 ok(528, "^", "[!]-a]", false, true);
179 ok(529, "[", "[!]-a]", true, true);
180 ok(530, "^", "[a^bc]", true, true);
181 ok(531, "-b]", "[a-]b]", true, true);
c21eeef5
WD
182 ok(532, "\\", "[\\]", false, true);
183 ok(533, "\\", "[\\\\]", true, true);
184 ok(534, "\\", "[!\\\\]", false, true);
185 ok(535, "G", "[A-\\\\]", true, true);
076f60ee
WD
186 ok(536, "aaabbb", "b*a", false, true);
187 ok(537, "aabcaa", "*ba*", false, true);
188 ok(538, ",", "[,]", true, true);
c21eeef5
WD
189 ok(539, ",", "[\\\\,]", true, true);
190 ok(540, "\\", "[\\\\,]", true, true);
076f60ee
WD
191 ok(541, "-", "[,-.]", true, true);
192 ok(542, "+", "[,-.]", false, true);
193 ok(543, "-.]", "[,-.]", false, true);
194
195 /* Test recursive calls and the ABORT code. */
196 ok(600, "-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", true, true);
197 ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
198 ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
199 ok(602, "/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", true, true);
200 ok(603, "/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", false, true);
201 ok(604, "abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt", "**/*a*b*g*n*t", true, true);
5de45bca
WD
202
203 return 0;
204}