Update contrib.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de>
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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
27 #include "glib/gpattern.h"
30 #include "mrt2_glib2_test.h"
34 static gboolean noisy = FALSE;
37 verbose (const gchar *format, ...)
42 va_start (args, format);
43 msg = g_strdup_vprintf (format, args);
51 /* keep enum and structure of gpattern.c and patterntest.c in sync */
54 G_MATCH_ALL, /* "*A?A*" */
55 G_MATCH_ALL_TAIL, /* "*A?AA" */
56 G_MATCH_HEAD, /* "AAAA*" */
57 G_MATCH_TAIL, /* "*AAAA" */
58 G_MATCH_EXACT, /* "AAAAA" */
64 GMatchType match_type;
73 match_type_name (GMatchType match_type)
80 case G_MATCH_ALL_TAIL:
81 return "G_MATCH_ALL_TAIL";
84 return "G_MATCH_HEAD";
87 return "G_MATCH_TAIL";
90 return "G_MATCH_EXACT";
93 return "unknown GMatchType";
99 test_compilation (gchar *src,
100 GMatchType match_type,
106 verbose ("compiling \"%s\" \t", src);
107 spec = g_pattern_spec_new (src);
109 if (spec->match_type != match_type)
111 g_print ("failed \t(match_type: %s, expected %s)\n",
112 match_type_name (spec->match_type),
113 match_type_name (match_type));
114 g_assert(FALSE && "patterntest");
118 if (strcmp (spec->pattern, pattern) != 0)
120 g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
123 g_assert(FALSE && "patterntest");
127 if (spec->pattern_length != strlen (spec->pattern))
129 g_print ("failed \t(pattern_length: %d, expected %d)\n",
130 spec->pattern_length,
131 (gint)strlen (spec->pattern));
132 g_assert(FALSE && "patterntest");
136 if (spec->min_length != min)
138 g_print ("failed \t(min_length: %d, expected %d)\n",
141 g_assert(FALSE && "patterntest");
145 verbose ("passed (%s: \"%s\")\n",
146 match_type_name (spec->match_type),
153 test_match (gchar *pattern,
157 verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
159 if (g_pattern_match_simple (pattern, string) != match)
161 g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
162 g_assert(FALSE && "patterntest");
166 verbose ("passed (%s)\n", match ? "match" : "nomatch");
172 test_equal (gchar *pattern1,
176 GPatternSpec *p1 = g_pattern_spec_new (pattern1);
177 GPatternSpec *p2 = g_pattern_spec_new (pattern2);
178 gboolean equal = g_pattern_spec_equal (p1, p2);
180 verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
182 if (expected != equal)
184 g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
185 match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
186 expected ? "!=" : "==",
187 match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
188 g_assert(FALSE && "patterntest");
191 verbose ("passed (%s)\n", equal ? "equal" : "unequal");
193 g_pattern_spec_free (p1);
194 g_pattern_spec_free (p2);
196 return expected == equal;
199 #define TEST_COMPILATION(src, type, pattern, min) { \
201 if (test_compilation (src, type, pattern, min)) \
207 #define TEST_MATCH(pattern, string, match) { \
209 if (test_match (pattern, string, match)) \
215 #define TEST_EQUAL(pattern1, pattern2, match) { \
217 if (test_equal (pattern1, pattern2, match)) \
224 main (int argc, char** argv)
232 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
233 g_set_print_handler(mrtPrintHandler);
237 for (i = 1; i < argc; i++)
238 if (strcmp ("--noisy", argv[i]) == 0)
241 TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
242 TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
243 TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
244 TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
245 TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
246 TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
247 TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
248 TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
249 TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
250 TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
251 TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
252 TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
253 TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
255 TEST_EQUAL("*A?B*", "*A?B*", TRUE);
256 TEST_EQUAL("A*BCD", "A*BCD", TRUE);
257 TEST_EQUAL("ABCD*", "ABCD****", TRUE);
258 TEST_EQUAL("A1*", "A1*", TRUE);
259 TEST_EQUAL("*YZ", "*YZ", TRUE);
260 TEST_EQUAL("A1x", "A1x", TRUE);
261 TEST_EQUAL("AB*CD", "AB**CD", TRUE);
262 TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
263 TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
264 TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
265 TEST_EQUAL("ABC*", "ABC?", FALSE);
267 TEST_MATCH("*x", "x", TRUE);
268 TEST_MATCH("*x", "xx", TRUE);
269 TEST_MATCH("*x", "yyyx", TRUE);
270 TEST_MATCH("*x", "yyxy", FALSE);
271 TEST_MATCH("?x", "x", FALSE);
272 TEST_MATCH("?x", "xx", TRUE);
273 TEST_MATCH("?x", "yyyx", FALSE);
274 TEST_MATCH("?x", "yyxy", FALSE);
275 TEST_MATCH("*?x", "xx", TRUE);
276 TEST_MATCH("?*x", "xx", TRUE);
277 TEST_MATCH("*?x", "x", FALSE);
278 TEST_MATCH("?*x", "x", FALSE);
279 TEST_MATCH("*?*x", "yx", TRUE);
280 TEST_MATCH("*?*x", "xxxx", TRUE);
281 TEST_MATCH("x*??", "xyzw", TRUE);
282 TEST_MATCH("*x", "\xc3\x84x", TRUE);
283 TEST_MATCH("?x", "\xc3\x84x", TRUE);
284 TEST_MATCH("??x", "\xc3\x84x", FALSE);
285 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
286 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
287 TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
288 TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
289 TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
290 TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
291 TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
292 TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
293 TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
294 TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
295 TEST_MATCH("", "", TRUE);
296 TEST_MATCH("", "abc", FALSE);
298 verbose ("\n%u tests passed, %u failed\n", passed, failed);
300 testResultXml("patterntest");
301 #endif /* EMULATOR */