Updated to fedora-glibc-20050504T1818
[glibc.git] / iconvdata / tst-e2big.c
blob8c4d273d011759e8c2233355ce22e17cb4cf6d1b
1 /* Test for a tricky E2BIG situation.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Bruno Haible <bruno@clisp.org>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <errno.h>
23 #include <iconv.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 /* In EUC-JISX0213 and TSCII, a single input character can convert to
29 a sequence of two or more Unicode characters. When the output buffer
30 has room for less Unicode characters than would be produced with an
31 unconstrained output buffer, the conversion must give errno = E2BIG. */
33 void
34 test (const char *encoding, char *inbuf, size_t inbufsize, size_t outbufsize)
36 char *outbuf = alloca (outbufsize);
37 iconv_t cd;
38 char *inptr;
39 size_t inlen;
40 char *outptr;
41 size_t outlen;
42 int result;
43 bool empty_input;
44 bool empty_output;
46 cd = iconv_open ("UTF-8", encoding);
47 if (cd == (iconv_t) -1)
49 fprintf (stderr, "cannot convert from %s\n", encoding);
50 exit (1);
53 inptr = inbuf;
54 inlen = inbufsize;
55 outptr = outbuf;
56 outlen = outbufsize;
58 result = iconv (cd, &inptr, &inlen, &outptr, &outlen);
59 if (!(result == -1 && errno == E2BIG))
61 fprintf (stderr, "%s: wrong iconv result: %d/%d (%m)\n",
62 encoding, result, errno);
63 exit (1);
65 empty_input = (inptr == inbuf && inlen == inbufsize);
66 empty_output = (outptr == outbuf && outlen == outbufsize);
68 if (!empty_input && empty_output)
70 fprintf (stderr, "%s: ate %td input bytes\n", encoding, inptr - inbuf);
71 exit (1);
73 if (empty_input && !empty_output)
75 fprintf (stderr, "%s: produced %td output bytes\n",
76 encoding, outptr - outbuf);
77 exit (1);
80 iconv_close (cd);
83 void
84 test_euc_jisx0213 (void)
86 char inbuf[2] = { 0xa4, 0xf7 };
87 test ("EUC-JISX0213", inbuf, sizeof (inbuf), 3);
90 void
91 test_tscii (void)
93 char inbuf[1] = { 0x82 };
94 test ("TSCII", inbuf, sizeof (inbuf), 3);
95 test ("TSCII", inbuf, sizeof (inbuf), 6);
96 test ("TSCII", inbuf, sizeof (inbuf), 9);
99 int
100 main (void)
102 test_euc_jisx0213 ();
103 test_tscii ();
104 return 0;