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