Implement and document g_ascii_isxxx.
[platform/upstream/glib.git] / tests / strfunc-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
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 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_LOG_DOMAIN
28
29 #include <stdio.h>
30 #include <string.h>
31 #include "glib.h"
32 #include <stdarg.h>
33 #include <ctype.h>
34
35 static gboolean any_failed = FALSE;
36 static gboolean failed = FALSE;
37
38 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
39 if (failed) \
40   { if (!m) \
41       g_print ("(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
42     else \
43       g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
44     fflush (stdout); \
45     any_failed = TRUE; \
46   } \
47 } G_STMT_END
48
49 #define TEST_FAILED(message) \
50   G_STMT_START { g_print ("Error: "); g_print message; g_print ("\n"); any_failed = TRUE; } G_STMT_END
51
52 #define GLIB_TEST_STRING "el dorado "
53
54 static gboolean
55 strv_check (gchar **strv, ...)
56 {
57   gboolean ok = TRUE;
58   gint i = 0;
59   va_list list;
60
61   va_start (list, strv);
62   while (ok)
63     {
64       const gchar *str = va_arg (list, const char *);
65       if (strv[i] == NULL)
66         {
67           ok = str == NULL;
68           break;
69         }
70       if (str == NULL)
71         ok = FALSE;
72       else if (strcmp (strv[i], str) != 0)
73         ok = FALSE;
74       i++;
75     }
76   va_end (list);
77
78   g_strfreev (strv);
79
80   return ok;
81 }
82
83 static gboolean
84 test_isalnum (gchar c)
85 {
86   return g_ascii_isalnum (c);
87 }
88
89 static gboolean
90 test_isalpha (gchar c)
91 {
92   return g_ascii_isalpha (c);
93 }
94
95 static gboolean
96 test_iscntrl (gchar c)
97 {
98   return g_ascii_iscntrl (c);
99 }
100
101 static gboolean
102 test_isdigit (gchar c)
103 {
104   return g_ascii_isdigit (c);
105 }
106
107 static gboolean
108 test_isgraph (gchar c)
109 {
110   return g_ascii_isgraph (c);
111 }
112
113 static gboolean
114 test_islower (gchar c)
115 {
116   return g_ascii_islower (c);
117 }
118
119 static gboolean
120 test_isprint (gchar c)
121 {
122   return g_ascii_isprint (c);
123 }
124
125 static gboolean
126 test_ispunct (gchar c)
127 {
128   return g_ascii_ispunct (c);
129 }
130
131 static gboolean
132 test_isspace (gchar c)
133 {
134   return g_ascii_isspace (c);
135 }
136
137 static gboolean
138 test_isupper (gchar c)
139 {
140   return g_ascii_isupper (c);
141 }
142
143 static gboolean
144 test_isxdigit (gchar c)
145 {
146   return g_ascii_isxdigit (c);
147 }
148
149
150
151 static void
152 test_is_function (const char *name,
153                   gboolean (* ascii_function) (gchar),
154                   int (* c_library_function) (int),
155                   gboolean (* unicode_function) (gunichar))
156 {
157   int c;
158
159   for (c = 0; c <= 0x7F; c++)
160     {
161       gboolean ascii_result = ascii_function ((gchar)c);
162       gboolean c_library_result = c_library_function (c) != 0;
163       gboolean unicode_result = unicode_function ((gunichar) c);
164       if (ascii_result != c_library_result && c != '\v')
165         TEST_FAILED (("g_ascii_%s returned %d and %s returned %d for 0x%X",
166                       name, ascii_result, name, c_library_result, c));
167       if (ascii_result != unicode_result)
168         TEST_FAILED (("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
169                       name, ascii_result, name, unicode_result, c));
170     }
171   for (c = 0x80; c <= 0xFF; c++)
172     {
173       gboolean ascii_result = ascii_function ((gchar)c);
174       if (ascii_result)
175         TEST_FAILED (("g_ascii_%s returned TRUE for 0x%X",
176                       name, c));
177     }
178 }
179
180 static void
181 test_to_function (const char *name,
182                   gchar (* ascii_function) (gchar),
183                   int (* c_library_function) (int),
184                   gunichar (* unicode_function) (gunichar))
185 {
186   int c;
187
188   for (c = 0; c <= 0x7F; c++)
189     {
190       int ascii_result = (guchar) ascii_function ((gchar) c);
191       int c_library_result = c_library_function (c);
192       int unicode_result = unicode_function ((gunichar) c);
193       if (ascii_result != c_library_result)
194         TEST_FAILED (("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
195                       name, ascii_result, name, c_library_result, c));
196       if (ascii_result != unicode_result)
197         TEST_FAILED (("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
198                       name, ascii_result, name, unicode_result, c));
199     }
200   for (c = 0x80; c <= 0xFF; c++)
201     {
202       int ascii_result = (guchar) ascii_function ((gchar) c);
203       if (ascii_result != c)
204         TEST_FAILED (("g_ascii_%s returned 0x%X for 0x%X",
205                       name, ascii_result, c));
206     }
207 }
208
209 static void
210 test_digit_function (const char *name,
211                      int (* ascii_function) (gchar),
212                      int (* unicode_function) (gunichar))
213 {
214   int c;
215
216   for (c = 0; c <= 0x7F; c++)
217     {
218       int ascii_result = ascii_function ((gchar) c);
219       int unicode_result = unicode_function ((gunichar) c);
220       if (ascii_result != unicode_result)
221         TEST_FAILED (("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
222                       name, ascii_result, name, unicode_result, c));
223     }
224   for (c = 0x80; c <= 0xFF; c++)
225     {
226       int ascii_result = ascii_function ((gchar) c);
227       if (ascii_result != -1)
228         TEST_FAILED (("g_ascii_%s_value returned %d for 0x%X",
229                       name, ascii_result, c));
230     }
231 }
232
233 int
234 main (int   argc,
235       char *argv[])
236 {
237   gchar *string;
238   gchar *vec[] = { "Foo", "Bar", NULL };
239   gchar **copy;
240   
241   TEST (NULL, g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
242   TEST (NULL, g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
243   TEST (NULL, g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
244   TEST (NULL, g_ascii_strcasecmp ("FROBOZZ", "froboz") != 0);
245   TEST (NULL, g_ascii_strcasecmp ("", "") == 0);
246   TEST (NULL, g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
247   TEST (NULL, g_ascii_strcasecmp ("a", "b") < 0);
248   TEST (NULL, g_ascii_strcasecmp ("a", "B") < 0);
249   TEST (NULL, g_ascii_strcasecmp ("A", "b") < 0);
250   TEST (NULL, g_ascii_strcasecmp ("A", "B") < 0);
251   TEST (NULL, g_ascii_strcasecmp ("b", "a") > 0);
252   TEST (NULL, g_ascii_strcasecmp ("b", "A") > 0);
253   TEST (NULL, g_ascii_strcasecmp ("B", "a") > 0);
254   TEST (NULL, g_ascii_strcasecmp ("B", "A") > 0);
255
256   TEST (NULL, g_strdup (NULL) == NULL);
257   string = g_strdup (GLIB_TEST_STRING);
258   TEST (NULL, string != NULL);
259   TEST (NULL, strcmp (string, GLIB_TEST_STRING) == 0);
260   g_free(string);
261   
262   string = g_strconcat (GLIB_TEST_STRING, NULL);
263   TEST (NULL, string != NULL);
264   TEST (NULL, strcmp (string, GLIB_TEST_STRING) == 0);
265   g_free(string);
266   
267   string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING, 
268                         GLIB_TEST_STRING, NULL);
269   TEST (NULL, string != NULL);
270   TEST (NULL, strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
271                       GLIB_TEST_STRING) == 0);
272   g_free(string);
273   
274   string = g_strdup_printf ("%05d %-5s", 21, "test");
275   TEST (NULL, string != NULL);
276   TEST (NULL, strcmp(string, "00021 test ") == 0);
277   g_free (string);
278   
279   TEST (NULL, strcmp
280         (g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z"),
281          "abc\\\"\b\f\n\r\t\003\177\234\313\12345z") == 0);
282   TEST (NULL, strcmp (g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313", NULL),
283                       "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313") == 0);
284   TEST (NULL, strcmp(g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313",
285                                  "\b\f\001\002\003\004"),
286                      "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313") == 0);
287
288   copy = g_strdupv (vec);
289   TEST (NULL, strcmp (copy[0], "Foo") == 0);
290   TEST (NULL, strcmp (copy[1], "Bar") == 0);
291   TEST (NULL, copy[2] == NULL);
292   g_strfreev (copy);
293   
294   TEST (NULL, strcmp (g_strstr_len ("FooBarFooBarFoo", 6, "Bar"),
295                       "BarFooBarFoo") == 0);
296   TEST (NULL, strcmp (g_strrstr ("FooBarFooBarFoo", "Bar"),
297                       "BarFoo") == 0);
298   TEST (NULL, strcmp (g_strrstr_len ("FooBarFooBarFoo", 14, "BarFoo"),
299                       "BarFooBarFoo") == 0);
300
301   TEST (NULL, strv_check (g_strsplit ("", ",", 0), NULL));
302   TEST (NULL, strv_check (g_strsplit ("x", ",", 0), "x", NULL));
303   TEST (NULL, strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL));
304   TEST (NULL, strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL));
305   TEST (NULL, strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL));
306   TEST (NULL, strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL));
307   TEST (NULL, strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL));
308   TEST (NULL, strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL));
309   TEST (NULL, strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL));
310   TEST (NULL, strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL));
311   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL));
312   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL));
313
314   TEST (NULL, strv_check (g_strsplit ("", ",", 2), NULL));
315   TEST (NULL, strv_check (g_strsplit ("x", ",", 2), "x", NULL));
316   TEST (NULL, strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL));
317   TEST (NULL, strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL));
318   TEST (NULL, strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL));
319   TEST (NULL, strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL));
320   TEST (NULL, strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL));
321   TEST (NULL, strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL));
322   TEST (NULL, strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL));
323   TEST (NULL, strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL));
324   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL));
325   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL));
326
327   #define TEST_IS(name) test_is_function (#name, test_##name, name, g_unichar_##name)
328
329   TEST_IS (isalnum);
330   TEST_IS (isalpha);
331   TEST_IS (iscntrl);
332   TEST_IS (isdigit);
333   TEST_IS (isgraph);
334   TEST_IS (islower);
335   TEST_IS (isprint);
336   TEST_IS (ispunct);
337   TEST_IS (isspace);
338   TEST_IS (isupper);
339   TEST_IS (isxdigit);
340
341   #undef TEST_IS
342
343   #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
344
345   TEST_TO (tolower);
346   TEST_TO (toupper);
347
348   #undef TEST_TO
349
350   #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
351
352   TEST_DIGIT (digit);
353   TEST_DIGIT (xdigit);
354
355   #undef TEST_DIGIT
356
357   return any_failed;
358 }