Extend MARC-8 to handle ESC-G0 EACC. Fix conversion order for MARCXML
[yaz-moved-to-github.git] / test / tsticonv.c
1 /*
2  * Copyright (c) 2002-2004, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: tsticonv.c,v 1.2 2004-03-15 21:39:06 adam Exp $
6  */
7
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <errno.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 #include <yaz/yaz-util.h>
17
18 /* some test strings in ISO-8859-1 format */
19 static const char *iso_8859_1_a[] = {
20     "ax" ,
21     "\330",
22     "eneb\346r",
23     "\xfc",
24     "\xfb",
25     "\xfbr",
26     0 };
27
28 /* same test strings in MARC-8 format */
29 static const char *marc8_a[] = {
30     "ax",   
31     "\xa2",          /* latin capital letter o with stroke */
32     "eneb\xb5r",     /* latin small letter ae */
33     "\xe8\x75",      /* latin small letter u with umlaut */
34     "\xe3\x75",      /* latin small letter u with circumflex */
35     "\xe3\x75r",     /* latin small letter u with circumflex */
36     0
37 };
38
39 static void marc8_tst_a()
40 {
41     int i;
42     yaz_iconv_t cd;
43
44     cd = yaz_iconv_open("ISO-8859-1", "MARC8");
45     if (!cd)
46     {
47         printf("tsticonv 10 yaz_iconv_open failed\n");
48         exit(10);
49     }
50     for (i = 0; iso_8859_1_a[i]; i++)
51     {
52         size_t r;
53         char *inbuf= (char*) marc8_a[i];
54         size_t inbytesleft = strlen(inbuf);
55         char outbuf0[24];
56         char *outbuf = outbuf0;
57         size_t outbytesleft = sizeof(outbuf0);
58
59         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
60         if (r == (size_t) (-1))
61         {
62             int e = yaz_iconv_error(cd);
63
64             printf ("tsticonv 11 i=%d e=%d\n", i, e);
65             exit(11);
66         }
67         if ((outbuf - outbuf0) != strlen(iso_8859_1_a[i]) 
68             || memcmp(outbuf0, iso_8859_1_a[i],
69                       strlen(iso_8859_1_a[i])))
70         {
71             printf ("tsticonv 12 i=%d\n", i);
72             printf ("buf=%s   out=%s\n", iso_8859_1_a[i], outbuf0);
73             exit(12);
74         }
75     }
76     yaz_iconv_close(cd);
77 }
78
79 static void marc8_tst_b()
80 {
81     static const char *marc8_b[] = {
82         "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
83         "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
84         "\033$1"
85         "\x21\x50\x6E"  /* 7CFB */
86         "\x21\x51\x31"  /* 7D71 */
87         "\x21\x3A\x67"  /* 5B89 */
88         "\x21\x33\x22"  /* 5168 */
89         "\x21\x33\x53"  /* 5206 */
90         "\x21\x44\x2B"  /* 6790 */
91         "\033(B",
92         0
93     };
94     static const char *ucs4_b[] = {
95         "\x00\x00\xFF\x1F" "\x00\x00\x00o",
96         "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
97         "\x00\x00\x7C\xFB"
98         "\x00\x00\x7D\x71"
99         "\x00\x00\x5B\x89"
100         "\x00\x00\x51\x68"
101         "\x00\x00\x52\x06"
102         "\x00\x00\x67\x90",
103         0
104     };
105     int i;
106     yaz_iconv_t cd;
107
108     cd = yaz_iconv_open("UCS4", "MARC8");
109     if (!cd)
110     {
111         printf ("tsticonv 20 yaz_iconv_open failed\n");
112         exit(20);
113     }
114     for (i = 0; marc8_b[i]; i++)
115     {
116         size_t r;
117         size_t len;
118         size_t expect_len = (i == 2 ? 24 : 8);
119         char *inbuf= (char*) marc8_b[i];
120         size_t inbytesleft = strlen(inbuf);
121         char outbuf0[24];
122         char *outbuf = outbuf0;
123         size_t outbytesleft = sizeof(outbuf0);
124
125         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
126         if (r == (size_t) (-1))
127         {
128             int e = yaz_iconv_error(cd);
129
130             printf ("tsticonv 21 i=%d e=%d\n", i, e);
131             exit(21);
132         }
133         len = outbuf - outbuf0;
134         if (len != expect_len || memcmp(outbuf0, ucs4_b[i], len))
135         {
136             printf ("tsticonv 22 len=%d gotlen=%d i=%d\n", expect_len, len, i);
137             exit(22);
138         }
139     }
140     yaz_iconv_close(cd);
141 }
142
143 static void marc8_tst_c()
144 {
145     static const char *ucs4_c[] = {
146         "\x00\x00\xFF\x1F\x00\x00\x00o",
147         "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
148         0
149     };
150     static const char *utf8_c[] = {
151         "\xEF\xBC\x9F\x6F",
152         "\xEA\xB8\x8E\xEC\x83\xB4",
153         0
154     };
155     
156     int i;
157     yaz_iconv_t cd;
158
159     cd = yaz_iconv_open("UTF8", "UCS4");
160     if (!cd)
161     {
162         printf ("tsticonv 30 yaz_iconv_open failed\n");
163         exit(30);
164     }
165     for (i = 0; ucs4_c[i]; i++)
166     {
167         size_t r;
168         size_t len;
169         char *inbuf= (char*) ucs4_c[i];
170         size_t inbytesleft = 8;
171         char outbuf0[24];
172         char *outbuf = outbuf0;
173         size_t outbytesleft = sizeof(outbuf0);
174
175         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
176         if (r == (size_t) (-1))
177         {
178             int e = yaz_iconv_error(cd);
179
180             printf ("tsticonv 31 i=%d e=%d\n", i, e);
181             exit(31);
182         }
183         len = outbuf - outbuf0;
184         if (len != strlen(utf8_c[i]) || memcmp(outbuf0, utf8_c[i], len))
185         {
186             printf ("tsticonv 32 len=%d gotlen=%d i=%d\n",
187                     strlen(utf8_c[i]), len, i);
188             exit(32);
189         }
190     }
191     yaz_iconv_close(cd);
192 }
193
194 static void dconvert(int mandatory, const char *tmpcode)
195 {
196     int i;
197     yaz_iconv_t cd;
198     for (i = 0; iso_8859_1_a[i]; i++)
199     {
200         size_t r;
201         char *inbuf = (char*) iso_8859_1_a[i];
202         size_t inbytesleft = strlen(inbuf);
203         char outbuf0[24];
204         char outbuf1[10];
205         char *outbuf = outbuf0;
206         size_t outbytesleft = sizeof(outbuf0);
207
208         cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
209         if (!cd)
210         {
211             if (!mandatory)
212                 return;
213             printf ("tsticonv code=%s 1\n", tmpcode);
214             exit(1);
215         }
216         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
217         if (r == (size_t)(-1))
218         {
219             int e = yaz_iconv_error(cd);
220
221             printf ("tsticonv code=%s 2 e=%d\n", tmpcode, e);
222             exit(2);
223         }
224         yaz_iconv_close(cd);
225         
226         cd = yaz_iconv_open("ISO-8859-1", tmpcode);
227         if (!cd)
228         {
229             if (!mandatory)
230                 return;
231             printf ("tsticonv code=%s 3\n", tmpcode);
232             exit(3);
233         }
234         inbuf = outbuf0;
235         inbytesleft = sizeof(outbuf0) - outbytesleft;
236
237         outbuf = outbuf1;
238         outbytesleft = sizeof(outbuf1);
239         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
240         if (r == (size_t)(-1)) {
241             int e = yaz_iconv_error(cd);
242
243             printf ("tsticonv code=%s 4 e=%d\n", tmpcode, e);
244             exit(4);
245         }
246         if (strlen(iso_8859_1_a[i]) == 
247             (sizeof(outbuf1) - outbytesleft) &&
248             memcmp(outbuf1, iso_8859_1_a[i],
249                    strlen(iso_8859_1_a[i])))
250         {
251             printf ("tsticonv code=%s 5\n", tmpcode);
252             exit(5);
253         }
254         yaz_iconv_close(cd);
255     }
256 }
257         
258 int main (int argc, char **argv)
259 {
260     dconvert(1, "UTF-8");
261     dconvert(1, "ISO-8859-1");
262     dconvert(1, "UCS4");
263     dconvert(1, "UCS4LE");
264     dconvert(0, "CP865");
265     marc8_tst_a();
266     marc8_tst_b();
267     marc8_tst_c();
268     exit (0);
269 }