1 /* Test of conversion of multibyte character to wide character.
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. */
29 main (int argc
, char *argv
[])
36 /* configure should already have checked that the locale is supported. */
37 if (setlocale (LC_ALL
, "") == NULL
) {
38 fprintf(stderr
, "unable to set standard locale\n");
42 /* Test zero-length input. */
43 printf("zero-length input ...\n");
45 memset (&state
, '\0', sizeof (mbstate_t));
46 wc
= (wchar_t) 0xBADFACE;
47 ret
= mbrtowc (&wc
, "x", 0, &state
);
48 /* gnulib's implementation returns (size_t)(-2).
49 The AIX 5.1 implementation returns (size_t)(-1).
50 glibc's implementation returns 0. */
51 assert (ret
== (size_t)(-2) || ret
== (size_t)(-1) || ret
== 0);
52 assert (mbsinit (&state
));
55 /* Test NUL byte input. */
56 printf("NUL byte input ...\n");
58 memset (&state
, '\0', sizeof (mbstate_t));
59 wc
= (wchar_t) 0xBADFACE;
60 ret
= mbrtowc (&wc
, "", 1, &state
);
63 assert (mbsinit (&state
));
64 ret
= mbrtowc (NULL
, "", 1, &state
);
66 assert (mbsinit (&state
));
69 /* Test single-byte input. */
70 printf("single-byte input ...\n");
75 memset (&state
, '\0', sizeof (mbstate_t));
76 for (c
= 0; c
< 0x100; c
++)
79 case '\t': case '\v': case '\f':
80 case ' ': case '!': case '"': case '#': case '%':
81 case '&': case '\'': case '(': case ')': case '*':
82 case '+': case ',': case '-': case '.': case '/':
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 case ':': case ';': case '<': case '=': case '>':
87 case 'A': case 'B': case 'C': case 'D': case 'E':
88 case 'F': case 'G': case 'H': case 'I': case 'J':
89 case 'K': case 'L': case 'M': case 'N': case 'O':
90 case 'P': case 'Q': case 'R': case 'S': case 'T':
91 case 'U': case 'V': case 'W': case 'X': case 'Y':
93 case '[': case '\\': case ']': case '^': case '_':
94 case 'a': case 'b': case 'c': case 'd': case 'e':
95 case 'f': case 'g': case 'h': case 'i': case 'j':
96 case 'k': case 'l': case 'm': case 'n': case 'o':
97 case 'p': case 'q': case 'r': case 's': case 't':
98 case 'u': case 'v': case 'w': case 'x': case 'y':
99 case 'z': case '{': case '|': case '}': case '~':
100 /* c is in the ISO C "basic character set". */
102 wc
= (wchar_t) 0xBADFACE;
103 ret
= mbrtowc (&wc
, buf
, 1, &state
);
106 assert (mbsinit (&state
));
107 ret
= mbrtowc (NULL
, buf
, 1, &state
);
109 assert (mbsinit (&state
));
114 /* Test special calling convention, passing a NULL pointer. */
115 printf("special calling convention, passing NULL ...\n");
117 memset (&state
, '\0', sizeof (mbstate_t));
118 wc
= (wchar_t) 0xBADFACE;
119 ret
= mbrtowc (&wc
, NULL
, 5, &state
);
121 assert (wc
== (wchar_t) 0xBADFACE);
122 assert (mbsinit (&state
));
125 for (i
= '1'; i
<= '4'; ++i
) {
129 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
130 printf("ISO8859-1 ...\n");
132 char input
[] = "B\374\337er"; /* "Büßer" */
133 memset (&state
, '\0', sizeof (mbstate_t));
135 if (setlocale (LC_ALL
, "en_US.ISO8859-1") == NULL
) {
136 fprintf(stderr
, "unable to set ISO8859-1 locale, skipping\n");
140 wc
= (wchar_t) 0xBADFACE;
141 ret
= mbrtowc (&wc
, input
, 1, &state
);
144 assert (mbsinit (&state
));
147 wc
= (wchar_t) 0xBADFACE;
148 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
150 assert (wctob (wc
) == (unsigned char) '\374');
151 assert (mbsinit (&state
));
154 /* Test support of NULL first argument. */
155 ret
= mbrtowc (NULL
, input
+ 2, 3, &state
);
157 assert (mbsinit (&state
));
159 wc
= (wchar_t) 0xBADFACE;
160 ret
= mbrtowc (&wc
, input
+ 2, 3, &state
);
162 assert (wctob (wc
) == (unsigned char) '\337');
163 assert (mbsinit (&state
));
166 wc
= (wchar_t) 0xBADFACE;
167 ret
= mbrtowc (&wc
, input
+ 3, 2, &state
);
170 assert (mbsinit (&state
));
173 wc
= (wchar_t) 0xBADFACE;
174 ret
= mbrtowc (&wc
, input
+ 4, 1, &state
);
177 assert (mbsinit (&state
));
182 /* Locale encoding is UTF-8. */
183 printf("UTF-8 ...\n");
185 char input
[] = "B\303\274\303\237er"; /* "Büßer" */
186 memset (&state
, '\0', sizeof (mbstate_t));
188 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
) {
189 fprintf(stderr
, "unable to set UTF-8 locale, skipping\n");
193 wc
= (wchar_t) 0xBADFACE;
194 ret
= mbrtowc (&wc
, input
, 1, &state
);
197 assert (mbsinit (&state
));
200 wc
= (wchar_t) 0xBADFACE;
201 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
202 assert (ret
== (size_t)(-2));
203 assert (wc
== (wchar_t) 0xBADFACE);
204 assert (!mbsinit (&state
));
207 wc
= (wchar_t) 0xBADFACE;
208 ret
= mbrtowc (&wc
, input
+ 2, 5, &state
);
210 assert (wctob (wc
) == EOF
);
211 assert (mbsinit (&state
));
214 /* Test support of NULL first argument. */
215 ret
= mbrtowc (NULL
, input
+ 3, 4, &state
);
217 assert (mbsinit (&state
));
219 wc
= (wchar_t) 0xBADFACE;
220 ret
= mbrtowc (&wc
, input
+ 3, 4, &state
);
222 assert (wctob (wc
) == EOF
);
223 assert (mbsinit (&state
));
227 wc
= (wchar_t) 0xBADFACE;
228 ret
= mbrtowc (&wc
, input
+ 5, 2, &state
);
231 assert (mbsinit (&state
));
234 wc
= (wchar_t) 0xBADFACE;
235 ret
= mbrtowc (&wc
, input
+ 6, 1, &state
);
238 assert (mbsinit (&state
));
240 /* reproduce a valid use case from readline (as used in our bash): */
242 char tooShort
[] = "\303";
243 char ok
[] = "\303\274";
244 /* make a backup of the state */
245 mbstate_t stateBackup
= state
;
246 /* try with a source that's too short */
247 ret
= mbrtowc (&wc
, tooShort
, 1, &state
);
248 assert (ret
== (size_t)-2);
249 /* restore the state from the backup */
251 /* retry with enough source */
252 ret
= mbrtowc (&wc
, ok
, 2, &state
);
259 /* Locale encoding is EUC-JP. */
260 printf("EUC-JP ...\n");
262 char input
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
263 memset (&state
, '\0', sizeof (mbstate_t));
265 if (setlocale (LC_ALL
, "en_US.EUC-JP") == NULL
) {
266 fprintf(stderr
, "unable to set EUC-JP locale, skipping\n");
270 wc
= (wchar_t) 0xBADFACE;
271 ret
= mbrtowc (&wc
, input
, 1, &state
);
274 assert (mbsinit (&state
));
277 wc
= (wchar_t) 0xBADFACE;
278 ret
= mbrtowc (&wc
, input
+ 1, 2, &state
);
280 assert (wctob (wc
) == EOF
);
281 assert (mbsinit (&state
));
285 wc
= (wchar_t) 0xBADFACE;
286 ret
= mbrtowc (&wc
, input
+ 3, 1, &state
);
287 assert (ret
== (size_t)(-2));
288 assert (wc
== (wchar_t) 0xBADFACE);
289 assert (!mbsinit (&state
));
292 wc
= (wchar_t) 0xBADFACE;
293 ret
= mbrtowc (&wc
, input
+ 4, 4, &state
);
295 assert (wctob (wc
) == EOF
);
296 assert (mbsinit (&state
));
299 /* Test support of NULL first argument. */
300 ret
= mbrtowc (NULL
, input
+ 5, 3, &state
);
302 assert (mbsinit (&state
));
304 wc
= (wchar_t) 0xBADFACE;
305 ret
= mbrtowc (&wc
, input
+ 5, 3, &state
);
307 assert (wctob (wc
) == EOF
);
308 assert (mbsinit (&state
));
312 wc
= (wchar_t) 0xBADFACE;
313 ret
= mbrtowc (&wc
, input
+ 7, 1, &state
);
316 assert (mbsinit (&state
));
321 /* Locale encoding is GB18030. */
322 printf("GB18030 ...\n");
324 char input
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
325 memset (&state
, '\0', sizeof (mbstate_t));
327 if (setlocale (LC_ALL
, "en_US.GB18030") == NULL
) {
328 fprintf(stderr
, "unable to set GB18030 locale, skipping\n");
332 wc
= (wchar_t) 0xBADFACE;
333 ret
= mbrtowc (&wc
, input
, 1, &state
);
336 assert (mbsinit (&state
));
339 wc
= (wchar_t) 0xBADFACE;
340 ret
= mbrtowc (&wc
, input
+ 1, 1, &state
);
341 assert (ret
== (size_t)(-2));
342 assert (wc
== (wchar_t) 0xBADFACE);
343 assert (!mbsinit (&state
));
346 wc
= (wchar_t) 0xBADFACE;
347 ret
= mbrtowc (&wc
, input
+ 2, 7, &state
);
349 assert (wctob (wc
) == EOF
);
350 assert (mbsinit (&state
));
353 /* Test support of NULL first argument. */
354 ret
= mbrtowc (NULL
, input
+ 3, 6, &state
);
356 assert (mbsinit (&state
));
358 wc
= (wchar_t) 0xBADFACE;
359 ret
= mbrtowc (&wc
, input
+ 3, 6, &state
);
361 assert (wctob (wc
) == EOF
);
362 assert (mbsinit (&state
));
368 wc
= (wchar_t) 0xBADFACE;
369 ret
= mbrtowc (&wc
, input
+ 7, 2, &state
);
372 assert (mbsinit (&state
));
375 wc
= (wchar_t) 0xBADFACE;
376 ret
= mbrtowc (&wc
, input
+ 8, 1, &state
);
379 assert (mbsinit (&state
));