Remove redundant header inclusions
[glib.git] / glib / tests / strfuncs.c
blob66768e4e637e8e3596db914ab2d50717f4eab26b
1 /* Unit tests for gstrfuncs
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #define _XOPEN_SOURCE
23 #include <ctype.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <math.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "glib.h"
33 #define GLIB_TEST_STRING "el dorado "
35 #define FOR_ALL_CTYPE(macro) \
36 macro(isalnum) \
37 macro(isalpha) \
38 macro(iscntrl) \
39 macro(isdigit) \
40 macro(isgraph) \
41 macro(islower) \
42 macro(isprint) \
43 macro(ispunct) \
44 macro(isspace) \
45 macro(isupper) \
46 macro(isxdigit)
48 #define DEFINE_CALL_CTYPE(function) \
49 static int \
50 call_##function (int c) \
51 { \
52 return function (c); \
55 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
56 static gboolean \
57 call_g_ascii_##function (gchar c) \
58 { \
59 return g_ascii_##function (c); \
62 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
63 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
65 static void
66 test_is_function (const char *name,
67 gboolean (* ascii_function) (gchar),
68 int (* c_library_function) (int),
69 gboolean (* unicode_function) (gunichar))
71 int c;
73 for (c = 0; c <= 0x7F; c++)
75 gboolean ascii_result = ascii_function ((gchar)c);
76 gboolean c_library_result = c_library_function (c) != 0;
77 gboolean unicode_result = unicode_function ((gunichar) c);
78 if (ascii_result != c_library_result && c != '\v')
80 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
81 name, ascii_result, name, c_library_result, c);
83 if (ascii_result != unicode_result)
85 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
86 name, ascii_result, name, unicode_result, c);
89 for (c = 0x80; c <= 0xFF; c++)
91 gboolean ascii_result = ascii_function ((gchar)c);
92 if (ascii_result)
94 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
99 static void
100 test_to_function (const char *name,
101 gchar (* ascii_function) (gchar),
102 int (* c_library_function) (int),
103 gunichar (* unicode_function) (gunichar))
105 int c;
107 for (c = 0; c <= 0x7F; c++)
109 int ascii_result = (guchar) ascii_function ((gchar) c);
110 int c_library_result = c_library_function (c);
111 int unicode_result = unicode_function ((gunichar) c);
112 if (ascii_result != c_library_result)
114 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
115 name, ascii_result, name, c_library_result, c);
117 if (ascii_result != unicode_result)
119 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
120 name, ascii_result, name, unicode_result, c);
123 for (c = 0x80; c <= 0xFF; c++)
125 int ascii_result = (guchar) ascii_function ((gchar) c);
126 if (ascii_result != c)
128 g_error ("g_ascii_%s returned 0x%X for 0x%X",
129 name, ascii_result, c);
134 static void
135 test_digit_function (const char *name,
136 int (* ascii_function) (gchar),
137 int (* unicode_function) (gunichar))
139 int c;
141 for (c = 0; c <= 0x7F; c++)
143 int ascii_result = ascii_function ((gchar) c);
144 int unicode_result = unicode_function ((gunichar) c);
145 if (ascii_result != unicode_result)
147 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
148 name, ascii_result, name, unicode_result, c);
151 for (c = 0x80; c <= 0xFF; c++)
153 int ascii_result = ascii_function ((gchar) c);
154 if (ascii_result != -1)
156 g_error ("g_ascii_%s_value returned %d for 0x%X",
157 name, ascii_result, c);
162 static void
163 test_is_to_digit (void)
165 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
167 FOR_ALL_CTYPE(TEST_IS)
169 #undef TEST_IS
171 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
173 TEST_TO (tolower);
174 TEST_TO (toupper);
176 #undef TEST_TO
178 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
180 TEST_DIGIT (digit);
181 TEST_DIGIT (xdigit);
183 #undef TEST_DIGIT
186 static void
187 test_strdup (void)
189 gchar *str;
191 str = g_strdup (NULL);
192 g_assert (str == NULL);
194 str = g_strdup (GLIB_TEST_STRING);
195 g_assert (str != NULL);
196 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
197 g_free (str);
200 static void
201 test_strndup (void)
203 gchar *str;
205 str = g_strndup (NULL, 3);
206 g_assert (str == NULL);
208 str = g_strndup ("aaaa", 5);
209 g_assert (str != NULL);
210 g_assert_cmpstr (str, ==, "aaaa");
211 g_free (str);
213 str = g_strndup ("aaaa", 2);
214 g_assert (str != NULL);
215 g_assert_cmpstr (str, ==, "aa");
216 g_free (str);
219 static void
220 test_strdup_printf (void)
222 gchar *str;
224 str = g_strdup_printf ("%05d %-5s", 21, "test");
225 g_assert (str != NULL);
226 g_assert_cmpstr (str, ==, "00021 test ");
227 g_free (str);
230 static void
231 test_strdupv (void)
233 gchar *vec[] = { "Foo", "Bar", NULL };
234 gchar **copy;
236 copy = g_strdupv (NULL);
237 g_assert (copy == NULL);
239 copy = g_strdupv (vec);
240 g_assert (copy != NULL);
241 g_assert_cmpstr (copy[0], ==, "Foo");
242 g_assert_cmpstr (copy[1], ==, "Bar");
243 g_assert (copy[2] == NULL);
244 g_strfreev (copy);
247 static void
248 test_strnfill (void)
250 gchar *str;
252 str = g_strnfill (0, 'a');
253 g_assert (str != NULL);
254 g_assert (*str == '\0');
255 g_free (str);
257 str = g_strnfill (5, 'a');
258 g_assert (str != NULL);
259 g_assert_cmpstr (str, ==, "aaaaa");
260 g_free (str);
263 static void
264 test_strconcat (void)
266 gchar *str;
268 str = g_strconcat (GLIB_TEST_STRING, NULL);
269 g_assert (str != NULL);
270 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
271 g_free (str);
273 str = g_strconcat (GLIB_TEST_STRING,
274 GLIB_TEST_STRING,
275 GLIB_TEST_STRING,
276 NULL);
277 g_assert (str != NULL);
278 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
279 g_free (str);
282 static void
283 test_strjoin (void)
285 gchar *str;
287 str = g_strjoin (NULL, NULL);
288 g_assert (str != NULL);
289 g_assert (*str == '\0');
290 g_free (str);
292 str = g_strjoin (":", NULL);
293 g_assert (str != NULL);
294 g_assert (*str == '\0');
295 g_free (str);
297 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
298 g_assert (str != NULL);
299 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
300 g_free (str);
302 str = g_strjoin (NULL,
303 GLIB_TEST_STRING,
304 GLIB_TEST_STRING,
305 GLIB_TEST_STRING,
306 NULL);
307 g_assert (str != NULL);
308 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
309 g_free (str);
311 str = g_strjoin (":",
312 GLIB_TEST_STRING,
313 GLIB_TEST_STRING,
314 GLIB_TEST_STRING,
315 NULL);
316 g_assert (str != NULL);
317 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
318 g_free (str);
321 static void
322 test_strcanon (void)
324 gchar *str;
326 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
328 str = g_strcanon (NULL, "ab", 'y');
330 g_test_trap_assert_failed ();
332 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
334 str = g_strdup ("abxabxab");
335 str = g_strcanon (str, NULL, 'y');
336 g_free (str);
338 g_test_trap_assert_failed ();
340 str = g_strdup ("abxabxab");
341 str = g_strcanon (str, "ab", 'y');
342 g_assert (str != NULL);
343 g_assert_cmpstr (str, ==, "abyabyab");
344 g_free (str);
347 static void
348 test_strcompress_strescape (void)
350 gchar *str;
351 gchar *tmp;
353 /* test compress */
354 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
356 str = g_strcompress (NULL);
358 g_test_trap_assert_failed ();
360 /* trailing slashes are not allowed */
361 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
363 str = g_strcompress ("abc\\");
365 g_test_trap_assert_failed ();
367 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
368 g_assert (str != NULL);
369 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
370 g_free (str);
372 /* test escape */
373 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
375 str = g_strescape (NULL, NULL);
377 g_test_trap_assert_failed ();
379 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
380 g_assert (str != NULL);
381 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
382 g_free (str);
384 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
385 "\b\f\001\002\003\004");
386 g_assert (str != NULL);
387 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
388 g_free (str);
390 /* round trip */
391 tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
392 str = g_strcompress (tmp);
393 g_assert (str != NULL);
394 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
395 g_free (str);
396 g_free (tmp);
399 static void
400 test_ascii_strcasecmp (void)
402 gboolean res;
404 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
406 res = g_ascii_strcasecmp ("foo", NULL);
408 g_test_trap_assert_failed ();
410 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
412 res = g_ascii_strcasecmp (NULL, "foo");
414 g_test_trap_assert_failed ();
416 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
417 g_assert_cmpint (res, ==, 0);
419 res = g_ascii_strcasecmp ("frobozz", "frobozz");
420 g_assert_cmpint (res, ==, 0);
422 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
423 g_assert_cmpint (res, ==, 0);
425 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
426 g_assert_cmpint (res, !=, 0);
428 res = g_ascii_strcasecmp ("", "");
429 g_assert_cmpint (res, ==, 0);
431 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
432 g_assert_cmpint (res, ==, 0);
434 res = g_ascii_strcasecmp ("a", "b");
435 g_assert_cmpint (res, <, 0);
437 res = g_ascii_strcasecmp ("a", "B");
438 g_assert_cmpint (res, <, 0);
440 res = g_ascii_strcasecmp ("A", "b");
441 g_assert_cmpint (res, <, 0);
443 res = g_ascii_strcasecmp ("A", "B");
444 g_assert_cmpint (res, <, 0);
446 res = g_ascii_strcasecmp ("b", "a");
447 g_assert_cmpint (res, >, 0);
449 res = g_ascii_strcasecmp ("b", "A");
450 g_assert_cmpint (res, >, 0);
452 res = g_ascii_strcasecmp ("B", "a");
453 g_assert_cmpint (res, >, 0);
455 res = g_ascii_strcasecmp ("B", "A");
456 g_assert_cmpint (res, >, 0);
459 static void
460 do_test_strchug (const gchar *str, const gchar *expected)
462 gchar *tmp;
463 gboolean res;
465 tmp = g_strdup (str);
467 g_strchug (tmp);
468 res = (strcmp (tmp, expected) == 0);
469 g_free (tmp);
471 g_assert_cmpint (res, ==, TRUE);
474 static void
475 test_strchug (void)
477 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
479 g_strchug (NULL);
481 g_test_trap_assert_failed ();
483 do_test_strchug ("", "");
484 do_test_strchug (" ", "");
485 do_test_strchug ("\t\r\n ", "");
486 do_test_strchug (" a", "a");
487 do_test_strchug (" a", "a");
488 do_test_strchug ("a a", "a a");
489 do_test_strchug (" a a", "a a");
492 static void
493 do_test_strchomp (const gchar *str, const gchar *expected)
495 gchar *tmp;
496 gboolean res;
498 tmp = g_strdup (str);
500 g_strchomp (tmp);
501 res = (strcmp (tmp, expected) == 0);
502 g_free (tmp);
504 g_assert_cmpint (res, ==, TRUE);
507 static void
508 test_strchomp (void)
510 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
512 g_strchomp (NULL);
514 g_test_trap_assert_failed ();
516 do_test_strchomp ("", "");
517 do_test_strchomp (" ", "");
518 do_test_strchomp (" \t\r\n", "");
519 do_test_strchomp ("a ", "a");
520 do_test_strchomp ("a ", "a");
521 do_test_strchomp ("a a", "a a");
522 do_test_strchomp ("a a ", "a a");
525 static void
526 test_strreverse (void)
528 gchar *str;
529 gchar *p;
531 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
533 str = g_strreverse (NULL);
535 g_test_trap_assert_failed ();
537 str = p = g_strdup ("abcde");
538 str = g_strreverse (str);
539 g_assert (str != NULL);
540 g_assert (p == str);
541 g_assert_cmpstr (str, ==, "edcba");
542 g_free (str);
545 static void
546 test_strstr (void)
548 gchar *haystack;
549 gchar *res;
551 haystack = g_strdup ("FooBarFooBarFoo");
553 /* strstr_len */
554 res = g_strstr_len (haystack, 6, "xxx");
555 g_assert (res == NULL);
557 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
558 g_assert (res == NULL);
560 res = g_strstr_len (haystack, 3, "Bar");
561 g_assert (res == NULL);
563 res = g_strstr_len (haystack, 6, "");
564 g_assert (res == haystack);
565 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
567 res = g_strstr_len (haystack, 6, "Bar");
568 g_assert (res == haystack + 3);
569 g_assert_cmpstr (res, ==, "BarFooBarFoo");
571 res = g_strstr_len (haystack, -1, "Bar");
572 g_assert (res == haystack + 3);
573 g_assert_cmpstr (res, ==, "BarFooBarFoo");
575 /* strrstr */
576 res = g_strrstr (haystack, "xxx");
577 g_assert (res == NULL);
579 res = g_strrstr (haystack, "FooBarFooBarFooBar");
580 g_assert (res == NULL);
582 res = g_strrstr (haystack, "");
583 g_assert (res == haystack);
584 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
586 res = g_strrstr (haystack, "Bar");
587 g_assert (res == haystack + 9);
588 g_assert_cmpstr (res, ==, "BarFoo");
590 /* strrstr_len */
591 res = g_strrstr_len (haystack, 14, "xxx");
592 g_assert (res == NULL);
594 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
595 g_assert (res == NULL);
597 res = g_strrstr_len (haystack, 3, "Bar");
598 g_assert (res == NULL);
600 res = g_strrstr_len (haystack, 14, "BarFoo");
601 g_assert (res == haystack + 3);
602 g_assert_cmpstr (res, ==, "BarFooBarFoo");
604 res = g_strrstr_len (haystack, 15, "BarFoo");
605 g_assert (res == haystack + 9);
606 g_assert_cmpstr (res, ==, "BarFoo");
608 res = g_strrstr_len (haystack, -1, "BarFoo");
609 g_assert (res == haystack + 9);
610 g_assert_cmpstr (res, ==, "BarFoo");
612 /* test case for strings with \0 in the middle */
613 *(haystack + 7) = '\0';
614 res = g_strstr_len (haystack, 15, "BarFoo");
615 g_assert (res == NULL);
617 g_free (haystack);
620 static void
621 test_has_prefix (void)
623 gboolean res;
625 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
627 res = g_str_has_prefix ("foo", NULL);
629 g_test_trap_assert_failed ();
631 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
633 res = g_str_has_prefix (NULL, "foo");
635 g_test_trap_assert_failed ();
637 res = g_str_has_prefix ("foo", "bar");
638 g_assert_cmpint (res, ==, FALSE);
640 res = g_str_has_prefix ("foo", "foobar");
641 g_assert_cmpint (res, ==, FALSE);
643 res = g_str_has_prefix ("foobar", "bar");
644 g_assert_cmpint (res, ==, FALSE);
646 res = g_str_has_prefix ("foobar", "foo");
647 g_assert_cmpint (res, ==, TRUE);
649 res = g_str_has_prefix ("foo", "");
650 g_assert_cmpint (res, ==, TRUE);
652 res = g_str_has_prefix ("foo", "foo");
653 g_assert_cmpint (res, ==, TRUE);
655 res = g_str_has_prefix ("", "");
656 g_assert_cmpint (res, ==, TRUE);
659 static void
660 test_has_suffix (void)
662 gboolean res;
664 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
666 res = g_str_has_suffix ("foo", NULL);
668 g_test_trap_assert_failed ();
670 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
672 res = g_str_has_suffix (NULL, "foo");
674 g_test_trap_assert_failed ();
676 res = g_str_has_suffix ("foo", "bar");
677 g_assert_cmpint (res, ==, FALSE);
679 res = g_str_has_suffix ("bar", "foobar");
680 g_assert_cmpint (res, ==, FALSE);
682 res = g_str_has_suffix ("foobar", "foo");
683 g_assert_cmpint (res, ==, FALSE);
685 res = g_str_has_suffix ("foobar", "bar");
686 g_assert_cmpint (res, ==, TRUE);
688 res = g_str_has_suffix ("foo", "");
689 g_assert_cmpint (res, ==, TRUE);
691 res = g_str_has_suffix ("foo", "foo");
692 g_assert_cmpint (res, ==, TRUE);
694 res = g_str_has_suffix ("", "");
695 g_assert_cmpint (res, ==, TRUE);
698 static void
699 strv_check (gchar **strv, ...)
701 gboolean ok = TRUE;
702 gint i = 0;
703 va_list list;
705 va_start (list, strv);
706 while (ok)
708 const gchar *str = va_arg (list, const char *);
709 if (strv[i] == NULL)
711 g_assert (str == NULL);
712 break;
714 if (str == NULL)
716 ok = FALSE;
718 else
720 g_assert_cmpstr (strv[i], ==, str);
722 i++;
724 va_end (list);
726 g_strfreev (strv);
729 static void
730 test_strsplit (void)
732 strv_check (g_strsplit ("", ",", 0), NULL);
733 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
734 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
735 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
736 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
737 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
738 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
739 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
740 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
741 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
742 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
743 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
745 strv_check (g_strsplit ("", ",", 1), NULL);
746 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
747 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
748 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
749 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
750 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
751 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
752 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
753 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
754 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
755 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
756 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
758 strv_check (g_strsplit ("", ",", 2), NULL);
759 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
760 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
761 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
762 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
763 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
764 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
765 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
766 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
767 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
768 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
769 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
772 static void
773 test_strsplit_set (void)
775 strv_check (g_strsplit_set ("", ",/", 0), NULL);
776 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
777 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
778 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
779 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
781 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
782 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
783 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
784 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
785 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
786 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
787 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
788 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
790 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
791 strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
792 strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
793 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
794 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
795 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
797 strv_check (g_strsplit_set ("", ",", 0), NULL);
798 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
799 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
800 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
801 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
802 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
803 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
804 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
805 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
806 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
807 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
809 strv_check (g_strsplit_set ("", ",", 1), NULL);
810 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
811 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
812 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
813 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
814 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
815 strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
816 strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
817 strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
818 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
819 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
820 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
822 strv_check (g_strsplit_set ("", ",", 2), NULL);
823 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
824 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
825 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
826 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
827 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
828 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
829 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
830 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
831 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
832 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
834 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
837 static void
838 test_strv_length (void)
840 guint l;
842 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
844 l = g_strv_length (NULL);
846 g_test_trap_assert_failed ();
848 l = g_strv_length (g_strsplit ("1,2,3,4", ",", -1));
849 g_assert_cmpuint (l, ==, 4);
852 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
854 void
855 check_strtod_string (gchar *number,
856 double res,
857 gboolean check_end,
858 gint correct_len)
860 double d;
861 gint l;
862 gchar *dummy;
864 /* we try a copy of number, with some free space for malloc before that.
865 * This is supposed to smash the some wrong pointer calculations. */
867 dummy = g_malloc (100000);
868 number = g_strdup (number);
869 g_free (dummy);
871 for (l = 0; l < G_N_ELEMENTS (locales); l++)
873 gboolean ok;
874 gchar *end = "(unset)";
876 setlocale (LC_NUMERIC, locales[l]);
877 d = g_ascii_strtod (number, &end);
878 ok = isnan (res) ? isnan (d) : (d == res);
879 if (!ok)
881 g_error ("g_ascii_strtod on \"%s\" for locale %s failed\n" \
882 "expected %f (nan %d) actual %f (nan %d)\n",
883 number, locales[l],
884 res, isnan (res),
885 d, isnan (d));
888 ok = (end - number) == (check_end ? correct_len : strlen (number));
889 if (!ok) {
890 if (end == NULL)
891 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
892 number, locales[l]);
893 else if (end >= number && end <= number + strlen (number))
894 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
895 number, locales[l], end);
896 else
897 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
898 number, locales[l], number, end);
902 g_free (number);
905 static void
906 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
908 int l;
909 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
911 for (l = 0; l < G_N_ELEMENTS (locales); l++)
913 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
914 g_assert_cmpstr (buf, ==, str);
918 static void
919 test_strtod (void)
921 gdouble d, our_nan, our_inf;
922 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
924 #ifdef NAN
925 our_nan = NAN;
926 #else
927 /* Do this before any call to setlocale. */
928 our_nan = atof ("NaN");
929 #endif
930 g_assert (isnan (our_nan));
932 #ifdef INFINITY
933 our_inf = INFINITY;
934 #else
935 our_inf = atof ("Infinity");
936 #endif
937 g_assert (our_inf > 1 && our_inf == our_inf / 2);
939 check_strtod_string ("123.123", 123.123, FALSE, 0);
940 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
941 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
942 check_strtod_string ("-123.123", -123.123, FALSE, 0);
943 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
944 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
945 check_strtod_string ("5.4", 5.4, TRUE, 3);
946 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
947 check_strtod_string ("5,4", 5.0, TRUE, 1);
948 check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
949 check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
950 check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
951 check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
952 check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
953 /* the following are for #156421 */
954 check_strtod_string ("1e1", 1e1, FALSE, 0);
955 check_strtod_string ("NAN", our_nan, FALSE, 0);
956 check_strtod_string ("-nan", -our_nan, FALSE, 0);
957 check_strtod_string ("INF", our_inf, FALSE, 0);
958 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
959 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
961 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
962 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
964 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
965 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
967 d = pow (2.0, -1024.1);
968 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
970 d = -pow (2.0, -1024.1);
971 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
973 /* for #343899 */
974 check_strtod_string (" 0.75", 0.75, FALSE, 0);
975 check_strtod_string (" +0.75", 0.75, FALSE, 0);
976 check_strtod_string (" -0.75", -0.75, FALSE, 0);
977 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
978 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
979 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
980 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
982 #if 0
983 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
984 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
985 #endif
987 /* for #343899 */
988 check_strtod_number (0.75, "%0.2f", "0.75");
989 check_strtod_number (0.75, "%5.2f", " 0.75");
990 check_strtod_number (-0.75, "%0.2f", "-0.75");
991 check_strtod_number (-0.75, "%5.2f", "-0.75");
992 check_strtod_number (1e99, "%.0e", "1e+99");
995 static void
996 check_uint64 (const gchar *str,
997 const gchar *end,
998 gint base,
999 guint64 result,
1000 gint error)
1002 guint64 actual;
1003 gchar *endptr = NULL;
1004 gint err;
1006 errno = 0;
1007 actual = g_ascii_strtoull (str, &endptr, base);
1008 err = errno;
1010 g_assert (actual == result);
1011 g_assert_cmpstr (end, ==, endptr);
1012 g_assert (err == error);
1015 static void
1016 check_int64 (const gchar *str,
1017 const gchar *end,
1018 gint base,
1019 gint64 result,
1020 gint error)
1022 gint64 actual;
1023 gchar *endptr = NULL;
1024 gint err;
1026 errno = 0;
1027 actual = g_ascii_strtoll (str, &endptr, base);
1028 err = errno;
1030 g_assert (actual == result);
1031 g_assert_cmpstr (end, ==, endptr);
1032 g_assert (err == error);
1035 static void
1036 test_strtoll (void)
1038 check_uint64 ("0", "", 10, 0, 0);
1039 check_uint64 ("+0", "", 10, 0, 0);
1040 check_uint64 ("-0", "", 10, 0, 0);
1041 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1042 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1043 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1044 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1046 check_int64 ("0", "", 10, 0, 0);
1047 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1048 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1049 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1050 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1051 check_int64 ("32768", "", 10, 32768, 0);
1052 check_int64 ("-32768", "", 10, -32768, 0);
1053 check_int64 ("001", "", 10, 1, 0);
1054 check_int64 ("-001", "", 10, -1, 0);
1057 static void
1058 test_bounds (void)
1060 GMappedFile *file, *before, *after;
1061 char buffer[4097];
1062 char *tmp, *tmp2;
1063 char **array;
1064 char *string;
1066 /* if we allocate the file between two others and then free those
1067 * other two, then hopefully we end up with unmapped memory on either
1068 * side.
1070 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1072 /* quick workaround until #549783 can be fixed */
1073 if (before == NULL)
1074 return;
1076 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1077 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1078 g_mapped_file_free (before);
1079 g_mapped_file_free (after);
1081 g_assert (file != NULL);
1082 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1083 string = g_mapped_file_get_contents (file);
1085 /* ensure they're all non-nul */
1086 g_assert (memchr (string, '\0', 4096) == NULL);
1088 /* test set 1: ensure that nothing goes past its maximum length, even in
1089 * light of a missing nul terminator.
1091 * we try to test all of the 'n' functions here.
1093 tmp = g_strndup (string, 4096);
1094 g_assert_cmpint (strlen (tmp), ==, 4096);
1095 g_free (tmp);
1097 /* found no bugs in gnome, i hope :) */
1098 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1099 g_strstr_len (string, 4096, "B");
1100 g_strstr_len (string, 4096, ".");
1101 g_strstr_len (string, 4096, "");
1103 g_strrstr_len (string, 4096, "BUGS");
1104 g_strrstr_len (string, 4096, "B");
1105 g_strrstr_len (string, 4096, ".");
1106 g_strrstr_len (string, 4096, "");
1108 g_ascii_strdown (string, 4096);
1109 g_ascii_strdown (string, 4096);
1110 g_ascii_strup (string, 4096);
1111 g_ascii_strup (string, 4096);
1113 g_ascii_strncasecmp (string, string, 4096);
1115 tmp = g_markup_escape_text (string, 4096);
1116 g_free (tmp);
1118 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1120 g_assert_cmpint (string[4095], ==, '\n');
1121 string[4095] = '\0';
1123 tmp = g_strdup (string);
1124 g_assert_cmpint (strlen (tmp), ==, 4095);
1125 g_free (tmp);
1127 tmp = g_strndup (string, 10000);
1128 g_assert_cmpint (strlen (tmp), ==, 4095);
1129 g_free (tmp);
1131 g_stpcpy (buffer, string);
1132 g_assert_cmpint (strlen (buffer), ==, 4095);
1134 g_strstr_len (string, 10000, "BUGS");
1135 g_strstr_len (string, 10000, "B");
1136 g_strstr_len (string, 10000, ".");
1137 g_strstr_len (string, 10000, "");
1139 g_strrstr (string, "BUGS");
1140 g_strrstr (string, "B");
1141 g_strrstr (string, ".");
1142 g_strrstr (string, "");
1144 g_strrstr_len (string, 10000, "BUGS");
1145 g_strrstr_len (string, 10000, "B");
1146 g_strrstr_len (string, 10000, ".");
1147 g_strrstr_len (string, 10000, "");
1149 g_str_has_prefix (string, "this won't do very much...");
1150 g_str_has_suffix (string, "but maybe this will...");
1151 g_str_has_suffix (string, "HMMMM.");
1152 g_str_has_suffix (string, "MMMM.");
1153 g_str_has_suffix (string, "M.");
1155 g_strlcpy (buffer, string, sizeof buffer);
1156 g_assert_cmpint (strlen (buffer), ==, 4095);
1157 g_strlcpy (buffer, string, sizeof buffer);
1158 buffer[0] = '\0';
1159 g_strlcat (buffer, string, sizeof buffer);
1160 g_assert_cmpint (strlen (buffer), ==, 4095);
1162 tmp = g_strdup_printf ("<%s>", string);
1163 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1164 g_free (tmp);
1166 g_ascii_strdown (string, -1);
1167 g_ascii_strdown (string, -1);
1168 g_ascii_strup (string, -1);
1169 g_ascii_strup (string, -1);
1171 g_ascii_strcasecmp (string, string);
1172 g_ascii_strncasecmp (string, string, 10000);
1174 g_strreverse (string);
1175 g_strreverse (string);
1176 g_strchug (string);
1177 g_strchomp (string);
1178 g_strstrip (string);
1179 g_assert_cmpint (strlen (string), ==, 4095);
1181 g_strdelimit (string, "M", 'N');
1182 g_strcanon (string, " N.", ':');
1183 g_assert_cmpint (strlen (string), ==, 4095);
1185 array = g_strsplit (string, ".", -1);
1186 tmp = g_strjoinv (".", array);
1187 g_strfreev (array);
1189 g_assert_cmpint (strlen (tmp), ==, 4095);
1190 g_assert (memcmp (tmp, string, 4095) == 0);
1191 g_free (tmp);
1193 tmp = g_strconcat (string, string, string, NULL);
1194 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1195 g_free (tmp);
1197 tmp = g_strjoin ("!", string, string, NULL);
1198 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1199 g_free (tmp);
1201 tmp = g_markup_escape_text (string, -1);
1202 g_free (tmp);
1204 tmp = g_markup_printf_escaped ("%s", string);
1205 g_free (tmp);
1207 tmp = g_strescape (string, NULL);
1208 tmp2 = g_strcompress (tmp);
1209 g_assert_cmpstr (string, ==, tmp2);
1210 g_free (tmp2);
1211 g_free (tmp);
1213 g_mapped_file_free (file);
1216 static void
1217 test_strip_context (void)
1219 const gchar *msgid;
1220 const gchar *msgval;
1221 const gchar *s;
1224 msgid = "blabla";
1225 msgval = "bla";
1226 s = g_strip_context (msgid, msgval);
1227 g_assert (s == msgval);
1229 msgid = msgval = "blabla";
1230 s = g_strip_context (msgid, msgval);
1231 g_assert (s == msgval);
1233 msgid = msgval = "blabla|foo";
1234 s = g_strip_context (msgid, msgval);
1235 g_assert (s == msgval + 7);
1237 msgid = msgval = "blabla||bar";
1238 s = g_strip_context (msgid, msgval);
1239 g_assert (s == msgval + 7);
1243 main (int argc,
1244 char *argv[])
1246 g_test_init (&argc, &argv, NULL);
1248 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1249 g_test_add_func ("/strfuncs/strdup", test_strdup);
1250 g_test_add_func ("/strfuncs/strndup", test_strndup);
1251 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1252 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1253 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1254 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1255 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1256 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1257 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1258 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1259 g_test_add_func ("/strfuncs/strchug", test_strchug);
1260 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1261 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1262 g_test_add_func ("/strfuncs/strstr", test_strstr);
1263 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1264 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1265 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1266 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1267 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1268 g_test_add_func ("/strfuncs/strtod", test_strtod);
1269 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1270 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1271 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1273 return g_test_run();