|
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. |
|
8 * |
|
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. |
|
13 * |
|
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. |
|
18 */ |
|
19 |
|
20 #undef G_DISABLE_ASSERT |
|
21 #undef G_LOG_DOMAIN |
|
22 |
|
23 #include <string.h> |
|
24 #include <glib.h> |
|
25 #ifdef __SYMBIAN32__ |
|
26 #include "mrt2_glib2_test.h" |
|
27 #endif /*__SYMBIAN32__*/ |
|
28 |
|
29 |
|
30 static gboolean noisy = FALSE; |
|
31 |
|
32 static void |
|
33 verbose (const gchar *format, ...) |
|
34 { |
|
35 gchar *msg; |
|
36 va_list args; |
|
37 |
|
38 va_start (args, format); |
|
39 msg = g_strdup_vprintf (format, args); |
|
40 va_end (args); |
|
41 |
|
42 if (noisy) |
|
43 g_print (msg); |
|
44 g_free (msg); |
|
45 } |
|
46 |
|
47 /* keep enum and structure of gpattern.c and patterntest.c in sync */ |
|
48 typedef enum |
|
49 { |
|
50 G_MATCH_ALL, /* "*A?A*" */ |
|
51 G_MATCH_ALL_TAIL, /* "*A?AA" */ |
|
52 G_MATCH_HEAD, /* "AAAA*" */ |
|
53 G_MATCH_TAIL, /* "*AAAA" */ |
|
54 G_MATCH_EXACT, /* "AAAAA" */ |
|
55 G_MATCH_LAST |
|
56 } GMatchType; |
|
57 |
|
58 struct _GPatternSpec |
|
59 { |
|
60 GMatchType match_type; |
|
61 guint pattern_length; |
|
62 guint min_length; |
|
63 guint max_length; |
|
64 gchar *pattern; |
|
65 }; |
|
66 |
|
67 |
|
68 static gchar * |
|
69 match_type_name (GMatchType match_type) |
|
70 { |
|
71 switch (match_type) |
|
72 { |
|
73 case G_MATCH_ALL: |
|
74 return "G_MATCH_ALL"; |
|
75 break; |
|
76 case G_MATCH_ALL_TAIL: |
|
77 return "G_MATCH_ALL_TAIL"; |
|
78 break; |
|
79 case G_MATCH_HEAD: |
|
80 return "G_MATCH_HEAD"; |
|
81 break; |
|
82 case G_MATCH_TAIL: |
|
83 return "G_MATCH_TAIL"; |
|
84 break; |
|
85 case G_MATCH_EXACT: |
|
86 return "G_MATCH_EXACT"; |
|
87 break; |
|
88 default: |
|
89 return "unknown GMatchType"; |
|
90 break; |
|
91 } |
|
92 } |
|
93 |
|
94 static gboolean |
|
95 test_compilation (gchar *src, |
|
96 GMatchType match_type, |
|
97 gchar *pattern, |
|
98 guint min) |
|
99 { |
|
100 GPatternSpec *spec; |
|
101 |
|
102 verbose ("compiling \"%s\" \t", src); |
|
103 spec = g_pattern_spec_new (src); |
|
104 |
|
105 if (spec->match_type != match_type) |
|
106 { |
|
107 g_print ("failed \t(match_type: %s, expected %s)\n", |
|
108 match_type_name (spec->match_type), |
|
109 match_type_name (match_type)); |
|
110 g_pattern_spec_free (spec); |
|
111 return FALSE; |
|
112 } |
|
113 |
|
114 if (strcmp (spec->pattern, pattern) != 0) |
|
115 { |
|
116 g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n", |
|
117 spec->pattern, |
|
118 pattern); |
|
119 g_pattern_spec_free (spec); |
|
120 return FALSE; |
|
121 } |
|
122 |
|
123 if (spec->pattern_length != strlen (spec->pattern)) |
|
124 { |
|
125 g_print ("failed \t(pattern_length: %d, expected %d)\n", |
|
126 spec->pattern_length, |
|
127 (gint)strlen (spec->pattern)); |
|
128 g_pattern_spec_free (spec); |
|
129 return FALSE; |
|
130 } |
|
131 |
|
132 if (spec->min_length != min) |
|
133 { |
|
134 g_print ("failed \t(min_length: %d, expected %d)\n", |
|
135 spec->min_length, |
|
136 min); |
|
137 g_pattern_spec_free (spec); |
|
138 return FALSE; |
|
139 } |
|
140 |
|
141 verbose ("passed (%s: \"%s\")\n", |
|
142 match_type_name (spec->match_type), |
|
143 spec->pattern); |
|
144 |
|
145 g_pattern_spec_free (spec); |
|
146 |
|
147 return TRUE; |
|
148 } |
|
149 |
|
150 static gboolean |
|
151 test_match (gchar *pattern, |
|
152 gchar *string, |
|
153 gboolean match) |
|
154 { |
|
155 verbose ("matching \"%s\" against \"%s\" \t", string, pattern); |
|
156 |
|
157 if (g_pattern_match_simple (pattern, string) != match) |
|
158 { |
|
159 g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match")); |
|
160 return FALSE; |
|
161 } |
|
162 |
|
163 verbose ("passed (%s)\n", match ? "match" : "nomatch"); |
|
164 |
|
165 return TRUE; |
|
166 } |
|
167 |
|
168 static gboolean |
|
169 test_equal (gchar *pattern1, |
|
170 gchar *pattern2, |
|
171 gboolean expected) |
|
172 { |
|
173 GPatternSpec *p1 = g_pattern_spec_new (pattern1); |
|
174 GPatternSpec *p2 = g_pattern_spec_new (pattern2); |
|
175 gboolean equal = g_pattern_spec_equal (p1, p2); |
|
176 |
|
177 verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2); |
|
178 |
|
179 if (expected != equal) |
|
180 { |
|
181 g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n", |
|
182 match_type_name (p1->match_type), p1->pattern_length, p1->pattern, |
|
183 expected ? "!=" : "==", |
|
184 match_type_name (p2->match_type), p2->pattern_length, p2->pattern); |
|
185 } |
|
186 else |
|
187 verbose ("passed (%s)\n", equal ? "equal" : "unequal"); |
|
188 |
|
189 g_pattern_spec_free (p1); |
|
190 g_pattern_spec_free (p2); |
|
191 |
|
192 return expected == equal; |
|
193 } |
|
194 |
|
195 #define TEST_COMPILATION(src, type, pattern, min) { \ |
|
196 total++; \ |
|
197 if (test_compilation (src, type, pattern, min)) \ |
|
198 passed++; \ |
|
199 else \ |
|
200 failed++; \ |
|
201 } |
|
202 |
|
203 #define TEST_MATCH(pattern, string, match) { \ |
|
204 total++; \ |
|
205 if (test_match (pattern, string, match)) \ |
|
206 passed++; \ |
|
207 else \ |
|
208 failed++; \ |
|
209 } |
|
210 |
|
211 #define TEST_EQUAL(pattern1, pattern2, match) { \ |
|
212 total++; \ |
|
213 if (test_equal (pattern1, pattern2, match)) \ |
|
214 passed++; \ |
|
215 else \ |
|
216 failed++; \ |
|
217 } |
|
218 |
|
219 int |
|
220 main (int argc, char** argv) |
|
221 { |
|
222 gint total = 0; |
|
223 gint passed = 0; |
|
224 gint failed = 0; |
|
225 gint i; |
|
226 |
|
227 #ifdef __SYMBIAN32__ |
|
228 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); |
|
229 g_set_print_handler(mrtPrintHandler); |
|
230 #endif /*__SYMBIAN32__*/ |
|
231 |
|
232 |
|
233 for (i = 1; i < argc; i++) |
|
234 if (strcmp ("--noisy", argv[i]) == 0) |
|
235 noisy = TRUE; |
|
236 |
|
237 TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3); |
|
238 TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8); |
|
239 TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8); |
|
240 TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11); |
|
241 TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4); |
|
242 TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4); |
|
243 TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4); |
|
244 TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5); |
|
245 TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5); |
|
246 TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2); |
|
247 TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2); |
|
248 TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2); |
|
249 TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3); |
|
250 |
|
251 TEST_EQUAL("*A?B*", "*A?B*", TRUE); |
|
252 TEST_EQUAL("A*BCD", "A*BCD", TRUE); |
|
253 TEST_EQUAL("ABCD*", "ABCD****", TRUE); |
|
254 TEST_EQUAL("A1*", "A1*", TRUE); |
|
255 TEST_EQUAL("*YZ", "*YZ", TRUE); |
|
256 TEST_EQUAL("A1x", "A1x", TRUE); |
|
257 TEST_EQUAL("AB*CD", "AB**CD", TRUE); |
|
258 TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE); |
|
259 TEST_EQUAL("AB*?CD", "AB?*CD", TRUE); |
|
260 TEST_EQUAL("AB*CD", "AB*?*CD", FALSE); |
|
261 TEST_EQUAL("ABC*", "ABC?", FALSE); |
|
262 |
|
263 TEST_MATCH("*x", "x", TRUE); |
|
264 TEST_MATCH("*x", "xx", TRUE); |
|
265 TEST_MATCH("*x", "yyyx", TRUE); |
|
266 TEST_MATCH("*x", "yyxy", FALSE); |
|
267 TEST_MATCH("?x", "x", FALSE); |
|
268 TEST_MATCH("?x", "xx", TRUE); |
|
269 TEST_MATCH("?x", "yyyx", FALSE); |
|
270 TEST_MATCH("?x", "yyxy", FALSE); |
|
271 TEST_MATCH("*?x", "xx", TRUE); |
|
272 TEST_MATCH("?*x", "xx", TRUE); |
|
273 TEST_MATCH("*?x", "x", FALSE); |
|
274 TEST_MATCH("?*x", "x", FALSE); |
|
275 TEST_MATCH("*?*x", "yx", TRUE); |
|
276 TEST_MATCH("*?*x", "xxxx", TRUE); |
|
277 TEST_MATCH("x*??", "xyzw", TRUE); |
|
278 TEST_MATCH("*x", "\xc3\x84x", TRUE); |
|
279 TEST_MATCH("?x", "\xc3\x84x", TRUE); |
|
280 TEST_MATCH("??x", "\xc3\x84x", FALSE); |
|
281 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
282 TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE); |
|
283 TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
284 TEST_MATCH("ab?\xc3\xb6", "abao", FALSE); |
|
285 TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
286 TEST_MATCH("ab\xc3\xa4?", "abao", FALSE); |
|
287 TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
288 TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
289 TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); |
|
290 TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE); |
|
291 TEST_MATCH("", "abc", FALSE); |
|
292 |
|
293 TEST_MATCH("", "", TRUE); |
|
294 TEST_MATCH("abc", "abc", TRUE); |
|
295 TEST_MATCH("*fo1*bar", "yyyfoxfo1bar", TRUE); |
|
296 TEST_MATCH("12*fo1g*bar", "12yyyfoxfo1gbar", TRUE); |
|
297 TEST_MATCH("__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE); |
|
298 TEST_MATCH("*abc*cde", "abcde", FALSE); |
|
299 TEST_MATCH("*abc*cde", "abccde", TRUE); |
|
300 TEST_MATCH("*abc*cde", "abcxcde", TRUE); |
|
301 TEST_MATCH("*abc*?cde", "abccde", FALSE); |
|
302 TEST_MATCH("*abc*?cde", "abcxcde", TRUE); |
|
303 TEST_MATCH("*abc*def", "abababcdededef", TRUE); |
|
304 TEST_MATCH("*abc*def", "abcbcbcdededef", TRUE); |
|
305 TEST_MATCH("*acbc*def", "acbcbcbcdededef", TRUE); |
|
306 TEST_MATCH("*a?bc*def", "acbcbcbcdededef", TRUE); |
|
307 TEST_MATCH("*abc*def", "bcbcbcdefdef", FALSE); |
|
308 TEST_MATCH("*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE); |
|
309 TEST_MATCH("*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE); |
|
310 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); |
|
311 TEST_MATCH("fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE); |
|
312 |
|
313 verbose ("\n%u tests passed, %u failed\n", passed, failed); |
|
314 #ifdef __SYMBIAN32__ |
|
315 testResultXml("patterntest"); |
|
316 #endif /* EMULATOR */ |
|
317 |
|
318 return failed; |
|
319 } |
|
320 |
|
321 |