00157dad13b4cddaeee2d0536ed66677b9643114
[gnulib.git] / tests / test-iconv.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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if HAVE_ICONV
25 # include <iconv.h>
26 #endif
27
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define ASSERT(expr) if (!(expr)) abort ();
33
34 int
35 main ()
36 {
37 #if HAVE_ICONV
38   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
39      and UTF-8.  */
40   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
41   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
42
43   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
44   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
45
46   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
47   {
48     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
49     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
50     char buf[50];
51     const char *inptr = input;
52     size_t inbytesleft = strlen (input);
53     char *outptr = buf;
54     size_t outbytesleft = sizeof (buf);
55     size_t res = iconv (cd_88591_to_utf8,
56                         (ICONV_CONST char **) &inptr, &inbytesleft,
57                         &outptr, &outbytesleft);
58     ASSERT (res == 0 && inbytesleft == 0);
59     ASSERT (outptr == buf + strlen (expected));
60     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
61   }
62
63   /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG.  */
64   {
65     static const char input[] = "\304";
66     static char buf[2] = { (char)0xDE, (char)0xAD };
67     const char *inptr = input;
68     size_t inbytesleft = 1;
69     char *outptr = buf;
70     size_t outbytesleft = 1;
71     size_t res = iconv (cd_88591_to_utf8,
72                         (ICONV_CONST char **) &inptr, &inbytesleft,
73                         &outptr, &outbytesleft);
74     ASSERT (res == (size_t)(-1) && errno == E2BIG);
75     ASSERT (inbytesleft == 1);
76     ASSERT (outbytesleft == 1);
77     ASSERT ((unsigned char) buf[1] == 0xAD);
78     ASSERT ((unsigned char) buf[0] == 0xDE);
79   }
80
81   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
82   {
83     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
84     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
85     char buf[50];
86     const char *inptr = input;
87     size_t inbytesleft = strlen (input);
88     char *outptr = buf;
89     size_t outbytesleft = sizeof (buf);
90     size_t res = iconv (cd_utf8_to_88591,
91                         (ICONV_CONST char **) &inptr, &inbytesleft,
92                         &outptr, &outbytesleft);
93     ASSERT (res == 0 && inbytesleft == 0);
94     ASSERT (outptr == buf + strlen (expected));
95     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
96   }
97
98   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
99   {
100     static const char input[] = "\342\202\254"; /* EURO SIGN */
101     char buf[10];
102     const char *inptr = input;
103     size_t inbytesleft = strlen (input);
104     char *outptr = buf;
105     size_t outbytesleft = sizeof (buf);
106     size_t res = iconv (cd_utf8_to_88591,
107                         (ICONV_CONST char **) &inptr, &inbytesleft,
108                         &outptr, &outbytesleft);
109     if (res == (size_t)(-1))
110       {
111         ASSERT (errno == EILSEQ);
112         ASSERT (inbytesleft == strlen (input) && outptr == buf);
113       }
114     else
115       {
116         ASSERT (res == 1);
117         ASSERT (inbytesleft == 0);
118       }
119   }
120
121   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
122   {
123     static const char input[] = "\342";
124     char buf[10];
125     const char *inptr = input;
126     size_t inbytesleft = 1;
127     char *outptr = buf;
128     size_t outbytesleft = sizeof (buf);
129     size_t res = iconv (cd_utf8_to_88591,
130                         (ICONV_CONST char **) &inptr, &inbytesleft,
131                         &outptr, &outbytesleft);
132     ASSERT (res == (size_t)(-1) && errno == EINVAL);
133     ASSERT (inbytesleft == 1 && outptr == buf);
134   }
135
136   iconv_close (cd_88591_to_utf8);
137   iconv_close (cd_utf8_to_88591);
138 #endif
139
140   return 0;
141 }