repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / tests / system / libroot / posix / gnulib-test-wcsrtombs.c
blob9a7154ee3d2f9b6cccf4606aca584bf942ca2c52
1 /* Test of conversion of wide string to 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. */
19 #undef NDEBUG
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
28 #define BUFSIZE 20
31 int
32 main (int argc, char *argv[])
34 int mode;
36 for (mode = '0'; mode <= '4'; ++mode)
38 wchar_t input[10];
39 size_t n;
40 const wchar_t *src;
41 char buf[BUFSIZE];
42 size_t ret;
45 size_t i;
46 for (i = 0; i < BUFSIZE; i++)
47 buf[i] = '_';
50 switch (mode)
52 case '0':
53 /* Locale encoding is POSIX */
54 printf("POSIX ...\n");
56 const char original[] = "Buesser";
58 ret = mbstowcs (input, original, 10);
59 assert(ret == 7);
61 for (n = 0; n < 10; n++)
63 src = input;
64 ret = wcsrtombs (NULL, &src, n, NULL);
65 assert(ret == 7);
66 assert(src == input);
68 src = input;
69 ret = wcsrtombs (buf, &src, n, NULL);
70 assert(ret == (n <= 7 ? n : 7));
71 assert(src == (n <= 7 ? input + n : NULL));
72 assert(memcmp (buf, original, ret) == 0);
73 if (src == NULL)
74 assert(buf[ret] == '\0');
75 assert(buf[ret + (src == NULL) + 0] == '_');
76 assert(buf[ret + (src == NULL) + 1] == '_');
77 assert(buf[ret + (src == NULL) + 2] == '_');
80 input[2] = 0xDEADBEEFul;
81 src = input;
82 ret = wcsrtombs(buf, &src, BUFSIZE, NULL);
83 assert(ret == (size_t)-1);
84 assert(src == input + 2);
86 break;
88 case '1':
89 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
90 printf("ISO8859-1 ...\n");
92 const char original[] = "B\374\337er"; /* "Büßer" */
94 if (setlocale (LC_ALL, "en_US.ISO8859-1") == NULL)
96 fprintf(stderr, "unable to set ISO8859-1 locale, skipping\n");
97 break;
100 ret = mbstowcs (input, original, 10);
101 assert(ret == 5);
103 for (n = 0; n < 10; n++)
105 src = input;
106 ret = wcsrtombs (NULL, &src, n, NULL);
107 assert(ret == 5);
108 assert(src == input);
110 src = input;
111 ret = wcsrtombs (buf, &src, n, NULL);
112 assert(ret == (n <= 5 ? n : 5));
113 assert(src == (n <= 5 ? input + n : NULL));
114 assert(memcmp (buf, original, ret) == 0);
115 if (src == NULL)
116 assert(buf[ret] == '\0');
117 assert(buf[ret + (src == NULL) + 0] == '_');
118 assert(buf[ret + (src == NULL) + 1] == '_');
119 assert(buf[ret + (src == NULL) + 2] == '_');
122 break;
124 case '2':
125 /* Locale encoding is UTF-8. */
126 printf("UTF-8 ... \n");
128 const char original[] = "B\303\274\303\237er"; /* "Büßer" */
130 if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
132 fprintf(stderr, "unable to set UTF-8 locale, skipping\n");
133 break;
136 ret = mbstowcs (input, original, 10);
137 assert(ret == 5);
139 for (n = 0; n < 10; n++)
141 src = input;
142 ret = wcsrtombs (NULL, &src, n, NULL);
143 assert(ret == 7);
144 assert(src == input);
146 src = input;
147 ret = wcsrtombs (buf, &src, n, NULL);
148 assert(ret == (n < 1 ? n :
149 n < 3 ? 1 :
150 n < 5 ? 3 :
151 n <= 7 ? n : 7));
152 assert(src == (n < 1 ? input + n :
153 n < 3 ? input + 1 :
154 n < 5 ? input + 2 :
155 n <= 7 ? input + (n - 2) : NULL));
156 assert(memcmp (buf, original, ret) == 0);
157 if (src == NULL)
158 assert(buf[ret] == '\0');
159 assert(buf[ret + (src == NULL) + 0] == '_');
160 assert(buf[ret + (src == NULL) + 1] == '_');
161 assert(buf[ret + (src == NULL) + 2] == '_');
164 input[2] = 0xDEADBEEFul;
165 src = input;
166 ret = wcsrtombs(buf, &src, BUFSIZE, NULL);
167 assert(ret == (size_t)-1);
168 assert(src == input + 2);
170 break;
172 case '3':
173 /* Locale encoding is EUC-JP. */
174 printf("EUC-JP ... \n");
176 const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
178 if (setlocale (LC_ALL, "en_US.EUC-JP") == NULL)
180 fprintf(stderr, "unable to set EUC-JP locale, skipping\n");
181 break;
184 ret = mbstowcs (input, original, 10);
185 assert(ret == 5);
187 for (n = 0; n < 10; n++)
189 src = input;
190 ret = wcsrtombs (NULL, &src, n, NULL);
191 assert(ret == 8);
192 assert(src == input);
194 src = input;
195 ret = wcsrtombs (buf, &src, n, NULL);
196 assert(ret == (n < 1 ? n :
197 n < 3 ? 1 :
198 n < 5 ? 3 :
199 n < 7 ? 5 :
200 n <= 8 ? n : 8));
201 assert(src == (n < 1 ? input + n :
202 n < 3 ? input + 1 :
203 n < 5 ? input + 2 :
204 n < 7 ? input + 3 :
205 n <= 8 ? input + (n - 3) : NULL));
206 assert(memcmp (buf, original, ret) == 0);
207 if (src == NULL)
208 assert(buf[ret] == '\0');
209 assert(buf[ret + (src == NULL) + 0] == '_');
210 assert(buf[ret + (src == NULL) + 1] == '_');
211 assert(buf[ret + (src == NULL) + 2] == '_');
214 break;
217 case '4':
218 printf("GB18030 ... \n");
219 /* Locale encoding is GB18030. */
221 const char original[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
223 if (setlocale (LC_ALL, "en_US.GB18030") == NULL)
225 fprintf(stderr, "unable to set GB18030 locale, skipping\n");
226 break;
229 ret = mbstowcs (input, original, 10);
230 assert(ret == 5);
232 for (n = 0; n < 10; n++)
234 src = input;
235 ret = wcsrtombs (NULL, &src, n, NULL);
236 assert(ret == 9);
237 assert(src == input);
239 src = input;
240 ret = wcsrtombs (buf, &src, n, NULL);
241 assert(ret == (n < 1 ? n :
242 n < 3 ? 1 :
243 n < 7 ? 3 :
244 n <= 9 ? n : 9));
245 assert(src == (n < 1 ? input + n :
246 n < 3 ? input + 1 :
247 n < 7 ? input + 2 :
248 n <= 9 ? input + (n - 4) : NULL));
249 assert(memcmp (buf, original, ret) == 0);
250 if (src == NULL)
251 assert(buf[ret] == '\0');
252 assert(buf[ret + (src == NULL) + 0] == '_');
253 assert(buf[ret + (src == NULL) + 1] == '_');
254 assert(buf[ret + (src == NULL) + 2] == '_');
257 break;
259 default:
260 return 1;
264 return 0;