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 // ----------------------------------------------------------------------------
37 //#define TEST_STRINGS
38 //#define TEST_THREADS
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());
198 #if wxUSE_LONGLONG_NATIVE
203 for ( n
= 0; n
< max
; n
++ )
208 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
210 #endif // wxUSE_LONGLONG_NATIVE
216 for ( n
= 0; n
< max
; n
++ )
221 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
225 static void TestDivision()
227 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
229 // seed pseudo random generator
230 //srand((unsigned)time(NULL));
234 for ( size_t n
= 0; n
< 10000; n
++ )
236 // get a random wxLongLong (shifting by 12 the MSB ensures that the
237 // multiplication will not overflow)
238 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
240 // get a random long (not wxLongLong for now) to divide it with
245 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
250 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
255 #endif // TEST_LONGLONG
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
265 #include <wx/datetime.h>
270 wxDateTime::wxDateTime_t day
;
271 wxDateTime::Month month
;
273 wxDateTime::wxDateTime_t hour
, min
, sec
;
275 wxDateTime::WeekDay wday
;
276 time_t gmticks
, ticks
;
278 void Init(const wxDateTime::Tm
& tm
)
287 gmticks
= ticks
= -1;
290 wxDateTime
DT() const
291 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
293 bool SameDay(const wxDateTime::Tm
& tm
) const
295 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
298 wxString
Format() const
301 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
303 wxDateTime::GetMonthName(month
).c_str(),
305 abs(wxDateTime::ConvertYearToBC(year
)),
306 year
> 0 ? "AD" : "BC");
310 wxString
FormatDate() const
313 s
.Printf("%02d-%s-%4d%s",
315 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
316 abs(wxDateTime::ConvertYearToBC(year
)),
317 year
> 0 ? "AD" : "BC");
322 static const Date testDates
[] =
324 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
325 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
326 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
327 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
328 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
329 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
330 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
331 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
332 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
333 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
334 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
335 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
336 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
337 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
338 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
341 // this test miscellaneous static wxDateTime functions
342 static void TestTimeStatic()
344 puts("\n*** wxDateTime static methods test ***");
346 // some info about the current date
347 int year
= wxDateTime::GetCurrentYear();
348 printf("Current year %d is %sa leap one and has %d days.\n",
350 wxDateTime::IsLeapYear(year
) ? "" : "not ",
351 wxDateTime::GetNumberOfDays(year
));
353 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
354 printf("Current month is '%s' ('%s') and it has %d days\n",
355 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
356 wxDateTime::GetMonthName(month
).c_str(),
357 wxDateTime::GetNumberOfDays(month
));
360 static const size_t nYears
= 5;
361 static const size_t years
[2][nYears
] =
363 // first line: the years to test
364 { 1990, 1976, 2000, 2030, 1984, },
366 // second line: TRUE if leap, FALSE otherwise
367 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
370 for ( size_t n
= 0; n
< nYears
; n
++ )
372 int year
= years
[0][n
];
373 bool should
= years
[1][n
] != 0,
374 is
= wxDateTime::IsLeapYear(year
);
376 printf("Year %d is %sa leap year (%s)\n",
379 should
== is
? "ok" : "ERROR");
381 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
385 // test constructing wxDateTime objects
386 static void TestTimeSet()
388 puts("\n*** wxDateTime construction test ***");
390 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
392 const Date
& d1
= testDates
[n
];
393 wxDateTime dt
= d1
.DT();
398 wxString s1
= d1
.Format(),
401 printf("Date: %s == %s (%s)\n",
402 s1
.c_str(), s2
.c_str(),
403 s1
== s2
? "ok" : "ERROR");
407 // test time zones stuff
408 static void TestTimeZones()
410 puts("\n*** wxDateTime timezone test ***");
412 wxDateTime now
= wxDateTime::Now();
414 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
415 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
416 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
417 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
418 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
419 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
422 // test some minimal support for the dates outside the standard range
423 static void TestTimeRange()
425 puts("\n*** wxDateTime out-of-standard-range dates test ***");
427 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
429 printf("Unix epoch:\t%s\n",
430 wxDateTime(2440587.5).Format(fmt
).c_str());
431 printf("Feb 29, 0: \t%s\n",
432 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
433 printf("JDN 0: \t%s\n",
434 wxDateTime(0.0).Format(fmt
).c_str());
435 printf("Jan 1, 1AD:\t%s\n",
436 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
437 printf("May 29, 2099:\t%s\n",
438 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
441 static void TestTimeTicks()
443 puts("\n*** wxDateTime ticks test ***");
445 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
447 const Date
& d
= testDates
[n
];
451 wxDateTime dt
= d
.DT();
452 long ticks
= (dt
.GetValue() / 1000).ToLong();
453 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
454 if ( ticks
== d
.ticks
)
460 printf(" (ERROR: should be %ld, delta = %ld)\n",
461 d
.ticks
, ticks
- d
.ticks
);
464 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
465 ticks
= (dt
.GetValue() / 1000).ToLong();
466 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
467 if ( ticks
== d
.gmticks
)
473 printf(" (ERROR: should be %ld, delta = %ld)\n",
474 d
.gmticks
, ticks
- d
.gmticks
);
481 // test conversions to JDN &c
482 static void TestTimeJDN()
484 puts("\n*** wxDateTime to JDN test ***");
486 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
488 const Date
& d
= testDates
[n
];
489 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
490 double jdn
= dt
.GetJulianDayNumber();
492 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
499 printf(" (ERROR: should be %f, delta = %f)\n",
505 // test week days computation
506 static void TestTimeWDays()
508 puts("\n*** wxDateTime weekday test ***");
512 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
514 const Date
& d
= testDates
[n
];
515 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
517 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
520 wxDateTime::GetWeekDayName(wday
).c_str());
521 if ( wday
== d
.wday
)
527 printf(" (ERROR: should be %s)\n",
528 wxDateTime::GetWeekDayName(d
.wday
).c_str());
534 // test SetToWeekDay()
535 struct WeekDateTestData
537 Date date
; // the real date (precomputed)
538 int nWeek
; // its week index in the month
539 wxDateTime::WeekDay wday
; // the weekday
540 wxDateTime::Month month
; // the month
541 int year
; // and the year
543 wxString
Format() const
546 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
548 case 1: which
= "first"; break;
549 case 2: which
= "second"; break;
550 case 3: which
= "third"; break;
551 case 4: which
= "fourth"; break;
552 case 5: which
= "fifth"; break;
554 case -1: which
= "last"; break;
559 which
+= " from end";
562 s
.Printf("The %s %s of %s in %d",
564 wxDateTime::GetWeekDayName(wday
).c_str(),
565 wxDateTime::GetMonthName(month
).c_str(),
572 // the array data was generated by the following python program
574 from DateTime import *
575 from whrandom import *
578 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
579 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
581 week = DateTimeDelta(7)
584 year = randint(1900, 2100)
585 month = randint(1, 12)
587 dt = DateTime(year, month, day)
588 wday = dt.day_of_week
590 countFromEnd = choice([-1, 1])
593 while dt.month is month:
594 dt = dt - countFromEnd * week
595 weekNum = weekNum + countFromEnd
597 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
599 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
600 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
603 static const WeekDateTestData weekDatesTestData
[] =
605 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
606 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
607 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
608 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
609 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
610 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
611 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
612 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
613 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
614 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
615 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
616 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
617 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
618 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
619 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
620 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
621 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
622 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
623 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
624 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
627 static const char *fmt
= "%d-%b-%Y";
630 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
632 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
634 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
636 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
638 const Date
& d
= wd
.date
;
639 if ( d
.SameDay(dt
.GetTm()) )
645 dt
.Set(d
.day
, d
.month
, d
.year
);
647 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
652 // test the computation of (ISO) week numbers
653 static void TestTimeWNumber()
655 puts("\n*** wxDateTime week number test ***");
657 struct WeekNumberTestData
659 Date date
; // the date
660 wxDateTime::wxDateTime_t week
; // the week number
661 wxDateTime::wxDateTime_t dnum
; // day number in the year
664 // data generated with the following python script:
666 from DateTime import *
667 from whrandom import *
670 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
671 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
674 year = randint(1900, 2100)
675 month = randint(1, 12)
677 dt = DateTime(year, month, day)
678 dayNum = dt.day_of_year
679 weekNum = dt.iso_week[1]
681 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) }
683 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\
684 "%(dayNum)s }," % data
686 static const WeekNumberTestData weekNumberTestDates
[] =
688 { { 2, wxDateTime::Jul
, 2093 }, 27, 183 },
689 { { 25, wxDateTime::Jun
, 1986 }, 26, 176 },
690 { { 15, wxDateTime::Jun
, 2014 }, 24, 166 },
691 { { 20, wxDateTime::Jul
, 2018 }, 29, 201 },
692 { { 3, wxDateTime::Aug
, 2074 }, 31, 215 },
693 { { 26, wxDateTime::Jul
, 2012 }, 30, 208 },
694 { { 4, wxDateTime::Nov
, 1915 }, 44, 308 },
695 { { 11, wxDateTime::Feb
, 2035 }, 6, 42 },
696 { { 15, wxDateTime::Feb
, 1942 }, 7, 46 },
697 { { 5, wxDateTime::Jan
, 2087 }, 1, 5 },
698 { { 6, wxDateTime::Nov
, 2016 }, 44, 311 },
699 { { 6, wxDateTime::Jun
, 2057 }, 23, 157 },
700 { { 25, wxDateTime::Feb
, 1976 }, 9, 56 },
701 { { 12, wxDateTime::Jan
, 2073 }, 2, 12 },
702 { { 12, wxDateTime::Sep
, 2040 }, 37, 256 },
703 { { 15, wxDateTime::Jul
, 1931 }, 29, 196 },
704 { { 23, wxDateTime::Mar
, 2084 }, 12, 83 },
705 { { 12, wxDateTime::Dec
, 1970 }, 50, 346 },
706 { { 6, wxDateTime::Sep
, 1996 }, 36, 250 },
707 { { 7, wxDateTime::Jan
, 2076 }, 2, 7 },
710 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
712 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
713 const Date
& d
= wn
.date
;
715 wxDateTime dt
= d
.DT();
717 wxDateTime::wxDateTime_t week
= dt
.GetWeekOfYear(),
718 dnum
= dt
.GetDayOfYear();
720 printf("%s: the day number is %d",
721 d
.FormatDate().c_str(), dnum
);
722 if ( dnum
== wn
.dnum
)
728 printf(" (ERROR: should be %d)", wn
.dnum
);
731 printf(", week number is %d", week
);
732 if ( week
== wn
.week
)
738 printf(" (ERROR: should be %d)\n", wn
.week
);
743 // test DST calculations
744 static void TestTimeDST()
746 puts("\n*** wxDateTime DST test ***");
748 printf("DST is%s in effect now.\n\n",
749 wxDateTime::Now().IsDST() ? "" : " not");
751 // taken from http://www.energy.ca.gov/daylightsaving.html
752 static const Date datesDST
[2][2004 - 1900 + 1] =
755 { 1, wxDateTime::Apr
, 1990 },
756 { 7, wxDateTime::Apr
, 1991 },
757 { 5, wxDateTime::Apr
, 1992 },
758 { 4, wxDateTime::Apr
, 1993 },
759 { 3, wxDateTime::Apr
, 1994 },
760 { 2, wxDateTime::Apr
, 1995 },
761 { 7, wxDateTime::Apr
, 1996 },
762 { 6, wxDateTime::Apr
, 1997 },
763 { 5, wxDateTime::Apr
, 1998 },
764 { 4, wxDateTime::Apr
, 1999 },
765 { 2, wxDateTime::Apr
, 2000 },
766 { 1, wxDateTime::Apr
, 2001 },
767 { 7, wxDateTime::Apr
, 2002 },
768 { 6, wxDateTime::Apr
, 2003 },
769 { 4, wxDateTime::Apr
, 2004 },
772 { 28, wxDateTime::Oct
, 1990 },
773 { 27, wxDateTime::Oct
, 1991 },
774 { 25, wxDateTime::Oct
, 1992 },
775 { 31, wxDateTime::Oct
, 1993 },
776 { 30, wxDateTime::Oct
, 1994 },
777 { 29, wxDateTime::Oct
, 1995 },
778 { 27, wxDateTime::Oct
, 1996 },
779 { 26, wxDateTime::Oct
, 1997 },
780 { 25, wxDateTime::Oct
, 1998 },
781 { 31, wxDateTime::Oct
, 1999 },
782 { 29, wxDateTime::Oct
, 2000 },
783 { 28, wxDateTime::Oct
, 2001 },
784 { 27, wxDateTime::Oct
, 2002 },
785 { 26, wxDateTime::Oct
, 2003 },
786 { 31, wxDateTime::Oct
, 2004 },
791 for ( year
= 1990; year
< 2005; year
++ )
793 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
794 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
796 printf("DST period in the US for year %d: from %s to %s",
797 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
799 size_t n
= year
- 1990;
800 const Date
& dBegin
= datesDST
[0][n
];
801 const Date
& dEnd
= datesDST
[1][n
];
803 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
809 printf(" (ERROR: should be %s %d to %s %d)\n",
810 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
811 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
817 for ( year
= 1990; year
< 2005; year
++ )
819 printf("DST period in Europe for year %d: from %s to %s\n",
821 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
822 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
826 // test wxDateTime -> text conversion
827 static void TestTimeFormat()
829 puts("\n*** wxDateTime formatting test ***");
831 // some information may be lost during conversion, so store what kind
832 // of info should we recover after a round trip
835 CompareNone
, // don't try comparing
836 CompareBoth
, // dates and times should be identical
837 CompareDate
, // dates only
838 CompareTime
// time only
843 CompareKind compareKind
;
845 } formatTestFormats
[] =
847 { CompareBoth
, "---> %c" },
848 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
849 { CompareBoth
, "Date is %x, time is %X" },
850 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
851 { CompareNone
, "The day of year: %j, the week of year: %W" },
854 static const Date formatTestDates
[] =
856 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
857 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
859 // this test can't work for other centuries because it uses two digit
860 // years in formats, so don't even try it
861 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
862 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
863 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
867 // an extra test (as it doesn't depend on date, don't do it in the loop)
868 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
870 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
874 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
875 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
877 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
878 printf("%s", s
.c_str());
880 // what can we recover?
881 int kind
= formatTestFormats
[n
].compareKind
;
885 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
888 // converion failed - should it have?
889 if ( kind
== CompareNone
)
892 puts(" (ERROR: conversion back failed)");
896 // should have parsed the entire string
897 puts(" (ERROR: conversion back stopped too soon)");
901 bool equal
= FALSE
; // suppress compilaer warning
909 equal
= dt
.IsSameDate(dt2
);
913 equal
= dt
.IsSameTime(dt2
);
919 printf(" (ERROR: got back '%s' instead of '%s')\n",
920 dt2
.Format().c_str(), dt
.Format().c_str());
931 // test text -> wxDateTime conversion
932 static void TestTimeParse()
934 puts("\n*** wxDateTime parse test ***");
943 static const ParseTestData parseTestDates
[] =
945 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
946 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
949 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
951 const char *format
= parseTestDates
[n
].format
;
953 printf("%s => ", format
);
956 if ( dt
.ParseRfc822Date(format
) )
958 printf("%s ", dt
.Format().c_str());
960 if ( parseTestDates
[n
].good
)
962 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
969 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
974 puts("(ERROR: bad format)");
979 printf("bad format (%s)\n",
980 parseTestDates
[n
].good
? "ERROR" : "ok");
987 // test compatibility with the old wxDate/wxTime classes
988 static void TestTimeCompatibility()
990 puts("\n*** wxDateTime compatibility test ***");
992 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
993 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
995 double jdnNow
= wxDateTime::Now().GetJDN();
996 long jdnMidnight
= (long)(jdnNow
- 0.5);
997 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
999 jdnMidnight
= wxDate().Set().GetJulianDate();
1000 printf("wxDateTime for today: %s\n",
1001 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1003 int flags
= wxEUROPEAN
;//wxFULL;
1006 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1007 for ( int n
= 0; n
< 7; n
++ )
1009 printf("Previous %s is %s\n",
1010 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1011 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1019 // ----------------------------------------------------------------------------
1021 // ----------------------------------------------------------------------------
1025 #include <wx/thread.h>
1027 static size_t gs_counter
= (size_t)-1;
1028 static wxCriticalSection gs_critsect
;
1029 static wxCondition gs_cond
;
1031 class MyJoinableThread
: public wxThread
1034 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1035 { m_n
= n
; Create(); }
1037 // thread execution starts here
1038 virtual ExitCode
Entry();
1044 wxThread::ExitCode
MyJoinableThread::Entry()
1046 unsigned long res
= 1;
1047 for ( size_t n
= 1; n
< m_n
; n
++ )
1051 // it's a loooong calculation :-)
1055 return (ExitCode
)res
;
1058 class MyDetachedThread
: public wxThread
1061 MyDetachedThread(size_t n
, char ch
)
1065 m_cancelled
= FALSE
;
1070 // thread execution starts here
1071 virtual ExitCode
Entry();
1074 virtual void OnExit();
1077 size_t m_n
; // number of characters to write
1078 char m_ch
; // character to write
1080 bool m_cancelled
; // FALSE if we exit normally
1083 wxThread::ExitCode
MyDetachedThread::Entry()
1086 wxCriticalSectionLocker
lock(gs_critsect
);
1087 if ( gs_counter
== (size_t)-1 )
1093 for ( size_t n
= 0; n
< m_n
; n
++ )
1095 if ( TestDestroy() )
1105 wxThread::Sleep(100);
1111 void MyDetachedThread::OnExit()
1113 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1115 wxCriticalSectionLocker
lock(gs_critsect
);
1116 if ( !--gs_counter
&& !m_cancelled
)
1120 void TestDetachedThreads()
1122 puts("\n*** Testing detached threads ***");
1124 static const size_t nThreads
= 3;
1125 MyDetachedThread
*threads
[nThreads
];
1127 for ( n
= 0; n
< nThreads
; n
++ )
1129 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1132 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1133 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1135 for ( n
= 0; n
< nThreads
; n
++ )
1140 // wait until all threads terminate
1146 void TestJoinableThreads()
1148 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1150 // calc 10! in the background
1151 MyJoinableThread
thread(10);
1154 printf("\nThread terminated with exit code %lu.\n",
1155 (unsigned long)thread
.Wait());
1158 void TestThreadSuspend()
1160 puts("\n*** Testing thread suspend/resume functions ***");
1162 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1166 // this is for this demo only, in a real life program we'd use another
1167 // condition variable which would be signaled from wxThread::Entry() to
1168 // tell us that the thread really started running - but here just wait a
1169 // bit and hope that it will be enough (the problem is, of course, that
1170 // the thread might still not run when we call Pause() which will result
1172 wxThread::Sleep(300);
1174 for ( size_t n
= 0; n
< 3; n
++ )
1178 puts("\nThread suspended");
1181 // don't sleep but resume immediately the first time
1182 wxThread::Sleep(300);
1184 puts("Going to resume the thread");
1189 puts("Waiting until it terminates now");
1191 // wait until the thread terminates
1197 void TestThreadDelete()
1199 // As above, using Sleep() is only for testing here - we must use some
1200 // synchronisation object instead to ensure that the thread is still
1201 // running when we delete it - deleting a detached thread which already
1202 // terminated will lead to a crash!
1204 puts("\n*** Testing thread delete function ***");
1206 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1210 puts("\nDeleted a thread which didn't start to run yet.");
1212 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1216 wxThread::Sleep(300);
1220 puts("\nDeleted a running thread.");
1222 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1226 wxThread::Sleep(300);
1232 puts("\nDeleted a sleeping thread.");
1234 MyJoinableThread
thread3(20);
1239 puts("\nDeleted a joinable thread.");
1241 MyJoinableThread
thread4(2);
1244 wxThread::Sleep(300);
1248 puts("\nDeleted a joinable thread which already terminated.");
1253 #endif // TEST_THREADS
1255 // ----------------------------------------------------------------------------
1257 // ----------------------------------------------------------------------------
1261 void PrintArray(const char* name
, const wxArrayString
& array
)
1263 printf("Dump of the array '%s'\n", name
);
1265 size_t nCount
= array
.GetCount();
1266 for ( size_t n
= 0; n
< nCount
; n
++ )
1268 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1272 #endif // TEST_ARRAYS
1274 // ----------------------------------------------------------------------------
1276 // ----------------------------------------------------------------------------
1280 #include "wx/timer.h"
1282 static void TestString()
1292 for (int i
= 0; i
< 1000000; ++i
)
1296 c
= "! How'ya doin'?";
1299 c
= "Hello world! What's up?";
1304 printf ("TestString elapsed time: %ld\n", sw
.Time());
1307 static void TestPChar()
1315 for (int i
= 0; i
< 1000000; ++i
)
1317 strcpy (a
, "Hello");
1318 strcpy (b
, " world");
1319 strcpy (c
, "! How'ya doin'?");
1322 strcpy (c
, "Hello world! What's up?");
1323 if (strcmp (c
, a
) == 0)
1327 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1330 static void TestStringSub()
1332 wxString
s("Hello, world!");
1334 puts("*** Testing wxString substring extraction ***");
1336 printf("String = '%s'\n", s
.c_str());
1337 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1338 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1339 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1340 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1341 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1342 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1347 static void TestStringFormat()
1349 puts("*** Testing wxString formatting ***");
1352 s
.Printf("%03d", 18);
1354 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
1355 printf("Number 18: %s\n", s
.c_str());
1360 #endif // TEST_STRINGS
1362 // ----------------------------------------------------------------------------
1364 // ----------------------------------------------------------------------------
1366 int main(int argc
, char **argv
)
1368 if ( !wxInitialize() )
1370 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1384 #endif // TEST_STRINGS
1395 puts("*** Initially:");
1397 PrintArray("a1", a1
);
1399 wxArrayString
a2(a1
);
1400 PrintArray("a2", a2
);
1402 wxSortedArrayString
a3(a1
);
1403 PrintArray("a3", a3
);
1405 puts("*** After deleting a string from a1");
1408 PrintArray("a1", a1
);
1409 PrintArray("a2", a2
);
1410 PrintArray("a3", a3
);
1412 puts("*** After reassigning a1 to a2 and a3");
1414 PrintArray("a2", a2
);
1415 PrintArray("a3", a3
);
1416 #endif // TEST_ARRAYS
1424 for ( size_t n
= 0; n
< 8000; n
++ )
1426 s
<< (char)('A' + (n
% 26));
1430 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
1432 // this one shouldn't be truncated
1435 // but this one will because log functions use fixed size buffer
1436 // (note that it doesn't need '\n' at the end neither - will be added
1438 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
1442 int nCPUs
= wxThread::GetCPUCount();
1443 printf("This system has %d CPUs\n", nCPUs
);
1445 wxThread::SetConcurrency(nCPUs
);
1447 if ( argc
> 1 && argv
[1][0] == 't' )
1448 wxLog::AddTraceMask("thread");
1451 TestDetachedThreads();
1453 TestJoinableThreads();
1455 TestThreadSuspend();
1459 #endif // TEST_THREADS
1461 #ifdef TEST_LONGLONG
1466 #endif // TEST_LONGLONG