]>
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 VZ |
31 | |
32 | //#define TEST_ARRAYS | |
211c2250 | 33 | //#define TEST_DIR |
b568d04f | 34 | //#define TEST_LOG |
1944c6bd | 35 | //#define TEST_MIME |
9fc3ad34 | 36 | //#define TEST_STRINGS |
696e1ea0 | 37 | //#define TEST_THREADS |
211c2250 | 38 | #define TEST_TIME |
9fc3ad34 | 39 | //#define TEST_LONGLONG |
e87271f3 VZ |
40 | |
41 | // ============================================================================ | |
42 | // implementation | |
43 | // ============================================================================ | |
44 | ||
1944c6bd VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // wxDir | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | #ifdef TEST_DIR | |
50 | ||
51 | #include <wx/dir.h> | |
52 | ||
53 | static void TestDirEnumHelper(wxDir& dir, | |
54 | int flags = wxDIR_DEFAULT, | |
55 | const wxString& filespec = wxEmptyString) | |
56 | { | |
57 | wxString filename; | |
58 | ||
59 | if ( !dir.IsOpened() ) | |
60 | return; | |
61 | ||
62 | bool cont = dir.GetFirst(&filename, filespec, flags); | |
63 | while ( cont ) | |
64 | { | |
65 | printf("\t%s\n", filename.c_str()); | |
66 | ||
67 | cont = dir.GetNext(&filename); | |
68 | } | |
69 | ||
70 | puts(""); | |
71 | } | |
72 | ||
73 | static void TestDirEnum() | |
74 | { | |
75 | wxDir dir(wxGetCwd()); | |
76 | ||
77 | puts("Enumerating everything in current directory:"); | |
78 | TestDirEnumHelper(dir); | |
79 | ||
80 | puts("Enumerating really everything in current directory:"); | |
81 | TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT); | |
82 | ||
83 | puts("Enumerating object files in current directory:"); | |
84 | TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o"); | |
85 | ||
86 | puts("Enumerating directories in current directory:"); | |
87 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
88 | ||
89 | puts("Enumerating files in current directory:"); | |
90 | TestDirEnumHelper(dir, wxDIR_FILES); | |
91 | ||
92 | puts("Enumerating files including hidden in current directory:"); | |
93 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
94 | ||
95 | #ifdef __UNIX__ | |
96 | dir.Open("/"); | |
97 | #elif defined(__WXMSW__) | |
98 | dir.Open("c:\\"); | |
99 | #else | |
100 | #error "don't know where the root directory is" | |
101 | #endif | |
102 | ||
103 | puts("Enumerating everything in root directory:"); | |
104 | TestDirEnumHelper(dir, wxDIR_DEFAULT); | |
105 | ||
106 | puts("Enumerating directories in root directory:"); | |
107 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
108 | ||
109 | puts("Enumerating files in root directory:"); | |
110 | TestDirEnumHelper(dir, wxDIR_FILES); | |
111 | ||
112 | puts("Enumerating files including hidden in root directory:"); | |
113 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
114 | ||
115 | puts("Enumerating files in non existing directory:"); | |
116 | wxDir dirNo("nosuchdir"); | |
117 | TestDirEnumHelper(dirNo); | |
118 | } | |
119 | ||
120 | #endif // TEST_DIR | |
121 | ||
696e1ea0 VZ |
122 | // ---------------------------------------------------------------------------- |
123 | // MIME types | |
124 | // ---------------------------------------------------------------------------- | |
125 | ||
126 | #ifdef TEST_MIME | |
127 | ||
128 | #include <wx/mimetype.h> | |
129 | ||
130 | static void TestMimeEnum() | |
131 | { | |
132 | wxMimeTypesManager mimeTM; | |
133 | wxArrayString mimetypes; | |
134 | ||
135 | size_t count = mimeTM.EnumAllFileTypes(mimetypes); | |
136 | ||
137 | printf("*** All %u known filetypes: ***\n", count); | |
138 | ||
139 | wxArrayString exts; | |
140 | wxString desc; | |
141 | ||
142 | for ( size_t n = 0; n < count; n++ ) | |
143 | { | |
144 | wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]); | |
145 | if ( !filetype ) | |
c61f4f6d | 146 | { |
97e0ceea VZ |
147 | printf("nothing known about the filetype '%s'!\n", |
148 | mimetypes[n].c_str()); | |
696e1ea0 | 149 | continue; |
c61f4f6d VZ |
150 | } |
151 | ||
696e1ea0 VZ |
152 | filetype->GetDescription(&desc); |
153 | filetype->GetExtensions(exts); | |
154 | ||
299fcbfe VZ |
155 | filetype->GetIcon(NULL); |
156 | ||
696e1ea0 VZ |
157 | wxString extsAll; |
158 | for ( size_t e = 0; e < exts.GetCount(); e++ ) | |
159 | { | |
160 | if ( e > 0 ) | |
161 | extsAll << _T(", "); | |
162 | extsAll += exts[e]; | |
163 | } | |
164 | ||
54acce90 VZ |
165 | printf("\t%s: %s (%s)\n", |
166 | mimetypes[n].c_str(), desc.c_str(), extsAll.c_str()); | |
696e1ea0 VZ |
167 | } |
168 | } | |
169 | ||
170 | #endif // TEST_MIME | |
171 | ||
b76b015e VZ |
172 | // ---------------------------------------------------------------------------- |
173 | // long long | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | #ifdef TEST_LONGLONG | |
177 | ||
178 | #include <wx/longlong.h> | |
179 | #include <wx/timer.h> | |
180 | ||
181 | static void TestSpeed() | |
182 | { | |
183 | static const long max = 100000000; | |
184 | long n; | |
9fc3ad34 | 185 | |
b76b015e VZ |
186 | { |
187 | wxStopWatch sw; | |
188 | ||
189 | long l = 0; | |
190 | for ( n = 0; n < max; n++ ) | |
191 | { | |
192 | l += n; | |
193 | } | |
194 | ||
195 | printf("Summing longs took %ld milliseconds.\n", sw.Time()); | |
196 | } | |
197 | ||
198 | { | |
199 | wxStopWatch sw; | |
200 | ||
201 | __int64 l = 0; | |
202 | for ( n = 0; n < max; n++ ) | |
203 | { | |
204 | l += n; | |
205 | } | |
206 | ||
207 | printf("Summing __int64s took %ld milliseconds.\n", sw.Time()); | |
208 | } | |
209 | ||
210 | { | |
211 | wxStopWatch sw; | |
212 | ||
213 | wxLongLong l; | |
214 | for ( n = 0; n < max; n++ ) | |
215 | { | |
216 | l += n; | |
217 | } | |
218 | ||
219 | printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time()); | |
220 | } | |
221 | } | |
222 | ||
223 | static void TestDivision() | |
224 | { | |
2f02cb89 | 225 | #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3) |
b76b015e | 226 | |
2f02cb89 VZ |
227 | // seed pseudo random generator |
228 | //srand((unsigned)time(NULL)); | |
229 | ||
230 | size_t nTested = 0; | |
231 | for ( size_t n = 0; n < 10000; n++ ) | |
232 | { | |
233 | // get a random wxLongLong (shifting by 12 the MSB ensures that the | |
234 | // multiplication will not overflow) | |
235 | wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand()); | |
236 | ||
237 | wxASSERT( (ll * 1000l)/1000l == ll ); | |
238 | ||
239 | nTested++; | |
240 | } | |
241 | ||
242 | printf("\n*** Tested %u divisions/multiplications: ok\n", nTested); | |
243 | ||
244 | #undef MAKE_LL | |
b76b015e VZ |
245 | } |
246 | ||
247 | #endif // TEST_LONGLONG | |
248 | ||
249 | // ---------------------------------------------------------------------------- | |
250 | // date time | |
251 | // ---------------------------------------------------------------------------- | |
252 | ||
253 | #ifdef TEST_TIME | |
254 | ||
97e0ceea VZ |
255 | #include <wx/date.h> |
256 | ||
b76b015e VZ |
257 | #include <wx/datetime.h> |
258 | ||
299fcbfe VZ |
259 | // the test data |
260 | struct Date | |
261 | { | |
262 | wxDateTime::wxDateTime_t day; | |
263 | wxDateTime::Month month; | |
264 | int year; | |
265 | wxDateTime::wxDateTime_t hour, min, sec; | |
266 | double jdn; | |
211c2250 | 267 | wxDateTime::WeekDay wday; |
299fcbfe VZ |
268 | time_t gmticks, ticks; |
269 | ||
270 | void Init(const wxDateTime::Tm& tm) | |
271 | { | |
272 | day = tm.mday; | |
273 | month = tm.mon; | |
274 | year = tm.year; | |
275 | hour = tm.hour; | |
276 | min = tm.min; | |
277 | sec = tm.sec; | |
278 | jdn = 0.0; | |
279 | gmticks = ticks = -1; | |
280 | } | |
281 | ||
282 | wxDateTime DT() const | |
283 | { return wxDateTime(day, month, year, hour, min, sec); } | |
284 | ||
239446b4 VZ |
285 | bool SameDay(const wxDateTime::Tm& tm) const |
286 | { | |
287 | return day == tm.mday && month == tm.mon && year == tm.year; | |
288 | } | |
289 | ||
299fcbfe VZ |
290 | wxString Format() const |
291 | { | |
292 | wxString s; | |
293 | s.Printf("%02d:%02d:%02d %10s %02d, %4d%s", | |
294 | hour, min, sec, | |
295 | wxDateTime::GetMonthName(month).c_str(), | |
296 | day, | |
297 | abs(wxDateTime::ConvertYearToBC(year)), | |
298 | year > 0 ? "AD" : "BC"); | |
299 | return s; | |
300 | } | |
239446b4 VZ |
301 | |
302 | wxString FormatDate() const | |
303 | { | |
304 | wxString s; | |
305 | s.Printf("%02d-%s-%4d%s", | |
306 | day, | |
307 | wxDateTime::GetMonthName(month, TRUE).c_str(), | |
308 | abs(wxDateTime::ConvertYearToBC(year)), | |
309 | year > 0 ? "AD" : "BC"); | |
310 | return s; | |
311 | } | |
299fcbfe VZ |
312 | }; |
313 | ||
314 | static const Date testDates[] = | |
315 | { | |
211c2250 VZ |
316 | { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 }, |
317 | { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 }, | |
318 | { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 }, | |
319 | { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 }, | |
320 | { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 }, | |
321 | { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 }, | |
322 | { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 }, | |
323 | { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 }, | |
324 | { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 }, | |
325 | { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 }, | |
239446b4 VZ |
326 | { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 }, |
327 | { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 }, | |
328 | { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 }, | |
329 | { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 }, | |
211c2250 | 330 | { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 }, |
299fcbfe VZ |
331 | }; |
332 | ||
2f02cb89 VZ |
333 | // this test miscellaneous static wxDateTime functions |
334 | static void TestTimeStatic() | |
335 | { | |
336 | puts("\n*** wxDateTime static methods test ***"); | |
337 | ||
338 | // some info about the current date | |
339 | int year = wxDateTime::GetCurrentYear(); | |
340 | printf("Current year %d is %sa leap one and has %d days.\n", | |
341 | year, | |
342 | wxDateTime::IsLeapYear(year) ? "" : "not ", | |
343 | wxDateTime::GetNumberOfDays(year)); | |
344 | ||
345 | wxDateTime::Month month = wxDateTime::GetCurrentMonth(); | |
346 | printf("Current month is '%s' ('%s') and it has %d days\n", | |
347 | wxDateTime::GetMonthName(month, TRUE).c_str(), | |
348 | wxDateTime::GetMonthName(month).c_str(), | |
349 | wxDateTime::GetNumberOfDays(month)); | |
350 | ||
351 | // leap year logic | |
fcc3d7cb VZ |
352 | static const size_t nYears = 5; |
353 | static const size_t years[2][nYears] = | |
2f02cb89 VZ |
354 | { |
355 | // first line: the years to test | |
356 | { 1990, 1976, 2000, 2030, 1984, }, | |
357 | ||
358 | // second line: TRUE if leap, FALSE otherwise | |
359 | { FALSE, TRUE, TRUE, FALSE, TRUE } | |
360 | }; | |
361 | ||
362 | for ( size_t n = 0; n < nYears; n++ ) | |
363 | { | |
364 | int year = years[0][n]; | |
239446b4 VZ |
365 | bool should = years[1][n] != 0, |
366 | is = wxDateTime::IsLeapYear(year); | |
2f02cb89 | 367 | |
239446b4 | 368 | printf("Year %d is %sa leap year (%s)\n", |
2f02cb89 | 369 | year, |
239446b4 VZ |
370 | is ? "" : "not ", |
371 | should == is ? "ok" : "ERROR"); | |
2f02cb89 VZ |
372 | |
373 | wxASSERT( should == wxDateTime::IsLeapYear(year) ); | |
374 | } | |
375 | } | |
376 | ||
377 | // test constructing wxDateTime objects | |
378 | static void TestTimeSet() | |
379 | { | |
380 | puts("\n*** wxDateTime construction test ***"); | |
381 | ||
299fcbfe VZ |
382 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
383 | { | |
384 | const Date& d1 = testDates[n]; | |
385 | wxDateTime dt = d1.DT(); | |
386 | ||
387 | Date d2; | |
388 | d2.Init(dt.GetTm()); | |
389 | ||
390 | wxString s1 = d1.Format(), | |
391 | s2 = d2.Format(); | |
392 | ||
393 | printf("Date: %s == %s (%s)\n", | |
394 | s1.c_str(), s2.c_str(), | |
395 | s1 == s2 ? "ok" : "ERROR"); | |
396 | } | |
2f02cb89 VZ |
397 | } |
398 | ||
fcc3d7cb VZ |
399 | // test time zones stuff |
400 | static void TestTimeZones() | |
401 | { | |
402 | puts("\n*** wxDateTime timezone test ***"); | |
403 | ||
404 | wxDateTime now = wxDateTime::Now(); | |
405 | ||
299fcbfe VZ |
406 | printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str()); |
407 | printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str()); | |
408 | printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str()); | |
409 | printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str()); | |
410 | printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str()); | |
411 | printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str()); | |
fcc3d7cb VZ |
412 | } |
413 | ||
e6ec579c VZ |
414 | // test some minimal support for the dates outside the standard range |
415 | static void TestTimeRange() | |
416 | { | |
417 | puts("\n*** wxDateTime out-of-standard-range dates test ***"); | |
418 | ||
211c2250 VZ |
419 | static const char *fmt = "%d-%b-%Y %H:%M:%S"; |
420 | ||
1ef54dcf | 421 | printf("Unix epoch:\t%s\n", |
211c2250 | 422 | wxDateTime(2440587.5).Format(fmt).c_str()); |
1ef54dcf | 423 | printf("Feb 29, 0: \t%s\n", |
211c2250 | 424 | wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str()); |
e6ec579c | 425 | printf("JDN 0: \t%s\n", |
211c2250 | 426 | wxDateTime(0.0).Format(fmt).c_str()); |
e6ec579c | 427 | printf("Jan 1, 1AD:\t%s\n", |
211c2250 | 428 | wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str()); |
e6ec579c | 429 | printf("May 29, 2099:\t%s\n", |
211c2250 | 430 | wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str()); |
e6ec579c VZ |
431 | } |
432 | ||
299fcbfe | 433 | static void TestTimeTicks() |
e6ec579c | 434 | { |
299fcbfe | 435 | puts("\n*** wxDateTime ticks test ***"); |
e6ec579c | 436 | |
299fcbfe | 437 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
1ef54dcf | 438 | { |
299fcbfe VZ |
439 | const Date& d = testDates[n]; |
440 | if ( d.ticks == -1 ) | |
441 | continue; | |
1ef54dcf | 442 | |
299fcbfe VZ |
443 | wxDateTime dt = d.DT(); |
444 | long ticks = (dt.GetValue() / 1000).ToLong(); | |
445 | printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks); | |
446 | if ( ticks == d.ticks ) | |
447 | { | |
448 | puts(" (ok)"); | |
449 | } | |
450 | else | |
451 | { | |
452 | printf(" (ERROR: should be %ld, delta = %ld)\n", | |
453 | d.ticks, ticks - d.ticks); | |
454 | } | |
455 | ||
456 | dt = d.DT().ToTimezone(wxDateTime::GMT0); | |
457 | ticks = (dt.GetValue() / 1000).ToLong(); | |
458 | printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks); | |
459 | if ( ticks == d.gmticks ) | |
460 | { | |
461 | puts(" (ok)"); | |
462 | } | |
463 | else | |
464 | { | |
465 | printf(" (ERROR: should be %ld, delta = %ld)\n", | |
466 | d.gmticks, ticks - d.gmticks); | |
467 | } | |
468 | } | |
469 | ||
470 | puts(""); | |
471 | } | |
472 | ||
473 | // test conversions to JDN &c | |
474 | static void TestTimeJDN() | |
475 | { | |
476 | puts("\n*** wxDateTime to JDN test ***"); | |
1ef54dcf VZ |
477 | |
478 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) | |
479 | { | |
480 | const Date& d = testDates[n]; | |
299fcbfe | 481 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); |
1ef54dcf VZ |
482 | double jdn = dt.GetJulianDayNumber(); |
483 | ||
299fcbfe | 484 | printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn); |
1ef54dcf VZ |
485 | if ( jdn == d.jdn ) |
486 | { | |
487 | puts(" (ok)"); | |
488 | } | |
489 | else | |
490 | { | |
491 | printf(" (ERROR: should be %f, delta = %f)\n", | |
492 | d.jdn, jdn - d.jdn); | |
493 | } | |
494 | } | |
e6ec579c VZ |
495 | } |
496 | ||
211c2250 VZ |
497 | // test week days computation |
498 | static void TestTimeWDays() | |
499 | { | |
500 | puts("\n*** wxDateTime weekday test ***"); | |
501 | ||
239446b4 VZ |
502 | // test GetWeekDay() |
503 | size_t n; | |
504 | for ( n = 0; n < WXSIZEOF(testDates); n++ ) | |
211c2250 VZ |
505 | { |
506 | const Date& d = testDates[n]; | |
507 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); | |
508 | ||
509 | wxDateTime::WeekDay wday = dt.GetWeekDay(); | |
510 | printf("%s is: %s", | |
511 | d.Format().c_str(), | |
239446b4 | 512 | wxDateTime::GetWeekDayName(wday).c_str()); |
211c2250 VZ |
513 | if ( wday == d.wday ) |
514 | { | |
515 | puts(" (ok)"); | |
516 | } | |
517 | else | |
518 | { | |
519 | printf(" (ERROR: should be %s)\n", | |
239446b4 VZ |
520 | wxDateTime::GetWeekDayName(d.wday).c_str()); |
521 | } | |
522 | } | |
523 | ||
524 | puts(""); | |
525 | ||
526 | // test SetToWeekDay() | |
527 | struct WeekDateTestData | |
528 | { | |
529 | Date date; // the real date (precomputed) | |
530 | int nWeek; // its week index in the month | |
531 | wxDateTime::WeekDay wday; // the weekday | |
532 | wxDateTime::Month month; // the month | |
533 | int year; // and the year | |
534 | ||
535 | wxString Format() const | |
536 | { | |
537 | wxString s, which; | |
538 | switch ( nWeek < -1 ? -nWeek : nWeek ) | |
539 | { | |
540 | case 1: which = "first"; break; | |
541 | case 2: which = "second"; break; | |
542 | case 3: which = "third"; break; | |
543 | case 4: which = "fourth"; break; | |
544 | case 5: which = "fifth"; break; | |
545 | ||
546 | case -1: which = "last"; break; | |
547 | } | |
548 | ||
549 | if ( nWeek < -1 ) | |
550 | { | |
551 | which += " from end"; | |
552 | } | |
553 | ||
554 | s.Printf("The %s %s of %s in %d", | |
555 | which.c_str(), | |
556 | wxDateTime::GetWeekDayName(wday).c_str(), | |
557 | wxDateTime::GetMonthName(month).c_str(), | |
558 | year); | |
559 | ||
560 | return s; | |
561 | } | |
562 | }; | |
563 | ||
564 | // the array data was generated by the following python program | |
565 | /* | |
566 | from DateTime import * | |
567 | from whrandom import * | |
568 | from string import * | |
569 | ||
570 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
571 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
572 | ||
573 | week = DateTimeDelta(7) | |
574 | ||
575 | for n in range(20): | |
576 | year = randint(1900, 2100) | |
577 | month = randint(1, 12) | |
578 | day = randint(1, 28) | |
579 | dt = DateTime(year, month, day) | |
580 | wday = dt.day_of_week | |
581 | ||
582 | countFromEnd = choice([-1, 1]) | |
583 | weekNum = 0; | |
584 | ||
585 | while dt.month is month: | |
586 | dt = dt - countFromEnd * week | |
587 | weekNum = weekNum + countFromEnd | |
588 | ||
589 | data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] } | |
97e0ceea | 590 | |
239446b4 VZ |
591 | print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\ |
592 | "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data | |
97e0ceea | 593 | */ |
239446b4 VZ |
594 | |
595 | static const WeekDateTestData weekDatesTestData[] = | |
596 | { | |
597 | { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 }, | |
598 | { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 }, | |
599 | { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 }, | |
600 | { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 }, | |
601 | { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 }, | |
602 | { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 }, | |
603 | { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 }, | |
604 | { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 }, | |
605 | { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 }, | |
606 | { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 }, | |
607 | { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 }, | |
608 | { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 }, | |
609 | { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 }, | |
610 | { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 }, | |
611 | { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 }, | |
612 | { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 }, | |
613 | { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 }, | |
614 | { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 }, | |
615 | { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 }, | |
616 | { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }, | |
617 | }; | |
618 | ||
619 | static const char *fmt = "%d-%b-%Y"; | |
620 | ||
621 | wxDateTime dt; | |
622 | for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ ) | |
623 | { | |
624 | const WeekDateTestData& wd = weekDatesTestData[n]; | |
625 | ||
626 | dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year); | |
627 | ||
628 | printf("%s is %s", wd.Format().c_str(), dt.Format(fmt).c_str()); | |
629 | ||
630 | const Date& d = wd.date; | |
631 | if ( d.SameDay(dt.GetTm()) ) | |
632 | { | |
633 | puts(" (ok)"); | |
634 | } | |
635 | else | |
636 | { | |
637 | dt.Set(d.day, d.month, d.year); | |
638 | ||
639 | printf(" (ERROR: should be %s)\n", dt.Format(fmt).c_str()); | |
211c2250 VZ |
640 | } |
641 | } | |
642 | } | |
643 | ||
239446b4 VZ |
644 | // test the computation of (ISO) week numbers |
645 | static void TestTimeWNumber() | |
646 | { | |
647 | puts("\n*** wxDateTime week number test ***"); | |
648 | ||
649 | struct WeekNumberTestData | |
650 | { | |
651 | Date date; // the date | |
652 | wxDateTime::wxDateTime_t week; // the week number | |
653 | wxDateTime::wxDateTime_t dnum; // day number in the year | |
654 | }; | |
655 | ||
656 | // data generated with the following python script: | |
657 | /* | |
658 | from DateTime import * | |
659 | from whrandom import * | |
660 | from string import * | |
661 | ||
662 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
663 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
664 | ||
665 | for n in range(20): | |
666 | year = randint(1900, 2100) | |
667 | month = randint(1, 12) | |
668 | day = randint(1, 28) | |
669 | dt = DateTime(year, month, day) | |
670 | dayNum = dt.day_of_year | |
671 | weekNum = dt.iso_week[1] | |
672 | ||
673 | data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) } | |
97e0ceea | 674 | |
239446b4 VZ |
675 | print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\ |
676 | "%(dayNum)s }," % data | |
677 | */ | |
678 | static const WeekNumberTestData weekNumberTestDates[] = | |
679 | { | |
680 | { { 2, wxDateTime::Jul, 2093 }, 27, 183 }, | |
681 | { { 25, wxDateTime::Jun, 1986 }, 26, 176 }, | |
682 | { { 15, wxDateTime::Jun, 2014 }, 24, 166 }, | |
683 | { { 20, wxDateTime::Jul, 2018 }, 29, 201 }, | |
684 | { { 3, wxDateTime::Aug, 2074 }, 31, 215 }, | |
685 | { { 26, wxDateTime::Jul, 2012 }, 30, 208 }, | |
686 | { { 4, wxDateTime::Nov, 1915 }, 44, 308 }, | |
687 | { { 11, wxDateTime::Feb, 2035 }, 6, 42 }, | |
688 | { { 15, wxDateTime::Feb, 1942 }, 7, 46 }, | |
689 | { { 5, wxDateTime::Jan, 2087 }, 1, 5 }, | |
690 | { { 6, wxDateTime::Nov, 2016 }, 44, 311 }, | |
691 | { { 6, wxDateTime::Jun, 2057 }, 23, 157 }, | |
692 | { { 25, wxDateTime::Feb, 1976 }, 9, 56 }, | |
693 | { { 12, wxDateTime::Jan, 2073 }, 2, 12 }, | |
694 | { { 12, wxDateTime::Sep, 2040 }, 37, 256 }, | |
695 | { { 15, wxDateTime::Jul, 1931 }, 29, 196 }, | |
696 | { { 23, wxDateTime::Mar, 2084 }, 12, 83 }, | |
697 | { { 12, wxDateTime::Dec, 1970 }, 50, 346 }, | |
698 | { { 6, wxDateTime::Sep, 1996 }, 36, 250 }, | |
699 | { { 7, wxDateTime::Jan, 2076 }, 2, 7 }, | |
700 | }; | |
701 | ||
702 | for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ ) | |
703 | { | |
704 | const WeekNumberTestData& wn = weekNumberTestDates[n]; | |
705 | const Date& d = wn.date; | |
706 | ||
707 | wxDateTime dt = d.DT(); | |
708 | ||
709 | wxDateTime::wxDateTime_t week = dt.GetWeekOfYear(), | |
710 | dnum = dt.GetDayOfYear(); | |
711 | ||
712 | printf("%s: the day number is %d", | |
713 | d.FormatDate().c_str(), dnum); | |
714 | if ( dnum == wn.dnum ) | |
715 | { | |
716 | printf(" (ok)"); | |
717 | } | |
718 | else | |
719 | { | |
720 | printf(" (ERROR: should be %d)", wn.dnum); | |
721 | } | |
722 | ||
723 | printf(", week number is %d", week); | |
724 | if ( week == wn.week ) | |
725 | { | |
726 | puts(" (ok)"); | |
727 | } | |
728 | else | |
729 | { | |
730 | printf(" (ERROR: should be %d)\n", wn.week); | |
731 | } | |
732 | } | |
733 | } | |
734 | ||
735 | // test DST calculations | |
736 | static void TestTimeDST() | |
737 | { | |
738 | puts("\n*** wxDateTime DST test ***"); | |
739 | ||
740 | printf("DST is%s in effect now.\n\n", | |
741 | wxDateTime::Now().IsDST() ? "" : " not"); | |
742 | ||
743 | // taken from http://www.energy.ca.gov/daylightsaving.html | |
744 | static const Date datesDST[2][2004 - 1900 + 1] = | |
745 | { | |
746 | { | |
747 | { 1, wxDateTime::Apr, 1990 }, | |
748 | { 7, wxDateTime::Apr, 1991 }, | |
749 | { 5, wxDateTime::Apr, 1992 }, | |
750 | { 4, wxDateTime::Apr, 1993 }, | |
751 | { 3, wxDateTime::Apr, 1994 }, | |
752 | { 2, wxDateTime::Apr, 1995 }, | |
753 | { 7, wxDateTime::Apr, 1996 }, | |
754 | { 6, wxDateTime::Apr, 1997 }, | |
755 | { 5, wxDateTime::Apr, 1998 }, | |
756 | { 4, wxDateTime::Apr, 1999 }, | |
757 | { 2, wxDateTime::Apr, 2000 }, | |
758 | { 1, wxDateTime::Apr, 2001 }, | |
759 | { 7, wxDateTime::Apr, 2002 }, | |
760 | { 6, wxDateTime::Apr, 2003 }, | |
761 | { 4, wxDateTime::Apr, 2004 }, | |
762 | }, | |
763 | { | |
764 | { 28, wxDateTime::Oct, 1990 }, | |
765 | { 27, wxDateTime::Oct, 1991 }, | |
766 | { 25, wxDateTime::Oct, 1992 }, | |
767 | { 31, wxDateTime::Oct, 1993 }, | |
768 | { 30, wxDateTime::Oct, 1994 }, | |
769 | { 29, wxDateTime::Oct, 1995 }, | |
770 | { 27, wxDateTime::Oct, 1996 }, | |
771 | { 26, wxDateTime::Oct, 1997 }, | |
772 | { 25, wxDateTime::Oct, 1998 }, | |
773 | { 31, wxDateTime::Oct, 1999 }, | |
774 | { 29, wxDateTime::Oct, 2000 }, | |
775 | { 28, wxDateTime::Oct, 2001 }, | |
776 | { 27, wxDateTime::Oct, 2002 }, | |
777 | { 26, wxDateTime::Oct, 2003 }, | |
778 | { 31, wxDateTime::Oct, 2004 }, | |
779 | } | |
780 | }; | |
781 | ||
782 | int year; | |
783 | for ( year = 1990; year < 2005; year++ ) | |
784 | { | |
785 | wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA), | |
786 | dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA); | |
787 | ||
788 | printf("DST period in the US for year %d: from %s to %s", | |
789 | year, dtBegin.Format().c_str(), dtEnd.Format().c_str()); | |
790 | ||
791 | size_t n = year - 1990; | |
792 | const Date& dBegin = datesDST[0][n]; | |
793 | const Date& dEnd = datesDST[1][n]; | |
97e0ceea | 794 | |
239446b4 VZ |
795 | if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) ) |
796 | { | |
797 | puts(" (ok)"); | |
798 | } | |
799 | else | |
800 | { | |
801 | printf(" (ERROR: should be %s %d to %s %d)\n", | |
802 | wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day, | |
803 | wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day); | |
804 | } | |
805 | } | |
806 | ||
807 | puts(""); | |
808 | ||
809 | for ( year = 1990; year < 2005; year++ ) | |
810 | { | |
811 | printf("DST period in Europe for year %d: from %s to %s\n", | |
812 | year, | |
813 | wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(), | |
814 | wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str()); | |
815 | } | |
816 | } | |
817 | ||
68ee7c47 VZ |
818 | // test wxDateTime -> text conversion |
819 | static void TestTimeFormat() | |
820 | { | |
821 | puts("\n*** wxDateTime formatting test ***"); | |
822 | ||
823 | static const char *formatTestFormats[] = | |
824 | { | |
825 | "%c", | |
826 | "Date is %A, %d of %B, in year %Y", | |
827 | "Date is %x, time is %X", | |
828 | "Time is %H:%M:%S or %I:%M:%S %p", | |
829 | "The day of year: %j, the week of year: %W", | |
830 | }; | |
831 | ||
832 | static const Date formatTestDates[] = | |
833 | { | |
834 | { }, // unused | |
835 | { 29, wxDateTime::May, 1976, 18, 30, 00 }, | |
836 | { 31, wxDateTime::Dec, 1999, 23, 30, 00 }, | |
837 | { 29, wxDateTime::May, 2076, 18, 30, 00 }, | |
838 | { 29, wxDateTime::Feb, 2400, 02, 15, 25 }, | |
839 | { 01, wxDateTime::Jan, -52, 03, 16, 47 }, | |
840 | }; | |
841 | ||
842 | // an extra test (as it doesn't depend on date, don't do it in the loop) | |
843 | printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str()); | |
844 | ||
845 | for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ ) | |
846 | { | |
847 | puts(""); | |
848 | ||
849 | wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d].DT(); | |
850 | for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) | |
851 | { | |
852 | printf("%s\n", dt.Format(formatTestFormats[n]).c_str()); | |
853 | } | |
854 | } | |
855 | } | |
856 | ||
97e0ceea VZ |
857 | // test text -> wxDateTime conversion |
858 | static void TestTimeParse() | |
859 | { | |
860 | puts("\n*** wxDateTime parse test ***"); | |
861 | ||
862 | struct ParseTestData | |
863 | { | |
864 | const char *format; | |
865 | Date date; | |
866 | bool good; | |
867 | }; | |
868 | ||
869 | static const ParseTestData parseTestDates[] = | |
870 | { | |
68ee7c47 VZ |
871 | { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE }, |
872 | { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE }, | |
97e0ceea VZ |
873 | }; |
874 | ||
875 | for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) | |
876 | { | |
877 | const char *format = parseTestDates[n].format; | |
878 | ||
879 | printf("%s => ", format); | |
880 | ||
881 | wxDateTime dt; | |
882 | if ( dt.ParseRfc822Date(format) ) | |
883 | { | |
884 | printf("%s ", dt.Format().c_str()); | |
885 | ||
886 | if ( parseTestDates[n].good ) | |
887 | { | |
888 | wxDateTime dtReal = parseTestDates[n].date.DT(); | |
889 | if ( dt == dtReal ) | |
890 | { | |
891 | puts("(ok)"); | |
892 | } | |
893 | else | |
894 | { | |
895 | printf("(ERROR: should be %s)\n", dtReal.Format().c_str()); | |
896 | } | |
897 | } | |
898 | else | |
899 | { | |
900 | puts("(ERROR: bad format)"); | |
901 | } | |
902 | } | |
903 | else | |
904 | { | |
905 | printf("bad format (%s)\n", | |
906 | parseTestDates[n].good ? "ERROR" : "ok"); | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
68ee7c47 VZ |
911 | #if 0 |
912 | ||
97e0ceea VZ |
913 | // test compatibility with the old wxDate/wxTime classes |
914 | static void TestTimeCompatibility() | |
915 | { | |
916 | puts("\n*** wxDateTime compatibility test ***"); | |
917 | ||
918 | printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str()); | |
919 | printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str()); | |
920 | ||
921 | double jdnNow = wxDateTime::Now().GetJDN(); | |
922 | long jdnMidnight = (long)(jdnNow - 0.5); | |
923 | printf("wxDate for today: %s\n", wxDate(jdnMidnight).FormatDate().c_str()); | |
924 | ||
925 | jdnMidnight = wxDate().Set().GetJulianDate(); | |
926 | printf("wxDateTime for today: %s\n", | |
927 | wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str()); | |
928 | ||
929 | int flags = wxEUROPEAN;//wxFULL; | |
930 | wxDate date; | |
931 | date.Set(); | |
932 | printf("Today is %s\n", date.FormatDate(flags).c_str()); | |
933 | for ( int n = 0; n < 7; n++ ) | |
934 | { | |
935 | printf("Previous %s is %s\n", | |
936 | wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n), | |
937 | date.Previous(n + 1).FormatDate(flags).c_str()); | |
938 | } | |
939 | } | |
940 | ||
68ee7c47 VZ |
941 | #endif // 0 |
942 | ||
b76b015e VZ |
943 | #endif // TEST_TIME |
944 | ||
e87271f3 VZ |
945 | // ---------------------------------------------------------------------------- |
946 | // threads | |
947 | // ---------------------------------------------------------------------------- | |
948 | ||
949 | #ifdef TEST_THREADS | |
950 | ||
bbfa0322 | 951 | #include <wx/thread.h> |
37667812 | 952 | |
bbfa0322 VZ |
953 | static size_t gs_counter = (size_t)-1; |
954 | static wxCriticalSection gs_critsect; | |
b568d04f | 955 | static wxCondition gs_cond; |
bbfa0322 | 956 | |
b568d04f | 957 | class MyJoinableThread : public wxThread |
bbfa0322 VZ |
958 | { |
959 | public: | |
b568d04f VZ |
960 | MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE) |
961 | { m_n = n; Create(); } | |
bbfa0322 VZ |
962 | |
963 | // thread execution starts here | |
b568d04f | 964 | virtual ExitCode Entry(); |
bbfa0322 | 965 | |
b568d04f VZ |
966 | private: |
967 | size_t m_n; | |
bbfa0322 VZ |
968 | }; |
969 | ||
b568d04f | 970 | wxThread::ExitCode MyJoinableThread::Entry() |
bbfa0322 | 971 | { |
b568d04f VZ |
972 | unsigned long res = 1; |
973 | for ( size_t n = 1; n < m_n; n++ ) | |
974 | { | |
975 | res *= n; | |
976 | ||
977 | // it's a loooong calculation :-) | |
978 | Sleep(100); | |
979 | } | |
bbfa0322 | 980 | |
b568d04f | 981 | return (ExitCode)res; |
bbfa0322 VZ |
982 | } |
983 | ||
b568d04f VZ |
984 | class MyDetachedThread : public wxThread |
985 | { | |
986 | public: | |
fcc3d7cb VZ |
987 | MyDetachedThread(size_t n, char ch) |
988 | { | |
989 | m_n = n; | |
990 | m_ch = ch; | |
991 | m_cancelled = FALSE; | |
992 | ||
993 | Create(); | |
994 | } | |
b568d04f VZ |
995 | |
996 | // thread execution starts here | |
997 | virtual ExitCode Entry(); | |
998 | ||
999 | // and stops here | |
1000 | virtual void OnExit(); | |
1001 | ||
1002 | private: | |
9fc3ad34 VZ |
1003 | size_t m_n; // number of characters to write |
1004 | char m_ch; // character to write | |
fcc3d7cb VZ |
1005 | |
1006 | bool m_cancelled; // FALSE if we exit normally | |
b568d04f VZ |
1007 | }; |
1008 | ||
1009 | wxThread::ExitCode MyDetachedThread::Entry() | |
bbfa0322 VZ |
1010 | { |
1011 | { | |
1012 | wxCriticalSectionLocker lock(gs_critsect); | |
1013 | if ( gs_counter == (size_t)-1 ) | |
1014 | gs_counter = 1; | |
1015 | else | |
1016 | gs_counter++; | |
1017 | } | |
1018 | ||
9fc3ad34 | 1019 | for ( size_t n = 0; n < m_n; n++ ) |
bbfa0322 VZ |
1020 | { |
1021 | if ( TestDestroy() ) | |
fcc3d7cb VZ |
1022 | { |
1023 | m_cancelled = TRUE; | |
1024 | ||
bbfa0322 | 1025 | break; |
fcc3d7cb | 1026 | } |
bbfa0322 VZ |
1027 | |
1028 | putchar(m_ch); | |
1029 | fflush(stdout); | |
1030 | ||
1031 | wxThread::Sleep(100); | |
1032 | } | |
1033 | ||
b568d04f | 1034 | return 0; |
bbfa0322 VZ |
1035 | } |
1036 | ||
b568d04f | 1037 | void MyDetachedThread::OnExit() |
bbfa0322 | 1038 | { |
9fc3ad34 VZ |
1039 | wxLogTrace("thread", "Thread %ld is in OnExit", GetId()); |
1040 | ||
bbfa0322 | 1041 | wxCriticalSectionLocker lock(gs_critsect); |
fcc3d7cb | 1042 | if ( !--gs_counter && !m_cancelled ) |
b568d04f | 1043 | gs_cond.Signal(); |
bbfa0322 VZ |
1044 | } |
1045 | ||
9fc3ad34 VZ |
1046 | void TestDetachedThreads() |
1047 | { | |
2f02cb89 | 1048 | puts("\n*** Testing detached threads ***"); |
9fc3ad34 VZ |
1049 | |
1050 | static const size_t nThreads = 3; | |
1051 | MyDetachedThread *threads[nThreads]; | |
1052 | size_t n; | |
1053 | for ( n = 0; n < nThreads; n++ ) | |
1054 | { | |
1055 | threads[n] = new MyDetachedThread(10, 'A' + n); | |
1056 | } | |
1057 | ||
1058 | threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY); | |
1059 | threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY); | |
1060 | ||
1061 | for ( n = 0; n < nThreads; n++ ) | |
1062 | { | |
1063 | threads[n]->Run(); | |
1064 | } | |
1065 | ||
1066 | // wait until all threads terminate | |
1067 | gs_cond.Wait(); | |
1068 | ||
1069 | puts(""); | |
1070 | } | |
1071 | ||
1072 | void TestJoinableThreads() | |
1073 | { | |
2f02cb89 | 1074 | puts("\n*** Testing a joinable thread (a loooong calculation...) ***"); |
9fc3ad34 VZ |
1075 | |
1076 | // calc 10! in the background | |
1077 | MyJoinableThread thread(10); | |
1078 | thread.Run(); | |
1079 | ||
1080 | printf("\nThread terminated with exit code %lu.\n", | |
1081 | (unsigned long)thread.Wait()); | |
1082 | } | |
1083 | ||
1084 | void TestThreadSuspend() | |
1085 | { | |
2f02cb89 VZ |
1086 | puts("\n*** Testing thread suspend/resume functions ***"); |
1087 | ||
1088 | MyDetachedThread *thread = new MyDetachedThread(15, 'X'); | |
9fc3ad34 VZ |
1089 | |
1090 | thread->Run(); | |
1091 | ||
1092 | // this is for this demo only, in a real life program we'd use another | |
1093 | // condition variable which would be signaled from wxThread::Entry() to | |
1094 | // tell us that the thread really started running - but here just wait a | |
1095 | // bit and hope that it will be enough (the problem is, of course, that | |
1096 | // the thread might still not run when we call Pause() which will result | |
1097 | // in an error) | |
1098 | wxThread::Sleep(300); | |
1099 | ||
1100 | for ( size_t n = 0; n < 3; n++ ) | |
1101 | { | |
1102 | thread->Pause(); | |
1103 | ||
1104 | puts("\nThread suspended"); | |
1105 | if ( n > 0 ) | |
1106 | { | |
1107 | // don't sleep but resume immediately the first time | |
1108 | wxThread::Sleep(300); | |
1109 | } | |
1110 | puts("Going to resume the thread"); | |
1111 | ||
1112 | thread->Resume(); | |
1113 | } | |
1114 | ||
4c460b34 VZ |
1115 | puts("Waiting until it terminates now"); |
1116 | ||
9fc3ad34 VZ |
1117 | // wait until the thread terminates |
1118 | gs_cond.Wait(); | |
1119 | ||
1120 | puts(""); | |
1121 | } | |
1122 | ||
2f02cb89 VZ |
1123 | void TestThreadDelete() |
1124 | { | |
1125 | // As above, using Sleep() is only for testing here - we must use some | |
1126 | // synchronisation object instead to ensure that the thread is still | |
1127 | // running when we delete it - deleting a detached thread which already | |
1128 | // terminated will lead to a crash! | |
1129 | ||
1130 | puts("\n*** Testing thread delete function ***"); | |
1131 | ||
4c460b34 VZ |
1132 | MyDetachedThread *thread0 = new MyDetachedThread(30, 'W'); |
1133 | ||
1134 | thread0->Delete(); | |
1135 | ||
1136 | puts("\nDeleted a thread which didn't start to run yet."); | |
1137 | ||
2f02cb89 VZ |
1138 | MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y'); |
1139 | ||
1140 | thread1->Run(); | |
1141 | ||
1142 | wxThread::Sleep(300); | |
1143 | ||
1144 | thread1->Delete(); | |
1145 | ||
1146 | puts("\nDeleted a running thread."); | |
1147 | ||
1148 | MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z'); | |
1149 | ||
1150 | thread2->Run(); | |
1151 | ||
1152 | wxThread::Sleep(300); | |
1153 | ||
1154 | thread2->Pause(); | |
1155 | ||
1156 | thread2->Delete(); | |
1157 | ||
1158 | puts("\nDeleted a sleeping thread."); | |
1159 | ||
4c460b34 VZ |
1160 | MyJoinableThread thread3(20); |
1161 | thread3.Run(); | |
2f02cb89 | 1162 | |
4c460b34 | 1163 | thread3.Delete(); |
2f02cb89 VZ |
1164 | |
1165 | puts("\nDeleted a joinable thread."); | |
1166 | ||
4c460b34 VZ |
1167 | MyJoinableThread thread4(2); |
1168 | thread4.Run(); | |
2f02cb89 VZ |
1169 | |
1170 | wxThread::Sleep(300); | |
1171 | ||
4c460b34 | 1172 | thread4.Delete(); |
2f02cb89 VZ |
1173 | |
1174 | puts("\nDeleted a joinable thread which already terminated."); | |
1175 | ||
1176 | puts(""); | |
1177 | } | |
1178 | ||
e87271f3 VZ |
1179 | #endif // TEST_THREADS |
1180 | ||
1181 | // ---------------------------------------------------------------------------- | |
1182 | // arrays | |
1183 | // ---------------------------------------------------------------------------- | |
1184 | ||
1185 | #ifdef TEST_ARRAYS | |
1186 | ||
1187 | void PrintArray(const char* name, const wxArrayString& array) | |
1188 | { | |
1189 | printf("Dump of the array '%s'\n", name); | |
1190 | ||
1191 | size_t nCount = array.GetCount(); | |
1192 | for ( size_t n = 0; n < nCount; n++ ) | |
1193 | { | |
1194 | printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str()); | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | #endif // TEST_ARRAYS | |
1199 | ||
9fc3ad34 VZ |
1200 | // ---------------------------------------------------------------------------- |
1201 | // strings | |
1202 | // ---------------------------------------------------------------------------- | |
1203 | ||
1204 | #ifdef TEST_STRINGS | |
1205 | ||
1206 | #include "wx/timer.h" | |
1207 | ||
299fcbfe | 1208 | static void TestString() |
9fc3ad34 VZ |
1209 | { |
1210 | wxStopWatch sw; | |
1211 | ||
1212 | wxString a, b, c; | |
1213 | ||
1214 | a.reserve (128); | |
1215 | b.reserve (128); | |
1216 | c.reserve (128); | |
1217 | ||
1218 | for (int i = 0; i < 1000000; ++i) | |
1219 | { | |
1220 | a = "Hello"; | |
1221 | b = " world"; | |
1222 | c = "! How'ya doin'?"; | |
1223 | a += b; | |
1224 | a += c; | |
1225 | c = "Hello world! What's up?"; | |
1226 | if (c != a) | |
1227 | c = "Doh!"; | |
1228 | } | |
1229 | ||
1230 | printf ("TestString elapsed time: %ld\n", sw.Time()); | |
1231 | } | |
1232 | ||
299fcbfe | 1233 | static void TestPChar() |
9fc3ad34 VZ |
1234 | { |
1235 | wxStopWatch sw; | |
1236 | ||
1237 | char a [128]; | |
1238 | char b [128]; | |
1239 | char c [128]; | |
1240 | ||
1241 | for (int i = 0; i < 1000000; ++i) | |
1242 | { | |
1243 | strcpy (a, "Hello"); | |
1244 | strcpy (b, " world"); | |
1245 | strcpy (c, "! How'ya doin'?"); | |
1246 | strcat (a, b); | |
1247 | strcat (a, c); | |
1248 | strcpy (c, "Hello world! What's up?"); | |
1249 | if (strcmp (c, a) == 0) | |
1250 | strcpy (c, "Doh!"); | |
1251 | } | |
1252 | ||
1253 | printf ("TestPChar elapsed time: %ld\n", sw.Time()); | |
1254 | } | |
1255 | ||
299fcbfe VZ |
1256 | static void TestStringSub() |
1257 | { | |
1258 | wxString s("Hello, world!"); | |
1259 | ||
1260 | puts("*** Testing wxString substring extraction ***"); | |
1261 | ||
1262 | printf("String = '%s'\n", s.c_str()); | |
1263 | printf("Left(5) = '%s'\n", s.Left(5).c_str()); | |
1264 | printf("Right(6) = '%s'\n", s.Right(6).c_str()); | |
1265 | printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str()); | |
1266 | printf("Mid(3) = '%s'\n", s.Mid(3).c_str()); | |
1267 | printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str()); | |
1268 | printf("substr(3) = '%s'\n", s.substr(3).c_str()); | |
1269 | ||
1270 | puts(""); | |
1271 | } | |
1272 | ||
9fc3ad34 VZ |
1273 | #endif // TEST_STRINGS |
1274 | ||
e87271f3 VZ |
1275 | // ---------------------------------------------------------------------------- |
1276 | // entry point | |
1277 | // ---------------------------------------------------------------------------- | |
1278 | ||
bbfa0322 | 1279 | int main(int argc, char **argv) |
37667812 VZ |
1280 | { |
1281 | if ( !wxInitialize() ) | |
1282 | { | |
1283 | fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); | |
1284 | } | |
1285 | ||
9fc3ad34 | 1286 | #ifdef TEST_STRINGS |
299fcbfe VZ |
1287 | if ( 0 ) |
1288 | { | |
1289 | TestPChar(); | |
1290 | TestString(); | |
1291 | } | |
1292 | TestStringSub(); | |
9fc3ad34 VZ |
1293 | #endif // TEST_STRINGS |
1294 | ||
e87271f3 VZ |
1295 | #ifdef TEST_ARRAYS |
1296 | wxArrayString a1; | |
1297 | a1.Add("tiger"); | |
1298 | a1.Add("cat"); | |
1299 | a1.Add("lion"); | |
1300 | a1.Add("dog"); | |
1301 | a1.Add("human"); | |
1302 | a1.Add("ape"); | |
1303 | ||
1304 | puts("*** Initially:"); | |
1305 | ||
1306 | PrintArray("a1", a1); | |
1307 | ||
1308 | wxArrayString a2(a1); | |
1309 | PrintArray("a2", a2); | |
1310 | ||
1311 | wxSortedArrayString a3(a1); | |
1312 | PrintArray("a3", a3); | |
1313 | ||
1314 | puts("*** After deleting a string from a1"); | |
1315 | a1.Remove(2); | |
1316 | ||
1317 | PrintArray("a1", a1); | |
1318 | PrintArray("a2", a2); | |
1319 | PrintArray("a3", a3); | |
1320 | ||
1321 | puts("*** After reassigning a1 to a2 and a3"); | |
1322 | a3 = a2 = a1; | |
1323 | PrintArray("a2", a2); | |
1324 | PrintArray("a3", a3); | |
1325 | #endif // TEST_ARRAYS | |
1326 | ||
1944c6bd VZ |
1327 | #ifdef TEST_DIR |
1328 | TestDirEnum(); | |
1329 | #endif // TEST_DIR | |
1330 | ||
378b05f7 VZ |
1331 | #ifdef TEST_LOG |
1332 | wxString s; | |
1333 | for ( size_t n = 0; n < 8000; n++ ) | |
1334 | { | |
1335 | s << (char)('A' + (n % 26)); | |
1336 | } | |
1337 | ||
1338 | wxString msg; | |
1339 | msg.Printf("A very very long message: '%s', the end!\n", s.c_str()); | |
1340 | ||
1341 | // this one shouldn't be truncated | |
1342 | printf(msg); | |
1343 | ||
1344 | // but this one will because log functions use fixed size buffer | |
b568d04f VZ |
1345 | // (note that it doesn't need '\n' at the end neither - will be added |
1346 | // by wxLog anyhow) | |
1347 | wxLogMessage("A very very long message 2: '%s', the end!", s.c_str()); | |
378b05f7 VZ |
1348 | #endif // TEST_LOG |
1349 | ||
e87271f3 | 1350 | #ifdef TEST_THREADS |
696e1ea0 VZ |
1351 | int nCPUs = wxThread::GetCPUCount(); |
1352 | printf("This system has %d CPUs\n", nCPUs); | |
1353 | if ( nCPUs != -1 ) | |
1354 | wxThread::SetConcurrency(nCPUs); | |
ef8d96c2 | 1355 | |
9fc3ad34 VZ |
1356 | if ( argc > 1 && argv[1][0] == 't' ) |
1357 | wxLog::AddTraceMask("thread"); | |
b568d04f | 1358 | |
4c460b34 | 1359 | if ( 1 ) |
2f02cb89 | 1360 | TestDetachedThreads(); |
4c460b34 | 1361 | if ( 1 ) |
2f02cb89 | 1362 | TestJoinableThreads(); |
4c460b34 | 1363 | if ( 1 ) |
2f02cb89 VZ |
1364 | TestThreadSuspend(); |
1365 | if ( 1 ) | |
1366 | TestThreadDelete(); | |
1367 | ||
e87271f3 | 1368 | #endif // TEST_THREADS |
37667812 | 1369 | |
b76b015e VZ |
1370 | #ifdef TEST_LONGLONG |
1371 | if ( 0 ) | |
1372 | TestSpeed(); | |
1373 | if ( 1 ) | |
1374 | TestDivision(); | |
1375 | #endif // TEST_LONGLONG | |
1376 | ||
696e1ea0 VZ |
1377 | #ifdef TEST_MIME |
1378 | TestMimeEnum(); | |
1379 | #endif // TEST_MIME | |
1380 | ||
b76b015e | 1381 | #ifdef TEST_TIME |
299fcbfe VZ |
1382 | if ( 0 ) |
1383 | { | |
211c2250 | 1384 | TestTimeSet(); |
299fcbfe | 1385 | TestTimeStatic(); |
fcc3d7cb | 1386 | TestTimeZones(); |
e6ec579c | 1387 | TestTimeRange(); |
299fcbfe | 1388 | TestTimeTicks(); |
151d66be | 1389 | TestTimeJDN(); |
239446b4 | 1390 | TestTimeDST(); |
211c2250 | 1391 | TestTimeWDays(); |
239446b4 | 1392 | TestTimeWNumber(); |
97e0ceea VZ |
1393 | TestTimeParse(); |
1394 | } | |
68ee7c47 VZ |
1395 | |
1396 | TestTimeFormat(); | |
b76b015e VZ |
1397 | #endif // TEST_TIME |
1398 | ||
37667812 VZ |
1399 | wxUninitialize(); |
1400 | ||
1401 | return 0; | |
1402 | } |