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 wxDateTime -> text conversion 
 819 static void TestTimeFormat() 
 821     puts("\n*** wxDateTime formatting test ***"); 
 823     static const char *formatTestFormats
[] = 
 826         "Date is %A, %d of %B, in year %Y", 
 827         "Date is %x, time is %X", 
 828         "Time is %H:%M:%S or %I:%M:%S %p", 
 829         "The day of year: %j, the week of year: %W", 
 832     static const Date formatTestDates
[] = 
 835         { 29, wxDateTime::May
, 1976, 18, 30, 00 }, 
 836         { 31, wxDateTime::Dec
, 1999, 23, 30, 00 }, 
 837         { 29, wxDateTime::May
, 2076, 18, 30, 00 }, 
 838         { 29, wxDateTime::Feb
, 2400, 02, 15, 25 }, 
 839         { 01, wxDateTime::Jan
,  -52, 03, 16, 47 }, 
 842     // an extra test (as it doesn't depend on date, don't do it in the loop) 
 843     printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str()); 
 845     for ( size_t d 
= 0; d 
< WXSIZEOF(formatTestDates
); d
++ ) 
 849         wxDateTime dt 
= d 
== 0 ? wxDateTime::Now() : formatTestDates
[d
].DT(); 
 850         for ( size_t n 
= 0; n 
< WXSIZEOF(formatTestFormats
); n
++ ) 
 852             printf("%s\n", dt
.Format(formatTestFormats
[n
]).c_str()); 
 857 // test text -> wxDateTime conversion 
 858 static void TestTimeParse() 
 860     puts("\n*** wxDateTime parse test ***"); 
 869     static const ParseTestData parseTestDates
[] = 
 871         { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE 
}, 
 872         { "Wed, 1 Dec 1999 05:17:20 +0300",  {  1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE 
}, 
 875     for ( size_t n 
= 0; n 
< WXSIZEOF(parseTestDates
); n
++ ) 
 877         const char *format 
= parseTestDates
[n
].format
; 
 879         printf("%s => ", format
); 
 882         if ( dt
.ParseRfc822Date(format
) ) 
 884             printf("%s ", dt
.Format().c_str()); 
 886             if ( parseTestDates
[n
].good 
) 
 888                 wxDateTime dtReal 
= parseTestDates
[n
].date
.DT(); 
 895                     printf("(ERROR: should be %s)\n", dtReal
.Format().c_str()); 
 900                 puts("(ERROR: bad format)"); 
 905             printf("bad format (%s)\n", 
 906                    parseTestDates
[n
].good 
? "ERROR" : "ok"); 
 913 // test compatibility with the old wxDate/wxTime classes 
 914 static void TestTimeCompatibility() 
 916     puts("\n*** wxDateTime compatibility test ***"); 
 918     printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str()); 
 919     printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str()); 
 921     double jdnNow 
= wxDateTime::Now().GetJDN(); 
 922     long jdnMidnight 
= (long)(jdnNow 
- 0.5); 
 923     printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str()); 
 925     jdnMidnight 
= wxDate().Set().GetJulianDate(); 
 926     printf("wxDateTime for today: %s\n", 
 927             wxDateTime((double)(jdnMidnight 
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str()); 
 929     int flags 
= wxEUROPEAN
;//wxFULL; 
 932     printf("Today is %s\n", date
.FormatDate(flags
).c_str()); 
 933     for ( int n 
= 0; n 
< 7; n
++ ) 
 935         printf("Previous %s is %s\n", 
 936                wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
), 
 937                date
.Previous(n 
+ 1).FormatDate(flags
).c_str()); 
 945 // ---------------------------------------------------------------------------- 
 947 // ---------------------------------------------------------------------------- 
 951 #include <wx/thread.h> 
 953 static size_t gs_counter 
= (size_t)-1; 
 954 static wxCriticalSection gs_critsect
; 
 955 static wxCondition gs_cond
; 
 957 class MyJoinableThread 
: public wxThread
 
 960     MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
) 
 961         { m_n 
= n
; Create(); } 
 963     // thread execution starts here 
 964     virtual ExitCode 
Entry(); 
 970 wxThread::ExitCode 
