Add docs
[glib.git] / tests / patterntest.c
blob494de17009cca3a5ab5c1a3eb2022b927fa5e5be
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
21 #undef G_LOG_DOMAIN
23 #include <string.h>
25 #include "glib.h"
26 #include "glib/gpattern.h"
28 static gboolean noisy = FALSE;
30 static void
31 verbose (const gchar *format, ...)
33 gchar *msg;
34 va_list args;
36 va_start (args, format);
37 msg = g_strdup_vprintf (format, args);
38 va_end (args);
40 if (noisy)
41 g_print (msg);
42 g_free (msg);
45 /* keep enum and structure of gpattern.c and patterntest.c in sync */
46 typedef enum
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" */
53 G_MATCH_LAST
54 } GMatchType;
56 struct _GPatternSpec
58 GMatchType match_type;
59 guint pattern_length;
60 guint min_length;
61 guint max_length;
62 gchar *pattern;
66 static gchar *
67 match_type_name (GMatchType match_type)
69 switch (match_type)
71 case G_MATCH_ALL:
72 return "G_MATCH_ALL";
73 break;
74 case G_MATCH_ALL_TAIL:
75 return "G_MATCH_ALL_TAIL";
76 break;
77 case G_MATCH_HEAD:
78 return "G_MATCH_HEAD";
79 break;
80 case G_MATCH_TAIL:
81 return "G_MATCH_TAIL";
82 break;
83 case G_MATCH_EXACT:
84 return "G_MATCH_EXACT";
85 break;
86 default:
87 return "unknown GMatchType";
88 break;
92 static gboolean
93 test_compilation (gchar *src,
94 GMatchType match_type,
95 gchar *pattern,
96 guint min)
98 GPatternSpec *spec;
100 verbose ("compiling \"%s\" \t", src);
101 spec = g_pattern_spec_new (src);
103 if (spec->match_type != match_type)
105 g_print ("failed \t(match_type: %s, expected %s)\n",
106 match_type_name (spec->match_type),
107 match_type_name (match_type));
108 g_pattern_spec_free (spec);
109 return FALSE;
112 if (strcmp (spec->pattern, pattern) != 0)
114 g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
115 spec->pattern,
116 pattern);
117 g_pattern_spec_free (spec);
118 return FALSE;
121 if (spec->pattern_length != strlen (spec->pattern))
123 g_print ("failed \t(pattern_length: %d, expected %d)\n",
124 spec->pattern_length,
125 (gint)strlen (spec->pattern));
126 g_pattern_spec_free (spec);
127 return FALSE;
130 if (spec->min_length != min)
132 g_print ("failed \t(min_length: %d, expected %d)\n",
133 spec->min_length,
134 min);
135 g_pattern_spec_free (spec);
136 return FALSE;
139 verbose ("passed (%s: \"%s\")\n",
140 match_type_name (spec->match_type),
141 spec->pattern);
143 g_pattern_spec_free (spec);
145 return TRUE;
148 static gboolean
149 test_match (gchar *pattern,
150 gchar *string,
151 gboolean match)
153 verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
155 if (g_pattern_match_simple (pattern, string) != match)
157 g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
158 return FALSE;
161 verbose ("passed (%s)\n", match ? "match" : "nomatch");
163 return TRUE;
166 static gboolean
167 test_equal (gchar *pattern1,
168 gchar *pattern2,
169 gboolean expected)
171 GPatternSpec *p1 = g_pattern_spec_new (pattern1);
172 GPatternSpec *p2 = g_pattern_spec_new (pattern2);
173 gboolean equal = g_pattern_spec_equal (p1, p2);
175 verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
177 if (expected != equal)
179 g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
180 match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
181 expected ? "!=" : "==",
182 match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
184 else
185 verbose ("passed (%s)\n", equal ? "equal" : "unequal");
187 g_pattern_spec_free (p1);
188 g_pattern_spec_free (p2);
190 return expected == equal;
193 #define TEST_COMPILATION(src, type, pattern, min) { \
194 total++; \
195 if (test_compilation (src, type, pattern, min)) \
196 passed++; \
197 else \
198 failed++; \
201 #define TEST_MATCH(pattern, string, match) { \
202 total++; \
203 if (test_match (pattern, string, match)) \
204 passed++; \
205 else \
206 failed++; \
209 #define TEST_EQUAL(pattern1, pattern2, match) { \
210 total++; \
211 if (test_equal (pattern1, pattern2, match)) \
212 passed++; \
213 else \
214 failed++; \
218 main (int argc, char** argv)
220 gint total = 0;
221 gint passed = 0;
222 gint failed = 0;
223 gint i;
225 for (i = 1; i < argc; i++)
226 if (strcmp ("--noisy", argv[i]) == 0)
227 noisy = TRUE;
229 TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
230 TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
231 TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
232 TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
233 TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
234 TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
235 TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
236 TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
237 TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
238 TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
239 TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
240 TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
241 TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
243 TEST_EQUAL("*A?B*", "*A?B*", TRUE);
244 TEST_EQUAL("A*BCD", "A*BCD", TRUE);
245 TEST_EQUAL("ABCD*", "ABCD****", TRUE);
246 TEST_EQUAL("A1*", "A1*", TRUE);
247 TEST_EQUAL("*YZ", "*YZ", TRUE);
248 TEST_EQUAL("A1x", "A1x", TRUE);
249 TEST_EQUAL("AB*CD", "AB**CD", TRUE);
250 TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
251 TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
252 TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
253 TEST_EQUAL("ABC*", "ABC?", FALSE);
255 TEST_MATCH("*x", "x", TRUE);
256 TEST_MATCH("*x", "xx", TRUE);
257 TEST_MATCH("*x", "yyyx", TRUE);
258 TEST_MATCH("*x", "yyxy", FALSE);
259 TEST_MATCH("?x", "x", FALSE);
260 TEST_MATCH("?x", "xx", TRUE);
261 TEST_MATCH("?x", "yyyx", FALSE);
262 TEST_MATCH("?x", "yyxy", FALSE);
263 TEST_MATCH("*?x", "xx", TRUE);
264 TEST_MATCH("?*x", "xx", TRUE);
265 TEST_MATCH("*?x", "x", FALSE);
266 TEST_MATCH("?*x", "x", FALSE);
267 TEST_MATCH("*?*x", "yx", TRUE);
268 TEST_MATCH("*?*x", "xxxx", TRUE);
269 TEST_MATCH("x*??", "xyzw", TRUE);
270 TEST_MATCH("*x", "\xc3\x84x", TRUE);
271 TEST_MATCH("?x", "\xc3\x84x", TRUE);
272 TEST_MATCH("??x", "\xc3\x84x", FALSE);
273 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
274 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
275 TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
276 TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
277 TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
278 TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
279 TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
280 TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
281 TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
282 TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
283 TEST_MATCH("", "abc", FALSE);
285 TEST_MATCH("", "", TRUE);
286 TEST_MATCH("abc", "abc", TRUE);
287 TEST_MATCH("*fo1*bar", "yyyfoxfo1bar", TRUE);
288 TEST_MATCH("12*fo1g*bar", "12yyyfoxfo1gbar", TRUE);
289 TEST_MATCH("__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE);
290 TEST_MATCH("*abc*cde", "abcde", FALSE);
291 TEST_MATCH("*abc*cde", "abccde", TRUE);
292 TEST_MATCH("*abc*cde", "abcxcde", TRUE);
293 TEST_MATCH("*abc*?cde", "abccde", FALSE);
294 TEST_MATCH("*abc*?cde", "abcxcde", TRUE);
295 TEST_MATCH("*abc*def", "abababcdededef", TRUE);
296 TEST_MATCH("*abc*def", "abcbcbcdededef", TRUE);
297 TEST_MATCH("*acbc*def", "acbcbcbcdededef", TRUE);
298 TEST_MATCH("*a?bc*def", "acbcbcbcdededef", TRUE);
299 TEST_MATCH("*abc*def", "bcbcbcdefdef", FALSE);
300 TEST_MATCH("*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE);
301 TEST_MATCH("*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE);
302 TEST_MATCH("_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_*abc*def*ghi", "_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_abcbcbcbcbcbcdefefdefdefghi", TRUE);
303 TEST_MATCH("fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE);
305 verbose ("\n%u tests passed, %u failed\n", passed, failed);
307 return failed;