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