]>
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 ); | |
39 | CPPUNIT_TEST( Extraction ); | |
e6a99197 | 40 | CPPUNIT_TEST( Trim ); |
1cd53e88 | 41 | CPPUNIT_TEST( Find ); |
1cd53e88 VS |
42 | CPPUNIT_TEST( Replace ); |
43 | CPPUNIT_TEST( Match ); | |
bd7f096d | 44 | CPPUNIT_TEST( CaseChanges ); |
dcb68102 RN |
45 | CPPUNIT_TEST( Compare ); |
46 | CPPUNIT_TEST( CompareNoCase ); | |
c4f35063 | 47 | CPPUNIT_TEST( Contains ); |
4f7ee81a VZ |
48 | CPPUNIT_TEST( ToLong ); |
49 | CPPUNIT_TEST( ToULong ); | |
d6718dd1 VZ |
50 | #ifdef wxLongLong_t |
51 | CPPUNIT_TEST( ToLongLong ); | |
52 | CPPUNIT_TEST( ToULongLong ); | |
53 | #endif // wxLongLong_t | |
4f7ee81a | 54 | CPPUNIT_TEST( ToDouble ); |
db419f1f | 55 | CPPUNIT_TEST( WriteBuf ); |
1cd53e88 VS |
56 | CPPUNIT_TEST_SUITE_END(); |
57 | ||
58 | void String(); | |
59 | void PChar(); | |
60 | void Format(); | |
61 | void Constructors(); | |
62 | void Extraction(); | |
e6a99197 | 63 | void Trim(); |
1cd53e88 | 64 | void Find(); |
1cd53e88 VS |
65 | void Replace(); |
66 | void Match(); | |
bd7f096d | 67 | void CaseChanges(); |
dcb68102 RN |
68 | void Compare(); |
69 | void CompareNoCase(); | |
c4f35063 | 70 | void Contains(); |
4f7ee81a VZ |
71 | void ToLong(); |
72 | void ToULong(); | |
d6718dd1 VZ |
73 | #ifdef wxLongLong_t |
74 | void ToLongLong(); | |
75 | void ToULongLong(); | |
76 | #endif // wxLongLong_t | |
4f7ee81a | 77 | void ToDouble(); |
db419f1f | 78 | void WriteBuf(); |
1cd53e88 VS |
79 | |
80 | DECLARE_NO_COPY_CLASS(StringTestCase) | |
81 | }; | |
82 | ||
83 | // register in the unnamed registry so that these tests are run by default | |
84 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
85 | ||
86 | // also include in it's own registry so that these tests can be run alone | |
87 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
88 | ||
89 | StringTestCase::StringTestCase() | |
90 | { | |
91 | } | |
92 | ||
93 | void StringTestCase::String() | |
94 | { | |
95 | wxString a, b, c; | |
96 | ||
97 | a.reserve (128); | |
98 | b.reserve (128); | |
99 | c.reserve (128); | |
100 | ||
101 | for (int i = 0; i < 2; ++i) | |
102 | { | |
103 | a = _T("Hello"); | |
104 | b = _T(" world"); | |
105 | c = _T("! How'ya doin'?"); | |
106 | a += b; | |
107 | a += c; | |
108 | c = _T("Hello world! What's up?"); | |
109 | CPPUNIT_ASSERT( c != a ); | |
110 | } | |
111 | } | |
112 | ||
113 | void StringTestCase::PChar() | |
114 | { | |
115 | wxChar a [128]; | |
116 | wxChar b [128]; | |
117 | wxChar c [128]; | |
118 | ||
119 | for (int i = 0; i < 2; ++i) | |
120 | { | |
121 | wxStrcpy (a, _T("Hello")); | |
122 | wxStrcpy (b, _T(" world")); | |
123 | wxStrcpy (c, _T("! How'ya doin'?")); | |
124 | wxStrcat (a, b); | |
125 | wxStrcat (a, c); | |
126 | wxStrcpy (c, _T("Hello world! What's up?")); | |
127 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); | |
128 | } | |
129 | } | |
130 | ||
131 | void StringTestCase::Format() | |
132 | { | |
133 | wxString s1,s2; | |
134 | s1.Printf(_T("%03d"), 18); | |
135 | CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) ); | |
136 | s2.Printf(_T("Number 18: %s\n"), s1.c_str()); | |
137 | CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) ); | |
b03cd7e6 VZ |
138 | |
139 | static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 }; | |
140 | for ( size_t n = 0; n < WXSIZEOF(lengths); n++ ) | |
141 | { | |
142 | const size_t len = lengths[n]; | |
143 | ||
144 | wxString s(_T('Z'), len); | |
145 | CPPUNIT_ASSERT_EQUAL( len, wxString::Format(_T("%s"), s.c_str()).length()); | |
146 | } | |
1cd53e88 VS |
147 | } |
148 | ||
149 | void StringTestCase::Constructors() | |
150 | { | |
151 | #define TEST_CTOR(args, res) \ | |
152 | { \ | |
153 | wxString s args ; \ | |
154 | CPPUNIT_ASSERT( s == res ); \ | |
155 | } | |
156 | ||
157 | TEST_CTOR((_T('Z'), 4), _T("ZZZZ")); | |
158 | TEST_CTOR((_T("Hello"), 4), _T("Hell")); | |
159 | TEST_CTOR((_T("Hello"), 5), _T("Hello")); | |
160 | ||
161 | static const wxChar *s = _T("?really!"); | |
162 | const wxChar *start = wxStrchr(s, _T('r')); | |
163 | const wxChar *end = wxStrchr(s, _T('!')); | |
164 | TEST_CTOR((start, end), _T("really")); | |
165 | } | |
166 | ||
265d5cce | 167 | |
1cd53e88 VS |
168 | void StringTestCase::Extraction() |
169 | { | |
170 | wxString s(_T("Hello, world!")); | |
171 | ||
172 | CPPUNIT_ASSERT( wxStrcmp( s.c_str() , _T("Hello, world!") ) == 0 ); | |
173 | CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , _T("Hello") ) == 0 ); | |
174 | CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , _T("world!") ) == 0 ); | |
175 | CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
176 | CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , _T("lo, world!") ) == 0 ); | |
177 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , _T("lo, w") ) == 0 ); | |
178 | CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , _T("lo, world!") ) == 0 ); | |
179 | ||
180 | wxString rest; | |
181 | ||
3affcd07 VZ |
182 | #define TEST_STARTS_WITH(prefix, correct_rest, result) \ |
183 | CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \ | |
184 | if ( result ) \ | |
185 | CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest) | |
1cd53e88 VS |
186 | |
187 | TEST_STARTS_WITH( _T("Hello"), _T(", world!"), true ); | |
188 | TEST_STARTS_WITH( _T("Hello, "), _T("world!"), true ); | |
189 | TEST_STARTS_WITH( _T("Hello, world!"), _T(""), true ); | |
190 | TEST_STARTS_WITH( _T("Hello, world!!!"), _T(""), false ); | |
191 | TEST_STARTS_WITH( _T(""), _T("Hello, world!"), true ); | |
192 | TEST_STARTS_WITH( _T("Goodbye"), _T(""), false ); | |
193 | TEST_STARTS_WITH( _T("Hi"), _T(""), false ); | |
194 | ||
195 | #undef TEST_STARTS_WITH | |
3affcd07 VZ |
196 | |
197 | #define TEST_ENDS_WITH(suffix, correct_rest, result) \ | |
198 | CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \ | |
199 | if ( result ) \ | |
200 | CPPUNIT_ASSERT_EQUAL(wxString(correct_rest), rest) | |
201 | ||
202 | TEST_ENDS_WITH( _T(""), _T("Hello, world!"), true ); | |
203 | TEST_ENDS_WITH( _T("!"), _T("Hello, world"), true ); | |
204 | TEST_ENDS_WITH( _T(", world!"), _T("Hello"), true ); | |
205 | TEST_ENDS_WITH( _T("ello, world!"), _T("H"), true ); | |
206 | TEST_ENDS_WITH( _T("Hello, world!"), _T(""), true ); | |
207 | TEST_ENDS_WITH( _T("very long string"), _T(""), false ); | |
208 | TEST_ENDS_WITH( _T("?"), _T(""), false ); | |
209 | TEST_ENDS_WITH( _T("Hello, world"), _T(""), false ); | |
210 | TEST_ENDS_WITH( _T("Gello, world!"), _T(""), false ); | |
211 | ||
212 | #undef TEST_ENDS_WITH | |
1cd53e88 VS |
213 | } |
214 | ||
e6a99197 WS |
215 | void StringTestCase::Trim() |
216 | { | |
217 | #define TEST_TRIM( str , dir , result ) \ | |
218 | CPPUNIT_ASSERT( wxString(str).Trim(dir) == result ) | |
219 | ||
220 | TEST_TRIM( _T(" Test "), true, _T(" Test") ); | |
221 | TEST_TRIM( _T(" "), true, _T("") ); | |
222 | TEST_TRIM( _T(" "), true, _T("") ); | |
223 | TEST_TRIM( _T(""), true, _T("") ); | |
224 | ||
225 | TEST_TRIM( _T(" Test "), false, _T("Test ") ); | |
226 | TEST_TRIM( _T(" "), false, _T("") ); | |
227 | TEST_TRIM( _T(" "), false, _T("") ); | |
228 | TEST_TRIM( _T(""), false, _T("") ); | |
229 | ||
230 | #undef TEST_TRIM | |
231 | } | |
232 | ||
1cd53e88 VS |
233 | void StringTestCase::Find() |
234 | { | |
235 | #define TEST_FIND( str , start , result ) \ | |
236 | CPPUNIT_ASSERT( wxString(str).find(_T("ell"), start) == result ); | |
237 | ||
238 | TEST_FIND( _T("Well, hello world"), 0, 1 ); | |
239 | TEST_FIND( _T("Well, hello world"), 6, 7 ); | |
240 | TEST_FIND( _T("Well, hello world"), 9, wxString::npos ); | |
241 | ||
242 | #undef TEST_FIND | |
243 | } | |
244 | ||
1cd53e88 VS |
245 | void StringTestCase::Replace() |
246 | { | |
247 | #define TEST_REPLACE( original , pos , len , replacement , result ) \ | |
248 | { \ | |
249 | wxString s = original; \ | |
250 | s.replace( pos , len , replacement ); \ | |
251 | CPPUNIT_ASSERT( s == result ); \ | |
252 | } | |
253 | ||
254 | TEST_REPLACE( _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") ); | |
255 | TEST_REPLACE( _T("increase"), 0, 2, _T("de"), _T("decrease") ); | |
256 | TEST_REPLACE( _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") ); | |
257 | TEST_REPLACE( _T("foobar"), 3, 0, _T("-"), _T("foo-bar") ); | |
258 | TEST_REPLACE( _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") ); | |
259 | ||
7634e443 RN |
260 | |
261 | #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \ | |
262 | { \ | |
263 | wxString s(o,olen); \ | |
264 | s.replace( pos , len , replacement ); \ | |
265 | CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ | |
266 | } | |
267 | ||
268 | TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"), | |
269 | _T("null\0dhar"), 9 ); | |
270 | ||
271 | #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \ | |
272 | { \ | |
273 | wxString s(o,olen); \ | |
274 | s.Replace( olds, news, all ); \ | |
275 | CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ | |
276 | } | |
277 | ||
2df0258e RN |
278 | TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true, |
279 | _T("null\0dehar"), 10 ); | |
7634e443 | 280 | |
4629f07e RN |
281 | TEST_WXREPLACE( _T("null\0dehar"), 10, _T("de"), _T("c"), true, |
282 | _T("null\0char"), 9 ); | |
283 | ||
7634e443 RN |
284 | #undef TEST_WXREPLACE |
285 | #undef TEST_NULLCHARREPLACE | |
1cd53e88 VS |
286 | #undef TEST_REPLACE |
287 | } | |
288 | ||
289 | void StringTestCase::Match() | |
290 | { | |
291 | #define TEST_MATCH( s1 , s2 , result ) \ | |
292 | CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result ) | |
293 | ||
294 | TEST_MATCH( _T("foobar"), _T("foo*"), true ); | |
295 | TEST_MATCH( _T("foobar"), _T("*oo*"), true ); | |
296 | TEST_MATCH( _T("foobar"), _T("*bar"), true ); | |
297 | TEST_MATCH( _T("foobar"), _T("??????"), true ); | |
298 | TEST_MATCH( _T("foobar"), _T("f??b*"), true ); | |
299 | TEST_MATCH( _T("foobar"), _T("f?b*"), false ); | |
300 | TEST_MATCH( _T("foobar"), _T("*goo*"), false ); | |
301 | TEST_MATCH( _T("foobar"), _T("*foo"), false ); | |
302 | TEST_MATCH( _T("foobarfoo"), _T("*foo"), true ); | |
303 | TEST_MATCH( _T(""), _T("*"), true ); | |
304 | TEST_MATCH( _T(""), _T("?"), false ); | |
305 | ||
306 | #undef TEST_MATCH | |
307 | } | |
308 | ||
bd7f096d VS |
309 | |
310 | void StringTestCase::CaseChanges() | |
311 | { | |
312 | wxString s1(_T("Hello!")); | |
313 | wxString s1u(s1); | |
314 | wxString s1l(s1); | |
315 | s1u.MakeUpper(); | |
316 | s1l.MakeLower(); | |
317 | wxString s2u, s2l; | |
318 | s2u.MakeUpper(); | |
319 | s2l.MakeLower(); | |
320 | ||
321 | CPPUNIT_ASSERT( s1u == _T("HELLO!") ); | |
322 | CPPUNIT_ASSERT( s1l == _T("hello!") ); | |
323 | CPPUNIT_ASSERT( s2u == wxEmptyString ); | |
324 | CPPUNIT_ASSERT( s2l == wxEmptyString ); | |
329b43f6 VZ |
325 | |
326 | #if !wxUSE_UNICODE | |
327 | wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */); | |
328 | if ( locRu.IsOk() ) | |
329 | { | |
330 | // try upper casing 8bit strings | |
b26ed94e MW |
331 | const wchar_t capital_ya[] = { 0x42f, 0 }, |
332 | small_ya[] = { 0x44f, 0 }; | |
333 | ||
334 | wxString sUpper(wxConvLibc.cWC2MB(capital_ya)), | |
335 | sLower(wxConvLibc.cWC2MB(small_ya)); | |
329b43f6 VZ |
336 | |
337 | CPPUNIT_ASSERT( sUpper.Lower() == sLower ); | |
338 | CPPUNIT_ASSERT( sLower.Upper() == sUpper ); | |
339 | } | |
340 | #endif // !wxUSE_UNICODE | |
bd7f096d | 341 | } |
dcb68102 RN |
342 | |
343 | void StringTestCase::Compare() | |
344 | { | |
345 | wxString s1 = wxT("AHH"); | |
346 | wxString eq = wxT("AHH"); | |
347 | wxString neq1 = wxT("HAH"); | |
348 | wxString neq2 = wxT("AH"); | |
349 | wxString neq3 = wxT("AHHH"); | |
350 | wxString neq4 = wxT("AhH"); | |
0c924034 | 351 | |
dcb68102 RN |
352 | CPPUNIT_ASSERT( s1 == eq ); |
353 | CPPUNIT_ASSERT( s1 != neq1 ); | |
354 | CPPUNIT_ASSERT( s1 != neq2 ); | |
355 | CPPUNIT_ASSERT( s1 != neq3 ); | |
356 | CPPUNIT_ASSERT( s1 != neq4 ); | |
357 | ||
358 | // wxString _s1 = wxT("A\0HH"); | |
359 | // wxString _eq = wxT("A\0HH"); | |
360 | // wxString _neq1 = wxT("H\0AH"); | |
361 | // wxString _neq2 = wxT("A\0H"); | |
362 | // wxString _neq3 = wxT("A\0HHH"); | |
363 | // wxString _neq4 = wxT("A\0hH"); | |
364 | s1.insert(1,1,'\0'); | |
365 | eq.insert(1,1,'\0'); | |
366 | neq1.insert(1,1,'\0'); | |
367 | neq2.insert(1,1,'\0'); | |
368 | neq3.insert(1,1,'\0'); | |
369 | neq4.insert(1,1,'\0'); | |
0c924034 | 370 | |
dcb68102 RN |
371 | CPPUNIT_ASSERT( s1 == eq ); |
372 | CPPUNIT_ASSERT( s1 != neq1 ); | |
373 | CPPUNIT_ASSERT( s1 != neq2 ); | |
374 | CPPUNIT_ASSERT( s1 != neq3 ); | |
375 | CPPUNIT_ASSERT( s1 != neq4 ); | |
376 | } | |
377 | ||
378 | void StringTestCase::CompareNoCase() | |
379 | { | |
380 | wxString s1 = wxT("AHH"); | |
381 | wxString eq = wxT("AHH"); | |
382 | wxString eq2 = wxT("AhH"); | |
383 | wxString eq3 = wxT("ahh"); | |
384 | wxString neq = wxT("HAH"); | |
385 | wxString neq2 = wxT("AH"); | |
386 | wxString neq3 = wxT("AHHH"); | |
0c924034 | 387 | |
dcb68102 RN |
388 | #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0) |
389 | #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0) | |
390 | ||
391 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
392 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
393 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
394 | ||
395 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
396 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
397 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
398 | ||
399 | ||
400 | // wxString _s1 = wxT("A\0HH"); | |
401 | // wxString _eq = wxT("A\0HH"); | |
402 | // wxString _eq2 = wxT("A\0hH"); | |
403 | // wxString _eq3 = wxT("a\0hh"); | |
404 | // wxString _neq = wxT("H\0AH"); | |
405 | // wxString _neq2 = wxT("A\0H"); | |
406 | // wxString _neq3 = wxT("A\0HHH"); | |
0c924034 | 407 | |
dcb68102 RN |
408 | s1.insert(1,1,'\0'); |
409 | eq.insert(1,1,'\0'); | |
410 | eq2.insert(1,1,'\0'); | |
411 | eq3.insert(1,1,'\0'); | |
412 | neq.insert(1,1,'\0'); | |
413 | neq2.insert(1,1,'\0'); | |
414 | neq3.insert(1,1,'\0'); | |
415 | ||
416 | CPPUNIT_CNCEQ_ASSERT( s1, eq ); | |
417 | CPPUNIT_CNCEQ_ASSERT( s1, eq2 ); | |
418 | CPPUNIT_CNCEQ_ASSERT( s1, eq3 ); | |
419 | ||
420 | CPPUNIT_CNCNEQ_ASSERT( s1, neq ); | |
421 | CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); | |
422 | CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); | |
0c924034 WS |
423 | } |
424 | ||
c4f35063 VZ |
425 | void StringTestCase::Contains() |
426 | { | |
427 | static const struct ContainsData | |
428 | { | |
429 | const wxChar *hay; | |
430 | const wxChar *needle; | |
431 | bool contains; | |
432 | } containsData[] = | |
433 | { | |
434 | { _T(""), _T(""), true }, | |
435 | { _T(""), _T("foo"), false }, | |
436 | { _T("foo"), _T(""), true }, | |
437 | { _T("foo"), _T("f"), true }, | |
438 | { _T("foo"), _T("o"), true }, | |
439 | { _T("foo"), _T("oo"), true }, | |
440 | { _T("foo"), _T("ooo"), false }, | |
441 | { _T("foo"), _T("oooo"), false }, | |
442 | { _T("foo"), _T("fooo"), false }, | |
443 | }; | |
444 | ||
445 | for ( size_t n = 0; n < WXSIZEOF(containsData); n++ ) | |
446 | { | |
447 | const ContainsData& cd = containsData[n]; | |
448 | CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) ); | |
449 | } | |
450 | } | |
451 | ||
d6718dd1 VZ |
452 | // flags used in ToLongData.flags |
453 | enum | |
4f7ee81a | 454 | { |
d6718dd1 VZ |
455 | Number_Ok = 0, |
456 | Number_Invalid = 1, | |
457 | Number_Unsigned = 2, // if not specified, works for signed conversion | |
458 | Number_Signed = 4, // if not specified, works for unsigned | |
459 | Number_LongLong = 8, // only for long long tests | |
460 | Number_Long = 16, // only for long tests | |
461 | }; | |
462 | ||
463 | static const struct ToLongData | |
464 | { | |
465 | const wxChar *str; | |
d6718dd1 | 466 | #ifdef wxLongLong_t |
efc57671 VZ |
467 | wxLongLong_t value; |
468 | #else | |
469 | long value; | |
d6718dd1 | 470 | #endif // wxLongLong_t |
d6718dd1 VZ |
471 | int flags; |
472 | ||
efc57671 VZ |
473 | long LValue() const { return value; } |
474 | unsigned long ULValue() const { return value; } | |
475 | #ifdef wxLongLong_t | |
476 | wxLongLong_t LLValue() const { return value; } | |
477 | wxULongLong_t ULLValue() const { return (wxULongLong_t)value; } | |
478 | #endif // wxLongLong_t | |
479 | ||
d6718dd1 VZ |
480 | bool IsOk() const { return !(flags & Number_Invalid); } |
481 | } longData[] = | |
482 | { | |
483 | { _T("1"), 1, Number_Ok }, | |
484 | { _T("0"), 0, Number_Ok }, | |
485 | { _T("a"), 0, Number_Invalid }, | |
486 | { _T("12345"), 12345, Number_Ok }, | |
487 | { _T("--1"), 0, Number_Invalid }, | |
488 | ||
489 | { _T("-1"), -1, Number_Signed | Number_Long }, | |
490 | // this is surprizing but consistent with strtoul() behaviour | |
491 | { _T("-1"), ULONG_MAX, Number_Unsigned | Number_Long }, | |
492 | ||
493 | // this must overflow, even with 64 bit long | |
494 | { _T("922337203685477580711"), 0, Number_Invalid }, | |
495 | ||
496 | #ifdef wxLongLong_t | |
497 | { _T("2147483648"), wxLL(2147483648), Number_LongLong }, | |
498 | { _T("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed }, | |
499 | { _T("9223372036854775808"), wxULL(9223372036854775808), Number_LongLong | | |
500 | Number_Unsigned }, | |
501 | #endif // wxLongLong_t | |
502 | }; | |
503 | ||
504 | void StringTestCase::ToLong() | |
505 | { | |
c4f35063 VZ |
506 | long l; |
507 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a VZ |
508 | { |
509 | const ToLongData& ld = longData[n]; | |
d6718dd1 VZ |
510 | |
511 | if ( ld.flags & (Number_LongLong | Number_Unsigned) ) | |
512 | continue; | |
513 | ||
514 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) ); | |
515 | if ( ld.IsOk() ) | |
efc57671 | 516 | CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); |
4f7ee81a VZ |
517 | } |
518 | } | |
519 | ||
520 | void StringTestCase::ToULong() | |
521 | { | |
522 | unsigned long ul; | |
d6718dd1 | 523 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) |
4f7ee81a | 524 | { |
d6718dd1 VZ |
525 | const ToLongData& ld = longData[n]; |
526 | ||
527 | if ( ld.flags & (Number_LongLong | Number_Signed) ) | |
528 | continue; | |
529 | ||
530 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) ); | |
531 | if ( ld.IsOk() ) | |
efc57671 | 532 | CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); |
d6718dd1 VZ |
533 | } |
534 | } | |
535 | ||
536 | #ifdef wxLongLong_t | |
537 | ||
538 | void StringTestCase::ToLongLong() | |
539 | { | |
540 | wxLongLong_t l; | |
541 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 542 | { |
d6718dd1 | 543 | const ToLongData& ld = longData[n]; |
4f7ee81a | 544 | |
d6718dd1 VZ |
545 | if ( ld.flags & (Number_Long | Number_Unsigned) ) |
546 | continue; | |
547 | ||
548 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLongLong(&l) ); | |
549 | if ( ld.IsOk() ) | |
efc57671 | 550 | CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l ); |
d6718dd1 VZ |
551 | } |
552 | } | |
553 | ||
554 | void StringTestCase::ToULongLong() | |
555 | { | |
556 | wxULongLong_t ul; | |
557 | for ( size_t n = 0; n < WXSIZEOF(longData); n++ ) | |
4f7ee81a | 558 | { |
d6718dd1 VZ |
559 | const ToLongData& ld = longData[n]; |
560 | ||
561 | if ( ld.flags & (Number_Long | Number_Signed) ) | |
562 | continue; | |
563 | ||
564 | CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULongLong(&ul) ); | |
565 | if ( ld.IsOk() ) | |
efc57671 | 566 | CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul ); |
4f7ee81a VZ |
567 | } |
568 | } | |
569 | ||
d6718dd1 VZ |
570 | #endif // wxLongLong_t |
571 | ||
4f7ee81a VZ |
572 | void StringTestCase::ToDouble() |
573 | { | |
574 | double d; | |
575 | static const struct ToDoubleData | |
576 | { | |
577 | const wxChar *str; | |
578 | double value; | |
579 | bool ok; | |
580 | } doubleData[] = | |
581 | { | |
582 | { _T("1"), 1, true }, | |
583 | { _T("1.23"), 1.23, true }, | |
584 | { _T(".1"), .1, true }, | |
585 | { _T("1."), 1, true }, | |
586 | { _T("1.."), 0, false }, | |
587 | { _T("0"), 0, true }, | |
588 | { _T("a"), 0, false }, | |
589 | { _T("12345"), 12345, true }, | |
590 | { _T("-1"), -1, true }, | |
591 | { _T("--1"), 0, false }, | |
592 | }; | |
593 | ||
594 | // we need to use decimal point, not comma or whatever is its value for the | |
595 | // current locale | |
596 | wxSetlocale(LC_ALL, _T("C")); | |
597 | ||
598 | size_t n; | |
599 | for ( n = 0; n < WXSIZEOF(doubleData); n++ ) | |
600 | { | |
601 | const ToDoubleData& ld = doubleData[n]; | |
602 | CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); | |
603 | if ( ld.ok ) | |
604 | CPPUNIT_ASSERT_EQUAL( ld.value, d ); | |
605 | } | |
606 | } | |
db419f1f VZ |
607 | |
608 | void StringTestCase::WriteBuf() | |
609 | { | |
610 | wxString s; | |
611 | wxStrcpy(wxStringBuffer(s, 10), _T("foo")); | |
612 | ||
613 | CPPUNIT_ASSERT_EQUAL(_T('f'), s[0u]); | |
614 | CPPUNIT_ASSERT_EQUAL(_T('o'), s[1]); | |
615 | CPPUNIT_ASSERT_EQUAL(_T('o'), s[2]); | |
616 | CPPUNIT_ASSERT_EQUAL(3u, s.length()); | |
617 | ||
618 | { | |
619 | wxChar *p = s.GetWriteBuf(10); | |
620 | wxStrcpy(p, _T("barrbaz")); | |
621 | s.UngetWriteBuf(4); | |
622 | ||
623 | CPPUNIT_ASSERT_EQUAL(_T('b'), s[0u]); | |
624 | CPPUNIT_ASSERT_EQUAL(_T('a'), s[1]); | |
625 | CPPUNIT_ASSERT_EQUAL(_T('r'), s[2]); | |
626 | CPPUNIT_ASSERT_EQUAL(_T('r'), s[3]); | |
627 | CPPUNIT_ASSERT_EQUAL(4u, s.length()); | |
628 | ||
629 | CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) ); | |
630 | } | |
631 | } | |
632 |