Allow VPATH usage of vc-list-files.
[gnulib.git] / tests / uniconv / test-u8-conv-to-enc.c
1 /* Test of conversion from UTF-8 to 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 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           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 UTF-8 to ISO-8859-1 with no errors.  */
67   for (h = 0; h < SIZEOF (handlers); h++)
68     {
69       enum iconv_ilseq_handler handler = handlers[h];
70       static const uint8_t input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
71       static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
72       for (o = 0; o < 2; o++)
73         {
74           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
75           char *result = NULL;
76           size_t length = 0;
77           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
78                                             input, u8_strlen (input),
79                                             offsets,
80                                             &result, &length);
81           ASSERT (retval == 0);
82           ASSERT (length == strlen (expected));
83           ASSERT (result != NULL);
84           ASSERT (memcmp (result, expected, length) == 0);
85           if (o)
86             {
87               for (i = 0; i < 41; i++)
88                 ASSERT (offsets[i] == (i < 1 ? i :
89                                        i == 1 ? (size_t)(-1) :
90                                        i < 13 ? i - 1 :
91                                        i == 13 ? (size_t)(-1) :
92                                        i < 20 ? i - 2 :
93                                        i == 20 ? (size_t)(-1) :
94                                        i < 40 ? i - 3 :
95                                        i == 40 ? (size_t)(-1) :
96                                        i - 4));
97               ASSERT (offsets[41] == MAGIC);
98               free (offsets);
99             }
100           free (result);
101         }
102     }
103
104   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
105   for (h = 0; h < SIZEOF (handlers); h++)
106     {
107       enum iconv_ilseq_handler handler = handlers[h];
108       static const uint8_t input[] = "Rafa\305\202 Maszkowski"; /* RafaƂ Maszkowski */
109       for (o = 0; o < 2; o++)
110         {
111           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
112           char *result = NULL;
113           size_t length = 0;
114           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
115                                             input, u8_strlen (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 < 17; i++)
137                       ASSERT (offsets[i] == (i < 5 ? i :
138                                              i == 5 ? (size_t)(-1) :
139                                              i - 1));
140                     ASSERT (offsets[17] == MAGIC);
141                     free (offsets);
142                   }
143                 free (result);
144               }
145               break;
146             case iconveh_escape_sequence:
147               {
148                 static const char expected[] = "Rafa\\u0142 Maszkowski";
149                 ASSERT (retval == 0);
150                 ASSERT (length == strlen (expected));
151                 ASSERT (result != NULL);
152                 ASSERT (memcmp (result, expected, length) == 0);
153                 if (o)
154                   {
155                     for (i = 0; i < 17; i++)
156                       ASSERT (offsets[i] == (i < 5 ? i :
157                                              i == 5 ? (size_t)(-1) :
158                                              i + 4));
159                     ASSERT (offsets[17] == MAGIC);
160                     free (offsets);
161                   }
162                 free (result);
163               }
164               break;
165             }
166         }
167     }
168
169   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
170   for (h = 0; h < SIZEOF (handlers); h++)
171     {
172       enum iconv_ilseq_handler handler = handlers[h];
173       static const uint8_t input[] = "\342";
174       for (o = 0; o < 2; o++)
175         {
176           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
177           char *result = NULL;
178           size_t length = 0;
179           int retval = u8_conv_to_encoding ("ISO-8859-1", handler,
180                                             input, u8_strlen (input),
181                                             offsets,
182                                             &result, &length);
183           ASSERT (retval == 0);
184           ASSERT (length == strlen (""));
185           ASSERT (result != NULL);
186           if (o)
187             {
188               ASSERT (offsets[0] == 0);
189               ASSERT (offsets[1] == MAGIC);
190               free (offsets);
191             }
192           free (result);
193         }
194     }
195
196 #endif
197
198   return 0;
199 }