1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/unicode.cpp
3 // Purpose: Unicode unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
6 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/encconv.h"
25 // ----------------------------------------------------------------------------
26 // helper class holding the matching MB and WC strings
27 // ----------------------------------------------------------------------------
29 struct StringConversionData
31 // either str or wcs (but not both) may be NULL, this means that the conversion
33 StringConversionData(const char *str_
, const wchar_t *wcs_
, int flags_
= 0)
34 : str(str_
), wcs(wcs_
), flags(flags_
)
38 const char * const str
;
39 const wchar_t * const wcs
;
43 TEST_BOTH
= 0, // test both str -> wcs and wcs -> str
44 ONLY_MB2WC
= 1 // only test str -> wcs conversion
49 // test that the conversion between str and wcs (subject to flags) succeeds
51 // the first argument is the index in the test array and is used solely for
53 void Test(size_t n
, wxMBConv
& conv
) const
57 wxWCharBuffer wbuf
= conv
.cMB2WC(str
);
61 CPPUNIT_ASSERT_MESSAGE
63 Message(n
, "MB2WC failed"),
67 CPPUNIT_ASSERT_MESSAGE
69 Message(n
, "MB2WC", wbuf
, wcs
),
70 wxStrcmp(wbuf
, wcs
) == 0
73 else // conversion is supposed to fail
75 CPPUNIT_ASSERT_MESSAGE
77 Message(n
, "MB2WC succeeded"),
83 if ( wcs
&& !(flags
& ONLY_MB2WC
) )
85 wxCharBuffer buf
= conv
.cWC2MB(wcs
);
89 CPPUNIT_ASSERT_MESSAGE
91 Message(n
, "WC2MB failed"),
95 CPPUNIT_ASSERT_MESSAGE
97 Message(n
, "WC2MB", buf
, str
),
103 CPPUNIT_ASSERT_MESSAGE
105 Message(n
, "WC2MB succeeded"),
114 Message(size_t n
, const wxString
& msg
)
116 return std::string(wxString::Format("#%lu: %s", (unsigned long)n
, msg
));
119 template <typename T
>
123 const wxCharTypeBuffer
<T
>& actual
,
127 wxString::Format("%s returned \"%s\", expected \"%s\"",
128 func
, actual
.data(), expected
));
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class UnicodeTestCase
: public CppUnit::TestCase
142 CPPUNIT_TEST_SUITE( UnicodeTestCase
);
143 CPPUNIT_TEST( ToFromAscii
);
144 CPPUNIT_TEST( ConstructorsWithConversion
);
145 CPPUNIT_TEST( ConversionFixed
);
146 CPPUNIT_TEST( ConversionWithNULs
);
147 CPPUNIT_TEST( ConversionUTF7
);
148 CPPUNIT_TEST( ConversionUTF8
);
149 CPPUNIT_TEST( ConversionUTF16
);
150 CPPUNIT_TEST( ConversionUTF32
);
151 CPPUNIT_TEST( IsConvOk
);
153 CPPUNIT_TEST( Iteration
);
155 CPPUNIT_TEST_SUITE_END();
158 void ConstructorsWithConversion();
159 void ConversionFixed();
160 void ConversionWithNULs();
161 void ConversionUTF7();
162 void ConversionUTF8();
163 void ConversionUTF16();
164 void ConversionUTF32();
170 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
173 // register in the unnamed registry so that these tests are run by default
174 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
176 // also include in its own registry so that these tests can be run alone
177 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "UnicodeTestCase" );
179 UnicodeTestCase::UnicodeTestCase()
183 void UnicodeTestCase::ToFromAscii()
186 #define TEST_TO_FROM_ASCII(txt) \
188 static const char *msg = txt; \
189 wxString s = wxString::FromAscii(msg); \
190 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
193 TEST_TO_FROM_ASCII( "Hello, world!" );
194 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
197 void UnicodeTestCase::ConstructorsWithConversion()
199 // the string "Déjà" in UTF-8 and wchar_t:
200 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
201 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
202 const char *utf8
= (char *)utf8Buf
;
203 const char *utf8sub
= (char *)utf8subBuf
;
205 wxString
s1(utf8
, wxConvUTF8
);
208 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
209 CPPUNIT_ASSERT_EQUAL( wchar
, s1
);
212 CPPUNIT_ASSERT_EQUAL( wchar
, s2
);
213 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(utf8
), s2
);
215 CPPUNIT_ASSERT_EQUAL( utf8
, s1
);
218 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
219 wxString
s3(utf8
, wxConvUTF8
, 4);
220 CPPUNIT_ASSERT_EQUAL( sub
, s3
);
223 wxString
s4(wchar
, wxConvUTF8
, 3);
224 CPPUNIT_ASSERT_EQUAL( sub
, s4
);
226 // conversion should stop with failure at pos 35
227 wxString
s("\t[pl]open.format.Sformatuj dyskietk\xea=gfloppy %f", wxConvUTF8
);
228 CPPUNIT_ASSERT( s
.empty() );
229 #endif // wxUSE_UNICODE
232 // test using Unicode strings together with char* strings (this must work
233 // in ANSI mode as well, of course):
234 wxString
s5("ascii");
235 CPPUNIT_ASSERT_EQUAL( "ascii", s5
);
239 CPPUNIT_ASSERT( strcmp(s5
.mb_str(), "ascii value") == 0 );
240 CPPUNIT_ASSERT_EQUAL( "ascii value", s5
);
241 CPPUNIT_ASSERT( s5
!= "SomethingElse" );
244 void UnicodeTestCase::ConversionFixed()
249 wxConvLibc
.cWC2MB(L
"", 0, &len
);
250 #else // !wxUSE_UNICODE
251 wxConvLibc
.cMB2WC("", 0, &len
);
252 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
254 CPPUNIT_ASSERT_EQUAL( 0, len
);
257 // check that when we convert a fixed number of characters we obtain the
258 // expected return value
259 CPPUNIT_ASSERT_EQUAL( 0, wxConvLibc
.ToWChar(NULL
, 0, "", 0) );
260 CPPUNIT_ASSERT_EQUAL( 1, wxConvLibc
.ToWChar(NULL
, 0, "x", 1) );
261 CPPUNIT_ASSERT_EQUAL( 2, wxConvLibc
.ToWChar(NULL
, 0, "x", 2) );
262 CPPUNIT_ASSERT_EQUAL( 2, wxConvLibc
.ToWChar(NULL
, 0, "xy", 2) );
263 #endif // wxUSE_UNICODE
266 void UnicodeTestCase::ConversionWithNULs()
269 static const size_t lenNulString
= 10;
271 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
272 wxCharBuffer theBuffer
= szTheString
.mb_str();
274 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
275 lenNulString
+ 1) == 0 );
277 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
278 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
279 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
280 lenNulString
+ 1) == 0 );
281 #else // !wxUSE_UNICODE
282 wxString
szTheString("TheString");
283 szTheString
.insert(3, 1, '\0');
284 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
286 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
288 wxString
szLocalTheString("TheString");
289 szLocalTheString
.insert(3, 1, '\0');
290 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
292 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
293 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
296 void UnicodeTestCase::ConversionUTF7()
298 static const StringConversionData utf7data
[] =
301 StringConversionData("+AKM-", L
"\xa3"),
302 StringConversionData("+AOk-t+AOk-", L
"\xe9t\xe9"),
304 // this one is an alternative valid encoding of the same string
305 StringConversionData("+AOk-t+AOk", L
"\xe9t\xe9",
306 StringConversionData::ONLY_MB2WC
),
308 // some special cases
309 StringConversionData("+-", L
"+"),
310 StringConversionData("+--", L
"+-"),
312 // the following are invalid UTF-7 sequences
313 StringConversionData("\xa3", NULL
),
314 StringConversionData("+", NULL
),
315 StringConversionData("+~", NULL
),
316 StringConversionData("a+", NULL
),
319 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
321 const StringConversionData
& d
= utf7data
[n
];
323 // converting to/from UTF-7 using iconv() currently doesn't work
324 // because of several problems:
325 // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
326 // to an incomplete and anyhow nonsensical "+AA" string)
327 // - iconv refuses to convert "+-" (although it converts "+-\n" just
330 // I have no idea how to fix this so just disable the test for now
332 d
.Test(n
, wxCSConv("utf-7"));
334 d
.Test(n
, wxConvUTF7
);
338 void UnicodeTestCase::ConversionUTF8()
340 static const StringConversionData utf8data
[] =
342 #ifdef wxHAVE_U_ESCAPE
343 StringConversionData("\xc2\xa3", L
"\u00a3"),
345 StringConversionData("\xc2", NULL
),
348 wxCSConv
conv(wxT("utf-8"));
349 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
351 const StringConversionData
& d
= utf8data
[n
];
353 d
.Test(n
, wxConvUTF8
);
357 void UnicodeTestCase::ConversionUTF16()
359 static const StringConversionData utf16data
[] =
361 #ifdef wxHAVE_U_ESCAPE
362 StringConversionData(
363 "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
364 L
"\u041f\u0440\u0438\u0432\u0435\u0442"),
365 StringConversionData(
366 "\x01\0\0b\x01\0\0a\x01\0\0r\0\0",
367 L
"\u0100b\u0100a\u0100r"),
369 StringConversionData("\0f\0o\0o\0\0", L
"foo"),
372 wxCSConv
conv(wxFONTENCODING_UTF16BE
);
373 for ( size_t n
= 0; n
< WXSIZEOF(utf16data
); n
++ )
375 const StringConversionData
& d
= utf16data
[n
];
379 // special case: this string has consecutive NULs inside it which don't
380 // terminate the string, this exposed a bug in our conversion code which
381 // got confused in this case
383 conv
.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len
);
384 CPPUNIT_ASSERT_EQUAL( 3, len
);
387 void UnicodeTestCase::ConversionUTF32()
389 static const StringConversionData utf32data
[] =
391 #ifdef wxHAVE_U_ESCAPE
392 StringConversionData(
393 "\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",
394 L
"\u041f\u0440\u0438\u0432\u0435\u0442"),
396 StringConversionData("\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L
"foo"),
399 wxCSConv
conv(wxFONTENCODING_UTF32BE
);
400 for ( size_t n
= 0; n
< WXSIZEOF(utf32data
); n
++ )
402 const StringConversionData
& d
= utf32data
[n
];
407 conv
.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */, 12, &len
);
408 CPPUNIT_ASSERT_EQUAL( 3, len
);
411 void UnicodeTestCase::IsConvOk()
413 CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM
).IsOk() );
414 CPPUNIT_ASSERT( wxCSConv("US-ASCII").IsOk() );
415 CPPUNIT_ASSERT( wxCSConv("UTF-8").IsOk() );
416 CPPUNIT_ASSERT( !wxCSConv("NoSuchConversion").IsOk() );
419 CPPUNIT_ASSERT( wxCSConv("WINDOWS-437").IsOk() );
424 void UnicodeTestCase::Iteration()
426 // "czech" in Czech ("cestina"):
427 static const char *textUTF8
= "\304\215e\305\241tina";
428 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
430 wxString
text(wxString::FromUTF8(textUTF8
));
431 CPPUNIT_ASSERT( wxStrcmp(text
.wc_str(), textUTF16
) == 0 );
433 // verify the string was decoded correctly:
436 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
438 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
442 // overwrite the string with something that is shorter in UTF-8:
444 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
)
448 // restore the original text now:
450 wxString::iterator end1
= text
.end();
451 wxString::const_iterator end2
= text
.end();
454 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
458 CPPUNIT_ASSERT( end1
== text
.end() );
459 CPPUNIT_ASSERT( end2
== text
.end() );
462 CPPUNIT_ASSERT( end1
== text
.end() );
463 CPPUNIT_ASSERT( end2
== text
.end() );
466 // and verify it again:
469 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
471 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
475 #endif // wxUSE_UNICODE