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