2 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/endian.h>
31 #include <sys/types.h>
41 * iconv_open must return (iconv_t)-1 on non-existing encoding
42 * and set errno to EINVAL.
50 cd = iconv_open("nonexisting", "foobar");
52 if ((cd == (iconv_t)-1) && (errno == EINVAL))
61 * iconv_open must return (iconv_t)-1 if too much files are open
62 * and set errno to ENFILE.
64 #define MAX_LIMIT 1025
68 iconv_t cd[MAX_LIMIT];
72 for (i = 0; i < MAX_LIMIT; i++) {
73 cd[i] = iconv_open("ASCII", "UTF8");
74 if (cd[i] == (iconv_t)-1)
78 ret = (cd[i] == (iconv_t)-1) && ((errno == ENFILE) ||
79 (errno == EMFILE)) ? 0 : 1;
86 * iconv_close must return (iconv_t)-1 if conversion descriptor is
87 * invalid and set errno to EBADF.
92 iconv_t cd = (iconv_t)-1;
94 return ((iconv_close(cd) == -1) && (errno = EBADF) ? 0 : 1);
100 iconv_t cd = (iconv_t)-1;
103 return ((iconv(cd, NULL, 0, NULL, 0) == (size_t)-1 && errno == EBADF) ? 0 : 1);
110 size_t inbytesleft, outbytesleft;
114 uint32_t inbuf[2] = { 0x00000151, 0x00000171 };
116 if ((cd = iconv_open("ASCII", "UTF-32LE")) == (iconv_t)-1)
119 inptr = (const char *)inbuf;
120 outptr = (char *)outbuf;
124 return (iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft) == 2 ? 0 : 1);
131 size_t inbytesleft, outbytesleft;
138 if ((cd = iconv_open("ASCII", "ASCII")) == (iconv_t)-1)
141 inptr = (const char *)inbuf;
142 outptr = (char *)outbuf;
147 ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
150 printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
151 printf("inbytesleft = %d\n", inbytesleft);
152 printf("outbytesleft = %d\n", outbytesleft);
153 printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
154 printf("errno = %d\n", errno);
155 printf("ret = %d\n", (int)ret);
158 if (((const uint8_t *)inptr - (uint8_t *)inbuf == 8) && (inbytesleft == 8) &&
159 (outbytesleft == 0) && ((uint8_t *)outptr - (uint8_t *)outbuf == 8) &&
160 (errno == E2BIG) && ((size_t)ret == (size_t)-1))
170 size_t inbytesleft, outbytesleft;
174 uint16_t inbuf[1] = { 0xEA42 };
177 if ((cd = iconv_open("UTF-32", "BIG5")) == (iconv_t)-1)
180 inptr = (const char *)inbuf;
181 outptr = (char *)outbuf;
186 ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
189 printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
190 printf("inbytesleft = %d\n", inbytesleft);
191 printf("outbytesleft = %d\n", outbytesleft);
192 printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
193 printf("errno = %d\n", errno);
194 printf("ret = %d\n", (int)ret);
197 if (((const uint8_t *)inptr - (uint8_t *)inbuf == 1) && (inbytesleft == 1) &&
198 (outbytesleft == 8) && ((uint8_t *)outptr - (uint8_t *)outbuf == 8) &&
199 (errno == EINVAL) && ((size_t)ret == (size_t)-1))
209 size_t inbytesleft, outbytesleft;
213 uint16_t inbuf[1] = { 0x8AC0 };
216 if ((cd = iconv_open("Latin2", "UTF-16LE")) == (iconv_t)-1)
219 inptr = (const char *)inbuf;
220 outptr = (char *)outbuf;
225 ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
228 printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
229 printf("inbytesleft = %d\n", inbytesleft);
230 printf("outbytesleft = %d\n", outbytesleft);
231 printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
232 printf("errno = %d\n", errno);
233 printf("ret = %d\n", (int)ret);
236 if (((const uint8_t *)inptr - (uint8_t *)inbuf == 0) && (inbytesleft == 4) &&
237 (outbytesleft == 16) && ((uint8_t *)outptr - (uint8_t *)outbuf == 0) &&
238 (errno == EILSEQ) && ((size_t)ret == (size_t)-1))
245 test(int (tester) (void), const char * label)
249 if ((ret = tester()))
250 printf("%s failed (%d)\n", label, ret);
252 printf("%s succeeded\n", label);
259 test(open_1, "open_1");
260 test(open_2, "open_2");
261 test(close_1, "close_1");
262 test(conv_ret, "conv_ret");
263 test(conv_ebadf, "conv_ebadf");
264 test(conv_2big, "conv_2big");
265 test(conv_einval, "conv_einval");
266 test(conv_eilseq, "conv_eilseq");