54a69e3fd523d32baa30ad66b0d7fd6ea647cc8a
[platform/upstream/diffutils.git] / gnulib-tests / test-iconv.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Test of character set conversion.
4    Copyright (C) 2007-2011 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
20
21 #include <config.h>
22
23 #if HAVE_ICONV
24 # include <iconv.h>
25
26 # ifndef ICONV_CONST
27 #  define ICONV_CONST /* empty */
28 # endif
29
30 #include "signature.h"
31 SIGNATURE_CHECK (iconv, size_t, (iconv_t, ICONV_CONST char **, size_t *,
32                                  char **, size_t *));
33 SIGNATURE_CHECK (iconv_close, int, (iconv_t x));
34 SIGNATURE_CHECK (iconv_open, iconv_t, (char const *, char const *));
35
36 #endif
37
38 #include <errno.h>
39 #include <string.h>
40
41 #include "macros.h"
42
43 int
44 main ()
45 {
46 #if HAVE_ICONV
47   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
48      and UTF-8.  */
49   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
50   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
51
52   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
53   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
54
55   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
56   {
57     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
58     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
59     char buf[50];
60     const char *inptr = input;
61     size_t inbytesleft = strlen (input);
62     char *outptr = buf;
63     size_t outbytesleft = sizeof (buf);
64     size_t res = iconv (cd_88591_to_utf8,
65                         (ICONV_CONST char **) &inptr, &inbytesleft,
66                         &outptr, &outbytesleft);
67     ASSERT (res == 0 && inbytesleft == 0);
68     ASSERT (outptr == buf + strlen (expected));
69     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
70   }
71
72   /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG.  */
73   {
74     static const char input[] = "\304";
75     static char buf[2] = { (char)0xDE, (char)0xAD };
76     const char *inptr = input;
77     size_t inbytesleft = 1;
78     char *outptr = buf;
79     size_t outbytesleft = 1;
80     size_t res = iconv (cd_88591_to_utf8,
81                         (ICONV_CONST char **) &inptr, &inbytesleft,
82                         &outptr, &outbytesleft);
83     ASSERT (res == (size_t)(-1) && errno == E2BIG);
84     ASSERT (inbytesleft == 1);
85     ASSERT (outbytesleft == 1);
86     ASSERT ((unsigned char) buf[1] == 0xAD);
87     ASSERT ((unsigned char) buf[0] == 0xDE);
88   }
89
90   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
91   {
92     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
93     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
94     char buf[50];
95     const char *inptr = input;
96     size_t inbytesleft = strlen (input);
97     char *outptr = buf;
98     size_t outbytesleft = sizeof (buf);
99     size_t res = iconv (cd_utf8_to_88591,
100                         (ICONV_CONST char **) &inptr, &inbytesleft,
101                         &outptr, &outbytesleft);
102     ASSERT (res == 0 && inbytesleft == 0);
103     ASSERT (outptr == buf + strlen (expected));
104     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
105   }
106
107   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
108   {
109     static const char input[] = "\342\202\254"; /* EURO SIGN */
110     char buf[10];
111     const char *inptr = input;
112     size_t inbytesleft = strlen (input);
113     char *outptr = buf;
114     size_t outbytesleft = sizeof (buf);
115     size_t res = iconv (cd_utf8_to_88591,
116                         (ICONV_CONST char **) &inptr, &inbytesleft,
117                         &outptr, &outbytesleft);
118     if (res == (size_t)(-1))
119       {
120         ASSERT (errno == EILSEQ);
121         ASSERT (inbytesleft == strlen (input) && outptr == buf);
122       }
123     else
124       {
125         ASSERT (res == 1);
126         ASSERT (inbytesleft == 0);
127       }
128   }
129
130   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
131   {
132     static const char input[] = "\342";
133     char buf[10];
134     const char *inptr = input;
135     size_t inbytesleft = 1;
136     char *outptr = buf;
137     size_t outbytesleft = sizeof (buf);
138     size_t res = iconv (cd_utf8_to_88591,
139                         (ICONV_CONST char **) &inptr, &inbytesleft,
140                         &outptr, &outbytesleft);
141     ASSERT (res == (size_t)(-1) && errno == EINVAL);
142     ASSERT (inbytesleft == 1 && outptr == buf);
143   }
144
145   iconv_close (cd_88591_to_utf8);
146   iconv_close (cd_utf8_to_88591);
147 #endif
148
149   return 0;
150 }