2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.26 2007-03-12 10:59:59 adam Exp $
17 #include <yaz/yaz-util.h>
20 static int compare_buffers(char *msg, int no,
21 int expect_len, const char *expect_buf,
22 int got_len, const char *got_buf)
24 if (expect_len == got_len
25 && !memcmp(expect_buf, got_buf, expect_len))
28 if (0) /* use 1 see how the buffers differ (for debug purposes) */
31 printf("tsticonv test=%s i=%d failed\n", msg, no);
32 printf("off got exp\n");
33 for (i = 0; i<got_len || i<expect_len; i++)
39 sprintf(got_char, "%02X", got_buf[i]);
41 sprintf(got_char, "? ");
44 sprintf(expect_char, "%02X", expect_buf[i]);
46 sprintf(expect_char, "? ");
48 printf("%02d %s %s %c\n",
49 i, got_char, expect_char, got_buf[i] == expect_buf[i] ?
57 static int tst_convert_l(yaz_iconv_t cd, size_t in_len, const char *in_buf,
58 size_t expect_len, const char *expect_buf)
61 char *inbuf= (char*) in_buf;
62 size_t inbytesleft = in_len > 0 ? in_len : strlen(in_buf);
64 char *outbuf = outbuf0;
68 size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
69 if (outbytesleft > 12)
71 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
72 if (r == (size_t) (-1))
74 int e = yaz_iconv_error(cd);
75 if (e != YAZ_ICONV_E2BIG)
81 return compare_buffers("tsticonv 22", 0,
82 expect_len, expect_buf,
83 outbuf - outbuf0, outbuf0);
86 static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
89 WRBUF b = wrbuf_alloc();
91 size_t inbytesleft = strlen(buf);
92 const char *inp = buf;
94 for (rounds = 0; inbytesleft && rounds < sizeof(outbuf); rounds++)
96 size_t outbytesleft = sizeof(outbuf);
98 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
99 &outp, &outbytesleft);
100 wrbuf_write(b, outbuf, outp - outbuf);
101 if (r == (size_t) (-1))
103 int e = yaz_iconv_error(cd);
104 if (e != YAZ_ICONV_E2BIG)
108 if (wrbuf_len(b) == strlen(cmpbuf)
109 && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
112 yaz_log(YLOG_LOG, "GOT (%.*s)", wrbuf_len(b), wrbuf_buf(b));
118 /* some test strings in ISO-8859-1 format */
119 static const char *iso_8859_1_a[] = {
128 static void tst_marc8_to_ucs4b(void)
130 yaz_iconv_t cd = yaz_iconv_open("UCS4", "MARC8");
135 YAZ_CHECK(tst_convert_l(
138 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
140 "\x00\x00\xFF\x1F" "\x00\x00\x00o"));
141 YAZ_CHECK(tst_convert_l(
144 "\033$1" "\x6F\x77\x29" /* AE0E */
145 "\x6F\x52\x7C" /* c0F4 */ "\033(B",
147 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4"));
148 YAZ_CHECK(tst_convert_l(
152 "\x21\x50\x6E" /* UCS 7CFB */
153 "\x21\x51\x31" /* UCS 7D71 */
154 "\x21\x3A\x67" /* UCS 5B89 */
155 "\x21\x33\x22" /* UCS 5168 */
156 "\x21\x33\x53" /* UCS 5206 */
157 "\x21\x44\x2B" /* UCS 6790 */
165 "\x00\x00\x67\x90"));
167 YAZ_CHECK(tst_convert_l(
170 "\xB0\xB2", /* AYN and oSLASH */
172 "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"));
173 YAZ_CHECK(tst_convert_l(
176 "\xF6\x61", /* a underscore */
178 "\x00\x00\x00\x61" "\x00\x00\x03\x32"));
180 YAZ_CHECK(tst_convert_l(
183 "\x61\xC2", /* a, phonorecord mark */
185 "\x00\x00\x00\x61" "\x00\x00\x21\x17"));
188 YAZ_CHECK(tst_convert_l(
191 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
200 "\x00\x00\x00" "n"));
202 YAZ_CHECK(tst_convert_l(
207 "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"));
209 YAZ_CHECK(tst_convert_l(
214 "\x00\x00\x00\x74" "\x00\x00\x03\x61" "\x00\x00\x00\x73"));
216 YAZ_CHECK(tst_convert_l(
221 "\x00\x00\x00\x74" "\x00\x00\x03\x60" "\x00\x00\x00\x73"));
226 static void tst_ucs4b_to_utf8(void)
228 yaz_iconv_t cd = yaz_iconv_open("UTF8", "UCS4");
232 YAZ_CHECK(tst_convert_l(
235 "\x00\x00\xFF\x1F\x00\x00\x00o",
237 "\xEF\xBC\x9F\x6F"));
239 YAZ_CHECK(tst_convert_l(
242 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
244 "\xEA\xB8\x8E\xEC\x83\xB4"));
248 static void dconvert(int mandatory, const char *tmpcode)
253 for (i = 0; iso_8859_1_a[i]; i++)
256 char *inbuf = (char*) iso_8859_1_a[i];
257 size_t inbytesleft = strlen(inbuf);
260 char *outbuf = outbuf0;
261 size_t outbytesleft = sizeof(outbuf0);
263 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
264 YAZ_CHECK(cd || !mandatory);
267 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
268 YAZ_CHECK(r != (size_t) (-1));
270 if (r == (size_t) (-1))
273 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
274 YAZ_CHECK(cd || !mandatory);
278 inbytesleft = sizeof(outbuf0) - outbytesleft;
281 outbytesleft = sizeof(outbuf1);
282 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
283 YAZ_CHECK(r != (size_t) (-1));
284 if (r != (size_t)(-1))
286 ret = compare_buffers("dconvert", i,
287 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
288 sizeof(outbuf1) - outbytesleft, outbuf1);
295 int utf8_check(unsigned c)
304 size_t inbytesleft = 4;
305 char *outbuf = utf8buf;
306 size_t outbytesleft = sizeof(utf8buf);
308 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "UCS4LE");
311 for (i = 0; i<4; i++)
314 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
317 if (r == (size_t)(-1))
320 cd = yaz_iconv_open("UCS4LE", "UTF-8");
323 inbytesleft = sizeof(utf8buf) - outbytesleft;
329 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
330 if (r == (size_t)(-1))
335 if (memcmp(src, dst, 4))
341 static void tst_marc8_to_utf8(void)
343 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8");
349 YAZ_CHECK(tst_convert(cd, "Cours de math",
351 /* COMBINING ACUTE ACCENT */
352 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
353 "Cours de mathe\xcc\x81"));
356 YAZ_CHECK(tst_convert(cd, "a\xea\x1e", "a\x1e\xcc\x8a"));
358 YAZ_CHECK(tst_convert(cd, "a\xea", "a"));
362 static void tst_marc8s_to_utf8(void)
364 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8s");
370 YAZ_CHECK(tst_convert(cd, "Cours de math",
372 /* E9: LATIN SMALL LETTER E WITH ACUTE */
373 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
374 "Cours de math\xc3\xa9"));
380 static void tst_marc8_to_latin1(void)
382 yaz_iconv_t cd = yaz_iconv_open("ISO-8859-1", "MARC8");
388 YAZ_CHECK(tst_convert(cd, "ax", "ax"));
390 /* latin capital letter o with stroke */
391 YAZ_CHECK(tst_convert(cd, "\xa2", "\xd8"));
393 /* with latin small letter ae */
394 YAZ_CHECK(tst_convert(cd, "eneb\xb5r", "eneb\346r"));
396 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2", "\xe5" "\xd8"));
398 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2" "b", "\xe5" "\xd8" "b"));
400 YAZ_CHECK(tst_convert(cd, "\xea" "a" "\xea" "a", "\xe5" "\xe5"));
402 YAZ_CHECK(tst_convert(cd, "Cours de math",
404 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
406 YAZ_CHECK(tst_convert(cd, "12345678âe",
408 YAZ_CHECK(tst_convert(cd, "123456789âe",
410 YAZ_CHECK(tst_convert(cd, "1234567890âe",
412 YAZ_CHECK(tst_convert(cd, "12345678901âe",
414 YAZ_CHECK(tst_convert(cd, "Cours de mathâem",
416 YAZ_CHECK(tst_convert(cd, "Cours de mathâematiques",
417 "Cours de mathématiques"));
422 static void tst_utf8_to_marc8(void)
424 yaz_iconv_t cd = yaz_iconv_open("MARC8", "UTF-8");
430 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
432 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
433 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
435 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
436 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
438 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
439 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
441 /** UPPERCASE SCANDINAVIAN O */
442 YAZ_CHECK(tst_convert(cd, "S\xc3\x98", "S\xa2"));
445 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x8A", "\xEA" "A"));
447 /** A MACRON + UMLAUT, DIAERESIS */
448 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x84" "\xCC\x88",
451 /* Ligature spanning two characters */
452 YAZ_CHECK(tst_convert(cd,
453 "\x74" "\xCD\xA1" "\x73", /* UTF-8 */
454 "\xEB\x74\xEC\x73")); /* MARC-8 */
456 /* Double title spanning two characters */
457 YAZ_CHECK(tst_convert(cd,
458 "\x74" "\xCD\xA0" "\x73", /* UTF-8 */
459 "\xFA\x74\xFB\x73")); /* MARC-8 */
461 /** Ideographic question mark (Unicode FF1F) */
462 YAZ_CHECK(tst_convert(cd,
463 "\xEF\xBC\x9F" "o", /* UTF-8 */
464 "\033$1" "\x21\x2B\x3B" "\033(B" "o" ));
467 /** Superscript 0 . bug #642 */
468 YAZ_CHECK(tst_convert(cd,
469 "(\xe2\x81\xb0)", /* UTF-8 */
476 static void tst_advance_to_utf8(void)
478 yaz_iconv_t cd = yaz_iconv_open("utf-8", "advancegreek");
484 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
488 static void tst_utf8_to_advance(void)
490 yaz_iconv_t cd = yaz_iconv_open("advancegreek", "utf-8");
496 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
500 static void tst_latin1_to_marc8(void)
502 yaz_iconv_t cd = yaz_iconv_open("MARC8", "ISO-8859-1");
508 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
510 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
511 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
513 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
514 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
516 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
517 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
519 /** D8: UPPERCASE SCANDINAVIAN O */
520 YAZ_CHECK(tst_convert(cd, "S\xd8", "S\xa2"));
522 /** E9: LATIN SMALL LETTER E WITH ACUTE */
523 YAZ_CHECK(tst_convert(cd, "Cours de math\xe9", "Cours de mathâe"));
524 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"
526 YAZ_CHECK(tst_convert(cd, "Cours de mathé", "Cours de mathâe" ));
527 YAZ_CHECK(tst_convert(cd, "12345678é","12345678âe"));
528 YAZ_CHECK(tst_convert(cd, "123456789é", "123456789âe"));
529 YAZ_CHECK(tst_convert(cd, "1234567890é","1234567890âe"));
530 YAZ_CHECK(tst_convert(cd, "12345678901é", "12345678901âe"));
531 YAZ_CHECK(tst_convert(cd, "Cours de mathém", "Cours de mathâem"));
532 YAZ_CHECK(tst_convert(cd, "Cours de mathématiques",
533 "Cours de mathâematiques"));
537 static void tst_utf8_codes(void)
539 YAZ_CHECK(utf8_check(3));
540 YAZ_CHECK(utf8_check(127));
541 YAZ_CHECK(utf8_check(128));
542 YAZ_CHECK(utf8_check(255));
543 YAZ_CHECK(utf8_check(256));
544 YAZ_CHECK(utf8_check(900));
545 YAZ_CHECK(utf8_check(1000));
546 YAZ_CHECK(utf8_check(10000));
547 YAZ_CHECK(utf8_check(100000));
548 YAZ_CHECK(utf8_check(1000000));
549 YAZ_CHECK(utf8_check(10000000));
550 YAZ_CHECK(utf8_check(100000000));
553 int main (int argc, char **argv)
555 YAZ_CHECK_INIT(argc, argv);
561 tst_marc8s_to_utf8();
563 tst_marc8_to_latin1();
565 tst_advance_to_utf8();
566 tst_utf8_to_advance();
570 tst_latin1_to_marc8();
572 tst_marc8_to_ucs4b();
575 dconvert(1, "UTF-8");
576 dconvert(1, "ISO-8859-1");
578 dconvert(1, "UCS4LE");
579 dconvert(0, "CP865");
586 * indent-tabs-mode: nil
588 * vim: shiftwidth=4 tabstop=8 expandtab