Fix non-Intel/-Alpha version of the G_BREAKPOINT macro to include
[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 #include <string.h>
21
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 struct _GPatternSpec
35 {
36   GMatchType match_type;
37   guint      pattern_length;
38   gchar     *pattern;
39 };
40
41
42 static gchar *
43 match_type_name (GMatchType match_type)
44 {
45   switch (match_type)
46     {
47     case G_MATCH_ALL: 
48       return "G_MATCH_ALL";
49       break;
50     case G_MATCH_ALL_TAIL:
51       return "G_MATCH_ALL_TAIL";
52       break;
53     case G_MATCH_HEAD:
54       return "G_MATCH_HEAD";
55       break;
56     case G_MATCH_TAIL:
57       return "G_MATCH_TAIL";
58       break;
59     case G_MATCH_EXACT:
60       return "G_MATCH_EXACT";
61       break;
62     default:
63       return "unknown GMatchType";
64       break;
65     }
66 }
67
68 static gboolean
69 test_compilation (gchar *src, 
70                   GMatchType match_type, 
71                   gchar *pattern)
72 {
73   GPatternSpec *spec; 
74
75   g_print ("compiling \"%s\" \t", src);
76   spec = g_pattern_spec_new (src);
77   
78   if (spec->match_type != match_type)
79     {
80       g_print ("failed \t(match_type: %s, expected %s)\n",
81                match_type_name (spec->match_type), 
82                match_type_name (match_type));
83       return FALSE;
84     }
85   
86   if (strcmp (spec->pattern, pattern) != 0)
87     {
88       g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
89                spec->pattern,
90                pattern);
91       return FALSE;
92     }
93   
94   if (spec->pattern_length != strlen (spec->pattern))
95     {
96       g_print ("failed \t(pattern_length: %d, expected %d)\n",
97                spec->pattern_length,
98                strlen (spec->pattern));
99       return FALSE;
100     }
101   
102   g_print ("passed (%s: \"%s\")\n",
103            match_type_name (spec->match_type),
104            spec->pattern);
105   
106   return TRUE;
107 }
108
109 static gboolean
110 test_match (gchar *pattern, 
111             gchar *string, 
112             gboolean match)
113 {
114   g_print ("matching \"%s\" against \"%s\" \t", string, pattern);
115   
116   if (g_pattern_match_simple (pattern, string) != match)
117     {
118       g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
119       return FALSE;
120     }
121   
122   g_print ("passed (%s)\n", match ? "match" : "nomatch");
123   return TRUE;
124 }
125
126 static gboolean
127 test_equal (gchar *pattern1,
128             gchar *pattern2,
129             gboolean expected)
130 {
131   GPatternSpec *p1 = g_pattern_spec_new (pattern1);
132   GPatternSpec *p2 = g_pattern_spec_new (pattern2);
133   gboolean equal = g_pattern_spec_equal (p1, p2);
134
135   g_print ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
136
137   if (expected != equal)
138     {
139       g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
140                match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
141                expected ? "!=" : "==",
142                match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
143     }
144   else
145     g_print ("passed (%s)\n", equal ? "equal" : "unequal");
146   
147   g_pattern_spec_free (p1);
148   g_pattern_spec_free (p2);
149
150   return expected == equal;
151 }
152
153 #define TEST_COMPILATION(src, type, pattern) { \
154   total++; \
155   if (test_compilation (src, type, pattern)) \
156     passed++; \
157   else \
158     failed++; \
159 }
160
161 #define TEST_MATCH(pattern, string, match) { \
162   total++; \
163   if (test_match (pattern, string, match)) \
164     passed++; \
165   else \
166     failed++; \
167 }
168
169 #define TEST_EQUAL(pattern1, pattern2, match) { \
170   total++; \
171   if (test_equal (pattern1, pattern2, match)) \
172     passed++; \
173   else \
174     failed++; \
175 }
176
177 int
178 main (int argc, char** argv)
179 {
180   gint total = 0;
181   gint passed = 0;
182   gint failed = 0;
183   gchar *string;
184
185   TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*");
186   TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA");
187   TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH");
188   TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*?*??*DEF*GH");
189   TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*");
190   TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD");
191   TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "DCBA");
192   TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE");
193
194   TEST_EQUAL ("*A?B*", "*A?B*", TRUE);
195   TEST_EQUAL ("A*BCD", "A*BCD", TRUE);
196   TEST_EQUAL ("ABCD*", "ABCD****", TRUE);
197   TEST_EQUAL ("A1*", "A1*", TRUE);
198   TEST_EQUAL ("*YZ", "*YZ", TRUE);
199   TEST_EQUAL ("A1x", "A1x", TRUE);
200   TEST_EQUAL ("AB*CD", "AB**CD", TRUE);
201   TEST_EQUAL ("AB*CD", "AB*?*CD", FALSE);
202   TEST_EQUAL ("ABC*", "ABC?", FALSE);
203
204   TEST_MATCH("*x", "x", TRUE);
205   TEST_MATCH("*x", "xx", TRUE);
206   TEST_MATCH("*x", "yyyx", TRUE);
207   TEST_MATCH("*x", "yyxy", FALSE);
208   TEST_MATCH("?x", "x", FALSE);
209   TEST_MATCH("?x", "xx", TRUE);
210   TEST_MATCH("?x", "yyyx", FALSE);
211   TEST_MATCH("?x", "yyxy", FALSE);
212   TEST_MATCH("*?x", "xx", TRUE);
213   TEST_MATCH("?*x", "xx", TRUE);
214   TEST_MATCH("*?x", "x", FALSE);
215   TEST_MATCH("?*x", "x", FALSE);
216   TEST_MATCH("*?*x", "yx", TRUE);
217   TEST_MATCH("*?*x", "xxxx", TRUE);
218
219   string = g_convert ("Äx", -1, "UTF-8", "Latin1", NULL, NULL, NULL);
220   TEST_MATCH("*x", string, TRUE);
221   TEST_MATCH("?x", string, TRUE);
222   TEST_MATCH("??x", string, FALSE);
223   
224   g_print ("\n%u tests passed, %u failed\n", passed, failed);
225
226   return 0;
227 }