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_DLLLOADER
43 //#define TEST_EXECUTE
45 //#define TEST_FILECONF
49 //#define TEST_LONGLONG
51 //#define TEST_INFO_FUNCTIONS
52 //#define TEST_SOCKETS
54 //#define TEST_STRINGS
55 //#define TEST_THREADS
57 //#define TEST_VCARD -- don't enable this (VZ)
62 // ----------------------------------------------------------------------------
63 // test class for container objects
64 // ----------------------------------------------------------------------------
66 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
68 class Bar
// Foo is already taken in the hash test
71 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
74 static size_t GetNumber() { return ms_bars
; }
76 const char *GetName() const { return m_name
; }
81 static size_t ms_bars
;
84 size_t Bar::ms_bars
= 0;
86 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
98 // replace TABs with \t and CRs with \n
99 static wxString
MakePrintable(const wxChar
*s
)
102 (void)str
.Replace(_T("\t"), _T("\\t"));
103 (void)str
.Replace(_T("\n"), _T("\\n"));
104 (void)str
.Replace(_T("\r"), _T("\\r"));
109 #endif // MakePrintable() is used
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
117 #include <wx/cmdline.h>
118 #include <wx/datetime.h>
120 static void ShowCmdLine(const wxCmdLineParser
& parser
)
122 wxString s
= "Input files: ";
124 size_t count
= parser
.GetParamCount();
125 for ( size_t param
= 0; param
< count
; param
++ )
127 s
<< parser
.GetParam(param
) << ' ';
131 << "Verbose:\t" << (parser
.Found("v") ? "yes" : "no") << '\n'
132 << "Quiet:\t" << (parser
.Found("q") ? "yes" : "no") << '\n';
137 if ( parser
.Found("o", &strVal
) )
138 s
<< "Output file:\t" << strVal
<< '\n';
139 if ( parser
.Found("i", &strVal
) )
140 s
<< "Input dir:\t" << strVal
<< '\n';
141 if ( parser
.Found("s", &lVal
) )
142 s
<< "Size:\t" << lVal
<< '\n';
143 if ( parser
.Found("d", &dt
) )
144 s
<< "Date:\t" << dt
.FormatISODate() << '\n';
145 if ( parser
.Found("project_name", &strVal
) )
146 s
<< "Project:\t" << strVal
<< '\n';
151 #endif // TEST_CMDLINE
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
161 static void TestDirEnumHelper(wxDir
& dir
,
162 int flags
= wxDIR_DEFAULT
,
163 const wxString
& filespec
= wxEmptyString
)
167 if ( !dir
.IsOpened() )
170 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
173 printf("\t%s\n", filename
.c_str());
175 cont
= dir
.GetNext(&filename
);
181 static void TestDirEnum()
183 wxDir
dir(wxGetCwd());
185 puts("Enumerating everything in current directory:");
186 TestDirEnumHelper(dir
);
188 puts("Enumerating really everything in current directory:");
189 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
191 puts("Enumerating object files in current directory:");
192 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
194 puts("Enumerating directories in current directory:");
195 TestDirEnumHelper(dir
, wxDIR_DIRS
);
197 puts("Enumerating files in current directory:");
198 TestDirEnumHelper(dir
, wxDIR_FILES
);
200 puts("Enumerating files including hidden in current directory:");
201 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
205 #elif defined(__WXMSW__)
208 #error "don't know where the root directory is"
211 puts("Enumerating everything in root directory:");
212 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
214 puts("Enumerating directories in root directory:");
215 TestDirEnumHelper(dir
, wxDIR_DIRS
);
217 puts("Enumerating files in root directory:");
218 TestDirEnumHelper(dir
, wxDIR_FILES
);
220 puts("Enumerating files including hidden in root directory:");
221 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
223 puts("Enumerating files in non existing directory:");
224 wxDir
dirNo("nosuchdir");
225 TestDirEnumHelper(dirNo
);
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 #ifdef TEST_DLLLOADER
236 #include <wx/dynlib.h>
238 static void TestDllLoad()
240 #if defined(__WXMSW__)
241 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
242 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
243 #elif defined(__UNIX__)
244 // weird: using just libc.so does *not* work!
245 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
246 static const wxChar
*FUNC_NAME
= _T("strlen");
248 #error "don't know how to test wxDllLoader on this platform"
251 puts("*** testing wxDllLoader ***\n");
253 wxDllType dllHandle
= wxDllLoader::LoadLibrary(LIB_NAME
);
256 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
260 typedef int (*strlenType
)(char *);
261 strlenType pfnStrlen
= (strlenType
)wxDllLoader::GetSymbol(dllHandle
, FUNC_NAME
);
264 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
265 FUNC_NAME
, LIB_NAME
);
269 if ( pfnStrlen("foo") != 3 )
271 wxPrintf(_T("ERROR: loaded function is not strlen()!\n"));
279 wxDllLoader::UnloadLibrary(dllHandle
);
283 #endif // TEST_DLLLOADER
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
291 #include <wx/utils.h>
293 static void TestExecute()
295 puts("*** testing wxExecute ***");
298 #define COMMAND "echo hi"
299 #define SHELL_COMMAND "echo hi from shell"
300 #define REDIRECT_COMMAND "date"
301 #elif defined(__WXMSW__)
302 #define COMMAND "command.com -c 'echo hi'"
303 #define SHELL_COMMAND "echo hi"
304 #define REDIRECT_COMMAND COMMAND
306 #error "no command to exec"
309 printf("Testing wxShell: ");
311 if ( wxShell(SHELL_COMMAND
) )
316 printf("Testing wxExecute: ");
318 if ( wxExecute(COMMAND
, TRUE
/* sync */) == 0 )
323 #if 0 // no, it doesn't work (yet?)
324 printf("Testing async wxExecute: ");
326 if ( wxExecute(COMMAND
) != 0 )
327 puts("Ok (command launched).");
332 printf("Testing wxExecute with redirection:\n");
333 wxArrayString output
;
334 if ( wxExecute(REDIRECT_COMMAND
, output
) != 0 )
340 size_t count
= output
.GetCount();
341 for ( size_t n
= 0; n
< count
; n
++ )
343 printf("\t%s\n", output
[n
].c_str());
350 #endif // TEST_EXECUTE
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
359 #include <wx/textfile.h>
361 static void TestFileRead()
363 puts("*** wxFile read test ***");
365 wxFile
file(_T("testdata.fc"));
366 if ( file
.IsOpened() )
368 printf("File length: %lu\n", file
.Length());
370 puts("File dump:\n----------");
372 static const off_t len
= 1024;
376 off_t nRead
= file
.Read(buf
, len
);
377 if ( nRead
== wxInvalidOffset
)
379 printf("Failed to read the file.");
383 fwrite(buf
, nRead
, 1, stdout
);
393 printf("ERROR: can't open test file.\n");
399 static void TestTextFileRead()
401 puts("*** wxTextFile read test ***");
403 wxTextFile
file(_T("testdata.fc"));
406 printf("Number of lines: %u\n", file
.GetLineCount());
407 printf("Last line: '%s'\n", file
.GetLastLine().c_str());
411 puts("\nDumping the entire file:");
412 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
414 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
416 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
418 puts("\nAnd now backwards:");
419 for ( s
= file
.GetLastLine();
420 file
.GetCurrentLine() != 0;
421 s
= file
.GetPrevLine() )
423 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
425 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
429 printf("ERROR: can't open '%s'\n", file
.GetName());
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
443 #include <wx/confbase.h>
444 #include <wx/fileconf.h>
446 static const struct FileConfTestData
448 const wxChar
*name
; // value name
449 const wxChar
*value
; // the value from the file
452 { _T("value1"), _T("one") },
453 { _T("value2"), _T("two") },
454 { _T("novalue"), _T("default") },
457 static void TestFileConfRead()
459 puts("*** testing wxFileConfig loading/reading ***");
461 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
462 _T("testdata.fc"), wxEmptyString
,
463 wxCONFIG_USE_RELATIVE_PATH
);
465 // test simple reading
466 puts("\nReading config file:");
467 wxString
defValue(_T("default")), value
;
468 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
470 const FileConfTestData
& data
= fcTestData
[n
];
471 value
= fileconf
.Read(data
.name
, defValue
);
472 printf("\t%s = %s ", data
.name
, value
.c_str());
473 if ( value
== data
.value
)
479 printf("(ERROR: should be %s)\n", data
.value
);
483 // test enumerating the entries
484 puts("\nEnumerating all root entries:");
487 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
490 printf("\t%s = %s\n",
492 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
494 cont
= fileconf
.GetNextEntry(name
, dummy
);
498 #endif // TEST_FILECONF
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
510 Foo(int n_
) { n
= n_
; count
++; }
518 size_t Foo::count
= 0;
520 WX_DECLARE_LIST(Foo
, wxListFoos
);
521 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
523 #include <wx/listimpl.cpp>
525 WX_DEFINE_LIST(wxListFoos
);
527 static void TestHash()
529 puts("*** Testing wxHashTable ***\n");
533 hash
.DeleteContents(TRUE
);
535 printf("Hash created: %u foos in hash, %u foos totally\n",
536 hash
.GetCount(), Foo::count
);
538 static const int hashTestData
[] =
540 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
544 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
546 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
549 printf("Hash filled: %u foos in hash, %u foos totally\n",
550 hash
.GetCount(), Foo::count
);
552 puts("Hash access test:");
553 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
555 printf("\tGetting element with key %d, value %d: ",
557 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
560 printf("ERROR, not found.\n");
564 printf("%d (%s)\n", foo
->n
,
565 (size_t)foo
->n
== n
? "ok" : "ERROR");
569 printf("\nTrying to get an element not in hash: ");
571 if ( hash
.Get(1234) || hash
.Get(1, 0) )
573 puts("ERROR: found!");
577 puts("ok (not found)");
581 printf("Hash destroyed: %u foos left\n", Foo::count
);
586 // ----------------------------------------------------------------------------
588 // ----------------------------------------------------------------------------
594 WX_DECLARE_LIST(Bar
, wxListBars
);
595 #include <wx/listimpl.cpp>
596 WX_DEFINE_LIST(wxListBars
);
598 static void TestListCtor()
600 puts("*** Testing wxList construction ***\n");
604 list1
.Append(new Bar(_T("first")));
605 list1
.Append(new Bar(_T("second")));
607 printf("After 1st list creation: %u objects in the list, %u objects total.\n",
608 list1
.GetCount(), Bar::GetNumber());
613 printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n",
614 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
616 list1
.DeleteContents(TRUE
);
619 printf("After list destruction: %u objects left.\n", Bar::GetNumber());
624 // ----------------------------------------------------------------------------
626 // ----------------------------------------------------------------------------
630 #include <wx/mimetype.h>
632 static wxMimeTypesManager g_mimeManager
;
634 static void TestMimeEnum()
636 wxArrayString mimetypes
;
638 size_t count
= g_mimeManager
.EnumAllFileTypes(mimetypes
);
640 printf("*** All %u known filetypes: ***\n", count
);
645 for ( size_t n
= 0; n
< count
; n
++ )
647 wxFileType
*filetype
= g_mimeManager
.GetFileTypeFromMimeType(mimetypes
[n
]);
650 printf("nothing known about the filetype '%s'!\n",
651 mimetypes
[n
].c_str());
655 filetype
->GetDescription(&desc
);
656 filetype
->GetExtensions(exts
);
658 filetype
->GetIcon(NULL
);
661 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
668 printf("\t%s: %s (%s)\n",
669 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
673 static void TestMimeOverride()
675 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
677 wxString mailcap
= _T("/tmp/mailcap"),
678 mimetypes
= _T("/tmp/mime.types");
680 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
682 g_mimeManager
.ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
683 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
685 g_mimeManager
.ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
688 static void TestMimeFilename()
690 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
692 static const wxChar
*filenames
[] =
699 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
701 const wxString fname
= filenames
[n
];
702 wxString ext
= fname
.AfterLast(_T('.'));
703 wxFileType
*ft
= g_mimeManager
.GetFileTypeFromExtension(ext
);
706 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
711 if ( !ft
->GetDescription(&desc
) )
712 desc
= _T("<no description>");
715 if ( !ft
->GetOpenCommand(&cmd
,
716 wxFileType::MessageParameters(fname
, _T(""))) )
717 cmd
= _T("<no command available>");
719 wxPrintf(_T("To open %s (%s) do '%s'.\n"),
720 fname
.c_str(), desc
.c_str(), cmd
.c_str());
729 // ----------------------------------------------------------------------------
730 // misc information functions
731 // ----------------------------------------------------------------------------
733 #ifdef TEST_INFO_FUNCTIONS
735 #include <wx/utils.h>
737 static void TestOsInfo()
739 puts("*** Testing OS info functions ***\n");
742 wxGetOsVersion(&major
, &minor
);
743 printf("Running under: %s, version %d.%d\n",
744 wxGetOsDescription().c_str(), major
, minor
);
746 printf("%ld free bytes of memory left.\n", wxGetFreeMemory());
748 printf("Host name is %s (%s).\n",
749 wxGetHostName().c_str(), wxGetFullHostName().c_str());
754 static void TestUserInfo()
756 puts("*** Testing user info functions ***\n");
758 printf("User id is:\t%s\n", wxGetUserId().c_str());
759 printf("User name is:\t%s\n", wxGetUserName().c_str());
760 printf("Home dir is:\t%s\n", wxGetHomeDir().c_str());
761 printf("Email address:\t%s\n", wxGetEmailAddress().c_str());
766 #endif // TEST_INFO_FUNCTIONS
768 // ----------------------------------------------------------------------------
770 // ----------------------------------------------------------------------------
774 #include <wx/longlong.h>
775 #include <wx/timer.h>
777 // make a 64 bit number from 4 16 bit ones
778 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
780 // get a random 64 bit number
781 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
783 #if wxUSE_LONGLONG_WX
784 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
785 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
786 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
787 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
788 #endif // wxUSE_LONGLONG_WX
790 static void TestSpeed()
792 static const long max
= 100000000;
799 for ( n
= 0; n
< max
; n
++ )
804 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
807 #if wxUSE_LONGLONG_NATIVE
812 for ( n
= 0; n
< max
; n
++ )
817 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
819 #endif // wxUSE_LONGLONG_NATIVE
825 for ( n
= 0; n
< max
; n
++ )
830 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
834 static void TestLongLongConversion()
836 puts("*** Testing wxLongLong conversions ***\n");
840 for ( size_t n
= 0; n
< 100000; n
++ )
844 #if wxUSE_LONGLONG_NATIVE
845 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
847 wxASSERT_MSG( a
== b
, "conversions failure" );
849 puts("Can't do it without native long long type, test skipped.");
852 #endif // wxUSE_LONGLONG_NATIVE
854 if ( !(nTested
% 1000) )
866 static void TestMultiplication()
868 puts("*** Testing wxLongLong multiplication ***\n");
872 for ( size_t n
= 0; n
< 100000; n
++ )
877 #if wxUSE_LONGLONG_NATIVE
878 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
879 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
881 wxASSERT_MSG( a
*b
== aa
*bb
, "multiplication failure" );
882 #else // !wxUSE_LONGLONG_NATIVE
883 puts("Can't do it without native long long type, test skipped.");
886 #endif // wxUSE_LONGLONG_NATIVE
888 if ( !(nTested
% 1000) )
900 static void TestDivision()
902 puts("*** Testing wxLongLong division ***\n");
906 for ( size_t n
= 0; n
< 100000; n
++ )
908 // get a random wxLongLong (shifting by 12 the MSB ensures that the
909 // multiplication will not overflow)
910 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
912 // get a random long (not wxLongLong for now) to divide it with
917 #if wxUSE_LONGLONG_NATIVE
918 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
920 wxLongLongNative p
= m
/ l
, s
= m
% l
;
921 wxASSERT_MSG( q
== p
&& r
== s
, "division failure" );
922 #else // !wxUSE_LONGLONG_NATIVE
924 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
925 #endif // wxUSE_LONGLONG_NATIVE
927 if ( !(nTested
% 1000) )
939 static void TestAddition()
941 puts("*** Testing wxLongLong addition ***\n");
945 for ( size_t n
= 0; n
< 100000; n
++ )
951 #if wxUSE_LONGLONG_NATIVE
952 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
953 wxLongLongNative(b
.GetHi(), b
.GetLo()),
954 "addition failure" );
955 #else // !wxUSE_LONGLONG_NATIVE
956 wxASSERT_MSG( c
- b
== a
, "addition failure" );
957 #endif // wxUSE_LONGLONG_NATIVE
959 if ( !(nTested
% 1000) )
971 static void TestBitOperations()
973 puts("*** Testing wxLongLong bit operation ***\n");
977 for ( size_t n
= 0; n
< 100000; n
++ )
981 #if wxUSE_LONGLONG_NATIVE
982 for ( size_t n
= 0; n
< 33; n
++ )
985 #else // !wxUSE_LONGLONG_NATIVE
986 puts("Can't do it without native long long type, test skipped.");
989 #endif // wxUSE_LONGLONG_NATIVE
991 if ( !(nTested
% 1000) )
1003 static void TestLongLongComparison()
1005 puts("*** Testing wxLongLong comparison ***\n");
1007 static const long testLongs
[] =
1018 static const long ls
[2] =
1024 wxLongLongWx lls
[2];
1028 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
1032 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
1034 res
= lls
[m
] > testLongs
[n
];
1035 printf("0x%lx > 0x%lx is %s (%s)\n",
1036 ls
[m
], testLongs
[n
], res
? "true" : "false",
1037 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
1039 res
= lls
[m
] < testLongs
[n
];
1040 printf("0x%lx < 0x%lx is %s (%s)\n",
1041 ls
[m
], testLongs
[n
], res
? "true" : "false",
1042 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
1044 res
= lls
[m
] == testLongs
[n
];
1045 printf("0x%lx == 0x%lx is %s (%s)\n",
1046 ls
[m
], testLongs
[n
], res
? "true" : "false",
1047 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
1055 #endif // TEST_LONGLONG
1057 // ----------------------------------------------------------------------------
1059 // ----------------------------------------------------------------------------
1063 #include <wx/socket.h>
1064 #include <wx/protocol/protocol.h>
1065 #include <wx/protocol/ftp.h>
1066 #include <wx/protocol/http.h>
1068 static void TestSocketServer()
1070 puts("*** Testing wxSocketServer ***\n");
1072 static const int PORT
= 3000;
1077 wxSocketServer
*server
= new wxSocketServer(addr
);
1078 if ( !server
->Ok() )
1080 puts("ERROR: failed to bind");
1087 printf("Server: waiting for connection on port %d...\n", PORT
);
1089 wxSocketBase
*socket
= server
->Accept();
1092 puts("ERROR: wxSocketServer::Accept() failed.");
1096 puts("Server: got a client.");
1098 server
->SetTimeout(60); // 1 min
1100 while ( socket
->IsConnected() )
1106 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
1108 // don't log error if the client just close the connection
1109 if ( socket
->IsConnected() )
1111 puts("ERROR: in wxSocket::Read.");
1131 printf("Server: got '%s'.\n", s
.c_str());
1132 if ( s
== _T("bye") )
1139 socket
->Write(s
.MakeUpper().c_str(), s
.length());
1140 socket
->Write("\r\n", 2);
1141 printf("Server: wrote '%s'.\n", s
.c_str());
1144 puts("Server: lost a client.");
1149 // same as "delete server" but is consistent with GUI programs
1153 static void TestSocketClient()
1155 puts("*** Testing wxSocketClient ***\n");
1157 static const char *hostname
= "www.wxwindows.org";
1160 addr
.Hostname(hostname
);
1163 printf("--- Attempting to connect to %s:80...\n", hostname
);
1165 wxSocketClient client
;
1166 if ( !client
.Connect(addr
) )
1168 printf("ERROR: failed to connect to %s\n", hostname
);
1172 printf("--- Connected to %s:%u...\n",
1173 addr
.Hostname().c_str(), addr
.Service());
1177 // could use simply "GET" here I suppose
1179 wxString::Format("GET http://%s/\r\n", hostname
);
1180 client
.Write(cmdGet
, cmdGet
.length());
1181 printf("--- Sent command '%s' to the server\n",
1182 MakePrintable(cmdGet
).c_str());
1183 client
.Read(buf
, WXSIZEOF(buf
));
1184 printf("--- Server replied:\n%s", buf
);
1188 static void TestProtocolFtp()
1190 puts("*** Testing wxFTP download ***\n");
1192 wxLog::AddTraceMask(_T("ftp"));
1194 static const char *hostname
= "ftp.wxwindows.org";
1196 printf("--- Attempting to connect to %s:21...\n", hostname
);
1199 if ( !ftp
.Connect(hostname
) )
1201 printf("ERROR: failed to connect to %s\n", hostname
);
1205 printf("--- Connected to %s, current directory is '%s'\n",
1206 hostname
, ftp
.Pwd().c_str());
1207 if ( !ftp
.ChDir(_T("pub")) )
1209 puts("ERROR: failed to cd to pub");
1212 wxArrayString files
;
1213 if ( !ftp
.GetList(files
) )
1215 puts("ERROR: failed to get list of files");
1219 printf("List of files under '%s':\n", ftp
.Pwd().c_str());
1220 size_t count
= files
.GetCount();
1221 for ( size_t n
= 0; n
< count
; n
++ )
1223 printf("\t%s\n", files
[n
].c_str());
1225 puts("End of the file list");
1228 if ( !ftp
.ChDir(_T("..")) )
1230 puts("ERROR: failed to cd to ..");
1233 static const char *filename
= "welcome.msg";
1234 wxInputStream
*in
= ftp
.GetInputStream(filename
);
1237 puts("ERROR: couldn't get input stream");
1241 size_t size
= in
->StreamSize();
1242 printf("Reading file %s (%u bytes)...", filename
, size
);
1244 char *data
= new char[size
];
1245 if ( !in
->Read(data
, size
) )
1247 puts("ERROR: read error");
1251 printf("\nContents of %s:\n%s\n", filename
, data
);
1260 static void TestProtocolFtpUpload()
1262 puts("*** Testing wxFTP uploading ***\n");
1264 wxLog::AddTraceMask(_T("ftp"));
1266 static const char *hostname
= "localhost";
1268 printf("--- Attempting to connect to %s:21...\n", hostname
);
1271 ftp
.SetUser("zeitlin");
1272 ftp
.SetPassword("insert your password here");
1273 if ( !ftp
.Connect(hostname
) )
1275 printf("ERROR: failed to connect to %s\n", hostname
);
1279 printf("--- Connected to %s, current directory is '%s'\n",
1280 hostname
, ftp
.Pwd().c_str());
1283 static const char *file1
= "test1";
1284 static const char *file2
= "test2";
1285 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
1288 printf("--- Uploading to %s ---\n", file1
);
1289 out
->Write("First hello", 11);
1293 out
= ftp
.GetOutputStream(file2
);
1296 printf("--- Uploading to %s ---\n", file1
);
1297 out
->Write("Second hello", 12);
1303 #endif // TEST_SOCKETS
1305 // ----------------------------------------------------------------------------
1307 // ----------------------------------------------------------------------------
1311 #include <wx/mstream.h>
1313 static void TestMemoryStream()
1315 puts("*** Testing wxMemoryInputStream ***");
1318 wxStrncpy(buf
, _T("Hello, stream!"), WXSIZEOF(buf
));
1320 wxMemoryInputStream
memInpStream(buf
, wxStrlen(buf
));
1321 printf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
1322 while ( !memInpStream
.Eof() )
1324 putchar(memInpStream
.GetC());
1327 puts("\n*** wxMemoryInputStream test done ***");
1330 #endif // TEST_STREAMS
1332 // ----------------------------------------------------------------------------
1334 // ----------------------------------------------------------------------------
1338 #include <wx/timer.h>
1339 #include <wx/utils.h>
1341 static void TestStopWatch()
1343 puts("*** Testing wxStopWatch ***\n");
1346 printf("Sleeping 3 seconds...");
1348 printf("\telapsed time: %ldms\n", sw
.Time());
1351 printf("Sleeping 2 more seconds...");
1353 printf("\telapsed time: %ldms\n", sw
.Time());
1356 printf("And 3 more seconds...");
1358 printf("\telapsed time: %ldms\n", sw
.Time());
1361 puts("\nChecking for 'backwards clock' bug...");
1362 for ( size_t n
= 0; n
< 70; n
++ )
1366 for ( size_t m
= 0; m
< 100000; m
++ )
1368 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
1370 puts("\ntime is negative - ERROR!");
1380 #endif // TEST_TIMER
1382 // ----------------------------------------------------------------------------
1384 // ----------------------------------------------------------------------------
1388 #include <wx/vcard.h>
1390 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
1393 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
1397 wxString(_T('\t'), level
).c_str(),
1398 vcObj
->GetName().c_str());
1401 switch ( vcObj
->GetType() )
1403 case wxVCardObject::String
:
1404 case wxVCardObject::UString
:
1407 vcObj
->GetValue(&val
);
1408 value
<< _T('"') << val
<< _T('"');
1412 case wxVCardObject::Int
:
1415 vcObj
->GetValue(&i
);
1416 value
.Printf(_T("%u"), i
);
1420 case wxVCardObject::Long
:
1423 vcObj
->GetValue(&l
);
1424 value
.Printf(_T("%lu"), l
);
1428 case wxVCardObject::None
:
1431 case wxVCardObject::Object
:
1432 value
= _T("<node>");
1436 value
= _T("<unknown value type>");
1440 printf(" = %s", value
.c_str());
1443 DumpVObject(level
+ 1, *vcObj
);
1446 vcObj
= vcard
.GetNextProp(&cookie
);
1450 static void DumpVCardAddresses(const wxVCard
& vcard
)
1452 puts("\nShowing all addresses from vCard:\n");
1456 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
1460 int flags
= addr
->GetFlags();
1461 if ( flags
& wxVCardAddress::Domestic
)
1463 flagsStr
<< _T("domestic ");
1465 if ( flags
& wxVCardAddress::Intl
)
1467 flagsStr
<< _T("international ");
1469 if ( flags
& wxVCardAddress::Postal
)
1471 flagsStr
<< _T("postal ");
1473 if ( flags
& wxVCardAddress::Parcel
)
1475 flagsStr
<< _T("parcel ");
1477 if ( flags
& wxVCardAddress::Home
)
1479 flagsStr
<< _T("home ");
1481 if ( flags
& wxVCardAddress::Work
)
1483 flagsStr
<< _T("work ");
1486 printf("Address %u:\n"
1488 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
1491 addr
->GetPostOffice().c_str(),
1492 addr
->GetExtAddress().c_str(),
1493 addr
->GetStreet().c_str(),
1494 addr
->GetLocality().c_str(),
1495 addr
->GetRegion().c_str(),
1496 addr
->GetPostalCode().c_str(),
1497 addr
->GetCountry().c_str()
1501 addr
= vcard
.GetNextAddress(&cookie
);
1505 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
1507 puts("\nShowing all phone numbers from vCard:\n");
1511 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
1515 int flags
= phone
->GetFlags();
1516 if ( flags
& wxVCardPhoneNumber::Voice
)
1518 flagsStr
<< _T("voice ");
1520 if ( flags
& wxVCardPhoneNumber::Fax
)
1522 flagsStr
<< _T("fax ");
1524 if ( flags
& wxVCardPhoneNumber::Cellular
)
1526 flagsStr
<< _T("cellular ");
1528 if ( flags
& wxVCardPhoneNumber::Modem
)
1530 flagsStr
<< _T("modem ");
1532 if ( flags
& wxVCardPhoneNumber::Home
)
1534 flagsStr
<< _T("home ");
1536 if ( flags
& wxVCardPhoneNumber::Work
)
1538 flagsStr
<< _T("work ");
1541 printf("Phone number %u:\n"
1546 phone
->GetNumber().c_str()
1550 phone
= vcard
.GetNextPhoneNumber(&cookie
);
1554 static void TestVCardRead()
1556 puts("*** Testing wxVCard reading ***\n");
1558 wxVCard
vcard(_T("vcard.vcf"));
1559 if ( !vcard
.IsOk() )
1561 puts("ERROR: couldn't load vCard.");
1565 // read individual vCard properties
1566 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
1570 vcObj
->GetValue(&value
);
1575 value
= _T("<none>");
1578 printf("Full name retrieved directly: %s\n", value
.c_str());
1581 if ( !vcard
.GetFullName(&value
) )
1583 value
= _T("<none>");
1586 printf("Full name from wxVCard API: %s\n", value
.c_str());
1588 // now show how to deal with multiply occuring properties
1589 DumpVCardAddresses(vcard
);
1590 DumpVCardPhoneNumbers(vcard
);
1592 // and finally show all
1593 puts("\nNow dumping the entire vCard:\n"
1594 "-----------------------------\n");
1596 DumpVObject(0, vcard
);
1600 static void TestVCardWrite()
1602 puts("*** Testing wxVCard writing ***\n");
1605 if ( !vcard
.IsOk() )
1607 puts("ERROR: couldn't create vCard.");
1612 vcard
.SetName("Zeitlin", "Vadim");
1613 vcard
.SetFullName("Vadim Zeitlin");
1614 vcard
.SetOrganization("wxWindows", "R&D");
1616 // just dump the vCard back
1617 puts("Entire vCard follows:\n");
1618 puts(vcard
.Write());
1622 #endif // TEST_VCARD
1624 // ----------------------------------------------------------------------------
1625 // wide char (Unicode) support
1626 // ----------------------------------------------------------------------------
1630 #include <wx/strconv.h>
1631 #include <wx/buffer.h>
1633 static void TestUtf8()
1635 puts("*** Testing UTF8 support ***\n");
1637 wxString testString
= "français";
1639 "************ French - Français ****************"
1640 "Juste un petit exemple pour dire que les français aussi"
1641 "ont à cœur de pouvoir utiliser tous leurs caractères ! :)";
1644 wxWCharBuffer wchBuf
= testString
.wc_str(wxConvUTF8
);
1645 const wchar_t *pwz
= (const wchar_t *)wchBuf
;
1646 wxString
testString2(pwz
, wxConvLocal
);
1648 printf("Decoding '%s' => '%s'\n", testString
.c_str(), testString2
.c_str());
1650 char *psz
= "fran" "\xe7" "ais";
1651 size_t len
= strlen(psz
);
1652 wchar_t *pwz2
= new wchar_t[len
+ 1];
1653 for ( size_t n
= 0; n
<= len
; n
++ )
1655 pwz2
[n
] = (wchar_t)(unsigned char)psz
[n
];
1658 wxString
testString3(pwz2
, wxConvUTF8
);
1661 printf("Encoding '%s' -> '%s'\n", psz
, testString3
.c_str());
1664 #endif // TEST_WCHAR
1666 // ----------------------------------------------------------------------------
1668 // ----------------------------------------------------------------------------
1672 #include "wx/zipstrm.h"
1674 static void TestZipStreamRead()
1676 puts("*** Testing ZIP reading ***\n");
1678 wxZipInputStream
istr(_T("idx.zip"), _T("IDX.txt"));
1679 printf("Archive size: %u\n", istr
.GetSize());
1681 puts("Dumping the file:");
1682 while ( !istr
.Eof() )
1684 putchar(istr
.GetC());
1688 puts("\n----- done ------");
1693 // ----------------------------------------------------------------------------
1695 // ----------------------------------------------------------------------------
1699 #include <wx/zstream.h>
1700 #include <wx/wfstream.h>
1702 static const wxChar
*FILENAME_GZ
= _T("test.gz");
1703 static const char *TEST_DATA
= "hello and hello again";
1705 static void TestZlibStreamWrite()
1707 puts("*** Testing Zlib stream reading ***\n");
1709 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
1710 wxZlibOutputStream
ostr(fileOutStream
, 0);
1711 printf("Compressing the test string... ");
1712 ostr
.Write(TEST_DATA
, sizeof(TEST_DATA
));
1715 puts("(ERROR: failed)");
1722 puts("\n----- done ------");
1725 static void TestZlibStreamRead()
1727 puts("*** Testing Zlib stream reading ***\n");
1729 wxFileInputStream
fileInStream(FILENAME_GZ
);
1730 wxZlibInputStream
istr(fileInStream
);
1731 printf("Archive size: %u\n", istr
.GetSize());
1733 puts("Dumping the file:");
1734 while ( !istr
.Eof() )
1736 putchar(istr
.GetC());
1740 puts("\n----- done ------");
1745 // ----------------------------------------------------------------------------
1747 // ----------------------------------------------------------------------------
1749 #ifdef TEST_DATETIME
1751 #include <wx/date.h>
1753 #include <wx/datetime.h>
1758 wxDateTime::wxDateTime_t day
;
1759 wxDateTime::Month month
;
1761 wxDateTime::wxDateTime_t hour
, min
, sec
;
1763 wxDateTime::WeekDay wday
;
1764 time_t gmticks
, ticks
;
1766 void Init(const wxDateTime::Tm
& tm
)
1775 gmticks
= ticks
= -1;
1778 wxDateTime
DT() const
1779 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
1781 bool SameDay(const wxDateTime::Tm
& tm
) const
1783 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
1786 wxString
Format() const
1789 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
1791 wxDateTime::GetMonthName(month
).c_str(),
1793 abs(wxDateTime::ConvertYearToBC(year
)),
1794 year
> 0 ? "AD" : "BC");
1798 wxString
FormatDate() const
1801 s
.Printf("%02d-%s-%4d%s",
1803 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
1804 abs(wxDateTime::ConvertYearToBC(year
)),
1805 year
> 0 ? "AD" : "BC");
1810 static const Date testDates
[] =
1812 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
1813 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
1814 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
1815 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
1816 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
1817 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
1818 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
1819 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
1820 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
1821 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
1822 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
1823 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
1824 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
1825 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
1826 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
1829 // this test miscellaneous static wxDateTime functions
1830 static void TestTimeStatic()
1832 puts("\n*** wxDateTime static methods test ***");
1834 // some info about the current date
1835 int year
= wxDateTime::GetCurrentYear();
1836 printf("Current year %d is %sa leap one and has %d days.\n",
1838 wxDateTime::IsLeapYear(year
) ? "" : "not ",
1839 wxDateTime::GetNumberOfDays(year
));
1841 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
1842 printf("Current month is '%s' ('%s') and it has %d days\n",
1843 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
1844 wxDateTime::GetMonthName(month
).c_str(),
1845 wxDateTime::GetNumberOfDays(month
));
1848 static const size_t nYears
= 5;
1849 static const size_t years
[2][nYears
] =
1851 // first line: the years to test
1852 { 1990, 1976, 2000, 2030, 1984, },
1854 // second line: TRUE if leap, FALSE otherwise
1855 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
1858 for ( size_t n
= 0; n
< nYears
; n
++ )
1860 int year
= years
[0][n
];
1861 bool should
= years
[1][n
] != 0,
1862 is
= wxDateTime::IsLeapYear(year
);
1864 printf("Year %d is %sa leap year (%s)\n",
1867 should
== is
? "ok" : "ERROR");
1869 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
1873 // test constructing wxDateTime objects
1874 static void TestTimeSet()
1876 puts("\n*** wxDateTime construction test ***");
1878 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1880 const Date
& d1
= testDates
[n
];
1881 wxDateTime dt
= d1
.DT();
1884 d2
.Init(dt
.GetTm());
1886 wxString s1
= d1
.Format(),
1889 printf("Date: %s == %s (%s)\n",
1890 s1
.c_str(), s2
.c_str(),
1891 s1
== s2
? "ok" : "ERROR");
1895 // test time zones stuff
1896 static void TestTimeZones()
1898 puts("\n*** wxDateTime timezone test ***");
1900 wxDateTime now
= wxDateTime::Now();
1902 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
1903 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
1904 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
1905 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
1906 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
1907 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
1909 wxDateTime::Tm tm
= now
.GetTm();
1910 if ( wxDateTime(tm
) != now
)
1912 printf("ERROR: got %s instead of %s\n",
1913 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
1917 // test some minimal support for the dates outside the standard range
1918 static void TestTimeRange()
1920 puts("\n*** wxDateTime out-of-standard-range dates test ***");
1922 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
1924 printf("Unix epoch:\t%s\n",
1925 wxDateTime(2440587.5).Format(fmt
).c_str());
1926 printf("Feb 29, 0: \t%s\n",
1927 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
1928 printf("JDN 0: \t%s\n",
1929 wxDateTime(0.0).Format(fmt
).c_str());
1930 printf("Jan 1, 1AD:\t%s\n",
1931 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
1932 printf("May 29, 2099:\t%s\n",
1933 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
1936 static void TestTimeTicks()
1938 puts("\n*** wxDateTime ticks test ***");
1940 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1942 const Date
& d
= testDates
[n
];
1943 if ( d
.ticks
== -1 )
1946 wxDateTime dt
= d
.DT();
1947 long ticks
= (dt
.GetValue() / 1000).ToLong();
1948 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1949 if ( ticks
== d
.ticks
)
1955 printf(" (ERROR: should be %ld, delta = %ld)\n",
1956 d
.ticks
, ticks
- d
.ticks
);
1959 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
1960 ticks
= (dt
.GetValue() / 1000).ToLong();
1961 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1962 if ( ticks
== d
.gmticks
)
1968 printf(" (ERROR: should be %ld, delta = %ld)\n",
1969 d
.gmticks
, ticks
- d
.gmticks
);
1976 // test conversions to JDN &c
1977 static void TestTimeJDN()
1979 puts("\n*** wxDateTime to JDN test ***");
1981 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1983 const Date
& d
= testDates
[n
];
1984 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
1985 double jdn
= dt
.GetJulianDayNumber();
1987 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
1994 printf(" (ERROR: should be %f, delta = %f)\n",
1995 d
.jdn
, jdn
- d
.jdn
);
2000 // test week days computation
2001 static void TestTimeWDays()
2003 puts("\n*** wxDateTime weekday test ***");
2005 // test GetWeekDay()
2007 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
2009 const Date
& d
= testDates
[n
];
2010 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
2012 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
2015 wxDateTime::GetWeekDayName(wday
).c_str());
2016 if ( wday
== d
.wday
)
2022 printf(" (ERROR: should be %s)\n",
2023 wxDateTime::GetWeekDayName(d
.wday
).c_str());
2029 // test SetToWeekDay()
2030 struct WeekDateTestData
2032 Date date
; // the real date (precomputed)
2033 int nWeek
; // its week index in the month
2034 wxDateTime::WeekDay wday
; // the weekday
2035 wxDateTime::Month month
; // the month
2036 int year
; // and the year
2038 wxString
Format() const
2041 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
2043 case 1: which
= "first"; break;
2044 case 2: which
= "second"; break;
2045 case 3: which
= "third"; break;
2046 case 4: which
= "fourth"; break;
2047 case 5: which
= "fifth"; break;
2049 case -1: which
= "last"; break;
2054 which
+= " from end";
2057 s
.Printf("The %s %s of %s in %d",
2059 wxDateTime::GetWeekDayName(wday
).c_str(),
2060 wxDateTime::GetMonthName(month
).c_str(),
2067 // the array data was generated by the following python program
2069 from DateTime import *
2070 from whrandom import *
2071 from string import *
2073 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2074 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2076 week = DateTimeDelta(7)
2079 year = randint(1900, 2100)
2080 month = randint(1, 12)
2081 day = randint(1, 28)
2082 dt = DateTime(year, month, day)
2083 wday = dt.day_of_week
2085 countFromEnd = choice([-1, 1])
2088 while dt.month is month:
2089 dt = dt - countFromEnd * week
2090 weekNum = weekNum + countFromEnd
2092 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
2094 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
2095 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
2098 static const WeekDateTestData weekDatesTestData
[] =
2100 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
2101 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
2102 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
2103 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
2104 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
2105 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
2106 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
2107 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
2108 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
2109 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
2110 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
2111 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
2112 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
2113 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
2114 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
2115 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
2116 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
2117 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
2118 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
2119 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
2122 static const char *fmt
= "%d-%b-%Y";
2125 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
2127 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
2129 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
2131 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
2133 const Date
& d
= wd
.date
;
2134 if ( d
.SameDay(dt
.GetTm()) )
2140 dt
.Set(d
.day
, d
.month
, d
.year
);
2142 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
2147 // test the computation of (ISO) week numbers
2148 static void TestTimeWNumber()
2150 puts("\n*** wxDateTime week number test ***");
2152 struct WeekNumberTestData
2154 Date date
; // the date
2155 wxDateTime::wxDateTime_t week
; // the week number in the year
2156 wxDateTime::wxDateTime_t wmon
; // the week number in the month
2157 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
2158 wxDateTime::wxDateTime_t dnum
; // day number in the year
2161 // data generated with the following python script:
2163 from DateTime import *
2164 from whrandom import *
2165 from string import *
2167 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2168 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2170 def GetMonthWeek(dt):
2171 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
2172 if weekNumMonth < 0:
2173 weekNumMonth = weekNumMonth + 53
2176 def GetLastSundayBefore(dt):
2177 if dt.iso_week[2] == 7:
2180 return dt - DateTimeDelta(dt.iso_week[2])
2183 year = randint(1900, 2100)
2184 month = randint(1, 12)
2185 day = randint(1, 28)
2186 dt = DateTime(year, month, day)
2187 dayNum = dt.day_of_year
2188 weekNum = dt.iso_week[1]
2189 weekNumMonth = GetMonthWeek(dt)
2192 dtSunday = GetLastSundayBefore(dt)
2194 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
2195 weekNumMonth2 = weekNumMonth2 + 1
2196 dtSunday = dtSunday - DateTimeDelta(7)
2198 data = { 'day': rjust(`day`, 2), \
2199 'month': monthNames[month - 1], \
2201 'weekNum': rjust(`weekNum`, 2), \
2202 'weekNumMonth': weekNumMonth, \
2203 'weekNumMonth2': weekNumMonth2, \
2204 'dayNum': rjust(`dayNum`, 3) }
2206 print " { { %(day)s, "\
2207 "wxDateTime::%(month)s, "\
2210 "%(weekNumMonth)s, "\
2211 "%(weekNumMonth2)s, "\
2212 "%(dayNum)s }," % data
2215 static const WeekNumberTestData weekNumberTestDates
[] =
2217 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
2218 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
2219 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
2220 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
2221 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
2222 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
2223 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
2224 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
2225 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
2226 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
2227 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
2228 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
2229 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
2230 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
2231 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
2232 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
2233 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
2234 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
2235 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
2236 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
2239 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
2241 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
2242 const Date
& d
= wn
.date
;
2244 wxDateTime dt
= d
.DT();
2246 wxDateTime::wxDateTime_t
2247 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
2248 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
2249 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
2250 dnum
= dt
.GetDayOfYear();
2252 printf("%s: the day number is %d",
2253 d
.FormatDate().c_str(), dnum
);
2254 if ( dnum
== wn
.dnum
)
2260 printf(" (ERROR: should be %d)", wn
.dnum
);
2263 printf(", week in month is %d", wmon
);
2264 if ( wmon
== wn
.wmon
)
2270 printf(" (ERROR: should be %d)", wn
.wmon
);
2273 printf(" or %d", wmon2
);
2274 if ( wmon2
== wn
.wmon2
)
2280 printf(" (ERROR: should be %d)", wn
.wmon2
);
2283 printf(", week in year is %d", week
);
2284 if ( week
== wn
.week
)
2290 printf(" (ERROR: should be %d)\n", wn
.week
);
2295 // test DST calculations
2296 static void TestTimeDST()
2298 puts("\n*** wxDateTime DST test ***");
2300 printf("DST is%s in effect now.\n\n",
2301 wxDateTime::Now().IsDST() ? "" : " not");
2303 // taken from http://www.energy.ca.gov/daylightsaving.html
2304 static const Date datesDST
[2][2004 - 1900 + 1] =
2307 { 1, wxDateTime::Apr
, 1990 },
2308 { 7, wxDateTime::Apr
, 1991 },
2309 { 5, wxDateTime::Apr
, 1992 },
2310 { 4, wxDateTime::Apr
, 1993 },
2311 { 3, wxDateTime::Apr
, 1994 },
2312 { 2, wxDateTime::Apr
, 1995 },
2313 { 7, wxDateTime::Apr
, 1996 },
2314 { 6, wxDateTime::Apr
, 1997 },
2315 { 5, wxDateTime::Apr
, 1998 },
2316 { 4, wxDateTime::Apr
, 1999 },
2317 { 2, wxDateTime::Apr
, 2000 },
2318 { 1, wxDateTime::Apr
, 2001 },
2319 { 7, wxDateTime::Apr
, 2002 },
2320 { 6, wxDateTime::Apr
, 2003 },
2321 { 4, wxDateTime::Apr
, 2004 },
2324 { 28, wxDateTime::Oct
, 1990 },
2325 { 27, wxDateTime::Oct
, 1991 },
2326 { 25, wxDateTime::Oct
, 1992 },
2327 { 31, wxDateTime::Oct
, 1993 },
2328 { 30, wxDateTime::Oct
, 1994 },
2329 { 29, wxDateTime::Oct
, 1995 },
2330 { 27, wxDateTime::Oct
, 1996 },
2331 { 26, wxDateTime::Oct
, 1997 },
2332 { 25, wxDateTime::Oct
, 1998 },
2333 { 31, wxDateTime::Oct
, 1999 },
2334 { 29, wxDateTime::Oct
, 2000 },
2335 { 28, wxDateTime::Oct
, 2001 },
2336 { 27, wxDateTime::Oct
, 2002 },
2337 { 26, wxDateTime::Oct
, 2003 },
2338 { 31, wxDateTime::Oct
, 2004 },
2343 for ( year
= 1990; year
< 2005; year
++ )
2345 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
2346 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
2348 printf("DST period in the US for year %d: from %s to %s",
2349 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
2351 size_t n
= year
- 1990;
2352 const Date
& dBegin
= datesDST
[0][n
];
2353 const Date
& dEnd
= datesDST
[1][n
];
2355 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
2361 printf(" (ERROR: should be %s %d to %s %d)\n",
2362 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
2363 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
2369 for ( year
= 1990; year
< 2005; year
++ )
2371 printf("DST period in Europe for year %d: from %s to %s\n",
2373 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
2374 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
2378 // test wxDateTime -> text conversion
2379 static void TestTimeFormat()
2381 puts("\n*** wxDateTime formatting test ***");
2383 // some information may be lost during conversion, so store what kind
2384 // of info should we recover after a round trip
2387 CompareNone
, // don't try comparing
2388 CompareBoth
, // dates and times should be identical
2389 CompareDate
, // dates only
2390 CompareTime
// time only
2395 CompareKind compareKind
;
2397 } formatTestFormats
[] =
2399 { CompareBoth
, "---> %c" },
2400 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
2401 { CompareBoth
, "Date is %x, time is %X" },
2402 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
2403 { CompareNone
, "The day of year: %j, the week of year: %W" },
2404 { CompareDate
, "ISO date without separators: %4Y%2m%2d" },
2407 static const Date formatTestDates
[] =
2409 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
2410 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
2412 // this test can't work for other centuries because it uses two digit
2413 // years in formats, so don't even try it
2414 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
2415 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
2416 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
2420 // an extra test (as it doesn't depend on date, don't do it in the loop)
2421 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
2423 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
2427 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
2428 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
2430 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
2431 printf("%s", s
.c_str());
2433 // what can we recover?
2434 int kind
= formatTestFormats
[n
].compareKind
;
2438 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
2441 // converion failed - should it have?
2442 if ( kind
== CompareNone
)
2445 puts(" (ERROR: conversion back failed)");
2449 // should have parsed the entire string
2450 puts(" (ERROR: conversion back stopped too soon)");
2454 bool equal
= FALSE
; // suppress compilaer warning
2462 equal
= dt
.IsSameDate(dt2
);
2466 equal
= dt
.IsSameTime(dt2
);
2472 printf(" (ERROR: got back '%s' instead of '%s')\n",
2473 dt2
.Format().c_str(), dt
.Format().c_str());
2484 // test text -> wxDateTime conversion
2485 static void TestTimeParse()
2487 puts("\n*** wxDateTime parse test ***");
2489 struct ParseTestData
2496 static const ParseTestData parseTestDates
[] =
2498 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
2499 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
2502 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
2504 const char *format
= parseTestDates
[n
].format
;
2506 printf("%s => ", format
);
2509 if ( dt
.ParseRfc822Date(format
) )
2511 printf("%s ", dt
.Format().c_str());
2513 if ( parseTestDates
[n
].good
)
2515 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
2522 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
2527 puts("(ERROR: bad format)");
2532 printf("bad format (%s)\n",
2533 parseTestDates
[n
].good
? "ERROR" : "ok");
2538 static void TestInteractive()
2540 puts("\n*** interactive wxDateTime tests ***");
2546 printf("Enter a date: ");
2547 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
2550 // kill the last '\n'
2551 buf
[strlen(buf
) - 1] = 0;
2554 const char *p
= dt
.ParseDate(buf
);
2557 printf("ERROR: failed to parse the date '%s'.\n", buf
);
2563 printf("WARNING: parsed only first %u characters.\n", p
- buf
);
2566 printf("%s: day %u, week of month %u/%u, week of year %u\n",
2567 dt
.Format("%b %d, %Y").c_str(),
2569 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
2570 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
2571 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
2574 puts("\n*** done ***");
2577 static void TestTimeMS()
2579 puts("*** testing millisecond-resolution support in wxDateTime ***");
2581 wxDateTime dt1
= wxDateTime::Now(),
2582 dt2
= wxDateTime::UNow();
2584 printf("Now = %s\n", dt1
.Format("%H:%M:%S:%l").c_str());
2585 printf("UNow = %s\n", dt2
.Format("%H:%M:%S:%l").c_str());
2586 printf("Dummy loop: ");
2587 for ( int i
= 0; i
< 6000; i
++ )
2589 //for ( int j = 0; j < 10; j++ )
2592 s
.Printf("%g", sqrt(i
));
2601 dt2
= wxDateTime::UNow();
2602 printf("UNow = %s\n", dt2
.Format("%H:%M:%S:%l").c_str());
2604 printf("Loop executed in %s ms\n", (dt2
- dt1
).Format("%l").c_str());
2606 puts("\n*** done ***");
2609 static void TestTimeArithmetics()
2611 puts("\n*** testing arithmetic operations on wxDateTime ***");
2613 static const struct ArithmData
2615 ArithmData(const wxDateSpan
& sp
, const char *nam
)
2616 : span(sp
), name(nam
) { }
2620 } testArithmData
[] =
2622 ArithmData(wxDateSpan::Day(), "day"),
2623 ArithmData(wxDateSpan::Week(), "week"),
2624 ArithmData(wxDateSpan::Month(), "month"),
2625 ArithmData(wxDateSpan::Year(), "year"),
2626 ArithmData(wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days"),
2629 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
2631 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
2633 wxDateSpan span
= testArithmData
[n
].span
;
2637 const char *name
= testArithmData
[n
].name
;
2638 printf("%s + %s = %s, %s - %s = %s\n",
2639 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
2640 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
2642 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
2643 if ( dt1
- span
== dt
)
2649 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
2652 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
2653 if ( dt2
+ span
== dt
)
2659 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
2662 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
2663 if ( dt2
+ 2*span
== dt1
)
2669 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
2676 static void TestTimeHolidays()
2678 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
2680 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
2681 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
2682 dtEnd
= dtStart
.GetLastMonthDay();
2684 wxDateTimeArray hol
;
2685 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
2687 const wxChar
*format
= "%d-%b-%Y (%a)";
2689 printf("All holidays between %s and %s:\n",
2690 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
2692 size_t count
= hol
.GetCount();
2693 for ( size_t n
= 0; n
< count
; n
++ )
2695 printf("\t%s\n", hol
[n
].Format(format
).c_str());
2701 static void TestTimeZoneBug()
2703 puts("\n*** testing for DST/timezone bug ***\n");
2705 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
2706 for ( int i
= 0; i
< 31; i
++ )
2708 printf("Date %s: week day %s.\n",
2709 date
.Format(_T("%d-%m-%Y")).c_str(),
2710 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
2712 date
+= wxDateSpan::Day();
2720 // test compatibility with the old wxDate/wxTime classes
2721 static void TestTimeCompatibility()
2723 puts("\n*** wxDateTime compatibility test ***");
2725 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
2726 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
2728 double jdnNow
= wxDateTime::Now().GetJDN();
2729 long jdnMidnight
= (long)(jdnNow
- 0.5);
2730 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
2732 jdnMidnight
= wxDate().Set().GetJulianDate();
2733 printf("wxDateTime for today: %s\n",
2734 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
2736 int flags
= wxEUROPEAN
;//wxFULL;
2739 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
2740 for ( int n
= 0; n
< 7; n
++ )
2742 printf("Previous %s is %s\n",
2743 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
2744 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
2750 #endif // TEST_DATETIME
2752 // ----------------------------------------------------------------------------
2754 // ----------------------------------------------------------------------------
2758 #include <wx/thread.h>
2760 static size_t gs_counter
= (size_t)-1;
2761 static wxCriticalSection gs_critsect
;
2762 static wxCondition gs_cond
;
2764 class MyJoinableThread
: public wxThread
2767 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
2768 { m_n
= n
; Create(); }
2770 // thread execution starts here
2771 virtual ExitCode
Entry();
2777 wxThread::ExitCode
MyJoinableThread::Entry()
2779 unsigned long res
= 1;
2780 for ( size_t n
= 1; n
< m_n
; n
++ )
2784 // it's a loooong calculation :-)
2788 return (ExitCode
)res
;
2791 class MyDetachedThread
: public wxThread
2794 MyDetachedThread(size_t n
, char ch
)
2798 m_cancelled
= FALSE
;
2803 // thread execution starts here
2804 virtual ExitCode
Entry();
2807 virtual void OnExit();
2810 size_t m_n
; // number of characters to write
2811 char m_ch
; // character to write
2813 bool m_cancelled
; // FALSE if we exit normally
2816 wxThread::ExitCode
MyDetachedThread::Entry()
2819 wxCriticalSectionLocker
lock(gs_critsect
);
2820 if ( gs_counter
== (size_t)-1 )
2826 for ( size_t n
= 0; n
< m_n
; n
++ )
2828 if ( TestDestroy() )
2838 wxThread::Sleep(100);
2844 void MyDetachedThread::OnExit()
2846 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
2848 wxCriticalSectionLocker
lock(gs_critsect
);
2849 if ( !--gs_counter
&& !m_cancelled
)
2853 void TestDetachedThreads()
2855 puts("\n*** Testing detached threads ***");
2857 static const size_t nThreads
= 3;
2858 MyDetachedThread
*threads
[nThreads
];
2860 for ( n
= 0; n
< nThreads
; n
++ )
2862 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
2865 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
2866 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
2868 for ( n
= 0; n
< nThreads
; n
++ )
2873 // wait until all threads terminate
2879 void TestJoinableThreads()
2881 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
2883 // calc 10! in the background
2884 MyJoinableThread
thread(10);
2887 printf("\nThread terminated with exit code %lu.\n",
2888 (unsigned long)thread
.Wait());
2891 void TestThreadSuspend()
2893 puts("\n*** Testing thread suspend/resume functions ***");
2895 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
2899 // this is for this demo only, in a real life program we'd use another
2900 // condition variable which would be signaled from wxThread::Entry() to
2901 // tell us that the thread really started running - but here just wait a
2902 // bit and hope that it will be enough (the problem is, of course, that
2903 // the thread might still not run when we call Pause() which will result
2905 wxThread::Sleep(300);
2907 for ( size_t n
= 0; n
< 3; n
++ )
2911 puts("\nThread suspended");
2914 // don't sleep but resume immediately the first time
2915 wxThread::Sleep(300);
2917 puts("Going to resume the thread");
2922 puts("Waiting until it terminates now");
2924 // wait until the thread terminates
2930 void TestThreadDelete()
2932 // As above, using Sleep() is only for testing here - we must use some
2933 // synchronisation object instead to ensure that the thread is still
2934 // running when we delete it - deleting a detached thread which already
2935 // terminated will lead to a crash!
2937 puts("\n*** Testing thread delete function ***");
2939 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
2943 puts("\nDeleted a thread which didn't start to run yet.");
2945 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
2949 wxThread::Sleep(300);
2953 puts("\nDeleted a running thread.");
2955 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
2959 wxThread::Sleep(300);
2965 puts("\nDeleted a sleeping thread.");
2967 MyJoinableThread
thread3(20);
2972 puts("\nDeleted a joinable thread.");
2974 MyJoinableThread
thread4(2);
2977 wxThread::Sleep(300);
2981 puts("\nDeleted a joinable thread which already terminated.");
2986 #endif // TEST_THREADS
2988 // ----------------------------------------------------------------------------
2990 // ----------------------------------------------------------------------------
2994 static void PrintArray(const char* name
, const wxArrayString
& array
)
2996 printf("Dump of the array '%s'\n", name
);
2998 size_t nCount
= array
.GetCount();
2999 for ( size_t n
= 0; n
< nCount
; n
++ )
3001 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
3005 static int StringLenCompare(const wxString
& first
, const wxString
& second
)
3007 return first
.length() - second
.length();
3010 #include "wx/dynarray.h"
3012 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
3013 #include "wx/arrimpl.cpp"
3014 WX_DEFINE_OBJARRAY(ArrayBars
);
3016 static void TestArrayOfObjects()
3018 puts("*** Testing wxObjArray ***\n");
3022 Bar
bar("second bar");
3024 printf("Initially: %u objects in the array, %u objects total.\n",
3025 bars
.GetCount(), Bar::GetNumber());
3027 bars
.Add(new Bar("first bar"));
3030 printf("Now: %u objects in the array, %u objects total.\n",
3031 bars
.GetCount(), Bar::GetNumber());
3035 printf("After Empty(): %u objects in the array, %u objects total.\n",
3036 bars
.GetCount(), Bar::GetNumber());
3039 printf("Finally: no more objects in the array, %u objects total.\n",
3043 #endif // TEST_ARRAYS
3045 // ----------------------------------------------------------------------------
3047 // ----------------------------------------------------------------------------
3051 #include "wx/timer.h"
3052 #include "wx/tokenzr.h"
3054 static void TestStringConstruction()
3056 puts("*** Testing wxString constructores ***");
3058 #define TEST_CTOR(args, res) \
3061 printf("wxString%s = %s ", #args, s.c_str()); \
3068 printf("(ERROR: should be %s)\n", res); \
3072 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
3073 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
3074 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
3075 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
3077 static const wxChar
*s
= _T("?really!");
3078 const wxChar
*start
= wxStrchr(s
, _T('r'));
3079 const wxChar
*end
= wxStrchr(s
, _T('!'));
3080 TEST_CTOR((start
, end
), _T("really"));
3085 static void TestString()
3095 for (int i
= 0; i
< 1000000; ++i
)
3099 c
= "! How'ya doin'?";
3102 c
= "Hello world! What's up?";
3107 printf ("TestString elapsed time: %ld\n", sw
.Time());
3110 static void TestPChar()
3118 for (int i
= 0; i
< 1000000; ++i
)
3120 strcpy (a
, "Hello");
3121 strcpy (b
, " world");
3122 strcpy (c
, "! How'ya doin'?");
3125 strcpy (c
, "Hello world! What's up?");
3126 if (strcmp (c
, a
) == 0)
3130 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
3133 static void TestStringSub()
3135 wxString
s("Hello, world!");
3137 puts("*** Testing wxString substring extraction ***");
3139 printf("String = '%s'\n", s
.c_str());
3140 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
3141 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
3142 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
3143 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
3144 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
3145 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
3147 static const wxChar
*prefixes
[] =
3151 _T("Hello, world!"),
3152 _T("Hello, world!!!"),
3158 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
3160 wxString prefix
= prefixes
[n
], rest
;
3161 bool rc
= s
.StartsWith(prefix
, &rest
);
3162 printf("StartsWith('%s') = %s", prefix
.c_str(), rc
? "TRUE" : "FALSE");
3165 printf(" (the rest is '%s')\n", rest
.c_str());
3176 static void TestStringFormat()
3178 puts("*** Testing wxString formatting ***");
3181 s
.Printf("%03d", 18);
3183 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
3184 printf("Number 18: %s\n", s
.c_str());
3189 // returns "not found" for npos, value for all others
3190 static wxString
PosToString(size_t res
)
3192 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
3193 : wxString::Format(_T("%u"), res
);
3197 static void TestStringFind()
3199 puts("*** Testing wxString find() functions ***");
3201 static const wxChar
*strToFind
= _T("ell");
3202 static const struct StringFindTest
3206 result
; // of searching "ell" in str
3209 { _T("Well, hello world"), 0, 1 },
3210 { _T("Well, hello world"), 6, 7 },
3211 { _T("Well, hello world"), 9, wxString::npos
},
3214 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
3216 const StringFindTest
& ft
= findTestData
[n
];
3217 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
3219 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
3220 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
3222 size_t resTrue
= ft
.result
;
3223 if ( res
== resTrue
)
3229 printf(_T("(ERROR: should be %s)\n"),
3230 PosToString(resTrue
).c_str());
3237 static void TestStringTokenizer()
3239 puts("*** Testing wxStringTokenizer ***");
3241 static const wxChar
*modeNames
[] =
3245 _T("return all empty"),
3250 static const struct StringTokenizerTest
3252 const wxChar
*str
; // string to tokenize
3253 const wxChar
*delims
; // delimiters to use
3254 size_t count
; // count of token
3255 wxStringTokenizerMode mode
; // how should we tokenize it
3256 } tokenizerTestData
[] =
3258 { _T(""), _T(" "), 0 },
3259 { _T("Hello, world"), _T(" "), 2 },
3260 { _T("Hello, world "), _T(" "), 2 },
3261 { _T("Hello, world"), _T(","), 2 },
3262 { _T("Hello, world!"), _T(",!"), 2 },
3263 { _T("Hello,, world!"), _T(",!"), 3 },
3264 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
3265 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
3266 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
3267 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
3268 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
3269 { _T("01/02/99"), _T("/-"), 3 },
3270 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
3273 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
3275 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
3276 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
3278 size_t count
= tkz
.CountTokens();
3279 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
3280 MakePrintable(tt
.str
).c_str(),
3282 MakePrintable(tt
.delims
).c_str(),
3283 modeNames
[tkz
.GetMode()]);
3284 if ( count
== tt
.count
)
3290 printf(_T("(ERROR: should be %u)\n"), tt
.count
);
3295 // if we emulate strtok(), check that we do it correctly
3296 wxChar
*buf
, *s
= NULL
, *last
;
3298 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
3300 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
3301 wxStrcpy(buf
, tt
.str
);
3303 s
= wxStrtok(buf
, tt
.delims
, &last
);
3310 // now show the tokens themselves
3312 while ( tkz
.HasMoreTokens() )
3314 wxString token
= tkz
.GetNextToken();
3316 printf(_T("\ttoken %u: '%s'"),
3318 MakePrintable(token
).c_str());
3328 printf(" (ERROR: should be %s)\n", s
);
3331 s
= wxStrtok(NULL
, tt
.delims
, &last
);
3335 // nothing to compare with
3340 if ( count2
!= count
)
3342 puts(_T("\tERROR: token count mismatch"));
3351 static void TestStringReplace()
3353 puts("*** Testing wxString::replace ***");
3355 static const struct StringReplaceTestData
3357 const wxChar
*original
; // original test string
3358 size_t start
, len
; // the part to replace
3359 const wxChar
*replacement
; // the replacement string
3360 const wxChar
*result
; // and the expected result
3361 } stringReplaceTestData
[] =
3363 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
3364 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
3365 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
3366 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
3367 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
3370 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
3372 const StringReplaceTestData data
= stringReplaceTestData
[n
];
3374 wxString original
= data
.original
;
3375 original
.replace(data
.start
, data
.len
, data
.replacement
);
3377 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
3378 data
.original
, data
.start
, data
.len
, data
.replacement
,
3381 if ( original
== data
.result
)
3387 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
3394 #endif // TEST_STRINGS
3396 // ----------------------------------------------------------------------------
3398 // ----------------------------------------------------------------------------
3400 int main(int argc
, char **argv
)
3402 if ( !wxInitialize() )
3404 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
3408 puts("Sleeping for 3 seconds... z-z-z-z-z...");
3410 #endif // TEST_USLEEP
3413 static const wxCmdLineEntryDesc cmdLineDesc
[] =
3415 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
3416 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
3418 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
3419 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
3420 { wxCMD_LINE_OPTION
, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
3421 { wxCMD_LINE_OPTION
, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
3423 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
3424 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
3429 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
3431 parser
.AddOption("project_name", "", "full path to project file",
3432 wxCMD_LINE_VAL_STRING
,
3433 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
3435 switch ( parser
.Parse() )
3438 wxLogMessage("Help was given, terminating.");
3442 ShowCmdLine(parser
);
3446 wxLogMessage("Syntax error detected, aborting.");
3449 #endif // TEST_CMDLINE
3460 TestStringConstruction();
3463 TestStringTokenizer();
3464 TestStringReplace();
3466 #endif // TEST_STRINGS
3477 puts("*** Initially:");
3479 PrintArray("a1", a1
);
3481 wxArrayString
a2(a1
);
3482 PrintArray("a2", a2
);
3484 wxSortedArrayString
a3(a1
);
3485 PrintArray("a3", a3
);
3487 puts("*** After deleting a string from a1");
3490 PrintArray("a1", a1
);
3491 PrintArray("a2", a2
);
3492 PrintArray("a3", a3
);
3494 puts("*** After reassigning a1 to a2 and a3");
3496 PrintArray("a2", a2
);
3497 PrintArray("a3", a3
);
3499 puts("*** After sorting a1");
3501 PrintArray("a1", a1
);
3503 puts("*** After sorting a1 in reverse order");
3505 PrintArray("a1", a1
);
3507 puts("*** After sorting a1 by the string length");
3508 a1
.Sort(StringLenCompare
);
3509 PrintArray("a1", a1
);
3511 TestArrayOfObjects();
3512 #endif // TEST_ARRAYS
3518 #ifdef TEST_DLLLOADER
3520 #endif // TEST_DLLLOADER
3524 #endif // TEST_EXECUTE
3526 #ifdef TEST_FILECONF
3528 #endif // TEST_FILECONF
3536 for ( size_t n
= 0; n
< 8000; n
++ )
3538 s
<< (char)('A' + (n
% 26));
3542 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
3544 // this one shouldn't be truncated
3547 // but this one will because log functions use fixed size buffer
3548 // (note that it doesn't need '\n' at the end neither - will be added
3550 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
3560 int nCPUs
= wxThread::GetCPUCount();
3561 printf("This system has %d CPUs\n", nCPUs
);
3563 wxThread::SetConcurrency(nCPUs
);
3565 if ( argc
> 1 && argv
[1][0] == 't' )
3566 wxLog::AddTraceMask("thread");
3569 TestDetachedThreads();
3571 TestJoinableThreads();
3573 TestThreadSuspend();
3577 #endif // TEST_THREADS
3579 #ifdef TEST_LONGLONG
3580 // seed pseudo random generator
3581 srand((unsigned)time(NULL
));
3589 TestMultiplication();
3592 TestLongLongConversion();
3593 TestBitOperations();
3595 TestLongLongComparison();
3596 #endif // TEST_LONGLONG
3603 wxLog::AddTraceMask(_T("mime"));
3610 #ifdef TEST_INFO_FUNCTIONS
3613 #endif // TEST_INFO_FUNCTIONS
3622 TestProtocolFtpUpload();
3623 #endif // TEST_SOCKETS
3627 #endif // TEST_STREAMS
3631 #endif // TEST_TIMER
3633 #ifdef TEST_DATETIME
3646 TestTimeArithmetics();
3655 #endif // TEST_DATETIME
3661 #endif // TEST_VCARD
3665 #endif // TEST_WCHAR
3668 TestZipStreamRead();
3673 TestZlibStreamWrite();
3674 TestZlibStreamRead();