]>
Commit | Line | Data |
---|---|---|
1cd53e88 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/strings/strings.cpp | |
3 | // Purpose: wxString unit test | |
4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba | |
5 | // Created: 2004-04-19 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
1cd53e88 VS |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
1cd53e88 VS |
24 | // ---------------------------------------------------------------------------- |
25 | // test class | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class StringTestCase : public CppUnit::TestCase | |
29 | { | |
30 | public: | |
31 | StringTestCase(); | |
32 | ||
33 | private: | |
34 | CPPUNIT_TEST_SUITE( StringTestCase ); | |
35 | CPPUNIT_TEST( String ); | |
36 | CPPUNIT_TEST( PChar ); | |
37 | CPPUNIT_TEST( Format ); | |
38 | CPPUNIT_TEST( Constructors ); | |
f312f981 | 39 | CPPUNIT_TEST( StaticConstructors ); |
1cd53e88 | 40 | CPPUNIT_TEST( Extraction ); |
e6a99197 | 41 | CPPUNIT_TEST( Trim ); |
1cd53e88 | 42 | CPPUNIT_TEST( Find ); |
1cd53e88 VS |
43 | CPPUNIT_TEST( Replace ); |
44 | CPPUNIT_TEST( Match ); | |
bd7f096d | 45 | CPPUNIT_TEST( CaseChanges ); |
dcb68102 RN |
46 | CPPUNIT_TEST( Compare ); |
47 | CPPUNIT_TEST( CompareNoCase ); | |
c4f35063 | 48 | CPPUNIT_TEST( Contains ); |
4f7ee81a VZ |
49 | CPPUNIT_TEST( ToLong ); |
50 | CPPUNIT_TEST( ToULong ); | |
d6718dd1 VZ |
51 | #ifdef wxLongLong_t |
52 | CPPUNIT_TEST( ToLongLong ); | |
53 | CPPUNIT_TEST( ToULongLong ); | |
54 | #endif // wxLongLong_t | |
4f7ee81a | 55 | CPPUNIT_TEST( ToDouble ); |
062dc5fc | 56 | CPPUNIT_TEST( StringBuf ); |
628f87da | 57 | CPPUNIT_TEST( UTF8Buf ); |
ef0f1387 | 58 | CPPUNIT_TEST( CStrDataTernaryOperator ); |
3a69bca1 | 59 | CPPUNIT_TEST( CStrDataOperators ); |
ef0f1387 VS |
60 | CPPUNIT_TEST( CStrDataImplicitConversion ); |
61 | CPPUNIT_TEST( ExplicitConversion ); | |
6bd4f281 | 62 | CPPUNIT_TEST( IndexedAccess ); |
c565abe1 | 63 | CPPUNIT_TEST( BeforeAndAfter ); |
de4983f3 | 64 | CPPUNIT_TEST( ScopedBuffers ); |
1cd53e88 VS |
65 | CPPUNIT_TEST_SUITE_END(); |
66 | ||
67 | void String(); | |
68 | void PChar(); | |
69 | void Format(); | |
70 | void Constructors(); | |
f312f981 | 71 | void StaticConstructors(); |
1cd53e88 | 72 | void Extraction(); |
e6a99197 | 73 | void Trim(); |
1cd53e88 | 74 | void Find(); |
1cd53e88 VS |
75 | void Replace(); |
76 | void Match(); | |
bd7f096d | 77 | void CaseChanges(); |
dcb68102 RN |
78 | void Compare(); |
79 | void CompareNoCase(); | |
c4f35063 | 80 | void Contains(); |
4f7ee81a VZ |
81 | void ToLong(); |
82 | void ToULong(); | |
d6718dd1 VZ |
83 | #ifdef wxLongLong_t |
84 | void ToLongLong(); | |
85 | void ToULongLong(); | |
86 | #endif // wxLongLong_t | |
4f7ee81a | 87 | void ToDouble(); |
062dc5fc | 88 | void StringBuf(); |
628f87da | 89 | void UTF8Buf(); |
ef0f1387 VS |
90 | void CStrDataTernaryOperator(); |
91 | void DoCStrDataTernaryOperator(bool cond); | |
3a69bca1 | 92 | void CStrDataOperators(); |
ef0f1387 VS |
93 | void CStrDataImplicitConversion(); |
94 | void ExplicitConversion(); | |
6bd4f281 | 95 | void IndexedAccess(); |
c565abe1 | 96 | void BeforeAndAfter(); |
de4983f3 | 97 | void ScopedBuffers(); |
1cd53e88 VS |
98 | |
99 | DECLARE_NO_COPY_CLASS(StringTestCase) | |
100 | }; | |
101 | ||
102 | // register in the unnamed registry so that these tests are run by default | |
103 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
104 | ||
105 | // also include in it's own registry so that these tests can be run alone | |
106 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
107 | ||
108 | StringTestCase::StringTestCase() | |
109 | { | |
110 | } | |
111 | ||
112 | void StringTestCase::String() | |
113 | { | |
114 | wxString a, b, c; | |
115 | ||
116 | a.reserve (128); | |
117 | b.reserve (128); | |
118 | c.reserve (128); | |
119 | ||
120 | for (int i = 0; i < 2; ++i) | |
121 | { | |
9a83f860 VZ |
122 | a = wxT("Hello"); |
123 | b = wxT(" world"); | |
124 | c = wxT("! How'ya doin'?"); | |
1cd53e88 VS |
125 | a += b; |
126 | a += c; | |
9a83f860 | 127 | c = wxT("Hello world! What's up?"); |
1cd53e88 VS |
128 | CPPUNIT_ASSERT( c != a ); |
129 | } | |
130 | } | |
131 | ||
132 | void StringTestCase::PChar() | |
133 | { | |
134 | wxChar a [128]; | |
135 | wxChar b [128]; | |
136 | wxChar c [128]; | |
137 | ||
138 | for (int i = 0; i < 2; ++i) | |
139 | { | |
9a83f860 VZ |
140 | wxStrcpy (a, wxT("Hello")); |
141 | wxStrcpy (b, wxT(" world")); | |
142 | wxStrcpy (c, wxT("! How'ya doin'?")); | |
1cd53e88 VS |
143 | wxStrcat (a, b); |
144 | wxStrcat (a, c); | |
9a83f860 | 145 | wxStrcpy (c, wxT("Hello world! What's up?")); |
1cd53e88 VS |
146 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); |
147 | } | |
148 | } | |
149 | ||
150 | void StringTestCase::Format() | |
151 | { | |
152 | wxString s1,s2; | |
9a83f860 VZ |
153 | s1.Printf(wxT("%03d"), 18); |
154 | CPPUNIT_ASSERT( s1 == wxString::Format(wxT("%03d"), 18) ); | |
155 | s2.Printf(wxT("Number 18: %s\n"), s1.c_str()); | |
156 | CPPUNIT_ASSERT( s2 == wxString::Format(wxT("Number 18: %s\n"), s1.c_str()) ); | |
b03cd7e6 VZ |
157 | |
158 | static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 }; | |
159 | for ( size_t n = 0; n < WXSIZEOF(lengths); n++ ) | |
160 | { | |
161 | const size_t len = lengths[n]; | |
162 | ||
9a83f860 VZ |
163 | wxString s(wxT('Z'), len); |
164 | CPPUNIT_ASSERT_EQUAL( len, wxString::Format(wxT("%s"), s.c_str()).length()); | |
b03cd7e6 | 165 | } |
1cd53e88 VS |
166 | } |
167 | ||
168 | void StringTestCase::Constructors() | |
169 | { | |
1de532f5 VZ |
170 | CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) ); |
171 | CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') ); | |
172 | CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) ); | |
173 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) ); | |
174 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) ); | |
f312f981 VZ |
175 | |
176 | #if wxUSE_UNICODE | |
1de532f5 VZ |
177 | CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) ); |
178 | CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') ); | |
179 | CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) ); | |
180 | CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) ); | |
181 | CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) ); | |
f312f981 VZ |
182 | #endif // wxUSE_UNICODE |
183 | ||
184 | static const char *s = "?really!"; | |
185 | const char *start = wxStrchr(s, 'r'); | |
186 | const char *end = wxStrchr(s, '!'); | |
1de532f5 | 187 | CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) ); |
7652a41d VS |
188 | |
189 | // test if creating string from NULL C pointer works: | |
1de532f5 | 190 | CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) ); |
1cd53e88 VS |
191 | } |
192 | ||
f312f981 VZ |
193 | void StringTestCase::StaticConstructors() |
194 | { | |
1de532f5 VZ |
195 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") ); |
196 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) ); | |
197 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) ); | |
198 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) ); | |
199 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") ); | |
f312f981 VZ |
200 | |
201 | // FIXME: this doesn't work currently but should! | |
1de532f5 | 202 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() ); |
f312f981 VZ |
203 | |
204 | ||
1de532f5 VZ |
205 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") ); |
206 | CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) ); | |
207 | CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) ); | |
208 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) ); | |
209 | CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") ); | |
f312f981 | 210 | |
1de532f5 | 211 | //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() ); |
f312f981 | 212 | } |
265d5cce | 213 | |
1cd53e88 VS |
214 | void StringTestCase::Extraction() |
215 | { | |
9a83f860 | 216 | wxString s(wxT("Hello, world!")); |
1cd53e88 | 217 | |
9a83f860 VZ |
218 | CPPUNIT_ASSERT( wxStrcmp( s.c_str() , wxT("Hello, world!") ) == 0 ); |
219 | CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , wxT("Hello") ) == 0 ); | |
220 | CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , wxT("world!") ) == 0 ); | |
221 | CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 ); | |
222 | CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , wxT("lo, world!") ) == 0 ); | |
223 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , wxT("lo, w") ) == 0 ); | |
224 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , wxT("lo, world!") ) == 0 ); | |
1cd53e88 | 225 | |
478cbb08 VS |
226 | #if wxUSE_UNICODE |
227 | static const char *germanUTF8 = "Oberfl\303\244che"; | |
228 | wxString strUnicode(wxString::FromUTF8(germanUTF8)); | |
229 | ||
230 | CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode ); | |
231 | CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" ); | |
232 | #endif // wxUSE_UNICODE | |
233 | ||
1cd53e88 VS |
234 | wxString rest; |
235 | ||
3affcd07 VZ |
236 | #define TEST_STARTS_WITH(prefix, correct_rest, result) \ |
237 | CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \ | |
238 | if ( result ) \ | |
1de532f5 | 239 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) |
1cd53e88 | 240 | |
9a83f860 VZ |
241 | TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true ); |
242 | TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true ); | |
243 | TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true ); | |
244 | TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false ); | |
245 | TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true ); | |
246 | TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false ); | |
247 | TEST_STARTS_WITH( wxT("Hi"), wxT(""), false ); | |
1cd53e88 VS |
248 | |
249 | #undef TEST_STARTS_WITH | |
3affcd07 | 250 | |
c8f313b2 VZ |
251 | rest = "Hello world"; |
252 | CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) ); | |
1de532f5 | 253 | CPPUNIT_ASSERT_EQUAL("world", rest); |
c8f313b2 | 254 | |
3affcd07 VZ |
255 | #define TEST_ENDS_WITH(suffix, correct_rest, result) \ |
256 | CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \ | |
257 | if ( result ) \ | |
1de532f5 | 258 | CPPUNIT_ASSERT_EQUAL(correct_rest, rest) |
3affcd07 | 259 | |
9a83f860 VZ |
260 | TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true ); |
261 | TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true ); | |
262 | TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true ); | |
263 | TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true ); | |
264 | TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true ); | |
265 | TEST_ENDS_WITH( wxT("very long string"), wxT(""), false ); | |
266 | TEST_ENDS_WITH( wxT("?"), wxT(""), false ); | |
267 | TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false ); | |
268 | TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false ); | |
3affcd07 VZ |
269 | |
270 | #undef TEST_ENDS_WITH | |
1cd53e88 VS |
271 | } |
272 | ||
e6a99197 WS |
273 | void StringTestCase::Trim() |
274 | { | |
275 | #define TEST_TRIM( str , dir , result ) \ | |
276 | CPPUNIT_ASSERT( wxString(str).Trim(dir) == result ) | |
277 | ||
9a83f860 VZ |
278 | TEST_TRIM( wxT(" Test "), true, wxT(" Test") ); |
279 | TEST_TRIM( wxT(" "), true, wxT("") ); | |
280 | TEST_TRIM( wxT(" "), true, wxT("") ); | |
281 | TEST_TRIM( wxT(""), true, wxT("") ); | |
e6a99197 | 282 | |
9a83f860 VZ |
283 | TEST_TRIM( wxT(" Test "), false, wxT("Test ") ); |
284 | TEST_TRIM( wxT(" "), false, wxT("") ); | |
285 | TEST_TRIM( wxT(" "), false, wxT("") ); | |
286 | TEST_TRIM( wxT(""), false, wxT("") ); | |
e6a99197 WS |
287 | |
288 | #undef TEST_TRIM | |
289 | } | |
290 | ||
1cd53e88 VS |
291 | void StringTestCase::Find() |
292 | { | |
293 | #define TEST_FIND( str , start , result ) \ | |
9a83f860 | 294 | CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result ); |
1cd53e88 | 295 | |
9a83f860 VZ |
296 | TEST_FIND( wxT("Well, hello world"), 0, 1 ); |
297 | TEST_FIND( wxT("Well, hello world"), 6, 7 ); | |
298 | TEST_FIND( wxT("Well, hello world"), 9, wxString::npos ); | |
1cd53e88 VS |
299 | |
300 | #undef TEST_FIND | |
301 | } | |
302 | ||
1cd53e88 VS |
303 | void StringTestCase::Replace() |
304 | { | |
305 | #define TEST_REPLACE( original , pos , len , replacement , result ) \ | |
306 | { \ | |
307 | wxString s = original; \ | |
308 | s.replace( pos , len , replacement ); \ | |
1de532f5 | 309 | CPPUNIT_ASSERT_EQUAL( result, s ); \ |
1cd53e88 VS |
310 | } |
311 | ||
9a83f860 VZ |
312 | TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"), wxT("012-BWORD-XYZ") ); |
313 | TEST_REPLACE( wxT("increase"), 0, 2, wxT("de"), wxT("decrease") ); | |
314 | TEST_REPLACE( wxT("wxWindow"), 8, 0, wxT("s"), wxT("wxWindows") ); | |
315 | TEST_REPLACE( wxT("foobar"), 3, 0, wxT("-"), wxT("foo-bar") ); | |
316 | TEST_REPLACE( wxT("barfoo"), 0, 6, wxT("foobar"), wxT("foobar") ); | |
1cd53e88 | 317 | |
1de532f5 | 318 | |
7634e443 RN |
319 | #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \ |
320 | { \ | |
321 | wxString s(o,olen); \ | |
322 | s.replace( pos , len , replacement ); \ | |
072682ce | 323 | CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ |
7634e443 | 324 | } |
1de532f5 | 325 | |
9a83f860 VZ |
326 | TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"), |
327 | wxT("null\0dhar"), 9 ); | |
7634e443 RN |
328 | |
329 | #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \ | |
330 | { \ | |
331 | wxString s(o,olen); \ | |
332 | s.Replace( olds, news, all ); \ | |
072682ce | 333 | CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ |
7634e443 | 334 | } |
1de532f5 | 335 | |
9a83f860 VZ |
336 | TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true, |
337 | wxT("null\0dehar"), 10 ); | |
7634e443 | 338 | |
9a83f860 VZ |
339 | TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true, |
340 | wxT("null\0char"), 9 ); | |
4629f07e | 341 | |
817d0578 | 342 | TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 ); |
072682ce VZ |
343 | TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 ); |
344 | TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 ); | |
345 | TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 ); | |
346 | TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 ); | |
817d0578 | 347 | |
7634e443 RN |
348 | #undef TEST_WXREPLACE |
349 | #undef TEST_NULLCHARREPLACE | |
1cd53e88 VS |
350 | #undef TEST_REPLACE |
351 | } | |
352 | ||
353 | void StringTestCase::Match() | |
354 | { | |
355 | #define TEST_MATCH( s1 , s2 , result ) \ | |
356 | CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result ) | |
357 | ||
9a83f860 VZ |
358 | TEST_MATCH( wxT("foobar"), wxT("foo*"), true ); |
359 | TEST_MATCH( wxT("foobar"), wxT("*oo*"), true ); | |
360 | TEST_MATCH( wxT("foobar"), wxT("*bar"), true ); | |
361 | TEST_MATCH( wxT("foobar"), wxT("??????"), true ); | |
362 | TEST_MATCH( wxT("foobar"), wxT("f??b*"), true ); | |
363 | TEST_MATCH( wxT("foobar"), wxT("f?b*"), false ); | |
364 | TEST_MATCH( wxT("foobar"), wxT("*goo*"), false ); | |
365 | TEST_MATCH( wxT("foobar"), wxT("*foo"), false ); | |
366 | TEST_MATCH( wxT("foobarfoo"), wxT("*foo"), true ); | |
367 | TEST_MATCH( wxT(""), wxT("*"), true ); | |
368 | TEST_MATCH( wxT(""), wxT("?"), false ); | |
1cd53e88 VS |
369 | |
370 | #undef TEST_MATCH | |
371 | } | |
372 | ||
bd7f096d VS |
373 | |
374 | void StringTestCase::CaseChanges() | |
375 | { | |
9a83f860 | 376 | wxString s1(wxT("Hello!")); |
bd7f096d VS |
377 | wxString s1u(s1); |
378 | wxString s1l(s1); | |
379 | s1u.MakeUpper(); | |
380 | s1l.MakeLower(); | |
0c7db140 | 381 | |
9a83f860 VZ |
382 | CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u ); |
383 | CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l ); | |
0c7db140 | 384 | |
bd7f096d VS |
385 | wxString s2u, s2l; |
386 | s2u.MakeUpper(); | |
387 | s2l.MakeLower(); | |
388 | ||
0c7db140 VZ |
389 | CPPUNIT_ASSERT_EQUAL( "", s2u ); |
390 | CPPUNIT_ASSERT_EQUAL( "", s2l ); | |
391 | ||
392 | ||
393 | wxString s3("good bye"); | |
394 | CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() ); | |
395 | s3.MakeCapitalized(); | |
396 | CPPUNIT_ASSERT_EQUAL( "Good bye", s3 ); | |
397 | ||
398 | CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() ); | |
399 | ||
400 | CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() ); | |
bd7f096d | 401 | } |
dcb68102 RN |
402 | |
403 | void StringTestCase::Compare() | |
404 | { | |
405 | wxString s1 = wxT("AHH"); | |
406 | wxString eq = wxT("AHH"); | |
407 | wxString neq1 = wxT("HAH"); | |
408 | wxString neq2 = wxT("AH"); | |
409 | wxString neq3 = wxT("AHHH"); | |
410 | wxString neq4 = wxT("AhH"); | |
0c924034 | 411 | |
dcb68102 RN |
412 | CPPUNIT_ASSERT( s1 == eq ); |
413 | CPPUNIT_ASSERT( s1 != neq1 ); | |
414 | CPPUNIT_ASSERT( s1 != neq2 ); | |
415 | CPPUNIT_ASSERT( s1 != neq3 ); | |
416 | CPPUNIT_ASSERT( s1 != neq4 ); | |
417 | ||
d7330233 VS |
418 | CPPUNIT_ASSERT( s1 == wxT("AHH") ); |
419 | CPPUNIT_ASSERT( s1 != wxT("no") ); | |
420 | CPPUNIT_ASSERT( s1 < wxT("AZ") ); | |
421 | CPPUNIT_ASSERT( s1 <= wxT("AZ") ); | |
422 | CPPUNIT_ASSERT( s1 <= wxT("AHH") ); | |
423 | CPPUNIT_ASSERT( s1 > wxT("AA") ); | |
424 | CPPUNIT_ASSERT( s1 >= wxT("AA") ); | |
425 | CPPUNIT_ASSERT( s1 >= wxT("AHH") ); | |
426 | ||
427 | // test comparison with C strings in Unicode build (must work in ANSI as | |
428 | // well, of course): | |
429 | CPPUNIT_ASSERT( s1 == "AHH" ); | |
430 | CPPUNIT_ASSERT( s1 != "no" ); | |
431 | CPPUNIT_ASSERT( s1 < "AZ" ); | |
432 | CPPUNIT_ASSERT( s1 <= "AZ" ); | |
433 | CPPUNIT_ASSERT( s1 <= "AHH" ); | |
434 | CPPUNIT_ASSERT( s1 > "AA" ); | |
435 | CPPUNIT_ASSERT( s1 >= "AA" ); | |
436 | CPPUNIT_ASSERT( s1 >= "AHH" ); | |
437 | ||
dcb68102 RN |
438 | // wxString _s1 = wxT("A\0HH"); |
439 | // wxString _eq = wxT("A\0HH"); | |
440 | // wxString _neq1 = wxT("H\0AH"); | |
441 | // wxString _neq2 = wxT("A\0H"); | |
442 | // wxString _neq3 = wxT("A\0HHH"); | |
443 | // wxString _neq4 = wxT("A\0hH"); | |
444 | s1.insert(1,1,'\0'); | |
445 | eq.insert(1,1,'\0'); | |
446 | neq1.insert(1,1,'\0'); | |
447 | neq2.insert(1,1,'\0'); | |
448 | neq3.insert(1,1,'\0'); | |
449 | neq4.insert(1,1,'\0'); | |
0c924034 | 450 | |
dcb68102 RN |
451 | CPPUNIT_ASSERT( s1 == eq ); |
452 | CPPUNIT_ASSERT( s1 != neq1 ); | |
453 | CPPUNIT_ASSERT( s1 != neq2 ); | |
454 | CPPUNIT_ASSERT( s1 != neq3 ); | |
455 | CPPUNIT_ASSERT( s1 != neq4 ); | |
456 | } | |
457 | ||
458 | void StringTestCase::CompareNoCase() | |
459 | { | |
460 | wxString s1 = wxT("AHH"); | |
461 | wxString eq = wxT("AHH"); | |
462 | wxString eq2 = wxT("AhH"); | |
463 | wxString eq3 = wxT("ahh"); | |
464 | wxString neq = wxT("HAH"); | |
465 | wxString neq2 = wxT("AH"); | |
466 | wxString neq3 = wxT("AHHH"); | |
0c924034 | 467 | |
dcb68102 RN |
468 | #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0) |
469 | #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0) | |
470 | ||
471 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
472 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
473 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
474 | ||
475 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
476 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
477 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
478 | ||
479 | ||
480 | // wxString _s1 = wxT("A\0HH"); | |
481 | // wxString _eq = wxT("A\0HH"); | |
482 | // wxString _eq2 = wxT("A\0hH"); | |
483 | // wxString _eq3 = wxT("a\0hh"); | |
484 | // wxString _neq = wxT("H\0AH"); | |
485 | // wxString _neq2 = wxT("A\0H"); | |
486 | // wxString _neq3 = wxT("A\0HHH"); | |
0c924034 | 487 | |
dcb68102 RN |
488 | s1.insert(1,1,'\0'); |
489 | eq.insert(1,1,'\0'); | |
490 | eq2.insert(1,1,'\0'); | |
491 | eq3.insert(1,1,'\0'); | |
492 | neq.insert(1,1,'\0'); | |
493 | neq2.insert(1,1,'\0'); | |
494 | neq3.insert(1,1,'\0'); | |
495 | ||
496 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
497 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
498 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
499 | ||
500 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
501 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
502 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
0c924034 WS |
503 | } |
504 | ||
c4f35063 VZ |
505 | void StringTestCase::Contains() |
506 | { | |
507 | static const struct ContainsData | |
508 | { | |
509 | const wxChar *hay; | |
510 | const wxChar *needle; | |
511 | bool contains; | |
512 | } containsData[] = | |
513 | { | |
9a83f860 VZ |
514 | { wxT(""), wxT(""), true }, |
515 | { wxT(""), wxT("foo"), false }, | |
516 | { wxT("foo"), wxT(""), true }, | |
517 | { wxT("foo"), wxT("f"), true }, | |
518 | { wxT("foo"), wxT("o"), true }, | |
519 | { wxT("foo"), wxT("oo"), true }, | |
520 | { wxT("foo"), wxT("ooo"), false }, | |
521 | { wxT("foo"), wxT("oooo"), false }, | |
522 | { wxT("foo"), wxT("fooo"), false }, | |
c4f35063 VZ |
523 | }; |
524 | ||
525 | for ( size_t n = 0; n < WXSIZEOF(containsData); n++ ) | |
526 | { | |
527 | const ContainsData& cd = containsData[n]; | |
528 | CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) ); | |
529 | } | |
530 | } | |
531 | ||
d6718dd1 VZ |
532 | // flags used in ToLongData.flags |
533 | enum | |
4f7ee81a | 534 | { |
d6718dd1 VZ |
535 | Number_Ok = 0, |
536 | Number_Invalid = 1, | |
537 | Number_Unsigned = 2, // if not specified, works for signed conversion | |
538 | Number_Signed = 4, // if not specified, works for unsigned | |
539 | Number_LongLong = 8, // only for long long tests | |
93a800a9 | 540 | Number_Long = 16 // only for long tests |
d6718dd1 VZ |
541 | }; |
542 | ||
543 | static const struct ToLongData | |
544 | { | |
545 | const wxChar *str; | |
d6718dd1 | 546 | #ifdef wxLongLong_t |
efc57671 VZ |
547 | wxLongLong_t value; |
548 | #else | |
549 | long value; | |
d6718dd1 | 550 | #endif // wxLongLong_t |
d6718dd1 VZ |
551 | int flags; |
552 | ||
efc57671 VZ |
553 | long LValue() const { return value; } |
554 | unsigned long ULValue() const { return value; } | |
555 | #ifdef wxLongLong_t | |
556 | wxLongLong_t LLValue() const { return value; } | |
557 | wxULongLong_t ULLValue() const { return (wxULongLong_t)value; } | |
558 | #endif // wxLongLong_t | |
559 | ||
d6718dd1 VZ |
560 | bool IsOk() const { return !(flags & Number_Invalid); } |
561 | } longData[] = | |
562 | { | |
9a83f860 VZ |
563 | { wxT("1"), 1, Number_Ok }, |
564 | { wxT("0"), 0, Number_Ok }, | |
565 | { wxT("a"), 0, Number_Invalid }, | |
566 | { wxT("12345"), 12345, Number_Ok }, | |
567 | { wxT("--1"), 0, Number_Invalid }, | |
d6718dd1 | 568 | |
9a83f860 | 569 | { wxT("-1"), -1, Number_Signed | Number_Long }, |
d6718dd1 | 570 | // this is surprizing but consistent with strtoul() behaviour |
9a83f860 | 571 | { wxT("-1"), ULONG_MAX, Number_Unsigned | Number_Long }, |
d6718dd1 VZ |
572 | |
573 | // this must overflow, even with 64 bit long | |
9a83f860 | 574 | { wxT("922337203685477580711"), 0, Number_Invalid }, |
d6718dd1 VZ |
575 | |
576 | #ifdef wxLongLong_t | |
9a83f860 VZ |
577 | { wxT("2147483648"), wxLL(2147483648), Number_LongLong }, |
578 | { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed }, | |
579 | { wxT("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong | | |
d6718dd1 VZ |
580 | Number_Unsigned }, |
581 | #endif // wxLongLong_t | |
582 | }; | |
583 | ||
584 | void StringTestCase::ToLong() | |
585 | { | |
c4f35063 VZ |
586 | long l; |
587 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a VZ |
588 | { |
589 | const ToLongData& ld = longData[n]; | |
d6718dd1 VZ |
590 | |
591 | if ( ld.flags & (Number_LongLong | Number_Unsigned) ) | |
592 | continue; | |
529e491c FM |
593 | |
594 | // NOTE: unless you're using some exotic locale, ToCLong and ToLong | |
595 | // should behave the same for our test data set: | |
596 | ||
597 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCLong(&l) ); | |
598 | if ( ld.IsOk() ) | |
599 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); | |
d6718dd1 VZ |
600 | |
601 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) ); | |
602 | if ( ld.IsOk() ) | |
efc57671 | 603 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); |
4f7ee81a | 604 | } |
69d31e31 VZ |
605 | |
606 | // special case: check that the output is not modified if the parsing | |
607 | // failed completely | |
608 | l = 17; | |
609 | CPPUNIT_ASSERT( !wxString("foo").ToLong(&l) ); | |
610 | CPPUNIT_ASSERT_EQUAL( 17, l ); | |
611 | ||
612 | // also check that it is modified if we did parse something successfully in | |
613 | // the beginning of the string | |
614 | CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l) ); | |
615 | CPPUNIT_ASSERT_EQUAL( 9, l ); | |
4f7ee81a VZ |
616 | } |
617 | ||
618 | void StringTestCase::ToULong() | |
619 | { | |
620 | unsigned long ul; | |
d6718dd1 | 621 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) |
4f7ee81a | 622 | { |
d6718dd1 VZ |
623 | const ToLongData& ld = longData[n]; |
624 | ||
625 | if ( ld.flags & (Number_LongLong | Number_Signed) ) | |
626 | continue; | |
627 | ||
529e491c FM |
628 | // NOTE: unless you're using some exotic locale, ToCLong and ToLong |
629 | // should behave the same for our test data set: | |
630 | ||
631 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCULong(&ul) ); | |
632 | if ( ld.IsOk() ) | |
633 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); | |
634 | ||
d6718dd1 VZ |
635 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) ); |
636 | if ( ld.IsOk() ) | |
efc57671 | 637 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); |
d6718dd1 VZ |
638 | } |
639 | } | |
640 | ||
641 | #ifdef wxLongLong_t | |
642 | ||
643 | void StringTestCase::ToLongLong() | |
644 | { | |
645 | wxLongLong_t l; | |
646 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 647 | { |
d6718dd1 | 648 | const ToLongData& ld = longData[n]; |
4f7ee81a | 649 | |
d6718dd1 VZ |
650 | if ( ld.flags & (Number_Long | Number_Unsigned) ) |
651 | continue; | |
652 | ||
653 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) ); | |
654 | if ( ld.IsOk() ) | |
efc57671 | 655 | CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l ); |
d6718dd1 VZ |
656 | } |
657 | } | |
658 | ||
659 | void StringTestCase::ToULongLong() | |
660 | { | |
661 | wxULongLong_t ul; | |
662 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 663 | { |
d6718dd1 VZ |
664 | const ToLongData& ld = longData[n]; |
665 | ||
666 | if ( ld.flags & (Number_Long | Number_Signed) ) | |
667 | continue; | |
668 | ||
669 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) ); | |
670 | if ( ld.IsOk() ) | |
efc57671 | 671 | CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul ); |
4f7ee81a VZ |
672 | } |
673 | } | |
674 | ||
d6718dd1 VZ |
675 | #endif // wxLongLong_t |
676 | ||
4f7ee81a VZ |
677 | void StringTestCase::ToDouble() |
678 | { | |
679 | double d; | |
680 | static const struct ToDoubleData | |
681 | { | |
682 | const wxChar *str; | |
683 | double value; | |
684 | bool ok; | |
685 | } doubleData[] = | |
686 | { | |
9a83f860 VZ |
687 | { wxT("1"), 1, true }, |
688 | { wxT("1.23"), 1.23, true }, | |
689 | { wxT(".1"), .1, true }, | |
690 | { wxT("1."), 1, true }, | |
691 | { wxT("1.."), 0, false }, | |
692 | { wxT("0"), 0, true }, | |
693 | { wxT("a"), 0, false }, | |
694 | { wxT("12345"), 12345, true }, | |
695 | { wxT("-1"), -1, true }, | |
696 | { wxT("--1"), 0, false }, | |
697 | { wxT("-3E-5"), -3E-5, true }, | |
698 | { wxT("-3E-abcde5"), 0, false }, | |
4f7ee81a VZ |
699 | }; |
700 | ||
529e491c | 701 | // test ToCDouble() first: |
4f7ee81a VZ |
702 | |
703 | size_t n; | |
704 | for ( n = 0; n < WXSIZEOF(doubleData); n++ ) | |
705 | { | |
706 | const ToDoubleData& ld = doubleData[n]; | |
529e491c FM |
707 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToCDouble(&d) ); |
708 | if ( ld.ok ) | |
709 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
710 | } | |
711 | ||
712 | ||
713 | // test ToDouble() now: | |
714 | // NOTE: for the test to be reliable, we need to set the locale explicitely | |
715 | // so that we know the decimal point character to use | |
716 | ||
717 | if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH)) | |
718 | return; // you should have french support installed to continue this test! | |
719 | ||
720 | wxLocale *locale = new wxLocale; | |
721 | ||
722 | // don't load default catalog, it may be unavailable: | |
723 | CPPUNIT_ASSERT( locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING) ); | |
724 | ||
725 | static const struct ToDoubleData doubleData2[] = | |
726 | { | |
9a83f860 VZ |
727 | { wxT("1"), 1, true }, |
728 | { wxT("1,23"), 1.23, true }, | |
729 | { wxT(",1"), .1, true }, | |
730 | { wxT("1,"), 1, true }, | |
731 | { wxT("1,,"), 0, false }, | |
732 | { wxT("0"), 0, true }, | |
733 | { wxT("a"), 0, false }, | |
734 | { wxT("12345"), 12345, true }, | |
735 | { wxT("-1"), -1, true }, | |
736 | { wxT("--1"), 0, false }, | |
737 | { wxT("-3E-5"), -3E-5, true }, | |
738 | { wxT("-3E-abcde5"), 0, false }, | |
529e491c FM |
739 | }; |
740 | ||
741 | for ( n = 0; n < WXSIZEOF(doubleData2); n++ ) | |
742 | { | |
743 | const ToDoubleData& ld = doubleData2[n]; | |
4f7ee81a VZ |
744 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); |
745 | if ( ld.ok ) | |
746 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
747 | } | |
529e491c FM |
748 | |
749 | delete locale; | |
4f7ee81a | 750 | } |
db419f1f | 751 | |
062dc5fc | 752 | void StringTestCase::StringBuf() |
db419f1f | 753 | { |
062dc5fc | 754 | // check that buffer can be used to write into the string |
db419f1f | 755 | wxString s; |
9a83f860 | 756 | wxStrcpy(wxStringBuffer(s, 10), wxT("foo")); |
db419f1f | 757 | |
1de532f5 | 758 | CPPUNIT_ASSERT_EQUAL(3, s.length()); |
9a83f860 VZ |
759 | CPPUNIT_ASSERT(wxT('f') == s[0u]); |
760 | CPPUNIT_ASSERT(wxT('o') == s[1]); | |
761 | CPPUNIT_ASSERT(wxT('o') == s[2]); | |
db419f1f | 762 | |
062dc5fc VZ |
763 | { |
764 | // also check that the buffer initially contains the original string | |
765 | // contents | |
766 | wxStringBuffer buf(s, 10); | |
9a83f860 VZ |
767 | CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] ); |
768 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] ); | |
769 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] ); | |
770 | CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] ); | |
062dc5fc | 771 | } |
d8a4b666 | 772 | |
db419f1f | 773 | { |
d8a4b666 | 774 | wxStringBufferLength buf(s, 10); |
9a83f860 VZ |
775 | CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] ); |
776 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] ); | |
777 | CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] ); | |
778 | CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] ); | |
062dc5fc VZ |
779 | |
780 | // and check that it can be used to write only the specified number of | |
781 | // characters to the string | |
9a83f860 | 782 | wxStrcpy(buf, wxT("barrbaz")); |
d8a4b666 VS |
783 | buf.SetLength(4); |
784 | } | |
db419f1f | 785 | |
1de532f5 | 786 | CPPUNIT_ASSERT_EQUAL(4, s.length()); |
9a83f860 VZ |
787 | CPPUNIT_ASSERT(wxT('b') == s[0u]); |
788 | CPPUNIT_ASSERT(wxT('a') == s[1]); | |
789 | CPPUNIT_ASSERT(wxT('r') == s[2]); | |
790 | CPPUNIT_ASSERT(wxT('r') == s[3]); | |
db419f1f | 791 | |
062dc5fc VZ |
792 | // check that creating buffer of length smaller than string works, i.e. at |
793 | // least doesn't crash (it would if we naively copied the entire original | |
794 | // string contents in the buffer) | |
795 | *wxStringBuffer(s, 1) = '!'; | |
db419f1f VZ |
796 | } |
797 | ||
628f87da VS |
798 | void StringTestCase::UTF8Buf() |
799 | { | |
800 | #if wxUSE_UNICODE | |
801 | // "czech" in Czech ("cestina"): | |
802 | static const char *textUTF8 = "\304\215e\305\241tina"; | |
803 | static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0}; | |
804 | ||
805 | wxString s; | |
806 | wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8); | |
807 | CPPUNIT_ASSERT(s == textUTF16); | |
808 | ||
809 | { | |
810 | wxUTF8StringBufferLength buf(s, 20); | |
811 | wxStrcpy(buf, textUTF8); | |
812 | buf.SetLength(5); | |
813 | } | |
814 | CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3)); | |
815 | #endif // wxUSE_UNICODE | |
816 | } | |
817 | ||
4ca056ea VS |
818 | |
819 | ||
ef0f1387 | 820 | void StringTestCase::CStrDataTernaryOperator() |
4ca056ea | 821 | { |
ef0f1387 VS |
822 | DoCStrDataTernaryOperator(true); |
823 | DoCStrDataTernaryOperator(false); | |
4ca056ea VS |
824 | } |
825 | ||
826 | template<typename T> bool CheckStr(const wxString& expected, T s) | |
827 | { | |
828 | return expected == wxString(s); | |
829 | } | |
830 | ||
ef0f1387 | 831 | void StringTestCase::DoCStrDataTernaryOperator(bool cond) |
4ca056ea VS |
832 | { |
833 | // test compilation of wxCStrData when used with operator?: (the asserts | |
834 | // are not very important, we're testing if the code compiles at all): | |
835 | ||
836 | wxString s("foo"); | |
4ca056ea | 837 | |
abc505b4 | 838 | const wchar_t *wcStr = L"foo"; |
4ca056ea | 839 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) ); |
9ffb659a | 840 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) ); |
4ca056ea | 841 | CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) ); |
9ffb659a | 842 | CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) ); |
11aac4ba | 843 | |
9b59b90c | 844 | const char *mbStr = "foo"; |
4ca056ea VS |
845 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) ); |
846 | CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) ); | |
847 | CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) ); | |
848 | CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) ); | |
92258cc1 VS |
849 | |
850 | wxString empty(""); | |
851 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) ); | |
852 | CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) ); | |
4ca056ea | 853 | } |
ef0f1387 | 854 | |
3a69bca1 VS |
855 | void StringTestCase::CStrDataOperators() |
856 | { | |
857 | wxString s("hello"); | |
858 | ||
859 | CPPUNIT_ASSERT( s.c_str()[0] == 'h' ); | |
860 | CPPUNIT_ASSERT( s.c_str()[1] == 'e' ); | |
fdfc5e18 FM |
861 | |
862 | // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access | |
863 | // the final character results in an assert failure (with debug CRT) | |
864 | //CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); | |
3a69bca1 VS |
865 | |
866 | CPPUNIT_ASSERT( *s.c_str() == 'h' ); | |
867 | CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' ); | |
fdfc5e18 | 868 | //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); |
3a69bca1 VS |
869 | } |
870 | ||
ef0f1387 VS |
871 | bool CheckStrChar(const wxString& expected, char *s) |
872 | { return CheckStr(expected, s); } | |
873 | bool CheckStrWChar(const wxString& expected, wchar_t *s) | |
874 | { return CheckStr(expected, s); } | |
875 | bool CheckStrConstChar(const wxString& expected, const char *s) | |
876 | { return CheckStr(expected, s); } | |
877 | bool CheckStrConstWChar(const wxString& expected, const wchar_t *s) | |
878 | { return CheckStr(expected, s); } | |
879 | ||
880 | void StringTestCase::CStrDataImplicitConversion() | |
881 | { | |
882 | wxString s("foo"); | |
883 | ||
ef0f1387 | 884 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) ); |
ef0f1387 | 885 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) ); |
7978bc72 VS |
886 | |
887 | // implicit conversion of wxString is not available in STL build | |
888 | #if !wxUSE_STL | |
889 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s) ); | |
ef0f1387 | 890 | CPPUNIT_ASSERT( CheckStrConstChar(s, s) ); |
7978bc72 | 891 | #endif |
ef0f1387 VS |
892 | } |
893 | ||
894 | void StringTestCase::ExplicitConversion() | |
895 | { | |
896 | wxString s("foo"); | |
897 | ||
898 | CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) ); | |
899 | CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) ); | |
900 | CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) ); | |
901 | ||
902 | CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) ); | |
903 | CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) ); | |
904 | CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) ); | |
905 | } | |
6bd4f281 VS |
906 | |
907 | void StringTestCase::IndexedAccess() | |
908 | { | |
909 | wxString s("bar"); | |
94bc35dc | 910 | CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); |
6bd4f281 VS |
911 | |
912 | // this tests for a possible bug in UTF-8 based wxString implementation: | |
913 | // the 3rd character of the underlying byte string is going to change, but | |
914 | // the 3rd character of wxString should remain the same | |
39e12b2d | 915 | s[0] = L'\xe9'; |
94bc35dc | 916 | CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); |
6bd4f281 VS |
917 | } |
918 | ||
c565abe1 VZ |
919 | void StringTestCase::BeforeAndAfter() |
920 | { | |
39e12b2d | 921 | const wxString s(L"letter=\xe9;\xe7a=l\xe0"); |
c565abe1 VZ |
922 | |
923 | CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); | |
924 | CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); | |
39e12b2d | 925 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';') ); |
c565abe1 | 926 | |
39e12b2d | 927 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=') ); |
c565abe1 | 928 | CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); |
39e12b2d | 929 | CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';') ); |
c565abe1 | 930 | |
39e12b2d | 931 | CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", s.AfterFirst('=') ); |
c565abe1 | 932 | CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') ); |
39e12b2d | 933 | CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterFirst(';') ); |
c565abe1 | 934 | |
39e12b2d | 935 | CPPUNIT_ASSERT_EQUAL( L"l\xe0", s.AfterLast('=') ); |
c565abe1 | 936 | CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') ); |
39e12b2d | 937 | CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') ); |
c565abe1 VZ |
938 | } |
939 | ||
de4983f3 VS |
940 | void StringTestCase::ScopedBuffers() |
941 | { | |
942 | // wxString relies on efficient buffers, verify they work as they should | |
943 | ||
944 | const char *literal = "Hello World!"; | |
945 | ||
946 | // non-owned buffer points to the string passed to it | |
947 | wxScopedCharBuffer sbuf = wxScopedCharBuffer::CreateNonOwned(literal); | |
948 | CPPUNIT_ASSERT( sbuf.data() == literal ); | |
949 | ||
950 | // a copy of scoped non-owned buffer still points to the same string | |
951 | wxScopedCharBuffer sbuf2(sbuf); | |
952 | CPPUNIT_ASSERT( sbuf.data() == sbuf2.data() ); | |
953 | ||
954 | // but assigning it to wxCharBuffer makes a full copy | |
955 | wxCharBuffer buf(sbuf); | |
956 | CPPUNIT_ASSERT( buf.data() != literal ); | |
390b8241 | 957 | CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); |
de4983f3 VS |
958 | |
959 | wxCharBuffer buf2 = sbuf; | |
960 | CPPUNIT_ASSERT( buf2.data() != literal ); | |
390b8241 | 961 | CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); |
de4983f3 | 962 | } |