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());
430 wxDateTime::Tm tm
= now
.GetTm();
431 if ( wxDateTime(tm
) != now
)
433 printf("ERROR: got %s instead of %s\n",
434 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
438 // test some minimal support for the dates outside the standard range
439 static void TestTimeRange()
441 puts("\n*** wxDateTime out-of-standard-range dates test ***");
443 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
445 printf("Unix epoch:\t%s\n",
446 wxDateTime(2440587.5).Format(fmt
).c_str());
447 printf("Feb 29, 0: \t%s\n",
448 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
449 printf("JDN 0: \t%s\n",
450 wxDateTime(0.0).Format(fmt
).c_str());
451 printf("Jan 1, 1AD:\t%s\n",
452 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
453 printf("May 29, 2099:\t%s\n",
454 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
457 static void TestTimeTicks()
459 puts("\n*** wxDateTime ticks test ***");
461 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
463 const Date
& d
= testDates
[n
];
467 wxDateTime dt
= d
.DT();
468 long ticks
= (dt
.GetValue() / 1000).ToLong();
469 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
470 if ( ticks
== d
.ticks
)
476 printf(" (ERROR: should be %ld, delta = %ld)\n",
477 d
.ticks
, ticks
- d
.ticks
);
480 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
481 ticks
= (dt
.GetValue() / 1000).ToLong();
482 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
483 if ( ticks
== d
.gmticks
)
489 printf(" (ERROR: should be %ld, delta = %ld)\n",
490 d
.gmticks
, ticks
- d
.gmticks
);
497 // test conversions to JDN &c
498 static void TestTimeJDN()
500 puts("\n*** wxDateTime to JDN test ***");
502 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
504 const Date
& d
= testDates
[n
];
505 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
506 double jdn
= dt
.GetJulianDayNumber();
508 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
515 printf(" (ERROR: should be %f, delta = %f)\n",
521 // test week days computation
522 static void TestTimeWDays()
524 puts("\n*** wxDateTime weekday test ***");
528 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
530 const Date
& d
= testDates
[n
];
531 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
533 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
536 wxDateTime::GetWeekDayName(wday
).c_str());
537 if ( wday
== d
.wday
)
543 printf(" (ERROR: should be %s)\n",
544 wxDateTime::GetWeekDayName(d
.wday
).c_str());
550 // test SetToWeekDay()
551 struct WeekDateTestData
553 Date date
; // the real date (precomputed)
554 int nWeek
; // its week index in the month
555 wxDateTime::WeekDay wday
; // the weekday
556 wxDateTime::Month month
; // the month
557 int year
; // and the year
559 wxString
Format() const
562 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
564 case 1: which
= "first"; break;
565 case 2: which
= "second"; break;
566 case 3: which
= "third"; break;
567 case 4: which
= "fourth"; break;
568 case 5: which
= "fifth"; break;
570 case -1: which
= "last"; break;
575 which
+= " from end";
578 s
.Printf("The %s %s of %s in %d",
580 wxDateTime::GetWeekDayName(wday
).c_str(),
581 wxDateTime::GetMonthName(month
).c_str(),
588 // the array data was generated by the following python program
590 from DateTime import *
591 from whrandom import *
594 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
595 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
597 week = DateTimeDelta(7)
600 year = randint(1900, 2100)
601 month = randint(1, 12)
603 dt = DateTime(year, month, day)
604 wday = dt.day_of_week
606 countFromEnd = choice([-1, 1])
609 while dt.month is month:
610 dt = dt - countFromEnd * week
611 weekNum = weekNum + countFromEnd
613 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
615 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
616 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
619 static const WeekDateTestData weekDatesTestData
[] =
621 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
622 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
623 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
624 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
625 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
626 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
627 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
628 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
629 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
630 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
631 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
632 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
633 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
634 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
635 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
636 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
637 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
638 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
639 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
640 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
643 static const char *fmt
= "%d-%b-%Y";
646 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
648 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
650 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
652 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
654 const Date
& d
= wd
.date
;
655 if ( d
.SameDay(dt
.GetTm()) )
661 dt
.Set(d
.day
, d
.month
, d
.year
);
663 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
668 // test the computation of (ISO) week numbers
669 static void TestTimeWNumber()
671 puts("\n*** wxDateTime week number test ***");
673 struct WeekNumberTestData
675 Date date
; // the date
676 wxDateTime::wxDateTime_t week
; // the week number in the year
677 wxDateTime::wxDateTime_t wmon
; // the week number in the month
678 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
679 wxDateTime::wxDateTime_t dnum
; // day number in the year
682 // data generated with the following python script:
684 from DateTime import *
685 from whrandom import *
688 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
689 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
691 def GetMonthWeek(dt):
692 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
694 weekNumMonth = weekNumMonth + 53
697 def GetLastSundayBefore(dt):
698 if dt.iso_week[2] == 7:
701 return dt - DateTimeDelta(dt.iso_week[2])
704 year = randint(1900, 2100)
705 month = randint(1, 12)
707 dt = DateTime(year, month, day)
708 dayNum = dt.day_of_year
709 weekNum = dt.iso_week[1]
710 weekNumMonth = GetMonthWeek(dt)
713 dtSunday = GetLastSundayBefore(dt)
715 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
716 weekNumMonth2 = weekNumMonth2 + 1
717 dtSunday = dtSunday - DateTimeDelta(7)
719 data = { 'day': rjust(`day`, 2), \
720 'month': monthNames[month - 1], \
722 'weekNum': rjust(`weekNum`, 2), \
723 'weekNumMonth': weekNumMonth, \
724 'weekNumMonth2': weekNumMonth2, \
725 'dayNum': rjust(`dayNum`, 3) }
727 print " { { %(day)s, "\
728 "wxDateTime::%(month)s, "\
731 "%(weekNumMonth)s, "\
732 "%(weekNumMonth2)s, "\
733 "%(dayNum)s }," % data
736 static const WeekNumberTestData weekNumberTestDates
[] =
738 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
739 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
740 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
741 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
742 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
743 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
744 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
745 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
746 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
747 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
748 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
749 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
750 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
751 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
752 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
753 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
754 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
755 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
756 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
757 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
760 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
762 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
763 const Date
& d
= wn
.date
;
765 wxDateTime dt
= d
.DT();
767 wxDateTime::wxDateTime_t
768 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
769 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
770 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
771 dnum
= dt
.GetDayOfYear();
773 printf("%s: the day number is %d",
774 d
.FormatDate().c_str(), dnum
);
775 if ( dnum
== wn
.dnum
)
781 printf(" (ERROR: should be %d)", wn
.dnum
);
784 printf(", week in month is %d", wmon
);
785 if ( wmon
== wn
.wmon
)
791 printf(" (ERROR: should be %d)", wn
.wmon
);
794 printf(" or %d", wmon2
);
795 if ( wmon2
== wn
.wmon2
)
801 printf(" (ERROR: should be %d)", wn
.wmon2
);
804 printf(", week in year is %d", week
);
805 if ( week
== wn
.week
)
811 printf(" (ERROR: should be %d)\n", wn
.week
);
816 // test DST calculations
817 static void TestTimeDST()
819 puts("\n*** wxDateTime DST test ***");
821 printf("DST is%s in effect now.\n\n",
822 wxDateTime::Now().IsDST() ? "" : " not");
824 // taken from http://www.energy.ca.gov/daylightsaving.html
825 static const Date datesDST
[2][2004 - 1900 + 1] =
828 { 1, wxDateTime::Apr
, 1990 },
829 { 7, wxDateTime::Apr
, 1991 },
830 { 5, wxDateTime::Apr
, 1992 },
831 { 4, wxDateTime::Apr
, 1993 },
832 { 3, wxDateTime::Apr
, 1994 },
833 { 2, wxDateTime::Apr
, 1995 },
834 { 7, wxDateTime::Apr
, 1996 },
835 { 6, wxDateTime::Apr
, 1997 },
836 { 5, wxDateTime::Apr
, 1998 },
837 { 4, wxDateTime::Apr
, 1999 },
838 { 2, wxDateTime::Apr
, 2000 },
839 { 1, wxDateTime::Apr
, 2001 },
840 { 7, wxDateTime::Apr
, 2002 },
841 { 6, wxDateTime::Apr
, 2003 },
842 { 4, wxDateTime::Apr
, 2004 },
845 { 28, wxDateTime::Oct
, 1990 },
846 { 27, wxDateTime::Oct
, 1991 },
847 { 25, wxDateTime::Oct
, 1992 },
848 { 31, wxDateTime::Oct
, 1993 },
849 { 30, wxDateTime::Oct
, 1994 },
850 { 29, wxDateTime::Oct
, 1995 },
851 { 27, wxDateTime::Oct
, 1996 },
852 { 26, wxDateTime::Oct
, 1997 },
853 { 25, wxDateTime::Oct
, 1998 },
854 { 31, wxDateTime::Oct
, 1999 },
855 { 29, wxDateTime::Oct
, 2000 },
856 { 28, wxDateTime::Oct
, 2001 },
857 { 27, wxDateTime::Oct
, 2002 },
858 { 26, wxDateTime::Oct
, 2003 },
859 { 31, wxDateTime::Oct
, 2004 },
864 for ( year
= 1990; year
< 2005; year
++ )
866 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
867 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
869 printf("DST period in the US for year %d: from %s to %s",
870 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
872 size_t n
= year
- 1990;
873 const Date
& dBegin
= datesDST
[0][n
];
874 const Date
& dEnd
= datesDST
[1][n
];
876 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
882 printf(" (ERROR: should be %s %d to %s %d)\n",
883 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
884 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
890 for ( year
= 1990; year
< 2005; year
++ )
892 printf("DST period in Europe for year %d: from %s to %s\n",
894 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
895 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
899 // test wxDateTime -> text conversion
900 static void TestTimeFormat()
902 puts("\n*** wxDateTime formatting test ***");
904 // some information may be lost during conversion, so store what kind
905 // of info should we recover after a round trip
908 CompareNone
, // don't try comparing
909 CompareBoth
, // dates and times should be identical
910 CompareDate
, // dates only
911 CompareTime
// time only
916 CompareKind compareKind
;
918 } formatTestFormats
[] =
920 { CompareBoth
, "---> %c" },
921 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
922 { CompareBoth
, "Date is %x, time is %X" },
923 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
924 { CompareNone
, "The day of year: %j, the week of year: %W" },
927 static const Date formatTestDates
[] =
929 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
930 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
932 // this test can't work for other centuries because it uses two digit
933 // years in formats, so don't even try it
934 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
935 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
936 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
940 // an extra test (as it doesn't depend on date, don't do it in the loop)
941 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
943 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
947 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
948 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
950 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
951 printf("%s", s
.c_str());
953 // what can we recover?
954 int kind
= formatTestFormats
[n
].compareKind
;
958 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
961 // converion failed - should it have?
962 if ( kind
== CompareNone
)
965 puts(" (ERROR: conversion back failed)");
969 // should have parsed the entire string
970 puts(" (ERROR: conversion back stopped too soon)");
974 bool equal
= FALSE
; // suppress compilaer warning
982 equal
= dt
.IsSameDate(dt2
);
986 equal
= dt
.IsSameTime(dt2
);
992 printf(" (ERROR: got back '%s' instead of '%s')\n",
993 dt2
.Format().c_str(), dt
.Format().c_str());
1004 // test text -> wxDateTime conversion
1005 static void TestTimeParse()
1007 puts("\n*** wxDateTime parse test ***");
1009 struct ParseTestData
1016 static const ParseTestData parseTestDates
[] =
1018 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
1019 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
1022 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1024 const char *format
= parseTestDates
[n
].format
;
1026 printf("%s => ", format
);
1029 if ( dt
.ParseRfc822Date(format
) )
1031 printf("%s ", dt
.Format().c_str());
1033 if ( parseTestDates
[n
].good
)
1035 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
1042 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
1047 puts("(ERROR: bad format)");
1052 printf("bad format (%s)\n",
1053 parseTestDates
[n
].good
? "ERROR" : "ok");
1058 static void TestInteractive()
1060 puts("\n*** interactive wxDateTime tests ***");
1066 printf("Enter a date: ");
1067 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
1071 if ( !dt
.ParseDate(buf
) )
1073 puts("failed to parse the date");
1078 printf("%s: day %u, week of month %u/%u, week of year %u\n",
1079 dt
.FormatISODate().c_str(),
1081 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1082 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1083 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
1086 puts("\n*** done ***");
1089 static void TestTimeArithmetics()
1091 puts("\n*** testing arithmetic operations on wxDateTime ***");
1097 } testArithmData
[] =
1099 { wxDateSpan::Day(), "day" },
1100 { wxDateSpan::Week(), "week" },
1101 { wxDateSpan::Month(), "month" },
1102 { wxDateSpan::Year(), "year" },
1103 { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
1106 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
1108 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1110 wxDateSpan span
= testArithmData
[n
].span
;
1114 const char *name
= testArithmData
[n
].name
;
1115 printf("%s + %s = %s, %s - %s = %s\n",
1116 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
1117 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
1119 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
1120 if ( dt1
- span
== dt
)
1126 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1129 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
1130 if ( dt2
+ span
== dt
)
1136 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1139 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
1140 if ( dt2
+ 2*span
== dt1
)
1146 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
1155 // test compatibility with the old wxDate/wxTime classes
1156 static void TestTimeCompatibility()
1158 puts("\n*** wxDateTime compatibility test ***");
1160 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1161 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1163 double jdnNow
= wxDateTime::Now().GetJDN();
1164 long jdnMidnight
= (long)(jdnNow
- 0.5);
1165 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
1167 jdnMidnight
= wxDate().Set().GetJulianDate();
1168 printf("wxDateTime for today: %s\n",
1169 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1171 int flags
= wxEUROPEAN
;//wxFULL;
1174 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1175 for ( int n
= 0; n
< 7; n
++ )
1177 printf("Previous %s is %s\n",
1178 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1179 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1187 // ----------------------------------------------------------------------------
1189 // ----------------------------------------------------------------------------
1193 #include <wx/thread.h>
1195 static size_t gs_counter
= (size_t)-1;
1196 static wxCriticalSection gs_critsect
;
1197 static wxCondition gs_cond
;
1199 class MyJoinableThread
: public wxThread
1202 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1203 { m_n
= n
; Create(); }
1205 // thread execution starts here
1206 virtual ExitCode
Entry();
1212 wxThread::ExitCode
MyJoinableThread::Entry()
1214 unsigned long res
= 1;
1215 for ( size_t n
= 1; n
< m_n
; n
++ )
1219 // it's a loooong calculation :-)
1223 return (ExitCode
)res
;
1226 class MyDetachedThread
: public wxThread
1229 MyDetachedThread(size_t n
, char ch
)
1233 m_cancelled
= FALSE
;
1238 // thread execution starts here
1239 virtual ExitCode
Entry();
1242 virtual void OnExit();
1245 size_t m_n
; // number of characters to write
1246 char m_ch
; // character to write
1248 bool m_cancelled
; // FALSE if we exit normally
1251 wxThread::ExitCode
MyDetachedThread::Entry()
1254 wxCriticalSectionLocker
lock(gs_critsect
);
1255 if ( gs_counter
== (size_t)-1 )
1261 for ( size_t n
= 0; n
< m_n
; n
++ )
1263 if ( TestDestroy() )
1273 wxThread::Sleep(100);
1279 void MyDetachedThread::OnExit()
1281 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1283 wxCriticalSectionLocker
lock(gs_critsect
);
1284 if ( !--gs_counter
&& !m_cancelled
)
1288 void TestDetachedThreads()
1290 puts("\n*** Testing detached threads ***");
1292 static const size_t nThreads
= 3;
1293 MyDetachedThread
*threads
[nThreads
];
1295 for ( n
= 0; n
< nThreads
; n
++ )
1297 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1300 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1301 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1303 for ( n
= 0; n
< nThreads
; n
++ )
1308 // wait until all threads terminate
1314 void TestJoinableThreads()
1316 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1318 // calc 10! in the background
1319 MyJoinableThread
thread(10);
1322 printf("\nThread terminated with exit code %lu.\n",
1323 (unsigned long)thread
.Wait());
1326 void TestThreadSuspend()
1328 puts("\n*** Testing thread suspend/resume functions ***");
1330 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1334 // this is for this demo only, in a real life program we'd use another
1335 // condition variable which would be signaled from wxThread::Entry() to
1336 // tell us that the thread really started running - but here just wait a
1337 // bit and hope that it will be enough (the problem is, of course, that
1338 // the thread might still not run when we call Pause() which will result
1340 wxThread::Sleep(300);
1342 for ( size_t n
= 0; n
< 3; n
++ )
1346 puts("\nThread suspended");
1349 // don't sleep but resume immediately the first time
1350 wxThread::Sleep(300);
1352 puts("Going to resume the thread");
1357 puts("Waiting until it terminates now");
1359 // wait until the thread terminates
1365 void TestThreadDelete()
1367 // As above, using Sleep() is only for testing here - we must use some
1368 // synchronisation object instead to ensure that the thread is still
1369 // running when we delete it - deleting a detached thread which already
1370 // terminated will lead to a crash!
1372 puts("\n*** Testing thread delete function ***");
1374 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1378 puts("\nDeleted a thread which didn't start to run yet.");
1380 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1384 wxThread::Sleep(300);
1388 puts("\nDeleted a running thread.");
1390 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1394 wxThread::Sleep(300);
1400 puts("\nDeleted a sleeping thread.");
1402 MyJoinableThread
thread3(20);
1407 puts("\nDeleted a joinable thread.");
1409 MyJoinableThread
thread4(2);
1412 wxThread::Sleep(300);
1416 puts("\nDeleted a joinable thread which already terminated.");
1421 #endif // TEST_THREADS
1423 // ----------------------------------------------------------------------------
1425 // ----------------------------------------------------------------------------
1429 void PrintArray(const char* name
, const wxArrayString
& array
)
1431 printf("Dump of the array '%s'\n", name
);
1433 size_t nCount
= array
.GetCount();
1434 for ( size_t n
= 0; n
< nCount
; n
++ )
1436 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1440 #endif // TEST_ARRAYS
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1448 #include "wx/timer.h"
1450 static void TestString()
1460 for (int i
= 0; i
< 1000000; ++i
)
1464 c
= "! How'ya doin'?";
1467 c
= "Hello world! What's up?";
1472 printf ("TestString elapsed time: %ld\n", sw
.Time());
1475 static void TestPChar()
1483 for (int i
= 0; i
< 1000000; ++i
)
1485 strcpy (a
, "Hello");
1486 strcpy (b
, " world");
1487 strcpy (c
, "! How'ya doin'?");
1490 strcpy (c
, "Hello world! What's up?");
1491 if (strcmp (c
, a
) == 0)
1495 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1498 static void TestStringSub()
1500 wxString
s("Hello, world!");
1502 puts("*** Testing wxString substring extraction ***");
1504 printf("String = '%s'\n", s
.c_str());
1505 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1506 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1507 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1508 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1509 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1510 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1515 static void TestStringFormat()
1517 puts("*** Testing wxString formatting ***");
1520 s
.Printf("%03d", 18);
1522 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
1523 printf("Number 18: %s\n", s
.c_str());
1528 #endif // TEST_STRINGS
1530 // ----------------------------------------------------------------------------
1532 // ----------------------------------------------------------------------------
1534 int main(int argc
, char **argv
)
1536 if ( !wxInitialize() )
1538 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1552 #endif // TEST_STRINGS
1563 puts("*** Initially:");
1565 PrintArray("a1", a1
);
1567 wxArrayString
a2(a1
);
1568 PrintArray("a2", a2
);
1570 wxSortedArrayString
a3(a1
);
1571 PrintArray("a3", a3
);
1573 puts("*** After deleting a string from a1");
1576 PrintArray("a1", a1
);
1577 PrintArray("a2", a2
);
1578 PrintArray("a3", a3
);
1580 puts("*** After reassigning a1 to a2 and a3");
1582 PrintArray("a2", a2
);
1583 PrintArray("a3", a3
);
1584 #endif // TEST_ARRAYS
1592 for ( size_t n
= 0; n
< 8000; n
++ )
1594 s
<< (char)('A' + (n
% 26));
1598 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
1600 // this one shouldn't be truncated
1603 // but this one will because log functions use fixed size buffer
1604 // (note that it doesn't need '\n' at the end neither - will be added
1606 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
1610 int nCPUs
= wxThread::GetCPUCount();
1611 printf("This system has %d CPUs\n", nCPUs
);
1613 wxThread::SetConcurrency(nCPUs
);
1615 if ( argc
> 1 && argv
[1][0] == 't' )
1616 wxLog::AddTraceMask("thread");
1619 TestDetachedThreads();
1621 TestJoinableThreads();
1623 TestThreadSuspend();
1627 #endif // TEST_THREADS
1629 #ifdef TEST_LONGLONG
1634 #endif // TEST_LONGLONG
1654 TestTimeArithmetics();