1 /* $MirOS: src/lib/libcitrus_iconv/test/iconv_test.c,v 1.2 2006/06/02 20:18:41 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 2006
5  *	Thorsten Glaser <tg@mirbsd.de>
6  *
7  * Licensee is hereby permitted to deal in this work without restric-
8  * tion, including unlimited rights to use, publicly perform, modify,
9  * merge, distribute, sell, give away or sublicence, provided all co-
10  * pyright notices above, these terms and the disclaimer are retained
11  * in all redistributions or reproduced in accompanying documentation
12  * or other materials provided with binary redistributions.
13  *
14  * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
15  * express, or implied, to the maximum extent permitted by applicable
16  * law, without malicious intent or gross negligence; in no event may
17  * licensor, an author or contributor be held liable for any indirect
18  * or other damage, or direct damage except proven a consequence of a
19  * direct error of said person and intended use of this work, loss or
20  * other issues arising in any way out of its use, even if advised of
21  * the possibility of such damage or existence of a nontrivial bug.
22  */
23 
24 #include <sys/param.h>
25 #include <iconv.h>
26 #include <stdio.h>
27 
28 unsigned char sb[512] = "fAbianBHello, world!", db[512] = "aaaaaaaaaaaaaaaaaa";
29 unsigned char tb[512], ntb[512];
30 
31 int
main(int argc,char ** argv)32 main(int argc, char **argv)
33 {
34 	iconv_t i;
35 	size_t ss, ds, rv;
36 	unsigned char *sp, *dp;
37 
38 	sb[1]=0xE4;
39 	sb[6]=0;
40 
41 	i = iconv_open("UTF-8", "ISO-8859-1");
42 	if (i == (iconv_t)-1)
43 		perror("iconv_open");
44 
45 #if 0
46 	ss = sizeof (sb);
47 #else
48 	ss = strlen(sb);
49 #endif
50 	ds = sizeof (db);
51 
52 	sp = sb;
53 	dp = db;
54 
55 	rv = iconv(i, (char **)&sp, &ss, (char **)&dp, &ds);
56 
57 	if (rv == (size_t)-1)
58 		perror("iconv");
59 
60 	printf("iconv: %d invalid, new src=%d dst=%d\ndump:", rv, ss, ds);
61 	for (sp = db; sp <= dp; ++sp)
62 		printf(" %02X", *sp & 0xFF);
63 	*dp = '\0';
64 	printf("\nString: %s\n", db);
65 
66 	if (iconv_close(i))
67 		perror("iconv_close");
68 
69 	dp = db;
70 	if (*dp++ != 'f')
71 		return (1);
72 	if (*dp++ != 0xC3)
73 		return (1);
74 	if (*dp++ != 0xA4)
75 		return (1);
76 	if (strcmp(dp, "bian"))
77 		return (1);
78 
79 	printf("Transliteration test:\n");
80 
81 	i = iconv_open("ISO_646.irv:1991", "UTF-8");
82 	if (i == (iconv_t)-1)
83 		perror("iconv_open");
84 	ss = strlen(db);
85 	ds = sizeof (ntb);
86 	sp = db;
87 	dp = ntb;
88 	rv = iconv(i, (char **)&sp, &ss, (char **)&dp, &ds);
89 	if (rv == (size_t)-1)
90 		perror("iconv");
91 	if (iconv_close(i))
92 		perror("iconv_close");
93 	printf("iconv: %d invalid, new src=%d dst=%d\ndump:", rv, ss, ds);
94 	for (sp = ntb; sp <= dp; ++sp)
95 		printf(" %02X", *sp & 0xFF);
96 	*dp = '\0';
97 	printf("\nString (sans): %s\n", ntb);
98 
99 	i = iconv_open("ISO_646.irv:1991//TRANSLIT", "UTF-8");
100 	if (i == (iconv_t)-1)
101 		perror("iconv_open");
102 	ss = strlen(db);
103 	ds = sizeof (tb);
104 	sp = db;
105 	dp = tb;
106 	rv = iconv(i, (char **)&sp, &ss, (char **)&dp, &ds);
107 	if (rv == (size_t)-1)
108 		perror("iconv");
109 	if (iconv_close(i))
110 		perror("iconv_close");
111 	printf("iconv: %d invalid, new src=%d dst=%d\ndump:", rv, ss, ds);
112 	for (sp = tb; sp <= dp; ++sp)
113 		printf(" %02X", *sp & 0xFF);
114 	*dp = '\0';
115 	printf("\nString (avec): %s\n", tb);
116 
117 	return (0);
118 }
119