headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / posix / gnulib-test-mbrtowc.c
blob240d76928180874af5d4cf8259bb02e746eee996
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. */
19 #undef NDEBUG
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <wchar.h>
26 #include <Debug.h>
28 int
29 main (int argc, char *argv[])
31 mbstate_t state;
32 wchar_t wc;
33 size_t ret;
34 int i;
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");
39 return 1;
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);
61 assert (ret == 0);
62 assert (wc == 0);
63 assert (mbsinit (&state));
64 ret = mbrtowc (NULL, "", 1, &state);
65 assert (ret == 0);
66 assert (mbsinit (&state));
69 /* Test single-byte input. */
70 printf("single-byte input ...\n");
72 char buf[1];
73 int c;
75 memset (&state, '\0', sizeof (mbstate_t));
76 for (c = 0; c < 0x100; c++)
77 switch (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 '>':
86 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':
92 case 'Z':
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". */
101 buf[0] = c;
102 wc = (wchar_t) 0xBADFACE;
103 ret = mbrtowc (&wc, buf, 1, &state);
104 assert (ret == 1);
105 assert (wc == c);
106 assert (mbsinit (&state));
107 ret = mbrtowc (NULL, buf, 1, &state);
108 assert (ret == 1);
109 assert (mbsinit (&state));
110 break;
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);
120 assert (ret == 0);
121 assert (wc == (wchar_t) 0xBADFACE);
122 assert (mbsinit (&state));
125 for (i = '1'; i <= '4'; ++i) {
126 switch (i)
128 case '1':
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");
137 break;
140 wc = (wchar_t) 0xBADFACE;
141 ret = mbrtowc (&wc, input, 1, &state);
142 assert (ret == 1);
143 assert (wc == 'B');
144 assert (mbsinit (&state));
145 input[0] = '\0';
147 wc = (wchar_t) 0xBADFACE;
148 ret = mbrtowc (&wc, input + 1, 1, &state);
149 assert (ret == 1);
150 assert (wctob (wc) == (unsigned char) '\374');
151 assert (mbsinit (&state));
152 input[1] = '\0';
154 /* Test support of NULL first argument. */
155 ret = mbrtowc (NULL, input + 2, 3, &state);
156 assert (ret == 1);
157 assert (mbsinit (&state));
159 wc = (wchar_t) 0xBADFACE;
160 ret = mbrtowc (&wc, input + 2, 3, &state);
161 assert (ret == 1);
162 assert (wctob (wc) == (unsigned char) '\337');
163 assert (mbsinit (&state));
164 input[2] = '\0';
166 wc = (wchar_t) 0xBADFACE;
167 ret = mbrtowc (&wc, input + 3, 2, &state);
168 assert (ret == 1);
169 assert (wc == 'e');
170 assert (mbsinit (&state));
171 input[3] = '\0';
173 wc = (wchar_t) 0xBADFACE;
174 ret = mbrtowc (&wc, input + 4, 1, &state);
175 assert (ret == 1);
176 assert (wc == 'r');
177 assert (mbsinit (&state));
179 break;
181 case '2':
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");
190 break;
193 wc = (wchar_t) 0xBADFACE;
194 ret = mbrtowc (&wc, input, 1, &state);
195 assert (ret == 1);
196 assert (wc == 'B');
197 assert (mbsinit (&state));
198 input[0] = '\0';
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));
205 input[1] = '\0';
207 wc = (wchar_t) 0xBADFACE;
208 ret = mbrtowc (&wc, input + 2, 5, &state);
209 assert (ret == 1);
210 assert (wctob (wc) == EOF);
211 assert (mbsinit (&state));
212 input[2] = '\0';
214 /* Test support of NULL first argument. */
215 ret = mbrtowc (NULL, input + 3, 4, &state);
216 assert (ret == 2);
217 assert (mbsinit (&state));
219 wc = (wchar_t) 0xBADFACE;
220 ret = mbrtowc (&wc, input + 3, 4, &state);
221 assert (ret == 2);
222 assert (wctob (wc) == EOF);
223 assert (mbsinit (&state));
224 input[3] = '\0';
225 input[4] = '\0';
227 wc = (wchar_t) 0xBADFACE;
228 ret = mbrtowc (&wc, input + 5, 2, &state);
229 assert (ret == 1);
230 assert (wc == 'e');
231 assert (mbsinit (&state));
232 input[5] = '\0';
234 wc = (wchar_t) 0xBADFACE;
235 ret = mbrtowc (&wc, input + 6, 1, &state);
236 assert (ret == 1);
237 assert (wc == 'r');
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 */
250 state = stateBackup;
251 /* retry with enough source */
252 ret = mbrtowc (&wc, ok, 2, &state);
253 assert (ret == 2);
256 break;
258 case '3':
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");
267 break;
270 wc = (wchar_t) 0xBADFACE;
271 ret = mbrtowc (&wc, input, 1, &state);
272 assert (ret == 1);
273 assert (wc == '<');
274 assert (mbsinit (&state));
275 input[0] = '\0';
277 wc = (wchar_t) 0xBADFACE;
278 ret = mbrtowc (&wc, input + 1, 2, &state);
279 assert (ret == 2);
280 assert (wctob (wc) == EOF);
281 assert (mbsinit (&state));
282 input[1] = '\0';
283 input[2] = '\0';
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));
290 input[3] = '\0';
292 wc = (wchar_t) 0xBADFACE;
293 ret = mbrtowc (&wc, input + 4, 4, &state);
294 assert (ret == 1);
295 assert (wctob (wc) == EOF);
296 assert (mbsinit (&state));
297 input[4] = '\0';
299 /* Test support of NULL first argument. */
300 ret = mbrtowc (NULL, input + 5, 3, &state);
301 assert (ret == 2);
302 assert (mbsinit (&state));
304 wc = (wchar_t) 0xBADFACE;
305 ret = mbrtowc (&wc, input + 5, 3, &state);
306 assert (ret == 2);
307 assert (wctob (wc) == EOF);
308 assert (mbsinit (&state));
309 input[5] = '\0';
310 input[6] = '\0';
312 wc = (wchar_t) 0xBADFACE;
313 ret = mbrtowc (&wc, input + 7, 1, &state);
314 assert (ret == 1);
315 assert (wc == '>');
316 assert (mbsinit (&state));
318 break;
320 case '4':
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");
329 break;
332 wc = (wchar_t) 0xBADFACE;
333 ret = mbrtowc (&wc, input, 1, &state);
334 assert (ret == 1);
335 assert (wc == 'B');
336 assert (mbsinit (&state));
337 input[0] = '\0';
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));
344 input[1] = '\0';
346 wc = (wchar_t) 0xBADFACE;
347 ret = mbrtowc (&wc, input + 2, 7, &state);
348 assert (ret == 1);
349 assert (wctob (wc) == EOF);
350 assert (mbsinit (&state));
351 input[2] = '\0';
353 /* Test support of NULL first argument. */
354 ret = mbrtowc (NULL, input + 3, 6, &state);
355 assert (ret == 4);
356 assert (mbsinit (&state));
358 wc = (wchar_t) 0xBADFACE;
359 ret = mbrtowc (&wc, input + 3, 6, &state);
360 assert (ret == 4);
361 assert (wctob (wc) == EOF);
362 assert (mbsinit (&state));
363 input[3] = '\0';
364 input[4] = '\0';
365 input[5] = '\0';
366 input[6] = '\0';
368 wc = (wchar_t) 0xBADFACE;
369 ret = mbrtowc (&wc, input + 7, 2, &state);
370 assert (ret == 1);
371 assert (wc == 'e');
372 assert (mbsinit (&state));
373 input[5] = '\0';
375 wc = (wchar_t) 0xBADFACE;
376 ret = mbrtowc (&wc, input + 8, 1, &state);
377 assert (ret == 1);
378 assert (wc == 'r');
379 assert (mbsinit (&state));
381 break;
385 return 0;