]>
Commit | Line | Data |
---|---|---|
37667812 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/console/console.cpp | |
3 | // Purpose: a sample console (as opposed to GUI) progam using wxWindows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 04.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
e87271f3 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
37667812 VZ |
20 | #include <stdio.h> |
21 | ||
22 | #include <wx/string.h> | |
bbfa0322 | 23 | #include <wx/file.h> |
37667812 | 24 | #include <wx/app.h> |
e87271f3 | 25 | |
d31b7b68 VZ |
26 | // without this pragma, the stupid compiler precompiles #defines below so that |
27 | // changing them doesn't "take place" later! | |
28 | #ifdef __VISUALC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
e87271f3 VZ |
32 | // ---------------------------------------------------------------------------- |
33 | // conditional compilation | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
d31b7b68 | 36 | // what to test (in alphabetic order)? |
378b05f7 | 37 | |
bbf8fc53 VZ |
38 | //#define TEST_ARRAYS |
39 | //#define TEST_CMDLINE | |
d31b7b68 | 40 | //#define TEST_DATETIME |
bbf8fc53 | 41 | //#define TEST_DIR |
d31b7b68 | 42 | //#define TEST_EXECUTE |
2c8e4738 VZ |
43 | //#define TEST_FILECONF |
44 | //#define TEST_HASH | |
bbf8fc53 VZ |
45 | //#define TEST_LOG |
46 | //#define TEST_LONGLONG | |
47 | //#define TEST_MIME | |
2c8e4738 | 48 | //#define TEST_SOCKETS |
ee6e1b1d | 49 | //#define TEST_STRINGS |
bbf8fc53 | 50 | //#define TEST_THREADS |
d31b7b68 | 51 | #define TEST_TIMER |
e87271f3 VZ |
52 | |
53 | // ============================================================================ | |
54 | // implementation | |
55 | // ============================================================================ | |
56 | ||
d34bce84 VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // wxCmdLineParser | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
d31b7b68 VZ |
61 | #ifdef TEST_CMDLINE |
62 | ||
d34bce84 VZ |
63 | #include <wx/cmdline.h> |
64 | #include <wx/datetime.h> | |
65 | ||
66 | static void ShowCmdLine(const wxCmdLineParser& parser) | |
67 | { | |
68 | wxString s = "Input files: "; | |
69 | ||
70 | size_t count = parser.GetParamCount(); | |
71 | for ( size_t param = 0; param < count; param++ ) | |
72 | { | |
73 | s << parser.GetParam(param) << ' '; | |
74 | } | |
75 | ||
76 | s << '\n' | |
77 | << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n' | |
78 | << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n'; | |
79 | ||
80 | wxString strVal; | |
81 | long lVal; | |
82 | wxDateTime dt; | |
83 | if ( parser.Found("o", &strVal) ) | |
84 | s << "Output file:\t" << strVal << '\n'; | |
85 | if ( parser.Found("i", &strVal) ) | |
86 | s << "Input dir:\t" << strVal << '\n'; | |
87 | if ( parser.Found("s", &lVal) ) | |
88 | s << "Size:\t" << lVal << '\n'; | |
89 | if ( parser.Found("d", &dt) ) | |
90 | s << "Date:\t" << dt.FormatISODate() << '\n'; | |
91 | ||
92 | wxLogMessage(s); | |
93 | } | |
94 | ||
95 | #endif // TEST_CMDLINE | |
96 | ||
1944c6bd VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // wxDir | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
101 | #ifdef TEST_DIR | |
102 | ||
103 | #include <wx/dir.h> | |
104 | ||
105 | static void TestDirEnumHelper(wxDir& dir, | |
106 | int flags = wxDIR_DEFAULT, | |
107 | const wxString& filespec = wxEmptyString) | |
108 | { | |
109 | wxString filename; | |
110 | ||
111 | if ( !dir.IsOpened() ) | |
112 | return; | |
113 | ||
114 | bool cont = dir.GetFirst(&filename, filespec, flags); | |
115 | while ( cont ) | |
116 | { | |
117 | printf("\t%s\n", filename.c_str()); | |
118 | ||
119 | cont = dir.GetNext(&filename); | |
120 | } | |
121 | ||
122 | puts(""); | |
123 | } | |
124 | ||
125 | static void TestDirEnum() | |
126 | { | |
127 | wxDir dir(wxGetCwd()); | |
128 | ||
129 | puts("Enumerating everything in current directory:"); | |
130 | TestDirEnumHelper(dir); | |
131 | ||
132 | puts("Enumerating really everything in current directory:"); | |
133 | TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT); | |
134 | ||
135 | puts("Enumerating object files in current directory:"); | |
136 | TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o"); | |
137 | ||
138 | puts("Enumerating directories in current directory:"); | |
139 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
140 | ||
141 | puts("Enumerating files in current directory:"); | |
142 | TestDirEnumHelper(dir, wxDIR_FILES); | |
143 | ||
144 | puts("Enumerating files including hidden in current directory:"); | |
145 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
146 | ||
147 | #ifdef __UNIX__ | |
148 | dir.Open("/"); | |
149 | #elif defined(__WXMSW__) | |
150 | dir.Open("c:\\"); | |
151 | #else | |
152 | #error "don't know where the root directory is" | |
153 | #endif | |
154 | ||
155 | puts("Enumerating everything in root directory:"); | |
156 | TestDirEnumHelper(dir, wxDIR_DEFAULT); | |
157 | ||
158 | puts("Enumerating directories in root directory:"); | |
159 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
160 | ||
161 | puts("Enumerating files in root directory:"); | |
162 | TestDirEnumHelper(dir, wxDIR_FILES); | |
163 | ||
164 | puts("Enumerating files including hidden in root directory:"); | |
165 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
166 | ||
167 | puts("Enumerating files in non existing directory:"); | |
168 | wxDir dirNo("nosuchdir"); | |
169 | TestDirEnumHelper(dirNo); | |
170 | } | |
171 | ||
172 | #endif // TEST_DIR | |
173 | ||
d93c719a VZ |
174 | // ---------------------------------------------------------------------------- |
175 | // wxExecute | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | #ifdef TEST_EXECUTE | |
179 | ||
180 | #include <wx/utils.h> | |
181 | ||
182 | static void TestExecute() | |
183 | { | |
184 | puts("*** testing wxExecute ***"); | |
185 | ||
186 | #ifdef __UNIX__ | |
187 | #define COMMAND "echo hi" | |
2c8e4738 VZ |
188 | #define SHELL_COMMAND "echo hi from shell" |
189 | #define REDIRECT_COMMAND "date" | |
d93c719a VZ |
190 | #elif defined(__WXMSW__) |
191 | #define COMMAND "command.com -c 'echo hi'" | |
2c8e4738 VZ |
192 | #define SHELL_COMMAND "echo hi" |
193 | #define REDIRECT_COMMAND COMMAND | |
d93c719a VZ |
194 | #else |
195 | #error "no command to exec" | |
196 | #endif // OS | |
197 | ||
2c8e4738 VZ |
198 | printf("Testing wxShell: "); |
199 | fflush(stdout); | |
200 | if ( wxShell(SHELL_COMMAND) ) | |
201 | puts("Ok."); | |
d93c719a | 202 | else |
2c8e4738 VZ |
203 | puts("ERROR."); |
204 | ||
205 | printf("Testing wxExecute: "); | |
206 | fflush(stdout); | |
207 | if ( wxExecute(COMMAND, TRUE /* sync */) == 0 ) | |
208 | puts("Ok."); | |
209 | else | |
210 | puts("ERROR."); | |
211 | ||
212 | #if 0 // no, it doesn't work (yet?) | |
213 | printf("Testing async wxExecute: "); | |
214 | fflush(stdout); | |
215 | if ( wxExecute(COMMAND) != 0 ) | |
216 | puts("Ok (command launched)."); | |
217 | else | |
218 | puts("ERROR."); | |
219 | #endif // 0 | |
220 | ||
221 | printf("Testing wxExecute with redirection:\n"); | |
222 | wxArrayString output; | |
223 | if ( wxExecute(REDIRECT_COMMAND, output) != 0 ) | |
224 | { | |
225 | puts("ERROR."); | |
226 | } | |
227 | else | |
228 | { | |
229 | size_t count = output.GetCount(); | |
230 | for ( size_t n = 0; n < count; n++ ) | |
231 | { | |
232 | printf("\t%s\n", output[n].c_str()); | |
233 | } | |
234 | ||
235 | puts("Ok."); | |
236 | } | |
d93c719a VZ |
237 | } |
238 | ||
239 | #endif // TEST_EXECUTE | |
240 | ||
ee6e1b1d VZ |
241 | // ---------------------------------------------------------------------------- |
242 | // wxFileConfig | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
245 | #ifdef TEST_FILECONF | |
246 | ||
247 | #include <wx/confbase.h> | |
248 | #include <wx/fileconf.h> | |
249 | ||
250 | static const struct FileConfTestData | |
251 | { | |
252 | const wxChar *name; // value name | |
253 | const wxChar *value; // the value from the file | |
254 | } fcTestData[] = | |
255 | { | |
256 | { _T("value1"), _T("one") }, | |
257 | { _T("value2"), _T("two") }, | |
258 | { _T("novalue"), _T("default") }, | |
259 | }; | |
260 | ||
261 | static void TestFileConfRead() | |
262 | { | |
263 | puts("*** testing wxFileConfig loading/reading ***"); | |
264 | ||
265 | wxFileConfig fileconf(_T("test"), wxEmptyString, | |
266 | _T("testdata.fc"), wxEmptyString, | |
267 | wxCONFIG_USE_RELATIVE_PATH); | |
268 | ||
269 | // test simple reading | |
270 | puts("\nReading config file:"); | |
271 | wxString defValue(_T("default")), value; | |
272 | for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ ) | |
273 | { | |
274 | const FileConfTestData& data = fcTestData[n]; | |
275 | value = fileconf.Read(data.name, defValue); | |
276 | printf("\t%s = %s ", data.name, value.c_str()); | |
277 | if ( value == data.value ) | |
278 | { | |
279 | puts("(ok)"); | |
280 | } | |
281 | else | |
282 | { | |
283 | printf("(ERROR: should be %s)\n", data.value); | |
284 | } | |
285 | } | |
286 | ||
287 | // test enumerating the entries | |
288 | puts("\nEnumerating all root entries:"); | |
289 | long dummy; | |
290 | wxString name; | |
291 | bool cont = fileconf.GetFirstEntry(name, dummy); | |
292 | while ( cont ) | |
293 | { | |
294 | printf("\t%s = %s\n", | |
295 | name.c_str(), | |
296 | fileconf.Read(name.c_str(), _T("ERROR")).c_str()); | |
297 | ||
298 | cont = fileconf.GetNextEntry(name, dummy); | |
299 | } | |
300 | } | |
301 | ||
302 | #endif // TEST_FILECONF | |
303 | ||
2c8e4738 VZ |
304 | // ---------------------------------------------------------------------------- |
305 | // wxHashTable | |
306 | // ---------------------------------------------------------------------------- | |
307 | ||
308 | #ifdef TEST_HASH | |
309 | ||
310 | #include <wx/hash.h> | |
311 | ||
312 | struct Foo | |
313 | { | |
314 | Foo(int n_) { n = n_; count++; } | |
315 | ~Foo() { count--; } | |
316 | ||
317 | int n; | |
318 | ||
319 | static size_t count; | |
320 | }; | |
321 | ||
322 | size_t Foo::count = 0; | |
323 | ||
324 | WX_DECLARE_LIST(Foo, wxListFoos); | |
325 | WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos); | |
326 | ||
327 | #include <wx/listimpl.cpp> | |
328 | ||
329 | WX_DEFINE_LIST(wxListFoos); | |
330 | ||
331 | static void TestHash() | |
332 | { | |
333 | puts("*** Testing wxHashTable ***\n"); | |
334 | ||
335 | { | |
336 | wxHashFoos hash; | |
337 | hash.DeleteContents(TRUE); | |
338 | ||
339 | printf("Hash created: %u foos in hash, %u foos totally\n", | |
340 | hash.GetCount(), Foo::count); | |
341 | ||
342 | static const int hashTestData[] = | |
343 | { | |
344 | 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1, | |
345 | }; | |
346 | ||
347 | size_t n; | |
348 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) | |
349 | { | |
350 | hash.Put(hashTestData[n], n, new Foo(n)); | |
351 | } | |
352 | ||
353 | printf("Hash filled: %u foos in hash, %u foos totally\n", | |
354 | hash.GetCount(), Foo::count); | |
355 | ||
356 | puts("Hash access test:"); | |
357 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) | |
358 | { | |
359 | printf("\tGetting element with key %d, value %d: ", | |
360 | hashTestData[n], n); | |
361 | Foo *foo = hash.Get(hashTestData[n], n); | |
362 | if ( !foo ) | |
363 | { | |
364 | printf("ERROR, not found.\n"); | |
365 | } | |
366 | else | |
367 | { | |
368 | printf("%d (%s)\n", foo->n, | |
369 | (size_t)foo->n == n ? "ok" : "ERROR"); | |
370 | } | |
371 | } | |
372 | ||
373 | printf("\nTrying to get an element not in hash: "); | |
374 | ||
375 | if ( hash.Get(1234) || hash.Get(1, 0) ) | |
376 | { | |
377 | puts("ERROR: found!"); | |
378 | } | |
379 | else | |
380 | { | |
381 | puts("ok (not found)"); | |
382 | } | |
383 | } | |
384 | ||
385 | printf("Hash destroyed: %u foos left\n", Foo::count); | |
386 | } | |
387 | ||
388 | #endif // TEST_HASH | |
389 | ||
696e1ea0 VZ |
390 | // ---------------------------------------------------------------------------- |
391 | // MIME types | |
392 | // ---------------------------------------------------------------------------- | |
393 | ||
394 | #ifdef TEST_MIME | |
395 | ||
396 | #include <wx/mimetype.h> | |
397 | ||
398 | static void TestMimeEnum() | |
399 | { | |
400 | wxMimeTypesManager mimeTM; | |
401 | wxArrayString mimetypes; | |
402 | ||
403 | size_t count = mimeTM.EnumAllFileTypes(mimetypes); | |
404 | ||
405 | printf("*** All %u known filetypes: ***\n", count); | |
406 | ||
407 | wxArrayString exts; | |
408 | wxString desc; | |
409 | ||
410 | for ( size_t n = 0; n < count; n++ ) | |
411 | { | |
412 | wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]); | |
413 | if ( !filetype ) | |
c61f4f6d | 414 | { |
97e0ceea VZ |
415 | printf("nothing known about the filetype '%s'!\n", |
416 | mimetypes[n].c_str()); | |
696e1ea0 | 417 | continue; |
c61f4f6d VZ |
418 | } |
419 | ||
696e1ea0 VZ |
420 | filetype->GetDescription(&desc); |
421 | filetype->GetExtensions(exts); | |
422 | ||
299fcbfe VZ |
423 | filetype->GetIcon(NULL); |
424 | ||
696e1ea0 VZ |
425 | wxString extsAll; |
426 | for ( size_t e = 0; e < exts.GetCount(); e++ ) | |
427 | { | |
428 | if ( e > 0 ) | |
429 | extsAll << _T(", "); | |
430 | extsAll += exts[e]; | |
431 | } | |
432 | ||
54acce90 VZ |
433 | printf("\t%s: %s (%s)\n", |
434 | mimetypes[n].c_str(), desc.c_str(), extsAll.c_str()); | |
696e1ea0 VZ |
435 | } |
436 | } | |
437 | ||
438 | #endif // TEST_MIME | |
439 | ||
b76b015e VZ |
440 | // ---------------------------------------------------------------------------- |
441 | // long long | |
442 | // ---------------------------------------------------------------------------- | |
443 | ||
444 | #ifdef TEST_LONGLONG | |
445 | ||
446 | #include <wx/longlong.h> | |
447 | #include <wx/timer.h> | |
448 | ||
2a310492 VZ |
449 | // make a 64 bit number from 4 16 bit ones |
450 | #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3) | |
451 | ||
452 | // get a random 64 bit number | |
453 | #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand()) | |
454 | ||
7d0bb74d | 455 | #if wxUSE_LONGLONG_WX |
2a310492 VZ |
456 | inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b) |
457 | { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); } | |
458 | inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b) | |
459 | { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); } | |
7d0bb74d | 460 | #endif // wxUSE_LONGLONG_WX |
2a310492 | 461 | |
b76b015e VZ |
462 | static void TestSpeed() |
463 | { | |
464 | static const long max = 100000000; | |
465 | long n; | |
9fc3ad34 | 466 | |
b76b015e VZ |
467 | { |
468 | wxStopWatch sw; | |
469 | ||
470 | long l = 0; | |
471 | for ( n = 0; n < max; n++ ) | |
472 | { | |
473 | l += n; | |
474 | } | |
475 | ||
476 | printf("Summing longs took %ld milliseconds.\n", sw.Time()); | |
477 | } | |
478 | ||
2ea24d9f | 479 | #if wxUSE_LONGLONG_NATIVE |
b76b015e VZ |
480 | { |
481 | wxStopWatch sw; | |
482 | ||
2ea24d9f | 483 | wxLongLong_t l = 0; |
b76b015e VZ |
484 | for ( n = 0; n < max; n++ ) |
485 | { | |
486 | l += n; | |
487 | } | |
488 | ||
2ea24d9f | 489 | printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time()); |
b76b015e | 490 | } |
2ea24d9f | 491 | #endif // wxUSE_LONGLONG_NATIVE |
b76b015e VZ |
492 | |
493 | { | |
494 | wxStopWatch sw; | |
495 | ||
496 | wxLongLong l; | |
497 | for ( n = 0; n < max; n++ ) | |
498 | { | |
499 | l += n; | |
500 | } | |
501 | ||
502 | printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time()); | |
503 | } | |
504 | } | |
505 | ||
2a310492 | 506 | static void TestLongLongConversion() |
b76b015e | 507 | { |
2a310492 VZ |
508 | puts("*** Testing wxLongLong conversions ***\n"); |
509 | ||
510 | wxLongLong a; | |
511 | size_t nTested = 0; | |
512 | for ( size_t n = 0; n < 100000; n++ ) | |
513 | { | |
514 | a = RAND_LL(); | |
515 | ||
516 | #if wxUSE_LONGLONG_NATIVE | |
517 | wxLongLongNative b(a.GetHi(), a.GetLo()); | |
5e6a0e83 | 518 | |
2a310492 VZ |
519 | wxASSERT_MSG( a == b, "conversions failure" ); |
520 | #else | |
521 | puts("Can't do it without native long long type, test skipped."); | |
b76b015e | 522 | |
2a310492 VZ |
523 | return; |
524 | #endif // wxUSE_LONGLONG_NATIVE | |
525 | ||
526 | if ( !(nTested % 1000) ) | |
527 | { | |
528 | putchar('.'); | |
529 | fflush(stdout); | |
530 | } | |
531 | ||
532 | nTested++; | |
533 | } | |
534 | ||
535 | puts(" done!"); | |
536 | } | |
537 | ||
538 | static void TestMultiplication() | |
539 | { | |
540 | puts("*** Testing wxLongLong multiplication ***\n"); | |
541 | ||
542 | wxLongLong a, b; | |
543 | size_t nTested = 0; | |
544 | for ( size_t n = 0; n < 100000; n++ ) | |
545 | { | |
546 | a = RAND_LL(); | |
547 | b = RAND_LL(); | |
548 | ||
549 | #if wxUSE_LONGLONG_NATIVE | |
550 | wxLongLongNative aa(a.GetHi(), a.GetLo()); | |
551 | wxLongLongNative bb(b.GetHi(), b.GetLo()); | |
552 | ||
553 | wxASSERT_MSG( a*b == aa*bb, "multiplication failure" ); | |
554 | #else // !wxUSE_LONGLONG_NATIVE | |
555 | puts("Can't do it without native long long type, test skipped."); | |
556 | ||
557 | return; | |
558 | #endif // wxUSE_LONGLONG_NATIVE | |
559 | ||
560 | if ( !(nTested % 1000) ) | |
561 | { | |
562 | putchar('.'); | |
563 | fflush(stdout); | |
564 | } | |
565 | ||
566 | nTested++; | |
567 | } | |
568 | ||
569 | puts(" done!"); | |
570 | } | |
571 | ||
572 | static void TestDivision() | |
573 | { | |
574 | puts("*** Testing wxLongLong division ***\n"); | |
2f02cb89 | 575 | |
2ea24d9f | 576 | wxLongLong q, r; |
2f02cb89 | 577 | size_t nTested = 0; |
5e6a0e83 | 578 | for ( size_t n = 0; n < 100000; n++ ) |
2f02cb89 VZ |
579 | { |
580 | // get a random wxLongLong (shifting by 12 the MSB ensures that the | |
581 | // multiplication will not overflow) | |
582 | wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand()); | |
583 | ||
2ea24d9f VZ |
584 | // get a random long (not wxLongLong for now) to divide it with |
585 | long l = rand(); | |
586 | q = ll / l; | |
587 | r = ll % l; | |
588 | ||
2a310492 VZ |
589 | #if wxUSE_LONGLONG_NATIVE |
590 | wxLongLongNative m(ll.GetHi(), ll.GetLo()); | |
591 | ||
592 | wxLongLongNative p = m / l, s = m % l; | |
593 | wxASSERT_MSG( q == p && r == s, "division failure" ); | |
594 | #else // !wxUSE_LONGLONG_NATIVE | |
5e6a0e83 | 595 | // verify the result |
2ea24d9f | 596 | wxASSERT_MSG( ll == q*l + r, "division failure" ); |
2a310492 | 597 | #endif // wxUSE_LONGLONG_NATIVE |
2f02cb89 | 598 | |
5e6a0e83 VZ |
599 | if ( !(nTested % 1000) ) |
600 | { | |
601 | putchar('.'); | |
602 | fflush(stdout); | |
603 | } | |
604 | ||
2f02cb89 VZ |
605 | nTested++; |
606 | } | |
607 | ||
5e6a0e83 | 608 | puts(" done!"); |
2a310492 | 609 | } |
2f02cb89 | 610 | |
2a310492 VZ |
611 | static void TestAddition() |
612 | { | |
613 | puts("*** Testing wxLongLong addition ***\n"); | |
614 | ||
615 | wxLongLong a, b, c; | |
616 | size_t nTested = 0; | |
617 | for ( size_t n = 0; n < 100000; n++ ) | |
618 | { | |
619 | a = RAND_LL(); | |
620 | b = RAND_LL(); | |
621 | c = a + b; | |
622 | ||
623 | #if wxUSE_LONGLONG_NATIVE | |
624 | wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) + | |
625 | wxLongLongNative(b.GetHi(), b.GetLo()), | |
7c968cee | 626 | "addition failure" ); |
2a310492 VZ |
627 | #else // !wxUSE_LONGLONG_NATIVE |
628 | wxASSERT_MSG( c - b == a, "addition failure" ); | |
629 | #endif // wxUSE_LONGLONG_NATIVE | |
630 | ||
631 | if ( !(nTested % 1000) ) | |
632 | { | |
633 | putchar('.'); | |
634 | fflush(stdout); | |
635 | } | |
636 | ||
637 | nTested++; | |
638 | } | |
639 | ||
640 | puts(" done!"); | |
b76b015e VZ |
641 | } |
642 | ||
2a310492 VZ |
643 | static void TestBitOperations() |
644 | { | |
645 | puts("*** Testing wxLongLong bit operation ***\n"); | |
646 | ||
647 | wxLongLong a, c; | |
648 | size_t nTested = 0; | |
649 | for ( size_t n = 0; n < 100000; n++ ) | |
650 | { | |
651 | a = RAND_LL(); | |
652 | ||
653 | #if wxUSE_LONGLONG_NATIVE | |
654 | for ( size_t n = 0; n < 33; n++ ) | |
655 | { | |
656 | wxLongLongNative b(a.GetHi(), a.GetLo()); | |
657 | ||
658 | b >>= n; | |
659 | c = a >> n; | |
660 | ||
661 | wxASSERT_MSG( b == c, "bit shift failure" ); | |
662 | ||
663 | b = wxLongLongNative(a.GetHi(), a.GetLo()) << n; | |
664 | c = a << n; | |
665 | ||
666 | wxASSERT_MSG( b == c, "bit shift failure" ); | |
667 | } | |
668 | ||
669 | #else // !wxUSE_LONGLONG_NATIVE | |
670 | puts("Can't do it without native long long type, test skipped."); | |
671 | ||
672 | return; | |
673 | #endif // wxUSE_LONGLONG_NATIVE | |
674 | ||
675 | if ( !(nTested % 1000) ) | |
676 | { | |
677 | putchar('.'); | |
678 | fflush(stdout); | |
679 | } | |
680 | ||
681 | nTested++; | |
682 | } | |
683 | ||
684 | puts(" done!"); | |
685 | } | |
686 | ||
687 | #undef MAKE_LL | |
688 | #undef RAND_LL | |
689 | ||
b76b015e VZ |
690 | #endif // TEST_LONGLONG |
691 | ||
2c8e4738 VZ |
692 | // ---------------------------------------------------------------------------- |
693 | // sockets | |
694 | // ---------------------------------------------------------------------------- | |
695 | ||
696 | #ifdef TEST_SOCKETS | |
697 | ||
698 | #include <wx/socket.h> | |
699 | ||
700 | static void TestSocketClient() | |
701 | { | |
702 | puts("*** Testing wxSocketClient ***\n"); | |
703 | ||
704 | wxIPV4address addrDst; | |
705 | addrDst.Hostname("www.wxwindows.org"); | |
706 | addrDst.Service(80); | |
707 | ||
708 | wxSocketClient client; | |
709 | if ( !client.Connect(addrDst) ) | |
710 | { | |
711 | printf("ERROR: failed to connect to %s\n", addrDst.Hostname().c_str()); | |
712 | } | |
713 | else | |
714 | { | |
715 | char buf[8192]; | |
716 | ||
717 | client.Write("get /front.htm\n", 17); | |
718 | client.Read(buf, WXSIZEOF(buf)); | |
719 | printf("Server replied:\n%s", buf); | |
720 | } | |
721 | } | |
722 | ||
723 | #endif // TEST_SOCKETS | |
724 | ||
d31b7b68 VZ |
725 | // ---------------------------------------------------------------------------- |
726 | // timers | |
727 | // ---------------------------------------------------------------------------- | |
728 | ||
729 | #ifdef TEST_TIMER | |
730 | ||
731 | #include <wx/timer.h> | |
732 | #include <wx/utils.h> | |
733 | ||
734 | static void TestStopWatch() | |
735 | { | |
736 | puts("*** Testing wxStopWatch ***\n"); | |
737 | ||
738 | wxStopWatch sw; | |
739 | printf("Sleeping 3 seconds..."); | |
740 | wxSleep(3); | |
87798c00 | 741 | printf("\telapsed time: %ldms\n", sw.Time()); |
d31b7b68 VZ |
742 | |
743 | sw.Pause(); | |
744 | printf("Sleeping 2 more seconds..."); | |
745 | wxSleep(2); | |
87798c00 | 746 | printf("\telapsed time: %ldms\n", sw.Time()); |
d31b7b68 VZ |
747 | |
748 | sw.Resume(); | |
749 | printf("And 3 more seconds..."); | |
750 | wxSleep(3); | |
87798c00 VZ |
751 | printf("\telapsed time: %ldms\n", sw.Time()); |
752 | ||
753 | wxStopWatch sw2; | |
754 | puts("\nChecking for 'backwards clock' bug..."); | |
755 | for ( size_t n = 0; n < 70; n++ ) | |
756 | { | |
757 | sw2.Start(); | |
758 | if ( sw.Time() < 0 || sw2.Time() < 0 ) | |
759 | { | |
760 | puts("\ntime is negative - ERROR!"); | |
761 | } | |
762 | ||
763 | putchar('.'); | |
764 | } | |
765 | ||
766 | puts(", ok."); | |
d31b7b68 VZ |
767 | } |
768 | ||
769 | #endif // TEST_TIMER | |
770 | ||
b76b015e VZ |
771 | // ---------------------------------------------------------------------------- |
772 | // date time | |
773 | // ---------------------------------------------------------------------------- | |
774 | ||
d31b7b68 | 775 | #ifdef TEST_DATETIME |
b76b015e | 776 | |
97e0ceea VZ |
777 | #include <wx/date.h> |
778 | ||
b76b015e VZ |
779 | #include <wx/datetime.h> |
780 | ||
299fcbfe VZ |
781 | // the test data |
782 | struct Date | |
783 | { | |
784 | wxDateTime::wxDateTime_t day; | |
785 | wxDateTime::Month month; | |
786 | int year; | |
787 | wxDateTime::wxDateTime_t hour, min, sec; | |
788 | double jdn; | |
211c2250 | 789 | wxDateTime::WeekDay wday; |
299fcbfe VZ |
790 | time_t gmticks, ticks; |
791 | ||
792 | void Init(const wxDateTime::Tm& tm) | |
793 | { | |
794 | day = tm.mday; | |
795 | month = tm.mon; | |
796 | year = tm.year; | |
797 | hour = tm.hour; | |
798 | min = tm.min; | |
799 | sec = tm.sec; | |
800 | jdn = 0.0; | |
801 | gmticks = ticks = -1; | |
802 | } | |
803 | ||
804 | wxDateTime DT() const | |
805 | { return wxDateTime(day, month, year, hour, min, sec); } | |
806 | ||
239446b4 VZ |
807 | bool SameDay(const wxDateTime::Tm& tm) const |
808 | { | |
809 | return day == tm.mday && month == tm.mon && year == tm.year; | |
810 | } | |
811 | ||
299fcbfe VZ |
812 | wxString Format() const |
813 | { | |
814 | wxString s; | |
815 | s.Printf("%02d:%02d:%02d %10s %02d, %4d%s", | |
816 | hour, min, sec, | |
817 | wxDateTime::GetMonthName(month).c_str(), | |
818 | day, | |
819 | abs(wxDateTime::ConvertYearToBC(year)), | |
820 | year > 0 ? "AD" : "BC"); | |
821 | return s; | |
822 | } | |
239446b4 VZ |
823 | |
824 | wxString FormatDate() const | |
825 | { | |
826 | wxString s; | |
827 | s.Printf("%02d-%s-%4d%s", | |
828 | day, | |
f0f951fa | 829 | wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), |
239446b4 VZ |
830 | abs(wxDateTime::ConvertYearToBC(year)), |
831 | year > 0 ? "AD" : "BC"); | |
832 | return s; | |
833 | } | |
299fcbfe VZ |
834 | }; |
835 | ||
836 | static const Date testDates[] = | |
837 | { | |
211c2250 VZ |
838 | { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 }, |
839 | { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 }, | |
840 | { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 }, | |
841 | { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 }, | |
842 | { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 }, | |
843 | { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 }, | |
844 | { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 }, | |
845 | { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 }, | |
846 | { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 }, | |
847 | { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 }, | |
239446b4 VZ |
848 | { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 }, |
849 | { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 }, | |
850 | { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 }, | |
851 | { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 }, | |
211c2250 | 852 | { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 }, |
299fcbfe VZ |
853 | }; |
854 | ||
2f02cb89 VZ |
855 | // this test miscellaneous static wxDateTime functions |
856 | static void TestTimeStatic() | |
857 | { | |
858 | puts("\n*** wxDateTime static methods test ***"); | |
859 | ||
860 | // some info about the current date | |
861 | int year = wxDateTime::GetCurrentYear(); | |
862 | printf("Current year %d is %sa leap one and has %d days.\n", | |
863 | year, | |
864 | wxDateTime::IsLeapYear(year) ? "" : "not ", | |
865 | wxDateTime::GetNumberOfDays(year)); | |
866 | ||
867 | wxDateTime::Month month = wxDateTime::GetCurrentMonth(); | |
868 | printf("Current month is '%s' ('%s') and it has %d days\n", | |
f0f951fa | 869 | wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), |
2f02cb89 VZ |
870 | wxDateTime::GetMonthName(month).c_str(), |
871 | wxDateTime::GetNumberOfDays(month)); | |
872 | ||
873 | // leap year logic | |
fcc3d7cb VZ |
874 | static const size_t nYears = 5; |
875 | static const size_t years[2][nYears] = | |
2f02cb89 VZ |
876 | { |
877 | // first line: the years to test | |
878 | { 1990, 1976, 2000, 2030, 1984, }, | |
879 | ||
880 | // second line: TRUE if leap, FALSE otherwise | |
881 | { FALSE, TRUE, TRUE, FALSE, TRUE } | |
882 | }; | |
883 | ||
884 | for ( size_t n = 0; n < nYears; n++ ) | |
885 | { | |
886 | int year = years[0][n]; | |
239446b4 VZ |
887 | bool should = years[1][n] != 0, |
888 | is = wxDateTime::IsLeapYear(year); | |
2f02cb89 | 889 | |
239446b4 | 890 | printf("Year %d is %sa leap year (%s)\n", |
2f02cb89 | 891 | year, |
239446b4 VZ |
892 | is ? "" : "not ", |
893 | should == is ? "ok" : "ERROR"); | |
2f02cb89 VZ |
894 | |
895 | wxASSERT( should == wxDateTime::IsLeapYear(year) ); | |
896 | } | |
897 | } | |
898 | ||
899 | // test constructing wxDateTime objects | |
900 | static void TestTimeSet() | |
901 | { | |
902 | puts("\n*** wxDateTime construction test ***"); | |
903 | ||
299fcbfe VZ |
904 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
905 | { | |
906 | const Date& d1 = testDates[n]; | |
907 | wxDateTime dt = d1.DT(); | |
908 | ||
909 | Date d2; | |
910 | d2.Init(dt.GetTm()); | |
911 | ||
912 | wxString s1 = d1.Format(), | |
913 | s2 = d2.Format(); | |
914 | ||
915 | printf("Date: %s == %s (%s)\n", | |
916 | s1.c_str(), s2.c_str(), | |
917 | s1 == s2 ? "ok" : "ERROR"); | |
918 | } | |
2f02cb89 VZ |
919 | } |
920 | ||
fcc3d7cb VZ |
921 | // test time zones stuff |
922 | static void TestTimeZones() | |
923 | { | |
924 | puts("\n*** wxDateTime timezone test ***"); | |
925 | ||
926 | wxDateTime now = wxDateTime::Now(); | |
927 | ||
299fcbfe VZ |
928 | printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str()); |
929 | printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str()); | |
930 | printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str()); | |
931 | printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str()); | |
932 | printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str()); | |
933 | printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str()); | |
9d9b7755 VZ |
934 | |
935 | wxDateTime::Tm tm = now.GetTm(); | |
936 | if ( wxDateTime(tm) != now ) | |
937 | { | |
938 | printf("ERROR: got %s instead of %s\n", | |
939 | wxDateTime(tm).Format().c_str(), now.Format().c_str()); | |
940 | } | |
fcc3d7cb VZ |
941 | } |
942 | ||
e6ec579c VZ |
943 | // test some minimal support for the dates outside the standard range |
944 | static void TestTimeRange() | |
945 | { | |
946 | puts("\n*** wxDateTime out-of-standard-range dates test ***"); | |
947 | ||
211c2250 VZ |
948 | static const char *fmt = "%d-%b-%Y %H:%M:%S"; |
949 | ||
1ef54dcf | 950 | printf("Unix epoch:\t%s\n", |
211c2250 | 951 | wxDateTime(2440587.5).Format(fmt).c_str()); |
1ef54dcf | 952 | printf("Feb 29, 0: \t%s\n", |
211c2250 | 953 | wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str()); |
e6ec579c | 954 | printf("JDN 0: \t%s\n", |
211c2250 | 955 | wxDateTime(0.0).Format(fmt).c_str()); |
e6ec579c | 956 | printf("Jan 1, 1AD:\t%s\n", |
211c2250 | 957 | wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str()); |
e6ec579c | 958 | printf("May 29, 2099:\t%s\n", |
211c2250 | 959 | wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str()); |
e6ec579c VZ |
960 | } |
961 | ||
299fcbfe | 962 | static void TestTimeTicks() |
e6ec579c | 963 | { |
299fcbfe | 964 | puts("\n*** wxDateTime ticks test ***"); |
e6ec579c | 965 | |
299fcbfe | 966 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
1ef54dcf | 967 | { |
299fcbfe VZ |
968 | const Date& d = testDates[n]; |
969 | if ( d.ticks == -1 ) | |
970 | continue; | |
1ef54dcf | 971 | |
299fcbfe VZ |
972 | wxDateTime dt = d.DT(); |
973 | long ticks = (dt.GetValue() / 1000).ToLong(); | |
974 | printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks); | |
975 | if ( ticks == d.ticks ) | |
976 | { | |
977 | puts(" (ok)"); | |
978 | } | |
979 | else | |
980 | { | |
981 | printf(" (ERROR: should be %ld, delta = %ld)\n", | |
982 | d.ticks, ticks - d.ticks); | |
983 | } | |
984 | ||
985 | dt = d.DT().ToTimezone(wxDateTime::GMT0); | |
986 | ticks = (dt.GetValue() / 1000).ToLong(); | |
987 | printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks); | |
988 | if ( ticks == d.gmticks ) | |
989 | { | |
990 | puts(" (ok)"); | |
991 | } | |
992 | else | |
993 | { | |
994 | printf(" (ERROR: should be %ld, delta = %ld)\n", | |
995 | d.gmticks, ticks - d.gmticks); | |
996 | } | |
997 | } | |
998 | ||
999 | puts(""); | |
1000 | } | |
1001 | ||
1002 | // test conversions to JDN &c | |
1003 | static void TestTimeJDN() | |
1004 | { | |
1005 | puts("\n*** wxDateTime to JDN test ***"); | |
1ef54dcf VZ |
1006 | |
1007 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) | |
1008 | { | |
1009 | const Date& d = testDates[n]; | |
299fcbfe | 1010 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); |
1ef54dcf VZ |
1011 | double jdn = dt.GetJulianDayNumber(); |
1012 | ||
299fcbfe | 1013 | printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn); |
1ef54dcf VZ |
1014 | if ( jdn == d.jdn ) |
1015 | { | |
1016 | puts(" (ok)"); | |
1017 | } | |
1018 | else | |
1019 | { | |
1020 | printf(" (ERROR: should be %f, delta = %f)\n", | |
1021 | d.jdn, jdn - d.jdn); | |
1022 | } | |
1023 | } | |
e6ec579c VZ |
1024 | } |
1025 | ||
211c2250 VZ |
1026 | // test week days computation |
1027 | static void TestTimeWDays() | |
1028 | { | |
1029 | puts("\n*** wxDateTime weekday test ***"); | |
1030 | ||
239446b4 VZ |
1031 | // test GetWeekDay() |
1032 | size_t n; | |
1033 | for ( n = 0; n < WXSIZEOF(testDates); n++ ) | |
211c2250 VZ |
1034 | { |
1035 | const Date& d = testDates[n]; | |
1036 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); | |
1037 | ||
1038 | wxDateTime::WeekDay wday = dt.GetWeekDay(); | |
1039 | printf("%s is: %s", | |
1040 | d.Format().c_str(), | |
239446b4 | 1041 | wxDateTime::GetWeekDayName(wday).c_str()); |
211c2250 VZ |
1042 | if ( wday == d.wday ) |
1043 | { | |
1044 | puts(" (ok)"); | |
1045 | } | |
1046 | else | |
1047 | { | |
1048 | printf(" (ERROR: should be %s)\n", | |
239446b4 VZ |
1049 | wxDateTime::GetWeekDayName(d.wday).c_str()); |
1050 | } | |
1051 | } | |
1052 | ||
1053 | puts(""); | |
1054 | ||
1055 | // test SetToWeekDay() | |
1056 | struct WeekDateTestData | |
1057 | { | |
1058 | Date date; // the real date (precomputed) | |
1059 | int nWeek; // its week index in the month | |
1060 | wxDateTime::WeekDay wday; // the weekday | |
1061 | wxDateTime::Month month; // the month | |
1062 | int year; // and the year | |
1063 | ||
1064 | wxString Format() const | |
1065 | { | |
1066 | wxString s, which; | |
1067 | switch ( nWeek < -1 ? -nWeek : nWeek ) | |
1068 | { | |
1069 | case 1: which = "first"; break; | |
1070 | case 2: which = "second"; break; | |
1071 | case 3: which = "third"; break; | |
1072 | case 4: which = "fourth"; break; | |
1073 | case 5: which = "fifth"; break; | |
1074 | ||
1075 | case -1: which = "last"; break; | |
1076 | } | |
1077 | ||
1078 | if ( nWeek < -1 ) | |
1079 | { | |
1080 | which += " from end"; | |
1081 | } | |
1082 | ||
1083 | s.Printf("The %s %s of %s in %d", | |
1084 | which.c_str(), | |
1085 | wxDateTime::GetWeekDayName(wday).c_str(), | |
1086 | wxDateTime::GetMonthName(month).c_str(), | |
1087 | year); | |
1088 | ||
1089 | return s; | |
1090 | } | |
1091 | }; | |
1092 | ||
1093 | // the array data was generated by the following python program | |
1094 | /* | |
1095 | from DateTime import * | |
1096 | from whrandom import * | |
1097 | from string import * | |
1098 | ||
1099 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
1100 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
1101 | ||
1102 | week = DateTimeDelta(7) | |
1103 | ||
1104 | for n in range(20): | |
1105 | year = randint(1900, 2100) | |
1106 | month = randint(1, 12) | |
1107 | day = randint(1, 28) | |
1108 | dt = DateTime(year, month, day) | |
1109 | wday = dt.day_of_week | |
1110 | ||
1111 | countFromEnd = choice([-1, 1]) | |
1112 | weekNum = 0; | |
1113 | ||
1114 | while dt.month is month: | |
1115 | dt = dt - countFromEnd * week | |
1116 | weekNum = weekNum + countFromEnd | |
1117 | ||
1118 | data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] } | |
97e0ceea | 1119 | |
239446b4 VZ |
1120 | print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\ |
1121 | "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data | |
97e0ceea | 1122 | */ |
239446b4 VZ |
1123 | |
1124 | static const WeekDateTestData weekDatesTestData[] = | |
1125 | { | |
1126 | { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 }, | |
1127 | { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 }, | |
1128 | { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 }, | |
1129 | { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 }, | |
1130 | { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 }, | |
1131 | { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 }, | |
1132 | { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 }, | |
1133 | { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 }, | |
1134 | { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 }, | |
1135 | { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 }, | |
1136 | { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 }, | |
1137 | { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 }, | |
1138 | { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 }, | |
1139 | { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 }, | |
1140 | { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 }, | |
1141 | { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 }, | |
1142 | { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 }, | |
1143 | { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 }, | |
1144 | { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 }, | |
1145 | { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }, | |
1146 | }; | |
1147 | ||
1148 | static const char *fmt = "%d-%b-%Y"; | |
1149 | ||
1150 | wxDateTime dt; | |
1151 | for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ ) | |
1152 | { | |
1153 | const WeekDateTestData& wd = weekDatesTestData[n]; | |
1154 | ||
1155 | dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year); | |
1156 | ||
1157 | printf("%s is %s", wd.Format().c_str(), dt.Format(fmt).c_str()); | |
1158 | ||
1159 | const Date& d = wd.date; | |
1160 | if ( d.SameDay(dt.GetTm()) ) | |
1161 | { | |
1162 | puts(" (ok)"); | |
1163 | } | |
1164 | else | |
1165 | { | |
1166 | dt.Set(d.day, d.month, d.year); | |
1167 | ||
1168 | printf(" (ERROR: should be %s)\n", dt.Format(fmt).c_str()); | |
211c2250 VZ |
1169 | } |
1170 | } | |
1171 | } | |
1172 | ||
239446b4 VZ |
1173 | // test the computation of (ISO) week numbers |
1174 | static void TestTimeWNumber() | |
1175 | { | |
1176 | puts("\n*** wxDateTime week number test ***"); | |
1177 | ||
1178 | struct WeekNumberTestData | |
1179 | { | |
1180 | Date date; // the date | |
9d9b7755 VZ |
1181 | wxDateTime::wxDateTime_t week; // the week number in the year |
1182 | wxDateTime::wxDateTime_t wmon; // the week number in the month | |
1183 | wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun | |
239446b4 VZ |
1184 | wxDateTime::wxDateTime_t dnum; // day number in the year |
1185 | }; | |
1186 | ||
1187 | // data generated with the following python script: | |
1188 | /* | |
1189 | from DateTime import * | |
1190 | from whrandom import * | |
1191 | from string import * | |
1192 | ||
1193 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
1194 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
1195 | ||
9d9b7755 VZ |
1196 | def GetMonthWeek(dt): |
1197 | weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1 | |
1198 | if weekNumMonth < 0: | |
1199 | weekNumMonth = weekNumMonth + 53 | |
1200 | return weekNumMonth | |
7c968cee | 1201 | |
9d9b7755 VZ |
1202 | def GetLastSundayBefore(dt): |
1203 | if dt.iso_week[2] == 7: | |
1204 | return dt | |
1205 | else: | |
1206 | return dt - DateTimeDelta(dt.iso_week[2]) | |
1207 | ||
239446b4 VZ |
1208 | for n in range(20): |
1209 | year = randint(1900, 2100) | |
1210 | month = randint(1, 12) | |
1211 | day = randint(1, 28) | |
1212 | dt = DateTime(year, month, day) | |
1213 | dayNum = dt.day_of_year | |
1214 | weekNum = dt.iso_week[1] | |
9d9b7755 VZ |
1215 | weekNumMonth = GetMonthWeek(dt) |
1216 | ||
1217 | weekNumMonth2 = 0 | |
1218 | dtSunday = GetLastSundayBefore(dt) | |
1219 | ||
1220 | while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)): | |
1221 | weekNumMonth2 = weekNumMonth2 + 1 | |
1222 | dtSunday = dtSunday - DateTimeDelta(7) | |
1223 | ||
1224 | data = { 'day': rjust(`day`, 2), \ | |
1225 | 'month': monthNames[month - 1], \ | |
1226 | 'year': year, \ | |
1227 | 'weekNum': rjust(`weekNum`, 2), \ | |
1228 | 'weekNumMonth': weekNumMonth, \ | |
1229 | 'weekNumMonth2': weekNumMonth2, \ | |
1230 | 'dayNum': rjust(`dayNum`, 3) } | |
1231 | ||
1232 | print " { { %(day)s, "\ | |
1233 | "wxDateTime::%(month)s, "\ | |
1234 | "%(year)d }, "\ | |
1235 | "%(weekNum)s, "\ | |
1236 | "%(weekNumMonth)s, "\ | |
1237 | "%(weekNumMonth2)s, "\ | |
239446b4 | 1238 | "%(dayNum)s }," % data |
9d9b7755 | 1239 | |
239446b4 VZ |
1240 | */ |
1241 | static const WeekNumberTestData weekNumberTestDates[] = | |
1242 | { | |
9d9b7755 VZ |
1243 | { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 }, |
1244 | { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 }, | |
1245 | { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 }, | |
1246 | { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 }, | |
1247 | { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 }, | |
1248 | { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 }, | |
1249 | { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 }, | |
1250 | { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 }, | |
1251 | { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 }, | |
1252 | { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 }, | |
1253 | { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 }, | |
1254 | { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 }, | |
1255 | { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 }, | |
1256 | { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 }, | |
1257 | { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 }, | |
1258 | { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 }, | |
1259 | { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 }, | |
1260 | { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 }, | |
1261 | { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 }, | |
1262 | { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 }, | |
239446b4 VZ |
1263 | }; |
1264 | ||
1265 | for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ ) | |
1266 | { | |
1267 | const WeekNumberTestData& wn = weekNumberTestDates[n]; | |
1268 | const Date& d = wn.date; | |
1269 | ||
1270 | wxDateTime dt = d.DT(); | |
1271 | ||
9d9b7755 VZ |
1272 | wxDateTime::wxDateTime_t |
1273 | week = dt.GetWeekOfYear(wxDateTime::Monday_First), | |
1274 | wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First), | |
1275 | wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First), | |
1276 | dnum = dt.GetDayOfYear(); | |
239446b4 VZ |
1277 | |
1278 | printf("%s: the day number is %d", | |
1279 | d.FormatDate().c_str(), dnum); | |
1280 | if ( dnum == wn.dnum ) | |
1281 | { | |
1282 | printf(" (ok)"); | |
1283 | } | |
1284 | else | |
1285 | { | |
1286 | printf(" (ERROR: should be %d)", wn.dnum); | |
1287 | } | |
1288 | ||
9d9b7755 VZ |
1289 | printf(", week in month is %d", wmon); |
1290 | if ( wmon == wn.wmon ) | |
1291 | { | |
1292 | printf(" (ok)"); | |
1293 | } | |
1294 | else | |
1295 | { | |
1296 | printf(" (ERROR: should be %d)", wn.wmon); | |
1297 | } | |
1298 | ||
1299 | printf(" or %d", wmon2); | |
1300 | if ( wmon2 == wn.wmon2 ) | |
1301 | { | |
1302 | printf(" (ok)"); | |
1303 | } | |
1304 | else | |
1305 | { | |
1306 | printf(" (ERROR: should be %d)", wn.wmon2); | |
1307 | } | |
1308 | ||
1309 | printf(", week in year is %d", week); | |
239446b4 VZ |
1310 | if ( week == wn.week ) |
1311 | { | |
1312 | puts(" (ok)"); | |
1313 | } | |
1314 | else | |
1315 | { | |
1316 | printf(" (ERROR: should be %d)\n", wn.week); | |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | // test DST calculations | |
1322 | static void TestTimeDST() | |
1323 | { | |
1324 | puts("\n*** wxDateTime DST test ***"); | |
1325 | ||
1326 | printf("DST is%s in effect now.\n\n", | |
1327 | wxDateTime::Now().IsDST() ? "" : " not"); | |
1328 | ||
1329 | // taken from http://www.energy.ca.gov/daylightsaving.html | |
1330 | static const Date datesDST[2][2004 - 1900 + 1] = | |
1331 | { | |
1332 | { | |
1333 | { 1, wxDateTime::Apr, 1990 }, | |
1334 | { 7, wxDateTime::Apr, 1991 }, | |
1335 | { 5, wxDateTime::Apr, 1992 }, | |
1336 | { 4, wxDateTime::Apr, 1993 }, | |
1337 | { 3, wxDateTime::Apr, 1994 }, | |
1338 | { 2, wxDateTime::Apr, 1995 }, | |
1339 | { 7, wxDateTime::Apr, 1996 }, | |
1340 | { 6, wxDateTime::Apr, 1997 }, | |
1341 | { 5, wxDateTime::Apr, 1998 }, | |
1342 | { 4, wxDateTime::Apr, 1999 }, | |
1343 | { 2, wxDateTime::Apr, 2000 }, | |
1344 | { 1, wxDateTime::Apr, 2001 }, | |
1345 | { 7, wxDateTime::Apr, 2002 }, | |
1346 | { 6, wxDateTime::Apr, 2003 }, | |
1347 | { 4, wxDateTime::Apr, 2004 }, | |
1348 | }, | |
1349 | { | |
1350 | { 28, wxDateTime::Oct, 1990 }, | |
1351 | { 27, wxDateTime::Oct, 1991 }, | |
1352 | { 25, wxDateTime::Oct, 1992 }, | |
1353 | { 31, wxDateTime::Oct, 1993 }, | |
1354 | { 30, wxDateTime::Oct, 1994 }, | |
1355 | { 29, wxDateTime::Oct, 1995 }, | |
1356 | { 27, wxDateTime::Oct, 1996 }, | |
1357 | { 26, wxDateTime::Oct, 1997 }, | |
1358 | { 25, wxDateTime::Oct, 1998 }, | |
1359 | { 31, wxDateTime::Oct, 1999 }, | |
1360 | { 29, wxDateTime::Oct, 2000 }, | |
1361 | { 28, wxDateTime::Oct, 2001 }, | |
1362 | { 27, wxDateTime::Oct, 2002 }, | |
1363 | { 26, wxDateTime::Oct, 2003 }, | |
1364 | { 31, wxDateTime::Oct, 2004 }, | |
1365 | } | |
1366 | }; | |
1367 | ||
1368 | int year; | |
1369 | for ( year = 1990; year < 2005; year++ ) | |
1370 | { | |
1371 | wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA), | |
1372 | dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA); | |
1373 | ||
1374 | printf("DST period in the US for year %d: from %s to %s", | |
1375 | year, dtBegin.Format().c_str(), dtEnd.Format().c_str()); | |
1376 | ||
1377 | size_t n = year - 1990; | |
1378 | const Date& dBegin = datesDST[0][n]; | |
1379 | const Date& dEnd = datesDST[1][n]; | |
97e0ceea | 1380 | |
239446b4 VZ |
1381 | if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) ) |
1382 | { | |
1383 | puts(" (ok)"); | |
1384 | } | |
1385 | else | |
1386 | { | |
1387 | printf(" (ERROR: should be %s %d to %s %d)\n", | |
1388 | wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day, | |
1389 | wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | puts(""); | |
1394 | ||
1395 | for ( year = 1990; year < 2005; year++ ) | |
1396 | { | |
1397 | printf("DST period in Europe for year %d: from %s to %s\n", | |
1398 | year, | |
1399 | wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(), | |
1400 | wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str()); | |
1401 | } | |
1402 | } | |
1403 | ||
68ee7c47 VZ |
1404 | // test wxDateTime -> text conversion |
1405 | static void TestTimeFormat() | |
1406 | { | |
1407 | puts("\n*** wxDateTime formatting test ***"); | |
1408 | ||
b38e2f7d VZ |
1409 | // some information may be lost during conversion, so store what kind |
1410 | // of info should we recover after a round trip | |
1411 | enum CompareKind | |
68ee7c47 | 1412 | { |
b38e2f7d VZ |
1413 | CompareNone, // don't try comparing |
1414 | CompareBoth, // dates and times should be identical | |
1415 | CompareDate, // dates only | |
1416 | CompareTime // time only | |
1417 | }; | |
1418 | ||
1419 | static const struct | |
1420 | { | |
1421 | CompareKind compareKind; | |
1422 | const char *format; | |
1423 | } formatTestFormats[] = | |
1424 | { | |
1425 | { CompareBoth, "---> %c" }, | |
1426 | { CompareDate, "Date is %A, %d of %B, in year %Y" }, | |
1427 | { CompareBoth, "Date is %x, time is %X" }, | |
1428 | { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" }, | |
1429 | { CompareNone, "The day of year: %j, the week of year: %W" }, | |
68ee7c47 VZ |
1430 | }; |
1431 | ||
1432 | static const Date formatTestDates[] = | |
1433 | { | |
68ee7c47 VZ |
1434 | { 29, wxDateTime::May, 1976, 18, 30, 00 }, |
1435 | { 31, wxDateTime::Dec, 1999, 23, 30, 00 }, | |
b38e2f7d VZ |
1436 | #if 0 |
1437 | // this test can't work for other centuries because it uses two digit | |
1438 | // years in formats, so don't even try it | |
68ee7c47 VZ |
1439 | { 29, wxDateTime::May, 2076, 18, 30, 00 }, |
1440 | { 29, wxDateTime::Feb, 2400, 02, 15, 25 }, | |
1441 | { 01, wxDateTime::Jan, -52, 03, 16, 47 }, | |
b38e2f7d | 1442 | #endif |
68ee7c47 VZ |
1443 | }; |
1444 | ||
1445 | // an extra test (as it doesn't depend on date, don't do it in the loop) | |
1446 | printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str()); | |
1447 | ||
b38e2f7d | 1448 | for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ ) |
68ee7c47 VZ |
1449 | { |
1450 | puts(""); | |
1451 | ||
b38e2f7d | 1452 | wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT(); |
68ee7c47 VZ |
1453 | for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) |
1454 | { | |
b38e2f7d | 1455 | wxString s = dt.Format(formatTestFormats[n].format); |
f0f951fa VZ |
1456 | printf("%s", s.c_str()); |
1457 | ||
b38e2f7d VZ |
1458 | // what can we recover? |
1459 | int kind = formatTestFormats[n].compareKind; | |
1460 | ||
f0f951fa VZ |
1461 | // convert back |
1462 | wxDateTime dt2; | |
b38e2f7d | 1463 | const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format); |
f0f951fa VZ |
1464 | if ( !result ) |
1465 | { | |
b38e2f7d VZ |
1466 | // converion failed - should it have? |
1467 | if ( kind == CompareNone ) | |
1468 | puts(" (ok)"); | |
1469 | else | |
1470 | puts(" (ERROR: conversion back failed)"); | |
f0f951fa VZ |
1471 | } |
1472 | else if ( *result ) | |
1473 | { | |
1474 | // should have parsed the entire string | |
1475 | puts(" (ERROR: conversion back stopped too soon)"); | |
1476 | } | |
f0f951fa VZ |
1477 | else |
1478 | { | |
b38e2f7d VZ |
1479 | bool equal = FALSE; // suppress compilaer warning |
1480 | switch ( kind ) | |
1481 | { | |
1482 | case CompareBoth: | |
1483 | equal = dt2 == dt; | |
1484 | break; | |
1485 | ||
1486 | case CompareDate: | |
1487 | equal = dt.IsSameDate(dt2); | |
1488 | break; | |
1489 | ||
1490 | case CompareTime: | |
1491 | equal = dt.IsSameTime(dt2); | |
1492 | break; | |
1493 | } | |
1494 | ||
1495 | if ( !equal ) | |
1496 | { | |
1497 | printf(" (ERROR: got back '%s' instead of '%s')\n", | |
1498 | dt2.Format().c_str(), dt.Format().c_str()); | |
1499 | } | |
1500 | else | |
1501 | { | |
1502 | puts(" (ok)"); | |
1503 | } | |
f0f951fa | 1504 | } |
68ee7c47 VZ |
1505 | } |
1506 | } | |
1507 | } | |
1508 | ||
97e0ceea VZ |
1509 | // test text -> wxDateTime conversion |
1510 | static void TestTimeParse() | |
1511 | { | |
1512 | puts("\n*** wxDateTime parse test ***"); | |
1513 | ||
1514 | struct ParseTestData | |
1515 | { | |
1516 | const char *format; | |
1517 | Date date; | |
1518 | bool good; | |
1519 | }; | |
1520 | ||
1521 | static const ParseTestData parseTestDates[] = | |
1522 | { | |
68ee7c47 VZ |
1523 | { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE }, |
1524 | { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE }, | |
97e0ceea VZ |
1525 | }; |
1526 | ||
1527 | for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) | |
1528 | { | |
1529 | const char *format = parseTestDates[n].format; | |
1530 | ||
1531 | printf("%s => ", format); | |
1532 | ||
1533 | wxDateTime dt; | |
1534 | if ( dt.ParseRfc822Date(format) ) | |
1535 | { | |
1536 | printf("%s ", dt.Format().c_str()); | |
1537 | ||
1538 | if ( parseTestDates[n].good ) | |
1539 | { | |
1540 | wxDateTime dtReal = parseTestDates[n].date.DT(); | |
1541 | if ( dt == dtReal ) | |
1542 | { | |
1543 | puts("(ok)"); | |
1544 | } | |
1545 | else | |
1546 | { | |
1547 | printf("(ERROR: should be %s)\n", dtReal.Format().c_str()); | |
1548 | } | |
1549 | } | |
1550 | else | |
1551 | { | |
1552 | puts("(ERROR: bad format)"); | |
1553 | } | |
1554 | } | |
1555 | else | |
1556 | { | |
1557 | printf("bad format (%s)\n", | |
1558 | parseTestDates[n].good ? "ERROR" : "ok"); | |
1559 | } | |
1560 | } | |
1561 | } | |
1562 | ||
9d9b7755 VZ |
1563 | static void TestInteractive() |
1564 | { | |
1565 | puts("\n*** interactive wxDateTime tests ***"); | |
1566 | ||
1567 | char buf[128]; | |
1568 | ||
1569 | for ( ;; ) | |
1570 | { | |
1571 | printf("Enter a date: "); | |
1572 | if ( !fgets(buf, WXSIZEOF(buf), stdin) ) | |
1573 | break; | |
1574 | ||
1575 | wxDateTime dt; | |
1576 | if ( !dt.ParseDate(buf) ) | |
1577 | { | |
1578 | puts("failed to parse the date"); | |
1579 | ||
1580 | continue; | |
1581 | } | |
1582 | ||
1583 | printf("%s: day %u, week of month %u/%u, week of year %u\n", | |
1584 | dt.FormatISODate().c_str(), | |
1585 | dt.GetDayOfYear(), | |
1586 | dt.GetWeekOfMonth(wxDateTime::Monday_First), | |
1587 | dt.GetWeekOfMonth(wxDateTime::Sunday_First), | |
1588 | dt.GetWeekOfYear(wxDateTime::Monday_First)); | |
1589 | } | |
1590 | ||
1591 | puts("\n*** done ***"); | |
1592 | } | |
1593 | ||
1594 | static void TestTimeArithmetics() | |
1595 | { | |
1596 | puts("\n*** testing arithmetic operations on wxDateTime ***"); | |
1597 | ||
1598 | static const struct | |
1599 | { | |
1600 | wxDateSpan span; | |
1601 | const char *name; | |
7c968cee | 1602 | } testArithmData[] = |
9d9b7755 VZ |
1603 | { |
1604 | { wxDateSpan::Day(), "day" }, | |
1605 | { wxDateSpan::Week(), "week" }, | |
1606 | { wxDateSpan::Month(), "month" }, | |
1607 | { wxDateSpan::Year(), "year" }, | |
1608 | { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" }, | |
1609 | }; | |
7c968cee | 1610 | |
9d9b7755 VZ |
1611 | wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2; |
1612 | ||
1613 | for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ ) | |
1614 | { | |
1615 | wxDateSpan span = testArithmData[n].span; | |
1616 | dt1 = dt + span; | |
1617 | dt2 = dt - span; | |
1618 | ||
1619 | const char *name = testArithmData[n].name; | |
1620 | printf("%s + %s = %s, %s - %s = %s\n", | |
1621 | dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(), | |
1622 | dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str()); | |
1623 | ||
1624 | printf("Going back: %s", (dt1 - span).FormatISODate().c_str()); | |
1625 | if ( dt1 - span == dt ) | |
1626 | { | |
1627 | puts(" (ok)"); | |
1628 | } | |
1629 | else | |
1630 | { | |
1631 | printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str()); | |
1632 | } | |
1633 | ||
1634 | printf("Going forward: %s", (dt2 + span).FormatISODate().c_str()); | |
1635 | if ( dt2 + span == dt ) | |
1636 | { | |
1637 | puts(" (ok)"); | |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str()); | |
1642 | } | |
1643 | ||
1644 | printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str()); | |
1645 | if ( dt2 + 2*span == dt1 ) | |
1646 | { | |
1647 | puts(" (ok)"); | |
1648 | } | |
1649 | else | |
1650 | { | |
1651 | printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str()); | |
1652 | } | |
1653 | ||
1654 | puts(""); | |
1655 | } | |
1656 | } | |
1657 | ||
0de868d9 VZ |
1658 | static void TestTimeHolidays() |
1659 | { | |
1660 | puts("\n*** testing wxDateTimeHolidayAuthority ***\n"); | |
1661 | ||
1662 | wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm(); | |
1663 | wxDateTime dtStart(1, tm.mon, tm.year), | |
1664 | dtEnd = dtStart.GetLastMonthDay(); | |
1665 | ||
1666 | wxDateTimeArray hol; | |
1667 | wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol); | |
1668 | ||
1669 | const wxChar *format = "%d-%b-%Y (%a)"; | |
1670 | ||
1671 | printf("All holidays between %s and %s:\n", | |
1672 | dtStart.Format(format).c_str(), dtEnd.Format(format).c_str()); | |
1673 | ||
1674 | size_t count = hol.GetCount(); | |
1675 | for ( size_t n = 0; n < count; n++ ) | |
1676 | { | |
1677 | printf("\t%s\n", hol[n].Format(format).c_str()); | |
1678 | } | |
1679 | ||
1680 | puts(""); | |
1681 | } | |
1682 | ||
68ee7c47 VZ |
1683 | #if 0 |
1684 | ||
97e0ceea VZ |
1685 | // test compatibility with the old wxDate/wxTime classes |
1686 | static void TestTimeCompatibility() | |
1687 | { | |
1688 | puts("\n*** wxDateTime compatibility test ***"); | |
1689 | ||
1690 | printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str()); | |
1691 | printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str()); | |
1692 | ||
1693 | double jdnNow = wxDateTime::Now().GetJDN(); | |
1694 | long jdnMidnight = (long)(jdnNow - 0.5); | |
1695 | printf("wxDate for today: %s\n", wxDate(jdnMidnight).FormatDate().c_str()); | |
1696 | ||
1697 | jdnMidnight = wxDate().Set().GetJulianDate(); | |
1698 | printf("wxDateTime for today: %s\n", | |
1699 | wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str()); | |
1700 | ||
1701 | int flags = wxEUROPEAN;//wxFULL; | |
1702 | wxDate date; | |
1703 | date.Set(); | |
1704 | printf("Today is %s\n", date.FormatDate(flags).c_str()); | |
1705 | for ( int n = 0; n < 7; n++ ) | |
1706 | { | |
1707 | printf("Previous %s is %s\n", | |
1708 | wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n), | |
1709 | date.Previous(n + 1).FormatDate(flags).c_str()); | |
1710 | } | |
1711 | } | |
1712 | ||
68ee7c47 VZ |
1713 | #endif // 0 |
1714 | ||
d31b7b68 | 1715 | #endif // TEST_DATETIME |
b76b015e | 1716 | |
e87271f3 VZ |
1717 | // ---------------------------------------------------------------------------- |
1718 | // threads | |
1719 | // ---------------------------------------------------------------------------- | |
1720 | ||
1721 | #ifdef TEST_THREADS | |
1722 | ||
bbfa0322 | 1723 | #include <wx/thread.h> |
37667812 | 1724 | |
bbfa0322 VZ |
1725 | static size_t gs_counter = (size_t)-1; |
1726 | static wxCriticalSection gs_critsect; | |
b568d04f | 1727 | static wxCondition gs_cond; |
bbfa0322 | 1728 | |
b568d04f | 1729 | class MyJoinableThread : public wxThread |
bbfa0322 VZ |
1730 | { |
1731 | public: | |
b568d04f VZ |
1732 | MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE) |
1733 | { m_n = n; Create(); } | |
bbfa0322 VZ |
1734 | |
1735 | // thread execution starts here | |
b568d04f | 1736 | virtual ExitCode Entry(); |
bbfa0322 | 1737 | |
b568d04f VZ |
1738 | private: |
1739 | size_t m_n; | |
bbfa0322 VZ |
1740 | }; |
1741 | ||
b568d04f | 1742 | wxThread::ExitCode MyJoinableThread::Entry() |
bbfa0322 | 1743 | { |
b568d04f VZ |
1744 | unsigned long res = 1; |
1745 | for ( size_t n = 1; n < m_n; n++ ) | |
1746 | { | |
1747 | res *= n; | |
1748 | ||
1749 | // it's a loooong calculation :-) | |
1750 | Sleep(100); | |
1751 | } | |
bbfa0322 | 1752 | |
b568d04f | 1753 | return (ExitCode)res; |
bbfa0322 VZ |
1754 | } |
1755 | ||
b568d04f VZ |
1756 | class MyDetachedThread : public wxThread |
1757 | { | |
1758 | public: | |
fcc3d7cb VZ |
1759 | MyDetachedThread(size_t n, char ch) |
1760 | { | |
1761 | m_n = n; | |
1762 | m_ch = ch; | |
1763 | m_cancelled = FALSE; | |
1764 | ||
1765 | Create(); | |
1766 | } | |
b568d04f VZ |
1767 | |
1768 | // thread execution starts here | |
1769 | virtual ExitCode Entry(); | |
1770 | ||
1771 | // and stops here | |
1772 | virtual void OnExit(); | |
1773 | ||
1774 | private: | |
9fc3ad34 VZ |
1775 | size_t m_n; // number of characters to write |
1776 | char m_ch; // character to write | |
fcc3d7cb VZ |
1777 | |
1778 | bool m_cancelled; // FALSE if we exit normally | |
b568d04f VZ |
1779 | }; |
1780 | ||
1781 | wxThread::ExitCode MyDetachedThread::Entry() | |
bbfa0322 VZ |
1782 | { |
1783 | { | |
1784 | wxCriticalSectionLocker lock(gs_critsect); | |
1785 | if ( gs_counter == (size_t)-1 ) | |
1786 | gs_counter = 1; | |
1787 | else | |
1788 | gs_counter++; | |
1789 | } | |
1790 | ||
9fc3ad34 | 1791 | for ( size_t n = 0; n < m_n; n++ ) |
bbfa0322 VZ |
1792 | { |
1793 | if ( TestDestroy() ) | |
fcc3d7cb VZ |
1794 | { |
1795 | m_cancelled = TRUE; | |
1796 | ||
bbfa0322 | 1797 | break; |
fcc3d7cb | 1798 | } |
bbfa0322 VZ |
1799 | |
1800 | putchar(m_ch); | |
1801 | fflush(stdout); | |
1802 | ||
1803 | wxThread::Sleep(100); | |
1804 | } | |
1805 | ||
b568d04f | 1806 | return 0; |
bbfa0322 VZ |
1807 | } |
1808 | ||
b568d04f | 1809 | void MyDetachedThread::OnExit() |
bbfa0322 | 1810 | { |
9fc3ad34 VZ |
1811 | wxLogTrace("thread", "Thread %ld is in OnExit", GetId()); |
1812 | ||
bbfa0322 | 1813 | wxCriticalSectionLocker lock(gs_critsect); |
fcc3d7cb | 1814 | if ( !--gs_counter && !m_cancelled ) |
b568d04f | 1815 | gs_cond.Signal(); |
bbfa0322 VZ |
1816 | } |
1817 | ||
9fc3ad34 VZ |
1818 | void TestDetachedThreads() |
1819 | { | |
2f02cb89 | 1820 | puts("\n*** Testing detached threads ***"); |
9fc3ad34 VZ |
1821 | |
1822 | static const size_t nThreads = 3; | |
1823 | MyDetachedThread *threads[nThreads]; | |
1824 | size_t n; | |
1825 | for ( n = 0; n < nThreads; n++ ) | |
1826 | { | |
1827 | threads[n] = new MyDetachedThread(10, 'A' + n); | |
1828 | } | |
1829 | ||
1830 | threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY); | |
1831 | threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY); | |
1832 | ||
1833 | for ( n = 0; n < nThreads; n++ ) | |
1834 | { | |
1835 | threads[n]->Run(); | |
1836 | } | |
1837 | ||
1838 | // wait until all threads terminate | |
1839 | gs_cond.Wait(); | |
1840 | ||
1841 | puts(""); | |
1842 | } | |
1843 | ||
1844 | void TestJoinableThreads() | |
1845 | { | |
2f02cb89 | 1846 | puts("\n*** Testing a joinable thread (a loooong calculation...) ***"); |
9fc3ad34 VZ |
1847 | |
1848 | // calc 10! in the background | |
1849 | MyJoinableThread thread(10); | |
1850 | thread.Run(); | |
1851 | ||
1852 | printf("\nThread terminated with exit code %lu.\n", | |
1853 | (unsigned long)thread.Wait()); | |
1854 | } | |
1855 | ||
1856 | void TestThreadSuspend() | |
1857 | { | |
2f02cb89 VZ |
1858 | puts("\n*** Testing thread suspend/resume functions ***"); |
1859 | ||
1860 | MyDetachedThread *thread = new MyDetachedThread(15, 'X'); | |
9fc3ad34 VZ |
1861 | |
1862 | thread->Run(); | |
1863 | ||
1864 | // this is for this demo only, in a real life program we'd use another | |
1865 | // condition variable which would be signaled from wxThread::Entry() to | |
1866 | // tell us that the thread really started running - but here just wait a | |
1867 | // bit and hope that it will be enough (the problem is, of course, that | |
1868 | // the thread might still not run when we call Pause() which will result | |
1869 | // in an error) | |
1870 | wxThread::Sleep(300); | |
1871 | ||
1872 | for ( size_t n = 0; n < 3; n++ ) | |
1873 | { | |
1874 | thread->Pause(); | |
1875 | ||
1876 | puts("\nThread suspended"); | |
1877 | if ( n > 0 ) | |
1878 | { | |
1879 | // don't sleep but resume immediately the first time | |
1880 | wxThread::Sleep(300); | |
1881 | } | |
1882 | puts("Going to resume the thread"); | |
1883 | ||
1884 | thread->Resume(); | |
1885 | } | |
1886 | ||
4c460b34 VZ |
1887 | puts("Waiting until it terminates now"); |
1888 | ||
9fc3ad34 VZ |
1889 | // wait until the thread terminates |
1890 | gs_cond.Wait(); | |
1891 | ||
1892 | puts(""); | |
1893 | } | |
1894 | ||
2f02cb89 VZ |
1895 | void TestThreadDelete() |
1896 | { | |
1897 | // As above, using Sleep() is only for testing here - we must use some | |
1898 | // synchronisation object instead to ensure that the thread is still | |
1899 | // running when we delete it - deleting a detached thread which already | |
1900 | // terminated will lead to a crash! | |
1901 | ||
1902 | puts("\n*** Testing thread delete function ***"); | |
1903 | ||
4c460b34 VZ |
1904 | MyDetachedThread *thread0 = new MyDetachedThread(30, 'W'); |
1905 | ||
1906 | thread0->Delete(); | |
1907 | ||
1908 | puts("\nDeleted a thread which didn't start to run yet."); | |
1909 | ||
2f02cb89 VZ |
1910 | MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y'); |
1911 | ||
1912 | thread1->Run(); | |
1913 | ||
1914 | wxThread::Sleep(300); | |
1915 | ||
1916 | thread1->Delete(); | |
1917 | ||
1918 | puts("\nDeleted a running thread."); | |
1919 | ||
1920 | MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z'); | |
1921 | ||
1922 | thread2->Run(); | |
1923 | ||
1924 | wxThread::Sleep(300); | |
1925 | ||
1926 | thread2->Pause(); | |
1927 | ||
1928 | thread2->Delete(); | |
1929 | ||
1930 | puts("\nDeleted a sleeping thread."); | |
1931 | ||
4c460b34 VZ |
1932 | MyJoinableThread thread3(20); |
1933 | thread3.Run(); | |
2f02cb89 | 1934 | |
4c460b34 | 1935 | thread3.Delete(); |
2f02cb89 VZ |
1936 | |
1937 | puts("\nDeleted a joinable thread."); | |
1938 | ||
4c460b34 VZ |
1939 | MyJoinableThread thread4(2); |
1940 | thread4.Run(); | |
2f02cb89 VZ |
1941 | |
1942 | wxThread::Sleep(300); | |
1943 | ||
4c460b34 | 1944 | thread4.Delete(); |
2f02cb89 VZ |
1945 | |
1946 | puts("\nDeleted a joinable thread which already terminated."); | |
1947 | ||
1948 | puts(""); | |
1949 | } | |
1950 | ||
e87271f3 VZ |
1951 | #endif // TEST_THREADS |
1952 | ||
1953 | // ---------------------------------------------------------------------------- | |
1954 | // arrays | |
1955 | // ---------------------------------------------------------------------------- | |
1956 | ||
1957 | #ifdef TEST_ARRAYS | |
1958 | ||
1959 | void PrintArray(const char* name, const wxArrayString& array) | |
1960 | { | |
1961 | printf("Dump of the array '%s'\n", name); | |
1962 | ||
1963 | size_t nCount = array.GetCount(); | |
1964 | for ( size_t n = 0; n < nCount; n++ ) | |
1965 | { | |
1966 | printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str()); | |
1967 | } | |
1968 | } | |
1969 | ||
1970 | #endif // TEST_ARRAYS | |
1971 | ||
9fc3ad34 VZ |
1972 | // ---------------------------------------------------------------------------- |
1973 | // strings | |
1974 | // ---------------------------------------------------------------------------- | |
1975 | ||
1976 | #ifdef TEST_STRINGS | |
1977 | ||
1978 | #include "wx/timer.h" | |
bbf8fc53 | 1979 | #include "wx/tokenzr.h" |
9fc3ad34 | 1980 | |
7c968cee VZ |
1981 | static void TestStringConstruction() |
1982 | { | |
1983 | puts("*** Testing wxString constructores ***"); | |
1984 | ||
1985 | #define TEST_CTOR(args, res) \ | |
1986 | { \ | |
1987 | wxString s args ; \ | |
1988 | printf("wxString%s = %s ", #args, s.c_str()); \ | |
1989 | if ( s == res ) \ | |
1990 | { \ | |
1991 | puts("(ok)"); \ | |
1992 | } \ | |
1993 | else \ | |
1994 | { \ | |
1995 | printf("(ERROR: should be %s)\n", res); \ | |
1996 | } \ | |
1997 | } | |
1998 | ||
1999 | TEST_CTOR((_T('Z'), 4), _T("ZZZZ")); | |
2000 | TEST_CTOR((_T("Hello"), 4), _T("Hell")); | |
2001 | TEST_CTOR((_T("Hello"), 5), _T("Hello")); | |
2002 | // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure | |
2003 | ||
2004 | static const wxChar *s = _T("?really!"); | |
2005 | const wxChar *start = wxStrchr(s, _T('r')); | |
2006 | const wxChar *end = wxStrchr(s, _T('!')); | |
2007 | TEST_CTOR((start, end), _T("really")); | |
2008 | ||
2009 | puts(""); | |
2010 | } | |
2011 | ||
299fcbfe | 2012 | static void TestString() |
9fc3ad34 VZ |
2013 | { |
2014 | wxStopWatch sw; | |
2015 | ||
2016 | wxString a, b, c; | |
2017 | ||
2018 | a.reserve (128); | |
2019 | b.reserve (128); | |
2020 | c.reserve (128); | |
2021 | ||
2022 | for (int i = 0; i < 1000000; ++i) | |
2023 | { | |
2024 | a = "Hello"; | |
2025 | b = " world"; | |
2026 | c = "! How'ya doin'?"; | |
2027 | a += b; | |
2028 | a += c; | |
2029 | c = "Hello world! What's up?"; | |
2030 | if (c != a) | |
2031 | c = "Doh!"; | |
2032 | } | |
2033 | ||
2034 | printf ("TestString elapsed time: %ld\n", sw.Time()); | |
2035 | } | |
2036 | ||
299fcbfe | 2037 | static void TestPChar() |
9fc3ad34 VZ |
2038 | { |
2039 | wxStopWatch sw; | |
2040 | ||
2041 | char a [128]; | |
2042 | char b [128]; | |
2043 | char c [128]; | |
2044 | ||
2045 | for (int i = 0; i < 1000000; ++i) | |
2046 | { | |
2047 | strcpy (a, "Hello"); | |
2048 | strcpy (b, " world"); | |
2049 | strcpy (c, "! How'ya doin'?"); | |
2050 | strcat (a, b); | |
2051 | strcat (a, c); | |
2052 | strcpy (c, "Hello world! What's up?"); | |
2053 | if (strcmp (c, a) == 0) | |
2054 | strcpy (c, "Doh!"); | |
2055 | } | |
2056 | ||
2057 | printf ("TestPChar elapsed time: %ld\n", sw.Time()); | |
2058 | } | |
2059 | ||
299fcbfe VZ |
2060 | static void TestStringSub() |
2061 | { | |
2062 | wxString s("Hello, world!"); | |
2063 | ||
2064 | puts("*** Testing wxString substring extraction ***"); | |
2065 | ||
2066 | printf("String = '%s'\n", s.c_str()); | |
2067 | printf("Left(5) = '%s'\n", s.Left(5).c_str()); | |
2068 | printf("Right(6) = '%s'\n", s.Right(6).c_str()); | |
2069 | printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str()); | |
2070 | printf("Mid(3) = '%s'\n", s.Mid(3).c_str()); | |
2071 | printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str()); | |
2072 | printf("substr(3) = '%s'\n", s.substr(3).c_str()); | |
2073 | ||
2074 | puts(""); | |
2075 | } | |
2076 | ||
f0f951fa VZ |
2077 | static void TestStringFormat() |
2078 | { | |
2079 | puts("*** Testing wxString formatting ***"); | |
2080 | ||
2081 | wxString s; | |
2082 | s.Printf("%03d", 18); | |
2083 | ||
2084 | printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str()); | |
2085 | printf("Number 18: %s\n", s.c_str()); | |
2086 | ||
2087 | puts(""); | |
2088 | } | |
2089 | ||
d71fa6fb VZ |
2090 | // returns "not found" for npos, value for all others |
2091 | static wxString PosToString(size_t res) | |
2092 | { | |
2093 | wxString s = res == wxString::npos ? wxString(_T("not found")) | |
2094 | : wxString::Format(_T("%u"), res); | |
2095 | return s; | |
2096 | } | |
2097 | ||
2098 | static void TestStringFind() | |
2099 | { | |
2100 | puts("*** Testing wxString find() functions ***"); | |
2101 | ||
2102 | static const wxChar *strToFind = _T("ell"); | |
2103 | static const struct StringFindTest | |
2104 | { | |
2105 | const wxChar *str; | |
2106 | size_t start, | |
2107 | result; // of searching "ell" in str | |
2108 | } findTestData[] = | |
2109 | { | |
2110 | { _T("Well, hello world"), 0, 1 }, | |
2111 | { _T("Well, hello world"), 6, 7 }, | |
2112 | { _T("Well, hello world"), 9, wxString::npos }, | |
2113 | }; | |
2114 | ||
2115 | for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ ) | |
2116 | { | |
2117 | const StringFindTest& ft = findTestData[n]; | |
2118 | size_t res = wxString(ft.str).find(strToFind, ft.start); | |
2119 | ||
2120 | printf(_T("Index of '%s' in '%s' starting from %u is %s "), | |
2121 | strToFind, ft.str, ft.start, PosToString(res).c_str()); | |
2122 | ||
2123 | size_t resTrue = ft.result; | |
2124 | if ( res == resTrue ) | |
2125 | { | |
2126 | puts(_T("(ok)")); | |
2127 | } | |
2128 | else | |
2129 | { | |
2130 | printf(_T("(ERROR: should be %s)\n"), | |
2131 | PosToString(resTrue).c_str()); | |
2132 | } | |
2133 | } | |
2134 | ||
2135 | puts(""); | |
2136 | } | |
2137 | ||
bbf8fc53 VZ |
2138 | // replace TABs with \t and CRs with \n |
2139 | static wxString MakePrintable(const wxChar *s) | |
2140 | { | |
2141 | wxString str(s); | |
2142 | (void)str.Replace(_T("\t"), _T("\\t")); | |
2143 | (void)str.Replace(_T("\n"), _T("\\n")); | |
2144 | (void)str.Replace(_T("\r"), _T("\\r")); | |
2145 | ||
2146 | return str; | |
2147 | } | |
2148 | ||
2149 | static void TestStringTokenizer() | |
2150 | { | |
2151 | puts("*** Testing wxStringTokenizer ***"); | |
2152 | ||
7c968cee VZ |
2153 | static const wxChar *modeNames[] = |
2154 | { | |
2155 | _T("default"), | |
2156 | _T("return empty"), | |
2157 | _T("return all empty"), | |
2158 | _T("with delims"), | |
2159 | _T("like strtok"), | |
2160 | }; | |
2161 | ||
bbf8fc53 VZ |
2162 | static const struct StringTokenizerTest |
2163 | { | |
7c968cee VZ |
2164 | const wxChar *str; // string to tokenize |
2165 | const wxChar *delims; // delimiters to use | |
2166 | size_t count; // count of token | |
2167 | wxStringTokenizerMode mode; // how should we tokenize it | |
2168 | } tokenizerTestData[] = | |
2169 | { | |
2170 | { _T(""), _T(" "), 0 }, | |
2171 | { _T("Hello, world"), _T(" "), 2 }, | |
2172 | { _T("Hello, world "), _T(" "), 2 }, | |
2173 | { _T("Hello, world"), _T(","), 2 }, | |
2174 | { _T("Hello, world!"), _T(",!"), 2 }, | |
2175 | { _T("Hello,, world!"), _T(",!"), 3 }, | |
2176 | { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL }, | |
2177 | { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 }, | |
2178 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 }, | |
2179 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY }, | |
2180 | { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL }, | |
2181 | { _T("01/02/99"), _T("/-"), 3 }, | |
2182 | { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS }, | |
bbf8fc53 VZ |
2183 | }; |
2184 | ||
2185 | for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ ) | |
2186 | { | |
2187 | const StringTokenizerTest& tt = tokenizerTestData[n]; | |
7c968cee | 2188 | wxStringTokenizer tkz(tt.str, tt.delims, tt.mode); |
bbf8fc53 VZ |
2189 | |
2190 | size_t count = tkz.CountTokens(); | |
7c968cee VZ |
2191 | printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "), |
2192 | MakePrintable(tt.str).c_str(), | |
bbf8fc53 | 2193 | count, |
7c968cee VZ |
2194 | MakePrintable(tt.delims).c_str(), |
2195 | modeNames[tkz.GetMode()]); | |
bbf8fc53 VZ |
2196 | if ( count == tt.count ) |
2197 | { | |
2198 | puts(_T("(ok)")); | |
2199 | } | |
2200 | else | |
2201 | { | |
2202 | printf(_T("(ERROR: should be %u)\n"), tt.count); | |
2203 | ||
2204 | continue; | |
2205 | } | |
2206 | ||
7c968cee VZ |
2207 | // if we emulate strtok(), check that we do it correctly |
2208 | wxChar *buf, *s, *last; | |
2209 | ||
2210 | if ( tkz.GetMode() == wxTOKEN_STRTOK ) | |
2211 | { | |
2212 | buf = new wxChar[wxStrlen(tt.str) + 1]; | |
2213 | wxStrcpy(buf, tt.str); | |
2214 | ||
2215 | s = wxStrtok(buf, tt.delims, &last); | |
2216 | } | |
2217 | else | |
2218 | { | |
2219 | buf = NULL; | |
2220 | } | |
2221 | ||
bbf8fc53 VZ |
2222 | // now show the tokens themselves |
2223 | size_t count2 = 0; | |
2224 | while ( tkz.HasMoreTokens() ) | |
2225 | { | |
7c968cee VZ |
2226 | wxString token = tkz.GetNextToken(); |
2227 | ||
2228 | printf(_T("\ttoken %u: '%s'"), | |
bbf8fc53 | 2229 | ++count2, |
7c968cee VZ |
2230 | MakePrintable(token).c_str()); |
2231 | ||
2232 | if ( buf ) | |
2233 | { | |
2234 | if ( token == s ) | |
2235 | { | |
2236 | puts(" (ok)"); | |
2237 | } | |
2238 | else | |
2239 | { | |
2240 | printf(" (ERROR: should be %s)\n", s); | |
2241 | } | |
2242 | ||
2243 | s = wxStrtok(NULL, tt.delims, &last); | |
2244 | } | |
2245 | else | |
2246 | { | |
2247 | // nothing to compare with | |
2248 | puts(""); | |
2249 | } | |
bbf8fc53 VZ |
2250 | } |
2251 | ||
2252 | if ( count2 != count ) | |
2253 | { | |
7c968cee | 2254 | puts(_T("\tERROR: token count mismatch")); |
bbf8fc53 | 2255 | } |
7c968cee VZ |
2256 | |
2257 | delete [] buf; | |
bbf8fc53 VZ |
2258 | } |
2259 | ||
2260 | puts(""); | |
2261 | } | |
2262 | ||
9fc3ad34 VZ |
2263 | #endif // TEST_STRINGS |
2264 | ||
e87271f3 VZ |
2265 | // ---------------------------------------------------------------------------- |
2266 | // entry point | |
2267 | // ---------------------------------------------------------------------------- | |
2268 | ||
bbfa0322 | 2269 | int main(int argc, char **argv) |
37667812 VZ |
2270 | { |
2271 | if ( !wxInitialize() ) | |
2272 | { | |
2273 | fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); | |
2274 | } | |
2275 | ||
0de868d9 VZ |
2276 | #ifdef TEST_USLEEP |
2277 | puts("Sleeping for 3 seconds... z-z-z-z-z..."); | |
2278 | wxUsleep(3000); | |
2279 | #endif // TEST_USLEEP | |
2280 | ||
d34bce84 VZ |
2281 | #ifdef TEST_CMDLINE |
2282 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
2283 | { | |
2284 | { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, | |
2285 | { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" }, | |
2286 | ||
2287 | { wxCMD_LINE_OPTION, "o", "output", "output file" }, | |
2288 | { wxCMD_LINE_OPTION, "i", "input", "input dir" }, | |
2289 | { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER }, | |
1e245dc2 | 2290 | { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, |
d34bce84 VZ |
2291 | |
2292 | { wxCMD_LINE_PARAM, NULL, NULL, "input file", | |
2293 | wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, | |
2294 | ||
2295 | { wxCMD_LINE_NONE } | |
2296 | }; | |
2297 | ||
2298 | wxCmdLineParser parser(cmdLineDesc, argc, argv); | |
2299 | ||
2300 | switch ( parser.Parse() ) | |
2301 | { | |
2302 | case -1: | |
2303 | wxLogMessage("Help was given, terminating."); | |
2304 | break; | |
2305 | ||
2306 | case 0: | |
2307 | ShowCmdLine(parser); | |
2308 | break; | |
2309 | ||
2310 | default: | |
2311 | wxLogMessage("Syntax error detected, aborting."); | |
2312 | break; | |
2313 | } | |
2314 | #endif // TEST_CMDLINE | |
2315 | ||
9fc3ad34 | 2316 | #ifdef TEST_STRINGS |
299fcbfe VZ |
2317 | if ( 0 ) |
2318 | { | |
2319 | TestPChar(); | |
2320 | TestString(); | |
2321 | } | |
f0f951fa VZ |
2322 | if ( 0 ) |
2323 | { | |
7c968cee | 2324 | TestStringConstruction(); |
f0f951fa | 2325 | TestStringSub(); |
d71fa6fb | 2326 | TestStringFormat(); |
bbf8fc53 | 2327 | TestStringFind(); |
7c968cee | 2328 | TestStringTokenizer(); |
ee6e1b1d | 2329 | } |
9fc3ad34 VZ |
2330 | #endif // TEST_STRINGS |
2331 | ||
e87271f3 VZ |
2332 | #ifdef TEST_ARRAYS |
2333 | wxArrayString a1; | |
2334 | a1.Add("tiger"); | |
2335 | a1.Add("cat"); | |
2336 | a1.Add("lion"); | |
2337 | a1.Add("dog"); | |
2338 | a1.Add("human"); | |
2339 | a1.Add("ape"); | |
2340 | ||
2341 | puts("*** Initially:"); | |
2342 | ||
2343 | PrintArray("a1", a1); | |
2344 | ||
2345 | wxArrayString a2(a1); | |
2346 | PrintArray("a2", a2); | |
2347 | ||
2348 | wxSortedArrayString a3(a1); | |
2349 | PrintArray("a3", a3); | |
2350 | ||
2351 | puts("*** After deleting a string from a1"); | |
2352 | a1.Remove(2); | |
2353 | ||
2354 | PrintArray("a1", a1); | |
2355 | PrintArray("a2", a2); | |
2356 | PrintArray("a3", a3); | |
2357 | ||
2358 | puts("*** After reassigning a1 to a2 and a3"); | |
2359 | a3 = a2 = a1; | |
2360 | PrintArray("a2", a2); | |
2361 | PrintArray("a3", a3); | |
2362 | #endif // TEST_ARRAYS | |
2363 | ||
1944c6bd VZ |
2364 | #ifdef TEST_DIR |
2365 | TestDirEnum(); | |
2366 | #endif // TEST_DIR | |
2367 | ||
d93c719a VZ |
2368 | #ifdef TEST_EXECUTE |
2369 | TestExecute(); | |
2370 | #endif // TEST_EXECUTE | |
2371 | ||
ee6e1b1d VZ |
2372 | #ifdef TEST_FILECONF |
2373 | TestFileConfRead(); | |
2374 | #endif // TEST_FILECONF | |
2375 | ||
378b05f7 VZ |
2376 | #ifdef TEST_LOG |
2377 | wxString s; | |
2378 | for ( size_t n = 0; n < 8000; n++ ) | |
2379 | { | |
2380 | s << (char)('A' + (n % 26)); | |
2381 | } | |
2382 | ||
2383 | wxString msg; | |
2384 | msg.Printf("A very very long message: '%s', the end!\n", s.c_str()); | |
2385 | ||
2386 | // this one shouldn't be truncated | |
2387 | printf(msg); | |
2388 | ||
2389 | // but this one will because log functions use fixed size buffer | |
b568d04f VZ |
2390 | // (note that it doesn't need '\n' at the end neither - will be added |
2391 | // by wxLog anyhow) | |
2392 | wxLogMessage("A very very long message 2: '%s', the end!", s.c_str()); | |
378b05f7 VZ |
2393 | #endif // TEST_LOG |
2394 | ||
e87271f3 | 2395 | #ifdef TEST_THREADS |
696e1ea0 VZ |
2396 | int nCPUs = wxThread::GetCPUCount(); |
2397 | printf("This system has %d CPUs\n", nCPUs); | |
2398 | if ( nCPUs != -1 ) | |
2399 | wxThread::SetConcurrency(nCPUs); | |
ef8d96c2 | 2400 | |
9fc3ad34 VZ |
2401 | if ( argc > 1 && argv[1][0] == 't' ) |
2402 | wxLog::AddTraceMask("thread"); | |
b568d04f | 2403 | |
4c460b34 | 2404 | if ( 1 ) |
2f02cb89 | 2405 | TestDetachedThreads(); |
4c460b34 | 2406 | if ( 1 ) |
2f02cb89 | 2407 | TestJoinableThreads(); |
4c460b34 | 2408 | if ( 1 ) |
2f02cb89 VZ |
2409 | TestThreadSuspend(); |
2410 | if ( 1 ) | |
2411 | TestThreadDelete(); | |
2412 | ||
e87271f3 | 2413 | #endif // TEST_THREADS |
37667812 | 2414 | |
b76b015e | 2415 | #ifdef TEST_LONGLONG |
2a310492 VZ |
2416 | // seed pseudo random generator |
2417 | srand((unsigned)time(NULL)); | |
2418 | ||
b76b015e | 2419 | if ( 0 ) |
2a310492 | 2420 | { |
b76b015e | 2421 | TestSpeed(); |
2a310492 VZ |
2422 | } |
2423 | TestMultiplication(); | |
2424 | if ( 0 ) | |
2425 | { | |
b76b015e | 2426 | TestDivision(); |
2a310492 VZ |
2427 | TestAddition(); |
2428 | TestLongLongConversion(); | |
2429 | TestBitOperations(); | |
2430 | } | |
b76b015e VZ |
2431 | #endif // TEST_LONGLONG |
2432 | ||
2c8e4738 VZ |
2433 | #ifdef TEST_HASH |
2434 | TestHash(); | |
2435 | #endif // TEST_HASH | |
2436 | ||
696e1ea0 VZ |
2437 | #ifdef TEST_MIME |
2438 | TestMimeEnum(); | |
2439 | #endif // TEST_MIME | |
2440 | ||
2c8e4738 VZ |
2441 | #ifdef TEST_SOCKETS |
2442 | TestSocketClient(); | |
2443 | #endif // TEST_SOCKETS | |
2444 | ||
d31b7b68 VZ |
2445 | #ifdef TEST_TIMER |
2446 | TestStopWatch(); | |
2447 | #endif // TEST_TIMER | |
2448 | ||
2449 | #ifdef TEST_DATETIME | |
0de868d9 | 2450 | if ( 0 ) |
299fcbfe | 2451 | { |
9d9b7755 VZ |
2452 | TestTimeSet(); |
2453 | TestTimeStatic(); | |
2454 | TestTimeRange(); | |
2455 | TestTimeZones(); | |
2456 | TestTimeTicks(); | |
2457 | TestTimeJDN(); | |
2458 | TestTimeDST(); | |
2459 | TestTimeWDays(); | |
2460 | TestTimeWNumber(); | |
2461 | TestTimeParse(); | |
2462 | TestTimeFormat(); | |
2463 | TestTimeArithmetics(); | |
41acf5c0 | 2464 | } |
0de868d9 | 2465 | TestTimeHolidays(); |
9d9b7755 VZ |
2466 | if ( 0 ) |
2467 | TestInteractive(); | |
d31b7b68 | 2468 | #endif // TEST_DATETIME |
b76b015e | 2469 | |
37667812 VZ |
2470 | wxUninitialize(); |
2471 | ||
2472 | return 0; | |
2473 | } |