Merge branch 'upstream' into stable
[gnulib.git] / tests / test-wcrtomb.c
1 /* Test of conversion of wide character to multibyte character.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18
19 #include <config.h>
20
21 #include <wchar.h>
22
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 /* Check the multibyte character s[0..n-1].  */
41 static void
42 check_character (const char *s, size_t n)
43 {
44   wchar_t wc;
45   char buf[64];
46   int iret;
47   size_t ret;
48
49   wc = (wchar_t) 0xBADFACE;
50   iret = mbtowc (&wc, s, n);
51   ASSERT (iret == n);
52
53   ret = wcrtomb (buf, wc, NULL);
54   ASSERT (ret == n);
55   ASSERT (memcmp (buf, s, n) == 0);
56
57   /* Test special calling convention, passing a NULL pointer.  */
58   ret = wcrtomb (NULL, wc, NULL);
59   ASSERT (ret == 1);
60 }
61
62 int
63 main (int argc, char *argv[])
64 {
65   char buf[64];
66   size_t ret;
67
68   /* configure should already have checked that the locale is supported.  */
69   if (setlocale (LC_ALL, "") == NULL)
70     return 1;
71
72   /* Test NUL character.  */
73   {
74     buf[0] = 'x';
75     ret = wcrtomb (buf, 0, NULL);
76     ASSERT (ret == 1);
77     ASSERT (buf[0] == '\0');
78   }
79
80   /* Test single bytes.  */
81   {
82     int c;
83
84     for (c = 0; c < 0x100; c++)
85       switch (c)
86         {
87         case '\t': case '\v': case '\f':
88         case ' ': case '!': case '"': case '#': case '%':
89         case '&': case '\'': case '(': case ')': case '*':
90         case '+': case ',': case '-': case '.': case '/':
91         case '0': case '1': case '2': case '3': case '4':
92         case '5': case '6': case '7': case '8': case '9':
93         case ':': case ';': case '<': case '=': case '>':
94         case '?':
95         case 'A': case 'B': case 'C': case 'D': case 'E':
96         case 'F': case 'G': case 'H': case 'I': case 'J':
97         case 'K': case 'L': case 'M': case 'N': case 'O':
98         case 'P': case 'Q': case 'R': case 'S': case 'T':
99         case 'U': case 'V': case 'W': case 'X': case 'Y':
100         case 'Z':
101         case '[': case '\\': case ']': case '^': case '_':
102         case 'a': case 'b': case 'c': case 'd': case 'e':
103         case 'f': case 'g': case 'h': case 'i': case 'j':
104         case 'k': case 'l': case 'm': case 'n': case 'o':
105         case 'p': case 'q': case 'r': case 's': case 't':
106         case 'u': case 'v': case 'w': case 'x': case 'y':
107         case 'z': case '{': case '|': case '}': case '~':
108           /* c is in the ISO C "basic character set".  */
109           ret = wcrtomb (buf, btowc (c), NULL);
110           ASSERT (ret == 1);
111           ASSERT (buf[0] == (char) c);
112           break;
113         }
114   }
115
116   if (argc > 1)
117     switch (argv[1][0])
118       {
119       case '1':
120         /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
121         {
122           const char input[] = "B\374\337er"; /* "Büßer" */
123
124           check_character (input + 1, 1);
125           check_character (input + 2, 1);
126         }
127         return 0;
128
129       case '2':
130         /* Locale encoding is UTF-8.  */
131         {
132           const char input[] = "B\303\274\303\237er"; /* "Büßer" */
133
134           check_character (input + 1, 2);
135           check_character (input + 3, 2);
136         }
137         return 0;
138
139       case '3':
140         /* Locale encoding is EUC-JP.  */
141         {
142           const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
143
144           check_character (input + 1, 2);
145           check_character (input + 3, 2);
146           check_character (input + 5, 2);
147         }
148         return 0;
149
150       case '4':
151         /* Locale encoding is GB18030.  */
152         {
153           const char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
154
155           check_character (input + 1, 2);
156           check_character (input + 3, 4);
157         }
158         return 0;
159       }
160
161   return 1;
162 }