Add test case from a recent glibc bug.
[gnulib.git] / tests / test-mbrtowc.c
blob44da2953730fce689fd99ca33b79b2f443a92b8d
1 /* Test of conversion of multibyte character to wide character.
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
19 #include <config.h>
21 #include <wchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (mbrtowc, size_t, (wchar_t *, char const *, size_t,
25 mbstate_t *));
27 #include <locale.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "macros.h"
33 int
34 main (int argc, char *argv[])
36 mbstate_t state;
37 wchar_t wc;
38 size_t ret;
40 /* configure should already have checked that the locale is supported. */
41 if (setlocale (LC_ALL, "") == NULL)
42 return 1;
44 /* Test zero-length input. */
46 memset (&state, '\0', sizeof (mbstate_t));
47 wc = (wchar_t) 0xBADFACE;
48 ret = mbrtowc (&wc, "x", 0, &state);
49 ASSERT (ret == (size_t)(-2));
50 ASSERT (mbsinit (&state));
53 /* Test NUL byte input. */
55 memset (&state, '\0', sizeof (mbstate_t));
56 wc = (wchar_t) 0xBADFACE;
57 ret = mbrtowc (&wc, "", 1, &state);
58 ASSERT (ret == 0);
59 ASSERT (wc == 0);
60 ASSERT (mbsinit (&state));
61 ret = mbrtowc (NULL, "", 1, &state);
62 ASSERT (ret == 0);
63 ASSERT (mbsinit (&state));
66 /* Test single-byte input. */
68 int c;
69 char buf[1];
71 memset (&state, '\0', sizeof (mbstate_t));
72 for (c = 0; c < 0x100; c++)
73 switch (c)
75 default:
76 if (! (c && 1 < argc && argv[1][0] == '5'))
77 break;
78 FALLTHROUGH;
79 case '\t': case '\v': case '\f':
80 case ' ': case '!': case '"': case '#': case '%':
81 case '&': case '\'': case '(': case ')': case '*':
82 case '+': case ',': case '-': case '.': case '/':
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 case ':': case ';': case '<': case '=': case '>':
86 case '?':
87 case 'A': case 'B': case 'C': case 'D': case 'E':
88 case 'F': case 'G': case 'H': case 'I': case 'J':
89 case 'K': case 'L': case 'M': case 'N': case 'O':
90 case 'P': case 'Q': case 'R': case 'S': case 'T':
91 case 'U': case 'V': case 'W': case 'X': case 'Y':
92 case 'Z':
93 case '[': case '\\': case ']': case '^': case '_':
94 case 'a': case 'b': case 'c': case 'd': case 'e':
95 case 'f': case 'g': case 'h': case 'i': case 'j':
96 case 'k': case 'l': case 'm': case 'n': case 'o':
97 case 'p': case 'q': case 'r': case 's': case 't':
98 case 'u': case 'v': case 'w': case 'x': case 'y':
99 case 'z': case '{': case '|': case '}': case '~':
100 /* c is in the ISO C "basic character set", or argv[1] starts
101 with '5' so we are testing all nonnull bytes. */
102 buf[0] = c;
103 wc = (wchar_t) 0xBADFACE;
104 ret = mbrtowc (&wc, buf, 1, &state);
105 ASSERT (ret == 1);
106 if (c < 0x80)
107 /* c is an ASCII character. */
108 ASSERT (wc == c);
109 else
110 /* argv[1] starts with '5', that is, we are testing the C or POSIX
111 locale.
112 On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
113 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
114 ASSERT (wc == (btowc (c) == WEOF ? c : btowc (c)));
115 ASSERT (mbsinit (&state));
116 ret = mbrtowc (NULL, buf, 1, &state);
117 ASSERT (ret == 1);
118 ASSERT (mbsinit (&state));
119 break;
123 /* Test special calling convention, passing a NULL pointer. */
125 memset (&state, '\0', sizeof (mbstate_t));
126 wc = (wchar_t) 0xBADFACE;
127 ret = mbrtowc (&wc, NULL, 5, &state);
128 ASSERT (ret == 0);
129 ASSERT (wc == (wchar_t) 0xBADFACE);
130 ASSERT (mbsinit (&state));
133 if (argc > 1)
134 switch (argv[1][0])
136 case '1':
137 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
139 char input[] = "B\374\337er"; /* "Büßer" */
140 memset (&state, '\0', sizeof (mbstate_t));
142 wc = (wchar_t) 0xBADFACE;
143 ret = mbrtowc (&wc, input, 1, &state);
144 ASSERT (ret == 1);
145 ASSERT (wc == 'B');
146 ASSERT (mbsinit (&state));
147 input[0] = '\0';
149 wc = (wchar_t) 0xBADFACE;
150 ret = mbrtowc (&wc, input + 1, 1, &state);
151 ASSERT (ret == 1);
152 ASSERT (wctob (wc) == (unsigned char) '\374');
153 ASSERT (mbsinit (&state));
154 input[1] = '\0';
156 /* Test support of NULL first argument. */
157 ret = mbrtowc (NULL, input + 2, 3, &state);
158 ASSERT (ret == 1);
159 ASSERT (mbsinit (&state));
161 wc = (wchar_t) 0xBADFACE;
162 ret = mbrtowc (&wc, input + 2, 3, &state);
163 ASSERT (ret == 1);
164 ASSERT (wctob (wc) == (unsigned char) '\337');
165 ASSERT (mbsinit (&state));
166 input[2] = '\0';
168 wc = (wchar_t) 0xBADFACE;
169 ret = mbrtowc (&wc, input + 3, 2, &state);
170 ASSERT (ret == 1);
171 ASSERT (wc == 'e');
172 ASSERT (mbsinit (&state));
173 input[3] = '\0';
175 wc = (wchar_t) 0xBADFACE;
176 ret = mbrtowc (&wc, input + 4, 1, &state);
177 ASSERT (ret == 1);
178 ASSERT (wc == 'r');
179 ASSERT (mbsinit (&state));
181 return 0;
183 case '2':
184 /* Locale encoding is UTF-8. */
186 char input[] = "B\303\274\303\237er"; /* "Büßer" */
187 memset (&state, '\0', sizeof (mbstate_t));
189 wc = (wchar_t) 0xBADFACE;
190 ret = mbrtowc (&wc, input, 1, &state);
191 ASSERT (ret == 1);
192 ASSERT (wc == 'B');
193 ASSERT (mbsinit (&state));
194 input[0] = '\0';
196 wc = (wchar_t) 0xBADFACE;
197 ret = mbrtowc (&wc, input + 1, 1, &state);
198 ASSERT (ret == (size_t)(-2));
199 ASSERT (wc == (wchar_t) 0xBADFACE);
200 ASSERT (!mbsinit (&state));
201 input[1] = '\0';
203 wc = (wchar_t) 0xBADFACE;
204 ret = mbrtowc (&wc, input + 2, 5, &state);
205 ASSERT (ret == 1);
206 ASSERT (wctob (wc) == EOF);
207 ASSERT (mbsinit (&state));
208 input[2] = '\0';
210 /* Test support of NULL first argument. */
211 ret = mbrtowc (NULL, input + 3, 4, &state);
212 ASSERT (ret == 2);
213 ASSERT (mbsinit (&state));
215 wc = (wchar_t) 0xBADFACE;
216 ret = mbrtowc (&wc, input + 3, 4, &state);
217 ASSERT (ret == 2);
218 ASSERT (wctob (wc) == EOF);
219 ASSERT (mbsinit (&state));
220 input[3] = '\0';
221 input[4] = '\0';
223 wc = (wchar_t) 0xBADFACE;
224 ret = mbrtowc (&wc, input + 5, 2, &state);
225 ASSERT (ret == 1);
226 ASSERT (wc == 'e');
227 ASSERT (mbsinit (&state));
228 input[5] = '\0';
230 wc = (wchar_t) 0xBADFACE;
231 ret = mbrtowc (&wc, input + 6, 1, &state);
232 ASSERT (ret == 1);
233 ASSERT (wc == 'r');
234 ASSERT (mbsinit (&state));
236 return 0;
238 case '3':
239 /* Locale encoding is EUC-JP. */
241 char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
242 memset (&state, '\0', sizeof (mbstate_t));
244 wc = (wchar_t) 0xBADFACE;
245 ret = mbrtowc (&wc, input, 1, &state);
246 ASSERT (ret == 1);
247 ASSERT (wc == '<');
248 ASSERT (mbsinit (&state));
249 input[0] = '\0';
251 wc = (wchar_t) 0xBADFACE;
252 ret = mbrtowc (&wc, input + 1, 2, &state);
253 ASSERT (ret == 2);
254 ASSERT (wctob (wc) == EOF);
255 ASSERT (mbsinit (&state));
256 input[1] = '\0';
257 input[2] = '\0';
259 wc = (wchar_t) 0xBADFACE;
260 ret = mbrtowc (&wc, input + 3, 1, &state);
261 ASSERT (ret == (size_t)(-2));
262 ASSERT (wc == (wchar_t) 0xBADFACE);
263 ASSERT (!mbsinit (&state));
264 input[3] = '\0';
266 wc = (wchar_t) 0xBADFACE;
267 ret = mbrtowc (&wc, input + 4, 4, &state);
268 ASSERT (ret == 1);
269 ASSERT (wctob (wc) == EOF);
270 ASSERT (mbsinit (&state));
271 input[4] = '\0';
273 /* Test support of NULL first argument. */
274 ret = mbrtowc (NULL, input + 5, 3, &state);
275 ASSERT (ret == 2);
276 ASSERT (mbsinit (&state));
278 wc = (wchar_t) 0xBADFACE;
279 ret = mbrtowc (&wc, input + 5, 3, &state);
280 ASSERT (ret == 2);
281 ASSERT (wctob (wc) == EOF);
282 ASSERT (mbsinit (&state));
283 input[5] = '\0';
284 input[6] = '\0';
286 wc = (wchar_t) 0xBADFACE;
287 ret = mbrtowc (&wc, input + 7, 1, &state);
288 ASSERT (ret == 1);
289 ASSERT (wc == '>');
290 ASSERT (mbsinit (&state));
292 return 0;
294 case '4':
295 /* Locale encoding is GB18030. */
297 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
298 memset (&state, '\0', sizeof (mbstate_t));
300 wc = (wchar_t) 0xBADFACE;
301 ret = mbrtowc (&wc, input, 1, &state);
302 ASSERT (ret == 1);
303 ASSERT (wc == 'B');
304 ASSERT (mbsinit (&state));
305 input[0] = '\0';
307 wc = (wchar_t) 0xBADFACE;
308 ret = mbrtowc (&wc, input + 1, 1, &state);
309 ASSERT (ret == (size_t)(-2));
310 ASSERT (wc == (wchar_t) 0xBADFACE);
311 ASSERT (!mbsinit (&state));
312 input[1] = '\0';
314 wc = (wchar_t) 0xBADFACE;
315 ret = mbrtowc (&wc, input + 2, 7, &state);
316 ASSERT (ret == 1);
317 ASSERT (wctob (wc) == EOF);
318 ASSERT (mbsinit (&state));
319 input[2] = '\0';
321 /* Test support of NULL first argument. */
322 ret = mbrtowc (NULL, input + 3, 6, &state);
323 ASSERT (ret == 4);
324 ASSERT (mbsinit (&state));
326 wc = (wchar_t) 0xBADFACE;
327 ret = mbrtowc (&wc, input + 3, 6, &state);
328 ASSERT (ret == 4);
329 ASSERT (wctob (wc) == EOF);
330 ASSERT (mbsinit (&state));
331 input[3] = '\0';
332 input[4] = '\0';
333 input[5] = '\0';
334 input[6] = '\0';
336 wc = (wchar_t) 0xBADFACE;
337 ret = mbrtowc (&wc, input + 7, 2, &state);
338 ASSERT (ret == 1);
339 ASSERT (wc == 'e');
340 ASSERT (mbsinit (&state));
341 input[5] = '\0';
343 wc = (wchar_t) 0xBADFACE;
344 ret = mbrtowc (&wc, input + 8, 1, &state);
345 ASSERT (ret == 1);
346 ASSERT (wc == 'r');
347 ASSERT (mbsinit (&state));
349 return 0;
351 case '5':
352 /* C locale; tested above. */
353 return 0;
356 return 1;