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
26 /* keep enum and structure of gpattern.c and patterntest.c in sync */
29 G_MATCH_ALL, /* "*A?A*" */
30 G_MATCH_ALL_TAIL, /* "*A?AA" */
31 G_MATCH_HEAD, /* "AAAA*" */
32 G_MATCH_TAIL, /* "*AAAA" */
33 G_MATCH_EXACT, /* "AAAAA" */
39 GMatchType match_type;
46 typedef struct _CompileTest CompileTest;
51 GMatchType match_type;
56 static CompileTest compile_tests[] =
58 { "*A?B*", G_MATCH_ALL, "*A?B*", 3 },
59 { "ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8 },
60 { "ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8 },
61 { "ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11 },
62 { "*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4 },
63 { "ABCD*", G_MATCH_HEAD, "ABCD", 4 },
64 { "*ABCD", G_MATCH_TAIL, "ABCD", 4 },
65 { "ABCDE", G_MATCH_EXACT, "ABCDE", 5 },
66 { "A?C?E", G_MATCH_ALL, "A?C?E", 5 },
67 { "*?x", G_MATCH_ALL_TAIL, "x?*", 2 },
68 { "?*x", G_MATCH_ALL_TAIL, "x?*", 2 },
69 { "*?*x", G_MATCH_ALL_TAIL, "x?*", 2 },
70 { "x*??", G_MATCH_ALL_TAIL, "??*x", 3 }
74 test_compilation (gconstpointer d)
76 const CompileTest *test = d;
79 spec = g_pattern_spec_new (test->src);
81 g_assert_cmpint (spec->match_type, ==, test->match_type);
82 g_assert_cmpstr (spec->pattern, ==, test->pattern);
83 g_assert_cmpint (spec->pattern_length, ==, strlen (spec->pattern));
84 g_assert_cmpint (spec->min_length, ==, test->min);
86 g_pattern_spec_free (spec);
89 typedef struct _MatchTest MatchTest;
98 static MatchTest match_tests[] =
101 { "*x", "xx", TRUE },
102 { "*x", "yyyx", TRUE },
103 { "*x", "yyxy", FALSE },
104 { "?x", "x", FALSE },
105 { "?x", "xx", TRUE },
106 { "?x", "yyyx", FALSE },
107 { "?x", "yyxy", FALSE },
108 { "*?x", "xx", TRUE },
109 { "?*x", "xx", TRUE },
110 { "*?x", "x", FALSE },
111 { "?*x", "x", FALSE },
112 { "*?*x", "yx", TRUE },
113 { "*?*x", "xxxx", TRUE },
114 { "x*??", "xyzw", TRUE },
115 { "*x", "\xc3\x84x", TRUE },
116 { "?x", "\xc3\x84x", TRUE },
117 { "??x", "\xc3\x84x", FALSE },
118 { "ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
119 { "ab\xc3\xa4\xc3\xb6", "abao", FALSE },
120 { "ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
121 { "ab?\xc3\xb6", "abao", FALSE },
122 { "ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE },
123 { "ab\xc3\xa4?", "abao", FALSE },
124 { "ab??", "ab\xc3\xa4\xc3\xb6", TRUE },
125 { "ab*", "ab\xc3\xa4\xc3\xb6", TRUE },
126 { "ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
127 { "ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE },
128 { "", "abc", FALSE },
130 { "abc", "abc", TRUE },
131 { "*fo1*bar", "yyyfoxfo1bar", TRUE },
132 { "12*fo1g*bar", "12yyyfoxfo1gbar", TRUE },
133 { "__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE },
134 { "*abc*cde", "abcde", FALSE },
135 { "*abc*cde", "abccde", TRUE },
136 { "*abc*cde", "abcxcde", TRUE },
137 { "*abc*?cde", "abccde", FALSE },
138 { "*abc*?cde", "abcxcde", TRUE },
139 { "*abc*def", "abababcdededef", TRUE },
140 { "*abc*def", "abcbcbcdededef", TRUE },
141 { "*acbc*def", "acbcbcbcdededef", TRUE },
142 { "*a?bc*def", "acbcbcbcdededef", TRUE },
143 { "*abc*def", "bcbcbcdefdef", FALSE },
144 { "*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE },
145 { "*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE },
146 { "_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 },
147 { "fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE },
148 { "x*?", "x", FALSE },
149 { "abc*", "abc", TRUE },
154 test_match (gconstpointer d)
156 const MatchTest *test = d;
160 g_assert_cmpint (g_pattern_match_simple (test->pattern, test->string), ==, test->match);
162 p = g_pattern_spec_new (test->pattern);
163 g_assert_cmpint (g_pattern_match_string (p, test->string), ==, test->match);
165 r = g_utf8_strreverse (test->string, -1);
166 g_assert_cmpint (g_pattern_match (p, strlen (test->string), test->string, r), ==, test->match);
169 g_pattern_spec_free (p);
172 typedef struct _EqualTest EqualTest;
176 const gchar *pattern1;
177 const gchar *pattern2;
181 static EqualTest equal_tests[] =
183 { "*A?B*", "*A?B*", TRUE },
184 { "A*BCD", "A*BCD", TRUE },
185 { "ABCD*", "ABCD****", TRUE },
186 { "A1*", "A1*", TRUE },
187 { "*YZ", "*YZ", TRUE },
188 { "A1x", "A1x", TRUE },
189 { "AB*CD", "AB**CD", TRUE },
190 { "AB*?*CD", "AB*?CD", TRUE },
191 { "AB*?CD", "AB?*CD", TRUE },
192 { "AB*CD", "AB*?*CD", FALSE },
193 { "ABC*", "ABC?", FALSE },
197 test_equal (gconstpointer d)
199 const EqualTest *test = d;
200 GPatternSpec *p1, *p2;
202 p1 = g_pattern_spec_new (test->pattern1);
203 p2 = g_pattern_spec_new (test->pattern2);
205 g_assert_cmpint (g_pattern_spec_equal (p1, p2), ==, test->expected);
207 g_pattern_spec_free (p1);
208 g_pattern_spec_free (p2);
213 main (int argc, char** argv)
218 g_test_init (&argc, &argv, NULL);
220 for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
222 path = g_strdup_printf ("/pattern/compile/%d", i);
223 g_test_add_data_func (path, &compile_tests[i], test_compilation);
227 for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
229 path = g_strdup_printf ("/pattern/match/%d", i);
230 g_test_add_data_func (path, &match_tests[i], test_match);
234 for (i = 0; i < G_N_ELEMENTS (equal_tests); i++)
236 path = g_strdup_printf ("/pattern/equal/%d", i);
237 g_test_add_data_func (path, &equal_tests[i], test_equal);
241 return g_test_run ();