| 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 %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_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, "]", "]", 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 | |
| 106 | /* Extended slash-matching features */ |
| 107 | /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */ |
| 108 | ok(200, "foo/baz/bar", "foo*bar", false, true); |
| 109 | ok(201, "foo/baz/bar", "foo**bar", true, true); |
| 110 | ok(202, "foo/bar", "foo?bar", false, true); |
| 111 | ok(203, "foo/bar", "foo[/]bar", true, false); |
| 112 | ok(204, "foo", "**/foo", false, true); |
| 113 | ok(205, "/foo", "**/foo", true, true); |
| 114 | ok(206, "bar/baz/foo", "**/foo", true, true); |
| 115 | ok(207, "bar/baz/foo", "*/foo", false, true); |
| 116 | ok(208, "foo/bar/baz", "**/bar*", false, false); |
| 117 | ok(209, "foo/bar/baz", "**/bar**", true, true); |
| 118 | |
| 119 | /* Various additional tests. */ |
| 120 | /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */ |
| 121 | ok(300, "acrt", "a[c-c]st", false, true); |
| 122 | ok(301, "]", "[!]-]", false, true); |
| 123 | ok(302, "a", "[!]-]", true, true); |
| 124 | ok(303, "", "\\", false, true); |
| 125 | ok(304, "\\", "\\", false, true); |
| 126 | ok(305, "foo", "foo", true, true); |
| 127 | ok(306, "@foo", "@foo", true, true); |
| 128 | ok(307, "foo", "@foo", false, true); |
| 129 | ok(308, "[ab]", "\\[ab]", true, true); |
| 130 | ok(309, "?a?b", "\\??\\?b", true, true); |
| 131 | ok(310, "abc", "\\a\\b\\c", true, true); |
| 132 | ok(311, "foo", "", false, true); |
| 133 | ok(312, "foo/bar/baz/to", "**/t[o]", true, true); |
| 134 | |
| 135 | /* Additional tests, including some malformed wildmats. */ |
| 136 | /* TEST, "text", "pattern", MATCH?, SAME-AS-FNMATCH? */ |
| 137 | ok(500, "]", "[\\-_]", true, true); |
| 138 | ok(501, "[", "[\\-_]", false, true); |
| 139 | ok(502, ".", "[\\\\-_]", false, true); |
| 140 | ok(503, "^", "[\\\\-_]", true, true); |
| 141 | ok(504, "Z", "[\\\\-_]", false, true); |
| 142 | ok(505, "\\", "[\\]]", false, true); |
| 143 | ok(506, "ab", "a[]b", false, true); |
| 144 | ok(507, "a[]b", "a[]b", false, true); |
| 145 | ok(508, "ab[", "ab[", false, true); |
| 146 | ok(509, "ab", "[!", false, true); |
| 147 | ok(510, "ab", "[-", false, true); |
| 148 | ok(511, "-", "[-]", true, true); |
| 149 | ok(512, "-", "[a-", false, true); |
| 150 | ok(513, "-", "[!a-", false, true); |
| 151 | ok(514, "-", "[--A]", true, true); |
| 152 | ok(515, "5", "[--A]", true, true); |
| 153 | ok(516, "\303\206", "[--A]", false, true); |
| 154 | ok(517, " ", "[ --]", true, true); |
| 155 | ok(518, "$", "[ --]", true, true); |
| 156 | ok(519, "-", "[ --]", true, true); |
| 157 | ok(520, "0", "[ --]", false, true); |
| 158 | ok(521, "-", "[---]", true, true); |
| 159 | ok(522, "-", "[------]", true, true); |
| 160 | ok(523, "j", "[a-e-n]", false, true); |
| 161 | ok(524, "-", "[a-e-n]", true, true); |
| 162 | ok(525, "a", "[!------]", true, true); |
| 163 | ok(526, "[", "[]-a]", false, true); |
| 164 | ok(527, "^", "[]-a]", true, true); |
| 165 | ok(528, "^", "[!]-a]", false, true); |
| 166 | ok(529, "[", "[!]-a]", true, true); |
| 167 | ok(530, "^", "[a^bc]", true, true); |
| 168 | ok(531, "-b]", "[a-]b]", true, true); |
| 169 | ok(532, "\\]", "[\\]]", true, true); |
| 170 | ok(533, "\\", "[\\]", true, true); |
| 171 | ok(534, "\\", "[!\\]", false, true); |
| 172 | ok(535, "G", "[A-\\]", true, true); |
| 173 | ok(536, "aaabbb", "b*a", false, true); |
| 174 | ok(537, "aabcaa", "*ba*", false, true); |
| 175 | ok(538, ",", "[,]", true, true); |
| 176 | ok(539, ",", "[\\,]", true, true); |
| 177 | ok(540, "\\", "[\\,]", true, true); |
| 178 | ok(541, "-", "[,-.]", true, true); |
| 179 | ok(542, "+", "[,-.]", false, true); |
| 180 | ok(543, "-.]", "[,-.]", false, true); |
| 181 | |
| 182 | /* Test recursive calls and the ABORT code. */ |
| 183 | ok(600, "-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", true, true); |
| 184 | ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true); |
| 185 | ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true); |
| 186 | ok(602, "/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", true, true); |
| 187 | ok(603, "/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", false, true); |
| 188 | ok(604, "abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt", "**/*a*b*g*n*t", true, true); |
| 189 | |
| 190 | return 0; |
| 191 | } |