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. */
32 main (int argc
, char *argv
[])
36 for (mode
= '0'; mode
<= '4'; ++mode
)
46 for (i
= 0; i
< BUFSIZE
; i
++)
53 /* Locale encoding is POSIX */
54 printf("POSIX ...\n");
56 const char original
[] = "Buesser";
58 ret
= mbstowcs (input
, original
, 10);
61 for (n
= 0; n
< 10; n
++)
64 ret
= wcsrtombs (NULL
, &src
, n
, NULL
);
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);
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
;
82 ret
= wcsrtombs(buf
, &src
, BUFSIZE
, NULL
);
83 assert(ret
== (size_t)-1);
84 assert(src
== input
+ 2);
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");
100 ret
= mbstowcs (input
, original
, 10);
103 for (n
= 0; n
< 10; n
++)
106 ret
= wcsrtombs (NULL
, &src
, n
, NULL
);
108 assert(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);
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] == '_');
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");
136 ret
= mbstowcs (input
, original
, 10);
139 for (n
= 0; n
< 10; n
++)
142 ret
= wcsrtombs (NULL
, &src
, n
, NULL
);
144 assert(src
== input
);
147 ret
= wcsrtombs (buf
, &src
, n
, NULL
);
148 assert(ret
== (n
< 1 ? n
:
152 assert(src
== (n
< 1 ? input
+ n
:
155 n
<= 7 ? input
+ (n
- 2) : NULL
));
156 assert(memcmp (buf
, original
, ret
) == 0);
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
;
166 ret
= wcsrtombs(buf
, &src
, BUFSIZE
, NULL
);
167 assert(ret
== (size_t)-1);
168 assert(src
== input
+ 2);
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");
184 ret
= mbstowcs (input
, original
, 10);
187 for (n
= 0; n
< 10; n
++)
190 ret
= wcsrtombs (NULL
, &src
, n
, NULL
);
192 assert(src
== input
);
195 ret
= wcsrtombs (buf
, &src
, n
, NULL
);
196 assert(ret
== (n
< 1 ? n
:
201 assert(src
== (n
< 1 ? input
+ n
:
205 n
<= 8 ? input
+ (n
- 3) : NULL
));
206 assert(memcmp (buf
, original
, ret
) == 0);
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] == '_');
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");
229 ret
= mbstowcs (input
, original
, 10);
232 for (n
= 0; n
< 10; n
++)
235 ret
= wcsrtombs (NULL
, &src
, n
, NULL
);
237 assert(src
== input
);
240 ret
= wcsrtombs (buf
, &src
, n
, NULL
);
241 assert(ret
== (n
< 1 ? n
:
245 assert(src
== (n
< 1 ? input
+ n
:
248 n
<= 9 ? input
+ (n
- 4) : NULL
));
249 assert(memcmp (buf
, original
, ret
) == 0);
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] == '_');