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 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
29 // in case wcscmp is missing
30 static int wx_wcscmp(const wchar_t *s1
, const wchar_t *s2
)
32 while (*s1
== *s2
&& *s1
!= 0)
40 #endif // wxUSE_WCHAR_T && !wxUSE_UNICODE
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class UnicodeTestCase
: public CppUnit::TestCase
52 CPPUNIT_TEST_SUITE( UnicodeTestCase
);
53 CPPUNIT_TEST( ToFromAscii
);
55 CPPUNIT_TEST( ConstructorsWithConversion
);
56 CPPUNIT_TEST( ConversionEmpty
);
57 CPPUNIT_TEST( ConversionWithNULs
);
58 CPPUNIT_TEST( ConversionUTF7
);
59 CPPUNIT_TEST( ConversionUTF8
);
60 CPPUNIT_TEST( ConversionUTF16
);
61 CPPUNIT_TEST( ConversionUTF32
);
62 CPPUNIT_TEST( IsConvOk
);
63 #endif // wxUSE_WCHAR_T
65 CPPUNIT_TEST( Iteration
);
67 CPPUNIT_TEST_SUITE_END();
71 void ConstructorsWithConversion();
72 void ConversionEmpty();
73 void ConversionWithNULs();
74 void ConversionUTF7();
75 void ConversionUTF8();
76 void ConversionUTF16();
77 void ConversionUTF32();
83 // test if converting s using the given encoding gives ws and vice versa
85 // if either of the first 2 arguments is NULL, the conversion is supposed
87 void DoTestConversion(const char *s
, const wchar_t *w
, wxMBConv
& conv
);
88 #endif // wxUSE_WCHAR_T
91 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
94 // register in the unnamed registry so that these tests are run by default
95 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
97 // also include in it's own registry so that these tests can be run alone
98 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "UnicodeTestCase" );
100 UnicodeTestCase::UnicodeTestCase()
104 void UnicodeTestCase::ToFromAscii()
107 #define TEST_TO_FROM_ASCII(txt) \
109 static const char *msg = txt; \
110 wxString s = wxString::FromAscii(msg); \
111 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
114 TEST_TO_FROM_ASCII( "Hello, world!" );
115 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
119 void UnicodeTestCase::ConstructorsWithConversion()
121 // the string "Déjà" in UTF-8 and wchar_t:
122 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
123 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
124 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
125 const char *utf8
= (char *)utf8Buf
;
126 const char *utf8sub
= (char *)utf8subBuf
;
128 wxString
s1(utf8
, wxConvUTF8
);
129 wxString
s2(wchar
, wxConvUTF8
);
132 CPPUNIT_ASSERT( s1
== wchar
);
133 CPPUNIT_ASSERT( s2
== wchar
);
135 CPPUNIT_ASSERT( s1
== utf8
);
136 CPPUNIT_ASSERT( s2
== utf8
);
139 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
140 wxString
s3(utf8
, wxConvUTF8
, 4);
141 wxString
s4(wchar
, wxConvUTF8
, 3);
143 CPPUNIT_ASSERT( s3
== sub
);
144 CPPUNIT_ASSERT( s4
== sub
);
147 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
148 wxConvUTF8
) == wxT("") ); //should stop at pos 35
152 // test using Unicode strings together with char* strings (this must work
153 // in ANSI mode as well, of course):
154 wxString
s5("ascii");
155 CPPUNIT_ASSERT( s5
== "ascii" );
159 CPPUNIT_ASSERT( strcmp(s5
.mb_str(), "ascii value") == 0 );
160 CPPUNIT_ASSERT( s5
== "ascii value" );
161 CPPUNIT_ASSERT( s5
!= "SomethingElse" );
164 void UnicodeTestCase::ConversionEmpty()
169 wxCharBuffer buf
= wxConvLibc
.cWC2MB(L
"", 0, &len
);
170 #else // !wxUSE_UNICODE
171 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WC("", 0, &len
);
172 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
174 CPPUNIT_ASSERT(len
== 0);
177 void UnicodeTestCase::ConversionWithNULs()
180 static const size_t lenNulString
= 10;
182 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
183 wxCharBuffer theBuffer
= szTheString
.mb_str();
185 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
186 lenNulString
+ 1) == 0 );
188 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
189 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
190 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
191 lenNulString
+ 1) == 0 );
192 #else // !wxUSE_UNICODE
193 wxString
szTheString(wxT("TheString"));
194 szTheString
.insert(3, 1, '\0');
195 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
197 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
199 wxString
szLocalTheString(wxT("TheString"));
200 szLocalTheString
.insert(3, 1, '\0');
201 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
203 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
204 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
208 UnicodeTestCase::DoTestConversion(const char *s
,
215 wxCharBuffer buf
= conv
.cWC2MB(ws
, (size_t)-1, NULL
);
217 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
219 #else // wxUSE_UNICODE
222 wxWCharBuffer wbuf
= conv
.cMB2WC(s
, (size_t)-1, NULL
);
226 CPPUNIT_ASSERT( wbuf
.data() );
227 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
229 else // conversion is supposed to fail
231 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL
, wbuf
.data() );
234 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
237 struct StringConversionData
243 void UnicodeTestCase::ConversionUTF7()
245 static const StringConversionData utf7data
[] =
250 #ifdef wxHAVE_U_ESCAPE
251 { "+AKM-", L
"\u00a3" },
252 #endif // wxHAVE_U_ESCAPE
254 // the following are invalid UTF-7 sequences
260 wxCSConv
conv(_T("utf-7"));
261 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
263 const StringConversionData
& d
= utf7data
[n
];
265 // converting to/from UTF-7 using iconv() currently doesn't work
266 // because of several problems:
267 // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
268 // to an incomplete and anyhow nonsensical "+AA" string)
269 // - iconv refuses to convert "+-" (although it converts "+-\n" just
272 // I have no idea how to fix this so just disable the test for now
274 DoTestConversion(d
.str
, d
.wcs
, conv
);
276 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF7
);
280 void UnicodeTestCase::ConversionUTF8()
282 static const StringConversionData utf8data
[] =
284 #ifdef wxHAVE_U_ESCAPE
285 { "\xc2\xa3", L
"\u00a3" },
290 wxCSConv
conv(_T("utf-8"));
291 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
293 const StringConversionData
& d
= utf8data
[n
];
294 DoTestConversion(d
.str
, d
.wcs
, conv
);
295 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF8
);
299 void UnicodeTestCase::ConversionUTF16()
301 static const StringConversionData utf16data
[] =
303 #ifdef wxHAVE_U_ESCAPE
304 { "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
305 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
306 { "\x01\0\0b\x01\0\0a\x01\0\0r\0\0", L
"\u0100b\u0100a\u0100r" },
308 { "\0f\0o\0o\0\0", L
"foo" },
311 wxCSConv
conv(wxFONTENCODING_UTF16BE
);
312 for ( size_t n
= 0; n
< WXSIZEOF(utf16data
); n
++ )
314 const StringConversionData
& d
= utf16data
[n
];
315 DoTestConversion(d
.str
, d
.wcs
, conv
);
318 // special case: this string has consecutive NULs inside it which don't
319 // terminate the string, this exposed a bug in our conversion code which
320 // got confused in this case
322 wxWCharBuffer
wbuf(conv
.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len
));
323 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
326 void UnicodeTestCase::ConversionUTF32()
328 static const StringConversionData utf32data
[] =
330 #ifdef wxHAVE_U_ESCAPE
332 "\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",
333 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
335 { "\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L
"foo" },
338 wxCSConv
conv(wxFONTENCODING_UTF32BE
);
339 for ( size_t n
= 0; n
< WXSIZEOF(utf32data
); n
++ )
341 const StringConversionData
& d
= utf32data
[n
];
342 DoTestConversion(d
.str
, d
.wcs
, conv
);
346 wxWCharBuffer
wbuf(conv
.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */,
348 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
351 void UnicodeTestCase::IsConvOk()
353 CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM
).IsOk() );
354 CPPUNIT_ASSERT( wxCSConv(_T("UTF-8")).IsOk() );
355 CPPUNIT_ASSERT( !wxCSConv(_T("NoSuchConversion")).IsOk() );
358 CPPUNIT_ASSERT( wxCSConv(_T("WINDOWS-437")).IsOk() );
362 #endif // wxUSE_WCHAR_T
365 void UnicodeTestCase::Iteration()
367 // "czech" in Czech ("cestina"):
368 static const char *textUTF8
= "\304\215e\305\241tina";
369 static const wchar_t textUTF16
[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
371 wxString
text(wxString::FromUTF8(textUTF8
));
372 CPPUNIT_ASSERT( wxStrcmp(text
.wc_str(), textUTF16
) == 0 );
374 // verify the string was decoded correctly:
377 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
379 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
383 // overwrite the string with something that is shorter in UTF-8:
385 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
)
389 // restore the original text now:
391 wxString::iterator end1
= text
.end();
392 wxString::const_iterator end2
= text
.end();
395 for ( wxString::iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
399 CPPUNIT_ASSERT( end1
== text
.end() );
400 CPPUNIT_ASSERT( end2
== text
.end() );
403 CPPUNIT_ASSERT( end1
== text
.end() );
404 CPPUNIT_ASSERT( end2
== text
.end() );
407 // and verify it again:
410 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
, ++idx
)
412 CPPUNIT_ASSERT( *i
== textUTF16
[idx
] );
416 #endif // wxUSE_UNICODE