1 /* Test of character set conversion.
2 Copyright (C) 2007-2010 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. */
34 /* Assume that iconv() supports at least the encoding UTF-8. */
36 /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]". */
38 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
40 static const char input[] =
41 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
42 static const char expected[] =
43 "\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]";
52 cd = iconv_open ("UTF-16BE", "UTF-8");
53 ASSERT (cd != (iconv_t)(-1));
56 inbytesleft = sizeof (input) - 1;
58 outbytesleft = sizeof (buf);
60 (ICONV_CONST char **) &inptr, &inbytesleft,
61 &outptr, &outbytesleft);
62 ASSERT (res == 0 && inbytesleft == 0);
63 ASSERT (outptr == buf + (sizeof (expected) - 1));
64 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
66 ASSERT (iconv_close (cd) == 0);
69 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
71 static const char input[] =
72 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
73 static const char expected[] =
74 "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";
83 cd = iconv_open ("UTF-16LE", "UTF-8");
84 ASSERT (cd != (iconv_t)(-1));
87 inbytesleft = sizeof (input) - 1;
89 outbytesleft = sizeof (buf);
91 (ICONV_CONST char **) &inptr, &inbytesleft,
92 &outptr, &outbytesleft);
93 ASSERT (res == 0 && inbytesleft == 0);
94 ASSERT (outptr == buf + (sizeof (expected) - 1));
95 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
97 ASSERT (iconv_close (cd) == 0);
100 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
102 static const char input[] =
103 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
104 static const char expected[] =
105 "\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]";
114 cd = iconv_open ("UTF-32BE", "UTF-8");
115 ASSERT (cd != (iconv_t)(-1));
118 inbytesleft = sizeof (input) - 1;
120 outbytesleft = sizeof (buf);
122 (ICONV_CONST char **) &inptr, &inbytesleft,
123 &outptr, &outbytesleft);
124 ASSERT (res == 0 && inbytesleft == 0);
125 ASSERT (outptr == buf + (sizeof (expected) - 1));
126 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
128 ASSERT (iconv_close (cd) == 0);
131 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
133 static const char input[] =
134 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
135 static const char expected[] =
136 "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";
145 cd = iconv_open ("UTF-32LE", "UTF-8");
146 ASSERT (cd != (iconv_t)(-1));
149 inbytesleft = sizeof (input) - 1;
151 outbytesleft = sizeof (buf);
153 (ICONV_CONST char **) &inptr, &inbytesleft,
154 &outptr, &outbytesleft);
155 ASSERT (res == 0 && inbytesleft == 0);
156 ASSERT (outptr == buf + (sizeof (expected) - 1));
157 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
159 ASSERT (iconv_close (cd) == 0);
162 /* Test conversion from UTF-16BE to UTF-8 with no errors. */
164 static const char input[] =
165 "\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]";
166 static const char expected[] =
167 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
176 cd = iconv_open ("UTF-8", "UTF-16BE");
177 ASSERT (cd != (iconv_t)(-1));
180 inbytesleft = sizeof (input) - 1;
182 outbytesleft = sizeof (buf);
184 (ICONV_CONST char **) &inptr, &inbytesleft,
185 &outptr, &outbytesleft);
186 ASSERT (res == 0 && inbytesleft == 0);
187 ASSERT (outptr == buf + (sizeof (expected) - 1));
188 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
190 ASSERT (iconv_close (cd) == 0);
193 /* Test conversion from UTF-16LE to UTF-8 with no errors. */
195 static const char input[] =
196 "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";
197 static const char expected[] =
198 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
207 cd = iconv_open ("UTF-8", "UTF-16LE");
208 ASSERT (cd != (iconv_t)(-1));
211 inbytesleft = sizeof (input) - 1;
213 outbytesleft = sizeof (buf);
215 (ICONV_CONST char **) &inptr, &inbytesleft,
216 &outptr, &outbytesleft);
217 ASSERT (res == 0 && inbytesleft == 0);
218 ASSERT (outptr == buf + (sizeof (expected) - 1));
219 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
221 ASSERT (iconv_close (cd) == 0);
224 /* Test conversion from UTF-32BE to UTF-8 with no errors. */
226 static const char input[] =
227 "\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]";
228 static const char expected[] =
229 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
238 cd = iconv_open ("UTF-8", "UTF-32BE");
239 ASSERT (cd != (iconv_t)(-1));
242 inbytesleft = sizeof (input) - 1;
244 outbytesleft = sizeof (buf);
246 (ICONV_CONST char **) &inptr, &inbytesleft,
247 &outptr, &outbytesleft);
248 ASSERT (res == 0 && inbytesleft == 0);
249 ASSERT (outptr == buf + (sizeof (expected) - 1));
250 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
252 ASSERT (iconv_close (cd) == 0);
255 /* Test conversion from UTF-32LE to UTF-8 with no errors. */
257 static const char input[] =
258 "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";
259 static const char expected[] =
260 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
269 cd = iconv_open ("UTF-8", "UTF-32LE");
270 ASSERT (cd != (iconv_t)(-1));
273 inbytesleft = sizeof (input) - 1;
275 outbytesleft = sizeof (buf);
277 (ICONV_CONST char **) &inptr, &inbytesleft,
278 &outptr, &outbytesleft);
279 ASSERT (res == 0 && inbytesleft == 0);
280 ASSERT (outptr == buf + (sizeof (expected) - 1));
281 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
283 ASSERT (iconv_close (cd) == 0);