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 // ----------------------------------------------------------------------------
33 //#define TEST_CMDLINE
36 //#define TEST_LONGLONG
38 //#define TEST_STRINGS
39 //#define TEST_THREADS
42 // ============================================================================
44 // ============================================================================
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #include <wx/cmdline.h>
53 #include <wx/datetime.h>
55 static void ShowCmdLine(const wxCmdLineParser
& parser
)
57 wxString s
= "Input files: ";
59 size_t count
= parser
.GetParamCount();
60 for ( size_t param
= 0; param
< count
; param
++ )
62 s
<< parser
.GetParam(param
) << ' ';
66 << "Verbose:\t" << (parser
.Found("v") ? "yes" : "no") << '\n'
67 << "Quiet:\t" << (parser
.Found("q") ? "yes" : "no") << '\n';
72 if ( parser
.Found("o", &strVal
) )
73 s
<< "Output file:\t" << strVal
<< '\n';
74 if ( parser
.Found("i", &strVal
) )
75 s
<< "Input dir:\t" << strVal
<< '\n';
76 if ( parser
.Found("s", &lVal
) )
77 s
<< "Size:\t" << lVal
<< '\n';
78 if ( parser
.Found("d", &dt
) )
79 s
<< "Date:\t" << dt
.FormatISODate() << '\n';
84 #endif // TEST_CMDLINE
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
94 static void TestDirEnumHelper(wxDir
& dir
,
95 int flags
= wxDIR_DEFAULT
,
96 const wxString
& filespec
= wxEmptyString
)
100 if ( !dir
.IsOpened() )
103 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
106 printf("\t%s\n", filename
.c_str());
108 cont
= dir
.GetNext(&filename
);
114 static void TestDirEnum()
116 wxDir
dir(wxGetCwd());
118 puts("Enumerating everything in current directory:");
119 TestDirEnumHelper(dir
);
121 puts("Enumerating really everything in current directory:");
122 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
124 puts("Enumerating object files in current directory:");
125 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
127 puts("Enumerating directories in current directory:");
128 TestDirEnumHelper(dir
, wxDIR_DIRS
);
130 puts("Enumerating files in current directory:");
131 TestDirEnumHelper(dir
, wxDIR_FILES
);
133 puts("Enumerating files including hidden in current directory:");
134 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
138 #elif defined(__WXMSW__)
141 #error "don't know where the root directory is"
144 puts("Enumerating everything in root directory:");
145 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
147 puts("Enumerating directories in root directory:");
148 TestDirEnumHelper(dir
, wxDIR_DIRS
);
150 puts("Enumerating files in root directory:");
151 TestDirEnumHelper(dir
, wxDIR_FILES
);
153 puts("Enumerating files including hidden in root directory:");
154 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
156 puts("Enumerating files in non existing directory:");
157 wxDir
dirNo("nosuchdir");
158 TestDirEnumHelper(dirNo
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 #include <wx/mimetype.h>
171 static void TestMimeEnum()
173 wxMimeTypesManager mimeTM
;
174 wxArrayString mimetypes
;
176 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
178 printf("*** All %u known filetypes: ***\n", count
);
183 for ( size_t n
= 0; n
< count
; n
++ )
185 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
188 printf("nothing known about the filetype '%s'!\n",
189 mimetypes
[n
].c_str());
193 filetype
->GetDescription(&desc
);
194 filetype
->GetExtensions(exts
);
196 filetype
->GetIcon(NULL
);
199 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
206 printf("\t%s: %s (%s)\n",
207 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
219 #include <wx/longlong.h>
220 #include <wx/timer.h>
222 // make a 64 bit number from 4 16 bit ones
223 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
225 // get a random 64 bit number
226 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
228 #if wxUSE_LONGLONG_NATIVE
229 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
230 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
231 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
232 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
233 #endif // wxUSE_LONGLONG_NATIVE
235 static void TestSpeed()
237 static const long max
= 100000000;
244 for ( n
= 0; n
< max
; n
++ )
249 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
252 #if wxUSE_LONGLONG_NATIVE
257 for ( n
= 0; n
< max
; n
++ )
262 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
264 #endif // wxUSE_LONGLONG_NATIVE
270 for ( n
= 0; n
< max
; n
++ )
275 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
279 static void TestLongLongConversion()
281 puts("*** Testing wxLongLong conversions ***\n");
285 for ( size_t n
= 0; n
< 100000; n
++ )
289 #if wxUSE_LONGLONG_NATIVE
290 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
292 wxASSERT_MSG( a
== b
, "conversions failure" );
294 puts("Can't do it without native long long type, test skipped.");
297 #endif // wxUSE_LONGLONG_NATIVE
299 if ( !(nTested
% 1000) )
311 static void TestMultiplication()
313 puts("*** Testing wxLongLong multiplication ***\n");
317 for ( size_t n
= 0; n
< 100000; n
++ )
322 #if wxUSE_LONGLONG_NATIVE
323 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
324 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
326 wxASSERT_MSG( a
*b
== aa
*bb
, "multiplication failure" );
327 #else // !wxUSE_LONGLONG_NATIVE
328 puts("Can't do it without native long long type, test skipped.");
331 #endif // wxUSE_LONGLONG_NATIVE
333 if ( !(nTested
% 1000) )
345 static void TestDivision()
347 puts("*** Testing wxLongLong division ***\n");
351 for ( size_t n
= 0; n
< 100000; n
++ )
353 // get a random wxLongLong (shifting by 12 the MSB ensures that the
354 // multiplication will not overflow)
355 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
357 // get a random long (not wxLongLong for now) to divide it with
362 #if wxUSE_LONGLONG_NATIVE
363 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
365 wxLongLongNative p
= m
/ l
, s
= m
% l
;
366 wxASSERT_MSG( q
== p
&& r
== s
, "division failure" );
367 #else // !wxUSE_LONGLONG_NATIVE
369 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
370 #endif // wxUSE_LONGLONG_NATIVE
372 if ( !(nTested
% 1000) )
384 static void TestAddition()
386 puts("*** Testing wxLongLong addition ***\n");
390 for ( size_t n
= 0; n
< 100000; n
++ )
396 #if wxUSE_LONGLONG_NATIVE
397 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
398 wxLongLongNative(b
.GetHi(), b
.GetLo()),
399 "addition failure" );
400 #else // !wxUSE_LONGLONG_NATIVE
401 wxASSERT_MSG( c
- b
== a
, "addition failure" );
402 #endif // wxUSE_LONGLONG_NATIVE
404 if ( !(nTested
% 1000) )
416 static void TestBitOperations()
418 puts("*** Testing wxLongLong bit operation ***\n");
422 for ( size_t n
= 0; n
< 100000; n
++ )
426 #if wxUSE_LONGLONG_NATIVE
427 for ( size_t n
= 0; n
< 33; n
++ )
429 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
434 wxASSERT_MSG( b
== c
, "bit shift failure" );
436 b
= wxLongLongNative(a
.GetHi(), a
.GetLo()) << n
;
439 wxASSERT_MSG( b
== c
, "bit shift failure" );
442 #else // !wxUSE_LONGLONG_NATIVE
443 puts("Can't do it without native long long type, test skipped.");
446 #endif // wxUSE_LONGLONG_NATIVE
448 if ( !(nTested
% 1000) )
463 #endif // TEST_LONGLONG
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
473 #include <wx/datetime.h>
478 wxDateTime::wxDateTime_t day
;
479 wxDateTime::Month month
;
481 wxDateTime::wxDateTime_t hour
, min
, sec
;
483 wxDateTime::WeekDay wday
;
484 time_t gmticks
, ticks
;
486 void Init(const wxDateTime::Tm
& tm
)
495 gmticks
= ticks
= -1;
498 wxDateTime
DT() const
499 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
501 bool SameDay(const wxDateTime::Tm
& tm
) const
503 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
506 wxString
Format() const
509 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
511 wxDateTime::GetMonthName(month
).c_str(),
513 abs(wxDateTime::ConvertYearToBC(year
)),
514 year
> 0 ? "AD" : "BC");
518 wxString
FormatDate() const
521 s
.Printf("%02d-%s-%4d%s",
523 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
524 abs(wxDateTime::ConvertYearToBC(year
)),
525 year
> 0 ? "AD" : "BC");
530 static const Date testDates
[] =
532 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
533 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
534 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
535 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
536 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
537 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
538 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
539 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
540 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
541 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
542 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
543 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
544 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
545 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
546 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
549 // this test miscellaneous static wxDateTime functions
550 static void TestTimeStatic()
552 puts("\n*** wxDateTime static methods test ***");
554 // some info about the current date
555 int year
= wxDateTime::GetCurrentYear();
556 printf("Current year %d is %sa leap one and has %d days.\n",
558 wxDateTime::IsLeapYear(year
) ? "" : "not ",
559 wxDateTime::GetNumberOfDays(year
));
561 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
562 printf("Current month is '%s' ('%s') and it has %d days\n",
563 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
564 wxDateTime::GetMonthName(month
).c_str(),
565 wxDateTime::GetNumberOfDays(month
));
568 static const size_t nYears
= 5;
569 static const size_t years
[2][nYears
] =
571 // first line: the years to test
572 { 1990, 1976, 2000, 2030, 1984, },
574 // second line: TRUE if leap, FALSE otherwise
575 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
578 for ( size_t n
= 0; n
< nYears
; n
++ )
580 int year
= years
[0][n
];
581 bool should
= years
[1][n
] != 0,
582 is
= wxDateTime::IsLeapYear(year
);
584 printf("Year %d is %sa leap year (%s)\n",
587 should
== is
? "ok" : "ERROR");
589 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
593 // test constructing wxDateTime objects
594 static void TestTimeSet()
596 puts("\n*** wxDateTime construction test ***");
598 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
600 const Date
& d1
= testDates
[n
];
601 wxDateTime dt
= d1
.DT();
606 wxString s1
= d1
.Format(),
609 printf("Date: %s == %s (%s)\n",
610 s1
.c_str(), s2
.c_str(),
611 s1
== s2
? "ok" : "ERROR");
615 // test time zones stuff
616 static void TestTimeZones()
618 puts("\n*** wxDateTime timezone test ***");
620 wxDateTime now
= wxDateTime::Now();
622 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
623 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
624 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
625 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
626 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
627 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
629 wxDateTime::Tm tm
= now
.GetTm();
630 if ( wxDateTime(tm
) != now
)
632 printf("ERROR: got %s instead of %s\n",
633 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
637 // test some minimal support for the dates outside the standard range
638 static void TestTimeRange()
640 puts("\n*** wxDateTime out-of-standard-range dates test ***");
642 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
644 printf("Unix epoch:\t%s\n",
645 wxDateTime(2440587.5).Format(fmt
).c_str());
646 printf("Feb 29, 0: \t%s\n",
647 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
648 printf("JDN 0: \t%s\n",
649 wxDateTime(0.0).Format(fmt
).c_str());
650 printf("Jan 1, 1AD:\t%s\n",
651 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
652 printf("May 29, 2099:\t%s\n",
653 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
656 static void TestTimeTicks()
658 puts("\n*** wxDateTime ticks test ***");
660 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
662 const Date
& d
= testDates
[n
];
666 wxDateTime dt
= d
.DT();
667 long ticks
= (dt
.GetValue() / 1000).ToLong();
668 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
669 if ( ticks
== d
.ticks
)
675 printf(" (ERROR: should be %ld, delta = %ld)\n",
676 d
.ticks
, ticks
- d
.ticks
);
679 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
680 ticks
= (dt
.GetValue() / 1000).ToLong();
681 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
682 if ( ticks
== d
.gmticks
)
688 printf(" (ERROR: should be %ld, delta = %ld)\n",
689 d
.gmticks
, ticks
- d
.gmticks
);
696 // test conversions to JDN &c
697 static void TestTimeJDN()
699 puts("\n*** wxDateTime to JDN test ***");
701 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
703 const Date
& d
= testDates
[n
];
704 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
705 double jdn
= dt
.GetJulianDayNumber();
707 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
714 printf(" (ERROR: should be %f, delta = %f)\n",
720 // test week days computation
721 static void TestTimeWDays()
723 puts("\n*** wxDateTime weekday test ***");
727 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
729 const Date
& d
= testDates
[n
];
730 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
732 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
735 wxDateTime::GetWeekDayName(wday
).c_str());
736 if ( wday
== d
.wday
)
742 printf(" (ERROR: should be %s)\n",
743 wxDateTime::GetWeekDayName(d
.wday
).c_str());
749 // test SetToWeekDay()
750 struct WeekDateTestData
752 Date date
; // the real date (precomputed)
753 int nWeek
; // its week index in the month
754 wxDateTime::WeekDay wday
; // the weekday
755 wxDateTime::Month month
; // the month
756 int year
; // and the year
758 wxString
Format() const
761 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
763 case 1: which
= "first"; break;
764 case 2: which
= "second"; break;
765 case 3: which
= "third"; break;
766 case 4: which
= "fourth"; break;
767 case 5: which
= "fifth"; break;
769 case -1: which
= "last"; break;
774 which
+= " from end";
777 s
.Printf("The %s %s of %s in %d",
779 wxDateTime::GetWeekDayName(wday
).c_str(),
780 wxDateTime::GetMonthName(month
).c_str(),
787 // the array data was generated by the following python program
789 from DateTime import *
790 from whrandom import *
793 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
794 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
796 week = DateTimeDelta(7)
799 year = randint(1900, 2100)
800 month = randint(1, 12)
802 dt = DateTime(year, month, day)
803 wday = dt.day_of_week
805 countFromEnd = choice([-1, 1])
808 while dt.month is month:
809 dt = dt - countFromEnd * week
810 weekNum = weekNum + countFromEnd
812 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
814 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
815 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
818 static const WeekDateTestData weekDatesTestData
[] =
820 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
821 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
822 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
823 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
824 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
825 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
826 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
827 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
828 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
829 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
830 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
831 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
832 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
833 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
834 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
835 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
836 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
837 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
838 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
839 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
842 static const char *fmt
= "%d-%b-%Y";
845 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
847 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
849 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
851 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
853 const Date
& d
= wd
.date
;
854 if ( d
.SameDay(dt
.GetTm()) )
860 dt
.Set(d
.day
, d
.month
, d
.year
);
862 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
867 // test the computation of (ISO) week numbers
868 static void TestTimeWNumber()
870 puts("\n*** wxDateTime week number test ***");
872 struct WeekNumberTestData
874 Date date
; // the date
875 wxDateTime::wxDateTime_t week
; // the week number in the year
876 wxDateTime::wxDateTime_t wmon
; // the week number in the month
877 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
878 wxDateTime::wxDateTime_t dnum
; // day number in the year
881 // data generated with the following python script:
883 from DateTime import *
884 from whrandom import *
887 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
888 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
890 def GetMonthWeek(dt):
891 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
893 weekNumMonth = weekNumMonth + 53
896 def GetLastSundayBefore(dt):
897 if dt.iso_week[2] == 7:
900 return dt - DateTimeDelta(dt.iso_week[2])
903 year = randint(1900, 2100)
904 month = randint(1, 12)
906 dt = DateTime(year, month, day)
907 dayNum = dt.day_of_year
908 weekNum = dt.iso_week[1]
909 weekNumMonth = GetMonthWeek(dt)
912 dtSunday = GetLastSundayBefore(dt)
914 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
915 weekNumMonth2 = weekNumMonth2 + 1
916 dtSunday = dtSunday - DateTimeDelta(7)
918 data = { 'day': rjust(`day`, 2), \
919 'month': monthNames[month - 1], \
921 'weekNum': rjust(`weekNum`, 2), \
922 'weekNumMonth': weekNumMonth, \
923 'weekNumMonth2': weekNumMonth2, \
924 'dayNum': rjust(`dayNum`, 3) }
926 print " { { %(day)s, "\
927 "wxDateTime::%(month)s, "\
930 "%(weekNumMonth)s, "\
931 "%(weekNumMonth2)s, "\
932 "%(dayNum)s }," % data
935 static const WeekNumberTestData weekNumberTestDates
[] =
937 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
938 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
939 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
940 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
941 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
942 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
943 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
944 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
945 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
946 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
947 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
948 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
949 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
950 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
951 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
952 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
953 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
954 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
955 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
956 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
959 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
961 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
962 const Date
& d
= wn
.date
;
964 wxDateTime dt
= d
.DT();
966 wxDateTime::wxDateTime_t
967 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
968 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
969 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
970 dnum
= dt
.GetDayOfYear();
972 printf("%s: the day number is %d",
973 d
.FormatDate().c_str(), dnum
);
974 if ( dnum
== wn
.dnum
)
980 printf(" (ERROR: should be %d)", wn
.dnum
);
983 printf(", week in month is %d", wmon
);
984 if ( wmon
== wn
.wmon
)
990 printf(" (ERROR: should be %d)", wn
.wmon
);
993 printf(" or %d", wmon2
);
994 if ( wmon2
== wn
.wmon2
)
1000 printf(" (ERROR: should be %d)", wn
.wmon2
);
1003 printf(", week in year is %d", week
);
1004 if ( week
== wn
.week
)
1010 printf(" (ERROR: should be %d)\n", wn
.week
);
1015 // test DST calculations
1016 static void TestTimeDST()
1018 puts("\n*** wxDateTime DST test ***");
1020 printf("DST is%s in effect now.\n\n",
1021 wxDateTime::Now().IsDST() ? "" : " not");
1023 // taken from http://www.energy.ca.gov/daylightsaving.html
1024 static const Date datesDST
[2][2004 - 1900 + 1] =
1027 { 1, wxDateTime::Apr
, 1990 },
1028 { 7, wxDateTime::Apr
, 1991 },
1029 { 5, wxDateTime::Apr
, 1992 },
1030 { 4, wxDateTime::Apr
, 1993 },
1031 { 3, wxDateTime::Apr
, 1994 },
1032 { 2, wxDateTime::Apr
, 1995 },
1033 { 7, wxDateTime::Apr
, 1996 },
1034 { 6, wxDateTime::Apr
, 1997 },
1035 { 5, wxDateTime::Apr
, 1998 },
1036 { 4, wxDateTime::Apr
, 1999 },
1037 { 2, wxDateTime::Apr
, 2000 },
1038 { 1, wxDateTime::Apr
, 2001 },
1039 { 7, wxDateTime::Apr
, 2002 },
1040 { 6, wxDateTime::Apr
, 2003 },
1041 { 4, wxDateTime::Apr
, 2004 },
1044 { 28, wxDateTime::Oct
, 1990 },
1045 { 27, wxDateTime::Oct
, 1991 },
1046 { 25, wxDateTime::Oct
, 1992 },
1047 { 31, wxDateTime::Oct
, 1993 },
1048 { 30, wxDateTime::Oct
, 1994 },
1049 { 29, wxDateTime::Oct
, 1995 },
1050 { 27, wxDateTime::Oct
, 1996 },
1051 { 26, wxDateTime::Oct
, 1997 },
1052 { 25, wxDateTime::Oct
, 1998 },
1053 { 31, wxDateTime::Oct
, 1999 },
1054 { 29, wxDateTime::Oct
, 2000 },
1055 { 28, wxDateTime::Oct
, 2001 },
1056 { 27, wxDateTime::Oct
, 2002 },
1057 { 26, wxDateTime::Oct
, 2003 },
1058 { 31, wxDateTime::Oct
, 2004 },
1063 for ( year
= 1990; year
< 2005; year
++ )
1065 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
1066 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
1068 printf("DST period in the US for year %d: from %s to %s",
1069 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
1071 size_t n
= year
- 1990;
1072 const Date
& dBegin
= datesDST
[0][n
];
1073 const Date
& dEnd
= datesDST
[1][n
];
1075 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
1081 printf(" (ERROR: should be %s %d to %s %d)\n",
1082 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
1083 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
1089 for ( year
= 1990; year
< 2005; year
++ )
1091 printf("DST period in Europe for year %d: from %s to %s\n",
1093 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
1094 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
1098 // test wxDateTime -> text conversion
1099 static void TestTimeFormat()
1101 puts("\n*** wxDateTime formatting test ***");
1103 // some information may be lost during conversion, so store what kind
1104 // of info should we recover after a round trip
1107 CompareNone
, // don't try comparing
1108 CompareBoth
, // dates and times should be identical
1109 CompareDate
, // dates only
1110 CompareTime
// time only
1115 CompareKind compareKind
;
1117 } formatTestFormats
[] =
1119 { CompareBoth
, "---> %c" },
1120 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
1121 { CompareBoth
, "Date is %x, time is %X" },
1122 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
1123 { CompareNone
, "The day of year: %j, the week of year: %W" },
1126 static const Date formatTestDates
[] =
1128 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
1129 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
1131 // this test can't work for other centuries because it uses two digit
1132 // years in formats, so don't even try it
1133 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
1134 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
1135 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
1139 // an extra test (as it doesn't depend on date, don't do it in the loop)
1140 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
1142 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
1146 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
1147 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
1149 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
1150 printf("%s", s
.c_str());
1152 // what can we recover?
1153 int kind
= formatTestFormats
[n
].compareKind
;
1157 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
1160 // converion failed - should it have?
1161 if ( kind
== CompareNone
)
1164 puts(" (ERROR: conversion back failed)");
1168 // should have parsed the entire string
1169 puts(" (ERROR: conversion back stopped too soon)");
1173 bool equal
= FALSE
; // suppress compilaer warning
1181 equal
= dt
.IsSameDate(dt2
);
1185 equal
= dt
.IsSameTime(dt2
);
1191 printf(" (ERROR: got back '%s' instead of '%s')\n",
1192 dt2
.Format().c_str(), dt
.Format().c_str());
1203 // test text -> wxDateTime conversion
1204 static void TestTimeParse()
1206 puts("\n*** wxDateTime parse test ***");
1208 struct ParseTestData
1215 static const ParseTestData parseTestDates
[] =
1217 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
1218 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
1221 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1223 const char *format
= parseTestDates
[n
].format
;
1225 printf("%s => ", format
);
1228 if ( dt
.ParseRfc822Date(format
) )
1230 printf("%s ", dt
.Format().c_str());
1232 if ( parseTestDates
[n
].good
)
1234 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
1241 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
1246 puts("(ERROR: bad format)");
1251 printf("bad format (%s)\n",
1252 parseTestDates
[n
].good
? "ERROR" : "ok");
1257 static void TestInteractive()
1259 puts("\n*** interactive wxDateTime tests ***");
1265 printf("Enter a date: ");
1266 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
1270 if ( !dt
.ParseDate(buf
) )
1272 puts("failed to parse the date");
1277 printf("%s: day %u, week of month %u/%u, week of year %u\n",
1278 dt
.FormatISODate().c_str(),
1280 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1281 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1282 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
1285 puts("\n*** done ***");
1288 static void TestTimeArithmetics()
1290 puts("\n*** testing arithmetic operations on wxDateTime ***");
1296 } testArithmData
[] =
1298 { wxDateSpan::Day(), "day" },
1299 { wxDateSpan::Week(), "week" },
1300 { wxDateSpan::Month(), "month" },
1301 { wxDateSpan::Year(), "year" },
1302 { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
1305 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
1307 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1309 wxDateSpan span
= testArithmData
[n
].span
;
1313 const char *name
= testArithmData
[n
].name
;
1314 printf("%s + %s = %s, %s - %s = %s\n",
1315 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
1316 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
1318 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
1319 if ( dt1
- span
== dt
)
1325 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1328 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
1329 if ( dt2
+ span
== dt
)
1335 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1338 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
1339 if ( dt2
+ 2*span
== dt1
)
1345 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
1352 static void TestTimeHolidays()
1354 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
1356 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
1357 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1358 dtEnd
= dtStart
.GetLastMonthDay();
1360 wxDateTimeArray hol
;
1361 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1363 const wxChar
*format
= "%d-%b-%Y (%a)";
1365 printf("All holidays between %s and %s:\n",
1366 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
1368 size_t count
= hol
.GetCount();
1369 for ( size_t n
= 0; n
< count
; n
++ )
1371 printf("\t%s\n", hol
[n
].Format(format
).c_str());
1379 // test compatibility with the old wxDate/wxTime classes
1380 static void TestTimeCompatibility()
1382 puts("\n*** wxDateTime compatibility test ***");
1384 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1385 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1387 double jdnNow
= wxDateTime::Now().GetJDN();
1388 long jdnMidnight
= (long)(jdnNow
- 0.5);
1389 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
1391 jdnMidnight
= wxDate().Set().GetJulianDate();
1392 printf("wxDateTime for today: %s\n",
1393 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1395 int flags
= wxEUROPEAN
;//wxFULL;
1398 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1399 for ( int n
= 0; n
< 7; n
++ )
1401 printf("Previous %s is %s\n",
1402 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1403 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1411 // ----------------------------------------------------------------------------
1413 // ----------------------------------------------------------------------------
1417 #include <wx/thread.h>
1419 static size_t gs_counter
= (size_t)-1;
1420 static wxCriticalSection gs_critsect
;
1421 static wxCondition gs_cond
;
1423 class MyJoinableThread
: public wxThread
1426 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1427 { m_n
= n
; Create(); }
1429 // thread execution starts here
1430 virtual ExitCode
Entry();
1436 wxThread::ExitCode
MyJoinableThread::Entry()
1438 unsigned long res
= 1;
1439 for ( size_t n
= 1; n
< m_n
; n
++ )
1443 // it's a loooong calculation :-)
1447 return (ExitCode
)res
;
1450 class MyDetachedThread
: public wxThread
1453 MyDetachedThread(size_t n
, char ch
)
1457 m_cancelled
= FALSE
;
1462 // thread execution starts here
1463 virtual ExitCode
Entry();
1466 virtual void OnExit();
1469 size_t m_n
; // number of characters to write
1470 char m_ch
; // character to write
1472 bool m_cancelled
; // FALSE if we exit normally
1475 wxThread::ExitCode
MyDetachedThread::Entry()
1478 wxCriticalSectionLocker
lock(gs_critsect
);
1479 if ( gs_counter
== (size_t)-1 )
1485 for ( size_t n
= 0; n
< m_n
; n
++ )
1487 if ( TestDestroy() )
1497 wxThread::Sleep(100);
1503 void MyDetachedThread::OnExit()
1505 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1507 wxCriticalSectionLocker
lock(gs_critsect
);
1508 if ( !--gs_counter
&& !m_cancelled
)
1512 void TestDetachedThreads()
1514 puts("\n*** Testing detached threads ***");
1516 static const size_t nThreads
= 3;
1517 MyDetachedThread
*threads
[nThreads
];
1519 for ( n
= 0; n
< nThreads
; n
++ )
1521 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1524 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1525 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1527 for ( n
= 0; n
< nThreads
; n
++ )
1532 // wait until all threads terminate
1538 void TestJoinableThreads()
1540 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1542 // calc 10! in the background
1543 MyJoinableThread
thread(10);
1546 printf("\nThread terminated with exit code %lu.\n",
1547 (unsigned long)thread
.Wait());
1550 void TestThreadSuspend()
1552 puts("\n*** Testing thread suspend/resume functions ***");
1554 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1558 // this is for this demo only, in a real life program we'd use another
1559 // condition variable which would be signaled from wxThread::Entry() to
1560 // tell us that the thread really started running - but here just wait a
1561 // bit and hope that it will be enough (the problem is, of course, that
1562 // the thread might still not run when we call Pause() which will result
1564 wxThread::Sleep(300);
1566 for ( size_t n
= 0; n
< 3; n
++ )
1570 puts("\nThread suspended");
1573 // don't sleep but resume immediately the first time
1574 wxThread::Sleep(300);
1576 puts("Going to resume the thread");
1581 puts("Waiting until it terminates now");
1583 // wait until the thread terminates
1589 void TestThreadDelete()
1591 // As above, using Sleep() is only for testing here - we must use some
1592 // synchronisation object instead to ensure that the thread is still
1593 // running when we delete it - deleting a detached thread which already
1594 // terminated will lead to a crash!
1596 puts("\n*** Testing thread delete function ***");
1598 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1602 puts("\nDeleted a thread which didn't start to run yet.");
1604 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1608 wxThread::Sleep(300);
1612 puts("\nDeleted a running thread.");
1614 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1618 wxThread::Sleep(300);
1624 puts("\nDeleted a sleeping thread.");
1626 MyJoinableThread
thread3(20);
1631 puts("\nDeleted a joinable thread.");
1633 MyJoinableThread
thread4(2);
1636 wxThread::Sleep(300);
1640 puts("\nDeleted a joinable thread which already terminated.");
1645 #endif // TEST_THREADS
1647 // ----------------------------------------------------------------------------
1649 // ----------------------------------------------------------------------------
1653 void PrintArray(const char* name
, const wxArrayString
& array
)
1655 printf("Dump of the array '%s'\n", name
);
1657 size_t nCount
= array
.GetCount();
1658 for ( size_t n
= 0; n
< nCount
; n
++ )
1660 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1664 #endif // TEST_ARRAYS
1666 // ----------------------------------------------------------------------------
1668 // ----------------------------------------------------------------------------
1672 #include "wx/timer.h"
1674 static void TestString()
1684 for (int i
= 0; i
< 1000000; ++i
)
1688 c
= "! How'ya doin'?";
1691 c
= "Hello world! What's up?";
1696 printf ("TestString elapsed time: %ld\n", sw
.Time());
1699 static void TestPChar()
1707 for (int i
= 0; i
< 1000000; ++i
)
1709 strcpy (a
, "Hello");
1710 strcpy (b
, " world");
1711 strcpy (c
, "! How'ya doin'?");
1714 strcpy (c
, "Hello world! What's up?");
1715 if (strcmp (c
, a
) == 0)
1719 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1722 static void TestStringSub()
1724 wxString
s("Hello, world!");
1726 puts("*** Testing wxString substring extraction ***");
1728 printf("String = '%s'\n", s
.c_str());
1729 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1730 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1731 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1732 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1733 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1734 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1739 static void TestStringFormat()
1741 puts("*** Testing wxString formatting ***");
1744 s
.Printf("%03d", 18);
1746 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
1747 printf("Number 18: %s\n", s
.c_str());
1752 #endif // TEST_STRINGS
1754 // ----------------------------------------------------------------------------
1756 // ----------------------------------------------------------------------------
1758 int main(int argc
, char **argv
)
1760 if ( !wxInitialize() )
1762 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1766 puts("Sleeping for 3 seconds... z-z-z-z-z...");
1768 #endif // TEST_USLEEP
1771 static const wxCmdLineEntryDesc cmdLineDesc
[] =
1773 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
1774 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
1776 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
1777 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
1778 { wxCMD_LINE_OPTION
, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
1779 { wxCMD_LINE_OPTION
, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
1781 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
1782 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
1787 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
1789 switch ( parser
.Parse() )
1792 wxLogMessage("Help was given, terminating.");
1796 ShowCmdLine(parser
);
1800 wxLogMessage("Syntax error detected, aborting.");
1803 #endif // TEST_CMDLINE
1816 #endif // TEST_STRINGS
1827 puts("*** Initially:");
1829 PrintArray("a1", a1
);
1831 wxArrayString
a2(a1
);
1832 PrintArray("a2", a2
);
1834 wxSortedArrayString
a3(a1
);
1835 PrintArray("a3", a3
);
1837 puts("*** After deleting a string from a1");
1840 PrintArray("a1", a1
);
1841 PrintArray("a2", a2
);
1842 PrintArray("a3", a3
);
1844 puts("*** After reassigning a1 to a2 and a3");
1846 PrintArray("a2", a2
);
1847 PrintArray("a3", a3
);
1848 #endif // TEST_ARRAYS
1856 for ( size_t n
= 0; n
< 8000; n
++ )
1858 s
<< (char)('A' + (n
% 26));
1862 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
1864 // this one shouldn't be truncated
1867 // but this one will because log functions use fixed size buffer
1868 // (note that it doesn't need '\n' at the end neither - will be added
1870 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
1874 int nCPUs
= wxThread::GetCPUCount();
1875 printf("This system has %d CPUs\n", nCPUs
);
1877 wxThread::SetConcurrency(nCPUs
);
1879 if ( argc
> 1 && argv
[1][0] == 't' )
1880 wxLog::AddTraceMask("thread");
1883 TestDetachedThreads();
1885 TestJoinableThreads();
1887 TestThreadSuspend();
1891 #endif // TEST_THREADS
1893 #ifdef TEST_LONGLONG
1894 // seed pseudo random generator
1895 srand((unsigned)time(NULL
));
1901 TestMultiplication();
1906 TestLongLongConversion();
1907 TestBitOperations();
1909 #endif // TEST_LONGLONG
1929 TestTimeArithmetics();