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.1 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #undef G_DISABLE_ASSERT
19 #undef G_LOG_DOMAIN
20
21 #include <string.h>
22 #include <glib.h>
23
24 /* keep enum and structure of gpattern.c and patterntest.c in sync */
25 typedef enum
26 {
27 G_MATCH_ALL, /* "*A?A*" */
28 G_MATCH_ALL_TAIL, /* "*A?AA" */
29 G_MATCH_HEAD, /* "AAAA*" */
30 G_MATCH_TAIL, /* "*AAAA" */
31 G_MATCH_EXACT, /* "AAAAA" */
32 G_MATCH_LAST
33 } GMatchType;
34
35 struct _GPatternSpec
36 {
37 GMatchType match_type;
38 guint pattern_length;
39 guint min_length;
40 guint max_length;
41 gchar *pattern;
42 };
43
44 typedef struct _CompileTest CompileTest;
45
46 struct _CompileTest
47 {
48 const gchar *src;
49 GMatchType match_type;
50 gchar *pattern;
51 guint min;
52 };
53
54 static CompileTest compile_tests[] = {
55 { "*A?B*", G_MATCH_ALL, "*A?B*", 3 },
56 { "ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8 },
57 { "ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8 },
58 { "ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11 },
59 { "**ABC***?DEF**", G_MATCH_ALL, "*ABC*?DEF*", 11 },
60 { "*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4 },
61 { "ABCD*", G_MATCH_HEAD, "ABCD", 4 },
62 { "*ABCD", G_MATCH_TAIL, "ABCD", 4 },
63 { "ABCDE", G_MATCH_EXACT, "ABCDE", 5 },
64 { "A?C?E", G_MATCH_ALL, "A?C?E", 5 },
65 { "*?x", G_MATCH_ALL_TAIL, "x?*", 2 },
66 { "?*x", G_MATCH_ALL_TAIL, "x?*", 2 },
67 { "*?*x", G_MATCH_ALL_TAIL, "x?*", 2 },
68 { "x*??", G_MATCH_ALL_TAIL, "??*x", 3 }
69 };
70
71 static void
test_compilation(gconstpointer d)72 test_compilation (gconstpointer d)
73 {
74 const CompileTest *test = d;
75 GPatternSpec *spec;
76
77 spec = g_pattern_spec_new (test->src);
78
79 g_assert_cmpint (spec->match_type, ==, test->match_type);
80 g_assert_cmpstr (spec->pattern, ==, test->pattern);
81 g_assert_cmpint (spec->pattern_length, ==, strlen (spec->pattern));
82 g_assert_cmpint (spec->min_length, ==, test->min);
83
84 g_pattern_spec_free (spec);
85 }
86
87 static void
test_copy(gconstpointer d)88 test_copy (gconstpointer d)
89 {
90 const CompileTest *test = d;
91 GPatternSpec *p1, *p2;
92
93 p1 = g_pattern_spec_new (test->src);
94 p2 = g_pattern_spec_copy (p1);
95
96 g_assert_cmpint (p2->match_type, ==, test->match_type);
97 g_assert_cmpstr (p2->pattern, ==, test->pattern);
98 g_assert_cmpint (p2->pattern_length, ==, strlen (p1->pattern));
99 g_assert_cmpint (p2->min_length, ==, test->min);
100
101 g_pattern_spec_free (p1);
102 g_pattern_spec_free (p2);
103 }
104
105 typedef struct _MatchTest MatchTest;
106
107 struct _MatchTest
108 {
109 const gchar *pattern;
110 const gchar *string;
111 gboolean match;
112 };
113
114 static MatchTest match_tests[] =
115 {
116 { "*x", "x", TRUE },
117 { "*x", "xx", TRUE },
118 { "*x", "yyyx", TRUE },
119 { "*x", "yyxy", FALSE },
120 { "?x", "x", FALSE },
121 { "?x", "xx", TRUE },
122 { "?x", "yyyx", FALSE },
123 { "?x", "yyxy", FALSE },
124 { "*?x", "xx", TRUE },
125 { "?*x", "xx", TRUE },
126 { "*?x", "x", FALSE },
127 { "?*x", "x", FALSE },
128 { "*?*x", "yx", TRUE },
129 { "*?*x", "xxxx", TRUE },
130 { "x*??", "xyzw", TRUE },
131 { "*x", "\xc3\x84x", TRUE },
132 { "?x", "\xc3\x84x", TRUE },
133 { "??x", "\xc3\x84x", FALSE },
134 { "ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
135 { "ab\xc3\xa4\xc3\xb6", "abao", FALSE },
136 { "ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
137 { "ab?\xc3\xb6", "abao", FALSE },
138 { "ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE },
139 { "ab\xc3\xa4?", "abao", FALSE },
140 { "ab??", "ab\xc3\xa4\xc3\xb6", TRUE },
141 { "ab*", "ab\xc3\xa4\xc3\xb6", TRUE },
142 { "ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
143 { "ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE },
144 { "", "abc", FALSE },
145 { "", "", TRUE },
146 { "abc", "abc", TRUE },
147 { "*fo1*bar", "yyyfoxfo1bar", TRUE },
148 { "12*fo1g*bar", "12yyyfoxfo1gbar", TRUE },
149 { "__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE },
150 { "*abc*cde", "abcde", FALSE },
151 { "*abc*cde", "abccde", TRUE },
152 { "*abc*cde", "abcxcde", TRUE },
153 { "*abc*?cde", "abccde", FALSE },
154 { "*abc*?cde", "abcxcde", TRUE },
155 { "*abc*def", "abababcdededef", TRUE },
156 { "*abc*def", "abcbcbcdededef", TRUE },
157 { "*acbc*def", "acbcbcbcdededef", TRUE },
158 { "*a?bc*def", "acbcbcbcdededef", TRUE },
159 { "*abc*def", "bcbcbcdefdef", FALSE },
160 { "*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE },
161 { "*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE },
162 { "_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 },
163 { "fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE },
164 { "x*?", "x", FALSE },
165 { "abc*", "abc", TRUE },
166 { "*", "abc", TRUE }
167 };
168
169 static void
test_match(gconstpointer d)170 test_match (gconstpointer d)
171 {
172 const MatchTest *test = d;
173 GPatternSpec *p;
174 gchar *r;
175
176 g_assert_cmpint (g_pattern_match_simple (test->pattern, test->string), ==, test->match);
177
178 p = g_pattern_spec_new (test->pattern);
179 g_assert_cmpint (g_pattern_spec_match_string (p, test->string), ==, test->match);
180 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
181 g_assert_cmpint (g_pattern_match_string (p, test->string), ==, test->match);
182 G_GNUC_END_IGNORE_DEPRECATIONS
183
184 r = g_utf8_strreverse (test->string, -1);
185 g_assert_cmpint (g_pattern_spec_match (p, strlen (test->string), test->string, r), ==, test->match);
186 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
187 g_assert_cmpint (g_pattern_match (p, strlen (test->string), test->string, r), ==, test->match);
188 G_GNUC_END_IGNORE_DEPRECATIONS
189 g_free (r);
190
191 g_pattern_spec_free (p);
192 }
193
194 typedef struct _EqualTest EqualTest;
195
196 struct _EqualTest
197 {
198 const gchar *pattern1;
199 const gchar *pattern2;
200 gboolean expected;
201 };
202
203 static EqualTest equal_tests[] =
204 {
205 { "*A?B*", "*A?B*", TRUE },
206 { "A*BCD", "A*BCD", TRUE },
207 { "ABCD*", "ABCD****", TRUE },
208 { "A1*", "A1*", TRUE },
209 { "*YZ", "*YZ", TRUE },
210 { "A1x", "A1x", TRUE },
211 { "AB*CD", "AB**CD", TRUE },
212 { "AB*?*CD", "AB*?CD", TRUE },
213 { "AB*?CD", "AB?*CD", TRUE },
214 { "AB*CD", "AB*?*CD", FALSE },
215 { "ABC*", "ABC?", FALSE },
216 };
217
218 static void
test_equal(gconstpointer d)219 test_equal (gconstpointer d)
220 {
221 const EqualTest *test = d;
222 GPatternSpec *p1, *p2;
223
224 p1 = g_pattern_spec_new (test->pattern1);
225 p2 = g_pattern_spec_new (test->pattern2);
226
227 g_assert_cmpint (g_pattern_spec_equal (p1, p2), ==, test->expected);
228
229 g_pattern_spec_free (p1);
230 g_pattern_spec_free (p2);
231 }
232
233
234 int
main(int argc,char ** argv)235 main (int argc, char** argv)
236 {
237 gsize i;
238 gchar *path;
239
240 g_test_init (&argc, &argv, NULL);
241
242 for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
243 {
244 path = g_strdup_printf ("/pattern/compile/%" G_GSIZE_FORMAT, i);
245 g_test_add_data_func (path, &compile_tests[i], test_compilation);
246 g_free (path);
247 }
248
249 for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
250 {
251 path = g_strdup_printf ("/pattern/copy/%" G_GSIZE_FORMAT, i);
252 g_test_add_data_func (path, &compile_tests[i], test_copy);
253 g_free (path);
254 }
255
256 for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
257 {
258 path = g_strdup_printf ("/pattern/match/%" G_GSIZE_FORMAT, i);
259 g_test_add_data_func (path, &match_tests[i], test_match);
260 g_free (path);
261 }
262
263 for (i = 0; i < G_N_ELEMENTS (equal_tests); i++)
264 {
265 path = g_strdup_printf ("/pattern/equal/%" G_GSIZE_FORMAT, i);
266 g_test_add_data_func (path, &equal_tests[i], test_equal);
267 g_free (path);
268 }
269
270 return g_test_run ();
271 }
272