1 | /* GLIB - Library of useful routines for C programming |
---|
2 | * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de> |
---|
3 | * |
---|
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 | |
---|
25 | #include "glib.h" |
---|
26 | #include "glib/gpattern.h" |
---|
27 | |
---|
28 | |
---|
29 | /* keep enum and structure of gpattern.c and patterntest.c in sync */ |
---|
30 | typedef enum |
---|
31 | { |
---|
32 | G_MATCH_ALL, /* "*A?A*" */ |
---|
33 | G_MATCH_ALL_TAIL, /* "*A?AA" */ |
---|
34 | G_MATCH_HEAD, /* "AAAA*" */ |
---|
35 | G_MATCH_TAIL, /* "*AAAA" */ |
---|
36 | G_MATCH_EXACT, /* "AAAAA" */ |
---|
37 | G_MATCH_LAST |
---|
38 | } GMatchType; |
---|
39 | |
---|
40 | struct _GPatternSpec |
---|
41 | { |
---|
42 | GMatchType match_type; |
---|
43 | guint pattern_length; |
---|
44 | guint min_length; |
---|
45 | gchar *pattern; |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | static gchar * |
---|
50 | match_type_name (GMatchType match_type) |
---|
51 | { |
---|
52 | switch (match_type) |
---|
53 | { |
---|
54 | case G_MATCH_ALL: |
---|
55 | return "G_MATCH_ALL"; |
---|
56 | break; |
---|
57 | case G_MATCH_ALL_TAIL: |
---|
58 | return "G_MATCH_ALL_TAIL"; |
---|
59 | break; |
---|
60 | case G_MATCH_HEAD: |
---|
61 | return "G_MATCH_HEAD"; |
---|
62 | break; |
---|
63 | case G_MATCH_TAIL: |
---|
64 | return "G_MATCH_TAIL"; |
---|
65 | break; |
---|
66 | case G_MATCH_EXACT: |
---|
67 | return "G_MATCH_EXACT"; |
---|
68 | break; |
---|
69 | default: |
---|
70 | return "unknown GMatchType"; |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /* This leakes memory, but we don't care. The utf8 macro used to convert utf8 |
---|
76 | back to Latin1 for feeding it to g_print. As of 2.0.1, g_print expects utf8, |
---|
77 | so this is no longer necessary */ |
---|
78 | #define utf8(str) str |
---|
79 | #define latin1(str) g_convert (str, -1, "UTF-8", "Latin1", NULL, NULL, NULL) |
---|
80 | |
---|
81 | static gboolean |
---|
82 | test_compilation (gchar *src, |
---|
83 | GMatchType match_type, |
---|
84 | gchar *pattern, |
---|
85 | guint min) |
---|
86 | { |
---|
87 | GPatternSpec *spec; |
---|
88 | |
---|
89 | g_print ("compiling \"%s\" \t", utf8(src)); |
---|
90 | spec = g_pattern_spec_new (src); |
---|
91 | |
---|
92 | if (spec->match_type != match_type) |
---|
93 | { |
---|
94 | g_print ("failed \t(match_type: %s, expected %s)\n", |
---|
95 | match_type_name (spec->match_type), |
---|
96 | match_type_name (match_type)); |
---|
97 | return FALSE; |
---|
98 | } |
---|
99 | |
---|
100 | if (strcmp (spec->pattern, pattern) != 0) |
---|
101 | { |
---|
102 | g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n", |
---|
103 | utf8(spec->pattern), |
---|
104 | utf8(pattern)); |
---|
105 | return FALSE; |
---|
106 | } |
---|
107 | |
---|
108 | if (spec->pattern_length != strlen (spec->pattern)) |
---|
109 | { |
---|
110 | g_print ("failed \t(pattern_length: %d, expected %d)\n", |
---|
111 | spec->pattern_length, |
---|
112 | (gint)strlen (spec->pattern)); |
---|
113 | return FALSE; |
---|
114 | } |
---|
115 | |
---|
116 | if (spec->min_length != min) |
---|
117 | { |
---|
118 | g_print ("failed \t(min_length: %d, expected %d)\n", |
---|
119 | spec->min_length, |
---|
120 | min); |
---|
121 | return FALSE; |
---|
122 | } |
---|
123 | |
---|
124 | g_print ("passed (%s: \"%s\")\n", |
---|
125 | match_type_name (spec->match_type), |
---|
126 | spec->pattern); |
---|
127 | |
---|
128 | return TRUE; |
---|
129 | } |
---|
130 | |
---|
131 | static gboolean |
---|
132 | test_match (gchar *pattern, |
---|
133 | gchar *string, |
---|
134 | gboolean match) |
---|
135 | { |
---|
136 | g_print ("matching \"%s\" against \"%s\" \t", utf8(string), utf8(pattern)); |
---|
137 | |
---|
138 | if (g_pattern_match_simple (pattern, string) != match) |
---|
139 | { |
---|
140 | g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match")); |
---|
141 | return FALSE; |
---|
142 | } |
---|
143 | |
---|
144 | g_print ("passed (%s)\n", match ? "match" : "nomatch"); |
---|
145 | return TRUE; |
---|
146 | } |
---|
147 | |
---|
148 | static gboolean |
---|
149 | test_equal (gchar *pattern1, |
---|
150 | gchar *pattern2, |
---|
151 | gboolean expected) |
---|
152 | { |
---|
153 | GPatternSpec *p1 = g_pattern_spec_new (pattern1); |
---|
154 | GPatternSpec *p2 = g_pattern_spec_new (pattern2); |
---|
155 | gboolean equal = g_pattern_spec_equal (p1, p2); |
---|
156 | |
---|
157 | g_print ("comparing \"%s\" with \"%s\" \t", utf8(pattern1), utf8(pattern2)); |
---|
158 | |
---|
159 | if (expected != equal) |
---|
160 | { |
---|
161 | g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n", |
---|
162 | match_type_name (p1->match_type), p1->pattern_length, utf8(p1->pattern), |
---|
163 | expected ? "!=" : "==", |
---|
164 | match_type_name (p2->match_type), p2->pattern_length, utf8(p2->pattern)); |
---|
165 | } |
---|
166 | else |
---|
167 | g_print ("passed (%s)\n", equal ? "equal" : "unequal"); |
---|
168 | |
---|
169 | g_pattern_spec_free (p1); |
---|
170 | g_pattern_spec_free (p2); |
---|
171 | |
---|
172 | return expected == equal; |
---|
173 | } |
---|
174 | |
---|
175 | #define TEST_COMPILATION(src, type, pattern, min) { \ |
---|
176 | total++; \ |
---|
177 | if (test_compilation (latin1(src), type, latin1(pattern), min)) \ |
---|
178 | passed++; \ |
---|
179 | else \ |
---|
180 | failed++; \ |
---|
181 | } |
---|
182 | |
---|
183 | #define TEST_MATCH(pattern, string, match) { \ |
---|
184 | total++; \ |
---|
185 | if (test_match (latin1(pattern), latin1(string), match)) \ |
---|
186 | passed++; \ |
---|
187 | else \ |
---|
188 | failed++; \ |
---|
189 | } |
---|
190 | |
---|
191 | #define TEST_EQUAL(pattern1, pattern2, match) { \ |
---|
192 | total++; \ |
---|
193 | if (test_equal (latin1(pattern1), latin1(pattern2), match)) \ |
---|
194 | passed++; \ |
---|
195 | else \ |
---|
196 | failed++; \ |
---|
197 | } |
---|
198 | |
---|
199 | int |
---|
200 | main (int argc, char** argv) |
---|
201 | { |
---|
202 | gint total = 0; |
---|
203 | gint passed = 0; |
---|
204 | gint failed = 0; |
---|
205 | |
---|
206 | TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3); |
---|
207 | TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8); |
---|
208 | TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8); |
---|
209 | TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11); |
---|
210 | TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4); |
---|
211 | TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4); |
---|
212 | TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4); |
---|
213 | TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5); |
---|
214 | TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5); |
---|
215 | TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2); |
---|
216 | TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2); |
---|
217 | TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2); |
---|
218 | TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3); |
---|
219 | |
---|
220 | TEST_EQUAL ("*A?B*", "*A?B*", TRUE); |
---|
221 | TEST_EQUAL ("A*BCD", "A*BCD", TRUE); |
---|
222 | TEST_EQUAL ("ABCD*", "ABCD****", TRUE); |
---|
223 | TEST_EQUAL ("A1*", "A1*", TRUE); |
---|
224 | TEST_EQUAL ("*YZ", "*YZ", TRUE); |
---|
225 | TEST_EQUAL ("A1x", "A1x", TRUE); |
---|
226 | TEST_EQUAL ("AB*CD", "AB**CD", TRUE); |
---|
227 | TEST_EQUAL ("AB*?*CD", "AB*?CD", TRUE); |
---|
228 | TEST_EQUAL ("AB*?CD", "AB?*CD", TRUE); |
---|
229 | TEST_EQUAL ("AB*CD", "AB*?*CD", FALSE); |
---|
230 | TEST_EQUAL ("ABC*", "ABC?", FALSE); |
---|
231 | |
---|
232 | TEST_MATCH("*x", "x", TRUE); |
---|
233 | TEST_MATCH("*x", "xx", TRUE); |
---|
234 | TEST_MATCH("*x", "yyyx", TRUE); |
---|
235 | TEST_MATCH("*x", "yyxy", FALSE); |
---|
236 | TEST_MATCH("?x", "x", FALSE); |
---|
237 | TEST_MATCH("?x", "xx", TRUE); |
---|
238 | TEST_MATCH("?x", "yyyx", FALSE); |
---|
239 | TEST_MATCH("?x", "yyxy", FALSE); |
---|
240 | TEST_MATCH("*?x", "xx", TRUE); |
---|
241 | TEST_MATCH("?*x", "xx", TRUE); |
---|
242 | TEST_MATCH("*?x", "x", FALSE); |
---|
243 | TEST_MATCH("?*x", "x", FALSE); |
---|
244 | TEST_MATCH("*?*x", "yx", TRUE); |
---|
245 | TEST_MATCH("*?*x", "xxxx", TRUE); |
---|
246 | TEST_MATCH("x*??", "xyzw", TRUE); |
---|
247 | TEST_MATCH("*x", "\xc4x", TRUE); |
---|
248 | TEST_MATCH("?x", "\xc4x", TRUE); |
---|
249 | TEST_MATCH("??x", "\xc4x", FALSE); |
---|
250 | TEST_MATCH("ab\xe4\xf6", "ab\xe4\xf6", TRUE); |
---|
251 | TEST_MATCH("ab\xe4\xf6", "abao", FALSE); |
---|
252 | TEST_MATCH("ab?\xf6", "ab\xe4\xf6", TRUE); |
---|
253 | TEST_MATCH("ab?\xf6", "abao", FALSE); |
---|
254 | TEST_MATCH("ab\xe4?", "ab\xe4\xf6", TRUE); |
---|
255 | TEST_MATCH("ab\xe4?", "abao", FALSE); |
---|
256 | TEST_MATCH("ab??", "ab\xe4\xf6", TRUE); |
---|
257 | TEST_MATCH("ab*", "ab\xe4\xf6", TRUE); |
---|
258 | TEST_MATCH("ab*\xf6", "ab\xe4\xf6", TRUE); |
---|
259 | TEST_MATCH("ab*\xf6", "aba\xf6x\xf6", TRUE); |
---|
260 | |
---|
261 | g_print ("\n%u tests passed, %u failed\n", passed, failed); |
---|
262 | |
---|
263 | return 0; |
---|
264 | } |
---|