8e376f7401b0c4fcc0f71e389a32b92af52dee86
[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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
23
24 #define _XOPEN_SOURCE 600
25 #include <ctype.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <math.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "glib.h"
34
35 #define GLIB_TEST_STRING "el dorado "
36
37 #define FOR_ALL_CTYPE(macro)    \
38         macro(isalnum)          \
39         macro(isalpha)          \
40         macro(iscntrl)          \
41         macro(isdigit)          \
42         macro(isgraph)          \
43         macro(islower)          \
44         macro(isprint)          \
45         macro(ispunct)          \
46         macro(isspace)          \
47         macro(isupper)          \
48         macro(isxdigit)
49
50 #define DEFINE_CALL_CTYPE(function)             \
51         static int                              \
52         call_##function (int c)                 \
53         {                                       \
54                 return function (c);            \
55         }
56
57 #define DEFINE_CALL_G_ASCII_CTYPE(function)     \
58         static gboolean                         \
59         call_g_ascii_##function (gchar c)       \
60         {                                       \
61                 return g_ascii_##function (c);  \
62         }
63
64 FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
65 FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
66
67 static void
68 test_is_function (const char *name,
69                   gboolean (* ascii_function) (gchar),
70                   int (* c_library_function) (int),
71                   gboolean (* unicode_function) (gunichar))
72 {
73   int c;
74
75   for (c = 0; c <= 0x7F; c++)
76     {
77       gboolean ascii_result = ascii_function ((gchar)c);
78       gboolean c_library_result = c_library_function (c) != 0;
79       gboolean unicode_result = unicode_function ((gunichar) c);
80       if (ascii_result != c_library_result && c != '\v')
81         {
82           g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
83                    name, ascii_result, name, c_library_result, c);
84         }
85       if (ascii_result != unicode_result)
86         {
87           g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
88                    name, ascii_result, name, unicode_result, c);
89         }
90     }
91   for (c = 0x80; c <= 0xFF; c++)
92     {
93       gboolean ascii_result = ascii_function ((gchar)c);
94       if (ascii_result)
95         {
96           g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
97         }
98     }
99 }
100
101 static void
102 test_to_function (const char *name,
103                   gchar (* ascii_function) (gchar),
104                   int (* c_library_function) (int),
105                   gunichar (* unicode_function) (gunichar))
106 {
107   int c;
108
109   for (c = 0; c <= 0x7F; c++)
110     {
111       int ascii_result = (guchar) ascii_function ((gchar) c);
112       int c_library_result = c_library_function (c);
113       int unicode_result = unicode_function ((gunichar) c);
114       if (ascii_result != c_library_result)
115         {
116           g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
117                    name, ascii_result, name, c_library_result, c);
118         }
119       if (ascii_result != unicode_result)
120         {
121           g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
122                    name, ascii_result, name, unicode_result, c);
123         }
124     }
125   for (c = 0x80; c <= 0xFF; c++)
126     {
127       int ascii_result = (guchar) ascii_function ((gchar) c);
128       if (ascii_result != c)
129         {
130           g_error ("g_ascii_%s returned 0x%X for 0x%X",
131                    name, ascii_result, c);
132         }
133     }
134 }
135
136 static void
137 test_digit_function (const char *name,
138                      int (* ascii_function) (gchar),
139                      int (* unicode_function) (gunichar))
140 {
141   int c;
142
143   for (c = 0; c <= 0x7F; c++)
144     {
145       int ascii_result = ascii_function ((gchar) c);
146       int unicode_result = unicode_function ((gunichar) c);
147       if (ascii_result != unicode_result)
148         {
149           g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
150                    name, ascii_result, name, unicode_result, c);
151         }
152     }
153   for (c = 0x80; c <= 0xFF; c++)
154     {
155       int ascii_result = ascii_function ((gchar) c);
156       if (ascii_result != -1)
157         {
158           g_error ("g_ascii_%s_value returned %d for 0x%X",
159                    name, ascii_result, c);
160         }
161     }
162 }
163
164 static void
165 test_is_to_digit (void)
166 {
167   #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
168
169   FOR_ALL_CTYPE(TEST_IS)
170
171   #undef TEST_IS
172
173   #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
174
175   TEST_TO (tolower);
176   TEST_TO (toupper);
177
178   #undef TEST_TO
179
180   #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
181
182   TEST_DIGIT (digit);
183   TEST_DIGIT (xdigit);
184
185   #undef TEST_DIGIT
186 }
187
188 static void
189 test_strdup (void)
190 {
191   gchar *str;
192
193   str = g_strdup (NULL);
194   g_assert (str == NULL);
195
196   str = g_strdup (GLIB_TEST_STRING);
197   g_assert (str != NULL);
198   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
199   g_free (str);
200 }
201
202 static void
203 test_strndup (void)
204 {
205   gchar *str;
206
207   str = g_strndup (NULL, 3);
208   g_assert (str == NULL);
209
210   str = g_strndup ("aaaa", 5);
211   g_assert (str != NULL);
212   g_assert_cmpstr (str, ==, "aaaa");
213   g_free (str);
214
215   str = g_strndup ("aaaa", 2);
216   g_assert (str != NULL);
217   g_assert_cmpstr (str, ==, "aa");
218   g_free (str);
219 }
220
221 static void
222 test_strdup_printf (void)
223 {
224   gchar *str;
225
226   str = g_strdup_printf ("%05d %-5s", 21, "test");
227   g_assert (str != NULL);
228   g_assert_cmpstr (str, ==, "00021 test ");
229   g_free (str);
230 }
231
232 static void
233 test_strdupv (void)
234 {
235   gchar *vec[] = { "Foo", "Bar", NULL };
236   gchar **copy;
237
238   copy = g_strdupv (NULL);
239   g_assert (copy == NULL);  
240
241   copy = g_strdupv (vec);
242   g_assert (copy != NULL);
243   g_assert_cmpstr (copy[0], ==, "Foo");
244   g_assert_cmpstr (copy[1], ==, "Bar");
245   g_assert (copy[2] == NULL);
246   g_strfreev (copy);
247 }
248
249 static void
250 test_strnfill (void)
251 {
252   gchar *str;
253
254   str = g_strnfill (0, 'a');
255   g_assert (str != NULL);
256   g_assert (*str == '\0');
257   g_free (str);
258
259   str = g_strnfill (5, 'a');
260   g_assert (str != NULL);
261   g_assert_cmpstr (str, ==, "aaaaa");
262   g_free (str);
263 }
264
265 static void
266 test_strconcat (void)
267 {
268   gchar *str;
269
270   str = g_strconcat (GLIB_TEST_STRING, NULL);
271   g_assert (str != NULL);
272   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
273   g_free (str);
274
275   str = g_strconcat (GLIB_TEST_STRING,
276                      GLIB_TEST_STRING, 
277                      GLIB_TEST_STRING,
278                      NULL);
279   g_assert (str != NULL);
280   g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
281   g_free (str);
282
283   g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
284 }
285
286 static void
287 test_strjoin (void)
288 {
289   gchar *str;
290
291   str = g_strjoin (NULL, NULL);
292   g_assert (str != NULL);
293   g_assert (*str == '\0');
294   g_free (str);
295
296   str = g_strjoin (":", NULL);
297   g_assert (str != NULL);
298   g_assert (*str == '\0');
299   g_free (str);
300
301   str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
302   g_assert (str != NULL);
303   g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
304   g_free (str);
305
306   str = g_strjoin (NULL,
307                    GLIB_TEST_STRING,
308                    GLIB_TEST_STRING, 
309                    GLIB_TEST_STRING,
310                    NULL);
311   g_assert (str != NULL);
312   g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
313   g_free (str);
314
315   str = g_strjoin (":",
316                    GLIB_TEST_STRING,
317                    GLIB_TEST_STRING, 
318                    GLIB_TEST_STRING,
319                    NULL);
320   g_assert (str != NULL);
321   g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
322   g_free (str);
323 }
324
325 static void
326 test_strcanon (void)
327 {
328   gchar *str;
329
330   if (g_test_undefined ())
331     {
332       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
333         {
334           str = g_strcanon (NULL, "ab", 'y');
335         }
336       g_test_trap_assert_failed ();
337
338       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
339         {
340           str = g_strdup ("abxabxab");
341           str = g_strcanon (str, NULL, 'y');
342           g_free (str);
343         }
344       g_test_trap_assert_failed ();
345     }
346
347   str = g_strdup ("abxabxab");
348   str = g_strcanon (str, "ab", 'y');
349   g_assert (str != NULL);
350   g_assert_cmpstr (str, ==, "abyabyab");
351   g_free (str);
352 }
353
354 static void
355 test_strcompress_strescape (void)
356 {
357   gchar *str;
358   gchar *tmp;
359
360   /* test compress */
361   if (g_test_undefined ())
362     {
363       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
364         {
365           str = g_strcompress (NULL);
366         }
367       g_test_trap_assert_failed ();
368
369       /* trailing slashes are not allowed */
370       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
371         {
372           str = g_strcompress ("abc\\");
373         }
374       g_test_trap_assert_failed ();
375     }
376
377   str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
378   g_assert (str != NULL);
379   g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
380   g_free (str);
381
382   /* test escape */
383   if (g_test_undefined ())
384     {
385       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
386         {
387           str = g_strescape (NULL, NULL);
388         }
389       g_test_trap_assert_failed ();
390     }
391
392   str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
393   g_assert (str != NULL);
394   g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
395   g_free (str);
396
397   str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
398                      "\b\f\001\002\003\004");
399   g_assert (str != NULL);
400   g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
401   g_free (str);
402
403   /* round trip */
404   tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
405   str = g_strcompress (tmp);
406   g_assert (str != NULL); 
407   g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
408   g_free (str);
409   g_free (tmp);
410 }
411
412 static void
413 test_ascii_strcasecmp (void)
414 {
415   gboolean res;
416
417   if (g_test_undefined ())
418     {
419       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
420         {
421           res = g_ascii_strcasecmp ("foo", NULL);
422         }
423       g_test_trap_assert_failed ();
424
425       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
426         {
427           res = g_ascii_strcasecmp (NULL, "foo");
428         }
429       g_test_trap_assert_failed ();
430     }
431
432   res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
433   g_assert_cmpint (res, ==, 0);
434
435   res = g_ascii_strcasecmp ("frobozz", "frobozz");
436   g_assert_cmpint (res, ==, 0);
437
438   res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
439   g_assert_cmpint (res, ==, 0);
440
441   res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
442   g_assert_cmpint (res, !=, 0);
443
444   res = g_ascii_strcasecmp ("", "");
445   g_assert_cmpint (res, ==, 0);
446
447   res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
448   g_assert_cmpint (res, ==, 0);
449
450   res = g_ascii_strcasecmp ("a", "b");
451   g_assert_cmpint (res, <, 0);
452
453   res = g_ascii_strcasecmp ("a", "B");
454   g_assert_cmpint (res, <, 0);
455
456   res = g_ascii_strcasecmp ("A", "b");
457   g_assert_cmpint (res, <, 0);
458
459   res = g_ascii_strcasecmp ("A", "B");
460   g_assert_cmpint (res, <, 0);
461
462   res = g_ascii_strcasecmp ("b", "a");
463   g_assert_cmpint (res, >, 0);
464
465   res = g_ascii_strcasecmp ("b", "A");
466   g_assert_cmpint (res, >, 0);
467
468   res = g_ascii_strcasecmp ("B", "a");
469   g_assert_cmpint (res, >, 0);
470
471   res = g_ascii_strcasecmp ("B", "A");
472   g_assert_cmpint (res, >, 0);
473 }
474
475 static void
476 do_test_strchug (const gchar *str, const gchar *expected)
477 {
478   gchar *tmp;
479   gboolean res;
480
481   tmp = g_strdup (str);
482
483   g_strchug (tmp);
484   res = (strcmp (tmp, expected) == 0);
485   g_free (tmp);
486
487   g_assert_cmpint (res, ==, TRUE);
488 }
489
490 static void
491 test_strchug (void)
492 {
493   if (g_test_undefined ())
494     {
495       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
496         {
497           g_strchug (NULL);
498         }
499       g_test_trap_assert_failed ();
500     }
501
502   do_test_strchug ("", "");
503   do_test_strchug (" ", "");
504   do_test_strchug ("\t\r\n ", "");
505   do_test_strchug (" a", "a");
506   do_test_strchug ("  a", "a");
507   do_test_strchug ("a a", "a a");
508   do_test_strchug (" a a", "a a");
509 }
510
511 static void
512 do_test_strchomp (const gchar *str, const gchar *expected)
513 {
514   gchar *tmp;
515   gboolean res;
516
517   tmp = g_strdup (str);
518
519   g_strchomp (tmp);
520   res = (strcmp (tmp, expected) == 0);
521   g_free (tmp);
522
523   g_assert_cmpint (res, ==, TRUE);
524 }
525
526 static void
527 test_strchomp (void)
528 {
529   if (g_test_undefined ())
530     {
531       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
532         {
533           g_strchomp (NULL);
534         }
535       g_test_trap_assert_failed ();
536     }
537
538   do_test_strchomp ("", "");
539   do_test_strchomp (" ", "");
540   do_test_strchomp (" \t\r\n", "");
541   do_test_strchomp ("a ", "a");
542   do_test_strchomp ("a  ", "a");
543   do_test_strchomp ("a a", "a a");
544   do_test_strchomp ("a a ", "a a");
545 }
546
547 static void
548 test_strreverse (void)
549 {
550   gchar *str;
551   gchar *p;
552
553   if (g_test_undefined ())
554     {
555       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
556         {
557           str = g_strreverse (NULL);
558         }
559       g_test_trap_assert_failed ();
560     }
561
562   str = p = g_strdup ("abcde");
563   str = g_strreverse (str);
564   g_assert (str != NULL);
565   g_assert (p == str);
566   g_assert_cmpstr (str, ==, "edcba");
567   g_free (str);
568 }
569
570 static void
571 test_strncasecmp (void)
572 {
573   g_assert (g_strncasecmp ("abc1", "ABC2", 3) == 0);
574   g_assert (g_strncasecmp ("abc1", "ABC2", 4) != 0);
575 }
576
577 static void
578 test_strstr (void)
579 {
580   gchar *haystack;
581   gchar *res;
582
583   haystack = g_strdup ("FooBarFooBarFoo");
584
585   /* strstr_len */
586   res = g_strstr_len (haystack, 6, "xxx");
587   g_assert (res == NULL);
588
589   res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
590   g_assert (res == NULL);
591
592   res = g_strstr_len (haystack, 3, "Bar");
593   g_assert (res == NULL);
594
595   res = g_strstr_len (haystack, 6, "");
596   g_assert (res == haystack);
597   g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
598
599   res = g_strstr_len (haystack, 6, "Bar");
600   g_assert (res == haystack + 3);
601   g_assert_cmpstr (res, ==, "BarFooBarFoo");
602
603   res = g_strstr_len (haystack, -1, "Bar");
604   g_assert (res == haystack + 3);
605   g_assert_cmpstr (res, ==, "BarFooBarFoo");
606
607   /* strrstr */
608   res = g_strrstr (haystack, "xxx");
609   g_assert (res == NULL);
610
611   res = g_strrstr (haystack, "FooBarFooBarFooBar");
612   g_assert (res == NULL);
613
614   res = g_strrstr (haystack, "");
615   g_assert (res == haystack);
616   g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
617
618   res = g_strrstr (haystack, "Bar");
619   g_assert (res == haystack + 9);
620   g_assert_cmpstr (res, ==, "BarFoo");
621
622   /* strrstr_len */
623   res = g_strrstr_len (haystack, 14, "xxx");
624   g_assert (res == NULL);
625
626   res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
627   g_assert (res == NULL);
628
629   res = g_strrstr_len (haystack, 3, "Bar");
630   g_assert (res == NULL);
631
632   res = g_strrstr_len (haystack, 14, "BarFoo");
633   g_assert (res == haystack + 3);
634   g_assert_cmpstr (res, ==, "BarFooBarFoo");
635
636   res = g_strrstr_len (haystack, 15, "BarFoo");
637   g_assert (res == haystack + 9);
638   g_assert_cmpstr (res, ==, "BarFoo");
639
640   res = g_strrstr_len (haystack, -1, "BarFoo");
641   g_assert (res == haystack + 9);
642   g_assert_cmpstr (res, ==, "BarFoo");
643
644   /* test case for strings with \0 in the middle */
645   *(haystack + 7) = '\0';
646   res = g_strstr_len (haystack, 15, "BarFoo");
647   g_assert (res == NULL);
648
649   g_free (haystack);
650 }
651
652 static void
653 test_has_prefix (void)
654 {
655   gboolean res;
656
657   if (g_test_undefined ())
658     {
659       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
660         {
661           res = g_str_has_prefix ("foo", NULL);
662         }
663       g_test_trap_assert_failed ();
664
665       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
666         {
667           res = g_str_has_prefix (NULL, "foo");
668         }
669       g_test_trap_assert_failed ();
670     }
671
672   res = g_str_has_prefix ("foo", "bar");
673   g_assert_cmpint (res, ==, FALSE);
674
675   res = g_str_has_prefix ("foo", "foobar");
676   g_assert_cmpint (res, ==, FALSE);
677
678   res = g_str_has_prefix ("foobar", "bar");
679   g_assert_cmpint (res, ==, FALSE);
680
681   res = g_str_has_prefix ("foobar", "foo");
682   g_assert_cmpint (res, ==, TRUE);
683
684   res = g_str_has_prefix ("foo", "");
685   g_assert_cmpint (res, ==, TRUE);
686
687   res = g_str_has_prefix ("foo", "foo");
688   g_assert_cmpint (res, ==, TRUE);
689
690   res = g_str_has_prefix ("", "");
691   g_assert_cmpint (res, ==, TRUE);
692 }
693
694 static void
695 test_has_suffix (void)
696 {
697   gboolean res;
698
699   if (g_test_undefined ())
700     {
701       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
702         {
703           res = g_str_has_suffix ("foo", NULL);
704         }
705       g_test_trap_assert_failed ();
706
707       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
708         {
709           res = g_str_has_suffix (NULL, "foo");
710         }
711       g_test_trap_assert_failed ();
712     }
713
714   res = g_str_has_suffix ("foo", "bar");
715   g_assert_cmpint (res, ==, FALSE);
716
717   res = g_str_has_suffix ("bar", "foobar");
718   g_assert_cmpint (res, ==, FALSE);
719
720   res = g_str_has_suffix ("foobar", "foo");
721   g_assert_cmpint (res, ==, FALSE);
722
723   res = g_str_has_suffix ("foobar", "bar");
724   g_assert_cmpint (res, ==, TRUE);
725
726   res = g_str_has_suffix ("foo", "");
727   g_assert_cmpint (res, ==, TRUE);
728
729   res = g_str_has_suffix ("foo", "foo");
730   g_assert_cmpint (res, ==, TRUE);
731
732   res = g_str_has_suffix ("", "");
733   g_assert_cmpint (res, ==, TRUE);
734 }
735
736 static void
737 strv_check (gchar **strv, ...)
738 {
739   gboolean ok = TRUE;
740   gint i = 0;
741   va_list list;
742
743   va_start (list, strv);
744   while (ok)
745     {
746       const gchar *str = va_arg (list, const char *);
747       if (strv[i] == NULL)
748         {
749           g_assert (str == NULL);
750           break;
751         }
752       if (str == NULL)
753         {
754           ok = FALSE;
755         }
756       else
757         {
758           g_assert_cmpstr (strv[i], ==, str);
759         }
760       i++;
761     }
762   va_end (list);
763
764   g_strfreev (strv);
765 }
766
767 static void
768 test_strsplit (void)
769 {
770   strv_check (g_strsplit ("", ",", 0), NULL);
771   strv_check (g_strsplit ("x", ",", 0), "x", NULL);
772   strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
773   strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
774   strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
775   strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
776   strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
777   strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
778   strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
779   strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
780   strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
781   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
782
783   strv_check (g_strsplit ("", ",", 1), NULL);
784   strv_check (g_strsplit ("x", ",", 1), "x", NULL);
785   strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
786   strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
787   strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
788   strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
789   strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
790   strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
791   strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
792   strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
793   strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
794   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
795
796   strv_check (g_strsplit ("", ",", 2), NULL);
797   strv_check (g_strsplit ("x", ",", 2), "x", NULL);
798   strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
799   strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
800   strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
801   strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
802   strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
803   strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
804   strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
805   strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
806   strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
807   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
808 }
809
810 static void
811 test_strsplit_set (void)
812 {
813   strv_check (g_strsplit_set ("", ",/", 0), NULL);
814   strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
815   strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
816   strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
817   strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
818
819   strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
820   strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
821   strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
822   strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
823   strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
824   strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
825   strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
826   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
827
828   strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
829   strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
830   strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
831   strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
832   strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
833   strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
834    
835   strv_check (g_strsplit_set ("", ",", 0), NULL);
836   strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
837   strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
838   strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
839   strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
840   strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
841   strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
842   strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
843   strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
844   strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
845   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
846
847   strv_check (g_strsplit_set ("", ",", 1), NULL);
848   strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
849   strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
850   strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
851   strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
852   strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
853   strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
854   strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
855   strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
856   strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
857   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
858   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
859
860   strv_check (g_strsplit_set ("", ",", 2), NULL);
861   strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
862   strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
863   strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
864   strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
865   strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
866   strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
867   strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
868   strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
869   strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
870   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
871   
872   strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
873 }
874
875 static void
876 test_strv_length (void)
877 {
878   gchar **strv;
879   guint l;
880
881   if (g_test_undefined ())
882     {
883       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
884         {
885           l = g_strv_length (NULL);
886         }
887       g_test_trap_assert_failed ();
888     }
889
890   strv = g_strsplit ("1,2,3,4", ",", -1);
891   l = g_strv_length (strv);
892   g_assert_cmpuint (l, ==, 4);
893   g_strfreev (strv);
894 }
895
896 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
897
898 static void
899 check_strtod_string (gchar    *number,
900                      double    res,
901                      gboolean  check_end,
902                      gint      correct_len)
903 {
904   double d;
905   gint l;
906   gchar *dummy;
907
908   /* we try a copy of number, with some free space for malloc before that. 
909    * This is supposed to smash the some wrong pointer calculations. */
910
911   dummy = g_malloc (100000);
912   number = g_strdup (number);
913   g_free (dummy);
914
915   for (l = 0; l < G_N_ELEMENTS (locales); l++)
916     {
917       gchar *end = "(unset)";
918
919       setlocale (LC_NUMERIC, locales[l]);
920       d = g_ascii_strtod (number, &end);
921       g_assert (isnan (res) ? isnan (d) : (d == res));
922       g_assert ((end - number) == (check_end ? correct_len : strlen (number)));
923     }
924
925   g_free (number);
926 }
927
928 static void
929 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
930 {
931   int l;
932   gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
933
934   for (l = 0; l < G_N_ELEMENTS (locales); l++)
935     {
936       setlocale (LC_ALL, locales[l]);
937       g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
938       g_assert_cmpstr (buf, ==, str);
939     }
940 }
941
942 static void
943 test_strtod (void)
944 {
945   gdouble d, our_nan, our_inf;
946   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
947
948 #ifdef NAN
949   our_nan = NAN;
950 #else
951   /* Do this before any call to setlocale.  */
952   our_nan = atof ("NaN");
953 #endif
954   g_assert (isnan (our_nan));
955
956 #ifdef INFINITY
957   our_inf = INFINITY;
958 #else
959   our_inf = atof ("Infinity");
960 #endif
961   g_assert (our_inf > 1 && our_inf == our_inf / 2);
962
963   check_strtod_string ("123.123", 123.123, FALSE, 0);
964   check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
965   check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
966   check_strtod_string ("-123.123", -123.123, FALSE, 0);
967   check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
968   check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
969   check_strtod_string ("5.4", 5.4, TRUE, 3);
970   check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
971   check_strtod_string ("5,4", 5.0, TRUE, 1);
972   check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
973   check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
974   check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
975   check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
976   check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
977   /* the following are for #156421 */
978   check_strtod_string ("1e1", 1e1, FALSE, 0); 
979   check_strtod_string ("NAN", our_nan, FALSE, 0);
980   check_strtod_string ("-nan", -our_nan, FALSE, 0);
981   check_strtod_string ("INF", our_inf, FALSE, 0);
982   check_strtod_string ("-infinity", -our_inf, FALSE, 0);
983   check_strtod_string ("-.75,0", -0.75, TRUE, 4);
984   
985   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
986   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
987
988   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
989   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
990   
991   d = pow (2.0, -1024.1);
992   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
993   
994   d = -pow (2.0, -1024.1);
995   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
996
997   /* for #343899 */
998   check_strtod_string (" 0.75", 0.75, FALSE, 0);
999   check_strtod_string (" +0.75", 0.75, FALSE, 0);
1000   check_strtod_string (" -0.75", -0.75, FALSE, 0);
1001   check_strtod_string ("\f0.75", 0.75, FALSE, 0);
1002   check_strtod_string ("\n0.75", 0.75, FALSE, 0);
1003   check_strtod_string ("\r0.75", 0.75, FALSE, 0);
1004   check_strtod_string ("\t0.75", 0.75, FALSE, 0);
1005
1006 #if 0
1007   /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1008   check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1009 #endif
1010
1011   /* for #343899 */
1012   check_strtod_number (0.75, "%0.2f", "0.75");
1013   check_strtod_number (0.75, "%5.2f", " 0.75");
1014   check_strtod_number (-0.75, "%0.2f", "-0.75");
1015   check_strtod_number (-0.75, "%5.2f", "-0.75");
1016   check_strtod_number (1e99, "%.0e", "1e+99");
1017 }
1018
1019 static void
1020 check_uint64 (const gchar *str,
1021               const gchar *end,
1022               gint         base,
1023               guint64      result,
1024               gint         error)
1025 {
1026   guint64 actual;
1027   gchar *endptr = NULL;
1028   gint err;
1029
1030   errno = 0;
1031   actual = g_ascii_strtoull (str, &endptr, base);
1032   err = errno;
1033
1034   g_assert (actual == result);
1035   g_assert_cmpstr (end, ==, endptr);
1036   g_assert (err == error);
1037 }
1038
1039 static void
1040 check_int64 (const gchar *str,
1041              const gchar *end,
1042              gint         base,
1043              gint64       result,
1044              gint         error)
1045 {
1046   gint64 actual;
1047   gchar *endptr = NULL;
1048   gint err;
1049
1050   errno = 0;
1051   actual = g_ascii_strtoll (str, &endptr, base);
1052   err = errno;
1053
1054   g_assert (actual == result);
1055   g_assert_cmpstr (end, ==, endptr);
1056   g_assert (err == error);
1057 }
1058
1059 static void
1060 test_strtoll (void)
1061 {
1062   check_uint64 ("0", "", 10, 0, 0);
1063   check_uint64 ("+0", "", 10, 0, 0);
1064   check_uint64 ("-0", "", 10, 0, 0);
1065   check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1066   check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1067   check_uint64 ("20xyz", "xyz", 10, 20, 0);
1068   check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1069
1070   check_int64 ("0", "", 10, 0, 0);
1071   check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1072   check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1073   check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1074   check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1075   check_int64 ("32768", "", 10, 32768, 0);
1076   check_int64 ("-32768", "", 10, -32768, 0);
1077   check_int64 ("001", "", 10, 1, 0);
1078   check_int64 ("-001", "", 10, -1, 0);
1079 }
1080
1081 static void
1082 test_bounds (void)
1083 {
1084   GMappedFile *file, *before, *after;
1085   char buffer[4097];
1086   char *tmp, *tmp2;
1087   char **array;
1088   char *string;
1089
1090   /* if we allocate the file between two others and then free those
1091    * other two, then hopefully we end up with unmapped memory on either
1092    * side.
1093    */
1094   before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1095
1096   /* quick workaround until #549783 can be fixed */
1097   if (before == NULL)
1098     return;
1099
1100   file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1101   after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1102   g_mapped_file_unref (before);
1103   g_mapped_file_unref (after);
1104
1105   g_assert (file != NULL);
1106   g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1107   string = g_mapped_file_get_contents (file);
1108
1109   /* ensure they're all non-nul */
1110   g_assert (memchr (string, '\0', 4096) == NULL);
1111
1112   /* test set 1: ensure that nothing goes past its maximum length, even in
1113    *             light of a missing nul terminator.
1114    *
1115    * we try to test all of the 'n' functions here.
1116    */
1117   tmp = g_strndup (string, 4096);
1118   g_assert_cmpint (strlen (tmp), ==, 4096);
1119   g_free (tmp);
1120
1121   /* found no bugs in gnome, i hope :) */
1122   g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1123   g_strstr_len (string, 4096, "B");
1124   g_strstr_len (string, 4096, ".");
1125   g_strstr_len (string, 4096, "");
1126
1127   g_strrstr_len (string, 4096, "BUGS");
1128   g_strrstr_len (string, 4096, "B");
1129   g_strrstr_len (string, 4096, ".");
1130   g_strrstr_len (string, 4096, "");
1131
1132   tmp = g_ascii_strup (string, 4096);
1133   tmp2 = g_ascii_strup (tmp, 4096);
1134   g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1135   g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1136   g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1137   g_free (tmp);
1138   g_free (tmp2);
1139
1140   tmp = g_ascii_strdown (string, 4096);
1141   tmp2 = g_ascii_strdown (tmp, 4096);
1142   g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1143   g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1144   g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1145   g_free (tmp);
1146   g_free (tmp2);
1147
1148   tmp = g_markup_escape_text (string, 4096);
1149   g_free (tmp);
1150
1151   /* test set 2: ensure that nothing reads even one byte past a '\0'.
1152    */
1153   g_assert_cmpint (string[4095], ==, '\n');
1154   string[4095] = '\0';
1155
1156   tmp = g_strdup (string);
1157   g_assert_cmpint (strlen (tmp), ==, 4095);
1158   g_free (tmp);
1159
1160   tmp = g_strndup (string, 10000);
1161   g_assert_cmpint (strlen (tmp), ==, 4095);
1162   g_free (tmp);
1163
1164   g_stpcpy (buffer, string);
1165   g_assert_cmpint (strlen (buffer), ==, 4095);
1166
1167   g_strstr_len (string, 10000, "BUGS");
1168   g_strstr_len (string, 10000, "B");
1169   g_strstr_len (string, 10000, ".");
1170   g_strstr_len (string, 10000, "");
1171
1172   g_strrstr (string, "BUGS");
1173   g_strrstr (string, "B");
1174   g_strrstr (string, ".");
1175   g_strrstr (string, "");
1176
1177   g_strrstr_len (string, 10000, "BUGS");
1178   g_strrstr_len (string, 10000, "B");
1179   g_strrstr_len (string, 10000, ".");
1180   g_strrstr_len (string, 10000, "");
1181
1182   g_str_has_prefix (string, "this won't do very much...");
1183   g_str_has_suffix (string, "but maybe this will...");
1184   g_str_has_suffix (string, "HMMMM.");
1185   g_str_has_suffix (string, "MMMM.");
1186   g_str_has_suffix (string, "M.");
1187
1188   g_strlcpy (buffer, string, sizeof buffer);
1189   g_assert_cmpint (strlen (buffer), ==, 4095);
1190   g_strlcpy (buffer, string, sizeof buffer);
1191   buffer[0] = '\0';
1192   g_strlcat (buffer, string, sizeof buffer);
1193   g_assert_cmpint (strlen (buffer), ==, 4095);
1194
1195   tmp = g_strdup_printf ("<%s>", string);
1196   g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1197   g_free (tmp);
1198
1199   tmp = g_ascii_strdown (string, -1);
1200   tmp2 = g_ascii_strdown (tmp, -1);
1201   g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1202   g_assert_cmpint (strlen(string), ==, strlen(tmp));
1203   g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1204   g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1205   g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1206   g_free (tmp);
1207   g_free (tmp2);
1208
1209   tmp = g_ascii_strup (string, -1);
1210   tmp2 = g_ascii_strup (string, -1);
1211   g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
1212   g_assert_cmpint (strlen(string), ==, strlen(tmp));
1213   g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1214   g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1215   g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1216   g_free (tmp);
1217   g_free (tmp2);
1218
1219   g_ascii_strcasecmp (string, string);
1220   g_ascii_strncasecmp (string, string, 10000);
1221
1222   g_strreverse (string);
1223   g_strreverse (string);
1224   g_strchug (string);
1225   g_strchomp (string);
1226   g_strstrip (string);
1227   g_assert_cmpint (strlen (string), ==, 4095);
1228
1229   g_strdelimit (string, "M", 'N');
1230   g_strcanon (string, " N.", ':');
1231   g_assert_cmpint (strlen (string), ==, 4095);
1232
1233   array = g_strsplit (string, ".", -1);
1234   tmp = g_strjoinv (".", array);
1235   g_strfreev (array);
1236
1237   g_assert_cmpint (strlen (tmp), ==, 4095);
1238   g_assert (memcmp (tmp, string, 4095) == 0);
1239   g_free (tmp);
1240
1241   tmp = g_strconcat (string, string, string, NULL);
1242   g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1243   g_free (tmp);
1244
1245   tmp = g_strjoin ("!", string, string, NULL);
1246   g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1247   g_free (tmp);
1248
1249   tmp = g_markup_escape_text (string, -1);
1250   g_free (tmp);
1251
1252   tmp = g_markup_printf_escaped ("%s", string);
1253   g_free (tmp);
1254
1255   tmp = g_strescape (string, NULL);
1256   tmp2 = g_strcompress (tmp);
1257   g_assert_cmpstr (string, ==, tmp2);
1258   g_free (tmp2);
1259   g_free (tmp);
1260
1261   g_mapped_file_unref (file);
1262 }
1263
1264 static void
1265 test_strip_context (void)
1266 {
1267   const gchar *msgid;
1268   const gchar *msgval;
1269   const gchar *s;
1270
1271
1272   msgid = "blabla";
1273   msgval = "bla";
1274   s = g_strip_context (msgid, msgval);
1275   g_assert (s == msgval);
1276
1277   msgid = msgval = "blabla";
1278   s = g_strip_context (msgid, msgval);
1279   g_assert (s == msgval);
1280
1281   msgid = msgval = "blabla|foo";
1282   s = g_strip_context (msgid, msgval);
1283   g_assert (s == msgval + 7);
1284
1285   msgid = msgval = "blabla||bar";
1286   s = g_strip_context (msgid, msgval);
1287   g_assert (s == msgval + 7);
1288 }
1289
1290 static void
1291 test_strerror (void)
1292 {
1293   gint i;
1294   const gchar *str;
1295
1296   for (i = 1; i < 100; i++)
1297     {
1298       str = g_strerror (i);
1299       g_assert (str != NULL);
1300       g_assert (g_utf8_validate (str, -1, NULL));
1301     }
1302 }
1303
1304 static void
1305 test_strsignal (void)
1306 {
1307   gint i;
1308   const gchar *str;
1309
1310   for (i = 1; i < 20; i++)
1311     {
1312       str = g_strsignal (i);
1313       g_assert (str != NULL);
1314       g_assert (g_utf8_validate (str, -1, NULL));
1315     }
1316 }
1317
1318 static void
1319 test_strup (void)
1320 {
1321   gchar *s;
1322
1323   s = g_strdup ("lower");
1324   g_assert_cmpstr (g_strup (s), ==, "LOWER");
1325   g_assert_cmpstr (g_strdown (s), ==, "lower");
1326   g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1327   g_free (s);
1328 }
1329
1330 int
1331 main (int   argc,
1332       char *argv[])
1333 {
1334   g_test_init (&argc, &argv, NULL);
1335
1336   g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1337   g_test_add_func ("/strfuncs/strdup", test_strdup);
1338   g_test_add_func ("/strfuncs/strndup", test_strndup);
1339   g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1340   g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1341   g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1342   g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1343   g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1344   g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1345   g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1346   g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1347   g_test_add_func ("/strfuncs/strchug", test_strchug);
1348   g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1349   g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1350   g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
1351   g_test_add_func ("/strfuncs/strstr", test_strstr);
1352   g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1353   g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1354   g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1355   g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1356   g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1357   g_test_add_func ("/strfuncs/strtod", test_strtod);
1358   g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1359   g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1360   g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1361   g_test_add_func ("/strfuncs/strerror", test_strerror);
1362   g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1363   g_test_add_func ("/strfuncs/strup", test_strup);
1364
1365   return g_test_run();
1366 }