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
35 //#define TEST_EXECUTE
37 //#define TEST_LONGLONG
40 //#define TEST_THREADS
43 // ============================================================================
45 // ============================================================================
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #include <wx/cmdline.h>
54 #include <wx/datetime.h>
56 static void ShowCmdLine(const wxCmdLineParser
& parser
)
58 wxString s
= "Input files: ";
60 size_t count
= parser
.GetParamCount();
61 for ( size_t param
= 0; param
< count
; param
++ )
63 s
<< parser
.GetParam(param
) << ' ';
67 << "Verbose:\t" << (parser
.Found("v") ? "yes" : "no") << '\n'
68 << "Quiet:\t" << (parser
.Found("q") ? "yes" : "no") << '\n';
73 if ( parser
.Found("o", &strVal
) )
74 s
<< "Output file:\t" << strVal
<< '\n';
75 if ( parser
.Found("i", &strVal
) )
76 s
<< "Input dir:\t" << strVal
<< '\n';
77 if ( parser
.Found("s", &lVal
) )
78 s
<< "Size:\t" << lVal
<< '\n';
79 if ( parser
.Found("d", &dt
) )
80 s
<< "Date:\t" << dt
.FormatISODate() << '\n';
85 #endif // TEST_CMDLINE
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
95 static void TestDirEnumHelper(wxDir
& dir
,
96 int flags
= wxDIR_DEFAULT
,
97 const wxString
& filespec
= wxEmptyString
)
101 if ( !dir
.IsOpened() )
104 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
107 printf("\t%s\n", filename
.c_str());
109 cont
= dir
.GetNext(&filename
);
115 static void TestDirEnum()
117 wxDir
dir(wxGetCwd());
119 puts("Enumerating everything in current directory:");
120 TestDirEnumHelper(dir
);
122 puts("Enumerating really everything in current directory:");
123 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
125 puts("Enumerating object files in current directory:");
126 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
128 puts("Enumerating directories in current directory:");
129 TestDirEnumHelper(dir
, wxDIR_DIRS
);
131 puts("Enumerating files in current directory:");
132 TestDirEnumHelper(dir
, wxDIR_FILES
);
134 puts("Enumerating files including hidden in current directory:");
135 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
139 #elif defined(__WXMSW__)
142 #error "don't know where the root directory is"
145 puts("Enumerating everything in root directory:");
146 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
148 puts("Enumerating directories in root directory:");
149 TestDirEnumHelper(dir
, wxDIR_DIRS
);
151 puts("Enumerating files in root directory:");
152 TestDirEnumHelper(dir
, wxDIR_FILES
);
154 puts("Enumerating files including hidden in root directory:");
155 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
157 puts("Enumerating files in non existing directory:");
158 wxDir
dirNo("nosuchdir");
159 TestDirEnumHelper(dirNo
);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
170 #include <wx/utils.h>
172 static void TestExecute()
174 puts("*** testing wxExecute ***");
177 #define COMMAND "echo hi"
178 #elif defined(__WXMSW__)
179 #define COMMAND "command.com -c 'echo hi'"
181 #error "no command to exec"
184 if ( wxExecute(COMMAND
) == 0 )
190 #endif // TEST_EXECUTE
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
198 #include <wx/mimetype.h>
200 static void TestMimeEnum()
202 wxMimeTypesManager mimeTM
;
203 wxArrayString mimetypes
;
205 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
207 printf("*** All %u known filetypes: ***\n", count
);
212 for ( size_t n
= 0; n
< count
; n
++ )
214 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
217 printf("nothing known about the filetype '%s'!\n",
218 mimetypes
[n
].c_str());
222 filetype
->GetDescription(&desc
);
223 filetype
->GetExtensions(exts
);
225 filetype
->GetIcon(NULL
);
228 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
235 printf("\t%s: %s (%s)\n",
236 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
248 #include <wx/longlong.h>
249 #include <wx/timer.h>
251 // make a 64 bit number from 4 16 bit ones
252 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
254 // get a random 64 bit number
255 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
257 #if wxUSE_LONGLONG_WX
258 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
259 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
260 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
261 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
262 #endif // wxUSE_LONGLONG_WX
264 static void TestSpeed()
266 static const long max
= 100000000;
273 for ( n
= 0; n
< max
; n
++ )
278 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
281 #if wxUSE_LONGLONG_NATIVE
286 for ( n
= 0; n
< max
; n
++ )
291 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
293 #endif // wxUSE_LONGLONG_NATIVE
299 for ( n
= 0; n
< max
; n
++ )
304 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
308 static void TestLongLongConversion()
310 puts("*** Testing wxLongLong conversions ***\n");
314 for ( size_t n
= 0; n
< 100000; n
++ )
318 #if wxUSE_LONGLONG_NATIVE
319 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
321 wxASSERT_MSG( a
== b
, "conversions failure" );
323 puts("Can't do it without native long long type, test skipped.");
326 #endif // wxUSE_LONGLONG_NATIVE
328 if ( !(nTested
% 1000) )
340 static void TestMultiplication()
342 puts("*** Testing wxLongLong multiplication ***\n");
346 for ( size_t n
= 0; n
< 100000; n
++ )
351 #if wxUSE_LONGLONG_NATIVE
352 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
353 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
355 wxASSERT_MSG( a
*b
== aa
*bb
, "multiplication failure" );
356 #else // !wxUSE_LONGLONG_NATIVE
357 puts("Can't do it without native long long type, test skipped.");
360 #endif // wxUSE_LONGLONG_NATIVE
362 if ( !(nTested
% 1000) )
374 static void TestDivision()
376 puts("*** Testing wxLongLong division ***\n");
380 for ( size_t n
= 0; n
< 100000; n
++ )
382 // get a random wxLongLong (shifting by 12 the MSB ensures that the
383 // multiplication will not overflow)
384 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
386 // get a random long (not wxLongLong for now) to divide it with
391 #if wxUSE_LONGLONG_NATIVE
392 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
394 wxLongLongNative p
= m
/ l
, s
= m
% l
;
395 wxASSERT_MSG( q
== p
&& r
== s
, "division failure" );
396 #else // !wxUSE_LONGLONG_NATIVE
398 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
399 #endif // wxUSE_LONGLONG_NATIVE
401 if ( !(nTested
% 1000) )
413 static void TestAddition()
415 puts("*** Testing wxLongLong addition ***\n");
419 for ( size_t n
= 0; n
< 100000; n
++ )
425 #if wxUSE_LONGLONG_NATIVE
426 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
427 wxLongLongNative(b
.GetHi(), b
.GetLo()),
428 "addition failure" );
429 #else // !wxUSE_LONGLONG_NATIVE
430 wxASSERT_MSG( c
- b
== a
, "addition failure" );
431 #endif // wxUSE_LONGLONG_NATIVE
433 if ( !(nTested
% 1000) )
445 static void TestBitOperations()
447 puts("*** Testing wxLongLong bit operation ***\n");
451 for ( size_t n
= 0; n
< 100000; n
++ )
455 #if wxUSE_LONGLONG_NATIVE
456 for ( size_t n
= 0; n
< 33; n
++ )
458 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
463 wxASSERT_MSG( b
== c
, "bit shift failure" );
465 b
= wxLongLongNative(a
.GetHi(), a
.GetLo()) << n
;
468 wxASSERT_MSG( b
== c
, "bit shift failure" );
471 #else // !wxUSE_LONGLONG_NATIVE
472 puts("Can't do it without native long long type, test skipped.");
475 #endif // wxUSE_LONGLONG_NATIVE
477 if ( !(nTested
% 1000) )
492 #endif // TEST_LONGLONG
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
502 #include <wx/datetime.h>
507 wxDateTime::wxDateTime_t day
;
508 wxDateTime::Month month
;
510 wxDateTime::wxDateTime_t hour
, min
, sec
;
512 wxDateTime::WeekDay wday
;
513 time_t gmticks
, ticks
;
515 void Init(const wxDateTime::Tm
& tm
)
524 gmticks
= ticks
= -1;
527 wxDateTime
DT() const
528 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
530 bool SameDay(const wxDateTime::Tm
& tm
) const
532 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
535 wxString
Format() const
538 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
540 wxDateTime::GetMonthName(month
).c_str(),
542 abs(wxDateTime::ConvertYearToBC(year
)),
543 year
> 0 ? "AD" : "BC");
547 wxString
FormatDate() const
550 s
.Printf("%02d-%s-%4d%s",
552 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
553 abs(wxDateTime::ConvertYearToBC(year
)),
554 year
> 0 ? "AD" : "BC");
559 static const Date testDates
[] =
561 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
562 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
563 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
564 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
565 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
566 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
567 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
568 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
569 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
570 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
571 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
572 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
573 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
574 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
575 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
578 // this test miscellaneous static wxDateTime functions
579 static void TestTimeStatic()
581 puts("\n*** wxDateTime static methods test ***");
583 // some info about the current date
584 int year
= wxDateTime::GetCurrentYear();
585 printf("Current year %d is %sa leap one and has %d days.\n",
587 wxDateTime::IsLeapYear(year
) ? "" : "not ",
588 wxDateTime::GetNumberOfDays(year
));
590 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
591 printf("Current month is '%s' ('%s') and it has %d days\n",
592 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
593 wxDateTime::GetMonthName(month
).c_str(),
594 wxDateTime::GetNumberOfDays(month
));
597 static const size_t nYears
= 5;
598 static const size_t years
[2][nYears
] =
600 // first line: the years to test
601 { 1990, 1976, 2000, 2030, 1984, },
603 // second line: TRUE if leap, FALSE otherwise
604 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
607 for ( size_t n
= 0; n
< nYears
; n
++ )
609 int year
= years
[0][n
];
610 bool should
= years
[1][n
] != 0,
611 is
= wxDateTime::IsLeapYear(year
);
613 printf("Year %d is %sa leap year (%s)\n",
616 should
== is
? "ok" : "ERROR");
618 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
622 // test constructing wxDateTime objects
623 static void TestTimeSet()
625 puts("\n*** wxDateTime construction test ***");
627 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
629 const Date
& d1
= testDates
[n
];
630 wxDateTime dt
= d1
.DT();
635 wxString s1
= d1
.Format(),
638 printf("Date: %s == %s (%s)\n",
639 s1
.c_str(), s2
.c_str(),
640 s1
== s2
? "ok" : "ERROR");
644 // test time zones stuff
645 static void TestTimeZones()
647 puts("\n*** wxDateTime timezone test ***");
649 wxDateTime now
= wxDateTime::Now();
651 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
652 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
653 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
654 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
655 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
656 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
658 wxDateTime::Tm tm
= now
.GetTm();
659 if ( wxDateTime(tm
) != now
)
661 printf("ERROR: got %s instead of %s\n",
662 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
666 // test some minimal support for the dates outside the standard range
667 static void TestTimeRange()
669 puts("\n*** wxDateTime out-of-standard-range dates test ***");
671 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
673 printf("Unix epoch:\t%s\n",
674 wxDateTime(2440587.5).Format(fmt
).c_str());
675 printf("Feb 29, 0: \t%s\n",
676 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
677 printf("JDN 0: \t%s\n",
678 wxDateTime(0.0).Format(fmt
).c_str());
679 printf("Jan 1, 1AD:\t%s\n",
680 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
681 printf("May 29, 2099:\t%s\n",
682 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
685 static void TestTimeTicks()
687 puts("\n*** wxDateTime ticks test ***");
689 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
691 const Date
& d
= testDates
[n
];
695 wxDateTime dt
= d
.DT();
696 long ticks
= (dt
.GetValue() / 1000).ToLong();
697 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
698 if ( ticks
== d
.ticks
)
704 printf(" (ERROR: should be %ld, delta = %ld)\n",
705 d
.ticks
, ticks
- d
.ticks
);
708 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
709 ticks
= (dt
.GetValue() / 1000).ToLong();
710 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
711 if ( ticks
== d
.gmticks
)
717 printf(" (ERROR: should be %ld, delta = %ld)\n",
718 d
.gmticks
, ticks
- d
.gmticks
);
725 // test conversions to JDN &c
726 static void TestTimeJDN()
728 puts("\n*** wxDateTime to JDN test ***");
730 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
732 const Date
& d
= testDates
[n
];
733 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
734 double jdn
= dt
.GetJulianDayNumber();
736 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
743 printf(" (ERROR: should be %f, delta = %f)\n",
749 // test week days computation
750 static void TestTimeWDays()
752 puts("\n*** wxDateTime weekday test ***");
756 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
758 const Date
& d
= testDates
[n
];
759 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
761 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
764 wxDateTime::GetWeekDayName(wday
).c_str());
765 if ( wday
== d
.wday
)
771 printf(" (ERROR: should be %s)\n",
772 wxDateTime::GetWeekDayName(d
.wday
).c_str());
778 // test SetToWeekDay()
779 struct WeekDateTestData
781 Date date
; // the real date (precomputed)
782 int nWeek
; // its week index in the month
783 wxDateTime::WeekDay wday
; // the weekday
784 wxDateTime::Month month
; // the month
785 int year
; // and the year
787 wxString
Format() const
790 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
792 case 1: which
= "first"; break;
793 case 2: which
= "second"; break;
794 case 3: which
= "third"; break;
795 case 4: which
= "fourth"; break;
796 case 5: which
= "fifth"; break;
798 case -1: which
= "last"; break;
803 which
+= " from end";
806 s
.Printf("The %s %s of %s in %d",
808 wxDateTime::GetWeekDayName(wday
).c_str(),
809 wxDateTime::GetMonthName(month
).c_str(),
816 // the array data was generated by the following python program
818 from DateTime import *
819 from whrandom import *
822 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
823 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
825 week = DateTimeDelta(7)
828 year = randint(1900, 2100)
829 month = randint(1, 12)
831 dt = DateTime(year, month, day)
832 wday = dt.day_of_week
834 countFromEnd = choice([-1, 1])
837 while dt.month is month:
838 dt = dt - countFromEnd * week
839 weekNum = weekNum + countFromEnd
841 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
843 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
844 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
847 static const WeekDateTestData weekDatesTestData
[] =
849 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
850 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
851 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
852 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
853 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
854 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
855 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
856 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
857 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
858 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
859 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
860 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
861 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
862 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
863 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
864 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
865 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
866 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
867 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
868 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
871 static const char *fmt
= "%d-%b-%Y";
874 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
876 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
878 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
880 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
882 const Date
& d
= wd
.date
;
883 if ( d
.SameDay(dt
.GetTm()) )
889 dt
.Set(d
.day
, d
.month
, d
.year
);
891 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
896 // test the computation of (ISO) week numbers
897 static void TestTimeWNumber()
899 puts("\n*** wxDateTime week number test ***");
901 struct WeekNumberTestData
903 Date date
; // the date
904 wxDateTime::wxDateTime_t week
; // the week number in the year
905 wxDateTime::wxDateTime_t wmon
; // the week number in the month
906 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
907 wxDateTime::wxDateTime_t dnum
; // day number in the year
910 // data generated with the following python script:
912 from DateTime import *
913 from whrandom import *
916 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
917 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
919 def GetMonthWeek(dt):
920 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
922 weekNumMonth = weekNumMonth + 53
925 def GetLastSundayBefore(dt):
926 if dt.iso_week[2] == 7:
929 return dt - DateTimeDelta(dt.iso_week[2])
932 year = randint(1900, 2100)
933 month = randint(1, 12)
935 dt = DateTime(year, month, day)
936 dayNum = dt.day_of_year
937 weekNum = dt.iso_week[1]
938 weekNumMonth = GetMonthWeek(dt)
941 dtSunday = GetLastSundayBefore(dt)
943 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
944 weekNumMonth2 = weekNumMonth2 + 1
945 dtSunday = dtSunday - DateTimeDelta(7)
947 data = { 'day': rjust(`day`, 2), \
948 'month': monthNames[month - 1], \
950 'weekNum': rjust(`weekNum`, 2), \
951 'weekNumMonth': weekNumMonth, \
952 'weekNumMonth2': weekNumMonth2, \
953 'dayNum': rjust(`dayNum`, 3) }
955 print " { { %(day)s, "\
956 "wxDateTime::%(month)s, "\
959 "%(weekNumMonth)s, "\
960 "%(weekNumMonth2)s, "\
961 "%(dayNum)s }," % data
964 static const WeekNumberTestData weekNumberTestDates
[] =
966 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
967 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
968 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
969 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
970 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
971 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
972 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
973 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
974 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
975 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
976 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
977 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
978 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
979 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
980 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
981 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
982 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
983 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
984 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
985 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
988 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
990 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
991 const Date
& d
= wn
.date
;
993 wxDateTime dt
= d
.DT();
995 wxDateTime::wxDateTime_t
996 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
997 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
998 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
999 dnum
= dt
.GetDayOfYear();
1001 printf("%s: the day number is %d",
1002 d
.FormatDate().c_str(), dnum
);
1003 if ( dnum
== wn
.dnum
)
1009 printf(" (ERROR: should be %d)", wn
.dnum
);
1012 printf(", week in month is %d", wmon
);
1013 if ( wmon
== wn
.wmon
)
1019 printf(" (ERROR: should be %d)", wn
.wmon
);
1022 printf(" or %d", wmon2
);
1023 if ( wmon2
== wn
.wmon2
)
1029 printf(" (ERROR: should be %d)", wn
.wmon2
);
1032 printf(", week in year is %d", week
);
1033 if ( week
== wn
.week
)
1039 printf(" (ERROR: should be %d)\n", wn
.week
);
1044 // test DST calculations
1045 static void TestTimeDST()
1047 puts("\n*** wxDateTime DST test ***");
1049 printf("DST is%s in effect now.\n\n",
1050 wxDateTime::Now().IsDST() ? "" : " not");
1052 // taken from http://www.energy.ca.gov/daylightsaving.html
1053 static const Date datesDST
[2][2004 - 1900 + 1] =
1056 { 1, wxDateTime::Apr
, 1990 },
1057 { 7, wxDateTime::Apr
, 1991 },
1058 { 5, wxDateTime::Apr
, 1992 },
1059 { 4, wxDateTime::Apr
, 1993 },
1060 { 3, wxDateTime::Apr
, 1994 },
1061 { 2, wxDateTime::Apr
, 1995 },
1062 { 7, wxDateTime::Apr
, 1996 },
1063 { 6, wxDateTime::Apr
, 1997 },
1064 { 5, wxDateTime::Apr
, 1998 },
1065 { 4, wxDateTime::Apr
, 1999 },
1066 { 2, wxDateTime::Apr
, 2000 },
1067 { 1, wxDateTime::Apr
, 2001 },
1068 { 7, wxDateTime::Apr
, 2002 },
1069 { 6, wxDateTime::Apr
, 2003 },
1070 { 4, wxDateTime::Apr
, 2004 },
1073 { 28, wxDateTime::Oct
, 1990 },
1074 { 27, wxDateTime::Oct
, 1991 },
1075 { 25, wxDateTime::Oct
, 1992 },
1076 { 31, wxDateTime::Oct
, 1993 },
1077 { 30, wxDateTime::Oct
, 1994 },
1078 { 29, wxDateTime::Oct
, 1995 },
1079 { 27, wxDateTime::Oct
, 1996 },
1080 { 26, wxDateTime::Oct
, 1997 },
1081 { 25, wxDateTime::Oct
, 1998 },
1082 { 31, wxDateTime::Oct
, 1999 },
1083 { 29, wxDateTime::Oct
, 2000 },
1084 { 28, wxDateTime::Oct
, 2001 },
1085 { 27, wxDateTime::Oct
, 2002 },
1086 { 26, wxDateTime::Oct
, 2003 },
1087 { 31, wxDateTime::Oct
, 2004 },
1092 for ( year
= 1990; year
< 2005; year
++ )
1094 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
1095 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
1097 printf("DST period in the US for year %d: from %s to %s",
1098 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
1100 size_t n
= year
- 1990;
1101 const Date
& dBegin
= datesDST
[0][n
];
1102 const Date
& dEnd
= datesDST
[1][n
];
1104 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
1110 printf(" (ERROR: should be %s %d to %s %d)\n",
1111 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
1112 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
1118 for ( year
= 1990; year
< 2005; year
++ )
1120 printf("DST period in Europe for year %d: from %s to %s\n",
1122 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
1123 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
1127 // test wxDateTime -> text conversion
1128 static void TestTimeFormat()
1130 puts("\n*** wxDateTime formatting test ***");
1132 // some information may be lost during conversion, so store what kind
1133 // of info should we recover after a round trip
1136 CompareNone
, // don't try comparing
1137 CompareBoth
, // dates and times should be identical
1138 CompareDate
, // dates only
1139 CompareTime
// time only
1144 CompareKind compareKind
;
1146 } formatTestFormats
[] =
1148 { CompareBoth
, "---> %c" },
1149 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
1150 { CompareBoth
, "Date is %x, time is %X" },
1151 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
1152 { CompareNone
, "The day of year: %j, the week of year: %W" },
1155 static const Date formatTestDates
[] =
1157 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
1158 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
1160 // this test can't work for other centuries because it uses two digit
1161 // years in formats, so don't even try it
1162 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
1163 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
1164 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
1168 // an extra test (as it doesn't depend on date, don't do it in the loop)
1169 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
1171 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
1175 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
1176 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
1178 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
1179 printf("%s", s
.c_str());
1181 // what can we recover?
1182 int kind
= formatTestFormats
[n
].compareKind
;
1186 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
1189 // converion failed - should it have?
1190 if ( kind
== CompareNone
)
1193 puts(" (ERROR: conversion back failed)");
1197 // should have parsed the entire string
1198 puts(" (ERROR: conversion back stopped too soon)");
1202 bool equal
= FALSE
; // suppress compilaer warning
1210 equal
= dt
.IsSameDate(dt2
);
1214 equal
= dt
.IsSameTime(dt2
);
1220 printf(" (ERROR: got back '%s' instead of '%s')\n",
1221 dt2
.Format().c_str(), dt
.Format().c_str());
1232 // test text -> wxDateTime conversion
1233 static void TestTimeParse()
1235 puts("\n*** wxDateTime parse test ***");
1237 struct ParseTestData
1244 static const ParseTestData parseTestDates
[] =
1246 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
1247 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
1250 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1252 const char *format
= parseTestDates
[n
].format
;
1254 printf("%s => ", format
);
1257 if ( dt
.ParseRfc822Date(format
) )
1259 printf("%s ", dt
.Format().c_str());
1261 if ( parseTestDates
[n
].good
)
1263 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
1270 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
1275 puts("(ERROR: bad format)");
1280 printf("bad format (%s)\n",
1281 parseTestDates
[n
].good
? "ERROR" : "ok");
1286 static void TestInteractive()
1288 puts("\n*** interactive wxDateTime tests ***");
1294 printf("Enter a date: ");
1295 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
1299 if ( !dt
.ParseDate(buf
) )
1301 puts("failed to parse the date");
1306 printf("%s: day %u, week of month %u/%u, week of year %u\n",
1307 dt
.FormatISODate().c_str(),
1309 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1310 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1311 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
1314 puts("\n*** done ***");
1317 static void TestTimeArithmetics()
1319 puts("\n*** testing arithmetic operations on wxDateTime ***");
1325 } testArithmData
[] =
1327 { wxDateSpan::Day(), "day" },
1328 { wxDateSpan::Week(), "week" },
1329 { wxDateSpan::Month(), "month" },
1330 { wxDateSpan::Year(), "year" },
1331 { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
1334 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
1336 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1338 wxDateSpan span
= testArithmData
[n
].span
;
1342 const char *name
= testArithmData
[n
].name
;
1343 printf("%s + %s = %s, %s - %s = %s\n",
1344 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
1345 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
1347 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
1348 if ( dt1
- span
== dt
)
1354 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1357 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
1358 if ( dt2
+ span
== dt
)
1364 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1367 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
1368 if ( dt2
+ 2*span
== dt1
)
1374 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
1381 static void TestTimeHolidays()
1383 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
1385 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
1386 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1387 dtEnd
= dtStart
.GetLastMonthDay();
1389 wxDateTimeArray hol
;
1390 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1392 const wxChar
*format
= "%d-%b-%Y (%a)";
1394 printf("All holidays between %s and %s:\n",
1395 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
1397 size_t count
= hol
.GetCount();
1398 for ( size_t n
= 0; n
< count
; n
++ )
1400 printf("\t%s\n", hol
[n
].Format(format
).c_str());
1408 // test compatibility with the old wxDate/wxTime classes
1409 static void TestTimeCompatibility()
1411 puts("\n*** wxDateTime compatibility test ***");
1413 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1414 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1416 double jdnNow
= wxDateTime::Now().GetJDN();
1417 long jdnMidnight
= (long)(jdnNow
- 0.5);
1418 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
1420 jdnMidnight
= wxDate().Set().GetJulianDate();
1421 printf("wxDateTime for today: %s\n",
1422 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1424 int flags
= wxEUROPEAN
;//wxFULL;
1427 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1428 for ( int n
= 0; n
< 7; n
++ )
1430 printf("Previous %s is %s\n",
1431 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1432 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1440 // ----------------------------------------------------------------------------
1442 // ----------------------------------------------------------------------------
1446 #include <wx/thread.h>
1448 static size_t gs_counter
= (size_t)-1;
1449 static wxCriticalSection gs_critsect
;
1450 static wxCondition gs_cond
;
1452 class MyJoinableThread
: public wxThread
1455 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1456 { m_n
= n
; Create(); }
1458 // thread execution starts here
1459 virtual ExitCode
Entry();
1465 wxThread::ExitCode
MyJoinableThread::Entry()
1467 unsigned long res
= 1;
1468 for ( size_t n
= 1; n
< m_n
; n
++ )
1472 // it's a loooong calculation :-)
1476 return (ExitCode
)res
;
1479 class MyDetachedThread
: public wxThread
1482 MyDetachedThread(size_t n
, char ch
)
1486 m_cancelled
= FALSE
;
1491 // thread execution starts here
1492 virtual ExitCode
Entry();
1495 virtual void OnExit();
1498 size_t m_n
; // number of characters to write
1499 char m_ch
; // character to write
1501 bool m_cancelled
; // FALSE if we exit normally
1504 wxThread::ExitCode
MyDetachedThread::Entry()
1507 wxCriticalSectionLocker
lock(gs_critsect
);
1508 if ( gs_counter
== (size_t)-1 )
1514 for ( size_t n
= 0; n
< m_n
; n
++ )
1516 if ( TestDestroy() )
1526 wxThread::Sleep(100);
1532 void MyDetachedThread::OnExit()
1534 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1536 wxCriticalSectionLocker
lock(gs_critsect
);
1537 if ( !--gs_counter
&& !m_cancelled
)
1541 void TestDetachedThreads()
1543 puts("\n*** Testing detached threads ***");
1545 static const size_t nThreads
= 3;
1546 MyDetachedThread
*threads
[nThreads
];
1548 for ( n
= 0; n
< nThreads
; n
++ )
1550 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1553 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1554 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1556 for ( n
= 0; n
< nThreads
; n
++ )
1561 // wait until all threads terminate
1567 void TestJoinableThreads()
1569 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1571 // calc 10! in the background
1572 MyJoinableThread
thread(10);
1575 printf("\nThread terminated with exit code %lu.\n",
1576 (unsigned long)thread
.Wait());
1579 void TestThreadSuspend()
1581 puts("\n*** Testing thread suspend/resume functions ***");
1583 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1587 // this is for this demo only, in a real life program we'd use another
1588 // condition variable which would be signaled from wxThread::Entry() to
1589 // tell us that the thread really started running - but here just wait a
1590 // bit and hope that it will be enough (the problem is, of course, that
1591 // the thread might still not run when we call Pause() which will result
1593 wxThread::Sleep(300);
1595 for ( size_t n
= 0; n
< 3; n
++ )
1599 puts("\nThread suspended");
1602 // don't sleep but resume immediately the first time
1603 wxThread::Sleep(300);
1605 puts("Going to resume the thread");
1610 puts("Waiting until it terminates now");
1612 // wait until the thread terminates
1618 void TestThreadDelete()
1620 // As above, using Sleep() is only for testing here - we must use some
1621 // synchronisation object instead to ensure that the thread is still
1622 // running when we delete it - deleting a detached thread which already
1623 // terminated will lead to a crash!
1625 puts("\n*** Testing thread delete function ***");
1627 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
1631 puts("\nDeleted a thread which didn't start to run yet.");
1633 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
1637 wxThread::Sleep(300);
1641 puts("\nDeleted a running thread.");
1643 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
1647 wxThread::Sleep(300);
1653 puts("\nDeleted a sleeping thread.");
1655 MyJoinableThread
thread3(20);
1660 puts("\nDeleted a joinable thread.");
1662 MyJoinableThread
thread4(2);
1665 wxThread::Sleep(300);
1669 puts("\nDeleted a joinable thread which already terminated.");
1674 #endif // TEST_THREADS
1676 // ----------------------------------------------------------------------------
1678 // ----------------------------------------------------------------------------
1682 void PrintArray(const char* name
, const wxArrayString
& array
)
1684 printf("Dump of the array '%s'\n", name
);
1686 size_t nCount
= array
.GetCount();
1687 for ( size_t n
= 0; n
< nCount
; n
++ )
1689 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
1693 #endif // TEST_ARRAYS
1695 // ----------------------------------------------------------------------------
1697 // ----------------------------------------------------------------------------
1701 #include "wx/timer.h"
1702 #include "wx/tokenzr.h"
1704 static void TestString()
1714 for (int i
= 0; i
< 1000000; ++i
)
1718 c
= "! How'ya doin'?";
1721 c
= "Hello world! What's up?";
1726 printf ("TestString elapsed time: %ld\n", sw
.Time());
1729 static void TestPChar()
1737 for (int i
= 0; i
< 1000000; ++i
)
1739 strcpy (a
, "Hello");
1740 strcpy (b
, " world");
1741 strcpy (c
, "! How'ya doin'?");
1744 strcpy (c
, "Hello world! What's up?");
1745 if (strcmp (c
, a
) == 0)
1749 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
1752 static void TestStringSub()
1754 wxString
s("Hello, world!");
1756 puts("*** Testing wxString substring extraction ***");
1758 printf("String = '%s'\n", s
.c_str());
1759 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
1760 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
1761 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
1762 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
1763 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
1764 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
1769 static void TestStringFormat()
1771 puts("*** Testing wxString formatting ***");
1774 s
.Printf("%03d", 18);
1776 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
1777 printf("Number 18: %s\n", s
.c_str());
1782 // returns "not found" for npos, value for all others
1783 static wxString
PosToString(size_t res
)
1785 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
1786 : wxString::Format(_T("%u"), res
);
1790 static void TestStringFind()
1792 puts("*** Testing wxString find() functions ***");
1794 static const wxChar
*strToFind
= _T("ell");
1795 static const struct StringFindTest
1799 result
; // of searching "ell" in str
1802 { _T("Well, hello world"), 0, 1 },
1803 { _T("Well, hello world"), 6, 7 },
1804 { _T("Well, hello world"), 9, wxString::npos
},
1807 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
1809 const StringFindTest
& ft
= findTestData
[n
];
1810 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
1812 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
1813 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
1815 size_t resTrue
= ft
.result
;
1816 if ( res
== resTrue
)
1822 printf(_T("(ERROR: should be %s)\n"),
1823 PosToString(resTrue
).c_str());
1830 // replace TABs with \t and CRs with \n
1831 static wxString
MakePrintable(const wxChar
*s
)
1834 (void)str
.Replace(_T("\t"), _T("\\t"));
1835 (void)str
.Replace(_T("\n"), _T("\\n"));
1836 (void)str
.Replace(_T("\r"), _T("\\r"));
1841 static void TestStringTokenizer()
1843 puts("*** Testing wxStringTokenizer ***");
1845 static const struct StringTokenizerTest
1847 const wxChar
*str
; // string to tokenize
1848 const wxChar
*delims
; // delimiters to use
1849 size_t count
; // count of token
1850 bool with
; // return tokens with delimiters?
1851 } tokenizerTestData
[] =
1853 { _T(""), _T(" "), 0, FALSE
},
1854 { _T("Hello, world"), _T(" "), 2, FALSE
},
1855 { _T("Hello, world"), _T(","), 2, FALSE
},
1856 { _T("Hello, world!"), _T(",!"), 3, TRUE
},
1857 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, FALSE
},
1858 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, TRUE
},
1859 { _T("01/02/99"), _T("/-"), 3, FALSE
},
1862 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
1864 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
1865 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.with
);
1867 size_t count
= tkz
.CountTokens();
1868 printf(_T("String '%s' has %u tokens delimited by '%s' "),
1871 MakePrintable(tt
.delims
).c_str());
1872 if ( count
== tt
.count
)
1878 printf(_T("(ERROR: should be %u)\n"), tt
.count
);
1883 // now show the tokens themselves
1885 while ( tkz
.HasMoreTokens() )
1887 printf(_T("\ttoken %u: '%s'\n"),
1889 MakePrintable(tkz
.GetNextToken()).c_str());
1892 if ( count2
!= count
)
1894 puts(_T("ERROR: token count mismatch"));
1901 #endif // TEST_STRINGS
1903 // ----------------------------------------------------------------------------
1905 // ----------------------------------------------------------------------------
1907 int main(int argc
, char **argv
)
1909 if ( !wxInitialize() )
1911 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
1915 puts("Sleeping for 3 seconds... z-z-z-z-z...");
1917 #endif // TEST_USLEEP
1920 static const wxCmdLineEntryDesc cmdLineDesc
[] =
1922 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
1923 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
1925 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
1926 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
1927 { wxCMD_LINE_OPTION
, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
1928 { wxCMD_LINE_OPTION
, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
1930 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
1931 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
1936 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
1938 switch ( parser
.Parse() )
1941 wxLogMessage("Help was given, terminating.");
1945 ShowCmdLine(parser
);
1949 wxLogMessage("Syntax error detected, aborting.");
1952 #endif // TEST_CMDLINE
1966 TestStringTokenizer();
1967 #endif // TEST_STRINGS
1978 puts("*** Initially:");
1980 PrintArray("a1", a1
);
1982 wxArrayString
a2(a1
);
1983 PrintArray("a2", a2
);
1985 wxSortedArrayString
a3(a1
);
1986 PrintArray("a3", a3
);
1988 puts("*** After deleting a string from a1");
1991 PrintArray("a1", a1
);
1992 PrintArray("a2", a2
);
1993 PrintArray("a3", a3
);
1995 puts("*** After reassigning a1 to a2 and a3");
1997 PrintArray("a2", a2
);
1998 PrintArray("a3", a3
);
1999 #endif // TEST_ARRAYS
2007 #endif // TEST_EXECUTE
2011 for ( size_t n
= 0; n
< 8000; n
++ )
2013 s
<< (char)('A' + (n
% 26));
2017 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
2019 // this one shouldn't be truncated
2022 // but this one will because log functions use fixed size buffer
2023 // (note that it doesn't need '\n' at the end neither - will be added
2025 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
2029 int nCPUs
= wxThread::GetCPUCount();
2030 printf("This system has %d CPUs\n", nCPUs
);
2032 wxThread::SetConcurrency(nCPUs
);
2034 if ( argc
> 1 && argv
[1][0] == 't' )
2035 wxLog::AddTraceMask("thread");
2038 TestDetachedThreads();
2040 TestJoinableThreads();
2042 TestThreadSuspend();
2046 #endif // TEST_THREADS
2048 #ifdef TEST_LONGLONG
2049 // seed pseudo random generator
2050 srand((unsigned)time(NULL
));
2056 TestMultiplication();
2061 TestLongLongConversion();
2062 TestBitOperations();
2064 #endif // TEST_LONGLONG
2084 TestTimeArithmetics();