1 /* Test of conversion of string to 32-bit 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 (mbsrtoc32s
, size_t,
25 (char32_t
*, const char **, size_t, mbstate_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 NUL byte input. */
49 memset (&state
, '\0', sizeof (mbstate_t));
52 ret
= mbsrtoc32s (NULL
, &src
, 0, &state
);
54 ASSERT (mbsinit (&state
));
57 ret
= mbsrtoc32s (NULL
, &src
, 1, &state
);
59 ASSERT (mbsinit (&state
));
61 wc
= (char32_t
) 0xBADFACE;
63 ret
= mbsrtoc32s (&wc
, &src
, 0, &state
);
65 ASSERT (wc
== (char32_t
) 0xBADFACE);
66 ASSERT (mbsinit (&state
));
68 wc
= (char32_t
) 0xBADFACE;
70 ret
= mbsrtoc32s (&wc
, &src
, 1, &state
);
73 ASSERT (mbsinit (&state
));
77 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
78 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
79 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
80 that is, effectively the "C.UTF-8" locale. */
81 if (argc
> 1 && strcmp (argv
[1], "1") == 0 && MB_CUR_MAX
> 1)
89 for (unlimited
= 0; unlimited
< 2; unlimited
++)
92 char32_t buf
[BUFSIZE
];
98 for (i
= 0; i
< BUFSIZE
; i
++)
99 buf
[i
] = (char32_t
) 0xBADFACE;
105 /* C or POSIX locale. */
107 char input
[] = "n/a";
108 memset (&state
, '\0', sizeof (mbstate_t));
112 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 1, &temp_state
);
114 ASSERT (src
== input
);
115 ASSERT (mbsinit (&state
));
118 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
119 ASSERT (ret
== (unlimited
? 3 : 1));
120 ASSERT (src
== (unlimited
? NULL
: input
+ 1));
121 ASSERT (buf
[0] == 'n');
124 ASSERT (buf
[1] == '/');
125 ASSERT (buf
[2] == 'a');
126 ASSERT (buf
[3] == 0);
127 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
130 ASSERT (buf
[1] == (char32_t
) 0xBADFACE);
131 ASSERT (mbsinit (&state
));
137 memset (&state
, '\0', sizeof (mbstate_t));
138 for (c
= 0; c
< 0x100; c
++)
141 /* We are testing all nonnull bytes. */
146 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 1, &state
);
148 ASSERT (src
== input
);
149 ASSERT (mbsinit (&state
));
151 buf
[0] = buf
[1] = (char32_t
) 0xBADFACE;
153 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
154 /* POSIX:2018 says regarding mbsrtowcs: "In the POSIX locale an
155 [EILSEQ] error cannot occur since all byte values are valid
156 characters." It is reasonable to expect mbsrtoc32s to behave
159 ASSERT (src
== (unlimited
? NULL
: input
+ 1));
161 /* c is an ASCII character. */
162 ASSERT (buf
[0] == c
);
164 /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
165 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
166 ASSERT (buf
[0] == (btoc32 (c
) == 0xDF00 + c
? btoc32 (c
) : c
));
167 ASSERT (mbsinit (&state
));
173 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
175 char input
[] = "B\374\337er"; /* "Büßer" */
176 memset (&state
, '\0', sizeof (mbstate_t));
178 wc
= (char32_t
) 0xBADFACE;
179 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
182 ASSERT (mbsinit (&state
));
185 wc
= (char32_t
) 0xBADFACE;
186 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
188 ASSERT (c32tob (wc
) == (unsigned char) '\374');
189 ASSERT (mbsinit (&state
));
194 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 1, &temp_state
);
196 ASSERT (src
== input
+ 2);
197 ASSERT (mbsinit (&state
));
200 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
201 ASSERT (ret
== (unlimited
? 3 : 1));
202 ASSERT (src
== (unlimited
? NULL
: input
+ 3));
203 ASSERT (c32tob (buf
[0]) == (unsigned char) '\337');
206 ASSERT (buf
[1] == 'e');
207 ASSERT (buf
[2] == 'r');
208 ASSERT (buf
[3] == 0);
209 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
212 ASSERT (buf
[1] == (char32_t
) 0xBADFACE);
213 ASSERT (mbsinit (&state
));
218 /* Locale encoding is UTF-8. */
220 char input
[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
221 memset (&state
, '\0', sizeof (mbstate_t));
223 wc
= (char32_t
) 0xBADFACE;
224 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
227 ASSERT (mbsinit (&state
));
230 wc
= (char32_t
) 0xBADFACE;
231 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
232 ASSERT (ret
== (size_t)(-2));
233 ASSERT (wc
== (char32_t
) 0xBADFACE);
234 ASSERT (!mbsinit (&state
));
239 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
241 ASSERT (src
== input
+ 2);
242 ASSERT (!mbsinit (&state
));
245 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
246 ASSERT (ret
== (unlimited
? 4 : 2));
247 ASSERT (src
== (unlimited
? NULL
: input
+ 5));
248 ASSERT (c32tob (buf
[0]) == EOF
);
249 ASSERT (c32tob (buf
[1]) == EOF
);
252 ASSERT (buf
[2] == 0x1F60B); /* expect Unicode encoding */
253 ASSERT (buf
[3] == '!');
254 ASSERT (buf
[4] == 0);
255 ASSERT (buf
[5] == (char32_t
) 0xBADFACE);
258 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
259 ASSERT (mbsinit (&state
));
264 /* Locale encoding is EUC-JP. */
266 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
267 memset (&state
, '\0', sizeof (mbstate_t));
269 wc
= (char32_t
) 0xBADFACE;
270 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
273 ASSERT (mbsinit (&state
));
276 wc
= (char32_t
) 0xBADFACE;
277 ret
= mbrtoc32 (&wc
, input
+ 1, 2, &state
);
279 ASSERT (c32tob (wc
) == EOF
);
280 ASSERT (mbsinit (&state
));
284 wc
= (char32_t
) 0xBADFACE;
285 ret
= mbrtoc32 (&wc
, input
+ 3, 1, &state
);
286 ASSERT (ret
== (size_t)(-2));
287 ASSERT (wc
== (char32_t
) 0xBADFACE);
288 ASSERT (!mbsinit (&state
));
293 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
295 ASSERT (src
== input
+ 4);
296 ASSERT (!mbsinit (&state
));
299 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
300 ASSERT (ret
== (unlimited
? 3 : 2));
301 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
302 ASSERT (c32tob (buf
[0]) == EOF
);
303 ASSERT (c32tob (buf
[1]) == EOF
);
306 ASSERT (buf
[2] == '>');
307 ASSERT (buf
[3] == 0);
308 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
311 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
312 ASSERT (mbsinit (&state
));
317 /* Locale encoding is GB18030. */
318 #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
319 if (test_exit_status
!= EXIT_SUCCESS
)
320 return test_exit_status
;
321 fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr
);
325 char input
[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
326 memset (&state
, '\0', sizeof (mbstate_t));
328 wc
= (char32_t
) 0xBADFACE;
329 ret
= mbrtoc32 (&wc
, input
, 1, &state
);
332 ASSERT (mbsinit (&state
));
335 wc
= (char32_t
) 0xBADFACE;
336 ret
= mbrtoc32 (&wc
, input
+ 1, 1, &state
);
337 ASSERT (ret
== (size_t)(-2));
338 ASSERT (wc
== (char32_t
) 0xBADFACE);
339 ASSERT (!mbsinit (&state
));
344 ret
= mbsrtoc32s (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
346 ASSERT (src
== input
+ 2);
347 ASSERT (!mbsinit (&state
));
350 ret
= mbsrtoc32s (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
351 ASSERT (ret
== (unlimited
? 4 : 2));
352 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
353 ASSERT (c32tob (buf
[0]) == EOF
);
354 ASSERT (c32tob (buf
[1]) == EOF
);
357 ASSERT (c32tob (buf
[2]) == EOF
);
358 ASSERT (buf
[3] == '!');
359 ASSERT (buf
[4] == 0);
360 ASSERT (buf
[5] == (char32_t
) 0xBADFACE);
363 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
364 ASSERT (mbsinit (&state
));
373 return test_exit_status
;