1 /* Test of character set conversion.
2 Copyright (C) 2007-2017 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 /* If compiling on an EBCDIC system, keep the test strings in ASCII. */
31 #if defined __IBMC__ && 'A' != 0x41
32 # pragma convert("ISO8859-1")
33 # define CONVERT_ENABLED
36 /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]". */
38 const char test_utf8_string
[] = "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
40 const char test_utf16be_string
[] = "\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]";
42 const char test_utf16le_string
[] = "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";
44 const char test_utf32be_string
[] = "\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]";
46 const char test_utf32le_string
[] = "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";
48 #ifdef CONVERT_ENABLED
56 /* Assume that iconv() supports at least the encoding UTF-8. */
58 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
60 #define input test_utf8_string
61 #define expected test_utf16be_string
70 cd
= iconv_open ("UTF-16BE", "UTF-8");
71 ASSERT (cd
!= (iconv_t
)(-1));
74 inbytesleft
= sizeof (input
) - 1;
76 outbytesleft
= sizeof (buf
);
78 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
79 &outptr
, &outbytesleft
);
80 ASSERT (res
== 0 && inbytesleft
== 0);
81 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
82 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
84 ASSERT (iconv_close (cd
) == 0);
90 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
92 #define input test_utf8_string
93 #define expected test_utf16le_string
102 cd
= iconv_open ("UTF-16LE", "UTF-8");
103 ASSERT (cd
!= (iconv_t
)(-1));
106 inbytesleft
= sizeof (input
) - 1;
108 outbytesleft
= sizeof (buf
);
110 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
111 &outptr
, &outbytesleft
);
112 ASSERT (res
== 0 && inbytesleft
== 0);
113 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
114 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
116 ASSERT (iconv_close (cd
) == 0);
122 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
124 #define input test_utf8_string
125 #define expected test_utf32be_string
134 cd
= iconv_open ("UTF-32BE", "UTF-8");
135 ASSERT (cd
!= (iconv_t
)(-1));
138 inbytesleft
= sizeof (input
) - 1;
140 outbytesleft
= sizeof (buf
);
142 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
143 &outptr
, &outbytesleft
);
144 ASSERT (res
== 0 && inbytesleft
== 0);
145 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
146 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
148 ASSERT (iconv_close (cd
) == 0);
154 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
156 #define input test_utf8_string
157 #define expected test_utf32le_string
166 cd
= iconv_open ("UTF-32LE", "UTF-8");
167 ASSERT (cd
!= (iconv_t
)(-1));
170 inbytesleft
= sizeof (input
) - 1;
172 outbytesleft
= sizeof (buf
);
174 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
175 &outptr
, &outbytesleft
);
176 ASSERT (res
== 0 && inbytesleft
== 0);
177 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
178 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
180 ASSERT (iconv_close (cd
) == 0);
186 /* Test conversion from UTF-16BE to UTF-8 with no errors. */
188 #define input test_utf16be_string
189 #define expected test_utf8_string
198 cd
= iconv_open ("UTF-8", "UTF-16BE");
199 ASSERT (cd
!= (iconv_t
)(-1));
202 inbytesleft
= sizeof (input
) - 1;
204 outbytesleft
= sizeof (buf
);
206 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
207 &outptr
, &outbytesleft
);
208 ASSERT (res
== 0 && inbytesleft
== 0);
209 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
210 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
212 ASSERT (iconv_close (cd
) == 0);
218 /* Test conversion from UTF-16LE to UTF-8 with no errors. */
220 #define input test_utf16le_string
221 #define expected test_utf8_string
230 cd
= iconv_open ("UTF-8", "UTF-16LE");
231 ASSERT (cd
!= (iconv_t
)(-1));
234 inbytesleft
= sizeof (input
) - 1;
236 outbytesleft
= sizeof (buf
);
238 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
239 &outptr
, &outbytesleft
);
240 ASSERT (res
== 0 && inbytesleft
== 0);
241 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
242 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
244 ASSERT (iconv_close (cd
) == 0);
250 /* Test conversion from UTF-32BE to UTF-8 with no errors. */
252 #define input test_utf32be_string
253 #define expected test_utf8_string
262 cd
= iconv_open ("UTF-8", "UTF-32BE");
263 ASSERT (cd
!= (iconv_t
)(-1));
266 inbytesleft
= sizeof (input
) - 1;
268 outbytesleft
= sizeof (buf
);
270 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
271 &outptr
, &outbytesleft
);
272 ASSERT (res
== 0 && inbytesleft
== 0);
273 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
274 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
276 ASSERT (iconv_close (cd
) == 0);
282 /* Test conversion from UTF-32LE to UTF-8 with no errors. */
284 #define input test_utf32le_string
285 #define expected test_utf8_string
294 cd
= iconv_open ("UTF-8", "UTF-32LE");
295 ASSERT (cd
!= (iconv_t
)(-1));
298 inbytesleft
= sizeof (input
) - 1;
300 outbytesleft
= sizeof (buf
);
302 (ICONV_CONST
char **) &inptr
, &inbytesleft
,
303 &outptr
, &outbytesleft
);
304 ASSERT (res
== 0 && inbytesleft
== 0);
305 ASSERT (outptr
== buf
+ (sizeof (expected
) - 1));
306 ASSERT (memcmp (buf
, expected
, sizeof (expected
) - 1) == 0);
308 ASSERT (iconv_close (cd
) == 0);