2 * Copyright (c) 2002-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.4 2004-11-16 22:51:52 adam Exp $
16 #include <yaz/yaz-util.h>
18 /* some test strings in ISO-8859-1 format */
19 static const char *iso_8859_1_a[] = {
25 /* same test strings in MARC-8 format */
26 static const char *marc8_a[] = {
28 "\xa2", /* latin capital letter o with stroke */
29 "eneb\xb5r", /* latin small letter ae */
33 static void marc8_tst_a()
38 cd = yaz_iconv_open("ISO-8859-1", "MARC8");
41 printf("tsticonv 10 yaz_iconv_open failed\n");
44 for (i = 0; iso_8859_1_a[i]; i++)
47 char *inbuf= (char*) marc8_a[i];
48 size_t inbytesleft = strlen(inbuf);
50 char *outbuf = outbuf0;
51 size_t outbytesleft = sizeof(outbuf0);
53 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
54 if (r == (size_t) (-1))
56 int e = yaz_iconv_error(cd);
58 printf ("tsticonv 11 i=%d e=%d\n", i, e);
61 if ((outbuf - outbuf0) != strlen(iso_8859_1_a[i])
62 || memcmp(outbuf0, iso_8859_1_a[i],
63 strlen(iso_8859_1_a[i])))
65 printf ("tsticonv 12 i=%d\n", i);
66 printf ("buf=%s out=%s\n", iso_8859_1_a[i], outbuf0);
73 static void marc8_tst_b()
75 static const char *marc8_b[] = {
77 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
79 "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
82 "\x21\x50\x6E" /* UCS 7CFB */
83 "\x21\x51\x31" /* UCS 7D71 */
84 "\x21\x3A\x67" /* UCS 5B89 */
85 "\x21\x33\x22" /* UCS 5168 */
86 "\x21\x33\x53" /* UCS 5206 */
87 "\x21\x44\x2B" /* UCS 6790 */
90 "\xB0\xB2", /* AYN and oSLASH */
92 "\xF6\x61", /* a underscore */
94 "\x61\xC2", /* a, phonorecord mark */
97 static const char *ucs4_b[] = {
98 "\x00\x00\xFF\x1F" "\x00\x00\x00o",
99 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
106 "\x00\x00\x02\xBB" "\x00\x00\x00\xF8",
107 "\x00\x00\x00\x61" "\x00\x00\x03\x32",
108 "\x00\x00\x00\x61" "\x00\x00\x21\x17",
114 cd = yaz_iconv_open("UCS4", "MARC8");
117 printf ("tsticonv 20 yaz_iconv_open failed\n");
120 for (i = 0; marc8_b[i]; i++)
124 size_t expect_len = i == 2 ? 24 : 8;
125 char *inbuf= (char*) marc8_b[i];
126 size_t inbytesleft = strlen(inbuf);
128 char *outbuf = outbuf0;
129 size_t outbytesleft = sizeof(outbuf0);
131 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
132 if (r == (size_t) (-1))
134 int e = yaz_iconv_error(cd);
136 printf ("tsticonv 21 i=%d e=%d\n", i, e);
139 len = outbuf - outbuf0;
140 if (len != expect_len || memcmp(outbuf0, ucs4_b[i], len))
142 printf ("tsticonv 22 len=%d gotlen=%d i=%d\n", expect_len, len, i);
149 static void marc8_tst_c()
151 static const char *ucs4_c[] = {
152 "\x00\x00\xFF\x1F\x00\x00\x00o",
153 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
156 static const char *utf8_c[] = {
158 "\xEA\xB8\x8E\xEC\x83\xB4",
165 cd = yaz_iconv_open("UTF8", "UCS4");
168 printf ("tsticonv 30 yaz_iconv_open failed\n");
171 for (i = 0; ucs4_c[i]; i++)
175 char *inbuf= (char*) ucs4_c[i];
176 size_t inbytesleft = 8;
178 char *outbuf = outbuf0;
179 size_t outbytesleft = sizeof(outbuf0);
181 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
182 if (r == (size_t) (-1))
184 int e = yaz_iconv_error(cd);
186 printf ("tsticonv 31 i=%d e=%d\n", i, e);
189 len = outbuf - outbuf0;
190 if (len != strlen(utf8_c[i]) || memcmp(outbuf0, utf8_c[i], len))
192 printf ("tsticonv 32 len=%d gotlen=%d i=%d\n",
193 strlen(utf8_c[i]), len, i);
200 static void dconvert(int mandatory, const char *tmpcode)
204 for (i = 0; iso_8859_1_a[i]; i++)
207 char *inbuf = (char*) iso_8859_1_a[i];
208 size_t inbytesleft = strlen(inbuf);
211 char *outbuf = outbuf0;
212 size_t outbytesleft = sizeof(outbuf0);
214 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
219 printf ("tsticonv code=%s 1\n", tmpcode);
222 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
223 if (r == (size_t)(-1))
225 int e = yaz_iconv_error(cd);
227 printf ("tsticonv code=%s 2 e=%d\n", tmpcode, e);
232 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
237 printf ("tsticonv code=%s 3\n", tmpcode);
241 inbytesleft = sizeof(outbuf0) - outbytesleft;
244 outbytesleft = sizeof(outbuf1);
245 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
246 if (r == (size_t)(-1)) {
247 int e = yaz_iconv_error(cd);
249 printf ("tsticonv code=%s 4 e=%d\n", tmpcode, e);
252 if (strlen(iso_8859_1_a[i]) ==
253 (sizeof(outbuf1) - outbytesleft) &&
254 memcmp(outbuf1, iso_8859_1_a[i],
255 strlen(iso_8859_1_a[i])))
257 printf ("tsticonv code=%s 5\n", tmpcode);
264 int main (int argc, char **argv)
266 dconvert(1, "UTF-8");
267 dconvert(1, "ISO-8859-1");
269 dconvert(1, "UCS4LE");
270 dconvert(0, "CP865");