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