1 /* Test of character set conversion.
2 Copyright (C) 2007 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>, 2007. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 /* Assume that iconv() supports at least the encoding UTF-8. */
47 /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]". */
49 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
51 static const char input[] =
52 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
53 static const char expected[] =
54 "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
63 cd = iconv_open ("UTF-16BE", "UTF-8");
64 ASSERT (cd != (iconv_t)(-1));
67 inbytesleft = sizeof (input) - 1;
69 outbytesleft = sizeof (buf);
71 (ICONV_CONST char **) &inptr, &inbytesleft,
72 &outptr, &outbytesleft);
73 ASSERT (res == 0 && inbytesleft == 0);
74 ASSERT (outptr == buf + (sizeof (expected) - 1));
75 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
77 ASSERT (iconv_close (cd) == 0);
80 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
82 static const char input[] =
83 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
84 static const char expected[] =
85 "J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
94 cd = iconv_open ("UTF-16LE", "UTF-8");
95 ASSERT (cd != (iconv_t)(-1));
98 inbytesleft = sizeof (input) - 1;
100 outbytesleft = sizeof (buf);
102 (ICONV_CONST char **) &inptr, &inbytesleft,
103 &outptr, &outbytesleft);
104 ASSERT (res == 0 && inbytesleft == 0);
105 ASSERT (outptr == buf + (sizeof (expected) - 1));
106 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
108 ASSERT (iconv_close (cd) == 0);
111 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
113 static const char input[] =
114 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
115 static const char expected[] =
116 "\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
125 cd = iconv_open ("UTF-32BE", "UTF-8");
126 ASSERT (cd != (iconv_t)(-1));
129 inbytesleft = sizeof (input) - 1;
131 outbytesleft = sizeof (buf);
133 (ICONV_CONST char **) &inptr, &inbytesleft,
134 &outptr, &outbytesleft);
135 ASSERT (res == 0 && inbytesleft == 0);
136 ASSERT (outptr == buf + (sizeof (expected) - 1));
137 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
139 ASSERT (iconv_close (cd) == 0);
142 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
144 static const char input[] =
145 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
146 static const char expected[] =
147 "J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\000\345\145\000\000\054\147\000\000\236\212\000\000)\000\000\000 \000\000\000[\000\000\000\015\325\001\000\036\325\001\000\055\325\001\000]\000\000\000";
156 cd = iconv_open ("UTF-32LE", "UTF-8");
157 ASSERT (cd != (iconv_t)(-1));
160 inbytesleft = sizeof (input) - 1;
162 outbytesleft = sizeof (buf);
164 (ICONV_CONST char **) &inptr, &inbytesleft,
165 &outptr, &outbytesleft);
166 ASSERT (res == 0 && inbytesleft == 0);
167 ASSERT (outptr == buf + (sizeof (expected) - 1));
168 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
170 ASSERT (iconv_close (cd) == 0);
173 /* Test conversion from UTF-16BE to UTF-8 with no errors. */
175 static const char input[] =
176 "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
177 static const char expected[] =
178 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
187 cd = iconv_open ("UTF-8", "UTF-16BE");
188 ASSERT (cd != (iconv_t)(-1));
191 inbytesleft = sizeof (input) - 1;
193 outbytesleft = sizeof (buf);
195 (ICONV_CONST char **) &inptr, &inbytesleft,
196 &outptr, &outbytesleft);
197 ASSERT (res == 0 && inbytesleft == 0);
198 ASSERT (outptr == buf + (sizeof (expected) - 1));
199 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
201 ASSERT (iconv_close (cd) == 0);
204 /* Test conversion from UTF-16LE to UTF-8 with no errors. */
206 static const char input[] =
207 "J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
208 static const char expected[] =
209 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
218 cd = iconv_open ("UTF-8", "UTF-16LE");
219 ASSERT (cd != (iconv_t)(-1));
222 inbytesleft = sizeof (input) - 1;
224 outbytesleft = sizeof (buf);
226 (ICONV_CONST char **) &inptr, &inbytesleft,
227 &outptr, &outbytesleft);
228 ASSERT (res == 0 && inbytesleft == 0);
229 ASSERT (outptr == buf + (sizeof (expected) - 1));
230 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
232 ASSERT (iconv_close (cd) == 0);
235 /* Test conversion from UTF-32BE to UTF-8 with no errors. */
237 static const char input[] =
238 "\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
239 static const char expected[] =
240 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
249 cd = iconv_open ("UTF-8", "UTF-32BE");
250 ASSERT (cd != (iconv_t)(-1));
253 inbytesleft = sizeof (input) - 1;
255 outbytesleft = sizeof (buf);
257 (ICONV_CONST char **) &inptr, &inbytesleft,
258 &outptr, &outbytesleft);
259 ASSERT (res == 0 && inbytesleft == 0);
260 ASSERT (outptr == buf + (sizeof (expected) - 1));
261 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
263 ASSERT (iconv_close (cd) == 0);
266 /* Test conversion from UTF-32LE to UTF-8 with no errors. */
268 static const char input[] =
269 "J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\000\345\145\000\000\054\147\000\000\236\212\000\000)\000\000\000 \000\000\000[\000\000\000\015\325\001\000\036\325\001\000\055\325\001\000]\000\000\000";
270 static const char expected[] =
271 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
280 cd = iconv_open ("UTF-8", "UTF-32LE");
281 ASSERT (cd != (iconv_t)(-1));
284 inbytesleft = sizeof (input) - 1;
286 outbytesleft = sizeof (buf);
288 (ICONV_CONST char **) &inptr, &inbytesleft,
289 &outptr, &outbytesleft);
290 ASSERT (res == 0 && inbytesleft == 0);
291 ASSERT (outptr == buf + (sizeof (expected) - 1));
292 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
294 ASSERT (iconv_close (cd) == 0);