1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/unicode.cpp
3 // Purpose: Unicode unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 // helper class holding the matching MB and WC strings
26 // either str or wcs (but not both) may be NULL, this means that the conversion
28 struct StringConversionData
35 TEST_BOTH
= 0, // test both str -> wcs and wcs -> str
36 ONLY_MB2WC
= 1 // only test str -> wcs conversion
41 // test that the conversion between str and wcs (subject to flags) succeeds
43 // the first argument is the index in the test array and is used solely for
45 void Test(size_t n
, wxMBConv
& conv
) const
49 wxWCharBuffer wbuf
= conv
.cMB2WC(str
);
53 CPPUNIT_ASSERT_MESSAGE
55 Message(n
, "MB2WC failed"),
59 CPPUNIT_ASSERT_MESSAGE
61 Message(n
, "MB2WC", wbuf
, wcs
),
62 wxStrcmp(wbuf
, wcs
) == 0
65 else // conversion is supposed to fail
67 CPPUNIT_ASSERT_MESSAGE
69 Message(n
, "MB2WC succeeded"),
75 if ( wcs
&& !(flags
& ONLY_MB2WC
) )
77 wxCharBuffer buf
= conv
.cWC2MB(wcs
);
81 CPPUNIT_ASSERT_MESSAGE
83 Message(n
, "WC2MB failed"),
87 CPPUNIT_ASSERT_MESSAGE
89 Message(n
, "WC2MB", buf
, str
),
95 CPPUNIT_ASSERT_MESSAGE
97 Message(n
, "WC2MB succeeded"),
106 Message(size_t n
, const wxString
& msg
)
108 return std::string(wxString::Format("#%lu: %s", (unsigned long)n
, msg
));
111 template <typename T
>
115 const wxCharTypeBuffer
<T
>& actual
,
119 wxString::Format("%s returned \"%s\", expected \"%s\"",
120 func
, actual
.data(), expected
));
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class UnicodeTestCase
: public CppUnit::TestCase
134 CPPUNIT_TEST_SUITE( UnicodeTestCase
);
135 CPPUNIT_TEST( ToFromAscii
);
136 CPPUNIT_TEST( ConstructorsWithConversion
);
137 CPPUNIT_TEST( ConversionEmpty
);
138 CPPUNIT_TEST( ConversionWithNULs
);
139 CPPUNIT_TEST( ConversionUTF7
);
140 CPPUNIT_TEST( ConversionUTF8
);
141 CPPUNIT_TEST( ConversionUTF16
);
142 CPPUNIT_TEST( ConversionUTF32
);
143 CPPUNIT_TEST( IsConvOk
);
145 CPPUNIT_TEST( Iteration
);
147 CPPUNIT_TEST_SUITE_END();
150 void ConstructorsWithConversion();
151 void ConversionEmpty();
152 void ConversionWithNULs();
153 void ConversionUTF7();
154 void ConversionUTF8();
155 void ConversionUTF16();
156 void ConversionUTF32();
162 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
165 // register in the unnamed registry so that these tests are run by default
166 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
168 // also include in it's own registry so that these tests can be run alone
169 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "UnicodeTestCase" );
171 UnicodeTestCase::UnicodeTestCase()
175 void UnicodeTestCase::ToFromAscii()
178 #define TEST_TO_FROM_ASCII(txt) \
180 static const char *msg = txt; \
181 wxString s = wxString::FromAscii(msg); \
182 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
185 TEST_TO_FROM_ASCII( "Hello, world!" );
186 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
189 void UnicodeTestCase::ConstructorsWithConversion()
191 // the string "Déjà" in UTF-8 and wchar_t:
192 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
193 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
194 const char *utf8
= (char *)utf8Buf
;
195 const char *utf8sub
= (char *)utf8subBuf
;
197 wxString
s1(utf8
, wxConvUTF8
);
200 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
201 CPPUNIT_ASSERT_EQUAL( wchar
, s1
);
204 CPPUNIT_ASSERT_EQUAL( wchar
, s2
);
205 CPPUNIT_ASSERT_EQUAL( utf8
, s2
);
207 CPPUNIT_ASSERT_EQUAL( utf8
, s1
);
210 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
211 wxString
s3(utf8
, wxConvUTF8
, 4);
212 CPPUNIT_ASSERT_EQUAL( sub
, s3
);
215 wxString
s4(wchar
, wxConvUTF8
, 3);
216 CPPUNIT_ASSERT_EQUAL( sub
, s4
);
218 // conversion should stop with failure at pos 35
219 wxString
s("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f", wxConvUTF8
);
220 CPPUNIT_ASSERT( s
.empty() );
221 #endif // wxUSE_UNICODE
224 // test using Unicode strings together with char* strings (this must work
225 // in ANSI mode as well, of course):
226 wxString
s5("ascii");
227 CPPUNIT_ASSERT_EQUAL( "ascii", s5
);
231 CPPUNIT_ASSERT( strcmp(s5
.mb_str(), "ascii value") == 0 );
232 CPPUNIT_ASSERT_EQUAL( "ascii value", s5
);
233 CPPUNIT_ASSERT( s5
!= "SomethingElse" );
236 void UnicodeTestCase::ConversionEmpty()
241 wxCharBuffer buf
= wxConvLibc
.cWC2MB(L
"", 0, &len
);
242 #else // !wxUSE_UNICODE
243 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WC("", 0, &len
);
244 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
246 CPPUNIT_ASSERT(len
== 0);
249 void UnicodeTestCase::ConversionWithNULs()
252 static const size_t lenNulString
= 10;
254 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
255 wxCharBuffer theBuffer
= szTheString
.mb_str();
257 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
258 lenNulString
+ 1) == 0 );
260 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
261 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
262 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
263 lenNulString
+ 1) == 0 );
264 #else // !wxUSE_UNICODE
265 wxString
szTheString("TheString");
266 szTheString
.insert(3, 1, '\0');
267 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
269 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
271 wxString
szLocalTheString("TheString");
272 szLocalTheString
.insert(3, 1, '\0');
273 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
275 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
276 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
279 void UnicodeTestCase::ConversionUTF7()
281 static const StringConversionData utf7data
[] =
284 { "+AKM-", L
"\xa3" },
285 { "+AOk-t+AOk-", L
"\xe9t\xe9" },
287 // this one is an alternative valid encoding of the same string
288 { "+AOk-t+AOk", L
"\xe9t\xe9", StringConversionData::ONLY_MB2WC
},
290 // some special cases
294 // the following are invalid UTF-7 sequences
301 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
303 const StringConversionData
& d
= utf7data
[n
];
305 // converting to/from UTF-7 using iconv() currently doesn't work
306 // because of several problems:
307 // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
308 // to an incomplete and anyhow nonsensical "+AA" string)
309 // - iconv refuses to convert "+-" (although it converts "+-\n" just
312 // I have no idea how to fix this so just disable the test for now
314 d
.Test(n
, wxCSConv("utf-7"));
316 d
.Test(n
, wxConvUTF7
);
320 void UnicodeTestCase::ConversionUTF8()
322 static const StringConversionData utf8data
[] =
324 #ifdef wxHAVE_U_ESCAPE
325 { "\xc2\xa3", L
"\u00a3" },
330 wxCSConv
conv(_T("utf-8"));
331 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
333 const StringConversionData
& d
= utf8data
[n
];
335 d
.Test(n
, wxConvUTF8
);
339 void UnicodeTestCase::ConversionUTF16()
341 static const StringConversionData utf16data
[] =
343 #ifdef wxHAVE_U_ESCAPE
344 { "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
345 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
346 { "\x01\0\0b\x01\0\0a\x01\0\0r\0\0", L
"\u0100b\u0100a\u0100r" },
348 { "\0f\0o\0o\0\0", L
"foo" },
351 wxCSConv
conv(wxFONTENCODING_UTF16BE
);
352 for ( size_t n
= 0; n
< WXSIZEOF(utf16data
); n
++ )
354 const StringConversionData
& d
= utf16data
[n
];
358 // special case: this string has consecutive NULs inside it which don't
359 // terminate the string, this exposed a bug in our conversion code which
360 // got confused in this case
362 wxWCharBuffer
wbuf(conv
.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len
));
363 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
366 void UnicodeTestCase::ConversionUTF32()
368 static const StringConversionData utf32data
[] =
370 #ifdef wxHAVE_U_ESCAPE
372 "\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",
373 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
375 { "\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L
"foo" },
378 wxCSConv
conv(wxFONTENCODING_UTF32BE
);
379 for ( size_t n
= 0; n
< WXSIZEOF(utf32data
); n
++ )
381 const StringConversionData
& d
= utf32data
[n
];
386 wxWCharBuffer
wbuf(conv
.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */,
388 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
391 void UnicodeTestCase::IsConvOk()
393 CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM
).IsOk() );
394 CPPUNIT_ASSERT( wxCSConv("US-ASCII").IsOk() );
395 CPPUNIT_ASSERT( wxCSConv("UTF-8").IsOk() );
396 CPPUNIT_ASSERT( !wxCSConv("NoSuchConversion").IsOk() );
399 CPPUNIT_ASSERT( wxCSConv("WINDOWS-437").IsOk() );
404 void UnicodeTestCase::Iteration()
406 // "czech" in Czech ("cestina"):
407 static const char *textUTF8
= "\304\215e\305\241tina";
408 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
410 wxString
text(wxString::FromUTF8(textUTF8
));
411 CPPUNIT_ASSERT( wxStrcmp(text
.wc_str(), textUTF16
) == 0 );
413 // verify the string was decoded correctly:
416 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
418 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
422 // overwrite the string with something that is shorter in UTF-8:
424 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
)
428 // restore the original text now:
430 wxString::iterator end1
= text
.end();
431 wxString::const_iterator end2
= text
.end();
434 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
438 CPPUNIT_ASSERT( end1
== text
.end() );
439 CPPUNIT_ASSERT( end2
== text
.end() );
442 CPPUNIT_ASSERT( end1
== text
.end() );
443 CPPUNIT_ASSERT( end2
== text
.end() );
446 // and verify it again:
449 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
451 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
455 #endif // wxUSE_UNICODE