1 /* Test of conversion of string to wide string.
2 Copyright (C) 2008-2024 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 (mbsrtowcs
, 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 NUL byte input. */
48 memset (&state
, '\0', sizeof (mbstate_t));
51 ret
= mbsrtowcs (NULL
, &src
, 0, &state
);
53 ASSERT (mbsinit (&state
));
56 ret
= mbsrtowcs (NULL
, &src
, 1, &state
);
58 ASSERT (mbsinit (&state
));
60 wc
= (wchar_t) 0xBADFACE;
62 ret
= mbsrtowcs (&wc
, &src
, 0, &state
);
64 ASSERT (wc
== (wchar_t) 0xBADFACE);
65 ASSERT (mbsinit (&state
));
67 wc
= (wchar_t) 0xBADFACE;
69 ret
= mbsrtowcs (&wc
, &src
, 1, &state
);
72 ASSERT (mbsinit (&state
));
76 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
77 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
78 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
79 that is, effectively the "C.UTF-8" locale. */
80 if (argc
> 1 && strcmp (argv
[1], "1") == 0 && MB_CUR_MAX
> 1)
88 for (unlimited
= 0; unlimited
< 2; unlimited
++)
97 for (i
= 0; i
< BUFSIZE
; i
++)
98 buf
[i
] = (wchar_t) 0xBADFACE;
104 /* C or POSIX locale. */
106 char input
[] = "n/a";
107 memset (&state
, '\0', sizeof (mbstate_t));
111 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 1, &temp_state
);
113 ASSERT (src
== input
);
114 ASSERT (mbsinit (&state
));
117 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
118 ASSERT (ret
== (unlimited
? 3 : 1));
119 ASSERT (src
== (unlimited
? NULL
: input
+ 1));
120 ASSERT (buf
[0] == 'n');
123 ASSERT (buf
[1] == '/');
124 ASSERT (buf
[2] == 'a');
125 ASSERT (buf
[3] == 0);
126 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
129 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
130 ASSERT (mbsinit (&state
));
136 memset (&state
, '\0', sizeof (mbstate_t));
137 for (c
= 0; c
< 0x100; c
++)
140 /* We are testing all nonnull bytes. */
145 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 1, &state
);
147 ASSERT (src
== input
);
148 ASSERT (mbsinit (&state
));
150 buf
[0] = buf
[1] = (wchar_t) 0xBADFACE;
152 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
153 /* POSIX:2018 says: "In the POSIX locale an [EILSEQ] error
154 cannot occur since all byte values are valid characters." */
156 ASSERT (src
== (unlimited
? NULL
: input
+ 1));
158 /* c is an ASCII character. */
159 ASSERT (buf
[0] == c
);
161 /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
162 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
163 ASSERT (buf
[0] == (btowc (c
) == 0xDF00 + c
? btowc (c
) : c
));
164 ASSERT (mbsinit (&state
));
170 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
172 char input
[] = "B\374\337er"; /* "Büßer" */
173 memset (&state
, '\0', sizeof (mbstate_t));
175 wc
= (wchar_t) 0xBADFACE;
176 ret
= mbrtowc (&wc
, input
, 1, &state
);
179 ASSERT (mbsinit (&state
));
182 wc
= (wchar_t) 0xBADFACE;
183 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
185 ASSERT (wctob (wc
) == (unsigned char) '\374');
186 ASSERT (mbsinit (&state
));
191 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 1, &temp_state
);
193 ASSERT (src
== input
+ 2);
194 ASSERT (mbsinit (&state
));
197 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
198 ASSERT (ret
== (unlimited
? 3 : 1));
199 ASSERT (src
== (unlimited
? NULL
: input
+ 3));
200 ASSERT (wctob (buf
[0]) == (unsigned char) '\337');
203 ASSERT (buf
[1] == 'e');
204 ASSERT (buf
[2] == 'r');
205 ASSERT (buf
[3] == 0);
206 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
209 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
210 ASSERT (mbsinit (&state
));
215 /* Locale encoding is UTF-8. */
217 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
218 memset (&state
, '\0', sizeof (mbstate_t));
220 wc
= (wchar_t) 0xBADFACE;
221 ret
= mbrtowc (&wc
, input
, 1, &state
);
224 ASSERT (mbsinit (&state
));
227 wc
= (wchar_t) 0xBADFACE;
228 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
229 ASSERT (ret
== (size_t)(-2));
230 ASSERT (wc
== (wchar_t) 0xBADFACE);
231 ASSERT (!mbsinit (&state
));
236 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
238 ASSERT (src
== input
+ 2);
239 ASSERT (!mbsinit (&state
));
242 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
243 ASSERT (ret
== (unlimited
? 4 : 2));
244 ASSERT (src
== (unlimited
? NULL
: input
+ 5));
245 ASSERT (wctob (buf
[0]) == EOF
);
246 ASSERT (wctob (buf
[1]) == EOF
);
249 ASSERT (buf
[2] == 'e');
250 ASSERT (buf
[3] == 'r');
251 ASSERT (buf
[4] == 0);
252 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
255 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
256 ASSERT (mbsinit (&state
));
261 /* Locale encoding is EUC-JP. */
263 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
264 memset (&state
, '\0', sizeof (mbstate_t));
266 wc
= (wchar_t) 0xBADFACE;
267 ret
= mbrtowc (&wc
, input
, 1, &state
);
270 ASSERT (mbsinit (&state
));
273 wc
= (wchar_t) 0xBADFACE;
274 ret
= mbrtowc (&wc
, input
+ 1, 2, &state
);
276 ASSERT (wctob (wc
) == EOF
);
277 ASSERT (mbsinit (&state
));
281 wc
= (wchar_t) 0xBADFACE;
282 ret
= mbrtowc (&wc
, input
+ 3, 1, &state
);
283 ASSERT (ret
== (size_t)(-2));
284 ASSERT (wc
== (wchar_t) 0xBADFACE);
285 ASSERT (!mbsinit (&state
));
290 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
292 ASSERT (src
== input
+ 4);
293 ASSERT (!mbsinit (&state
));
296 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
297 ASSERT (ret
== (unlimited
? 3 : 2));
298 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
299 ASSERT (wctob (buf
[0]) == EOF
);
300 ASSERT (wctob (buf
[1]) == EOF
);
303 ASSERT (buf
[2] == '>');
304 ASSERT (buf
[3] == 0);
305 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
308 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
309 ASSERT (mbsinit (&state
));
314 /* Locale encoding is GB18030. */
316 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
317 memset (&state
, '\0', sizeof (mbstate_t));
319 wc
= (wchar_t) 0xBADFACE;
320 ret
= mbrtowc (&wc
, input
, 1, &state
);
323 ASSERT (mbsinit (&state
));
326 wc
= (wchar_t) 0xBADFACE;
327 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
328 ASSERT (ret
== (size_t)(-2));
329 ASSERT (wc
== (wchar_t) 0xBADFACE);
330 ASSERT (!mbsinit (&state
));
335 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
337 ASSERT (src
== input
+ 2);
338 ASSERT (!mbsinit (&state
));
341 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
342 ASSERT (ret
== (unlimited
? 4 : 2));
343 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
344 ASSERT (wctob (buf
[0]) == EOF
);
345 ASSERT (wctob (buf
[1]) == EOF
);
348 ASSERT (buf
[2] == 'e');
349 ASSERT (buf
[3] == 'r');
350 ASSERT (buf
[4] == 0);
351 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
354 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
355 ASSERT (mbsinit (&state
));
364 return test_exit_status
;