1 /********************************************************************************
2 * *
3 * Codecs Tests *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2005,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * $Id: codecs.cpp,v 1.23 2006/01/22 17:58:59 fox Exp $ *
9 ********************************************************************************/
10 #include "fx.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "FXKOI8RCodec.h"
16 #include "FX88592Codec.h"
17 #include "FXUTF16Codec.h"
18 #include "FXUTF32Codec.h"
19 #include "FXUTF8Codec.h"
20 /*
21
22
23 */
24
25 /*******************************************************************************/
26
27 FXKOI8RCodec koi8_r;
28 FX88592Codec iso8859_2;
29
30 FXUTF16Codec utf16;
31 FXUTF16BECodec utf16be;
32 FXUTF16LECodec utf16le;
33
34 FXUTF8Codec utf8;
35
36 FXUTF32Codec utf32;
37 FXUTF32BECodec utf32be;
38 FXUTF32LECodec utf32le;
39
40
41
42 // UTF8 string of 1, 2, 3, 4, 5, and 6 bytes
43 const FXchar utfteststring[]="\x7f\xdf\xbf\xef\xbf\xbf\xf7\xbf\xbf\xbf\xfb\xbf\xbf\xbf\xbf\xfd\xbf\xbf\xbf\xbf\xbf";
44
45
46 // Test roundtrip for codec
roundtriptest(FXTextCodec * codec)47 void roundtriptest(FXTextCodec *codec){
48 FXchar dst[32],src[32];
49 FXint c,n,i,j;
50 FXwchar wc;
51 printf("Roundtrip test for: %s..\n",codec->name());
52 for(i=0; i<256; i++){
53 src[0]=i;
54 codec->mb2wc(wc,src,1);
55 codec->wc2mb(dst,32,wc);
56 j=(FXuchar)dst[0];
57 printf("0x%02x -> 0x%04x -> 0x%02x %s\n",i,wc,j,(i!=j)?"BAD":"");
58 }
59 printf("Roundtrip test done!\n");
60 }
61
62
63 // Test utf8 roundtrip for codec
utf8roundtriptest(FXTextCodec * codec)64 void utf8roundtriptest(FXTextCodec *codec){
65 FXchar dst[32],src[32];
66 FXint c,n,i,j;
67 FXwchar wc;
68 printf("UTF8 Roundtrip test for: %s..\n",codec->name());
69 for(i=0; i<256; i++){
70 src[0]=i;
71 memset(dst,0,sizeof(dst));
72 n=codec->mb2utf(dst,32,src,1);
73 if(n<=0) printf("mb2utf(0x%02x) gave error %d\n",i,n);
74 memset(src,0,sizeof(src));
75 n=codec->utf2mb(src,32,dst,n);
76 if(n<=0) printf("utf2mb(0x%02x) gave error %d\n",i,n);
77 j=(FXuchar)src[0];
78 if(i!=j) printf("0x%02x -> utf8 -> 0x%02x\n",i,j);
79 }
80 printf("UTF8 Roundtrip test done!\n");
81 }
82
83
84 // Start the whole thing
main(int,char **)85 int main(int,char**){
86 const FXwchar *wcdec;
87 FXwchar wc,ww;
88 FXint i,n,dec,cc,num1,num2,numt;
89 FXString value,norm;
90 FXuchar buf[128];
91 FXchar input[256];
92 //utf8codingtest();
93 //utf16codingtest();
94 // Test codecs
95 // roundtriptest(&koi8_r);
96 // roundtriptest(&iso8859_2);
97
98 // utf8roundtriptest(&koi8_r);
99 // utf8roundtriptest(&iso8859_2);
100
101 /*
102 wc=0x7fffffff;
103 wc=0x3ffffff;
104 wc=0x1fffff;
105 wc=0xffff;
106 wc=0x7ff;
107 wc=0x7f;
108 value.assign(&wc,1);
109 switch(value.length()){
110 case 1: printf("%06X: \\x%02x\n",wc,(FXuchar)value[0]); break;
111 case 2: printf("%06X: \\x%02x\\x%02x\n",wc,(FXuchar)value[0],(FXuchar)value[1]); break;
112 case 3: printf("%06X: \\x%02x\\x%02x\\x%02x\n",wc,(FXuchar)value[0],(FXuchar)value[1],(FXuchar)value[2]); break;
113 case 4: printf("%06X: \\x%02x\\x%02x\\x%02x\\x%02x\n",wc,(FXuchar)value[0],(FXuchar)value[1],(FXuchar)value[2],(FXuchar)value[3]); break;
114 case 5: printf("%06X: \\x%02x\\x%02x\\x%02x\\x%02x\\x%02x\n",wc,(FXuchar)value[0],(FXuchar)value[1],(FXuchar)value[2],(FXuchar)value[3],(FXuchar)value[4]); break;
115 case 6: printf("%06X: \\x%02x\\x%02x\\x%02x\\x%02x\\x%02x\\x%02x\n",wc,(FXuchar)value[0],(FXuchar)value[1],(FXuchar)value[2],(FXuchar)value[3],(FXuchar)value[4],(FXuchar)value[5]); break;
116 }
117 value=utfteststring;
118 i=0;
119 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
120 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
121 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
122 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
123 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
124 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
125 printf("inc(%d)=",i); printf("%d\n",value.inc(i));
126 exit(0);
127 */
128
129 while(fgets(input,sizeof(input),stdin)){
130 input[strlen(input)-1]=0;
131 // value=unescape(input);
132 value=input;
133 printf("in : \"%s\"\n",escape(value).text());
134 value=fromAscii(value);
135 printf("org: \"%s\"\n",escape(value).text());
136 value=decompose(value,DecCanonical);
137 printf("dec: \"%s\"\n",escape(value).text());
138 value=toAscii(value);
139 printf("out: \"%s\"\n",value.text());
140 }
141
142 exit(0);
143
144 // Test utf
145 for(wc=0; wc<0x7fffffff; wc++){
146 n=wc2utfs((FXchar*)buf,&wc,1);
147 utf2wcs(&ww,(const FXchar*)buf,n);
148 if(wc!=ww) printf("%06X: Problem: %06X\n",wc,ww);
149 }
150
151 /*
152 // Test decompose table
153 for(wc=num1=num2=numt=0; wc<=0x110000; wc++){
154 dec=Unicode::decomposeType(wc);
155 cc=Unicode::charCombining(wc);
156 if(dec==DecCanonical && cc==0){
157 wcdec=Unicode::charDecompose(wc);
158 if(1<wcdec[-1]){
159 if(wcdec[-1]==1) num1++;
160 if(wcdec[-1]==2) num2++;
161 numt++;
162 printf("%04X: cc=%2d (%d): ",wc,cc,wcdec[-1]);
163 for(i=0; i<wcdec[-1]; i++){
164 printf("%04X ",wcdec[i]);
165 }
166 printf(" comp=%04X ",Unicode::charCompose(wcdec[0],wcdec[1]));
167 printf("\n");
168 }
169 }
170 }
171 printf("num1=%d num2=%d numt=%d",num1,num2,numt);
172 */
173 /*
174 for(wc=0; wc<0x110000; wc++){
175 value.assign(&wc,1);
176 norm=decompose(value,1);
177 printf("%04X: (%2d): ",wc,norm.length());
178 for(i=0; i<value.length(); i++){
179 printf("%02X ",(FXuchar)value[i]);
180 //printf("%c",(FXuchar)value[i]);
181 }
182 printf(" -> ");
183 for(i=0; i<norm.length(); i++){
184 printf("%02X ",(FXuchar)norm[i]);
185 //printf("%c",(FXuchar)norm[i]);
186 }
187 printf("\n");
188 }
189 */
190
191 return 1;
192 }
193