nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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, 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 | { |
||
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 } |
||
69 | }; |
||
70 | |||
71 | static void |
||
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 | typedef struct _MatchTest MatchTest; |
||
88 | |||
89 | struct _MatchTest |
||
90 | { |
||
91 | const gchar *pattern; |
||
92 | const gchar *string; |
||
93 | gboolean match; |
||
94 | }; |
||
95 | |||
96 | static MatchTest match_tests[] = |
||
97 | { |
||
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 } |
||
149 | }; |
||
150 | |||
151 | static void |
||
152 | test_match (gconstpointer d) |
||
153 | { |
||
154 | const MatchTest *test = d; |
||
155 | GPatternSpec *p; |
||
156 | gchar *r; |
||
157 | |||
158 | g_assert_cmpint (g_pattern_match_simple (test->pattern, test->string), ==, test->match); |
||
159 | |||
160 | p = g_pattern_spec_new (test->pattern); |
||
161 | g_assert_cmpint (g_pattern_match_string (p, test->string), ==, test->match); |
||
162 | |||
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); |
||
166 | |||
167 | g_pattern_spec_free (p); |
||
168 | } |
||
169 | |||
170 | typedef struct _EqualTest EqualTest; |
||
171 | |||
172 | struct _EqualTest |
||
173 | { |
||
174 | const gchar *pattern1; |
||
175 | const gchar *pattern2; |
||
176 | gboolean expected; |
||
177 | }; |
||
178 | |||
179 | static EqualTest equal_tests[] = |
||
180 | { |
||
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 }, |
||
192 | }; |
||
193 | |||
194 | static void |
||
195 | test_equal (gconstpointer d) |
||
196 | { |
||
197 | const EqualTest *test = d; |
||
198 | GPatternSpec *p1, *p2; |
||
199 | |||
200 | p1 = g_pattern_spec_new (test->pattern1); |
||
201 | p2 = g_pattern_spec_new (test->pattern2); |
||
202 | |||
203 | g_assert_cmpint (g_pattern_spec_equal (p1, p2), ==, test->expected); |
||
204 | |||
205 | g_pattern_spec_free (p1); |
||
206 | g_pattern_spec_free (p2); |
||
207 | } |
||
208 | |||
209 | |||
210 | int |
||
211 | main (int argc, char** argv) |
||
212 | { |
||
213 | gint i; |
||
214 | gchar *path; |
||
215 | |||
216 | g_test_init (&argc, &argv, NULL); |
||
217 | |||
218 | for (i = 0; i < G_N_ELEMENTS (compile_tests); i++) |
||
219 | { |
||
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); |
||
223 | } |
||
224 | |||
225 | for (i = 0; i < G_N_ELEMENTS (match_tests); i++) |
||
226 | { |
||
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); |
||
230 | } |
||
231 | |||
232 | for (i = 0; i < G_N_ELEMENTS (equal_tests); i++) |
||
233 | { |
||
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); |
||
237 | } |
||
238 | |||
239 | return g_test_run (); |
||
240 | } |
||
241 | |||
242 |