b308a2c44565d485638f68590a17688b6b1df74c
[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.4 2004-11-16 22:51:52 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     0 };
24
25 /* same test strings in MARC-8 format */
26 static const char *marc8_a[] = {
27     "ax",   
28     "\xa2",          /* latin capital letter o with stroke */
29     "eneb\xb5r",     /* latin small letter ae */
30     0
31 };
32
33 static void marc8_tst_a()
34 {
35     int i;
36     yaz_iconv_t cd;
37
38     cd = yaz_iconv_open("ISO-8859-1", "MARC8");
39     if (!cd)
40     {
41         printf("tsticonv 10 yaz_iconv_open failed\n");
42         exit(10);
43     }
44     for (i = 0; iso_8859_1_a[i]; i++)
45     {
46         size_t r;
47         char *inbuf= (char*) marc8_a[i];
48         size_t inbytesleft = strlen(inbuf);
49         char outbuf0[24];
50         char *outbuf = outbuf0;
51         size_t outbytesleft = sizeof(outbuf0);
52
53         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
54         if (r == (size_t) (-1))
55         {
56             int e = yaz_iconv_error(cd);
57
58             printf ("tsticonv 11 i=%d e=%d\n", i, e);
59             exit(11);
60         }
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])))
64         {
65             printf ("tsticonv 12 i=%d\n", i);
66             printf ("buf=%s   out=%s\n", iso_8859_1_a[i], outbuf0);
67             exit(12);
68         }
69     }
70     yaz_iconv_close(cd);
71 }
72
73 static void marc8_tst_b()
74 {
75     static const char *marc8_b[] = {
76         /* 0 */ 
77         "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
78         /* 1 */ 
79         "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
80         /* 2 */ 
81         "\033$1"
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 */
88         "\033(B",
89         /* 3 */
90         "\xB0\xB2",     /* AYN and oSLASH */
91         /* 4 */
92         "\xF6\x61",     /* a underscore */
93         /* 5 */
94         "\x61\xC2",     /* a, phonorecord mark */
95         0
96     };
97     static const char *ucs4_b[] = {
98         "\x00\x00\xFF\x1F" "\x00\x00\x00o",
99         "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
100         "\x00\x00\x7C\xFB"
101         "\x00\x00\x7D\x71"
102         "\x00\x00\x5B\x89"
103         "\x00\x00\x51\x68"
104         "\x00\x00\x52\x06"
105         "\x00\x00\x67\x90",
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",
109         0
110     };
111     int i;
112     yaz_iconv_t cd;
113
114     cd = yaz_iconv_open("UCS4", "MARC8");
115     if (!cd)
116     {
117         printf ("tsticonv 20 yaz_iconv_open failed\n");
118         exit(20);
119     }
120     for (i = 0; marc8_b[i]; i++)
121     {
122         size_t r;
123         size_t len;
124         size_t expect_len = i == 2 ? 24 : 8;
125         char *inbuf= (char*) marc8_b[i];
126         size_t inbytesleft = strlen(inbuf);
127         char outbuf0[24];
128         char *outbuf = outbuf0;
129         size_t outbytesleft = sizeof(outbuf0);
130
131         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
132         if (r == (size_t) (-1))
133         {
134             int e = yaz_iconv_error(cd);
135
136             printf ("tsticonv 21 i=%d e=%d\n", i, e);
137             exit(21);
138         }
139         len = outbuf - outbuf0;
140         if (len != expect_len || memcmp(outbuf0, ucs4_b[i], len))
141         {
142             printf ("tsticonv 22 len=%d gotlen=%d i=%d\n", expect_len, len, i);
143             exit(22);
144         }
145     }
146     yaz_iconv_close(cd);
147 }
148
149 static void marc8_tst_c()
150 {
151     static const char *ucs4_c[] = {
152         "\x00\x00\xFF\x1F\x00\x00\x00o",
153         "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
154         0
155     };
156     static const char *utf8_c[] = {
157         "\xEF\xBC\x9F\x6F",
158         "\xEA\xB8\x8E\xEC\x83\xB4",
159         0
160     };
161     
162     int i;
163     yaz_iconv_t cd;
164
165     cd = yaz_iconv_open("UTF8", "UCS4");
166     if (!cd)
167     {
168         printf ("tsticonv 30 yaz_iconv_open failed\n");
169         exit(30);
170     }
171     for (i = 0; ucs4_c[i]; i++)
172     {
173         size_t r;
174         size_t len;
175         char *inbuf= (char*) ucs4_c[i];
176         size_t inbytesleft = 8;
177         char outbuf0[24];
178         char *outbuf = outbuf0;
179         size_t outbytesleft = sizeof(outbuf0);
180
181         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
182         if (r == (size_t) (-1))
183         {
184             int e = yaz_iconv_error(cd);
185
186             printf ("tsticonv 31 i=%d e=%d\n", i, e);
187             exit(31);
188         }
189         len = outbuf - outbuf0;
190         if (len != strlen(utf8_c[i]) || memcmp(outbuf0, utf8_c[i], len))
191         {
192             printf ("tsticonv 32 len=%d gotlen=%d i=%d\n",
193                     strlen(utf8_c[i]), len, i);
194             exit(32);
195         }
196     }
197     yaz_iconv_close(cd);
198 }
199
200 static void dconvert(int mandatory, const char *tmpcode)
201 {
202     int i;
203     yaz_iconv_t cd;
204     for (i = 0; iso_8859_1_a[i]; i++)
205     {
206         size_t r;
207         char *inbuf = (char*) iso_8859_1_a[i];
208         size_t inbytesleft = strlen(inbuf);
209         char outbuf0[24];
210         char outbuf1[10];
211         char *outbuf = outbuf0;
212         size_t outbytesleft = sizeof(outbuf0);
213
214         cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
215         if (!cd)
216         {
217             if (!mandatory)
218                 return;
219             printf ("tsticonv code=%s 1\n", tmpcode);
220             exit(1);
221         }
222         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
223         if (r == (size_t)(-1))
224         {
225             int e = yaz_iconv_error(cd);
226
227             printf ("tsticonv code=%s 2 e=%d\n", tmpcode, e);
228             exit(2);
229         }
230         yaz_iconv_close(cd);
231         
232         cd = yaz_iconv_open("ISO-8859-1", tmpcode);
233         if (!cd)
234         {
235             if (!mandatory)
236                 return;
237             printf ("tsticonv code=%s 3\n", tmpcode);
238             exit(3);
239         }
240         inbuf = outbuf0;
241         inbytesleft = sizeof(outbuf0) - outbytesleft;
242
243         outbuf = outbuf1;
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);
248
249             printf ("tsticonv code=%s 4 e=%d\n", tmpcode, e);
250             exit(4);
251         }
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])))
256         {
257             printf ("tsticonv code=%s 5\n", tmpcode);
258             exit(5);
259         }
260         yaz_iconv_close(cd);
261     }
262 }
263         
264 int main (int argc, char **argv)
265 {
266     dconvert(1, "UTF-8");
267     dconvert(1, "ISO-8859-1");
268     dconvert(1, "UCS4");
269     dconvert(1, "UCS4LE");
270     dconvert(0, "CP865");
271     marc8_tst_a();
272     marc8_tst_b();
273     marc8_tst_c();
274     exit (0);
275 }