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