openat: don’t close (-1)
[gnulib.git] / tests / test-striconv.c
blob043c8a700074c7be1b9d14360aa82c1d4653fd03
1 /* Test of character set conversion.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 #include <config.h>
21 #include "striconv.h"
23 #if HAVE_ICONV
24 # include <iconv.h>
25 #endif
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "macros.h"
33 int
34 main ()
36 #if HAVE_ICONV
37 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
38 and UTF-8. */
39 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
40 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
42 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
43 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
45 /* ------------------------- Test mem_cd_iconv() ------------------------- */
47 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
49 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
50 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
51 char *result = NULL;
52 size_t length = 0;
53 int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
54 &result, &length);
55 ASSERT (retval == 0);
56 ASSERT (length == strlen (expected));
57 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
58 free (result);
61 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
63 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
64 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
65 char *result = NULL;
66 size_t length = 0;
67 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
68 &result, &length);
69 ASSERT (retval == 0);
70 ASSERT (length == strlen (expected));
71 ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
72 free (result);
75 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
77 static const char input[] = "\342\202\254"; /* EURO SIGN */
78 char *result = NULL;
79 size_t length = 0;
80 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
81 &result, &length);
82 ASSERT (retval == -1 && errno == EILSEQ);
83 ASSERT (result == NULL);
86 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
88 static const char input[] = "\342";
89 char *result = NULL;
90 size_t length = 0;
91 int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
92 &result, &length);
93 ASSERT (retval == 0);
94 ASSERT (length == 0);
95 free (result);
98 /* ------------------------- Test str_cd_iconv() ------------------------- */
100 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
102 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
103 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
104 char *result = str_cd_iconv (input, cd_88591_to_utf8);
105 ASSERT (result != NULL);
106 ASSERT (strcmp (result, expected) == 0);
107 free (result);
110 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
112 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
113 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
114 char *result = str_cd_iconv (input, cd_utf8_to_88591);
115 ASSERT (result != NULL);
116 ASSERT (strcmp (result, expected) == 0);
117 free (result);
120 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
122 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
123 char *result = str_cd_iconv (input, cd_utf8_to_88591);
124 ASSERT (result == NULL && errno == EILSEQ);
127 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
129 static const char input[] = "\342";
130 char *result = str_cd_iconv (input, cd_utf8_to_88591);
131 ASSERT (result != NULL);
132 ASSERT (strcmp (result, "") == 0);
133 free (result);
136 iconv_close (cd_88591_to_utf8);
137 iconv_close (cd_utf8_to_88591);
139 /* -------------------------- Test str_iconv() -------------------------- */
141 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
143 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
144 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
145 char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
146 ASSERT (result != NULL);
147 ASSERT (strcmp (result, expected) == 0);
148 free (result);
151 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
153 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
154 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
155 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
156 ASSERT (result != NULL);
157 ASSERT (strcmp (result, expected) == 0);
158 free (result);
161 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
163 static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
164 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
165 ASSERT (result == NULL && errno == EILSEQ);
168 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
170 static const char input[] = "\342";
171 char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
172 ASSERT (result != NULL);
173 ASSERT (strcmp (result, "") == 0);
174 free (result);
177 #endif
179 return test_exit_status;