11f1b5d5a78ec40ff28bcf76449b756069a09a9f
[rsync/rsync.git] / wildtest.c
1 /*
2 **  wildmatch test suite.
3 */
4
5 /*#define COMPARE_WITH_FNMATCH*/
6
7 #define WILD_TEST_ITERATIONS
8 #include "lib/wildmatch.c"
9
10 #include "popt.h"
11
12 #ifdef COMPARE_WITH_FNMATCH
13 #include <fnmatch.h>
14 #endif
15
16 typedef char bool;
17
18 #define false 0
19 #define true 1
20
21 int output_iterations = 0;
22
23 static struct poptOption long_options[] = {
24   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
25   {"iterations",     'i', POPT_ARG_NONE,   &output_iterations, 0, 0, 0},
26   {0,0,0,0, 0, 0, 0}
27 };
28
29 /* match just at the start of string (anchored tests) */
30 static void
31 ok(int n, const char *text, const char *pattern, bool matches, bool same_as_fnmatch)
32 {
33     bool matched;
34 #ifdef COMPARE_WITH_FNMATCH
35     bool fn_matched;
36     int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
37 #else
38     same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
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) {
46         printf("wildmatch failure on #%d:\n  %s\n  %s\n  expected %s match\n",
47                n, text, pattern, matches? "a" : "NO");
48     }
49 #ifdef COMPARE_WITH_FNMATCH
50     if (fn_matched != (matches ^ !same_as_fnmatch)) {
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");
53     }
54 #endif
55     if (output_iterations)
56         printf("[%s] iterations = %d\n", pattern, wildmatch_iteration_count);
57 }
58
59 int
60 main(int argc, char **argv)
61 {
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:
69             fprintf(stderr, "%s: %s\n",
70                     poptBadOption(pc, POPT_BADOPTION_NOALIAS),
71                     poptStrerror(opt));
72             exit(1);
73         }
74     }
75
76     /* Basic wildmat features. */
77     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
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);
99     ok(121, "ton",              "t[^a-g]n",             true,   true);
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);
105     ok(127, "]",                "]",                    true,   true);
106
107     /* Extended slash-matching features */
108     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
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);
119
120     /* Various additional tests. */
121     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
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);
135
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
148     /* Additional tests, including some malformed wildmats. */
149     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
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);
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);
182     ok(532, "\\",               "[\\]",                 false,  true);
183     ok(533, "\\",               "[\\\\]",               true,   true);
184     ok(534, "\\",               "[!\\\\]",              false,  true);
185     ok(535, "G",                "[A-\\\\]",             true,   true);
186     ok(536, "aaabbb",           "b*a",                  false,  true);
187     ok(537, "aabcaa",           "*ba*",                 false,  true);
188     ok(538, ",",                "[,]",                  true,   true);
189     ok(539, ",",                "[\\\\,]",              true,   true);
190     ok(540, "\\",               "[\\\\,]",              true,   true);
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);
202
203     return 0;
204 }