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