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( Conversion
);
57 CPPUNIT_TEST( ConversionUTF7
);
58 CPPUNIT_TEST( ConversionUTF8
);
59 #endif // wxUSE_WCHAR_T
60 CPPUNIT_TEST_SUITE_END();
64 void ConstructorsWithConversion();
66 void ConversionUTF7();
67 void ConversionUTF8();
69 // test if converting s using the given encoding gives ws and vice versa
71 // if either of the first 2 arguments is NULL, the conversion is supposed
73 void DoTestConversion(const char *s
, const wchar_t *w
, wxMBConv
& conv
);
74 #endif // wxUSE_WCHAR_T
77 DECLARE_NO_COPY_CLASS(UnicodeTestCase
)
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase
);
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase
, "UnicodeTestCase" );
86 UnicodeTestCase::UnicodeTestCase()
90 void UnicodeTestCase::ToFromAscii()
93 #define TEST_TO_FROM_ASCII(txt) \
95 static const char *msg = txt; \
96 wxString s = wxString::FromAscii(msg); \
97 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
100 TEST_TO_FROM_ASCII( "Hello, world!" );
101 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
105 void UnicodeTestCase::ConstructorsWithConversion()
107 // the string "Déjà" in UTF-8 and wchar_t:
108 const unsigned char utf8Buf
[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
109 const wchar_t wchar
[] = {0x44,0xE9,0x6A,0xE0,0};
110 const unsigned char utf8subBuf
[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
111 const char *utf8
= (char *)utf8Buf
;
112 const char *utf8sub
= (char *)utf8subBuf
;
114 wxString
s1(utf8
, wxConvUTF8
);
115 wxString
s2(wchar
, wxConvUTF8
);
118 CPPUNIT_ASSERT( s1
== wchar
);
119 CPPUNIT_ASSERT( s2
== wchar
);
121 CPPUNIT_ASSERT( s1
== utf8
);
122 CPPUNIT_ASSERT( s2
== utf8
);
125 wxString
sub(utf8sub
, wxConvUTF8
); // "Dej" substring
126 wxString
s3(utf8
, wxConvUTF8
, 4);
127 wxString
s4(wchar
, wxConvUTF8
, 3);
129 CPPUNIT_ASSERT( s3
== sub
);
130 CPPUNIT_ASSERT( s4
== sub
);
133 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
134 wxConvUTF8
) == wxT("") ); //should stop at pos 35
138 void UnicodeTestCase::Conversion()
141 static const size_t lenNulString
= 10;
143 wxString
szTheString(L
"The\0String", wxConvLibc
, lenNulString
);
144 wxCharBuffer theBuffer
= szTheString
.mb_str();
146 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), "The\0String",
147 lenNulString
+ 1) == 0 );
149 wxString
szTheString2("The\0String", wxConvLocal
, lenNulString
);
150 CPPUNIT_ASSERT_EQUAL( lenNulString
, szTheString2
.length() );
151 CPPUNIT_ASSERT( wxTmemcmp(szTheString2
.c_str(), L
"The\0String",
152 lenNulString
+ 1) == 0 );
154 wxString
szTheString(wxT("TheString"));
155 szTheString
.insert(3, 1, '\0');
156 wxWCharBuffer theBuffer
= szTheString
.wc_str(wxConvLibc
);
158 CPPUNIT_ASSERT( memcmp(theBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
160 wxString
szLocalTheString(wxT("TheString"));
161 szLocalTheString
.insert(3, 1, '\0');
162 wxWCharBuffer theLocalBuffer
= szLocalTheString
.wc_str(wxConvLocal
);
164 CPPUNIT_ASSERT( memcmp(theLocalBuffer
.data(), L
"The\0String", 11 * sizeof(wchar_t)) == 0 );
169 UnicodeTestCase::DoTestConversion(const char *s
,
176 wxCharBuffer
buf(wxString(ws
).mb_str(conv
));
178 CPPUNIT_ASSERT( strcmp(buf
, s
) == 0 );
180 #else // wxUSE_UNICODE
183 wxWCharBuffer
wbuf(wxString(s
).wc_str(conv
));
186 CPPUNIT_ASSERT( wx_wcscmp(wbuf
, ws
) == 0 );
188 CPPUNIT_ASSERT( !*wbuf
);
190 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
193 struct StringConversionData
199 void UnicodeTestCase::ConversionUTF7()
201 static const StringConversionData utf7data
[] =
206 #ifdef wxHAVE_U_ESCAPE
207 { "+AKM-", L
"\u00a3" },
208 #endif // wxHAVE_U_ESCAPE
210 // the following are invalid UTF-7 sequences
215 wxCSConv
conv(_T("utf-7"));
216 for ( size_t n
= 0; n
< WXSIZEOF(utf7data
); n
++ )
218 const StringConversionData
& d
= utf7data
[n
];
219 DoTestConversion(d
.str
, d
.wcs
, conv
);
220 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF7
);
224 void UnicodeTestCase::ConversionUTF8()
226 static const StringConversionData utf8data
[] =
228 //\u isn't recognized on MSVC 6
229 #ifdef wxHAVE_U_ESCAPE
230 { "\xc2\xa3", L
"\u00a3" },
235 wxCSConv
conv(_T("utf-8"));
236 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
238 const StringConversionData
& d
= utf8data
[n
];
239 DoTestConversion(d
.str
, d
.wcs
, conv
);
240 DoTestConversion(d
.str
, d
.wcs
, wxConvUTF8
);
244 #endif // wxUSE_WCHAR_T