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 // without this pragma, the stupid compiler precompiles #defines below so that
27 // changing them doesn't "take place" later!
32 // ----------------------------------------------------------------------------
33 // conditional compilation
34 // ----------------------------------------------------------------------------
36 // what to test (in alphabetic order)?
39 //#define TEST_CMDLINE
40 //#define TEST_DATETIME
42 //#define TEST_EXECUTE
43 //#define TEST_FILECONF
46 //#define TEST_LONGLONG
49 //#define TEST_STRINGS
50 //#define TEST_THREADS
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
63 // replace TABs with \t and CRs with \n
64 static wxString
MakePrintable(const wxChar
*s
)
67 (void)str
.Replace(_T("\t"), _T("\\t"));
68 (void)str
.Replace(_T("\n"), _T("\\n"));
69 (void)str
.Replace(_T("\r"), _T("\\r"));
74 #endif // MakePrintable() is used
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
82 #include <wx/cmdline.h>
83 #include <wx/datetime.h>
85 static void ShowCmdLine(const wxCmdLineParser
& parser
)
87 wxString s
= "Input files: ";
89 size_t count
= parser
.GetParamCount();
90 for ( size_t param
= 0; param
< count
; param
++ )
92 s
<< parser
.GetParam(param
) << ' ';
96 << "Verbose:\t" << (parser
.Found("v") ? "yes" : "no") << '\n'
97 << "Quiet:\t" << (parser
.Found("q") ? "yes" : "no") << '\n';
102 if ( parser
.Found("o", &strVal
) )
103 s
<< "Output file:\t" << strVal
<< '\n';
104 if ( parser
.Found("i", &strVal
) )
105 s
<< "Input dir:\t" << strVal
<< '\n';
106 if ( parser
.Found("s", &lVal
) )
107 s
<< "Size:\t" << lVal
<< '\n';
108 if ( parser
.Found("d", &dt
) )
109 s
<< "Date:\t" << dt
.FormatISODate() << '\n';
114 #endif // TEST_CMDLINE
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
124 static void TestDirEnumHelper(wxDir
& dir
,
125 int flags
= wxDIR_DEFAULT
,
126 const wxString
& filespec
= wxEmptyString
)
130 if ( !dir
.IsOpened() )
133 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
136 printf("\t%s\n", filename
.c_str());
138 cont
= dir
.GetNext(&filename
);
144 static void TestDirEnum()
146 wxDir
dir(wxGetCwd());
148 puts("Enumerating everything in current directory:");
149 TestDirEnumHelper(dir
);
151 puts("Enumerating really everything in current directory:");
152 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
154 puts("Enumerating object files in current directory:");
155 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
157 puts("Enumerating directories in current directory:");
158 TestDirEnumHelper(dir
, wxDIR_DIRS
);
160 puts("Enumerating files in current directory:");
161 TestDirEnumHelper(dir
, wxDIR_FILES
);
163 puts("Enumerating files including hidden in current directory:");
164 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
168 #elif defined(__WXMSW__)
171 #error "don't know where the root directory is"
174 puts("Enumerating everything in root directory:");
175 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
177 puts("Enumerating directories in root directory:");
178 TestDirEnumHelper(dir
, wxDIR_DIRS
);
180 puts("Enumerating files in root directory:");
181 TestDirEnumHelper(dir
, wxDIR_FILES
);
183 puts("Enumerating files including hidden in root directory:");
184 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
186 puts("Enumerating files in non existing directory:");
187 wxDir
dirNo("nosuchdir");
188 TestDirEnumHelper(dirNo
);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
199 #include <wx/utils.h>
201 static void TestExecute()
203 puts("*** testing wxExecute ***");
206 #define COMMAND "echo hi"
207 #define SHELL_COMMAND "echo hi from shell"
208 #define REDIRECT_COMMAND "date"
209 #elif defined(__WXMSW__)
210 #define COMMAND "command.com -c 'echo hi'"
211 #define SHELL_COMMAND "echo hi"
212 #define REDIRECT_COMMAND COMMAND
214 #error "no command to exec"
217 printf("Testing wxShell: ");
219 if ( wxShell(SHELL_COMMAND
) )
224 printf("Testing wxExecute: ");
226 if ( wxExecute(COMMAND
, TRUE
/* sync */) == 0 )
231 #if 0 // no, it doesn't work (yet?)
232 printf("Testing async wxExecute: ");
234 if ( wxExecute(COMMAND
) != 0 )
235 puts("Ok (command launched).");
240 printf("Testing wxExecute with redirection:\n");
241 wxArrayString output
;
242 if ( wxExecute(REDIRECT_COMMAND
, output
) != 0 )
248 size_t count
= output
.GetCount();
249 for ( size_t n
= 0; n
< count
; n
++ )
251 printf("\t%s\n", output
[n
].c_str());
258 #endif // TEST_EXECUTE
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
266 #include <wx/confbase.h>
267 #include <wx/fileconf.h>
269 static const struct FileConfTestData
271 const wxChar
*name
; // value name
272 const wxChar
*value
; // the value from the file
275 { _T("value1"), _T("one") },
276 { _T("value2"), _T("two") },
277 { _T("novalue"), _T("default") },
280 static void TestFileConfRead()
282 puts("*** testing wxFileConfig loading/reading ***");
284 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
285 _T("testdata.fc"), wxEmptyString
,
286 wxCONFIG_USE_RELATIVE_PATH
);
288 // test simple reading
289 puts("\nReading config file:");
290 wxString
defValue(_T("default")), value
;
291 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
293 const FileConfTestData
& data
= fcTestData
[n
];
294 value
= fileconf
.Read(data
.name
, defValue
);
295 printf("\t%s = %s ", data
.name
, value
.c_str());
296 if ( value
== data
.value
)
302 printf("(ERROR: should be %s)\n", data
.value
);
306 // test enumerating the entries
307 puts("\nEnumerating all root entries:");
310 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
313 printf("\t%s = %s\n",
315 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
317 cont
= fileconf
.GetNextEntry(name
, dummy
);
321 #endif // TEST_FILECONF
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
333 Foo(int n_
) { n
= n_
; count
++; }
341 size_t Foo::count
= 0;
343 WX_DECLARE_LIST(Foo
, wxListFoos
);
344 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
346 #include <wx/listimpl.cpp>
348 WX_DEFINE_LIST(wxListFoos
);
350 static void TestHash()
352 puts("*** Testing wxHashTable ***\n");
356 hash
.DeleteContents(TRUE
);
358 printf("Hash created: %u foos in hash, %u foos totally\n",
359 hash
.GetCount(), Foo::count
);
361 static const int hashTestData
[] =
363 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
367 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
369 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
372 printf("Hash filled: %u foos in hash, %u foos totally\n",
373 hash
.GetCount(), Foo::count
);
375 puts("Hash access test:");
376 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
378 printf("\tGetting element with key %d, value %d: ",
380 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
383 printf("ERROR, not found.\n");
387 printf("%d (%s)\n", foo
->n
,
388 (size_t)foo
->n
== n
? "ok" : "ERROR");
392 printf("\nTrying to get an element not in hash: ");
394 if ( hash
.Get(1234) || hash
.Get(1, 0) )
396 puts("ERROR: found!");
400 puts("ok (not found)");
404 printf("Hash destroyed: %u foos left\n", Foo::count
);
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
415 #include <wx/mimetype.h>
417 static void TestMimeEnum()
419 wxMimeTypesManager mimeTM
;
420 wxArrayString mimetypes
;
422 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
424 printf("*** All %u known filetypes: ***\n", count
);
429 for ( size_t n
= 0; n
< count
; n
++ )
431 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
434 printf("nothing known about the filetype '%s'!\n",
435 mimetypes
[n
].c_str());
439 filetype
->GetDescription(&desc
);
440 filetype
->GetExtensions(exts
);
442 filetype
->GetIcon(NULL
);
445 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
452 printf("\t%s: %s (%s)\n",
453 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
465 #include <wx/longlong.h>
466 #include <wx/timer.h>
468 // make a 64 bit number from 4 16 bit ones
469 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
471 // get a random 64 bit number
472 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
474 #if wxUSE_LONGLONG_WX
475 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
476 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
477 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
478 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
479 #endif // wxUSE_LONGLONG_WX
481 static void TestSpeed()
483 static const long max
= 100000000;
490 for ( n
= 0; n
< max
; n
++ )
495 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
498 #if wxUSE_LONGLONG_NATIVE
503 for ( n
= 0; n
< max
; n
++ )
508 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
510 #endif // wxUSE_LONGLONG_NATIVE
516 for ( n
= 0; n
< max
; n
++ )
521 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
525 static void TestLongLongConversion()
527 puts("*** Testing wxLongLong conversions ***\n");
531 for ( size_t n
= 0; n
< 100000; n
++ )
535 #if wxUSE_LONGLONG_NATIVE
536 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
538 wxASSERT_MSG( a
== b
, "conversions failure" );
540 puts("Can't do it without native long long type, test skipped.");
543 #endif // wxUSE_LONGLONG_NATIVE
545 if ( !(nTested
% 1000) )
557 static void TestMultiplication()
559 puts("*** Testing wxLongLong multiplication ***\n");
563 for ( size_t n
= 0; n
< 100000; n
++ )
568 #if wxUSE_LONGLONG_NATIVE
569 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
570 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
572 wxASSERT_MSG( a
*b
== aa
*bb
, "multiplication failure" );
573 #else // !wxUSE_LONGLONG_NATIVE
574 puts("Can't do it without native long long type, test skipped.");
577 #endif // wxUSE_LONGLONG_NATIVE
579 if ( !(nTested
% 1000) )
591 static void TestDivision()
593 puts("*** Testing wxLongLong division ***\n");
597 for ( size_t n
= 0; n
< 100000; n
++ )
599 // get a random wxLongLong (shifting by 12 the MSB ensures that the
600 // multiplication will not overflow)
601 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
603 // get a random long (not wxLongLong for now) to divide it with
608 #if wxUSE_LONGLONG_NATIVE
609 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
611 wxLongLongNative p
= m
/ l
, s
= m
% l
;
612 wxASSERT_MSG( q
== p
&& r
== s
, "division failure" );
613 #else // !wxUSE_LONGLONG_NATIVE
615 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
616 #endif // wxUSE_LONGLONG_NATIVE
618 if ( !(nTested
% 1000) )
630 static void TestAddition()
632 puts("*** Testing wxLongLong addition ***\n");
636 for ( size_t n
= 0; n
< 100000; n
++ )
642 #if wxUSE_LONGLONG_NATIVE
643 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
644 wxLongLongNative(b
.GetHi(), b
.GetLo()),
645 "addition failure" );
646 #else // !wxUSE_LONGLONG_NATIVE
647 wxASSERT_MSG( c
- b
== a
, "addition failure" );
648 #endif // wxUSE_LONGLONG_NATIVE
650 if ( !(nTested
% 1000) )
662 static void TestBitOperations()
664 puts("*** Testing wxLongLong bit operation ***\n");
668 for ( size_t n
= 0; n
< 100000; n
++ )
672 #if wxUSE_LONGLONG_NATIVE
673 for ( size_t n
= 0; n
< 33; n
++ )
675 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
680 wxASSERT_MSG( b
== c
, "bit shift failure" );
682 b
= wxLongLongNative(a
.GetHi(), a
.GetLo()) << n
;
685 wxASSERT_MSG( b
== c
, "bit shift failure" );
688 #else // !wxUSE_LONGLONG_NATIVE
689 puts("Can't do it without native long long type, test skipped.");
692 #endif // wxUSE_LONGLONG_NATIVE
694 if ( !(nTested
% 1000) )
709 #endif // TEST_LONGLONG
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
717 #include <wx/socket.h>
718 #include <wx/protocol/protocol.h>
719 #include <wx/protocol/ftp.h>
720 #include <wx/protocol/http.h>
722 static void TestSocketServer()
724 puts("*** Testing wxSocketServer ***\n");
726 // we want to launch a server
730 wxSocketServer
*server
= new wxSocketServer(addr
);
733 puts("ERROR: failed to bind");
737 static void TestSocketClient()
739 puts("*** Testing wxSocketClient ***\n");
741 static const char *hostname
= "www.wxwindows.org";
744 addr
.Hostname(hostname
);
747 printf("--- Attempting to connect to %s:80...\n", hostname
);
749 wxSocketClient client
;
750 if ( !client
.Connect(addr
) )
752 printf("ERROR: failed to connect to %s\n", hostname
);
756 printf("--- Connected to %s:%u...\n",
757 addr
.Hostname().c_str(), addr
.Service());
761 // could use simply "GET" here I suppose
763 wxString::Format("GET http://%s/\r\n", hostname
);
764 client
.Write(cmdGet
, cmdGet
.length());
765 printf("--- Sent command '%s' to the server\n",
766 MakePrintable(cmdGet
).c_str());
767 client
.Read(buf
, WXSIZEOF(buf
));
768 printf("--- Server replied:\n%s", buf
);
772 static void TestProtocolFtp()
774 puts("*** Testing wxFTP ***\n");
776 wxLog::AddTraceMask(_T("ftp"));
778 static const char *hostname
= "ftp.wxwindows.org";
780 printf("--- Attempting to connect to %s:21...\n", hostname
);
783 if ( !ftp
.Connect(hostname
) )
785 printf("ERROR: failed to connect to %s\n", hostname
);
789 printf("--- Connected to %s, current directory is '%s'\n",
790 hostname
, ftp
.Pwd().c_str());
791 if ( !ftp
.ChDir(_T("pub")) )
793 puts("ERROR: failed to cd to pub");
797 if ( !ftp
.GetList(files
) )
799 puts("ERROR: failed to get list of files");
803 printf("List of files under '%s':\n", ftp
.Pwd().c_str());
804 size_t count
= files
.GetCount();
805 for ( size_t n
= 0; n
< count
; n
++ )
807 printf("\t%s\n", files
[n
].c_str());
809 puts("End of the file list");
812 if ( !ftp
.ChDir(_T("..")) )
814 puts("ERROR: failed to cd to ..");
817 static const char *filename
= "welcome.msg";
818 wxInputStream
*in
= ftp
.GetInputStream(filename
);
821 puts("ERROR: couldn't get input stream");
825 size_t size
= in
->StreamSize();
826 printf("Reading file %s (%u bytes)...", filename
, size
);
828 char *data
= new char[size
];
829 if ( !in
->Read(data
, size
) )
831 puts("ERROR: read error");
835 printf("\nContents of %s:\n%s\n", filename
, data
);
844 #endif // TEST_SOCKETS
846 // ----------------------------------------------------------------------------
848 // ----------------------------------------------------------------------------
852 #include <wx/timer.h>
853 #include <wx/utils.h>
855 static void TestStopWatch()
857 puts("*** Testing wxStopWatch ***\n");
860 printf("Sleeping 3 seconds...");
862 printf("\telapsed time: %ldms\n", sw
.Time());
865 printf("Sleeping 2 more seconds...");
867 printf("\telapsed time: %ldms\n", sw
.Time());
870 printf("And 3 more seconds...");
872 printf("\telapsed time: %ldms\n", sw
.Time());
875 puts("\nChecking for 'backwards clock' bug...");
876 for ( size_t n
= 0; n
< 70; n
++ )
880 for ( size_t m
= 0; m
< 100000; m
++ )
882 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
884 puts("\ntime is negative - ERROR!");
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
904 #include <wx/datetime.h>
909 wxDateTime::wxDateTime_t day
;
910 wxDateTime::Month month
;
912 wxDateTime::wxDateTime_t hour
, min
, sec
;
914 wxDateTime::WeekDay wday
;
915 time_t gmticks
, ticks
;
917 void Init(const wxDateTime::Tm
& tm
)
926 gmticks
= ticks
= -1;
929 wxDateTime
DT() const
930 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
932 bool SameDay(const wxDateTime::Tm
& tm
) const
934 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
937 wxString
Format() const
940 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
942 wxDateTime::GetMonthName(month
).c_str(),
944 abs(wxDateTime::ConvertYearToBC(year
)),
945 year
> 0 ? "AD" : "BC");
949 wxString
FormatDate() const
952 s
.Printf("%02d-%s-%4d%s",
954 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
955 abs(wxDateTime::ConvertYearToBC(year
)),
956 year
> 0 ? "AD" : "BC");
961 static const Date testDates
[] =
963 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
964 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
965 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
966 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
967 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
968 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
969 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
970 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
971 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
972 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
973 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
974 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
975 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
976 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
977 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
980 // this test miscellaneous static wxDateTime functions
981 static void TestTimeStatic()
983 puts("\n*** wxDateTime static methods test ***");
985 // some info about the current date
986 int year
= wxDateTime::GetCurrentYear();
987 printf("Current year %d is %sa leap one and has %d days.\n",
989 wxDateTime::IsLeapYear(year
) ? "" : "not ",
990 wxDateTime::GetNumberOfDays(year
));
992 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
993 printf("Current month is '%s' ('%s') and it has %d days\n",
994 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
995 wxDateTime::GetMonthName(month
).c_str(),
996 wxDateTime::GetNumberOfDays(month
));
999 static const size_t nYears
= 5;
1000 static const size_t years
[2][nYears
] =
1002 // first line: the years to test
1003 { 1990, 1976, 2000, 2030, 1984, },
1005 // second line: TRUE if leap, FALSE otherwise
1006 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
1009 for ( size_t n
= 0; n
< nYears
; n
++ )
1011 int year
= years
[0][n
];
1012 bool should
= years
[1][n
] != 0,
1013 is
= wxDateTime::IsLeapYear(year
);
1015 printf("Year %d is %sa leap year (%s)\n",
1018 should
== is
? "ok" : "ERROR");
1020 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
1024 // test constructing wxDateTime objects
1025 static void TestTimeSet()
1027 puts("\n*** wxDateTime construction test ***");
1029 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1031 const Date
& d1
= testDates
[n
];
1032 wxDateTime dt
= d1
.DT();
1035 d2
.Init(dt
.GetTm());
1037 wxString s1
= d1
.Format(),
1040 printf("Date: %s == %s (%s)\n",
1041 s1
.c_str(), s2
.c_str(),
1042 s1
== s2
? "ok" : "ERROR");
1046 // test time zones stuff
1047 static void TestTimeZones()
1049 puts("\n*** wxDateTime timezone test ***");
1051 wxDateTime now
= wxDateTime::Now();
1053 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
1054 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
1055 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
1056 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
1057 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
1058 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
1060 wxDateTime::Tm tm
= now
.GetTm();
1061 if ( wxDateTime(tm
) != now
)
1063 printf("ERROR: got %s instead of %s\n",
1064 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
1068 // test some minimal support for the dates outside the standard range
1069 static void TestTimeRange()
1071 puts("\n*** wxDateTime out-of-standard-range dates test ***");
1073 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
1075 printf("Unix epoch:\t%s\n",
1076 wxDateTime(2440587.5).Format(fmt
).c_str());
1077 printf("Feb 29, 0: \t%s\n",
1078 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
1079 printf("JDN 0: \t%s\n",
1080 wxDateTime(0.0).Format(fmt
).c_str());
1081 printf("Jan 1, 1AD:\t%s\n",
1082 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
1083 printf("May 29, 2099:\t%s\n",
1084 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
1087 static void TestTimeTicks()
1089 puts("\n*** wxDateTime ticks test ***");
1091 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1093 const Date
& d
= testDates
[n
];
1094 if ( d
.ticks
== -1 )
1097 wxDateTime dt
= d
.DT();
1098 long ticks
= (dt
.GetValue() / 1000).ToLong();
1099 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1100 if ( ticks
== d
.ticks
)
1106 printf(" (ERROR: should be %ld, delta = %ld)\n",
1107 d
.ticks
, ticks
- d
.ticks
);
1110 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
1111 ticks
= (dt
.GetValue() / 1000).ToLong();
1112 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1113 if ( ticks
== d
.gmticks
)
1119 printf(" (ERROR: should be %ld, delta = %ld)\n",
1120 d
.gmticks
, ticks
- d
.gmticks
);
1127 // test conversions to JDN &c
1128 static void TestTimeJDN()
1130 puts("\n*** wxDateTime to JDN test ***");
1132 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1134 const Date
& d
= testDates
[n
];
1135 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
1136 double jdn
= dt
.GetJulianDayNumber();
1138 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
1145 printf(" (ERROR: should be %f, delta = %f)\n",
1146 d
.jdn
, jdn
- d
.jdn
);
1151 // test week days computation
1152 static void TestTimeWDays()
1154 puts("\n*** wxDateTime weekday test ***");
1156 // test GetWeekDay()
1158 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1160 const Date
& d
= testDates
[n
];
1161 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
1163 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
1166 wxDateTime::GetWeekDayName(wday
).c_str());
1167 if ( wday
== d
.wday
)
1173 printf(" (ERROR: should be %s)\n",
1174 wxDateTime::GetWeekDayName(d
.wday
).c_str());
1180 // test SetToWeekDay()
1181 struct WeekDateTestData
1183 Date date
; // the real date (precomputed)
1184 int nWeek
; // its week index in the month
1185 wxDateTime::WeekDay wday
; // the weekday
1186 wxDateTime::Month month
; // the month
1187 int year
; // and the year
1189 wxString
Format() const
1192 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
1194 case 1: which
= "first"; break;
1195 case 2: which
= "second"; break;
1196 case 3: which
= "third"; break;
1197 case 4: which
= "fourth"; break;
1198 case 5: which
= "fifth"; break;
1200 case -1: which
= "last"; break;
1205 which
+= " from end";
1208 s
.Printf("The %s %s of %s in %d",
1210 wxDateTime::GetWeekDayName(wday
).c_str(),
1211 wxDateTime::GetMonthName(month
).c_str(),
1218 // the array data was generated by the following python program
1220 from DateTime import *
1221 from whrandom import *
1222 from string import *
1224 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
1225 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
1227 week = DateTimeDelta(7)
1230 year = randint(1900, 2100)
1231 month = randint(1, 12)
1232 day = randint(1, 28)
1233 dt = DateTime(year, month, day)
1234 wday = dt.day_of_week
1236 countFromEnd = choice([-1, 1])
1239 while dt.month is month:
1240 dt = dt - countFromEnd * week
1241 weekNum = weekNum + countFromEnd
1243 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
1245 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
1246 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
1249 static const WeekDateTestData weekDatesTestData
[] =
1251 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
1252 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
1253 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
1254 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
1255 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
1256 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
1257 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
1258 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
1259 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
1260 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
1261 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
1262 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
1263 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
1264 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
1265 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
1266 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
1267 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
1268 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
1269 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
1270 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
1273 static const char *fmt
= "%d-%b-%Y";
1276 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
1278 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
1280 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
1282 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
1284 const Date
& d
= wd
.date
;
1285 if ( d
.SameDay(dt
.GetTm()) )
1291 dt
.Set(d
.day
, d
.month
, d
.year
);
1293 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
1298 // test the computation of (ISO) week numbers
1299 static void TestTimeWNumber()
1301 puts("\n*** wxDateTime week number test ***");
1303 struct WeekNumberTestData
1305 Date date
; // the date
1306 wxDateTime::wxDateTime_t week
; // the week number in the year
1307 wxDateTime::wxDateTime_t wmon
; // the week number in the month
1308 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
1309 wxDateTime::wxDateTime_t dnum
; // day number in the year
1312 // data generated with the following python script:
1314 from DateTime import *
1315 from whrandom import *
1316 from string import *
1318 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
1319 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
1321 def GetMonthWeek(dt):
1322 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
1323 if weekNumMonth < 0:
1324 weekNumMonth = weekNumMonth + 53
1327 def GetLastSundayBefore(dt):
1328 if dt.iso_week[2] == 7:
1331 return dt - DateTimeDelta(dt.iso_week[2])
1334 year = randint(1900, 2100)
1335 month = randint(1, 12)
1336 day = randint(1, 28)
1337 dt = DateTime(year, month, day)
1338 dayNum = dt.day_of_year
1339 weekNum = dt.iso_week[1]
1340 weekNumMonth = GetMonthWeek(dt)
1343 dtSunday = GetLastSundayBefore(dt)
1345 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
1346 weekNumMonth2 = weekNumMonth2 + 1
1347 dtSunday = dtSunday - DateTimeDelta(7)
1349 data = { 'day': rjust(`day`, 2), \
1350 'month': monthNames[month - 1], \
1352 'weekNum': rjust(`weekNum`, 2), \
1353 'weekNumMonth': weekNumMonth, \
1354 'weekNumMonth2': weekNumMonth2, \
1355 'dayNum': rjust(`dayNum`, 3) }
1357 print " { { %(day)s, "\
1358 "wxDateTime::%(month)s, "\
1361 "%(weekNumMonth)s, "\
1362 "%(weekNumMonth2)s, "\
1363 "%(dayNum)s }," % data
1366 static const WeekNumberTestData weekNumberTestDates
[] =
1368 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
1369 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
1370 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
1371 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
1372 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
1373 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
1374 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
1375 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
1376 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
1377 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
1378 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
1379 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
1380 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
1381 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
1382 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
1383 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
1384 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
1385 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
1386 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
1387 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
1390 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
1392 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
1393 const Date
& d
= wn
.date
;
1395 wxDateTime dt
= d
.DT();
1397 wxDateTime::wxDateTime_t
1398 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
1399 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1400 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1401 dnum
= dt
.GetDayOfYear();
1403 printf("%s: the day number is %d",
1404 d
.FormatDate().c_str(), dnum
);
1405 if ( dnum
== wn
.dnum
)
1411 printf(" (ERROR: should be %d)", wn
.dnum
);
1414 printf(", week in month is %d", wmon
);
1415 if ( wmon
== wn
.wmon
)
1421 printf(" (ERROR: should be %d)", wn
.wmon
);
1424 printf(" or %d", wmon2
);
1425 if ( wmon2
== wn
.wmon2
)
1431 printf(" (ERROR: should be %d)", wn
.wmon2
);
1434 printf(", week in year is %d", week
);
1435 if ( week
== wn
.week
)
1441 printf(" (ERROR: should be %d)\n", wn
.week
);
1446 // test DST calculations
1447 static void TestTimeDST()
1449 puts("\n*** wxDateTime DST test ***");
1451 printf("DST is%s in effect now.\n\n",
1452 wxDateTime::Now().IsDST() ? "" : " not");
1454 // taken from http://www.energy.ca.gov/daylightsaving.html
1455 static const Date datesDST
[2][2004 - 1900 + 1] =
1458 { 1, wxDateTime::Apr
, 1990 },
1459 { 7, wxDateTime::Apr
, 1991 },
1460 { 5, wxDateTime::Apr
, 1992 },
1461 { 4, wxDateTime::Apr
, 1993 },
1462 { 3, wxDateTime::Apr
, 1994 },
1463 { 2, wxDateTime::Apr
, 1995 },
1464 { 7, wxDateTime::Apr
, 1996 },
1465 { 6, wxDateTime::Apr
, 1997 },
1466 { 5, wxDateTime::Apr
, 1998 },
1467 { 4, wxDateTime::Apr
, 1999 },
1468 { 2, wxDateTime::Apr
, 2000 },
1469 { 1, wxDateTime::Apr
, 2001 },
1470 { 7, wxDateTime::Apr
, 2002 },
1471 { 6, wxDateTime::Apr
, 2003 },
1472 { 4, wxDateTime::Apr
, 2004 },
1475 { 28, wxDateTime::Oct
, 1990 },
1476 { 27, wxDateTime::Oct
, 1991 },
1477 { 25, wxDateTime::Oct
, 1992 },
1478 { 31, wxDateTime::Oct
, 1993 },
1479 { 30, wxDateTime::Oct
, 1994 },
1480 { 29, wxDateTime::Oct
, 1995 },
1481 { 27, wxDateTime::Oct
, 1996 },
1482 { 26, wxDateTime::Oct
, 1997 },
1483 { 25, wxDateTime::Oct
, 1998 },
1484 { 31, wxDateTime::Oct
, 1999 },
1485 { 29, wxDateTime::Oct
, 2000 },
1486 { 28, wxDateTime::Oct
, 2001 },
1487 { 27, wxDateTime::Oct
, 2002 },
1488 { 26, wxDateTime::Oct
, 2003 },
1489 { 31, wxDateTime::Oct
, 2004 },
1494 for ( year
= 1990; year
< 2005; year
++ )
1496 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
1497 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
1499 printf("DST period in the US for year %d: from %s to %s",
1500 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
1502 size_t n
= year
- 1990;
1503 const Date
& dBegin
= datesDST
[0][n
];
1504 const Date
& dEnd
= datesDST
[1][n
];
1506 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
1512 printf(" (ERROR: should be %s %d to %s %d)\n",
1513 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
1514 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
1520 for ( year
= 1990; year
< 2005; year
++ )
1522 printf("DST period in Europe for year %d: from %s to %s\n",
1524 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
1525 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
1529 // test wxDateTime -> text conversion
1530 static void TestTimeFormat()
1532 puts("\n*** wxDateTime formatting test ***");
1534 // some information may be lost during conversion, so store what kind
1535 // of info should we recover after a round trip
1538 CompareNone
, // don't try comparing
1539 CompareBoth
, // dates and times should be identical
1540 CompareDate
, // dates only
1541 CompareTime
// time only
1546 CompareKind compareKind
;
1548 } formatTestFormats
[] =
1550 { CompareBoth
, "---> %c" },
1551 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
1552 { CompareBoth
, "Date is %x, time is %X" },
1553 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
1554 { CompareNone
, "The day of year: %j, the week of year: %W" },
1557 static const Date formatTestDates
[] =
1559 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
1560 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
1562 // this test can't work for other centuries because it uses two digit
1563 // years in formats, so don't even try it
1564 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
1565 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
1566 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
1570 // an extra test (as it doesn't depend on date, don't do it in the loop)
1571 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
1573 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
1577 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
1578 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
1580 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
1581 printf("%s", s
.c_str());
1583 // what can we recover?
1584 int kind
= formatTestFormats
[n
].compareKind
;
1588 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
1591 // converion failed - should it have?
1592 if ( kind
== CompareNone
)
1595 puts(" (ERROR: conversion back failed)");
1599 // should have parsed the entire string
1600 puts(" (ERROR: conversion back stopped too soon)");
1604 bool equal
= FALSE
; // suppress compilaer warning
1612 equal
= dt
.IsSameDate(dt2
);
1616 equal
= dt
.IsSameTime(dt2
);
1622 printf(" (ERROR: got back '%s' instead of '%s')\n",
1623 dt2
.Format().c_str(), dt
.Format().c_str());
1634 // test text -> wxDateTime conversion
1635 static void TestTimeParse()
1637 puts("\n*** wxDateTime parse test ***");
1639 struct ParseTestData
1646 static const ParseTestData parseTestDates
[] =
1648 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
1649 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
1652 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
1654 const char *format
= parseTestDates
[n
].format
;
1656 printf("%s => ", format
);
1659 if ( dt
.ParseRfc822Date(format
) )
1661 printf("%s ", dt
.Format().c_str());
1663 if ( parseTestDates
[n
].good
)
1665 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
1672 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
1677 puts("(ERROR: bad format)");
1682 printf("bad format (%s)\n",
1683 parseTestDates
[n
].good
? "ERROR" : "ok");
1688 static void TestInteractive()
1690 puts("\n*** interactive wxDateTime tests ***");
1696 printf("Enter a date: ");
1697 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
1701 if ( !dt
.ParseDate(buf
) )
1703 puts("failed to parse the date");
1708 printf("%s: day %u, week of month %u/%u, week of year %u\n",
1709 dt
.FormatISODate().c_str(),
1711 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1712 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1713 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
1716 puts("\n*** done ***");
1719 static void TestTimeArithmetics()
1721 puts("\n*** testing arithmetic operations on wxDateTime ***");
1727 } testArithmData
[] =
1729 { wxDateSpan::Day(), "day" },
1730 { wxDateSpan::Week(), "week" },
1731 { wxDateSpan::Month(), "month" },
1732 { wxDateSpan::Year(), "year" },
1733 { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
1736 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
1738 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
1740 wxDateSpan span
= testArithmData
[n
].span
;
1744 const char *name
= testArithmData
[n
].name
;
1745 printf("%s + %s = %s, %s - %s = %s\n",
1746 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
1747 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
1749 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
1750 if ( dt1
- span
== dt
)
1756 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1759 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
1760 if ( dt2
+ span
== dt
)
1766 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
1769 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
1770 if ( dt2
+ 2*span
== dt1
)
1776 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
1783 static void TestTimeHolidays()
1785 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
1787 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
1788 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1789 dtEnd
= dtStart
.GetLastMonthDay();
1791 wxDateTimeArray hol
;
1792 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1794 const wxChar
*format
= "%d-%b-%Y (%a)";
1796 printf("All holidays between %s and %s:\n",
1797 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
1799 size_t count
= hol
.GetCount();
1800 for ( size_t n
= 0; n
< count
; n
++ )
1802 printf("\t%s\n", hol
[n
].Format(format
).c_str());
1810 // test compatibility with the old wxDate/wxTime classes
1811 static void TestTimeCompatibility()
1813 puts("\n*** wxDateTime compatibility test ***");
1815 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1816 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1818 double jdnNow
= wxDateTime::Now().GetJDN();
1819 long jdnMidnight
= (long)(jdnNow
- 0.5);
1820 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
1822 jdnMidnight
= wxDate().Set().GetJulianDate();
1823 printf("wxDateTime for today: %s\n",
1824 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
1826 int flags
= wxEUROPEAN
;//wxFULL;
1829 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
1830 for ( int n
= 0; n
< 7; n
++ )
1832 printf("Previous %s is %s\n",
1833 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
1834 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
1840 #endif // TEST_DATETIME
1842 // ----------------------------------------------------------------------------
1844 // ----------------------------------------------------------------------------
1848 #include <wx/thread.h>
1850 static size_t gs_counter
= (size_t)-1;
1851 static wxCriticalSection gs_critsect
;
1852 static wxCondition gs_cond
;
1854 class MyJoinableThread
: public wxThread
1857 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
1858 { m_n
= n
; Create(); }
1860 // thread execution starts here
1861 virtual ExitCode
Entry();
1867 wxThread::ExitCode
MyJoinableThread::Entry()
1869 unsigned long res
= 1;
1870 for ( size_t n
= 1; n
< m_n
; n
++ )
1874 // it's a loooong calculation :-)
1878 return (ExitCode
)res
;
1881 class MyDetachedThread
: public wxThread
1884 MyDetachedThread(size_t n
, char ch
)
1888 m_cancelled
= FALSE
;
1893 // thread execution starts here
1894 virtual ExitCode
Entry();
1897 virtual void OnExit();
1900 size_t m_n
; // number of characters to write
1901 char m_ch
; // character to write
1903 bool m_cancelled
; // FALSE if we exit normally
1906 wxThread::ExitCode
MyDetachedThread::Entry()
1909 wxCriticalSectionLocker
lock(gs_critsect
);
1910 if ( gs_counter
== (size_t)-1 )
1916 for ( size_t n
= 0; n
< m_n
; n
++ )
1918 if ( TestDestroy() )
1928 wxThread::Sleep(100);
1934 void MyDetachedThread::OnExit()
1936 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1938 wxCriticalSectionLocker
lock(gs_critsect
);
1939 if ( !--gs_counter
&& !m_cancelled
)
1943 void TestDetachedThreads()
1945 puts("\n*** Testing detached threads ***");
1947 static const size_t nThreads
= 3;
1948 MyDetachedThread
*threads
[nThreads
];
1950 for ( n
= 0; n
< nThreads
; n
++ )
1952 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
1955 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
1956 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
1958 for ( n
= 0; n
< nThreads
; n
++ )
1963 // wait until all threads terminate
1969 void TestJoinableThreads()
1971 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1973 // calc 10! in the background
1974 MyJoinableThread
thread(10);
1977 printf("\nThread terminated with exit code %lu.\n",
1978 (unsigned long)thread
.Wait());
1981 void TestThreadSuspend()
1983 puts("\n*** Testing thread suspend/resume functions ***");
1985 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
1989 // this is for this demo only, in a real life program we'd use another
1990 // condition variable which would be signaled from wxThread::Entry() to
1991 // tell us that the thread really started running - but here just wait a
1992 // bit and hope that it will be enough (the problem is, of course, that
1993 // the thread might still not run when we call Pause() which will result
1995 wxThread::Sleep(300);
1997 for ( size_t n
= 0; n
< 3; n
++ )
2001 puts("\nThread suspended");
2004 // don't sleep but resume immediately the first time
2005 wxThread::Sleep(300);
2007 puts("Going to resume the thread");
2012 puts("Waiting until it terminates now");
2014 // wait until the thread terminates
2020 void TestThreadDelete()
2022 // As above, using Sleep() is only for testing here - we must use some
2023 // synchronisation object instead to ensure that the thread is still
2024 // running when we delete it - deleting a detached thread which already
2025 // terminated will lead to a crash!
2027 puts("\n*** Testing thread delete function ***");
2029 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
2033 puts("\nDeleted a thread which didn't start to run yet.");
2035 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
2039 wxThread::Sleep(300);
2043 puts("\nDeleted a running thread.");
2045 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
2049 wxThread::Sleep(300);
2055 puts("\nDeleted a sleeping thread.");
2057 MyJoinableThread
thread3(20);
2062 puts("\nDeleted a joinable thread.");
2064 MyJoinableThread
thread4(2);
2067 wxThread::Sleep(300);
2071 puts("\nDeleted a joinable thread which already terminated.");
2076 #endif // TEST_THREADS
2078 // ----------------------------------------------------------------------------
2080 // ----------------------------------------------------------------------------
2084 void PrintArray(const char* name
, const wxArrayString
& array
)
2086 printf("Dump of the array '%s'\n", name
);
2088 size_t nCount
= array
.GetCount();
2089 for ( size_t n
= 0; n
< nCount
; n
++ )
2091 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
2095 #endif // TEST_ARRAYS
2097 // ----------------------------------------------------------------------------
2099 // ----------------------------------------------------------------------------
2103 #include "wx/timer.h"
2104 #include "wx/tokenzr.h"
2106 static void TestStringConstruction()
2108 puts("*** Testing wxString constructores ***");
2110 #define TEST_CTOR(args, res) \
2113 printf("wxString%s = %s ", #args, s.c_str()); \
2120 printf("(ERROR: should be %s)\n", res); \
2124 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
2125 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
2126 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
2127 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
2129 static const wxChar
*s
= _T("?really!");
2130 const wxChar
*start
= wxStrchr(s
, _T('r'));
2131 const wxChar
*end
= wxStrchr(s
, _T('!'));
2132 TEST_CTOR((start
, end
), _T("really"));
2137 static void TestString()
2147 for (int i
= 0; i
< 1000000; ++i
)
2151 c
= "! How'ya doin'?";
2154 c
= "Hello world! What's up?";
2159 printf ("TestString elapsed time: %ld\n", sw
.Time());
2162 static void TestPChar()
2170 for (int i
= 0; i
< 1000000; ++i
)
2172 strcpy (a
, "Hello");
2173 strcpy (b
, " world");
2174 strcpy (c
, "! How'ya doin'?");
2177 strcpy (c
, "Hello world! What's up?");
2178 if (strcmp (c
, a
) == 0)
2182 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
2185 static void TestStringSub()
2187 wxString
s("Hello, world!");
2189 puts("*** Testing wxString substring extraction ***");
2191 printf("String = '%s'\n", s
.c_str());
2192 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
2193 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
2194 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
2195 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
2196 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
2197 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
2202 static void TestStringFormat()
2204 puts("*** Testing wxString formatting ***");
2207 s
.Printf("%03d", 18);
2209 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
2210 printf("Number 18: %s\n", s
.c_str());
2215 // returns "not found" for npos, value for all others
2216 static wxString
PosToString(size_t res
)
2218 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
2219 : wxString::Format(_T("%u"), res
);
2223 static void TestStringFind()
2225 puts("*** Testing wxString find() functions ***");
2227 static const wxChar
*strToFind
= _T("ell");
2228 static const struct StringFindTest
2232 result
; // of searching "ell" in str
2235 { _T("Well, hello world"), 0, 1 },
2236 { _T("Well, hello world"), 6, 7 },
2237 { _T("Well, hello world"), 9, wxString::npos
},
2240 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
2242 const StringFindTest
& ft
= findTestData
[n
];
2243 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
2245 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
2246 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
2248 size_t resTrue
= ft
.result
;
2249 if ( res
== resTrue
)
2255 printf(_T("(ERROR: should be %s)\n"),
2256 PosToString(resTrue
).c_str());
2263 static void TestStringTokenizer()
2265 puts("*** Testing wxStringTokenizer ***");
2267 static const wxChar
*modeNames
[] =
2271 _T("return all empty"),
2276 static const struct StringTokenizerTest
2278 const wxChar
*str
; // string to tokenize
2279 const wxChar
*delims
; // delimiters to use
2280 size_t count
; // count of token
2281 wxStringTokenizerMode mode
; // how should we tokenize it
2282 } tokenizerTestData
[] =
2284 { _T(""), _T(" "), 0 },
2285 { _T("Hello, world"), _T(" "), 2 },
2286 { _T("Hello, world "), _T(" "), 2 },
2287 { _T("Hello, world"), _T(","), 2 },
2288 { _T("Hello, world!"), _T(",!"), 2 },
2289 { _T("Hello,, world!"), _T(",!"), 3 },
2290 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
2291 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
2292 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
2293 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
2294 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
2295 { _T("01/02/99"), _T("/-"), 3 },
2296 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
2299 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
2301 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
2302 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
2304 size_t count
= tkz
.CountTokens();
2305 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
2306 MakePrintable(tt
.str
).c_str(),
2308 MakePrintable(tt
.delims
).c_str(),
2309 modeNames
[tkz
.GetMode()]);
2310 if ( count
== tt
.count
)
2316 printf(_T("(ERROR: should be %u)\n"), tt
.count
);
2321 // if we emulate strtok(), check that we do it correctly
2322 wxChar
*buf
, *s
, *last
;
2324 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
2326 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
2327 wxStrcpy(buf
, tt
.str
);
2329 s
= wxStrtok(buf
, tt
.delims
, &last
);
2336 // now show the tokens themselves
2338 while ( tkz
.HasMoreTokens() )
2340 wxString token
= tkz
.GetNextToken();
2342 printf(_T("\ttoken %u: '%s'"),
2344 MakePrintable(token
).c_str());
2354 printf(" (ERROR: should be %s)\n", s
);
2357 s
= wxStrtok(NULL
, tt
.delims
, &last
);
2361 // nothing to compare with
2366 if ( count2
!= count
)
2368 puts(_T("\tERROR: token count mismatch"));
2377 #endif // TEST_STRINGS
2379 // ----------------------------------------------------------------------------
2381 // ----------------------------------------------------------------------------
2383 int main(int argc
, char **argv
)
2385 if ( !wxInitialize() )
2387 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
2391 puts("Sleeping for 3 seconds... z-z-z-z-z...");
2393 #endif // TEST_USLEEP
2396 static const wxCmdLineEntryDesc cmdLineDesc
[] =
2398 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
2399 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
2401 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
2402 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
2403 { wxCMD_LINE_OPTION
, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
2404 { wxCMD_LINE_OPTION
, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
2406 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
2407 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
2412 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
2414 switch ( parser
.Parse() )
2417 wxLogMessage("Help was given, terminating.");
2421 ShowCmdLine(parser
);
2425 wxLogMessage("Syntax error detected, aborting.");
2428 #endif // TEST_CMDLINE
2438 TestStringConstruction();
2442 TestStringTokenizer();
2444 #endif // TEST_STRINGS
2455 puts("*** Initially:");
2457 PrintArray("a1", a1
);
2459 wxArrayString
a2(a1
);
2460 PrintArray("a2", a2
);
2462 wxSortedArrayString
a3(a1
);
2463 PrintArray("a3", a3
);
2465 puts("*** After deleting a string from a1");
2468 PrintArray("a1", a1
);
2469 PrintArray("a2", a2
);
2470 PrintArray("a3", a3
);
2472 puts("*** After reassigning a1 to a2 and a3");
2474 PrintArray("a2", a2
);
2475 PrintArray("a3", a3
);
2476 #endif // TEST_ARRAYS
2484 #endif // TEST_EXECUTE
2486 #ifdef TEST_FILECONF
2488 #endif // TEST_FILECONF
2492 for ( size_t n
= 0; n
< 8000; n
++ )
2494 s
<< (char)('A' + (n
% 26));
2498 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
2500 // this one shouldn't be truncated
2503 // but this one will because log functions use fixed size buffer
2504 // (note that it doesn't need '\n' at the end neither - will be added
2506 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
2510 int nCPUs
= wxThread::GetCPUCount();
2511 printf("This system has %d CPUs\n", nCPUs
);
2513 wxThread::SetConcurrency(nCPUs
);
2515 if ( argc
> 1 && argv
[1][0] == 't' )
2516 wxLog::AddTraceMask("thread");
2519 TestDetachedThreads();
2521 TestJoinableThreads();
2523 TestThreadSuspend();
2527 #endif // TEST_THREADS
2529 #ifdef TEST_LONGLONG
2530 // seed pseudo random generator
2531 srand((unsigned)time(NULL
));
2537 TestMultiplication();
2542 TestLongLongConversion();
2543 TestBitOperations();
2545 #endif // TEST_LONGLONG
2562 #endif // TEST_SOCKETS
2566 #endif // TEST_TIMER
2568 #ifdef TEST_DATETIME
2582 TestTimeArithmetics();
2587 #endif // TEST_DATETIME