let the UTF7 test fail but not crash
[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( ConversionWithNULs );
57 CPPUNIT_TEST( ConversionUTF7 );
58 CPPUNIT_TEST( ConversionUTF8 );
59 CPPUNIT_TEST( ConversionUTF16 );
60 #endif // wxUSE_WCHAR_T
61 CPPUNIT_TEST_SUITE_END();
62
63 void ToFromAscii();
64 #if wxUSE_WCHAR_T
65 void ConstructorsWithConversion();
66 void ConversionWithNULs();
67 void ConversionUTF7();
68 void ConversionUTF8();
69 void ConversionUTF16();
70
71 // test if converting s using the given encoding gives ws and vice versa
72 //
73 // if either of the first 2 arguments is NULL, the conversion is supposed
74 // to fail
75 void DoTestConversion(const char *s, const wchar_t *w, wxMBConv& conv);
76 #endif // wxUSE_WCHAR_T
77
78
79 DECLARE_NO_COPY_CLASS(UnicodeTestCase)
80 };
81
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase );
84
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "UnicodeTestCase" );
87
88 UnicodeTestCase::UnicodeTestCase()
89 {
90 }
91
92 void UnicodeTestCase::ToFromAscii()
93 {
94
95 #define TEST_TO_FROM_ASCII(txt) \
96 { \
97 static const char *msg = txt; \
98 wxString s = wxString::FromAscii(msg); \
99 CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
100 }
101
102 TEST_TO_FROM_ASCII( "Hello, world!" );
103 TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
104 }
105
106 #if wxUSE_WCHAR_T
107 void UnicodeTestCase::ConstructorsWithConversion()
108 {
109 // the string "Déjà" in UTF-8 and wchar_t:
110 const unsigned char utf8Buf[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
111 const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
112 const unsigned char utf8subBuf[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
113 const char *utf8 = (char *)utf8Buf;
114 const char *utf8sub = (char *)utf8subBuf;
115
116 wxString s1(utf8, wxConvUTF8);
117 wxString s2(wchar, wxConvUTF8);
118
119 #if wxUSE_UNICODE
120 CPPUNIT_ASSERT( s1 == wchar );
121 CPPUNIT_ASSERT( s2 == wchar );
122 #else
123 CPPUNIT_ASSERT( s1 == utf8 );
124 CPPUNIT_ASSERT( s2 == utf8 );
125 #endif
126
127 wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
128 wxString s3(utf8, wxConvUTF8, 4);
129 wxString s4(wchar, wxConvUTF8, 3);
130
131 CPPUNIT_ASSERT( s3 == sub );
132 CPPUNIT_ASSERT( s4 == sub );
133
134 #if wxUSE_UNICODE
135 CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f",
136 wxConvUTF8) == wxT("") ); //should stop at pos 35
137 #endif
138 }
139
140 void UnicodeTestCase::ConversionWithNULs()
141 {
142 #if wxUSE_UNICODE
143 static const size_t lenNulString = 10;
144
145 wxString szTheString(L"The\0String", wxConvLibc, lenNulString);
146 wxCharBuffer theBuffer = szTheString.mb_str();
147
148 CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String",
149 lenNulString + 1) == 0 );
150
151 wxString szTheString2("The\0String", wxConvLocal, lenNulString);
152 CPPUNIT_ASSERT_EQUAL( lenNulString, szTheString2.length() );
153 CPPUNIT_ASSERT( wxTmemcmp(szTheString2.c_str(), L"The\0String",
154 lenNulString + 1) == 0 );
155 #else
156 wxString szTheString(wxT("TheString"));
157 szTheString.insert(3, 1, '\0');
158 wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
159
160 CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
161
162 wxString szLocalTheString(wxT("TheString"));
163 szLocalTheString.insert(3, 1, '\0');
164 wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
165
166 CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
167 #endif
168 }
169
170 void
171 UnicodeTestCase::DoTestConversion(const char *s,
172 const wchar_t *ws,
173 wxMBConv& conv)
174 {
175 #if wxUSE_UNICODE
176 if ( ws )
177 {
178 wxCharBuffer buf = conv.cWC2MB(ws, (size_t)-1, NULL);
179
180 CPPUNIT_ASSERT( strcmp(buf, s) == 0 );
181 }
182 #else // wxUSE_UNICODE
183 if ( s )
184 {
185 wxWCharBuffer wbuf = conv.cMB2WC(s, (size_t)-1, NULL);
186
187 if ( ws )
188 {
189 CPPUNIT_ASSERT( wbuf.data() );
190 CPPUNIT_ASSERT( wx_wcscmp(wbuf, ws) == 0 );
191 }
192 else // conversion is supposed to fail
193 {
194 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL, wbuf.data() );
195 }
196 }
197 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
198 }
199
200 struct StringConversionData
201 {
202 const char *str;
203 const wchar_t *wcs;
204 };
205
206 void UnicodeTestCase::ConversionUTF7()
207 {
208 static const StringConversionData utf7data[] =
209 {
210 { "+-", L"+" },
211 { "+--", L"+-" },
212
213 #ifdef wxHAVE_U_ESCAPE
214 { "+AKM-", L"\u00a3" },
215 #endif // wxHAVE_U_ESCAPE
216
217 // the following are invalid UTF-7 sequences
218 { "+", NULL },
219 { "+~", NULL },
220 { "a+", NULL },
221 };
222
223 wxCSConv conv(_T("utf-7"));
224 for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
225 {
226 const StringConversionData& d = utf7data[n];
227 DoTestConversion(d.str, d.wcs, conv);
228 DoTestConversion(d.str, d.wcs, wxConvUTF7);
229 }
230 }
231
232 void UnicodeTestCase::ConversionUTF8()
233 {
234 static const StringConversionData utf8data[] =
235 {
236 #ifdef wxHAVE_U_ESCAPE
237 { "\xc2\xa3", L"\u00a3" },
238 #endif
239 { "\xc2", NULL },
240 };
241
242 wxCSConv conv(_T("utf-8"));
243 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
244 {
245 const StringConversionData& d = utf8data[n];
246 DoTestConversion(d.str, d.wcs, conv);
247 DoTestConversion(d.str, d.wcs, wxConvUTF8);
248 }
249 }
250
251 void UnicodeTestCase::ConversionUTF16()
252 {
253 static const StringConversionData utf16data[] =
254 {
255 #ifdef wxHAVE_U_ESCAPE
256 { "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
257 L"\u041f\u0440\u0438\u0432\u0435\u0442" },
258 #endif
259 { "\0f\0o\0o\0\0", L"foo" },
260 };
261
262 wxCSConv conv(wxFONTENCODING_UTF16BE);
263 for ( size_t n = 0; n < WXSIZEOF(utf16data); n++ )
264 {
265 const StringConversionData& d = utf16data[n];
266 DoTestConversion(d.str, d.wcs, conv);
267 }
268 }
269
270 #endif // wxUSE_WCHAR_T
271