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