Tests for striconv module.
[gnulib.git] / tests / test-striconv.c
1 /* Test of character set conversion.
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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "striconv.h"
25
26 #if HAVE_ICONV
27 # include <iconv.h>
28 #endif
29
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #define ASSERT(expr) if (!(expr)) abort ();
35
36 int
37 main ()
38 {
39 #if HAVE_ICONV
40   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
41      and UTF-8.  */
42   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
43   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
44
45   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
46   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
47
48   /* ------------------------- Test mem_cd_iconv() ------------------------- */
49
50   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
51   {
52     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
53     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
54     char *result = NULL;
55     size_t length;
56     int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
57                                &result, &length);
58     ASSERT (retval == 0);
59     ASSERT (length == strlen (expected));
60     ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
61     free (result);
62   }
63
64   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
65   {
66     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
67     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
68     char *result = NULL;
69     size_t length;
70     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
71                                &result, &length);
72     ASSERT (retval == 0);
73     ASSERT (length == strlen (expected));
74     ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
75     free (result);
76   }
77
78   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
79   {
80     static const char input[] = "\342\202\254"; /* EURO SIGN */
81     char *result = NULL;
82     size_t length;
83     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
84                                &result, &length);
85     ASSERT (retval == -1 && errno == EILSEQ);
86     ASSERT (result == NULL);
87   }
88
89   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
90   {
91     static const char input[] = "\342";
92     char *result = NULL;
93     size_t length;
94     int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
95                                &result, &length);
96     ASSERT (retval == 0);
97     ASSERT (length == 0);
98     if (result != NULL)
99       free (result);
100   }
101
102   /* ------------------------- Test str_cd_iconv() ------------------------- */
103
104   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
105   {
106     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
107     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
108     char *result = str_cd_iconv (input, cd_88591_to_utf8);
109     ASSERT (result != NULL);
110     ASSERT (strcmp (result, expected) == 0);
111     free (result);
112   }
113
114   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
115   {
116     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
117     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
118     char *result = str_cd_iconv (input, cd_utf8_to_88591);
119     ASSERT (result != NULL);
120     ASSERT (strcmp (result, expected) == 0);
121     free (result);
122   }
123
124   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
125   {
126     static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
127     char *result = str_cd_iconv (input, cd_utf8_to_88591);
128     ASSERT (result == NULL && errno == EILSEQ);
129   }
130
131   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
132   {
133     static const char input[] = "\342";
134     char *result = str_cd_iconv (input, cd_utf8_to_88591);
135     ASSERT (result != NULL);
136     ASSERT (strcmp (result, "") == 0);
137     free (result);
138   }
139
140   iconv_close (cd_88591_to_utf8);
141   iconv_close (cd_utf8_to_88591);
142
143   /* -------------------------- Test str_iconv() -------------------------- */
144
145   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
146   {
147     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
148     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
149     char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
150     ASSERT (result != NULL);
151     ASSERT (strcmp (result, expected) == 0);
152     free (result);
153   }
154
155   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
156   {
157     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
158     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
159     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
160     ASSERT (result != NULL);
161     ASSERT (strcmp (result, expected) == 0);
162     free (result);
163   }
164
165   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
166   {
167     static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
168     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
169     ASSERT (result == NULL && errno == EILSEQ);
170   }
171
172   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
173   {
174     static const char input[] = "\342";
175     char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
176     ASSERT (result != NULL);
177     ASSERT (strcmp (result, "") == 0);
178     free (result);
179   }
180
181 #endif
182
183   return 0;
184 }