1 /* Test of conversion of string to wide string.
2 Copyright (C) 2008-2011 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. */
30 int main(int argc
, char *argv
[])
40 printf("POSIX ...\n");
42 char input
[] = "Buesser";
43 char isoInput
[] = "B\374\337er"; /* "Büßer" */
45 memset(&state
, '\0', sizeof(mbstate_t));
49 for (i
= 0; i
< BUFSIZE
; i
++)
50 buf
[i
] = (wchar_t) 0xBADFACE;
53 wc
= (wchar_t) 0xBADFACE;
54 ret
= mbrtowc(&wc
, input
, 1, &state
);
57 assert(mbsinit (&state
));
60 wc
= (wchar_t) 0xBADFACE;
61 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
63 assert(wctob (wc
) == (unsigned char) 'u');
64 assert(mbsinit (&state
));
69 ret
= mbsrtowcs(NULL
, &src
, BUFSIZE
, &temp_state
);
71 assert(src
== input
+ 2);
72 assert(mbsinit (&state
));
75 ret
= mbsrtowcs(buf
, &src
, BUFSIZE
, &state
);
78 assert(wctob (buf
[0]) == (unsigned char) 'e');
79 assert(buf
[1] == 's');
80 assert(buf
[2] == 's');
81 assert(buf
[3] == 'e');
82 assert(buf
[4] == 'r');
84 assert(buf
[6] == (wchar_t) 0xBADFACE);
85 assert(mbsinit (&state
));
88 ret
= mbsrtowcs(buf
, &src
, BUFSIZE
, &state
);
89 assert(ret
== (size_t)-1);
90 assert(src
== isoInput
+ 1);
93 /* configure should already have checked that the locale is supported. */
94 if (setlocale(LC_ALL
, "") == NULL
) {
95 fprintf(stderr
, "unable to set standard locale\n");
99 /* Test NUL byte input. */
103 memset(&state
, '\0', sizeof(mbstate_t));
106 ret
= mbsrtowcs(NULL
, &src
, 0, &state
);
108 assert(mbsinit (&state
));
111 ret
= mbsrtowcs(NULL
, &src
, 1, &state
);
113 assert(mbsinit (&state
));
115 wc
= (wchar_t) 0xBADFACE;
117 ret
= mbsrtowcs(&wc
, &src
, 0, &state
);
119 assert(wc
== (wchar_t) 0xBADFACE);
120 assert(mbsinit (&state
));
122 wc
= (wchar_t) 0xBADFACE;
124 ret
= mbsrtowcs(&wc
, &src
, 1, &state
);
127 assert(mbsinit (&state
));
130 for (mode
= '1'; mode
<= '4'; ++mode
) {
132 for (unlimited
= 0; unlimited
< 2; unlimited
++) {
135 for (i
= 0; i
< BUFSIZE
; i
++)
136 buf
[i
] = (wchar_t) 0xBADFACE;
141 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
142 printf("ISO8859-1 ...\n");
144 char input
[] = "B\374\337er"; /* "Büßer" */
145 memset(&state
, '\0', sizeof(mbstate_t));
147 if (setlocale (LC_ALL
, "en_US.ISO8859-1") == NULL
) {
149 "unable to set ISO8859-1 locale, skipping\n");
153 wc
= (wchar_t) 0xBADFACE;
154 ret
= mbrtowc(&wc
, input
, 1, &state
);
157 assert(mbsinit (&state
));
160 wc
= (wchar_t) 0xBADFACE;
161 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
163 assert(wctob (wc
) == (unsigned char) '\374');
164 assert(mbsinit (&state
));
169 ret
= mbsrtowcs(NULL
, &src
, unlimited
? BUFSIZE
: 1,
172 assert(src
== input
+ 2);
173 assert(mbsinit (&state
));
176 ret
= mbsrtowcs(buf
, &src
, unlimited
? BUFSIZE
: 1, &state
);
177 assert(ret
== (unlimited
? 3u : 1u));
178 assert(src
== (unlimited
? NULL
: input
+ 3));
179 assert(wctob (buf
[0]) == (unsigned char) '\337');
181 assert(buf
[1] == 'e');
182 assert(buf
[2] == 'r');
184 assert(buf
[4] == (wchar_t) 0xBADFACE);
186 assert(buf
[1] == (wchar_t) 0xBADFACE);
187 assert(mbsinit (&state
));
192 /* Locale encoding is UTF-8. */
193 printf("UTF-8 ...\n");
195 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
196 char isoInput
[] = "B\374\337er"; /* "Büßer" */
197 memset(&state
, '\0', sizeof(mbstate_t));
199 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
) {
201 "unable to set UTF-8 locale, skipping\n");
205 wc
= (wchar_t) 0xBADFACE;
206 ret
= mbrtowc(&wc
, input
, 1, &state
);
209 assert(mbsinit (&state
));
212 wc
= (wchar_t) 0xBADFACE;
213 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
214 assert(ret
== (size_t)(-2));
215 assert(wc
== (wchar_t) 0xBADFACE);
216 assert(!mbsinit (&state
));
221 ret
= mbsrtowcs(NULL
, &src
, unlimited
? BUFSIZE
: 2,
224 assert(src
== input
+ 2);
225 assert(!mbsinit (&state
));
228 ret
= mbsrtowcs(buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
229 assert(ret
== (unlimited
? 4u : 2u));
230 assert(src
== (unlimited
? NULL
: input
+ 5));
231 assert(wctob (buf
[0]) == EOF
);
232 assert(wctob (buf
[1]) == EOF
);
234 assert(buf
[2] == 'e');
235 assert(buf
[3] == 'r');
237 assert(buf
[5] == (wchar_t) 0xBADFACE);
239 assert(buf
[2] == (wchar_t) 0xBADFACE);
240 assert(mbsinit (&state
));
243 ret
= mbsrtowcs(buf
, &src
, BUFSIZE
, &state
);
244 assert(ret
== (size_t)-1);
245 assert(src
== isoInput
+ 1);
250 /* Locale encoding is EUC-JP. */
251 printf("EUC-JP ...\n");
253 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
254 memset(&state
, '\0', sizeof(mbstate_t));
256 if (setlocale (LC_ALL
, "en_US.EUC-JP") == NULL
) {
258 "unable to set EUC-JP locale, skipping\n");
262 wc
= (wchar_t) 0xBADFACE;
263 ret
= mbrtowc(&wc
, input
, 1, &state
);
266 assert(mbsinit (&state
));
269 wc
= (wchar_t) 0xBADFACE;
270 ret
= mbrtowc(&wc
, input
+ 1, 2, &state
);
272 assert(wctob (wc
) == EOF
);
273 assert(mbsinit (&state
));
277 wc
= (wchar_t) 0xBADFACE;
278 ret
= mbrtowc(&wc
, input
+ 3, 1, &state
);
279 assert(ret
== (size_t)(-2));
280 assert(wc
== (wchar_t) 0xBADFACE);
281 assert(!mbsinit (&state
));
286 ret
= mbsrtowcs(NULL
, &src
, unlimited
? BUFSIZE
: 2,
289 assert(src
== input
+ 4);
290 assert(!mbsinit (&state
));
293 ret
= mbsrtowcs(buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
294 assert(ret
== (unlimited
? 3u : 2u));
295 assert(src
== (unlimited
? NULL
: input
+ 7));
296 assert(wctob (buf
[0]) == EOF
);
297 assert(wctob (buf
[1]) == EOF
);
299 assert(buf
[2] == '>');
301 assert(buf
[4] == (wchar_t) 0xBADFACE);
303 assert(buf
[2] == (wchar_t) 0xBADFACE);
304 assert(mbsinit (&state
));
309 /* Locale encoding is GB18030. */
310 printf("GB18030 ...\n");
312 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
313 memset(&state
, '\0', sizeof(mbstate_t));
315 if (setlocale (LC_ALL
, "en_US.GB18030") == NULL
) {
317 "unable to set GB18030 locale, skipping\n");
321 wc
= (wchar_t) 0xBADFACE;
322 ret
= mbrtowc(&wc
, input
, 1, &state
);
325 assert(mbsinit (&state
));
328 wc
= (wchar_t) 0xBADFACE;
329 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
330 assert(ret
== (size_t)(-2));
331 assert(wc
== (wchar_t) 0xBADFACE);
332 assert(!mbsinit (&state
));
337 ret
= mbsrtowcs(NULL
, &src
, unlimited
? BUFSIZE
: 2,
340 assert(src
== input
+ 2);
341 assert(!mbsinit (&state
));
344 ret
= mbsrtowcs(buf
, &src
, unlimited
? BUFSIZE
: 2, &state
);
345 assert(ret
== (unlimited
? 4u : 2u));
346 assert(src
== (unlimited
? NULL
: input
+ 7));
347 assert(wctob (buf
[0]) == EOF
);
348 assert(wctob (buf
[1]) == EOF
);
350 assert(buf
[2] == 'e');
351 assert(buf
[3] == 'r');
353 assert(buf
[5] == (wchar_t) 0xBADFACE);
355 assert(buf
[2] == (wchar_t) 0xBADFACE);
356 assert(mbsinit (&state
));