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 #endif // wxUSE_WCHAR_T
63 CPPUNIT_TEST_SUITE_END();
67 void ConstructorsWithConversion();
68 void ConversionEmpty();
69 void ConversionWithNULs();
70 void ConversionUTF7();
71 void ConversionUTF8();
72 void ConversionUTF16();
73 void ConversionUTF32();
75 // test if converting s using the given encoding gives ws and vice versa
77 // if either of the first 2 arguments is NULL, the conversion is supposed
79 void DoTestConversion(const char *s
, const wchar_t *w
, wxMBConv
& conv
);
80 #endif // wxUSE_WCHAR_T
83 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "Unicode" );
92 UnicodeTestCase::UnicodeTestCase()
96 void UnicodeTestCase::ToFromAscii()
99 #define TEST_TO_FROM_ASCII(txt) \
101 static const char *msg = txt; \
102 wxString s = wxString::FromAscii(msg); \
103 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
106 TEST_TO_FROM_ASCII( "Hello, world!" );
107 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
111 void UnicodeTestCase::ConstructorsWithConversion()
113 // the string "Déjà" in UTF-8 and wchar_t:
114 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
115 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
116 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
117 const char *utf8
= (char *)utf8Buf
;
118 const char *utf8sub
= (char *)utf8subBuf
;
120 wxString
s1(utf8
, wxConvUTF8
);
121 wxString
s2(wchar
, wxConvUTF8
);
124 CPPUNIT_ASSERT( s1
== wchar
);
125 CPPUNIT_ASSERT( s2
== wchar
);
127 CPPUNIT_ASSERT( s1
== utf8
);
128 CPPUNIT_ASSERT( s2
== utf8
);
131 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
132 wxString
s3(utf8
, wxConvUTF8
, 4);
133 wxString
s4(wchar
, wxConvUTF8
, 3);
135 CPPUNIT_ASSERT( s3
== sub
);
136 CPPUNIT_ASSERT( s4
== sub
);
139 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
140 wxConvUTF8
) == wxT("") ); //should stop at pos 35
144 void UnicodeTestCase::ConversionEmpty()
149 wxCharBuffer buf
= wxConvLibc
.cWC2MB(L
"", 0, &len
);
150 #else // !wxUSE_UNICODE
151 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WC("", 0, &len
);
152 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
154 CPPUNIT_ASSERT_EQUAL(0u, len
);
157 void UnicodeTestCase::ConversionWithNULs()
160 static const size_t lenNulString
= 10;
162 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
163 wxCharBuffer theBuffer
= szTheString
.mb_str();
165 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
166 lenNulString
+ 1) == 0 );
168 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
169 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
170 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
171 lenNulString
+ 1) == 0 );
172 #else // !wxUSE_UNICODE
173 wxString
szTheString(wxT("TheString"));
174 szTheString
.insert(3, 1, '\0');
175 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
177 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
179 wxString
szLocalTheString(wxT("TheString"));
180 szLocalTheString
.insert(3, 1, '\0');
181 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
183 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
184 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
188 UnicodeTestCase::DoTestConversion(const char *s
,
195 wxCharBuffer buf
= conv
.cWC2MB(ws
, (size_t)-1, NULL
);
197 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
199 #else // wxUSE_UNICODE
202 wxWCharBuffer wbuf
= conv
.cMB2WC(s
, (size_t)-1, NULL
);
206 CPPUNIT_ASSERT( wbuf
.data() );
207 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
209 else // conversion is supposed to fail
211 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL
, wbuf
.data() );
214 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
217 struct StringConversionData
223 void UnicodeTestCase::ConversionUTF7()
225 static const StringConversionData utf7data
[] =
230 #ifdef wxHAVE_U_ESCAPE
231 { "+AKM-", L
"\u00a3" },
232 #endif // wxHAVE_U_ESCAPE
234 // the following are invalid UTF-7 sequences
240 wxCSConv
conv(_T("utf-7"));
241 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
243 const StringConversionData
& d
= utf7data
[n
];
244 DoTestConversion(d
.str
, d
.wcs
, conv
);
245 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF7
);
249 void UnicodeTestCase::ConversionUTF8()
251 static const StringConversionData utf8data
[] =
253 #ifdef wxHAVE_U_ESCAPE
254 { "\xc2\xa3", L
"\u00a3" },
259 wxCSConv
conv(_T("utf-8"));
260 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
262 const StringConversionData
& d
= utf8data
[n
];
263 DoTestConversion(d
.str
, d
.wcs
, conv
);
264 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF8
);
268 void UnicodeTestCase::ConversionUTF16()
270 static const StringConversionData utf16data
[] =
272 #ifdef wxHAVE_U_ESCAPE
273 { "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
274 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
275 { "\x01\0\0b\x01\0\0a\x01\0\0r\0\0", L
"\u0100b\u0100a\u0100r" },
277 { "\0f\0o\0o\0\0", L
"foo" },
280 wxCSConv
conv(wxFONTENCODING_UTF16BE
);
281 for ( size_t n
= 0; n
< WXSIZEOF(utf16data
); n
++ )
283 const StringConversionData
& d
= utf16data
[n
];
284 DoTestConversion(d
.str
, d
.wcs
, conv
);
287 // special case: this string has consecutive NULs inside it which don't
288 // terminate the string, this exposed a bug in our conversion code which
289 // got confused in this case
291 wxWCharBuffer
wbuf(conv
.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len
));
292 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
295 void UnicodeTestCase::ConversionUTF32()
297 static const StringConversionData utf32data
[] =
299 #ifdef wxHAVE_U_ESCAPE
301 "\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",
302 L
"\u041f\u0440\u0438\u0432\u0435\u0442" },
304 { "\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L
"foo" },
307 wxCSConv
conv(wxFONTENCODING_UTF32BE
);
308 for ( size_t n
= 0; n
< WXSIZEOF(utf32data
); n
++ )
310 const StringConversionData
& d
= utf32data
[n
];
311 DoTestConversion(d
.str
, d
.wcs
, conv
);
315 wxWCharBuffer
wbuf(conv
.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */,
317 CPPUNIT_ASSERT_EQUAL( (size_t)3, len
);
320 #endif // wxUSE_WCHAR_T