MyJoinableThread::Entry() 
 972     unsigned long res 
= 1; 
 973     for ( size_t n 
= 1; n 
< m_n
; n
++ ) 
 977         // it's a loooong calculation :-) 
 981     return (ExitCode
)res
; 
 984 class MyDetachedThread 
: public wxThread
 
 987     MyDetachedThread(size_t n
, char ch
) 
 996     // thread execution starts here 
 997     virtual ExitCode 
Entry(); 
1000     virtual void OnExit(); 
1003     size_t m_n
; // number of characters to write 
1004     char m_ch
;  // character to write 
1006     bool m_cancelled
;   // FALSE if we exit normally 
1009 wxThread::ExitCode 
MyDetachedThread::Entry() 
1012         wxCriticalSectionLocker 
lock(gs_critsect
); 
1013         if ( gs_counter 
== (size_t)-1 ) 
1019     for ( size_t n 
= 0; n 
< m_n
; n
++ ) 
1021         if ( TestDestroy() ) 
1031         wxThread::Sleep(100); 
1037 void MyDetachedThread::OnExit() 
1039     wxLogTrace("thread", "Thread %ld is in OnExit", GetId()); 
1041     wxCriticalSectionLocker 
lock(gs_critsect
); 
1042     if ( !--gs_counter 
&& !m_cancelled 
) 
1046 void TestDetachedThreads() 
1048     puts("\n*** Testing detached threads ***"); 
1050     static const size_t nThreads 
= 3; 
1051     MyDetachedThread 
*threads
[nThreads
]; 
1053     for ( n 
= 0; n 
< nThreads
; n
++ ) 
1055         threads
[n
] = new MyDetachedThread(10, 'A' + n
); 
1058     threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
); 
1059     threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
); 
1061     for ( n 
= 0; n 
< nThreads
; n
++ ) 
1066     // wait until all threads terminate 
1072 void TestJoinableThreads() 
1074     puts("\n*** Testing a joinable thread (a loooong calculation...) ***"); 
1076     // calc 10! in the background 
1077     MyJoinableThread 
thread(10); 
1080     printf("\nThread terminated with exit code %lu.\n", 
1081            (unsigned long)thread
.Wait()); 
1084 void TestThreadSuspend() 
1086     puts("\n*** Testing thread suspend/resume functions ***"); 
1088     MyDetachedThread 
*thread 
= new MyDetachedThread(15, 'X'); 
1092     // this is for this demo only, in a real life program we'd use another 
1093     // condition variable which would be signaled from wxThread::Entry() to 
1094     // tell us that the thread really started running - but here just wait a 
1095     // bit and hope that it will be enough (the problem is, of course, that 
1096     // the thread might still not run when we call Pause() which will result 
1098     wxThread::Sleep(300); 
1100     for ( size_t n 
= 0; n 
< 3; n
++ ) 
1104         puts("\nThread suspended"); 
1107             // don't sleep but resume immediately the first time 
1108             wxThread::Sleep(300); 
1110         puts("Going to resume the thread"); 
1115     puts("Waiting until it terminates now"); 
1117     // wait until the thread terminates 
1123 void TestThreadDelete() 
1125     // As above, using Sleep() is only for testing here - we must use some 
1126     // synchronisation object instead to ensure that the thread is still 
1127     // running when we delete it - deleting a detached thread which already 
1128     // terminated will lead to a crash! 
1130     puts("\n*** Testing thread delete function ***"); 
1132     MyDetachedThread 
*thread0 
= new MyDetachedThread(30, 'W'); 
1136     puts("\nDeleted a thread which didn't start to run yet."); 
1138     MyDetachedThread 
*thread1 
= new MyDetachedThread(30, 'Y'); 
1142     wxThread::Sleep(300); 
1146     puts("\nDeleted a running thread."); 
1148     MyDetachedThread 
*thread2 
= new MyDetachedThread(30, 'Z'); 
1152     wxThread::Sleep(300); 
1158     puts("\nDeleted a sleeping thread."); 
1160     MyJoinableThread 
thread3(20); 
1165     puts("\nDeleted a joinable thread."); 
1167     MyJoinableThread 
thread4(2); 
1170     wxThread::Sleep(300); 
1174     puts("\nDeleted a joinable thread which already terminated."); 
1179 #endif // TEST_THREADS 
1181 // ---------------------------------------------------------------------------- 
1183 // ---------------------------------------------------------------------------- 
1187 void PrintArray(const char* name
, const wxArrayString
& array
) 
1189     printf("Dump of the array '%s'\n", name
); 
1191     size_t nCount 
= array
.GetCount(); 
1192     for ( size_t n 
= 0; n 
< nCount
; n
++ ) 
1194         printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str()); 
1198 #endif // TEST_ARRAYS 
1200 // ---------------------------------------------------------------------------- 
1202 // ---------------------------------------------------------------------------- 
1206 #include "wx/timer.h" 
1208 static void TestString() 
1218     for (int i 
= 0; i 
< 1000000; ++i
) 
1222         c 
= "! How'ya doin'?"; 
1225         c 
= "Hello world! What's up?"; 
1230     printf ("TestString elapsed time: %ld\n", sw
.Time()); 
1233 static void TestPChar() 
1241     for (int i 
= 0; i 
< 1000000; ++i
) 
1243         strcpy (a
, "Hello"); 
1244         strcpy (b
, " world"); 
1245         strcpy (c
, "! How'ya doin'?"); 
1248         strcpy (c
, "Hello world! What's up?"); 
1249         if (strcmp (c
, a
) == 0) 
1253     printf ("TestPChar elapsed time: %ld\n", sw
.Time()); 
1256 static void TestStringSub() 
1258     wxString 
s("Hello, world!"); 
1260     puts("*** Testing wxString substring extraction ***"); 
1262     printf("String = '%s'\n", s
.c_str()); 
1263     printf("Left(5) = '%s'\n", s
.Left(5).c_str()); 
1264     printf("Right(6) = '%s'\n", s
.Right(6).c_str()); 
1265     printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str()); 
1266     printf("Mid(3) = '%s'\n", s
.Mid(3).c_str()); 
1267     printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str()); 
1268     printf("substr(3) = '%s'\n", s
.substr(3).c_str()); 
1273 #endif // TEST_STRINGS 
1275 // ---------------------------------------------------------------------------- 
1277 // ---------------------------------------------------------------------------- 
1279 int main(int argc
, char **argv
) 
1281     if ( !wxInitialize() ) 
1283         fprintf(stderr
, "Failed to initialize the wxWindows library, aborting."); 
1293 #endif // TEST_STRINGS 
1304     puts("*** Initially:"); 
1306     PrintArray("a1", a1
); 
1308     wxArrayString 
a2(a1
); 
1309     PrintArray("a2", a2
); 
1311     wxSortedArrayString 
a3(a1
); 
1312     PrintArray("a3", a3
); 
1314     puts("*** After deleting a string from a1"); 
1317     PrintArray("a1", a1
); 
1318     PrintArray("a2", a2
); 
1319     PrintArray("a3", a3
); 
1321     puts("*** After reassigning a1 to a2 and a3"); 
1323     PrintArray("a2", a2
); 
1324     PrintArray("a3", a3
); 
1325 #endif // TEST_ARRAYS 
1333     for ( size_t n 
= 0; n 
< 8000; n
++ ) 
1335         s 
<< (char)('A' + (n 
% 26)); 
1339     msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str()); 
1341     // this one shouldn't be truncated 
1344     // but this one will because log functions use fixed size buffer 
1345     // (note that it doesn't need '\n' at the end neither - will be added 
1347     wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str()); 
1351     int nCPUs 
= wxThread::GetCPUCount(); 
1352     printf("This system has %d CPUs\n", nCPUs
); 
1354         wxThread::SetConcurrency(nCPUs
); 
1356     if ( argc 
> 1 && argv
[1][0] == 't' ) 
1357         wxLog::AddTraceMask("thread"); 
1360         TestDetachedThreads(); 
1362         TestJoinableThreads(); 
1364         TestThreadSuspend(); 
1368 #endif // TEST_THREADS 
1370 #ifdef TEST_LONGLONG 
1375 #endif // TEST_LONGLONG