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.
32 #define GLIB_TEST_STRING "el dorado "
34 #define FOR_ALL_CTYPE(macro) \
47 #define DEFINE_CALL_CTYPE(function) \
49 call_##function (int c) \
51 return function (c); \
54 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
56 call_g_ascii_##function (gchar c) \
58 return g_ascii_##function (c); \
61 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
62 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
65 test_is_function (const char *name,
66 gboolean (* ascii_function) (gchar),
67 int (* c_library_function) (int),
68 gboolean (* unicode_function) (gunichar))
72 for (c = 0; c <= 0x7F; c++)
74 gboolean ascii_result = ascii_function ((gchar)c);
75 gboolean c_library_result = c_library_function (c) != 0;
76 gboolean unicode_result = unicode_function ((gunichar) c);
77 if (ascii_result != c_library_result && c != '\v')
79 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
80 name, ascii_result, name, c_library_result, c);
82 if (ascii_result != unicode_result)
84 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
85 name, ascii_result, name, unicode_result, c);
88 for (c = 0x80; c <= 0xFF; c++)
90 gboolean ascii_result = ascii_function ((gchar)c);
93 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
99 test_to_function (const char *name,
100 gchar (* ascii_function) (gchar),
101 int (* c_library_function) (int),
102 gunichar (* unicode_function) (gunichar))
106 for (c = 0; c <= 0x7F; c++)
108 int ascii_result = (guchar) ascii_function ((gchar) c);
109 int c_library_result = c_library_function (c);
110 int unicode_result = unicode_function ((gunichar) c);
111 if (ascii_result != c_library_result)
113 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
114 name, ascii_result, name, c_library_result, c);
116 if (ascii_result != unicode_result)
118 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
119 name, ascii_result, name, unicode_result, c);
122 for (c = 0x80; c <= 0xFF; c++)
124 int ascii_result = (guchar) ascii_function ((gchar) c);
125 if (ascii_result != c)
127 g_error ("g_ascii_%s returned 0x%X for 0x%X",
128 name, ascii_result, c);
134 test_digit_function (const char *name,
135 int (* ascii_function) (gchar),
136 int (* unicode_function) (gunichar))
140 for (c = 0; c <= 0x7F; c++)
142 int ascii_result = ascii_function ((gchar) c);
143 int unicode_result = unicode_function ((gunichar) c);
144 if (ascii_result != unicode_result)
146 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
147 name, ascii_result, name, unicode_result, c);
150 for (c = 0x80; c <= 0xFF; c++)
152 int ascii_result = ascii_function ((gchar) c);
153 if (ascii_result != -1)
155 g_error ("g_ascii_%s_value returned %d for 0x%X",
156 name, ascii_result, c);
162 test_is_to_digit (void)
164 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
166 FOR_ALL_CTYPE(TEST_IS)
170 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
177 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
190 str = g_strdup (NULL);
191 g_assert (str == NULL);
193 str = g_strdup (GLIB_TEST_STRING);
194 g_assert (str != NULL);
195 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
204 str = g_strndup (NULL, 3);
205 g_assert (str == NULL);
207 str = g_strndup ("aaaa", 5);
208 g_assert (str != NULL);
209 g_assert_cmpstr (str, ==, "aaaa");
212 str = g_strndup ("aaaa", 2);
213 g_assert (str != NULL);
214 g_assert_cmpstr (str, ==, "aa");
219 test_strdup_printf (void)
223 str = g_strdup_printf ("%05d %-5s", 21, "test");
224 g_assert (str != NULL);
225 g_assert_cmpstr (str, ==, "00021 test ");
232 gchar *vec[] = { "Foo", "Bar", NULL };
235 copy = g_strdupv (NULL);
236 g_assert (copy == NULL);
238 copy = g_strdupv (vec);
239 g_assert (copy != NULL);
240 g_assert_cmpstr (copy[0], ==, "Foo");
241 g_assert_cmpstr (copy[1], ==, "Bar");
242 g_assert (copy[2] == NULL);
251 str = g_strnfill (0, 'a');
252 g_assert (str != NULL);
253 g_assert (*str == '\0');
256 str = g_strnfill (5, 'a');
257 g_assert (str != NULL);
258 g_assert_cmpstr (str, ==, "aaaaa");
263 test_strconcat (void)
267 str = g_strconcat (GLIB_TEST_STRING, NULL);
268 g_assert (str != NULL);
269 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
272 str = g_strconcat (GLIB_TEST_STRING,
276 g_assert (str != NULL);
277 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
286 str = g_strjoin (NULL, NULL);
287 g_assert (str != NULL);
288 g_assert (*str == '\0');
291 str = g_strjoin (":", NULL);
292 g_assert (str != NULL);
293 g_assert (*str == '\0');
296 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
297 g_assert (str != NULL);
298 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
301 str = g_strjoin (NULL,
306 g_assert (str != NULL);
307 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
310 str = g_strjoin (":",
315 g_assert (str != NULL);
316 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
325 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
327 str = g_strcanon (NULL, "ab", 'y');
329 g_test_trap_assert_failed ();
331 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
333 str = g_strdup ("abxabxab");
334 str = g_strcanon (str, NULL, 'y');
337 g_test_trap_assert_failed ();
339 str = g_strdup ("abxabxab");
340 str = g_strcanon (str, "ab", 'y');
341 g_assert (str != NULL);
342 g_assert_cmpstr (str, ==, "abyabyab");
347 test_strcompress_strescape (void)
353 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
355 str = g_strcompress (NULL);
357 g_test_trap_assert_failed ();
359 /* trailing slashes are not allowed */
360 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
362 str = g_strcompress ("abc\\");
364 g_test_trap_assert_failed ();
366 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
367 g_assert (str != NULL);
368 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
372 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
374 str = g_strescape (NULL, NULL);
376 g_test_trap_assert_failed ();
378 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
379 g_assert (str != NULL);
380 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
383 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
384 "\b\f\001\002\003\004");
385 g_assert (str != NULL);
386 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
390 tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
391 str = g_strcompress (tmp);
392 g_assert (str != NULL);
393 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
399 test_ascii_strcasecmp (void)
403 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
405 res = g_ascii_strcasecmp ("foo", NULL);
407 g_test_trap_assert_failed ();
409 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
411 res = g_ascii_strcasecmp (NULL, "foo");
413 g_test_trap_assert_failed ();
415 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
416 g_assert_cmpint (res, ==, 0);
418 res = g_ascii_strcasecmp ("frobozz", "frobozz");
419 g_assert_cmpint (res, ==, 0);
421 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
422 g_assert_cmpint (res, ==, 0);
424 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
425 g_assert_cmpint (res, !=, 0);
427 res = g_ascii_strcasecmp ("", "");
428 g_assert_cmpint (res, ==, 0);
430 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
431 g_assert_cmpint (res, ==, 0);
433 res = g_ascii_strcasecmp ("a", "b");
434 g_assert_cmpint (res, <, 0);
436 res = g_ascii_strcasecmp ("a", "B");
437 g_assert_cmpint (res, <, 0);
439 res = g_ascii_strcasecmp ("A", "b");
440 g_assert_cmpint (res, <, 0);
442 res = g_ascii_strcasecmp ("A", "B");
443 g_assert_cmpint (res, <, 0);
445 res = g_ascii_strcasecmp ("b", "a");
446 g_assert_cmpint (res, >, 0);
448 res = g_ascii_strcasecmp ("b", "A");
449 g_assert_cmpint (res, >, 0);
451 res = g_ascii_strcasecmp ("B", "a");
452 g_assert_cmpint (res, >, 0);
454 res = g_ascii_strcasecmp ("B", "A");
455 g_assert_cmpint (res, >, 0);
459 do_test_strchug (const gchar *str, const gchar *expected)
464 tmp = g_strdup (str);
467 res = (strcmp (tmp, expected) == 0);
470 g_assert_cmpint (res, ==, TRUE);
476 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
480 g_test_trap_assert_failed ();
482 do_test_strchug ("", "");
483 do_test_strchug (" ", "");
484 do_test_strchug ("\t\r\n ", "");
485 do_test_strchug (" a", "a");
486 do_test_strchug (" a", "a");
487 do_test_strchug ("a a", "a a");
488 do_test_strchug (" a a", "a a");
492 do_test_strchomp (const gchar *str, const gchar *expected)
497 tmp = g_strdup (str);
500 res = (strcmp (tmp, expected) == 0);
503 g_assert_cmpint (res, ==, TRUE);
509 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
513 g_test_trap_assert_failed ();
515 do_test_strchomp ("", "");
516 do_test_strchomp (" ", "");
517 do_test_strchomp (" \t\r\n", "");
518 do_test_strchomp ("a ", "a");
519 do_test_strchomp ("a ", "a");
520 do_test_strchomp ("a a", "a a");
521 do_test_strchomp ("a a ", "a a");
525 test_strreverse (void)
530 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
532 str = g_strreverse (NULL);
534 g_test_trap_assert_failed ();
536 str = p = g_strdup ("abcde");
537 str = g_strreverse (str);
538 g_assert (str != NULL);
540 g_assert_cmpstr (str, ==, "edcba");
550 haystack = g_strdup ("FooBarFooBarFoo");
553 res = g_strstr_len (haystack, 6, "xxx");
554 g_assert (res == NULL);
556 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
557 g_assert (res == NULL);
559 res = g_strstr_len (haystack, 3, "Bar");
560 g_assert (res == NULL);
562 res = g_strstr_len (haystack, 6, "");
563 g_assert (res == haystack);
564 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
566 res = g_strstr_len (haystack, 6, "Bar");
567 g_assert (res == haystack + 3);
568 g_assert_cmpstr (res, ==, "BarFooBarFoo");
570 res = g_strstr_len (haystack, -1, "Bar");
571 g_assert (res == haystack + 3);
572 g_assert_cmpstr (res, ==, "BarFooBarFoo");
575 res = g_strrstr (haystack, "xxx");
576 g_assert (res == NULL);
578 res = g_strrstr (haystack, "FooBarFooBarFooBar");
579 g_assert (res == NULL);
581 res = g_strrstr (haystack, "");
582 g_assert (res == haystack);
583 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
585 res = g_strrstr (haystack, "Bar");
586 g_assert (res == haystack + 9);
587 g_assert_cmpstr (res, ==, "BarFoo");
590 res = g_strrstr_len (haystack, 14, "xxx");
591 g_assert (res == NULL);
593 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
594 g_assert (res == NULL);
596 res = g_strrstr_len (haystack, 3, "Bar");
597 g_assert (res == NULL);
599 res = g_strrstr_len (haystack, 14, "BarFoo");
600 g_assert (res == haystack + 3);
601 g_assert_cmpstr (res, ==, "BarFooBarFoo");
603 res = g_strrstr_len (haystack, 15, "BarFoo");
604 g_assert (res == haystack + 9);
605 g_assert_cmpstr (res, ==, "BarFoo");
607 res = g_strrstr_len (haystack, -1, "BarFoo");
608 g_assert (res == haystack + 9);
609 g_assert_cmpstr (res, ==, "BarFoo");
611 /* test case for strings with \0 in the middle */
612 *(haystack + 7) = '\0';
613 res = g_strstr_len (haystack, 15, "BarFoo");
614 g_assert (res == NULL);
620 test_has_prefix (void)
624 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
626 res = g_str_has_prefix ("foo", NULL);
628 g_test_trap_assert_failed ();
630 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
632 res = g_str_has_prefix (NULL, "foo");
634 g_test_trap_assert_failed ();
636 res = g_str_has_prefix ("foo", "bar");
637 g_assert_cmpint (res, ==, FALSE);
639 res = g_str_has_prefix ("foo", "foobar");
640 g_assert_cmpint (res, ==, FALSE);
642 res = g_str_has_prefix ("foobar", "bar");
643 g_assert_cmpint (res, ==, FALSE);
645 res = g_str_has_prefix ("foobar", "foo");
646 g_assert_cmpint (res, ==, TRUE);
648 res = g_str_has_prefix ("foo", "");
649 g_assert_cmpint (res, ==, TRUE);
651 res = g_str_has_prefix ("foo", "foo");
652 g_assert_cmpint (res, ==, TRUE);
654 res = g_str_has_prefix ("", "");
655 g_assert_cmpint (res, ==, TRUE);
659 test_has_suffix (void)
663 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
665 res = g_str_has_suffix ("foo", NULL);
667 g_test_trap_assert_failed ();
669 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
671 res = g_str_has_suffix (NULL, "foo");
673 g_test_trap_assert_failed ();
675 res = g_str_has_suffix ("foo", "bar");
676 g_assert_cmpint (res, ==, FALSE);
678 res = g_str_has_suffix ("bar", "foobar");
679 g_assert_cmpint (res, ==, FALSE);
681 res = g_str_has_suffix ("foobar", "foo");
682 g_assert_cmpint (res, ==, FALSE);
684 res = g_str_has_suffix ("foobar", "bar");
685 g_assert_cmpint (res, ==, TRUE);
687 res = g_str_has_suffix ("foo", "");
688 g_assert_cmpint (res, ==, TRUE);
690 res = g_str_has_suffix ("foo", "foo");
691 g_assert_cmpint (res, ==, TRUE);
693 res = g_str_has_suffix ("", "");
694 g_assert_cmpint (res, ==, TRUE);
698 strv_check (gchar **strv, ...)
704 va_start (list, strv);
707 const gchar *str = va_arg (list, const char *);
710 g_assert (str == NULL);
719 g_assert_cmpstr (strv[i], ==, str);
731 strv_check (g_strsplit ("", ",", 0), NULL);
732 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
733 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", 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,z", ",", 0), "x", "y", "z", 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);
744 strv_check (g_strsplit ("", ",", 1), NULL);
745 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
746 strv_check (g_strsplit ("x,y", ",", 1), "x,y", 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,z", ",", 1), "x,y,z", 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);
757 strv_check (g_strsplit ("", ",", 2), NULL);
758 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
759 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", 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,z", ",", 2), "x", "y,z", 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);
772 test_strsplit_set (void)
774 strv_check (g_strsplit_set ("", ",/", 0), NULL);
775 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
776 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
777 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
778 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
780 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
781 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
782 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", 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);
789 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "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);
796 strv_check (g_strsplit_set ("", ",", 0), NULL);
797 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
798 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", 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,z", ",", 0), "x", "y", "z", 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);
808 strv_check (g_strsplit_set ("", ",", 1), NULL);
809 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
810 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", 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,z", ",", 1), "x,y,z", 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);
821 strv_check (g_strsplit_set ("", ",", 2), NULL);
822 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
823 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", 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,z", ",", 2), "x", "y,z", 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);
833 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
837 test_strv_length (void)
841 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
843 l = g_strv_length (NULL);
845 g_test_trap_assert_failed ();
847 l = g_strv_length (g_strsplit ("1,2,3,4", ",", -1));
848 g_assert_cmpuint (l, ==, 4);
851 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
854 check_strtod_string (gchar *number,
863 /* we try a copy of number, with some free space for malloc before that.
864 * This is supposed to smash the some wrong pointer calculations. */
866 dummy = g_malloc (100000);
867 number = g_strdup (number);
870 for (l = 0; l < G_N_ELEMENTS (locales); l++)
873 gchar *end = "(unset)";
875 setlocale (LC_NUMERIC, locales[l]);
876 d = g_ascii_strtod (number, &end);
877 ok = isnan (res) ? isnan (d) : (d == res);
880 g_error ("g_ascii_strtod on \"%s\" for locale %s failed\n" \
881 "expected %f (nan %d) actual %f (nan %d)\n",
887 ok = (end - number) == (check_end ? correct_len : strlen (number));
890 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
892 else if (end >= number && end <= number + strlen (number))
893 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
894 number, locales[l], end);
896 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
897 number, locales[l], number, end);
905 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
908 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
910 for (l = 0; l < G_N_ELEMENTS (locales); l++)
912 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
913 g_assert_cmpstr (buf, ==, str);
920 gdouble d, our_nan, our_inf;
921 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
926 /* Do this before any call to setlocale. */
927 our_nan = atof ("NaN");
929 g_assert (isnan (our_nan));
934 our_inf = atof ("Infinity");
936 g_assert (our_inf > 1 && our_inf == our_inf / 2);
938 check_strtod_string ("123.123", 123.123, FALSE, 0);
939 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
940 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
941 check_strtod_string ("-123.123", -123.123, FALSE, 0);
942 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
943 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
944 check_strtod_string ("5.4", 5.4, TRUE, 3);
945 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
946 check_strtod_string ("5,4", 5.0, TRUE, 1);
947 /* the following are for #156421 */
948 check_strtod_string ("1e1", 1e1, FALSE, 0);
949 check_strtod_string ("NAN", our_nan, FALSE, 0);
950 check_strtod_string ("-nan", -our_nan, FALSE, 0);
951 check_strtod_string ("INF", our_inf, FALSE, 0);
952 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
953 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
955 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
956 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
958 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
959 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
961 d = pow (2.0, -1024.1);
962 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
964 d = -pow (2.0, -1024.1);
965 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
968 check_strtod_string (" 0.75", 0.75, FALSE, 0);
969 check_strtod_string (" +0.75", 0.75, FALSE, 0);
970 check_strtod_string (" -0.75", -0.75, FALSE, 0);
971 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
972 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
973 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
974 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
977 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
978 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
982 check_strtod_number (0.75, "%0.2f", "0.75");
983 check_strtod_number (0.75, "%5.2f", " 0.75");
984 check_strtod_number (-0.75, "%0.2f", "-0.75");
985 check_strtod_number (-0.75, "%5.2f", "-0.75");
986 check_strtod_number (1e99, "%.0e", "1e+99");
990 check_uint64 (const gchar *str,
997 gchar *endptr = NULL;
1001 actual = g_ascii_strtoull (str, &endptr, base);
1004 g_assert (actual == result);
1005 g_assert_cmpstr (end, ==, endptr);
1006 g_assert (err == error);
1010 check_int64 (const gchar *str,
1017 gchar *endptr = NULL;
1021 actual = g_ascii_strtoll (str, &endptr, base);
1024 g_assert (actual == result);
1025 g_assert_cmpstr (end, ==, endptr);
1026 g_assert (err == error);
1032 check_uint64 ("0", "", 10, 0, 0);
1033 check_uint64 ("+0", "", 10, 0, 0);
1034 check_uint64 ("-0", "", 10, 0, 0);
1035 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1036 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1037 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1038 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1040 check_int64 ("0", "", 10, 0, 0);
1041 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1042 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1043 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1044 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1045 check_int64 ("32768", "", 10, 32768, 0);
1046 check_int64 ("-32768", "", 10, -32768, 0);
1047 check_int64 ("001", "", 10, 1, 0);
1048 check_int64 ("-001", "", 10, -1, 0);
1054 GMappedFile *file, *before, *after;
1060 /* if we allocate the file between two others and then free those
1061 * other two, then hopefully we end up with unmapped memory on either
1064 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1066 /* quick workaround until #549783 can be fixed */
1070 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1071 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1072 g_mapped_file_free (before);
1073 g_mapped_file_free (after);
1075 g_assert (file != NULL);
1076 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1077 string = g_mapped_file_get_contents (file);
1079 /* ensure they're all non-nul */
1080 g_assert (memchr (string, '\0', 4096) == NULL);
1082 /* test set 1: ensure that nothing goes past its maximum length, even in
1083 * light of a missing nul terminator.
1085 * we try to test all of the 'n' functions here.
1087 tmp = g_strndup (string, 4096);
1088 g_assert_cmpint (strlen (tmp), ==, 4096);
1091 /* found no bugs in gnome, i hope :) */
1092 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1093 g_strstr_len (string, 4096, "B");
1094 g_strstr_len (string, 4096, ".");
1095 g_strstr_len (string, 4096, "");
1097 g_strrstr_len (string, 4096, "BUGS");
1098 g_strrstr_len (string, 4096, "B");
1099 g_strrstr_len (string, 4096, ".");
1100 g_strrstr_len (string, 4096, "");
1102 g_ascii_strdown (string, 4096);
1103 g_ascii_strdown (string, 4096);
1104 g_ascii_strup (string, 4096);
1105 g_ascii_strup (string, 4096);
1107 g_ascii_strncasecmp (string, string, 4096);
1109 tmp = g_markup_escape_text (string, 4096);
1112 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1114 g_assert_cmpint (string[4095], ==, '\n');
1115 string[4095] = '\0';
1117 tmp = g_strdup (string);
1118 g_assert_cmpint (strlen (tmp), ==, 4095);
1121 tmp = g_strndup (string, 10000);
1122 g_assert_cmpint (strlen (tmp), ==, 4095);
1125 g_stpcpy (buffer, string);
1126 g_assert_cmpint (strlen (buffer), ==, 4095);
1128 g_strstr_len (string, 10000, "BUGS");
1129 g_strstr_len (string, 10000, "B");
1130 g_strstr_len (string, 10000, ".");
1131 g_strstr_len (string, 10000, "");
1133 g_strrstr (string, "BUGS");
1134 g_strrstr (string, "B");
1135 g_strrstr (string, ".");
1136 g_strrstr (string, "");
1138 g_strrstr_len (string, 10000, "BUGS");
1139 g_strrstr_len (string, 10000, "B");
1140 g_strrstr_len (string, 10000, ".");
1141 g_strrstr_len (string, 10000, "");
1143 g_str_has_prefix (string, "this won't do very much...");
1144 g_str_has_suffix (string, "but maybe this will...");
1145 g_str_has_suffix (string, "HMMMM.");
1146 g_str_has_suffix (string, "MMMM.");
1147 g_str_has_suffix (string, "M.");
1149 g_strlcpy (buffer, string, sizeof buffer);
1150 g_assert_cmpint (strlen (buffer), ==, 4095);
1151 g_strlcpy (buffer, string, sizeof buffer);
1153 g_strlcat (buffer, string, sizeof buffer);
1154 g_assert_cmpint (strlen (buffer), ==, 4095);
1156 tmp = g_strdup_printf ("<%s>", string);
1157 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1160 g_ascii_strdown (string, -1);
1161 g_ascii_strdown (string, -1);
1162 g_ascii_strup (string, -1);
1163 g_ascii_strup (string, -1);
1165 g_ascii_strcasecmp (string, string);
1166 g_ascii_strncasecmp (string, string, 10000);
1168 g_strreverse (string);
1169 g_strreverse (string);
1171 g_strchomp (string);
1172 g_strstrip (string);
1173 g_assert_cmpint (strlen (string), ==, 4095);
1175 g_strdelimit (string, "M", 'N');
1176 g_strcanon (string, " N.", ':');
1177 g_assert_cmpint (strlen (string), ==, 4095);
1179 array = g_strsplit (string, ".", -1);
1180 tmp = g_strjoinv (".", array);
1183 g_assert_cmpint (strlen (tmp), ==, 4095);
1184 g_assert (memcmp (tmp, string, 4095) == 0);
1187 tmp = g_strconcat (string, string, string, NULL);
1188 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1191 tmp = g_strjoin ("!", string, string, NULL);
1192 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1195 tmp = g_markup_escape_text (string, -1);
1198 tmp = g_markup_printf_escaped ("%s", string);
1201 tmp = g_strescape (string, NULL);
1202 tmp2 = g_strcompress (tmp);
1203 g_assert_cmpstr (string, ==, tmp2);
1207 g_mapped_file_free (file);
1214 g_test_init (&argc, &argv, NULL);
1216 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1217 g_test_add_func ("/strfuncs/strdup", test_strdup);
1218 g_test_add_func ("/strfuncs/strndup", test_strndup);
1219 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1220 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1221 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1222 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1223 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1224 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1225 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1226 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1227 g_test_add_func ("/strfuncs/strchug", test_strchug);
1228 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1229 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1230 g_test_add_func ("/strfuncs/strstr", test_strstr);
1231 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1232 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1233 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1234 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1235 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1236 g_test_add_func ("/strfuncs/strtod", test_strtod);
1237 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1238 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1240 return g_test_run();