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 GLIB_DISABLE_DEPRECATION_WARNINGS
24 #define _XOPEN_SOURCE 600
35 #define GLIB_TEST_STRING "el dorado "
37 #define FOR_ALL_CTYPE(macro) \
50 #define DEFINE_CALL_CTYPE(function) \
52 call_##function (int c) \
54 return function (c); \
57 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
59 call_g_ascii_##function (gchar c) \
61 return g_ascii_##function (c); \
64 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
65 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
68 test_is_function (const char *name,
69 gboolean (* ascii_function) (gchar),
70 int (* c_library_function) (int),
71 gboolean (* unicode_function) (gunichar))
75 for (c = 0; c <= 0x7F; c++)
77 gboolean ascii_result = ascii_function ((gchar)c);
78 gboolean c_library_result = c_library_function (c) != 0;
79 gboolean unicode_result = unicode_function ((gunichar) c);
80 if (ascii_result != c_library_result && c != '\v')
82 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
83 name, ascii_result, name, c_library_result, c);
85 if (ascii_result != unicode_result)
87 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
88 name, ascii_result, name, unicode_result, c);
91 for (c = 0x80; c <= 0xFF; c++)
93 gboolean ascii_result = ascii_function ((gchar)c);
96 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
102 test_to_function (const char *name,
103 gchar (* ascii_function) (gchar),
104 int (* c_library_function) (int),
105 gunichar (* unicode_function) (gunichar))
109 for (c = 0; c <= 0x7F; c++)
111 int ascii_result = (guchar) ascii_function ((gchar) c);
112 int c_library_result = c_library_function (c);
113 int unicode_result = unicode_function ((gunichar) c);
114 if (ascii_result != c_library_result)
116 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
117 name, ascii_result, name, c_library_result, c);
119 if (ascii_result != unicode_result)
121 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
122 name, ascii_result, name, unicode_result, c);
125 for (c = 0x80; c <= 0xFF; c++)
127 int ascii_result = (guchar) ascii_function ((gchar) c);
128 if (ascii_result != c)
130 g_error ("g_ascii_%s returned 0x%X for 0x%X",
131 name, ascii_result, c);
137 test_digit_function (const char *name,
138 int (* ascii_function) (gchar),
139 int (* unicode_function) (gunichar))
143 for (c = 0; c <= 0x7F; c++)
145 int ascii_result = ascii_function ((gchar) c);
146 int unicode_result = unicode_function ((gunichar) c);
147 if (ascii_result != unicode_result)
149 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
150 name, ascii_result, name, unicode_result, c);
153 for (c = 0x80; c <= 0xFF; c++)
155 int ascii_result = ascii_function ((gchar) c);
156 if (ascii_result != -1)
158 g_error ("g_ascii_%s_value returned %d for 0x%X",
159 name, ascii_result, c);
165 test_is_to_digit (void)
167 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
169 FOR_ALL_CTYPE(TEST_IS)
173 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
180 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
193 str = g_strdup (NULL);
194 g_assert (str == NULL);
196 str = g_strdup (GLIB_TEST_STRING);
197 g_assert (str != NULL);
198 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
207 str = g_strndup (NULL, 3);
208 g_assert (str == NULL);
210 str = g_strndup ("aaaa", 5);
211 g_assert (str != NULL);
212 g_assert_cmpstr (str, ==, "aaaa");
215 str = g_strndup ("aaaa", 2);
216 g_assert (str != NULL);
217 g_assert_cmpstr (str, ==, "aa");
222 test_strdup_printf (void)
226 str = g_strdup_printf ("%05d %-5s", 21, "test");
227 g_assert (str != NULL);
228 g_assert_cmpstr (str, ==, "00021 test ");
235 gchar *vec[] = { "Foo", "Bar", NULL };
238 copy = g_strdupv (NULL);
239 g_assert (copy == NULL);
241 copy = g_strdupv (vec);
242 g_assert (copy != NULL);
243 g_assert_cmpstr (copy[0], ==, "Foo");
244 g_assert_cmpstr (copy[1], ==, "Bar");
245 g_assert (copy[2] == NULL);
254 str = g_strnfill (0, 'a');
255 g_assert (str != NULL);
256 g_assert (*str == '\0');
259 str = g_strnfill (5, 'a');
260 g_assert (str != NULL);
261 g_assert_cmpstr (str, ==, "aaaaa");
266 test_strconcat (void)
270 str = g_strconcat (GLIB_TEST_STRING, NULL);
271 g_assert (str != NULL);
272 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
275 str = g_strconcat (GLIB_TEST_STRING,
279 g_assert (str != NULL);
280 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
283 g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
291 str = g_strjoin (NULL, NULL);
292 g_assert (str != NULL);
293 g_assert (*str == '\0');
296 str = g_strjoin (":", NULL);
297 g_assert (str != NULL);
298 g_assert (*str == '\0');
301 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
302 g_assert (str != NULL);
303 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
306 str = g_strjoin (NULL,
311 g_assert (str != NULL);
312 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
315 str = g_strjoin (":",
320 g_assert (str != NULL);
321 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
330 if (g_test_undefined ())
332 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
334 str = g_strcanon (NULL, "ab", 'y');
336 g_test_trap_assert_failed ();
338 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
340 str = g_strdup ("abxabxab");
341 str = g_strcanon (str, NULL, 'y');
344 g_test_trap_assert_failed ();
347 str = g_strdup ("abxabxab");
348 str = g_strcanon (str, "ab", 'y');
349 g_assert (str != NULL);
350 g_assert_cmpstr (str, ==, "abyabyab");
355 test_strcompress_strescape (void)
361 if (g_test_undefined ())
363 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
365 str = g_strcompress (NULL);
367 g_test_trap_assert_failed ();
369 /* trailing slashes are not allowed */
370 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
372 str = g_strcompress ("abc\\");
374 g_test_trap_assert_failed ();
377 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
378 g_assert (str != NULL);
379 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
383 if (g_test_undefined ())
385 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
387 str = g_strescape (NULL, NULL);
389 g_test_trap_assert_failed ();
392 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
393 g_assert (str != NULL);
394 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
397 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
398 "\b\f\001\002\003\004");
399 g_assert (str != NULL);
400 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
404 tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
405 str = g_strcompress (tmp);
406 g_assert (str != NULL);
407 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
413 test_ascii_strcasecmp (void)
417 if (g_test_undefined ())
419 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
421 res = g_ascii_strcasecmp ("foo", NULL);
423 g_test_trap_assert_failed ();
425 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
427 res = g_ascii_strcasecmp (NULL, "foo");
429 g_test_trap_assert_failed ();
432 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
433 g_assert_cmpint (res, ==, 0);
435 res = g_ascii_strcasecmp ("frobozz", "frobozz");
436 g_assert_cmpint (res, ==, 0);
438 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
439 g_assert_cmpint (res, ==, 0);
441 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
442 g_assert_cmpint (res, !=, 0);
444 res = g_ascii_strcasecmp ("", "");
445 g_assert_cmpint (res, ==, 0);
447 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
448 g_assert_cmpint (res, ==, 0);
450 res = g_ascii_strcasecmp ("a", "b");
451 g_assert_cmpint (res, <, 0);
453 res = g_ascii_strcasecmp ("a", "B");
454 g_assert_cmpint (res, <, 0);
456 res = g_ascii_strcasecmp ("A", "b");
457 g_assert_cmpint (res, <, 0);
459 res = g_ascii_strcasecmp ("A", "B");
460 g_assert_cmpint (res, <, 0);
462 res = g_ascii_strcasecmp ("b", "a");
463 g_assert_cmpint (res, >, 0);
465 res = g_ascii_strcasecmp ("b", "A");
466 g_assert_cmpint (res, >, 0);
468 res = g_ascii_strcasecmp ("B", "a");
469 g_assert_cmpint (res, >, 0);
471 res = g_ascii_strcasecmp ("B", "A");
472 g_assert_cmpint (res, >, 0);
476 do_test_strchug (const gchar *str, const gchar *expected)
481 tmp = g_strdup (str);
484 res = (strcmp (tmp, expected) == 0);
487 g_assert_cmpint (res, ==, TRUE);
493 if (g_test_undefined ())
495 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
499 g_test_trap_assert_failed ();
502 do_test_strchug ("", "");
503 do_test_strchug (" ", "");
504 do_test_strchug ("\t\r\n ", "");
505 do_test_strchug (" a", "a");
506 do_test_strchug (" a", "a");
507 do_test_strchug ("a a", "a a");
508 do_test_strchug (" a a", "a a");
512 do_test_strchomp (const gchar *str, const gchar *expected)
517 tmp = g_strdup (str);
520 res = (strcmp (tmp, expected) == 0);
523 g_assert_cmpint (res, ==, TRUE);
529 if (g_test_undefined ())
531 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
535 g_test_trap_assert_failed ();
538 do_test_strchomp ("", "");
539 do_test_strchomp (" ", "");
540 do_test_strchomp (" \t\r\n", "");
541 do_test_strchomp ("a ", "a");
542 do_test_strchomp ("a ", "a");
543 do_test_strchomp ("a a", "a a");
544 do_test_strchomp ("a a ", "a a");
548 test_strreverse (void)
553 if (g_test_undefined ())
555 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
557 str = g_strreverse (NULL);
559 g_test_trap_assert_failed ();
562 str = p = g_strdup ("abcde");
563 str = g_strreverse (str);
564 g_assert (str != NULL);
566 g_assert_cmpstr (str, ==, "edcba");
571 test_strncasecmp (void)
573 g_assert (g_strncasecmp ("abc1", "ABC2", 3) == 0);
574 g_assert (g_strncasecmp ("abc1", "ABC2", 4) != 0);
583 haystack = g_strdup ("FooBarFooBarFoo");
586 res = g_strstr_len (haystack, 6, "xxx");
587 g_assert (res == NULL);
589 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
590 g_assert (res == NULL);
592 res = g_strstr_len (haystack, 3, "Bar");
593 g_assert (res == NULL);
595 res = g_strstr_len (haystack, 6, "");
596 g_assert (res == haystack);
597 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
599 res = g_strstr_len (haystack, 6, "Bar");
600 g_assert (res == haystack + 3);
601 g_assert_cmpstr (res, ==, "BarFooBarFoo");
603 res = g_strstr_len (haystack, -1, "Bar");
604 g_assert (res == haystack + 3);
605 g_assert_cmpstr (res, ==, "BarFooBarFoo");
608 res = g_strrstr (haystack, "xxx");
609 g_assert (res == NULL);
611 res = g_strrstr (haystack, "FooBarFooBarFooBar");
612 g_assert (res == NULL);
614 res = g_strrstr (haystack, "");
615 g_assert (res == haystack);
616 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
618 res = g_strrstr (haystack, "Bar");
619 g_assert (res == haystack + 9);
620 g_assert_cmpstr (res, ==, "BarFoo");
623 res = g_strrstr_len (haystack, 14, "xxx");
624 g_assert (res == NULL);
626 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
627 g_assert (res == NULL);
629 res = g_strrstr_len (haystack, 3, "Bar");
630 g_assert (res == NULL);
632 res = g_strrstr_len (haystack, 14, "BarFoo");
633 g_assert (res == haystack + 3);
634 g_assert_cmpstr (res, ==, "BarFooBarFoo");
636 res = g_strrstr_len (haystack, 15, "BarFoo");
637 g_assert (res == haystack + 9);
638 g_assert_cmpstr (res, ==, "BarFoo");
640 res = g_strrstr_len (haystack, -1, "BarFoo");
641 g_assert (res == haystack + 9);
642 g_assert_cmpstr (res, ==, "BarFoo");
644 /* test case for strings with \0 in the middle */
645 *(haystack + 7) = '\0';
646 res = g_strstr_len (haystack, 15, "BarFoo");
647 g_assert (res == NULL);
653 test_has_prefix (void)
657 if (g_test_undefined ())
659 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
661 res = g_str_has_prefix ("foo", NULL);
663 g_test_trap_assert_failed ();
665 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
667 res = g_str_has_prefix (NULL, "foo");
669 g_test_trap_assert_failed ();
672 res = g_str_has_prefix ("foo", "bar");
673 g_assert_cmpint (res, ==, FALSE);
675 res = g_str_has_prefix ("foo", "foobar");
676 g_assert_cmpint (res, ==, FALSE);
678 res = g_str_has_prefix ("foobar", "bar");
679 g_assert_cmpint (res, ==, FALSE);
681 res = g_str_has_prefix ("foobar", "foo");
682 g_assert_cmpint (res, ==, TRUE);
684 res = g_str_has_prefix ("foo", "");
685 g_assert_cmpint (res, ==, TRUE);
687 res = g_str_has_prefix ("foo", "foo");
688 g_assert_cmpint (res, ==, TRUE);
690 res = g_str_has_prefix ("", "");
691 g_assert_cmpint (res, ==, TRUE);
695 test_has_suffix (void)
699 if (g_test_undefined ())
701 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
703 res = g_str_has_suffix ("foo", NULL);
705 g_test_trap_assert_failed ();
707 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
709 res = g_str_has_suffix (NULL, "foo");
711 g_test_trap_assert_failed ();
714 res = g_str_has_suffix ("foo", "bar");
715 g_assert_cmpint (res, ==, FALSE);
717 res = g_str_has_suffix ("bar", "foobar");
718 g_assert_cmpint (res, ==, FALSE);
720 res = g_str_has_suffix ("foobar", "foo");
721 g_assert_cmpint (res, ==, FALSE);
723 res = g_str_has_suffix ("foobar", "bar");
724 g_assert_cmpint (res, ==, TRUE);
726 res = g_str_has_suffix ("foo", "");
727 g_assert_cmpint (res, ==, TRUE);
729 res = g_str_has_suffix ("foo", "foo");
730 g_assert_cmpint (res, ==, TRUE);
732 res = g_str_has_suffix ("", "");
733 g_assert_cmpint (res, ==, TRUE);
737 strv_check (gchar **strv, ...)
743 va_start (list, strv);
746 const gchar *str = va_arg (list, const char *);
749 g_assert (str == NULL);
758 g_assert_cmpstr (strv[i], ==, str);
770 strv_check (g_strsplit ("", ",", 0), NULL);
771 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
772 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
773 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
774 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
775 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
776 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
777 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
778 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
779 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
780 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
781 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
783 strv_check (g_strsplit ("", ",", 1), NULL);
784 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
785 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
786 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
787 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
788 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
789 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
790 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
791 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
792 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
793 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
794 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
796 strv_check (g_strsplit ("", ",", 2), NULL);
797 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
798 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
799 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
800 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
801 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
802 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
803 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
804 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
805 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
806 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
807 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
811 test_strsplit_set (void)
813 strv_check (g_strsplit_set ("", ",/", 0), NULL);
814 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
815 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
816 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
817 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
819 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
820 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
821 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
822 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
823 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
824 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
825 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
826 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
828 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
829 strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
830 strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
831 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
832 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
833 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
835 strv_check (g_strsplit_set ("", ",", 0), NULL);
836 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
837 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
838 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
839 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
840 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
841 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
842 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
843 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
844 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
845 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
847 strv_check (g_strsplit_set ("", ",", 1), NULL);
848 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
849 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
850 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
851 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
852 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
853 strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
854 strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
855 strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
856 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
857 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
858 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
860 strv_check (g_strsplit_set ("", ",", 2), NULL);
861 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
862 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
863 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
864 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
865 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
866 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
867 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
868 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
869 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
870 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
872 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
876 test_strv_length (void)
881 if (g_test_undefined ())
883 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
885 l = g_strv_length (NULL);
887 g_test_trap_assert_failed ();
890 strv = g_strsplit ("1,2,3,4", ",", -1);
891 l = g_strv_length (strv);
892 g_assert_cmpuint (l, ==, 4);
896 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
899 check_strtod_string (gchar *number,
908 /* we try a copy of number, with some free space for malloc before that.
909 * This is supposed to smash the some wrong pointer calculations. */
911 dummy = g_malloc (100000);
912 number = g_strdup (number);
915 for (l = 0; l < G_N_ELEMENTS (locales); l++)
917 gchar *end = "(unset)";
919 setlocale (LC_NUMERIC, locales[l]);
920 d = g_ascii_strtod (number, &end);
921 g_assert (isnan (res) ? isnan (d) : (d == res));
922 g_assert ((end - number) == (check_end ? correct_len : strlen (number)));
929 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
932 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
934 for (l = 0; l < G_N_ELEMENTS (locales); l++)
936 setlocale (LC_ALL, locales[l]);
937 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
938 g_assert_cmpstr (buf, ==, str);
945 gdouble d, our_nan, our_inf;
946 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
951 /* Do this before any call to setlocale. */
952 our_nan = atof ("NaN");
954 g_assert (isnan (our_nan));
959 our_inf = atof ("Infinity");
961 g_assert (our_inf > 1 && our_inf == our_inf / 2);
963 check_strtod_string ("123.123", 123.123, FALSE, 0);
964 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
965 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
966 check_strtod_string ("-123.123", -123.123, FALSE, 0);
967 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
968 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
969 check_strtod_string ("5.4", 5.4, TRUE, 3);
970 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
971 check_strtod_string ("5,4", 5.0, TRUE, 1);
972 check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
973 check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
974 check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
975 check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
976 check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
977 /* the following are for #156421 */
978 check_strtod_string ("1e1", 1e1, FALSE, 0);
979 check_strtod_string ("NAN", our_nan, FALSE, 0);
980 check_strtod_string ("-nan", -our_nan, FALSE, 0);
981 check_strtod_string ("INF", our_inf, FALSE, 0);
982 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
983 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
985 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
986 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
988 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
989 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
991 d = pow (2.0, -1024.1);
992 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
994 d = -pow (2.0, -1024.1);
995 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
998 check_strtod_string (" 0.75", 0.75, FALSE, 0);
999 check_strtod_string (" +0.75", 0.75, FALSE, 0);
1000 check_strtod_string (" -0.75", -0.75, FALSE, 0);
1001 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
1002 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
1003 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
1004 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
1007 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1008 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1012 check_strtod_number (0.75, "%0.2f", "0.75");
1013 check_strtod_number (0.75, "%5.2f", " 0.75");
1014 check_strtod_number (-0.75, "%0.2f", "-0.75");
1015 check_strtod_number (-0.75, "%5.2f", "-0.75");
1016 check_strtod_number (1e99, "%.0e", "1e+99");
1020 check_uint64 (const gchar *str,
1027 gchar *endptr = NULL;
1031 actual = g_ascii_strtoull (str, &endptr, base);
1034 g_assert (actual == result);
1035 g_assert_cmpstr (end, ==, endptr);
1036 g_assert (err == error);
1040 check_int64 (const gchar *str,
1047 gchar *endptr = NULL;
1051 actual = g_ascii_strtoll (str, &endptr, base);
1054 g_assert (actual == result);
1055 g_assert_cmpstr (end, ==, endptr);
1056 g_assert (err == error);
1062 check_uint64 ("0", "", 10, 0, 0);
1063 check_uint64 ("+0", "", 10, 0, 0);
1064 check_uint64 ("-0", "", 10, 0, 0);
1065 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1066 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1067 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1068 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1070 check_int64 ("0", "", 10, 0, 0);
1071 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1072 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1073 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1074 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1075 check_int64 ("32768", "", 10, 32768, 0);
1076 check_int64 ("-32768", "", 10, -32768, 0);
1077 check_int64 ("001", "", 10, 1, 0);
1078 check_int64 ("-001", "", 10, -1, 0);
1084 GMappedFile *file, *before, *after;
1090 /* if we allocate the file between two others and then free those
1091 * other two, then hopefully we end up with unmapped memory on either
1094 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1096 /* quick workaround until #549783 can be fixed */
1100 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1101 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1102 g_mapped_file_unref (before);
1103 g_mapped_file_unref (after);
1105 g_assert (file != NULL);
1106 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1107 string = g_mapped_file_get_contents (file);
1109 /* ensure they're all non-nul */
1110 g_assert (memchr (string, '\0', 4096) == NULL);
1112 /* test set 1: ensure that nothing goes past its maximum length, even in
1113 * light of a missing nul terminator.
1115 * we try to test all of the 'n' functions here.
1117 tmp = g_strndup (string, 4096);
1118 g_assert_cmpint (strlen (tmp), ==, 4096);
1121 /* found no bugs in gnome, i hope :) */
1122 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1123 g_strstr_len (string, 4096, "B");
1124 g_strstr_len (string, 4096, ".");
1125 g_strstr_len (string, 4096, "");
1127 g_strrstr_len (string, 4096, "BUGS");
1128 g_strrstr_len (string, 4096, "B");
1129 g_strrstr_len (string, 4096, ".");
1130 g_strrstr_len (string, 4096, "");
1132 tmp = g_ascii_strup (string, 4096);
1133 tmp2 = g_ascii_strup (tmp, 4096);
1134 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1135 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1136 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1140 tmp = g_ascii_strdown (string, 4096);
1141 tmp2 = g_ascii_strdown (tmp, 4096);
1142 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1143 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1144 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1148 tmp = g_markup_escape_text (string, 4096);
1151 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1153 g_assert_cmpint (string[4095], ==, '\n');
1154 string[4095] = '\0';
1156 tmp = g_strdup (string);
1157 g_assert_cmpint (strlen (tmp), ==, 4095);
1160 tmp = g_strndup (string, 10000);
1161 g_assert_cmpint (strlen (tmp), ==, 4095);
1164 g_stpcpy (buffer, string);
1165 g_assert_cmpint (strlen (buffer), ==, 4095);
1167 g_strstr_len (string, 10000, "BUGS");
1168 g_strstr_len (string, 10000, "B");
1169 g_strstr_len (string, 10000, ".");
1170 g_strstr_len (string, 10000, "");
1172 g_strrstr (string, "BUGS");
1173 g_strrstr (string, "B");
1174 g_strrstr (string, ".");
1175 g_strrstr (string, "");
1177 g_strrstr_len (string, 10000, "BUGS");
1178 g_strrstr_len (string, 10000, "B");
1179 g_strrstr_len (string, 10000, ".");
1180 g_strrstr_len (string, 10000, "");
1182 g_str_has_prefix (string, "this won't do very much...");
1183 g_str_has_suffix (string, "but maybe this will...");
1184 g_str_has_suffix (string, "HMMMM.");
1185 g_str_has_suffix (string, "MMMM.");
1186 g_str_has_suffix (string, "M.");
1188 g_strlcpy (buffer, string, sizeof buffer);
1189 g_assert_cmpint (strlen (buffer), ==, 4095);
1190 g_strlcpy (buffer, string, sizeof buffer);
1192 g_strlcat (buffer, string, sizeof buffer);
1193 g_assert_cmpint (strlen (buffer), ==, 4095);
1195 tmp = g_strdup_printf ("<%s>", string);
1196 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1199 tmp = g_ascii_strdown (string, -1);
1200 tmp2 = g_ascii_strdown (tmp, -1);
1201 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1202 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1203 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1204 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1205 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1209 tmp = g_ascii_strup (string, -1);
1210 tmp2 = g_ascii_strup (string, -1);
1211 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1212 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1213 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1214 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1215 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1219 g_ascii_strcasecmp (string, string);
1220 g_ascii_strncasecmp (string, string, 10000);
1222 g_strreverse (string);
1223 g_strreverse (string);
1225 g_strchomp (string);
1226 g_strstrip (string);
1227 g_assert_cmpint (strlen (string), ==, 4095);
1229 g_strdelimit (string, "M", 'N');
1230 g_strcanon (string, " N.", ':');
1231 g_assert_cmpint (strlen (string), ==, 4095);
1233 array = g_strsplit (string, ".", -1);
1234 tmp = g_strjoinv (".", array);
1237 g_assert_cmpint (strlen (tmp), ==, 4095);
1238 g_assert (memcmp (tmp, string, 4095) == 0);
1241 tmp = g_strconcat (string, string, string, NULL);
1242 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1245 tmp = g_strjoin ("!", string, string, NULL);
1246 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1249 tmp = g_markup_escape_text (string, -1);
1252 tmp = g_markup_printf_escaped ("%s", string);
1255 tmp = g_strescape (string, NULL);
1256 tmp2 = g_strcompress (tmp);
1257 g_assert_cmpstr (string, ==, tmp2);
1261 g_mapped_file_unref (file);
1265 test_strip_context (void)
1268 const gchar *msgval;
1274 s = g_strip_context (msgid, msgval);
1275 g_assert (s == msgval);
1277 msgid = msgval = "blabla";
1278 s = g_strip_context (msgid, msgval);
1279 g_assert (s == msgval);
1281 msgid = msgval = "blabla|foo";
1282 s = g_strip_context (msgid, msgval);
1283 g_assert (s == msgval + 7);
1285 msgid = msgval = "blabla||bar";
1286 s = g_strip_context (msgid, msgval);
1287 g_assert (s == msgval + 7);
1291 test_strerror (void)
1296 for (i = 1; i < 100; i++)
1298 str = g_strerror (i);
1299 g_assert (str != NULL);
1300 g_assert (g_utf8_validate (str, -1, NULL));
1305 test_strsignal (void)
1310 for (i = 1; i < 20; i++)
1312 str = g_strsignal (i);
1313 g_assert (str != NULL);
1314 g_assert (g_utf8_validate (str, -1, NULL));
1323 s = g_strdup ("lower");
1324 g_assert_cmpstr (g_strup (s), ==, "LOWER");
1325 g_assert_cmpstr (g_strdown (s), ==, "lower");
1326 g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1334 g_test_init (&argc, &argv, NULL);
1336 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1337 g_test_add_func ("/strfuncs/strdup", test_strdup);
1338 g_test_add_func ("/strfuncs/strndup", test_strndup);
1339 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1340 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1341 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1342 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1343 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1344 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1345 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1346 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1347 g_test_add_func ("/strfuncs/strchug", test_strchug);
1348 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1349 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1350 g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
1351 g_test_add_func ("/strfuncs/strstr", test_strstr);
1352 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1353 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1354 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1355 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1356 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1357 g_test_add_func ("/strfuncs/strtod", test_strtod);
1358 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1359 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1360 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1361 g_test_add_func ("/strfuncs/strerror", test_strerror);
1362 g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1363 g_test_add_func ("/strfuncs/strup", test_strup);
1365 return g_test_run();