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