Flush the standard error stream before aborting.
[gnulib.git] / tests / test-striconv.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 #include "striconv.h"
22
23 #if HAVE_ICONV
24 # include <iconv.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 int
45 main ()
46 {
47 #if HAVE_ICONV
48   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
49      and UTF-8.  */
50   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
51   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
52
53   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
54   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
55
56   /* ------------------------- Test mem_cd_iconv() ------------------------- */
57
58   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
59   {
60     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
61     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
62     char *result = NULL;
63     size_t length = 0;
64     int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
65                                &result, &length);
66     ASSERT (retval == 0);
67     ASSERT (length == strlen (expected));
68     ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
69     free (result);
70   }
71
72   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
73   {
74     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
75     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
76     char *result = NULL;
77     size_t length = 0;
78     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
79                                &result, &length);
80     ASSERT (retval == 0);
81     ASSERT (length == strlen (expected));
82     ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
83     free (result);
84   }
85
86   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
87   {
88     static const char input[] = "\342\202\254"; /* EURO SIGN */
89     char *result = NULL;
90     size_t length = 0;
91     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
92                                &result, &length);
93     ASSERT (retval == -1 && errno == EILSEQ);
94     ASSERT (result == NULL);
95   }
96
97   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
98   {
99     static const char input[] = "\342";
100     char *result = NULL;
101     size_t length = 0;
102     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
103                                &result, &length);
104     ASSERT (retval == 0);
105     ASSERT (length == 0);
106     free (result);
107   }
108
109   /* ------------------------- Test str_cd_iconv() ------------------------- */
110
111   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
112   {
113     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
114     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
115     char *result = str_cd_iconv (input, cd_88591_to_utf8);
116     ASSERT (result != NULL);
117     ASSERT (strcmp (result, expected) == 0);
118     free (result);
119   }
120
121   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
122   {
123     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
124     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
125     char *result = str_cd_iconv (input, cd_utf8_to_88591);
126     ASSERT (result != NULL);
127     ASSERT (strcmp (result, expected) == 0);
128     free (result);
129   }
130
131   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
132   {
133     static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
134     char *result = str_cd_iconv (input, cd_utf8_to_88591);
135     ASSERT (result == NULL && errno == EILSEQ);
136   }
137
138   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
139   {
140     static const char input[] = "\342";
141     char *result = str_cd_iconv (input, cd_utf8_to_88591);
142     ASSERT (result != NULL);
143     ASSERT (strcmp (result, "") == 0);
144     free (result);
145   }
146
147   iconv_close (cd_88591_to_utf8);
148   iconv_close (cd_utf8_to_88591);
149
150   /* -------------------------- Test str_iconv() -------------------------- */
151
152   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
153   {
154     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
155     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
156     char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
157     ASSERT (result != NULL);
158     ASSERT (strcmp (result, expected) == 0);
159     free (result);
160   }
161
162   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
163   {
164     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
165     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
166     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
167     ASSERT (result != NULL);
168     ASSERT (strcmp (result, expected) == 0);
169     free (result);
170   }
171
172   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
173   {
174     static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
175     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
176     ASSERT (result == NULL && errno == EILSEQ);
177   }
178
179   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
180   {
181     static const char input[] = "\342";
182     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
183     ASSERT (result != NULL);
184     ASSERT (strcmp (result, "") == 0);
185     free (result);
186   }
187
188 #endif
189
190   return 0;
191 }