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 (mbstoc32s
, size_t, (char32_t
*, const char *, size_t));
33 main (int argc
, char *argv
[])
38 /* configure should already have checked that the locale is supported. */
39 if (setlocale (LC_ALL
, "") == NULL
)
42 /* Test NUL byte input. */
47 ret
= mbstoc32s (NULL
, src
, 0);
51 ret
= mbstoc32s (NULL
, src
, 1);
54 wc
= (char32_t
) 0xBADFACE;
56 ret
= mbstoc32s (&wc
, src
, 0);
58 ASSERT (wc
== (char32_t
) 0xBADFACE);
60 wc
= (char32_t
) 0xBADFACE;
62 ret
= mbstoc32s (&wc
, src
, 1);
68 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
69 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
70 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
71 that is, effectively the "C.UTF-8" locale. */
72 if (argc
> 1 && strcmp (argv
[1], "1") == 0 && MB_CUR_MAX
> 1)
80 for (unlimited
= 0; unlimited
< 2; unlimited
++)
83 char32_t buf
[BUFSIZE
];
88 for (i
= 0; i
< BUFSIZE
; i
++)
89 buf
[i
] = (char32_t
) 0xBADFACE;
95 /* C or POSIX locale. */
100 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 1);
104 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 1);
105 ASSERT (ret
== (unlimited
? 3 : 1));
106 ASSERT (buf
[0] == 'n');
109 ASSERT (buf
[1] == '/');
110 ASSERT (buf
[2] == 'a');
111 ASSERT (buf
[3] == 0);
112 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
115 ASSERT (buf
[1] == (char32_t
) 0xBADFACE);
121 for (c
= 0; c
< 0x100; c
++)
124 /* We are testing all nonnull bytes. */
129 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 1);
132 buf
[0] = buf
[1] = (char32_t
) 0xBADFACE;
134 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 1);
135 /* POSIX:2018 says regarding mbstowcs: "In the POSIX locale an
136 [EILSEQ] error cannot occur since all byte values are valid
137 characters." It is reasonable to expect mbstoc32s to behave
141 /* c is an ASCII character. */
142 ASSERT (buf
[0] == c
);
144 /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
145 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
146 ASSERT (buf
[0] == (btoc32 (c
) == 0xDF00 + c
? btoc32 (c
) : c
));
152 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
154 char input
[] = "B\374\337er"; /* "Büßer" */
156 wc
= (char32_t
) 0xBADFACE;
157 ret
= mbstoc32s (&wc
, input
, 1);
162 wc
= (char32_t
) 0xBADFACE;
163 ret
= mbstoc32s (&wc
, input
+ 1, 1);
165 ASSERT (c32tob (wc
) == (unsigned char) '\374');
169 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 1);
173 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 1);
174 ASSERT (ret
== (unlimited
? 3 : 1));
175 ASSERT (c32tob (buf
[0]) == (unsigned char) '\337');
178 ASSERT (buf
[1] == 'e');
179 ASSERT (buf
[2] == 'r');
180 ASSERT (buf
[3] == 0);
181 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
184 ASSERT (buf
[1] == (char32_t
) 0xBADFACE);
189 /* Locale encoding is UTF-8. */
191 char input
[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
193 wc
= (char32_t
) 0xBADFACE;
194 ret
= mbstoc32s (&wc
, input
, 1);
199 wc
= (char32_t
) 0xBADFACE;
200 ret
= mbstoc32s (&wc
, input
+ 1, 1);
202 ASSERT (wc
== 0x00FC); /* expect Unicode encoding */
207 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 2);
211 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 2);
212 ASSERT (ret
== (unlimited
? 3 : 2));
213 ASSERT (buf
[0] == 0x00DF); /* expect Unicode encoding */
214 ASSERT (buf
[1] == 0x1F60B); /* expect Unicode encoding */
217 ASSERT (buf
[2] == '!');
218 ASSERT (buf
[3] == 0);
219 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
222 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
227 /* Locale encoding is EUC-JP. */
229 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
231 wc
= (char32_t
) 0xBADFACE;
232 ret
= mbstoc32s (&wc
, input
, 1);
237 wc
= (char32_t
) 0xBADFACE;
238 ret
= mbstoc32s (&wc
, input
+ 1, 1);
240 ASSERT (c32tob (wc
) == EOF
);
245 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 2);
249 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 2);
250 ASSERT (ret
== (unlimited
? 3 : 2));
251 ASSERT (c32tob (buf
[0]) == EOF
);
252 ASSERT (c32tob (buf
[1]) == EOF
);
255 ASSERT (buf
[2] == '>');
256 ASSERT (buf
[3] == 0);
257 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
260 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
265 /* Locale encoding is GB18030. */
266 #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
267 if (test_exit_status
!= EXIT_SUCCESS
)
268 return test_exit_status
;
269 fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr
);
273 char input
[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
275 wc
= (char32_t
) 0xBADFACE;
276 ret
= mbstoc32s (&wc
, input
, 1);
281 wc
= (char32_t
) 0xBADFACE;
282 ret
= mbstoc32s (&wc
, input
+ 1, 1);
284 ASSERT (c32tob (wc
) == EOF
);
288 ret
= mbstoc32s (NULL
, src
, unlimited
? BUFSIZE
: 2);
292 ret
= mbstoc32s (buf
, src
, unlimited
? BUFSIZE
: 2);
293 ASSERT (ret
== (unlimited
? 3 : 2));
294 ASSERT (c32tob (buf
[0]) == EOF
);
295 ASSERT (c32tob (buf
[1]) == EOF
);
298 ASSERT (buf
[2] == '!');
299 ASSERT (buf
[3] == 0);
300 ASSERT (buf
[4] == (char32_t
) 0xBADFACE);
303 ASSERT (buf
[2] == (char32_t
) 0xBADFACE);
312 return test_exit_status
;