1 /* Test of conversion of string to wide string.
2 Copyright (C) 2008, 2009 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,
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 main (int argc, char *argv[])
51 /* configure should already have checked that the locale is supported. */
52 if (setlocale (LC_ALL, "") == NULL)
55 /* Test NUL byte input. */
59 memset (&state, '\0', sizeof (mbstate_t));
62 ret = mbsrtowcs (NULL, &src, 0, &state);
64 ASSERT (mbsinit (&state));
67 ret = mbsrtowcs (NULL, &src, 1, &state);
69 ASSERT (mbsinit (&state));
71 wc = (wchar_t) 0xBADFACE;
73 ret = mbsrtowcs (&wc, &src, 0, &state);
75 ASSERT (wc == (wchar_t) 0xBADFACE);
76 ASSERT (mbsinit (&state));
78 wc = (wchar_t) 0xBADFACE;
80 ret = mbsrtowcs (&wc, &src, 1, &state);
83 ASSERT (mbsinit (&state));
90 for (unlimited = 0; unlimited < 2; unlimited++)
99 for (i = 0; i < BUFSIZE; i++)
100 buf[i] = (wchar_t) 0xBADFACE;
106 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
108 char input[] = "B\374\337er"; /* "Büßer" */
109 memset (&state, '\0', sizeof (mbstate_t));
111 wc = (wchar_t) 0xBADFACE;
112 ret = mbrtowc (&wc, input, 1, &state);
115 ASSERT (mbsinit (&state));
118 wc = (wchar_t) 0xBADFACE;
119 ret = mbrtowc (&wc, input + 1, 1, &state);
121 ASSERT (wctob (wc) == (unsigned char) '\374');
122 ASSERT (mbsinit (&state));
127 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 1, &temp_state);
129 ASSERT (src == input + 2);
130 ASSERT (mbsinit (&state));
133 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 1, &state);
134 ASSERT (ret == (unlimited ? 3 : 1));
135 ASSERT (src == (unlimited ? NULL : input + 3));
136 ASSERT (wctob (buf[0]) == (unsigned char) '\337');
139 ASSERT (buf[1] == 'e');
140 ASSERT (buf[2] == 'r');
141 ASSERT (buf[3] == 0);
142 ASSERT (buf[4] == (wchar_t) 0xBADFACE);
145 ASSERT (buf[1] == (wchar_t) 0xBADFACE);
146 ASSERT (mbsinit (&state));
151 /* Locale encoding is UTF-8. */
153 char input[] = "B\303\274\303\237er"; /* "Büßer" */
154 memset (&state, '\0', sizeof (mbstate_t));
156 wc = (wchar_t) 0xBADFACE;
157 ret = mbrtowc (&wc, input, 1, &state);
160 ASSERT (mbsinit (&state));
163 wc = (wchar_t) 0xBADFACE;
164 ret = mbrtowc (&wc, input + 1, 1, &state);
165 ASSERT (ret == (size_t)(-2));
166 ASSERT (wc == (wchar_t) 0xBADFACE);
167 ASSERT (!mbsinit (&state));
172 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state);
174 ASSERT (src == input + 2);
175 ASSERT (!mbsinit (&state));
178 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
179 ASSERT (ret == (unlimited ? 4 : 2));
180 ASSERT (src == (unlimited ? NULL : input + 5));
181 ASSERT (wctob (buf[0]) == EOF);
182 ASSERT (wctob (buf[1]) == EOF);
185 ASSERT (buf[2] == 'e');
186 ASSERT (buf[3] == 'r');
187 ASSERT (buf[4] == 0);
188 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
191 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
192 ASSERT (mbsinit (&state));
197 /* Locale encoding is EUC-JP. */
199 char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
200 memset (&state, '\0', sizeof (mbstate_t));
202 wc = (wchar_t) 0xBADFACE;
203 ret = mbrtowc (&wc, input, 1, &state);
206 ASSERT (mbsinit (&state));
209 wc = (wchar_t) 0xBADFACE;
210 ret = mbrtowc (&wc, input + 1, 2, &state);
212 ASSERT (wctob (wc) == EOF);
213 ASSERT (mbsinit (&state));
217 wc = (wchar_t) 0xBADFACE;
218 ret = mbrtowc (&wc, input + 3, 1, &state);
219 ASSERT (ret == (size_t)(-2));
220 ASSERT (wc == (wchar_t) 0xBADFACE);
221 ASSERT (!mbsinit (&state));
226 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state);
228 ASSERT (src == input + 4);
229 ASSERT (!mbsinit (&state));
232 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
233 ASSERT (ret == (unlimited ? 3 : 2));
234 ASSERT (src == (unlimited ? NULL : input + 7));
235 ASSERT (wctob (buf[0]) == EOF);
236 ASSERT (wctob (buf[1]) == EOF);
239 ASSERT (buf[2] == '>');
240 ASSERT (buf[3] == 0);
241 ASSERT (buf[4] == (wchar_t) 0xBADFACE);
244 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
245 ASSERT (mbsinit (&state));
250 /* Locale encoding is GB18030. */
252 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
253 memset (&state, '\0', sizeof (mbstate_t));
255 wc = (wchar_t) 0xBADFACE;
256 ret = mbrtowc (&wc, input, 1, &state);
259 ASSERT (mbsinit (&state));
262 wc = (wchar_t) 0xBADFACE;
263 ret = mbrtowc (&wc, input + 1, 1, &state);
264 ASSERT (ret == (size_t)(-2));
265 ASSERT (wc == (wchar_t) 0xBADFACE);
266 ASSERT (!mbsinit (&state));
271 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &temp_state);
273 ASSERT (src == input + 2);
274 ASSERT (!mbsinit (&state));
277 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
278 ASSERT (ret == (unlimited ? 4 : 2));
279 ASSERT (src == (unlimited ? NULL : input + 7));
280 ASSERT (wctob (buf[0]) == EOF);
281 ASSERT (wctob (buf[1]) == EOF);
284 ASSERT (buf[2] == 'e');
285 ASSERT (buf[3] == 'r');
286 ASSERT (buf[4] == 0);
287 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
290 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
291 ASSERT (mbsinit (&state));