Tizen 2.1 base
[platform/upstream/glib2.0.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_strstr (void)
572 {
573   gchar *haystack;
574   gchar *res;
575
576   haystack = g_strdup ("FooBarFooBarFoo");
577
578   /* strstr_len */
579   res = g_strstr_len (haystack, 6, "xxx");
580   g_assert (res == NULL);
581
582   res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
583   g_assert (res == NULL);
584
585   res = g_strstr_len (haystack, 3, "Bar");
586   g_assert (res == NULL);
587
588   res = g_strstr_len (haystack, 6, "");
589   g_assert (res == haystack);
590   g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
591
592   res = g_strstr_len (haystack, 6, "Bar");
593   g_assert (res == haystack + 3);
594   g_assert_cmpstr (res, ==, "BarFooBarFoo");
595
596   res = g_strstr_len (haystack, -1, "Bar");
597   g_assert (res == haystack + 3);
598   g_assert_cmpstr (res, ==, "BarFooBarFoo");
599
600   /* strrstr */
601   res = g_strrstr (haystack, "xxx");
602   g_assert (res == NULL);
603
604   res = g_strrstr (haystack, "FooBarFooBarFooBar");
605   g_assert (res == NULL);
606
607   res = g_strrstr (haystack, "");
608   g_assert (res == haystack);
609   g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
610
611   res = g_strrstr (haystack, "Bar");
612   g_assert (res == haystack + 9);
613   g_assert_cmpstr (res, ==, "BarFoo");
614
615   /* strrstr_len */
616   res = g_strrstr_len (haystack, 14, "xxx");
617   g_assert (res == NULL);
618
619   res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
620   g_assert (res == NULL);
621
622   res = g_strrstr_len (haystack, 3, "Bar");
623   g_assert (res == NULL);
624
625   res = g_strrstr_len (haystack, 14, "BarFoo");
626   g_assert (res == haystack + 3);
627   g_assert_cmpstr (res, ==, "BarFooBarFoo");
628
629   res = g_strrstr_len (haystack, 15, "BarFoo");
630   g_assert (res == haystack + 9);
631   g_assert_cmpstr (res, ==, "BarFoo");
632
633   res = g_strrstr_len (haystack, -1, "BarFoo");
634   g_assert (res == haystack + 9);
635   g_assert_cmpstr (res, ==, "BarFoo");
636
637   /* test case for strings with \0 in the middle */
638   *(haystack + 7) = '\0';
639   res = g_strstr_len (haystack, 15, "BarFoo");
640   g_assert (res == NULL);
641
642   g_free (haystack);
643 }
644
645 static void
646 test_has_prefix (void)
647 {
648   gboolean res;
649
650   if (g_test_undefined ())
651     {
652       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
653         {
654           res = g_str_has_prefix ("foo", NULL);
655         }
656       g_test_trap_assert_failed ();
657
658       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
659         {
660           res = g_str_has_prefix (NULL, "foo");
661         }
662       g_test_trap_assert_failed ();
663     }
664
665   res = g_str_has_prefix ("foo", "bar");
666   g_assert_cmpint (res, ==, FALSE);
667
668   res = g_str_has_prefix ("foo", "foobar");
669   g_assert_cmpint (res, ==, FALSE);
670
671   res = g_str_has_prefix ("foobar", "bar");
672   g_assert_cmpint (res, ==, FALSE);
673
674   res = g_str_has_prefix ("foobar", "foo");
675   g_assert_cmpint (res, ==, TRUE);
676
677   res = g_str_has_prefix ("foo", "");
678   g_assert_cmpint (res, ==, TRUE);
679
680   res = g_str_has_prefix ("foo", "foo");
681   g_assert_cmpint (res, ==, TRUE);
682
683   res = g_str_has_prefix ("", "");
684   g_assert_cmpint (res, ==, TRUE);
685 }
686
687 static void
688 test_has_suffix (void)
689 {
690   gboolean res;
691
692   if (g_test_undefined ())
693     {
694       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
695         {
696           res = g_str_has_suffix ("foo", NULL);
697         }
698       g_test_trap_assert_failed ();
699
700       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
701         {
702           res = g_str_has_suffix (NULL, "foo");
703         }
704       g_test_trap_assert_failed ();
705     }
706
707   res = g_str_has_suffix ("foo", "bar");
708   g_assert_cmpint (res, ==, FALSE);
709
710   res = g_str_has_suffix ("bar", "foobar");
711   g_assert_cmpint (res, ==, FALSE);
712
713   res = g_str_has_suffix ("foobar", "foo");
714   g_assert_cmpint (res, ==, FALSE);
715
716   res = g_str_has_suffix ("foobar", "bar");
717   g_assert_cmpint (res, ==, TRUE);
718
719   res = g_str_has_suffix ("foo", "");
720   g_assert_cmpint (res, ==, TRUE);
721
722   res = g_str_has_suffix ("foo", "foo");
723   g_assert_cmpint (res, ==, TRUE);
724
725   res = g_str_has_suffix ("", "");
726   g_assert_cmpint (res, ==, TRUE);
727 }
728
729 static void
730 strv_check (gchar **strv, ...)
731 {
732   gboolean ok = TRUE;
733   gint i = 0;
734   va_list list;
735
736   va_start (list, strv);
737   while (ok)
738     {
739       const gchar *str = va_arg (list, const char *);
740       if (strv[i] == NULL)
741         {
742           g_assert (str == NULL);
743           break;
744         }
745       if (str == NULL)
746         {
747           ok = FALSE;
748         }
749       else
750         {
751           g_assert_cmpstr (strv[i], ==, str);
752         }
753       i++;
754     }
755   va_end (list);
756
757   g_strfreev (strv);
758 }
759
760 static void
761 test_strsplit (void)
762 {
763   strv_check (g_strsplit ("", ",", 0), NULL);
764   strv_check (g_strsplit ("x", ",", 0), "x", NULL);
765   strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
766   strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
767   strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
768   strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
769   strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
770   strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
771   strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
772   strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
773   strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
774   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
775
776   strv_check (g_strsplit ("", ",", 1), NULL);
777   strv_check (g_strsplit ("x", ",", 1), "x", NULL);
778   strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
779   strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
780   strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
781   strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
782   strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
783   strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
784   strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
785   strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
786   strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
787   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
788
789   strv_check (g_strsplit ("", ",", 2), NULL);
790   strv_check (g_strsplit ("x", ",", 2), "x", NULL);
791   strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
792   strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
793   strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
794   strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
795   strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
796   strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
797   strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
798   strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
799   strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
800   strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
801 }
802
803 static void
804 test_strsplit_set (void)
805 {
806   strv_check (g_strsplit_set ("", ",/", 0), NULL);
807   strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
808   strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
809   strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
810   strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
811
812   strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
813   strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
814   strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
815   strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
816   strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
817   strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
818   strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
819   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
820
821   strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
822   strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
823   strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
824   strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
825   strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
826   strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
827    
828   strv_check (g_strsplit_set ("", ",", 0), NULL);
829   strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
830   strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
831   strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
832   strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
833   strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
834   strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
835   strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
836   strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
837   strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
838   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
839
840   strv_check (g_strsplit_set ("", ",", 1), NULL);
841   strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
842   strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
843   strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
844   strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
845   strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
846   strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
847   strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
848   strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
849   strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
850   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
851   strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
852
853   strv_check (g_strsplit_set ("", ",", 2), NULL);
854   strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
855   strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
856   strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
857   strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
858   strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
859   strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
860   strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
861   strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
862   strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
863   strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
864   
865   strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
866 }
867
868 static void
869 test_strv_length (void)
870 {
871   gchar **strv;
872   guint l;
873
874   if (g_test_undefined ())
875     {
876       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
877         {
878           l = g_strv_length (NULL);
879         }
880       g_test_trap_assert_failed ();
881     }
882
883   strv = g_strsplit ("1,2,3,4", ",", -1);
884   l = g_strv_length (strv);
885   g_assert_cmpuint (l, ==, 4);
886   g_strfreev (strv);
887 }
888
889 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
890
891 static void
892 check_strtod_string (gchar    *number,
893                      double    res,
894                      gboolean  check_end,
895                      gint      correct_len)
896 {
897   double d;
898   gint l;
899   gchar *dummy;
900
901   /* we try a copy of number, with some free space for malloc before that. 
902    * This is supposed to smash the some wrong pointer calculations. */
903
904   dummy = g_malloc (100000);
905   number = g_strdup (number);
906   g_free (dummy);
907
908   for (l = 0; l < G_N_ELEMENTS (locales); l++)
909     {
910       gchar *end = "(unset)";
911
912       setlocale (LC_NUMERIC, locales[l]);
913       d = g_ascii_strtod (number, &end);
914       g_assert (isnan (res) ? isnan (d) : (d == res));
915       g_assert ((end - number) == (check_end ? correct_len : strlen (number)));
916     }
917
918   g_free (number);
919 }
920
921 static void
922 check_strtod_number (gdouble num, gchar *fmt, gchar *str)
923 {
924   int l;
925   gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
926
927   for (l = 0; l < G_N_ELEMENTS (locales); l++)
928     {
929       setlocale (LC_ALL, locales[l]);
930       g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
931       g_assert_cmpstr (buf, ==, str);
932     }
933 }
934
935 static void
936 test_strtod (void)
937 {
938   gdouble d, our_nan, our_inf;
939   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
940
941 #ifdef NAN
942   our_nan = NAN;
943 #else
944   /* Do this before any call to setlocale.  */
945   our_nan = atof ("NaN");
946 #endif
947   g_assert (isnan (our_nan));
948
949 #ifdef INFINITY
950   our_inf = INFINITY;
951 #else
952   our_inf = atof ("Infinity");
953 #endif
954   g_assert (our_inf > 1 && our_inf == our_inf / 2);
955
956   check_strtod_string ("123.123", 123.123, FALSE, 0);
957   check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
958   check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
959   check_strtod_string ("-123.123", -123.123, FALSE, 0);
960   check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
961   check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
962   check_strtod_string ("5.4", 5.4, TRUE, 3);
963   check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
964   check_strtod_string ("5,4", 5.0, TRUE, 1);
965   check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
966   check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
967   check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
968   check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
969   check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
970   /* the following are for #156421 */
971   check_strtod_string ("1e1", 1e1, FALSE, 0); 
972   check_strtod_string ("NAN", our_nan, FALSE, 0);
973   check_strtod_string ("-nan", -our_nan, FALSE, 0);
974   check_strtod_string ("INF", our_inf, FALSE, 0);
975   check_strtod_string ("-infinity", -our_inf, FALSE, 0);
976   check_strtod_string ("-.75,0", -0.75, TRUE, 4);
977   
978   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
979   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
980
981   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
982   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
983   
984   d = pow (2.0, -1024.1);
985   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
986   
987   d = -pow (2.0, -1024.1);
988   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
989
990   /* for #343899 */
991   check_strtod_string (" 0.75", 0.75, FALSE, 0);
992   check_strtod_string (" +0.75", 0.75, FALSE, 0);
993   check_strtod_string (" -0.75", -0.75, FALSE, 0);
994   check_strtod_string ("\f0.75", 0.75, FALSE, 0);
995   check_strtod_string ("\n0.75", 0.75, FALSE, 0);
996   check_strtod_string ("\r0.75", 0.75, FALSE, 0);
997   check_strtod_string ("\t0.75", 0.75, FALSE, 0);
998
999 #if 0
1000   /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1001   check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1002 #endif
1003
1004   /* for #343899 */
1005   check_strtod_number (0.75, "%0.2f", "0.75");
1006   check_strtod_number (0.75, "%5.2f", " 0.75");
1007   check_strtod_number (-0.75, "%0.2f", "-0.75");
1008   check_strtod_number (-0.75, "%5.2f", "-0.75");
1009   check_strtod_number (1e99, "%.0e", "1e+99");
1010 }
1011
1012 static void
1013 check_uint64 (const gchar *str,
1014               const gchar *end,
1015               gint         base,
1016               guint64      result,
1017               gint         error)
1018 {
1019   guint64 actual;
1020   gchar *endptr = NULL;
1021   gint err;
1022
1023   errno = 0;
1024   actual = g_ascii_strtoull (str, &endptr, base);
1025   err = errno;
1026
1027   g_assert (actual == result);
1028   g_assert_cmpstr (end, ==, endptr);
1029   g_assert (err == error);
1030 }
1031
1032 static void
1033 check_int64 (const gchar *str,
1034              const gchar *end,
1035              gint         base,
1036              gint64       result,
1037              gint         error)
1038 {
1039   gint64 actual;
1040   gchar *endptr = NULL;
1041   gint err;
1042
1043   errno = 0;
1044   actual = g_ascii_strtoll (str, &endptr, base);
1045   err = errno;
1046
1047   g_assert (actual == result);
1048   g_assert_cmpstr (end, ==, endptr);
1049   g_assert (err == error);
1050 }
1051
1052 static void
1053 test_strtoll (void)
1054 {
1055   check_uint64 ("0", "", 10, 0, 0);
1056   check_uint64 ("+0", "", 10, 0, 0);
1057   check_uint64 ("-0", "", 10, 0, 0);
1058   check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1059   check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1060   check_uint64 ("20xyz", "xyz", 10, 20, 0);
1061   check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1062
1063   check_int64 ("0", "", 10, 0, 0);
1064   check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1065   check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1066   check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1067   check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1068   check_int64 ("32768", "", 10, 32768, 0);
1069   check_int64 ("-32768", "", 10, -32768, 0);
1070   check_int64 ("001", "", 10, 1, 0);
1071   check_int64 ("-001", "", 10, -1, 0);
1072 }
1073
1074 static void
1075 test_bounds (void)
1076 {
1077   GMappedFile *file, *before, *after;
1078   char buffer[4097];
1079   char *tmp, *tmp2;
1080   char **array;
1081   char *string;
1082
1083   /* if we allocate the file between two others and then free those
1084    * other two, then hopefully we end up with unmapped memory on either
1085    * side.
1086    */
1087   before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1088
1089   /* quick workaround until #549783 can be fixed */
1090   if (before == NULL)
1091     return;
1092
1093   file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1094   after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1095   g_mapped_file_unref (before);
1096   g_mapped_file_unref (after);
1097
1098   g_assert (file != NULL);
1099   g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1100   string = g_mapped_file_get_contents (file);
1101
1102   /* ensure they're all non-nul */
1103   g_assert (memchr (string, '\0', 4096) == NULL);
1104
1105   /* test set 1: ensure that nothing goes past its maximum length, even in
1106    *             light of a missing nul terminator.
1107    *
1108    * we try to test all of the 'n' functions here.
1109    */
1110   tmp = g_strndup (string, 4096);
1111   g_assert_cmpint (strlen (tmp), ==, 4096);
1112   g_free (tmp);
1113
1114   /* found no bugs in gnome, i hope :) */
1115   g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
1116   g_strstr_len (string, 4096, "B");
1117   g_strstr_len (string, 4096, ".");
1118   g_strstr_len (string, 4096, "");
1119
1120   g_strrstr_len (string, 4096, "BUGS");
1121   g_strrstr_len (string, 4096, "B");
1122   g_strrstr_len (string, 4096, ".");
1123   g_strrstr_len (string, 4096, "");
1124
1125   g_ascii_strdown (string, 4096);
1126   g_ascii_strdown (string, 4096);
1127   g_ascii_strup (string, 4096);
1128   g_ascii_strup (string, 4096);
1129
1130   g_ascii_strncasecmp (string, string, 4096);
1131
1132   tmp = g_markup_escape_text (string, 4096);
1133   g_free (tmp);
1134
1135   /* test set 2: ensure that nothing reads even one byte past a '\0'.
1136    */
1137   g_assert_cmpint (string[4095], ==, '\n');
1138   string[4095] = '\0';
1139
1140   tmp = g_strdup (string);
1141   g_assert_cmpint (strlen (tmp), ==, 4095);
1142   g_free (tmp);
1143
1144   tmp = g_strndup (string, 10000);
1145   g_assert_cmpint (strlen (tmp), ==, 4095);
1146   g_free (tmp);
1147
1148   g_stpcpy (buffer, string);
1149   g_assert_cmpint (strlen (buffer), ==, 4095);
1150
1151   g_strstr_len (string, 10000, "BUGS");
1152   g_strstr_len (string, 10000, "B");
1153   g_strstr_len (string, 10000, ".");
1154   g_strstr_len (string, 10000, "");
1155
1156   g_strrstr (string, "BUGS");
1157   g_strrstr (string, "B");
1158   g_strrstr (string, ".");
1159   g_strrstr (string, "");
1160
1161   g_strrstr_len (string, 10000, "BUGS");
1162   g_strrstr_len (string, 10000, "B");
1163   g_strrstr_len (string, 10000, ".");
1164   g_strrstr_len (string, 10000, "");
1165
1166   g_str_has_prefix (string, "this won't do very much...");
1167   g_str_has_suffix (string, "but maybe this will...");
1168   g_str_has_suffix (string, "HMMMM.");
1169   g_str_has_suffix (string, "MMMM.");
1170   g_str_has_suffix (string, "M.");
1171
1172   g_strlcpy (buffer, string, sizeof buffer);
1173   g_assert_cmpint (strlen (buffer), ==, 4095);
1174   g_strlcpy (buffer, string, sizeof buffer);
1175   buffer[0] = '\0';
1176   g_strlcat (buffer, string, sizeof buffer);
1177   g_assert_cmpint (strlen (buffer), ==, 4095);
1178
1179   tmp = g_strdup_printf ("<%s>", string);
1180   g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1181   g_free (tmp);
1182
1183   g_ascii_strdown (string, -1);
1184   g_ascii_strdown (string, -1);
1185   g_ascii_strup (string, -1);
1186   g_ascii_strup (string, -1);
1187
1188   g_ascii_strcasecmp (string, string);
1189   g_ascii_strncasecmp (string, string, 10000);
1190
1191   g_strreverse (string);
1192   g_strreverse (string);
1193   g_strchug (string);
1194   g_strchomp (string);
1195   g_strstrip (string);
1196   g_assert_cmpint (strlen (string), ==, 4095);
1197
1198   g_strdelimit (string, "M", 'N');
1199   g_strcanon (string, " N.", ':');
1200   g_assert_cmpint (strlen (string), ==, 4095);
1201
1202   array = g_strsplit (string, ".", -1);
1203   tmp = g_strjoinv (".", array);
1204   g_strfreev (array);
1205
1206   g_assert_cmpint (strlen (tmp), ==, 4095);
1207   g_assert (memcmp (tmp, string, 4095) == 0);
1208   g_free (tmp);
1209
1210   tmp = g_strconcat (string, string, string, NULL);
1211   g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
1212   g_free (tmp);
1213
1214   tmp = g_strjoin ("!", string, string, NULL);
1215   g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
1216   g_free (tmp);
1217
1218   tmp = g_markup_escape_text (string, -1);
1219   g_free (tmp);
1220
1221   tmp = g_markup_printf_escaped ("%s", string);
1222   g_free (tmp);
1223
1224   tmp = g_strescape (string, NULL);
1225   tmp2 = g_strcompress (tmp);
1226   g_assert_cmpstr (string, ==, tmp2);
1227   g_free (tmp2);
1228   g_free (tmp);
1229
1230   g_mapped_file_unref (file);
1231 }
1232
1233 static void
1234 test_strip_context (void)
1235 {
1236   const gchar *msgid;
1237   const gchar *msgval;
1238   const gchar *s;
1239
1240
1241   msgid = "blabla";
1242   msgval = "bla";
1243   s = g_strip_context (msgid, msgval);
1244   g_assert (s == msgval);
1245
1246   msgid = msgval = "blabla";
1247   s = g_strip_context (msgid, msgval);
1248   g_assert (s == msgval);
1249
1250   msgid = msgval = "blabla|foo";
1251   s = g_strip_context (msgid, msgval);
1252   g_assert (s == msgval + 7);
1253
1254   msgid = msgval = "blabla||bar";
1255   s = g_strip_context (msgid, msgval);
1256   g_assert (s == msgval + 7);
1257 }
1258
1259 static void
1260 test_strerror (void)
1261 {
1262   gint i;
1263   const gchar *str;
1264
1265   for (i = 1; i < 100; i++)
1266     {
1267       str = g_strerror (i);
1268       g_assert (str != NULL);
1269       g_assert (g_utf8_validate (str, -1, NULL));
1270     }
1271 }
1272
1273 static void
1274 test_strsignal (void)
1275 {
1276   gint i;
1277   const gchar *str;
1278
1279   for (i = 1; i < 20; i++)
1280     {
1281       str = g_strsignal (i);
1282       g_assert (str != NULL);
1283       g_assert (g_utf8_validate (str, -1, NULL));
1284     }
1285 }
1286
1287 static void
1288 test_strup (void)
1289 {
1290   gchar *s;
1291
1292   s = g_strdup ("lower");
1293   g_assert_cmpstr (g_strup (s), ==, "LOWER");
1294   g_assert_cmpstr (g_strdown (s), ==, "lower");
1295   g_assert (g_strcasecmp ("lower", "LOWER") == 0);
1296   g_free (s);
1297 }
1298
1299 int
1300 main (int   argc,
1301       char *argv[])
1302 {
1303   g_test_init (&argc, &argv, NULL);
1304
1305   g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
1306   g_test_add_func ("/strfuncs/strdup", test_strdup);
1307   g_test_add_func ("/strfuncs/strndup", test_strndup);
1308   g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
1309   g_test_add_func ("/strfuncs/strdupv", test_strdupv);
1310   g_test_add_func ("/strfuncs/strnfill", test_strnfill);
1311   g_test_add_func ("/strfuncs/strconcat", test_strconcat);
1312   g_test_add_func ("/strfuncs/strjoin", test_strjoin);
1313   g_test_add_func ("/strfuncs/strcanon", test_strcanon);
1314   g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
1315   g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
1316   g_test_add_func ("/strfuncs/strchug", test_strchug);
1317   g_test_add_func ("/strfuncs/strchomp", test_strchomp);
1318   g_test_add_func ("/strfuncs/strreverse", test_strreverse);
1319   g_test_add_func ("/strfuncs/strstr", test_strstr);
1320   g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
1321   g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
1322   g_test_add_func ("/strfuncs/strsplit", test_strsplit);
1323   g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
1324   g_test_add_func ("/strfuncs/strv-length", test_strv_length);
1325   g_test_add_func ("/strfuncs/strtod", test_strtod);
1326   g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
1327   g_test_add_func ("/strfuncs/bounds-check", test_bounds);
1328   g_test_add_func ("/strfuncs/strip-context", test_strip_context);
1329   g_test_add_func ("/strfuncs/strerror", test_strerror);
1330   g_test_add_func ("/strfuncs/strsignal", test_strsignal);
1331   g_test_add_func ("/strfuncs/strup", test_strup);
1332
1333   return g_test_run();
1334 }