removed unused CByteArrayFormat function
[wxWidgets.git] / tests / mbconv / mbconvtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/mbconv/main.cpp
3 // Purpose: wxMBConv unit test
4 // Author: Vadim Zeitlin, Mike Wetherell, Vince Harron
5 // Created: 14.02.04
6 // RCS-ID: $Id$
7 // Copyright: (c) 2003 TT-Solutions, (c) 2005 Mike Wetherell, Vince Harron
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/strconv.h"
25 #include "wx/string.h"
26 #include "wx/txtstrm.h"
27 #include "wx/mstream.h"
28
29 #if defined wxHAVE_TCHAR_SUPPORT && !defined HAVE_WCHAR_H
30 #define HAVE_WCHAR_H
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // Some wide character constants. "\uXXXX" escapes aren't supported by old
35 // compilers such as VC++ 5 and g++ 2.95.
36 // ----------------------------------------------------------------------------
37
38 wchar_t u41[] = { 0x41, 0 };
39 wchar_t u7f[] = { 0x7f, 0 };
40
41 wchar_t u80[] = { 0x80, 0 };
42 wchar_t u391[] = { 0x391, 0 };
43 wchar_t u7ff[] = { 0x7ff, 0 };
44
45 wchar_t u800[] = { 0x800, 0 };
46 wchar_t u2620[] = { 0x2620, 0 };
47 wchar_t ufffd[] = { 0xfffd, 0 };
48
49 #if SIZEOF_WCHAR_T == 4
50 wchar_t u10000[] = { 0x10000, 0 };
51 wchar_t u1000a5[] = { 0x1000a5, 0 };
52 wchar_t u10fffd[] = { 0x10fffd, 0 };
53 #else
54 wchar_t u10000[] = { 0xd800, 0xdc00, 0 };
55 wchar_t u1000a5[] = { 0xdbc0, 0xdca5, 0 };
56 wchar_t u10fffd[] = { 0xdbff, 0xdffd, 0 };
57 #endif
58
59 // ----------------------------------------------------------------------------
60 // test class
61 // ----------------------------------------------------------------------------
62
63 class MBConvTestCase : public CppUnit::TestCase
64 {
65 public:
66 MBConvTestCase() { }
67
68 private:
69 CPPUNIT_TEST_SUITE( MBConvTestCase );
70 CPPUNIT_TEST( UTF32LETests );
71 CPPUNIT_TEST( UTF32BETests );
72 CPPUNIT_TEST( WC2CP1250 );
73 CPPUNIT_TEST( UTF7Tests );
74 CPPUNIT_TEST( UTF8Tests );
75 CPPUNIT_TEST( UTF16LETests );
76 CPPUNIT_TEST( UTF16BETests );
77 CPPUNIT_TEST( CP932Tests );
78 CPPUNIT_TEST( CP1252Tests ); // depends on UTF8 Decoder functioning correctly
79 CPPUNIT_TEST( LibcTests );
80 CPPUNIT_TEST( IconvTests );
81 CPPUNIT_TEST( FontmapTests );
82 #ifdef HAVE_WCHAR_H
83 CPPUNIT_TEST( UTF8_41 );
84 CPPUNIT_TEST( UTF8_7f );
85 CPPUNIT_TEST( UTF8_80 );
86 CPPUNIT_TEST( UTF8_c2_7f );
87 CPPUNIT_TEST( UTF8_c2_80 );
88 CPPUNIT_TEST( UTF8_ce_91 );
89 CPPUNIT_TEST( UTF8_df_bf );
90 CPPUNIT_TEST( UTF8_df_c0 );
91 CPPUNIT_TEST( UTF8_e0_a0_7f );
92 CPPUNIT_TEST( UTF8_e0_a0_80 );
93 CPPUNIT_TEST( UTF8_e2_98_a0 );
94 CPPUNIT_TEST( UTF8_ef_bf_bd );
95 CPPUNIT_TEST( UTF8_ef_bf_c0 );
96 CPPUNIT_TEST( UTF8_f0_90_80_7f );
97 CPPUNIT_TEST( UTF8_f0_90_80_80 );
98 CPPUNIT_TEST( UTF8_f4_8f_bf_bd );
99 CPPUNIT_TEST( UTF8PUA_f4_80_82_a5 );
100 CPPUNIT_TEST( UTF8Octal_backslash245 );
101 #endif // HAVE_WCHAR_H
102 CPPUNIT_TEST_SUITE_END();
103
104 void WC2CP1250();
105 void UTF7Tests();
106 void UTF8Tests();
107 void UTF16LETests();
108 void UTF16BETests();
109 void UTF32LETests();
110 void UTF32BETests();
111 void CP932Tests();
112 void CP1252Tests();
113 void LibcTests();
114 void FontmapTests();
115 void IconvTests();
116
117 // verifies that the specified multibyte sequence decodes to the specified wchar_t sequence
118 void TestDecoder(
119 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
120 size_t wideChars, // the number of wide characters at wideBuffer
121 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
122 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
123 wxMBConv* converter, // the wxMBConv object that can decode multiBuffer into a wide character sequence
124 int sizeofNull // number of bytes occupied by terminating null in this encoding
125 );
126
127 // verifies that the specified wchar_t sequence encodes to the specified multibyte sequence
128 void TestEncoder(
129 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
130 size_t wideChars, // the number of wide characters at wideBuffer
131 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
132 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
133 wxMBConv* converter, // the wxMBConv object that can decode multiBuffer into a wide character sequence
134 int sizeofNull // number of bytes occupied by terminating null in this encoding
135 );
136
137 #if wxUSE_UNICODE && wxUSE_STREAMS
138 // use wxTextInputStream to exercise wxMBConv interface
139 // (this reveals some bugs in certain wxMBConv subclasses)
140 void TestStreamDecoder(
141 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
142 size_t wideChars, // the number of wide characters at wideBuffer
143 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
144 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
145 wxMBConv* converter // the wxMBConv object that can decode multiBuffer into a wide character sequence
146 );
147
148 // use wxTextOutputStream to exercise wxMBConv interface
149 // (this reveals some bugs in certain wxMBConv subclasses)
150 void TestStreamEncoder(
151 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
152 size_t wideChars, // the number of wide characters at wideBuffer
153 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
154 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
155 wxMBConv* converter // the wxMBConv object that can decode multiBuffer into a wide character sequence
156 );
157 #endif
158
159 // tests the encoding and decoding capability of an wxMBConv object
160 //
161 // decodes the utf-8 bytes into wide characters
162 // encodes the wide characters to compare against input multiBuffer
163 // decodes the multiBuffer to compare against wide characters
164 // decodes the multiBuffer into wide characters
165 void TestCoder(
166 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
167 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
168 const char* utf8Buffer, // the same character sequence as multiBuffer, encoded as UTF-8
169 size_t utf8Bytes, // the byte length of the UTF-8 encoded character sequence
170 wxMBConv* converter, // the wxMBConv object that can decode multiBuffer into a wide character sequence
171 int sizeofNull // the number of bytes occupied by a terminating null in the converter's encoding
172 );
173
174 #ifdef HAVE_WCHAR_H
175 // UTF-8 tests. Test the first, last and one in the middle for sequences
176 // of each length
177 void UTF8_41() { UTF8("\x41", u41); }
178 void UTF8_7f() { UTF8("\x7f", u7f); }
179 void UTF8_80() { UTF8("\x80", NULL); }
180
181 void UTF8_c2_7f() { UTF8("\xc2\x7f", NULL); }
182 void UTF8_c2_80() { UTF8("\xc2\x80", u80); }
183 void UTF8_ce_91() { UTF8("\xce\x91", u391); }
184 void UTF8_df_bf() { UTF8("\xdf\xbf", u7ff); }
185 void UTF8_df_c0() { UTF8("\xdf\xc0", NULL); }
186
187 void UTF8_e0_a0_7f() { UTF8("\xe0\xa0\x7f", NULL); }
188 void UTF8_e0_a0_80() { UTF8("\xe0\xa0\x80", u800); }
189 void UTF8_e2_98_a0() { UTF8("\xe2\x98\xa0", u2620); }
190 void UTF8_ef_bf_bd() { UTF8("\xef\xbf\xbd", ufffd); }
191 void UTF8_ef_bf_c0() { UTF8("\xef\xbf\xc0", NULL); }
192
193 void UTF8_f0_90_80_7f() { UTF8("\xf0\x90\x80\x7f", NULL); }
194 void UTF8_f0_90_80_80() { UTF8("\xf0\x90\x80\x80", u10000); }
195 void UTF8_f4_8f_bf_bd() { UTF8("\xf4\x8f\xbf\xbd", u10fffd); }
196
197 // test 'escaping the escape characters' for the two escaping schemes
198 void UTF8PUA_f4_80_82_a5() { UTF8PUA("\xf4\x80\x82\xa5", u1000a5); }
199 void UTF8Octal_backslash245() { UTF8Octal("\\245", L"\\245"); }
200
201 // implementation for the utf-8 tests (see comments below)
202 void UTF8(const char *charSequence, const wchar_t *wideSequence);
203 void UTF8PUA(const char *charSequence, const wchar_t *wideSequence);
204 void UTF8Octal(const char *charSequence, const wchar_t *wideSequence);
205 void UTF8(const char *charSequence, const wchar_t *wideSequence, int option);
206 #endif // HAVE_WCHAR_H
207
208 DECLARE_NO_COPY_CLASS(MBConvTestCase)
209 };
210
211 // register in the unnamed registry so that these tests are run by default
212 CPPUNIT_TEST_SUITE_REGISTRATION( MBConvTestCase );
213
214 // also include in it's own registry so that these tests can be run alone
215 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MBConvTestCase, "MBConv" );
216
217 void MBConvTestCase::WC2CP1250()
218 {
219 static const struct Data
220 {
221 const wchar_t *wc;
222 const char *cp1250;
223 } data[] =
224 {
225 { L"hello", "hello" }, // test that it works in simplest case
226 { L"\xBD of \xBD is \xBC", NULL }, // this should fail as cp1250 doesn't have 1/2
227 };
228
229 wxCSConv cs1250(wxFONTENCODING_CP1250);
230 for ( size_t n = 0; n < WXSIZEOF(data); n++ )
231 {
232 const Data& d = data[n];
233 if (d.cp1250)
234 {
235 CPPUNIT_ASSERT( strcmp(cs1250.cWC2MB(d.wc), d.cp1250) == 0 );
236 }
237 else
238 {
239 CPPUNIT_ASSERT( (const char*)cs1250.cWC2MB(d.wc) == NULL );
240 }
241 }
242 }
243
244 // The following bytes represent the same string, containing Japanese and English
245 // characters, encoded in several different formats.
246
247 // encoded by iconv
248 const static unsigned char welcome_utf7_iconv[84] =
249 {
250 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63,
251 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65,
252 0x2e,0x20,0x20,0x2b,0x4d,0x46,0x6b,0x77,0x55,0x49,0x74,0x6d,0x57,0x39,0x38,0x77,
253 0x61,0x35,0x62,0x37,0x69,0x6e,0x45,0x77,0x6b,0x6a,0x42,0x5a,0x4d,0x49,0x73,0x77,
254 0x65,0x7a,0x42,0x47,0x4d,0x45,0x77,0x77,0x52,0x44,0x42,0x45,0x4d,0x47,0x63,0x77,
255 0x57,0x54,0x41,0x43
256 };
257 // encoded by wxWindows (iconv can decode this successfully)
258 const static unsigned char welcome_utf7_wx[109] =
259 {
260 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x2b,0x41,0x43,0x41,0x2d,0x74,0x6f,0x2b,0x41,
261 0x43,0x41,0x2d,0x6f,0x75,0x72,0x2b,0x41,0x43,0x41,0x2d,0x63,0x79,0x62,0x65,0x72,
262 0x2b,0x41,0x43,0x41,0x2d,0x73,0x70,0x61,0x63,0x65,0x2b,0x41,0x43,0x41,0x2d,0x66,
263 0x6f,0x72,0x63,0x65,0x2e,0x2b,0x41,0x43,0x41,0x41,0x49,0x44,0x42,0x5a,0x4d,0x46,
264 0x43,0x4c,0x5a,0x6c,0x76,0x66,0x4d,0x47,0x75,0x57,0x2b,0x34,0x70,0x78,0x4d,0x4a,
265 0x49,0x77,0x57,0x54,0x43,0x4c,0x4d,0x48,0x73,0x77,0x52,0x6a,0x42,0x4d,0x4d,0x45,
266 0x51,0x77,0x52,0x44,0x42,0x6e,0x4d,0x46,0x6b,0x77,0x41,0x67,0x2d
267 };
268 // encoded by iconv
269 const static unsigned char welcome_utf8[89] =
270 {
271 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63,
272 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65,
273 0x2e,0x20,0x20,0xe3,0x81,0x99,0xe3,0x81,0x90,0xe8,0xad,0xa6,0xe5,0xaf,0x9f,0xe3,
274 0x81,0xab,0xe9,0x9b,0xbb,0xe8,0xa9,0xb1,0xe3,0x82,0x92,0xe3,0x81,0x99,0xe3,0x82,
275 0x8b,0xe3,0x81,0xbb,0xe3,0x81,0x86,0xe3,0x81,0x8c,0xe3,0x81,0x84,0xe3,0x81,0x84,
276 0xe3,0x81,0xa7,0xe3,0x81,0x99,0xe3,0x80,0x82
277 };
278 // encoded by iconv
279 const static unsigned char welcome_utf16le[106] =
280 {
281 0x57,0x00,0x65,0x00,0x6c,0x00,0x63,0x00,0x6f,0x00,0x6d,0x00,0x65,0x00,0x20,0x00,
282 0x74,0x00,0x6f,0x00,0x20,0x00,0x6f,0x00,0x75,0x00,0x72,0x00,0x20,0x00,0x63,0x00,
283 0x79,0x00,0x62,0x00,0x65,0x00,0x72,0x00,0x20,0x00,0x73,0x00,0x70,0x00,0x61,0x00,
284 0x63,0x00,0x65,0x00,0x20,0x00,0x66,0x00,0x6f,0x00,0x72,0x00,0x63,0x00,0x65,0x00,
285 0x2e,0x00,0x20,0x00,0x20,0x00,0x59,0x30,0x50,0x30,0x66,0x8b,0xdf,0x5b,0x6b,0x30,
286 0xfb,0x96,0x71,0x8a,0x92,0x30,0x59,0x30,0x8b,0x30,0x7b,0x30,0x46,0x30,0x4c,0x30,
287 0x44,0x30,0x44,0x30,0x67,0x30,0x59,0x30,0x02,0x30
288 };
289 // encoded by iconv
290 const static unsigned char welcome_utf16be[106] =
291 {
292 0x00,0x57,0x00,0x65,0x00,0x6c,0x00,0x63,0x00,0x6f,0x00,0x6d,0x00,0x65,0x00,0x20,
293 0x00,0x74,0x00,0x6f,0x00,0x20,0x00,0x6f,0x00,0x75,0x00,0x72,0x00,0x20,0x00,0x63,
294 0x00,0x79,0x00,0x62,0x00,0x65,0x00,0x72,0x00,0x20,0x00,0x73,0x00,0x70,0x00,0x61,
295 0x00,0x63,0x00,0x65,0x00,0x20,0x00,0x66,0x00,0x6f,0x00,0x72,0x00,0x63,0x00,0x65,
296 0x00,0x2e,0x00,0x20,0x00,0x20,0x30,0x59,0x30,0x50,0x8b,0x66,0x5b,0xdf,0x30,0x6b,
297 0x96,0xfb,0x8a,0x71,0x30,0x92,0x30,0x59,0x30,0x8b,0x30,0x7b,0x30,0x46,0x30,0x4c,
298 0x30,0x44,0x30,0x44,0x30,0x67,0x30,0x59,0x30,0x02
299 };
300 // encoded by iconv
301 const static unsigned char welcome_utf32le[212] =
302 {
303 0x57,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x63,0x00,0x00,0x00,
304 0x6f,0x00,0x00,0x00,0x6d,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
305 0x74,0x00,0x00,0x00,0x6f,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x6f,0x00,0x00,0x00,
306 0x75,0x00,0x00,0x00,0x72,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x63,0x00,0x00,0x00,
307 0x79,0x00,0x00,0x00,0x62,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x72,0x00,0x00,0x00,
308 0x20,0x00,0x00,0x00,0x73,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x61,0x00,0x00,0x00,
309 0x63,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x66,0x00,0x00,0x00,
310 0x6f,0x00,0x00,0x00,0x72,0x00,0x00,0x00,0x63,0x00,0x00,0x00,0x65,0x00,0x00,0x00,
311 0x2e,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x59,0x30,0x00,0x00,
312 0x50,0x30,0x00,0x00,0x66,0x8b,0x00,0x00,0xdf,0x5b,0x00,0x00,0x6b,0x30,0x00,0x00,
313 0xfb,0x96,0x00,0x00,0x71,0x8a,0x00,0x00,0x92,0x30,0x00,0x00,0x59,0x30,0x00,0x00,
314 0x8b,0x30,0x00,0x00,0x7b,0x30,0x00,0x00,0x46,0x30,0x00,0x00,0x4c,0x30,0x00,0x00,
315 0x44,0x30,0x00,0x00,0x44,0x30,0x00,0x00,0x67,0x30,0x00,0x00,0x59,0x30,0x00,0x00,
316 0x02,0x30,0x00,0x00
317 };
318 // encoded by iconv
319 const static unsigned char welcome_utf32be[212] =
320 {
321 0x00,0x00,0x00,0x57,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x63,
322 0x00,0x00,0x00,0x6f,0x00,0x00,0x00,0x6d,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20,
323 0x00,0x00,0x00,0x74,0x00,0x00,0x00,0x6f,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x6f,
324 0x00,0x00,0x00,0x75,0x00,0x00,0x00,0x72,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x63,
325 0x00,0x00,0x00,0x79,0x00,0x00,0x00,0x62,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x72,
326 0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x73,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x61,
327 0x00,0x00,0x00,0x63,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x66,
328 0x00,0x00,0x00,0x6f,0x00,0x00,0x00,0x72,0x00,0x00,0x00,0x63,0x00,0x00,0x00,0x65,
329 0x00,0x00,0x00,0x2e,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x20,0x00,0x00,0x30,0x59,
330 0x00,0x00,0x30,0x50,0x00,0x00,0x8b,0x66,0x00,0x00,0x5b,0xdf,0x00,0x00,0x30,0x6b,
331 0x00,0x00,0x96,0xfb,0x00,0x00,0x8a,0x71,0x00,0x00,0x30,0x92,0x00,0x00,0x30,0x59,
332 0x00,0x00,0x30,0x8b,0x00,0x00,0x30,0x7b,0x00,0x00,0x30,0x46,0x00,0x00,0x30,0x4c,
333 0x00,0x00,0x30,0x44,0x00,0x00,0x30,0x44,0x00,0x00,0x30,0x67,0x00,0x00,0x30,0x59,
334 0x00,0x00,0x30,0x02
335 };
336 // encoded by iconv
337 const static unsigned char welcome_cp932[71] =
338 {
339 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63,
340 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65,
341 0x2e,0x20,0x20,0x82,0xb7,0x82,0xae,0x8c,0x78,0x8e,0x40,0x82,0xc9,0x93,0x64,0x98,
342 0x62,0x82,0xf0,0x82,0xb7,0x82,0xe9,0x82,0xd9,0x82,0xa4,0x82,0xaa,0x82,0xa2,0x82,
343 0xa2,0x82,0xc5,0x82,0xb7,0x81,0x42
344 };
345
346 #if wxBYTE_ORDER == wxBIG_ENDIAN
347 #if SIZEOF_WCHAR_T == 2
348 #define welcome_wchar_t welcome_utf16be
349 #elif SIZEOF_WCHAR_T == 4
350 #define welcome_wchar_t welcome_utf32be
351 #endif
352 #elif wxBYTE_ORDER == wxLITTLE_ENDIAN
353 #if SIZEOF_WCHAR_T == 2
354 #define welcome_wchar_t welcome_utf16le
355 #elif SIZEOF_WCHAR_T == 4
356 #define welcome_wchar_t welcome_utf32le
357 #endif
358 #endif
359
360 void MBConvTestCase::UTF7Tests()
361 {
362 TestDecoder
363 (
364 (const wchar_t*)welcome_wchar_t,
365 sizeof(welcome_wchar_t)/sizeof(wchar_t),
366 (const char*)welcome_utf7_iconv,
367 sizeof(welcome_utf7_iconv),
368 &wxConvUTF7,
369 1
370 );
371 TestDecoder
372 (
373 (const wchar_t*)welcome_wchar_t,
374 sizeof(welcome_wchar_t)/sizeof(wchar_t),
375 (const char*)welcome_utf7_wx,
376 sizeof(welcome_utf7_wx),
377 &wxConvUTF7,
378 1
379 );
380 #if 0
381 // wxWidget's UTF-7 encoder generates different byte sequences than iconv's.
382 // but both seem to be equally legal.
383 // This test won't work and that's okay.
384 TestEncoder
385 (
386 (const wchar_t*)welcome_wchar_t,
387 sizeof(welcome_wchar_t)/sizeof(wchar_t),
388 (const char*)welcome_utf7_iconv,
389 sizeof(welcome_utf7_iconv),
390 &wxConvUTF7,
391 1
392 );
393 #endif
394 TestEncoder
395 (
396 (const wchar_t*)welcome_wchar_t,
397 sizeof(welcome_wchar_t)/sizeof(wchar_t),
398 (const char*)welcome_utf7_wx,
399 sizeof(welcome_utf7_wx),
400 &wxConvUTF7,
401 1
402 );
403 }
404
405 void MBConvTestCase::UTF8Tests()
406 {
407 TestDecoder
408 (
409 (const wchar_t*)welcome_wchar_t,
410 sizeof(welcome_wchar_t)/sizeof(wchar_t),
411 (const char*)welcome_utf8,
412 sizeof(welcome_utf8),
413 &wxConvUTF8,
414 1
415 );
416 TestEncoder
417 (
418 (const wchar_t*)welcome_wchar_t,
419 sizeof(welcome_wchar_t)/sizeof(wchar_t),
420 (const char*)welcome_utf8,
421 sizeof(welcome_utf8),
422 &wxConvUTF8,
423 1
424 );
425 }
426
427 void MBConvTestCase::UTF16LETests()
428 {
429 wxMBConvUTF16LE convUTF16LE;
430 TestDecoder
431 (
432 (const wchar_t*)welcome_wchar_t,
433 sizeof(welcome_wchar_t)/sizeof(wchar_t),
434 (const char*)welcome_utf16le,
435 sizeof(welcome_utf16le),
436 &convUTF16LE,
437 2
438 );
439 TestEncoder
440 (
441 (const wchar_t*)welcome_wchar_t,
442 sizeof(welcome_wchar_t)/sizeof(wchar_t),
443 (const char*)welcome_utf16le,
444 sizeof(welcome_utf16le),
445 &convUTF16LE,
446 2
447 );
448 }
449
450 void MBConvTestCase::UTF16BETests()
451 {
452 wxMBConvUTF16BE convUTF16BE;
453 TestDecoder
454 (
455 (const wchar_t*)welcome_wchar_t,
456 sizeof(welcome_wchar_t)/sizeof(wchar_t),
457 (const char*)welcome_utf16be,
458 sizeof(welcome_utf16be),
459 &convUTF16BE,
460 2
461 );
462 TestEncoder
463 (
464 (const wchar_t*)welcome_wchar_t,
465 sizeof(welcome_wchar_t)/sizeof(wchar_t),
466 (const char*)welcome_utf16be,
467 sizeof(welcome_utf16be),
468 &convUTF16BE,
469 2
470 );
471 }
472
473 void MBConvTestCase::UTF32LETests()
474 {
475 wxMBConvUTF32LE convUTF32LE;
476 TestDecoder
477 (
478 (const wchar_t*)welcome_wchar_t,
479 sizeof(welcome_wchar_t)/sizeof(wchar_t),
480 (const char*)welcome_utf32le,
481 sizeof(welcome_utf32le),
482 &convUTF32LE,
483 4
484 );
485 TestEncoder
486 (
487 (const wchar_t*)welcome_wchar_t,
488 sizeof(welcome_wchar_t)/sizeof(wchar_t),
489 (const char*)welcome_utf32le,
490 sizeof(welcome_utf32le),
491 &convUTF32LE,
492 4
493 );
494 }
495
496 void MBConvTestCase::UTF32BETests()
497 {
498 wxMBConvUTF32BE convUTF32BE;
499 TestDecoder
500 (
501 (const wchar_t*)welcome_wchar_t,
502 sizeof(welcome_wchar_t)/sizeof(wchar_t),
503 (const char*)welcome_utf32be,
504 sizeof(welcome_utf32be),
505 &convUTF32BE,
506 4
507 );
508 TestEncoder
509 (
510 (const wchar_t*)welcome_wchar_t,
511 sizeof(welcome_wchar_t)/sizeof(wchar_t),
512 (const char*)welcome_utf32be,
513 sizeof(welcome_utf32be),
514 &convUTF32BE,
515 4
516 );
517 }
518
519 void MBConvTestCase::CP932Tests()
520 {
521 wxCSConv convCP932( wxFONTENCODING_CP932 );
522 TestDecoder
523 (
524 (const wchar_t*)welcome_wchar_t,
525 sizeof(welcome_wchar_t)/sizeof(wchar_t),
526 (const char*)welcome_cp932,
527 sizeof(welcome_cp932),
528 &convCP932,
529 1
530 );
531 TestEncoder
532 (
533 (const wchar_t*)welcome_wchar_t,
534 sizeof(welcome_wchar_t)/sizeof(wchar_t),
535 (const char*)welcome_cp932,
536 sizeof(welcome_cp932),
537 &convCP932,
538 1
539 );
540 }
541
542 // a character sequence encoded as iso8859-1 (iconv)
543 static const unsigned char iso8859_1[251] =
544 {
545 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
546 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
547 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
548 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
549 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
550 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
551 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
552 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x80,0x81,0x82,0x83,0x84,
553 0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,
554 0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa1,0xa2,0xa3,0xa4,
555 0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,
556 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc4,
557 0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0xd4,
558 0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,0xe0,0xe1,0xe2,0xe3,0xe4,
559 0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,0xf0,0xf1,0xf2,0xf3,0xf4,
560 0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
561 };
562 // the above character sequence encoded as UTF-8 (iconv)
563 static const unsigned char iso8859_1_utf8[379] =
564 {
565 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
566 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
567 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
568 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
569 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
570 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
571 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
572 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0xc2,0x80,0xc2,0x81,0xc2,
573 0x82,0xc2,0x83,0xc2,0x84,0xc2,0x85,0xc2,0x86,0xc2,0x87,0xc2,0x88,0xc2,0x89,0xc2,
574 0x8a,0xc2,0x8b,0xc2,0x8c,0xc2,0x8d,0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0xb4,0xc3,0xb5,0xc3,0xb6,0xc3,0xb7,0xc3,0xb8,0xc3,0xb9,0xc3,
588 0xba,0xc3,0xbb,0xc3,0xbc,0xc3,0xbd,0xc3,0xbe,0xc3,0xbf
589 };
590
591 // a character sequence encoded as CP1252 (iconv)
592 static const unsigned char CP1252[246] =
593 {
594 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
595 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
596 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
597 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
598 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
599 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
600 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
601 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0xa0,0xa1,0xa2,0xa3,0xa4,
602 0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,
603 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc4,
604 0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0xd4,
605 0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,0xe0,0xe1,0xe2,0xe3,0xe4,
606 0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,0xf0,0xf1,0xf2,0xf3,0xf4,
607 0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,0x8c,0x9c,0x8a,0x9a,0x9f,
608 0x8e,0x9e,0x83,0x88,0x98,0x96,0x97,0x91,0x92,0x82,0x93,0x94,0x84,0x86,0x87,0x95,
609 0x85,0x89,0x8b,0x9b,0x80,0x99
610 };
611 // the above character sequence encoded as UTF-8 (iconv)
612 static const unsigned char CP1252_utf8[386] =
613 {
614 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
615 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
616 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
617 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
618 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
619 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
620 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
621 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0xc2,0xa0,0xc2,0xa1,0xc2,
622 0xa2,0xc2,0xa3,0xc2,0xa4,0xc2,0xa5,0xc2,0xa6,0xc2,0xa7,0xc2,0xa8,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x99,0xe2,0x80,0x9a,0xe2,0x80,
636 0x9c,0xe2,0x80,0x9d,0xe2,0x80,0x9e,0xe2,0x80,0xa0,0xe2,0x80,0xa1,0xe2,0x80,0xa2,
637 0xe2,0x80,0xa6,0xe2,0x80,0xb0,0xe2,0x80,0xb9,0xe2,0x80,0xba,0xe2,0x82,0xac,0xe2,
638 0x84,0xa2
639 };
640
641 // a character sequence encoded as iso8859-5 (iconv)
642 static const unsigned char iso8859_5[251] =
643 {
644 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
645 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
646 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
647 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
648 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
649 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
650 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
651 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x80,0x81,0x82,0x83,0x84,
652 0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,
653 0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xfd,0xad,0xa1,0xa2,
654 0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,
655 0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,
656 0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,
657 0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,0xe0,0xe1,0xe2,0xe3,
658 0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,0xf1,0xf2,0xf3,0xf4,
659 0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfe,0xff,0xf0
660 };
661 // the above character sequence encoded as UTF-8 (iconv)
662 static const unsigned char iso8859_5_utf8[380] =
663 {
664 0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,
665 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
666 0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,
667 0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,
668 0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,
669 0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,
670 0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,
671 0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d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,0x85,0xd1,0x86,0xd1,0x87,0xd1,0x88,0xd1,
685 0x89,0xd1,0x8a,0xd1,0x8b,0xd1,0x8c,0xd1,0x8d,0xd1,0x8e,0xd1,0x8f,0xd1,0x91,0xd1,
686 0x92,0xd1,0x93,0xd1,0x94,0xd1,0x95,0xd1,0x96,0xd1,0x97,0xd1,0x98,0xd1,0x99,0xd1,
687 0x9a,0xd1,0x9b,0xd1,0x9c,0xd1,0x9e,0xd1,0x9f,0xe2,0x84,0x96
688 };
689
690 // DecodeUTF8
691 // decodes the specified *unterminated* UTF-8 byte array
692 wxWCharBuffer DecodeUTF8(
693 const void* data, // an unterminated UTF-8 encoded byte array
694 size_t size // the byte length of data
695 )
696 {
697 // the decoder requires a null terminated buffer.
698 // the input data is not null terminated.
699 // copy to null terminated buffer
700
701 wxCharBuffer nullTerminated( size+1 );
702 memcpy( nullTerminated.data(), data, size );
703 nullTerminated.data()[size] = 0;
704 return wxConvUTF8.cMB2WC(nullTerminated.data());
705 }
706
707 // tests the encoding and decoding capability of an wxMBConv object
708 //
709 // decodes the utf-8 bytes into wide characters
710 // encodes the wide characters to compare against input multiBuffer
711 // decodes the multiBuffer to compare against wide characters
712 // decodes the multiBuffer into wide characters
713 void MBConvTestCase::TestCoder(
714 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
715 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
716 const char* utf8Buffer, // the same character sequence as multiBuffer, encoded as UTF-8
717 size_t utf8Bytes, // the byte length of the UTF-8 encoded character sequence
718 wxMBConv* converter, // the wxMBConv object thta can decode multiBuffer into a wide character sequence
719 int sizeofNull // the number of bytes occupied by a terminating null in the converter's encoding
720 )
721 {
722 // wide character size and endian-ess varies from platform to platform
723 // compiler support for wide character literals varies from compiler to compiler
724 // so we should store the wide character version as UTF-8 and depend on
725 // the UTF-8 converter's ability to decode it to platform specific wide characters
726 // this test is invalid if the UTF-8 converter can't decode
727 wxWCharBuffer wideBuffer((size_t)0);
728 wideBuffer = DecodeUTF8( utf8Buffer, utf8Bytes );
729 size_t wideChars = wxWcslen( wideBuffer.data() );
730
731 TestDecoder
732 (
733 wideBuffer.data(),
734 wideChars,
735 (const char*)multiBuffer,
736 multiBytes,
737 converter,
738 sizeofNull
739 );
740 TestEncoder
741 (
742 wideBuffer.data(),
743 wideChars,
744 (const char*)multiBuffer,
745 multiBytes,
746 converter,
747 sizeofNull
748 );
749 }
750
751
752 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_wxwin( const wxChar* name );
753
754 void MBConvTestCase::FontmapTests()
755 {
756 #ifdef wxUSE_FONTMAP
757 wxMBConv* converter = new_wxMBConv_wxwin( _T("CP1252") );
758 if ( !converter )
759 {
760 return;
761 }
762 TestCoder(
763 (const char*)CP1252,
764 sizeof(CP1252),
765 (const char*)CP1252_utf8,
766 sizeof(CP1252_utf8),
767 converter,
768 1
769 );
770 delete converter;
771 #endif
772 }
773
774
775 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_iconv( const wxChar* name );
776
777 void MBConvTestCase::IconvTests()
778 {
779 #ifdef HAVE_ICONV
780 wxMBConv* converter = new_wxMBConv_iconv( _T("CP932") );
781 if ( !converter )
782 {
783 return;
784 }
785 TestCoder(
786 (const char*)welcome_cp932,
787 sizeof(welcome_cp932),
788 (const char*)welcome_utf8,
789 sizeof(welcome_utf8),
790 converter,
791 1
792 );
793 delete converter;
794 #endif
795 }
796
797 void MBConvTestCase::CP1252Tests()
798 {
799 wxCSConv convCP1252( wxFONTENCODING_CP1252 );
800 TestCoder(
801 (const char*)CP1252,
802 sizeof(CP1252),
803 (const char*)CP1252_utf8,
804 sizeof(CP1252_utf8),
805 &convCP1252,
806 1
807 );
808 }
809
810 void MBConvTestCase::LibcTests()
811 {
812 // There isn't a locale that all systems support (except "C"), so leave
813 // this one disabled for non-Windows systems for the moment, until
814 // a solution can be found.
815 #ifdef __WXMSW__
816
817 #ifdef __WXMSW__
818 setlocale( LC_ALL, "English_United States.1252" );
819 const unsigned char* systemMB = CP1252;
820 size_t systemMB_size = sizeof(CP1252);
821 const unsigned char* systemMB_utf8 = CP1252_utf8;
822 size_t systemMB_utf8_size = sizeof(CP1252_utf8);
823 #else
824 setlocale( LC_ALL, "en_US.iso8859-1" );
825 const unsigned char* systemMB = iso8859_1;
826 size_t systemMB_size = sizeof(iso8859_1);
827 const unsigned char* systemMB_utf8 = iso8859_1_utf8;
828 size_t systemMB_utf8_size = sizeof(iso8859_1_utf8);
829 #endif
830 wxMBConvLibc convLibc;
831 TestCoder(
832 (const char*)systemMB,
833 systemMB_size,
834 (const char*)systemMB_utf8,
835 systemMB_utf8_size,
836 &convLibc,
837 1
838 );
839
840 #endif // __WXMSW__
841 }
842
843 // verifies that the specified mb sequences decode to the specified wc sequence
844 void MBConvTestCase::TestDecoder(
845 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
846 size_t wideChars, // the number of wide characters at wideBuffer
847 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
848 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
849 wxMBConv* converter, // the wxMBConv object that can decode multiBuffer into a wide character sequence
850 int sizeofNull // number of bytes occupied by terminating null in this encoding
851 )
852 {
853 const unsigned UNINITIALIZED = 0xcd;
854
855 // copy the input bytes into a null terminated buffer
856 wxCharBuffer inputCopy( multiBytes+sizeofNull );
857 memcpy( inputCopy.data(), multiBuffer, multiBytes );
858 memset( &inputCopy.data()[multiBytes], 0, sizeofNull );
859
860 // calculate the output size
861 size_t outputWritten = converter->MB2WC
862 (
863 0,
864 (const char*)inputCopy.data(),
865 0
866 );
867 // make sure the correct output length was calculated
868 CPPUNIT_ASSERT( outputWritten == wideChars );
869
870 // convert the string
871 size_t guardChars = 8; // to make sure we're not overrunning the output buffer
872 size_t nullCharacters = 1;
873 size_t outputBufferChars = outputWritten + nullCharacters + guardChars;
874 wxWCharBuffer outputBuffer(outputBufferChars);
875 memset( outputBuffer.data(), UNINITIALIZED, outputBufferChars*sizeof(wchar_t) );
876
877 outputWritten = converter->MB2WC
878 (
879 outputBuffer.data(),
880 (const char*)inputCopy.data(),
881 outputBufferChars
882 );
883 // make sure the correct number of characters were outputs
884 CPPUNIT_ASSERT( outputWritten == wideChars );
885
886 // make sure the characters generated are correct
887 CPPUNIT_ASSERT( 0 == memcmp( outputBuffer, wideBuffer, wideChars*sizeof(wchar_t) ) );
888
889 // the output buffer should be null terminated
890 CPPUNIT_ASSERT( outputBuffer[outputWritten] == 0 );
891
892 // make sure the rest of the output buffer is untouched
893 for ( size_t i = (wideChars+1)*sizeof(wchar_t); i < (outputBufferChars*sizeof(wchar_t)); i++ )
894 {
895 CPPUNIT_ASSERT( ((unsigned char*)outputBuffer.data())[i] == UNINITIALIZED );
896 }
897
898 #if wxUSE_UNICODE && wxUSE_STREAMS
899 TestStreamDecoder( wideBuffer, wideChars, multiBuffer, multiBytes, converter );
900 #endif
901 }
902
903 // verifies that the specified wc sequences encodes to the specified mb sequence
904 void MBConvTestCase::TestEncoder(
905 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
906 size_t wideChars, // the number of wide characters at wideBuffer
907 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
908 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
909 wxMBConv* converter, // the wxMBConv object that can decode multiBuffer into a wide character sequence
910 int sizeofNull // number of bytes occupied by terminating null in this encoding
911 )
912 {
913 const unsigned UNINITIALIZED = 0xcd;
914
915 // copy the input bytes into a null terminated buffer
916 wxWCharBuffer inputCopy( wideChars + 1 );
917 memcpy( inputCopy.data(), wideBuffer, (wideChars*sizeof(wchar_t)) );
918 inputCopy.data()[wideChars] = 0;
919
920 // calculate the output size
921 size_t outputWritten = converter->WC2MB
922 (
923 0,
924 (const wchar_t*)inputCopy.data(),
925 0
926 );
927 // make sure the correct output length was calculated
928 CPPUNIT_ASSERT( outputWritten == multiBytes );
929
930 // convert the string
931 size_t guardBytes = 8; // to make sure we're not overrunning the output buffer
932 size_t outputBufferSize = outputWritten + sizeofNull + guardBytes;
933 wxCharBuffer outputBuffer(outputBufferSize);
934 memset( outputBuffer.data(), UNINITIALIZED, outputBufferSize );
935
936 outputWritten = converter->WC2MB
937 (
938 outputBuffer.data(),
939 (const wchar_t*)inputCopy.data(),
940 outputBufferSize
941 );
942
943 // make sure the correct number of characters were output
944 CPPUNIT_ASSERT( outputWritten == multiBytes );
945
946 // make sure the characters generated are correct
947 CPPUNIT_ASSERT( 0 == memcmp( outputBuffer, multiBuffer, multiBytes ) );
948
949 size_t i;
950
951 // the output buffer should be null terminated
952 for ( i = multiBytes; i < multiBytes + sizeofNull; i++ )
953 {
954 CPPUNIT_ASSERT( ((unsigned char*)outputBuffer.data())[i] == 0 );
955 }
956
957 // make sure the rest of the output buffer is untouched
958 for ( i = multiBytes + sizeofNull; i < outputBufferSize; i++ )
959 {
960 CPPUNIT_ASSERT( ((unsigned char*)outputBuffer.data())[i] == UNINITIALIZED );
961 }
962
963 #if wxUSE_UNICODE && wxUSE_STREAMS
964 TestStreamEncoder( wideBuffer, wideChars, multiBuffer, multiBytes, converter );
965 #endif
966 }
967
968 #if wxUSE_UNICODE && wxUSE_STREAMS
969 // use wxTextInputStream to exercise wxMBConv interface
970 // (this reveals some bugs in certain wxMBConv subclasses)
971 void MBConvTestCase::TestStreamDecoder(
972 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
973 size_t wideChars, // the number of wide characters at wideBuffer
974 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
975 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
976 wxMBConv* converter // the wxMBConv object that can decode multiBuffer into a wide character sequence
977 )
978 {
979 // this isn't meant to test wxMemoryInputStream or wxTextInputStream
980 // it's meant to test the way wxTextInputStream uses wxMBConv
981 // (which has exposed some problems with wxMBConv)
982 wxMemoryInputStream memoryInputStream( multiBuffer, multiBytes );
983 wxTextInputStream textInputStream( memoryInputStream, wxT(""), *converter );
984 for ( size_t i = 0; i < wideChars; i++ )
985 {
986 wxChar wc = textInputStream.GetChar();
987 CPPUNIT_ASSERT( wc == wideBuffer[i] );
988 }
989 CPPUNIT_ASSERT( 0 == textInputStream.GetChar() );
990 CPPUNIT_ASSERT( memoryInputStream.Eof() );
991 }
992 #endif
993
994 #if wxUSE_UNICODE && wxUSE_STREAMS
995 // use wxTextInputStream to exercise wxMBConv interface
996 // (this reveals some bugs in certain wxMBConv subclasses)
997 void MBConvTestCase::TestStreamEncoder(
998 const wchar_t* wideBuffer, // the same character sequence as multiBuffer, encoded as wchar_t
999 size_t wideChars, // the number of wide characters at wideBuffer
1000 const char* multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
1001 size_t multiBytes, // the byte length of the multibyte character sequence that can be decoded by "converter"
1002 wxMBConv* converter // the wxMBConv object that can decode multiBuffer into a wide character sequence
1003 )
1004 {
1005 // this isn't meant to test wxMemoryOutputStream or wxTextOutputStream
1006 // it's meant to test the way wxTextOutputStream uses wxMBConv
1007 // (which has exposed some problems with wxMBConv)
1008 wxMemoryOutputStream memoryOutputStream;
1009 // wxEOL_UNIX will pass \n \r unchanged
1010 wxTextOutputStream textOutputStream( memoryOutputStream, wxEOL_UNIX, *converter );
1011 for ( size_t i = 0; i < wideChars; i++ )
1012 {
1013 textOutputStream.PutChar( wideBuffer[i] );
1014 }
1015 CPPUNIT_ASSERT_EQUAL( (wxFileOffset)multiBytes, memoryOutputStream.TellO() );
1016 wxCharBuffer copy( memoryOutputStream.TellO() );
1017 memoryOutputStream.CopyTo( copy.data(), memoryOutputStream.TellO());
1018 CPPUNIT_ASSERT_EQUAL( 0, memcmp( copy.data(), multiBuffer, multiBytes ) );
1019 }
1020 #endif
1021
1022
1023 // ----------------------------------------------------------------------------
1024 // UTF-8 tests
1025 // ----------------------------------------------------------------------------
1026
1027 #ifdef HAVE_WCHAR_H
1028
1029 // Check that 'charSequence' translates to 'wideSequence' and back.
1030 // Invalid sequences can be tested by giving NULL for 'wideSequence'. Even
1031 // invalid sequences should roundtrip when an option is given and this is
1032 // checked.
1033 //
1034 void MBConvTestCase::UTF8(const char *charSequence,
1035 const wchar_t *wideSequence)
1036 {
1037 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_NOT);
1038 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_TO_PUA);
1039 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
1040 }
1041
1042 // Use this alternative when 'charSequence' contains a PUA character. Such
1043 // sequences should still roundtrip ok, and this is checked.
1044 //
1045 void MBConvTestCase::UTF8PUA(const char *charSequence,
1046 const wchar_t *wideSequence)
1047 {
1048 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_NOT);
1049 UTF8(charSequence, NULL, wxMBConvUTF8::MAP_INVALID_UTF8_TO_PUA);
1050 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
1051 }
1052
1053 // Use this alternative when 'charSequence' contains an octal escape sequence.
1054 // Such sequences should still roundtrip ok, and this is checked.
1055 //
1056 void MBConvTestCase::UTF8Octal(const char *charSequence,
1057 const wchar_t *wideSequence)
1058 {
1059 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_NOT);
1060 UTF8(charSequence, wideSequence, wxMBConvUTF8::MAP_INVALID_UTF8_TO_PUA);
1061 UTF8(charSequence, NULL, wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
1062 }
1063
1064 // in case wcscpy is missing
1065 //
1066 static wchar_t *wx_wcscpy(wchar_t *dest, const wchar_t *src)
1067 {
1068 wchar_t *d = dest;
1069 while ((*d++ = *src++) != 0)
1070 ;
1071 return dest;
1072 }
1073
1074 // in case wcscat is missing
1075 //
1076 static wchar_t *wx_wcscat(wchar_t *dest, const wchar_t *src)
1077 {
1078 wchar_t *d = dest;
1079 while (*d)
1080 d++;
1081 while ((*d++ = *src++) != 0)
1082 ;
1083 return dest;
1084 }
1085
1086 // in case wcscmp is missing
1087 //
1088 static int wx_wcscmp(const wchar_t *s1, const wchar_t *s2)
1089 {
1090 while (*s1 == *s2 && *s1 != 0)
1091 {
1092 s1++;
1093 s2++;
1094 }
1095 return *s1 - *s2;
1096 }
1097
1098 // in case wcslen is missing
1099 //
1100 static size_t wx_wcslen(const wchar_t *s)
1101 {
1102 const wchar_t *t = s;
1103 while (*t != 0)
1104 t++;
1105 return t - s;
1106 }
1107
1108 // include the option in the error messages so it's possible to see which
1109 // test failed
1110 #define UTF8ASSERT(expr) CPPUNIT_ASSERT_MESSAGE(#expr + errmsg, expr)
1111
1112 // The test implementation
1113 //
1114 void MBConvTestCase::UTF8(const char *charSequence,
1115 const wchar_t *wideSequence,
1116 int option)
1117 {
1118 const size_t BUFSIZE = 128;
1119 wxASSERT(strlen(charSequence) * 3 + 10 < BUFSIZE);
1120 char bytes[BUFSIZE];
1121
1122 // include the option in the error messages so it's possible to see
1123 // which test failed
1124 sprintf(bytes, " (with option == %d)", option);
1125 std::string errmsg(bytes);
1126
1127 // put the charSequence at the start, middle and end of a string
1128 strcpy(bytes, charSequence);
1129 strcat(bytes, "ABC");
1130 strcat(bytes, charSequence);
1131 strcat(bytes, "XYZ");
1132 strcat(bytes, charSequence);
1133
1134 // translate it into wide characters
1135 wxMBConvUTF8 utf8(option);
1136 wchar_t widechars[BUFSIZE];
1137 size_t lenResult = utf8.MB2WC(NULL, bytes, 0);
1138 size_t result = utf8.MB2WC(widechars, bytes, BUFSIZE);
1139 UTF8ASSERT(result == lenResult);
1140
1141 // check we got the expected result
1142 if (wideSequence) {
1143 UTF8ASSERT(result != (size_t)-1);
1144 wxASSERT(result < BUFSIZE);
1145
1146 wchar_t expected[BUFSIZE];
1147 wx_wcscpy(expected, wideSequence);
1148 wx_wcscat(expected, L"ABC");
1149 wx_wcscat(expected, wideSequence);
1150 wx_wcscat(expected, L"XYZ");
1151 wx_wcscat(expected, wideSequence);
1152
1153 UTF8ASSERT(wx_wcscmp(widechars, expected) == 0);
1154 UTF8ASSERT(wx_wcslen(widechars) == result);
1155 }
1156 else {
1157 // If 'wideSequence' is NULL, then the result is expected to be
1158 // invalid. Normally that is as far as we can go, but if there is an
1159 // option then the conversion should succeed anyway, and it should be
1160 // possible to translate back to the original
1161 if (!option) {
1162 UTF8ASSERT(result == (size_t)-1);
1163 return;
1164 }
1165 else {
1166 UTF8ASSERT(result != (size_t)-1);
1167 }
1168 }
1169
1170 // translate it back and check we get the original
1171 char bytesAgain[BUFSIZE];
1172 size_t lenResultAgain = utf8.WC2MB(NULL, widechars, 0);
1173 size_t resultAgain = utf8.WC2MB(bytesAgain, widechars, BUFSIZE);
1174 UTF8ASSERT(resultAgain == lenResultAgain);
1175 UTF8ASSERT(resultAgain != (size_t)-1);
1176 wxASSERT(resultAgain < BUFSIZE);
1177
1178 UTF8ASSERT(strcmp(bytes, bytesAgain) == 0);
1179 UTF8ASSERT(strlen(bytesAgain) == resultAgain);
1180 }
1181
1182 #endif // HAVE_WCHAR_H