1 /* Test of character set conversion.
2 Copyright (C) 2007-2008 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__); \
46 /* Assume that iconv() supports at least the encoding UTF-8. */
48 /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]". */
50 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
52 static const char input[] =
53 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
54 static const char expected[] =
55 "\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]";
64 cd = iconv_open ("UTF-16BE", "UTF-8");
65 ASSERT (cd != (iconv_t)(-1));
68 inbytesleft = sizeof (input) - 1;
70 outbytesleft = sizeof (buf);
72 (ICONV_CONST char **) &inptr, &inbytesleft,
73 &outptr, &outbytesleft);
74 ASSERT (res == 0 && inbytesleft == 0);
75 ASSERT (outptr == buf + (sizeof (expected) - 1));
76 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
78 ASSERT (iconv_close (cd) == 0);
81 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
83 static const char input[] =
84 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
85 static const char expected[] =
86 "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";
95 cd = iconv_open ("UTF-16LE", "UTF-8");
96 ASSERT (cd != (iconv_t)(-1));
99 inbytesleft = sizeof (input) - 1;
101 outbytesleft = sizeof (buf);
103 (ICONV_CONST char **) &inptr, &inbytesleft,
104 &outptr, &outbytesleft);
105 ASSERT (res == 0 && inbytesleft == 0);
106 ASSERT (outptr == buf + (sizeof (expected) - 1));
107 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
109 ASSERT (iconv_close (cd) == 0);
112 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
114 static const char input[] =
115 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
116 static const char expected[] =
117 "\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]";
126 cd = iconv_open ("UTF-32BE", "UTF-8");
127 ASSERT (cd != (iconv_t)(-1));
130 inbytesleft = sizeof (input) - 1;
132 outbytesleft = sizeof (buf);
134 (ICONV_CONST char **) &inptr, &inbytesleft,
135 &outptr, &outbytesleft);
136 ASSERT (res == 0 && inbytesleft == 0);
137 ASSERT (outptr == buf + (sizeof (expected) - 1));
138 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
140 ASSERT (iconv_close (cd) == 0);
143 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
145 static const char input[] =
146 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
147 static const char expected[] =
148 "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";
157 cd = iconv_open ("UTF-32LE", "UTF-8");
158 ASSERT (cd != (iconv_t)(-1));
161 inbytesleft = sizeof (input) - 1;
163 outbytesleft = sizeof (buf);
165 (ICONV_CONST char **) &inptr, &inbytesleft,
166 &outptr, &outbytesleft);
167 ASSERT (res == 0 && inbytesleft == 0);
168 ASSERT (outptr == buf + (sizeof (expected) - 1));
169 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
171 ASSERT (iconv_close (cd) == 0);
174 /* Test conversion from UTF-16BE to UTF-8 with no errors. */
176 static const char input[] =
177 "\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]";
178 static const char expected[] =
179 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
188 cd = iconv_open ("UTF-8", "UTF-16BE");
189 ASSERT (cd != (iconv_t)(-1));
192 inbytesleft = sizeof (input) - 1;
194 outbytesleft = sizeof (buf);
196 (ICONV_CONST char **) &inptr, &inbytesleft,
197 &outptr, &outbytesleft);
198 ASSERT (res == 0 && inbytesleft == 0);
199 ASSERT (outptr == buf + (sizeof (expected) - 1));
200 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
202 ASSERT (iconv_close (cd) == 0);
205 /* Test conversion from UTF-16LE to UTF-8 with no errors. */
207 static const char input[] =
208 "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";
209 static const char expected[] =
210 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
219 cd = iconv_open ("UTF-8", "UTF-16LE");
220 ASSERT (cd != (iconv_t)(-1));
223 inbytesleft = sizeof (input) - 1;
225 outbytesleft = sizeof (buf);
227 (ICONV_CONST char **) &inptr, &inbytesleft,
228 &outptr, &outbytesleft);
229 ASSERT (res == 0 && inbytesleft == 0);
230 ASSERT (outptr == buf + (sizeof (expected) - 1));
231 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
233 ASSERT (iconv_close (cd) == 0);
236 /* Test conversion from UTF-32BE to UTF-8 with no errors. */
238 static const char input[] =
239 "\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]";
240 static const char expected[] =
241 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
250 cd = iconv_open ("UTF-8", "UTF-32BE");
251 ASSERT (cd != (iconv_t)(-1));
254 inbytesleft = sizeof (input) - 1;
256 outbytesleft = sizeof (buf);
258 (ICONV_CONST char **) &inptr, &inbytesleft,
259 &outptr, &outbytesleft);
260 ASSERT (res == 0 && inbytesleft == 0);
261 ASSERT (outptr == buf + (sizeof (expected) - 1));
262 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
264 ASSERT (iconv_close (cd) == 0);
267 /* Test conversion from UTF-32LE to UTF-8 with no errors. */
269 static const char input[] =
270 "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";
271 static const char expected[] =
272 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
281 cd = iconv_open ("UTF-8", "UTF-32LE");
282 ASSERT (cd != (iconv_t)(-1));
285 inbytesleft = sizeof (input) - 1;
287 outbytesleft = sizeof (buf);
289 (ICONV_CONST char **) &inptr, &inbytesleft,
290 &outptr, &outbytesleft);
291 ASSERT (res == 0 && inbytesleft == 0);
292 ASSERT (outptr == buf + (sizeof (expected) - 1));
293 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
295 ASSERT (iconv_close (cd) == 0);