1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
35 static gboolean any_failed = FALSE;
36 static gboolean failed = FALSE;
38 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
41 g_print ("(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
43 g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
49 #define TEST_FAILED(message) \
50 G_STMT_START { g_print ("Error: "); g_print message; g_print ("\n"); any_failed = TRUE; } G_STMT_END
52 #define GLIB_TEST_STRING "el dorado "
55 strv_check (gchar **strv, ...)
61 va_start (list, strv);
64 const gchar *str = va_arg (list, const char *);
72 else if (strcmp (strv[i], str) != 0)
84 test_isalnum (gchar c)
86 return g_ascii_isalnum (c);
90 test_isalpha (gchar c)
92 return g_ascii_isalpha (c);
96 test_iscntrl (gchar c)
98 return g_ascii_iscntrl (c);
102 test_isdigit (gchar c)
104 return g_ascii_isdigit (c);
108 test_isgraph (gchar c)
110 return g_ascii_isgraph (c);
114 test_islower (gchar c)
116 return g_ascii_islower (c);
120 test_isprint (gchar c)
122 return g_ascii_isprint (c);
126 test_ispunct (gchar c)
128 return g_ascii_ispunct (c);
132 test_isspace (gchar c)
134 return g_ascii_isspace (c);
138 test_isupper (gchar c)
140 return g_ascii_isupper (c);
144 test_isxdigit (gchar c)
146 return g_ascii_isxdigit (c);
152 test_is_function (const char *name,
153 gboolean (* ascii_function) (gchar),
154 int (* c_library_function) (int),
155 gboolean (* unicode_function) (gunichar))
159 for (c = 0; c <= 0x7F; c++)
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));
171 for (c = 0x80; c <= 0xFF; c++)
173 gboolean ascii_result = ascii_function ((gchar)c);
175 TEST_FAILED (("g_ascii_%s returned TRUE for 0x%X",
181 test_to_function (const char *name,
182 gchar (* ascii_function) (gchar),
183 int (* c_library_function) (int),
184 gunichar (* unicode_function) (gunichar))
188 for (c = 0; c <= 0x7F; c++)
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));
200 for (c = 0x80; c <= 0xFF; c++)
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));
210 test_digit_function (const char *name,
211 int (* ascii_function) (gchar),
212 int (* unicode_function) (gunichar))
216 for (c = 0; c <= 0x7F; c++)
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));
224 for (c = 0x80; c <= 0xFF; c++)
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));
238 gchar *vec[] = { "Foo", "Bar", NULL };
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);
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);
262 string = g_strconcat (GLIB_TEST_STRING, NULL);
263 TEST (NULL, string != NULL);
264 TEST (NULL, strcmp (string, GLIB_TEST_STRING) == 0);
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);
274 string = g_strdup_printf ("%05d %-5s", 21, "test");
275 TEST (NULL, string != NULL);
276 TEST (NULL, strcmp(string, "00021 test ") == 0);
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);
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);
294 TEST (NULL, strcmp (g_strstr_len ("FooBarFooBarFoo", 6, "Bar"),
295 "BarFooBarFoo") == 0);
296 TEST (NULL, strcmp (g_strrstr ("FooBarFooBarFoo", "Bar"),
298 TEST (NULL, strcmp (g_strrstr_len ("FooBarFooBarFoo", 14, "BarFoo"),
299 "BarFooBarFoo") == 0);
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));
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));
327 #define TEST_IS(name) test_is_function (#name, test_##name, name, g_unichar_##name)
343 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
350 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)