1 /* Test of conversion of multibyte character to wide character.
2 Copyright (C) 2008-2025 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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mbrtowc
, size_t, (wchar_t *, char const *, size_t,
35 main (int argc
, char *argv
[])
41 /* configure should already have checked that the locale is supported. */
42 if (setlocale (LC_ALL
, "") == NULL
)
45 /* Test zero-length input. */
47 memset (&state
, '\0', sizeof (mbstate_t));
48 wc
= (wchar_t) 0xBADFACE;
49 ret
= mbrtowc (&wc
, "x", 0, &state
);
50 ASSERT (ret
== (size_t)(-2));
51 ASSERT (mbsinit (&state
));
54 /* Test NUL byte input. */
56 memset (&state
, '\0', sizeof (mbstate_t));
57 wc
= (wchar_t) 0xBADFACE;
58 ret
= mbrtowc (&wc
, "", 1, &state
);
61 ASSERT (mbsinit (&state
));
62 ret
= mbrtowc (NULL
, "", 1, &state
);
64 ASSERT (mbsinit (&state
));
67 /* Test single-byte input. */
72 memset (&state
, '\0', sizeof (mbstate_t));
73 for (c
= 0; c
< 0x100; c
++)
76 case '\t': case '\v': case '\f':
77 case ' ': case '!': case '"': case '#': case '%':
78 case '&': case '\'': case '(': case ')': case '*':
79 case '+': case ',': case '-': case '.': case '/':
80 case '0': case '1': case '2': case '3': case '4':
81 case '5': case '6': case '7': case '8': case '9':
82 case ':': case ';': case '<': case '=': case '>':
84 case 'A': case 'B': case 'C': case 'D': case 'E':
85 case 'F': case 'G': case 'H': case 'I': case 'J':
86 case 'K': case 'L': case 'M': case 'N': case 'O':
87 case 'P': case 'Q': case 'R': case 'S': case 'T':
88 case 'U': case 'V': case 'W': case 'X': case 'Y':
90 case '[': case '\\': case ']': case '^': case '_':
91 case 'a': case 'b': case 'c': case 'd': case 'e':
92 case 'f': case 'g': case 'h': case 'i': case 'j':
93 case 'k': case 'l': case 'm': case 'n': case 'o':
94 case 'p': case 'q': case 'r': case 's': case 't':
95 case 'u': case 'v': case 'w': case 'x': case 'y':
96 case 'z': case '{': case '|': case '}': case '~':
97 /* c is in the ISO C "basic character set". */
99 /* c is an ASCII character. */
102 wc
= (wchar_t) 0xBADFACE;
103 ret
= mbrtowc (&wc
, buf
, 1, &state
);
106 ASSERT (mbsinit (&state
));
108 ret
= mbrtowc (NULL
, buf
, 1, &state
);
110 ASSERT (mbsinit (&state
));
118 /* Test special calling convention, passing a NULL pointer. */
120 memset (&state
, '\0', sizeof (mbstate_t));
121 wc
= (wchar_t) 0xBADFACE;
122 ret
= mbrtowc (&wc
, NULL
, 5, &state
);
124 ASSERT (wc
== (wchar_t) 0xBADFACE);
125 ASSERT (mbsinit (&state
));
129 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
130 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
131 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
132 that is, effectively the "C.UTF-8" locale. */
133 if (argc
> 1 && strcmp (argv
[1], "1") == 0 && MB_CUR_MAX
> 1)
141 /* C or POSIX locale. */
146 memset (&state
, '\0', sizeof (mbstate_t));
147 for (c
= 0; c
< 0x100; c
++)
150 /* We are testing all nonnull bytes. */
153 wc
= (wchar_t) 0xBADFACE;
154 ret
= mbrtowc (&wc
, buf
, 1, &state
);
155 /* POSIX:2018 says: "In the POSIX locale an [EILSEQ] error
156 cannot occur since all byte values are valid characters." */
159 /* c is an ASCII character. */
162 /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
163 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
164 ASSERT (wc
== (btowc (c
) == 0xDF00 + c
? btowc (c
) : c
));
165 ASSERT (mbsinit (&state
));
167 ret
= mbrtowc (NULL
, buf
, 1, &state
);
169 ASSERT (mbsinit (&state
));
172 return test_exit_status
;
175 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
177 char input
[] = "B\374\337er"; /* "Büßer" */
178 memset (&state
, '\0', sizeof (mbstate_t));
180 wc
= (wchar_t) 0xBADFACE;
181 ret
= mbrtowc (&wc
, input
, 1, &state
);
184 ASSERT (mbsinit (&state
));
187 wc
= (wchar_t) 0xBADFACE;
188 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
190 ASSERT (wctob (wc
) == (unsigned char) '\374');
191 ASSERT (mbsinit (&state
));
194 /* Test support of NULL first argument. */
195 ret
= mbrtowc (NULL
, input
+ 2, 3, &state
);
197 ASSERT (mbsinit (&state
));
199 wc
= (wchar_t) 0xBADFACE;
200 ret
= mbrtowc (&wc
, input
+ 2, 3, &state
);
202 ASSERT (wctob (wc
) == (unsigned char) '\337');
203 ASSERT (mbsinit (&state
));
206 wc
= (wchar_t) 0xBADFACE;
207 ret
= mbrtowc (&wc
, input
+ 3, 2, &state
);
210 ASSERT (mbsinit (&state
));
213 wc
= (wchar_t) 0xBADFACE;
214 ret
= mbrtowc (&wc
, input
+ 4, 1, &state
);
217 ASSERT (mbsinit (&state
));
219 return test_exit_status
;
222 /* Locale encoding is UTF-8. */
224 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
225 memset (&state
, '\0', sizeof (mbstate_t));
227 wc
= (wchar_t) 0xBADFACE;
228 ret
= mbrtowc (&wc
, input
, 1, &state
);
231 ASSERT (mbsinit (&state
));
234 wc
= (wchar_t) 0xBADFACE;
235 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
236 ASSERT (ret
== (size_t)(-2));
237 ASSERT (wc
== (wchar_t) 0xBADFACE);
238 ASSERT (!mbsinit (&state
));
241 wc
= (wchar_t) 0xBADFACE;
242 ret
= mbrtowc (&wc
, input
+ 2, 5, &state
);
244 ASSERT (wctob (wc
) == EOF
);
245 ASSERT (mbsinit (&state
));
248 /* Test support of NULL first argument. */
249 ret
= mbrtowc (NULL
, input
+ 3, 4, &state
);
251 ASSERT (mbsinit (&state
));
253 wc
= (wchar_t) 0xBADFACE;
254 ret
= mbrtowc (&wc
, input
+ 3, 4, &state
);
256 ASSERT (wctob (wc
) == EOF
);
257 ASSERT (mbsinit (&state
));
261 wc
= (wchar_t) 0xBADFACE;
262 ret
= mbrtowc (&wc
, input
+ 5, 2, &state
);
265 ASSERT (mbsinit (&state
));
268 wc
= (wchar_t) 0xBADFACE;
269 ret
= mbrtowc (&wc
, input
+ 6, 1, &state
);
272 ASSERT (mbsinit (&state
));
274 if (sizeof (wchar_t) > 2)
275 { /* \360\237\220\203 = U+0001F403 */
276 memset (&state
, '\0', sizeof (mbstate_t));
278 wc
= (wchar_t) 0xBADFACE;
279 ret
= mbrtowc (&wc
, "\360", 1, &state
);
280 ASSERT (ret
== (size_t)(-2));
281 ASSERT (wc
== (wchar_t) 0xBADFACE);
282 ASSERT (!mbsinit (&state
));
284 wc
= (wchar_t) 0xBADFACE;
285 ret
= mbrtowc (&wc
, "\237", 1, &state
);
286 ASSERT (ret
== (size_t)(-2));
287 ASSERT (wc
== (wchar_t) 0xBADFACE);
288 ASSERT (!mbsinit (&state
));
290 wc
= (wchar_t) 0xBADFACE;
291 ret
= mbrtowc (&wc
, "\220", 1, &state
);
292 ASSERT (ret
== (size_t)(-2));
293 ASSERT (wc
== (wchar_t) 0xBADFACE);
294 ASSERT (!mbsinit (&state
));
296 wc
= (wchar_t) 0xBADFACE;
297 ret
= mbrtowc (&wc
, "\203", 1, &state
);
299 ASSERT (wctob (wc
) == EOF
);
300 ASSERT (mbsinit (&state
));
302 return test_exit_status
;
305 /* Locale encoding is EUC-JP. */
307 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
308 memset (&state
, '\0', sizeof (mbstate_t));
310 wc
= (wchar_t) 0xBADFACE;
311 ret
= mbrtowc (&wc
, input
, 1, &state
);
314 ASSERT (mbsinit (&state
));
317 wc
= (wchar_t) 0xBADFACE;
318 ret
= mbrtowc (&wc
, input
+ 1, 2, &state
);
320 ASSERT (wctob (wc
) == EOF
);
321 ASSERT (mbsinit (&state
));
325 wc
= (wchar_t) 0xBADFACE;
326 ret
= mbrtowc (&wc
, input
+ 3, 1, &state
);
327 ASSERT (ret
== (size_t)(-2));
328 ASSERT (wc
== (wchar_t) 0xBADFACE);
329 ASSERT (!mbsinit (&state
));
332 wc
= (wchar_t) 0xBADFACE;
333 ret
= mbrtowc (&wc
, input
+ 4, 4, &state
);
335 ASSERT (wctob (wc
) == EOF
);
336 ASSERT (mbsinit (&state
));
339 /* Test support of NULL first argument. */
340 ret
= mbrtowc (NULL
, input
+ 5, 3, &state
);
342 ASSERT (mbsinit (&state
));
344 wc
= (wchar_t) 0xBADFACE;
345 ret
= mbrtowc (&wc
, input
+ 5, 3, &state
);
347 ASSERT (wctob (wc
) == EOF
);
348 ASSERT (mbsinit (&state
));
352 wc
= (wchar_t) 0xBADFACE;
353 ret
= mbrtowc (&wc
, input
+ 7, 1, &state
);
356 ASSERT (mbsinit (&state
));
358 return test_exit_status
;
361 /* Locale encoding is GB18030. */
363 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
364 memset (&state
, '\0', sizeof (mbstate_t));
366 wc
= (wchar_t) 0xBADFACE;
367 ret
= mbrtowc (&wc
, input
, 1, &state
);
370 ASSERT (mbsinit (&state
));
373 wc
= (wchar_t) 0xBADFACE;
374 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
375 ASSERT (ret
== (size_t)(-2));
376 ASSERT (wc
== (wchar_t) 0xBADFACE);
377 ASSERT (!mbsinit (&state
));
380 wc
= (wchar_t) 0xBADFACE;
381 ret
= mbrtowc (&wc
, input
+ 2, 7, &state
);
383 ASSERT (wctob (wc
) == EOF
);
384 ASSERT (mbsinit (&state
));
387 /* Test support of NULL first argument. */
388 ret
= mbrtowc (NULL
, input
+ 3, 6, &state
);
390 ASSERT (mbsinit (&state
));
392 wc
= (wchar_t) 0xBADFACE;
393 ret
= mbrtowc (&wc
, input
+ 3, 6, &state
);
395 ASSERT (wctob (wc
) == EOF
);
396 ASSERT (mbsinit (&state
));
402 wc
= (wchar_t) 0xBADFACE;
403 ret
= mbrtowc (&wc
, input
+ 7, 2, &state
);
406 ASSERT (mbsinit (&state
));
409 wc
= (wchar_t) 0xBADFACE;
410 ret
= mbrtowc (&wc
, input
+ 8, 1, &state
);
413 ASSERT (mbsinit (&state
));
415 if (sizeof (wchar_t) > 2)
416 { /* \224\071\311\067 = U+0001F403 */
417 memset (&state
, '\0', sizeof (mbstate_t));
419 wc
= (wchar_t) 0xBADFACE;
420 ret
= mbrtowc (&wc
, "\224", 1, &state
);
421 ASSERT (ret
== (size_t)(-2));
422 ASSERT (wc
== (wchar_t) 0xBADFACE);
423 ASSERT (!mbsinit (&state
));
425 wc
= (wchar_t) 0xBADFACE;
426 ret
= mbrtowc (&wc
, "\071", 1, &state
);
427 ASSERT (ret
== (size_t)(-2));
428 ASSERT (wc
== (wchar_t) 0xBADFACE);
429 ASSERT (!mbsinit (&state
));
431 wc
= (wchar_t) 0xBADFACE;
432 ret
= mbrtowc (&wc
, "\311", 1, &state
);
433 ASSERT (ret
== (size_t)(-2));
434 ASSERT (wc
== (wchar_t) 0xBADFACE);
435 ASSERT (!mbsinit (&state
));
437 wc
= (wchar_t) 0xBADFACE;
438 ret
= mbrtowc (&wc
, "\067", 1, &state
);
440 ASSERT (wctob (wc
) == EOF
);
441 ASSERT (mbsinit (&state
));
443 return test_exit_status
;