gatomic: Add missing new line in API doc comment
[glib.git] / glib / tests / pattern.c
blob538c4d2211b24faa9895a2072a63a296fb641e14
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, see <http://www.gnu.org/licenses/>.
18 #undef G_DISABLE_ASSERT
19 #undef G_LOG_DOMAIN
21 #include <string.h>
22 #include <glib.h>
24 /* keep enum and structure of gpattern.c and patterntest.c in sync */
25 typedef enum
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;
35 struct _GPatternSpec
37 GMatchType match_type;
38 guint pattern_length;
39 guint min_length;
40 guint max_length;
41 gchar *pattern;
44 typedef struct _CompileTest CompileTest;
46 struct _CompileTest
48 const gchar *src;
49 GMatchType match_type;
50 gchar *pattern;
51 guint min;
54 static CompileTest compile_tests[] =
56 { "*A?B*", G_MATCH_ALL, "*A?B*", 3 },
57 { "ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8 },
58 { "ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8 },
59 { "ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 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 }
71 static void
72 test_compilation (gconstpointer d)
74 const CompileTest *test = d;
75 GPatternSpec *spec;
77 spec = g_pattern_spec_new (test->src);
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);
84 g_pattern_spec_free (spec);
87 typedef struct _MatchTest MatchTest;
89 struct _MatchTest
91 const gchar *pattern;
92 const gchar *string;
93 gboolean match;
96 static MatchTest match_tests[] =
98 { "*x", "x", TRUE },
99 { "*x", "xx", TRUE },
100 { "*x", "yyyx", TRUE },
101 { "*x", "yyxy", FALSE },
102 { "?x", "x", FALSE },
103 { "?x", "xx", TRUE },
104 { "?x", "yyyx", FALSE },
105 { "?x", "yyxy", FALSE },
106 { "*?x", "xx", TRUE },
107 { "?*x", "xx", TRUE },
108 { "*?x", "x", FALSE },
109 { "?*x", "x", FALSE },
110 { "*?*x", "yx", TRUE },
111 { "*?*x", "xxxx", TRUE },
112 { "x*??", "xyzw", TRUE },
113 { "*x", "\xc3\x84x", TRUE },
114 { "?x", "\xc3\x84x", TRUE },
115 { "??x", "\xc3\x84x", FALSE },
116 { "ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
117 { "ab\xc3\xa4\xc3\xb6", "abao", FALSE },
118 { "ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
119 { "ab?\xc3\xb6", "abao", FALSE },
120 { "ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE },
121 { "ab\xc3\xa4?", "abao", FALSE },
122 { "ab??", "ab\xc3\xa4\xc3\xb6", TRUE },
123 { "ab*", "ab\xc3\xa4\xc3\xb6", TRUE },
124 { "ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE },
125 { "ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE },
126 { "", "abc", FALSE },
127 { "", "", TRUE },
128 { "abc", "abc", TRUE },
129 { "*fo1*bar", "yyyfoxfo1bar", TRUE },
130 { "12*fo1g*bar", "12yyyfoxfo1gbar", TRUE },
131 { "__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE },
132 { "*abc*cde", "abcde", FALSE },
133 { "*abc*cde", "abccde", TRUE },
134 { "*abc*cde", "abcxcde", TRUE },
135 { "*abc*?cde", "abccde", FALSE },
136 { "*abc*?cde", "abcxcde", TRUE },
137 { "*abc*def", "abababcdededef", TRUE },
138 { "*abc*def", "abcbcbcdededef", TRUE },
139 { "*acbc*def", "acbcbcbcdededef", TRUE },
140 { "*a?bc*def", "acbcbcbcdededef", TRUE },
141 { "*abc*def", "bcbcbcdefdef", FALSE },
142 { "*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE },
143 { "*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE },
144 { "_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 },
145 { "fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE },
146 { "x*?", "x", FALSE },
147 { "abc*", "abc", TRUE },
148 { "*", "abc", TRUE }
151 static void
152 test_match (gconstpointer d)
154 const MatchTest *test = d;
155 GPatternSpec *p;
156 gchar *r;
158 g_assert_cmpint (g_pattern_match_simple (test->pattern, test->string), ==, test->match);
160 p = g_pattern_spec_new (test->pattern);
161 g_assert_cmpint (g_pattern_match_string (p, test->string), ==, test->match);
163 r = g_utf8_strreverse (test->string, -1);
164 g_assert_cmpint (g_pattern_match (p, strlen (test->string), test->string, r), ==, test->match);
165 g_free (r);
167 g_pattern_spec_free (p);
170 typedef struct _EqualTest EqualTest;
172 struct _EqualTest
174 const gchar *pattern1;
175 const gchar *pattern2;
176 gboolean expected;
179 static EqualTest equal_tests[] =
181 { "*A?B*", "*A?B*", TRUE },
182 { "A*BCD", "A*BCD", TRUE },
183 { "ABCD*", "ABCD****", TRUE },
184 { "A1*", "A1*", TRUE },
185 { "*YZ", "*YZ", TRUE },
186 { "A1x", "A1x", TRUE },
187 { "AB*CD", "AB**CD", TRUE },
188 { "AB*?*CD", "AB*?CD", TRUE },
189 { "AB*?CD", "AB?*CD", TRUE },
190 { "AB*CD", "AB*?*CD", FALSE },
191 { "ABC*", "ABC?", FALSE },
194 static void
195 test_equal (gconstpointer d)
197 const EqualTest *test = d;
198 GPatternSpec *p1, *p2;
200 p1 = g_pattern_spec_new (test->pattern1);
201 p2 = g_pattern_spec_new (test->pattern2);
203 g_assert_cmpint (g_pattern_spec_equal (p1, p2), ==, test->expected);
205 g_pattern_spec_free (p1);
206 g_pattern_spec_free (p2);
211 main (int argc, char** argv)
213 gint i;
214 gchar *path;
216 g_test_init (&argc, &argv, NULL);
218 for (i = 0; i < G_N_ELEMENTS (compile_tests); i++)
220 path = g_strdup_printf ("/pattern/compile/%d", i);
221 g_test_add_data_func (path, &compile_tests[i], test_compilation);
222 g_free (path);
225 for (i = 0; i < G_N_ELEMENTS (match_tests); i++)
227 path = g_strdup_printf ("/pattern/match/%d", i);
228 g_test_add_data_func (path, &match_tests[i], test_match);
229 g_free (path);
232 for (i = 0; i < G_N_ELEMENTS (equal_tests); i++)
234 path = g_strdup_printf ("/pattern/equal/%d", i);
235 g_test_add_data_func (path, &equal_tests[i], test_equal);
236 g_free (path);
239 return g_test_run ();