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