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 (mbstowcs
, size_t, (wchar_t *, const char *, size_t));
34 main (int argc
, char *argv
[])
39 /* configure should already have checked that the locale is supported. */
40 if (setlocale (LC_ALL
, "") == NULL
)
43 /* Test NUL byte input. */
48 ret
= mbstowcs (NULL
, src
, 0);
52 ret
= mbstowcs (NULL
, src
, 1);
55 wc
= (wchar_t) 0xBADFACE;
57 ret
= mbstowcs (&wc
, src
, 0);
59 ASSERT (wc
== (wchar_t) 0xBADFACE);
61 wc
= (wchar_t) 0xBADFACE;
63 ret
= mbstowcs (&wc
, src
, 1);
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)
81 for (unlimited
= 0; unlimited
< 2; unlimited
++)
89 for (i
= 0; i
< BUFSIZE
; i
++)
90 buf
[i
] = (wchar_t) 0xBADFACE;
96 /* C or POSIX locale. */
101 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 1);
105 ret
= mbstowcs (buf
, src
, unlimited
? BUFSIZE
: 1);
106 ASSERT (ret
== (unlimited
? 3 : 1));
107 ASSERT (buf
[0] == 'n');
110 ASSERT (buf
[1] == '/');
111 ASSERT (buf
[2] == 'a');
112 ASSERT (buf
[3] == 0);
113 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
116 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
122 for (c
= 0; c
< 0x100; c
++)
125 /* We are testing all nonnull bytes. */
130 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 1);
133 buf
[0] = buf
[1] = (wchar_t) 0xBADFACE;
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." */
140 /* c is an ASCII character. */
141 ASSERT (buf
[0] == c
);
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
));
151 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
153 char input
[] = "B\374\337er"; /* "Büßer" */
156 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 1);
160 ret
= mbstowcs (buf
, src
, unlimited
? BUFSIZE
: 1);
161 ASSERT (ret
== (unlimited
? 4 : 1));
162 ASSERT (wctob (buf
[0]) == (unsigned char) '\374');
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);
172 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
177 /* Locale encoding is UTF-8. */
179 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
182 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 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
);
192 ASSERT (buf
[2] == 'e');
193 ASSERT (buf
[3] == 'r');
194 ASSERT (buf
[4] == 0);
195 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
198 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
203 /* Locale encoding is EUC-JP. */
205 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
208 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 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
);
219 ASSERT (buf
[3] == '>');
220 ASSERT (buf
[4] == 0);
221 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
224 ASSERT (buf
[3] == (wchar_t) 0xBADFACE);
229 /* Locale encoding is GB18030. */
231 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
234 ret
= mbstowcs (NULL
, src
, unlimited
? BUFSIZE
: 1);
238 ret
= mbstowcs (buf
, src
, unlimited
? BUFSIZE
: 1);
239 ASSERT (ret
== (unlimited
? 4 : 1));
240 ASSERT (wctob (buf
[0]) == EOF
);
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);
250 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
259 return test_exit_status
;