]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/unicode.cpp
fix wxTextCtrl::Replace() under wxGTK; added unit test for it and describe its effect...
[wxWidgets.git] / tests / strings / unicode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/unicode.cpp
3 // Purpose: Unicode unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-04-28
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
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 // helper class holding the matching MB and WC strings
25 //
26 // either str or wcs (but not both) may be NULL, this means that the conversion
27 // to it should fail
28 struct StringConversionData
29 {
30 StringConversionData(const char *str_, const wchar_t *wcs_, int flags_ = 0)
31 : str(str_), wcs(wcs_), flags(flags_)
32 {
33 }
34
35 const char * const str;
36 const wchar_t * const wcs;
37
38 enum
39 {
40 TEST_BOTH = 0, // test both str -> wcs and wcs -> str
41 ONLY_MB2WC = 1 // only test str -> wcs conversion
42 };
43
44 const int flags;
45
46 // test that the conversion between str and wcs (subject to flags) succeeds
47 //
48 // the first argument is the index in the test array and is used solely for
49 // diagnostics
50 void Test(size_t n, wxMBConv& conv) const
51 {
52 if ( str )
53 {
54 wxWCharBuffer wbuf = conv.cMB2WC(str);
55
56 if ( wcs )
57 {
58 CPPUNIT_ASSERT_MESSAGE
59 (
60 Message(n, "MB2WC failed"),
61 wbuf.data()
62 );
63
64 CPPUNIT_ASSERT_MESSAGE
65 (
66 Message(n, "MB2WC", wbuf, wcs),
67 wxStrcmp(wbuf, wcs) == 0
68 );
69 }
70 else // conversion is supposed to fail
71 {
72 CPPUNIT_ASSERT_MESSAGE
73 (
74 Message(n, "MB2WC succeeded"),
75 !wbuf.data()
76 );
77 }
78 }
79
80 if ( wcs && !(flags & ONLY_MB2WC) )
81 {
82 wxCharBuffer buf = conv.cWC2MB(wcs);
83
84 if ( str )
85 {
86 CPPUNIT_ASSERT_MESSAGE
87 (
88 Message(n, "WC2MB failed"),
89 buf.data()
90 );
91
92 CPPUNIT_ASSERT_MESSAGE
93 (
94 Message(n, "WC2MB", buf, str),
95 strcmp(buf, str) == 0
96 );
97 }
98 else
99 {
100 CPPUNIT_ASSERT_MESSAGE
101 (
102 Message(n, "WC2MB succeeded"),
103 !buf.data()
104 );
105 }
106 }
107 }
108
109 private:
110 static std::string
111 Message(size_t n, const wxString& msg)
112 {
113 return std::string(wxString::Format("#%lu: %s", (unsigned long)n, msg));
114 }
115
116 template <typename T>
117 static std::string
118 Message(size_t n,
119 const char *func,
120 const wxCharTypeBuffer<T>& actual,
121 const T *expected)
122 {
123 return Message(n,
124 wxString::Format("%s returned \"%s\", expected \"%s\"",
125 func, actual.data(), expected));
126 }
127 };
128
129 // ----------------------------------------------------------------------------
130 // test class
131 // ----------------------------------------------------------------------------
132
133 class UnicodeTestCase : public CppUnit::TestCase
134 {
135 public:
136 UnicodeTestCase();
137
138 private:
139 CPPUNIT_TEST_SUITE( UnicodeTestCase );
140 CPPUNIT_TEST( ToFromAscii );
141 CPPUNIT_TEST( ConstructorsWithConversion );
142 CPPUNIT_TEST( ConversionEmpty );
143 CPPUNIT_TEST( ConversionWithNULs );
144 CPPUNIT_TEST( ConversionUTF7 );
145 CPPUNIT_TEST( ConversionUTF8 );
146 CPPUNIT_TEST( ConversionUTF16 );
147 CPPUNIT_TEST( ConversionUTF32 );
148 CPPUNIT_TEST( IsConvOk );
149 #if wxUSE_UNICODE
150 CPPUNIT_TEST( Iteration );
151 #endif
152 CPPUNIT_TEST_SUITE_END();
153
154 void ToFromAscii();
155 void ConstructorsWithConversion();
156 void ConversionEmpty();
157 void ConversionWithNULs();
158 void ConversionUTF7();
159 void ConversionUTF8();
160 void ConversionUTF16();
161 void ConversionUTF32();
162 void IsConvOk();
163 #if wxUSE_UNICODE
164 void Iteration();
165 #endif
166
167 DECLARE_NO_COPY_CLASS(UnicodeTestCase)
168 };
169
170 // register in the unnamed registry so that these tests are run by default
171 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase );
172
173 // also include in it's own registry so that these tests can be run alone
174 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "UnicodeTestCase" );
175
176 UnicodeTestCase::UnicodeTestCase()
177 {
178 }
179
180 void UnicodeTestCase::ToFromAscii()
181 {
182
183 #define TEST_TO_FROM_ASCII(txt) \
184 { \
185 static const char *msg = txt; \
186 wxString s = wxString::FromAscii(msg); \
187 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
188 }
189
190 TEST_TO_FROM_ASCII( "Hello, world!" );
191 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
192 }
193
194 void UnicodeTestCase::ConstructorsWithConversion()
195 {
196 // the string "Déjà" in UTF-8 and wchar_t:
197 const unsigned char utf8Buf[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
198 const unsigned char utf8subBuf[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
199 const char *utf8 = (char *)utf8Buf;
200 const char *utf8sub = (char *)utf8subBuf;
201
202 wxString s1(utf8, wxConvUTF8);
203
204 #if wxUSE_UNICODE
205 const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
206 CPPUNIT_ASSERT_EQUAL( wchar, s1 );
207
208 wxString s2(wchar);
209 CPPUNIT_ASSERT_EQUAL( wchar, s2 );
210 CPPUNIT_ASSERT_EQUAL( utf8, s2 );
211 #else
212 CPPUNIT_ASSERT_EQUAL( utf8, s1 );
213 #endif
214
215 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
216 wxString s3(utf8, wxConvUTF8, 4);
217 CPPUNIT_ASSERT_EQUAL( sub, s3 );
218
219 #if wxUSE_UNICODE
220 wxString s4(wchar, wxConvUTF8, 3);
221 CPPUNIT_ASSERT_EQUAL( sub, s4 );
222
223 // conversion should stop with failure at pos 35
224 wxString s("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f", wxConvUTF8);
225 CPPUNIT_ASSERT( s.empty() );
226 #endif // wxUSE_UNICODE
227
228
229 // test using Unicode strings together with char* strings (this must work
230 // in ANSI mode as well, of course):
231 wxString s5("ascii");
232 CPPUNIT_ASSERT_EQUAL( "ascii", s5 );
233
234 s5 += " value";
235
236 CPPUNIT_ASSERT( strcmp(s5.mb_str(), "ascii value") == 0 );
237 CPPUNIT_ASSERT_EQUAL( "ascii value", s5 );
238 CPPUNIT_ASSERT( s5 != "SomethingElse" );
239 }
240
241 void UnicodeTestCase::ConversionEmpty()
242 {
243 size_t len;
244
245 #if wxUSE_UNICODE
246 wxConvLibc.cWC2MB(L"", 0, &len);
247 #else // !wxUSE_UNICODE
248 wxConvLibc.cMB2WC("", 0, &len);
249 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
250
251 CPPUNIT_ASSERT_EQUAL( 0, len );
252 }
253
254 void UnicodeTestCase::ConversionWithNULs()
255 {
256 #if wxUSE_UNICODE
257 static const size_t lenNulString = 10;
258
259 wxString szTheString(L"The\0String", wxConvLibc, lenNulString);
260 wxCharBuffer theBuffer = szTheString.mb_str();
261
262 CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String",
263 lenNulString + 1) == 0 );
264
265 wxString szTheString2("The\0String", wxConvLocal, lenNulString);
266 CPPUNIT_ASSERT_EQUAL( lenNulString, szTheString2.length() );
267 CPPUNIT_ASSERT( wxTmemcmp(szTheString2.c_str(), L"The\0String",
268 lenNulString + 1) == 0 );
269 #else // !wxUSE_UNICODE
270 wxString szTheString("TheString");
271 szTheString.insert(3, 1, '\0');
272 wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
273
274 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
275
276 wxString szLocalTheString("TheString");
277 szLocalTheString.insert(3, 1, '\0');
278 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
279
280 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
281 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
282 }
283
284 void UnicodeTestCase::ConversionUTF7()
285 {
286 static const StringConversionData utf7data[] =
287 {
288 // normal fragments
289 StringConversionData("+AKM-", L"\xa3"),
290 StringConversionData("+AOk-t+AOk-", L"\xe9t\xe9"),
291
292 // this one is an alternative valid encoding of the same string
293 StringConversionData("+AOk-t+AOk", L"\xe9t\xe9",
294 StringConversionData::ONLY_MB2WC),
295
296 // some special cases
297 StringConversionData("+-", L"+"),
298 StringConversionData("+--", L"+-"),
299
300 // the following are invalid UTF-7 sequences
301 StringConversionData("\xa3", NULL),
302 StringConversionData("+", NULL),
303 StringConversionData("+~", NULL),
304 StringConversionData("a+", NULL),
305 };
306
307 for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
308 {
309 const StringConversionData& d = utf7data[n];
310
311 // converting to/from UTF-7 using iconv() currently doesn't work
312 // because of several problems:
313 // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
314 // to an incomplete and anyhow nonsensical "+AA" string)
315 // - iconv refuses to convert "+-" (although it converts "+-\n" just
316 // fine, go figure)
317 //
318 // I have no idea how to fix this so just disable the test for now
319 #if 0
320 d.Test(n, wxCSConv("utf-7"));
321 #endif
322 d.Test(n, wxConvUTF7);
323 }
324 }
325
326 void UnicodeTestCase::ConversionUTF8()
327 {
328 static const StringConversionData utf8data[] =
329 {
330 #ifdef wxHAVE_U_ESCAPE
331 StringConversionData("\xc2\xa3", L"\u00a3"),
332 #endif
333 StringConversionData("\xc2", NULL),
334 };
335
336 wxCSConv conv(_T("utf-8"));
337 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
338 {
339 const StringConversionData& d = utf8data[n];
340 d.Test(n, conv);
341 d.Test(n, wxConvUTF8);
342 }
343 }
344
345 void UnicodeTestCase::ConversionUTF16()
346 {
347 static const StringConversionData utf16data[] =
348 {
349 #ifdef wxHAVE_U_ESCAPE
350 StringConversionData(
351 "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
352 L"\u041f\u0440\u0438\u0432\u0435\u0442"),
353 StringConversionData(
354 "\x01\0\0b\x01\0\0a\x01\0\0r\0\0",
355 L"\u0100b\u0100a\u0100r"),
356 #endif
357 StringConversionData("\0f\0o\0o\0\0", L"foo"),
358 };
359
360 wxCSConv conv(wxFONTENCODING_UTF16BE);
361 for ( size_t n = 0; n < WXSIZEOF(utf16data); n++ )
362 {
363 const StringConversionData& d = utf16data[n];
364 d.Test(n, conv);
365 }
366
367 // special case: this string has consecutive NULs inside it which don't
368 // terminate the string, this exposed a bug in our conversion code which
369 // got confused in this case
370 size_t len;
371 conv.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len);
372 CPPUNIT_ASSERT_EQUAL( 3, len );
373 }
374
375 void UnicodeTestCase::ConversionUTF32()
376 {
377 static const StringConversionData utf32data[] =
378 {
379 #ifdef wxHAVE_U_ESCAPE
380 StringConversionData(
381 "\0\0\x04\x1f\0\0\x04\x40\0\0\x04\x38\0\0\x04\x32\0\0\x04\x35\0\0\x04\x42\0\0\0\0",
382 L"\u041f\u0440\u0438\u0432\u0435\u0442"),
383 #endif
384 StringConversionData("\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L"foo"),
385 };
386
387 wxCSConv conv(wxFONTENCODING_UTF32BE);
388 for ( size_t n = 0; n < WXSIZEOF(utf32data); n++ )
389 {
390 const StringConversionData& d = utf32data[n];
391 d.Test(n, conv);
392 }
393
394 size_t len;
395 conv.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */, 12, &len);
396 CPPUNIT_ASSERT_EQUAL( 3, len );
397 }
398
399 void UnicodeTestCase::IsConvOk()
400 {
401 CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM).IsOk() );
402 CPPUNIT_ASSERT( wxCSConv("US-ASCII").IsOk() );
403 CPPUNIT_ASSERT( wxCSConv("UTF-8").IsOk() );
404 CPPUNIT_ASSERT( !wxCSConv("NoSuchConversion").IsOk() );
405
406 #ifdef __WINDOWS__
407 CPPUNIT_ASSERT( wxCSConv("WINDOWS-437").IsOk() );
408 #endif
409 }
410
411 #if wxUSE_UNICODE
412 void UnicodeTestCase::Iteration()
413 {
414 // "czech" in Czech ("cestina"):
415 static const char *textUTF8 = "\304\215e\305\241tina";
416 static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
417
418 wxString text(wxString::FromUTF8(textUTF8));
419 CPPUNIT_ASSERT( wxStrcmp(text.wc_str(), textUTF16) == 0 );
420
421 // verify the string was decoded correctly:
422 {
423 size_t idx = 0;
424 for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i, ++idx )
425 {
426 CPPUNIT_ASSERT( *i == textUTF16[idx] );
427 }
428 }
429
430 // overwrite the string with something that is shorter in UTF-8:
431 {
432 for ( wxString::iterator i = text.begin(); i != text.end(); ++i )
433 *i = 'x';
434 }
435
436 // restore the original text now:
437 {
438 wxString::iterator end1 = text.end();
439 wxString::const_iterator end2 = text.end();
440
441 size_t idx = 0;
442 for ( wxString::iterator i = text.begin(); i != text.end(); ++i, ++idx )
443 {
444 *i = textUTF16[idx];
445
446 CPPUNIT_ASSERT( end1 == text.end() );
447 CPPUNIT_ASSERT( end2 == text.end() );
448 }
449
450 CPPUNIT_ASSERT( end1 == text.end() );
451 CPPUNIT_ASSERT( end2 == text.end() );
452 }
453
454 // and verify it again:
455 {
456 size_t idx = 0;
457 for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i, ++idx )
458 {
459 CPPUNIT_ASSERT( *i == textUTF16[idx] );
460 }
461 }
462 }
463 #endif // wxUSE_UNICODE