summaryrefslogtreecommitdiff
path: root/wildtest.c
blob: b64d671dfcf4fbf1dfd40adc2c654a1ed8f42f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
**  wildmatch test suite.
*/

/*#define COMPARE_WITH_FNMATCH*/

#define WILD_TEST_DEPTH
#include "lib/wildmatch.c"

#include "popt.h"

#ifdef COMPARE_WITH_FNMATCH
#include <fnmatch.h>
#endif

typedef char bool;

#define false 0
#define true 1

int output_depth = 0;

static struct poptOption long_options[] = {
  /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
  {"depth",          'd', POPT_ARG_NONE,   &output_depth, 0, 0, 0},
  {0,0,0,0, 0, 0, 0}
};

/* match just at the start of string (anchored tests) */
static void
ok(int n, const char *text, const char *pattern, bool matches, bool same_as_fnmatch)
{
    bool matched;
#ifdef COMPARE_WITH_FNMATCH
    bool fn_matched;
    int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
#else
    same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
#endif

    matched = wildmatch(pattern, text);
#ifdef COMPARE_WITH_FNMATCH
    fn_matched = !fnmatch(pattern, text, flags);
#endif
    if (matched != matches) {
	printf("wildmatch failure on #%d:\n  %s\n  %s\n  expected %d\n",
	       n, text, pattern, matches);
    }
#ifdef COMPARE_WITH_FNMATCH
    if (fn_matched != (matches ^ !same_as_fnmatch)) {
	printf("fnmatch disagreement on #%d:\n  %s\n  %s\n  expected %d\n",
	       n, text, pattern, matches ^ !same_as_fnmatch);
    }
#endif
    if (output_depth)
	printf("[%s] depth = %d\n", pattern, wildmatch_depth);
}

