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 #if defined (_MSC_VER) && (_MSC_VER <= 1800)
36 #define isnan(x) _isnan(x)
39 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
40 #define NAN (*(const float *) __nan)
44 #define INFINITY HUGE_VAL
49 #define GLIB_TEST_STRING "el dorado "
51 #define FOR_ALL_CTYPE(macro) \
64 #define DEFINE_CALL_CTYPE(function) \
66 call_##function (int c) \
68 return function (c); \
71 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
73 call_g_ascii_##function (gchar c) \
75 return g_ascii_##function (c); \
78 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
79 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
82 test_is_function (const char *name,
83 gboolean (* ascii_function) (gchar),
84 int (* c_library_function) (int),
85 gboolean (* unicode_function) (gunichar))
89 for (c = 0; c <= 0x7F; c++)
91 gboolean ascii_result = ascii_function ((gchar)c);
92 gboolean c_library_result = c_library_function (c) != 0;
93 gboolean unicode_result = unicode_function ((gunichar) c);
94 if (ascii_result != c_library_result && c != '\v')
96 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
97 name, ascii_result, name, c_library_result, c);
99 if (ascii_result != unicode_result)
101 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
102 name, ascii_result, name, unicode_result, c);
105 for (c = 0x80; c <= 0xFF; c++)
107 gboolean ascii_result = ascii_function ((gchar)c);
110 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
116 test_to_function (const char *name,
117 gchar (* ascii_function) (gchar),
118 int (* c_library_function) (int),
119 gunichar (* unicode_function) (gunichar))
123 for (c = 0; c <= 0x7F; c++)
125 int ascii_result = (guchar) ascii_function ((gchar) c);
126 int c_library_result = c_library_function (c);
127 int unicode_result = unicode_function ((gunichar) c);
128 if (ascii_result != c_library_result)
130 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
131 name, ascii_result, name, c_library_result, c);
133 if (ascii_result != unicode_result)
135 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
136 name, ascii_result, name, unicode_result, c);
139 for (c = 0x80; c <= 0xFF; c++)
141 int ascii_result = (guchar) ascii_function ((gchar) c);
142 if (ascii_result != c)
144 g_error ("g_ascii_%s returned 0x%X for 0x%X",
145 name, ascii_result, c);
151 test_digit_function (const char *name,
152 int (* ascii_function) (gchar),
153 int (* unicode_function) (gunichar))
157 for (c = 0; c <= 0x7F; c++)
159 int ascii_result = ascii_function ((gchar) c);
160 int unicode_result = unicode_function ((gunichar) c);
161 if (ascii_result != unicode_result)
163 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
164 name, ascii_result, name, unicode_result, c);
167 for (c = 0x80; c <= 0xFF; c++)
169 int ascii_result = ascii_function ((gchar) c);
170 if (ascii_result != -1)
172 g_error ("g_ascii_%s_value returned %d for 0x%X",
173 name, ascii_result, c);
179 test_is_to_digit (void)
181 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
183 FOR_ALL_CTYPE(TEST_IS)
187 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
194 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
207 str = g_strdup (NULL);
208 g_assert (str == NULL);
210 str = g_strdup (GLIB_TEST_STRING);
211 g_assert (str != NULL);
212 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
221 str = g_strndup (NULL, 3);
222 g_assert (str == NULL);
224 str = g_strndup ("aaaa", 5);
225 g_assert (str != NULL);
226 g_assert_cmpstr (str, ==, "aaaa");
229 str = g_strndup ("aaaa", 2);
230 g_assert (str != NULL);
231 g_assert_cmpstr (str, ==, "aa");
236 test_strdup_printf (void)
240 str = g_strdup_printf ("%05d %-5s", 21, "test");
241 g_assert (str != NULL);
242 g_assert_cmpstr (str, ==, "00021 test ");
249 gchar *vec[] = { "Foo", "Bar", NULL };
252 copy = g_strdupv (NULL);
253 g_assert (copy == NULL);
255 copy = g_strdupv (vec);
256 g_assert (copy != NULL);
257 g_assert_cmpstr (copy[0], ==, "Foo");
258 g_assert_cmpstr (copy[1], ==, "Bar");
259 g_assert (copy[2] == NULL);
268 str = g_strnfill (0, 'a');
269 g_assert (str != NULL);
270 g_assert (*str == '\0');
273 str = g_strnfill (5, 'a');
274 g_assert (str != NULL);
275 g_assert_cmpstr (str, ==, "aaaaa");
280 test_strconcat (void)
284 str = g_strconcat (GLIB_TEST_STRING, NULL);
285 g_assert (str != NULL);
286 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
289 str = g_strconcat (GLIB_TEST_STRING,
293 g_assert (str != NULL);
294 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
297 g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
305 str = g_strjoin (NULL, NULL);
306 g_assert (str != NULL);
307 g_assert (*str == '\0');
310 str = g_strjoin (":", NULL);
311 g_assert (str != NULL);
312 g_assert (*str == '\0');
315 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
316 g_assert (str != NULL);
317 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
320 str = g_strjoin (NULL,
325 g_assert (str != NULL);
326 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
329 str = g_strjoin (":",
334 g_assert (str != NULL);
335 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
344 if (g_test_undefined ())
348 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
349 "*assertion*!= NULL*");
350 str = g_strcanon (NULL, "ab", 'y');
351 g_test_assert_expected_messages ();
352 g_assert (str == NULL);
354 str = g_strdup ("abxabxab");
355 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
356 "*assertion*!= NULL*");
357 ret = g_strcanon (str, NULL, 'y');
358 g_test_assert_expected_messages ();
359 g_assert (ret == NULL);
363 str = g_strdup ("abxabxab");
364 str = g_strcanon (str, "ab", 'y');
365 g_assert (str != NULL);
366 g_assert_cmpstr (str, ==, "abyabyab");
371 test_strcompress_strescape (void)
377 if (g_test_undefined ())
379 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
380 "*assertion*!= NULL*");
381 str = g_strcompress (NULL);
382 g_test_assert_expected_messages ();
383 g_assert (str == NULL);
385 /* trailing slashes are not allowed */
386 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
388 str = g_strcompress ("abc\\");
389 g_test_assert_expected_messages ();
390 g_assert_cmpstr (str, ==, "abc");
394 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
395 g_assert (str != NULL);
396 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
400 if (g_test_undefined ())
402 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
403 "*assertion*!= NULL*");
404 str = g_strescape (NULL, NULL);
405 g_test_assert_expected_messages ();
406 g_assert (str == NULL);
409 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
410 g_assert (str != NULL);
411 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
414 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
415 "\b\f\001\002\003\004");
416 g_assert (str != NULL);
417 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
421 tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
422 str = g_strcompress (tmp);
423 g_assert (str != NULL);
424 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
430 test_ascii_strcasecmp (void)
434 if (g_test_undefined ())
436 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
437 "*assertion*!= NULL*");
438 res = g_ascii_strcasecmp ("foo", NULL);
439 g_test_assert_expected_messages ();
440 g_assert (res == FALSE);
442 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
443 "*assertion*!= NULL*");
444 res = g_ascii_strcasecmp (NULL, "foo");
445 g_test_assert_expected_messages ();
446 g_assert (res == FALSE);
449 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
450 g_assert_cmpint (res, ==, 0);
452 res = g_ascii_strcasecmp ("frobozz", "frobozz");
453 g_assert_cmpint (res, ==, 0);
455 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
456 g_assert_cmpint (res, ==, 0);
458 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
459 g_assert_cmpint (res, !=, 0);
461 res = g_ascii_strcasecmp ("", "");
462 g_assert_cmpint (res, ==, 0);
464 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
465 g_assert_cmpint (res, ==, 0);
467 res = g_ascii_strcasecmp ("a", "b");
468 g_assert_cmpint (res, <, 0);
470 res = g_ascii_strcasecmp ("a", "B");
471 g_assert_cmpint (res, <, 0);
473 res = g_ascii_strcasecmp ("A", "b");
474 g_assert_cmpint (res, <, 0);
476 res = g_ascii_strcasecmp ("A", "B");
477 g_assert_cmpint (res, <, 0);
479 res = g_ascii_strcasecmp ("b", "a");
480 g_assert_cmpint (res, >, 0);
482 res = g_ascii_strcasecmp ("b", "A");
483 g_assert_cmpint (res, >, 0);
485 res = g_ascii_strcasecmp ("B", "a");
486 g_assert_cmpint (res, >, 0);
488 res = g_ascii_strcasecmp ("B", "A");
489 g_assert_cmpint (res, >, 0);
493 do_test_strchug (const gchar *str, const gchar *expected)
498 tmp = g_strdup (str);
501 res = (strcmp (tmp, expected) == 0);
504 g_assert_cmpint (res, ==, TRUE);
510 if (g_test_undefined ())
512 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
513 "*assertion*!= NULL*");
515 g_test_assert_expected_messages ();
518 do_test_strchug ("", "");
519 do_test_strchug (" ", "");
520 do_test_strchug ("\t\r\n ", "");
521 do_test_strchug (" a", "a");
522 do_test_strchug (" a", "a");
523 do_test_strchug ("a a", "a a");
524 do_test_strchug (" a a", "a a");
528 do_test_strchomp (const gchar *str, const gchar *expected)
533 tmp = g_strdup (str);
536 res = (strcmp (tmp, expected) == 0);
539 g_assert_cmpint (res, ==, TRUE);
545 if (g_test_undefined ())
547 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
548 "*assertion*!= NULL*");
550 g_test_assert_expected_messages ();
553 do_test_strchomp ("", "");
554 do_test_strchomp (" ", "");
555 do_test_strchomp (" \t\r\n", "");
556 do_test_strchomp ("a ", "a");
557 do_test_strchomp ("a ", "a");
558 do_test_strchomp ("a a", "a a");
559 do_test_strchomp ("a a ", "a a");
563 test_strreverse (void)
568 if (g_test_undefined ())
570 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
571 "*assertion*!= NULL*");
572 str = g_strreverse (NULL);
573 g_test_assert_expected_messages ();
574 g_assert (str == NULL);
577 str = p = g_strdup ("abcde");
578 str = g_strreverse (str);
579 g_assert (str != NULL);
581 g_assert_cmpstr (str, ==, "edcba");
586 test_strncasecmp (void)
588 g_assert (g_strncasecmp ("abc1", "ABC2", 3) == 0);
589 g_assert (g_strncasecmp ("abc1", "ABC2", 4) != 0);
598 haystack = g_strdup ("FooBarFooBarFoo");
601 res = g_strstr_len (haystack, 6, "xxx");
602 g_assert (res == NULL);
604 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
605 g_assert (res == NULL);
607 res = g_strstr_len (haystack, 3, "Bar");
608 g_assert (res == NULL);
610 res = g_strstr_len (haystack, 6, "");
611 g_assert (res == haystack);
612 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
614 res = g_strstr_len (haystack, 6, "Bar");
615 g_assert (res == haystack + 3);
616 g_assert_cmpstr (res, ==, "BarFooBarFoo");
618 res = g_strstr_len (haystack, -1, "Bar");
619 g_assert (res == haystack + 3);
620 g_assert_cmpstr (res, ==, "BarFooBarFoo");
623 res = g_strrstr (haystack, "xxx");
624 g_assert (res == NULL);
626 res = g_strrstr (haystack, "FooBarFooBarFooBar");
627 g_assert (res == NULL);
629 res = g_strrstr (haystack, "");
630 g_assert (res == haystack);
631 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
633 res = g_strrstr (haystack, "Bar");
634 g_assert (res == haystack + 9);
635 g_assert_cmpstr (res, ==, "BarFoo");
638 res = g_strrstr_len (haystack, 14, "xxx");
639 g_assert (res == NULL);
641 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
642 g_assert (res == NULL);
644 res = g_strrstr_len (haystack, 3, "Bar");
645 g_assert (res == NULL);
647 res = g_strrstr_len (haystack, 14, "BarFoo");
648 g_assert (res == haystack + 3);
649 g_assert_cmpstr (res, ==, "BarFooBarFoo");
651 res = g_strrstr_len (haystack, 15, "BarFoo");
652 g_assert (res == haystack + 9);
653 g_assert_cmpstr (res, ==, "BarFoo");
655 res = g_strrstr_len (haystack, -1, "BarFoo");
656 g_assert (res == haystack + 9);
657 g_assert_cmpstr (res, ==, "BarFoo");
659 /* test case for strings with \0 in the middle */
660 *(haystack + 7) = '\0';
661 res = g_strstr_len (haystack, 15, "BarFoo");
662 g_assert (res == NULL);
668 test_has_prefix (void)
672 if (g_test_undefined ())
674 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
675 "*assertion*!= NULL*");
676 res = g_str_has_prefix ("foo", NULL);
677 g_test_assert_expected_messages ();
678 g_assert (res == FALSE);
680 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
681 "*assertion*!= NULL*");
682 res = g_str_has_prefix (NULL, "foo");
683 g_test_assert_expected_messages ();
684 g_assert (res == FALSE);
687 res = g_str_has_prefix ("foo", "bar");
688 g_assert_cmpint (res, ==, FALSE);
690 res = g_str_has_prefix ("foo", "foobar");
691 g_assert_cmpint (res, ==, FALSE);
693 res = g_str_has_prefix ("foobar", "bar");
694 g_assert_cmpint (res, ==, FALSE);
696 res = g_str_has_prefix ("foobar", "foo");
697 g_assert_cmpint (res, ==, TRUE);
699 res = g_str_has_prefix ("foo", "");
700 g_assert_cmpint (res, ==, TRUE);
702 res = g_str_has_prefix ("foo", "foo");
703 g_assert_cmpint (res, ==, TRUE);
705 res = g_str_has_prefix ("", "");
706 g_assert_cmpint (res, ==, TRUE);
710 test_has_suffix (void)
714 if (g_test_undefined ())
716 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
717 "*assertion*!= NULL*");
718 res = g_str_has_suffix ("foo", NULL);
719 g_test_assert_expected_messages ();
720 g_assert (res == FALSE);
722 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
723 "*assertion*!= NULL*");
724 res = g_str_has_suffix (NULL, "foo");
725 g_test_assert_expected_messages ();
726 g_assert (res == FALSE);
729 res = g_str_has_suffix ("foo", "bar");
730 g_assert_cmpint (res, ==, FALSE);
732 res = g_str_has_suffix ("bar", "foobar");
733 g_assert_cmpint (res, ==, FALSE);
735 res = g_str_has_suffix ("foobar", "foo");
736 g_assert_cmpint (res, ==, FALSE);
738 res = g_str_has_suffix ("foobar", "bar");
739 g_assert_cmpint (res, ==, TRUE);
741 res = g_str_has_suffix ("foo", "");
742 g_assert_cmpint (res, ==, TRUE);
744 res = g_str_has_suffix ("foo", "foo");
745 g_assert_cmpint (res, ==, TRUE);
747 res = g_str_has_suffix ("", "");
748 g_assert_cmpint (res, ==, TRUE);
752 strv_check (gchar **strv, ...)
758 va_start (list, strv);
761 const gchar *str = va_arg (list, const char *);
764 g_assert (str == NULL);
773 g_assert_cmpstr (strv[i], ==, str);
785 strv_check (g_strsplit ("", ",", 0), NULL);
786 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
787 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
788 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
789 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
790 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
791 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
792 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
793 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
794 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
795 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
796 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
798 strv_check (g_strsplit ("", ",", 1), NULL);
799 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
800 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
801 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
802 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
803 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
804 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
805 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
806 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
807 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
808 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
809 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
811 strv_check (g_strsplit ("", ",", 2), NULL);
812 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
813 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
814 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
815 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
816 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
817 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
818 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
819 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
820 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
821 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
822 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
826 test_strsplit_set (void)
828 strv_check (g_strsplit_set ("", ",/", 0), NULL);
829 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
830 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
831 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
832 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
834 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
835 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
836 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
837 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
838 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
839 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
840 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
841 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
843 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
844 strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
845 strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
846 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
847 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
848 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
850 strv_check (g_strsplit_set ("", ",", 0), NULL);
851 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
852 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
853 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
854 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
855 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
856 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
857 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
858 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
859 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
860 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
862 strv_check (g_strsplit_set ("", ",", 1), NULL);
863 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
864 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
865 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
866 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
867 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
868 strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
869 strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
870 strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
871 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
872 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
873 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
875 strv_check (g_strsplit_set ("", ",", 2), NULL);
876 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
877 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
878 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
879 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
880 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
881 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
882 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
883 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
884 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
885 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
887 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
891 test_strv_length (void)
896 if (g_test_undefined ())
898 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
899 "*assertion*!= NULL*");
900 l = g_strv_length (NULL);
901 g_test_assert_expected_messages ();
902 g_assert_cmpint (l, ==, 0);
905 strv = g_strsplit ("1,2,3,4", ",", -1);
906 l = g_strv_length (strv);
907 g_assert_cmpuint (l, ==, 4);
911 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
914 check_strtod_string (gchar *number,
923 /* we try a copy of number, with some free space for malloc before that.
924 * This is supposed to smash the some wrong pointer calculations. */
926 dummy = g_malloc (100000);
927 number = g_strdup (number);
930 for (l = 0; l < G_N_ELEMENTS (locales); l++)
932 gchar *end = "(unset)";
934 setlocale (LC_NUMERIC, locales[l]);
935 d = g_ascii_strtod (number, &end);
936 g_assert (isnan (res) ? isnan (d) : (d == res));
937 g_assert ((end - number) == (check_end ? correct_len : strlen (number)));
944 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
947 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
949 for (l = 0; l < G_N_ELEMENTS (locales); l++)
951 setlocale (LC_ALL, locales[l]);
952 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
953 g_assert_cmpstr (buf, ==, str);
960 gdouble d, our_nan, our_inf;
961 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
966 /* Do this before any call to setlocale. */
967 our_nan = atof ("NaN");
969 g_assert (isnan (our_nan));
974 our_inf = atof ("Infinity");
976 g_assert (our_inf > 1 && our_inf == our_inf / 2);
978 check_strtod_string ("123.123", 123.123, FALSE, 0);
979 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
980 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
981 check_strtod_string ("-123.123", -123.123, FALSE, 0);
982 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
983 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
984 check_strtod_string ("5.4", 5.4, TRUE, 3);
985 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
986 check_strtod_string ("5,4", 5.0, TRUE, 1);
988 /* hex strings for strtod() is a C99 feature which Visual C++ does not support */
989 check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
990 check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
991 check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
992 check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
993 check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
995 /* the following are for #156421 */
996 check_strtod_string ("1e1", 1e1, FALSE, 0);
998 /* NAN/-nan/INF/-infinity strings for strtod() are C99 features which Visual C++ does not support */
999 check_strtod_string ("NAN", our_nan, FALSE, 0);
1000 check_strtod_string ("-nan", -our_nan, FALSE, 0);
1001 check_strtod_string ("INF", our_inf, FALSE, 0);
1002 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
1004 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
1007 /* the values of d in the following 2 tests generate a C1064 compiler limit error */
1008 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1009 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1011 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1012 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1015 d = pow (2.0, -1024.1);
1016 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1018 d = -pow (2.0, -1024.1);
1019 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1022 check_strtod_string (" 0.75", 0.75, FALSE, 0);
1023 check_strtod_string (" +0.75", 0.75, FALSE, 0);
1024 check_strtod_string (" -0.75", -0.75, FALSE, 0);
1025 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
1026 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
1027 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
1028 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
1031 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1032 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1036 check_strtod_number (0.75, "%0.2f", "0.75");
1037 check_strtod_number (0.75, "%5.2f", " 0.75");
1038 check_strtod_number (-0.75, "%0.2f", "-0.75");
1039 check_strtod_number (-0.75, "%5.2f", "-0.75");
1041 check_strtod_number (1e99, "%0.e", "1e+099");
1043 check_strtod_number (1e99, "%.0e", "1e+99");
1048 check_uint64 (const gchar *str,
1055 gchar *endptr = NULL;
1059 actual = g_ascii_strtoull (str, &endptr, base);
1062 g_assert (actual == result);
1063 g_assert_cmpstr (end, ==, endptr);
1064 g_assert (err == error);
1068 check_int64 (const gchar *str,
1075 gchar *endptr = NULL;
1079 actual = g_ascii_strtoll (str, &endptr, base);
1082 g_assert (actual == result);
1083 g_assert_cmpstr (end, ==, endptr);
1084 g_assert (err == error);
1090 check_uint64 ("0", "", 10, 0, 0);
1091 check_uint64 ("+0", "", 10, 0, 0);
1092 check_uint64 ("-0", "", 10, 0, 0);
1093 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1094 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1095 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1096 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1098 check_int64 ("0", "", 10, 0, 0);
1099 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1100 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1101 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1102 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1103 check_int64 ("32768", "", 10, 32768, 0);
1104 check_int64 ("-32768", "", 10, -32768, 0);
1105 check_int64 ("001", "", 10, 1, 0);
1106 check_int64 ("-001", "", 10, -1, 0);
1112 GMappedFile *file, *before, *after;
1118 /* if we allocate the file between two others and then free those
1119 * other two, then hopefully we end up with unmapped memory on either
1122 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1124 /* quick workaround until #549783 can be fixed */
1128 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1129 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1130 g_mapped_file_unref (before);
1131 g_mapped_file_unref (after);
1133 g_assert (file != NULL);
1134 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1135 string = g_mapped_file_get_contents (file);
1137 /* ensure they're all non-nul */
1138 g_assert (memchr (string, '\0', 4096) == NULL);
1140 /* test set 1: ensure that nothing goes past its maximum length, even in
1141 * light of a missing nul terminator.
1143 * we try to test all of the 'n' functions here.
1145 tmp = g_strndup (string, 4096);
1146 g_assert_cmpint (strlen (tmp), ==, 4096);
1149 /* found no bugs in gnome, i hope :) */
1150 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1151 g_strstr_len (string, 4096, "B");
1152 g_strstr_len (string, 4096, ".");
1153 g_strstr_len (string, 4096, "");
1155 g_strrstr_len (string, 4096, "BUGS");
1156 g_strrstr_len (string, 4096, "B");
1157 g_strrstr_len (string, 4096, ".");
1158 g_strrstr_len (string, 4096, "");
1160 tmp = g_ascii_strup (string, 4096);
1161 tmp2 = g_ascii_strup (tmp, 4096);
1162 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1163 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1164 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1168 tmp = g_ascii_strdown (string, 4096);
1169 tmp2 = g_ascii_strdown (tmp, 4096);
1170 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1171 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1172 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1176 tmp = g_markup_escape_text (string, 4096);
1179 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1181 g_assert_cmpint (string[4095], ==, '\n');
1182 string[4095] = '\0';
1184 tmp = g_strdup (string);
1185 g_assert_cmpint (strlen (tmp), ==, 4095);
1188 tmp = g_strndup (string, 10000);
1189 g_assert_cmpint (strlen (tmp), ==, 4095);
1192 g_stpcpy (buffer, string);
1193 g_assert_cmpint (strlen (buffer), ==, 4095);
1195 g_strstr_len (string, 10000, "BUGS");
1196 g_strstr_len (string, 10000, "B");
1197 g_strstr_len (string, 10000, ".");
1198 g_strstr_len (string, 10000, "");
1200 g_strrstr (string, "BUGS");
1201 g_strrstr (string, "B");
1202 g_strrstr (string, ".");
1203 g_strrstr (string, "");
1205 g_strrstr_len (string, 10000, "BUGS");
1206 g_strrstr_len (string, 10000, "B");
1207 g_strrstr_len (string, 10000, ".");
1208 g_strrstr_len (string, 10000, "");
1210 g_str_has_prefix (string, "this won't do very much...");
1211 g_str_has_suffix (string, "but maybe this will...");
1212 g_str_has_suffix (string, "HMMMM.");
1213 g_str_has_suffix (string, "MMMM.");
1214 g_str_has_suffix (string, "M.");
1216 g_strlcpy (buffer, string, sizeof buffer);
1217 g_assert_cmpint (strlen (buffer), ==, 4095);
1218 g_strlcpy (buffer, string, sizeof buffer);
1220 g_strlcat (buffer, string, sizeof buffer);
1221 g_assert_cmpint (strlen (buffer), ==, 4095);
1223 tmp = g_strdup_printf ("<%s>", string);
1224 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1227 tmp = g_ascii_strdown (string, -1);
1228 tmp2 = g_ascii_strdown (tmp, -1);
1229 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1230 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1231 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1232 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1233 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1237 tmp = g_ascii_strup (string, -1);
1238 tmp2 = g_ascii_strup (string, -1);
1239 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1240 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1241 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1242 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1243 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1247 g_ascii_strcasecmp (string, string);
1248 g_ascii_strncasecmp (string, string, 10000);
1250 g_strreverse (string);
1251 g_strreverse (string);
1253 g_strchomp (string);
1254 g_strstrip (string);
1255 g_assert_cmpint (strlen (string), ==, 4095);
1257 g_strdelimit (string, "M", 'N');
1258 g_strcanon (string, " N.", ':');
1259 g_assert_cmpint (strlen (string), ==, 4095);
1261 array = g_strsplit (string, ".", -1);
1262 tmp = g_strjoinv (".", array);
1265 g_assert_cmpint (strlen (tmp), ==, 4095);
1266 g_assert (memcmp (tmp, string, 4095) == 0);
1269 tmp = g_strconcat (string, string, string, NULL);
1270 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1273 tmp = g_strjoin ("!", string, string, NULL);
1274 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1277 tmp = g_markup_escape_text (string, -1);
1280 tmp = g_markup_printf_escaped ("%s", string);
1283 tmp = g_strescape (string, NULL);
1284 tmp2 = g_strcompress (tmp);
1285 g_assert_cmpstr (string, ==, tmp2);
1289 g_mapped_file_unref (file);
1293 test_strip_context (void)
1296 const gchar *msgval;
1302 s = g_strip_context (msgid, msgval);
1303 g_assert (s == msgval);
1305 msgid = msgval = "blabla";
1306 s = g_strip_context (msgid, msgval);
1307 g_assert (s == msgval);
1309 msgid = msgval = "blabla|foo";
1310 s = g_strip_context (msgid, msgval);
1311 g_assert (s == msgval + 7);
1313 msgid = msgval = "blabla||bar";
1314 s = g_strip_context (msgid, msgval);
1315 g_assert (s == msgval + 7);
1319 test_strerror (void)
1324 for (i = 1; i < 100; i++)
1326 str = g_strerror (i);
1327 g_assert (str != NULL);
1328 g_assert (g_utf8_validate (str, -1, NULL));
1333 test_strsignal (void)
1338 for (i = 1; i < 20; i++)
1340 str = g_strsignal (i);
1341 g_assert (str != NULL);
1342 g_assert (g_utf8_validate (str, -1, NULL));
1351 s = g_strdup ("lower");
1352 g_assert_cmpstr (g_strup (s), ==, "LOWER");
1353 g_assert_cmpstr (g_strdown (s), ==, "lower");
1354 g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1362 g_test_init (&argc, &argv, NULL);
1364 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1365 g_test_add_func ("/strfuncs/strdup", test_strdup);
1366 g_test_add_func ("/strfuncs/strndup", test_strndup);
1367 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1368 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1369 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1370 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1371 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1372 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1373 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1374 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1375 g_test_add_func ("/strfuncs/strchug", test_strchug);
1376 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1377 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1378 g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
1379 g_test_add_func ("/strfuncs/strstr", test_strstr);
1380 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1381 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1382 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1383 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1384 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1385 g_test_add_func ("/strfuncs/strtod", test_strtod);
1386 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1387 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1388 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1389 g_test_add_func ("/strfuncs/strerror", test_strerror);
1390 g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1391 g_test_add_func ("/strfuncs/strup", test_strup);
1393 return g_test_run();