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 // ----------------------------------------------------------------------------
35 //#define TEST_LONGLONG
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 puts("*** Testing wxLongLong division ***\n");
229 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
231 // seed pseudo random generator
232 srand((unsigned)time(NULL
));
236 for ( size_t n
= 0; n
< 100000; n
++ )
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());
242 // get a random long (not wxLongLong for now) to divide it with
248 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
250 if ( !(nTested
% 1000) )
264 #endif // TEST_LONGLONG
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
274 #include <wx/datetime.h>
279 wxDateTime::wxDateTime_t day
;
280 wxDateTime::Month month
;
282 wxDateTime::wxDateTime_t hour
, min
, sec
;
284 wxDateTime::WeekDay wday
;
285 time_t gmticks
, ticks
;
287 void Init(const wxDateTime::Tm
& tm
)
296 gmticks
= ticks
= -1;
299 wxDateTime
DT() const
300 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
302 bool SameDay(const wxDateTime::Tm
& tm
) const
304 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
307 wxString
Format() const
310 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
312 wxDateTime::GetMonthName(month
).c_str(),
314 abs(wxDateTime::ConvertYearToBC(year
)),
315 year
> 0 ? "AD" : "BC");
319 wxString
FormatDate() const
322 s
.Printf("%02d-%s-%4d%s",
324 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
325 abs(wxDateTime::ConvertYearToBC(year
)),
326 year
> 0 ? "AD" : "BC");
331 static const Date testDates
[] =
333 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
334 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
335 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
336 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
337 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
338 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
339 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
340 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
341 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
342 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
343 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
344 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
345 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
346 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
347 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
350 // this test miscellaneous static wxDateTime functions
351 static void TestTimeStatic()
353 puts("\n*** wxDateTime static methods test ***");
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",
359 wxDateTime::IsLeapYear(year
) ? "" : "not ",
360 wxDateTime::GetNumberOfDays(year
));
362 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
363 printf("Current month is '%s' ('%s') and it has %d days\n",
364 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
365 wxDateTime::GetMonthName(month
).c_str(),
366 wxDateTime::GetNumberOfDays(month
));
369 static const size_t nYears
= 5;
370 static const size_t years
[2][nYears
] =
372 // first line: the years to test
373 { 1990, 1976, 2000, 2030, 1984, },
375 // second line: TRUE if leap, FALSE otherwise
376 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
379 for ( size_t n
= 0; n
< nYears
; n
++ )
381 int year
= years
[0][n
];
382 bool should
= years
[1][n
] != 0,
383 is
= wxDateTime::IsLeapYear(year
);
385 printf("Year %d is %sa leap year (%s)\n",
388 should
== is
? "ok" : "ERROR");
390 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
394 // test constructing wxDateTime objects
395 static void TestTimeSet()
397 puts("\n*** wxDateTime construction test ***");
399 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
401 const Date
& d1
= testDates
[n
];
402 wxDateTime dt
= d1
.DT();
407 wxString s1
= d1
.Format(),
410 printf("Date: %s == %s (%s)\n",
411 s1
.c_str(), s2
.c_str(),
412 s1
== s2
? "ok" : "ERROR");
416 // test time zones stuff
417 static void TestTimeZones()
419 puts("\n*** wxDateTime timezone test ***");
421 wxDateTime now
= wxDateTime::Now();
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());
431 // test some minimal support for the dates outside the standard range
432 static void TestTimeRange()
434 puts("\n*** wxDateTime out-of-standard-range dates test ***");
436 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
438 printf("Unix epoch:\t%s\n",
439 wxDateTime(2440587.5).Format(fmt
).c_str());
440 printf("Feb 29, 0: \t%s\n",
441 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
442 printf("JDN 0: \t%s\n",
443 wxDateTime(0.0).Format(fmt
).c_str());
444 printf("Jan 1, 1AD:\t%s\n",
445 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
446 printf("May 29, 2099:\t%s\n",
447 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
450 static void TestTimeTicks()
452 puts("\n*** wxDateTime ticks test ***");
454 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
456 const Date
& d
= testDates
[n
];
460 wxDateTime dt
= d
.DT();
461 long ticks
= (dt
.GetValue() / 1000).ToLong();
462 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
463 if ( ticks
== d
.ticks
)
469 printf(" (ERROR: should be %ld, delta = %ld)\n",
470 d
.ticks
, ticks
- d
.ticks
);
473 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
474 ticks
= (dt
.GetValue() / 1000).ToLong();
475 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
476 if ( ticks
== d
.gmticks
)
482 printf(" (ERROR: should be %ld, delta = %ld)\n",
483 d
.gmticks
, ticks
- d
.gmticks
);
490 // test conversions to JDN &c
491 static void TestTimeJDN()
493 puts("\n*** wxDateTime to JDN test ***");
495 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
497 const Date
& d
= testDates
[n
];
498 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
499 double jdn
= dt
.GetJulianDayNumber();
501 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
508 printf(" (ERROR: should be %f, delta = %f)\n",
514 // test week days computation
515 static void TestTimeWDays()
517 puts("\n*** wxDateTime weekday test ***");
521 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
523 const Date
& d
= testDates
[n
];
524 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
526 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
529 wxDateTime::GetWeekDayName(wday
).c_str());
530 if ( wday
== d
.wday
)
536 printf(" (ERROR: should be %s)\n",
537 wxDateTime::GetWeekDayName(d
.wday
).c_str());
543 // test SetToWeekDay()
544 struct WeekDateTestData
546 Date date
; // the real date (precomputed)
547 int nWeek
; // its week index in the month
548 wxDateTime::WeekDay wday
; // the weekday
549 wxDateTime::Month month
; // the month
550 int year
; // and the year
552 wxString
Format() const
555 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
557 case 1: which
= "first"; break;
558 case 2: which
= "second"; break;
559 case 3: which
= "third"; break;
560 case 4: which
= "fourth"; break;
561 case 5: which
= "fifth"; break;
563 case -1: which
= "last"; break;
568 which
+= " from end";
571 s
.Printf("The %s %s of %s in %d",
573 wxDateTime::GetWeekDayName(wday
).c_str(),
574 wxDateTime::GetMonthName(month
).c_str(),
581 // the array data was generated by the following python program
583 from DateTime import *
584 from whrandom import *
587 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
588 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
590 week = DateTimeDelta(7)
593 year = randint(1900, 2100)
594 month = randint(1, 12)
596 dt = DateTime(year, month, day)
597 wday = dt.day_of_week
599 countFromEnd = choice([-1, 1])
602 while dt.month is month:
603 dt = dt - countFromEnd * week
604 weekNum = weekNum + countFromEnd
606 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
608 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
609 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
612 static const WeekDateTestData weekDatesTestData
[] =
614 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
615 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
616 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
617 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
618 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
619 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
620 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
621 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
622 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
623 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
624 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
625 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
626 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
627 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
628 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
629 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
630 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
631 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
632 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
633 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
636 static const char *fmt
= "%d-%b-%Y";
639 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
641 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
643 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
645 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
647 const Date
& d
= wd
.date
;
648 if ( d
.SameDay(dt
.GetTm()) )
654 dt
.Set(d
.day
, d
.month
, d
.year
);
656 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
661 // test the computation of (ISO) week numbers
662 static void TestTimeWNumber()
664 puts("\n*** wxDateTime week number test ***");
666 struct WeekNumberTestData
668 Date date
; // the date
669 wxDateTime::wxDateTime_t week
; // the week number
670 wxDateTime::wxDateTime_t dnum
; // day number in the year
673 // data generated with the following python script:
675 from DateTime import *
676 from whrandom import *
679 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
680 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
683 year = randint(1900, 2100)
684 month = randint(1, 12)
686 dt = DateTime(year, month, day)
687 dayNum = dt.day_of_year
688 weekNum = dt.iso_week[1]
690 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) }
692 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\
693 "%(dayNum)s }," % data
695 static const WeekNumberTestData weekNumberTestDates
[] =
697 { { 2, wxDateTime::Jul
, 2093 }, 27, 183 },
698 { { 25, wxDateTime::Jun
, 1986 }, 26, 176 },
699 { { 15, wxDateTime::Jun
, 2014 }, 24, 166 },
700 { { 20, wxDateTime::Jul
, 2018 }, 29, 201 },
701 { { 3, wxDateTime::Aug
, 2074 }, 31, 215 },
702 { { 26, wxDateTime::Jul
, 2012 }, 30, 208 },
703 { { 4, wxDateTime::Nov
, 1915 }, 44, 308 },
704 { { 11, wxDateTime::Feb
, 2035 }, 6, 42 },
705 { { 15, wxDateTime::Feb
, 1942 }, 7, 46 },
706 { { 5, wxDateTime::Jan
, 2087 }, 1, 5 },
707 { { 6, wxDateTime::Nov
, 2016 }, 44, 311 },
708 { { 6, wxDateTime::Jun
, 2057 }, 23, 157 },
709 { { 25, wxDateTime::Feb
, 1976 }, 9, 56 },
710 { { 12, wxDateTime::Jan
, 2073 }, 2, 12 },
711 { { 12, wxDateTime::Sep
, 2040 }, 37, 256 },
712 { { 15, wxDateTime::Jul
, 1931 }, 29, 196 },
713 { { 23, wxDateTime::Mar
, 2084 }, 12, 83 },
714 { { 12, wxDateTime::Dec
, 1970 }, 50, 346 },
715 { { 6, wxDateTime::Sep
, 1996 }, 36, 250 },
716 { { 7, wxDateTime::Jan
, 2076 }, 2, 7 },
719 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
721 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
722 const Date
& d
= wn
.date
;
724 wxDateTime dt
= d
.DT();
726 wxDateTime::wxDateTime_t week
= dt
.GetWeekOfYear(),
727 dnum
= dt
.GetDayOfYear();
729 printf("%s: the day number is %d",
730 d
.FormatDate().c_str(), dnum
);
731 if ( dnum
== wn
.dnum
)
737 printf(" (ERROR: should be %d)", wn
.dnum
);
740 printf(", week number is %d", week
);
741 if ( week
== wn
.week
)
747 printf(" (ERROR: should be %d)\n", wn
.week
);
752 // test DST calculations
753 static void TestTimeDST()
755 puts("\n*** wxDateTime DST test ***");
757 printf("DST is%s in effect now.\n\n",
758 wxDateTime::Now().IsDST() ? "" : " not");
760 // taken from http://www.energy.ca.gov/daylightsaving.html
761 static const Date datesDST
[2][2004 - 1900 + 1] =
764 { 1, wxDateTime::Apr
, 1990 },
765 { 7, wxDateTime::Apr
, 1991 },
766 { 5, wxDateTime::Apr
, 1992 },
767 { 4, wxDateTime::Apr
, 1993 },
768 { 3, wxDateTime::Apr
, 1994 },
769 { 2, wxDateTime::Apr
, 1995 },
770 { 7, wxDateTime::Apr
, 1996 },
771 { 6, wxDateTime::Apr
, 1997 },
772 { 5, wxDateTime::Apr
, 1998 },
773 { 4, wxDateTime::Apr
, 1999 },
774 { 2, wxDateTime::Apr
, 2000 },
775 { 1, wxDateTime::Apr
, 2001 },
776 { 7, wxDateTime::Apr
, 2002 },
777 { 6, wxDateTime::Apr
, 2003 },
778 { 4, wxDateTime::Apr
, 2004 },
781 { 28, wxDateTime::Oct
, 1990 },
782 { 27, wxDateTime::Oct
, 1991 },
783 { 25, wxDateTime::Oct
, 1992 },
784 { 31, wxDateTime::Oct
, 1993 },
785 { 30, wxDateTime::Oct
, 1994 },
786 { 29, wxDateTime::Oct
, 1995 },
787 { 27, wxDateTime::Oct
, 1996 },
788 { 26, wxDateTime::Oct
, 1997 },
789 { 25, wxDateTime::Oct
, 1998 },
790 { 31, wxDateTime::Oct
, 1999 },
791 { 29, wxDateTime::Oct
, 2000 },
792 { 28, wxDateTime::Oct
, 2001 },
793 { 27, wxDateTime::Oct
, 2002 },
794 { 26, wxDateTime::Oct
, 2003 },
795 { 31, wxDateTime::Oct
, 2004 },
800 for ( year
= 1990; year
< 2005; year
++ )
802 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
803 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
805 printf("DST period in the US for year %d: from %s to %s",
806 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
808 size_t n
= year
- 1990;
809 const Date
& dBegin
= datesDST
[0][n
];
810 const Date
& dEnd
= datesDST
[1][n
];
812 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
818 printf(" (ERROR: should be %s %d to %s %d)\n",
819 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
820 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
826 for ( year
= 1990; year
< 2005; year
++ )
828 printf("DST period in Europe for year %d: from %s to %s\n",
830 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
831 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
835 // test wxDateTime -> text conversion
836 static void TestTimeFormat()
838 puts("\n*** wxDateTime formatting test ***");
840 // some information may be lost during conversion, so store what kind
841 // of info should we recover after a round trip
844 CompareNone
, // don't try comparing
845 CompareBoth
, // dates and times should be identical
846 CompareDate
, // dates only
847 CompareTime
// time only
852 CompareKind compareKind
;
854 } formatTestFormats
[] =
856 { CompareBoth
, "---> %c" },
857 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
858 { CompareBoth
, "Date is %x, time is %X" },
859 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
860 { CompareNone
, "The day of year: %j, the week of year: %W" },
863 static const Date formatTestDates
[] =
865 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
866 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
868 // this test can't work for other centuries because it uses two digit
869 // years in formats, so don't even try it
870 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
871 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
872 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
876 // an extra test (as it doesn't depend on date, don't do it in the loop)
877 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
879 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
883 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
884 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
886 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
887 printf("%s", s
.c_str());
889 // what can we recover?
890 int kind
= formatTestFormats
[n
].compareKind
;
894 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
897 // converion failed - should it have?
898 if ( kind
== CompareNone
)
901 puts(" (ERROR: conversion back failed)");
905 // should have parsed the entire string
906 puts(" (ERROR: conversion back stopped too soon)");
910 bool equal
= FALSE
; // suppress compilaer warning
918 equal
= dt
.IsSameDate(dt2
);
922 equal
= dt
.IsSameTime(dt2
);
928 printf(" (ERROR: got back '%s' instead of '%s')\n",
929 dt2
.Format().c_str(), dt
.Format().c_str());
940 // test text -> wxDateTime conversion
941 static void TestTimeParse()
943 puts("\n*** wxDateTime parse test ***");
952 static const ParseTestData parseTestDates
[] =
954 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
955 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
958 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
960 const char *format
= parseTestDates
[n
].format
;
962 printf("%s => ", format
);
965 if ( dt
.ParseRfc822Date(format
) )
967 printf("%s ", dt
.Format().c_str());
969 if ( parseTestDates
[n
].good
)
971 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
978 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
983 puts("(ERROR: bad format)");
988 printf("bad format (%s)\n",
989 parseTestDates
[n
].good
? "ERROR" : "ok");
996 // test compatibility with the old wxDate/wxTime classes
997 static void TestTimeCompatibility()
999 puts("\n*** wxDateTime compatibility test ***");
1001 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1002 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1004 double jdnNow
= wxDateTime::Now().GetJDN();
1005 long jdnMidnight
= (long)(jdnNow
- 0.5);
1006 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
1008 jdnMidnight
= wxDate().Set().GetJulianDate();
1009 printf("wxDateTime for today: %s\n",
1010 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1012 int flags
= wxEUROPEAN
;//wxFULL;
1015 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1016 for ( int n
= 0; n
< 7; n
++ )
1018 printf("Previous %s is %s\n",
1019 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1020 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1028 // ----------------------------------------------------------------------------
1030 // ----------------------------------------------------------------------------
1034 #include <wx/thread.h>
1036 static size_t gs_counter
= (size_t)-1;
1037 static wxCriticalSection gs_critsect
;
1038 static wxCondition gs_cond
;
1040 class MyJoinableThread
: public wxThread
1043 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1044 { m_n
= n
; Create(); }
1046 // thread execution starts here
1047 virtual ExitCode
Entry();
1053 wxThread::ExitCode
MyJoinableThread::Entry()
1055 unsigned long res
= 1;
1056 for ( size_t n
= 1; n
< m_n
; n
++ )
1060 // it's a loooong calculation :-)
1064 return (ExitCode
)res
;
1067 class MyDetachedThread
: public wxThread
1070 MyDetachedThread(size_t n
, char ch
)
1074 m_cancelled
= FALSE
;
1079 // thread execution starts here
1080 virtual ExitCode
Entry();
1083 virtual void OnExit();
1086 size_t m_n
; // number of characters to write
1087 char m_ch
; // character to write
1089 bool m_cancelled
; // FALSE if we exit normally
1092 wxThread::ExitCode
MyDetachedThread::Entry()
1095 wxCriticalSectionLocker
lock(gs_critsect
);
1096 if ( gs_counter
== (size_t)-1 )
1102 for ( size_t n
= 0; n
< m_n
; n
++ )
1104 if ( TestDestroy() )
1114 wxThread::Sleep(100);
1120 void MyDetachedThread::OnExit()
1122 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1124 wxCriticalSectionLocker
lock(gs_critsect
);
1125 if ( !--gs_counter
&& !m_cancelled
)
1129 void TestDetachedThreads()
1131 puts("\n*** Testing detached threads ***");
1133 static const size_t nThreads
= 3;
1134 MyDetachedThread
*threads
[nThreads
];
1136 for ( n
= 0; n
< nThreads
; n
++ )
1138 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1141 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1142 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1144 for ( n
= 0; n
< nThreads
; n
++ )
1149 // wait until all threads terminate
1155 void TestJoinableThreads()
1157 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1159 // calc 10! in the background
1160 MyJoinableThread
thread(10);
1163 printf("\nThread terminated with exit code %lu.\n",
1164 (unsigned long)thread
.Wait());
1167 void TestThreadSuspend()
1169 puts("\n*** Testing thread suspend/resume functions ***");
1171 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1175 // this is for this demo only, in a real life program we'd use another
1176 // condition variable which would be signaled from wxThread::Entry() to
1177 // tell us that the thread really started running - but here just wait a
1178 // bit and hope that it will be enough (the problem is, of course, that
1179 // the thread might still not run when we call Pause() which will result
1181 wxThread::Sleep(300);
1183 for ( size_t n
= 0; n
< 3; n
++ )
1187 puts("\nThread suspended");
1190 // don't sleep but resume immediately the first time
1191 wxThread::Sleep(300);
1193 puts("Going to resume the thread");
1198 puts("Waiting until it terminates now");
1200 // wait until the thread terminates
1206 void TestThreadDelete()
1208 // As above, using Sleep() is only for testing here - we must use some
1209 // synchronisation object instead to ensure that the thread is still
1210 // running when we delete it - deleting a detached thread which already
1211 // terminated will lead to a crash!
1213 puts("\n*** Testing thread delete function ***");
1215 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1219 puts("\nDeleted a thread which didn't start to run yet.");
1221 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1225 wxThread::Sleep(300);
1229 puts("\nDeleted a running thread.");
1231 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1235 wxThread::Sleep(300);
1241 puts("\nDeleted a sleeping thread.");
1243 MyJoinableThread
thread3(20);
1248 puts("\nDeleted a joinable thread.");
1250 MyJoinableThread
thread4(2);
1253 wxThread::Sleep(300);
1257 puts("\nDeleted a joinable thread which already terminated.");
1262 #endif // TEST_THREADS
1264 // ----------------------------------------------------------------------------
1266 // ----------------------------------------------------------------------------
1270 void PrintArray(const char* name
, const wxArrayString
& array
)
1272 printf("Dump of the array '%s'\n", name
);
1274 size_t nCount
= array
.GetCount();
1275 for ( size_t n
= 0; n
< nCount
; n
++ )
1277 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1281 #endif // TEST_ARRAYS
1283 // ----------------------------------------------------------------------------
1285 // ----------------------------------------------------------------------------
1289 #include "wx/timer.h"
1291 static void TestString()
1301 for (int i
= 0; i
< 1000000; ++i
)
1305 c
= "! How'ya doin'?";
1308 c
= "Hello world! What's up?";
1313 printf ("TestString elapsed time: %ld\n", sw
.Time());
1316 static void TestPChar()
1324 for (int i
= 0; i
< 1000000; ++i
)
1326 strcpy (a
, "Hello");
1327 strcpy (b
, " world");
1328 strcpy (c
, "! How'ya doin'?");
1331 strcpy (c
, "Hello world! What's up?");
1332 if (strcmp (c
, a
) == 0)
1336 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1339 static void TestStringSub()
1341 wxString
s("Hello, world!");
1343 puts("*** Testing wxString substring extraction ***");
1345 printf("String = '%s'\n", s
.c_str());
1346 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1347 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1348 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1349 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1350 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1351 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1356 static void TestStringFormat()
1358 puts("*** Testing wxString formatting ***");
1361 s
.Printf("%03d", 18);
1363 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
1364 printf("Number 18: %s\n", s
.c_str());
1369 #endif // TEST_STRINGS
1371 // ----------------------------------------------------------------------------
1373 // ----------------------------------------------------------------------------
1375 int main(int argc
, char **argv
)
1377 if ( !wxInitialize() )
1379 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1393 #endif // TEST_STRINGS
1404 puts("*** Initially:");
1406 PrintArray("a1", a1
);
1408 wxArrayString
a2(a1
);
1409 PrintArray("a2", a2
);
1411 wxSortedArrayString
a3(a1
);
1412 PrintArray("a3", a3
);
1414 puts("*** After deleting a string from a1");
1417 PrintArray("a1", a1
);
1418 PrintArray("a2", a2
);
1419 PrintArray("a3", a3
);
1421 puts("*** After reassigning a1 to a2 and a3");
1423 PrintArray("a2", a2
);
1424 PrintArray("a3", a3
);
1425 #endif // TEST_ARRAYS
1433 for ( size_t n
= 0; n
< 8000; n
++ )
1435 s
<< (char)('A' + (n
% 26));
1439 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
1441 // this one shouldn't be truncated
1444 // but this one will because log functions use fixed size buffer
1445 // (note that it doesn't need '\n' at the end neither - will be added
1447 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
1451 int nCPUs
= wxThread::GetCPUCount();
1452 printf("This system has %d CPUs\n", nCPUs
);
1454 wxThread::SetConcurrency(nCPUs
);
1456 if ( argc
> 1 && argv
[1][0] == 't' )
1457 wxLog::AddTraceMask("thread");
1460 TestDetachedThreads();
1462 TestJoinableThreads();
1464 TestThreadSuspend();
1468 #endif // TEST_THREADS
1470 #ifdef TEST_LONGLONG
1475 #endif // TEST_LONGLONG