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 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 #define _XOPEN_SOURCE 600
37 #if defined (_MSC_VER) && (_MSC_VER <= 1800)
38 #define isnan(x) _isnan(x)
41 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
42 #define NAN (*(const float *) __nan)
46 #define INFINITY HUGE_VAL
51 #define GLIB_TEST_STRING "el dorado "
53 #define FOR_ALL_CTYPE(macro) \
66 #define DEFINE_CALL_CTYPE(function) \
68 call_##function (int c) \
70 return function (c); \
73 #define DEFINE_CALL_G_ASCII_CTYPE(function) \
75 call_g_ascii_##function (gchar c) \
77 return g_ascii_##function (c); \
80 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
81 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
84 test_is_function (const char *name,
85 gboolean (* ascii_function) (gchar),
86 int (* c_library_function) (int),
87 gboolean (* unicode_function) (gunichar))
91 for (c = 0; c <= 0x7F; c++)
93 gboolean ascii_result = ascii_function ((gchar)c);
94 gboolean c_library_result = c_library_function (c) != 0;
95 gboolean unicode_result = unicode_function ((gunichar) c);
96 if (ascii_result != c_library_result && c != '\v')
98 g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
99 name, ascii_result, name, c_library_result, c);
101 if (ascii_result != unicode_result)
103 g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
104 name, ascii_result, name, unicode_result, c);
107 for (c = 0x80; c <= 0xFF; c++)
109 gboolean ascii_result = ascii_function ((gchar)c);
112 g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
118 test_to_function (const char *name,
119 gchar (* ascii_function) (gchar),
120 int (* c_library_function) (int),
121 gunichar (* unicode_function) (gunichar))
125 for (c = 0; c <= 0x7F; c++)
127 int ascii_result = (guchar) ascii_function ((gchar) c);
128 int c_library_result = c_library_function (c);
129 int unicode_result = unicode_function ((gunichar) c);
130 if (ascii_result != c_library_result)
132 g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
133 name, ascii_result, name, c_library_result, c);
135 if (ascii_result != unicode_result)
137 g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
138 name, ascii_result, name, unicode_result, c);
141 for (c = 0x80; c <= 0xFF; c++)
143 int ascii_result = (guchar) ascii_function ((gchar) c);
144 if (ascii_result != c)
146 g_error ("g_ascii_%s returned 0x%X for 0x%X",
147 name, ascii_result, c);
153 test_digit_function (const char *name,
154 int (* ascii_function) (gchar),
155 int (* unicode_function) (gunichar))
159 for (c = 0; c <= 0x7F; c++)
161 int ascii_result = ascii_function ((gchar) c);
162 int unicode_result = unicode_function ((gunichar) c);
163 if (ascii_result != unicode_result)
165 g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
166 name, ascii_result, name, unicode_result, c);
169 for (c = 0x80; c <= 0xFF; c++)
171 int ascii_result = ascii_function ((gchar) c);
172 if (ascii_result != -1)
174 g_error ("g_ascii_%s_value returned %d for 0x%X",
175 name, ascii_result, c);
181 test_is_to_digit (void)
183 #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
185 FOR_ALL_CTYPE(TEST_IS)
189 #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
196 #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
204 /* Testing g_memdup() function with various positive and negative cases */
208 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
210 gchar *str_dup = NULL;
211 const gchar *str = "The quick brown fox jumps over the lazy dog";
213 /* Testing negative cases */
214 g_assert_null (g_memdup (NULL, 1024));
215 g_assert_null (g_memdup (str, 0));
216 g_assert_null (g_memdup (NULL, 0));
218 /* Testing normal usage cases */
219 str_dup = g_memdup (str, strlen (str) + 1);
220 g_assert_nonnull (str_dup);
221 g_assert_cmpstr (str, ==, str_dup);
225 G_GNUC_END_IGNORE_DEPRECATIONS
228 /* Testing g_memdup2() function with various positive and negative cases */
232 gchar *str_dup = NULL;
233 const gchar *str = "The quick brown fox jumps over the lazy dog";
235 /* Testing negative cases */
236 g_assert_null (g_memdup2 (NULL, 1024));
237 g_assert_null (g_memdup2 (str, 0));
238 g_assert_null (g_memdup2 (NULL, 0));
240 /* Testing normal usage cases */
241 str_dup = g_memdup2 (str, strlen (str) + 1);
242 g_assert_nonnull (str_dup);
243 g_assert_cmpstr (str, ==, str_dup);
248 /* Testing g_strpcpy() function with various positive and negative cases */
252 gchar *str = "The quick brown fox jumps over the lazy dog";
253 gchar str_cpy[45], *str_cpy_end = NULL;
255 if (g_test_undefined ())
257 /* Testing degenerated cases */
258 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
259 "*assertion*!= NULL*");
260 str_cpy_end = g_stpcpy (str_cpy, NULL);
261 g_test_assert_expected_messages ();
263 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
264 "*assertion*!= NULL*");
265 str_cpy_end = g_stpcpy (NULL, str);
266 g_test_assert_expected_messages ();
269 /* Testing normal usage cases */
270 str_cpy_end = g_stpcpy (str_cpy, str);
271 g_assert_nonnull (str_cpy);
272 g_assert_true (str_cpy + strlen (str) == str_cpy_end);
273 g_assert_cmpstr (str, ==, str_cpy);
274 g_assert_cmpstr (str, ==, str_cpy_end - strlen (str));
277 /* Testing g_strlcpy() function with various positive and negative cases */
281 gchar *str = "The quick brown fox jumps over the lazy dog";
283 gsize str_cpy_size = 0;
285 if (g_test_undefined ())
287 /* Testing degenerated cases */
288 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
289 "*assertion*!= NULL*");
290 str_cpy_size = g_strlcpy (str_cpy, NULL, 0);
291 g_test_assert_expected_messages ();
292 /* Returned 0 because g_strlcpy() failed */
293 g_assert_cmpint (str_cpy_size, ==, 0);
295 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
296 "*assertion*!= NULL*");
297 str_cpy_size = g_strlcpy (NULL, str, 0);
298 g_test_assert_expected_messages ();
299 /* Returned 0 because g_strlcpy() failed */
300 g_assert_cmpint (str_cpy_size, ==, 0);
303 str_cpy_size = g_strlcpy (str_cpy, "", 0);
304 g_assert_cmpint (str_cpy_size, ==, strlen (""));
306 /* Testing normal usage cases.
307 * Note that the @dest_size argument to g_strlcpy() is normally meant to be
308 * set to `sizeof (dest)`. We set it to various values `≤ sizeof (str_cpy)`
309 * for testing purposes. */
310 str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) + 1);
311 g_assert_nonnull (str_cpy);
312 g_assert_cmpstr (str, ==, str_cpy);
313 g_assert_cmpint (str_cpy_size, ==, strlen (str));
315 str_cpy_size = g_strlcpy (str_cpy, str, strlen (str));
316 g_assert_nonnull (str_cpy);
317 g_assert_cmpstr ("The quick brown fox jumps over the lazy do", ==, str_cpy);
318 g_assert_cmpint (str_cpy_size, ==, strlen (str));
320 str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) - 15);
321 g_assert_nonnull (str_cpy);
322 g_assert_cmpstr ("The quick brown fox jumps o", ==, str_cpy);
323 g_assert_cmpint (str_cpy_size, ==, strlen (str));
325 str_cpy_size = g_strlcpy (str_cpy, str, 0);
326 g_assert_nonnull (str_cpy);
327 g_assert_cmpstr ("The quick brown fox jumps o", ==, str_cpy);
328 g_assert_cmpint (str_cpy_size, ==, strlen (str));
330 str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) + 15);
331 g_assert_nonnull (str_cpy);
332 g_assert_cmpstr (str, ==, str_cpy);
333 g_assert_cmpint (str_cpy_size, ==, strlen (str));
336 /* Testing g_strlcat() function with various positive and negative cases */
340 gchar *str = "The quick brown fox jumps over the lazy dog";
341 gchar str_cpy[60] = { 0 };
342 gsize str_cpy_size = 0;
344 if (g_test_undefined ())
346 /* Testing degenerated cases */
347 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
348 "*assertion*!= NULL*");
349 str_cpy_size = g_strlcat (str_cpy, NULL, 0);
350 g_test_assert_expected_messages ();
351 /* Returned 0 because g_strlcpy() failed */
352 g_assert_cmpint (str_cpy_size, ==, 0);
354 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
355 "*assertion*!= NULL*");
356 str_cpy_size = g_strlcat (NULL, str, 0);
357 g_test_assert_expected_messages ();
358 /* Returned 0 because g_strlcpy() failed */
359 g_assert_cmpint (str_cpy_size, ==, 0);
362 str_cpy_size = g_strlcat (str_cpy, "", 0);
363 g_assert_cmpint (str_cpy_size, ==, strlen (""));
365 /* Testing normal usage cases.
366 * Note that the @dest_size argument to g_strlcat() is normally meant to be
367 * set to `sizeof (dest)`. We set it to various values `≤ sizeof (str_cpy)`
368 * for testing purposes. */
369 g_assert_cmpuint (strlen (str) + 1, <=, sizeof (str_cpy));
370 str_cpy_size = g_strlcat (str_cpy, str, strlen (str) + 1);
371 g_assert_cmpstr (str, ==, str_cpy);
372 g_assert_cmpint (str_cpy_size, ==, strlen (str));
374 g_assert_cmpuint (strlen (str), <=, sizeof (str_cpy));
375 str_cpy_size = g_strlcat (str_cpy, str, strlen (str));
376 g_assert_cmpstr (str, ==, str_cpy);
377 g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str));
379 g_assert_cmpuint (strlen (str) - 15, <=, sizeof (str_cpy));
380 str_cpy_size = g_strlcat (str_cpy, str, strlen (str) - 15);
381 g_assert_cmpstr (str, ==, str_cpy);
382 g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str) - 15);
384 g_assert_cmpuint (0, <=, sizeof (str_cpy));
385 str_cpy_size = g_strlcat (str_cpy, str, 0);
386 g_assert_cmpstr (str, ==, str_cpy);
387 g_assert_cmpint (str_cpy_size, ==, strlen (str));
389 g_assert_cmpuint (strlen (str) + 15, <=, sizeof (str_cpy));
390 str_cpy_size = g_strlcat (str_cpy, str, strlen (str) + 15);
391 g_assert_cmpstr ("The quick brown fox jumps over the lazy dogThe quick brow",
393 g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str));
396 /* Testing g_ascii_strdown() function with various positive and negative cases */
398 test_ascii_strdown (void)
400 const gchar *str_down = "the quick brown fox jumps over the lazy dog.";
401 const gchar *str_up = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
404 if (g_test_undefined ())
406 /* Testing degenerated cases */
407 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
408 "*assertion*!= NULL*");
409 str = g_ascii_strdown (NULL, 0);
410 g_test_assert_expected_messages ();
413 str = g_ascii_strdown ("", 0);
414 g_assert_nonnull (str);
415 g_assert_cmpstr (str, ==, "");
418 str = g_ascii_strdown ("", -1);
419 g_assert_nonnull (str);
420 g_assert_cmpstr (str, ==, "");
423 /* Testing normal usage cases */
424 str = g_ascii_strdown (str_down, strlen (str_down));
425 g_assert_nonnull (str);
426 g_assert_cmpstr (str, ==, str_down);
429 str = g_ascii_strdown (str_up, strlen (str_up));
430 g_assert_nonnull (str);
431 g_assert_cmpstr (str, ==, str_down);
434 str = g_ascii_strdown (str_up, -1);
435 g_assert_nonnull (str);
436 g_assert_cmpstr (str, ==, str_down);
439 str = g_ascii_strdown (str_up, 0);
440 g_assert_nonnull (str);
441 g_assert_cmpstr (str, ==, "");
445 /* Testing g_ascii_strup() function with various positive and negative cases */
447 test_ascii_strup (void)
449 const gchar *str_down = "the quick brown fox jumps over the lazy dog.";
450 const gchar *str_up = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
453 if (g_test_undefined ())
455 /* Testing degenerated cases */
456 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
457 "*assertion*!= NULL*");
458 str = g_ascii_strup (NULL, 0);
459 g_test_assert_expected_messages ();
462 str = g_ascii_strup ("", 0);
463 g_assert_nonnull (str);
464 g_assert_cmpstr (str, ==, "");
467 str = g_ascii_strup ("", -1);
468 g_assert_nonnull (str);
469 g_assert_cmpstr (str, ==, "");
472 /* Testing normal usage cases */
473 str = g_ascii_strup (str_up, strlen (str_up));
474 g_assert_nonnull (str);
475 g_assert_cmpstr (str, ==, str_up);
478 str = g_ascii_strup (str_down, strlen (str_down));
479 g_assert_nonnull (str);
480 g_assert_cmpstr (str, ==, str_up);
483 str = g_ascii_strup (str_down, -1);
484 g_assert_nonnull (str);
485 g_assert_cmpstr (str, ==, str_up);
488 str = g_ascii_strup (str_down, 0);
489 g_assert_nonnull (str);
490 g_assert_cmpstr (str, ==, "");
494 /* Testing g_strdup() function with various positive and negative cases */
500 g_assert_null (g_strdup (NULL));
502 str = g_strdup (GLIB_TEST_STRING);
503 g_assert_nonnull (str);
504 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
508 /* Testing g_strndup() function with various positive and negative cases */
514 str = g_strndup (NULL, 3);
517 str = g_strndup ("aaaa", 5);
518 g_assert_nonnull (str);
519 g_assert_cmpstr (str, ==, "aaaa");
522 str = g_strndup ("aaaa", 2);
523 g_assert_nonnull (str);
524 g_assert_cmpstr (str, ==, "aa");
528 /* Testing g_strdup_printf() function with various positive and negative cases */
530 test_strdup_printf (void)
534 str = g_strdup_printf ("%05d %-5s", 21, "test");
535 g_assert_nonnull (str);
536 g_assert_cmpstr (str, ==, "00021 test ");
540 /* Testing g_strdupv() function with various positive and negative cases */
544 gchar *vec[] = { "Foo", "Bar", NULL };
547 copy = g_strdupv (NULL);
548 g_assert_null (copy);
550 copy = g_strdupv (vec);
551 g_assert_nonnull (copy);
552 g_assert_cmpstrv (copy, vec);
556 /* Testing g_strfill() function with various positive and negative cases */
562 str = g_strnfill (0, 'a');
563 g_assert_nonnull (str);
564 g_assert_true (*str == '\0');
567 str = g_strnfill (5, 'a');
568 g_assert_nonnull (str);
569 g_assert_cmpstr (str, ==, "aaaaa");
573 /* Testing g_strconcat() function with various positive and negative cases */
575 test_strconcat (void)
579 str = g_strconcat (GLIB_TEST_STRING, NULL);
580 g_assert_nonnull (str);
581 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
584 str = g_strconcat (GLIB_TEST_STRING,
588 g_assert_nonnull (str);
589 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
592 g_assert_null (g_strconcat (NULL, "bla", NULL));
595 /* Testing g_strjoinv() function with various positive and negative cases */
599 gchar *strings[] = { "string1", "string2", NULL };
600 gchar *empty_strings[] = { NULL };
603 if (g_test_undefined ())
605 /* Testing degenerated cases */
606 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
607 "*assertion*!= NULL*");
608 str = g_strjoinv (NULL, NULL);
609 g_test_assert_expected_messages ();
612 str = g_strjoinv (":", strings);
613 g_assert_nonnull (str);
614 g_assert_cmpstr (str, ==, "string1:string2");
617 str = g_strjoinv (NULL, strings);
618 g_assert_nonnull (str);
619 g_assert_cmpstr (str, ==, "string1string2");
622 str = g_strjoinv (NULL, empty_strings);
623 g_assert_nonnull (str);
624 g_assert_cmpstr (str, ==, "");
628 /* Testing g_strjoin() function with various positive and negative cases */
634 str = g_strjoin (NULL, NULL);
635 g_assert_nonnull (str);
636 g_assert_true (*str == '\0');
639 str = g_strjoin (":", NULL);
640 g_assert_nonnull (str);
641 g_assert_true (*str == '\0');
644 str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
645 g_assert_nonnull (str);
646 g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
649 str = g_strjoin (NULL,
654 g_assert_nonnull (str);
655 g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
658 str = g_strjoin (":",
663 g_assert_nonnull (str);
664 g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
668 /* Testing g_strcanon() function with various positive and negative cases */
674 if (g_test_undefined ())
678 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
679 "*assertion*!= NULL*");
680 str = g_strcanon (NULL, "ab", 'y');
681 g_test_assert_expected_messages ();
684 str = g_strdup ("abxabxab");
685 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
686 "*assertion*!= NULL*");
687 ret = g_strcanon (str, NULL, 'y');
688 g_test_assert_expected_messages ();
693 str = g_strdup ("abxabxab");
694 str = g_strcanon (str, "ab", 'y');
695 g_assert_nonnull (str);
696 g_assert_cmpstr (str, ==, "abyabyab");
700 /* Testing g_strcompress() and g_strescape() functions with various cases */
702 test_strcompress_strescape (void)
708 if (g_test_undefined ())
710 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
711 "*assertion*!= NULL*");
712 str = g_strcompress (NULL);
713 g_test_assert_expected_messages ();
716 /* trailing slashes are not allowed */
717 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
719 str = g_strcompress ("abc\\");
720 g_test_assert_expected_messages ();
721 g_assert_cmpstr (str, ==, "abc");
725 str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
726 g_assert_nonnull (str);
727 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
731 if (g_test_undefined ())
733 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
734 "*assertion*!= NULL*");
735 str = g_strescape (NULL, NULL);
736 g_test_assert_expected_messages ();
740 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
741 g_assert_nonnull (str);
742 g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
745 str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
746 "\b\f\001\002\003\004");
747 g_assert_nonnull (str);
748 g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
752 tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
753 str = g_strcompress (tmp);
754 g_assert_nonnull (str);
755 g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
759 /* Unicode round trip */
760 str = g_strescape ("héllø there⸘", NULL);
761 g_assert_nonnull (str);
762 g_assert_cmpstr (str, ==, "h\\303\\251ll\\303\\270 there\\342\\270\\230");
763 tmp = g_strcompress (str);
764 g_assert_nonnull (tmp);
765 g_assert_cmpstr (tmp, ==, "héllø there⸘");
769 /* Test expanding invalid escapes */
770 str = g_strcompress ("\\11/ \\118 \\8aa \\19");
771 g_assert_nonnull (str);
772 g_assert_cmpstr (str, ==, "\t/ \t8 8aa \0019");
776 /* Testing g_ascii_strcasecmp() and g_ascii_strncasecmp() */
778 test_ascii_strcasecmp (void)
782 if (g_test_undefined ())
784 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
785 "*assertion*!= NULL*");
786 res = g_ascii_strcasecmp ("foo", NULL);
787 g_test_assert_expected_messages ();
788 g_assert_false (res);
790 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
791 "*assertion*!= NULL*");
792 res = g_ascii_strcasecmp (NULL, "foo");
793 g_test_assert_expected_messages ();
794 g_assert_false (res);
796 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
797 "*assertion*!= NULL*");
798 res = g_ascii_strncasecmp ("foo", NULL, 0);
799 g_test_assert_expected_messages ();
800 g_assert_false (res);
802 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
803 "*assertion*!= NULL*");
804 res = g_ascii_strncasecmp (NULL, "foo", 0);
805 g_test_assert_expected_messages ();
806 g_assert_false (res);
809 res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
810 g_assert_cmpint (res, ==, 0);
812 res = g_ascii_strcasecmp ("frobozz", "frobozz");
813 g_assert_cmpint (res, ==, 0);
815 res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
816 g_assert_cmpint (res, ==, 0);
818 res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
819 g_assert_cmpint (res, !=, 0);
821 res = g_ascii_strcasecmp ("", "");
822 g_assert_cmpint (res, ==, 0);
824 res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
825 g_assert_cmpint (res, ==, 0);
827 res = g_ascii_strcasecmp ("a", "b");
828 g_assert_cmpint (res, <, 0);
830 res = g_ascii_strcasecmp ("a", "B");
831 g_assert_cmpint (res, <, 0);
833 res = g_ascii_strcasecmp ("A", "b");
834 g_assert_cmpint (res, <, 0);
836 res = g_ascii_strcasecmp ("A", "B");
837 g_assert_cmpint (res, <, 0);
839 res = g_ascii_strcasecmp ("b", "a");
840 g_assert_cmpint (res, >, 0);
842 res = g_ascii_strcasecmp ("b", "A");
843 g_assert_cmpint (res, >, 0);
845 res = g_ascii_strcasecmp ("B", "a");
846 g_assert_cmpint (res, >, 0);
848 res = g_ascii_strcasecmp ("B", "A");
849 g_assert_cmpint (res, >, 0);
851 /* g_ascii_strncasecmp() */
852 res = g_ascii_strncasecmp ("", "", 10);
853 g_assert_cmpint (res, ==, 0);
855 res = g_ascii_strncasecmp ("Frob0ZZ", "frob0zz", strlen ("frobozz"));
856 g_assert_cmpint (res, ==, 0);
858 res = g_ascii_strncasecmp ("Frob0ZZ", "frobozz", strlen ("frobozz"));
859 g_assert_cmpint (res, !=, 0);
861 res = g_ascii_strncasecmp ("frob0ZZ", "FroB0zz", strlen ("frobozz"));
862 g_assert_cmpint (res, ==, 0);
864 res = g_ascii_strncasecmp ("Frob0ZZ", "froB0zz", strlen ("frobozz") - 5);
865 g_assert_cmpint (res, ==, 0);
867 res = g_ascii_strncasecmp ("Frob0ZZ", "froB0zz", strlen ("frobozz") + 5);
868 g_assert_cmpint (res, ==, 0);
872 do_test_strchug (const gchar *str, const gchar *expected)
877 tmp = g_strdup (str);
880 res = (strcmp (tmp, expected) == 0);
883 g_assert_cmpint (res, ==, TRUE);
886 /* Testing g_strchug() function with various positive and negative cases */
890 if (g_test_undefined ())
892 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
893 "*assertion*!= NULL*");
895 g_test_assert_expected_messages ();
898 do_test_strchug ("", "");
899 do_test_strchug (" ", "");
900 do_test_strchug ("\t\r\n ", "");
901 do_test_strchug (" a", "a");
902 do_test_strchug (" a", "a");
903 do_test_strchug ("a a", "a a");
904 do_test_strchug (" a a", "a a");
908 do_test_strchomp (const gchar *str, const gchar *expected)
913 tmp = g_strdup (str);
916 res = (strcmp (tmp, expected) == 0);
919 g_assert_cmpint (res, ==, TRUE);
922 /* Testing g_strchomp() function with various positive and negative cases */
926 if (g_test_undefined ())
928 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
929 "*assertion*!= NULL*");
931 g_test_assert_expected_messages ();
934 do_test_strchomp ("", "");
935 do_test_strchomp (" ", "");
936 do_test_strchomp (" \t\r\n", "");
937 do_test_strchomp ("a ", "a");
938 do_test_strchomp ("a ", "a");
939 do_test_strchomp ("a a", "a a");
940 do_test_strchomp ("a a ", "a a");
943 /* Testing g_str_tokenize_and_fold() functions */
945 test_str_tokenize_and_fold (void)
947 const gchar *local_str = "en_GB";
948 const gchar *sample = "The quick brown fox¸ jumps over the lazy dog.";
949 const gchar *special_cases = "quıck QUİCK QUİı QUıİ İıck ıİCK àìøş";
950 gchar **tokens, **alternates;
952 *expected_tokens[] = \
953 {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", NULL},
954 *expected_tokens_alt[] = \
955 { "quick", "quick", "quii", "quii", "iick", "iick", "àìøş", NULL};
957 if (g_test_undefined ())
959 /* Testing degenerated cases */
960 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
961 "*assertion*!= NULL*");
962 tokens = g_str_tokenize_and_fold (NULL, local_str, NULL);
963 g_test_assert_expected_messages ();
966 tokens = g_str_tokenize_and_fold (special_cases, local_str, &alternates);
967 g_assert_cmpint (g_strv_length (tokens), ==,
968 g_strv_length (expected_tokens_alt));
969 g_assert_true (g_strv_equal ((const gchar * const *) tokens,
970 (const gchar * const *) expected_tokens_alt));
972 g_strfreev (alternates);
974 tokens = g_str_tokenize_and_fold (sample, local_str, &alternates);
975 g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
976 g_assert_true (g_strv_equal ((const gchar * const *) tokens,
977 (const gchar * const *) expected_tokens));
979 g_strfreev (alternates);
981 tokens = g_str_tokenize_and_fold (sample, local_str, NULL);
982 g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
983 g_assert_true (g_strv_equal ((const gchar * const *) tokens,
984 (const gchar * const *) expected_tokens));
987 tokens = g_str_tokenize_and_fold (sample, NULL, &alternates);
988 g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
989 g_assert_true (g_strv_equal ((const gchar * const *) tokens,
990 (const gchar * const *) expected_tokens));
992 g_strfreev (alternates);
995 /* Testing g_strreverse() function with various positive and negative cases */
997 test_strreverse (void)
1002 if (g_test_undefined ())
1004 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1005 "*assertion*!= NULL*");
1006 str = g_strreverse (NULL);
1007 g_test_assert_expected_messages ();
1008 g_assert_null (str);
1011 str = p = g_strdup ("abcde");
1012 str = g_strreverse (str);
1013 g_assert_nonnull (str);
1014 g_assert_true (p == str);
1015 g_assert_cmpstr (str, ==, "edcba");
1019 /* Testing g_strncasecmp() functions */
1021 test_strncasecmp (void)
1023 g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 3), ==, 0);
1024 g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 4), !=, 0);
1033 haystack = g_strdup ("FooBarFooBarFoo");
1035 if (g_test_undefined ())
1037 /* Testing degenerated cases */
1038 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1039 "*assertion*!= NULL*");
1040 res = g_strstr_len (NULL, 0, "xxx");
1041 g_test_assert_expected_messages ();
1043 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1044 "*assertion*!= NULL*");
1045 res = g_strstr_len ("xxx", 0, NULL);
1046 g_test_assert_expected_messages ();
1050 res = g_strstr_len (haystack, 6, "xxx");
1051 g_assert_null (res);
1053 res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
1054 g_assert_null (res);
1056 res = g_strstr_len (haystack, 3, "Bar");
1057 g_assert_null (res);
1059 res = g_strstr_len (haystack, 6, "");
1060 g_assert_true (res == haystack);
1061 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
1063 res = g_strstr_len (haystack, 6, "Bar");
1064 g_assert_true (res == haystack + 3);
1065 g_assert_cmpstr (res, ==, "BarFooBarFoo");
1067 res = g_strstr_len (haystack, -1, "Bar");
1068 g_assert_true (res == haystack + 3);
1069 g_assert_cmpstr (res, ==, "BarFooBarFoo");
1072 if (g_test_undefined ())
1074 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1075 "*assertion*!= NULL*");
1076 res = g_strrstr (NULL, "xxx");
1077 g_test_assert_expected_messages ();
1079 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1080 "*assertion*!= NULL*");
1081 res = g_strrstr ("xxx", NULL);
1082 g_test_assert_expected_messages ();
1085 res = g_strrstr (haystack, "xxx");
1086 g_assert_null (res);
1088 res = g_strrstr (haystack, "FooBarFooBarFooBar");
1089 g_assert_null (res);
1091 res = g_strrstr (haystack, "");
1092 g_assert_true (res == haystack);
1093 g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
1095 res = g_strrstr (haystack, "Bar");
1096 g_assert_true (res == haystack + 9);
1097 g_assert_cmpstr (res, ==, "BarFoo");
1100 if (g_test_undefined ())
1102 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1103 "*assertion*!= NULL*");
1104 res = g_strrstr_len (NULL, 14, "xxx");
1105 g_test_assert_expected_messages ();
1107 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1108 "*assertion*!= NULL*");
1109 res = g_strrstr_len ("xxx", 14, NULL);
1110 g_test_assert_expected_messages ();
1113 res = g_strrstr_len (haystack, 14, "xxx");
1114 g_assert_null (res);
1116 res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
1117 g_assert_null (res);
1119 res = g_strrstr_len (haystack, 3, "Bar");
1120 g_assert_null (res);
1122 res = g_strrstr_len (haystack, 14, "BarFoo");
1123 g_assert_true (res == haystack + 3);
1124 g_assert_cmpstr (res, ==, "BarFooBarFoo");
1126 res = g_strrstr_len (haystack, 15, "BarFoo");
1127 g_assert_true (res == haystack + 9);
1128 g_assert_cmpstr (res, ==, "BarFoo");
1130 res = g_strrstr_len (haystack, -1, "BarFoo");
1131 g_assert_true (res == haystack + 9);
1132 g_assert_cmpstr (res, ==, "BarFoo");
1134 /* test case for strings with \0 in the middle */
1135 *(haystack + 7) = '\0';
1136 res = g_strstr_len (haystack, 15, "BarFoo");
1137 g_assert_null (res);
1142 /* Testing g_strtod() function with various positive and negative cases */
1146 gchar *str_end = NULL;
1148 const double gold_ratio = 1.61803398874989484;
1149 const gchar *gold_ratio_str = "1.61803398874989484";
1150 const gchar *minus_gold_ratio_str = "-1.61803398874989484";
1152 if (g_test_undefined ())
1154 /* Testing degenerated cases */
1155 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1156 "*assertion*!= NULL*");
1157 value = g_strtod (NULL, NULL);
1158 g_test_assert_expected_messages ();
1159 g_assert_cmpfloat (value, ==, 0.0);
1162 g_assert_cmpfloat (g_strtod ("\x00\x00\x00\x00", NULL), ==, 0.0);
1163 g_assert_cmpfloat (g_strtod ("\x00\x00\x00\x00", &str_end), ==, 0.0);
1164 g_assert_cmpstr (str_end, ==, "");
1165 g_assert_cmpfloat (g_strtod ("\xff\xff\xff\xff", NULL), ==, 0.0);
1166 g_assert_cmpfloat (g_strtod ("\xff\xff\xff\xff", &str_end), ==, 0.0);
1167 g_assert_cmpstr (str_end, ==, "\xff\xff\xff\xff");
1169 /* Testing normal usage cases */
1170 g_assert_cmpfloat (g_strtod (gold_ratio_str, NULL), ==, gold_ratio);
1171 g_assert_cmpfloat (g_strtod (gold_ratio_str, &str_end), ==, gold_ratio);
1172 g_assert_true (str_end == gold_ratio_str + strlen (gold_ratio_str));
1173 g_assert_cmpfloat (g_strtod (minus_gold_ratio_str, NULL), ==, -gold_ratio);
1174 g_assert_cmpfloat (g_strtod (minus_gold_ratio_str, &str_end), ==, -gold_ratio);
1175 g_assert_true (str_end == minus_gold_ratio_str + strlen (minus_gold_ratio_str));
1178 /* Testing g_strdelimit() function */
1180 test_strdelimit (void)
1182 const gchar *const_string = "ABCDE<*>Q";
1185 if (g_test_undefined ())
1187 /* Testing degenerated cases */
1188 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1189 "*assertion*!= NULL*");
1190 string = g_strdelimit (NULL, "ABCDE", 'N');
1191 g_test_assert_expected_messages ();
1194 string = g_strdelimit (g_strdup (const_string), "<>", '?');
1195 g_assert_cmpstr (string, ==, "ABCDE?*?Q");
1198 string = g_strdelimit (g_strdup (const_string), NULL, '?');
1199 g_assert_cmpstr (string, ==, "ABCDE?*?Q");
1203 /* Testing g_str_has_prefix() */
1205 test_has_prefix (void)
1209 if (g_test_undefined ())
1211 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1212 "*assertion*!= NULL*");
1213 res = g_str_has_prefix ("foo", NULL);
1214 g_test_assert_expected_messages ();
1215 g_assert_false (res);
1217 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1218 "*assertion*!= NULL*");
1219 res = g_str_has_prefix (NULL, "foo");
1220 g_test_assert_expected_messages ();
1221 g_assert_false (res);
1224 res = g_str_has_prefix ("foo", "bar");
1225 g_assert_cmpint (res, ==, FALSE);
1227 res = g_str_has_prefix ("foo", "foobar");
1228 g_assert_cmpint (res, ==, FALSE);
1230 res = g_str_has_prefix ("foobar", "bar");
1231 g_assert_cmpint (res, ==, FALSE);
1233 res = g_str_has_prefix ("foobar", "foo");
1234 g_assert_cmpint (res, ==, TRUE);
1236 res = g_str_has_prefix ("foo", "");
1237 g_assert_cmpint (res, ==, TRUE);
1239 res = g_str_has_prefix ("foo", "foo");
1240 g_assert_cmpint (res, ==, TRUE);
1242 res = g_str_has_prefix ("", "");
1243 g_assert_cmpint (res, ==, TRUE);
1247 test_has_suffix (void)
1251 if (g_test_undefined ())
1253 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1254 "*assertion*!= NULL*");
1255 res = g_str_has_suffix ("foo", NULL);
1256 g_test_assert_expected_messages ();
1257 g_assert_false (res);
1259 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1260 "*assertion*!= NULL*");
1261 res = g_str_has_suffix (NULL, "foo");
1262 g_test_assert_expected_messages ();
1263 g_assert_false (res);
1266 res = g_str_has_suffix ("foo", "bar");
1267 g_assert_false (res);
1269 res = g_str_has_suffix ("bar", "foobar");
1270 g_assert_false (res);
1272 res = g_str_has_suffix ("foobar", "foo");
1273 g_assert_false (res);
1275 res = g_str_has_suffix ("foobar", "bar");
1276 g_assert_true (res);
1278 res = g_str_has_suffix ("foo", "");
1279 g_assert_true (res);
1281 res = g_str_has_suffix ("foo", "foo");
1282 g_assert_true (res);
1284 res = g_str_has_suffix ("", "");
1285 g_assert_true (res);
1289 strv_check (gchar **strv, ...)
1295 va_start (list, strv);
1298 const gchar *str = va_arg (list, const char *);
1299 if (strv[i] == NULL)
1301 g_assert_null (str);
1310 g_assert_cmpstr (strv[i], ==, str);
1319 /* Testing g_strsplit() function with various positive and negative cases */
1321 test_strsplit (void)
1323 gchar **string = NULL;
1325 if (g_test_undefined ())
1327 /* Testing degenerated cases */
1328 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1329 "*assertion*!= NULL*");
1330 string = g_strsplit (NULL, ",", 0);
1331 g_test_assert_expected_messages ();
1332 g_assert_null (string);
1334 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1335 "*assertion*!= NULL*");
1336 string = g_strsplit ("x", NULL, 0);
1337 g_test_assert_expected_messages ();
1338 g_assert_null (string);
1340 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1341 "*assertion \'delimiter[0] != \'\\0\'*");
1342 string = g_strsplit ("x", "", 0);
1343 g_test_assert_expected_messages ();
1344 g_assert_null (string);
1347 strv_check (g_strsplit ("", ",", 0), NULL);
1348 strv_check (g_strsplit ("x", ",", 0), "x", NULL);
1349 strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
1350 strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
1351 strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
1352 strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
1353 strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
1354 strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
1355 strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
1356 strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
1357 strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1358 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
1360 strv_check (g_strsplit ("", ",", 1), NULL);
1361 strv_check (g_strsplit ("x", ",", 1), "x", NULL);
1362 strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
1363 strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
1364 strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
1365 strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
1366 strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
1367 strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
1368 strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
1369 strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
1370 strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
1371 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
1373 strv_check (g_strsplit ("", ",", 2), NULL);
1374 strv_check (g_strsplit ("x", ",", 2), "x", NULL);
1375 strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
1376 strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
1377 strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
1378 strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
1379 strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
1380 strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
1381 strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
1382 strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
1383 strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
1384 strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
1387 /* Testing function g_strsplit_set() */
1389 test_strsplit_set (void)
1391 gchar **string = NULL;
1393 if (g_test_undefined ())
1395 /* Testing degenerated cases */
1396 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1397 "*assertion*!= NULL*");
1398 string = g_strsplit_set (NULL, ",/", 0);
1399 g_test_assert_expected_messages ();
1400 g_assert_null (string);
1402 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1403 "*assertion*!= NULL*");
1404 string = g_strsplit_set ("", NULL, 0);
1405 g_test_assert_expected_messages ();
1406 g_assert_null (string);
1409 strv_check (g_strsplit_set ("", ",/", 0), NULL);
1410 strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
1411 strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
1412 strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
1413 strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
1415 strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
1416 strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
1417 strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
1418 strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
1419 strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
1420 strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
1421 strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1422 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1424 strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
1425 strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
1426 strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
1427 strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
1428 strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
1429 strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
1431 strv_check (g_strsplit_set ("", ",", 0), NULL);
1432 strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
1433 strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
1434 strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
1435 strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
1436 strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
1437 strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
1438 strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
1439 strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
1440 strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
1441 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1443 strv_check (g_strsplit_set ("", ",", 1), NULL);
1444 strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
1445 strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
1446 strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
1447 strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
1448 strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
1449 strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
1450 strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
1451 strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
1452 strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
1453 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
1454 strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
1456 strv_check (g_strsplit_set ("", ",", 2), NULL);
1457 strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
1458 strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
1459 strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
1460 strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
1461 strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
1462 strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
1463 strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
1464 strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
1465 strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
1466 strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
1468 strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
1471 /* Testing g_strv_length() function with various positive and negative cases */
1473 test_strv_length (void)
1478 if (g_test_undefined ())
1480 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1481 "*assertion*!= NULL*");
1482 l = g_strv_length (NULL);
1483 g_test_assert_expected_messages ();
1484 g_assert_cmpint (l, ==, 0);
1487 strv = g_strsplit ("1,2,3,4", ",", -1);
1488 l = g_strv_length (strv);
1489 g_assert_cmpuint (l, ==, 4);
1493 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
1496 check_strtod_string (gchar *number,
1505 /* we try a copy of number, with some free space for malloc before that.
1506 * This is supposed to smash the some wrong pointer calculations. */
1508 dummy = g_malloc (100000);
1509 number = g_strdup (number);
1512 for (l = 0; l < G_N_ELEMENTS (locales); l++)
1514 gchar *end = "(unset)";
1516 setlocale (LC_NUMERIC, locales[l]);
1517 d = g_ascii_strtod (number, &end);
1518 g_assert_true (isnan (res) ? isnan (d) : (d == res));
1519 g_assert_true ((gsize) (end - number) ==
1520 (check_end ? correct_len : strlen (number)));
1527 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
1530 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
1532 for (l = 0; l < G_N_ELEMENTS (locales); l++)
1534 setlocale (LC_ALL, locales[l]);
1535 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
1536 g_assert_cmpstr (buf, ==, str);
1540 /* Testing g_ascii_strtod() function with various positive and negative cases */
1542 test_ascii_strtod (void)
1544 gdouble d, our_nan, our_inf;
1545 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
1550 /* Do this before any call to setlocale. */
1551 our_nan = atof ("NaN");
1553 g_assert_true (isnan (our_nan));
1558 our_inf = atof ("Infinity");
1560 g_assert_true (our_inf > 1 && our_inf == our_inf / 2);
1562 /* Testing degenerated cases */
1563 if (g_test_undefined ())
1565 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1566 "*assertion*!= NULL*");
1567 d = g_ascii_strtod (NULL, NULL);
1568 g_test_assert_expected_messages ();
1571 /* Testing normal cases */
1572 check_strtod_string ("123.123", 123.123, FALSE, 0);
1573 check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
1574 check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
1575 check_strtod_string ("-123.123", -123.123, FALSE, 0);
1576 check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
1577 check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
1578 check_strtod_string ("5.4", 5.4, TRUE, 3);
1579 check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
1580 check_strtod_string ("5,4", 5.0, TRUE, 1);
1582 /* hex strings for strtod() is a C99 feature which Visual C++ does not support */
1583 check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
1584 check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
1585 check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
1586 check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
1587 check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
1589 /* the following are for #156421 */
1590 check_strtod_string ("1e1", 1e1, FALSE, 0);
1592 /* NAN/-nan/INF/-infinity strings for strtod() are C99 features which Visual C++ does not support */
1593 check_strtod_string ("NAN", our_nan, FALSE, 0);
1594 check_strtod_string ("-nan", -our_nan, FALSE, 0);
1595 check_strtod_string ("INF", our_inf, FALSE, 0);
1596 check_strtod_string ("-infinity", -our_inf, FALSE, 0);
1598 check_strtod_string ("-.75,0", -0.75, TRUE, 4);
1601 /* the values of d in the following 2 tests generate a C1064 compiler limit error */
1602 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1603 g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1605 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1606 g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1609 d = pow (2.0, -1024.1);
1610 g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1612 d = -pow (2.0, -1024.1);
1613 g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1616 check_strtod_string (" 0.75", 0.75, FALSE, 0);
1617 check_strtod_string (" +0.75", 0.75, FALSE, 0);
1618 check_strtod_string (" -0.75", -0.75, FALSE, 0);
1619 check_strtod_string ("\f0.75", 0.75, FALSE, 0);
1620 check_strtod_string ("\n0.75", 0.75, FALSE, 0);
1621 check_strtod_string ("\r0.75", 0.75, FALSE, 0);
1622 check_strtod_string ("\t0.75", 0.75, FALSE, 0);
1625 /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1626 check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1630 check_strtod_number (0.75, "%0.2f", "0.75");
1631 check_strtod_number (0.75, "%5.2f", " 0.75");
1632 check_strtod_number (-0.75, "%0.2f", "-0.75");
1633 check_strtod_number (-0.75, "%5.2f", "-0.75");
1634 check_strtod_number (1e99, "%.0e", "1e+99");
1638 check_uint64 (const gchar *str,
1645 gchar *endptr = NULL;
1649 actual = g_ascii_strtoull (str, &endptr, base);
1652 g_assert_true (actual == result);
1653 g_assert_cmpstr (end, ==, endptr);
1654 g_assert_true (err == error);
1658 check_int64 (const gchar *str,
1665 gchar *endptr = NULL;
1669 actual = g_ascii_strtoll (str, &endptr, base);
1672 g_assert_true (actual == result);
1673 g_assert_cmpstr (end, ==, endptr);
1674 g_assert_true (err == error);
1680 check_uint64 ("0", "", 10, 0, 0);
1681 check_uint64 ("+0", "", 10, 0, 0);
1682 check_uint64 ("-0", "", 10, 0, 0);
1683 check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1684 check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1685 check_uint64 ("20xyz", "xyz", 10, 20, 0);
1686 check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1687 check_uint64 ("-FF4", "", 16, -((guint64) 0xFF4), 0);
1689 check_int64 ("0", "", 10, 0, 0);
1690 check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1691 check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1692 check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1693 check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1694 check_int64 ("32768", "", 10, 32768, 0);
1695 check_int64 ("-32768", "", 10, -32768, 0);
1696 check_int64 ("001", "", 10, 1, 0);
1697 check_int64 ("-001", "", 10, -1, 0);
1700 /* Testing g_str_match_string() function with various cases */
1702 test_str_match_string (void)
1704 gboolean result = TRUE;
1705 const gchar *str = "The quick brown fox¸ jumps over the lazy dog.";
1707 if (g_test_undefined ())
1709 /* Testing degenerated cases */
1710 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1711 "*assertion*!= NULL*");
1712 result = g_str_match_string (NULL, "AAA", TRUE);
1713 g_test_assert_expected_messages ();
1714 g_assert_false (result);
1716 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1717 "*assertion*!= NULL*");
1718 result = g_str_match_string (str, NULL, TRUE);
1719 g_test_assert_expected_messages ();
1720 g_assert_false (result);
1723 g_assert_false (g_str_match_string (str, "AAA", TRUE));
1724 g_assert_false (g_str_match_string (str, "AAA", FALSE));
1727 /* Testing functions bounds */
1731 GMappedFile *file, *before, *after;
1736 const char * const strjoinv_0[] = { NULL };
1737 const char * const strjoinv_1[] = { "foo", NULL };
1739 /* if we allocate the file between two others and then free those
1740 * other two, then hopefully we end up with unmapped memory on either
1743 before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1745 /* quick workaround until #549783 can be fixed */
1749 file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1750 after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1751 g_mapped_file_unref (before);
1752 g_mapped_file_unref (after);
1754 g_assert_nonnull (file);
1755 g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1756 string = g_mapped_file_get_contents (file);
1758 /* ensure they're all non-nul */
1759 g_assert_null (memchr (string, '\0', 4096));
1761 /* test set 1: ensure that nothing goes past its maximum length, even in
1762 * light of a missing nul terminator.
1764 * we try to test all of the 'n' functions here.
1766 tmp = g_strndup (string, 4096);
1767 g_assert_cmpint (strlen (tmp), ==, 4096);
1770 /* found no bugs in gnome, i hope :) */
1771 g_assert_null (g_strstr_len (string, 4096, "BUGS"));
1772 g_strstr_len (string, 4096, "B");
1773 g_strstr_len (string, 4096, ".");
1774 g_strstr_len (string, 4096, "");
1776 g_strrstr_len (string, 4096, "BUGS");
1777 g_strrstr_len (string, 4096, "B");
1778 g_strrstr_len (string, 4096, ".");
1779 g_strrstr_len (string, 4096, "");
1781 tmp = g_ascii_strup (string, 4096);
1782 tmp2 = g_ascii_strup (tmp, 4096);
1783 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1784 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1785 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1789 tmp = g_ascii_strdown (string, 4096);
1790 tmp2 = g_ascii_strdown (tmp, 4096);
1791 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1792 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1793 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1797 tmp = g_markup_escape_text (string, 4096);
1800 /* test set 2: ensure that nothing reads even one byte past a '\0'.
1802 g_assert_cmpint (string[4095], ==, '\n');
1803 string[4095] = '\0';
1805 tmp = g_strdup (string);
1806 g_assert_cmpint (strlen (tmp), ==, 4095);
1809 tmp = g_strndup (string, 10000);
1810 g_assert_cmpint (strlen (tmp), ==, 4095);
1813 g_stpcpy (buffer, string);
1814 g_assert_cmpint (strlen (buffer), ==, 4095);
1816 g_strstr_len (string, 10000, "BUGS");
1817 g_strstr_len (string, 10000, "B");
1818 g_strstr_len (string, 10000, ".");
1819 g_strstr_len (string, 10000, "");
1821 g_strrstr (string, "BUGS");
1822 g_strrstr (string, "B");
1823 g_strrstr (string, ".");
1824 g_strrstr (string, "");
1826 g_strrstr_len (string, 10000, "BUGS");
1827 g_strrstr_len (string, 10000, "B");
1828 g_strrstr_len (string, 10000, ".");
1829 g_strrstr_len (string, 10000, "");
1831 g_str_has_prefix (string, "this won't do very much...");
1832 g_str_has_suffix (string, "but maybe this will...");
1833 g_str_has_suffix (string, "HMMMM.");
1834 g_str_has_suffix (string, "MMMM.");
1835 g_str_has_suffix (string, "M.");
1837 g_strlcpy (buffer, string, sizeof buffer);
1838 g_assert_cmpint (strlen (buffer), ==, 4095);
1839 g_strlcpy (buffer, string, sizeof buffer);
1841 g_strlcat (buffer, string, sizeof buffer);
1842 g_assert_cmpint (strlen (buffer), ==, 4095);
1844 tmp = g_strdup_printf ("<%s>", string);
1845 g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1848 tmp = g_ascii_strdown (string, -1);
1849 tmp2 = g_ascii_strdown (tmp, -1);
1850 g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
1851 g_assert_cmpint (strlen (string), ==, strlen (tmp));
1852 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1853 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1854 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1858 tmp = g_ascii_strup (string, -1);
1859 tmp2 = g_ascii_strup (string, -1);
1860 g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
1861 g_assert_cmpint (strlen (string), ==, strlen (tmp));
1862 g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1863 g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1864 g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1868 g_ascii_strcasecmp (string, string);
1869 g_ascii_strncasecmp (string, string, 10000);
1871 g_strreverse (string);
1872 g_strreverse (string);
1874 g_strchomp (string);
1875 g_strstrip (string);
1876 g_assert_cmpint (strlen (string), ==, 4095);
1878 g_strdelimit (string, "M", 'N');
1879 g_strcanon (string, " N.", ':');
1880 g_assert_cmpint (strlen (string), ==, 4095);
1882 array = g_strsplit (string, ".", -1);
1883 tmp = g_strjoinv (".", array);
1886 g_assert_cmpmem (tmp, strlen (tmp), string, 4095);
1889 tmp = g_strjoinv ("/", (char **) strjoinv_0);
1890 g_assert_cmpstr (tmp, ==, "");
1893 tmp = g_strjoinv ("/", (char **) strjoinv_1);
1894 g_assert_cmpstr (tmp, ==, "foo");
1897 tmp = g_strconcat (string, string, string, NULL);
1898 g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1901 tmp = g_strjoin ("!", string, string, NULL);
1902 g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1905 tmp = g_markup_escape_text (string, -1);
1908 tmp = g_markup_printf_escaped ("%s", string);
1911 tmp = g_strescape (string, NULL);
1912 tmp2 = g_strcompress (tmp);
1913 g_assert_cmpstr (string, ==, tmp2);
1917 g_mapped_file_unref (file);
1920 /* Testing g_strip_context() function with various cases */
1922 test_strip_context (void)
1925 const gchar *msgval;
1930 s = g_strip_context (msgid, msgval);
1931 g_assert_true (s == msgval);
1933 msgid = msgval = "blabla";
1934 s = g_strip_context (msgid, msgval);
1935 g_assert_true (s == msgval);
1937 msgid = msgval = "blabla|foo";
1938 s = g_strip_context (msgid, msgval);
1939 g_assert_true (s == msgval + 7);
1941 msgid = msgval = "blabla||bar";
1942 s = g_strip_context (msgid, msgval);
1943 g_assert_true (s == msgval + 7);
1946 /* Test the strings returned by g_strerror() are valid and unique. On Windows,
1947 * fewer than 200 error numbers are used, so we expect some strings to
1948 * return a generic ‘unknown error code’ message. */
1950 test_strerror (void)
1954 const gchar *str, *unknown_str;
1956 setlocale (LC_ALL, "C");
1958 unknown_str = g_strerror (-1);
1959 strs = g_hash_table_new (g_str_hash, g_str_equal);
1960 for (i = 1; i < 200; i++)
1962 gboolean is_unknown;
1963 str = g_strerror (i);
1964 is_unknown = (strcmp (str, unknown_str) == 0);
1965 g_assert_nonnull (str);
1966 g_assert_true (g_utf8_validate (str, -1, NULL));
1967 g_assert_true (!g_hash_table_contains (strs, str) || is_unknown);
1968 g_hash_table_add (strs, (gpointer) str);
1971 g_hash_table_unref (strs);
1974 /* Testing g_strsignal() function with various cases */
1976 test_strsignal (void)
1981 for (i = 1; i < 20; i++)
1983 str = g_strsignal (i);
1984 g_assert_nonnull (str);
1985 g_assert_true (g_utf8_validate (str, -1, NULL));
1989 /* Testing g_strup(), g_strdown() and g_strcasecmp() */
1995 if (g_test_undefined ())
1997 /* Testing degenerated cases */
1998 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1999 "*assertion*!= NULL*");
2001 g_test_assert_expected_messages ();
2003 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2004 "*assertion*!= NULL*");
2005 s = g_strdown (NULL);
2006 g_test_assert_expected_messages ();
2008 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2009 "*assertion*!= NULL*");
2010 g_strcasecmp (NULL, "ABCD");
2011 g_test_assert_expected_messages ();
2013 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2014 "*assertion*!= NULL*");
2015 g_strcasecmp ("abcd", NULL);
2016 g_test_assert_expected_messages ();
2019 s = g_strdup ("lower UPPER");
2020 g_assert_cmpstr (g_strup (s), ==, "LOWER UPPER");
2021 g_assert_cmpstr (g_strdown (s), ==, "lower upper");
2022 g_assert_true (g_strcasecmp ("lower", "LOWER") == 0);
2026 /* Testing g_str_to_ascii() function with various cases */
2028 test_transliteration (void)
2032 /* ...to test the defaults */
2033 setlocale (LC_ALL, "C");
2035 /* Test something trivial */
2036 out = g_str_to_ascii ("hello", NULL);
2037 g_assert_cmpstr (out, ==, "hello");
2040 /* Test something above 0xffff */
2041 out = g_str_to_ascii ("𝐀𝐀𝐀", NULL);
2042 g_assert_cmpstr (out, ==, "AAA");
2045 /* Test something with no good match */
2046 out = g_str_to_ascii ("a ∧ ¬a", NULL);
2047 g_assert_cmpstr (out, ==, "a ? ?a");
2050 /* Make sure 'ö' is handled differently per locale */
2051 out = g_str_to_ascii ("ö", NULL);
2052 g_assert_cmpstr (out, ==, "o");
2055 out = g_str_to_ascii ("ö", "sv");
2056 g_assert_cmpstr (out, ==, "o");
2059 out = g_str_to_ascii ("ö", "de");
2060 g_assert_cmpstr (out, ==, "oe");
2063 /* Make sure we can find a locale by a wide range of names */
2064 out = g_str_to_ascii ("ö", "de_DE");
2065 g_assert_cmpstr (out, ==, "oe");
2068 out = g_str_to_ascii ("ö", "de_DE.UTF-8");
2069 g_assert_cmpstr (out, ==, "oe");
2072 out = g_str_to_ascii ("ö", "de_DE.UTF-8@euro");
2073 g_assert_cmpstr (out, ==, "oe");
2076 out = g_str_to_ascii ("ö", "de@euro");
2077 g_assert_cmpstr (out, ==, "oe");
2080 /* Test some invalid locale names */
2081 out = g_str_to_ascii ("ö", "de_DE@euro.UTF-8");
2082 g_assert_cmpstr (out, ==, "o");
2085 out = g_str_to_ascii ("ö", "de@DE@euro");
2086 g_assert_cmpstr (out, ==, "o");
2089 out = g_str_to_ascii ("ö", "doesnotexist");
2090 g_assert_cmpstr (out, ==, "o");
2093 out = g_str_to_ascii ("ö", "thislocalenameistoolong");
2094 g_assert_cmpstr (out, ==, "o");
2097 /* Try a lookup of a locale with a variant */
2098 out = g_str_to_ascii ("б", "sr_RS");
2099 g_assert_cmpstr (out, ==, "b");
2102 out = g_str_to_ascii ("б", "sr_RS@latin");
2103 g_assert_cmpstr (out, ==, "?");
2106 /* Ukrainian contains the only multi-character mappings.
2107 * Try a string that contains one ('зг') along with a partial
2108 * sequence ('з') at the end.
2110 out = g_str_to_ascii ("Зліва направо, згори вниз", "uk");
2111 g_assert_cmpstr (out, ==, "Zliva napravo, zghory vnyz");
2114 /* Try out the other combinations */
2115 out = g_str_to_ascii ("Зг", "uk");
2116 g_assert_cmpstr (out, ==, "Zgh");
2119 out = g_str_to_ascii ("зГ", "uk");
2120 g_assert_cmpstr (out, ==, "zGH");
2123 out = g_str_to_ascii ("ЗГ", "uk");
2124 g_assert_cmpstr (out, ==, "ZGH");
2127 /* And a non-combination */
2128 out = g_str_to_ascii ("зя", "uk");
2129 g_assert_cmpstr (out, ==, "zya");
2133 /* Testing g_strv_contains() function with various cases */
2135 test_strv_contains (void)
2137 gboolean result = TRUE;
2138 const gchar *strv_simple[] = { "hello", "there", NULL };
2139 const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
2140 const gchar *strv_empty[] = { NULL };
2142 if (g_test_undefined ())
2144 /* Testing degenerated cases */
2145 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2146 "*assertion*!= NULL*");
2147 result = g_strv_contains (NULL, "hello");
2148 g_test_assert_expected_messages ();
2149 g_assert_false (result);
2151 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2152 "*assertion*!= NULL*");
2153 result = g_strv_contains (strv_simple, NULL);
2154 g_test_assert_expected_messages ();
2155 g_assert_false (result);
2158 g_assert_true (g_strv_contains (strv_simple, "hello"));
2159 g_assert_true (g_strv_contains (strv_simple, "there"));
2160 g_assert_false (g_strv_contains (strv_simple, "non-existent"));
2161 g_assert_false (g_strv_contains (strv_simple, ""));
2163 g_assert_true (g_strv_contains (strv_dupe, "dupe"));
2165 g_assert_false (g_strv_contains (strv_empty, "empty!"));
2166 g_assert_false (g_strv_contains (strv_empty, ""));
2169 /* Test g_strv_equal() works for various inputs. */
2171 test_strv_equal (void)
2173 gboolean result = TRUE;
2174 const gchar *strv_empty[] = { NULL };
2175 const gchar *strv_empty2[] = { NULL };
2176 const gchar *strv_simple[] = { "hello", "you", NULL };
2177 const gchar *strv_simple2[] = { "hello", "you", NULL };
2178 const gchar *strv_simple_reordered[] = { "you", "hello", NULL };
2179 const gchar *strv_simple_superset[] = { "hello", "you", "again", NULL };
2180 const gchar *strv_another[] = { "not", "a", "coded", "message", NULL };
2182 if (g_test_undefined ())
2184 /* Testing degenerated cases */
2185 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2186 "*assertion*!= NULL*");
2187 result = g_strv_equal (NULL, strv_simple2);
2188 g_test_assert_expected_messages ();
2189 g_assert_false (result);
2191 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2192 "*assertion*!= NULL*");
2193 result = g_strv_equal (strv_simple, NULL);
2194 g_test_assert_expected_messages ();
2195 g_assert_false (result);
2198 g_assert_true (g_strv_equal (strv_empty, strv_empty));
2199 g_assert_true (g_strv_equal (strv_empty, strv_empty2));
2200 g_assert_true (g_strv_equal (strv_empty2, strv_empty));
2201 g_assert_false (g_strv_equal (strv_empty, strv_simple));
2202 g_assert_false (g_strv_equal (strv_simple, strv_empty));
2203 g_assert_true (g_strv_equal (strv_simple, strv_simple));
2204 g_assert_true (g_strv_equal (strv_simple, strv_simple2));
2205 g_assert_true (g_strv_equal (strv_simple2, strv_simple));
2206 g_assert_false (g_strv_equal (strv_simple, strv_simple_reordered));
2207 g_assert_false (g_strv_equal (strv_simple_reordered, strv_simple));
2208 g_assert_false (g_strv_equal (strv_simple, strv_simple_superset));
2209 g_assert_false (g_strv_equal (strv_simple_superset, strv_simple));
2210 g_assert_false (g_strv_equal (strv_simple, strv_another));
2211 g_assert_false (g_strv_equal (strv_another, strv_simple));
2228 gboolean should_fail;
2229 GNumberParserError error_code;
2232 const TestData test_data[] = {
2233 /* typical cases for signed */
2234 { "0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2235 { "+0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2236 { "-0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2237 { "-2", SIGNED, 10, -2, 2, -2, FALSE, 0 },
2238 {"-02", SIGNED, 10, -2, 2, -2, FALSE, 0 },
2239 { "2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2240 { "02", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2241 { "+2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2242 {"+02", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2243 { "3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2244 { "+3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2245 { "-3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2247 /* typical cases for unsigned */
2248 { "-1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2249 { "1", UNSIGNED, 10, 0, 2, 1, FALSE, 0 },
2250 { "+1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2251 { "0", UNSIGNED, 10, 0, 2, 0, FALSE, 0 },
2252 { "+0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2253 { "-0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2254 { "2", UNSIGNED, 10, 0, 2, 2, FALSE, 0 },
2255 { "+2", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2256 { "3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2257 { "+3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2259 /* min == max cases for signed */
2260 { "-2", SIGNED, 10, -2, -2, -2, FALSE, 0 },
2261 { "-1", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2262 { "-3", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2264 /* min == max cases for unsigned */
2265 { "2", UNSIGNED, 10, 2, 2, 2, FALSE, 0 },
2266 { "3", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2267 { "1", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2269 /* invalid inputs */
2270 { "", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2271 { "", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2272 { "a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2273 { "a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2274 { "1a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2275 { "1a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2276 { "- 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2278 /* leading/trailing whitespace */
2279 { " 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2280 { " 1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2281 { "1 ", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2282 { "1 ", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2284 /* hexadecimal numbers */
2285 { "a", SIGNED, 16, 0, 15, 10, FALSE, 0 },
2286 { "a", UNSIGNED, 16, 0, 15, 10, FALSE, 0 },
2287 { "0a", UNSIGNED, 16, 0, 15, 10, FALSE, 0 },
2288 { "0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2289 { "0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2290 { "-0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2291 { "-0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2292 { "+0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2293 { "+0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2294 { "- 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2295 { "- 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2296 { "+ 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2297 { "+ 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2300 /* Testing g_ascii_string_to_signed() and g_ascii_string_to_unsigned() functions */
2302 test_ascii_string_to_number_usual (void)
2306 GError *error = NULL;
2307 const TestData *data;
2310 guint64 valueu64 = 0;
2312 /*** g_ascii_string_to_signed() ***/
2313 data = &test_data[0]; /* Setting data to signed data */
2315 if (g_test_undefined ())
2317 /* Testing degenerated cases */
2318 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2319 "*assertion*!= NULL*");
2320 result = g_ascii_string_to_signed (NULL,
2326 g_test_assert_expected_messages ();
2328 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2329 "*assertion \'base >= 2 && base <= 36\'*");
2330 result = g_ascii_string_to_signed (data->str,
2336 g_test_assert_expected_messages ();
2338 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2339 "*assertion \'base >= 2 && base <= 36\'*");
2340 result = g_ascii_string_to_signed (data->str,
2346 g_test_assert_expected_messages ();
2348 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2349 "*assertion \'min <= max\'*");
2350 result = g_ascii_string_to_signed (data->str,
2356 g_test_assert_expected_messages ();
2359 /* Catching first part of (error == NULL || *error == NULL) */
2360 result = g_ascii_string_to_signed (data->str,
2367 /*** g_ascii_string_to_unsigned() ***/
2368 data = &test_data[12]; /* Setting data to unsigned data */
2370 if (g_test_undefined ())
2372 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2373 "*assertion*!= NULL*");
2374 result = g_ascii_string_to_unsigned (NULL,
2380 g_test_assert_expected_messages ();
2382 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2383 "*assertion \'base >= 2 && base <= 36\'*");
2384 result = g_ascii_string_to_unsigned (data->str,
2390 g_test_assert_expected_messages ();
2392 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2393 "*assertion \'base >= 2 && base <= 36\'*");
2394 result = g_ascii_string_to_unsigned (data->str,
2400 g_test_assert_expected_messages ();
2402 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2403 "*assertion \'min <= max\'*");
2404 result = g_ascii_string_to_unsigned (data->str,
2410 g_test_assert_expected_messages ();
2413 /* Catching first part of (error == NULL || *error == NULL) */
2414 result = g_ascii_string_to_unsigned (data->str,
2421 /* Testing usual cases */
2422 for (idx = 0; idx < G_N_ELEMENTS (test_data); ++idx)
2424 data = &test_data[idx];
2426 switch (data->sign_type)
2430 result = g_ascii_string_to_signed (data->str,
2437 g_assert_cmpint (value, ==, value64);
2443 guint64 value64 = 0;
2444 result = g_ascii_string_to_unsigned (data->str,
2451 g_assert_cmpint (value, ==, value64);
2456 g_assert_not_reached ();
2459 if (data->should_fail)
2461 g_assert_false (result);
2462 g_assert_error (error, G_NUMBER_PARSER_ERROR, (gint) data->error_code);
2463 g_clear_error (&error);
2467 g_assert_true (result);
2468 g_assert_no_error (error);
2469 g_assert_cmpint (value, ==, data->expected);
2474 /* Testing pathological cases for g_ascii_string_to_(un)signed() */
2476 test_ascii_string_to_number_pathological (void)
2478 GError *error = NULL;
2479 const gchar *crazy_high = "999999999999999999999999999999999999";
2480 const gchar *crazy_low = "-999999999999999999999999999999999999";
2481 const gchar *max_uint64 = "18446744073709551615";
2482 const gchar *max_int64 = "9223372036854775807";
2483 const gchar *min_int64 = "-9223372036854775808";
2487 g_assert_false (g_ascii_string_to_unsigned (crazy_high,
2493 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2494 g_clear_error (&error);
2495 g_assert_false (g_ascii_string_to_unsigned (crazy_low,
2501 // crazy_low is a signed number so it is not a valid unsigned number
2502 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID);
2503 g_clear_error (&error);
2505 g_assert_false (g_ascii_string_to_signed (crazy_high,
2511 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2512 g_clear_error (&error);
2513 g_assert_false (g_ascii_string_to_signed (crazy_low,
2519 g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2520 g_clear_error (&error);
2522 g_assert_true (g_ascii_string_to_unsigned (max_uint64,
2528 g_assert_no_error (error);
2529 g_assert_cmpint (uvalue, ==, G_MAXUINT64);
2531 g_assert_true (g_ascii_string_to_signed (max_int64,
2537 g_assert_no_error (error);
2538 g_assert_cmpint (svalue, ==, G_MAXINT64);
2540 g_assert_true (g_ascii_string_to_signed (min_int64,
2546 g_assert_no_error (error);
2547 g_assert_cmpint (svalue, ==, G_MININT64);
2554 g_test_init (&argc, &argv, NULL);
2556 g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
2557 g_test_add_func ("/strfuncs/ascii-string-to-num/pathological", test_ascii_string_to_number_pathological);
2558 g_test_add_func ("/strfuncs/ascii-string-to-num/usual", test_ascii_string_to_number_usual);
2559 g_test_add_func ("/strfuncs/ascii_strdown", test_ascii_strdown);
2560 g_test_add_func ("/strfuncs/ascii_strdup", test_ascii_strup);
2561 g_test_add_func ("/strfuncs/ascii_strtod", test_ascii_strtod);
2562 g_test_add_func ("/strfuncs/bounds-check", test_bounds);
2563 g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
2564 g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
2565 g_test_add_func ("/strfuncs/memdup", test_memdup);
2566 g_test_add_func ("/strfuncs/memdup2", test_memdup2);
2567 g_test_add_func ("/strfuncs/stpcpy", test_stpcpy);
2568 g_test_add_func ("/strfuncs/str_match_string", test_str_match_string);
2569 g_test_add_func ("/strfuncs/str_tokenize_and_fold", test_str_tokenize_and_fold);
2570 g_test_add_func ("/strfuncs/strcanon", test_strcanon);
2571 g_test_add_func ("/strfuncs/strchomp", test_strchomp);
2572 g_test_add_func ("/strfuncs/strchug", test_strchug);
2573 g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
2574 g_test_add_func ("/strfuncs/strconcat", test_strconcat);
2575 g_test_add_func ("/strfuncs/strdelimit", test_strdelimit);
2576 g_test_add_func ("/strfuncs/strdup", test_strdup);
2577 g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
2578 g_test_add_func ("/strfuncs/strdupv", test_strdupv);
2579 g_test_add_func ("/strfuncs/strerror", test_strerror);
2580 g_test_add_func ("/strfuncs/strip-context", test_strip_context);
2581 g_test_add_func ("/strfuncs/strjoin", test_strjoin);
2582 g_test_add_func ("/strfuncs/strjoinv", test_strjoinv);
2583 g_test_add_func ("/strfuncs/strlcat", test_strlcat);
2584 g_test_add_func ("/strfuncs/strlcpy", test_strlcpy);
2585 g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
2586 g_test_add_func ("/strfuncs/strndup", test_strndup);
2587 g_test_add_func ("/strfuncs/strnfill", test_strnfill);
2588 g_test_add_func ("/strfuncs/strreverse", test_strreverse);
2589 g_test_add_func ("/strfuncs/strsignal", test_strsignal);
2590 g_test_add_func ("/strfuncs/strsplit", test_strsplit);
2591 g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
2592 g_test_add_func ("/strfuncs/strstr", test_strstr);
2593 g_test_add_func ("/strfuncs/strtod", test_strtod);
2594 g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
2595 g_test_add_func ("/strfuncs/strup", test_strup);
2596 g_test_add_func ("/strfuncs/strv-contains", test_strv_contains);
2597 g_test_add_func ("/strfuncs/strv-equal", test_strv_equal);
2598 g_test_add_func ("/strfuncs/strv-length", test_strv_length);
2599 g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
2600 g_test_add_func ("/strfuncs/transliteration", test_transliteration);
2602 return g_test_run();