maint: update copyright
[gnulib.git] / tests / test-iconv-utf.c
1 /* Test of character set conversion.
2    Copyright (C) 2007-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #if HAVE_ICONV
22 # include <iconv.h>
23 #endif
24
25 #include <errno.h>
26 #include <string.h>
27
28 #include "macros.h"
29
30 int
31 main ()
32 {
33 #if HAVE_ICONV
34   /* Assume that iconv() supports at least the encoding UTF-8.  */
35
36   /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]".  */
37
38   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
39   {
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]";
44     iconv_t cd;
45     char buf[100];
46     const char *inptr;
47     size_t inbytesleft;
48     char *outptr;
49     size_t outbytesleft;
50     size_t res;
51
52     cd = iconv_open ("UTF-16BE", "UTF-8");
53     ASSERT (cd != (iconv_t)(-1));
54
55     inptr = input;
56     inbytesleft = sizeof (input) - 1;
57     outptr = buf;
58     outbytesleft = sizeof (buf);
59     res = iconv (cd,
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);
65
66     ASSERT (iconv_close (cd) == 0);
67   }
68
69   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
70   {
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";
75     iconv_t cd;
76     char buf[100];
77     const char *inptr;
78     size_t inbytesleft;
79     char *outptr;
80     size_t outbytesleft;
81     size_t res;
82
83     cd = iconv_open ("UTF-16LE", "UTF-8");
84     ASSERT (cd != (iconv_t)(-1));
85
86     inptr = input;
87     inbytesleft = sizeof (input) - 1;
88     outptr = buf;
89     outbytesleft = sizeof (buf);
90     res = iconv (cd,
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);
96
97     ASSERT (iconv_close (cd) == 0);
98   }
99
100   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
101   {
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]";
106     iconv_t cd;
107     char buf[100];
108     const char *inptr;
109     size_t inbytesleft;
110     char *outptr;
111     size_t outbytesleft;
112     size_t res;
113
114     cd = iconv_open ("UTF-32BE", "UTF-8");
115     ASSERT (cd != (iconv_t)(-1));
116
117     inptr = input;
118     inbytesleft = sizeof (input) - 1;
119     outptr = buf;
120     outbytesleft = sizeof (buf);
121     res = iconv (cd,
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);
127
128     ASSERT (iconv_close (cd) == 0);
129   }
130
131   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
132   {
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";
137     iconv_t cd;
138     char buf[100];
139     const char *inptr;
140     size_t inbytesleft;
141     char *outptr;
142     size_t outbytesleft;
143     size_t res;
144
145     cd = iconv_open ("UTF-32LE", "UTF-8");
146     ASSERT (cd != (iconv_t)(-1));
147
148     inptr = input;
149     inbytesleft = sizeof (input) - 1;
150     outptr = buf;
151     outbytesleft = sizeof (buf);
152     res = iconv (cd,
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);
158
159     ASSERT (iconv_close (cd) == 0);
160   }
161
162   /* Test conversion from UTF-16BE to UTF-8 with no errors.  */
163   {
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]";
168     iconv_t cd;
169     char buf[100];
170     const char *inptr;
171     size_t inbytesleft;
172     char *outptr;
173     size_t outbytesleft;
174     size_t res;
175
176     cd = iconv_open ("UTF-8", "UTF-16BE");
177     ASSERT (cd != (iconv_t)(-1));
178
179     inptr = input;
180     inbytesleft = sizeof (input) - 1;
181     outptr = buf;
182     outbytesleft = sizeof (buf);
183     res = iconv (cd,
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);
189
190     ASSERT (iconv_close (cd) == 0);
191   }
192
193   /* Test conversion from UTF-16LE to UTF-8 with no errors.  */
194   {
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]";
199     iconv_t cd;
200     char buf[100];
201     const char *inptr;
202     size_t inbytesleft;
203     char *outptr;
204     size_t outbytesleft;
205     size_t res;
206
207     cd = iconv_open ("UTF-8", "UTF-16LE");
208     ASSERT (cd != (iconv_t)(-1));
209
210     inptr = input;
211     inbytesleft = sizeof (input) - 1;
212     outptr = buf;
213     outbytesleft = sizeof (buf);
214     res = iconv (cd,
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);
220
221     ASSERT (iconv_close (cd) == 0);
222   }
223
224   /* Test conversion from UTF-32BE to UTF-8 with no errors.  */
225   {
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]";
230     iconv_t cd;
231     char buf[100];
232     const char *inptr;
233     size_t inbytesleft;
234     char *outptr;
235     size_t outbytesleft;
236     size_t res;
237
238     cd = iconv_open ("UTF-8", "UTF-32BE");
239     ASSERT (cd != (iconv_t)(-1));
240
241     inptr = input;
242     inbytesleft = sizeof (input) - 1;
243     outptr = buf;
244     outbytesleft = sizeof (buf);
245     res = iconv (cd,
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);
251
252     ASSERT (iconv_close (cd) == 0);
253   }
254
255   /* Test conversion from UTF-32LE to UTF-8 with no errors.  */
256   {
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]";
261     iconv_t cd;
262     char buf[100];
263     const char *inptr;
264     size_t inbytesleft;
265     char *outptr;
266     size_t outbytesleft;
267     size_t res;
268
269     cd = iconv_open ("UTF-8", "UTF-32LE");
270     ASSERT (cd != (iconv_t)(-1));
271
272     inptr = input;
273     inbytesleft = sizeof (input) - 1;
274     outptr = buf;
275     outbytesleft = sizeof (buf);
276     res = iconv (cd,
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);
282
283     ASSERT (iconv_close (cd) == 0);
284   }
285 #endif
286
287   return 0;
288 }