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
24 /* keep enum and structure of gpattern.c and patterntest.c in sync */
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" */
37 GMatchType match_type
;
44 typedef struct _CompileTest CompileTest
;
49 GMatchType match_type
;
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 }
72 test_compilation (gconstpointer d
)
74 const CompileTest
*test
= d
;
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
;
96 static MatchTest match_tests
[] =
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
},
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
},
152 test_match (gconstpointer d
)
154 const MatchTest
*test
= d
;
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
);
167 g_pattern_spec_free (p
);
170 typedef struct _EqualTest EqualTest
;
174 const gchar
*pattern1
;
175 const gchar
*pattern2
;
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
},
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
)
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
);
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
);
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
);
239 return g_test_run ();