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