Add MSVC-specific text by Hans Breuer.
[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 str_check (gchar *str,
85            gchar *expected)
86 {
87   gboolean ok = (strcmp (str, expected) == 0);
88
89   g_free (str);
90
91   return ok;
92 }
93
94 #define FOR_ALL_CTYPE(macro)    \
95         macro(isalnum)          \
96         macro(isalpha)          \
97         macro(iscntrl)          \
98         macro(isdigit)          \
99         macro(isgraph)          \
100         macro(islower)          \
101         macro(isprint)          \
102         macro(ispunct)          \
103         macro(isspace)          \
104         macro(isupper)          \
105         macro(isxdigit)
106
107 #define DEFINE_CALL_CTYPE(function)             \
108         static int                              \
109         call_##function (int c)                 \
110         {                                       \
111                 return function (c);            \
112         }
113
114 #define DEFINE_CALL_G_ASCII_CTYPE(function)     \
115         static gboolean                         \
116         call_g_ascii_##function (gchar c)       \
117         {                                       \
118                 return g_ascii_##function (c);  \
119         }
120
121 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
122 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
123
124 static void
125 test_is_function (const char *name,
126                   gboolean (* ascii_function) (gchar),
127                   int (* c_library_function) (int),
128                   gboolean (* unicode_function) (gunichar))
129 {
130   int c;
131
132   for (c = 0; c <= 0x7F; c++)
133     {
134       gboolean ascii_result = ascii_function ((gchar)c);
135       gboolean c_library_result = c_library_function (c) != 0;
136       gboolean unicode_result = unicode_function ((gunichar) c);
137       if (ascii_result != c_library_result && c != '\v')
138         TEST_FAILED (("g_ascii_%s returned %d and %s returned %d for 0x%X",
139                       name, ascii_result, name, c_library_result, c));
140       if (ascii_result != unicode_result)
141         TEST_FAILED (("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
142                       name, ascii_result, name, unicode_result, c));
143     }
144   for (c = 0x80; c <= 0xFF; c++)
145     {
146       gboolean ascii_result = ascii_function ((gchar)c);
147       if (ascii_result)
148         TEST_FAILED (("g_ascii_%s returned TRUE for 0x%X",
149                       name, c));
150     }
151 }
152
153 static void
154 test_to_function (const char *name,
155                   gchar (* ascii_function) (gchar),
156                   int (* c_library_function) (int),
157                   gunichar (* unicode_function) (gunichar))
158 {
159   int c;
160
161   for (c = 0; c <= 0x7F; c++)
162     {
163       int ascii_result = (guchar) ascii_function ((gchar) c);
164       int c_library_result = c_library_function (c);
165       int unicode_result = unicode_function ((gunichar) c);
166       if (ascii_result != c_library_result)
167         TEST_FAILED (("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
168                       name, ascii_result, name, c_library_result, c));
169       if (ascii_result != unicode_result)
170         TEST_FAILED (("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
171                       name, ascii_result, name, unicode_result, c));
172     }
173   for (c = 0x80; c <= 0xFF; c++)
174     {
175       int ascii_result = (guchar) ascii_function ((gchar) c);
176       if (ascii_result != c)
177         TEST_FAILED (("g_ascii_%s returned 0x%X for 0x%X",
178                       name, ascii_result, c));
179     }
180 }
181
182 static void
183 test_digit_function (const char *name,
184                      int (* ascii_function) (gchar),
185                      int (* unicode_function) (gunichar))
186 {
187   int c;
188
189   for (c = 0; c <= 0x7F; c++)
190     {
191       int ascii_result = ascii_function ((gchar) c);
192       int unicode_result = unicode_function ((gunichar) c);
193       if (ascii_result != unicode_result)
194         TEST_FAILED (("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
195                       name, ascii_result, name, unicode_result, c));
196     }
197   for (c = 0x80; c <= 0xFF; c++)
198     {
199       int ascii_result = ascii_function ((gchar) c);
200       if (ascii_result != -1)
201         TEST_FAILED (("g_ascii_%s_value returned %d for 0x%X",
202                       name, ascii_result, c));
203     }
204 }
205
206 int
207 main (int   argc,
208       char *argv[])
209 {
210   gchar *string;
211   gchar *vec[] = { "Foo", "Bar", NULL };
212   gchar **copy;
213   
214   TEST (NULL, g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
215   TEST (NULL, g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
216   TEST (NULL, g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
217   TEST (NULL, g_ascii_strcasecmp ("FROBOZZ", "froboz") != 0);
218   TEST (NULL, g_ascii_strcasecmp ("", "") == 0);
219   TEST (NULL, g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
220   TEST (NULL, g_ascii_strcasecmp ("a", "b") < 0);
221   TEST (NULL, g_ascii_strcasecmp ("a", "B") < 0);
222   TEST (NULL, g_ascii_strcasecmp ("A", "b") < 0);
223   TEST (NULL, g_ascii_strcasecmp ("A", "B") < 0);
224   TEST (NULL, g_ascii_strcasecmp ("b", "a") > 0);
225   TEST (NULL, g_ascii_strcasecmp ("b", "A") > 0);
226   TEST (NULL, g_ascii_strcasecmp ("B", "a") > 0);
227   TEST (NULL, g_ascii_strcasecmp ("B", "A") > 0);
228
229   TEST (NULL, g_strdup (NULL) == NULL);
230   string = g_strdup (GLIB_TEST_STRING);
231   TEST (NULL, string != NULL);
232   TEST (NULL, strcmp (string, GLIB_TEST_STRING) == 0);
233   g_free(string);
234   
235   string = g_strconcat (GLIB_TEST_STRING, NULL);
236   TEST (NULL, string != NULL);
237   TEST (NULL, strcmp (string, GLIB_TEST_STRING) == 0);
238   g_free(string);
239   
240   string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING, 
241                         GLIB_TEST_STRING, NULL);
242   TEST (NULL, string != NULL);
243   TEST (NULL, strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
244                       GLIB_TEST_STRING) == 0);
245   g_free(string);
246   
247   string = g_strdup_printf ("%05d %-5s", 21, "test");
248   TEST (NULL, string != NULL);
249   TEST (NULL, strcmp(string, "00021 test ") == 0);
250   g_free (string);
251   
252   TEST (NULL, strcmp
253         (g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z"),
254          "abc\\\"\b\f\n\r\t\003\177\234\313\12345z") == 0);
255   TEST (NULL, strcmp (g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313", NULL),
256                       "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313") == 0);
257   TEST (NULL, strcmp(g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313",
258                                  "\b\f\001\002\003\004"),
259                      "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313") == 0);
260
261   copy = g_strdupv (vec);
262   TEST (NULL, strcmp (copy[0], "Foo") == 0);
263   TEST (NULL, strcmp (copy[1], "Bar") == 0);
264   TEST (NULL, copy[2] == NULL);
265   g_strfreev (copy);
266   
267   TEST (NULL, strcmp (g_strstr_len ("FooBarFooBarFoo", 6, "Bar"),
268                       "BarFooBarFoo") == 0);
269   TEST (NULL, strcmp (g_strrstr ("FooBarFooBarFoo", "Bar"),
270                       "BarFoo") == 0);
271   TEST (NULL, strcmp (g_strrstr_len ("FooBarFooBarFoo", 14, "BarFoo"),
272                       "BarFooBarFoo") == 0);
273
274   TEST (NULL, strv_check (g_strsplit ("", ",", 0), NULL));
275   TEST (NULL, strv_check (g_strsplit ("x", ",", 0), "x", NULL));
276   TEST (NULL, strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL));
277   TEST (NULL, strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL));
278   TEST (NULL, strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL));
279   TEST (NULL, strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL));
280   TEST (NULL, strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL));
281   TEST (NULL, strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL));
282   TEST (NULL, strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL));
283   TEST (NULL, strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL));
284   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL));
285   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL));
286
287   TEST (NULL, strv_check (g_strsplit ("", ",", 1), NULL));
288   TEST (NULL, strv_check (g_strsplit ("x", ",", 1), "x", NULL));
289   TEST (NULL, strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL));
290   TEST (NULL, strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL));
291   TEST (NULL, strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL));
292   TEST (NULL, strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL));
293   TEST (NULL, strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL));
294   TEST (NULL, strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL));
295   TEST (NULL, strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL));
296   TEST (NULL, strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL));
297   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL));
298   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL));
299
300   TEST (NULL, strv_check (g_strsplit ("", ",", 2), NULL));
301   TEST (NULL, strv_check (g_strsplit ("x", ",", 2), "x", NULL));
302   TEST (NULL, strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL));
303   TEST (NULL, strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL));
304   TEST (NULL, strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL));
305   TEST (NULL, strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL));
306   TEST (NULL, strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL));
307   TEST (NULL, strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL));
308   TEST (NULL, strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL));
309   TEST (NULL, strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL));
310   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL));
311   TEST (NULL, strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL));
312
313   #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
314
315   FOR_ALL_CTYPE(TEST_IS)
316
317   #undef TEST_IS
318
319   #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
320
321   TEST_TO (tolower);
322   TEST_TO (toupper);
323
324   #undef TEST_TO
325
326   #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
327
328   TEST_DIGIT (digit);
329   TEST_DIGIT (xdigit);
330
331   #undef TEST_DIGIT
332
333   /* Tests for g_build_path, g_build_filename */
334
335   TEST (NULL, str_check (g_build_path ("", NULL), ""));
336   TEST (NULL, str_check (g_build_path ("", "", NULL), ""));
337   TEST (NULL, str_check (g_build_path ("", "x", NULL), "x"));
338   TEST (NULL, str_check (g_build_path ("", "x", "y",  NULL), "xy"));
339   TEST (NULL, str_check (g_build_path ("", "x", "y", "z", NULL), "xyz"));
340
341   TEST (NULL, str_check (g_build_path (":", NULL), ""));
342   TEST (NULL, str_check (g_build_path (":", ":", NULL), ":"));
343   TEST (NULL, str_check (g_build_path (":", ":x", NULL), ":x"));
344   TEST (NULL, str_check (g_build_path (":", "x:", NULL), "x:"));
345   TEST (NULL, str_check (g_build_path (":", "x", "y",  NULL), "x:y"));
346   TEST (NULL, str_check (g_build_path (":", ":x", "y", NULL), ":x:y"));
347   TEST (NULL, str_check (g_build_path (":", "x", "y:", NULL), "x:y:"));
348   TEST (NULL, str_check (g_build_path (":", ":x:", ":y:", NULL), ":x:y:"));
349   TEST (NULL, str_check (g_build_path (":", ":x::", "::y:", NULL), ":x:y:"));
350   TEST (NULL, str_check (g_build_path (":", "x", "y", "z", NULL), "x:y:z"));
351   TEST (NULL, str_check (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:"));
352   TEST (NULL, str_check (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::"));
353
354   TEST (NULL, str_check (g_build_path ("::", NULL), ""));
355   TEST (NULL, str_check (g_build_path ("::", "::", NULL), "::"));
356   TEST (NULL, str_check (g_build_path ("::", "::x", NULL), "::x"));
357   TEST (NULL, str_check (g_build_path ("::", "x::", NULL), "x::"));
358   TEST (NULL, str_check (g_build_path ("::", "x", "y",  NULL), "x::y"));
359   TEST (NULL, str_check (g_build_path ("::", "::x", "y", NULL), "::x::y"));
360   TEST (NULL, str_check (g_build_path ("::", "x", "y::", NULL), "x::y::"));
361   TEST (NULL, str_check (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::"));
362   TEST (NULL, str_check (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::"));
363   TEST (NULL, str_check (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::"));
364   TEST (NULL, str_check (g_build_path ("::", "x", "y", "z", NULL), "x::y::z"));
365   TEST (NULL, str_check (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::"));
366   TEST (NULL, str_check (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::"));
367   TEST (NULL, str_check (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::"));
368
369 #define S G_DIR_SEPARATOR_S
370
371   TEST (NULL, str_check (g_build_filename (NULL), ""));
372   TEST (NULL, str_check (g_build_filename (S, NULL), S));
373   TEST (NULL, str_check (g_build_filename (S"x", NULL), S"x"));
374   TEST (NULL, str_check (g_build_filename ("x"S, NULL), "x"S));
375   TEST (NULL, str_check (g_build_filename ("x", "y",  NULL), "x"S"y"));
376   TEST (NULL, str_check (g_build_filename (S"x", "y", NULL), S"x"S"y"));
377   TEST (NULL, str_check (g_build_filename ("x", "y"S, NULL), "x"S"y"S));
378   TEST (NULL, str_check (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S));
379   TEST (NULL, str_check (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S));
380   TEST (NULL, str_check (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z"));
381   TEST (NULL, str_check (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S));
382   TEST (NULL, str_check (g_build_filename (S S"x"S S, S S"y"S S, S S"z"S S, NULL), S S"x"S"y"S"z"S S));
383
384 #undef S
385
386   {
387     gchar buf[5];
388     
389     TEST (NULL, 3 == g_snprintf (buf, 0, "%s", "abc"));
390     TEST (NULL, 3 == g_snprintf (NULL,0, "%s", "abc"));
391     TEST (NULL, 3 == g_snprintf (buf, 5, "%s", "abc"));
392     TEST (NULL, 4 == g_snprintf (buf, 5, "%s", "abcd"));
393     TEST (NULL, 9 == g_snprintf (buf, 5, "%s", "abcdefghi"));
394   }
395
396   return any_failed;
397 }