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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mbrtowc
, size_t, (wchar_t *, char const *, size_t,
34 main (int argc
, char *argv
[])
40 /* configure should already have checked that the locale is supported. */
41 if (setlocale (LC_ALL
, "") == NULL
)
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
);
60 ASSERT (mbsinit (&state
));
61 ret
= mbrtowc (NULL
, "", 1, &state
);
63 ASSERT (mbsinit (&state
));
66 /* Test single-byte input. */
71 memset (&state
, '\0', sizeof (mbstate_t));
72 for (c
= 0; c
< 0x100; c
++)
76 if (! (c
&& 1 < argc
&& argv
[1][0] == '5'))
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 '>':
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':
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. */
103 wc
= (wchar_t) 0xBADFACE;
104 ret
= mbrtowc (&wc
, buf
, 1, &state
);
107 /* c is an ASCII character. */
110 /* argv[1] starts with '5', that is, we are testing the C or POSIX
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
);
118 ASSERT (mbsinit (&state
));
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
);
129 ASSERT (wc
== (wchar_t) 0xBADFACE);
130 ASSERT (mbsinit (&state
));
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
);
146 ASSERT (mbsinit (&state
));
149 wc
= (wchar_t) 0xBADFACE;
150 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
152 ASSERT (wctob (wc
) == (unsigned char) '\374');
153 ASSERT (mbsinit (&state
));
156 /* Test support of NULL first argument. */
157 ret
= mbrtowc (NULL
, input
+ 2, 3, &state
);
159 ASSERT (mbsinit (&state
));
161 wc
= (wchar_t) 0xBADFACE;
162 ret
= mbrtowc (&wc
, input
+ 2, 3, &state
);
164 ASSERT (wctob (wc
) == (unsigned char) '\337');
165 ASSERT (mbsinit (&state
));
168 wc
= (wchar_t) 0xBADFACE;
169 ret
= mbrtowc (&wc
, input
+ 3, 2, &state
);
172 ASSERT (mbsinit (&state
));
175 wc
= (wchar_t) 0xBADFACE;
176 ret
= mbrtowc (&wc
, input
+ 4, 1, &state
);
179 ASSERT (mbsinit (&state
));
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
);
193 ASSERT (mbsinit (&state
));
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
));
203 wc
= (wchar_t) 0xBADFACE;
204 ret
= mbrtowc (&wc
, input
+ 2, 5, &state
);
206 ASSERT (wctob (wc
) == EOF
);
207 ASSERT (mbsinit (&state
));
210 /* Test support of NULL first argument. */
211 ret
= mbrtowc (NULL
, input
+ 3, 4, &state
);
213 ASSERT (mbsinit (&state
));
215 wc
= (wchar_t) 0xBADFACE;
216 ret
= mbrtowc (&wc
, input
+ 3, 4, &state
);
218 ASSERT (wctob (wc
) == EOF
);
219 ASSERT (mbsinit (&state
));
223 wc
= (wchar_t) 0xBADFACE;
224 ret
= mbrtowc (&wc
, input
+ 5, 2, &state
);
227 ASSERT (mbsinit (&state
));
230 wc
= (wchar_t) 0xBADFACE;
231 ret
= mbrtowc (&wc
, input
+ 6, 1, &state
);
234 ASSERT (mbsinit (&state
));
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
);
248 ASSERT (mbsinit (&state
));
251 wc
= (wchar_t) 0xBADFACE;
252 ret
= mbrtowc (&wc
, input
+ 1, 2, &state
);
254 ASSERT (wctob (wc
) == EOF
);
255 ASSERT (mbsinit (&state
));
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
));
266 wc
= (wchar_t) 0xBADFACE;
267 ret
= mbrtowc (&wc
, input
+ 4, 4, &state
);
269 ASSERT (wctob (wc
) == EOF
);
270 ASSERT (mbsinit (&state
));
273 /* Test support of NULL first argument. */
274 ret
= mbrtowc (NULL
, input
+ 5, 3, &state
);
276 ASSERT (mbsinit (&state
));
278 wc
= (wchar_t) 0xBADFACE;
279 ret
= mbrtowc (&wc
, input
+ 5, 3, &state
);
281 ASSERT (wctob (wc
) == EOF
);
282 ASSERT (mbsinit (&state
));
286 wc
= (wchar_t) 0xBADFACE;
287 ret
= mbrtowc (&wc
, input
+ 7, 1, &state
);
290 ASSERT (mbsinit (&state
));
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
);
304 ASSERT (mbsinit (&state
));
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
));
314 wc
= (wchar_t) 0xBADFACE;
315 ret
= mbrtowc (&wc
, input
+ 2, 7, &state
);
317 ASSERT (wctob (wc
) == EOF
);
318 ASSERT (mbsinit (&state
));
321 /* Test support of NULL first argument. */
322 ret
= mbrtowc (NULL
, input
+ 3, 6, &state
);
324 ASSERT (mbsinit (&state
));
326 wc
= (wchar_t) 0xBADFACE;
327 ret
= mbrtowc (&wc
, input
+ 3, 6, &state
);
329 ASSERT (wctob (wc
) == EOF
);
330 ASSERT (mbsinit (&state
));
336 wc
= (wchar_t) 0xBADFACE;
337 ret
= mbrtowc (&wc
, input
+ 7, 2, &state
);
340 ASSERT (mbsinit (&state
));
343 wc
= (wchar_t) 0xBADFACE;
344 ret
= mbrtowc (&wc
, input
+ 8, 1, &state
);
347 ASSERT (mbsinit (&state
));
352 /* C locale; tested above. */