1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by GOTO Masanori <gotom@debian.or.jp>, 2004
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #define SIZE 256 /* enough room for conversion */
26 #define SAMPLESTR "abc"
40 /* test builtin transformation */
41 struct convcode testcode
[] = {
50 int number
= (int) sizeof (testcode
) / sizeof (struct convcode
);
53 convert (const char *tocode
, const char *fromcode
, char *inbufp
,
54 size_t inbytesleft
, char *outbufp
, size_t outbytesleft
)
57 size_t outbytes
= outbytesleft
;
60 ic
= iconv_open (tocode
, fromcode
);
61 if (ic
== (iconv_t
*) - 1)
63 printf ("iconv_open failed: from: %s, to: %s: %s",
64 fromcode
, tocode
, strerror (errno
));
68 while (inbytesleft
> 0)
70 ret
= iconv (ic
, &inbufp
, &inbytesleft
, &outbufp
, &outbytes
);
73 printf ("iconv failed: from: %s, to: %s: %s",
74 fromcode
, tocode
, strerror (errno
));
79 ret
= iconv_close (ic
);
82 printf ("iconv_close failed: from: %s, to: %s: %s",
83 fromcode
, tocode
, strerror (errno
));
87 return outbytesleft
- outbytes
;
92 test_unalign (struct convcode
*codes
, char *str
, int len
)
94 struct unalign
*inbufp
, *outbufp
;
96 size_t inbytesleft
, outbytesleft
;
99 /* allocating unaligned buffer for both inbuf and outbuf */
100 inbufp
= (struct unalign
*) malloc (sizeof (struct unalign
));
103 printf ("no memory available\n");
106 inbuf
= inbufp
->str2
;
108 outbufp
= (struct unalign
*) malloc (sizeof (struct unalign
));
111 printf ("no memory available\n");
114 outbuf
= outbufp
->str2
;
116 /* first iconv phase */
117 memcpy (inbuf
, str
, len
);
119 outbytesleft
= sizeof (struct unalign
);
120 retlen
= convert (codes
->tocode
, codes
->fromcode
, inbuf
, inbytesleft
,
121 outbuf
, outbytesleft
);
122 if (retlen
== -1) /* failed */
125 /* second round trip iconv phase */
126 memcpy (inbuf
, outbuf
, retlen
);
127 inbytesleft
= retlen
;
128 outbytesleft
= sizeof (struct unalign
);
129 retlen
= convert (codes
->fromcode
, codes
->tocode
, inbuf
, inbytesleft
,
130 outbuf
, outbytesleft
);
131 if (retlen
== -1) /* failed */
141 main (int argc
, char *argv
[])
146 for (i
= 0; i
< number
; i
++)
148 ret
= test_unalign (&testcode
[i
], (char *) SAMPLESTR
, sizeof (SAMPLESTR
));
151 printf ("iconv: %s <-> %s: ok\n",
152 testcode
[i
].fromcode
, testcode
[i
].tocode
);
154 printf ("Succeeded.\n");