1 /* Unit tests for gstrfuncs
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
32 #define GLIB_TEST_STRING "el dorado "
34 #define FOR_ALL_CTYPE(macro) \
47 #define DEFINE_CALL_CTYPE(function) \
49 call_##function (int c) \
51 return function (c); \
54 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
56 call_g_ascii_##function (gchar c) \
58 return g_ascii_##function (c); \
61 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
62 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
65 test_is_function (const char *name,
66 gboolean (* ascii_function) (gchar),
67 int (* c_library_function) (int),
68 gboolean (* unicode_function) (gunichar))
72 for (c = 0; c <= 0x7F; c++)
74 gboolean ascii_result = ascii_function ((gchar)c);
75 gboolean c_library_result = c_library_function (c) != 0;
76 gboolean unicode_result = unicode_function ((gunichar) c);
77 if (ascii_result != c_library_result && c != '\v')
79 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
80 name, ascii_result, name, c_library_result, c);
82 if (ascii_result != unicode_result)
84 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
85 name, ascii_result, name, unicode_result, c);
88 for (c = 0x80; c <= 0xFF; c++)
90 gboolean ascii_result = ascii_function ((gchar)c);
93 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
99 test_to_function (const char *name,
100 gchar (* ascii_function) (gchar),
101 int (* c_library_function) (int),
102 gunichar (* unicode_function) (gunichar))
106 for (c = 0; c <= 0x7F; c++)
108 int ascii_result = (guchar) ascii_function ((gchar) c);
109 int c_library_result = c_library_function (c);
110 int unicode_result = unicode_function ((gunichar) c);
111 if (ascii_result != c_library_result)
113 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
114 name, ascii_result, name, c_library_result, c);
116 if (ascii_result != unicode_result)
118 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
119 name, ascii_result, name, unicode_result, c);
122 for (c = 0x80; c <= 0xFF; c++)
124 int ascii_result = (guchar) ascii_function ((gchar) c);
125 if (ascii_result != c)
127 g_error ("g_ascii_%s returned 0x%X for 0x%X",
128 name, ascii_result, c);
134 test_digit_function (const char *name,
135 int (* ascii_function) (gchar),
136 int (* unicode_function) (gunichar))
140 for (c = 0; c <= 0x7F; c++)
142 int ascii_result = ascii_function ((gchar) c);
143 int unicode_result = unicode_function ((gunichar) c);
144 if (ascii_result != unicode_result)
146 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
147 name, ascii_result, name, unicode_result, c);
150 for (c = 0x80; c <= 0xFF; c++)
152 int ascii_result = ascii_function ((gchar) c);
153 if (ascii_result != -1)
155 g_error ("g_ascii_%s_value returned %d for 0x%X",
156 name, ascii_result, c);
162 test_is_to_digit (void)
164 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
166 FOR_ALL_CTYPE(TEST_IS)
170 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
177 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
190 str = g_strdup (NULL);
191 g_assert (str == NULL);
193 str = g_strdup (GLIB_TEST_STRING);
194 g_assert (str != NULL);
195 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
204 str = g_strndup (NULL, 3);
205 g_assert (str == NULL);
207 str = g_strndup ("aaaa", 5);
208 g_assert (str != NULL);
209 g_assert_cmpstr (str, ==, "aaaa");
212 str = g_strndup ("aaaa", 2);
213 g_assert (str != NULL);
214 g_assert_cmpstr (str, ==, "aa");
219 test_strdup_printf (void)
223 str = g_strdup_printf ("%05d %-5s", 21, "test");
224 g_assert (str != NULL);
225 g_assert_cmpstr (str, ==, "00021 test ");
232 gchar *vec[] = { "Foo", "Bar", NULL };
235 copy = g_strdupv (NULL);
236 g_assert (copy == NULL);
238 copy = g_strdupv (vec);
239 g_assert (copy != NULL);
240 g_assert_cmpstr (copy[0], ==, "Foo");
241 g_assert_cmpstr (copy[1], ==, "Bar");
242 g_assert (copy[2] == NULL);
251 str = g_strnfill (0, 'a');
252 g_assert (str != NULL);
253 g_assert (*str == '\0');
256 str = g_strnfill (5, 'a');
257 g_assert (str != NULL);
258 g_assert_cmpstr (str, ==, "aaaaa");
263 test_strconcat (void)
267 str = g_strconcat (GLIB_TEST_STRING, NULL);
268 g_assert (str != NULL);
269 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
272 str = g_strconcat (GLIB_TEST_STRING,
276 g_assert (str != NULL);
277 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
286 str = g_strjoin (NULL, NULL);
287 g_assert (str != NULL);
288 g_assert (*str == '\0');
291 str = g_strjoin (":", NULL);
292 g_assert (str != NULL);
293 g_assert (*str == '\0');
296 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
297 g_assert (str != NULL);
298 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
301 str = g_strjoin (NULL,
306 g_assert (str != NULL);
307 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
310 str = g_strjoin (":",
315 g_assert (str != NULL);
316 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
325 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
327 str = g_strcanon (NULL, "ab", 'y');
329 g_test_trap_assert_failed ();
331 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
333 str = g_strdup ("abxabxab");
334 str = g_strcanon (str, NULL, 'y');
337 g_test_trap_assert_failed ();
339 str = g_strdup ("abxabxab");
340 str = g_strcanon (str, "ab", 'y');
341 g_assert (str != NULL);
342 g_assert_cmpstr (str, ==, "abyabyab");
347 test_strcompress_strescape (void)
353 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
355 str = g_strcompress (NULL);
357 g_test_trap_assert_failed ();
359 /* trailing slashes are not allowed */
360 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
362 str = g_strcompress ("abc\\");
364 g_test_trap_assert_failed ();
366 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
367 g_assert (str != NULL);
368 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
372 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
374 str = g_strescape (NULL, NULL);
376 g_test_trap_assert_failed ();
378 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
379 g_assert (str != NULL);
380 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
383 str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
384 "\b\f\001\002\003\004");
385 g_assert (str != NULL);
386 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
390 tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
391 str = g_strcompress (tmp);
392 g_assert (str != NULL);
393 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
399 test_ascii_strcasecmp (void)
403 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
405 res = g_ascii_strcasecmp ("foo", NULL);
407 g_test_trap_assert_failed ();
409 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
411 res = g_ascii_strcasecmp (NULL, "foo");
413 g_test_trap_assert_failed ();
415 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
416 g_assert_cmpint (res, ==, 0);
418 res = g_ascii_strcasecmp ("frobozz", "frobozz");
419 g_assert_cmpint (res, ==, 0);
421 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
422 g_assert_cmpint (res, ==, 0);
424 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
425 g_assert_cmpint (res, !=, 0);
427 res = g_ascii_strcasecmp ("", "");
428 g_assert_cmpint (res, ==, 0);
430 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
431 g_assert_cmpint (res, ==, 0);
433 res = g_ascii_strcasecmp ("a", "b");
434 g_assert_cmpint (res, <, 0);
436 res = g_ascii_strcasecmp ("a", "B");
437 g_assert_cmpint (res, <, 0);
439 res = g_ascii_strcasecmp ("A", "b");
440 g_assert_cmpint (res, <, 0);
442 res = g_ascii_strcasecmp ("A", "B");
443 g_assert_cmpint (res, <, 0);
445 res = g_ascii_strcasecmp ("b", "a");
446 g_assert_cmpint (res, >, 0);
448 res = g_ascii_strcasecmp ("b", "A");
449 g_assert_cmpint (res, >, 0);
451 res = g_ascii_strcasecmp ("B", "a");
452 g_assert_cmpint (res, >, 0);
454 res = g_ascii_strcasecmp ("B", "A");
455 g_assert_cmpint (res, >, 0);
459 do_test_strchug (const gchar *str, const gchar *expected)
464 tmp = g_strdup (str);
467 res = (strcmp (tmp, expected) == 0);
470 g_assert_cmpint (res, ==, TRUE);
476 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
480 g_test_trap_assert_failed ();
482 do_test_strchug ("", "");
483 do_test_strchug (" ", "");
484 do_test_strchug ("\t\r\n ", "");
485 do_test_strchug (" a", "a");
486 do_test_strchug (" a", "a");
487 do_test_strchug ("a a", "a a");
488 do_test_strchug (" a a", "a a");
492 do_test_strchomp (const gchar *str, const gchar *expected)
497 tmp = g_strdup (str);
500 res = (strcmp (tmp, expected) == 0);
503 g_assert_cmpint (res, ==, TRUE);
509 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
513 g_test_trap_assert_failed ();
515 do_test_strchomp ("", "");
516 do_test_strchomp (" ", "");
517 do_test_strchomp (" \t\r\n", "");
518 do_test_strchomp ("a ", "a");
519 do_test_strchomp ("a ", "a");
520 do_test_strchomp ("a a", "a a");
521 do_test_strchomp ("a a ", "a a");
525 test_strreverse (void)
530 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
532 str = g_strreverse (NULL);
534 g_test_trap_assert_failed ();
536 str = p = g_strdup ("abcde");
537 str = g_strreverse (str);
538 g_assert (str != NULL);
540 g_assert_cmpstr (str, ==, "edcba");
550 haystack = g_strdup ("FooBarFooBarFoo");
553 res = g_strstr_len (haystack, 6, "xxx");
554 g_assert (res == NULL);
556 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
557 g_assert (res == NULL);
559 res = g_strstr_len (haystack, 6, "");
560 g_assert (res == haystack);
562 res = g_strstr_len (haystack, 6, "Bar");
563 g_assert_cmpstr (res, ==, "BarFooBarFoo");
566 res = g_strrstr (haystack, "xxx");
567 g_assert (res == NULL);
569 res = g_strrstr (haystack, "FooBarFooBarFooBar");
570 g_assert (res == NULL);
572 res = g_strrstr (haystack, "");
573 g_assert (res == haystack);
575 res = g_strrstr (haystack, "Bar");
576 g_assert_cmpstr (res, ==, "BarFoo");
579 res = g_strrstr_len (haystack, 14, "xxx");
580 g_assert (res == NULL);
582 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
583 g_assert (res == NULL);
585 res = g_strrstr_len (haystack, 14, "BarFoo");
586 g_assert_cmpstr (res, ==, "BarFooBarFoo");
592 test_has_prefix (void)
596 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
598 res = g_str_has_prefix ("foo", NULL);
600 g_test_trap_assert_failed ();
602 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
604 res = g_str_has_prefix (NULL, "foo");
606 g_test_trap_assert_failed ();
608 res = g_str_has_prefix ("foo", "bar");
609 g_assert_cmpint (res, ==, FALSE);
611 res = g_str_has_prefix ("foo", "foobar");
612 g_assert_cmpint (res, ==, FALSE);
614 res = g_str_has_prefix ("foobar", "bar");
615 g_assert_cmpint (res, ==, FALSE);
617 res = g_str_has_prefix ("foobar", "foo");
618 g_assert_cmpint (res, ==, TRUE);
620 res = g_str_has_prefix ("foo", "");
621 g_assert_cmpint (res, ==, TRUE);
623 res = g_str_has_prefix ("foo", "foo");
624 g_assert_cmpint (res, ==, TRUE);
626 res = g_str_has_prefix ("", "");
627 g_assert_cmpint (res, ==, TRUE);
631 test_has_suffix (void)
635 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
637 res = g_str_has_suffix ("foo", NULL);
639 g_test_trap_assert_failed ();
641 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
643 res = g_str_has_suffix (NULL, "foo");
645 g_test_trap_assert_failed ();
647 res = g_str_has_suffix ("foo", "bar");
648 g_assert_cmpint (res, ==, FALSE);
650 res = g_str_has_suffix ("bar", "foobar");
651 g_assert_cmpint (res, ==, FALSE);
653 res = g_str_has_suffix ("foobar", "foo");
654 g_assert_cmpint (res, ==, FALSE);
656 res = g_str_has_suffix ("foobar", "bar");
657 g_assert_cmpint (res, ==, TRUE);
659 res = g_str_has_suffix ("foo", "");
660 g_assert_cmpint (res, ==, TRUE);
662 res = g_str_has_suffix ("foo", "foo");
663 g_assert_cmpint (res, ==, TRUE);
665 res = g_str_has_suffix ("", "");
666 g_assert_cmpint (res, ==, TRUE);
670 strv_check (gchar **strv, ...)
676 va_start (list, strv);
679 const gchar *str = va_arg (list, const char *);
682 g_assert (str == NULL);
691 g_assert_cmpstr (strv[i], ==, str);
703 strv_check (g_strsplit ("", ",", 0), NULL);
704 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
705 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
706 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
707 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
708 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
709 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
710 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
711 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
712 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
713 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
714 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
716 strv_check (g_strsplit ("", ",", 1), NULL);
717 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
718 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
719 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
720 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
721 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
722 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
723 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
724 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
725 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
726 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
727 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
729 strv_check (g_strsplit ("", ",", 2), NULL);
730 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
731 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
732 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
733 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
734 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
735 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
736 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
737 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
738 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
739 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
740 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
744 test_strsplit_set (void)
746 strv_check (g_strsplit_set ("", ",/", 0), NULL);
747 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
748 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
749 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
750 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
752 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
753 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
754 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
755 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
756 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
757 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
758 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
759 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
761 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
762 strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
763 strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
764 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
765 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
766 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
768 strv_check (g_strsplit_set ("", ",", 0), NULL);
769 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
770 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
771 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
772 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
773 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
774 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
775 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
776 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
777 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
778 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
780 strv_check (g_strsplit_set ("", ",", 1), NULL);
781 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
782 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
783 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
784 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
785 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
786 strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
787 strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
788 strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
789 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
790 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
791 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
793 strv_check (g_strsplit_set ("", ",", 2), NULL);
794 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
795 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
796 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
797 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
798 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
799 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
800 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
801 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
802 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
803 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
805 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
809 test_strv_length (void)
813 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
815 l = g_strv_length (NULL);
817 g_test_trap_assert_failed ();
819 l = g_strv_length (g_strsplit ("1,2,3,4", ",", -1));
820 g_assert_cmpuint (l, ==, 4);
823 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
826 check_strtod_string (gchar *number,
835 /* we try a copy of number, with some free space for malloc before that.
836 * This is supposed to smash the some wrong pointer calculations. */
838 dummy = g_malloc (100000);
839 number = g_strdup (number);
842 for (l = 0; l < G_N_ELEMENTS (locales); l++)
845 gchar *end = "(unset)";
847 setlocale (LC_NUMERIC, locales[l]);
848 d = g_ascii_strtod (number, &end);
849 ok = isnan (res) ? isnan (d) : (d == res);
852 g_error ("g_ascii_strtod on \"%s\" for locale %s failed\n" \
853 "expected %f (nan %d) actual %f (nan %d)\n",
859 ok = (end - number) == (check_end ? correct_len : strlen (number));
862 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
864 else if (end >= number && end <= number + strlen (number))
865 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
866 number, locales[l], end);
868 g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
869 number, locales[l], number, end);
877 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
880 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
882 for (l = 0; l < G_N_ELEMENTS (locales); l++)
884 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
885 g_assert_cmpstr (buf, ==, str);
892 gdouble d, our_nan, our_inf;
893 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
898 /* Do this before any call to setlocale. */
899 our_nan = atof ("NaN");
901 g_assert (isnan (our_nan));
906 our_inf = atof ("Infinity");
908 g_assert (our_inf > 1 && our_inf == our_inf / 2);
910 check_strtod_string ("123.123", 123.123, FALSE, 0);
911 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
912 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
913 check_strtod_string ("-123.123", -123.123, FALSE, 0);
914 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
915 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
916 check_strtod_string ("5.4", 5.4, TRUE, 3);
917 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
918 check_strtod_string ("5,4", 5.0, TRUE, 1);
919 /* the following are for #156421 */
920 check_strtod_string ("1e1", 1e1, FALSE, 0);
921 check_strtod_string ("NAN", our_nan, FALSE, 0);
922 check_strtod_string ("-nan", -our_nan, FALSE, 0);
923 check_strtod_string ("INF", our_inf, FALSE, 0);
924 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
925 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
927 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
928 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
930 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
931 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
933 d = pow (2.0, -1024.1);
934 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
936 d = -pow (2.0, -1024.1);
937 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
940 check_strtod_string (" 0.75", 0.75, FALSE, 0);
941 check_strtod_string (" +0.75", 0.75, FALSE, 0);
942 check_strtod_string (" -0.75", -0.75, FALSE, 0);
943 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
944 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
945 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
946 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
949 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
950 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
954 check_strtod_number (0.75, "%0.2f", "0.75");
955 check_strtod_number (0.75, "%5.2f", " 0.75");
956 check_strtod_number (-0.75, "%0.2f", "-0.75");
957 check_strtod_number (-0.75, "%5.2f", "-0.75");
958 check_strtod_number (1e99, "%.0e", "1e+99");
962 check_uint64 (const gchar *str,
969 gchar *endptr = NULL;
973 actual = g_ascii_strtoull (str, &endptr, base);
976 g_assert (actual == result);
977 g_assert_cmpstr (end, ==, endptr);
978 g_assert (err == error);
982 check_int64 (const gchar *str,
989 gchar *endptr = NULL;
993 actual = g_ascii_strtoll (str, &endptr, base);
996 g_assert (actual == result);
997 g_assert_cmpstr (end, ==, endptr);
998 g_assert (err == error);
1004 check_uint64 ("0", "", 10, 0, 0);
1005 check_uint64 ("+0", "", 10, 0, 0);
1006 check_uint64 ("-0", "", 10, 0, 0);
1007 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1008 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1009 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1010 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1012 check_int64 ("0", "", 10, 0, 0);
1013 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1014 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1015 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1016 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1017 check_int64 ("32768", "", 10, 32768, 0);
1018 check_int64 ("-32768", "", 10, -32768, 0);
1019 check_int64 ("001", "", 10, 1, 0);
1020 check_int64 ("-001", "", 10, -1, 0);
1027 g_test_init (&argc, &argv, NULL);
1029 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1030 g_test_add_func ("/strfuncs/strdup", test_strdup);
1031 g_test_add_func ("/strfuncs/strndup", test_strndup);
1032 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1033 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1034 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1035 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1036 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1037 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1038 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1039 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1040 g_test_add_func ("/strfuncs/strchug", test_strchug);
1041 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1042 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1043 g_test_add_func ("/strfuncs/strstr", test_strstr);
1044 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1045 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1046 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1047 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1048 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1049 g_test_add_func ("/strfuncs/strtod", test_strtod);
1050 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1052 return g_test_run();