1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #undef G_DISABLE_ASSERT
26 #include "glib/gpattern.h"
28 static gboolean noisy
= FALSE
;
31 verbose (const gchar
*format
, ...)
36 va_start (args
, format
);
37 msg
= g_strdup_vprintf (format
, args
);
45 /* keep enum and structure of gpattern.c and patterntest.c in sync */
48 G_MATCH_ALL
, /* "*A?A*" */
49 G_MATCH_ALL_TAIL
, /* "*A?AA" */
50 G_MATCH_HEAD
, /* "AAAA*" */
51 G_MATCH_TAIL
, /* "*AAAA" */
52 G_MATCH_EXACT
, /* "AAAAA" */
58 GMatchType match_type
;
66 match_type_name (GMatchType match_type
)
73 case G_MATCH_ALL_TAIL
:
74 return "G_MATCH_ALL_TAIL";
77 return "G_MATCH_HEAD";
80 return "G_MATCH_TAIL";
83 return "G_MATCH_EXACT";
86 return "unknown GMatchType";
92 test_compilation (gchar
*src
,
93 GMatchType match_type
,
99 verbose ("compiling \"%s\" \t", src
);
100 spec
= g_pattern_spec_new (src
);
102 if (spec
->match_type
!= match_type
)
104 g_print ("failed \t(match_type: %s, expected %s)\n",
105 match_type_name (spec
->match_type
),
106 match_type_name (match_type
));
110 if (strcmp (spec
->pattern
, pattern
) != 0)
112 g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
118 if (spec
->pattern_length
!= strlen (spec
->pattern
))
120 g_print ("failed \t(pattern_length: %d, expected %d)\n",
121 spec
->pattern_length
,
122 (gint
)strlen (spec
->pattern
));
126 if (spec
->min_length
!= min
)
128 g_print ("failed \t(min_length: %d, expected %d)\n",
134 verbose ("passed (%s: \"%s\")\n",
135 match_type_name (spec
->match_type
),
142 test_match (gchar
*pattern
,
146 verbose ("matching \"%s\" against \"%s\" \t", string
, pattern
);
148 if (g_pattern_match_simple (pattern
, string
) != match
)
150 g_print ("failed \t(unexpected %s)\n", (match
? "mismatch" : "match"));
154 verbose ("passed (%s)\n", match
? "match" : "nomatch");
160 test_equal (gchar
*pattern1
,
164 GPatternSpec
*p1
= g_pattern_spec_new (pattern1
);
165 GPatternSpec
*p2
= g_pattern_spec_new (pattern2
);
166 gboolean equal
= g_pattern_spec_equal (p1
, p2
);
168 verbose ("comparing \"%s\" with \"%s\" \t", pattern1
, pattern2
);
170 if (expected
!= equal
)
172 g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
173 match_type_name (p1
->match_type
), p1
->pattern_length
, p1
->pattern
,
174 expected
? "!=" : "==",
175 match_type_name (p2
->match_type
), p2
->pattern_length
, p2
->pattern
);
178 verbose ("passed (%s)\n", equal
? "equal" : "unequal");
180 g_pattern_spec_free (p1
);
181 g_pattern_spec_free (p2
);
183 return expected
== equal
;
186 #define TEST_COMPILATION(src, type, pattern, min) { \
188 if (test_compilation (src, type, pattern, min)) \
194 #define TEST_MATCH(pattern, string, match) { \
196 if (test_match (pattern, string, match)) \
202 #define TEST_EQUAL(pattern1, pattern2, match) { \
204 if (test_equal (pattern1, pattern2, match)) \
211 main (int argc
, char** argv
)
218 for (i
= 1; i
< argc
; i
++)
219 if (strcmp ("--noisy", argv
[i
]) == 0)
222 TEST_COMPILATION("*A?B*", G_MATCH_ALL
, "*A?B*", 3);
223 TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL
, "HGFED*CBA", 8);
224 TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL
, "ABCDEF*GH", 8);
225 TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL
, "ABC*???DEF*GH", 11);
226 TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL
, "AA?A*", 4);
227 TEST_COMPILATION("ABCD*", G_MATCH_HEAD
, "ABCD", 4);
228 TEST_COMPILATION("*ABCD", G_MATCH_TAIL
, "ABCD", 4);
229 TEST_COMPILATION("ABCDE", G_MATCH_EXACT
, "ABCDE", 5);
230 TEST_COMPILATION("A?C?E", G_MATCH_ALL
, "A?C?E", 5);
231 TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL
, "x?*", 2);
232 TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL
, "x?*", 2);
233 TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL
, "x?*", 2);
234 TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL
, "??*x", 3);
236 TEST_EQUAL("*A?B*", "*A?B*", TRUE
);
237 TEST_EQUAL("A*BCD", "A*BCD", TRUE
);
238 TEST_EQUAL("ABCD*", "ABCD****", TRUE
);
239 TEST_EQUAL("A1*", "A1*", TRUE
);
240 TEST_EQUAL("*YZ", "*YZ", TRUE
);
241 TEST_EQUAL("A1x", "A1x", TRUE
);
242 TEST_EQUAL("AB*CD", "AB**CD", TRUE
);
243 TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE
);
244 TEST_EQUAL("AB*?CD", "AB?*CD", TRUE
);
245 TEST_EQUAL("AB*CD", "AB*?*CD", FALSE
);
246 TEST_EQUAL("ABC*", "ABC?", FALSE
);
248 TEST_MATCH("*x", "x", TRUE
);
249 TEST_MATCH("*x", "xx", TRUE
);
250 TEST_MATCH("*x", "yyyx", TRUE
);
251 TEST_MATCH("*x", "yyxy", FALSE
);
252 TEST_MATCH("?x", "x", FALSE
);
253 TEST_MATCH("?x", "xx", TRUE
);
254 TEST_MATCH("?x", "yyyx", FALSE
);
255 TEST_MATCH("?x", "yyxy", FALSE
);
256 TEST_MATCH("*?x", "xx", TRUE
);
257 TEST_MATCH("?*x", "xx", TRUE
);
258 TEST_MATCH("*?x", "x", FALSE
);
259 TEST_MATCH("?*x", "x", FALSE
);
260 TEST_MATCH("*?*x", "yx", TRUE
);
261 TEST_MATCH("*?*x", "xxxx", TRUE
);
262 TEST_MATCH("x*??", "xyzw", TRUE
);
263 TEST_MATCH("*x", "\xc3\x84x", TRUE
);
264 TEST_MATCH("?x", "\xc3\x84x", TRUE
);
265 TEST_MATCH("??x", "\xc3\x84x", FALSE
);
266 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE
);
267 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE
);
268 TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE
);
269 TEST_MATCH("ab?\xc3\xb6", "abao", FALSE
);
270 TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE
);
271 TEST_MATCH("ab\xc3\xa4?", "abao", FALSE
);
272 TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE
);
273 TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE
);
274 TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE
);
275 TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE
);
276 TEST_MATCH("", "", TRUE
);
277 TEST_MATCH("", "abc", FALSE
);
279 verbose ("\n%u tests passed, %u failed\n", passed
, failed
);