Add some more tests.
[platform/upstream/glib.git] / tests / patterntest.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
25 #include "glib.h"
26 #include "glib/gpattern.h"
27
28 static gboolean noisy = FALSE;
29
30 static void
31 verbose (const gchar *format, ...)
32 {
33   gchar *msg;
34   va_list args;
35
36   va_start (args, format);
37   msg = g_strdup_vprintf (format, args);
38   va_end (args);
39
40   if (noisy) 
41     g_print (msg);
42   g_free (msg);
43 }
44
45 /* keep enum and structure of gpattern.c and patterntest.c in sync */
46 typedef enum
47 {
48   G_MATCH_ALL,       /* "*A?A*" */
49   G_MATCH_ALL_TAIL,  /* "*A?AA" */
50   G_MATCH_HEAD,      /* "AAAA*" */
51   G_MATCH_TAIL,      /* "*AAAA" */
52   G_MATCH_EXACT,     /* "AAAAA" */
53   G_MATCH_LAST
54 } GMatchType;
55
56 struct _GPatternSpec
57 {
58   GMatchType match_type;
59   guint      pattern_length;
60   guint      min_length;
61   gchar     *pattern;
62 };
63
64
65 static gchar *
66 match_type_name (GMatchType match_type)
67 {
68   switch (match_type)
69     {
70     case G_MATCH_ALL: 
71       return "G_MATCH_ALL";
72       break;
73     case G_MATCH_ALL_TAIL:
74       return "G_MATCH_ALL_TAIL";
75       break;
76     case G_MATCH_HEAD:
77       return "G_MATCH_HEAD";
78       break;
79     case G_MATCH_TAIL:
80       return "G_MATCH_TAIL";
81       break;
82     case G_MATCH_EXACT:
83       return "G_MATCH_EXACT";
84       break;
85     default:
86       return "unknown GMatchType";
87       break;
88     }
89 }
90
91 static gboolean
92 test_compilation (gchar *src, 
93                   GMatchType match_type, 
94                   gchar *pattern,
95                   guint min)
96 {
97   GPatternSpec *spec; 
98
99   verbose ("compiling \"%s\" \t", src);
100   spec = g_pattern_spec_new (src);
101
102   if (spec->match_type != match_type)
103     {
104       g_print ("failed \t(match_type: %s, expected %s)\n",
105                match_type_name (spec->match_type), 
106                match_type_name (match_type));
107       return FALSE;
108     }
109   
110   if (strcmp (spec->pattern, pattern) != 0)
111     {
112       g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
113                spec->pattern,
114                pattern);
115       return FALSE;
116     }
117   
118   if (spec->pattern_length != strlen (spec->pattern))
119     {
120       g_print ("failed \t(pattern_length: %d, expected %d)\n",
121                spec->pattern_length,
122                (gint)strlen (spec->pattern));
123       return FALSE;
124     }
125   
126   if (spec->min_length != min)
127     {
128       g_print ("failed \t(min_length: %d, expected %d)\n",
129                spec->min_length,
130                min);
131       return FALSE;
132     }
133   
134   verbose ("passed (%s: \"%s\")\n",
135            match_type_name (spec->match_type),
136            spec->pattern);
137   
138   return TRUE;
139 }
140
141 static gboolean
142 test_match (gchar *pattern, 
143             gchar *string, 
144             gboolean match)
145 {
146   verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
147   
148   if (g_pattern_match_simple (pattern, string) != match)
149     {
150       g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
151       return FALSE;
152     }
153   
154   verbose ("passed (%s)\n", match ? "match" : "nomatch");
155
156   return TRUE;
157 }
158
159 static gboolean
160 test_equal (gchar *pattern1,
161             gchar *pattern2,
162             gboolean expected)
163 {
164   GPatternSpec *p1 = g_pattern_spec_new (pattern1);
165   GPatternSpec *p2 = g_pattern_spec_new (pattern2);
166   gboolean equal = g_pattern_spec_equal (p1, p2);
167
168   verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
169
170   if (expected != equal)
171     {
172       g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
173                match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
174                expected ? "!=" : "==",
175                match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
176     }
177   else
178     verbose ("passed (%s)\n", equal ? "equal" : "unequal");
179   
180   g_pattern_spec_free (p1);
181   g_pattern_spec_free (p2);
182
183   return expected == equal;
184 }
185
186 #define TEST_COMPILATION(src, type, pattern, min) { \
187   total++; \
188   if (test_compilation (src, type, pattern, min)) \
189     passed++; \
190   else \
191     failed++; \
192 }
193
194 #define TEST_MATCH(pattern, string, match) { \
195   total++; \
196   if (test_match (pattern, string, match)) \
197     passed++; \
198   else \
199     failed++; \
200 }
201
202 #define TEST_EQUAL(pattern1, pattern2, match) { \
203   total++; \
204   if (test_equal (pattern1, pattern2, match)) \
205     passed++; \
206   else \
207     failed++; \
208 }
209
210 int
211 main (int argc, char** argv)
212 {
213   gint total = 0;
214   gint passed = 0;
215   gint failed = 0;
216   gint i;
217
218   for (i = 1; i < argc; i++) 
219       if (strcmp ("--noisy", argv[i]) == 0)
220         noisy = TRUE;
221
222   TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
223   TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
224   TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
225   TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
226   TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
227   TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
228   TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
229   TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
230   TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
231   TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
232   TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
233   TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
234   TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
235
236   TEST_EQUAL("*A?B*", "*A?B*", TRUE);
237   TEST_EQUAL("A*BCD", "A*BCD", TRUE);
238   TEST_EQUAL("ABCD*", "ABCD****", TRUE);
239   TEST_EQUAL("A1*", "A1*", TRUE);
240   TEST_EQUAL("*YZ", "*YZ", TRUE);
241   TEST_EQUAL("A1x", "A1x", TRUE);
242   TEST_EQUAL("AB*CD", "AB**CD", TRUE);
243   TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
244   TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
245   TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
246   TEST_EQUAL("ABC*", "ABC?", FALSE);
247
248   TEST_MATCH("*x", "x", TRUE);
249   TEST_MATCH("*x", "xx", TRUE);
250   TEST_MATCH("*x", "yyyx", TRUE);
251   TEST_MATCH("*x", "yyxy", FALSE);
252   TEST_MATCH("?x", "x", FALSE);
253   TEST_MATCH("?x", "xx", TRUE);
254   TEST_MATCH("?x", "yyyx", FALSE);
255   TEST_MATCH("?x", "yyxy", FALSE);
256   TEST_MATCH("*?x", "xx", TRUE);
257   TEST_MATCH("?*x", "xx", TRUE);
258   TEST_MATCH("*?x", "x", FALSE);
259   TEST_MATCH("?*x", "x", FALSE);
260   TEST_MATCH("*?*x", "yx", TRUE);
261   TEST_MATCH("*?*x", "xxxx", TRUE);
262   TEST_MATCH("x*??", "xyzw", TRUE);
263   TEST_MATCH("*x", "\xc3\x84x", TRUE);
264   TEST_MATCH("?x", "\xc3\x84x", TRUE);
265   TEST_MATCH("??x", "\xc3\x84x", FALSE);
266   TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
267   TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
268   TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
269   TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
270   TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
271   TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
272   TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
273   TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
274   TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
275   TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
276   TEST_MATCH("", "", TRUE);
277   TEST_MATCH("", "abc", FALSE);
278   
279   verbose ("\n%u tests passed, %u failed\n", passed, failed);
280
281   return failed;
282 }
283
284