]> git.saurik.com Git - wxWidgets.git/blame - samples/console/console.cpp
Removed two more GUI lock-ups. This time when
[wxWidgets.git] / samples / console / console.cpp
CommitLineData
37667812
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/console/console.cpp
3// Purpose: a sample console (as opposed to GUI) progam using wxWindows
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
e87271f3
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
37667812
VZ
20#include <stdio.h>
21
22#include <wx/string.h>
bbfa0322 23#include <wx/file.h>
37667812 24#include <wx/app.h>
e87271f3
VZ
25
26// ----------------------------------------------------------------------------
27// conditional compilation
28// ----------------------------------------------------------------------------
29
30// what to test?
378b05f7
VZ
31
32//#define TEST_ARRAYS
211c2250 33//#define TEST_DIR
b568d04f 34//#define TEST_LOG
5e6a0e83 35//#define TEST_LONGLONG
1944c6bd 36//#define TEST_MIME
9fc3ad34 37//#define TEST_STRINGS
696e1ea0 38//#define TEST_THREADS
5e6a0e83 39#define TEST_TIME
e87271f3
VZ
40
41// ============================================================================
42// implementation
43// ============================================================================
44
1944c6bd
VZ
45// ----------------------------------------------------------------------------
46// wxDir
47// ----------------------------------------------------------------------------
48
49#ifdef TEST_DIR
50
51#include <wx/dir.h>
52
53static 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
73static void TestDirEnum()
74{
75 wxDir dir(wxGetCwd());
76
77 puts("Enumerating everything in current directory:");
78 TestDirEnumHelper(dir);
79
80 puts("Enumerating really everything in current directory:");
81 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
82
83 puts("Enumerating object files in current directory:");
84 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o");
85
86 puts("Enumerating directories in current directory:");
87 TestDirEnumHelper(dir, wxDIR_DIRS);
88
89 puts("Enumerating files in current directory:");
90 TestDirEnumHelper(dir, wxDIR_FILES);
91
92 puts("Enumerating files including hidden in current directory:");
93 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
94
95#ifdef __UNIX__
96 dir.Open("/");
97#elif defined(__WXMSW__)
98 dir.Open("c:\\");
99#else
100 #error "don't know where the root directory is"
101#endif
102
103 puts("Enumerating everything in root directory:");
104 TestDirEnumHelper(dir, wxDIR_DEFAULT);
105
106 puts("Enumerating directories in root directory:");
107 TestDirEnumHelper(dir, wxDIR_DIRS);
108
109 puts("Enumerating files in root directory:");
110 TestDirEnumHelper(dir, wxDIR_FILES);
111
112 puts("Enumerating files including hidden in root directory:");
113 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
114
115 puts("Enumerating files in non existing directory:");
116 wxDir dirNo("nosuchdir");
117 TestDirEnumHelper(dirNo);
118}
119
120#endif // TEST_DIR
121
696e1ea0
VZ
122// ----------------------------------------------------------------------------
123// MIME types
124// ----------------------------------------------------------------------------
125
126#ifdef TEST_MIME
127
128#include <wx/mimetype.h>
129
130static void TestMimeEnum()
131{
132 wxMimeTypesManager mimeTM;
133 wxArrayString mimetypes;
134
135 size_t count = mimeTM.EnumAllFileTypes(mimetypes);
136
137 printf("*** All %u known filetypes: ***\n", count);
138
139 wxArrayString exts;
140 wxString desc;
141
142 for ( size_t n = 0; n < count; n++ )
143 {
144 wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]);
145 if ( !filetype )
c61f4f6d 146 {
97e0ceea
VZ
147 printf("nothing known about the filetype '%s'!\n",
148 mimetypes[n].c_str());
696e1ea0 149 continue;
c61f4f6d
VZ
150 }
151
696e1ea0
VZ
152 filetype->GetDescription(&desc);
153 filetype->GetExtensions(exts);
154
299fcbfe
VZ
155 filetype->GetIcon(NULL);
156
696e1ea0
VZ
157 wxString extsAll;
158 for ( size_t e = 0; e < exts.GetCount(); e++ )
159 {
160 if ( e > 0 )
161 extsAll << _T(", ");
162 extsAll += exts[e];
163 }
164
54acce90
VZ
165 printf("\t%s: %s (%s)\n",
166 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
696e1ea0
VZ
167 }
168}
169
170#endif // TEST_MIME
171
b76b015e
VZ
172// ----------------------------------------------------------------------------
173// long long
174// ----------------------------------------------------------------------------
175
176#ifdef TEST_LONGLONG
177
178#include <wx/longlong.h>
179#include <wx/timer.h>
180
181static void TestSpeed()
182{
183 static const long max = 100000000;
184 long n;
9fc3ad34 185
b76b015e
VZ
186 {
187 wxStopWatch sw;
188
189 long l = 0;
190 for ( n = 0; n < max; n++ )
191 {
192 l += n;
193 }
194
195 printf("Summing longs took %ld milliseconds.\n", sw.Time());
196 }
197
2ea24d9f 198#if wxUSE_LONGLONG_NATIVE
b76b015e
VZ
199 {
200 wxStopWatch sw;
201
2ea24d9f 202 wxLongLong_t l = 0;
b76b015e
VZ
203 for ( n = 0; n < max; n++ )
204 {
205 l += n;
206 }
207
2ea24d9f 208 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time());
b76b015e 209 }
2ea24d9f 210#endif // wxUSE_LONGLONG_NATIVE
b76b015e
VZ
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
225static void TestDivision()
226{
5e6a0e83
VZ
227 puts("*** Testing wxLongLong division ***\n");
228
2f02cb89 229 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
b76b015e 230
2f02cb89 231 // seed pseudo random generator
5e6a0e83 232 srand((unsigned)time(NULL));
2f02cb89 233
2ea24d9f 234 wxLongLong q, r;
2f02cb89 235 size_t nTested = 0;
5e6a0e83 236 for ( size_t n = 0; n < 100000; n++ )
2f02cb89
VZ
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
2ea24d9f
VZ
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
5e6a0e83 247 // verify the result
2ea24d9f 248 wxASSERT_MSG( ll == q*l + r, "division failure" );
2f02cb89 249
5e6a0e83
VZ
250 if ( !(nTested % 1000) )
251 {
252 putchar('.');
253 fflush(stdout);
254 }
255
2f02cb89
VZ
256 nTested++;
257 }
258
5e6a0e83 259 puts(" done!");
2f02cb89
VZ
260
261 #undef MAKE_LL
b76b015e
VZ
262}
263
264#endif // TEST_LONGLONG
265
266// ----------------------------------------------------------------------------
267// date time
268// ----------------------------------------------------------------------------
269
270#ifdef TEST_TIME
271
97e0ceea
VZ
272#include <wx/date.h>
273
b76b015e
VZ
274#include <wx/datetime.h>
275
299fcbfe
VZ
276// the test data
277struct Date
278{
279 wxDateTime::wxDateTime_t day;
280 wxDateTime::Month month;
281 int year;
282 wxDateTime::wxDateTime_t hour, min, sec;
283 double jdn;
211c2250 284 wxDateTime::WeekDay wday;
299fcbfe
VZ
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
239446b4
VZ
302 bool SameDay(const wxDateTime::Tm& tm) const
303 {
304 return day == tm.mday && month == tm.mon && year == tm.year;
305 }
306
299fcbfe
VZ
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 }
239446b4
VZ
318
319 wxString FormatDate() const
320 {
321 wxString s;
322 s.Printf("%02d-%s-%4d%s",
323 day,
f0f951fa 324 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
239446b4
VZ
325 abs(wxDateTime::ConvertYearToBC(year)),
326 year > 0 ? "AD" : "BC");
327 return s;
328 }
299fcbfe
VZ
329};
330
331static const Date testDates[] =
332{
211c2250
VZ
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 },
239446b4
VZ
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 },
211c2250 347 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
299fcbfe
VZ
348};
349
2f02cb89
VZ
350// this test miscellaneous static wxDateTime functions
351static 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",
f0f951fa 364 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
2f02cb89
VZ
365 wxDateTime::GetMonthName(month).c_str(),
366 wxDateTime::GetNumberOfDays(month));
367
368 // leap year logic
fcc3d7cb
VZ
369 static const size_t nYears = 5;
370 static const size_t years[2][nYears] =
2f02cb89
VZ
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];
239446b4
VZ
382 bool should = years[1][n] != 0,
383 is = wxDateTime::IsLeapYear(year);
2f02cb89 384
239446b4 385 printf("Year %d is %sa leap year (%s)\n",
2f02cb89 386 year,
239446b4
VZ
387 is ? "" : "not ",
388 should == is ? "ok" : "ERROR");
2f02cb89
VZ
389
390 wxASSERT( should == wxDateTime::IsLeapYear(year) );
391 }
392}
393
394// test constructing wxDateTime objects
395static void TestTimeSet()
396{
397 puts("\n*** wxDateTime construction test ***");
398
299fcbfe
VZ
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 }
2f02cb89
VZ
414}
415
fcc3d7cb
VZ
416// test time zones stuff
417static void TestTimeZones()
418{
419 puts("\n*** wxDateTime timezone test ***");
420
421 wxDateTime now = wxDateTime::Now();
422
299fcbfe
VZ
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());
9d9b7755
VZ
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 }
fcc3d7cb
VZ
436}
437
e6ec579c
VZ
438// test some minimal support for the dates outside the standard range
439static void TestTimeRange()
440{
441 puts("\n*** wxDateTime out-of-standard-range dates test ***");
442
211c2250
VZ
443 static const char *fmt = "%d-%b-%Y %H:%M:%S";
444
1ef54dcf 445 printf("Unix epoch:\t%s\n",
211c2250 446 wxDateTime(2440587.5).Format(fmt).c_str());
1ef54dcf 447 printf("Feb 29, 0: \t%s\n",
211c2250 448 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
e6ec579c 449 printf("JDN 0: \t%s\n",
211c2250 450 wxDateTime(0.0).Format(fmt).c_str());
e6ec579c 451 printf("Jan 1, 1AD:\t%s\n",
211c2250 452 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
e6ec579c 453 printf("May 29, 2099:\t%s\n",
211c2250 454 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
e6ec579c
VZ
455}
456
299fcbfe 457static void TestTimeTicks()
e6ec579c 458{
299fcbfe 459 puts("\n*** wxDateTime ticks test ***");
e6ec579c 460
299fcbfe 461 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1ef54dcf 462 {
299fcbfe
VZ
463 const Date& d = testDates[n];
464 if ( d.ticks == -1 )
465 continue;
1ef54dcf 466
299fcbfe
VZ
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
498static void TestTimeJDN()
499{
500 puts("\n*** wxDateTime to JDN test ***");
1ef54dcf
VZ
501
502 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
503 {
504 const Date& d = testDates[n];
299fcbfe 505 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
1ef54dcf
VZ
506 double jdn = dt.GetJulianDayNumber();
507
299fcbfe 508 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
1ef54dcf
VZ
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 }
e6ec579c
VZ
519}
520
211c2250
VZ
521// test week days computation
522static void TestTimeWDays()
523{
524 puts("\n*** wxDateTime weekday test ***");
525
239446b4
VZ
526 // test GetWeekDay()
527 size_t n;
528 for ( n = 0; n < WXSIZEOF(testDates); n++ )
211c2250
VZ
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(),
239446b4 536 wxDateTime::GetWeekDayName(wday).c_str());
211c2250
VZ
537 if ( wday == d.wday )
538 {
539 puts(" (ok)");
540 }
541 else
542 {
543 printf(" (ERROR: should be %s)\n",
239446b4
VZ
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 /*
590from DateTime import *
591from whrandom import *
592from string import *
593
594monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
595wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
596
597week = DateTimeDelta(7)
598
599for 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] }
97e0ceea 614
239446b4
VZ
615 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
616 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
97e0ceea 617 */
239446b4
VZ
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());
211c2250
VZ
664 }
665 }
666}
667
239446b4
VZ
668// test the computation of (ISO) week numbers
669static void TestTimeWNumber()
670{
671 puts("\n*** wxDateTime week number test ***");
672
673 struct WeekNumberTestData
674 {
675 Date date; // the date
9d9b7755
VZ
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
239446b4
VZ
679 wxDateTime::wxDateTime_t dnum; // day number in the year
680 };
681
682 // data generated with the following python script:
683 /*
684from DateTime import *
685from whrandom import *
686from string import *
687
688monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
689wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
690
9d9b7755
VZ
691def 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
697def GetLastSundayBefore(dt):
698 if dt.iso_week[2] == 7:
699 return dt
700 else:
701 return dt - DateTimeDelta(dt.iso_week[2])
702
239446b4
VZ
703for 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]
9d9b7755
VZ
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, "\
239446b4 733 "%(dayNum)s }," % data
9d9b7755 734
239446b4
VZ
735 */
736 static const WeekNumberTestData weekNumberTestDates[] =
737 {
9d9b7755
VZ
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 },
239446b4
VZ
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
9d9b7755
VZ
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();
239446b4
VZ
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
9d9b7755
VZ
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);
239446b4
VZ
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
817static 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];
97e0ceea 875
239446b4
VZ
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
68ee7c47
VZ
899// test wxDateTime -> text conversion
900static void TestTimeFormat()
901{
902 puts("\n*** wxDateTime formatting test ***");
903
b38e2f7d
VZ
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
68ee7c47 907 {
b38e2f7d
VZ
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" },
68ee7c47
VZ
925 };
926
927 static const Date formatTestDates[] =
928 {
68ee7c47
VZ
929 { 29, wxDateTime::May, 1976, 18, 30, 00 },
930 { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
b38e2f7d
VZ
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
68ee7c47
VZ
934 { 29, wxDateTime::May, 2076, 18, 30, 00 },
935 { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
936 { 01, wxDateTime::Jan, -52, 03, 16, 47 },
b38e2f7d 937#endif
68ee7c47
VZ
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
b38e2f7d 943 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
68ee7c47
VZ
944 {
945 puts("");
946
b38e2f7d 947 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
68ee7c47
VZ
948 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
949 {
b38e2f7d 950 wxString s = dt.Format(formatTestFormats[n].format);
f0f951fa
VZ
951 printf("%s", s.c_str());
952
b38e2f7d
VZ
953 // what can we recover?
954 int kind = formatTestFormats[n].compareKind;
955
f0f951fa
VZ
956 // convert back
957 wxDateTime dt2;
b38e2f7d 958 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
f0f951fa
VZ
959 if ( !result )
960 {
b38e2f7d
VZ
961 // converion failed - should it have?
962 if ( kind == CompareNone )
963 puts(" (ok)");
964 else
965 puts(" (ERROR: conversion back failed)");
f0f951fa
VZ
966 }
967 else if ( *result )
968 {
969 // should have parsed the entire string
970 puts(" (ERROR: conversion back stopped too soon)");
971 }
f0f951fa
VZ
972 else
973 {
b38e2f7d
VZ
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 }
f0f951fa 999 }
68ee7c47
VZ
1000 }
1001 }
1002}
1003
97e0ceea
VZ
1004// test text -> wxDateTime conversion
1005static 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 {
68ee7c47
VZ
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 },
97e0ceea
VZ
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
9d9b7755
VZ
1058static 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
1089static 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
68ee7c47
VZ
1153#if 0
1154
97e0ceea
VZ
1155// test compatibility with the old wxDate/wxTime classes
1156static 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
68ee7c47
VZ
1183#endif // 0
1184
b76b015e
VZ
1185#endif // TEST_TIME
1186
e87271f3
VZ
1187// ----------------------------------------------------------------------------
1188// threads
1189// ----------------------------------------------------------------------------
1190
1191#ifdef TEST_THREADS
1192
bbfa0322 1193#include <wx/thread.h>
37667812 1194
bbfa0322
VZ
1195static size_t gs_counter = (size_t)-1;
1196static wxCriticalSection gs_critsect;
b568d04f 1197static wxCondition gs_cond;
bbfa0322 1198
b568d04f 1199class MyJoinableThread : public wxThread
bbfa0322
VZ
1200{
1201public:
b568d04f
VZ
1202 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
1203 { m_n = n; Create(); }
bbfa0322
VZ
1204
1205 // thread execution starts here
b568d04f 1206 virtual ExitCode Entry();
bbfa0322 1207
b568d04f
VZ
1208private:
1209 size_t m_n;
bbfa0322
VZ
1210};
1211
b568d04f 1212wxThread::ExitCode MyJoinableThread::Entry()
bbfa0322 1213{
b568d04f
VZ
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 }
bbfa0322 1222
b568d04f 1223 return (ExitCode)res;
bbfa0322
VZ
1224}
1225
b568d04f
VZ
1226class MyDetachedThread : public wxThread
1227{
1228public:
fcc3d7cb
VZ
1229 MyDetachedThread(size_t n, char ch)
1230 {
1231 m_n = n;
1232 m_ch = ch;
1233 m_cancelled = FALSE;
1234
1235 Create();
1236 }
b568d04f
VZ
1237
1238 // thread execution starts here
1239 virtual ExitCode Entry();
1240
1241 // and stops here
1242 virtual void OnExit();
1243
1244private:
9fc3ad34
VZ
1245 size_t m_n; // number of characters to write
1246 char m_ch; // character to write
fcc3d7cb
VZ
1247
1248 bool m_cancelled; // FALSE if we exit normally
b568d04f
VZ
1249};
1250
1251wxThread::ExitCode MyDetachedThread::Entry()
bbfa0322
VZ
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
9fc3ad34 1261 for ( size_t n = 0; n < m_n; n++ )
bbfa0322
VZ
1262 {
1263 if ( TestDestroy() )
fcc3d7cb
VZ
1264 {
1265 m_cancelled = TRUE;
1266
bbfa0322 1267 break;
fcc3d7cb 1268 }
bbfa0322
VZ
1269
1270 putchar(m_ch);
1271 fflush(stdout);
1272
1273 wxThread::Sleep(100);
1274 }
1275
b568d04f 1276 return 0;
bbfa0322
VZ
1277}
1278
b568d04f 1279void MyDetachedThread::OnExit()
bbfa0322 1280{
9fc3ad34
VZ
1281 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1282
bbfa0322 1283 wxCriticalSectionLocker lock(gs_critsect);
fcc3d7cb 1284 if ( !--gs_counter && !m_cancelled )
b568d04f 1285 gs_cond.Signal();
bbfa0322
VZ
1286}
1287
9fc3ad34
VZ
1288void TestDetachedThreads()
1289{
2f02cb89 1290 puts("\n*** Testing detached threads ***");
9fc3ad34
VZ
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
1314void TestJoinableThreads()
1315{
2f02cb89 1316 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
9fc3ad34
VZ
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
1326void TestThreadSuspend()
1327{
2f02cb89
VZ
1328 puts("\n*** Testing thread suspend/resume functions ***");
1329
1330 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
9fc3ad34
VZ
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
4c460b34
VZ
1357 puts("Waiting until it terminates now");
1358
9fc3ad34
VZ
1359 // wait until the thread terminates
1360 gs_cond.Wait();
1361
1362 puts("");
1363}
1364
2f02cb89
VZ
1365void 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
4c460b34
VZ
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
2f02cb89
VZ
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
4c460b34
VZ
1402 MyJoinableThread thread3(20);
1403 thread3.Run();
2f02cb89 1404
4c460b34 1405 thread3.Delete();
2f02cb89
VZ
1406
1407 puts("\nDeleted a joinable thread.");
1408
4c460b34
VZ
1409 MyJoinableThread thread4(2);
1410 thread4.Run();
2f02cb89
VZ
1411
1412 wxThread::Sleep(300);
1413
4c460b34 1414 thread4.Delete();
2f02cb89
VZ
1415
1416 puts("\nDeleted a joinable thread which already terminated.");
1417
1418 puts("");
1419}
1420
e87271f3
VZ
1421#endif // TEST_THREADS
1422
1423// ----------------------------------------------------------------------------
1424// arrays
1425// ----------------------------------------------------------------------------
1426
1427#ifdef TEST_ARRAYS
1428
1429void 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
9fc3ad34
VZ
1442// ----------------------------------------------------------------------------
1443// strings
1444// ----------------------------------------------------------------------------
1445
1446#ifdef TEST_STRINGS
1447
1448#include "wx/timer.h"
1449
299fcbfe 1450static void TestString()
9fc3ad34
VZ
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
299fcbfe 1475static void TestPChar()
9fc3ad34
VZ
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
299fcbfe
VZ
1498static 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
f0f951fa
VZ
1515static 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
9fc3ad34
VZ
1528#endif // TEST_STRINGS
1529
e87271f3
VZ
1530// ----------------------------------------------------------------------------
1531// entry point
1532// ----------------------------------------------------------------------------
1533
bbfa0322 1534int main(int argc, char **argv)
37667812
VZ
1535{
1536 if ( !wxInitialize() )
1537 {
1538 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
1539 }
1540
9fc3ad34 1541#ifdef TEST_STRINGS
299fcbfe
VZ
1542 if ( 0 )
1543 {
1544 TestPChar();
1545 TestString();
1546 }
f0f951fa
VZ
1547 if ( 0 )
1548 {
1549 TestStringSub();
1550 }
1551 TestStringFormat();
9fc3ad34
VZ
1552#endif // TEST_STRINGS
1553
e87271f3
VZ
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
1944c6bd
VZ
1586#ifdef TEST_DIR
1587 TestDirEnum();
1588#endif // TEST_DIR
1589
378b05f7
VZ
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
b568d04f
VZ
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());
378b05f7
VZ
1607#endif // TEST_LOG
1608
e87271f3 1609#ifdef TEST_THREADS
696e1ea0
VZ
1610 int nCPUs = wxThread::GetCPUCount();
1611 printf("This system has %d CPUs\n", nCPUs);
1612 if ( nCPUs != -1 )
1613 wxThread::SetConcurrency(nCPUs);
ef8d96c2 1614
9fc3ad34
VZ
1615 if ( argc > 1 && argv[1][0] == 't' )
1616 wxLog::AddTraceMask("thread");
b568d04f 1617
4c460b34 1618 if ( 1 )
2f02cb89 1619 TestDetachedThreads();
4c460b34 1620 if ( 1 )
2f02cb89 1621 TestJoinableThreads();
4c460b34 1622 if ( 1 )
2f02cb89
VZ
1623 TestThreadSuspend();
1624 if ( 1 )
1625 TestThreadDelete();
1626
e87271f3 1627#endif // TEST_THREADS
37667812 1628
b76b015e
VZ
1629#ifdef TEST_LONGLONG
1630 if ( 0 )
1631 TestSpeed();
1632 if ( 1 )
1633 TestDivision();
1634#endif // TEST_LONGLONG
1635
696e1ea0
VZ
1636#ifdef TEST_MIME
1637 TestMimeEnum();
1638#endif // TEST_MIME
1639
b76b015e 1640#ifdef TEST_TIME
9d9b7755 1641 if ( 0 )
299fcbfe 1642 {
9d9b7755
VZ
1643 TestTimeSet();
1644 TestTimeStatic();
1645 TestTimeRange();
1646 TestTimeZones();
1647 TestTimeTicks();
1648 TestTimeJDN();
1649 TestTimeDST();
1650 TestTimeWDays();
1651 TestTimeWNumber();
1652 TestTimeParse();
1653 TestTimeFormat();
1654 TestTimeArithmetics();
41acf5c0 1655 }
9d9b7755
VZ
1656 if ( 0 )
1657 TestInteractive();
b76b015e
VZ
1658#endif // TEST_TIME
1659
37667812
VZ
1660 wxUninitialize();
1661
1662 return 0;
1663}