1 /* Test of conversion of string to wide string.
2 Copyright (C) 2008 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. */
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 main (int argc, char *argv[])
47 /* configure should already have checked that the locale is supported. */
48 if (setlocale (LC_ALL, "") == NULL)
51 /* Test NUL byte input. */
55 memset (&state, '\0', sizeof (mbstate_t));
58 ret = mbsrtowcs (NULL, &src, 0, &state);
60 ASSERT (mbsinit (&state));
63 ret = mbsrtowcs (NULL, &src, 1, &state);
65 ASSERT (mbsinit (&state));
67 wc = (wchar_t) 0xBADFACE;
69 ret = mbsrtowcs (&wc, &src, 0, &state);
71 ASSERT (wc == (wchar_t) 0xBADFACE);
72 ASSERT (mbsinit (&state));
74 wc = (wchar_t) 0xBADFACE;
76 ret = mbsrtowcs (&wc, &src, 1, &state);
79 ASSERT (mbsinit (&state));
86 for (unlimited = 0; unlimited < 2; unlimited++)
94 for (i = 0; i < BUFSIZE; i++)
95 buf[i] = (wchar_t) 0xBADFACE;
101 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
103 char input[] = "B\374\337er"; /* "Büßer" */
104 memset (&state, '\0', sizeof (mbstate_t));
106 wc = (wchar_t) 0xBADFACE;
107 ret = mbrtowc (&wc, input, 1, &state);
110 ASSERT (mbsinit (&state));
113 wc = (wchar_t) 0xBADFACE;
114 ret = mbrtowc (&wc, input + 1, 1, &state);
116 ASSERT (wctob (wc) == (unsigned char) '\374');
117 ASSERT (mbsinit (&state));
121 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 1, &state);
123 ASSERT (src == input + 2);
124 ASSERT (mbsinit (&state));
127 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 1, &state);
128 ASSERT (ret == (unlimited ? 3 : 1));
129 ASSERT (src == (unlimited ? NULL : input + 3));
130 ASSERT (wctob (buf[0]) == (unsigned char) '\337');
133 ASSERT (buf[1] == 'e');
134 ASSERT (buf[2] == 'r');
135 ASSERT (buf[3] == 0);
136 ASSERT (buf[4] == (wchar_t) 0xBADFACE);
139 ASSERT (buf[1] == (wchar_t) 0xBADFACE);
140 ASSERT (mbsinit (&state));
145 /* Locale encoding is UTF-8. */
147 char input[] = "B\303\274\303\237er"; /* "Büßer" */
148 memset (&state, '\0', sizeof (mbstate_t));
150 wc = (wchar_t) 0xBADFACE;
151 ret = mbrtowc (&wc, input, 1, &state);
154 ASSERT (mbsinit (&state));
157 wc = (wchar_t) 0xBADFACE;
158 ret = mbrtowc (&wc, input + 1, 1, &state);
159 ASSERT (ret == (size_t)(-2));
160 ASSERT (wc == (wchar_t) 0xBADFACE);
161 ASSERT (!mbsinit (&state));
165 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state);
167 ASSERT (src == input + 2);
168 ASSERT (!mbsinit (&state));
171 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
172 ASSERT (ret == (unlimited ? 4 : 2));
173 ASSERT (src == (unlimited ? NULL : input + 5));
174 ASSERT (wctob (buf[0]) == EOF);
175 ASSERT (wctob (buf[1]) == EOF);
178 ASSERT (buf[2] == 'e');
179 ASSERT (buf[3] == 'r');
180 ASSERT (buf[4] == 0);
181 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
184 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
185 ASSERT (mbsinit (&state));
190 /* Locale encoding is EUC-JP. */
192 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
193 memset (&state, '\0', sizeof (mbstate_t));
195 wc = (wchar_t) 0xBADFACE;
196 ret = mbrtowc (&wc, input, 1, &state);
199 ASSERT (mbsinit (&state));
202 wc = (wchar_t) 0xBADFACE;
203 ret = mbrtowc (&wc, input + 1, 1, &state);
204 ASSERT (ret == (size_t)(-2));
205 ASSERT (wc == (wchar_t) 0xBADFACE);
206 ASSERT (!mbsinit (&state));
210 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state);
212 ASSERT (src == input + 2);
213 ASSERT (!mbsinit (&state));
216 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
217 ASSERT (ret == (unlimited ? 4 : 2));
218 ASSERT (src == (unlimited ? NULL : input + 7));
219 ASSERT (wctob (buf[0]) == EOF);
220 ASSERT (wctob (buf[1]) == EOF);
223 ASSERT (buf[2] == 'e');
224 ASSERT (buf[3] == 'r');
225 ASSERT (buf[4] == 0);
226 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
229 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
230 ASSERT (mbsinit (&state));
235 /* Locale encoding is GB18030. */
237 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
238 memset (&state, '\0', sizeof (mbstate_t));
240 wc = (wchar_t) 0xBADFACE;
241 ret = mbrtowc (&wc, input, 1, &state);
244 ASSERT (mbsinit (&state));
247 wc = (wchar_t) 0xBADFACE;
248 ret = mbrtowc (&wc, input + 1, 1, &state);
249 ASSERT (ret == (size_t)(-2));
250 ASSERT (wc == (wchar_t) 0xBADFACE);
251 ASSERT (!mbsinit (&state));
255 ret = mbsrtowcs (NULL, &src, unlimited ? BUFSIZE : 2, &state);
257 ASSERT (src == input + 2);
258 ASSERT (!mbsinit (&state));
261 ret = mbsrtowcs (buf, &src, unlimited ? BUFSIZE : 2, &state);
262 ASSERT (ret == (unlimited ? 4 : 2));
263 ASSERT (src == (unlimited ? NULL : input + 7));
264 ASSERT (wctob (buf[0]) == EOF);
265 ASSERT (wctob (buf[1]) == EOF);
268 ASSERT (buf[2] == 'e');
269 ASSERT (buf[3] == 'r');
270 ASSERT (buf[4] == 0);
271 ASSERT (buf[5] == (wchar_t) 0xBADFACE);
274 ASSERT (buf[2] == (wchar_t) 0xBADFACE);
275 ASSERT (mbsinit (&state));