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 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
30 // in case wcscmp is missing
31 static int wx_wcscmp(const wchar_t *s1
, const wchar_t *s2
)
33 while (*s1
== *s2
&& *s1
!= 0)
41 #endif // wxUSE_WCHAR_T && !wxUSE_UNICODE
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class UnicodeTestCase
: public CppUnit::TestCase
53 CPPUNIT_TEST_SUITE( UnicodeTestCase
);
54 CPPUNIT_TEST( ToFromAscii
);
56 CPPUNIT_TEST( ConstructorsWithConversion
);
57 CPPUNIT_TEST( ConversionEmpty
);
58 CPPUNIT_TEST( ConversionWithNULs
);
59 CPPUNIT_TEST( ConversionUTF7
);
60 CPPUNIT_TEST( ConversionUTF8
);
61 CPPUNIT_TEST( ConversionUTF16
);
62 CPPUNIT_TEST( ConversionUTF32
);
63 CPPUNIT_TEST( IsConvOk
);
64 #endif // wxUSE_WCHAR_T
66 CPPUNIT_TEST( Iteration
);
68 CPPUNIT_TEST_SUITE_END();
72 void ConstructorsWithConversion();
73 void ConversionEmpty();
74 void ConversionWithNULs();
75 void ConversionUTF7();
76 void ConversionUTF8();
77 void ConversionUTF16();
78 void ConversionUTF32();
84 // test if converting s using the given encoding gives ws and vice versa
86 // if either of the first 2 arguments is NULL, the conversion is supposed
88 void DoTestConversion(const char *s
, const wchar_t *w
, wxMBConv
& conv
);
89 #endif // wxUSE_WCHAR_T
92 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
95 // register in the unnamed registry so that these tests are run by default
96 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
98 // also include in it's own registry so that these tests can be run alone
99 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "UnicodeTestCase" );
101 UnicodeTestCase::UnicodeTestCase()
105 void UnicodeTestCase::ToFromAscii()
108 #define TEST_TO_FROM_ASCII(txt) \
110 static const char *msg = txt; \
111 wxString s = wxString::FromAscii(msg); \
112 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
115 TEST_TO_FROM_ASCII( "Hello, world!" );
116 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
120 void UnicodeTestCase::ConstructorsWithConversion()
122 // the string "Déjà" in UTF-8 and wchar_t:
123 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
124 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
125 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
126 const char *utf8
= (char *)utf8Buf
;
127 const char *utf8sub
= (char *)utf8subBuf
;
129 wxString
s1(utf8
, wxConvUTF8
);
130 wxString
s2(wchar
, wxConvUTF8
);
133 CPPUNIT_ASSERT( s1
== wchar
);
134 CPPUNIT_ASSERT( s2
== wchar
);
136 CPPUNIT_ASSERT( s1
== utf8
);
137 CPPUNIT_ASSERT( s2
== utf8
);
140 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
141 wxString
s3(utf8
, wxConvUTF8
, 4);
142 wxString
s4(wchar
, wxConvUTF8
, 3);
144 CPPUNIT_ASSERT( s3
== sub
);
145 CPPUNIT_ASSERT( s4
== sub
);
148 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
149 wxConvUTF8
) == wxT("") ); //should stop at pos 35
153 // test using Unicode strings together with char* strings (this must work
154 // in ANSI mode as well, of course):
155 wxString
s5("ascii");
156 CPPUNIT_ASSERT( s5
== "ascii" );
160 CPPUNIT_ASSERT( strcmp(s5
.mb_str(), "ascii value") == 0 );
161 CPPUNIT_ASSERT( s5
== "ascii value" );
162 CPPUNIT_ASSERT( s5
!= "SomethingElse" );
165 void UnicodeTestCase::ConversionEmpty()
170 wxCharBuffer buf
= wxConvLibc
.cWC2MB(L
"", 0, &len
);
171 #else // !wxUSE_UNICODE
172 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WC("", 0, &len
);
173 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
175 CPPUNIT_ASSERT(len
== 0);
178 void UnicodeTestCase::ConversionWithNULs()
181 static const size_t lenNulString
= 10;
183 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
184 wxCharBuffer theBuffer
= szTheString
.mb_str();
186 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
187 lenNulString
+ 1) == 0 );
189 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
190 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
191 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
192 lenNulString
+ 1) == 0 );
193 #else // !wxUSE_UNICODE
194 wxString
szTheString(wxT("TheString"));
195 szTheString
.insert(3, 1, '\0');
196 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
198 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
200 wxString
szLocalTheString(wxT("TheString"));
201 szLocalTheString
.insert(3, 1, '\0');
202 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
204 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
205 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
209 UnicodeTestCase::DoTestConversion(const char *s
,
216 wxCharBuffer buf
= conv
.cWC2MB(ws
, (size_t)-1, NULL
);
218 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
220 #else // wxUSE_UNICODE
223 wxWCharBuffer wbuf
= conv
.cMB2WC(s
, (size_t)-1, NULL
);
227 CPPUNIT_ASSERT( wbuf
.data() );
228 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
230 else // conversion is supposed to fail
232 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL
, wbuf
.data() );
235 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
238 struct StringConversionData
244 void UnicodeTestCase::ConversionUTF7()
246 static const StringConversionData utf7data
[] =
251 #ifdef wxHAVE_U_ESCAPE
252 { "+AKM-", L
"\u00a3" },
253 #endif // wxHAVE_U_ESCAPE
255 // the following are invalid UTF-7 sequences
261 wxCSConv
conv(_T("utf-7"));
262 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
264 const StringConversionData
& d
= utf7data
[n
];
266 // converting to/from UTF-7 using iconv() currently doesn't work
267 // because of several problems:
268 // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
269 // to an incomplete and anyhow nonsensical "+AA" string)
270 // - iconv refuses to convert "+-" (although it converts "+-\n" just
273 // I have no idea how to fix this so just disable the test for now
275 DoTestConversion(d
.str
, d
.wcs
, conv
);
277 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF7
);
281 void UnicodeTestCase::ConversionUTF8()
283 static const StringConversionData utf8data
[] =
285 #ifdef wxHAVE_U_ESCAPE
286 { "\xc2\xa3", L
"\u00a3" },
291 wxCSConv
conv(_T("utf-8"));
292 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
294 const StringConversionData
& d
= utf8data
[n
];
295 DoTestConversion(d
.str
, d
.wcs
, conv
);
296 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF8
);
300 void UnicodeTestCase::ConversionUTF16()
302 static const StringConversionData utf16data
[] =
304 #ifdef wxHAVE_U_ESCAPE
305 { "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
306 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
307 { "\x01\0\0b\x01\0\0a\x01\0\0r\0\0", L
"\u0100b\u0100a\u0100r" },
309 { "\0f\0o\0o\0\0", L
"foo" },
312 wxCSConv
conv(wxFONTENCODING_UTF16BE
);
313 for ( size_t n
= 0; n
< WXSIZEOF(utf16data
); n
++ )
315 const StringConversionData
& d
= utf16data
[n
];
316 DoTestConversion(d
.str
, d
.wcs
, conv
);
319 // special case: this string has consecutive NULs inside it which don't
320 // terminate the string, this exposed a bug in our conversion code which
321 // got confused in this case
323 wxWCharBuffer
wbuf(conv
.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len
));
324 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
327 void UnicodeTestCase::ConversionUTF32()
329 static const StringConversionData utf32data
[] =
331 #ifdef wxHAVE_U_ESCAPE
333 "\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",
334 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
336 { "\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L
"foo" },
339 wxCSConv
conv(wxFONTENCODING_UTF32BE
);
340 for ( size_t n
= 0; n
< WXSIZEOF(utf32data
); n
++ )
342 const StringConversionData
& d
= utf32data
[n
];
343 DoTestConversion(d
.str
, d
.wcs
, conv
);
347 wxWCharBuffer
wbuf(conv
.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */,
349 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
352 void UnicodeTestCase::IsConvOk()
354 CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM
).IsOk() );
355 CPPUNIT_ASSERT( wxCSConv(_T("UTF-8")).IsOk() );
356 CPPUNIT_ASSERT( !wxCSConv(_T("NoSuchConversion")).IsOk() );
359 CPPUNIT_ASSERT( wxCSConv(_T("WINDOWS-437")).IsOk() );
363 #endif // wxUSE_WCHAR_T
366 void UnicodeTestCase::Iteration()
368 // "czech" in Czech ("cestina"):
369 static const char *textUTF8
= "\304\215e\305\241tina";
370 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
372 wxString
text(wxString::FromUTF8(textUTF8
));
373 CPPUNIT_ASSERT( wxStrcmp(text
.wc_str(), textUTF16
) == 0 );
375 // verify the string was decoded correctly:
378 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
380 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
384 // overwrite the string with something that is shorter in UTF-8:
386 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
)
390 // restore the original text now:
392 wxString::iterator end1
= text
.end();
393 wxString::const_iterator end2
= text
.end();
396 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
400 CPPUNIT_ASSERT( end1
== text
.end() );
401 CPPUNIT_ASSERT( end2
== text
.end() );
404 CPPUNIT_ASSERT( end1
== text
.end() );
405 CPPUNIT_ASSERT( end2
== text
.end() );
408 // and verify it again:
411 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
413 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
417 #endif // wxUSE_UNICODE