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