080896fbfc33c6f5454fb0517135841bfea04df6
[gnulib.git] / tests / uniconv / test-u8-conv-to-enc.c
1 /* Test of conversion from UTF-8 to legacy encodings.
2    Copyright (C) 2007-2009 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           size_t length;
77           char *result = u8_conv_to_encoding ("ISO-8859-1", handler,
78                                               input, u8_strlen (input),
79                                               offsets,
80                                               NULL, &length);
81           ASSERT (result != NULL);
82           ASSERT (length == strlen (expected));
83           ASSERT (memcmp (result, expected, length) == 0);
84           if (o)
85             {
86               for (i = 0; i < 41; i++)
87                 ASSERT (offsets[i] == (i < 1 ? i :
88                                        i == 1 ? (size_t)(-1) :
89                                        i < 13 ? i - 1 :
90                                        i == 13 ? (size_t)(-1) :
91                                        i < 20 ? i - 2 :
92                                        i == 20 ? (size_t)(-1) :
93                                        i < 40 ? i - 3 :
94                                        i == 40 ? (size_t)(-1) :
95                                        i - 4));
96               ASSERT (offsets[41] == MAGIC);
97               free (offsets);
98             }
99           free (result);
100         }
101     }
102
103   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
104   for (h = 0; h < SIZEOF (handlers); h++)
105     {
106       enum iconv_ilseq_handler handler = handlers[h];
107       static const uint8_t input[] = "Rafa\305\202 Maszkowski"; /* RafaƂ Maszkowski */
108       for (o = 0; o < 2; o++)
109         {
110           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
111           size_t length = 0xdead;
112           char *result = u8_conv_to_encoding ("ISO-8859-1", handler,
113                                               input, u8_strlen (input),
114                                               offsets,
115                                               NULL, &length);
116           switch (handler)
117             {
118             case iconveh_error:
119               ASSERT (result == NULL);
120               ASSERT (errno == EILSEQ);
121               ASSERT (length == 0xdead);
122               break;
123             case iconveh_question_mark:
124               {
125                 static const char expected[] = "Rafa? Maszkowski";
126                 static const char expected_translit[] = "Rafal Maszkowski";
127                 ASSERT (result != NULL);
128                 ASSERT (length == strlen (expected));
129                 ASSERT (memcmp (result, expected, length) == 0
130                         || memcmp (result, expected_translit, length) == 0);
131                 if (o)
132                   {
133                     for (i = 0; i < 17; i++)
134                       ASSERT (offsets[i] == (i < 5 ? i :
135                                              i == 5 ? (size_t)(-1) :
136                                              i - 1));
137                     ASSERT (offsets[17] == MAGIC);
138                     free (offsets);
139                   }
140                 free (result);
141               }
142               break;
143             case iconveh_escape_sequence:
144               {
145                 static const char expected[] = "Rafa\\u0142 Maszkowski";
146                 ASSERT (result != NULL);
147                 ASSERT (length == strlen (expected));
148                 ASSERT (memcmp (result, expected, length) == 0);
149                 if (o)
150                   {
151                     for (i = 0; i < 17; i++)
152                       ASSERT (offsets[i] == (i < 5 ? i :
153                                              i == 5 ? (size_t)(-1) :
154                                              i + 4));
155                     ASSERT (offsets[17] == MAGIC);
156                     free (offsets);
157                   }
158                 free (result);
159               }
160               break;
161             }
162         }
163     }
164
165   /* Test conversion from UTF-8 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 uint8_t input[] = "\342";
170       for (o = 0; o < 2; o++)
171         {
172           size_t *offsets = (o ? new_offsets (u8_strlen (input)) : NULL);
173           size_t length;
174           char *result = u8_conv_to_encoding ("ISO-8859-1", handler,
175                                               input, u8_strlen (input),
176                                               offsets,
177                                               NULL, &length);
178           ASSERT (result != NULL);
179           ASSERT (length == strlen (""));
180           if (o)
181             {
182               ASSERT (offsets[0] == 0);
183               ASSERT (offsets[1] == MAGIC);
184               free (offsets);
185             }
186           free (result);
187         }
188     }
189
190 #endif
191
192   return 0;
193 }