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. */
31 main(int argc
, char *argv
[])
38 /* configure should already have checked that the locale is supported. */
39 if (setlocale(LC_ALL
, "") == NULL
) {
40 fprintf(stderr
, "unable to set standard locale\n");
44 /* Test NUL byte input. */
48 memset(&state
, '\0', sizeof(mbstate_t));
51 ret
= mbsnrtowcs(NULL
, &src
, 1, 0, &state
);
53 assert(mbsinit (&state
));
56 ret
= mbsnrtowcs(NULL
, &src
, 1, 1, &state
);
58 assert(mbsinit (&state
));
60 wc
= (wchar_t) 0xBADFACE;
62 ret
= mbsnrtowcs(&wc
, &src
, 1, 0, &state
);
64 assert(wc
== (wchar_t) 0xBADFACE);
65 assert(mbsinit (&state
));
67 wc
= (wchar_t) 0xBADFACE;
69 ret
= mbsnrtowcs(&wc
, &src
, 1, 1, &state
);
72 assert(mbsinit (&state
));
75 for (mode
= '1'; mode
<= '4'; ++mode
) {
77 for (unlimited
= 0; unlimited
< 2; unlimited
++) {
84 for (i
= 0; i
< BUFSIZE
; i
++)
85 buf
[i
] = (wchar_t) 0xBADFACE;
90 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
91 printf("ISO8859-1 ...\n");
93 char input
[] = "B\374\337er"; /* "Büßer" */
94 memset(&state
, '\0', sizeof(mbstate_t));
96 if (setlocale (LC_ALL
, "en_US.ISO8859-1") == NULL
) {
98 "unable to set ISO8859-1 locale, skipping\n");
102 wc
= (wchar_t) 0xBADFACE;
103 ret
= mbrtowc(&wc
, input
, 1, &state
);
106 assert(mbsinit (&state
));
109 wc
= (wchar_t) 0xBADFACE;
110 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
112 assert(wctob (wc
) == (unsigned char) '\374');
113 assert(mbsinit (&state
));
118 ret
= mbsnrtowcs(NULL
, &src
, 4, unlimited
? BUFSIZE
: 1,
121 assert(src
== input
+ 2);
122 assert(mbsinit (&state
));
125 ret
= mbsnrtowcs(buf
, &src
, 4, unlimited
? BUFSIZE
: 1,
127 assert(ret
== (unlimited
? 3 : 1));
128 assert(src
== (unlimited
? NULL
: input
+ 3));
129 assert(wctob (buf
[0]) == (unsigned char) '\337');
131 assert(buf
[1] == 'e');
132 assert(buf
[2] == 'r');
134 assert(buf
[4] == (wchar_t) 0xBADFACE);
136 assert(buf
[1] == (wchar_t) 0xBADFACE);
137 assert(mbsinit (&state
));
142 /* Locale encoding is UTF-8. */
143 printf("UTF-8 ...\n");
145 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
146 memset(&state
, '\0', sizeof(mbstate_t));
148 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
) {
150 "unable to set UTF-8 locale, skipping\n");
154 wc
= (wchar_t) 0xBADFACE;
155 ret
= mbrtowc(&wc
, input
, 1, &state
);
158 assert(mbsinit (&state
));
161 wc
= (wchar_t) 0xBADFACE;
162 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
163 assert(ret
== (size_t)(-2));
164 assert(wc
== (wchar_t) 0xBADFACE);
165 assert(!mbsinit (&state
));
168 // Copying mbstate_t doesn't really copy the ICU-converter's state, so this
169 // doesn't work on Haiku.
173 ret
= mbsnrtowcs(NULL
, &src
, 6, unlimited
? BUFSIZE
: 2,
176 assert(src
== input
+ 2);
177 assert(!mbsinit (&state
));
181 ret
= mbsnrtowcs(buf
, &src
, 6, unlimited
? BUFSIZE
: 2,
183 assert(ret
== (unlimited
? 4 : 2));
184 assert(src
== (unlimited
? NULL
: input
+ 5));
185 assert(wctob (buf
[0]) == EOF
);
186 assert(wctob (buf
[1]) == EOF
);
188 assert(buf
[2] == 'e');
189 assert(buf
[3] == 'r');
191 assert(buf
[5] == (wchar_t) 0xBADFACE);
193 assert(buf
[2] == (wchar_t) 0xBADFACE);
194 assert(mbsinit (&state
));
199 /* Locale encoding is EUC-JP. */
200 printf("EUC-JP ...\n");
202 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
203 memset(&state
, '\0', sizeof(mbstate_t));
205 if (setlocale (LC_ALL
, "en_US.EUC-JP") == NULL
) {
207 "unable to set EUC-JP locale, skipping\n");
211 wc
= (wchar_t) 0xBADFACE;
212 ret
= mbrtowc(&wc
, input
, 1, &state
);
215 assert(mbsinit (&state
));
218 wc
= (wchar_t) 0xBADFACE;
219 ret
= mbrtowc(&wc
, input
+ 1, 2, &state
);
221 assert(wctob (wc
) == EOF
);
222 assert(mbsinit (&state
));
226 wc
= (wchar_t) 0xBADFACE;
227 ret
= mbrtowc(&wc
, input
+ 3, 1, &state
);
228 assert(ret
== (size_t)(-2));
229 assert(wc
== (wchar_t) 0xBADFACE);
230 assert(!mbsinit (&state
));
233 // Copying mbstate_t doesn't really copy the ICU-converter's state, so this
234 // doesn't work on Haiku.
238 ret
= mbsnrtowcs(NULL
, &src
, 5, unlimited
? BUFSIZE
: 2,
241 assert(src
== input
+ 4);
242 assert(!mbsinit (&state
));
246 ret
= mbsnrtowcs(buf
, &src
, 5, unlimited
? BUFSIZE
: 2,
248 assert(ret
== (unlimited
? 3 : 2));
249 assert(src
== (unlimited
? NULL
: input
+ 7));
250 assert(wctob (buf
[0]) == EOF
);
251 assert(wctob (buf
[1]) == EOF
);
253 assert(buf
[2] == '>');
255 assert(buf
[4] == (wchar_t) 0xBADFACE);
257 assert(buf
[2] == (wchar_t) 0xBADFACE);
258 assert(mbsinit (&state
));
263 /* Locale encoding is GB18030. */
264 printf("GB18030 ...\n");
266 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
267 memset(&state
, '\0', sizeof(mbstate_t));
269 if (setlocale (LC_ALL
, "en_US.GB18030") == NULL
) {
271 "unable to set GB18030 locale, skipping\n");
275 wc
= (wchar_t) 0xBADFACE;
276 ret
= mbrtowc(&wc
, input
, 1, &state
);
279 assert(mbsinit (&state
));
282 wc
= (wchar_t) 0xBADFACE;
283 ret
= mbrtowc(&wc
, input
+ 1, 1, &state
);
284 assert(ret
== (size_t)(-2));
285 assert(wc
== (wchar_t) 0xBADFACE);
286 assert(!mbsinit (&state
));
289 // Copying mbstate_t doesn't really copy the ICU-converter's state, so this
290 // doesn't work on Haiku.
294 ret
= mbsnrtowcs(NULL
, &src
, 8, unlimited
? BUFSIZE
: 2,
297 assert(src
== input
+ 2);
298 assert(!mbsinit (&state
));
302 ret
= mbsnrtowcs(buf
, &src
, 8, unlimited
? BUFSIZE
: 2,
304 assert(ret
== (unlimited
? 4 : 2));
305 assert(src
== (unlimited
? NULL
: input
+ 7));
306 assert(wctob (buf
[0]) == EOF
);
307 assert(wctob (buf
[1]) == EOF
);
309 assert(buf
[2] == 'e');
310 assert(buf
[3] == 'r');
312 assert(buf
[5] == (wchar_t) 0xBADFACE);
314 assert(buf
[2] == (wchar_t) 0xBADFACE);
315 assert(mbsinit (&state
));