Make roundf-tests module depend on floorf, ceilf.
[gnulib.git] / tests / test-iconv-utf.c
1 /* Test of character set conversion.
2    Copyright (C) 2007 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main ()
43 {
44 #if HAVE_ICONV
45   /* Assume that iconv() supports at least the encoding UTF-8.  */
46
47   /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]".  */
48
49   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
50   {
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]";
55     iconv_t cd;
56     char buf[100];
57     const char *inptr;
58     size_t inbytesleft;
59     char *outptr;
60     size_t outbytesleft;
61     size_t res;
62
63     cd = iconv_open ("UTF-16BE", "UTF-8");
64     ASSERT (cd != (iconv_t)(-1));
65
66     inptr = input;
67     inbytesleft = sizeof (input) - 1;
68     outptr = buf;
69     outbytesleft = sizeof (buf);
70     res = iconv (cd,
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);
76
77     ASSERT (iconv_close (cd) == 0);
78   }
79
80   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
81   {
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";
86     iconv_t cd;
87     char buf[100];
88     const char *inptr;
89     size_t inbytesleft;
90     char *outptr;
91     size_t outbytesleft;
92     size_t res;
93
94     cd = iconv_open ("UTF-16LE", "UTF-8");
95     ASSERT (cd != (iconv_t)(-1));
96
97     inptr = input;
98     inbytesleft = sizeof (input) - 1;
99     outptr = buf;
100     outbytesleft = sizeof (buf);
101     res = iconv (cd,
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);
107
108     ASSERT (iconv_close (cd) == 0);
109   }
110
111   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
112   {
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]";
117     iconv_t cd;
118     char buf[100];
119     const char *inptr;
120     size_t inbytesleft;
121     char *outptr;
122     size_t outbytesleft;
123     size_t res;
124
125     cd = iconv_open ("UTF-32BE", "UTF-8");
126     ASSERT (cd != (iconv_t)(-1));
127
128     inptr = input;
129     inbytesleft = sizeof (input) - 1;
130     outptr = buf;
131     outbytesleft = sizeof (buf);
132     res = iconv (cd,
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);
138
139     ASSERT (iconv_close (cd) == 0);
140   }
141
142   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
143   {
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";
148     iconv_t cd;
149     char buf[100];
150     const char *inptr;
151     size_t inbytesleft;
152     char *outptr;
153     size_t outbytesleft;
154     size_t res;
155
156     cd = iconv_open ("UTF-32LE", "UTF-8");
157     ASSERT (cd != (iconv_t)(-1));
158
159     inptr = input;
160     inbytesleft = sizeof (input) - 1;
161     outptr = buf;
162     outbytesleft = sizeof (buf);
163     res = iconv (cd,
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);
169
170     ASSERT (iconv_close (cd) == 0);
171   }
172
173   /* Test conversion from UTF-16BE to UTF-8 with no errors.  */
174   {
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]";
179     iconv_t cd;
180     char buf[100];
181     const char *inptr;
182     size_t inbytesleft;
183     char *outptr;
184     size_t outbytesleft;
185     size_t res;
186
187     cd = iconv_open ("UTF-8", "UTF-16BE");
188     ASSERT (cd != (iconv_t)(-1));
189
190     inptr = input;
191     inbytesleft = sizeof (input) - 1;
192     outptr = buf;
193     outbytesleft = sizeof (buf);
194     res = iconv (cd,
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);
200
201     ASSERT (iconv_close (cd) == 0);
202   }
203
204   /* Test conversion from UTF-16LE to UTF-8 with no errors.  */
205   {
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]";
210     iconv_t cd;
211     char buf[100];
212     const char *inptr;
213     size_t inbytesleft;
214     char *outptr;
215     size_t outbytesleft;
216     size_t res;
217
218     cd = iconv_open ("UTF-8", "UTF-16LE");
219     ASSERT (cd != (iconv_t)(-1));
220
221     inptr = input;
222     inbytesleft = sizeof (input) - 1;
223     outptr = buf;
224     outbytesleft = sizeof (buf);
225     res = iconv (cd,
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);
231
232     ASSERT (iconv_close (cd) == 0);
233   }
234
235   /* Test conversion from UTF-32BE to UTF-8 with no errors.  */
236   {
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]";
241     iconv_t cd;
242     char buf[100];
243     const char *inptr;
244     size_t inbytesleft;
245     char *outptr;
246     size_t outbytesleft;
247     size_t res;
248
249     cd = iconv_open ("UTF-8", "UTF-32BE");
250     ASSERT (cd != (iconv_t)(-1));
251
252     inptr = input;
253     inbytesleft = sizeof (input) - 1;
254     outptr = buf;
255     outbytesleft = sizeof (buf);
256     res = iconv (cd,
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);
262
263     ASSERT (iconv_close (cd) == 0);
264   }
265
266   /* Test conversion from UTF-32LE to UTF-8 with no errors.  */
267   {
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]";
272     iconv_t cd;
273     char buf[100];
274     const char *inptr;
275     size_t inbytesleft;
276     char *outptr;
277     size_t outbytesleft;
278     size_t res;
279
280     cd = iconv_open ("UTF-8", "UTF-32LE");
281     ASSERT (cd != (iconv_t)(-1));
282
283     inptr = input;
284     inbytesleft = sizeof (input) - 1;
285     outptr = buf;
286     outbytesleft = sizeof (buf);
287     res = iconv (cd,
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);
293
294     ASSERT (iconv_close (cd) == 0);
295   }
296 #endif
297
298   return 0;
299 }