1 /* Test of conversion of string to wide string.
2 Copyright (C) 2008-2017 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mbsrtowcs
, size_t, (wchar_t *, char const **, size_t,
34 main (int argc
, char *argv
[])
40 /* configure should already have checked that the locale is supported. */
41 if (setlocale (LC_ALL
, "") == NULL
)
44 /* Test NUL byte input. */
48 memset (&state
, '\0', sizeof (mbstate_t));
51 ret
= mbsrtowcs (NULL
, &src
, 0, &state
);
53 ASSERT (mbsinit (&state
));
56 ret
= mbsrtowcs (NULL
, &src
, 1, &state
);
58 ASSERT (mbsinit (&state
));
60 wc
= (wchar_t) 0xBADFACE;
62 ret
= mbsrtowcs (&wc
, &src
, 0, &state
);
64 ASSERT (wc
== (wchar_t) 0xBADFACE);
65 ASSERT (mbsinit (&state
));
67 wc
= (wchar_t) 0xBADFACE;
69 ret
= mbsrtowcs (&wc
, &src
, 1, &state
);
72 ASSERT (mbsinit (&state
));
79 for (unlimited
= 0; unlimited
< 2; unlimited
++)
88 for (i
= 0; i
< BUFSIZE
; i
++)
89 buf
[i
] = (wchar_t) 0xBADFACE;
95 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
97 char input
[] = "B\374\337er"; /* "Büßer" */
98 memset (&state
, '\0', sizeof (mbstate_t));
100 wc
= (wchar_t) 0xBADFACE;
101 ret
= mbrtowc (&wc
, input
, 1, &state
);
104 ASSERT (mbsinit (&state
));
107 wc
= (wchar_t) 0xBADFACE;
108 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
110 ASSERT (wctob (wc
) == (unsigned char) '\374');
111 ASSERT (mbsinit (&state
));
116 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 1, &temp_state
);
118 ASSERT (src
== input
+ 2);
119 ASSERT (mbsinit (&state
));
122 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
123 ASSERT (ret
== (unlimited
? 3 : 1));
124 ASSERT (src
== (unlimited
? NULL
: input
+ 3));
125 ASSERT (wctob (buf
[0]) == (unsigned char) '\337');
128 ASSERT (buf
[1] == 'e');
129 ASSERT (buf
[2] == 'r');
130 ASSERT (buf
[3] == 0);
131 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
134 ASSERT (buf
[1] == (wchar_t) 0xBADFACE);
135 ASSERT (mbsinit (&state
));
140 /* Locale encoding is UTF-8. */
142 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
143 memset (&state
, '\0', sizeof (mbstate_t));
145 wc
= (wchar_t) 0xBADFACE;
146 ret
= mbrtowc (&wc
, input
, 1, &state
);
149 ASSERT (mbsinit (&state
));
152 wc
= (wchar_t) 0xBADFACE;
153 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
154 ASSERT (ret
== (size_t)(-2));
155 ASSERT (wc
== (wchar_t) 0xBADFACE);
156 ASSERT (!mbsinit (&state
));
161 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
163 ASSERT (src
== input
+ 2);
164 ASSERT (!mbsinit (&state
));
167 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
168 ASSERT (ret
== (unlimited
? 4 : 2));
169 ASSERT (src
== (unlimited
? NULL
: input
+ 5));
170 ASSERT (wctob (buf
[0]) == EOF
);
171 ASSERT (wctob (buf
[1]) == EOF
);
174 ASSERT (buf
[2] == 'e');
175 ASSERT (buf
[3] == 'r');
176 ASSERT (buf
[4] == 0);
177 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
180 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
181 ASSERT (mbsinit (&state
));
186 /* Locale encoding is EUC-JP. */
188 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
189 memset (&state
, '\0', sizeof (mbstate_t));
191 wc
= (wchar_t) 0xBADFACE;
192 ret
= mbrtowc (&wc
, input
, 1, &state
);
195 ASSERT (mbsinit (&state
));
198 wc
= (wchar_t) 0xBADFACE;
199 ret
= mbrtowc (&wc
, input
+ 1, 2, &state
);
201 ASSERT (wctob (wc
) == EOF
);
202 ASSERT (mbsinit (&state
));
206 wc
= (wchar_t) 0xBADFACE;
207 ret
= mbrtowc (&wc
, input
+ 3, 1, &state
);
208 ASSERT (ret
== (size_t)(-2));
209 ASSERT (wc
== (wchar_t) 0xBADFACE);
210 ASSERT (!mbsinit (&state
));
215 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
217 ASSERT (src
== input
+ 4);
218 ASSERT (!mbsinit (&state
));
221 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
222 ASSERT (ret
== (unlimited
? 3 : 2));
223 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
224 ASSERT (wctob (buf
[0]) == EOF
);
225 ASSERT (wctob (buf
[1]) == EOF
);
228 ASSERT (buf
[2] == '>');
229 ASSERT (buf
[3] == 0);
230 ASSERT (buf
[4] == (wchar_t) 0xBADFACE);
233 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
234 ASSERT (mbsinit (&state
));
239 /* Locale encoding is GB18030. */
241 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
242 memset (&state
, '\0', sizeof (mbstate_t));
244 wc
= (wchar_t) 0xBADFACE;
245 ret
= mbrtowc (&wc
, input
, 1, &state
);
248 ASSERT (mbsinit (&state
));
251 wc
= (wchar_t) 0xBADFACE;
252 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
253 ASSERT (ret
== (size_t)(-2));
254 ASSERT (wc
== (wchar_t) 0xBADFACE);
255 ASSERT (!mbsinit (&state
));
260 ret
= mbsrtowcs (NULL
, &src
, unlimited
? BUFSIZE
: 2, &temp_state
);
262 ASSERT (src
== input
+ 2);
263 ASSERT (!mbsinit (&state
));
266 ret
= mbsrtowcs (buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
267 ASSERT (ret
== (unlimited
? 4 : 2));
268 ASSERT (src
== (unlimited
? NULL
: input
+ 7));
269 ASSERT (wctob (buf
[0]) == EOF
);
270 ASSERT (wctob (buf
[1]) == EOF
);
273 ASSERT (buf
[2] == 'e');
274 ASSERT (buf
[3] == 'r');
275 ASSERT (buf
[4] == 0);
276 ASSERT (buf
[5] == (wchar_t) 0xBADFACE);
279 ASSERT (buf
[2] == (wchar_t) 0xBADFACE);
280 ASSERT (mbsinit (&state
));