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
34 #define GLIB_TEST_STRING "el dorado "
36 #define FOR_ALL_CTYPE(macro) \
49 #define DEFINE_CALL_CTYPE(function) \
51 call_##function (int c) \
53 return function (c); \
56 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
58 call_g_ascii_##function (gchar c) \
60 return g_ascii_##function (c); \
63 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
64 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
67 test_is_function (const char *name,
68 gboolean (* ascii_function) (gchar),
69 int (* c_library_function) (int),
70 gboolean (* unicode_function) (gunichar))
74 for (c = 0; c <= 0x7F; c++)
76 gboolean ascii_result = ascii_function ((gchar)c);
77 gboolean c_library_result = c_library_function (c) != 0;
78 gboolean unicode_result = unicode_function ((gunichar) c);
79 if (ascii_result != c_library_result && c != '\v')
81 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
82 name, ascii_result, name, c_library_result, c);
84 if (ascii_result != unicode_result)
86 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
87 name, ascii_result, name, unicode_result, c);
90 for (c = 0x80; c <= 0xFF; c++)
92 gboolean ascii_result = ascii_function ((gchar)c);
95 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
101 test_to_function (const char *name,
102 gchar (* ascii_function) (gchar),
103 int (* c_library_function) (int),
104 gunichar (* unicode_function) (gunichar))
108 for (c = 0; c <= 0x7F; c++)
110 int ascii_result = (guchar) ascii_function ((gchar) c);
111 int c_library_result = c_library_function (c);
112 int unicode_result = unicode_function ((gunichar) c);
113 if (ascii_result != c_library_result)
115 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
116 name, ascii_result, name, c_library_result, c);
118 if (ascii_result != unicode_result)
120 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
121 name, ascii_result, name, unicode_result, c);
124 for (c = 0x80; c <= 0xFF; c++)
126 int ascii_result = (guchar) ascii_function ((gchar) c);
127 if (ascii_result != c)
129 g_error ("g_ascii_%s returned 0x%X for 0x%X",
130 name, ascii_result, c);
136 test_digit_function (const char *name,
137 int (* ascii_function) (gchar),
138 int (* unicode_function) (gunichar))
142 for (c = 0; c <= 0x7F; c++)
144 int ascii_result = ascii_function ((gchar) c);
145 int unicode_result = unicode_function ((gunichar) c);
146 if (ascii_result != unicode_result)
148 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
149 name, ascii_result, name, unicode_result, c);
152 for (c = 0x80; c <= 0xFF; c++)
154 int ascii_result = ascii_function ((gchar) c);
155 if (ascii_result != -1)
157 g_error ("g_ascii_%s_value returned %d for 0x%X",
158 name, ascii_result, c);
164 test_is_to_digit (void)
166 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
168 FOR_ALL_CTYPE(TEST_IS)
172 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
179 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
192 str = g_strdup (NULL);
193 g_assert (str == NULL);
195 str = g_strdup (GLIB_TEST_STRING);
196 g_assert (str != NULL);
197 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
206 str = g_strndup (NULL, 3);
207 g_assert (str == NULL);
209 str = g_strndup ("aaaa", 5);
210 g_assert (str != NULL);
211 g_assert_cmpstr (str, ==, "aaaa");
214 str = g_strndup ("aaaa", 2);
215 g_assert (str != NULL);
216 g_assert_cmpstr (str, ==, "aa");
221 test_strdup_printf (void)
225 str = g_strdup_printf ("%05d %-5s", 21, "test");
226 g_assert (str != NULL);
227 g_assert_cmpstr (str, ==, "00021 test ");
234 gchar *vec[] = { "Foo", "Bar", NULL };
237 copy = g_strdupv (NULL);
238 g_assert (copy == NULL);
240 copy = g_strdupv (vec);
241 g_assert (copy != NULL);
242 g_assert_cmpstr (copy[0], ==, "Foo");
243 g_assert_cmpstr (copy[1], ==, "Bar");
244 g_assert (copy[2] == NULL);
253 str = g_strnfill (0, 'a');
254 g_assert (str != NULL);
255 g_assert (*str == '\0');
258 str = g_strnfill (5, 'a');
259 g_assert (str != NULL);
260 g_assert_cmpstr (str, ==, "aaaaa");
265 test_strconcat (void)
269 str = g_strconcat (GLIB_TEST_STRING, NULL);
270 g_assert (str != NULL);
271 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
274 str = g_strconcat (GLIB_TEST_STRING,
278 g_assert (str != NULL);
279 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
282 g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
290 str = g_strjoin (NULL, NULL);
291 g_assert (str != NULL);
292 g_assert (*str == '\0');
295 str = g_strjoin (":", NULL);
296 g_assert (str != NULL);
297 g_assert (*str == '\0');
300 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
301 g_assert (str != NULL);
302 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
305 str = g_strjoin (NULL,
310 g_assert (str != NULL);
311 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
314 str = g_strjoin (":",
319 g_assert (str != NULL);
320 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
329 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
331 str = g_strcanon (NULL, "ab", 'y');
333 g_test_trap_assert_failed ();
335 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
337 str = g_strdup ("abxabxab");
338 str = g_strcanon (str, NULL, 'y');
341 g_test_trap_assert_failed ();
343 str = g_strdup ("abxabxab");
344 str = g_strcanon (str, "ab", 'y');
345 g_assert (str != NULL);
346 g_assert_cmpstr (str, ==, "abyabyab");
351 test_strcompress_strescape (void)
357 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
359 str = g_strcompress (NULL);
361 g_test_trap_assert_failed ();
363 /* trailing slashes are not allowed */
364 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
366 str = g_strcompress ("abc\\");
368 g_test_trap_assert_failed ();
370 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
371 g_assert (str != NULL);
372 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
376 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
378 str = g_strescape (NULL, NULL);
380 g_test_trap_assert_failed ();
382 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
383 g_assert (str != NULL);
384 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
387 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
388 "\b\f\001\002\003\004");
389 g_assert (str != NULL);
390 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
394 tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
395 str = g_strcompress (tmp);
396 g_assert (str != NULL);
397 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
403 test_ascii_strcasecmp (void)
407 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
409 res = g_ascii_strcasecmp ("foo", NULL);
411 g_test_trap_assert_failed ();
413 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
415 res = g_ascii_strcasecmp (NULL, "foo");
417 g_test_trap_assert_failed ();
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", "FROBOZZ");
426 g_assert_cmpint (res, ==, 0);
428 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
429 g_assert_cmpint (res, !=, 0);
431 res = g_ascii_strcasecmp ("", "");
432 g_assert_cmpint (res, ==, 0);
434 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
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 ("A", "B");
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);
458 res = g_ascii_strcasecmp ("B", "A");
459 g_assert_cmpint (res, >, 0);
463 do_test_strchug (const gchar *str, const gchar *expected)
468 tmp = g_strdup (str);
471 res = (strcmp (tmp, expected) == 0);
474 g_assert_cmpint (res, ==, TRUE);
480 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
484 g_test_trap_assert_failed ();
486 do_test_strchug ("", "");
487 do_test_strchug (" ", "");
488 do_test_strchug ("\t\r\n ", "");
489 do_test_strchug (" a", "a");
490 do_test_strchug (" a", "a");
491 do_test_strchug ("a a", "a a");
492 do_test_strchug (" a a", "a a");
496 do_test_strchomp (const gchar *str, const gchar *expected)
501 tmp = g_strdup (str);
504 res = (strcmp (tmp, expected) == 0);
507 g_assert_cmpint (res, ==, TRUE);
513 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
517 g_test_trap_assert_failed ();
519 do_test_strchomp ("", "");
520 do_test_strchomp (" ", "");
521 do_test_strchomp (" \t\r\n", "");
522 do_test_strchomp ("a ", "a");
523 do_test_strchomp ("a ", "a");
524 do_test_strchomp ("a a", "a a");
525 do_test_strchomp ("a a ", "a a");
529 test_strreverse (void)
534 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
536 str = g_strreverse (NULL);
538 g_test_trap_assert_failed ();
540 str = p = g_strdup ("abcde");
541 str = g_strreverse (str);
542 g_assert (str != NULL);
544 g_assert_cmpstr (str, ==, "edcba");
554 haystack = g_strdup ("FooBarFooBarFoo");
557 res = g_strstr_len (haystack, 6, "xxx");
558 g_assert (res == NULL);
560 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
561 g_assert (res == NULL);
563 res = g_strstr_len (haystack, 3, "Bar");
564 g_assert (res == NULL);
566 res = g_strstr_len (haystack, 6, "");
567 g_assert (res == haystack);
568 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
570 res = g_strstr_len (haystack, 6, "Bar");
571 g_assert (res == haystack + 3);
572 g_assert_cmpstr (res, ==, "BarFooBarFoo");
574 res = g_strstr_len (haystack, -1, "Bar");
575 g_assert (res == haystack + 3);
576 g_assert_cmpstr (res, ==, "BarFooBarFoo");
579 res = g_strrstr (haystack, "xxx");
580 g_assert (res == NULL);
582 res = g_strrstr (haystack, "FooBarFooBarFooBar");
583 g_assert (res == NULL);
585 res = g_strrstr (haystack, "");
586 g_assert (res == haystack);
587 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
589 res = g_strrstr (haystack, "Bar");
590 g_assert (res == haystack + 9);
591 g_assert_cmpstr (res, ==, "BarFoo");
594 res = g_strrstr_len (haystack, 14, "xxx");
595 g_assert (res == NULL);
597 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
598 g_assert (res == NULL);
600 res = g_strrstr_len (haystack, 3, "Bar");
601 g_assert (res == NULL);
603 res = g_strrstr_len (haystack, 14, "BarFoo");
604 g_assert (res == haystack + 3);
605 g_assert_cmpstr (res, ==, "BarFooBarFoo");
607 res = g_strrstr_len (haystack, 15, "BarFoo");
608 g_assert (res == haystack + 9);
609 g_assert_cmpstr (res, ==, "BarFoo");
611 res = g_strrstr_len (haystack, -1, "BarFoo");
612 g_assert (res == haystack + 9);
613 g_assert_cmpstr (res, ==, "BarFoo");
615 /* test case for strings with \0 in the middle */
616 *(haystack + 7) = '\0';
617 res = g_strstr_len (haystack, 15, "BarFoo");
618 g_assert (res == NULL);
624 test_has_prefix (void)
628 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
630 res = g_str_has_prefix ("foo", NULL);
632 g_test_trap_assert_failed ();
634 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
636 res = g_str_has_prefix (NULL, "foo");
638 g_test_trap_assert_failed ();
640 res = g_str_has_prefix ("foo", "bar");
641 g_assert_cmpint (res, ==, FALSE);
643 res = g_str_has_prefix ("foo", "foobar");
644 g_assert_cmpint (res, ==, FALSE);
646 res = g_str_has_prefix ("foobar", "bar");
647 g_assert_cmpint (res, ==, FALSE);
649 res = g_str_has_prefix ("foobar", "foo");
650 g_assert_cmpint (res, ==, TRUE);
652 res = g_str_has_prefix ("foo", "");
653 g_assert_cmpint (res, ==, TRUE);
655 res = g_str_has_prefix ("foo", "foo");
656 g_assert_cmpint (res, ==, TRUE);
658 res = g_str_has_prefix ("", "");
659 g_assert_cmpint (res, ==, TRUE);
663 test_has_suffix (void)
667 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
669 res = g_str_has_suffix ("foo", NULL);
671 g_test_trap_assert_failed ();
673 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
675 res = g_str_has_suffix (NULL, "foo");
677 g_test_trap_assert_failed ();
679 res = g_str_has_suffix ("foo", "bar");
680 g_assert_cmpint (res, ==, FALSE);
682 res = g_str_has_suffix ("bar", "foobar");
683 g_assert_cmpint (res, ==, FALSE);
685 res = g_str_has_suffix ("foobar", "foo");
686 g_assert_cmpint (res, ==, FALSE);
688 res = g_str_has_suffix ("foobar", "bar");
689 g_assert_cmpint (res, ==, TRUE);
691 res = g_str_has_suffix ("foo", "");
692 g_assert_cmpint (res, ==, TRUE);
694 res = g_str_has_suffix ("foo", "foo");
695 g_assert_cmpint (res, ==, TRUE);
697 res = g_str_has_suffix ("", "");
698 g_assert_cmpint (res, ==, TRUE);
702 strv_check (gchar **strv, ...)
708 va_start (list, strv);
711 const gchar *str = va_arg (list, const char *);
714 g_assert (str == NULL);
723 g_assert_cmpstr (strv[i], ==, str);
735 strv_check (g_strsplit ("", ",", 0), NULL);
736 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
737 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
738 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
739 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
740 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", 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);
744 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
745 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
746 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
748 strv_check (g_strsplit ("", ",", 1), NULL);
749 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
750 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
751 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
752 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
753 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", 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);
757 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
758 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
759 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
761 strv_check (g_strsplit ("", ",", 2), NULL);
762 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
763 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
764 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
765 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
766 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", 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);
770 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
771 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
772 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
776 test_strsplit_set (void)
778 strv_check (g_strsplit_set ("", ",/", 0), NULL);
779 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
780 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
781 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
782 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
784 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
785 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", 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);
789 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
790 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
791 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "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);
796 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
797 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
798 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
800 strv_check (g_strsplit_set ("", ",", 0), NULL);
801 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
802 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
803 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
804 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
805 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", 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);
808 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
809 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
810 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
812 strv_check (g_strsplit_set ("", ",", 1), NULL);
813 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
814 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
815 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
816 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
817 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", 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);
821 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
822 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
823 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
825 strv_check (g_strsplit_set ("", ",", 2), NULL);
826 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
827 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
828 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
829 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
830 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", 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);
833 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
834 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
835 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
837 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
841 test_strv_length (void)
845 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
847 l = g_strv_length (NULL);
849 g_test_trap_assert_failed ();
851 l = g_strv_length (g_strsplit ("1,2,3,4", ",", -1));
852 g_assert_cmpuint (l, ==, 4);
855 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
858 check_strtod_string (gchar *number,
867 /* we try a copy of number, with some free space for malloc before that.
868 * This is supposed to smash the some wrong pointer calculations. */
870 dummy = g_malloc (100000);
871 number = g_strdup (number);
874 for (l = 0; l < G_N_ELEMENTS (locales); l++)
877 gchar *end = "(unset)";
879 setlocale (LC_NUMERIC, locales[l]);
880 d = g_ascii_strtod (number, &end);
881 ok = isnan (res) ? isnan (d) : (d == res);
884 g_error ("g_ascii_strtod on \"%s\" for locale %s failed\n" \
885 "expected %f (nan %d) actual %f (nan %d)\n",
891 ok = (end - number) == (check_end ? correct_len : strlen (number));
894 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
896 else if (end >= number && end <= number + strlen (number))
897 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
898 number, locales[l], end);
900 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
901 number, locales[l], number, end);
909 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
912 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
914 for (l = 0; l < G_N_ELEMENTS (locales); l++)
916 setlocale (LC_ALL, locales[l]);
917 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
918 g_assert_cmpstr (buf, ==, str);
925 gdouble d, our_nan, our_inf;
926 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
931 /* Do this before any call to setlocale. */
932 our_nan = atof ("NaN");
934 g_assert (isnan (our_nan));
939 our_inf = atof ("Infinity");
941 g_assert (our_inf > 1 && our_inf == our_inf / 2);
943 check_strtod_string ("123.123", 123.123, FALSE, 0);
944 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
945 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
946 check_strtod_string ("-123.123", -123.123, FALSE, 0);
947 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
948 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
949 check_strtod_string ("5.4", 5.4, TRUE, 3);
950 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
951 check_strtod_string ("5,4", 5.0, TRUE, 1);
952 check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
953 check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
954 check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
955 check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
956 check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
957 /* the following are for #156421 */
958 check_strtod_string ("1e1", 1e1, FALSE, 0);
959 check_strtod_string ("NAN", our_nan, FALSE, 0);
960 check_strtod_string ("-nan", -our_nan, FALSE, 0);
961 check_strtod_string ("INF", our_inf, FALSE, 0);
962 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
963 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
965 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
966 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
968 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
969 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
971 d = pow (2.0, -1024.1);
972 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
974 d = -pow (2.0, -1024.1);
975 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
978 check_strtod_string (" 0.75", 0.75, FALSE, 0);
979 check_strtod_string (" +0.75", 0.75, FALSE, 0);
980 check_strtod_string (" -0.75", -0.75, FALSE, 0);
981 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
982 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
983 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
984 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
987 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
988 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
992 check_strtod_number (0.75, "%0.2f", "0.75");
993 check_strtod_number (0.75, "%5.2f", " 0.75");
994 check_strtod_number (-0.75, "%0.2f", "-0.75");
995 check_strtod_number (-0.75, "%5.2f", "-0.75");
996 check_strtod_number (1e99, "%.0e", "1e+99");
1000 check_uint64 (const gchar *str,
1007 gchar *endptr = NULL;
1011 actual = g_ascii_strtoull (str, &endptr, base);
1014 g_assert (actual == result);
1015 g_assert_cmpstr (end, ==, endptr);
1016 g_assert (err == error);
1020 check_int64 (const gchar *str,
1027 gchar *endptr = NULL;
1031 actual = g_ascii_strtoll (str, &endptr, base);
1034 g_assert (actual == result);
1035 g_assert_cmpstr (end, ==, endptr);
1036 g_assert (err == error);
1042 check_uint64 ("0", "", 10, 0, 0);
1043 check_uint64 ("+0", "", 10, 0, 0);
1044 check_uint64 ("-0", "", 10, 0, 0);
1045 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1046 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1047 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1048 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1050 check_int64 ("0", "", 10, 0, 0);
1051 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1052 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1053 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1054 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1055 check_int64 ("32768", "", 10, 32768, 0);
1056 check_int64 ("-32768", "", 10, -32768, 0);
1057 check_int64 ("001", "", 10, 1, 0);
1058 check_int64 ("-001", "", 10, -1, 0);
1064 GMappedFile *file, *before, *after;
1070 /* if we allocate the file between two others and then free those
1071 * other two, then hopefully we end up with unmapped memory on either
1074 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1076 /* quick workaround until #549783 can be fixed */
1080 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1081 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1082 g_mapped_file_unref (before);
1083 g_mapped_file_unref (after);
1085 g_assert (file != NULL);
1086 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1087 string = g_mapped_file_get_contents (file);
1089 /* ensure they're all non-nul */
1090 g_assert (memchr (string, '\0', 4096) == NULL);
1092 /* test set 1: ensure that nothing goes past its maximum length, even in
1093 * light of a missing nul terminator.
1095 * we try to test all of the 'n' functions here.
1097 tmp = g_strndup (string, 4096);
1098 g_assert_cmpint (strlen (tmp), ==, 4096);
1101 /* found no bugs in gnome, i hope :) */
1102 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1103 g_strstr_len (string, 4096, "B");
1104 g_strstr_len (string, 4096, ".");
1105 g_strstr_len (string, 4096, "");
1107 g_strrstr_len (string, 4096, "BUGS");
1108 g_strrstr_len (string, 4096, "B");
1109 g_strrstr_len (string, 4096, ".");
1110 g_strrstr_len (string, 4096, "");
1112 g_ascii_strdown (string, 4096);
1113 g_ascii_strdown (string, 4096);
1114 g_ascii_strup (string, 4096);
1115 g_ascii_strup (string, 4096);
1117 g_ascii_strncasecmp (string, string, 4096);
1119 tmp = g_markup_escape_text (string, 4096);
1122 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1124 g_assert_cmpint (string[4095], ==, '\n');
1125 string[4095] = '\0';
1127 tmp = g_strdup (string);
1128 g_assert_cmpint (strlen (tmp), ==, 4095);
1131 tmp = g_strndup (string, 10000);
1132 g_assert_cmpint (strlen (tmp), ==, 4095);
1135 g_stpcpy (buffer, string);
1136 g_assert_cmpint (strlen (buffer), ==, 4095);
1138 g_strstr_len (string, 10000, "BUGS");
1139 g_strstr_len (string, 10000, "B");
1140 g_strstr_len (string, 10000, ".");
1141 g_strstr_len (string, 10000, "");
1143 g_strrstr (string, "BUGS");
1144 g_strrstr (string, "B");
1145 g_strrstr (string, ".");
1146 g_strrstr (string, "");
1148 g_strrstr_len (string, 10000, "BUGS");
1149 g_strrstr_len (string, 10000, "B");
1150 g_strrstr_len (string, 10000, ".");
1151 g_strrstr_len (string, 10000, "");
1153 g_str_has_prefix (string, "this won't do very much...");
1154 g_str_has_suffix (string, "but maybe this will...");
1155 g_str_has_suffix (string, "HMMMM.");
1156 g_str_has_suffix (string, "MMMM.");
1157 g_str_has_suffix (string, "M.");
1159 g_strlcpy (buffer, string, sizeof buffer);
1160 g_assert_cmpint (strlen (buffer), ==, 4095);
1161 g_strlcpy (buffer, string, sizeof buffer);
1163 g_strlcat (buffer, string, sizeof buffer);
1164 g_assert_cmpint (strlen (buffer), ==, 4095);
1166 tmp = g_strdup_printf ("<%s>", string);
1167 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1170 g_ascii_strdown (string, -1);
1171 g_ascii_strdown (string, -1);
1172 g_ascii_strup (string, -1);
1173 g_ascii_strup (string, -1);
1175 g_ascii_strcasecmp (string, string);
1176 g_ascii_strncasecmp (string, string, 10000);
1178 g_strreverse (string);
1179 g_strreverse (string);
1181 g_strchomp (string);
1182 g_strstrip (string);
1183 g_assert_cmpint (strlen (string), ==, 4095);
1185 g_strdelimit (string, "M", 'N');
1186 g_strcanon (string, " N.", ':');
1187 g_assert_cmpint (strlen (string), ==, 4095);
1189 array = g_strsplit (string, ".", -1);
1190 tmp = g_strjoinv (".", array);
1193 g_assert_cmpint (strlen (tmp), ==, 4095);
1194 g_assert (memcmp (tmp, string, 4095) == 0);
1197 tmp = g_strconcat (string, string, string, NULL);
1198 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1201 tmp = g_strjoin ("!", string, string, NULL);
1202 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1205 tmp = g_markup_escape_text (string, -1);
1208 tmp = g_markup_printf_escaped ("%s", string);
1211 tmp = g_strescape (string, NULL);
1212 tmp2 = g_strcompress (tmp);
1213 g_assert_cmpstr (string, ==, tmp2);
1217 g_mapped_file_unref (file);
1221 test_strip_context (void)
1224 const gchar *msgval;
1230 s = g_strip_context (msgid, msgval);
1231 g_assert (s == msgval);
1233 msgid = msgval = "blabla";
1234 s = g_strip_context (msgid, msgval);
1235 g_assert (s == msgval);
1237 msgid = msgval = "blabla|foo";
1238 s = g_strip_context (msgid, msgval);
1239 g_assert (s == msgval + 7);
1241 msgid = msgval = "blabla||bar";
1242 s = g_strip_context (msgid, msgval);
1243 g_assert (s == msgval + 7);
1247 test_strerror (void)
1252 for (i = 1; i < 100; i++)
1254 str = g_strerror (i);
1255 g_assert (str != NULL);
1256 g_assert (g_utf8_validate (str, -1, NULL));
1261 test_strsignal (void)
1266 for (i = 1; i < 20; i++)
1268 str = g_strsignal (i);
1269 g_assert (str != NULL);
1270 g_assert (g_utf8_validate (str, -1, NULL));
1279 s = g_strdup ("lower");
1280 g_assert_cmpstr (g_strup (s), ==, "LOWER");
1281 g_assert_cmpstr (g_strdown (s), ==, "lower");
1282 g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1290 g_test_init (&argc, &argv, NULL);
1292 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1293 g_test_add_func ("/strfuncs/strdup", test_strdup);
1294 g_test_add_func ("/strfuncs/strndup", test_strndup);
1295 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1296 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1297 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1298 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1299 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1300 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1301 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1302 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1303 g_test_add_func ("/strfuncs/strchug", test_strchug);
1304 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1305 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1306 g_test_add_func ("/strfuncs/strstr", test_strstr);
1307 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1308 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1309 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1310 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1311 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1312 g_test_add_func ("/strfuncs/strtod", test_strtod);
1313 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1314 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1315 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1316 g_test_add_func ("/strfuncs/strerror", test_strerror);
1317 g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1318 g_test_add_func ("/strfuncs/strup", test_strup);
1320 return g_test_run();