No more manifest files
[yaz-moved-to-github.git] / test / test_iconv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <string.h>
12
13 #include <yaz/yaz-util.h>
14 #include <yaz/test.h>
15
16 #define ESC "\x1b"
17
18 static int compare_buffers(char *msg, int no,
19                            int expect_len, const char *expect_buf,
20                            int got_len, const char *got_buf)
21 {
22     if (expect_len == got_len
23         && !memcmp(expect_buf, got_buf, expect_len))
24         return 1;
25
26     if (0) /* use 1 see how the buffers differ (for debug purposes) */
27     {
28         int i;
29         printf("tsticonv test=%s i=%d failed\n", msg, no);
30         printf("off got exp\n");
31         for (i = 0; i<got_len || i<expect_len; i++)
32         {
33             char got_char[10];
34             char expect_char[10];
35
36             if (i < got_len)
37                 sprintf(got_char, "%02X", got_buf[i]);
38             else
39                 sprintf(got_char, "?  ");
40
41             if (i < expect_len)
42                 sprintf(expect_char, "%02X", expect_buf[i]);
43             else
44                 sprintf(expect_char, "?  ");
45
46             printf("%02d  %s  %s %c\n",
47                    i, got_char, expect_char, got_buf[i] == expect_buf[i] ?
48                    ' ' : '*');
49
50         }
51     }
52     return 0;
53 }
54
55 static int tst_convert_l(yaz_iconv_t cd, size_t in_len, const char *in_buf,
56                          size_t expect_len, const char *expect_buf)
57 {
58     size_t r;
59     char *inbuf= (char*) in_buf;
60     size_t inbytesleft = in_len > 0 ? in_len : strlen(in_buf);
61     char outbuf0[64];
62     char *outbuf = outbuf0;
63
64     while (inbytesleft)
65     {
66         size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
67         if (outbytesleft > 12)
68             outbytesleft = 12;
69         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
70         if (r == (size_t) (-1))
71         {
72             int e = yaz_iconv_error(cd);
73             if (e != YAZ_ICONV_E2BIG)
74                 return 0;
75         }
76         else
77         {
78             yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
79             break;
80         }
81     }
82
83     return compare_buffers("tsticonv 22", 0,
84                            expect_len, expect_buf,
85                            outbuf - outbuf0, outbuf0);
86 }
87
88 static int tst_convert_x(yaz_iconv_t cd, const char *buf, const char *cmpbuf,
89                          int expect_error)
90 {
91     int ret = 1;
92     WRBUF b = wrbuf_alloc();
93     char outbuf[16];
94     size_t inbytesleft = strlen(buf);
95     const char *inp = buf;
96     int rounds = 0;
97     for (rounds = 0; inbytesleft && rounds < (int) sizeof(outbuf); rounds++)
98     {
99         size_t outbytesleft = sizeof(outbuf);
100         char *outp = outbuf;
101         size_t r = yaz_iconv(cd, (char**) &inp,  &inbytesleft,
102                              &outp, &outbytesleft);
103         wrbuf_write(b, outbuf, outp - outbuf);
104         if (r == (size_t) (-1))
105         {
106             int e = yaz_iconv_error(cd);
107             if (e != YAZ_ICONV_E2BIG)
108             {
109                 if (expect_error != -1)
110                     if (e != expect_error)
111                         ret = 0;
112                 break;
113             }
114         }
115         else
116         {
117             size_t outbytesleft = sizeof(outbuf);
118             char *outp = outbuf;
119             r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
120             wrbuf_write(b, outbuf, outp - outbuf);
121             if (expect_error != -1)
122                 if (expect_error)
123                     ret = 0;
124             break;
125         }
126     }
127     if (wrbuf_len(b) == strlen(cmpbuf)
128         && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
129         ;
130     else
131     {
132         WRBUF w = wrbuf_alloc();
133
134         ret = 0;
135         wrbuf_rewind(w);
136         wrbuf_puts_escaped(w, buf);
137         yaz_log(YLOG_LOG, "input %s", wrbuf_cstr(w));
138
139         wrbuf_rewind(w);
140         wrbuf_write_escaped(w, wrbuf_buf(b), wrbuf_len(b));
141         yaz_log(YLOG_LOG, "got %s", wrbuf_cstr(w));
142
143         wrbuf_rewind(w);
144         wrbuf_puts_escaped(w, cmpbuf);
145         yaz_log(YLOG_LOG, "exp %s", wrbuf_cstr(w));
146
147         wrbuf_destroy(w);
148     }
149
150     wrbuf_destroy(b);
151     return ret;
152 }
153
154 static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
155 {
156     return tst_convert_x(cd, buf, cmpbuf, 0);
157 }
158
159 static void tst_marc8_to_ucs4b(void)
160 {
161     yaz_iconv_t cd = yaz_iconv_open("UCS4", "MARC8");
162     YAZ_CHECK(cd);
163     if (!cd)
164         return;
165
166     YAZ_CHECK(tst_convert_l(
167                   cd,
168                   0,
169                   "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
170                   8,
171                   "\x00\x00\xFF\x1F" "\x00\x00\x00o"));
172     YAZ_CHECK(tst_convert_l(
173                   cd,
174                   0,
175                   "\033$1" "\x6F\x77\x29" /* AE0E */
176                   "\x6F\x52\x7C" /* c0F4 */ "\033(B",
177                   8,
178                   "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4"));
179     YAZ_CHECK(tst_convert_l(
180                   cd,
181                   0,
182                   "\033$1"
183                   "\x21\x50\x6E"  /* UCS 7CFB */
184                   "\x21\x51\x31"  /* UCS 7D71 */
185                   "\x21\x3A\x67"  /* UCS 5B89 */
186                   "\x21\x33\x22"  /* UCS 5168 */
187                   "\x21\x33\x53"  /* UCS 5206 */
188                   "\x21\x44\x2B"  /* UCS 6790 */
189                   "\033(B",
190                   24,
191                   "\x00\x00\x7C\xFB"
192                   "\x00\x00\x7D\x71"
193                   "\x00\x00\x5B\x89"
194                   "\x00\x00\x51\x68"
195                   "\x00\x00\x52\x06"
196                   "\x00\x00\x67\x90"));
197
198     YAZ_CHECK(tst_convert_l(
199                   cd,
200                   0,
201                   "\xB0\xB2",     /* AYN and oSLASH */
202                   8,
203                   "\x00\x00\x02\xBB"  "\x00\x00\x00\xF8"));
204     YAZ_CHECK(tst_convert_l(
205                   cd,
206                   0,
207                   "\xF6\x61",     /* a underscore */
208                   8,
209                   "\x00\x00\x00\x61"  "\x00\x00\x03\x32"));
210
211     YAZ_CHECK(tst_convert_l(
212                   cd,
213                   0,
214                   "\x61\xC2",     /* a, phonorecord mark */
215                   8,
216                   "\x00\x00\x00\x61"  "\x00\x00\x21\x17"));
217
218     /* bug #258 */
219     YAZ_CHECK(tst_convert_l(
220                   cd,
221                   0,
222                   "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
223                   32,
224                   "\x00\x00\x00" "e"
225                   "\x00\x00\x00" "l"
226                   "\x00\x00\x00" "a"
227                   "\x00\x00\x03\x08"
228                   "\x00\x00\x00" "m"
229                   "\x00\x00\x00" "a"
230                   "\x00\x00\x03\x08"
231                   "\x00\x00\x00" "n"));
232     /* bug #260 */
233     YAZ_CHECK(tst_convert_l(
234                   cd,
235                   0,
236                   "\xe5\xe8\x41",
237                   12,
238                   "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"));
239     /* bug #416 */
240     YAZ_CHECK(tst_convert_l(
241                   cd,
242                   0,
243                   "\xEB\x74\xEC\x73",
244                   12,
245                   "\x00\x00\x00\x74" "\x00\x00\x03\x61" "\x00\x00\x00\x73"));
246     /* bug #416 */
247     YAZ_CHECK(tst_convert_l(
248                   cd,
249                   0,
250                   "\xFA\x74\xFB\x73",
251                   12,
252                   "\x00\x00\x00\x74" "\x00\x00\x03\x60" "\x00\x00\x00\x73"));
253
254     yaz_iconv_close(cd);
255 }
256
257 static void tst_ucs4b_to_utf8(void)
258 {
259     yaz_iconv_t cd = yaz_iconv_open("UTF8", "UCS4");
260     YAZ_CHECK(cd);
261     if (!cd)
262         return;
263     YAZ_CHECK(tst_convert_l(
264                   cd,
265                   8,
266                   "\x00\x00\xFF\x1F\x00\x00\x00o",
267                   4,
268                   "\xEF\xBC\x9F\x6F"));
269
270     YAZ_CHECK(tst_convert_l(
271                   cd,
272                   8,
273                   "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
274                   6,
275                   "\xEA\xB8\x8E\xEC\x83\xB4"));
276     yaz_iconv_close(cd);
277 }
278
279 static void dconvert(int mandatory, const char *tmpcode)
280 {
281     /* some test strings in ISO-8859-1 format */
282     static const char *iso_8859_1_a[] = {
283         "ax" ,
284         "\xd8",
285         "eneb\346r",
286         "\xe5" "\xd8",
287         "\xe5" "\xd8" "b",
288         "\xe5" "\xe5",
289         0 };
290     int i;
291     int ret;
292     yaz_iconv_t cd;
293     for (i = 0; iso_8859_1_a[i]; i++)
294     {
295         size_t r;
296         char *inbuf = (char*) iso_8859_1_a[i];
297         size_t inbytesleft = strlen(inbuf);
298         char outbuf0[24];
299         char outbuf1[10];
300         char *outbuf = outbuf0;
301         size_t outbytesleft = sizeof(outbuf0);
302
303         cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
304         YAZ_CHECK(cd || !mandatory);
305         if (!cd)
306             return;
307         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
308         YAZ_CHECK(r != (size_t) (-1));
309
310         r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
311         YAZ_CHECK(r != (size_t) (-1));
312         yaz_iconv_close(cd);
313         if (r == (size_t) (-1))
314             return;
315
316         cd = yaz_iconv_open("ISO-8859-1", tmpcode);
317         YAZ_CHECK(cd || !mandatory);
318         if (!cd)
319             return;
320         inbuf = outbuf0;
321         inbytesleft = sizeof(outbuf0) - outbytesleft;
322
323         outbuf = outbuf1;
324         outbytesleft = sizeof(outbuf1);
325         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
326         YAZ_CHECK(r != (size_t) (-1));
327
328         r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
329         if (r == (size_t)(-1))
330         {
331             fprintf(stderr, "failed\n");
332         }
333         YAZ_CHECK(r != (size_t) (-1));
334
335         if (r != (size_t)(-1))
336         {
337             ret = compare_buffers("dconvert", i,
338                                   strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
339                                   sizeof(outbuf1) - outbytesleft, outbuf1);
340             YAZ_CHECK(ret);
341         }
342         yaz_iconv_close(cd);
343     }
344 }
345
346 int utf8_check(unsigned c)
347 {
348     if (sizeof(c) >= 4)
349     {
350         size_t r;
351         char src[4];
352         char dst[4];
353         char utf8buf[6];
354         char *inbuf = src;
355         size_t inbytesleft = 4;
356         char *outbuf = utf8buf;
357         size_t outbytesleft = sizeof(utf8buf);
358         int i;
359         yaz_iconv_t cd = yaz_iconv_open("UTF-8", "UCS4LE");
360         if (!cd)
361             return 0;
362         for (i = 0; i<4; i++)
363             src[i] = c >> (i*8);
364
365         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
366         yaz_iconv_close(cd);
367
368         if (r == (size_t)(-1))
369             return 0;
370
371         cd = yaz_iconv_open("UCS4LE", "UTF-8");
372         if (!cd)
373             return 0;
374         inbytesleft = sizeof(utf8buf) - outbytesleft;
375         inbuf = utf8buf;
376
377         outbuf = dst;
378         outbytesleft = 4;
379
380         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
381         if (r == (size_t)(-1))
382             return 0;
383
384         yaz_iconv_close(cd);
385
386         if (memcmp(src, dst, 4))
387             return 0;
388     }
389     return 1;
390 }
391
392 static void tst_marc8_to_utf8(void)
393 {
394     yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8");
395
396     YAZ_CHECK(cd);
397     if (!cd)
398         return;
399
400     YAZ_CHECK(tst_convert(cd, "Cours de math",
401                           "Cours de math"));
402     /* COMBINING ACUTE ACCENT */
403     YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
404                           "Cours de mathe\xcc\x81"));
405
406     YAZ_CHECK(tst_convert(cd, "\xea" "a", "a\xcc\x8a"));
407     YAZ_CHECK(tst_convert(cd, "a" "\xea" "\x1e", "a" "\x1e\xcc\x8a"));
408     YAZ_CHECK(tst_convert(cd, "a" "\xea" "p", "a" "p\xcc\x8a"));
409
410     YAZ_CHECK(tst_convert_x(cd, "a\xea", "a", YAZ_ICONV_EINVAL));
411     YAZ_CHECK(tst_convert(cd, "p", "\xcc\x8a")); /* note: missing p */
412     yaz_iconv(cd, 0, 0, 0, 0);     /* incomplete. so we have to reset */
413
414     /* bug #2115 */
415     YAZ_CHECK(tst_convert(cd, ESC "(N" ESC ")Qp" ESC "(B", "\xd0\x9f"));
416
417     YAZ_CHECK(tst_convert_x(cd, ESC , "", YAZ_ICONV_EINVAL));
418     YAZ_CHECK(tst_convert_x(cd, ESC "(", "", YAZ_ICONV_EINVAL));
419     YAZ_CHECK(tst_convert_x(cd, ESC "(B", "", 0));
420
421     YAZ_CHECK(tst_convert(cd, ESC "(B" "\x31", "1"));  /* ASCII in G0 */
422     YAZ_CHECK(tst_convert(cd, ESC ")B" "\xB1", "1"));  /* ASCII in G1 */
423
424     yaz_iconv_close(cd);
425 }
426
427 static void tst_marc8s_to_utf8(void)
428 {
429     yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8s");
430
431     YAZ_CHECK(cd);
432     if (!cd)
433         return;
434
435     YAZ_CHECK(tst_convert(cd, "Cours de math",
436                           "Cours de math"));
437     /* E9: LATIN SMALL LETTER E WITH ACUTE */
438     YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
439                           "Cours de math\xc3\xa9"));
440
441     yaz_iconv_close(cd);
442 }
443
444
445 static void tst_marc8_to_latin1(void)
446 {
447     yaz_iconv_t cd = yaz_iconv_open("ISO-8859-1", "MARC8");
448
449     YAZ_CHECK(cd);
450     if (!cd)
451         return;
452
453     YAZ_CHECK(tst_convert(cd, "ax", "ax"));
454
455     /* latin capital letter o with stroke */
456     YAZ_CHECK(tst_convert(cd, "\xa2", "\xd8"));
457
458     /* with latin small letter ae */
459     YAZ_CHECK(tst_convert(cd, "eneb\xb5r", "eneb\346r"));
460
461     YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2", "\xe5" "\xd8"));
462
463     YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2" "b", "\xe5" "\xd8" "b"));
464
465     YAZ_CHECK(tst_convert(cd, "\xea" "a"  "\xea" "a", "\xe5" "\xe5"));
466
467     YAZ_CHECK(tst_convert(cd, "Cours de math",
468                           "Cours de math"));
469     YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
470                           "Cours de mathé"));
471     YAZ_CHECK(tst_convert(cd, "12345678âe",
472                           "12345678é"));
473     YAZ_CHECK(tst_convert(cd, "123456789âe",
474                           "123456789é"));
475     YAZ_CHECK(tst_convert(cd, "1234567890âe",
476                           "1234567890é"));
477     YAZ_CHECK(tst_convert(cd, "12345678901âe",
478                           "12345678901é"));
479     YAZ_CHECK(tst_convert(cd, "Cours de mathâem",
480                           "Cours de mathém"));
481     YAZ_CHECK(tst_convert(cd, "Cours de mathâematiques",
482                           "Cours de mathématiques"));
483
484     yaz_iconv_close(cd);
485 }
486
487 static void tst_utf8_to_marc8(const char *marc8_type)
488 {
489     yaz_iconv_t cd = yaz_iconv_open(marc8_type, "UTF-8");
490
491     YAZ_CHECK(cd);
492     if (!cd)
493         return;
494
495     YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
496
497     /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
498     YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
499
500     /** Pure ASCII. 12 characters (sizeof(outbuf)) */
501     YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
502
503     /** Pure ASCII. 13 characters (sizeof(outbuf)+1) */
504     YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
505
506     /** UPPERCASE SCANDINAVIAN O */
507     YAZ_CHECK(tst_convert(cd, "S\xc3\x98", "S\xa2"));
508
509     /** ARING (NFD) */
510     YAZ_CHECK(tst_convert(cd, "A" "\xCC\x8A", "\xEA" "A"));
511
512     /** ARING (NFC) */
513     YAZ_CHECK(tst_convert(cd, "\xC3\x85", "\xEA" "A"));
514
515     /** A MACRON + UMLAUT, DIAERESIS */
516     YAZ_CHECK(tst_convert(cd, "A" "\xCC\x84" "\xCC\x88",
517                           "\xE5\xE8\x41"));
518
519     /* Ligature spanning two characters */
520     YAZ_CHECK(tst_convert(cd,
521                           "\x74" "\xCD\xA1" "\x73",  /* UTF-8 */
522                           "\xEB\x74\xEC\x73"));      /* MARC-8 */
523
524     /* Double title spanning two characters */
525     YAZ_CHECK(tst_convert(cd,
526                           "\x74" "\xCD\xA0" "\x73",  /* UTF-8 */
527                           "\xFA\x74\xFB\x73"));      /* MARC-8 */
528
529     /** Ideographic question mark (Unicode FF1F) */
530     YAZ_CHECK(tst_convert(cd,
531                           "\xEF\xBC\x9F" "o",        /* UTF-8 */
532                           "\033$1" "\x21\x2B\x3B" "\033(B" "o" ));
533
534
535     /** Ideographic space per ANSI Z39.64 */
536     YAZ_CHECK(tst_convert(cd,
537                           "\xe3\x80\x80" "o",        /* UTF-8 */
538                           "\033$1" "\x21\x23\x21" "\033(B" "o" ));
539
540     /** Superscript 0 . bug #642 */
541     YAZ_CHECK(tst_convert(cd,
542                           "(\xe2\x81\xb0)",        /* UTF-8 */
543                           "(\033p0\x1bs)"));
544
545
546     /** bug #1778 */
547     YAZ_CHECK(tst_convert(cd,
548                           /* offset 0x530 in UTF-8 rec marccol4.u8.marc */
549                           "\xE3\x83\xB3" "\xE3\x82\xBF"
550                           "\xCC\x84" "\xCC\x84" "\xE3\x83\xBC" /* UTF-8 */,
551                           "\x1B\x24\x31" "\x69\x25\x73"
552                           "\x1B\x28\x42" "\xE5\xE5" "\x1B\x24\x31"
553                           "\x69\x25\x3F"
554                           "\x69\x21\x3C" "\x1B\x28\x42"));
555
556
557     /** bug #2120 */
558     YAZ_CHECK(tst_convert(cd,
559                           "\xCE\x94\xCE\xB5\xCF\x84"
560                           "\xCE\xBF\xCF\x81\xCE\xB1"
561                           "\xCE\xBA\xCE\xB7\xCF\x82\x2C",
562
563                           "\x1B\x28\x53\x45\x66\x78\x72\x75"
564                           "\x61\x6D\x6A\x77"
565                           "\x1B\x28\x42\x2C"
566                   ));
567
568     {
569         char *inbuf0 = "\xe2\x81\xb0";
570         char *inbuf = inbuf0;
571         size_t inbytesleft = strlen(inbuf);
572         char outbuf0[64];
573         char *outbuf = outbuf0;
574         size_t outbytesleft = sizeof(outbuf0)-1;
575         size_t r;
576 #if 0
577         int i;
578 #endif
579         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
580         YAZ_CHECK(r != (size_t) (-1));
581
582 #if 0
583         *outbuf = '\0';  /* so we know when to stop printing */
584         for (i = 0; outbuf0[i]; i++)
585         {
586             int ch = outbuf0[i] & 0xff;
587             yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
588         }
589 #endif
590
591         r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
592         YAZ_CHECK(r != (size_t) (-1));
593         *outbuf = '\0';  /* for strcmp test below and printing */
594 #if 0
595         for (i = 0; outbuf0[i]; i++)
596         {
597             int ch = outbuf0[i] & 0xff;
598             yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
599         }
600 #endif
601         YAZ_CHECK(strcmp("\033p0\x1bs", outbuf0) == 0);
602     }
603     yaz_iconv(cd, 0, 0, 0, 0);
604     yaz_iconv_close(cd);
605 }
606
607 static void tst_advance_to_utf8(void)
608 {
609     yaz_iconv_t cd = yaz_iconv_open("utf-8", "advancegreek");
610
611     YAZ_CHECK(cd);
612     if (!cd)
613         return;
614
615     YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
616     yaz_iconv_close(cd);
617 }
618
619 static void tst_utf8_to_advance(void)
620 {
621     yaz_iconv_t cd = yaz_iconv_open("advancegreek", "utf-8");
622
623     YAZ_CHECK(cd);
624     if (!cd)
625         return;
626
627     YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
628     yaz_iconv_close(cd);
629 }
630
631 static void tst_latin1_to_marc8(void)
632 {
633     yaz_iconv_t cd = yaz_iconv_open("MARC8", "ISO-8859-1");
634
635     YAZ_CHECK(cd);
636     if (!cd)
637         return;
638
639     YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
640
641     /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
642     YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
643
644     /** Pure ASCII. 12 characters (sizeof(outbuf)) */
645     YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
646
647     /** Pure ASCII. 13 characters (sizeof(outbuf)) */
648     YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
649
650     /** D8: UPPERCASE SCANDINAVIAN O */
651     YAZ_CHECK(tst_convert(cd, "S\xd8", "S\xa2"));
652
653     /** E9: LATIN SMALL LETTER E WITH ACUTE */
654     YAZ_CHECK(tst_convert(cd, "Cours de math\xe9", "Cours de mathâe"));
655     YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"
656                   ));
657     YAZ_CHECK(tst_convert(cd, "Cours de mathé", "Cours de mathâe" ));
658     YAZ_CHECK(tst_convert(cd, "12345678é","12345678âe"));
659     YAZ_CHECK(tst_convert(cd, "123456789é", "123456789âe"));
660     YAZ_CHECK(tst_convert(cd, "1234567890é","1234567890âe"));
661     YAZ_CHECK(tst_convert(cd, "12345678901é", "12345678901âe"));
662     YAZ_CHECK(tst_convert(cd, "Cours de mathém", "Cours de mathâem"));
663     YAZ_CHECK(tst_convert(cd, "Cours de mathématiques",
664                           "Cours de mathâematiques"));
665     yaz_iconv_close(cd);
666 }
667
668 static void tst_utf8_codes(void)
669 {
670     YAZ_CHECK(utf8_check(3));
671     YAZ_CHECK(utf8_check(127));
672     YAZ_CHECK(utf8_check(128));
673     YAZ_CHECK(utf8_check(255));
674     YAZ_CHECK(utf8_check(256));
675     YAZ_CHECK(utf8_check(900));
676     YAZ_CHECK(utf8_check(1000));
677     YAZ_CHECK(utf8_check(10000));
678     YAZ_CHECK(utf8_check(100000));
679     YAZ_CHECK(utf8_check(1000000));
680     YAZ_CHECK(utf8_check(10000000));
681     YAZ_CHECK(utf8_check(100000000));
682 }
683
684 static void tst_danmarc_to_utf8(void)
685 {
686     yaz_iconv_t cd = yaz_iconv_open("utf-8", "danmarc");
687
688     YAZ_CHECK(cd);
689     if (!cd)
690         return;
691
692     YAZ_CHECK(tst_convert(cd, "ax", "ax"));
693
694     YAZ_CHECK(tst_convert(cd, "a@@b", "a@b"));
695     YAZ_CHECK(tst_convert(cd, "a@@@@b", "a@@b"));
696
697     YAZ_CHECK(tst_convert(cd, "@*",  "*"));
698     YAZ_CHECK(tst_convert(cd, "@@",  "@"));
699     YAZ_CHECK(tst_convert(cd, "@\xa4",  "\xC2\xA4"));
700     YAZ_CHECK(tst_convert(cd, "\xa4",  "\xC2\xA4"));
701     YAZ_CHECK(tst_convert(cd, "@\xe5", "\xEA\x9C\xB3"));
702     YAZ_CHECK(tst_convert(cd, "@\xc5.", "\xEA\x9C\xB2" "."));
703
704     YAZ_CHECK(tst_convert(cd, "@a733",  "\xEA\x9C\xB3"));
705     YAZ_CHECK(tst_convert(cd, "@a732.",  "\xEA\x9C\xB2" "."));
706
707     YAZ_CHECK(tst_convert(cd, "a@03BBb", "a\xce\xbb" "b")); /* lambda */
708
709     yaz_iconv_close(cd);
710 }
711
712 static void tst_utf8_to_danmarc(void)
713 {
714     yaz_iconv_t cd = yaz_iconv_open("danmarc", "utf-8");
715
716     YAZ_CHECK(cd);
717     if (!cd)
718         return;
719
720     YAZ_CHECK(tst_convert(cd, "ax", "ax"));
721
722     YAZ_CHECK(tst_convert(cd, "a@b", "a@@b"));
723     YAZ_CHECK(tst_convert(cd, "a@@b", "a@@@@b"));
724
725     YAZ_CHECK(tst_convert(cd, "*",  "@*"));
726     YAZ_CHECK(tst_convert(cd, "@", "@@"));
727     YAZ_CHECK(tst_convert(cd, "\xC2\xA4", "\xa4"));
728
729     YAZ_CHECK(tst_convert(cd, "a\xc3\xa5" "b", "a\xe5" "b")); /* aring */
730     YAZ_CHECK(tst_convert(cd, "a\xce\xbb" "b", "a@03BBb")); /* lambda */
731
732     YAZ_CHECK(tst_convert(cd, "\xEA\x9C\xB2" ".", "@\xc5."));
733     YAZ_CHECK(tst_convert(cd, "\xEA\x9C\xB3", "@\xe5"));
734
735     yaz_iconv_close(cd);
736 }
737
738
739
740 int main (int argc, char **argv)
741 {
742     YAZ_CHECK_INIT(argc, argv);
743
744     tst_utf8_codes();
745
746     tst_marc8_to_utf8();
747
748     tst_marc8s_to_utf8();
749
750     tst_marc8_to_latin1();
751
752     tst_advance_to_utf8();
753     tst_utf8_to_advance();
754
755     tst_utf8_to_marc8("marc8");
756     tst_utf8_to_marc8("marc8lossy");
757     tst_utf8_to_marc8("marc8lossless");
758
759     tst_danmarc_to_utf8();
760     tst_utf8_to_danmarc();
761
762     tst_latin1_to_marc8();
763
764     tst_marc8_to_ucs4b();
765     tst_ucs4b_to_utf8();
766
767     dconvert(1, "UTF-8");
768     dconvert(1, "ISO-8859-1");
769     dconvert(1, "UCS4");
770     dconvert(1, "UCS4LE");
771     dconvert(0, "CP865");
772
773     YAZ_CHECK_TERM;
774 }
775 /*
776  * Local variables:
777  * c-basic-offset: 4
778  * c-file-style: "Stroustrup"
779  * indent-tabs-mode: nil
780  * End:
781  * vim: shiftwidth=4 tabstop=8 expandtab
782  */
783