1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Test of character set conversion.
4 Copyright (C) 2007-2011 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
27 # define ICONV_CONST /* empty */
30 #include "signature.h"
31 SIGNATURE_CHECK (iconv, size_t, (iconv_t, ICONV_CONST char **, size_t *,
33 SIGNATURE_CHECK (iconv_close, int, (iconv_t x));
34 SIGNATURE_CHECK (iconv_open, iconv_t, (char const *, char const *));
47 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
49 iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
50 iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
52 ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
53 ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
55 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
57 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
58 static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
60 const char *inptr = input;
61 size_t inbytesleft = strlen (input);
63 size_t outbytesleft = sizeof (buf);
64 size_t res = iconv (cd_88591_to_utf8,
65 (ICONV_CONST char **) &inptr, &inbytesleft,
66 &outptr, &outbytesleft);
67 ASSERT (res == 0 && inbytesleft == 0);
68 ASSERT (outptr == buf + strlen (expected));
69 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
72 /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG. */
74 static const char input[] = "\304";
75 static char buf[2] = { (char)0xDE, (char)0xAD };
76 const char *inptr = input;
77 size_t inbytesleft = 1;
79 size_t outbytesleft = 1;
80 size_t res = iconv (cd_88591_to_utf8,
81 (ICONV_CONST char **) &inptr, &inbytesleft,
82 &outptr, &outbytesleft);
83 ASSERT (res == (size_t)(-1) && errno == E2BIG);
84 ASSERT (inbytesleft == 1);
85 ASSERT (outbytesleft == 1);
86 ASSERT ((unsigned char) buf[1] == 0xAD);
87 ASSERT ((unsigned char) buf[0] == 0xDE);
90 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
92 static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
93 static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
95 const char *inptr = input;
96 size_t inbytesleft = strlen (input);
98 size_t outbytesleft = sizeof (buf);
99 size_t res = iconv (cd_utf8_to_88591,
100 (ICONV_CONST char **) &inptr, &inbytesleft,
101 &outptr, &outbytesleft);
102 ASSERT (res == 0 && inbytesleft == 0);
103 ASSERT (outptr == buf + strlen (expected));
104 ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
107 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
109 static const char input[] = "\342\202\254"; /* EURO SIGN */
111 const char *inptr = input;
112 size_t inbytesleft = strlen (input);
114 size_t outbytesleft = sizeof (buf);
115 size_t res = iconv (cd_utf8_to_88591,
116 (ICONV_CONST char **) &inptr, &inbytesleft,
117 &outptr, &outbytesleft);
118 if (res == (size_t)(-1))
120 ASSERT (errno == EILSEQ);
121 ASSERT (inbytesleft == strlen (input) && outptr == buf);
126 ASSERT (inbytesleft == 0);
130 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
132 static const char input[] = "\342";
134 const char *inptr = input;
135 size_t inbytesleft = 1;
137 size_t outbytesleft = sizeof (buf);
138 size_t res = iconv (cd_utf8_to_88591,
139 (ICONV_CONST char **) &inptr, &inbytesleft,
140 &outptr, &outbytesleft);
141 ASSERT (res == (size_t)(-1) && errno == EINVAL);
142 ASSERT (inbytesleft == 1 && outptr == buf);
145 iconv_close (cd_88591_to_utf8);
146 iconv_close (cd_utf8_to_88591);