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. */
31 main (int argc
, char *argv
[])
35 /* configure should already have checked that the locale is supported. */
36 if (setlocale (LC_ALL
, "") == NULL
)
39 for (mode
= '1'; mode
<= '4'; ++mode
)
49 for (i
= 0; i
< BUFSIZE
; i
++)
56 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
57 printf("ISO8859-1 ...\n");
59 const char original
[] = "B\374\337er"; /* "Büßer" */
61 if (setlocale (LC_ALL
, "en_US.ISO8859-1") == NULL
)
63 fprintf(stderr
, "unable to set ISO8859-1 locale, skipping\n");
67 ret
= mbstowcs (input
, original
, 10);
70 for (n
= 0; n
< 10; n
++)
73 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
78 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
79 assert(ret
== (n
<= 5 ? n
: 5));
80 assert(src
== (n
<= 5 ? input
+ n
: NULL
));
81 assert(memcmp (buf
, original
, ret
) == 0);
83 assert(buf
[ret
] == '\0');
84 assert(buf
[ret
+ (src
== NULL
) + 0] == '_');
85 assert(buf
[ret
+ (src
== NULL
) + 1] == '_');
86 assert(buf
[ret
+ (src
== NULL
) + 2] == '_');
92 /* Locale encoding is UTF-8. */
93 printf("UTF-8 ... \n");
95 const char original
[] = "B\303\274\303\237er"; /* "Büßer" */
97 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
)
99 fprintf(stderr
, "unable to set UTF-8 locale, skipping\n");
103 ret
= mbstowcs (input
, original
, 10);
106 for (n
= 0; n
< 10; n
++)
109 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
111 assert(src
== input
);
114 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
115 assert(ret
== (n
< 1 ? n
:
119 assert(src
== (n
< 1 ? input
+ n
:
122 n
<= 7 ? input
+ (n
- 2) : NULL
));
123 assert(memcmp (buf
, original
, ret
) == 0);
125 assert(buf
[ret
] == '\0');
126 assert(buf
[ret
+ (src
== NULL
) + 0] == '_');
127 assert(buf
[ret
+ (src
== NULL
) + 1] == '_');
128 assert(buf
[ret
+ (src
== NULL
) + 2] == '_');
134 /* Locale encoding is EUC-JP. */
135 printf("EUC-JP ... \n");
137 const char original
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
139 if (setlocale (LC_ALL
, "en_US.EUC-JP") == NULL
)
141 fprintf(stderr
, "unable to set EUC-JP locale, skipping\n");
145 ret
= mbstowcs (input
, original
, 10);
148 for (n
= 0; n
< 10; n
++)
151 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
153 assert(src
== input
);
156 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
157 assert(ret
== (n
< 1 ? n
:
162 assert(src
== (n
< 1 ? input
+ n
:
166 n
<= 8 ? input
+ (n
- 3) : NULL
));
167 assert(memcmp (buf
, original
, ret
) == 0);
169 assert(buf
[ret
] == '\0');
170 assert(buf
[ret
+ (src
== NULL
) + 0] == '_');
171 assert(buf
[ret
+ (src
== NULL
) + 1] == '_');
172 assert(buf
[ret
+ (src
== NULL
) + 2] == '_');
179 /* Locale encoding is GB18030. */
180 printf("GB18030 ... \n");
182 const char original
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
184 if (setlocale (LC_ALL
, "en_US.GB18030") == NULL
)
186 fprintf(stderr
, "unable to set GB18030 locale, skipping\n");
190 ret
= mbstowcs (input
, original
, 10);
193 for (n
= 0; n
< 10; n
++)
196 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
198 assert(src
== input
);
201 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
202 assert(ret
== (n
< 1 ? n
:
206 assert(src
== (n
< 1 ? input
+ n
:
209 n
<= 9 ? input
+ (n
- 4) : NULL
));
210 assert(memcmp (buf
, original
, ret
) == 0);
212 assert(buf
[ret
] == '\0');
213 assert(buf
[ret
+ (src
== NULL
) + 0] == '_');
214 assert(buf
[ret
+ (src
== NULL
) + 1] == '_');
215 assert(buf
[ret
+ (src
== NULL
) + 2] == '_');