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