int
main(int argc, char **argv)
{
    int opt;
    poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
				    long_options, 0);

    while ((opt = poptGetNextOpt(pc)) != -1) {
	switch (opt) {
	  default:
	    fprintf(stderr, "Unknown option: `%c'\n", opt);
	    exit(1);
	}
    }

    /* Basic wildmat features. */
    /* TEST, "text",		"pattern",		MATCH?, SAME-AS-FNMATCH? */
    ok(100, "foo",		"foo",			true,	true);
    ok(101, "foo",		"bar",			false,	true);
    ok(102, "",			"",			true,	true);
    ok(103, "foo",		"???",			true,	true);
    ok(104, "foo",		"??",			false,	true);
    ok(105, "foo",		"*",			true,	true);
    ok(106, "foo",		"f*",			true,	true);
    ok(107, "foo",		"*f",			false,	true);
    ok(108, "foo",		"*foo*",		true,	true);
    ok(109, "foobar",		"*ob*a*r*",		true,	true);
    ok(110, "aaaaaaabababab",	"*ab",			true,	true);
    ok(111, "foo*",		"foo\\*",		true,	true);
    ok(112, "foobar",		"foo\\*bar",		false,	true);
    ok(113, "f\\oo",		"f\\\\oo",		true,	true);
    ok(114, "ball",		"*[al]?",		true,	true);
    ok(115, "ten",		"[ten]",		false,	true);
    ok(116, "ten",		"**[!te]",		true,	true);
    ok(117, "ten",		"**[!ten]",		false,	true);
    ok(118, "ten",		"t[a-g]n",		true,	true);
    ok(119, "ten",		"t[!a-g]n",		false,	true);
    ok(120, "ton",		"t[!a-g]n",		true,	true);
    ok(121, "]",		"]",			true,	true);
    ok(122, "a]b",		"a[]]b",		true,	true);
    ok(123, "a-b",		"a[]-]b",		true,	true);
    ok(124, "a]b",		"a[]-]b",		true,	true);
    ok(125, "aab",		"a[]-]b",		false,	true);
    ok(126, "aab",		"a[]a-]b",		true,	true);

    /* Extended slash-matching features */
    /* TEST, "text",		"pattern",		MATCH?, SAME-AS-FNMATCH? */
    ok(200, "foo/baz/bar",	"foo*bar",		false,	true);
    ok(201, "foo/baz/bar",	"foo**bar",		true,	true);
    ok(202, "foo/bar",		"foo?bar",		false,	true);
    ok(203, "foo/bar",		"foo[/]bar",		true,	false);
    ok(204, "foo",		"**/foo",		false,	true);
    ok(205, "/foo",		"**/foo",		true,	true);
    ok(206, "bar/baz/foo",	"**/foo",		true,	true);
    ok(207, "bar/baz/foo",	"*/foo",		false,	true);
    ok(208, "foo/bar/baz",	"**/bar*",		false,	false);
    ok(209, "foo/bar/baz",	"**/bar**",		true,	true);

    /* Various additional tests. */
    /* TEST, "text",		"pattern",		MATCH?, SAME-AS-FNMATCH? */
    ok(300, "acrt",		"a[c-c]st",		false,	true);
    ok(301, "]",		"[!]-]",		false,	true);
    ok(302, "a",		"[!]-]",		true,	true);
    ok(303, "",			"\\",			false,	true);
    ok(304, "\\",		"\\",			false,	true);
    ok(305, "foo",		"foo",			true,	true);
    ok(306, "@foo",		"@foo",			true,	true);
    ok(307, "foo",		"@foo",			false,	true);
    ok(308, "[ab]",		"\\[ab]",		true,	true);
    ok(309, "?a?b",		"\\??\\?b",		true,	true);
    ok(310, "abc",		"\\a\\b\\c",		true,	true);
    ok(311, "foo",		"",			false,	true);
    ok(312, "foo/bar/baz/to",	"**/t[o]",		true,	true);

    /* Additional tests, including some malformed wildmats. */
    /* TEST, "text",		"pattern",		MATCH?, SAME-AS-FNMATCH? */
    ok(500, "]",		"[\\-_]",		true,	true);
    ok(501, "[",		"[\\-_]",		false,	true);
    ok(502, ".",		"[\\\\-_]",		false,	true);
    ok(503, "^",		"[\\\\-_]",		true,	true);
    ok(504, "Z",		"[\\\\-_]",		false,	true);
    ok(505, "\\",		"[\\]]",		false,	true);
    ok(506, "ab",		"a[]b",			false,	true);
    ok(507, "a[]b",		"a[]b",			false,	true);
    ok(508, "ab[",		"ab[",			false,	true);
    ok(509, "ab",		"[!",			false,	true);
    ok(510, "ab",		"[-",			false,	true);
    ok(511, "-",		"[-]",			true,	true);
    ok(512, "-",		"[a-",			false,	true);
    ok(513, "-",		"[!a-",			false,	true);
    ok(514, "-",		"[--A]",		true,	true);
    ok(515, "5",		"[--A]",		true,	true);
    ok(516, "\303\206",		"[--A]",		false,	true);
    ok(517, " ",		"[ --]",		true,	true);
    ok(518, "$",		"[ --]",		true,	true);
    ok(519, "-",		"[ --]",		true,	true);
    ok(520, "0",		"[ --]",		false,	true);
    ok(521, "-",		"[---]",		true,	true);
    ok(522, "-",		"[------]",		true,	true);
    ok(523, "j",		"[a-e-n]",		false,	true);
    ok(524, "-",		"[a-e-n]",		true,	true);
    ok(525, "a",		"[!------]",		true,	true);
    ok(526, "[",		"[]-a]",		false,	true);
    ok(527, "^",		"[]-a]",		true,	true);
    ok(528, "^",		"[!]-a]",		false,	true);
    ok(529, "[",		"[!]-a]",		true,	true);
    ok(530, "^",		"[a^bc]",		true,	true);
    ok(531, "-b]",		"[a-]b]",		true,	true);
    ok(532, "\\]",		"[\\]]",		true,	true);
    ok(533, "\\",		"[\\]",			true,	true);
    ok(534, "\\",		"[!\\]",		false,	true);
    ok(535, "G",		"[A-\\]",		true,	true);
    ok(536, "aaabbb",		"b*a",			false,	true);
    ok(537, "aabcaa",		"*ba*",			false,	true);
    ok(538, ",",		"[,]",			true,	true);
    ok(539, ",",		"[\\,]",		true,	true);
    ok(540, "\\",		"[\\,]",		true,	true);
    ok(541, "-",		"[,-.]",		true,	true);
    ok(542, "+",		"[,-.]",		false,	true);
    ok(543, "-.]",		"[,-.]",		false,	true);

    /* Test recursive calls and the ABORT code. */
    ok(600, "-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", true, true);
    ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
    ok(601, "-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", false, true);
    ok(602, "/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", true, true);
    ok(603, "/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1", "/*/*/*/*/*/*/12/*/*/*/m/*/*/*", false, true);
    ok(604, "abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt", "**/*a*b*g*n*t", true, true);

    return 0;
}