1 /* Unit tests for gstrfuncs
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24 #define _XOPEN_SOURCE 600
35 #define GLIB_TEST_STRING "el dorado "
37 #define FOR_ALL_CTYPE(macro) \
50 #define DEFINE_CALL_CTYPE(function) \
52 call_##function (int c) \
54 return function (c); \
57 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
59 call_g_ascii_##function (gchar c) \
61 return g_ascii_##function (c); \
64 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE
)
65 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE
)
68 test_is_function (const char *name
,
69 gboolean (* ascii_function
) (gchar
),
70 int (* c_library_function
) (int),
71 gboolean (* unicode_function
) (gunichar
))
75 for (c
= 0; c
<= 0x7F; c
++)
77 gboolean ascii_result
= ascii_function ((gchar
)c
);
78 gboolean c_library_result
= c_library_function (c
) != 0;
79 gboolean unicode_result
= unicode_function ((gunichar
) c
);
80 if (ascii_result
!= c_library_result
&& c
!= '\v')
82 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
83 name
, ascii_result
, name
, c_library_result
, c
);
85 if (ascii_result
!= unicode_result
)
87 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
88 name
, ascii_result
, name
, unicode_result
, c
);
91 for (c
= 0x80; c
<= 0xFF; c
++)
93 gboolean ascii_result
= ascii_function ((gchar
)c
);
96 g_error ("g_ascii_%s returned TRUE for 0x%X", name
, c
);
102 test_to_function (const char *name
,
103 gchar (* ascii_function
) (gchar
),
104 int (* c_library_function
) (int),
105 gunichar (* unicode_function
) (gunichar
))
109 for (c
= 0; c
<= 0x7F; c
++)
111 int ascii_result
= (guchar
) ascii_function ((gchar
) c
);
112 int c_library_result
= c_library_function (c
);
113 int unicode_result
= unicode_function ((gunichar
) c
);
114 if (ascii_result
!= c_library_result
)
116 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
117 name
, ascii_result
, name
, c_library_result
, c
);
119 if (ascii_result
!= unicode_result
)
121 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
122 name
, ascii_result
, name
, unicode_result
, c
);
125 for (c
= 0x80; c
<= 0xFF; c
++)
127 int ascii_result
= (guchar
) ascii_function ((gchar
) c
);
128 if (ascii_result
!= c
)
130 g_error ("g_ascii_%s returned 0x%X for 0x%X",
131 name
, ascii_result
, c
);
137 test_digit_function (const char *name
,
138 int (* ascii_function
) (gchar
),
139 int (* unicode_function
) (gunichar
))
143 for (c
= 0; c
<= 0x7F; c
++)
145 int ascii_result
= ascii_function ((gchar
) c
);
146 int unicode_result
= unicode_function ((gunichar
) c
);
147 if (ascii_result
!= unicode_result
)
149 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
150 name
, ascii_result
, name
, unicode_result
, c
);
153 for (c
= 0x80; c
<= 0xFF; c
++)
155 int ascii_result
= ascii_function ((gchar
) c
);
156 if (ascii_result
!= -1)
158 g_error ("g_ascii_%s_value returned %d for 0x%X",
159 name
, ascii_result
, c
);
165 test_is_to_digit (void)
167 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
169 FOR_ALL_CTYPE(TEST_IS
)
173 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
180 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
193 str
= g_strdup (NULL
);
194 g_assert (str
== NULL
);
196 str
= g_strdup (GLIB_TEST_STRING
);
197 g_assert (str
!= NULL
);
198 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING
);
207 str
= g_strndup (NULL
, 3);
208 g_assert (str
== NULL
);
210 str
= g_strndup ("aaaa", 5);
211 g_assert (str
!= NULL
);
212 g_assert_cmpstr (str
, ==, "aaaa");
215 str
= g_strndup ("aaaa", 2);
216 g_assert (str
!= NULL
);
217 g_assert_cmpstr (str
, ==, "aa");
222 test_strdup_printf (void)
226 str
= g_strdup_printf ("%05d %-5s", 21, "test");
227 g_assert (str
!= NULL
);
228 g_assert_cmpstr (str
, ==, "00021 test ");
235 gchar
*vec
[] = { "Foo", "Bar", NULL
};
238 copy
= g_strdupv (NULL
);
239 g_assert (copy
== NULL
);
241 copy
= g_strdupv (vec
);
242 g_assert (copy
!= NULL
);
243 g_assert_cmpstr (copy
[0], ==, "Foo");
244 g_assert_cmpstr (copy
[1], ==, "Bar");
245 g_assert (copy
[2] == NULL
);
254 str
= g_strnfill (0, 'a');
255 g_assert (str
!= NULL
);
256 g_assert (*str
== '\0');
259 str
= g_strnfill (5, 'a');
260 g_assert (str
!= NULL
);
261 g_assert_cmpstr (str
, ==, "aaaaa");
266 test_strconcat (void)
270 str
= g_strconcat (GLIB_TEST_STRING
, NULL
);
271 g_assert (str
!= NULL
);
272 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING
);
275 str
= g_strconcat (GLIB_TEST_STRING
,
279 g_assert (str
!= NULL
);
280 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING
);
283 g_assert (g_strconcat (NULL
, "bla", NULL
) == NULL
);
291 str
= g_strjoin (NULL
, NULL
);
292 g_assert (str
!= NULL
);
293 g_assert (*str
== '\0');
296 str
= g_strjoin (":", NULL
);
297 g_assert (str
!= NULL
);
298 g_assert (*str
== '\0');
301 str
= g_strjoin (NULL
, GLIB_TEST_STRING
, NULL
);
302 g_assert (str
!= NULL
);
303 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING
);
306 str
= g_strjoin (NULL
,
311 g_assert (str
!= NULL
);
312 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING
);
315 str
= g_strjoin (":",
320 g_assert (str
!= NULL
);
321 g_assert_cmpstr (str
, ==, GLIB_TEST_STRING
":" GLIB_TEST_STRING
":" GLIB_TEST_STRING
);
330 if (g_test_undefined ())
332 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
334 str
= g_strcanon (NULL
, "ab", 'y');
336 g_test_trap_assert_failed ();
338 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
340 str
= g_strdup ("abxabxab");
341 str
= g_strcanon (str
, NULL
, 'y');
344 g_test_trap_assert_failed ();
347 str
= g_strdup ("abxabxab");
348 str
= g_strcanon (str
, "ab", 'y');
349 g_assert (str
!= NULL
);
350 g_assert_cmpstr (str
, ==, "abyabyab");
355 test_strcompress_strescape (void)
361 if (g_test_undefined ())
363 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
365 str
= g_strcompress (NULL
);
367 g_test_trap_assert_failed ();
369 /* trailing slashes are not allowed */
370 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
372 str
= g_strcompress ("abc\\");
374 g_test_trap_assert_failed ();
377 str
= g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
378 g_assert (str
!= NULL
);
379 g_assert_cmpstr (str
, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
383 if (g_test_undefined ())
385 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
387 str
= g_strescape (NULL
, NULL
);
389 g_test_trap_assert_failed ();
392 str
= g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL
);
393 g_assert (str
!= NULL
);
394 g_assert_cmpstr (str
, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
397 str
= g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
398 "\b\f\001\002\003\004");
399 g_assert (str
!= NULL
);
400 g_assert_cmpstr (str
, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
404 tmp
= g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL
);
405 str
= g_strcompress (tmp
);
406 g_assert (str
!= NULL
);
407 g_assert_cmpstr (str
, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
413 test_ascii_strcasecmp (void)
417 if (g_test_undefined ())
419 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
421 res
= g_ascii_strcasecmp ("foo", NULL
);
423 g_test_trap_assert_failed ();
425 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
427 res
= g_ascii_strcasecmp (NULL
, "foo");
429 g_test_trap_assert_failed ();
432 res
= g_ascii_strcasecmp ("FroboZZ", "frobozz");
433 g_assert_cmpint (res
, ==, 0);
435 res
= g_ascii_strcasecmp ("frobozz", "frobozz");
436 g_assert_cmpint (res
, ==, 0);
438 res
= g_ascii_strcasecmp ("frobozz", "FROBOZZ");
439 g_assert_cmpint (res
, ==, 0);
441 res
= g_ascii_strcasecmp ("FROBOZZ", "froboz");
442 g_assert_cmpint (res
, !=, 0);
444 res
= g_ascii_strcasecmp ("", "");
445 g_assert_cmpint (res
, ==, 0);
447 res
= g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
448 g_assert_cmpint (res
, ==, 0);
450 res
= g_ascii_strcasecmp ("a", "b");
451 g_assert_cmpint (res
, <, 0);
453 res
= g_ascii_strcasecmp ("a", "B");
454 g_assert_cmpint (res
, <, 0);
456 res
= g_ascii_strcasecmp ("A", "b");
457 g_assert_cmpint (res
, <, 0);
459 res
= g_ascii_strcasecmp ("A", "B");
460 g_assert_cmpint (res
, <, 0);
462 res
= g_ascii_strcasecmp ("b", "a");
463 g_assert_cmpint (res
, >, 0);
465 res
= g_ascii_strcasecmp ("b", "A");
466 g_assert_cmpint (res
, >, 0);
468 res
= g_ascii_strcasecmp ("B", "a");
469 g_assert_cmpint (res
, >, 0);
471 res
= g_ascii_strcasecmp ("B", "A");
472 g_assert_cmpint (res
, >, 0);
476 do_test_strchug (const gchar
*str
, const gchar
*expected
)
481 tmp
= g_strdup (str
);
484 res
= (strcmp (tmp
, expected
) == 0);
487 g_assert_cmpint (res
, ==, TRUE
);
493 if (g_test_undefined ())
495 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
499 g_test_trap_assert_failed ();
502 do_test_strchug ("", "");
503 do_test_strchug (" ", "");
504 do_test_strchug ("\t\r\n ", "");
505 do_test_strchug (" a", "a");
506 do_test_strchug (" a", "a");
507 do_test_strchug ("a a", "a a");
508 do_test_strchug (" a a", "a a");
512 do_test_strchomp (const gchar
*str
, const gchar
*expected
)
517 tmp
= g_strdup (str
);
520 res
= (strcmp (tmp
, expected
) == 0);
523 g_assert_cmpint (res
, ==, TRUE
);
529 if (g_test_undefined ())
531 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
535 g_test_trap_assert_failed ();
538 do_test_strchomp ("", "");
539 do_test_strchomp (" ", "");
540 do_test_strchomp (" \t\r\n", "");
541 do_test_strchomp ("a ", "a");
542 do_test_strchomp ("a ", "a");
543 do_test_strchomp ("a a", "a a");
544 do_test_strchomp ("a a ", "a a");
548 test_strreverse (void)
553 if (g_test_undefined ())
555 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
557 str
= g_strreverse (NULL
);
559 g_test_trap_assert_failed ();
562 str
= p
= g_strdup ("abcde");
563 str
= g_strreverse (str
);
564 g_assert (str
!= NULL
);
566 g_assert_cmpstr (str
, ==, "edcba");
576 haystack
= g_strdup ("FooBarFooBarFoo");
579 res
= g_strstr_len (haystack
, 6, "xxx");
580 g_assert (res
== NULL
);
582 res
= g_strstr_len (haystack
, 6, "FooBarFooBarFooBar");
583 g_assert (res
== NULL
);
585 res
= g_strstr_len (haystack
, 3, "Bar");
586 g_assert (res
== NULL
);
588 res
= g_strstr_len (haystack
, 6, "");
589 g_assert (res
== haystack
);
590 g_assert_cmpstr (res
, ==, "FooBarFooBarFoo");
592 res
= g_strstr_len (haystack
, 6, "Bar");
593 g_assert (res
== haystack
+ 3);
594 g_assert_cmpstr (res
, ==, "BarFooBarFoo");
596 res
= g_strstr_len (haystack
, -1, "Bar");
597 g_assert (res
== haystack
+ 3);
598 g_assert_cmpstr (res
, ==, "BarFooBarFoo");
601 res
= g_strrstr (haystack
, "xxx");
602 g_assert (res
== NULL
);
604 res
= g_strrstr (haystack
, "FooBarFooBarFooBar");
605 g_assert (res
== NULL
);
607 res
= g_strrstr (haystack
, "");
608 g_assert (res
== haystack
);
609 g_assert_cmpstr (res
, ==, "FooBarFooBarFoo");
611 res
= g_strrstr (haystack
, "Bar");
612 g_assert (res
== haystack
+ 9);
613 g_assert_cmpstr (res
, ==, "BarFoo");
616 res
= g_strrstr_len (haystack
, 14, "xxx");
617 g_assert (res
== NULL
);
619 res
= g_strrstr_len (haystack
, 14, "FooBarFooBarFooBar");
620 g_assert (res
== NULL
);
622 res
= g_strrstr_len (haystack
, 3, "Bar");
623 g_assert (res
== NULL
);
625 res
= g_strrstr_len (haystack
, 14, "BarFoo");
626 g_assert (res
== haystack
+ 3);
627 g_assert_cmpstr (res
, ==, "BarFooBarFoo");
629 res
= g_strrstr_len (haystack
, 15, "BarFoo");
630 g_assert (res
== haystack
+ 9);
631 g_assert_cmpstr (res
, ==, "BarFoo");
633 res
= g_strrstr_len (haystack
, -1, "BarFoo");
634 g_assert (res
== haystack
+ 9);
635 g_assert_cmpstr (res
, ==, "BarFoo");
637 /* test case for strings with \0 in the middle */
638 *(haystack
+ 7) = '\0';
639 res
= g_strstr_len (haystack
, 15, "BarFoo");
640 g_assert (res
== NULL
);
646 test_has_prefix (void)
650 if (g_test_undefined ())
652 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
654 res
= g_str_has_prefix ("foo", NULL
);
656 g_test_trap_assert_failed ();
658 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
660 res
= g_str_has_prefix (NULL
, "foo");
662 g_test_trap_assert_failed ();
665 res
= g_str_has_prefix ("foo", "bar");
666 g_assert_cmpint (res
, ==, FALSE
);
668 res
= g_str_has_prefix ("foo", "foobar");
669 g_assert_cmpint (res
, ==, FALSE
);
671 res
= g_str_has_prefix ("foobar", "bar");
672 g_assert_cmpint (res
, ==, FALSE
);
674 res
= g_str_has_prefix ("foobar", "foo");
675 g_assert_cmpint (res
, ==, TRUE
);
677 res
= g_str_has_prefix ("foo", "");
678 g_assert_cmpint (res
, ==, TRUE
);
680 res
= g_str_has_prefix ("foo", "foo");
681 g_assert_cmpint (res
, ==, TRUE
);
683 res
= g_str_has_prefix ("", "");
684 g_assert_cmpint (res
, ==, TRUE
);
688 test_has_suffix (void)
692 if (g_test_undefined ())
694 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
696 res
= g_str_has_suffix ("foo", NULL
);
698 g_test_trap_assert_failed ();
700 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
702 res
= g_str_has_suffix (NULL
, "foo");
704 g_test_trap_assert_failed ();
707 res
= g_str_has_suffix ("foo", "bar");
708 g_assert_cmpint (res
, ==, FALSE
);
710 res
= g_str_has_suffix ("bar", "foobar");
711 g_assert_cmpint (res
, ==, FALSE
);
713 res
= g_str_has_suffix ("foobar", "foo");
714 g_assert_cmpint (res
, ==, FALSE
);
716 res
= g_str_has_suffix ("foobar", "bar");
717 g_assert_cmpint (res
, ==, TRUE
);
719 res
= g_str_has_suffix ("foo", "");
720 g_assert_cmpint (res
, ==, TRUE
);
722 res
= g_str_has_suffix ("foo", "foo");
723 g_assert_cmpint (res
, ==, TRUE
);
725 res
= g_str_has_suffix ("", "");
726 g_assert_cmpint (res
, ==, TRUE
);
730 strv_check (gchar
**strv
, ...)
736 va_start (list
, strv
);
739 const gchar
*str
= va_arg (list
, const char *);
742 g_assert (str
== NULL
);
751 g_assert_cmpstr (strv
[i
], ==, str
);
763 strv_check (g_strsplit ("", ",", 0), NULL
);
764 strv_check (g_strsplit ("x", ",", 0), "x", NULL
);
765 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL
);
766 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL
);
767 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL
);
768 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL
);
769 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL
);
770 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL
);
771 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL
);
772 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL
);
773 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL
);
774 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL
);
776 strv_check (g_strsplit ("", ",", 1), NULL
);
777 strv_check (g_strsplit ("x", ",", 1), "x", NULL
);
778 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL
);
779 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL
);
780 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL
);
781 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL
);
782 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL
);
783 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL
);
784 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL
);
785 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL
);
786 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL
);
787 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL
);
789 strv_check (g_strsplit ("", ",", 2), NULL
);
790 strv_check (g_strsplit ("x", ",", 2), "x", NULL
);
791 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL
);
792 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL
);
793 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL
);
794 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL
);
795 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL
);
796 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL
);
797 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL
);
798 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL
);
799 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL
);
800 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL
);
804 test_strsplit_set (void)
806 strv_check (g_strsplit_set ("", ",/", 0), NULL
);
807 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL
);
808 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL
);
809 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL
);
810 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL
);
812 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL
);
813 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL
);
814 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL
);
815 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL
);
816 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL
);
817 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL
);
818 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL
);
819 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "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
);
824 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL
);
825 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL
);
826 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL
);
828 strv_check (g_strsplit_set ("", ",", 0), NULL
);
829 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL
);
830 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL
);
831 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL
);
832 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL
);
833 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL
);
834 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL
);
835 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", 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
);
840 strv_check (g_strsplit_set ("", ",", 1), NULL
);
841 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL
);
842 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL
);
843 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL
);
844 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL
);
845 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", 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
);
849 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL
);
850 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL
);
851 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL
);
853 strv_check (g_strsplit_set ("", ",", 2), NULL
);
854 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL
);
855 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL
);
856 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL
);
857 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL
);
858 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL
);
859 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL
);
860 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL
);
861 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL
);
862 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL
);
863 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL
);
865 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL
);
869 test_strv_length (void)
874 if (g_test_undefined ())
876 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
878 l
= g_strv_length (NULL
);
880 g_test_trap_assert_failed ();
883 strv
= g_strsplit ("1,2,3,4", ",", -1);
884 l
= g_strv_length (strv
);
885 g_assert_cmpuint (l
, ==, 4);
889 static char *locales
[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
892 check_strtod_string (gchar
*number
,
901 /* we try a copy of number, with some free space for malloc before that.
902 * This is supposed to smash the some wrong pointer calculations. */
904 dummy
= g_malloc (100000);
905 number
= g_strdup (number
);
908 for (l
= 0; l
< G_N_ELEMENTS (locales
); l
++)
910 gchar
*end
= "(unset)";
912 setlocale (LC_NUMERIC
, locales
[l
]);
913 d
= g_ascii_strtod (number
, &end
);
914 g_assert (isnan (res
) ? isnan (d
) : (d
== res
));
915 g_assert ((end
- number
) == (check_end
? correct_len
: strlen (number
)));
922 check_strtod_number (gdouble num
, gchar
*fmt
, gchar
*str
)
925 gchar buf
[G_ASCII_DTOSTR_BUF_SIZE
];
927 for (l
= 0; l
< G_N_ELEMENTS (locales
); l
++)
929 setlocale (LC_ALL
, locales
[l
]);
930 g_ascii_formatd (buf
, G_ASCII_DTOSTR_BUF_SIZE
, fmt
, num
);
931 g_assert_cmpstr (buf
, ==, str
);
938 gdouble d
, our_nan
, our_inf
;
939 char buffer
[G_ASCII_DTOSTR_BUF_SIZE
];
944 /* Do this before any call to setlocale. */
945 our_nan
= atof ("NaN");
947 g_assert (isnan (our_nan
));
952 our_inf
= atof ("Infinity");
954 g_assert (our_inf
> 1 && our_inf
== our_inf
/ 2);
956 check_strtod_string ("123.123", 123.123, FALSE
, 0);
957 check_strtod_string ("123.123e2", 123.123e2
, FALSE
, 0);
958 check_strtod_string ("123.123e-2", 123.123e-2, FALSE
, 0);
959 check_strtod_string ("-123.123", -123.123, FALSE
, 0);
960 check_strtod_string ("-123.123e2", -123.123e2
, FALSE
, 0);
961 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE
, 0);
962 check_strtod_string ("5.4", 5.4, TRUE
, 3);
963 check_strtod_string ("5.4,5.5", 5.4, TRUE
, 3);
964 check_strtod_string ("5,4", 5.0, TRUE
, 1);
965 check_strtod_string ("0xa.b", 10.6875, TRUE
, 5);
966 check_strtod_string ("0xa.bP3", 85.5, TRUE
, 7);
967 check_strtod_string ("0xa.bp+3", 85.5, TRUE
, 8);
968 check_strtod_string ("0xa.bp-2", 2.671875, TRUE
, 8);
969 check_strtod_string ("0xA.BG", 10.6875, TRUE
, 5);
970 /* the following are for #156421 */
971 check_strtod_string ("1e1", 1e1
, FALSE
, 0);
972 check_strtod_string ("NAN", our_nan
, FALSE
, 0);
973 check_strtod_string ("-nan", -our_nan
, FALSE
, 0);
974 check_strtod_string ("INF", our_inf
, FALSE
, 0);
975 check_strtod_string ("-infinity", -our_inf
, FALSE
, 0);
976 check_strtod_string ("-.75,0", -0.75, TRUE
, 4);
978 d
= 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
979 g_assert (d
== g_ascii_strtod (g_ascii_dtostr (buffer
, sizeof (buffer
), d
), NULL
));
981 d
= -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
982 g_assert (d
== g_ascii_strtod (g_ascii_dtostr (buffer
, sizeof (buffer
), d
), NULL
));
984 d
= pow (2.0, -1024.1);
985 g_assert (d
== g_ascii_strtod (g_ascii_dtostr (buffer
, sizeof (buffer
), d
), NULL
));
987 d
= -pow (2.0, -1024.1);
988 g_assert (d
== g_ascii_strtod (g_ascii_dtostr (buffer
, sizeof (buffer
), d
), NULL
));
991 check_strtod_string (" 0.75", 0.75, FALSE
, 0);
992 check_strtod_string (" +0.75", 0.75, FALSE
, 0);
993 check_strtod_string (" -0.75", -0.75, FALSE
, 0);
994 check_strtod_string ("\f0.75", 0.75, FALSE
, 0);
995 check_strtod_string ("\n0.75", 0.75, FALSE
, 0);
996 check_strtod_string ("\r0.75", 0.75, FALSE
, 0);
997 check_strtod_string ("\t0.75", 0.75, FALSE
, 0);
1000 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1001 check_strtod_string ("\v0.75", 0.75, FALSE
, 0);
1005 check_strtod_number (0.75, "%0.2f", "0.75");
1006 check_strtod_number (0.75, "%5.2f", " 0.75");
1007 check_strtod_number (-0.75, "%0.2f", "-0.75");
1008 check_strtod_number (-0.75, "%5.2f", "-0.75");
1009 check_strtod_number (1e99
, "%.0e", "1e+99");
1013 check_uint64 (const gchar
*str
,
1020 gchar
*endptr
= NULL
;
1024 actual
= g_ascii_strtoull (str
, &endptr
, base
);
1027 g_assert (actual
== result
);
1028 g_assert_cmpstr (end
, ==, endptr
);
1029 g_assert (err
== error
);
1033 check_int64 (const gchar
*str
,
1040 gchar
*endptr
= NULL
;
1044 actual
= g_ascii_strtoll (str
, &endptr
, base
);
1047 g_assert (actual
== result
);
1048 g_assert_cmpstr (end
, ==, endptr
);
1049 g_assert (err
== error
);
1055 check_uint64 ("0", "", 10, 0, 0);
1056 check_uint64 ("+0", "", 10, 0, 0);
1057 check_uint64 ("-0", "", 10, 0, 0);
1058 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64
, 0);
1059 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64
, ERANGE
);
1060 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1061 check_uint64 ("-1", "", 10, G_MAXUINT64
, 0);
1063 check_int64 ("0", "", 10, 0, 0);
1064 check_int64 ("9223372036854775807", "", 10, G_MAXINT64
, 0);
1065 check_int64 ("9223372036854775808", "", 10, G_MAXINT64
, ERANGE
);
1066 check_int64 ("-9223372036854775808", "", 10, G_MININT64
, 0);
1067 check_int64 ("-9223372036854775809", "", 10, G_MININT64
, ERANGE
);
1068 check_int64 ("32768", "", 10, 32768, 0);
1069 check_int64 ("-32768", "", 10, -32768, 0);
1070 check_int64 ("001", "", 10, 1, 0);
1071 check_int64 ("-001", "", 10, -1, 0);
1077 GMappedFile
*file
, *before
, *after
;
1083 /* if we allocate the file between two others and then free those
1084 * other two, then hopefully we end up with unmapped memory on either
1087 before
= g_mapped_file_new ("4096-random-bytes", TRUE
, NULL
);
1089 /* quick workaround until #549783 can be fixed */
1093 file
= g_mapped_file_new ("4096-random-bytes", TRUE
, NULL
);
1094 after
= g_mapped_file_new ("4096-random-bytes", TRUE
, NULL
);
1095 g_mapped_file_unref (before
);
1096 g_mapped_file_unref (after
);
1098 g_assert (file
!= NULL
);
1099 g_assert_cmpint (g_mapped_file_get_length (file
), ==, 4096);
1100 string
= g_mapped_file_get_contents (file
);
1102 /* ensure they're all non-nul */
1103 g_assert (memchr (string
, '\0', 4096) == NULL
);
1105 /* test set 1: ensure that nothing goes past its maximum length, even in
1106 * light of a missing nul terminator.
1108 * we try to test all of the 'n' functions here.
1110 tmp
= g_strndup (string
, 4096);
1111 g_assert_cmpint (strlen (tmp
), ==, 4096);
1114 /* found no bugs in gnome, i hope :) */
1115 g_assert (g_strstr_len (string
, 4096, "BUGS") == NULL
);
1116 g_strstr_len (string
, 4096, "B");
1117 g_strstr_len (string
, 4096, ".");
1118 g_strstr_len (string
, 4096, "");
1120 g_strrstr_len (string
, 4096, "BUGS");
1121 g_strrstr_len (string
, 4096, "B");
1122 g_strrstr_len (string
, 4096, ".");
1123 g_strrstr_len (string
, 4096, "");
1125 g_ascii_strdown (string
, 4096);
1126 g_ascii_strdown (string
, 4096);
1127 g_ascii_strup (string
, 4096);
1128 g_ascii_strup (string
, 4096);
1130 g_ascii_strncasecmp (string
, string
, 4096);
1132 tmp
= g_markup_escape_text (string
, 4096);
1135 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1137 g_assert_cmpint (string
[4095], ==, '\n');
1138 string
[4095] = '\0';
1140 tmp
= g_strdup (string
);
1141 g_assert_cmpint (strlen (tmp
), ==, 4095);
1144 tmp
= g_strndup (string
, 10000);
1145 g_assert_cmpint (strlen (tmp
), ==, 4095);
1148 g_stpcpy (buffer
, string
);
1149 g_assert_cmpint (strlen (buffer
), ==, 4095);
1151 g_strstr_len (string
, 10000, "BUGS");
1152 g_strstr_len (string
, 10000, "B");
1153 g_strstr_len (string
, 10000, ".");
1154 g_strstr_len (string
, 10000, "");
1156 g_strrstr (string
, "BUGS");
1157 g_strrstr (string
, "B");
1158 g_strrstr (string
, ".");
1159 g_strrstr (string
, "");
1161 g_strrstr_len (string
, 10000, "BUGS");
1162 g_strrstr_len (string
, 10000, "B");
1163 g_strrstr_len (string
, 10000, ".");
1164 g_strrstr_len (string
, 10000, "");
1166 g_str_has_prefix (string
, "this won't do very much...");
1167 g_str_has_suffix (string
, "but maybe this will...");
1168 g_str_has_suffix (string
, "HMMMM.");
1169 g_str_has_suffix (string
, "MMMM.");
1170 g_str_has_suffix (string
, "M.");
1172 g_strlcpy (buffer
, string
, sizeof buffer
);
1173 g_assert_cmpint (strlen (buffer
), ==, 4095);
1174 g_strlcpy (buffer
, string
, sizeof buffer
);
1176 g_strlcat (buffer
, string
, sizeof buffer
);
1177 g_assert_cmpint (strlen (buffer
), ==, 4095);
1179 tmp
= g_strdup_printf ("<%s>", string
);
1180 g_assert_cmpint (strlen (tmp
), ==, 4095 + 2);
1183 g_ascii_strdown (string
, -1);
1184 g_ascii_strdown (string
, -1);
1185 g_ascii_strup (string
, -1);
1186 g_ascii_strup (string
, -1);
1188 g_ascii_strcasecmp (string
, string
);
1189 g_ascii_strncasecmp (string
, string
, 10000);
1191 g_strreverse (string
);
1192 g_strreverse (string
);
1194 g_strchomp (string
);
1195 g_strstrip (string
);
1196 g_assert_cmpint (strlen (string
), ==, 4095);
1198 g_strdelimit (string
, "M", 'N');
1199 g_strcanon (string
, " N.", ':');
1200 g_assert_cmpint (strlen (string
), ==, 4095);
1202 array
= g_strsplit (string
, ".", -1);
1203 tmp
= g_strjoinv (".", array
);
1206 g_assert_cmpint (strlen (tmp
), ==, 4095);
1207 g_assert (memcmp (tmp
, string
, 4095) == 0);
1210 tmp
= g_strconcat (string
, string
, string
, NULL
);
1211 g_assert_cmpint (strlen (tmp
), ==, 4095 * 3);
1214 tmp
= g_strjoin ("!", string
, string
, NULL
);
1215 g_assert_cmpint (strlen (tmp
), ==, 4095 + 1 + 4095);
1218 tmp
= g_markup_escape_text (string
, -1);
1221 tmp
= g_markup_printf_escaped ("%s", string
);
1224 tmp
= g_strescape (string
, NULL
);
1225 tmp2
= g_strcompress (tmp
);
1226 g_assert_cmpstr (string
, ==, tmp2
);
1230 g_mapped_file_unref (file
);
1234 test_strip_context (void)
1237 const gchar
*msgval
;
1243 s
= g_strip_context (msgid
, msgval
);
1244 g_assert (s
== msgval
);
1246 msgid
= msgval
= "blabla";
1247 s
= g_strip_context (msgid
, msgval
);
1248 g_assert (s
== msgval
);
1250 msgid
= msgval
= "blabla|foo";
1251 s
= g_strip_context (msgid
, msgval
);
1252 g_assert (s
== msgval
+ 7);
1254 msgid
= msgval
= "blabla||bar";
1255 s
= g_strip_context (msgid
, msgval
);
1256 g_assert (s
== msgval
+ 7);
1260 test_strerror (void)
1265 for (i
= 1; i
< 100; i
++)
1267 str
= g_strerror (i
);
1268 g_assert (str
!= NULL
);
1269 g_assert (g_utf8_validate (str
, -1, NULL
));
1274 test_strsignal (void)
1279 for (i
= 1; i
< 20; i
++)
1281 str
= g_strsignal (i
);
1282 g_assert (str
!= NULL
);
1283 g_assert (g_utf8_validate (str
, -1, NULL
));
1292 s
= g_strdup ("lower");
1293 g_assert_cmpstr (g_strup (s
), ==, "LOWER");
1294 g_assert_cmpstr (g_strdown (s
), ==, "lower");
1295 g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1303 g_test_init (&argc
, &argv
, NULL
);
1305 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit
);
1306 g_test_add_func ("/strfuncs/strdup", test_strdup
);
1307 g_test_add_func ("/strfuncs/strndup", test_strndup
);
1308 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf
);
1309 g_test_add_func ("/strfuncs/strdupv", test_strdupv
);
1310 g_test_add_func ("/strfuncs/strnfill", test_strnfill
);
1311 g_test_add_func ("/strfuncs/strconcat", test_strconcat
);
1312 g_test_add_func ("/strfuncs/strjoin", test_strjoin
);
1313 g_test_add_func ("/strfuncs/strcanon", test_strcanon
);
1314 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape
);
1315 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp
);
1316 g_test_add_func ("/strfuncs/strchug", test_strchug
);
1317 g_test_add_func ("/strfuncs/strchomp", test_strchomp
);
1318 g_test_add_func ("/strfuncs/strreverse", test_strreverse
);
1319 g_test_add_func ("/strfuncs/strstr", test_strstr
);
1320 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix
);
1321 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix
);
1322 g_test_add_func ("/strfuncs/strsplit", test_strsplit
);
1323 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set
);
1324 g_test_add_func ("/strfuncs/strv-length", test_strv_length
);
1325 g_test_add_func ("/strfuncs/strtod", test_strtod
);
1326 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll
);
1327 g_test_add_func ("/strfuncs/bounds-check", test_bounds
);
1328 g_test_add_func ("/strfuncs/strip-context", test_strip_context
);
1329 g_test_add_func ("/strfuncs/strerror", test_strerror
);
1330 g_test_add_func ("/strfuncs/strsignal", test_strsignal
);
1331 g_test_add_func ("/strfuncs/strup", test_strup
);
1333 return g_test_run();