1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
22 #include <wx/string.h>
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
36 //#define TEST_STRINGS
37 //#define TEST_THREADS
39 //#define TEST_LONGLONG
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
53 static void TestDirEnumHelper(wxDir
& dir
,
54 int flags
= wxDIR_DEFAULT
,
55 const wxString
& filespec
= wxEmptyString
)
59 if ( !dir
.IsOpened() )
62 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
65 printf("\t%s\n", filename
.c_str());
67 cont
= dir
.GetNext(&filename
);
73 static void TestDirEnum()
75 wxDir
dir(wxGetCwd());
77 puts("Enumerating everything in current directory:");
78 TestDirEnumHelper(dir
);
80 puts("Enumerating really everything in current directory:");
81 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
83 puts("Enumerating object files in current directory:");
84 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
86 puts("Enumerating directories in current directory:");
87 TestDirEnumHelper(dir
, wxDIR_DIRS
);
89 puts("Enumerating files in current directory:");
90 TestDirEnumHelper(dir
, wxDIR_FILES
);
92 puts("Enumerating files including hidden in current directory:");
93 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
97 #elif defined(__WXMSW__)
100 #error "don't know where the root directory is"
103 puts("Enumerating everything in root directory:");
104 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
106 puts("Enumerating directories in root directory:");
107 TestDirEnumHelper(dir
, wxDIR_DIRS
);
109 puts("Enumerating files in root directory:");
110 TestDirEnumHelper(dir
, wxDIR_FILES
);
112 puts("Enumerating files including hidden in root directory:");
113 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
115 puts("Enumerating files in non existing directory:");
116 wxDir
dirNo("nosuchdir");
117 TestDirEnumHelper(dirNo
);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
128 #include <wx/mimetype.h>
130 static void TestMimeEnum()
132 wxMimeTypesManager mimeTM
;
133 wxArrayString mimetypes
;
135 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
137 printf("*** All %u known filetypes: ***\n", count
);
142 for ( size_t n
= 0; n
< count
; n
++ )
144 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
147 printf("nothing known about the filetype '%s'!\n",
148 mimetypes
[n
].c_str());
152 filetype
->GetDescription(&desc
);
153 filetype
->GetExtensions(exts
);
155 filetype
->GetIcon(NULL
);
158 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
165 printf("\t%s: %s (%s)\n",
166 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
178 #include <wx/longlong.h>
179 #include <wx/timer.h>
181 static void TestSpeed()
183 static const long max
= 100000000;
190 for ( n
= 0; n
< max
; n
++ )
195 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
202 for ( n
= 0; n
< max
; n
++ )
207 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
214 for ( n
= 0; n
< max
; n
++ )
219 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
223 static void TestDivision()
225 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
227 // seed pseudo random generator
228 //srand((unsigned)time(NULL));
231 for ( size_t n
= 0; n
< 10000; n
++ )
233 // get a random wxLongLong (shifting by 12 the MSB ensures that the
234 // multiplication will not overflow)
235 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
237 wxASSERT( (ll
* 1000l)/1000l == ll
);
242 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
247 #endif // TEST_LONGLONG
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
257 #include <wx/datetime.h>
262 wxDateTime::wxDateTime_t day
;
263 wxDateTime::Month month
;
265 wxDateTime::wxDateTime_t hour
, min
, sec
;
267 wxDateTime::WeekDay wday
;
268 time_t gmticks
, ticks
;
270 void Init(const wxDateTime::Tm
& tm
)
279 gmticks
= ticks
= -1;
282 wxDateTime
DT() const
283 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
285 bool SameDay(const wxDateTime::Tm
& tm
) const
287 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
290 wxString
Format() const
293 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
295 wxDateTime::GetMonthName(month
).c_str(),
297 abs(wxDateTime::ConvertYearToBC(year
)),
298 year
> 0 ? "AD" : "BC");
302 wxString
FormatDate() const
305 s
.Printf("%02d-%s-%4d%s",
307 wxDateTime::GetMonthName(month
, TRUE
).c_str(),
308 abs(wxDateTime::ConvertYearToBC(year
)),
309 year
> 0 ? "AD" : "BC");
314 static const Date testDates
[] =
316 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
317 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
318 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
319 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
320 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
321 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
322 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
323 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
324 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
325 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
326 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
327 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
328 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
329 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
330 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
333 // this test miscellaneous static wxDateTime functions
334 static void TestTimeStatic()
336 puts("\n*** wxDateTime static methods test ***");
338 // some info about the current date
339 int year
= wxDateTime::GetCurrentYear();
340 printf("Current year %d is %sa leap one and has %d days.\n",
342 wxDateTime::IsLeapYear(year
) ? "" : "not ",
343 wxDateTime::GetNumberOfDays(year
));
345 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
346 printf("Current month is '%s' ('%s') and it has %d days\n",
347 wxDateTime::GetMonthName(month
, TRUE
).c_str(),
348 wxDateTime::GetMonthName(month
).c_str(),
349 wxDateTime::GetNumberOfDays(month
));
352 static const size_t nYears
= 5;
353 static const size_t years
[2][nYears
] =
355 // first line: the years to test
356 { 1990, 1976, 2000, 2030, 1984, },
358 // second line: TRUE if leap, FALSE otherwise
359 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
362 for ( size_t n
= 0; n
< nYears
; n
++ )
364 int year
= years
[0][n
];
365 bool should
= years
[1][n
] != 0,
366 is
= wxDateTime::IsLeapYear(year
);
368 printf("Year %d is %sa leap year (%s)\n",
371 should
== is
? "ok" : "ERROR");
373 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
377 // test constructing wxDateTime objects
378 static void TestTimeSet()
380 puts("\n*** wxDateTime construction test ***");
382 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
384 const Date
& d1
= testDates
[n
];
385 wxDateTime dt
= d1
.DT();
390 wxString s1
= d1
.Format(),
393 printf("Date: %s == %s (%s)\n",
394 s1
.c_str(), s2
.c_str(),
395 s1
== s2
? "ok" : "ERROR");
399 // test time zones stuff
400 static void TestTimeZones()
402 puts("\n*** wxDateTime timezone test ***");
404 wxDateTime now
= wxDateTime::Now();
406 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
407 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
408 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
409 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
410 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
411 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
414 // test some minimal support for the dates outside the standard range
415 static void TestTimeRange()
417 puts("\n*** wxDateTime out-of-standard-range dates test ***");
419 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
421 printf("Unix epoch:\t%s\n",
422 wxDateTime(2440587.5).Format(fmt
).c_str());
423 printf("Feb 29, 0: \t%s\n",
424 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
425 printf("JDN 0: \t%s\n",
426 wxDateTime(0.0).Format(fmt
).c_str());
427 printf("Jan 1, 1AD:\t%s\n",
428 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
429 printf("May 29, 2099:\t%s\n",
430 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
433 static void TestTimeTicks()
435 puts("\n*** wxDateTime ticks test ***");
437 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
439 const Date
& d
= testDates
[n
];
443 wxDateTime dt
= d
.DT();
444 long ticks
= (dt
.GetValue() / 1000).ToLong();
445 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
446 if ( ticks
== d
.ticks
)
452 printf(" (ERROR: should be %ld, delta = %ld)\n",
453 d
.ticks
, ticks
- d
.ticks
);
456 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
457 ticks
= (dt
.GetValue() / 1000).ToLong();
458 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
459 if ( ticks
== d
.gmticks
)
465 printf(" (ERROR: should be %ld, delta = %ld)\n",
466 d
.gmticks
, ticks
- d
.gmticks
);
473 // test conversions to JDN &c
474 static void TestTimeJDN()
476 puts("\n*** wxDateTime to JDN test ***");
478 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
480 const Date
& d
= testDates
[n
];
481 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
482 double jdn
= dt
.GetJulianDayNumber();
484 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
491 printf(" (ERROR: should be %f, delta = %f)\n",
497 // test week days computation
498 static void TestTimeWDays()
500 puts("\n*** wxDateTime weekday test ***");
504 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
506 const Date
& d
= testDates
[n
];
507 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
509 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
512 wxDateTime::GetWeekDayName(wday
).c_str());
513 if ( wday
== d
.wday
)
519 printf(" (ERROR: should be %s)\n",
520 wxDateTime::GetWeekDayName(d
.wday
).c_str());
526 // test SetToWeekDay()
527 struct WeekDateTestData
529 Date date
; // the real date (precomputed)
530 int nWeek
; // its week index in the month
531 wxDateTime::WeekDay wday
; // the weekday
532 wxDateTime::Month month
; // the month
533 int year
; // and the year
535 wxString
Format() const
538 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
540 case 1: which
= "first"; break;
541 case 2: which
= "second"; break;
542 case 3: which
= "third"; break;
543 case 4: which
= "fourth"; break;
544 case 5: which
= "fifth"; break;
546 case -1: which
= "last"; break;
551 which
+= " from end";
554 s
.Printf("The %s %s of %s in %d",
556 wxDateTime::GetWeekDayName(wday
).c_str(),
557 wxDateTime::GetMonthName(month
).c_str(),
564 // the array data was generated by the following python program
566 from DateTime import *
567 from whrandom import *
570 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
571 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
573 week = DateTimeDelta(7)
576 year = randint(1900, 2100)
577 month = randint(1, 12)
579 dt = DateTime(year, month, day)
580 wday = dt.day_of_week
582 countFromEnd = choice([-1, 1])
585 while dt.month is month:
586 dt = dt - countFromEnd * week
587 weekNum = weekNum + countFromEnd
589 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
591 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
592 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
595 static const WeekDateTestData weekDatesTestData
[] =
597 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
598 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
599 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
600 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
601 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
602 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
603 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
604 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
605 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
606 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
607 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
608 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
609 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
610 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
611 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
612 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
613 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
614 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
615 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
616 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
619 static const char *fmt
= "%d-%b-%Y";
622 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
624 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
626 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
628 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
630 const Date
& d
= wd
.date
;
631 if ( d
.SameDay(dt
.GetTm()) )
637 dt
.Set(d
.day
, d
.month
, d
.year
);
639 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
644 // test the computation of (ISO) week numbers
645 static void TestTimeWNumber()
647 puts("\n*** wxDateTime week number test ***");
649 struct WeekNumberTestData
651 Date date
; // the date
652 wxDateTime::wxDateTime_t week
; // the week number
653 wxDateTime::wxDateTime_t dnum
; // day number in the year
656 // data generated with the following python script:
658 from DateTime import *
659 from whrandom import *
662 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
663 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
666 year = randint(1900, 2100)
667 month = randint(1, 12)
669 dt = DateTime(year, month, day)
670 dayNum = dt.day_of_year
671 weekNum = dt.iso_week[1]
673 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) }
675 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\
676 "%(dayNum)s }," % data
678 static const WeekNumberTestData weekNumberTestDates
[] =
680 { { 2, wxDateTime::Jul
, 2093 }, 27, 183 },
681 { { 25, wxDateTime::Jun
, 1986 }, 26, 176 },
682 { { 15, wxDateTime::Jun
, 2014 }, 24, 166 },
683 { { 20, wxDateTime::Jul
, 2018 }, 29, 201 },
684 { { 3, wxDateTime::Aug
, 2074 }, 31, 215 },
685 { { 26, wxDateTime::Jul
, 2012 }, 30, 208 },
686 { { 4, wxDateTime::Nov
, 1915 }, 44, 308 },
687 { { 11, wxDateTime::Feb
, 2035 }, 6, 42 },
688 { { 15, wxDateTime::Feb
, 1942 }, 7, 46 },
689 { { 5, wxDateTime::Jan
, 2087 }, 1, 5 },
690 { { 6, wxDateTime::Nov
, 2016 }, 44, 311 },
691 { { 6, wxDateTime::Jun
, 2057 }, 23, 157 },
692 { { 25, wxDateTime::Feb
, 1976 }, 9, 56 },
693 { { 12, wxDateTime::Jan
, 2073 }, 2, 12 },
694 { { 12, wxDateTime::Sep
, 2040 }, 37, 256 },
695 { { 15, wxDateTime::Jul
, 1931 }, 29, 196 },
696 { { 23, wxDateTime::Mar
, 2084 }, 12, 83 },
697 { { 12, wxDateTime::Dec
, 1970 }, 50, 346 },
698 { { 6, wxDateTime::Sep
, 1996 }, 36, 250 },
699 { { 7, wxDateTime::Jan
, 2076 }, 2, 7 },
702 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
704 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
705 const Date
& d
= wn
.date
;
707 wxDateTime dt
= d
.DT();
709 wxDateTime::wxDateTime_t week
= dt
.GetWeekOfYear(),
710 dnum
= dt
.GetDayOfYear();
712 printf("%s: the day number is %d",
713 d
.FormatDate().c_str(), dnum
);
714 if ( dnum
== wn
.dnum
)
720 printf(" (ERROR: should be %d)", wn
.dnum
);
723 printf(", week number is %d", week
);
724 if ( week
== wn
.week
)
730 printf(" (ERROR: should be %d)\n", wn
.week
);
735 // test DST calculations
736 static void TestTimeDST()
738 puts("\n*** wxDateTime DST test ***");
740 printf("DST is%s in effect now.\n\n",
741 wxDateTime::Now().IsDST() ? "" : " not");
743 // taken from http://www.energy.ca.gov/daylightsaving.html
744 static const Date datesDST
[2][2004 - 1900 + 1] =
747 { 1, wxDateTime::Apr
, 1990 },
748 { 7, wxDateTime::Apr
, 1991 },
749 { 5, wxDateTime::Apr
, 1992 },
750 { 4, wxDateTime::Apr
, 1993 },
751 { 3, wxDateTime::Apr
, 1994 },
752 { 2, wxDateTime::Apr
, 1995 },
753 { 7, wxDateTime::Apr
, 1996 },
754 { 6, wxDateTime::Apr
, 1997 },
755 { 5, wxDateTime::Apr
, 1998 },
756 { 4, wxDateTime::Apr
, 1999 },
757 { 2, wxDateTime::Apr
, 2000 },
758 { 1, wxDateTime::Apr
, 2001 },
759 { 7, wxDateTime::Apr
, 2002 },
760 { 6, wxDateTime::Apr
, 2003 },
761 { 4, wxDateTime::Apr
, 2004 },
764 { 28, wxDateTime::Oct
, 1990 },
765 { 27, wxDateTime::Oct
, 1991 },
766 { 25, wxDateTime::Oct
, 1992 },
767 { 31, wxDateTime::Oct
, 1993 },
768 { 30, wxDateTime::Oct
, 1994 },
769 { 29, wxDateTime::Oct
, 1995 },
770 { 27, wxDateTime::Oct
, 1996 },
771 { 26, wxDateTime::Oct
, 1997 },
772 { 25, wxDateTime::Oct
, 1998 },
773 { 31, wxDateTime::Oct
, 1999 },
774 { 29, wxDateTime::Oct
, 2000 },
775 { 28, wxDateTime::Oct
, 2001 },
776 { 27, wxDateTime::Oct
, 2002 },
777 { 26, wxDateTime::Oct
, 2003 },
778 { 31, wxDateTime::Oct
, 2004 },
783 for ( year
= 1990; year
< 2005; year
++ )
785 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
786 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
788 printf("DST period in the US for year %d: from %s to %s",
789 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
791 size_t n
= year
- 1990;
792 const Date
& dBegin
= datesDST
[0][n
];
793 const Date
& dEnd
= datesDST
[1][n
];
795 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
801 printf(" (ERROR: should be %s %d to %s %d)\n",
802 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
803 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
809 for ( year
= 1990; year
< 2005; year
++ )
811 printf("DST period in Europe for year %d: from %s to %s\n",
813 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
814 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
818 // test text -> wxDateTime conversion
819 static void TestTimeParse()
821 puts("\n*** wxDateTime parse test ***");
830 static const ParseTestData parseTestDates
[] =
832 "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
,
835 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
837 const char *format
= parseTestDates
[n
].format
;
839 printf("%s => ", format
);
842 if ( dt
.ParseRfc822Date(format
) )
844 printf("%s ", dt
.Format().c_str());
846 if ( parseTestDates
[n
].good
)
848 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
855 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
860 puts("(ERROR: bad format)");
865 printf("bad format (%s)\n",
866 parseTestDates
[n
].good
? "ERROR" : "ok");
871 // test compatibility with the old wxDate/wxTime classes
872 static void TestTimeCompatibility()
874 puts("\n*** wxDateTime compatibility test ***");
876 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
877 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
879 double jdnNow
= wxDateTime::Now().GetJDN();
880 long jdnMidnight
= (long)(jdnNow
- 0.5);
881 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
883 jdnMidnight
= wxDate().Set().GetJulianDate();
884 printf("wxDateTime for today: %s\n",
885 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
887 int flags
= wxEUROPEAN
;//wxFULL;
890 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
891 for ( int n
= 0; n
< 7; n
++ )
893 printf("Previous %s is %s\n",
894 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
895 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
907 #include <wx/thread.h>
909 static size_t gs_counter
= (size_t)-1;
910 static wxCriticalSection gs_critsect
;
911 static wxCondition gs_cond
;
913 class MyJoinableThread
: public wxThread
916 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
917 { m_n
= n
; Create(); }
919 // thread execution starts here
920 virtual ExitCode
Entry();
926 wxThread::ExitCode
MyJoinableThread::Entry()
928 unsigned long res
= 1;
929 for ( size_t n
= 1; n
< m_n
; n
++ )
933 // it's a loooong calculation :-)
937 return (ExitCode
)res
;
940 class MyDetachedThread
: public wxThread
943 MyDetachedThread(size_t n
, char ch
)
952 // thread execution starts here
953 virtual ExitCode
Entry();
956 virtual void OnExit();
959 size_t m_n
; // number of characters to write
960 char m_ch
; // character to write
962 bool m_cancelled
; // FALSE if we exit normally
965 wxThread::ExitCode
MyDetachedThread::Entry()
968 wxCriticalSectionLocker
lock(gs_critsect
);
969 if ( gs_counter
== (size_t)-1 )
975 for ( size_t n
= 0; n
< m_n
; n
++ )
987 wxThread::Sleep(100);
993 void MyDetachedThread::OnExit()
995 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
997 wxCriticalSectionLocker
lock(gs_critsect
);
998 if ( !--gs_counter
&& !m_cancelled
)
1002 void TestDetachedThreads()
1004 puts("\n*** Testing detached threads ***");
1006 static const size_t nThreads
= 3;
1007 MyDetachedThread
*threads
[nThreads
];
1009 for ( n
= 0; n
< nThreads
; n
++ )
1011 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1014 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1015 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1017 for ( n
= 0; n
< nThreads
; n
++ )
1022 // wait until all threads terminate
1028 void TestJoinableThreads()
1030 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1032 // calc 10! in the background
1033 MyJoinableThread
thread(10);
1036 printf("\nThread terminated with exit code %lu.\n",
1037 (unsigned long)thread
.Wait());
1040 void TestThreadSuspend()
1042 puts("\n*** Testing thread suspend/resume functions ***");
1044 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1048 // this is for this demo only, in a real life program we'd use another
1049 // condition variable which would be signaled from wxThread::Entry() to
1050 // tell us that the thread really started running - but here just wait a
1051 // bit and hope that it will be enough (the problem is, of course, that
1052 // the thread might still not run when we call Pause() which will result
1054 wxThread::Sleep(300);
1056 for ( size_t n
= 0; n
< 3; n
++ )
1060 puts("\nThread suspended");
1063 // don't sleep but resume immediately the first time
1064 wxThread::Sleep(300);
1066 puts("Going to resume the thread");
1071 puts("Waiting until it terminates now");
1073 // wait until the thread terminates
1079 void TestThreadDelete()
1081 // As above, using Sleep() is only for testing here - we must use some
1082 // synchronisation object instead to ensure that the thread is still
1083 // running when we delete it - deleting a detached thread which already
1084 // terminated will lead to a crash!
1086 puts("\n*** Testing thread delete function ***");
1088 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1092 puts("\nDeleted a thread which didn't start to run yet.");
1094 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1098 wxThread::Sleep(300);
1102 puts("\nDeleted a running thread.");
1104 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1108 wxThread::Sleep(300);
1114 puts("\nDeleted a sleeping thread.");
1116 MyJoinableThread
thread3(20);
1121 puts("\nDeleted a joinable thread.");
1123 MyJoinableThread
thread4(2);
1126 wxThread::Sleep(300);
1130 puts("\nDeleted a joinable thread which already terminated.");
1135 #endif // TEST_THREADS
1137 // ----------------------------------------------------------------------------
1139 // ----------------------------------------------------------------------------
1143 void PrintArray(const char* name
, const wxArrayString
& array
)
1145 printf("Dump of the array '%s'\n", name
);
1147 size_t nCount
= array
.GetCount();
1148 for ( size_t n
= 0; n
< nCount
; n
++ )
1150 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1154 #endif // TEST_ARRAYS
1156 // ----------------------------------------------------------------------------
1158 // ----------------------------------------------------------------------------
1162 #include "wx/timer.h"
1164 static void TestString()
1174 for (int i
= 0; i
< 1000000; ++i
)
1178 c
= "! How'ya doin'?";
1181 c
= "Hello world! What's up?";
1186 printf ("TestString elapsed time: %ld\n", sw
.Time());
1189 static void TestPChar()
1197 for (int i
= 0; i
< 1000000; ++i
)
1199 strcpy (a
, "Hello");
1200 strcpy (b
, " world");
1201 strcpy (c
, "! How'ya doin'?");
1204 strcpy (c
, "Hello world! What's up?");
1205 if (strcmp (c
, a
) == 0)
1209 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1212 static void TestStringSub()
1214 wxString
s("Hello, world!");
1216 puts("*** Testing wxString substring extraction ***");
1218 printf("String = '%s'\n", s
.c_str());
1219 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1220 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1221 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1222 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1223 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1224 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1229 #endif // TEST_STRINGS
1231 // ----------------------------------------------------------------------------
1233 // ----------------------------------------------------------------------------
1235 int main(int argc
, char **argv
)
1237 if ( !wxInitialize() )
1239 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1249 #endif // TEST_STRINGS
1260 puts("*** Initially:");
1262 PrintArray("a1", a1
);
1264 wxArrayString
a2(a1
);
1265 PrintArray("a2", a2
);
1267 wxSortedArrayString
a3(a1
);
1268 PrintArray("a3", a3
);
1270 puts("*** After deleting a string from a1");
1273 PrintArray("a1", a1
);
1274 PrintArray("a2", a2
);
1275 PrintArray("a3", a3
);
1277 puts("*** After reassigning a1 to a2 and a3");
1279 PrintArray("a2", a2
);
1280 PrintArray("a3", a3
);
1281 #endif // TEST_ARRAYS
1289 for ( size_t n
= 0; n
< 8000; n
++ )
1291 s
<< (char)('A' + (n
% 26));
1295 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
1297 // this one shouldn't be truncated
1300 // but this one will because log functions use fixed size buffer
1301 // (note that it doesn't need '\n' at the end neither - will be added
1303 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
1307 int nCPUs
= wxThread::GetCPUCount();
1308 printf("This system has %d CPUs\n", nCPUs
);
1310 wxThread::SetConcurrency(nCPUs
);
1312 if ( argc
> 1 && argv
[1][0] == 't' )
1313 wxLog::AddTraceMask("thread");
1316 TestDetachedThreads();
1318 TestJoinableThreads();
1320 TestThreadSuspend();
1324 #endif // TEST_THREADS
1326 #ifdef TEST_LONGLONG
1331 #endif // TEST_LONGLONG
1351 TestTimeCompatibility();