added ConversionEmpty() test
[wxWidgets.git] / tests / strings / unicode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/unicode.cpp
3 // Purpose: Unicode unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-04-28
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 // ----------------------------------------------------------------------------
24 // local functions
25 // ----------------------------------------------------------------------------
26
27 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
28
29 // in case wcscmp is missing
30 static int wx_wcscmp(const wchar_t *s1, const wchar_t *s2)
31 {
32 while (*s1 == *s2 && *s1 != 0)
33 {
34 s1++;
35 s2++;
36 }
37 return *s1 - *s2;
38 }
39
40 #endif // wxUSE_WCHAR_T && !wxUSE_UNICODE
41
42 // ----------------------------------------------------------------------------
43 // test class
44 // ----------------------------------------------------------------------------
45
46 class UnicodeTestCase : public CppUnit::TestCase
47 {
48 public:
49 UnicodeTestCase();
50
51 private:
52 CPPUNIT_TEST_SUITE( UnicodeTestCase );
53 CPPUNIT_TEST( ToFromAscii );
54 #if wxUSE_WCHAR_T
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();
64
65 void ToFromAscii();
66 #if wxUSE_WCHAR_T
67 void ConstructorsWithConversion();
68 void ConversionEmpty();
69 void ConversionWithNULs();
70 void ConversionUTF7();
71 void ConversionUTF8();
72 void ConversionUTF16();
73 void ConversionUTF32();
74
75 // test if converting s using the given encoding gives ws and vice versa
76 //
77 // if either of the first 2 arguments is NULL, the conversion is supposed
78 // to fail
79 void DoTestConversion(const char *s, const wchar_t *w, wxMBConv& conv);
80 #endif // wxUSE_WCHAR_T
81
82
83 DECLARE_NO_COPY_CLASS(UnicodeTestCase)
84 };
85
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase );
88
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "Unicode" );
91
92 UnicodeTestCase::UnicodeTestCase()
93 {
94 }
95
96 void UnicodeTestCase::ToFromAscii()
97 {
98
99 #define TEST_TO_FROM_ASCII(txt) \
100 { \
101 static const char *msg = txt; \
102 wxString s = wxString::FromAscii(msg); \
103 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
104 }
105
106 TEST_TO_FROM_ASCII( "Hello, world!" );
107 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
108 }
109
110 #if wxUSE_WCHAR_T
111 void UnicodeTestCase::ConstructorsWithConversion()
112 {
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;
119
120 wxString s1(utf8, wxConvUTF8);
121 wxString s2(wchar, wxConvUTF8);
122
123 #if wxUSE_UNICODE
124 CPPUNIT_ASSERT( s1 == wchar );
125 CPPUNIT_ASSERT( s2 == wchar );
126 #else
127 CPPUNIT_ASSERT( s1 == utf8 );
128 CPPUNIT_ASSERT( s2 == utf8 );
129 #endif
130
131 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
132 wxString s3(utf8, wxConvUTF8, 4);
133 wxString s4(wchar, wxConvUTF8, 3);
134
135 CPPUNIT_ASSERT( s3 == sub );
136 CPPUNIT_ASSERT( s4 == sub );
137
138 #if wxUSE_UNICODE
139 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
140 wxConvUTF8) == wxT("") ); //should stop at pos 35
141 #endif
142 }
143
144 void UnicodeTestCase::ConversionEmpty()
145 {
146 size_t len;
147
148 #if wxUSE_UNICODE
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
153
154 CPPUNIT_ASSERT_EQUAL(0u, len);
155 }
156
157 void UnicodeTestCase::ConversionWithNULs()
158 {
159 #if wxUSE_UNICODE
160 static const size_t lenNulString = 10;
161
162 wxString szTheString(L"The\0String", wxConvLibc, lenNulString);
163 wxCharBuffer theBuffer = szTheString.mb_str();
164
165 CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String",
166 lenNulString + 1) == 0 );
167
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);
176
177 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
178
179 wxString szLocalTheString(wxT("TheString"));
180 szLocalTheString.insert(3, 1, '\0');
181 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
182
183 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
184 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
185 }
186
187 void
188 UnicodeTestCase::DoTestConversion(const char *s,
189 const wchar_t *ws,
190 wxMBConv& conv)
191 {
192 #if wxUSE_UNICODE
193 if ( ws )
194 {
195 wxCharBuffer buf = conv.cWC2MB(ws, (size_t)-1, NULL);
196
197 CPPUNIT_ASSERT( strcmp(buf, s) == 0 );
198 }
199 #else // wxUSE_UNICODE
200 if ( s )
201 {
202 wxWCharBuffer wbuf = conv.cMB2WC(s, (size_t)-1, NULL);
203
204 if ( ws )
205 {
206 CPPUNIT_ASSERT( wbuf.data() );
207 CPPUNIT_ASSERT( wx_wcscmp(wbuf, ws) == 0 );
208 }
209 else // conversion is supposed to fail
210 {
211 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL, wbuf.data() );
212 }
213 }
214 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
215 }
216
217 struct StringConversionData
218 {
219 const char *str;
220 const wchar_t *wcs;
221 };
222
223 void UnicodeTestCase::ConversionUTF7()
224 {
225 static const StringConversionData utf7data[] =
226 {
227 { "+-", L"+" },
228 { "+--", L"+-" },
229
230 #ifdef wxHAVE_U_ESCAPE
231 { "+AKM-", L"\u00a3" },
232 #endif // wxHAVE_U_ESCAPE
233
234 // the following are invalid UTF-7 sequences
235 { "+", NULL },
236 { "+~", NULL },
237 { "a+", NULL },
238 };
239
240 wxCSConv conv(_T("utf-7"));
241 for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
242 {
243 const StringConversionData& d = utf7data[n];
244 DoTestConversion(d.str, d.wcs, conv);
245 DoTestConversion(d.str, d.wcs, wxConvUTF7);
246 }
247 }
248
249 void UnicodeTestCase::ConversionUTF8()
250 {
251 static const StringConversionData utf8data[] =
252 {
253 #ifdef wxHAVE_U_ESCAPE
254 { "\xc2\xa3", L"\u00a3" },
255 #endif
256 { "\xc2", NULL },
257 };
258
259 wxCSConv conv(_T("utf-8"));
260 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
261 {
262 const StringConversionData& d = utf8data[n];
263 DoTestConversion(d.str, d.wcs, conv);
264 DoTestConversion(d.str, d.wcs, wxConvUTF8);
265 }
266 }
267
268 void UnicodeTestCase::ConversionUTF16()
269 {
270 static const StringConversionData utf16data[] =
271 {
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" },
276 #endif
277 { "\0f\0o\0o\0\0", L"foo" },
278 };
279
280 wxCSConv conv(wxFONTENCODING_UTF16BE);
281 for ( size_t n = 0; n < WXSIZEOF(utf16data); n++ )
282 {
283 const StringConversionData& d = utf16data[n];
284 DoTestConversion(d.str, d.wcs, conv);
285 }
286
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
290 size_t len;
291 wxWCharBuffer wbuf(conv.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len));
292 CPPUNIT_ASSERT_EQUAL( (size_t)3, len );
293 }
294
295 void UnicodeTestCase::ConversionUTF32()
296 {
297 static const StringConversionData utf32data[] =
298 {
299 #ifdef wxHAVE_U_ESCAPE
300 {
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" },
303 #endif
304 { "\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L"foo" },
305 };
306
307 wxCSConv conv(wxFONTENCODING_UTF32BE);
308 for ( size_t n = 0; n < WXSIZEOF(utf32data); n++ )
309 {
310 const StringConversionData& d = utf32data[n];
311 DoTestConversion(d.str, d.wcs, conv);
312 }
313
314 size_t len;
315 wxWCharBuffer wbuf(conv.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */,
316 12, &len));
317 CPPUNIT_ASSERT_EQUAL( (size_t)3, len );
318 }
319
320 #endif // wxUSE_WCHAR_T
321