usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-mbstowcs.c
blobb057b3089233cceb3b89e59e7d27341e4d8ae1da
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. */
19 #include <config.h>
21 #include <stdlib.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (mbstowcs, size_t, (wchar_t *, const char *, size_t));
26 #include <locale.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <wchar.h>
31 #include "macros.h"
33 int
34 main (int argc, char *argv[])
36 wchar_t wc;
37 size_t ret;
39 /* configure should already have checked that the locale is supported. */
40 if (setlocale (LC_ALL, "") == NULL)
41 return 1;
43 /* Test NUL byte input. */
45 const char *src;
47 src = "";
48 ret = mbstowcs (NULL, src, 0);
49 ASSERT (ret == 0);
51 src = "";
52 ret = mbstowcs (NULL, src, 1);
53 ASSERT (ret == 0);
55 wc = (wchar_t) 0xBADFACE;
56 src = "";
57 ret = mbstowcs (&wc, src, 0);
58 ASSERT (ret == 0);
59 ASSERT (wc == (wchar_t) 0xBADFACE);
61 wc = (wchar_t) 0xBADFACE;
62 src = "";
63 ret = mbstowcs (&wc, src, 1);
64 ASSERT (ret == 0);
65 ASSERT (wc == 0);
68 #ifdef __ANDROID__
69 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
70 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
71 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
72 that is, effectively the "C.UTF-8" locale. */
73 if (argc > 1 && strcmp (argv[1], "1") == 0 && MB_CUR_MAX > 1)
74 argv[1] = "3";
75 #endif
77 if (argc > 1)
79 int unlimited;
81 for (unlimited = 0; unlimited < 2; unlimited++)
83 #define BUFSIZE 10
84 wchar_t buf[BUFSIZE];
85 const char *src;
88 size_t i;
89 for (i = 0; i < BUFSIZE; i++)
90 buf[i] = (wchar_t) 0xBADFACE;
93 switch (argv[1][0])
95 case '1':
96 /* C or POSIX locale. */
98 char input[] = "n/a";
100 src = input;
101 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
102 ASSERT (ret == 3);
104 src = input;
105 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
106 ASSERT (ret == (unlimited ? 3 : 1));
107 ASSERT (buf[0] == 'n');
108 if (unlimited)
110 ASSERT (buf[1] == '/');
111 ASSERT (buf[2] == 'a');
112 ASSERT (buf[3] == 0);
113 ASSERT (buf[4] == (wchar_t) 0xBADFACE);
115 else
116 ASSERT (buf[1] == (wchar_t) 0xBADFACE);
119 int c;
120 char input[2];
122 for (c = 0; c < 0x100; c++)
123 if (c != 0)
125 /* We are testing all nonnull bytes. */
126 input[0] = c;
127 input[1] = '\0';
129 src = input;
130 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
131 ASSERT (ret == 1);
133 buf[0] = buf[1] = (wchar_t) 0xBADFACE;
134 src = input;
135 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
136 /* POSIX:2018 says: "In the POSIX locale an [EILSEQ] error
137 cannot occur since all byte values are valid characters." */
138 ASSERT (ret == 1);
139 if (c < 0x80)
140 /* c is an ASCII character. */
141 ASSERT (buf[0] == c);
142 else
143 /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
144 But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
145 ASSERT (buf[0] == (btowc (c) == 0xDF00 + c ? btowc (c) : c));
148 break;
150 case '2':
151 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
153 char input[] = "B\374\337er"; /* "Büßer" */
155 src = input + 1;
156 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
157 ASSERT (ret == 4);
159 src = input + 1;
160 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
161 ASSERT (ret == (unlimited ? 4 : 1));
162 ASSERT (wctob (buf[0]) == (unsigned char) '\374');
163 if (unlimited)
165 ASSERT (wctob (buf[1]) == (unsigned char) '\337');
166 ASSERT (buf[2] == 'e');
167 ASSERT (buf[3] == 'r');
168 ASSERT (buf[4] == 0);
169 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
171 else
172 ASSERT (buf[1] == (wchar_t) 0xBADFACE);
174 break;
176 case '3':
177 /* Locale encoding is UTF-8. */
179 char input[] = "B\303\274\303\237er"; /* "Büßer" */
181 src = input + 1;
182 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
183 ASSERT (ret == 4);
185 src = input + 1;
186 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
187 ASSERT (ret == (unlimited ? 4 : 1));
188 ASSERT (wctob (buf[0]) == EOF);
189 ASSERT (wctob (buf[1]) == EOF);
190 if (unlimited)
192 ASSERT (buf[2] == 'e');
193 ASSERT (buf[3] == 'r');
194 ASSERT (buf[4] == 0);
195 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
197 else
198 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
200 break;
202 case '4':
203 /* Locale encoding is EUC-JP. */
205 char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
207 src = input + 1;
208 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
209 ASSERT (ret == 4);
211 src = input + 1;
212 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
213 ASSERT (ret == (unlimited ? 4 : 1));
214 ASSERT (wctob (buf[0]) == EOF);
215 ASSERT (wctob (buf[1]) == EOF);
216 ASSERT (wctob (buf[2]) == EOF);
217 if (unlimited)
219 ASSERT (buf[3] == '>');
220 ASSERT (buf[4] == 0);
221 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
223 else
224 ASSERT (buf[3] == (wchar_t) 0xBADFACE);
226 break;
228 case '5':
229 /* Locale encoding is GB18030. */
231 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
233 src = input + 1;
234 ret = mbstowcs (NULL, src, unlimited ? BUFSIZE : 1);
235 ASSERT (ret == 4);
237 src = input + 1;
238 ret = mbstowcs (buf, src, unlimited ? BUFSIZE : 1);
239 ASSERT (ret == (unlimited ? 4 : 1));
240 ASSERT (wctob (buf[0]) == EOF);
241 if (unlimited)
243 ASSERT (wctob (buf[1]) == EOF);
244 ASSERT (buf[2] == 'e');
245 ASSERT (buf[3] == 'r');
246 ASSERT (buf[4] == 0);
247 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
249 else
250 ASSERT (buf[1] == (wchar_t) 0xBADFACE);
252 break;
254 default:
255 return 1;
259 return test_exit_status;
262 return 1;