Flush the standard error stream before aborting.
[gnulib.git] / tests / uniconv / test-u8-conv-to-enc.c
1 /* Test of conversion from UTF-8 to legacy encodings.
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 "uniconv.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "unistr.h"
29
30 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           fflush (stderr);                                                   \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 /* Magic number for detecting bounds violations.  */
44 #define MAGIC 0x1983EFF1
45
46 static size_t *
47 new_offsets (size_t n)
48 {
49   size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
50   offsets[n] = MAGIC;
51   return offsets;
52 }
53
54 int
55 main ()
56 {
57   static enum iconv_ilseq_handler handlers[] =
58     { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
59   size_t h;
60   size_t o;
61   size_t i;
62
63 #if HAVE_ICONV
64   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
65      ISO-8859-2, and UTF-8.  */
66
67   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
68   for (h = 0; h < SIZEOF (handlers); h++)
69     {
70       enum iconv_ilseq_handler handler = handlers[h];
71       static const uint8_t input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
72       static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
73       for (o = 0; o < 2; o++)
74         {
75           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
76           char *result = NULL;
77           size_t length = 0;
78           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
79                                             input, u8_strlen (input),
80                                             offsets,
81                                             &result, &length);
82           ASSERT (retval == 0);
83           ASSERT (length == strlen (expected));
84           ASSERT (result != NULL);
85           ASSERT (memcmp (result, expected, length) == 0);
86           if (o)
87             {
88               for (i = 0; i < 41; i++)
89                 ASSERT (offsets[i] == (i < 1 ? i :
90                                        i == 1 ? (size_t)(-1) :
91                                        i < 13 ? i - 1 :
92                                        i == 13 ? (size_t)(-1) :
93                                        i < 20 ? i - 2 :
94                                        i == 20 ? (size_t)(-1) :
95                                        i < 40 ? i - 3 :
96                                        i == 40 ? (size_t)(-1) :
97                                        i - 4));
98               ASSERT (offsets[41] == MAGIC);
99               free (offsets);
100             }
101           free (result);
102         }
103     }
104
105   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
106   for (h = 0; h < SIZEOF (handlers); h++)
107     {
108       enum iconv_ilseq_handler handler = handlers[h];
109       static const uint8_t input[] = "Rafa\305\202 Maszkowski"; /* RafaƂ Maszkowski */
110       for (o = 0; o < 2; o++)
111         {
112           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
113           char *result = NULL;
114           size_t length = 0;
115           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
116                                             input, u8_strlen (input),
117                                             offsets,
118                                             &result, &length);
119           switch (handler)
120             {
121             case iconveh_error:
122               ASSERT (retval == -1 && errno == EILSEQ);
123               ASSERT (result == NULL);
124               ASSERT (length == 0);
125               break;
126             case iconveh_question_mark:
127               {
128                 static const char expected[] = "Rafa? Maszkowski";
129                 static const char expected_translit[] = "Rafal Maszkowski";
130                 ASSERT (retval == 0);
131                 ASSERT (length == strlen (expected));
132                 ASSERT (result != NULL);
133                 ASSERT (memcmp (result, expected, length) == 0
134                         || memcmp (result, expected_translit, length) == 0);
135                 if (o)
136                   {
137                     for (i = 0; i < 17; i++)
138                       ASSERT (offsets[i] == (i < 5 ? i :
139                                              i == 5 ? (size_t)(-1) :
140                                              i - 1));
141                     ASSERT (offsets[17] == MAGIC);
142                     free (offsets);
143                   }
144                 free (result);
145               }
146               break;
147             case iconveh_escape_sequence:
148               {
149                 static const char expected[] = "Rafa\\u0142 Maszkowski";
150                 ASSERT (retval == 0);
151                 ASSERT (length == strlen (expected));
152                 ASSERT (result != NULL);
153                 ASSERT (memcmp (result, expected, length) == 0);
154                 if (o)
155                   {
156                     for (i = 0; i < 17; i++)
157                       ASSERT (offsets[i] == (i < 5 ? i :
158                                              i == 5 ? (size_t)(-1) :
159                                              i + 4));
160                     ASSERT (offsets[17] == MAGIC);
161                     free (offsets);
162                   }
163                 free (result);
164               }
165               break;
166             }
167         }
168     }
169
170   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
171   for (h = 0; h < SIZEOF (handlers); h++)
172     {
173       enum iconv_ilseq_handler handler = handlers[h];
174       static const uint8_t input[] = "\342";
175       for (o = 0; o < 2; o++)
176         {
177           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
178           char *result = NULL;
179           size_t length = 0;
180           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
181                                             input, u8_strlen (input),
182                                             offsets,
183                                             &result, &length);
184           ASSERT (retval == 0);
185           ASSERT (length == strlen (""));
186           ASSERT (result != NULL);
187           if (o)
188             {
189               ASSERT (offsets[0] == 0);
190               ASSERT (offsets[1] == MAGIC);
191               free (offsets);
192             }
193           free (result);
194         }
195     }
196
197 #endif
198
199   return 0;
200 }