Bug 539770 - migrate gstrfunc unit tests to gtest
[platform/upstream/glib.git] / glib / tests / strfuncs.c
1 /* Unit tests for gstrfuncs
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
7  *
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.
11  *
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.
20  */
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <math.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "glib.h"
31
32 #define GLIB_TEST_STRING "el dorado "
33
34 #define FOR_ALL_CTYPE(macro)    \
35         macro(isalnum)          \
36         macro(isalpha)          \
37         macro(iscntrl)          \
38         macro(isdigit)          \
39         macro(isgraph)          \
40         macro(islower)          \
41         macro(isprint)          \
42         macro(ispunct)          \
43         macro(isspace)          \
44         macro(isupper)          \
45         macro(isxdigit)
46
47 #define DEFINE_CALL_CTYPE(function)             \
48         static int                              \
49         call_##function (int c)                 \
50         {                                       \
51                 return function (c);            \
52         }
53
54 #define DEFINE_CALL_G_ASCII_CTYPE(function)     \
55         static gboolean                         \
56         call_g_ascii_##function (gchar c)       \
57         {                                       \
58                 return g_ascii_##function (c);  \
59         }
60
61 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
62 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
63
64 static void
65 test_is_function (const char *name,
66                   gboolean (* ascii_function) (gchar),
67                   int (* c_library_function) (int),
68                   gboolean (* unicode_function) (gunichar))
69 {
70   int c;
71
72   for (c = 0; c <= 0x7F; c++)
73     {
74       gboolean ascii_result = ascii_function ((gchar)c);
75       gboolean c_library_result = c_library_function (c) != 0;
76       gboolean unicode_result = unicode_function ((gunichar) c);
77       if (ascii_result != c_library_result && c != '\v')
78         {
79           g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
80                    name, ascii_result, name, c_library_result, c);
81         }
82       if (ascii_result != unicode_result)
83         {
84           g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
85                    name, ascii_result, name, unicode_result, c);
86         }
87     }
88   for (c = 0x80; c <= 0xFF; c++)
89     {
90       gboolean ascii_result = ascii_function ((gchar)c);
91       if (ascii_result)
92         {
93           g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
94         }
95     }
96 }
97
98 static void
99 test_to_function (const char *name,
100                   gchar (* ascii_function) (gchar),
101                   int (* c_library_function) (int),
102                   gunichar (* unicode_function) (gunichar))
103 {
104   int c;
105
106   for (c = 0; c <= 0x7F; c++)
107     {
108       int ascii_result = (guchar) ascii_function ((gchar) c);
109       int c_library_result = c_library_function (c);
110       int unicode_result = unicode_function ((gunichar) c);
111       if (ascii_result != c_library_result)
112         {
113           g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
114                    name, ascii_result, name, c_library_result, c);
115         }
116       if (ascii_result != unicode_result)
117         {
118           g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
119                    name, ascii_result, name, unicode_result, c);
120         }
121     }
122   for (c = 0x80; c <= 0xFF; c++)
123     {
124       int ascii_result = (guchar) ascii_function ((gchar) c);
125       if (ascii_result != c)
126         {
127           g_error ("g_ascii_%s returned 0x%X for 0x%X",
128                    name, ascii_result, c);
129         }
130     }
131 }
132
133 static void
134 test_digit_function (const char *name,
135                      int (* ascii_function) (gchar),
136                      int (* unicode_function) (gunichar))
137 {
138   int c;
139
140   for (c = 0; c <= 0x7F; c++)
141     {
142       int ascii_result = ascii_function ((gchar) c);
143       int unicode_result = unicode_function ((gunichar) c);
144       if (ascii_result != unicode_result)
145         {
146           g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
147                    name, ascii_result, name, unicode_result, c);
148         }
149     }
150   for (c = 0x80; c <= 0xFF; c++)
151     {
152       int ascii_result = ascii_function ((gchar) c);
153       if (ascii_result != -1)
154         {
155           g_error ("g_ascii_%s_value returned %d for 0x%X",
156                    name, ascii_result, c);
157         }
158     }
159 }
160
161 static void
162 test_is_to_digit (void)
163 {
164   #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
165
166   FOR_ALL_CTYPE(TEST_IS)
167
168   #undef TEST_IS
169
170   #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
171
172   TEST_TO (tolower);
173   TEST_TO (toupper);
174
175   #undef TEST_TO
176
177   #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
178
179   TEST_DIGIT (digit);
180   TEST_DIGIT (xdigit);
181
182   #undef TEST_DIGIT
183 }
184
185 static void
186 test_strdup (void)
187 {
188   gchar *str;
189
190   str = g_strdup (NULL);
191   g_assert (str == NULL);
192
193   str = g_strdup (GLIB_TEST_STRING);
194   g_assert (str != NULL);
195   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
196   g_free (str);
197 }
198
199 static void
200 test_strndup (void)
201 {
202   gchar *str;
203
204   str = g_strndup (NULL, 3);
205   g_assert (str == NULL);
206
207   str = g_strndup ("aaaa", 5);
208   g_assert (str != NULL);
209   g_assert_cmpstr (str, ==, "aaaa");
210   g_free (str);
211
212   str = g_strndup ("aaaa", 2);
213   g_assert (str != NULL);
214   g_assert_cmpstr (str, ==, "aa");
215   g_free (str);
216 }
217
218 static void
219 test_strdup_printf (void)
220 {
221   gchar *str;
222
223   str = g_strdup_printf ("%05d %-5s", 21, "test");
224   g_assert (str != NULL);
225   g_assert_cmpstr (str, ==, "00021 test ");
226   g_free (str);
227 }
228
229 static void
230 test_strdupv (void)
231 {
232   gchar *vec[] = { "Foo", "Bar", NULL };
233   gchar **copy;
234
235   copy = g_strdupv (NULL);
236   g_assert (copy == NULL);  
237
238   copy = g_strdupv (vec);
239   g_assert (copy != NULL);
240   g_assert_cmpstr (copy[0], ==, "Foo");
241   g_assert_cmpstr (copy[1], ==, "Bar");
242   g_assert (copy[2] == NULL);
243   g_strfreev (copy);
244 }
245
246 static void
247 test_strnfill (void)
248 {
249   gchar *str;
250
251   str = g_strnfill (0, 'a');
252   g_assert (str != NULL);
253   g_assert (*str == '\0');
254   g_free (str);
255
256   str = g_strnfill (5, 'a');
257   g_assert (str != NULL);
258   g_assert_cmpstr (str, ==, "aaaaa");
259   g_free (str);
260 }
261
262 static void
263 test_strconcat (void)
264 {
265   gchar *str;
266
267   str = g_strconcat (GLIB_TEST_STRING, NULL);
268   g_assert (str != NULL);
269   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
270   g_free (str);
271
272   str = g_strconcat (GLIB_TEST_STRING,
273                      GLIB_TEST_STRING, 
274                      GLIB_TEST_STRING,
275                      NULL);
276   g_assert (str != NULL);
277   g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
278   g_free (str);
279 }
280
281 static void
282 test_strjoin (void)
283 {
284   gchar *str;
285
286   str = g_strjoin (NULL, NULL);
287   g_assert (str != NULL);
288   g_assert (*str == '\0');
289   g_free (str);
290
291   str = g_strjoin (":", NULL);
292   g_assert (str != NULL);
293   g_assert (*str == '\0');
294   g_free (str);
295
296   str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
297   g_assert (str != NULL);
298   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
299   g_free (str);
300
301   str = g_strjoin (NULL,
302                    GLIB_TEST_STRING,
303                    GLIB_TEST_STRING, 
304                    GLIB_TEST_STRING,
305                    NULL);
306   g_assert (str != NULL);
307   g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
308   g_free (str);
309
310   str = g_strjoin (":",
311                    GLIB_TEST_STRING,
312                    GLIB_TEST_STRING, 
313                    GLIB_TEST_STRING,
314                    NULL);
315   g_assert (str != NULL);
316   g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
317   g_free (str);
318 }
319
320 static void
321 test_strcanon (void)
322 {
323   gchar *str;
324
325   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
326     {
327       str = g_strcanon (NULL, "ab", 'y');
328     }
329   g_test_trap_assert_failed ();
330
331   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
332     {
333       str = g_strdup ("abxabxab");
334       str = g_strcanon (str, NULL, 'y');
335       g_free (str);
336     }
337   g_test_trap_assert_failed ();
338
339   str = g_strdup ("abxabxab");
340   str = g_strcanon (str, "ab", 'y');
341   g_assert (str != NULL);
342   g_assert_cmpstr (str, ==, "abyabyab");
343   g_free (str);
344 }
345
346 static void
347 test_strcompress_strescape (void)
348 {
349   gchar *str;
350   gchar *tmp;
351
352   /* test compress */
353   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
354     {
355       str = g_strcompress (NULL);
356     }
357   g_test_trap_assert_failed ();
358
359   /* trailing slashes are not allowed */
360   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
361     {
362       str = g_strcompress ("abc\\");
363     }
364   g_test_trap_assert_failed ();
365
366   str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");
367   g_assert (str != NULL);
368   g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");
369   g_free (str);
370
371   /* test escape */
372   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
373     {
374       str = g_strescape (NULL, NULL);
375     }
376   g_test_trap_assert_failed ();
377
378   str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
379   g_assert (str != NULL);
380   g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");
381   g_free (str);
382
383   str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",
384                      "\b\f\001\002\003\004");
385   g_assert (str != NULL);
386   g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");
387   g_free (str);
388
389   /* round trip */
390   tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);
391   str = g_strcompress (tmp);
392   g_assert (str != NULL); 
393   g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");
394   g_free (str);
395   g_free (tmp);
396 }
397
398 static void
399 test_ascii_strcasecmp (void)
400 {
401   gboolean res;
402
403   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
404     {
405       res = g_ascii_strcasecmp ("foo", NULL);
406     }
407   g_test_trap_assert_failed ();
408
409   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
410     {
411       res = g_ascii_strcasecmp (NULL, "foo");
412     }
413   g_test_trap_assert_failed ();
414
415   res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
416   g_assert_cmpint (res, ==, 0);
417
418   res = g_ascii_strcasecmp ("frobozz", "frobozz");
419   g_assert_cmpint (res, ==, 0);
420
421   res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
422   g_assert_cmpint (res, ==, 0);
423
424   res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
425   g_assert_cmpint (res, !=, 0);
426
427   res = g_ascii_strcasecmp ("", "");
428   g_assert_cmpint (res, ==, 0);
429
430   res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
431   g_assert_cmpint (res, ==, 0);
432
433   res = g_ascii_strcasecmp ("a", "b");
434   g_assert_cmpint (res, <, 0);
435
436   res = g_ascii_strcasecmp ("a", "B");
437   g_assert_cmpint (res, <, 0);
438
439   res = g_ascii_strcasecmp ("A", "b");
440   g_assert_cmpint (res, <, 0);
441
442   res = g_ascii_strcasecmp ("A", "B");
443   g_assert_cmpint (res, <, 0);
444
445   res = g_ascii_strcasecmp ("b", "a");
446   g_assert_cmpint (res, >, 0);
447
448   res = g_ascii_strcasecmp ("b", "A");
449   g_assert_cmpint (res, >, 0);
450
451   res = g_ascii_strcasecmp ("B", "a");
452   g_assert_cmpint (res, >, 0);
453
454   res = g_ascii_strcasecmp ("B", "A");
455   g_assert_cmpint (res, >, 0);
456 }
457
458 static void
459 do_test_strchug (const gchar *str, const gchar *expected)
460 {
461   gchar *tmp;
462   gboolean res;
463
464   tmp = g_strdup (str);
465
466   g_strchug (tmp);
467   res = (strcmp (tmp, expected) == 0);
468   g_free (tmp);
469
470   g_assert_cmpint (res, ==, TRUE);
471 }
472
473 static void
474 test_strchug (void)
475 {
476   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
477     {
478       g_strchug (NULL);
479     }
480   g_test_trap_assert_failed ();
481
482   do_test_strchug ("", "");
483   do_test_strchug (" ", "");
484   do_test_strchug ("\t\r\n ", "");
485   do_test_strchug (" a", "a");
486   do_test_strchug ("  a", "a");
487   do_test_strchug ("a a", "a a");
488   do_test_strchug (" a a", "a a");
489 }
490
491 static void
492 do_test_strchomp (const gchar *str, const gchar *expected)
493 {
494   gchar *tmp;
495   gboolean res;
496
497   tmp = g_strdup (str);
498
499   g_strchomp (tmp);
500   res = (strcmp (tmp, expected) == 0);
501   g_free (tmp);
502
503   g_assert_cmpint (res, ==, TRUE);
504 }
505
506 static void
507 test_strchomp (void)
508 {
509   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
510     {
511       g_strchomp (NULL);
512     }
513   g_test_trap_assert_failed ();
514
515   do_test_strchomp ("", "");
516   do_test_strchomp (" ", "");
517   do_test_strchomp (" \t\r\n", "");
518   do_test_strchomp ("a ", "a");
519   do_test_strchomp ("a  ", "a");
520   do_test_strchomp ("a a", "a a");
521   do_test_strchomp ("a a ", "a a");
522 }
523
524 static void
525 test_strreverse (void)
526 {
527   gchar *str;
528   gchar *p;
529
530   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
531     {
532       str = g_strreverse (NULL);
533     }
534   g_test_trap_assert_failed ();
535
536   str = p = g_strdup ("abcde");
537   str = g_strreverse (str);
538   g_assert (str != NULL);
539   g_assert (p == str);
540   g_assert_cmpstr (str, ==, "edcba");
541   g_free (str);
542 }
543
544 static void
545 test_strstr (void)
546 {
547   gchar *haystack;
548   gchar *res;
549
550   haystack = g_strdup ("FooBarFooBarFoo");
551
552   /* strstr_len */
553   res = g_strstr_len (haystack, 6, "xxx");
554   g_assert (res == NULL);
555
556   res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
557   g_assert (res == NULL);
558
559   res = g_strstr_len (haystack, 6, "");
560   g_assert (res == haystack);
561
562   res = g_strstr_len (haystack, 6, "Bar");
563   g_assert_cmpstr (res, ==, "BarFooBarFoo");
564
565   /* strrstr */
566   res = g_strrstr (haystack, "xxx");
567   g_assert (res == NULL);
568
569   res = g_strrstr (haystack, "FooBarFooBarFooBar");
570   g_assert (res == NULL);
571
572   res = g_strrstr (haystack, "");
573   g_assert (res == haystack);
574
575   res = g_strrstr (haystack, "Bar");
576   g_assert_cmpstr (res, ==, "BarFoo");
577
578   /* strrstr_len */
579   res = g_strrstr_len (haystack, 14, "xxx");
580   g_assert (res == NULL);
581
582   res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
583   g_assert (res == NULL);
584
585   res = g_strrstr_len (haystack, 14, "BarFoo");
586   g_assert_cmpstr (res, ==, "BarFooBarFoo");
587
588   g_free (haystack);
589 }
590
591 static void
592 test_has_prefix (void)
593 {
594   gboolean res;
595
596   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
597     {
598       res = g_str_has_prefix ("foo", NULL);
599     }
600   g_test_trap_assert_failed ();
601
602   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
603     {
604       res = g_str_has_prefix (NULL, "foo");
605     }
606   g_test_trap_assert_failed ();
607
608   res = g_str_has_prefix ("foo", "bar");
609   g_assert_cmpint (res, ==, FALSE);
610
611   res = g_str_has_prefix ("foo", "foobar");
612   g_assert_cmpint (res, ==, FALSE);
613
614   res = g_str_has_prefix ("foobar", "bar");
615   g_assert_cmpint (res, ==, FALSE);
616
617   res = g_str_has_prefix ("foobar", "foo");
618   g_assert_cmpint (res, ==, TRUE);
619
620   res = g_str_has_prefix ("foo", "");
621   g_assert_cmpint (res, ==, TRUE);
622
623   res = g_str_has_prefix ("foo", "foo");
624   g_assert_cmpint (res, ==, TRUE);
625
626   res = g_str_has_prefix ("", "");
627   g_assert_cmpint (res, ==, TRUE);
628 }
629
630 static void
631 test_has_suffix (void)
632 {
633   gboolean res;
634
635   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
636     {
637       res = g_str_has_suffix ("foo", NULL);
638     }
639   g_test_trap_assert_failed ();
640
641   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
642     {
643       res = g_str_has_suffix (NULL, "foo");
644     }
645   g_test_trap_assert_failed ();
646
647   res = g_str_has_suffix ("foo", "bar");
648   g_assert_cmpint (res, ==, FALSE);
649
650   res = g_str_has_suffix ("bar", "foobar");
651   g_assert_cmpint (res, ==, FALSE);
652
653   res = g_str_has_suffix ("foobar", "foo");
654   g_assert_cmpint (res, ==, FALSE);
655
656   res = g_str_has_suffix ("foobar", "bar");
657   g_assert_cmpint (res, ==, TRUE);
658
659   res = g_str_has_suffix ("foo", "");
660   g_assert_cmpint (res, ==, TRUE);
661
662   res = g_str_has_suffix ("foo", "foo");
663   g_assert_cmpint (res, ==, TRUE);
664
665   res = g_str_has_suffix ("", "");
666   g_assert_cmpint (res, ==, TRUE);
667 }
668
669 static void
670 strv_check (gchar **strv, ...)
671 {
672   gboolean ok = TRUE;
673   gint i = 0;
674   va_list list;
675
676   va_start (list, strv);
677   while (ok)
678     {
679       const gchar *str = va_arg (list, const char *);
680       if (strv[i] == NULL)
681         {
682           g_assert (str == NULL);
683           break;
684         }
685       if (str == NULL)
686         {
687           ok = FALSE;
688         }
689       else
690         {
691           g_assert_cmpstr (strv[i], ==, str);
692         }
693       i++;
694     }
695   va_end (list);
696
697   g_strfreev (strv);
698 }
699
700 static void
701 test_strsplit (void)
702 {
703   strv_check (g_strsplit ("", ",", 0), NULL);
704   strv_check (g_strsplit ("x", ",", 0), "x", NULL);
705   strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
706   strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
707   strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
708   strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
709   strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
710   strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
711   strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
712   strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
713   strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
714   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
715
716   strv_check (g_strsplit ("", ",", 1), NULL);
717   strv_check (g_strsplit ("x", ",", 1), "x", NULL);
718   strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
719   strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
720   strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
721   strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
722   strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
723   strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
724   strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
725   strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
726   strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
727   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
728
729   strv_check (g_strsplit ("", ",", 2), NULL);
730   strv_check (g_strsplit ("x", ",", 2), "x", NULL);
731   strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
732   strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
733   strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
734   strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
735   strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
736   strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
737   strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
738   strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
739   strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
740   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
741 }
742
743 static void
744 test_strsplit_set (void)
745 {
746   strv_check (g_strsplit_set ("", ",/", 0), NULL);
747   strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
748   strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
749   strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
750   strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
751
752   strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
753   strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
754   strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
755   strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
756   strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
757   strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
758   strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
759   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
760
761   strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
762   strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
763   strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
764   strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
765   strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
766   strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
767    
768   strv_check (g_strsplit_set ("", ",", 0), NULL);
769   strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
770   strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
771   strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
772   strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
773   strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
774   strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
775   strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
776   strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
777   strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
778   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
779
780   strv_check (g_strsplit_set ("", ",", 1), NULL);
781   strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
782   strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
783   strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
784   strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
785   strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
786   strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
787   strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
788   strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
789   strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
790   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
791   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
792
793   strv_check (g_strsplit_set ("", ",", 2), NULL);
794   strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
795   strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
796   strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
797   strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
798   strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
799   strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
800   strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
801   strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
802   strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
803   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
804   
805   strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
806 }
807
808 static void
809 test_strv_length (void)
810 {
811   guint l;
812
813   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
814     {
815       l = g_strv_length (NULL);
816     }
817   g_test_trap_assert_failed ();
818
819   l = g_strv_length (g_strsplit ("1,2,3,4", ",", -1));
820   g_assert_cmpuint (l, ==, 4);
821 }
822
823 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
824
825 void
826 check_strtod_string (gchar    *number,
827                      double    res,
828                      gboolean  check_end,
829                      gint      correct_len)
830 {
831   double d;
832   gint l;
833   gchar *dummy;
834
835   /* we try a copy of number, with some free space for malloc before that. 
836    * This is supposed to smash the some wrong pointer calculations. */
837
838   dummy = g_malloc (100000);
839   number = g_strdup (number);
840   g_free (dummy);
841
842   for (l = 0; l < G_N_ELEMENTS (locales); l++)
843     {
844       gboolean ok;
845       gchar *end = "(unset)";
846
847       setlocale (LC_NUMERIC, locales[l]);
848       d = g_ascii_strtod (number, &end);
849       ok = isnan (res) ? isnan (d) : (d == res);
850       if (!ok)
851         {
852           g_error ("g_ascii_strtod on \"%s\" for locale %s failed\n" \
853                    "expected %f (nan %d) actual %f (nan %d)\n", 
854                    number, locales[l],
855                    res, isnan (res),
856                    d, isnan (d));
857         }
858
859       ok = (end - number) == (check_end ? correct_len : strlen (number));
860       if (!ok) {
861         if (end == NULL)
862           g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
863                    number, locales[l]);
864         else if (end >= number && end <= number + strlen (number))
865           g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
866                    number, locales[l], end);
867         else
868           g_error ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
869                    number, locales[l], number, end);
870       }
871     }
872
873   g_free (number);
874 }
875
876 static void
877 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
878 {
879   int l;
880   gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
881
882   for (l = 0; l < G_N_ELEMENTS (locales); l++)
883     {
884       g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
885       g_assert_cmpstr (buf, ==, str);
886     }
887 }
888
889 static void
890 test_strtod (void)
891 {
892   gdouble d, our_nan, our_inf;
893   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
894
895 #ifdef NAN
896   our_nan = NAN;
897 #else
898   /* Do this before any call to setlocale.  */
899   our_nan = atof ("NaN");
900 #endif
901   g_assert (isnan (our_nan));
902
903 #ifdef INFINITY
904   our_inf = INFINITY;
905 #else
906   our_inf = atof ("Infinity");
907 #endif
908   g_assert (our_inf > 1 && our_inf == our_inf / 2);
909
910   check_strtod_string ("123.123", 123.123, FALSE, 0);
911   check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
912   check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
913   check_strtod_string ("-123.123", -123.123, FALSE, 0);
914   check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
915   check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
916   check_strtod_string ("5.4", 5.4, TRUE, 3);
917   check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
918   check_strtod_string ("5,4", 5.0, TRUE, 1);
919   /* the following are for #156421 */
920   check_strtod_string ("1e1", 1e1, FALSE, 0); 
921   check_strtod_string ("NAN", our_nan, FALSE, 0);
922   check_strtod_string ("-nan", -our_nan, FALSE, 0);
923   check_strtod_string ("INF", our_inf, FALSE, 0);
924   check_strtod_string ("-infinity", -our_inf, FALSE, 0);
925   check_strtod_string ("-.75,0", -0.75, TRUE, 4);
926   
927   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
928   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
929
930   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
931   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
932   
933   d = pow (2.0, -1024.1);
934   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
935   
936   d = -pow (2.0, -1024.1);
937   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
938
939   /* for #343899 */
940   check_strtod_string (" 0.75", 0.75, FALSE, 0);
941   check_strtod_string (" +0.75", 0.75, FALSE, 0);
942   check_strtod_string (" -0.75", -0.75, FALSE, 0);
943   check_strtod_string ("\f0.75", 0.75, FALSE, 0);
944   check_strtod_string ("\n0.75", 0.75, FALSE, 0);
945   check_strtod_string ("\r0.75", 0.75, FALSE, 0);
946   check_strtod_string ("\t0.75", 0.75, FALSE, 0);
947
948 #if 0
949   /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
950   check_strtod_string ("\v0.75", 0.75, FALSE, 0);
951 #endif
952
953   /* for #343899 */
954   check_strtod_number (0.75, "%0.2f", "0.75");
955   check_strtod_number (0.75, "%5.2f", " 0.75");
956   check_strtod_number (-0.75, "%0.2f", "-0.75");
957   check_strtod_number (-0.75, "%5.2f", "-0.75");
958   check_strtod_number (1e99, "%.0e", "1e+99");
959 }
960
961 static void
962 check_uint64 (const gchar *str,
963               const gchar *end,
964               gint         base,
965               guint64      result,
966               gint         error)
967 {
968   guint64 actual;
969   gchar *endptr = NULL;
970   gint err;
971
972   errno = 0;
973   actual = g_ascii_strtoull (str, &endptr, base);
974   err = errno;
975
976   g_assert (actual == result);
977   g_assert_cmpstr (end, ==, endptr);
978   g_assert (err == error);
979 }
980
981 static void
982 check_int64 (const gchar *str,
983              const gchar *end,
984              gint         base,
985              gint64       result,
986              gint         error)
987 {
988   gint64 actual;
989   gchar *endptr = NULL;
990   gint err;
991
992   errno = 0;
993   actual = g_ascii_strtoll (str, &endptr, base);
994   err = errno;
995
996   g_assert (actual == result);
997   g_assert_cmpstr (end, ==, endptr);
998   g_assert (err == error);
999 }
1000
1001 static void
1002 test_strtoll (void)
1003 {
1004   check_uint64 ("0", "", 10, 0, 0);
1005   check_uint64 ("+0", "", 10, 0, 0);
1006   check_uint64 ("-0", "", 10, 0, 0);
1007   check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1008   check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1009   check_uint64 ("20xyz", "xyz", 10, 20, 0);
1010   check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1011
1012   check_int64 ("0", "", 10, 0, 0);
1013   check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1014   check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1015   check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1016   check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1017   check_int64 ("32768", "", 10, 32768, 0);
1018   check_int64 ("-32768", "", 10, -32768, 0);
1019   check_int64 ("001", "", 10, 1, 0);
1020   check_int64 ("-001", "", 10, -1, 0);
1021 }
1022
1023 int
1024 main (int   argc,
1025       char *argv[])
1026 {
1027   g_test_init (&argc, &argv, NULL);
1028
1029   g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1030   g_test_add_func ("/strfuncs/strdup", test_strdup);
1031   g_test_add_func ("/strfuncs/strndup", test_strndup);
1032   g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1033   g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1034   g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1035   g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1036   g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1037   g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1038   g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1039   g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1040   g_test_add_func ("/strfuncs/strchug", test_strchug);
1041   g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1042   g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1043   g_test_add_func ("/strfuncs/strstr", test_strstr);
1044   g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1045   g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1046   g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1047   g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1048   g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1049   g_test_add_func ("/strfuncs/strtod", test_strtod);
1050   g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1051
1052   return g_test_run();
1053 }