usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-mbstoc32s.c
blobc481dc3354464ce3a3e7dfbdf7b04a9387718c78
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. */
19 #include <config.h>
21 #include <uchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (mbstoc32s, size_t, (char32_t *, const char *, size_t));
26 #include <locale.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "macros.h"
32 int
33 main (int argc, char *argv[])
35 char32_t wc;
36 size_t ret;
38 /* configure should already have checked that the locale is supported. */
39 if (setlocale (LC_ALL, "") == NULL)
40 return 1;
42 /* Test NUL byte input. */
44 const char *src;
46 src = "";
47 ret = mbstoc32s (NULL, src, 0);
48 ASSERT (ret == 0);
50 src = "";
51 ret = mbstoc32s (NULL, src, 1);
52 ASSERT (ret == 0);
54 wc = (char32_t) 0xBADFACE;
55 src = "";
56 ret = mbstoc32s (&wc, src, 0);
57 ASSERT (ret == 0);
58 ASSERT (wc == (char32_t) 0xBADFACE);
60 wc = (char32_t) 0xBADFACE;
61 src = "";
62 ret = mbstoc32s (&wc, src, 1);
63 ASSERT (ret == 0);
64 ASSERT (wc == 0);
67 #ifdef __ANDROID__
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)
73 argv[1] = "3";
74 #endif
76 if (argc > 1)
78 int unlimited;
80 for (unlimited = 0; unlimited < 2; unlimited++)
82 #define BUFSIZE 10
83 char32_t buf[BUFSIZE];
84 const char *src;
87 size_t i;
88 for (i = 0; i < BUFSIZE; i++)
89 buf[i] = (char32_t) 0xBADFACE;
92 switch (argv[1][0])
94 case '1':
95 /* C or POSIX locale. */
97 char input[] = "n/a";
99 src = input;
100 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1);
101 ASSERT (ret == 3);
103 src = input;
104 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 1);
105 ASSERT (ret == (unlimited ? 3 : 1));
106 ASSERT (buf[0] == 'n');
107 if (unlimited)
109 ASSERT (buf[1] == '/');
110 ASSERT (buf[2] == 'a');
111 ASSERT (buf[3] == 0);
112 ASSERT (buf[4] == (char32_t) 0xBADFACE);
114 else
115 ASSERT (buf[1] == (char32_t) 0xBADFACE);
118 int c;
119 char input[2];
121 for (c = 0; c < 0x100; c++)
122 if (c != 0)
124 /* We are testing all nonnull bytes. */
125 input[0] = c;
126 input[1] = '\0';
128 src = input;
129 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1);
130 ASSERT (ret == 1);
132 buf[0] = buf[1] = (char32_t) 0xBADFACE;
133 src = input;
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
138 in the same way. */
139 ASSERT (ret == 1);
140 if (c < 0x80)
141 /* c is an ASCII character. */
142 ASSERT (buf[0] == c);
143 else
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));
149 break;
151 case '2':
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);
158 ASSERT (ret == 1);
159 ASSERT (wc == 'B');
160 input[0] = '\0';
162 wc = (char32_t) 0xBADFACE;
163 ret = mbstoc32s (&wc, input + 1, 1);
164 ASSERT (ret == 1);
165 ASSERT (c32tob (wc) == (unsigned char) '\374');
166 input[1] = '\0';
168 src = input + 2;
169 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1);
170 ASSERT (ret == 3);
172 src = input + 2;
173 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 1);
174 ASSERT (ret == (unlimited ? 3 : 1));
175 ASSERT (c32tob (buf[0]) == (unsigned char) '\337');
176 if (unlimited)
178 ASSERT (buf[1] == 'e');
179 ASSERT (buf[2] == 'r');
180 ASSERT (buf[3] == 0);
181 ASSERT (buf[4] == (char32_t) 0xBADFACE);
183 else
184 ASSERT (buf[1] == (char32_t) 0xBADFACE);
186 break;
188 case '3':
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);
195 ASSERT (ret == 1);
196 ASSERT (wc == 's');
197 input[0] = '\0';
199 wc = (char32_t) 0xBADFACE;
200 ret = mbstoc32s (&wc, input + 1, 1);
201 ASSERT (ret == 1);
202 ASSERT (wc == 0x00FC); /* expect Unicode encoding */
203 input[1] = '\0';
204 input[2] = '\0';
206 src = input + 3;
207 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
208 ASSERT (ret == 3);
210 src = input + 3;
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 */
215 if (unlimited)
217 ASSERT (buf[2] == '!');
218 ASSERT (buf[3] == 0);
219 ASSERT (buf[4] == (char32_t) 0xBADFACE);
221 else
222 ASSERT (buf[2] == (char32_t) 0xBADFACE);
224 break;
226 case '4':
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);
233 ASSERT (ret == 1);
234 ASSERT (wc == '<');
235 input[0] = '\0';
237 wc = (char32_t) 0xBADFACE;
238 ret = mbstoc32s (&wc, input + 1, 1);
239 ASSERT (ret == 1);
240 ASSERT (c32tob (wc) == EOF);
241 input[1] = '\0';
242 input[2] = '\0';
244 src = input + 3;
245 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
246 ASSERT (ret == 3);
248 src = input + 3;
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);
253 if (unlimited)
255 ASSERT (buf[2] == '>');
256 ASSERT (buf[3] == 0);
257 ASSERT (buf[4] == (char32_t) 0xBADFACE);
259 else
260 ASSERT (buf[2] == (char32_t) 0xBADFACE);
262 break;
264 case '5':
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);
270 return 77;
271 #endif
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);
277 ASSERT (ret == 1);
278 ASSERT (wc == 's');
279 input[0] = '\0';
281 wc = (char32_t) 0xBADFACE;
282 ret = mbstoc32s (&wc, input + 1, 1);
283 ASSERT (ret == 1);
284 ASSERT (c32tob (wc) == EOF);
285 input[1] = '\0';
287 src = input + 3;
288 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
289 ASSERT (ret == 3);
291 src = input + 3;
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);
296 if (unlimited)
298 ASSERT (buf[2] == '!');
299 ASSERT (buf[3] == 0);
300 ASSERT (buf[4] == (char32_t) 0xBADFACE);
302 else
303 ASSERT (buf[2] == (char32_t) 0xBADFACE);
305 break;
307 default:
308 return 1;
312 return test_exit_status;
315 return 1;