Added a --depth option to report recursion depth values.
[rsync/rsync.git] / wildtest.c
1 /*
2 **  wildmatch test suite.
3 */
4
5 /*#define COMPARE_WITH_FNMATCH*/
6
7 #define WILD_TEST_DEPTH
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_depth = 0;
22
23 static struct poptOption long_options[] = {
24   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
25   {"depth",          'd', POPT_ARG_NONE,   &output_depth, 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 %d\n",
47                n, text, pattern, matches);
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 %d\n",
52                n, text, pattern, matches ^ !same_as_fnmatch);
53     }
54 #endif
55     if (output_depth)
56         printf("[%s] depth = %d\n", pattern, wildmatch_depth);
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, "Unknown option: `%c'\n", opt);
70             exit(1);
71         }
72     }
73
74     /* Basic wildmat features. */
75     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
76     ok(100, "foo",              "foo",                  true,   true);
77     ok(101, "foo",              "bar",                  false,  true);
78     ok(102, "",                 "",                     true,   true);
79     ok(103, "foo",              "???",                  true,   true);
80     ok(104, "foo",              "??",                   false,  true);
81     ok(105, "foo",              "*",                    true,   true);
82     ok(106, "foo",              "f*",                   true,   true);
83     ok(107, "foo",              "*f",                   false,  true);
84     ok(108, "foo",              "*foo*",                true,   true);
85     ok(109, "foobar",           "*ob*a*r*",             true,   true);
86     ok(110, "aaaaaaabababab",   "*ab",                  true,   true);
87     ok(111, "foo*",             "foo\\*",               true,   true);
88     ok(112, "foobar",           "foo\\*bar",            false,  true);
89     ok(113, "f\\oo",            "f\\\\oo",              true,   true);
90     ok(114, "ball",             "*[al]?",               true,   true);
91     ok(115, "ten",              "[ten]",                false,  true);
92     ok(116, "ten",              "**[!te]",              true,   true);
93     ok(117, "ten",              "**[!ten]",             false,  true);
94     ok(118, "ten",              "t[a-g]n",              true,   true);
95     ok(119, "ten",              "t[!a-g]n",             false,  true);
96     ok(120, "ton",              "t[!a-g]n",             true,   true);
97     ok(121, "]",                "]",                    true,   true);
98     ok(122, "a]b",              "a[]]b",                true,   true);
99     ok(123, "a-b",              "a[]-]b",               true,   true);
100     ok(124, "a]b",              "a[]-]b",               true,   true);
101     ok(125, "aab",              "a[]-]b",               false,  true);
102     ok(126, "aab",              "a[]a-]b",              true,   true);
103
104     /* Extended slash-matching features */
105     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
106     ok(200, "foo/baz/bar",      "foo*bar",              false,  true);
107     ok(201, "foo/baz/bar",      "foo**bar",             true,   true);
108     ok(202, "foo/bar",          "foo?bar",              false,  true);
109     ok(203, "foo/bar",          "foo[/]bar",            true,   false);
110     ok(204, "foo",              "**/foo",               false,  true);
111     ok(205, "/foo",             "**/foo",               true,   true);
112     ok(206, "bar/baz/foo",      "**/foo",               true,   true);
113     ok(207, "bar/baz/foo",      "*/foo",                false,  true);
114     ok(208, "foo/bar/baz",      "**/bar*",              false,  false);
115     ok(209, "foo/bar/baz",      "**/bar**",             true,   true);
116
117     /* Various additional tests. */
118     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
119     ok(300, "acrt",             "a[c-c]st",             false,  true);
120     ok(301, "]",                "[!]-]",                false,  true);
121     ok(302, "a",                "[!]-]",                true,   true);
122     ok(303, "",                 "\\",                   false,  true);
123     ok(304, "\\",               "\\",                   false,  true);
124     ok(305, "foo",              "foo",                  true,   true);
125     ok(306, "@foo",             "@foo",                 true,   true);
126     ok(307, "foo",              "@foo",                 false,  true);
127     ok(308, "[ab]",             "\\[ab]",               true,   true);
128     ok(309, "?a?b",             "\\??\\?b",             true,   true);
129     ok(310, "abc",              "\\a\\b\\c",            true,   true);
130     ok(311, "foo",              "",                     false,  true);
131     ok(312, "foo/bar/baz/to",   "**/t[o]",              true,   true);
132
133     /* Additional tests, including some malformed wildmats. */
134     /* TEST, "text",            "pattern",              MATCH?, SAME-AS-FNMATCH? */
135     ok(500, "]",                "[\\-_]",               true,   true);
136     ok(501, "[",                "[\\-_]",               false,  true);
137     ok(502, ".",                "[\\\\-_]",             false,  true);
138     ok(503, "^",                "[\\\\-_]",             true,   true);
139     ok(504, "Z",                "[\\\\-_]",             false,  true);
140     ok(505, "\\",               "[\\]]",                false,  true);
141     ok(506, "ab",               "a[]b",                 false,  true);
142     ok(507, "a[]b",             "a[]b",                 false,  true);
143     ok(508, "ab[",              "ab[",                  false,  true);
144     ok(509, "ab",               "[!",                   false,  true);
145     ok(510, "ab",               "[-",                   false,  true);
146     ok(511, "-",                "[-]",                  true,   true);
147     ok(512, "-",                "[a-",                  false,  true);
148     ok(513, "-",                "[!a-",                 false,  true);
149     ok(514, "-",                "[--A]",                true,   true);
150     ok(515, "5",                "[--A]",                true,   true);
151     ok(516, "\303\206",         "[--A]",                false,  true);
152     ok(517, " ",                "[ --]",                true,   true);
153     ok(518, "$",                "[ --]",                true,   true);
154     ok(519, "-",                "[ --]",                true,   true);
155     ok(520, "0",                "[ --]",                false,  true);
156     ok(521, "-",                "[---]",                true,   true);
157     ok(522, "-",                "[------]",             true,   true);
158     ok(523, "j",                "[a-e-n]",              false,  true);
159     ok(524, "-",                "[a-e-n]",              true,   true);
160     ok(525, "a",                "[!------]",            true,   true);
161     ok(526, "[",                "[]-a]",                false,  true);
162     ok(527, "^",                "[]-a]",                true,   true);
163     ok(528, "^",                "[!]-a]",               false,  true);
164     ok(529, "[",                "[!]-a]",               true,   true);
165     ok(530, "^",                "[a^bc]",               true,   true);
166     ok(531, "-b]",              "[a-]b]",               true,   true);
167     ok(532, "\\]",              "[\\]]",                true,   true);
168     ok(533, "\\",               "[\\]",                 true,   true);
169     ok(534, "\\",               "[!\\]",                false,  true);
170     ok(535, "G",                "[A-\\]",               true,   true);
171     ok(536, "aaabbb",           "b*a",                  false,  true);
172     ok(537, "aabcaa",           "*ba*",                 false,  true);
173     ok(538, ",",                "[,]",                  true,   true);
174     ok(539, ",",                "[\\,]",                true,   true);
175     ok(540, "\\",               "[\\,]",                true,   true);
176     ok(541, "-",                "[,-.]",                true,   true);
177     ok(542, "+",                "[,-.]",                false,  true);
178     ok(543, "-.]",              "[,-.]",                false,  true);
179
180     /* Test recursive calls and the ABORT code. */
181     ok(600, "-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", true, true);
182     ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
183     ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
184     ok(602, "/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", true, true);
185     ok(603, "/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", false, true);
186     ok(604, "abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt", "**/*a*b*g*n*t", true, true);
187
188     return 0;
189 }