Assume <config.h> exists.
[gnulib.git] / tests / uniconv / test-u8-conv-from-enc.c
1 /* Test of conversion to UTF-8 from legacy encodings.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "uniconv.h"
23
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           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 /* Magic number for detecting bounds violations.  */
43 #define MAGIC 0x1983EFF1
44
45 static size_t *
46 new_offsets (size_t n)
47 {
48   size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
49   offsets[n] = MAGIC;
50   return offsets;
51 }
52
53 int
54 main ()
55 {
56   static enum iconv_ilseq_handler handlers[] =
57     { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
58   size_t h;
59   size_t o;
60   size_t i;
61
62 #if HAVE_ICONV
63   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
64      ISO-8859-2, and UTF-8.  */
65
66   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
67   for (h = 0; h < SIZEOF (handlers); h++)
68     {
69       enum iconv_ilseq_handler handler = handlers[h];
70       static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
71       static const uint8_t expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
72       for (o = 0; o < 2; o++)
73         {
74           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
75           uint8_t *result = NULL;
76           size_t length = 0;
77           int retval = u8_conv_from_encoding ("ISO-8859-1", handler,
78                                               input, strlen (input),
79                                               offsets,
80                                               &result, &length);
81           ASSERT (retval == 0);
82           ASSERT (length == u8_strlen (expected));
83           ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
84           if (o)
85             {
86               for (i = 0; i < 37; i++)
87                 ASSERT (offsets[i] == (i < 1 ? i :
88                                        i < 12 ? i + 1 :
89                                        i < 18 ? i + 2 :
90                                        i + 3));
91               ASSERT (offsets[37] == MAGIC);
92               free (offsets);
93             }
94           free (result);
95         }
96     }
97
98   /* Test conversion from ISO-8859-2 to UTF-8 with no errors.  */
99   for (h = 0; h < SIZEOF (handlers); h++)
100     {
101       enum iconv_ilseq_handler handler = handlers[h];
102       static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
103       static const uint8_t expected[] = "Rafa\305\202 Maszkowski";
104       for (o = 0; o < 2; o++)
105         {
106           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
107           uint8_t *result = NULL;
108           size_t length = 0;
109           int retval = u8_conv_from_encoding ("ISO-8859-2", handler,
110                                               input, strlen (input),
111                                               offsets,
112                                               &result, &length);
113           ASSERT (retval == 0);
114           ASSERT (length == u8_strlen (expected));
115           ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
116           if (o)
117             {
118               for (i = 0; i < 16; i++)
119                 ASSERT (offsets[i] == (i < 5 ? i :
120                                        i + 1));
121               ASSERT (offsets[16] == MAGIC);
122               free (offsets);
123             }
124           free (result);
125         }
126     }
127
128   /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2.  */
129 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__)
130   /* Test conversions from autodetect_jp to UTF-8.  */
131   for (h = 0; h < SIZEOF (handlers); h++)
132     {
133       enum iconv_ilseq_handler handler = handlers[h];
134       static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
135       static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
136       for (o = 0; o < 2; o++)
137         {
138           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
139           uint8_t *result = NULL;
140           size_t length = 0;
141           int retval = u8_conv_from_encoding ("autodetect_jp", handler,
142                                               input, strlen (input),
143                                               offsets,
144                                               &result, &length);
145           ASSERT (retval == 0);
146           ASSERT (length == u8_strlen (expected));
147           ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
148           if (o)
149             {
150               for (i = 0; i < 10; i++)
151                 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
152               ASSERT (offsets[10] == MAGIC);
153               free (offsets);
154             }
155           free (result);
156         }
157     }
158   for (h = 0; h < SIZEOF (handlers); h++)
159     {
160       enum iconv_ilseq_handler handler = handlers[h];
161       static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
162       static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
163       for (o = 0; o < 2; o++)
164         {
165           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
166           uint8_t *result = NULL;
167           size_t length = 0;
168           int retval = u8_conv_from_encoding ("autodetect_jp", handler,
169                                               input, strlen (input),
170                                               offsets,
171                                               &result, &length);
172           ASSERT (retval == 0);
173           ASSERT (length == u8_strlen (expected));
174           ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
175           if (o)
176             {
177               for (i = 0; i < 10; i++)
178                 ASSERT (offsets[i] == ((i % 2) == 0 ? (i / 2) * 3 : (size_t)(-1)));
179               ASSERT (offsets[10] == MAGIC);
180               free (offsets);
181             }
182           free (result);
183         }
184     }
185   for (h = 0; h < SIZEOF (handlers); h++)
186     {
187       enum iconv_ilseq_handler handler = handlers[h];
188       static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
189       static const uint8_t expected[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
190       for (o = 0; o < 2; o++)
191         {
192           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
193           uint8_t *result = NULL;
194           size_t length = 0;
195           int retval = u8_conv_from_encoding ("autodetect_jp", handler,
196                                               input, strlen (input),
197                                               offsets,
198                                               &result, &length);
199           ASSERT (retval == 0);
200           ASSERT (length == u8_strlen (expected));
201           ASSERT (result != NULL && u8_cmp (result, expected, u8_strlen (expected)) == 0);
202           if (o)
203             {
204               for (i = 0; i < 16; i++)
205                 ASSERT (offsets[i] == (i == 0 ? 0 :
206                                        i == 5 ? 3 :
207                                        i == 7 ? 6 :
208                                        i == 9 ? 9 :
209                                        i == 11 ? 12 :
210                                        i == 13 ? 15 :
211                                        (size_t)(-1)));
212               ASSERT (offsets[16] == MAGIC);
213               free (offsets);
214             }
215           free (result);
216         }
217     }
218 # endif
219
220 #endif
221
222   return 0;
223 }