Version 11, interface, binary age 0.
[glib.git] / tests / patterntest.c
blob81c49b905be8280c91acb7cbc24a7ca7e0f266a3
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.
19 #include <string.h>
21 #include "glib.h"
22 #include "glib/gpattern.h"
25 /* keep enum and structure of gpattern.c and patterntest.c in sync */
26 typedef enum
28 G_MATCH_ALL, /* "*A?A*" */
29 G_MATCH_ALL_TAIL, /* "*A?AA" */
30 G_MATCH_HEAD, /* "AAAA*" */
31 G_MATCH_TAIL, /* "*AAAA" */
32 G_MATCH_EXACT, /* "AAAAA" */
33 G_MATCH_LAST
34 } GMatchType;
36 struct _GPatternSpec
38 GMatchType match_type;
39 guint pattern_length;
40 guint min_length;
41 gchar *pattern;
45 static gchar *
46 match_type_name (GMatchType match_type)
48 switch (match_type)
50 case G_MATCH_ALL:
51 return "G_MATCH_ALL";
52 break;
53 case G_MATCH_ALL_TAIL:
54 return "G_MATCH_ALL_TAIL";
55 break;
56 case G_MATCH_HEAD:
57 return "G_MATCH_HEAD";
58 break;
59 case G_MATCH_TAIL:
60 return "G_MATCH_TAIL";
61 break;
62 case G_MATCH_EXACT:
63 return "G_MATCH_EXACT";
64 break;
65 default:
66 return "unknown GMatchType";
67 break;
71 /* this leakes memory, but we don't care */
73 #define utf8(str) g_convert (str, -1, "Latin1", "UTF-8", NULL, NULL, NULL)
74 #define latin1(str) g_convert (str, -1, "UTF-8", "Latin1", NULL, NULL, NULL)
76 static gboolean
77 test_compilation (gchar *src,
78 GMatchType match_type,
79 gchar *pattern,
80 guint min)
82 GPatternSpec *spec;
84 g_print ("compiling \"%s\" \t", utf8(src));
85 spec = g_pattern_spec_new (src);
87 if (spec->match_type != match_type)
89 g_print ("failed \t(match_type: %s, expected %s)\n",
90 match_type_name (spec->match_type),
91 match_type_name (match_type));
92 return FALSE;
95 if (strcmp (spec->pattern, pattern) != 0)
97 g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
98 utf8(spec->pattern),
99 utf8(pattern));
100 return FALSE;
103 if (spec->pattern_length != strlen (spec->pattern))
105 g_print ("failed \t(pattern_length: %d, expected %d)\n",
106 spec->pattern_length,
107 strlen (spec->pattern));
108 return FALSE;
111 if (spec->min_length != min)
113 g_print ("failed \t(min_length: %d, expected %d)\n",
114 spec->min_length,
115 min);
116 return FALSE;
119 g_print ("passed (%s: \"%s\")\n",
120 match_type_name (spec->match_type),
121 spec->pattern);
123 return TRUE;
126 static gboolean
127 test_match (gchar *pattern,
128 gchar *string,
129 gboolean match)
131 g_print ("matching \"%s\" against \"%s\" \t", utf8(string), utf8(pattern));
133 if (g_pattern_match_simple (pattern, string) != match)
135 g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
136 return FALSE;
139 g_print ("passed (%s)\n", match ? "match" : "nomatch");
140 return TRUE;
143 static gboolean
144 test_equal (gchar *pattern1,
145 gchar *pattern2,
146 gboolean expected)
148 GPatternSpec *p1 = g_pattern_spec_new (pattern1);
149 GPatternSpec *p2 = g_pattern_spec_new (pattern2);
150 gboolean equal = g_pattern_spec_equal (p1, p2);
152 g_print ("comparing \"%s\" with \"%s\" \t", utf8(pattern1), utf8(pattern2));
154 if (expected != equal)
156 g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
157 match_type_name (p1->match_type), p1->pattern_length, utf8(p1->pattern),
158 expected ? "!=" : "==",
159 match_type_name (p2->match_type), p2->pattern_length, utf8(p2->pattern));
161 else
162 g_print ("passed (%s)\n", equal ? "equal" : "unequal");
164 g_pattern_spec_free (p1);
165 g_pattern_spec_free (p2);
167 return expected == equal;
170 #define TEST_COMPILATION(src, type, pattern, min) { \
171 total++; \
172 if (test_compilation (latin1(src), type, latin1(pattern), min)) \
173 passed++; \
174 else \
175 failed++; \
178 #define TEST_MATCH(pattern, string, match) { \
179 total++; \
180 if (test_match (latin1(pattern), latin1(string), match)) \
181 passed++; \
182 else \
183 failed++; \
186 #define TEST_EQUAL(pattern1, pattern2, match) { \
187 total++; \
188 if (test_equal (latin1(pattern1), latin1(pattern2), match)) \
189 passed++; \
190 else \
191 failed++; \
195 main (int argc, char** argv)
197 gint total = 0;
198 gint passed = 0;
199 gint failed = 0;
201 TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
202 TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
203 TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
204 TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
205 TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
206 TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
207 TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
208 TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
209 TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
210 TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
211 TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
212 TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
213 TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
215 TEST_EQUAL ("*A?B*", "*A?B*", TRUE);
216 TEST_EQUAL ("A*BCD", "A*BCD", TRUE);
217 TEST_EQUAL ("ABCD*", "ABCD****", TRUE);
218 TEST_EQUAL ("A1*", "A1*", TRUE);
219 TEST_EQUAL ("*YZ", "*YZ", TRUE);
220 TEST_EQUAL ("A1x", "A1x", TRUE);
221 TEST_EQUAL ("AB*CD", "AB**CD", TRUE);
222 TEST_EQUAL ("AB*?*CD", "AB*?CD", TRUE);
223 TEST_EQUAL ("AB*?CD", "AB?*CD", TRUE);
224 TEST_EQUAL ("AB*CD", "AB*?*CD", FALSE);
225 TEST_EQUAL ("ABC*", "ABC?", FALSE);
227 TEST_MATCH("*x", "x", TRUE);
228 TEST_MATCH("*x", "xx", TRUE);
229 TEST_MATCH("*x", "yyyx", TRUE);
230 TEST_MATCH("*x", "yyxy", FALSE);
231 TEST_MATCH("?x", "x", FALSE);
232 TEST_MATCH("?x", "xx", TRUE);
233 TEST_MATCH("?x", "yyyx", FALSE);
234 TEST_MATCH("?x", "yyxy", FALSE);
235 TEST_MATCH("*?x", "xx", TRUE);
236 TEST_MATCH("?*x", "xx", TRUE);
237 TEST_MATCH("*?x", "x", FALSE);
238 TEST_MATCH("?*x", "x", FALSE);
239 TEST_MATCH("*?*x", "yx", TRUE);
240 TEST_MATCH("*?*x", "xxxx", TRUE);
241 TEST_MATCH("x*??", "xyzw", TRUE);
242 TEST_MATCH("*x", "Äx", TRUE);
243 TEST_MATCH("?x", "Äx", TRUE);
244 TEST_MATCH("??x", "Äx", FALSE);
245 TEST_MATCH("abäö", "abäö", TRUE);
246 TEST_MATCH("abäö", "abao", FALSE);
247 TEST_MATCH("ab?ö", "abäö", TRUE);
248 TEST_MATCH("ab?ö", "abao", FALSE);
249 TEST_MATCH("abä?", "abäö", TRUE);
250 TEST_MATCH("abä?", "abao", FALSE);
251 TEST_MATCH("ab??", "abäö", TRUE);
252 TEST_MATCH("ab*", "abäö", TRUE);
253 TEST_MATCH("ab*ö", "abäö", TRUE);
254 TEST_MATCH("ab*ö", "abaöxö", TRUE);
256 g_print ("\n%u tests passed, %u failed\n", passed, failed);
258 return 0;