Merge branch 'upstream' into stable
[gnulib.git] / tests / test-iconv.c
1 /* Test of character set conversion.
2    Copyright (C) 2007-2008 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           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main ()
44 {
45 #if HAVE_ICONV
46   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
47      and UTF-8.  */
48   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
49   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
50
51   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
52   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
53
54   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
55   {
56     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
57     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
58     char buf[50];
59     const char *inptr = input;
60     size_t inbytesleft = strlen (input);
61     char *outptr = buf;
62     size_t outbytesleft = sizeof (buf);
63     size_t res = iconv (cd_88591_to_utf8,
64                         (ICONV_CONST char **) &inptr, &inbytesleft,
65                         &outptr, &outbytesleft);
66     ASSERT (res == 0 && inbytesleft == 0);
67     ASSERT (outptr == buf + strlen (expected));
68     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
69   }
70
71   /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG.  */
72   {
73     static const char input[] = "\304";
74     static char buf[2] = { (char)0xDE, (char)0xAD };
75     const char *inptr = input;
76     size_t inbytesleft = 1;
77     char *outptr = buf;
78     size_t outbytesleft = 1;
79     size_t res = iconv (cd_88591_to_utf8,
80                         (ICONV_CONST char **) &inptr, &inbytesleft,
81                         &outptr, &outbytesleft);
82     ASSERT (res == (size_t)(-1) && errno == E2BIG);
83     ASSERT (inbytesleft == 1);
84     ASSERT (outbytesleft == 1);
85     ASSERT ((unsigned char) buf[1] == 0xAD);
86     ASSERT ((unsigned char) buf[0] == 0xDE);
87   }
88
89   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
90   {
91     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
92     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
93     char buf[50];
94     const char *inptr = input;
95     size_t inbytesleft = strlen (input);
96     char *outptr = buf;
97     size_t outbytesleft = sizeof (buf);
98     size_t res = iconv (cd_utf8_to_88591,
99                         (ICONV_CONST char **) &inptr, &inbytesleft,
100                         &outptr, &outbytesleft);
101     ASSERT (res == 0 && inbytesleft == 0);
102     ASSERT (outptr == buf + strlen (expected));
103     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
104   }
105
106   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
107   {
108     static const char input[] = "\342\202\254"; /* EURO SIGN */
109     char buf[10];
110     const char *inptr = input;
111     size_t inbytesleft = strlen (input);
112     char *outptr = buf;
113     size_t outbytesleft = sizeof (buf);
114     size_t res = iconv (cd_utf8_to_88591,
115                         (ICONV_CONST char **) &inptr, &inbytesleft,
116                         &outptr, &outbytesleft);
117     if (res == (size_t)(-1))
118       {
119         ASSERT (errno == EILSEQ);
120         ASSERT (inbytesleft == strlen (input) && outptr == buf);
121       }
122     else
123       {
124         ASSERT (res == 1);
125         ASSERT (inbytesleft == 0);
126       }
127   }
128
129   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
130   {
131     static const char input[] = "\342";
132     char buf[10];
133     const char *inptr = input;
134     size_t inbytesleft = 1;
135     char *outptr = buf;
136     size_t outbytesleft = sizeof (buf);
137     size_t res = iconv (cd_utf8_to_88591,
138                         (ICONV_CONST char **) &inptr, &inbytesleft,
139                         &outptr, &outbytesleft);
140     ASSERT (res == (size_t)(-1) && errno == EINVAL);
141     ASSERT (inbytesleft == 1 && outptr == buf);
142   }
143
144   iconv_close (cd_88591_to_utf8);
145   iconv_close (cd_utf8_to_88591);
146 #endif
147
148   return 0;
149 }