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