Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / pattern.c
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 #include <glib.h>
25
26 /* keep enum and structure of gpattern.c and patterntest.c in sync */
27 typedef enum
28 {
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" */
34   G_MATCH_LAST
35 } GMatchType;
36
37 struct _GPatternSpec
38 {
39   GMatchType match_type;
40   guint      pattern_length;
41   guint      min_length;
42   guint      max_length;
43   gchar     *pattern;
44 };
45
46 typedef struct _CompileTest CompileTest;
47
48 struct _CompileTest
49 {
50   const gchar *src;
51   GMatchType match_type;
52   gchar *pattern;
53   guint min;
54 };
55
56 static CompileTest compile_tests[] =
57 {
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 }
71 };
72
73 static void
74 test_compilation (gconstpointer d)
75 {
76   const CompileTest *test = d;
77   GPatternSpec *spec;
78
79   spec = g_pattern_spec_new (test->src);
80
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);
85
86   g_pattern_spec_free (spec);
87 }
88
89 typedef struct _MatchTest MatchTest;
90
91 struct _MatchTest
92 {
93   const gchar *pattern;
94   const gchar *string;
95   gboolean match;
96 };
97
98 static MatchTest match_tests[] =
99 {
100   { "*x", "x", TRUE },
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 },
129   { "", "", TRUE },
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 },
150   { "*", "abc", TRUE }
151 };
152
153 static void
154 test_match (gconstpointer d)
155 {
156   const MatchTest *test = d;
157   GPatternSpec *p;
158   gchar *r;
159
160   g_assert_cmpint (g_pattern_match_simple (test->pattern, test->string), ==, test->match);
161
162   p = g_pattern_spec_new (test->pattern);
163   g_assert_cmpint (g_pattern_match_string (p, test->string), ==, test->match);
164
165   r = g_utf8_strreverse (test->string, -1);
166   g_assert_cmpint (g_pattern_match (p, strlen (test->string), test->string, r), ==, test->match);
167   g_free (r);
168
169   g_pattern_spec_free (p);
170 }
171
172 typedef struct _EqualTest EqualTest;
173
174 struct _EqualTest
175 {
176   const gchar *pattern1;
177   const gchar *pattern2;
178   gboolean expected;
179 };
180
181 static EqualTest equal_tests[] =
182 {
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 },
194 };
195
196 static void
197 test_equal (gconstpointer d)
198 {
199   const EqualTest *test = d;
200   GPatternSpec *p1, *p2;
201
202   p1 = g_pattern_spec_new (test->pattern1);
203   p2 = g_pattern_spec_new (test->pattern2);
204
205   g_assert_cmpint (g_pattern_spec_equal (p1, p2), ==, test->expected);
206
207   g_pattern_spec_free (p1);
208   g_pattern_spec_free (p2);
209 }
210
211
212 int
213 main (int argc, char** argv)
214 {
215   gint i;
216   gchar *path;
217
218   g_test_init (&argc, &argv, NULL);
219
220   for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
221     {
222       path = g_strdup_printf ("/pattern/compile/%d", i);
223       g_test_add_data_func (path, &compile_tests[i], test_compilation);
224       g_free (path);
225     }
226
227   for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
228     {
229       path = g_strdup_printf ("/pattern/match/%d", i);
230       g_test_add_data_func (path, &match_tests[i], test_match);
231       g_free (path);
232     }
233
234   for (i = 0; i < G_N_ELEMENTS (equal_tests); i++)
235     {
236       path = g_strdup_printf ("/pattern/equal/%d", i);
237       g_test_add_data_func (path, &equal_tests[i], test_equal);
238       g_free (path);
239     }
240
241   return g_test_run ();
242 }
243
244