tests: Mark gdbus-threading test as slow
[glib.git] / glib / tests / strfuncs.c
blobd974ad4360a0beb2b6f4ee0fb79d45227f431877
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
25 #include <ctype.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <math.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "glib.h"
35 #if defined (_MSC_VER) && (_MSC_VER <= 1800)
36 #define isnan(x) _isnan(x)
38 #ifndef NAN
39 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
40 #define NAN (*(const float *) __nan)
41 #endif
43 #ifndef INFINITY
44 #define INFINITY HUGE_VAL
45 #endif
47 #endif
49 #define GLIB_TEST_STRING "el dorado "
51 #define FOR_ALL_CTYPE(macro) \
52 macro(isalnum) \
53 macro(isalpha) \
54 macro(iscntrl) \
55 macro(isdigit) \
56 macro(isgraph) \
57 macro(islower) \
58 macro(isprint) \
59 macro(ispunct) \
60 macro(isspace) \
61 macro(isupper) \
62 macro(isxdigit)
64 #define DEFINE_CALL_CTYPE(function) \
65 static int \
66 call_##function (int c) \
67 { \
68 return function (c); \
71 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
72 static gboolean \
73 call_g_ascii_##function (gchar c) \
74 { \
75 return g_ascii_##function (c); \
78 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
79 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
81 static void
82 test_is_function (const char *name,
83 gboolean (* ascii_function) (gchar),
84 int (* c_library_function) (int),
85 gboolean (* unicode_function) (gunichar))
87 int c;
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);
108 if (ascii_result)
110 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
115 static void
116 test_to_function (const char *name,
117 gchar (* ascii_function) (gchar),
118 int (* c_library_function) (int),
119 gunichar (* unicode_function) (gunichar))
121 int c;
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);
150 static void
151 test_digit_function (const char *name,
152 int (* ascii_function) (gchar),
153 int (* unicode_function) (gunichar))
155 int c;
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);
178 static void
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)
185 #undef TEST_IS
187 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
189 TEST_TO (tolower);
190 TEST_TO (toupper);
192 #undef TEST_TO
194 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
196 TEST_DIGIT (digit);
197 TEST_DIGIT (xdigit);
199 #undef TEST_DIGIT
202 static void
203 test_strdup (void)
205 gchar *str;
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);
213 g_free (str);
216 static void
217 test_strndup (void)
219 gchar *str;
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");
227 g_free (str);
229 str = g_strndup ("aaaa", 2);
230 g_assert (str != NULL);
231 g_assert_cmpstr (str, ==, "aa");
232 g_free (str);
235 static void
236 test_strdup_printf (void)
238 gchar *str;
240 str = g_strdup_printf ("%05d %-5s", 21, "test");
241 g_assert (str != NULL);
242 g_assert_cmpstr (str, ==, "00021 test ");
243 g_free (str);
246 static void
247 test_strdupv (void)
249 gchar *vec[] = { "Foo", "Bar", NULL };
250 gchar **copy;
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);
260 g_strfreev (copy);
263 static void
264 test_strnfill (void)
266 gchar *str;
268 str = g_strnfill (0, 'a');
269 g_assert (str != NULL);
270 g_assert (*str == '\0');
271 g_free (str);
273 str = g_strnfill (5, 'a');
274 g_assert (str != NULL);
275 g_assert_cmpstr (str, ==, "aaaaa");
276 g_free (str);
279 static void
280 test_strconcat (void)
282 gchar *str;
284 str = g_strconcat (GLIB_TEST_STRING, NULL);
285 g_assert (str != NULL);
286 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
287 g_free (str);
289 str = g_strconcat (GLIB_TEST_STRING,
290 GLIB_TEST_STRING,
291 GLIB_TEST_STRING,
292 NULL);
293 g_assert (str != NULL);
294 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
295 g_free (str);
297 g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
300 static void
301 test_strjoin (void)
303 gchar *str;
305 str = g_strjoin (NULL, NULL);
306 g_assert (str != NULL);
307 g_assert (*str == '\0');
308 g_free (str);
310 str = g_strjoin (":", NULL);
311 g_assert (str != NULL);
312 g_assert (*str == '\0');
313 g_free (str);
315 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
316 g_assert (str != NULL);
317 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
318 g_free (str);
320 str = g_strjoin (NULL,
321 GLIB_TEST_STRING,
322 GLIB_TEST_STRING,
323 GLIB_TEST_STRING,
324 NULL);
325 g_assert (str != NULL);
326 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
327 g_free (str);
329 str = g_strjoin (":",
330 GLIB_TEST_STRING,
331 GLIB_TEST_STRING,
332 GLIB_TEST_STRING,
333 NULL);
334 g_assert (str != NULL);
335 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
336 g_free (str);
339 static void
340 test_strcanon (void)
342 gchar *str;
344 if (g_test_undefined ())
346 gchar *ret;
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);
360 g_free (str);
363 str = g_strdup ("abxabxab");
364 str = g_strcanon (str, "ab", 'y');
365 g_assert (str != NULL);
366 g_assert_cmpstr (str, ==, "abyabyab");
367 g_free (str);
370 static void
371 test_strcompress_strescape (void)
373 gchar *str;
374 gchar *tmp;
376 /* test compress */
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,
387 "*trailing \\*");
388 str = g_strcompress ("abc\\");
389 g_test_assert_expected_messages ();
390 g_assert_cmpstr (str, ==, "abc");
391 g_free (str);
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");
397 g_free (str);
399 /* test escape */
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");
412 g_free (str);
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");
418 g_free (str);
420 /* round trip */
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");
425 g_free (str);
426 g_free (tmp);
429 static void
430 test_ascii_strcasecmp (void)
432 gboolean res;
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);
492 static void
493 do_test_strchug (const gchar *str, const gchar *expected)
495 gchar *tmp;
496 gboolean res;
498 tmp = g_strdup (str);
500 g_strchug (tmp);
501 res = (strcmp (tmp, expected) == 0);
502 g_free (tmp);
504 g_assert_cmpint (res, ==, TRUE);
507 static void
508 test_strchug (void)
510 if (g_test_undefined ())
512 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
513 "*assertion*!= NULL*");
514 g_strchug (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");
527 static void
528 do_test_strchomp (const gchar *str, const gchar *expected)
530 gchar *tmp;
531 gboolean res;
533 tmp = g_strdup (str);
535 g_strchomp (tmp);
536 res = (strcmp (tmp, expected) == 0);
537 g_free (tmp);
539 g_assert_cmpint (res, ==, TRUE);
542 static void
543 test_strchomp (void)
545 if (g_test_undefined ())
547 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
548 "*assertion*!= NULL*");
549 g_strchomp (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");
562 static void
563 test_strreverse (void)
565 gchar *str;
566 gchar *p;
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);
580 g_assert (p == str);
581 g_assert_cmpstr (str, ==, "edcba");
582 g_free (str);
585 static void
586 test_strncasecmp (void)
588 g_assert (g_strncasecmp ("abc1", "ABC2", 3) == 0);
589 g_assert (g_strncasecmp ("abc1", "ABC2", 4) != 0);
592 static void
593 test_strstr (void)
595 gchar *haystack;
596 gchar *res;
598 haystack = g_strdup ("FooBarFooBarFoo");
600 /* strstr_len */
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");
622 /* strrstr */
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");
637 /* strrstr_len */
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);
664 g_free (haystack);
667 static void
668 test_has_prefix (void)
670 gboolean res;
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);
709 static void
710 test_has_suffix (void)
712 gboolean res;
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);
751 static void
752 strv_check (gchar **strv, ...)
754 gboolean ok = TRUE;
755 gint i = 0;
756 va_list list;
758 va_start (list, strv);
759 while (ok)
761 const gchar *str = va_arg (list, const char *);
762 if (strv[i] == NULL)
764 g_assert (str == NULL);
765 break;
767 if (str == NULL)
769 ok = FALSE;
771 else
773 g_assert_cmpstr (strv[i], ==, str);
775 i++;
777 va_end (list);
779 g_strfreev (strv);
782 static void
783 test_strsplit (void)
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);
825 static void
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);
890 static void
891 test_strv_length (void)
893 gchar **strv;
894 guint l;
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);
908 g_strfreev (strv);
911 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
913 static void
914 check_strtod_string (gchar *number,
915 double res,
916 gboolean check_end,
917 gint correct_len)
919 double d;
920 gint l;
921 gchar *dummy;
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);
928 g_free (dummy);
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)));
940 g_free (number);
943 static void
944 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
946 int l;
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);
957 static void
958 test_strtod (void)
960 gdouble d, our_nan, our_inf;
961 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
963 #ifdef NAN
964 our_nan = NAN;
965 #else
966 /* Do this before any call to setlocale. */
967 our_nan = atof ("NaN");
968 #endif
969 g_assert (isnan (our_nan));
971 #ifdef INFINITY
972 our_inf = INFINITY;
973 #else
974 our_inf = atof ("Infinity");
975 #endif
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);
987 #ifndef _MSC_VER
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);
994 #endif
995 /* the following are for #156421 */
996 check_strtod_string ("1e1", 1e1, FALSE, 0);
997 #ifndef _MSC_VER
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);
1003 #endif
1004 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
1006 #ifndef _MSC_VER
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));
1013 #endif
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));
1021 /* for #343899 */
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);
1030 #if 0
1031 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1032 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1033 #endif
1035 /* for #343899 */
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");
1040 #if defined(_MSC_VER) || defined(__MINGW32__)
1041 /* FIXME: The included gnulib and the mingw-w64 implementation
1042 * currently don't follow C99 and print 3 digits for the exponent.
1043 * In case of mingw-w64 this was fixed but not released yet:
1044 * https://sourceforge.net/p/mingw-w64/bugs/732/ */
1045 check_strtod_number (1e99, "%0.e", "1e+099");
1046 #else
1047 check_strtod_number (1e99, "%.0e", "1e+99");
1048 #endif
1051 static void
1052 check_uint64 (const gchar *str,
1053 const gchar *end,
1054 gint base,
1055 guint64 result,
1056 gint error)
1058 guint64 actual;
1059 gchar *endptr = NULL;
1060 gint err;
1062 errno = 0;
1063 actual = g_ascii_strtoull (str, &endptr, base);
1064 err = errno;
1066 g_assert (actual == result);
1067 g_assert_cmpstr (end, ==, endptr);
1068 g_assert (err == error);
1071 static void
1072 check_int64 (const gchar *str,
1073 const gchar *end,
1074 gint base,
1075 gint64 result,
1076 gint error)
1078 gint64 actual;
1079 gchar *endptr = NULL;
1080 gint err;
1082 errno = 0;
1083 actual = g_ascii_strtoll (str, &endptr, base);
1084 err = errno;
1086 g_assert (actual == result);
1087 g_assert_cmpstr (end, ==, endptr);
1088 g_assert (err == error);
1091 static void
1092 test_strtoll (void)
1094 check_uint64 ("0", "", 10, 0, 0);
1095 check_uint64 ("+0", "", 10, 0, 0);
1096 check_uint64 ("-0", "", 10, 0, 0);
1097 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1098 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1099 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1100 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1102 check_int64 ("0", "", 10, 0, 0);
1103 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1104 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1105 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1106 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1107 check_int64 ("32768", "", 10, 32768, 0);
1108 check_int64 ("-32768", "", 10, -32768, 0);
1109 check_int64 ("001", "", 10, 1, 0);
1110 check_int64 ("-001", "", 10, -1, 0);
1113 static void
1114 test_bounds (void)
1116 GMappedFile *file, *before, *after;
1117 char buffer[4097];
1118 char *tmp, *tmp2;
1119 char **array;
1120 char *string;
1121 const char * const strjoinv_0[] = { NULL };
1122 const char * const strjoinv_1[] = { "foo", NULL };
1124 /* if we allocate the file between two others and then free those
1125 * other two, then hopefully we end up with unmapped memory on either
1126 * side.
1128 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1130 /* quick workaround until #549783 can be fixed */
1131 if (before == NULL)
1132 return;
1134 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1135 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1136 g_mapped_file_unref (before);
1137 g_mapped_file_unref (after);
1139 g_assert (file != NULL);
1140 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1141 string = g_mapped_file_get_contents (file);
1143 /* ensure they're all non-nul */
1144 g_assert (memchr (string, '\0', 4096) == NULL);
1146 /* test set 1: ensure that nothing goes past its maximum length, even in
1147 * light of a missing nul terminator.
1149 * we try to test all of the 'n' functions here.
1151 tmp = g_strndup (string, 4096);
1152 g_assert_cmpint (strlen (tmp), ==, 4096);
1153 g_free (tmp);
1155 /* found no bugs in gnome, i hope :) */
1156 g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1157 g_strstr_len (string, 4096, "B");
1158 g_strstr_len (string, 4096, ".");
1159 g_strstr_len (string, 4096, "");
1161 g_strrstr_len (string, 4096, "BUGS");
1162 g_strrstr_len (string, 4096, "B");
1163 g_strrstr_len (string, 4096, ".");
1164 g_strrstr_len (string, 4096, "");
1166 tmp = g_ascii_strup (string, 4096);
1167 tmp2 = g_ascii_strup (tmp, 4096);
1168 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1169 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1170 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1171 g_free (tmp);
1172 g_free (tmp2);
1174 tmp = g_ascii_strdown (string, 4096);
1175 tmp2 = g_ascii_strdown (tmp, 4096);
1176 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1177 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1178 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1179 g_free (tmp);
1180 g_free (tmp2);
1182 tmp = g_markup_escape_text (string, 4096);
1183 g_free (tmp);
1185 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1187 g_assert_cmpint (string[4095], ==, '\n');
1188 string[4095] = '\0';
1190 tmp = g_strdup (string);
1191 g_assert_cmpint (strlen (tmp), ==, 4095);
1192 g_free (tmp);
1194 tmp = g_strndup (string, 10000);
1195 g_assert_cmpint (strlen (tmp), ==, 4095);
1196 g_free (tmp);
1198 g_stpcpy (buffer, string);
1199 g_assert_cmpint (strlen (buffer), ==, 4095);
1201 g_strstr_len (string, 10000, "BUGS");
1202 g_strstr_len (string, 10000, "B");
1203 g_strstr_len (string, 10000, ".");
1204 g_strstr_len (string, 10000, "");
1206 g_strrstr (string, "BUGS");
1207 g_strrstr (string, "B");
1208 g_strrstr (string, ".");
1209 g_strrstr (string, "");
1211 g_strrstr_len (string, 10000, "BUGS");
1212 g_strrstr_len (string, 10000, "B");
1213 g_strrstr_len (string, 10000, ".");
1214 g_strrstr_len (string, 10000, "");
1216 g_str_has_prefix (string, "this won't do very much...");
1217 g_str_has_suffix (string, "but maybe this will...");
1218 g_str_has_suffix (string, "HMMMM.");
1219 g_str_has_suffix (string, "MMMM.");
1220 g_str_has_suffix (string, "M.");
1222 g_strlcpy (buffer, string, sizeof buffer);
1223 g_assert_cmpint (strlen (buffer), ==, 4095);
1224 g_strlcpy (buffer, string, sizeof buffer);
1225 buffer[0] = '\0';
1226 g_strlcat (buffer, string, sizeof buffer);
1227 g_assert_cmpint (strlen (buffer), ==, 4095);
1229 tmp = g_strdup_printf ("<%s>", string);
1230 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1231 g_free (tmp);
1233 tmp = g_ascii_strdown (string, -1);
1234 tmp2 = g_ascii_strdown (tmp, -1);
1235 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1236 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1237 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1238 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1239 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1240 g_free (tmp);
1241 g_free (tmp2);
1243 tmp = g_ascii_strup (string, -1);
1244 tmp2 = g_ascii_strup (string, -1);
1245 g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1246 g_assert_cmpint (strlen(string), ==, strlen(tmp));
1247 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1248 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1249 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1250 g_free (tmp);
1251 g_free (tmp2);
1253 g_ascii_strcasecmp (string, string);
1254 g_ascii_strncasecmp (string, string, 10000);
1256 g_strreverse (string);
1257 g_strreverse (string);
1258 g_strchug (string);
1259 g_strchomp (string);
1260 g_strstrip (string);
1261 g_assert_cmpint (strlen (string), ==, 4095);
1263 g_strdelimit (string, "M", 'N');
1264 g_strcanon (string, " N.", ':');
1265 g_assert_cmpint (strlen (string), ==, 4095);
1267 array = g_strsplit (string, ".", -1);
1268 tmp = g_strjoinv (".", array);
1269 g_strfreev (array);
1271 g_assert_cmpmem (tmp, strlen (tmp), string, 4095);
1272 g_free (tmp);
1274 tmp = g_strjoinv ("/", (char **) strjoinv_0);
1275 g_assert_cmpstr (tmp, ==, "");
1276 g_free (tmp);
1278 tmp = g_strjoinv ("/", (char **) strjoinv_1);
1279 g_assert_cmpstr (tmp, ==, "foo");
1280 g_free (tmp);
1282 tmp = g_strconcat (string, string, string, NULL);
1283 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1284 g_free (tmp);
1286 tmp = g_strjoin ("!", string, string, NULL);
1287 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1288 g_free (tmp);
1290 tmp = g_markup_escape_text (string, -1);
1291 g_free (tmp);
1293 tmp = g_markup_printf_escaped ("%s", string);
1294 g_free (tmp);
1296 tmp = g_strescape (string, NULL);
1297 tmp2 = g_strcompress (tmp);
1298 g_assert_cmpstr (string, ==, tmp2);
1299 g_free (tmp2);
1300 g_free (tmp);
1302 g_mapped_file_unref (file);
1305 static void
1306 test_strip_context (void)
1308 const gchar *msgid;
1309 const gchar *msgval;
1310 const gchar *s;
1313 msgid = "blabla";
1314 msgval = "bla";
1315 s = g_strip_context (msgid, msgval);
1316 g_assert (s == msgval);
1318 msgid = msgval = "blabla";
1319 s = g_strip_context (msgid, msgval);
1320 g_assert (s == msgval);
1322 msgid = msgval = "blabla|foo";
1323 s = g_strip_context (msgid, msgval);
1324 g_assert (s == msgval + 7);
1326 msgid = msgval = "blabla||bar";
1327 s = g_strip_context (msgid, msgval);
1328 g_assert (s == msgval + 7);
1331 /* Test the strings returned by g_strerror() are valid and unique. On Windows,
1332 * fewer than 200 error numbers are used, so we expect some strings to
1333 * return a generic ‘unknown error code’ message. */
1334 static void
1335 test_strerror (void)
1337 GHashTable *strs;
1338 gint i;
1339 const gchar *str, *unknown_str;
1341 setlocale (LC_ALL, "C");
1343 unknown_str = g_strerror (-1);
1344 strs = g_hash_table_new (g_str_hash, g_str_equal);
1345 for (i = 1; i < 200; i++)
1347 gboolean is_unknown;
1348 str = g_strerror (i);
1349 is_unknown = (strcmp (str, unknown_str) == 0);
1350 g_assert (str != NULL);
1351 g_assert (g_utf8_validate (str, -1, NULL));
1352 g_assert_true (!g_hash_table_contains (strs, str) || is_unknown);
1353 g_hash_table_add (strs, (char *)str);
1356 g_hash_table_unref (strs);
1359 static void
1360 test_strsignal (void)
1362 gint i;
1363 const gchar *str;
1365 for (i = 1; i < 20; i++)
1367 str = g_strsignal (i);
1368 g_assert (str != NULL);
1369 g_assert (g_utf8_validate (str, -1, NULL));
1373 static void
1374 test_strup (void)
1376 gchar *s;
1378 s = g_strdup ("lower");
1379 g_assert_cmpstr (g_strup (s), ==, "LOWER");
1380 g_assert_cmpstr (g_strdown (s), ==, "lower");
1381 g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1382 g_free (s);
1385 static void
1386 test_transliteration (void)
1388 gchar *out;
1390 /* ...to test the defaults */
1391 setlocale (LC_ALL, "C");
1393 /* Test something trivial */
1394 out = g_str_to_ascii ("hello", NULL);
1395 g_assert_cmpstr (out, ==, "hello");
1396 g_free (out);
1398 /* Test something above 0xffff */
1399 out = g_str_to_ascii ("𝐀𝐀𝐀", NULL);
1400 g_assert_cmpstr (out, ==, "AAA");
1401 g_free (out);
1403 /* Test something with no good match */
1404 out = g_str_to_ascii ("a ∧ ¬a", NULL);
1405 g_assert_cmpstr (out, ==, "a ? ?a");
1406 g_free (out);
1408 /* Make sure 'ö' is handled differently per locale */
1409 out = g_str_to_ascii ("ö", NULL);
1410 g_assert_cmpstr (out, ==, "o");
1411 g_free (out);
1413 out = g_str_to_ascii ("ö", "sv");
1414 g_assert_cmpstr (out, ==, "o");
1415 g_free (out);
1417 out = g_str_to_ascii ("ö", "de");
1418 g_assert_cmpstr (out, ==, "oe");
1419 g_free (out);
1421 /* Make sure we can find a locale by a wide range of names */
1422 out = g_str_to_ascii ("ö", "de_DE");
1423 g_assert_cmpstr (out, ==, "oe");
1424 g_free (out);
1426 out = g_str_to_ascii ("ö", "de_DE.UTF-8");
1427 g_assert_cmpstr (out, ==, "oe");
1428 g_free (out);
1430 out = g_str_to_ascii ("ö", "de_DE.UTF-8@euro");
1431 g_assert_cmpstr (out, ==, "oe");
1432 g_free (out);
1434 out = g_str_to_ascii ("ö", "de@euro");
1435 g_assert_cmpstr (out, ==, "oe");
1436 g_free (out);
1438 /* Test some invalid locale names */
1439 out = g_str_to_ascii ("ö", "de_DE@euro.UTF-8");
1440 g_assert_cmpstr (out, ==, "o");
1441 g_free (out);
1443 out = g_str_to_ascii ("ö", "de@DE@euro");
1444 g_assert_cmpstr (out, ==, "o");
1445 g_free (out);
1447 out = g_str_to_ascii ("ö", "doesnotexist");
1448 g_assert_cmpstr (out, ==, "o");
1449 g_free (out);
1451 out = g_str_to_ascii ("ö", "thislocalenameistoolong");
1452 g_assert_cmpstr (out, ==, "o");
1453 g_free (out);
1455 /* Try a lookup of a locale with a variant */
1456 out = g_str_to_ascii ("б", "sr_RS");
1457 g_assert_cmpstr (out, ==, "b");
1458 g_free (out);
1460 out = g_str_to_ascii ("б", "sr_RS@latin");
1461 g_assert_cmpstr (out, ==, "?");
1462 g_free (out);
1464 /* Ukrainian contains the only multi-character mappings.
1465 * Try a string that contains one ('зг') along with a partial
1466 * sequence ('з') at the end.
1468 out = g_str_to_ascii ("Зліва направо, згори вниз", "uk");
1469 g_assert_cmpstr (out, ==, "Zliva napravo, zghory vnyz");
1470 g_free (out);
1472 /* Try out the other combinations */
1473 out = g_str_to_ascii ("Зг", "uk");
1474 g_assert_cmpstr (out, ==, "Zgh");
1475 g_free (out);
1477 out = g_str_to_ascii ("зГ", "uk");
1478 g_assert_cmpstr (out, ==, "zGH");
1479 g_free (out);
1481 out = g_str_to_ascii ("ЗГ", "uk");
1482 g_assert_cmpstr (out, ==, "ZGH");
1483 g_free (out);
1485 /* And a non-combination */
1486 out = g_str_to_ascii ("зя", "uk");
1487 g_assert_cmpstr (out, ==, "zya");
1488 g_free (out);
1491 static void
1492 test_strv_contains (void)
1494 static const gchar *strv_simple[] = { "hello", "there", NULL };
1495 static const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
1496 static const gchar *strv_empty[] = { NULL };
1498 g_assert_true (g_strv_contains (strv_simple, "hello"));
1499 g_assert_true (g_strv_contains (strv_simple, "there"));
1500 g_assert_false (g_strv_contains (strv_simple, "non-existent"));
1501 g_assert_false (g_strv_contains (strv_simple, ""));
1503 g_assert_true (g_strv_contains (strv_dupe, "dupe"));
1505 g_assert_false (g_strv_contains (strv_empty, "empty!"));
1506 g_assert_false (g_strv_contains (strv_empty, ""));
1509 typedef enum
1511 SIGNED,
1512 UNSIGNED
1513 } SignType;
1515 typedef struct
1517 const gchar *str;
1518 SignType sign_type;
1519 guint base;
1520 gint min;
1521 gint max;
1522 gint expected;
1523 gboolean should_fail;
1524 GNumberParserError error_code;
1525 } TestData;
1527 const TestData test_data[] = {
1528 /* typical cases for signed */
1529 { "0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
1530 { "+0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
1531 { "-0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
1532 { "-2", SIGNED, 10, -2, 2, -2, FALSE, 0 },
1533 { "2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
1534 { "+2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
1535 { "3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1536 { "+3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1537 { "-3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1539 /* typical cases for unsigned */
1540 { "-1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1541 { "1", UNSIGNED, 10, 0, 2, 1, FALSE, 0 },
1542 { "+1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1543 { "0", UNSIGNED, 10, 0, 2, 0, FALSE, 0 },
1544 { "+0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1545 { "-0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1546 { "2", UNSIGNED, 10, 0, 2, 2, FALSE, 0 },
1547 { "+2", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1548 { "3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1549 { "+3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1551 /* min == max cases for signed */
1552 { "-2", SIGNED, 10, -2, -2, -2, FALSE, 0 },
1553 { "-1", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1554 { "-3", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1556 /* min == max cases for unsigned */
1557 { "2", UNSIGNED, 10, 2, 2, 2, FALSE, 0 },
1558 { "3", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1559 { "1", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
1561 /* invalid inputs */
1562 { "", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1563 { "", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1564 { "a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1565 { "a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1566 { "1a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1567 { "1a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1568 { "- 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1570 /* leading/trailing whitespace */
1571 { " 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1572 { " 1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1573 { "1 ", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1574 { "1 ", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1576 /* hexadecimal numbers */
1577 { "a", SIGNED, 16, 0, 15, 10, FALSE, 0 },
1578 { "a", UNSIGNED, 16, 0, 15, 10, FALSE, 0 },
1579 { "0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1580 { "0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1581 { "-0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1582 { "-0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1583 { "+0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1584 { "+0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1585 { "- 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1586 { "- 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1587 { "+ 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1588 { "+ 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
1591 static void
1592 test_ascii_string_to_number_usual (void)
1594 gsize idx;
1596 for (idx = 0; idx < G_N_ELEMENTS (test_data); ++idx)
1598 GError *error = NULL;
1599 const TestData *data = &test_data[idx];
1600 gboolean result;
1601 gint value;
1603 switch (data->sign_type)
1605 case SIGNED:
1607 gint64 value64 = 0;
1608 result = g_ascii_string_to_signed (data->str,
1609 data->base,
1610 data->min,
1611 data->max,
1612 &value64,
1613 &error);
1614 value = value64;
1615 g_assert_cmpint (value, ==, value64);
1616 break;
1619 case UNSIGNED:
1621 guint64 value64 = 0;
1622 result = g_ascii_string_to_unsigned (data->str,
1623 data->base,
1624 data->min,
1625 data->max,
1626 &value64,
1627 &error);
1628 value = value64;
1629 g_assert_cmpint (value, ==, value64);
1630 break;
1633 default:
1634 g_assert_not_reached ();
1637 if (data->should_fail)
1639 g_assert_false (result);
1640 g_assert_error (error, G_NUMBER_PARSER_ERROR, data->error_code);
1641 g_clear_error (&error);
1643 else
1645 g_assert_true (result);
1646 g_assert_no_error (error);
1647 g_assert_cmpint (value, ==, data->expected);
1652 static void
1653 test_ascii_string_to_number_pathological (void)
1655 GError *error = NULL;
1656 const gchar *crazy_high = "999999999999999999999999999999999999";
1657 const gchar *crazy_low = "-999999999999999999999999999999999999";
1658 const gchar *max_uint64 = "18446744073709551615";
1659 const gchar *max_int64 = "9223372036854775807";
1660 const gchar *min_int64 = "-9223372036854775808";
1661 guint64 uvalue = 0;
1662 gint64 svalue = 0;
1664 g_assert_false (g_ascii_string_to_unsigned (crazy_high,
1667 G_MAXUINT64,
1668 NULL,
1669 &error));
1670 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
1671 g_clear_error (&error);
1672 g_assert_false (g_ascii_string_to_unsigned (crazy_low,
1675 G_MAXUINT64,
1676 NULL,
1677 &error));
1678 // crazy_low is a signed number so it is not a valid unsigned number
1679 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID);
1680 g_clear_error (&error);
1682 g_assert_false (g_ascii_string_to_signed (crazy_high,
1684 G_MININT64,
1685 G_MAXINT64,
1686 NULL,
1687 &error));
1688 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
1689 g_clear_error (&error);
1690 g_assert_false (g_ascii_string_to_signed (crazy_low,
1692 G_MININT64,
1693 G_MAXINT64,
1694 NULL,
1695 &error));
1696 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
1697 g_clear_error (&error);
1699 g_assert_true (g_ascii_string_to_unsigned (max_uint64,
1702 G_MAXUINT64,
1703 &uvalue,
1704 &error));
1705 g_assert_no_error (error);
1706 g_assert_cmpint (uvalue, ==, G_MAXUINT64);
1708 g_assert_true (g_ascii_string_to_signed (max_int64,
1710 G_MININT64,
1711 G_MAXINT64,
1712 &svalue,
1713 &error));
1714 g_assert_no_error (error);
1715 g_assert_cmpint (svalue, ==, G_MAXINT64);
1717 g_assert_true (g_ascii_string_to_signed (min_int64,
1719 G_MININT64,
1720 G_MAXINT64,
1721 &svalue,
1722 &error));
1723 g_assert_no_error (error);
1724 g_assert_cmpint (svalue, ==, G_MININT64);
1728 main (int argc,
1729 char *argv[])
1731 g_test_init (&argc, &argv, NULL);
1733 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1734 g_test_add_func ("/strfuncs/strdup", test_strdup);
1735 g_test_add_func ("/strfuncs/strndup", test_strndup);
1736 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1737 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1738 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1739 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1740 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1741 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1742 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1743 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1744 g_test_add_func ("/strfuncs/strchug", test_strchug);
1745 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1746 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1747 g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
1748 g_test_add_func ("/strfuncs/strstr", test_strstr);
1749 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1750 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1751 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1752 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1753 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1754 g_test_add_func ("/strfuncs/strtod", test_strtod);
1755 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1756 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1757 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1758 g_test_add_func ("/strfuncs/strerror", test_strerror);
1759 g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1760 g_test_add_func ("/strfuncs/strup", test_strup);
1761 g_test_add_func ("/strfuncs/transliteration", test_transliteration);
1762 g_test_add_func ("/strfuncs/strv-contains", test_strv_contains);
1763 g_test_add_func ("/strfuncs/ascii-string-to-num/usual", test_ascii_string_to_number_usual);
1764 g_test_add_func ("/strfuncs/ascii-string-to-num/pathological", test_ascii_string_to_number_pathological);
1766 return g_test_run();