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
53 //#define TEST_STRINGS
54 //#define TEST_THREADS
56 //#define TEST_VCARD -- don't enable this (VZ)
61 // ----------------------------------------------------------------------------
62 // test class for container objects
63 // ----------------------------------------------------------------------------
65 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
67 class Bar
// Foo is already taken in the hash test
70 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
73 static size_t GetNumber() { return ms_bars
; }
75 const char *GetName() const { return m_name
; }
80 static size_t ms_bars
;
83 size_t Bar::ms_bars
= 0;
85 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
87 // ============================================================================
89 // ============================================================================
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
97 // replace TABs with \t and CRs with \n
98 static wxString
MakePrintable(const wxChar
*s
)
101 (void)str
.Replace(_T("\t"), _T("\\t"));
102 (void)str
.Replace(_T("\n"), _T("\\n"));
103 (void)str
.Replace(_T("\r"), _T("\\r"));
108 #endif // MakePrintable() is used
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
116 #include <wx/cmdline.h>
117 #include <wx/datetime.h>
119 static void ShowCmdLine(const wxCmdLineParser
& parser
)
121 wxString s
= "Input files: ";
123 size_t count
= parser
.GetParamCount();
124 for ( size_t param
= 0; param
< count
; param
++ )
126 s
<< parser
.GetParam(param
) << ' ';
130 << "Verbose:\t" << (parser
.Found("v") ? "yes" : "no") << '\n'
131 << "Quiet:\t" << (parser
.Found("q") ? "yes" : "no") << '\n';
136 if ( parser
.Found("o", &strVal
) )
137 s
<< "Output file:\t" << strVal
<< '\n';
138 if ( parser
.Found("i", &strVal
) )
139 s
<< "Input dir:\t" << strVal
<< '\n';
140 if ( parser
.Found("s", &lVal
) )
141 s
<< "Size:\t" << lVal
<< '\n';
142 if ( parser
.Found("d", &dt
) )
143 s
<< "Date:\t" << dt
.FormatISODate() << '\n';
144 if ( parser
.Found("project_name", &strVal
) )
145 s
<< "Project:\t" << strVal
<< '\n';
150 #endif // TEST_CMDLINE
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
160 static void TestDirEnumHelper(wxDir
& dir
,
161 int flags
= wxDIR_DEFAULT
,
162 const wxString
& filespec
= wxEmptyString
)
166 if ( !dir
.IsOpened() )
169 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
172 printf("\t%s\n", filename
.c_str());
174 cont
= dir
.GetNext(&filename
);
180 static void TestDirEnum()
182 wxDir
dir(wxGetCwd());
184 puts("Enumerating everything in current directory:");
185 TestDirEnumHelper(dir
);
187 puts("Enumerating really everything in current directory:");
188 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
190 puts("Enumerating object files in current directory:");
191 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
193 puts("Enumerating directories in current directory:");
194 TestDirEnumHelper(dir
, wxDIR_DIRS
);
196 puts("Enumerating files in current directory:");
197 TestDirEnumHelper(dir
, wxDIR_FILES
);
199 puts("Enumerating files including hidden in current directory:");
200 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
204 #elif defined(__WXMSW__)
207 #error "don't know where the root directory is"
210 puts("Enumerating everything in root directory:");
211 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
213 puts("Enumerating directories in root directory:");
214 TestDirEnumHelper(dir
, wxDIR_DIRS
);
216 puts("Enumerating files in root directory:");
217 TestDirEnumHelper(dir
, wxDIR_FILES
);
219 puts("Enumerating files including hidden in root directory:");
220 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
222 puts("Enumerating files in non existing directory:");
223 wxDir
dirNo("nosuchdir");
224 TestDirEnumHelper(dirNo
);
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 #ifdef TEST_DLLLOADER
235 #include <wx/dynlib.h>
237 static void TestDllLoad()
239 #if defined(__WXMSW__)
240 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
241 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
242 #elif defined(__UNIX__)
243 // weird: using just libc.so does *not* work!
244 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
245 static const wxChar
*FUNC_NAME
= _T("strlen");
247 #error "don't know how to test wxDllLoader on this platform"
250 puts("*** testing wxDllLoader ***\n");
252 wxDllType dllHandle
= wxDllLoader::LoadLibrary(LIB_NAME
);
255 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
259 typedef int (*strlenType
)(char *);
260 strlenType pfnStrlen
= (strlenType
)wxDllLoader::GetSymbol(dllHandle
, FUNC_NAME
);
263 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
264 FUNC_NAME
, LIB_NAME
);
268 if ( pfnStrlen("foo") != 3 )
270 wxPrintf(_T("ERROR: loaded function is not strlen()!\n"));
278 wxDllLoader::UnloadLibrary(dllHandle
);
282 #endif // TEST_DLLLOADER
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
290 #include <wx/utils.h>
292 static void TestExecute()
294 puts("*** testing wxExecute ***");
297 #define COMMAND "echo hi"
298 #define SHELL_COMMAND "echo hi from shell"
299 #define REDIRECT_COMMAND "date"
300 #elif defined(__WXMSW__)
301 #define COMMAND "command.com -c 'echo hi'"
302 #define SHELL_COMMAND "echo hi"
303 #define REDIRECT_COMMAND COMMAND
305 #error "no command to exec"
308 printf("Testing wxShell: ");
310 if ( wxShell(SHELL_COMMAND
) )
315 printf("Testing wxExecute: ");
317 if ( wxExecute(COMMAND
, TRUE
/* sync */) == 0 )
322 #if 0 // no, it doesn't work (yet?)
323 printf("Testing async wxExecute: ");
325 if ( wxExecute(COMMAND
) != 0 )
326 puts("Ok (command launched).");
331 printf("Testing wxExecute with redirection:\n");
332 wxArrayString output
;
333 if ( wxExecute(REDIRECT_COMMAND
, output
) != 0 )
339 size_t count
= output
.GetCount();
340 for ( size_t n
= 0; n
< count
; n
++ )
342 printf("\t%s\n", output
[n
].c_str());
349 #endif // TEST_EXECUTE
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
358 #include <wx/textfile.h>
360 static void TestFileRead()
362 puts("*** wxFile read test ***");
364 wxFile
file(_T("testdata.fc"));
365 if ( file
.IsOpened() )
367 printf("File length: %lu\n", file
.Length());
369 puts("File dump:\n----------");
371 static const off_t len
= 1024;
375 off_t nRead
= file
.Read(buf
, len
);
376 if ( nRead
== wxInvalidOffset
)
378 printf("Failed to read the file.");
382 fwrite(buf
, nRead
, 1, stdout
);
392 printf("ERROR: can't open test file.\n");
398 static void TestTextFileRead()
400 puts("*** wxTextFile read test ***");
402 wxTextFile
file(_T("testdata.fc"));
405 printf("Number of lines: %u\n", file
.GetLineCount());
406 printf("Last line: '%s'\n", file
.GetLastLine().c_str());
410 puts("\nDumping the entire file:");
411 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
413 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
415 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
417 puts("\nAnd now backwards:");
418 for ( s
= file
.GetLastLine();
419 file
.GetCurrentLine() != 0;
420 s
= file
.GetPrevLine() )
422 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
424 printf("%6u: %s\n", file
.GetCurrentLine() + 1, s
.c_str());
428 printf("ERROR: can't open '%s'\n", file
.GetName());
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
442 #include <wx/confbase.h>
443 #include <wx/fileconf.h>
445 static const struct FileConfTestData
447 const wxChar
*name
; // value name
448 const wxChar
*value
; // the value from the file
451 { _T("value1"), _T("one") },
452 { _T("value2"), _T("two") },
453 { _T("novalue"), _T("default") },
456 static void TestFileConfRead()
458 puts("*** testing wxFileConfig loading/reading ***");
460 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
461 _T("testdata.fc"), wxEmptyString
,
462 wxCONFIG_USE_RELATIVE_PATH
);
464 // test simple reading
465 puts("\nReading config file:");
466 wxString
defValue(_T("default")), value
;
467 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
469 const FileConfTestData
& data
= fcTestData
[n
];
470 value
= fileconf
.Read(data
.name
, defValue
);
471 printf("\t%s = %s ", data
.name
, value
.c_str());
472 if ( value
== data
.value
)
478 printf("(ERROR: should be %s)\n", data
.value
);
482 // test enumerating the entries
483 puts("\nEnumerating all root entries:");
486 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
489 printf("\t%s = %s\n",
491 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
493 cont
= fileconf
.GetNextEntry(name
, dummy
);
497 #endif // TEST_FILECONF
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
509 Foo(int n_
) { n
= n_
; count
++; }
517 size_t Foo::count
= 0;
519 WX_DECLARE_LIST(Foo
, wxListFoos
);
520 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
522 #include <wx/listimpl.cpp>
524 WX_DEFINE_LIST(wxListFoos
);
526 static void TestHash()
528 puts("*** Testing wxHashTable ***\n");
532 hash
.DeleteContents(TRUE
);
534 printf("Hash created: %u foos in hash, %u foos totally\n",
535 hash
.GetCount(), Foo::count
);
537 static const int hashTestData
[] =
539 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
543 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
545 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
548 printf("Hash filled: %u foos in hash, %u foos totally\n",
549 hash
.GetCount(), Foo::count
);
551 puts("Hash access test:");
552 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
554 printf("\tGetting element with key %d, value %d: ",
556 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
559 printf("ERROR, not found.\n");
563 printf("%d (%s)\n", foo
->n
,
564 (size_t)foo
->n
== n
? "ok" : "ERROR");
568 printf("\nTrying to get an element not in hash: ");
570 if ( hash
.Get(1234) || hash
.Get(1, 0) )
572 puts("ERROR: found!");
576 puts("ok (not found)");
580 printf("Hash destroyed: %u foos left\n", Foo::count
);
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
593 WX_DECLARE_LIST(Bar
, wxListBars
);
594 #include <wx/listimpl.cpp>
595 WX_DEFINE_LIST(wxListBars
);
597 static void TestListCtor()
599 puts("*** Testing wxList construction ***\n");
603 list1
.Append(new Bar(_T("first")));
604 list1
.Append(new Bar(_T("second")));
606 printf("After 1st list creation: %u objects in the list, %u objects total.\n",
607 list1
.GetCount(), Bar::GetNumber());
612 printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n",
613 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
615 list1
.DeleteContents(TRUE
);
618 printf("After list destruction: %u objects left.\n", Bar::GetNumber());
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
629 #include <wx/mimetype.h>
631 static wxMimeTypesManager g_mimeManager
;
633 static void TestMimeEnum()
635 wxArrayString mimetypes
;
637 size_t count
= g_mimeManager
.EnumAllFileTypes(mimetypes
);
639 printf("*** All %u known filetypes: ***\n", count
);
644 for ( size_t n
= 0; n
< count
; n
++ )
646 wxFileType
*filetype
= g_mimeManager
.GetFileTypeFromMimeType(mimetypes
[n
]);
649 printf("nothing known about the filetype '%s'!\n",
650 mimetypes
[n
].c_str());
654 filetype
->GetDescription(&desc
);
655 filetype
->GetExtensions(exts
);
657 filetype
->GetIcon(NULL
);
660 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
667 printf("\t%s: %s (%s)\n",
668 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
672 static void TestMimeOverride()
674 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
676 wxString mailcap
= _T("/tmp/mailcap"),
677 mimetypes
= _T("/tmp/mime.types");
679 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
681 g_mimeManager
.ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
682 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
684 g_mimeManager
.ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
687 static void TestMimeFilename()
689 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
691 static const wxChar
*filenames
[] =
698 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
700 const wxString fname
= filenames
[n
];
701 wxString ext
= fname
.AfterLast(_T('.'));
702 wxFileType
*ft
= g_mimeManager
.GetFileTypeFromExtension(ext
);
705 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
710 if ( !ft
->GetDescription(&desc
) )
711 desc
= _T("<no description>");
714 if ( !ft
->GetOpenCommand(&cmd
,
715 wxFileType::MessageParameters(fname
, _T(""))) )
716 cmd
= _T("<no command available>");
718 wxPrintf(_T("To open %s (%s) do '%s'.\n"),
719 fname
.c_str(), desc
.c_str(), cmd
.c_str());
728 // ----------------------------------------------------------------------------
729 // misc information functions
730 // ----------------------------------------------------------------------------
732 #ifdef TEST_INFO_FUNCTIONS
734 #include <wx/utils.h>
736 static void TestOsInfo()
738 puts("*** Testing OS info functions ***\n");
741 wxGetOsVersion(&major
, &minor
);
742 printf("Running under: %s, version %d.%d\n",
743 wxGetOsDescription().c_str(), major
, minor
);
745 printf("%ld free bytes of memory left.\n", wxGetFreeMemory());
747 printf("Host name is %s (%s).\n",
748 wxGetHostName().c_str(), wxGetFullHostName().c_str());
753 static void TestUserInfo()
755 puts("*** Testing user info functions ***\n");
757 printf("User id is:\t%s\n", wxGetUserId().c_str());
758 printf("User name is:\t%s\n", wxGetUserName().c_str());
759 printf("Home dir is:\t%s\n", wxGetHomeDir().c_str());
760 printf("Email address:\t%s\n", wxGetEmailAddress().c_str());
765 #endif // TEST_INFO_FUNCTIONS
767 // ----------------------------------------------------------------------------
769 // ----------------------------------------------------------------------------
773 #include <wx/longlong.h>
774 #include <wx/timer.h>
776 // make a 64 bit number from 4 16 bit ones
777 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
779 // get a random 64 bit number
780 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
782 #if wxUSE_LONGLONG_WX
783 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
784 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
785 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
786 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
787 #endif // wxUSE_LONGLONG_WX
789 static void TestSpeed()
791 static const long max
= 100000000;
798 for ( n
= 0; n
< max
; n
++ )
803 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
806 #if wxUSE_LONGLONG_NATIVE
811 for ( n
= 0; n
< max
; n
++ )
816 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw
.Time());
818 #endif // wxUSE_LONGLONG_NATIVE
824 for ( n
= 0; n
< max
; n
++ )
829 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
833 static void TestLongLongConversion()
835 puts("*** Testing wxLongLong conversions ***\n");
839 for ( size_t n
= 0; n
< 100000; n
++ )
843 #if wxUSE_LONGLONG_NATIVE
844 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
846 wxASSERT_MSG( a
== b
, "conversions failure" );
848 puts("Can't do it without native long long type, test skipped.");
851 #endif // wxUSE_LONGLONG_NATIVE
853 if ( !(nTested
% 1000) )
865 static void TestMultiplication()
867 puts("*** Testing wxLongLong multiplication ***\n");
871 for ( size_t n
= 0; n
< 100000; n
++ )
876 #if wxUSE_LONGLONG_NATIVE
877 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
878 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
880 wxASSERT_MSG( a
*b
== aa
*bb
, "multiplication failure" );
881 #else // !wxUSE_LONGLONG_NATIVE
882 puts("Can't do it without native long long type, test skipped.");
885 #endif // wxUSE_LONGLONG_NATIVE
887 if ( !(nTested
% 1000) )
899 static void TestDivision()
901 puts("*** Testing wxLongLong division ***\n");
905 for ( size_t n
= 0; n
< 100000; n
++ )
907 // get a random wxLongLong (shifting by 12 the MSB ensures that the
908 // multiplication will not overflow)
909 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
911 // get a random long (not wxLongLong for now) to divide it with
916 #if wxUSE_LONGLONG_NATIVE
917 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
919 wxLongLongNative p
= m
/ l
, s
= m
% l
;
920 wxASSERT_MSG( q
== p
&& r
== s
, "division failure" );
921 #else // !wxUSE_LONGLONG_NATIVE
923 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
924 #endif // wxUSE_LONGLONG_NATIVE
926 if ( !(nTested
% 1000) )
938 static void TestAddition()
940 puts("*** Testing wxLongLong addition ***\n");
944 for ( size_t n
= 0; n
< 100000; n
++ )
950 #if wxUSE_LONGLONG_NATIVE
951 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
952 wxLongLongNative(b
.GetHi(), b
.GetLo()),
953 "addition failure" );
954 #else // !wxUSE_LONGLONG_NATIVE
955 wxASSERT_MSG( c
- b
== a
, "addition failure" );
956 #endif // wxUSE_LONGLONG_NATIVE
958 if ( !(nTested
% 1000) )
970 static void TestBitOperations()
972 puts("*** Testing wxLongLong bit operation ***\n");
976 for ( size_t n
= 0; n
< 100000; n
++ )
980 #if wxUSE_LONGLONG_NATIVE
981 for ( size_t n
= 0; n
< 33; n
++ )
984 #else // !wxUSE_LONGLONG_NATIVE
985 puts("Can't do it without native long long type, test skipped.");
988 #endif // wxUSE_LONGLONG_NATIVE
990 if ( !(nTested
% 1000) )
1002 static void TestLongLongComparison()
1004 puts("*** Testing wxLongLong comparison ***\n");
1006 static const long testLongs
[] =
1017 static const long ls
[2] =
1023 wxLongLongWx lls
[2];
1027 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
1031 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
1033 res
= lls
[m
] > testLongs
[n
];
1034 printf("0x%lx > 0x%lx is %s (%s)\n",
1035 ls
[m
], testLongs
[n
], res
? "true" : "false",
1036 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
1038 res
= lls
[m
] < testLongs
[n
];
1039 printf("0x%lx < 0x%lx is %s (%s)\n",
1040 ls
[m
], testLongs
[n
], res
? "true" : "false",
1041 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
1043 res
= lls
[m
] == testLongs
[n
];
1044 printf("0x%lx == 0x%lx is %s (%s)\n",
1045 ls
[m
], testLongs
[n
], res
? "true" : "false",
1046 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
1054 #endif // TEST_LONGLONG
1056 // ----------------------------------------------------------------------------
1058 // ----------------------------------------------------------------------------
1062 #include <wx/socket.h>
1063 #include <wx/protocol/protocol.h>
1064 #include <wx/protocol/ftp.h>
1065 #include <wx/protocol/http.h>
1067 static void TestSocketServer()
1069 puts("*** Testing wxSocketServer ***\n");
1071 static const int PORT
= 3000;
1076 wxSocketServer
*server
= new wxSocketServer(addr
);
1077 if ( !server
->Ok() )
1079 puts("ERROR: failed to bind");
1086 printf("Server: waiting for connection on port %d...\n", PORT
);
1088 wxSocketBase
*socket
= server
->Accept();
1091 puts("ERROR: wxSocketServer::Accept() failed.");
1095 puts("Server: got a client.");
1097 server
->SetTimeout(60); // 1 min
1099 while ( socket
->IsConnected() )
1105 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
1107 // don't log error if the client just close the connection
1108 if ( socket
->IsConnected() )
1110 puts("ERROR: in wxSocket::Read.");
1130 printf("Server: got '%s'.\n", s
.c_str());
1131 if ( s
== _T("bye") )
1138 socket
->Write(s
.MakeUpper().c_str(), s
.length());
1139 socket
->Write("\r\n", 2);
1140 printf("Server: wrote '%s'.\n", s
.c_str());
1143 puts("Server: lost a client.");
1148 // same as "delete server" but is consistent with GUI programs
1152 static void TestSocketClient()
1154 puts("*** Testing wxSocketClient ***\n");
1156 static const char *hostname
= "www.wxwindows.org";
1159 addr
.Hostname(hostname
);
1162 printf("--- Attempting to connect to %s:80...\n", hostname
);
1164 wxSocketClient client
;
1165 if ( !client
.Connect(addr
) )
1167 printf("ERROR: failed to connect to %s\n", hostname
);
1171 printf("--- Connected to %s:%u...\n",
1172 addr
.Hostname().c_str(), addr
.Service());
1176 // could use simply "GET" here I suppose
1178 wxString::Format("GET http://%s/\r\n", hostname
);
1179 client
.Write(cmdGet
, cmdGet
.length());
1180 printf("--- Sent command '%s' to the server\n",
1181 MakePrintable(cmdGet
).c_str());
1182 client
.Read(buf
, WXSIZEOF(buf
));
1183 printf("--- Server replied:\n%s", buf
);
1187 static void TestProtocolFtp()
1189 puts("*** Testing wxFTP download ***\n");
1191 wxLog::AddTraceMask(_T("ftp"));
1193 static const char *hostname
= "ftp.wxwindows.org";
1195 printf("--- Attempting to connect to %s:21...\n", hostname
);
1198 if ( !ftp
.Connect(hostname
) )
1200 printf("ERROR: failed to connect to %s\n", hostname
);
1204 printf("--- Connected to %s, current directory is '%s'\n",
1205 hostname
, ftp
.Pwd().c_str());
1206 if ( !ftp
.ChDir(_T("pub")) )
1208 puts("ERROR: failed to cd to pub");
1211 wxArrayString files
;
1212 if ( !ftp
.GetList(files
) )
1214 puts("ERROR: failed to get list of files");
1218 printf("List of files under '%s':\n", ftp
.Pwd().c_str());
1219 size_t count
= files
.GetCount();
1220 for ( size_t n
= 0; n
< count
; n
++ )
1222 printf("\t%s\n", files
[n
].c_str());
1224 puts("End of the file list");
1227 if ( !ftp
.ChDir(_T("..")) )
1229 puts("ERROR: failed to cd to ..");
1232 static const char *filename
= "welcome.msg";
1233 wxInputStream
*in
= ftp
.GetInputStream(filename
);
1236 puts("ERROR: couldn't get input stream");
1240 size_t size
= in
->StreamSize();
1241 printf("Reading file %s (%u bytes)...", filename
, size
);
1243 char *data
= new char[size
];
1244 if ( !in
->Read(data
, size
) )
1246 puts("ERROR: read error");
1250 printf("\nContents of %s:\n%s\n", filename
, data
);
1259 static void TestProtocolFtpUpload()
1261 puts("*** Testing wxFTP uploading ***\n");
1263 wxLog::AddTraceMask(_T("ftp"));
1265 static const char *hostname
= "localhost";
1267 printf("--- Attempting to connect to %s:21...\n", hostname
);
1270 ftp
.SetUser("zeitlin");
1271 ftp
.SetPassword("insert your password here");
1272 if ( !ftp
.Connect(hostname
) )
1274 printf("ERROR: failed to connect to %s\n", hostname
);
1278 printf("--- Connected to %s, current directory is '%s'\n",
1279 hostname
, ftp
.Pwd().c_str());
1282 static const char *file1
= "test1";
1283 static const char *file2
= "test2";
1284 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
1287 printf("--- Uploading to %s ---\n", file1
);
1288 out
->Write("First hello", 11);
1292 out
= ftp
.GetOutputStream(file2
);
1295 printf("--- Uploading to %s ---\n", file1
);
1296 out
->Write("Second hello", 12);
1302 #endif // TEST_SOCKETS
1304 // ----------------------------------------------------------------------------
1306 // ----------------------------------------------------------------------------
1310 #include <wx/timer.h>
1311 #include <wx/utils.h>
1313 static void TestStopWatch()
1315 puts("*** Testing wxStopWatch ***\n");
1318 printf("Sleeping 3 seconds...");
1320 printf("\telapsed time: %ldms\n", sw
.Time());
1323 printf("Sleeping 2 more seconds...");
1325 printf("\telapsed time: %ldms\n", sw
.Time());
1328 printf("And 3 more seconds...");
1330 printf("\telapsed time: %ldms\n", sw
.Time());
1333 puts("\nChecking for 'backwards clock' bug...");
1334 for ( size_t n
= 0; n
< 70; n
++ )
1338 for ( size_t m
= 0; m
< 100000; m
++ )
1340 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
1342 puts("\ntime is negative - ERROR!");
1352 #endif // TEST_TIMER
1354 // ----------------------------------------------------------------------------
1356 // ----------------------------------------------------------------------------
1360 #include <wx/vcard.h>
1362 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
1365 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
1369 wxString(_T('\t'), level
).c_str(),
1370 vcObj
->GetName().c_str());
1373 switch ( vcObj
->GetType() )
1375 case wxVCardObject::String
:
1376 case wxVCardObject::UString
:
1379 vcObj
->GetValue(&val
);
1380 value
<< _T('"') << val
<< _T('"');
1384 case wxVCardObject::Int
:
1387 vcObj
->GetValue(&i
);
1388 value
.Printf(_T("%u"), i
);
1392 case wxVCardObject::Long
:
1395 vcObj
->GetValue(&l
);
1396 value
.Printf(_T("%lu"), l
);
1400 case wxVCardObject::None
:
1403 case wxVCardObject::Object
:
1404 value
= _T("<node>");
1408 value
= _T("<unknown value type>");
1412 printf(" = %s", value
.c_str());
1415 DumpVObject(level
+ 1, *vcObj
);
1418 vcObj
= vcard
.GetNextProp(&cookie
);
1422 static void DumpVCardAddresses(const wxVCard
& vcard
)
1424 puts("\nShowing all addresses from vCard:\n");
1428 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
1432 int flags
= addr
->GetFlags();
1433 if ( flags
& wxVCardAddress::Domestic
)
1435 flagsStr
<< _T("domestic ");
1437 if ( flags
& wxVCardAddress::Intl
)
1439 flagsStr
<< _T("international ");
1441 if ( flags
& wxVCardAddress::Postal
)
1443 flagsStr
<< _T("postal ");
1445 if ( flags
& wxVCardAddress::Parcel
)
1447 flagsStr
<< _T("parcel ");
1449 if ( flags
& wxVCardAddress::Home
)
1451 flagsStr
<< _T("home ");
1453 if ( flags
& wxVCardAddress::Work
)
1455 flagsStr
<< _T("work ");
1458 printf("Address %u:\n"
1460 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
1463 addr
->GetPostOffice().c_str(),
1464 addr
->GetExtAddress().c_str(),
1465 addr
->GetStreet().c_str(),
1466 addr
->GetLocality().c_str(),
1467 addr
->GetRegion().c_str(),
1468 addr
->GetPostalCode().c_str(),
1469 addr
->GetCountry().c_str()
1473 addr
= vcard
.GetNextAddress(&cookie
);
1477 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
1479 puts("\nShowing all phone numbers from vCard:\n");
1483 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
1487 int flags
= phone
->GetFlags();
1488 if ( flags
& wxVCardPhoneNumber::Voice
)
1490 flagsStr
<< _T("voice ");
1492 if ( flags
& wxVCardPhoneNumber::Fax
)
1494 flagsStr
<< _T("fax ");
1496 if ( flags
& wxVCardPhoneNumber::Cellular
)
1498 flagsStr
<< _T("cellular ");
1500 if ( flags
& wxVCardPhoneNumber::Modem
)
1502 flagsStr
<< _T("modem ");
1504 if ( flags
& wxVCardPhoneNumber::Home
)
1506 flagsStr
<< _T("home ");
1508 if ( flags
& wxVCardPhoneNumber::Work
)
1510 flagsStr
<< _T("work ");
1513 printf("Phone number %u:\n"
1518 phone
->GetNumber().c_str()
1522 phone
= vcard
.GetNextPhoneNumber(&cookie
);
1526 static void TestVCardRead()
1528 puts("*** Testing wxVCard reading ***\n");
1530 wxVCard
vcard(_T("vcard.vcf"));
1531 if ( !vcard
.IsOk() )
1533 puts("ERROR: couldn't load vCard.");
1537 // read individual vCard properties
1538 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
1542 vcObj
->GetValue(&value
);
1547 value
= _T("<none>");
1550 printf("Full name retrieved directly: %s\n", value
.c_str());
1553 if ( !vcard
.GetFullName(&value
) )
1555 value
= _T("<none>");
1558 printf("Full name from wxVCard API: %s\n", value
.c_str());
1560 // now show how to deal with multiply occuring properties
1561 DumpVCardAddresses(vcard
);
1562 DumpVCardPhoneNumbers(vcard
);
1564 // and finally show all
1565 puts("\nNow dumping the entire vCard:\n"
1566 "-----------------------------\n");
1568 DumpVObject(0, vcard
);
1572 static void TestVCardWrite()
1574 puts("*** Testing wxVCard writing ***\n");
1577 if ( !vcard
.IsOk() )
1579 puts("ERROR: couldn't create vCard.");
1584 vcard
.SetName("Zeitlin", "Vadim");
1585 vcard
.SetFullName("Vadim Zeitlin");
1586 vcard
.SetOrganization("wxWindows", "R&D");
1588 // just dump the vCard back
1589 puts("Entire vCard follows:\n");
1590 puts(vcard
.Write());
1594 #endif // TEST_VCARD
1596 // ----------------------------------------------------------------------------
1597 // wide char (Unicode) support
1598 // ----------------------------------------------------------------------------
1602 #include <wx/strconv.h>
1603 #include <wx/buffer.h>
1605 static void TestUtf8()
1607 puts("*** Testing UTF8 support ***\n");
1609 wxString testString
= "français";
1611 "************ French - Français ****************"
1612 "Juste un petit exemple pour dire que les français aussi"
1613 "ont à cœur de pouvoir utiliser tous leurs caractères ! :)";
1616 wxWCharBuffer wchBuf
= testString
.wc_str(wxConvUTF8
);
1617 const wchar_t *pwz
= (const wchar_t *)wchBuf
;
1618 wxString
testString2(pwz
, wxConvLocal
);
1620 printf("Decoding '%s' => '%s'\n", testString
.c_str(), testString2
.c_str());
1622 char *psz
= "fran" "\xe7" "ais";
1623 size_t len
= strlen(psz
);
1624 wchar_t *pwz2
= new wchar_t[len
+ 1];
1625 for ( size_t n
= 0; n
<= len
; n
++ )
1627 pwz2
[n
] = (wchar_t)(unsigned char)psz
[n
];
1630 wxString
testString3(pwz2
, wxConvUTF8
);
1633 printf("Encoding '%s' -> '%s'\n", psz
, testString3
.c_str());
1636 #endif // TEST_WCHAR
1638 // ----------------------------------------------------------------------------
1640 // ----------------------------------------------------------------------------
1644 #include "wx/zipstrm.h"
1646 static void TestZipStreamRead()
1648 puts("*** Testing ZIP reading ***\n");
1650 wxZipInputStream
istr(_T("idx.zip"), _T("IDX.txt"));
1651 printf("Archive size: %u\n", istr
.GetSize());
1653 puts("Dumping the file:");
1654 while ( !istr
.Eof() )
1656 putchar(istr
.GetC());
1660 puts("\n----- done ------");
1665 // ----------------------------------------------------------------------------
1667 // ----------------------------------------------------------------------------
1671 #include <wx/zstream.h>
1672 #include <wx/wfstream.h>
1674 static const wxChar
*FILENAME_GZ
= _T("test.gz");
1675 static const char *TEST_DATA
= "hello and hello again";
1677 static void TestZlibStreamWrite()
1679 puts("*** Testing Zlib stream reading ***\n");
1681 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
1682 wxZlibOutputStream
ostr(fileOutStream
, 0);
1683 printf("Compressing the test string... ");
1684 ostr
.Write(TEST_DATA
, sizeof(TEST_DATA
));
1687 puts("(ERROR: failed)");
1694 puts("\n----- done ------");
1697 static void TestZlibStreamRead()
1699 puts("*** Testing Zlib stream reading ***\n");
1701 wxFileInputStream
fileInStream(FILENAME_GZ
);
1702 wxZlibInputStream
istr(fileInStream
);
1703 printf("Archive size: %u\n", istr
.GetSize());
1705 puts("Dumping the file:");
1706 while ( !istr
.Eof() )
1708 putchar(istr
.GetC());
1712 puts("\n----- done ------");
1717 // ----------------------------------------------------------------------------
1719 // ----------------------------------------------------------------------------
1721 #ifdef TEST_DATETIME
1723 #include <wx/date.h>
1725 #include <wx/datetime.h>
1730 wxDateTime::wxDateTime_t day
;
1731 wxDateTime::Month month
;
1733 wxDateTime::wxDateTime_t hour
, min
, sec
;
1735 wxDateTime::WeekDay wday
;
1736 time_t gmticks
, ticks
;
1738 void Init(const wxDateTime::Tm
& tm
)
1747 gmticks
= ticks
= -1;
1750 wxDateTime
DT() const
1751 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
1753 bool SameDay(const wxDateTime::Tm
& tm
) const
1755 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
1758 wxString
Format() const
1761 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
1763 wxDateTime::GetMonthName(month
).c_str(),
1765 abs(wxDateTime::ConvertYearToBC(year
)),
1766 year
> 0 ? "AD" : "BC");
1770 wxString
FormatDate() const
1773 s
.Printf("%02d-%s-%4d%s",
1775 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
1776 abs(wxDateTime::ConvertYearToBC(year
)),
1777 year
> 0 ? "AD" : "BC");
1782 static const Date testDates
[] =
1784 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
1785 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
1786 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
1787 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
1788 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
1789 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
1790 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
1791 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
1792 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
1793 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
1794 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
1795 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
1796 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
1797 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
1798 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
1801 // this test miscellaneous static wxDateTime functions
1802 static void TestTimeStatic()
1804 puts("\n*** wxDateTime static methods test ***");
1806 // some info about the current date
1807 int year
= wxDateTime::GetCurrentYear();
1808 printf("Current year %d is %sa leap one and has %d days.\n",
1810 wxDateTime::IsLeapYear(year
) ? "" : "not ",
1811 wxDateTime::GetNumberOfDays(year
));
1813 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
1814 printf("Current month is '%s' ('%s') and it has %d days\n",
1815 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
1816 wxDateTime::GetMonthName(month
).c_str(),
1817 wxDateTime::GetNumberOfDays(month
));
1820 static const size_t nYears
= 5;
1821 static const size_t years
[2][nYears
] =
1823 // first line: the years to test
1824 { 1990, 1976, 2000, 2030, 1984, },
1826 // second line: TRUE if leap, FALSE otherwise
1827 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
1830 for ( size_t n
= 0; n
< nYears
; n
++ )
1832 int year
= years
[0][n
];
1833 bool should
= years
[1][n
] != 0,
1834 is
= wxDateTime::IsLeapYear(year
);
1836 printf("Year %d is %sa leap year (%s)\n",
1839 should
== is
? "ok" : "ERROR");
1841 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
1845 // test constructing wxDateTime objects
1846 static void TestTimeSet()
1848 puts("\n*** wxDateTime construction test ***");
1850 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1852 const Date
& d1
= testDates
[n
];
1853 wxDateTime dt
= d1
.DT();
1856 d2
.Init(dt
.GetTm());
1858 wxString s1
= d1
.Format(),
1861 printf("Date: %s == %s (%s)\n",
1862 s1
.c_str(), s2
.c_str(),
1863 s1
== s2
? "ok" : "ERROR");
1867 // test time zones stuff
1868 static void TestTimeZones()
1870 puts("\n*** wxDateTime timezone test ***");
1872 wxDateTime now
= wxDateTime::Now();
1874 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
1875 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
1876 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
1877 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
1878 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
1879 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
1881 wxDateTime::Tm tm
= now
.GetTm();
1882 if ( wxDateTime(tm
) != now
)
1884 printf("ERROR: got %s instead of %s\n",
1885 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
1889 // test some minimal support for the dates outside the standard range
1890 static void TestTimeRange()
1892 puts("\n*** wxDateTime out-of-standard-range dates test ***");
1894 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
1896 printf("Unix epoch:\t%s\n",
1897 wxDateTime(2440587.5).Format(fmt
).c_str());
1898 printf("Feb 29, 0: \t%s\n",
1899 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
1900 printf("JDN 0: \t%s\n",
1901 wxDateTime(0.0).Format(fmt
).c_str());
1902 printf("Jan 1, 1AD:\t%s\n",
1903 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
1904 printf("May 29, 2099:\t%s\n",
1905 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
1908 static void TestTimeTicks()
1910 puts("\n*** wxDateTime ticks test ***");
1912 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1914 const Date
& d
= testDates
[n
];
1915 if ( d
.ticks
== -1 )
1918 wxDateTime dt
= d
.DT();
1919 long ticks
= (dt
.GetValue() / 1000).ToLong();
1920 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1921 if ( ticks
== d
.ticks
)
1927 printf(" (ERROR: should be %ld, delta = %ld)\n",
1928 d
.ticks
, ticks
- d
.ticks
);
1931 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
1932 ticks
= (dt
.GetValue() / 1000).ToLong();
1933 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
1934 if ( ticks
== d
.gmticks
)
1940 printf(" (ERROR: should be %ld, delta = %ld)\n",
1941 d
.gmticks
, ticks
- d
.gmticks
);
1948 // test conversions to JDN &c
1949 static void TestTimeJDN()
1951 puts("\n*** wxDateTime to JDN test ***");
1953 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1955 const Date
& d
= testDates
[n
];
1956 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
1957 double jdn
= dt
.GetJulianDayNumber();
1959 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
1966 printf(" (ERROR: should be %f, delta = %f)\n",
1967 d
.jdn
, jdn
- d
.jdn
);
1972 // test week days computation
1973 static void TestTimeWDays()
1975 puts("\n*** wxDateTime weekday test ***");
1977 // test GetWeekDay()
1979 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
1981 const Date
& d
= testDates
[n
];
1982 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
1984 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
1987 wxDateTime::GetWeekDayName(wday
).c_str());
1988 if ( wday
== d
.wday
)
1994 printf(" (ERROR: should be %s)\n",
1995 wxDateTime::GetWeekDayName(d
.wday
).c_str());
2001 // test SetToWeekDay()
2002 struct WeekDateTestData
2004 Date date
; // the real date (precomputed)
2005 int nWeek
; // its week index in the month
2006 wxDateTime::WeekDay wday
; // the weekday
2007 wxDateTime::Month month
; // the month
2008 int year
; // and the year
2010 wxString
Format() const
2013 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
2015 case 1: which
= "first"; break;
2016 case 2: which
= "second"; break;
2017 case 3: which
= "third"; break;
2018 case 4: which
= "fourth"; break;
2019 case 5: which
= "fifth"; break;
2021 case -1: which
= "last"; break;
2026 which
+= " from end";
2029 s
.Printf("The %s %s of %s in %d",
2031 wxDateTime::GetWeekDayName(wday
).c_str(),
2032 wxDateTime::GetMonthName(month
).c_str(),
2039 // the array data was generated by the following python program
2041 from DateTime import *
2042 from whrandom import *
2043 from string import *
2045 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2046 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2048 week = DateTimeDelta(7)
2051 year = randint(1900, 2100)
2052 month = randint(1, 12)
2053 day = randint(1, 28)
2054 dt = DateTime(year, month, day)
2055 wday = dt.day_of_week
2057 countFromEnd = choice([-1, 1])
2060 while dt.month is month:
2061 dt = dt - countFromEnd * week
2062 weekNum = weekNum + countFromEnd
2064 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
2066 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
2067 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
2070 static const WeekDateTestData weekDatesTestData
[] =
2072 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
2073 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
2074 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
2075 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
2076 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
2077 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
2078 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
2079 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
2080 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
2081 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
2082 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
2083 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
2084 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
2085 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
2086 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
2087 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
2088 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
2089 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
2090 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
2091 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
2094 static const char *fmt
= "%d-%b-%Y";
2097 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
2099 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
2101 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
2103 printf("%s is %s", wd
.Format().c_str(), dt
.Format(fmt
).c_str());
2105 const Date
& d
= wd
.date
;
2106 if ( d
.SameDay(dt
.GetTm()) )
2112 dt
.Set(d
.day
, d
.month
, d
.year
);
2114 printf(" (ERROR: should be %s)\n", dt
.Format(fmt
).c_str());
2119 // test the computation of (ISO) week numbers
2120 static void TestTimeWNumber()
2122 puts("\n*** wxDateTime week number test ***");
2124 struct WeekNumberTestData
2126 Date date
; // the date
2127 wxDateTime::wxDateTime_t week
; // the week number in the year
2128 wxDateTime::wxDateTime_t wmon
; // the week number in the month
2129 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
2130 wxDateTime::wxDateTime_t dnum
; // day number in the year
2133 // data generated with the following python script:
2135 from DateTime import *
2136 from whrandom import *
2137 from string import *
2139 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2140 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2142 def GetMonthWeek(dt):
2143 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
2144 if weekNumMonth < 0:
2145 weekNumMonth = weekNumMonth + 53
2148 def GetLastSundayBefore(dt):
2149 if dt.iso_week[2] == 7:
2152 return dt - DateTimeDelta(dt.iso_week[2])
2155 year = randint(1900, 2100)
2156 month = randint(1, 12)
2157 day = randint(1, 28)
2158 dt = DateTime(year, month, day)
2159 dayNum = dt.day_of_year
2160 weekNum = dt.iso_week[1]
2161 weekNumMonth = GetMonthWeek(dt)
2164 dtSunday = GetLastSundayBefore(dt)
2166 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
2167 weekNumMonth2 = weekNumMonth2 + 1
2168 dtSunday = dtSunday - DateTimeDelta(7)
2170 data = { 'day': rjust(`day`, 2), \
2171 'month': monthNames[month - 1], \
2173 'weekNum': rjust(`weekNum`, 2), \
2174 'weekNumMonth': weekNumMonth, \
2175 'weekNumMonth2': weekNumMonth2, \
2176 'dayNum': rjust(`dayNum`, 3) }
2178 print " { { %(day)s, "\
2179 "wxDateTime::%(month)s, "\
2182 "%(weekNumMonth)s, "\
2183 "%(weekNumMonth2)s, "\
2184 "%(dayNum)s }," % data
2187 static const WeekNumberTestData weekNumberTestDates
[] =
2189 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
2190 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
2191 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
2192 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
2193 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
2194 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
2195 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
2196 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
2197 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
2198 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
2199 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
2200 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
2201 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
2202 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
2203 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
2204 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
2205 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
2206 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
2207 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
2208 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
2211 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
2213 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
2214 const Date
& d
= wn
.date
;
2216 wxDateTime dt
= d
.DT();
2218 wxDateTime::wxDateTime_t
2219 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
2220 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
2221 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
2222 dnum
= dt
.GetDayOfYear();
2224 printf("%s: the day number is %d",
2225 d
.FormatDate().c_str(), dnum
);
2226 if ( dnum
== wn
.dnum
)
2232 printf(" (ERROR: should be %d)", wn
.dnum
);
2235 printf(", week in month is %d", wmon
);
2236 if ( wmon
== wn
.wmon
)
2242 printf(" (ERROR: should be %d)", wn
.wmon
);
2245 printf(" or %d", wmon2
);
2246 if ( wmon2
== wn
.wmon2
)
2252 printf(" (ERROR: should be %d)", wn
.wmon2
);
2255 printf(", week in year is %d", week
);
2256 if ( week
== wn
.week
)
2262 printf(" (ERROR: should be %d)\n", wn
.week
);
2267 // test DST calculations
2268 static void TestTimeDST()
2270 puts("\n*** wxDateTime DST test ***");
2272 printf("DST is%s in effect now.\n\n",
2273 wxDateTime::Now().IsDST() ? "" : " not");
2275 // taken from http://www.energy.ca.gov/daylightsaving.html
2276 static const Date datesDST
[2][2004 - 1900 + 1] =
2279 { 1, wxDateTime::Apr
, 1990 },
2280 { 7, wxDateTime::Apr
, 1991 },
2281 { 5, wxDateTime::Apr
, 1992 },
2282 { 4, wxDateTime::Apr
, 1993 },
2283 { 3, wxDateTime::Apr
, 1994 },
2284 { 2, wxDateTime::Apr
, 1995 },
2285 { 7, wxDateTime::Apr
, 1996 },
2286 { 6, wxDateTime::Apr
, 1997 },
2287 { 5, wxDateTime::Apr
, 1998 },
2288 { 4, wxDateTime::Apr
, 1999 },
2289 { 2, wxDateTime::Apr
, 2000 },
2290 { 1, wxDateTime::Apr
, 2001 },
2291 { 7, wxDateTime::Apr
, 2002 },
2292 { 6, wxDateTime::Apr
, 2003 },
2293 { 4, wxDateTime::Apr
, 2004 },
2296 { 28, wxDateTime::Oct
, 1990 },
2297 { 27, wxDateTime::Oct
, 1991 },
2298 { 25, wxDateTime::Oct
, 1992 },
2299 { 31, wxDateTime::Oct
, 1993 },
2300 { 30, wxDateTime::Oct
, 1994 },
2301 { 29, wxDateTime::Oct
, 1995 },
2302 { 27, wxDateTime::Oct
, 1996 },
2303 { 26, wxDateTime::Oct
, 1997 },
2304 { 25, wxDateTime::Oct
, 1998 },
2305 { 31, wxDateTime::Oct
, 1999 },
2306 { 29, wxDateTime::Oct
, 2000 },
2307 { 28, wxDateTime::Oct
, 2001 },
2308 { 27, wxDateTime::Oct
, 2002 },
2309 { 26, wxDateTime::Oct
, 2003 },
2310 { 31, wxDateTime::Oct
, 2004 },
2315 for ( year
= 1990; year
< 2005; year
++ )
2317 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
2318 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
2320 printf("DST period in the US for year %d: from %s to %s",
2321 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
2323 size_t n
= year
- 1990;
2324 const Date
& dBegin
= datesDST
[0][n
];
2325 const Date
& dEnd
= datesDST
[1][n
];
2327 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
2333 printf(" (ERROR: should be %s %d to %s %d)\n",
2334 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
2335 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
2341 for ( year
= 1990; year
< 2005; year
++ )
2343 printf("DST period in Europe for year %d: from %s to %s\n",
2345 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
2346 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
2350 // test wxDateTime -> text conversion
2351 static void TestTimeFormat()
2353 puts("\n*** wxDateTime formatting test ***");
2355 // some information may be lost during conversion, so store what kind
2356 // of info should we recover after a round trip
2359 CompareNone
, // don't try comparing
2360 CompareBoth
, // dates and times should be identical
2361 CompareDate
, // dates only
2362 CompareTime
// time only
2367 CompareKind compareKind
;
2369 } formatTestFormats
[] =
2371 { CompareBoth
, "---> %c" },
2372 { CompareDate
, "Date is %A, %d of %B, in year %Y" },
2373 { CompareBoth
, "Date is %x, time is %X" },
2374 { CompareTime
, "Time is %H:%M:%S or %I:%M:%S %p" },
2375 { CompareNone
, "The day of year: %j, the week of year: %W" },
2376 { CompareDate
, "ISO date without separators: %4Y%2m%2d" },
2379 static const Date formatTestDates
[] =
2381 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
2382 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
2384 // this test can't work for other centuries because it uses two digit
2385 // years in formats, so don't even try it
2386 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
2387 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
2388 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
2392 // an extra test (as it doesn't depend on date, don't do it in the loop)
2393 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
2395 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
2399 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
2400 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
2402 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
2403 printf("%s", s
.c_str());
2405 // what can we recover?
2406 int kind
= formatTestFormats
[n
].compareKind
;
2410 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
2413 // converion failed - should it have?
2414 if ( kind
== CompareNone
)
2417 puts(" (ERROR: conversion back failed)");
2421 // should have parsed the entire string
2422 puts(" (ERROR: conversion back stopped too soon)");
2426 bool equal
= FALSE
; // suppress compilaer warning
2434 equal
= dt
.IsSameDate(dt2
);
2438 equal
= dt
.IsSameTime(dt2
);
2444 printf(" (ERROR: got back '%s' instead of '%s')\n",
2445 dt2
.Format().c_str(), dt
.Format().c_str());
2456 // test text -> wxDateTime conversion
2457 static void TestTimeParse()
2459 puts("\n*** wxDateTime parse test ***");
2461 struct ParseTestData
2468 static const ParseTestData parseTestDates
[] =
2470 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, TRUE
},
2471 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, TRUE
},
2474 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
2476 const char *format
= parseTestDates
[n
].format
;
2478 printf("%s => ", format
);
2481 if ( dt
.ParseRfc822Date(format
) )
2483 printf("%s ", dt
.Format().c_str());
2485 if ( parseTestDates
[n
].good
)
2487 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
2494 printf("(ERROR: should be %s)\n", dtReal
.Format().c_str());
2499 puts("(ERROR: bad format)");
2504 printf("bad format (%s)\n",
2505 parseTestDates
[n
].good
? "ERROR" : "ok");
2510 static void TestInteractive()
2512 puts("\n*** interactive wxDateTime tests ***");
2518 printf("Enter a date: ");
2519 if ( !fgets(buf
, WXSIZEOF(buf
), stdin
) )
2522 // kill the last '\n'
2523 buf
[strlen(buf
) - 1] = 0;
2526 const char *p
= dt
.ParseDate(buf
);
2529 printf("ERROR: failed to parse the date '%s'.\n", buf
);
2535 printf("WARNING: parsed only first %u characters.\n", p
- buf
);
2538 printf("%s: day %u, week of month %u/%u, week of year %u\n",
2539 dt
.Format("%b %d, %Y").c_str(),
2541 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
2542 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
2543 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
2546 puts("\n*** done ***");
2549 static void TestTimeMS()
2551 puts("*** testing millisecond-resolution support in wxDateTime ***");
2553 wxDateTime dt1
= wxDateTime::Now(),
2554 dt2
= wxDateTime::UNow();
2556 printf("Now = %s\n", dt1
.Format("%H:%M:%S:%l").c_str());
2557 printf("UNow = %s\n", dt2
.Format("%H:%M:%S:%l").c_str());
2558 printf("Dummy loop: ");
2559 for ( int i
= 0; i
< 6000; i
++ )
2561 //for ( int j = 0; j < 10; j++ )
2564 s
.Printf("%g", sqrt(i
));
2573 dt2
= wxDateTime::UNow();
2574 printf("UNow = %s\n", dt2
.Format("%H:%M:%S:%l").c_str());
2576 printf("Loop executed in %s ms\n", (dt2
- dt1
).Format("%l").c_str());
2578 puts("\n*** done ***");
2581 static void TestTimeArithmetics()
2583 puts("\n*** testing arithmetic operations on wxDateTime ***");
2585 static const struct ArithmData
2587 ArithmData(const wxDateSpan
& sp
, const char *nam
)
2588 : span(sp
), name(nam
) { }
2592 } testArithmData
[] =
2594 ArithmData(wxDateSpan::Day(), "day"),
2595 ArithmData(wxDateSpan::Week(), "week"),
2596 ArithmData(wxDateSpan::Month(), "month"),
2597 ArithmData(wxDateSpan::Year(), "year"),
2598 ArithmData(wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days"),
2601 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
2603 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
2605 wxDateSpan span
= testArithmData
[n
].span
;
2609 const char *name
= testArithmData
[n
].name
;
2610 printf("%s + %s = %s, %s - %s = %s\n",
2611 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
2612 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
2614 printf("Going back: %s", (dt1
- span
).FormatISODate().c_str());
2615 if ( dt1
- span
== dt
)
2621 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
2624 printf("Going forward: %s", (dt2
+ span
).FormatISODate().c_str());
2625 if ( dt2
+ span
== dt
)
2631 printf(" (ERROR: should be %s)\n", dt
.FormatISODate().c_str());
2634 printf("Double increment: %s", (dt2
+ 2*span
).FormatISODate().c_str());
2635 if ( dt2
+ 2*span
== dt1
)
2641 printf(" (ERROR: should be %s)\n", dt2
.FormatISODate().c_str());
2648 static void TestTimeHolidays()
2650 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
2652 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
2653 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
2654 dtEnd
= dtStart
.GetLastMonthDay();
2656 wxDateTimeArray hol
;
2657 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
2659 const wxChar
*format
= "%d-%b-%Y (%a)";
2661 printf("All holidays between %s and %s:\n",
2662 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
2664 size_t count
= hol
.GetCount();
2665 for ( size_t n
= 0; n
< count
; n
++ )
2667 printf("\t%s\n", hol
[n
].Format(format
).c_str());
2673 static void TestTimeZoneBug()
2675 puts("\n*** testing for DST/timezone bug ***\n");
2677 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
2678 for ( int i
= 0; i
< 31; i
++ )
2680 printf("Date %s: week day %s.\n",
2681 date
.Format(_T("%d-%m-%Y")).c_str(),
2682 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
2684 date
+= wxDateSpan::Day();
2692 // test compatibility with the old wxDate/wxTime classes
2693 static void TestTimeCompatibility()
2695 puts("\n*** wxDateTime compatibility test ***");
2697 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
2698 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
2700 double jdnNow
= wxDateTime::Now().GetJDN();
2701 long jdnMidnight
= (long)(jdnNow
- 0.5);
2702 printf("wxDate for today: %s\n", wxDate(jdnMidnight
).FormatDate().c_str());
2704 jdnMidnight
= wxDate().Set().GetJulianDate();
2705 printf("wxDateTime for today: %s\n",
2706 wxDateTime((double)(jdnMidnight
+ 0.5)).Format("%c", wxDateTime::GMT0
).c_str());
2708 int flags
= wxEUROPEAN
;//wxFULL;
2711 printf("Today is %s\n", date
.FormatDate(flags
).c_str());
2712 for ( int n
= 0; n
< 7; n
++ )
2714 printf("Previous %s is %s\n",
2715 wxDateTime::GetWeekDayName((wxDateTime::WeekDay
)n
),
2716 date
.Previous(n
+ 1).FormatDate(flags
).c_str());
2722 #endif // TEST_DATETIME
2724 // ----------------------------------------------------------------------------
2726 // ----------------------------------------------------------------------------
2730 #include <wx/thread.h>
2732 static size_t gs_counter
= (size_t)-1;
2733 static wxCriticalSection gs_critsect
;
2734 static wxCondition gs_cond
;
2736 class MyJoinableThread
: public wxThread
2739 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
2740 { m_n
= n
; Create(); }
2742 // thread execution starts here
2743 virtual ExitCode
Entry();
2749 wxThread::ExitCode
MyJoinableThread::Entry()
2751 unsigned long res
= 1;
2752 for ( size_t n
= 1; n
< m_n
; n
++ )
2756 // it's a loooong calculation :-)
2760 return (ExitCode
)res
;
2763 class MyDetachedThread
: public wxThread
2766 MyDetachedThread(size_t n
, char ch
)
2770 m_cancelled
= FALSE
;
2775 // thread execution starts here
2776 virtual ExitCode
Entry();
2779 virtual void OnExit();
2782 size_t m_n
; // number of characters to write
2783 char m_ch
; // character to write
2785 bool m_cancelled
; // FALSE if we exit normally
2788 wxThread::ExitCode
MyDetachedThread::Entry()
2791 wxCriticalSectionLocker
lock(gs_critsect
);
2792 if ( gs_counter
== (size_t)-1 )
2798 for ( size_t n
= 0; n
< m_n
; n
++ )
2800 if ( TestDestroy() )
2810 wxThread::Sleep(100);
2816 void MyDetachedThread::OnExit()
2818 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
2820 wxCriticalSectionLocker
lock(gs_critsect
);
2821 if ( !--gs_counter
&& !m_cancelled
)
2825 void TestDetachedThreads()
2827 puts("\n*** Testing detached threads ***");
2829 static const size_t nThreads
= 3;
2830 MyDetachedThread
*threads
[nThreads
];
2832 for ( n
= 0; n
< nThreads
; n
++ )
2834 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
2837 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
2838 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
2840 for ( n
= 0; n
< nThreads
; n
++ )
2845 // wait until all threads terminate
2851 void TestJoinableThreads()
2853 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
2855 // calc 10! in the background
2856 MyJoinableThread
thread(10);
2859 printf("\nThread terminated with exit code %lu.\n",
2860 (unsigned long)thread
.Wait());
2863 void TestThreadSuspend()
2865 puts("\n*** Testing thread suspend/resume functions ***");
2867 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
2871 // this is for this demo only, in a real life program we'd use another
2872 // condition variable which would be signaled from wxThread::Entry() to
2873 // tell us that the thread really started running - but here just wait a
2874 // bit and hope that it will be enough (the problem is, of course, that
2875 // the thread might still not run when we call Pause() which will result
2877 wxThread::Sleep(300);
2879 for ( size_t n
= 0; n
< 3; n
++ )
2883 puts("\nThread suspended");
2886 // don't sleep but resume immediately the first time
2887 wxThread::Sleep(300);
2889 puts("Going to resume the thread");
2894 puts("Waiting until it terminates now");
2896 // wait until the thread terminates
2902 void TestThreadDelete()
2904 // As above, using Sleep() is only for testing here - we must use some
2905 // synchronisation object instead to ensure that the thread is still
2906 // running when we delete it - deleting a detached thread which already
2907 // terminated will lead to a crash!
2909 puts("\n*** Testing thread delete function ***");
2911 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
2915 puts("\nDeleted a thread which didn't start to run yet.");
2917 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
2921 wxThread::Sleep(300);
2925 puts("\nDeleted a running thread.");
2927 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
2931 wxThread::Sleep(300);
2937 puts("\nDeleted a sleeping thread.");
2939 MyJoinableThread
thread3(20);
2944 puts("\nDeleted a joinable thread.");
2946 MyJoinableThread
thread4(2);
2949 wxThread::Sleep(300);
2953 puts("\nDeleted a joinable thread which already terminated.");
2958 #endif // TEST_THREADS
2960 // ----------------------------------------------------------------------------
2962 // ----------------------------------------------------------------------------
2966 static void PrintArray(const char* name
, const wxArrayString
& array
)
2968 printf("Dump of the array '%s'\n", name
);
2970 size_t nCount
= array
.GetCount();
2971 for ( size_t n
= 0; n
< nCount
; n
++ )
2973 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
2977 static int StringLenCompare(const wxString
& first
, const wxString
& second
)
2979 return first
.length() - second
.length();
2982 #include "wx/dynarray.h"
2984 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
2985 #include "wx/arrimpl.cpp"
2986 WX_DEFINE_OBJARRAY(ArrayBars
);
2988 static void TestArrayOfObjects()
2990 puts("*** Testing wxObjArray ***\n");
2994 Bar
bar("second bar");
2996 printf("Initially: %u objects in the array, %u objects total.\n",
2997 bars
.GetCount(), Bar::GetNumber());
2999 bars
.Add(new Bar("first bar"));
3002 printf("Now: %u objects in the array, %u objects total.\n",
3003 bars
.GetCount(), Bar::GetNumber());
3007 printf("After Empty(): %u objects in the array, %u objects total.\n",
3008 bars
.GetCount(), Bar::GetNumber());
3011 printf("Finally: no more objects in the array, %u objects total.\n",
3015 #endif // TEST_ARRAYS
3017 // ----------------------------------------------------------------------------
3019 // ----------------------------------------------------------------------------
3023 #include "wx/timer.h"
3024 #include "wx/tokenzr.h"
3026 static void TestStringConstruction()
3028 puts("*** Testing wxString constructores ***");
3030 #define TEST_CTOR(args, res) \
3033 printf("wxString%s = %s ", #args, s.c_str()); \
3040 printf("(ERROR: should be %s)\n", res); \
3044 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
3045 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
3046 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
3047 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
3049 static const wxChar
*s
= _T("?really!");
3050 const wxChar
*start
= wxStrchr(s
, _T('r'));
3051 const wxChar
*end
= wxStrchr(s
, _T('!'));
3052 TEST_CTOR((start
, end
), _T("really"));
3057 static void TestString()
3067 for (int i
= 0; i
< 1000000; ++i
)
3071 c
= "! How'ya doin'?";
3074 c
= "Hello world! What's up?";
3079 printf ("TestString elapsed time: %ld\n", sw
.Time());
3082 static void TestPChar()
3090 for (int i
= 0; i
< 1000000; ++i
)
3092 strcpy (a
, "Hello");
3093 strcpy (b
, " world");
3094 strcpy (c
, "! How'ya doin'?");
3097 strcpy (c
, "Hello world! What's up?");
3098 if (strcmp (c
, a
) == 0)
3102 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
3105 static void TestStringSub()
3107 wxString
s("Hello, world!");
3109 puts("*** Testing wxString substring extraction ***");
3111 printf("String = '%s'\n", s
.c_str());
3112 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
3113 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
3114 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
3115 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
3116 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
3117 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
3119 static const wxChar
*prefixes
[] =
3123 _T("Hello, world!"),
3124 _T("Hello, world!!!"),
3130 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
3132 wxString prefix
= prefixes
[n
], rest
;
3133 bool rc
= s
.StartsWith(prefix
, &rest
);
3134 printf("StartsWith('%s') = %s", prefix
.c_str(), rc
? "TRUE" : "FALSE");
3137 printf(" (the rest is '%s')\n", rest
.c_str());
3148 static void TestStringFormat()
3150 puts("*** Testing wxString formatting ***");
3153 s
.Printf("%03d", 18);
3155 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
3156 printf("Number 18: %s\n", s
.c_str());
3161 // returns "not found" for npos, value for all others
3162 static wxString
PosToString(size_t res
)
3164 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
3165 : wxString::Format(_T("%u"), res
);
3169 static void TestStringFind()
3171 puts("*** Testing wxString find() functions ***");
3173 static const wxChar
*strToFind
= _T("ell");
3174 static const struct StringFindTest
3178 result
; // of searching "ell" in str
3181 { _T("Well, hello world"), 0, 1 },
3182 { _T("Well, hello world"), 6, 7 },
3183 { _T("Well, hello world"), 9, wxString::npos
},
3186 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
3188 const StringFindTest
& ft
= findTestData
[n
];
3189 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
3191 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
3192 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
3194 size_t resTrue
= ft
.result
;
3195 if ( res
== resTrue
)
3201 printf(_T("(ERROR: should be %s)\n"),
3202 PosToString(resTrue
).c_str());
3209 static void TestStringTokenizer()
3211 puts("*** Testing wxStringTokenizer ***");
3213 static const wxChar
*modeNames
[] =
3217 _T("return all empty"),
3222 static const struct StringTokenizerTest
3224 const wxChar
*str
; // string to tokenize
3225 const wxChar
*delims
; // delimiters to use
3226 size_t count
; // count of token
3227 wxStringTokenizerMode mode
; // how should we tokenize it
3228 } tokenizerTestData
[] =
3230 { _T(""), _T(" "), 0 },
3231 { _T("Hello, world"), _T(" "), 2 },
3232 { _T("Hello, world "), _T(" "), 2 },
3233 { _T("Hello, world"), _T(","), 2 },
3234 { _T("Hello, world!"), _T(",!"), 2 },
3235 { _T("Hello,, world!"), _T(",!"), 3 },
3236 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
3237 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
3238 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
3239 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
3240 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
3241 { _T("01/02/99"), _T("/-"), 3 },
3242 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
3245 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
3247 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
3248 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
3250 size_t count
= tkz
.CountTokens();
3251 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
3252 MakePrintable(tt
.str
).c_str(),
3254 MakePrintable(tt
.delims
).c_str(),
3255 modeNames
[tkz
.GetMode()]);
3256 if ( count
== tt
.count
)
3262 printf(_T("(ERROR: should be %u)\n"), tt
.count
);
3267 // if we emulate strtok(), check that we do it correctly
3268 wxChar
*buf
, *s
= NULL
, *last
;
3270 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
3272 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
3273 wxStrcpy(buf
, tt
.str
);
3275 s
= wxStrtok(buf
, tt
.delims
, &last
);
3282 // now show the tokens themselves
3284 while ( tkz
.HasMoreTokens() )
3286 wxString token
= tkz
.GetNextToken();
3288 printf(_T("\ttoken %u: '%s'"),
3290 MakePrintable(token
).c_str());
3300 printf(" (ERROR: should be %s)\n", s
);
3303 s
= wxStrtok(NULL
, tt
.delims
, &last
);
3307 // nothing to compare with
3312 if ( count2
!= count
)
3314 puts(_T("\tERROR: token count mismatch"));
3323 static void TestStringReplace()
3325 puts("*** Testing wxString::replace ***");
3327 static const struct StringReplaceTestData
3329 const wxChar
*original
; // original test string
3330 size_t start
, len
; // the part to replace
3331 const wxChar
*replacement
; // the replacement string
3332 const wxChar
*result
; // and the expected result
3333 } stringReplaceTestData
[] =
3335 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
3336 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
3337 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
3338 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
3339 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
3342 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
3344 const StringReplaceTestData data
= stringReplaceTestData
[n
];
3346 wxString original
= data
.original
;
3347 original
.replace(data
.start
, data
.len
, data
.replacement
);
3349 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
3350 data
.original
, data
.start
, data
.len
, data
.replacement
,
3353 if ( original
== data
.result
)
3359 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
3366 #endif // TEST_STRINGS
3368 // ----------------------------------------------------------------------------
3370 // ----------------------------------------------------------------------------
3372 int main(int argc
, char **argv
)
3374 if ( !wxInitialize() )
3376 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
3380 puts("Sleeping for 3 seconds... z-z-z-z-z...");
3382 #endif // TEST_USLEEP
3385 static const wxCmdLineEntryDesc cmdLineDesc
[] =
3387 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
3388 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
3390 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
3391 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
3392 { wxCMD_LINE_OPTION
, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
3393 { wxCMD_LINE_OPTION
, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
3395 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
3396 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
3401 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
3403 parser
.AddOption("project_name", "", "full path to project file",
3404 wxCMD_LINE_VAL_STRING
,
3405 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
3407 switch ( parser
.Parse() )
3410 wxLogMessage("Help was given, terminating.");
3414 ShowCmdLine(parser
);
3418 wxLogMessage("Syntax error detected, aborting.");
3421 #endif // TEST_CMDLINE
3432 TestStringConstruction();
3435 TestStringTokenizer();
3436 TestStringReplace();
3438 #endif // TEST_STRINGS
3449 puts("*** Initially:");
3451 PrintArray("a1", a1
);
3453 wxArrayString
a2(a1
);
3454 PrintArray("a2", a2
);
3456 wxSortedArrayString
a3(a1
);
3457 PrintArray("a3", a3
);
3459 puts("*** After deleting a string from a1");
3462 PrintArray("a1", a1
);
3463 PrintArray("a2", a2
);
3464 PrintArray("a3", a3
);
3466 puts("*** After reassigning a1 to a2 and a3");
3468 PrintArray("a2", a2
);
3469 PrintArray("a3", a3
);
3471 puts("*** After sorting a1");
3473 PrintArray("a1", a1
);
3475 puts("*** After sorting a1 in reverse order");
3477 PrintArray("a1", a1
);
3479 puts("*** After sorting a1 by the string length");
3480 a1
.Sort(StringLenCompare
);
3481 PrintArray("a1", a1
);
3483 TestArrayOfObjects();
3484 #endif // TEST_ARRAYS
3490 #ifdef TEST_DLLLOADER
3492 #endif // TEST_DLLLOADER
3496 #endif // TEST_EXECUTE
3498 #ifdef TEST_FILECONF
3500 #endif // TEST_FILECONF
3508 for ( size_t n
= 0; n
< 8000; n
++ )
3510 s
<< (char)('A' + (n
% 26));
3514 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
3516 // this one shouldn't be truncated
3519 // but this one will because log functions use fixed size buffer
3520 // (note that it doesn't need '\n' at the end neither - will be added
3522 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
3532 int nCPUs
= wxThread::GetCPUCount();
3533 printf("This system has %d CPUs\n", nCPUs
);
3535 wxThread::SetConcurrency(nCPUs
);
3537 if ( argc
> 1 && argv
[1][0] == 't' )
3538 wxLog::AddTraceMask("thread");
3541 TestDetachedThreads();
3543 TestJoinableThreads();
3545 TestThreadSuspend();
3549 #endif // TEST_THREADS
3551 #ifdef TEST_LONGLONG
3552 // seed pseudo random generator
3553 srand((unsigned)time(NULL
));
3561 TestMultiplication();
3564 TestLongLongConversion();
3565 TestBitOperations();
3567 TestLongLongComparison();
3568 #endif // TEST_LONGLONG
3575 wxLog::AddTraceMask(_T("mime"));
3582 #ifdef TEST_INFO_FUNCTIONS
3585 #endif // TEST_INFO_FUNCTIONS
3594 TestProtocolFtpUpload();
3595 #endif // TEST_SOCKETS
3599 #endif // TEST_TIMER
3601 #ifdef TEST_DATETIME
3614 TestTimeArithmetics();
3623 #endif // TEST_DATETIME
3629 #endif // TEST_VCARD
3633 #endif // TEST_WCHAR
3636 TestZipStreamRead();
3641 TestZlibStreamWrite();
3642 TestZlibStreamRead();