]> git.saurik.com Git - wxWidgets.git/blame - samples/console/console.cpp
another GetInt/GetId bug fix
[wxWidgets.git] / samples / console / console.cpp
CommitLineData
37667812
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/console/console.cpp
3// Purpose: a sample console (as opposed to GUI) progam using wxWindows
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
e87271f3
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
37667812
VZ
20#include <stdio.h>
21
22#include <wx/string.h>
bbfa0322 23#include <wx/file.h>
37667812 24#include <wx/app.h>
e87271f3 25
d31b7b68
VZ
26// without this pragma, the stupid compiler precompiles #defines below so that
27// changing them doesn't "take place" later!
28#ifdef __VISUALC__
29 #pragma hdrstop
30#endif
31
e87271f3
VZ
32// ----------------------------------------------------------------------------
33// conditional compilation
34// ----------------------------------------------------------------------------
35
d31b7b68 36// what to test (in alphabetic order)?
378b05f7 37
bbf8fc53
VZ
38//#define TEST_ARRAYS
39//#define TEST_CMDLINE
3ca6a5f0 40//#define TEST_DATETIME
bbf8fc53 41//#define TEST_DIR
f6bcfd97 42//#define TEST_DLLLOADER
d31b7b68 43//#define TEST_EXECUTE
83141d3a 44//#define TEST_FILE
2c8e4738
VZ
45//#define TEST_FILECONF
46//#define TEST_HASH
f6bcfd97 47//#define TEST_LIST
bbf8fc53
VZ
48//#define TEST_LOG
49//#define TEST_LONGLONG
50//#define TEST_MIME
f6bcfd97 51//#define TEST_INFO_FUNCTIONS
89e60357 52//#define TEST_SOCKETS
83141d3a 53#define TEST_STREAMS
ee6e1b1d 54//#define TEST_STRINGS
bbf8fc53 55//#define TEST_THREADS
8e907a13 56//#define TEST_TIMER
3ca6a5f0 57//#define TEST_VCARD -- don't enable this (VZ)
f6bcfd97
BP
58//#define TEST_WCHAR
59//#define TEST_ZIP
3ca6a5f0 60//#define TEST_ZLIB
f6bcfd97
BP
61
62// ----------------------------------------------------------------------------
63// test class for container objects
64// ----------------------------------------------------------------------------
65
66#if defined(TEST_ARRAYS) || defined(TEST_LIST)
67
68class Bar // Foo is already taken in the hash test
69{
70public:
71 Bar(const wxString& name) : m_name(name) { ms_bars++; }
72 ~Bar() { ms_bars--; }
73
74 static size_t GetNumber() { return ms_bars; }
75
76 const char *GetName() const { return m_name; }
77
78private:
79 wxString m_name;
80
81 static size_t ms_bars;
82};
83
84size_t Bar::ms_bars = 0;
85
86#endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
e87271f3
VZ
87
88// ============================================================================
89// implementation
90// ============================================================================
91
8e907a13
VZ
92// ----------------------------------------------------------------------------
93// helper functions
94// ----------------------------------------------------------------------------
95
96#if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
97
98// replace TABs with \t and CRs with \n
99static wxString MakePrintable(const wxChar *s)
100{
101 wxString str(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"));
105
106 return str;
107}
108
109#endif // MakePrintable() is used
110
d34bce84
VZ
111// ----------------------------------------------------------------------------
112// wxCmdLineParser
113// ----------------------------------------------------------------------------
114
d31b7b68
VZ
115#ifdef TEST_CMDLINE
116
d34bce84
VZ
117#include <wx/cmdline.h>
118#include <wx/datetime.h>
119
120static void ShowCmdLine(const wxCmdLineParser& parser)
121{
122 wxString s = "Input files: ";
123
124 size_t count = parser.GetParamCount();
125 for ( size_t param = 0; param < count; param++ )
126 {
127 s << parser.GetParam(param) << ' ';
128 }
129
130 s << '\n'
131 << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n'
132 << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n';
133
134 wxString strVal;
135 long lVal;
136 wxDateTime dt;
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';
f6bcfd97
BP
145 if ( parser.Found("project_name", &strVal) )
146 s << "Project:\t" << strVal << '\n';
d34bce84
VZ
147
148 wxLogMessage(s);
149}
150
151#endif // TEST_CMDLINE
152
1944c6bd
VZ
153// ----------------------------------------------------------------------------
154// wxDir
155// ----------------------------------------------------------------------------
156
157#ifdef TEST_DIR
158
159#include <wx/dir.h>
160
161static void TestDirEnumHelper(wxDir& dir,
162 int flags = wxDIR_DEFAULT,
163 const wxString& filespec = wxEmptyString)
164{
165 wxString filename;
166
167 if ( !dir.IsOpened() )
168 return;
169
170 bool cont = dir.GetFirst(&filename, filespec, flags);
171 while ( cont )
172 {
173 printf("\t%s\n", filename.c_str());
174
175 cont = dir.GetNext(&filename);
176 }
177
178 puts("");
179}
180
181static void TestDirEnum()
182{
183 wxDir dir(wxGetCwd());
184
185 puts("Enumerating everything in current directory:");
186 TestDirEnumHelper(dir);
187
188 puts("Enumerating really everything in current directory:");
189 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
190
191 puts("Enumerating object files in current directory:");
192 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o");
193
194 puts("Enumerating directories in current directory:");
195 TestDirEnumHelper(dir, wxDIR_DIRS);
196
197 puts("Enumerating files in current directory:");
198 TestDirEnumHelper(dir, wxDIR_FILES);
199
200 puts("Enumerating files including hidden in current directory:");
201 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
202
203#ifdef __UNIX__
204 dir.Open("/");
205#elif defined(__WXMSW__)
206 dir.Open("c:\\");
207#else
208 #error "don't know where the root directory is"
209#endif
210
211 puts("Enumerating everything in root directory:");
212 TestDirEnumHelper(dir, wxDIR_DEFAULT);
213
214 puts("Enumerating directories in root directory:");
215 TestDirEnumHelper(dir, wxDIR_DIRS);
216
217 puts("Enumerating files in root directory:");
218 TestDirEnumHelper(dir, wxDIR_FILES);
219
220 puts("Enumerating files including hidden in root directory:");
221 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
222
223 puts("Enumerating files in non existing directory:");
224 wxDir dirNo("nosuchdir");
225 TestDirEnumHelper(dirNo);
226}
227
228#endif // TEST_DIR
229
f6bcfd97
BP
230// ----------------------------------------------------------------------------
231// wxDllLoader
232// ----------------------------------------------------------------------------
233
234#ifdef TEST_DLLLOADER
235
236#include <wx/dynlib.h>
237
238static void TestDllLoad()
239{
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");
247#else
248 #error "don't know how to test wxDllLoader on this platform"
249#endif
250
251 puts("*** testing wxDllLoader ***\n");
252
253 wxDllType dllHandle = wxDllLoader::LoadLibrary(LIB_NAME);
254 if ( !dllHandle )
255 {
256 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
257 }
258 else
259 {
260 typedef int (*strlenType)(char *);
261 strlenType pfnStrlen = (strlenType)wxDllLoader::GetSymbol(dllHandle, FUNC_NAME);
262 if ( !pfnStrlen )
263 {
264 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
265 FUNC_NAME, LIB_NAME);
266 }
267 else
268 {
269 if ( pfnStrlen("foo") != 3 )
270 {
271 wxPrintf(_T("ERROR: loaded function is not strlen()!\n"));
272 }
273 else
274 {
275 puts("... ok");
276 }
277 }
278
279 wxDllLoader::UnloadLibrary(dllHandle);
280 }
281}
282
283#endif // TEST_DLLLOADER
284
d93c719a
VZ
285// ----------------------------------------------------------------------------
286// wxExecute
287// ----------------------------------------------------------------------------
288
289#ifdef TEST_EXECUTE
290
291#include <wx/utils.h>
292
293static void TestExecute()
294{
295 puts("*** testing wxExecute ***");
296
297#ifdef __UNIX__
298 #define COMMAND "echo hi"
2c8e4738
VZ
299 #define SHELL_COMMAND "echo hi from shell"
300 #define REDIRECT_COMMAND "date"
d93c719a
VZ
301#elif defined(__WXMSW__)
302 #define COMMAND "command.com -c 'echo hi'"
2c8e4738
VZ
303 #define SHELL_COMMAND "echo hi"
304 #define REDIRECT_COMMAND COMMAND
d93c719a
VZ
305#else
306 #error "no command to exec"
307#endif // OS
308
2c8e4738
VZ
309 printf("Testing wxShell: ");
310 fflush(stdout);
311 if ( wxShell(SHELL_COMMAND) )
312 puts("Ok.");
d93c719a 313 else
2c8e4738
VZ
314 puts("ERROR.");
315
316 printf("Testing wxExecute: ");
317 fflush(stdout);
318 if ( wxExecute(COMMAND, TRUE /* sync */) == 0 )
319 puts("Ok.");
320 else
321 puts("ERROR.");
322
323#if 0 // no, it doesn't work (yet?)
324 printf("Testing async wxExecute: ");
325 fflush(stdout);
326 if ( wxExecute(COMMAND) != 0 )
327 puts("Ok (command launched).");
328 else
329 puts("ERROR.");
330#endif // 0
331
332 printf("Testing wxExecute with redirection:\n");
333 wxArrayString output;
334 if ( wxExecute(REDIRECT_COMMAND, output) != 0 )
335 {
336 puts("ERROR.");
337 }
338 else
339 {
340 size_t count = output.GetCount();
341 for ( size_t n = 0; n < count; n++ )
342 {
343 printf("\t%s\n", output[n].c_str());
344 }
345
346 puts("Ok.");
347 }
d93c719a
VZ
348}
349
350#endif // TEST_EXECUTE
351
f6bcfd97
BP
352// ----------------------------------------------------------------------------
353// file
354// ----------------------------------------------------------------------------
355
356#ifdef TEST_FILE
357
358#include <wx/file.h>
359#include <wx/textfile.h>
360
361static void TestFileRead()
362{
363 puts("*** wxFile read test ***");
364
365 wxFile file(_T("testdata.fc"));
366 if ( file.IsOpened() )
367 {
368 printf("File length: %lu\n", file.Length());
369
370 puts("File dump:\n----------");
371
3ca6a5f0 372 static const off_t len = 1024;
f6bcfd97
BP
373 char buf[len];
374 for ( ;; )
375 {
376 off_t nRead = file.Read(buf, len);
377 if ( nRead == wxInvalidOffset )
378 {
379 printf("Failed to read the file.");
380 break;
381 }
382
383 fwrite(buf, nRead, 1, stdout);
384
385 if ( nRead < len )
386 break;
387 }
388
389 puts("----------");
390 }
391 else
392 {
393 printf("ERROR: can't open test file.\n");
394 }
395
396 puts("");
397}
398
399static void TestTextFileRead()
400{
401 puts("*** wxTextFile read test ***");
402
403 wxTextFile file(_T("testdata.fc"));
404 if ( file.Open() )
405 {
406 printf("Number of lines: %u\n", file.GetLineCount());
407 printf("Last line: '%s'\n", file.GetLastLine().c_str());
3ca6a5f0
BP
408
409 wxString s;
410
411 puts("\nDumping the entire file:");
412 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
413 {
414 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
415 }
416 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
417
418 puts("\nAnd now backwards:");
419 for ( s = file.GetLastLine();
420 file.GetCurrentLine() != 0;
421 s = file.GetPrevLine() )
422 {
423 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
424 }
425 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
f6bcfd97
BP
426 }
427 else
428 {
429 printf("ERROR: can't open '%s'\n", file.GetName());
430 }
431
432 puts("");
433}
434
435#endif // TEST_FILE
436
ee6e1b1d
VZ
437// ----------------------------------------------------------------------------
438// wxFileConfig
439// ----------------------------------------------------------------------------
440
441#ifdef TEST_FILECONF
442
443#include <wx/confbase.h>
444#include <wx/fileconf.h>
445
446static const struct FileConfTestData
447{
448 const wxChar *name; // value name
449 const wxChar *value; // the value from the file
450} fcTestData[] =
451{
452 { _T("value1"), _T("one") },
453 { _T("value2"), _T("two") },
454 { _T("novalue"), _T("default") },
455};
456
457static void TestFileConfRead()
458{
459 puts("*** testing wxFileConfig loading/reading ***");
460
461 wxFileConfig fileconf(_T("test"), wxEmptyString,
462 _T("testdata.fc"), wxEmptyString,
463 wxCONFIG_USE_RELATIVE_PATH);
464
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++ )
469 {
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 )
474 {
475 puts("(ok)");
476 }
477 else
478 {
479 printf("(ERROR: should be %s)\n", data.value);
480 }
481 }
482
483 // test enumerating the entries
484 puts("\nEnumerating all root entries:");
485 long dummy;
486 wxString name;
487 bool cont = fileconf.GetFirstEntry(name, dummy);
488 while ( cont )
489 {
490 printf("\t%s = %s\n",
491 name.c_str(),
492 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
493
494 cont = fileconf.GetNextEntry(name, dummy);
495 }
496}
497
498#endif // TEST_FILECONF
499
2c8e4738
VZ
500// ----------------------------------------------------------------------------
501// wxHashTable
502// ----------------------------------------------------------------------------
503
504#ifdef TEST_HASH
505
506#include <wx/hash.h>
507
508struct Foo
509{
510 Foo(int n_) { n = n_; count++; }
511 ~Foo() { count--; }
512
513 int n;
514
515 static size_t count;
516};
517
518size_t Foo::count = 0;
519
520WX_DECLARE_LIST(Foo, wxListFoos);
521WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos);
522
523#include <wx/listimpl.cpp>
524
525WX_DEFINE_LIST(wxListFoos);
526
527static void TestHash()
528{
529 puts("*** Testing wxHashTable ***\n");
530
531 {
532 wxHashFoos hash;
533 hash.DeleteContents(TRUE);
534
535 printf("Hash created: %u foos in hash, %u foos totally\n",
536 hash.GetCount(), Foo::count);
537
538 static const int hashTestData[] =
539 {
540 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
541 };
542
543 size_t n;
544 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
545 {
546 hash.Put(hashTestData[n], n, new Foo(n));
547 }
548
549 printf("Hash filled: %u foos in hash, %u foos totally\n",
550 hash.GetCount(), Foo::count);
551
552 puts("Hash access test:");
553 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
554 {
555 printf("\tGetting element with key %d, value %d: ",
556 hashTestData[n], n);
557 Foo *foo = hash.Get(hashTestData[n], n);
558 if ( !foo )
559 {
560 printf("ERROR, not found.\n");
561 }
562 else
563 {
564 printf("%d (%s)\n", foo->n,
565 (size_t)foo->n == n ? "ok" : "ERROR");
566 }
567 }
568
569 printf("\nTrying to get an element not in hash: ");
570
571 if ( hash.Get(1234) || hash.Get(1, 0) )
572 {
573 puts("ERROR: found!");
574 }
575 else
576 {
577 puts("ok (not found)");
578 }
579 }
580
581 printf("Hash destroyed: %u foos left\n", Foo::count);
582}
583
584#endif // TEST_HASH
585
f6bcfd97
BP
586// ----------------------------------------------------------------------------
587// wxList
588// ----------------------------------------------------------------------------
589
590#ifdef TEST_LIST
591
592#include <wx/list.h>
593
594WX_DECLARE_LIST(Bar, wxListBars);
595#include <wx/listimpl.cpp>
596WX_DEFINE_LIST(wxListBars);
597
598static void TestListCtor()
599{
600 puts("*** Testing wxList construction ***\n");
601
602 {
603 wxListBars list1;
604 list1.Append(new Bar(_T("first")));
605 list1.Append(new Bar(_T("second")));
606
607 printf("After 1st list creation: %u objects in the list, %u objects total.\n",
608 list1.GetCount(), Bar::GetNumber());
609
610 wxListBars list2;
611 list2 = list1;
612
613 printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n",
614 list1.GetCount(), list2.GetCount(), Bar::GetNumber());
615
616 list1.DeleteContents(TRUE);
617 }
618
619 printf("After list destruction: %u objects left.\n", Bar::GetNumber());
620}
621
622#endif // TEST_LIST
623
696e1ea0
VZ
624// ----------------------------------------------------------------------------
625// MIME types
626// ----------------------------------------------------------------------------
627
628#ifdef TEST_MIME
629
630#include <wx/mimetype.h>
631
f6bcfd97
BP
632static wxMimeTypesManager g_mimeManager;
633
696e1ea0
VZ
634static void TestMimeEnum()
635{
696e1ea0
VZ
636 wxArrayString mimetypes;
637
f6bcfd97 638 size_t count = g_mimeManager.EnumAllFileTypes(mimetypes);
696e1ea0
VZ
639
640 printf("*** All %u known filetypes: ***\n", count);
641
642 wxArrayString exts;
643 wxString desc;
644
645 for ( size_t n = 0; n < count; n++ )
646 {
f6bcfd97 647 wxFileType *filetype = g_mimeManager.GetFileTypeFromMimeType(mimetypes[n]);
696e1ea0 648 if ( !filetype )
c61f4f6d 649 {
97e0ceea
VZ
650 printf("nothing known about the filetype '%s'!\n",
651 mimetypes[n].c_str());
696e1ea0 652 continue;
c61f4f6d
VZ
653 }
654
696e1ea0
VZ
655 filetype->GetDescription(&desc);
656 filetype->GetExtensions(exts);
657
299fcbfe
VZ
658 filetype->GetIcon(NULL);
659
696e1ea0
VZ
660 wxString extsAll;
661 for ( size_t e = 0; e < exts.GetCount(); e++ )
662 {
663 if ( e > 0 )
664 extsAll << _T(", ");
665 extsAll += exts[e];
666 }
667
54acce90
VZ
668 printf("\t%s: %s (%s)\n",
669 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
696e1ea0
VZ
670 }
671}
672
f6bcfd97
BP
673static void TestMimeOverride()
674{
675 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
676
677 wxString mailcap = _T("/tmp/mailcap"),
678 mimetypes = _T("/tmp/mime.types");
679
680 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
681 mailcap.c_str(),
682 g_mimeManager.ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
683 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
684 mimetypes.c_str(),
685 g_mimeManager.ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
686}
687
688static void TestMimeFilename()
689{
690 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
691
692 static const wxChar *filenames[] =
693 {
694 _T("readme.txt"),
695 _T("document.pdf"),
696 _T("image.gif"),
697 };
698
699 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
700 {
701 const wxString fname = filenames[n];
702 wxString ext = fname.AfterLast(_T('.'));
703 wxFileType *ft = g_mimeManager.GetFileTypeFromExtension(ext);
704 if ( !ft )
705 {
706 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
707 }
708 else
709 {
710 wxString desc;
711 if ( !ft->GetDescription(&desc) )
712 desc = _T("<no description>");
713
714 wxString cmd;
715 if ( !ft->GetOpenCommand(&cmd,
716 wxFileType::MessageParameters(fname, _T(""))) )
717 cmd = _T("<no command available>");
718
719 wxPrintf(_T("To open %s (%s) do '%s'.\n"),
720 fname.c_str(), desc.c_str(), cmd.c_str());
721
722 delete ft;
723 }
724 }
725}
726
696e1ea0
VZ
727#endif // TEST_MIME
728
89e60357
VZ
729// ----------------------------------------------------------------------------
730// misc information functions
731// ----------------------------------------------------------------------------
732
733#ifdef TEST_INFO_FUNCTIONS
734
735#include <wx/utils.h>
736
737static void TestOsInfo()
738{
739 puts("*** Testing OS info functions ***\n");
740
741 int major, minor;
742 wxGetOsVersion(&major, &minor);
743 printf("Running under: %s, version %d.%d\n",
744 wxGetOsDescription().c_str(), major, minor);
745
bd3277fe 746 printf("%ld free bytes of memory left.\n", wxGetFreeMemory());
89e60357
VZ
747
748 printf("Host name is %s (%s).\n",
749 wxGetHostName().c_str(), wxGetFullHostName().c_str());
bd3277fe
VZ
750
751 puts("");
89e60357
VZ
752}
753
754static void TestUserInfo()
755{
756 puts("*** Testing user info functions ***\n");
757
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());
bd3277fe
VZ
762
763 puts("");
89e60357
VZ
764}
765
766#endif // TEST_INFO_FUNCTIONS
767
b76b015e
VZ
768// ----------------------------------------------------------------------------
769// long long
770// ----------------------------------------------------------------------------
771
772#ifdef TEST_LONGLONG
773
774#include <wx/longlong.h>
775#include <wx/timer.h>
776
2a310492
VZ
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)
779
780// get a random 64 bit number
781#define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
782
7d0bb74d 783#if wxUSE_LONGLONG_WX
2a310492
VZ
784inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
785 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
786inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
787 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
7d0bb74d 788#endif // wxUSE_LONGLONG_WX
2a310492 789
b76b015e
VZ
790static void TestSpeed()
791{
792 static const long max = 100000000;
793 long n;
9fc3ad34 794
b76b015e
VZ
795 {
796 wxStopWatch sw;
797
798 long l = 0;
799 for ( n = 0; n < max; n++ )
800 {
801 l += n;
802 }
803
804 printf("Summing longs took %ld milliseconds.\n", sw.Time());
805 }
806
2ea24d9f 807#if wxUSE_LONGLONG_NATIVE
b76b015e
VZ
808 {
809 wxStopWatch sw;
810
2ea24d9f 811 wxLongLong_t l = 0;
b76b015e
VZ
812 for ( n = 0; n < max; n++ )
813 {
814 l += n;
815 }
816
2ea24d9f 817 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time());
b76b015e 818 }
2ea24d9f 819#endif // wxUSE_LONGLONG_NATIVE
b76b015e
VZ
820
821 {
822 wxStopWatch sw;
823
824 wxLongLong l;
825 for ( n = 0; n < max; n++ )
826 {
827 l += n;
828 }
829
830 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
831 }
832}
833
2a310492 834static void TestLongLongConversion()
b76b015e 835{
2a310492
VZ
836 puts("*** Testing wxLongLong conversions ***\n");
837
838 wxLongLong a;
839 size_t nTested = 0;
840 for ( size_t n = 0; n < 100000; n++ )
841 {
842 a = RAND_LL();
843
844#if wxUSE_LONGLONG_NATIVE
845 wxLongLongNative b(a.GetHi(), a.GetLo());
5e6a0e83 846
2a310492
VZ
847 wxASSERT_MSG( a == b, "conversions failure" );
848#else
849 puts("Can't do it without native long long type, test skipped.");
b76b015e 850
2a310492
VZ
851 return;
852#endif // wxUSE_LONGLONG_NATIVE
853
854 if ( !(nTested % 1000) )
855 {
856 putchar('.');
857 fflush(stdout);
858 }
859
860 nTested++;
861 }
862
863 puts(" done!");
864}
865
866static void TestMultiplication()
867{
868 puts("*** Testing wxLongLong multiplication ***\n");
869
870 wxLongLong a, b;
871 size_t nTested = 0;
872 for ( size_t n = 0; n < 100000; n++ )
873 {
874 a = RAND_LL();
875 b = RAND_LL();
876
877#if wxUSE_LONGLONG_NATIVE
878 wxLongLongNative aa(a.GetHi(), a.GetLo());
879 wxLongLongNative bb(b.GetHi(), b.GetLo());
880
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.");
884
885 return;
886#endif // wxUSE_LONGLONG_NATIVE
887
888 if ( !(nTested % 1000) )
889 {
890 putchar('.');
891 fflush(stdout);
892 }
893
894 nTested++;
895 }
896
897 puts(" done!");
898}
899
900static void TestDivision()
901{
902 puts("*** Testing wxLongLong division ***\n");
2f02cb89 903
2ea24d9f 904 wxLongLong q, r;
2f02cb89 905 size_t nTested = 0;
5e6a0e83 906 for ( size_t n = 0; n < 100000; n++ )
2f02cb89
VZ
907 {
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());
911
2ea24d9f
VZ
912 // get a random long (not wxLongLong for now) to divide it with
913 long l = rand();
914 q = ll / l;
915 r = ll % l;
916
2a310492
VZ
917#if wxUSE_LONGLONG_NATIVE
918 wxLongLongNative m(ll.GetHi(), ll.GetLo());
919
920 wxLongLongNative p = m / l, s = m % l;
921 wxASSERT_MSG( q == p && r == s, "division failure" );
922#else // !wxUSE_LONGLONG_NATIVE
5e6a0e83 923 // verify the result
2ea24d9f 924 wxASSERT_MSG( ll == q*l + r, "division failure" );
2a310492 925#endif // wxUSE_LONGLONG_NATIVE
2f02cb89 926
5e6a0e83
VZ
927 if ( !(nTested % 1000) )
928 {
929 putchar('.');
930 fflush(stdout);
931 }
932
2f02cb89
VZ
933 nTested++;
934 }
935
5e6a0e83 936 puts(" done!");
2a310492 937}
2f02cb89 938
2a310492
VZ
939static void TestAddition()
940{
941 puts("*** Testing wxLongLong addition ***\n");
942
943 wxLongLong a, b, c;
944 size_t nTested = 0;
945 for ( size_t n = 0; n < 100000; n++ )
946 {
947 a = RAND_LL();
948 b = RAND_LL();
949 c = a + b;
950
951#if wxUSE_LONGLONG_NATIVE
952 wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
953 wxLongLongNative(b.GetHi(), b.GetLo()),
7c968cee 954 "addition failure" );
2a310492
VZ
955#else // !wxUSE_LONGLONG_NATIVE
956 wxASSERT_MSG( c - b == a, "addition failure" );
957#endif // wxUSE_LONGLONG_NATIVE
958
959 if ( !(nTested % 1000) )
960 {
961 putchar('.');
962 fflush(stdout);
963 }
964
965 nTested++;
966 }
967
968 puts(" done!");
b76b015e
VZ
969}
970
2a310492
VZ
971static void TestBitOperations()
972{
973 puts("*** Testing wxLongLong bit operation ***\n");
974
f6bcfd97 975 wxLongLong ll;
2a310492
VZ
976 size_t nTested = 0;
977 for ( size_t n = 0; n < 100000; n++ )
978 {
f6bcfd97 979 ll = RAND_LL();
2a310492
VZ
980
981#if wxUSE_LONGLONG_NATIVE
982 for ( size_t n = 0; n < 33; n++ )
983 {
2a310492 984 }
2a310492
VZ
985#else // !wxUSE_LONGLONG_NATIVE
986 puts("Can't do it without native long long type, test skipped.");
987
988 return;
989#endif // wxUSE_LONGLONG_NATIVE
990
991 if ( !(nTested % 1000) )
992 {
993 putchar('.');
994 fflush(stdout);
995 }
996
997 nTested++;
998 }
999
1000 puts(" done!");
1001}
1002
f6bcfd97
BP
1003static void TestLongLongComparison()
1004{
1005 puts("*** Testing wxLongLong comparison ***\n");
1006
1007 static const long testLongs[] =
1008 {
1009 0,
1010 1,
1011 -1,
1012 LONG_MAX,
1013 LONG_MIN,
1014 0x1234,
1015 -0x1234
1016 };
1017
1018 static const long ls[2] =
1019 {
1020 0x1234,
1021 -0x1234,
1022 };
1023
1024 wxLongLongWx lls[2];
1025 lls[0] = ls[0];
1026 lls[1] = ls[1];
1027
1028 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
1029 {
1030 bool res;
1031
1032 for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
1033 {
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");
1038
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");
1043
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");
1048 }
1049 }
1050}
1051
2a310492
VZ
1052#undef MAKE_LL
1053#undef RAND_LL
1054
b76b015e
VZ
1055#endif // TEST_LONGLONG
1056
2c8e4738
VZ
1057// ----------------------------------------------------------------------------
1058// sockets
1059// ----------------------------------------------------------------------------
1060
1061#ifdef TEST_SOCKETS
1062
1063#include <wx/socket.h>
8e907a13
VZ
1064#include <wx/protocol/protocol.h>
1065#include <wx/protocol/ftp.h>
1066#include <wx/protocol/http.h>
1067
1068static void TestSocketServer()
1069{
1070 puts("*** Testing wxSocketServer ***\n");
1071
ccdb23df
VZ
1072 static const int PORT = 3000;
1073
8e907a13 1074 wxIPV4address addr;
ccdb23df 1075 addr.Service(PORT);
8e907a13
VZ
1076
1077 wxSocketServer *server = new wxSocketServer(addr);
1078 if ( !server->Ok() )
1079 {
1080 puts("ERROR: failed to bind");
ccdb23df
VZ
1081
1082 return;
8e907a13 1083 }
8dfea369
VZ
1084
1085 for ( ;; )
1086 {
ccdb23df 1087 printf("Server: waiting for connection on port %d...\n", PORT);
8dfea369
VZ
1088
1089 wxSocketBase *socket = server->Accept();
1090 if ( !socket )
1091 {
1092 puts("ERROR: wxSocketServer::Accept() failed.");
1093 break;
1094 }
1095
1096 puts("Server: got a client.");
1097
ccdb23df
VZ
1098 server->SetTimeout(60); // 1 min
1099
1100 while ( socket->IsConnected() )
8dfea369 1101 {
ccdb23df
VZ
1102 wxString s;
1103 char ch = '\0';
1104 for ( ;; )
8dfea369 1105 {
ccdb23df
VZ
1106 if ( socket->Read(&ch, sizeof(ch)).Error() )
1107 {
1108 // don't log error if the client just close the connection
1109 if ( socket->IsConnected() )
1110 {
1111 puts("ERROR: in wxSocket::Read.");
1112 }
8dfea369 1113
ccdb23df
VZ
1114 break;
1115 }
8dfea369 1116
ccdb23df
VZ
1117 if ( ch == '\r' )
1118 continue;
8dfea369 1119
ccdb23df
VZ
1120 if ( ch == '\n' )
1121 break;
8dfea369 1122
ccdb23df
VZ
1123 s += ch;
1124 }
8dfea369 1125
ccdb23df
VZ
1126 if ( ch != '\n' )
1127 {
1128 break;
1129 }
8dfea369 1130
ccdb23df
VZ
1131 printf("Server: got '%s'.\n", s.c_str());
1132 if ( s == _T("bye") )
1133 {
1134 delete socket;
8dfea369 1135
ccdb23df
VZ
1136 break;
1137 }
1138
1139 socket->Write(s.MakeUpper().c_str(), s.length());
1140 socket->Write("\r\n", 2);
1141 printf("Server: wrote '%s'.\n", s.c_str());
8dfea369
VZ
1142 }
1143
ccdb23df 1144 puts("Server: lost a client.");
8dfea369 1145
ccdb23df 1146 socket->Destroy();
8dfea369 1147 }
9fc3cba7 1148
ccdb23df
VZ
1149 // same as "delete server" but is consistent with GUI programs
1150 server->Destroy();
8e907a13 1151}
2c8e4738
VZ
1152
1153static void TestSocketClient()
1154{
1155 puts("*** Testing wxSocketClient ***\n");
1156
8e907a13
VZ
1157 static const char *hostname = "www.wxwindows.org";
1158
1159 wxIPV4address addr;
1160 addr.Hostname(hostname);
1161 addr.Service(80);
1162
1163 printf("--- Attempting to connect to %s:80...\n", hostname);
2c8e4738
VZ
1164
1165 wxSocketClient client;
8e907a13 1166 if ( !client.Connect(addr) )
2c8e4738 1167 {
8e907a13 1168 printf("ERROR: failed to connect to %s\n", hostname);
2c8e4738
VZ
1169 }
1170 else
1171 {
8e907a13
VZ
1172 printf("--- Connected to %s:%u...\n",
1173 addr.Hostname().c_str(), addr.Service());
1174
2c8e4738
VZ
1175 char buf[8192];
1176
8e907a13
VZ
1177 // could use simply "GET" here I suppose
1178 wxString cmdGet =
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());
2c8e4738 1183 client.Read(buf, WXSIZEOF(buf));
8e907a13
VZ
1184 printf("--- Server replied:\n%s", buf);
1185 }
1186}
1187
1188static void TestProtocolFtp()
1189{
f6bcfd97 1190 puts("*** Testing wxFTP download ***\n");
8e907a13
VZ
1191
1192 wxLog::AddTraceMask(_T("ftp"));
1193
1194 static const char *hostname = "ftp.wxwindows.org";
1195
1196 printf("--- Attempting to connect to %s:21...\n", hostname);
1197
1198 wxFTP ftp;
1199 if ( !ftp.Connect(hostname) )
1200 {
1201 printf("ERROR: failed to connect to %s\n", hostname);
1202 }
1203 else
1204 {
1205 printf("--- Connected to %s, current directory is '%s'\n",
1206 hostname, ftp.Pwd().c_str());
1207 if ( !ftp.ChDir(_T("pub")) )
1208 {
1209 puts("ERROR: failed to cd to pub");
1210 }
1211
1212 wxArrayString files;
1213 if ( !ftp.GetList(files) )
1214 {
1215 puts("ERROR: failed to get list of files");
1216 }
1217 else
1218 {
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++ )
1222 {
1223 printf("\t%s\n", files[n].c_str());
1224 }
1225 puts("End of the file list");
1226 }
1227
1228 if ( !ftp.ChDir(_T("..")) )
1229 {
1230 puts("ERROR: failed to cd to ..");
1231 }
1232
1233 static const char *filename = "welcome.msg";
1234 wxInputStream *in = ftp.GetInputStream(filename);
1235 if ( !in )
1236 {
1237 puts("ERROR: couldn't get input stream");
1238 }
1239 else
1240 {
1241 size_t size = in->StreamSize();
1242 printf("Reading file %s (%u bytes)...", filename, size);
1243
1244 char *data = new char[size];
1245 if ( !in->Read(data, size) )
1246 {
1247 puts("ERROR: read error");
1248 }
1249 else
1250 {
1251 printf("\nContents of %s:\n%s\n", filename, data);
1252 }
1253
1254 delete [] data;
1255 delete in;
1256 }
2c8e4738
VZ
1257 }
1258}
1259
f6bcfd97
BP
1260static void TestProtocolFtpUpload()
1261{
1262 puts("*** Testing wxFTP uploading ***\n");
1263
1264 wxLog::AddTraceMask(_T("ftp"));
1265
1266 static const char *hostname = "localhost";
1267
1268 printf("--- Attempting to connect to %s:21...\n", hostname);
1269
1270 wxFTP ftp;
1271 ftp.SetUser("zeitlin");
1272 ftp.SetPassword("insert your password here");
1273 if ( !ftp.Connect(hostname) )
1274 {
1275 printf("ERROR: failed to connect to %s\n", hostname);
1276 }
1277 else
1278 {
1279 printf("--- Connected to %s, current directory is '%s'\n",
1280 hostname, ftp.Pwd().c_str());
1281
1282 // upload a file
1283 static const char *file1 = "test1";
1284 static const char *file2 = "test2";
1285 wxOutputStream *out = ftp.GetOutputStream(file1);
1286 if ( out )
1287 {
1288 printf("--- Uploading to %s ---\n", file1);
1289 out->Write("First hello", 11);
1290 delete out;
1291 }
1292
1293 out = ftp.GetOutputStream(file2);
1294 if ( out )
1295 {
1296 printf("--- Uploading to %s ---\n", file1);
1297 out->Write("Second hello", 12);
1298 delete out;
1299 }
1300 }
1301}
1302
2c8e4738
VZ
1303#endif // TEST_SOCKETS
1304
83141d3a
VZ
1305// ----------------------------------------------------------------------------
1306// streams
1307// ----------------------------------------------------------------------------
1308
1309#ifdef TEST_STREAMS
1310
1311#include <wx/mstream.h>
1312
1313static void TestMemoryStream()
1314{
1315 puts("*** Testing wxMemoryInputStream ***");
1316
1317 wxChar buf[1024];
1318 wxStrncpy(buf, _T("Hello, stream!"), WXSIZEOF(buf));
1319
1320 wxMemoryInputStream memInpStream(buf, wxStrlen(buf));
1321 printf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
1322 while ( !memInpStream.Eof() )
1323 {
1324 putchar(memInpStream.GetC());
1325 }
1326
1327 puts("\n*** wxMemoryInputStream test done ***");
1328}
1329
1330#endif // TEST_STREAMS
1331
d31b7b68
VZ
1332// ----------------------------------------------------------------------------
1333// timers
1334// ----------------------------------------------------------------------------
1335
1336#ifdef TEST_TIMER
1337
1338#include <wx/timer.h>
1339#include <wx/utils.h>
1340
1341static void TestStopWatch()
1342{
1343 puts("*** Testing wxStopWatch ***\n");
1344
1345 wxStopWatch sw;
1346 printf("Sleeping 3 seconds...");
1347 wxSleep(3);
87798c00 1348 printf("\telapsed time: %ldms\n", sw.Time());
d31b7b68
VZ
1349
1350 sw.Pause();
1351 printf("Sleeping 2 more seconds...");
1352 wxSleep(2);
87798c00 1353 printf("\telapsed time: %ldms\n", sw.Time());
d31b7b68
VZ
1354
1355 sw.Resume();
1356 printf("And 3 more seconds...");
1357 wxSleep(3);
87798c00
VZ
1358 printf("\telapsed time: %ldms\n", sw.Time());
1359
1360 wxStopWatch sw2;
1361 puts("\nChecking for 'backwards clock' bug...");
1362 for ( size_t n = 0; n < 70; n++ )
1363 {
1364 sw2.Start();
89e6463c
GRG
1365
1366 for ( size_t m = 0; m < 100000; m++ )
87798c00 1367 {
89e6463c
GRG
1368 if ( sw.Time() < 0 || sw2.Time() < 0 )
1369 {
1370 puts("\ntime is negative - ERROR!");
1371 }
87798c00
VZ
1372 }
1373
1374 putchar('.');
1375 }
1376
1377 puts(", ok.");
d31b7b68
VZ
1378}
1379
1380#endif // TEST_TIMER
1381
f6bcfd97
BP
1382// ----------------------------------------------------------------------------
1383// vCard support
1384// ----------------------------------------------------------------------------
1385
1386#ifdef TEST_VCARD
1387
1388#include <wx/vcard.h>
1389
1390static void DumpVObject(size_t level, const wxVCardObject& vcard)
1391{
1392 void *cookie;
1393 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
1394 while ( vcObj )
1395 {
1396 printf("%s%s",
1397 wxString(_T('\t'), level).c_str(),
1398 vcObj->GetName().c_str());
1399
1400 wxString value;
1401 switch ( vcObj->GetType() )
1402 {
1403 case wxVCardObject::String:
1404 case wxVCardObject::UString:
1405 {
1406 wxString val;
1407 vcObj->GetValue(&val);
1408 value << _T('"') << val << _T('"');
1409 }
1410 break;
1411
1412 case wxVCardObject::Int:
1413 {
1414 unsigned int i;
1415 vcObj->GetValue(&i);
1416 value.Printf(_T("%u"), i);
1417 }
1418 break;
1419
1420 case wxVCardObject::Long:
1421 {
1422 unsigned long l;
1423 vcObj->GetValue(&l);
1424 value.Printf(_T("%lu"), l);
1425 }
1426 break;
1427
1428 case wxVCardObject::None:
1429 break;
1430
1431 case wxVCardObject::Object:
1432 value = _T("<node>");
1433 break;
1434
1435 default:
1436 value = _T("<unknown value type>");
1437 }
1438
1439 if ( !!value )
1440 printf(" = %s", value.c_str());
1441 putchar('\n');
1442
1443 DumpVObject(level + 1, *vcObj);
1444
1445 delete vcObj;
1446 vcObj = vcard.GetNextProp(&cookie);
1447 }
1448}
1449
1450static void DumpVCardAddresses(const wxVCard& vcard)
1451{
1452 puts("\nShowing all addresses from vCard:\n");
1453
1454 size_t nAdr = 0;
1455 void *cookie;
1456 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
1457 while ( addr )
1458 {
1459 wxString flagsStr;
1460 int flags = addr->GetFlags();
1461 if ( flags & wxVCardAddress::Domestic )
1462 {
1463 flagsStr << _T("domestic ");
1464 }
1465 if ( flags & wxVCardAddress::Intl )
1466 {
1467 flagsStr << _T("international ");
1468 }
1469 if ( flags & wxVCardAddress::Postal )
1470 {
1471 flagsStr << _T("postal ");
1472 }
1473 if ( flags & wxVCardAddress::Parcel )
1474 {
1475 flagsStr << _T("parcel ");
1476 }
1477 if ( flags & wxVCardAddress::Home )
1478 {
1479 flagsStr << _T("home ");
1480 }
1481 if ( flags & wxVCardAddress::Work )
1482 {
1483 flagsStr << _T("work ");
1484 }
1485
1486 printf("Address %u:\n"
1487 "\tflags = %s\n"
1488 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
1489 ++nAdr,
1490 flagsStr.c_str(),
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()
1498 );
1499
1500 delete addr;
1501 addr = vcard.GetNextAddress(&cookie);
1502 }
1503}
1504
1505static void DumpVCardPhoneNumbers(const wxVCard& vcard)
1506{
1507 puts("\nShowing all phone numbers from vCard:\n");
1508
1509 size_t nPhone = 0;
1510 void *cookie;
1511 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
1512 while ( phone )
1513 {
1514 wxString flagsStr;
1515 int flags = phone->GetFlags();
1516 if ( flags & wxVCardPhoneNumber::Voice )
1517 {
1518 flagsStr << _T("voice ");
1519 }
1520 if ( flags & wxVCardPhoneNumber::Fax )
1521 {
1522 flagsStr << _T("fax ");
1523 }
1524 if ( flags & wxVCardPhoneNumber::Cellular )
1525 {
1526 flagsStr << _T("cellular ");
1527 }
1528 if ( flags & wxVCardPhoneNumber::Modem )
1529 {
1530 flagsStr << _T("modem ");
1531 }
1532 if ( flags & wxVCardPhoneNumber::Home )
1533 {
1534 flagsStr << _T("home ");
1535 }
1536 if ( flags & wxVCardPhoneNumber::Work )
1537 {
1538 flagsStr << _T("work ");
1539 }
1540
1541 printf("Phone number %u:\n"
1542 "\tflags = %s\n"
1543 "\tvalue = %s\n",
1544 ++nPhone,
1545 flagsStr.c_str(),
1546 phone->GetNumber().c_str()
1547 );
1548
1549 delete phone;
1550 phone = vcard.GetNextPhoneNumber(&cookie);
1551 }
1552}
1553
1554static void TestVCardRead()
1555{
1556 puts("*** Testing wxVCard reading ***\n");
1557
1558 wxVCard vcard(_T("vcard.vcf"));
1559 if ( !vcard.IsOk() )
1560 {
1561 puts("ERROR: couldn't load vCard.");
1562 }
1563 else
1564 {
1565 // read individual vCard properties
1566 wxVCardObject *vcObj = vcard.GetProperty("FN");
1567 wxString value;
1568 if ( vcObj )
1569 {
1570 vcObj->GetValue(&value);
1571 delete vcObj;
1572 }
1573 else
1574 {
1575 value = _T("<none>");
1576 }
1577
1578 printf("Full name retrieved directly: %s\n", value.c_str());
1579
1580
1581 if ( !vcard.GetFullName(&value) )
1582 {
1583 value = _T("<none>");
1584 }
1585
1586 printf("Full name from wxVCard API: %s\n", value.c_str());
1587
1588 // now show how to deal with multiply occuring properties
1589 DumpVCardAddresses(vcard);
1590 DumpVCardPhoneNumbers(vcard);
1591
1592 // and finally show all
1593 puts("\nNow dumping the entire vCard:\n"
1594 "-----------------------------\n");
1595
1596 DumpVObject(0, vcard);
1597 }
1598}
1599
1600static void TestVCardWrite()
1601{
1602 puts("*** Testing wxVCard writing ***\n");
1603
1604 wxVCard vcard;
1605 if ( !vcard.IsOk() )
1606 {
1607 puts("ERROR: couldn't create vCard.");
1608 }
1609 else
1610 {
1611 // set some fields
1612 vcard.SetName("Zeitlin", "Vadim");
1613 vcard.SetFullName("Vadim Zeitlin");
1614 vcard.SetOrganization("wxWindows", "R&D");
1615
1616 // just dump the vCard back
1617 puts("Entire vCard follows:\n");
1618 puts(vcard.Write());
1619 }
1620}
1621
1622#endif // TEST_VCARD
1623
1624// ----------------------------------------------------------------------------
1625// wide char (Unicode) support
1626// ----------------------------------------------------------------------------
1627
1628#ifdef TEST_WCHAR
1629
1630#include <wx/strconv.h>
1631#include <wx/buffer.h>
1632
1633static void TestUtf8()
1634{
1635 puts("*** Testing UTF8 support ***\n");
1636
1637 wxString testString = "français";
1638#if 0
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 ! :)";
1642#endif
1643
1644 wxWCharBuffer wchBuf = testString.wc_str(wxConvUTF8);
1645 const wchar_t *pwz = (const wchar_t *)wchBuf;
1646 wxString testString2(pwz, wxConvLocal);
1647
1648 printf("Decoding '%s' => '%s'\n", testString.c_str(), testString2.c_str());
1649
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++ )
1654 {
1655 pwz2[n] = (wchar_t)(unsigned char)psz[n];
1656 }
1657
1658 wxString testString3(pwz2, wxConvUTF8);
1659 delete [] pwz2;
1660
1661 printf("Encoding '%s' -> '%s'\n", psz, testString3.c_str());
1662}
1663
1664#endif // TEST_WCHAR
1665
1666// ----------------------------------------------------------------------------
1667// ZIP stream
1668// ----------------------------------------------------------------------------
1669
1670#ifdef TEST_ZIP
1671
1672#include "wx/zipstrm.h"
1673
1674static void TestZipStreamRead()
1675{
1676 puts("*** Testing ZIP reading ***\n");
1677
1678 wxZipInputStream istr(_T("idx.zip"), _T("IDX.txt"));
1679 printf("Archive size: %u\n", istr.GetSize());
1680
1681 puts("Dumping the file:");
1682 while ( !istr.Eof() )
1683 {
1684 putchar(istr.GetC());
1685 fflush(stdout);
1686 }
1687
1688 puts("\n----- done ------");
1689}
1690
1691#endif // TEST_ZIP
1692
3ca6a5f0
BP
1693// ----------------------------------------------------------------------------
1694// ZLIB stream
1695// ----------------------------------------------------------------------------
1696
1697#ifdef TEST_ZLIB
1698
1699#include <wx/zstream.h>
1700#include <wx/wfstream.h>
1701
1702static const wxChar *FILENAME_GZ = _T("test.gz");
1703static const char *TEST_DATA = "hello and hello again";
1704
1705static void TestZlibStreamWrite()
1706{
1707 puts("*** Testing Zlib stream reading ***\n");
1708
1709 wxFileOutputStream fileOutStream(FILENAME_GZ);
1710 wxZlibOutputStream ostr(fileOutStream, 0);
1711 printf("Compressing the test string... ");
1712 ostr.Write(TEST_DATA, sizeof(TEST_DATA));
1713 if ( !ostr )
1714 {
1715 puts("(ERROR: failed)");
1716 }
1717 else
1718 {
1719 puts("(ok)");
1720 }
1721
1722 puts("\n----- done ------");
1723}
1724
1725static void TestZlibStreamRead()
1726{
1727 puts("*** Testing Zlib stream reading ***\n");
1728
1729 wxFileInputStream fileInStream(FILENAME_GZ);
1730 wxZlibInputStream istr(fileInStream);
1731 printf("Archive size: %u\n", istr.GetSize());
1732
1733 puts("Dumping the file:");
1734 while ( !istr.Eof() )
1735 {
1736 putchar(istr.GetC());
1737 fflush(stdout);
1738 }
1739
1740 puts("\n----- done ------");
1741}
1742
1743#endif // TEST_ZLIB
1744
b76b015e
VZ
1745// ----------------------------------------------------------------------------
1746// date time
1747// ----------------------------------------------------------------------------
1748
d31b7b68 1749#ifdef TEST_DATETIME
b76b015e 1750
97e0ceea
VZ
1751#include <wx/date.h>
1752
b76b015e
VZ
1753#include <wx/datetime.h>
1754
299fcbfe
VZ
1755// the test data
1756struct Date
1757{
1758 wxDateTime::wxDateTime_t day;
1759 wxDateTime::Month month;
1760 int year;
1761 wxDateTime::wxDateTime_t hour, min, sec;
1762 double jdn;
211c2250 1763 wxDateTime::WeekDay wday;
299fcbfe
VZ
1764 time_t gmticks, ticks;
1765
1766 void Init(const wxDateTime::Tm& tm)
1767 {
1768 day = tm.mday;
1769 month = tm.mon;
1770 year = tm.year;
1771 hour = tm.hour;
1772 min = tm.min;
1773 sec = tm.sec;
1774 jdn = 0.0;
1775 gmticks = ticks = -1;
1776 }
1777
1778 wxDateTime DT() const
1779 { return wxDateTime(day, month, year, hour, min, sec); }
1780
239446b4
VZ
1781 bool SameDay(const wxDateTime::Tm& tm) const
1782 {
1783 return day == tm.mday && month == tm.mon && year == tm.year;
1784 }
1785
299fcbfe
VZ
1786 wxString Format() const
1787 {
1788 wxString s;
1789 s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
1790 hour, min, sec,
1791 wxDateTime::GetMonthName(month).c_str(),
1792 day,
1793 abs(wxDateTime::ConvertYearToBC(year)),
1794 year > 0 ? "AD" : "BC");
1795 return s;
1796 }
239446b4
VZ
1797
1798 wxString FormatDate() const
1799 {
1800 wxString s;
1801 s.Printf("%02d-%s-%4d%s",
1802 day,
f0f951fa 1803 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
239446b4
VZ
1804 abs(wxDateTime::ConvertYearToBC(year)),
1805 year > 0 ? "AD" : "BC");
1806 return s;
1807 }
299fcbfe
VZ
1808};
1809
1810static const Date testDates[] =
1811{
211c2250
VZ
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 },
239446b4
VZ
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 },
211c2250 1826 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
299fcbfe
VZ
1827};
1828
2f02cb89
VZ
1829// this test miscellaneous static wxDateTime functions
1830static void TestTimeStatic()
1831{
1832 puts("\n*** wxDateTime static methods test ***");
1833
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",
1837 year,
1838 wxDateTime::IsLeapYear(year) ? "" : "not ",
1839 wxDateTime::GetNumberOfDays(year));
1840
1841 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
1842 printf("Current month is '%s' ('%s') and it has %d days\n",
f0f951fa 1843 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
2f02cb89
VZ
1844 wxDateTime::GetMonthName(month).c_str(),
1845 wxDateTime::GetNumberOfDays(month));
1846
1847 // leap year logic
fcc3d7cb
VZ
1848 static const size_t nYears = 5;
1849 static const size_t years[2][nYears] =
2f02cb89
VZ
1850 {
1851 // first line: the years to test
1852 { 1990, 1976, 2000, 2030, 1984, },
1853
1854 // second line: TRUE if leap, FALSE otherwise
1855 { FALSE, TRUE, TRUE, FALSE, TRUE }
1856 };
1857
1858 for ( size_t n = 0; n < nYears; n++ )
1859 {
1860 int year = years[0][n];
239446b4
VZ
1861 bool should = years[1][n] != 0,
1862 is = wxDateTime::IsLeapYear(year);
2f02cb89 1863
239446b4 1864 printf("Year %d is %sa leap year (%s)\n",
2f02cb89 1865 year,
239446b4
VZ
1866 is ? "" : "not ",
1867 should == is ? "ok" : "ERROR");
2f02cb89
VZ
1868
1869 wxASSERT( should == wxDateTime::IsLeapYear(year) );
1870 }
1871}
1872
1873// test constructing wxDateTime objects
1874static void TestTimeSet()
1875{
1876 puts("\n*** wxDateTime construction test ***");
1877
299fcbfe
VZ
1878 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1879 {
1880 const Date& d1 = testDates[n];
1881 wxDateTime dt = d1.DT();
1882
1883 Date d2;
1884 d2.Init(dt.GetTm());
1885
1886 wxString s1 = d1.Format(),
1887 s2 = d2.Format();
1888
1889 printf("Date: %s == %s (%s)\n",
1890 s1.c_str(), s2.c_str(),
1891 s1 == s2 ? "ok" : "ERROR");
1892 }
2f02cb89
VZ
1893}
1894
fcc3d7cb
VZ
1895// test time zones stuff
1896static void TestTimeZones()
1897{
1898 puts("\n*** wxDateTime timezone test ***");
1899
1900 wxDateTime now = wxDateTime::Now();
1901
299fcbfe
VZ
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());
9d9b7755
VZ
1908
1909 wxDateTime::Tm tm = now.GetTm();
1910 if ( wxDateTime(tm) != now )
1911 {
1912 printf("ERROR: got %s instead of %s\n",
1913 wxDateTime(tm).Format().c_str(), now.Format().c_str());
1914 }
fcc3d7cb
VZ
1915}
1916
e6ec579c
VZ
1917// test some minimal support for the dates outside the standard range
1918static void TestTimeRange()
1919{
1920 puts("\n*** wxDateTime out-of-standard-range dates test ***");
1921
211c2250
VZ
1922 static const char *fmt = "%d-%b-%Y %H:%M:%S";
1923
1ef54dcf 1924 printf("Unix epoch:\t%s\n",
211c2250 1925 wxDateTime(2440587.5).Format(fmt).c_str());
1ef54dcf 1926 printf("Feb 29, 0: \t%s\n",
211c2250 1927 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
e6ec579c 1928 printf("JDN 0: \t%s\n",
211c2250 1929 wxDateTime(0.0).Format(fmt).c_str());
e6ec579c 1930 printf("Jan 1, 1AD:\t%s\n",
211c2250 1931 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
e6ec579c 1932 printf("May 29, 2099:\t%s\n",
211c2250 1933 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
e6ec579c
VZ
1934}
1935
299fcbfe 1936static void TestTimeTicks()
e6ec579c 1937{
299fcbfe 1938 puts("\n*** wxDateTime ticks test ***");
e6ec579c 1939
299fcbfe 1940 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1ef54dcf 1941 {
299fcbfe
VZ
1942 const Date& d = testDates[n];
1943 if ( d.ticks == -1 )
1944 continue;
1ef54dcf 1945
299fcbfe
VZ
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 )
1950 {
1951 puts(" (ok)");
1952 }
1953 else
1954 {
1955 printf(" (ERROR: should be %ld, delta = %ld)\n",
1956 d.ticks, ticks - d.ticks);
1957 }
1958
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 )
1963 {
1964 puts(" (ok)");
1965 }
1966 else
1967 {
1968 printf(" (ERROR: should be %ld, delta = %ld)\n",
1969 d.gmticks, ticks - d.gmticks);
1970 }
1971 }
1972
1973 puts("");
1974}
1975
1976// test conversions to JDN &c
1977static void TestTimeJDN()
1978{
1979 puts("\n*** wxDateTime to JDN test ***");
1ef54dcf
VZ
1980
1981 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1982 {
1983 const Date& d = testDates[n];
299fcbfe 1984 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
1ef54dcf
VZ
1985 double jdn = dt.GetJulianDayNumber();
1986
299fcbfe 1987 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
1ef54dcf
VZ
1988 if ( jdn == d.jdn )
1989 {
1990 puts(" (ok)");
1991 }
1992 else
1993 {
1994 printf(" (ERROR: should be %f, delta = %f)\n",
1995 d.jdn, jdn - d.jdn);
1996 }
1997 }
e6ec579c
VZ
1998}
1999
211c2250
VZ
2000// test week days computation
2001static void TestTimeWDays()
2002{
2003 puts("\n*** wxDateTime weekday test ***");
2004
239446b4
VZ
2005 // test GetWeekDay()
2006 size_t n;
2007 for ( n = 0; n < WXSIZEOF(testDates); n++ )
211c2250
VZ
2008 {
2009 const Date& d = testDates[n];
2010 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
2011
2012 wxDateTime::WeekDay wday = dt.GetWeekDay();
2013 printf("%s is: %s",
2014 d.Format().c_str(),
239446b4 2015 wxDateTime::GetWeekDayName(wday).c_str());
211c2250
VZ
2016 if ( wday == d.wday )
2017 {
2018 puts(" (ok)");
2019 }
2020 else
2021 {
2022 printf(" (ERROR: should be %s)\n",
239446b4
VZ
2023 wxDateTime::GetWeekDayName(d.wday).c_str());
2024 }
2025 }
2026
2027 puts("");
2028
2029 // test SetToWeekDay()
2030 struct WeekDateTestData
2031 {
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
2037
2038 wxString Format() const
2039 {
2040 wxString s, which;
2041 switch ( nWeek < -1 ? -nWeek : nWeek )
2042 {
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;
2048
2049 case -1: which = "last"; break;
2050 }
2051
2052 if ( nWeek < -1 )
2053 {
2054 which += " from end";
2055 }
2056
2057 s.Printf("The %s %s of %s in %d",
2058 which.c_str(),
2059 wxDateTime::GetWeekDayName(wday).c_str(),
2060 wxDateTime::GetMonthName(month).c_str(),
2061 year);
2062
2063 return s;
2064 }
2065 };
2066
2067 // the array data was generated by the following python program
2068 /*
2069from DateTime import *
2070from whrandom import *
2071from string import *
2072
2073monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2074wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2075
2076week = DateTimeDelta(7)
2077
2078for n in range(20):
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
2084
2085 countFromEnd = choice([-1, 1])
2086 weekNum = 0;
2087
2088 while dt.month is month:
2089 dt = dt - countFromEnd * week
2090 weekNum = weekNum + countFromEnd
2091
2092 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
97e0ceea 2093
239446b4
VZ
2094 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
2095 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
97e0ceea 2096 */
239446b4
VZ
2097
2098 static const WeekDateTestData weekDatesTestData[] =
2099 {
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 },
2120 };
2121
2122 static const char *fmt = "%d-%b-%Y";
2123
2124 wxDateTime dt;
2125 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
2126 {
2127 const WeekDateTestData& wd = weekDatesTestData[n];
2128
2129 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
2130
2131 printf("%s is %s", wd.Format().c_str(), dt.Format(fmt).c_str());
2132
2133 const Date& d = wd.date;
2134 if ( d.SameDay(dt.GetTm()) )
2135 {
2136 puts(" (ok)");
2137 }
2138 else
2139 {
2140 dt.Set(d.day, d.month, d.year);
2141
2142 printf(" (ERROR: should be %s)\n", dt.Format(fmt).c_str());
211c2250
VZ
2143 }
2144 }
2145}
2146
239446b4
VZ
2147// test the computation of (ISO) week numbers
2148static void TestTimeWNumber()
2149{
2150 puts("\n*** wxDateTime week number test ***");
2151
2152 struct WeekNumberTestData
2153 {
2154 Date date; // the date
9d9b7755
VZ
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
239446b4
VZ
2158 wxDateTime::wxDateTime_t dnum; // day number in the year
2159 };
2160
2161 // data generated with the following python script:
2162 /*
2163from DateTime import *
2164from whrandom import *
2165from string import *
2166
2167monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
2168wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
2169
9d9b7755
VZ
2170def 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
2174 return weekNumMonth
7c968cee 2175
9d9b7755
VZ
2176def GetLastSundayBefore(dt):
2177 if dt.iso_week[2] == 7:
2178 return dt
2179 else:
2180 return dt - DateTimeDelta(dt.iso_week[2])
2181
239446b4
VZ
2182for n in range(20):
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]
9d9b7755
VZ
2189 weekNumMonth = GetMonthWeek(dt)
2190
2191 weekNumMonth2 = 0
2192 dtSunday = GetLastSundayBefore(dt)
2193
2194 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
2195 weekNumMonth2 = weekNumMonth2 + 1
2196 dtSunday = dtSunday - DateTimeDelta(7)
2197
2198 data = { 'day': rjust(`day`, 2), \
2199 'month': monthNames[month - 1], \
2200 'year': year, \
2201 'weekNum': rjust(`weekNum`, 2), \
2202 'weekNumMonth': weekNumMonth, \
2203 'weekNumMonth2': weekNumMonth2, \
2204 'dayNum': rjust(`dayNum`, 3) }
2205
2206 print " { { %(day)s, "\
2207 "wxDateTime::%(month)s, "\
2208 "%(year)d }, "\
2209 "%(weekNum)s, "\
2210 "%(weekNumMonth)s, "\
2211 "%(weekNumMonth2)s, "\
239446b4 2212 "%(dayNum)s }," % data
9d9b7755 2213
239446b4
VZ
2214 */
2215 static const WeekNumberTestData weekNumberTestDates[] =
2216 {
9d9b7755
VZ
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 },
239446b4
VZ
2237 };
2238
2239 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
2240 {
2241 const WeekNumberTestData& wn = weekNumberTestDates[n];
2242 const Date& d = wn.date;
2243
2244 wxDateTime dt = d.DT();
2245
9d9b7755
VZ
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();
239446b4
VZ
2251
2252 printf("%s: the day number is %d",
2253 d.FormatDate().c_str(), dnum);
2254 if ( dnum == wn.dnum )
2255 {
2256 printf(" (ok)");
2257 }
2258 else
2259 {
2260 printf(" (ERROR: should be %d)", wn.dnum);
2261 }
2262
9d9b7755
VZ
2263 printf(", week in month is %d", wmon);
2264 if ( wmon == wn.wmon )
2265 {
2266 printf(" (ok)");
2267 }
2268 else
2269 {
2270 printf(" (ERROR: should be %d)", wn.wmon);
2271 }
2272
2273 printf(" or %d", wmon2);
2274 if ( wmon2 == wn.wmon2 )
2275 {
2276 printf(" (ok)");
2277 }
2278 else
2279 {
2280 printf(" (ERROR: should be %d)", wn.wmon2);
2281 }
2282
2283 printf(", week in year is %d", week);
239446b4
VZ
2284 if ( week == wn.week )
2285 {
2286 puts(" (ok)");
2287 }
2288 else
2289 {
2290 printf(" (ERROR: should be %d)\n", wn.week);
2291 }
2292 }
2293}
2294
2295// test DST calculations
2296static void TestTimeDST()
2297{
2298 puts("\n*** wxDateTime DST test ***");
2299
2300 printf("DST is%s in effect now.\n\n",
2301 wxDateTime::Now().IsDST() ? "" : " not");
2302
2303 // taken from http://www.energy.ca.gov/daylightsaving.html
2304 static const Date datesDST[2][2004 - 1900 + 1] =
2305 {
2306 {
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 },
2322 },
2323 {
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 },
2339 }
2340 };
2341
2342 int year;
2343 for ( year = 1990; year < 2005; year++ )
2344 {
2345 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
2346 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
2347
2348 printf("DST period in the US for year %d: from %s to %s",
2349 year, dtBegin.Format().c_str(), dtEnd.Format().c_str());
2350
2351 size_t n = year - 1990;
2352 const Date& dBegin = datesDST[0][n];
2353 const Date& dEnd = datesDST[1][n];
97e0ceea 2354
239446b4
VZ
2355 if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) )
2356 {
2357 puts(" (ok)");
2358 }
2359 else
2360 {
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);
2364 }
2365 }
2366
2367 puts("");
2368
2369 for ( year = 1990; year < 2005; year++ )
2370 {
2371 printf("DST period in Europe for year %d: from %s to %s\n",
2372 year,
2373 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
2374 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
2375 }
2376}
2377
68ee7c47
VZ
2378// test wxDateTime -> text conversion
2379static void TestTimeFormat()
2380{
2381 puts("\n*** wxDateTime formatting test ***");
2382
b38e2f7d
VZ
2383 // some information may be lost during conversion, so store what kind
2384 // of info should we recover after a round trip
2385 enum CompareKind
68ee7c47 2386 {
b38e2f7d
VZ
2387 CompareNone, // don't try comparing
2388 CompareBoth, // dates and times should be identical
2389 CompareDate, // dates only
2390 CompareTime // time only
2391 };
2392
2393 static const struct
2394 {
2395 CompareKind compareKind;
2396 const char *format;
2397 } formatTestFormats[] =
2398 {
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" },
f6bcfd97 2404 { CompareDate, "ISO date without separators: %4Y%2m%2d" },
68ee7c47
VZ
2405 };
2406
2407 static const Date formatTestDates[] =
2408 {
68ee7c47
VZ
2409 { 29, wxDateTime::May, 1976, 18, 30, 00 },
2410 { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
b38e2f7d
VZ
2411#if 0
2412 // this test can't work for other centuries because it uses two digit
2413 // years in formats, so don't even try it
68ee7c47
VZ
2414 { 29, wxDateTime::May, 2076, 18, 30, 00 },
2415 { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
2416 { 01, wxDateTime::Jan, -52, 03, 16, 47 },
b38e2f7d 2417#endif
68ee7c47
VZ
2418 };
2419
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());
2422
b38e2f7d 2423 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
68ee7c47
VZ
2424 {
2425 puts("");
2426
b38e2f7d 2427 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
68ee7c47
VZ
2428 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
2429 {
b38e2f7d 2430 wxString s = dt.Format(formatTestFormats[n].format);
f0f951fa
VZ
2431 printf("%s", s.c_str());
2432
b38e2f7d
VZ
2433 // what can we recover?
2434 int kind = formatTestFormats[n].compareKind;
2435
f0f951fa
VZ
2436 // convert back
2437 wxDateTime dt2;
b38e2f7d 2438 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
f0f951fa
VZ
2439 if ( !result )
2440 {
b38e2f7d
VZ
2441 // converion failed - should it have?
2442 if ( kind == CompareNone )
2443 puts(" (ok)");
2444 else
2445 puts(" (ERROR: conversion back failed)");
f0f951fa
VZ
2446 }
2447 else if ( *result )
2448 {
2449 // should have parsed the entire string
2450 puts(" (ERROR: conversion back stopped too soon)");
2451 }
f0f951fa
VZ
2452 else
2453 {
b38e2f7d
VZ
2454 bool equal = FALSE; // suppress compilaer warning
2455 switch ( kind )
2456 {
2457 case CompareBoth:
2458 equal = dt2 == dt;
2459 break;
2460
2461 case CompareDate:
2462 equal = dt.IsSameDate(dt2);
2463 break;
2464
2465 case CompareTime:
2466 equal = dt.IsSameTime(dt2);
2467 break;
2468 }
2469
2470 if ( !equal )
2471 {
2472 printf(" (ERROR: got back '%s' instead of '%s')\n",
2473 dt2.Format().c_str(), dt.Format().c_str());
2474 }
2475 else
2476 {
2477 puts(" (ok)");
2478 }
f0f951fa 2479 }
68ee7c47
VZ
2480 }
2481 }
2482}
2483
97e0ceea
VZ
2484// test text -> wxDateTime conversion
2485static void TestTimeParse()
2486{
2487 puts("\n*** wxDateTime parse test ***");
2488
2489 struct ParseTestData
2490 {
2491 const char *format;
2492 Date date;
2493 bool good;
2494 };
2495
2496 static const ParseTestData parseTestDates[] =
2497 {
68ee7c47
VZ
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 },
97e0ceea
VZ
2500 };
2501
2502 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
2503 {
2504 const char *format = parseTestDates[n].format;
2505
2506 printf("%s => ", format);
2507
2508 wxDateTime dt;
2509 if ( dt.ParseRfc822Date(format) )
2510 {
2511 printf("%s ", dt.Format().c_str());
2512
2513 if ( parseTestDates[n].good )
2514 {
2515 wxDateTime dtReal = parseTestDates[n].date.DT();
2516 if ( dt == dtReal )
2517 {
2518 puts("(ok)");
2519 }
2520 else
2521 {
2522 printf("(ERROR: should be %s)\n", dtReal.Format().c_str());
2523 }
2524 }
2525 else
2526 {
2527 puts("(ERROR: bad format)");
2528 }
2529 }
2530 else
2531 {
2532 printf("bad format (%s)\n",
2533 parseTestDates[n].good ? "ERROR" : "ok");
2534 }
2535 }
2536}
2537
9d9b7755
VZ
2538static void TestInteractive()
2539{
2540 puts("\n*** interactive wxDateTime tests ***");
2541
2542 char buf[128];
2543
2544 for ( ;; )
2545 {
2546 printf("Enter a date: ");
2547 if ( !fgets(buf, WXSIZEOF(buf), stdin) )
2548 break;
2549
f6bcfd97
BP
2550 // kill the last '\n'
2551 buf[strlen(buf) - 1] = 0;
2552
9d9b7755 2553 wxDateTime dt;
f6bcfd97
BP
2554 const char *p = dt.ParseDate(buf);
2555 if ( !p )
9d9b7755 2556 {
f6bcfd97 2557 printf("ERROR: failed to parse the date '%s'.\n", buf);
9d9b7755
VZ
2558
2559 continue;
2560 }
f6bcfd97
BP
2561 else if ( *p )
2562 {
2563 printf("WARNING: parsed only first %u characters.\n", p - buf);
2564 }
9d9b7755
VZ
2565
2566 printf("%s: day %u, week of month %u/%u, week of year %u\n",
f6bcfd97 2567 dt.Format("%b %d, %Y").c_str(),
9d9b7755
VZ
2568 dt.GetDayOfYear(),
2569 dt.GetWeekOfMonth(wxDateTime::Monday_First),
2570 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
2571 dt.GetWeekOfYear(wxDateTime::Monday_First));
2572 }
2573
2574 puts("\n*** done ***");
2575}
2576
f6bcfd97
BP
2577static void TestTimeMS()
2578{
2579 puts("*** testing millisecond-resolution support in wxDateTime ***");
2580
2581 wxDateTime dt1 = wxDateTime::Now(),
2582 dt2 = wxDateTime::UNow();
2583
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());
3ca6a5f0
BP
2586 printf("Dummy loop: ");
2587 for ( int i = 0; i < 6000; i++ )
2588 {
2589 //for ( int j = 0; j < 10; j++ )
2590 {
2591 wxString s;
2592 s.Printf("%g", sqrt(i));
2593 }
2594
2595 if ( !(i % 100) )
2596 putchar('.');
2597 }
2598 puts(", done");
2599
2600 dt1 = dt2;
2601 dt2 = wxDateTime::UNow();
2602 printf("UNow = %s\n", dt2.Format("%H:%M:%S:%l").c_str());
2603
2604 printf("Loop executed in %s ms\n", (dt2 - dt1).Format("%l").c_str());
f6bcfd97
BP
2605
2606 puts("\n*** done ***");
2607}
2608
9d9b7755
VZ
2609static void TestTimeArithmetics()
2610{
2611 puts("\n*** testing arithmetic operations on wxDateTime ***");
2612
f6bcfd97 2613 static const struct ArithmData
9d9b7755 2614 {
f6bcfd97
BP
2615 ArithmData(const wxDateSpan& sp, const char *nam)
2616 : span(sp), name(nam) { }
2617
9d9b7755
VZ
2618 wxDateSpan span;
2619 const char *name;
7c968cee 2620 } testArithmData[] =
9d9b7755 2621 {
f6bcfd97
BP
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"),
9d9b7755 2627 };
7c968cee 2628
9d9b7755
VZ
2629 wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2;
2630
2631 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
2632 {
2633 wxDateSpan span = testArithmData[n].span;
2634 dt1 = dt + span;
2635 dt2 = dt - span;
2636
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());
2641
2642 printf("Going back: %s", (dt1 - span).FormatISODate().c_str());
2643 if ( dt1 - span == dt )
2644 {
2645 puts(" (ok)");
2646 }
2647 else
2648 {
2649 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
2650 }
2651
2652 printf("Going forward: %s", (dt2 + span).FormatISODate().c_str());
2653 if ( dt2 + span == dt )
2654 {
2655 puts(" (ok)");
2656 }
2657 else
2658 {
2659 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
2660 }
2661
2662 printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str());
2663 if ( dt2 + 2*span == dt1 )
2664 {
2665 puts(" (ok)");
2666 }
2667 else
2668 {
2669 printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str());
2670 }
2671
2672 puts("");
2673 }
2674}
2675
0de868d9
VZ
2676static void TestTimeHolidays()
2677{
2678 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
2679
2680 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
2681 wxDateTime dtStart(1, tm.mon, tm.year),
2682 dtEnd = dtStart.GetLastMonthDay();
2683
2684 wxDateTimeArray hol;
2685 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
2686
2687 const wxChar *format = "%d-%b-%Y (%a)";
2688
2689 printf("All holidays between %s and %s:\n",
2690 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
2691
2692 size_t count = hol.GetCount();
2693 for ( size_t n = 0; n < count; n++ )
2694 {
2695 printf("\t%s\n", hol[n].Format(format).c_str());
2696 }
2697
2698 puts("");
2699}
2700
f6bcfd97
BP
2701static void TestTimeZoneBug()
2702{
2703 puts("\n*** testing for DST/timezone bug ***\n");
2704
2705 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
2706 for ( int i = 0; i < 31; i++ )
2707 {
2708 printf("Date %s: week day %s.\n",
2709 date.Format(_T("%d-%m-%Y")).c_str(),
2710 date.GetWeekDayName(date.GetWeekDay()).c_str());
2711
2712 date += wxDateSpan::Day();
2713 }
2714
2715 puts("");
2716}
2717
68ee7c47
VZ
2718#if 0
2719
97e0ceea
VZ
2720// test compatibility with the old wxDate/wxTime classes
2721static void TestTimeCompatibility()
2722{
2723 puts("\n*** wxDateTime compatibility test ***");
2724
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());
2727
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());
2731
2732 jdnMidnight = wxDate().Set().GetJulianDate();
2733 printf("wxDateTime for today: %s\n",
2734 wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str());
2735
2736 int flags = wxEUROPEAN;//wxFULL;
2737 wxDate date;
2738 date.Set();
2739 printf("Today is %s\n", date.FormatDate(flags).c_str());
2740 for ( int n = 0; n < 7; n++ )
2741 {
2742 printf("Previous %s is %s\n",
2743 wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n),
2744 date.Previous(n + 1).FormatDate(flags).c_str());
2745 }
2746}
2747
68ee7c47
VZ
2748#endif // 0
2749
d31b7b68 2750#endif // TEST_DATETIME
b76b015e 2751
e87271f3
VZ
2752// ----------------------------------------------------------------------------
2753// threads
2754// ----------------------------------------------------------------------------
2755
2756#ifdef TEST_THREADS
2757
bbfa0322 2758#include <wx/thread.h>
37667812 2759
bbfa0322
VZ
2760static size_t gs_counter = (size_t)-1;
2761static wxCriticalSection gs_critsect;
b568d04f 2762static wxCondition gs_cond;
bbfa0322 2763
b568d04f 2764class MyJoinableThread : public wxThread
bbfa0322
VZ
2765{
2766public:
b568d04f
VZ
2767 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
2768 { m_n = n; Create(); }
bbfa0322
VZ
2769
2770 // thread execution starts here
b568d04f 2771 virtual ExitCode Entry();
bbfa0322 2772
b568d04f
VZ
2773private:
2774 size_t m_n;
bbfa0322
VZ
2775};
2776
b568d04f 2777wxThread::ExitCode MyJoinableThread::Entry()
bbfa0322 2778{
b568d04f
VZ
2779 unsigned long res = 1;
2780 for ( size_t n = 1; n < m_n; n++ )
2781 {
2782 res *= n;
2783
2784 // it's a loooong calculation :-)
2785 Sleep(100);
2786 }
bbfa0322 2787
b568d04f 2788 return (ExitCode)res;
bbfa0322
VZ
2789}
2790
b568d04f
VZ
2791class MyDetachedThread : public wxThread
2792{
2793public:
fcc3d7cb
VZ
2794 MyDetachedThread(size_t n, char ch)
2795 {
2796 m_n = n;
2797 m_ch = ch;
2798 m_cancelled = FALSE;
2799
2800 Create();
2801 }
b568d04f
VZ
2802
2803 // thread execution starts here
2804 virtual ExitCode Entry();
2805
2806 // and stops here
2807 virtual void OnExit();
2808
2809private:
9fc3ad34
VZ
2810 size_t m_n; // number of characters to write
2811 char m_ch; // character to write
fcc3d7cb
VZ
2812
2813 bool m_cancelled; // FALSE if we exit normally
b568d04f
VZ
2814};
2815
2816wxThread::ExitCode MyDetachedThread::Entry()
bbfa0322
VZ
2817{
2818 {
2819 wxCriticalSectionLocker lock(gs_critsect);
2820 if ( gs_counter == (size_t)-1 )
2821 gs_counter = 1;
2822 else
2823 gs_counter++;
2824 }
2825
9fc3ad34 2826 for ( size_t n = 0; n < m_n; n++ )
bbfa0322
VZ
2827 {
2828 if ( TestDestroy() )
fcc3d7cb
VZ
2829 {
2830 m_cancelled = TRUE;
2831
bbfa0322 2832 break;
fcc3d7cb 2833 }
bbfa0322
VZ
2834
2835 putchar(m_ch);
2836 fflush(stdout);
2837
2838 wxThread::Sleep(100);
2839 }
2840
b568d04f 2841 return 0;
bbfa0322
VZ
2842}
2843
b568d04f 2844void MyDetachedThread::OnExit()
bbfa0322 2845{
9fc3ad34
VZ
2846 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
2847
bbfa0322 2848 wxCriticalSectionLocker lock(gs_critsect);
fcc3d7cb 2849 if ( !--gs_counter && !m_cancelled )
b568d04f 2850 gs_cond.Signal();
bbfa0322
VZ
2851}
2852
9fc3ad34
VZ
2853void TestDetachedThreads()
2854{
2f02cb89 2855 puts("\n*** Testing detached threads ***");
9fc3ad34
VZ
2856
2857 static const size_t nThreads = 3;
2858 MyDetachedThread *threads[nThreads];
2859 size_t n;
2860 for ( n = 0; n < nThreads; n++ )
2861 {
2862 threads[n] = new MyDetachedThread(10, 'A' + n);
2863 }
2864
2865 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
2866 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
2867
2868 for ( n = 0; n < nThreads; n++ )
2869 {
2870 threads[n]->Run();
2871 }
2872
2873 // wait until all threads terminate
2874 gs_cond.Wait();
2875
2876 puts("");
2877}
2878
2879void TestJoinableThreads()
2880{
2f02cb89 2881 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
9fc3ad34
VZ
2882
2883 // calc 10! in the background
2884 MyJoinableThread thread(10);
2885 thread.Run();
2886
2887 printf("\nThread terminated with exit code %lu.\n",
2888 (unsigned long)thread.Wait());
2889}
2890
2891void TestThreadSuspend()
2892{
2f02cb89
VZ
2893 puts("\n*** Testing thread suspend/resume functions ***");
2894
2895 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
9fc3ad34
VZ
2896
2897 thread->Run();
2898
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
2904 // in an error)
2905 wxThread::Sleep(300);
2906
2907 for ( size_t n = 0; n < 3; n++ )
2908 {
2909 thread->Pause();
2910
2911 puts("\nThread suspended");
2912 if ( n > 0 )
2913 {
2914 // don't sleep but resume immediately the first time
2915 wxThread::Sleep(300);
2916 }
2917 puts("Going to resume the thread");
2918
2919 thread->Resume();
2920 }
2921
4c460b34
VZ
2922 puts("Waiting until it terminates now");
2923
9fc3ad34
VZ
2924 // wait until the thread terminates
2925 gs_cond.Wait();
2926
2927 puts("");
2928}
2929
2f02cb89
VZ
2930void TestThreadDelete()
2931{
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!
2936
2937 puts("\n*** Testing thread delete function ***");
2938
4c460b34
VZ
2939 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
2940
2941 thread0->Delete();
2942
2943 puts("\nDeleted a thread which didn't start to run yet.");
2944
2f02cb89
VZ
2945 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
2946
2947 thread1->Run();
2948
2949 wxThread::Sleep(300);
2950
2951 thread1->Delete();
2952
2953 puts("\nDeleted a running thread.");
2954
2955 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
2956
2957 thread2->Run();
2958
2959 wxThread::Sleep(300);
2960
2961 thread2->Pause();
2962
2963 thread2->Delete();
2964
2965 puts("\nDeleted a sleeping thread.");
2966
4c460b34
VZ
2967 MyJoinableThread thread3(20);
2968 thread3.Run();
2f02cb89 2969
4c460b34 2970 thread3.Delete();
2f02cb89
VZ
2971
2972 puts("\nDeleted a joinable thread.");
2973
4c460b34
VZ
2974 MyJoinableThread thread4(2);
2975 thread4.Run();
2f02cb89
VZ
2976
2977 wxThread::Sleep(300);
2978
4c460b34 2979 thread4.Delete();
2f02cb89
VZ
2980
2981 puts("\nDeleted a joinable thread which already terminated.");
2982
2983 puts("");
2984}
2985
e87271f3
VZ
2986#endif // TEST_THREADS
2987
2988// ----------------------------------------------------------------------------
2989// arrays
2990// ----------------------------------------------------------------------------
2991
2992#ifdef TEST_ARRAYS
2993
f6bcfd97 2994static void PrintArray(const char* name, const wxArrayString& array)
e87271f3
VZ
2995{
2996 printf("Dump of the array '%s'\n", name);
2997
2998 size_t nCount = array.GetCount();
2999 for ( size_t n = 0; n < nCount; n++ )
3000 {
3001 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
3002 }
3003}
3004
f6bcfd97
BP
3005static int StringLenCompare(const wxString& first, const wxString& second)
3006{
3007 return first.length() - second.length();
3008}
3009
3010#include "wx/dynarray.h"
3011
3012WX_DECLARE_OBJARRAY(Bar, ArrayBars);
3013#include "wx/arrimpl.cpp"
3014WX_DEFINE_OBJARRAY(ArrayBars);
3015
3016static void TestArrayOfObjects()
3017{
3018 puts("*** Testing wxObjArray ***\n");
3019
3020 {
3021 ArrayBars bars;
3022 Bar bar("second bar");
3023
3024 printf("Initially: %u objects in the array, %u objects total.\n",
3025 bars.GetCount(), Bar::GetNumber());
3026
3027 bars.Add(new Bar("first bar"));
3028 bars.Add(bar);
3029
3030 printf("Now: %u objects in the array, %u objects total.\n",
3031 bars.GetCount(), Bar::GetNumber());
3032
3033 bars.Empty();
3034
3035 printf("After Empty(): %u objects in the array, %u objects total.\n",
3036 bars.GetCount(), Bar::GetNumber());
3037 }
3038
3039 printf("Finally: no more objects in the array, %u objects total.\n",
3040 Bar::GetNumber());
3041}
3042
e87271f3
VZ
3043#endif // TEST_ARRAYS
3044
9fc3ad34
VZ
3045// ----------------------------------------------------------------------------
3046// strings
3047// ----------------------------------------------------------------------------
3048
3049#ifdef TEST_STRINGS
3050
3051#include "wx/timer.h"
bbf8fc53 3052#include "wx/tokenzr.h"
9fc3ad34 3053
7c968cee
VZ
3054static void TestStringConstruction()
3055{
3056 puts("*** Testing wxString constructores ***");
3057
3058 #define TEST_CTOR(args, res) \
3059 { \
3060 wxString s args ; \
3061 printf("wxString%s = %s ", #args, s.c_str()); \
3062 if ( s == res ) \
3063 { \
3064 puts("(ok)"); \
3065 } \
3066 else \
3067 { \
3068 printf("(ERROR: should be %s)\n", res); \
3069 } \
3070 }
3071
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
3076
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"));
3081
3082 puts("");
3083}
3084
299fcbfe 3085static void TestString()
9fc3ad34
VZ
3086{
3087 wxStopWatch sw;
3088
3089 wxString a, b, c;
3090
3091 a.reserve (128);
3092 b.reserve (128);
3093 c.reserve (128);
3094
3095 for (int i = 0; i < 1000000; ++i)
3096 {
3097 a = "Hello";
3098 b = " world";
3099 c = "! How'ya doin'?";
3100 a += b;
3101 a += c;
3102 c = "Hello world! What's up?";
3103 if (c != a)
3104 c = "Doh!";
3105 }
3106
3107 printf ("TestString elapsed time: %ld\n", sw.Time());
3108}
3109
299fcbfe 3110static void TestPChar()
9fc3ad34
VZ
3111{
3112 wxStopWatch sw;
3113
3114 char a [128];
3115 char b [128];
3116 char c [128];
3117
3118 for (int i = 0; i < 1000000; ++i)
3119 {
3120 strcpy (a, "Hello");
3121 strcpy (b, " world");
3122 strcpy (c, "! How'ya doin'?");
3123 strcat (a, b);
3124 strcat (a, c);
3125 strcpy (c, "Hello world! What's up?");
3126 if (strcmp (c, a) == 0)
3127 strcpy (c, "Doh!");
3128 }
3129
3130 printf ("TestPChar elapsed time: %ld\n", sw.Time());
3131}
3132
299fcbfe
VZ
3133static void TestStringSub()
3134{
3135 wxString s("Hello, world!");
3136
3137 puts("*** Testing wxString substring extraction ***");
3138
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());
3146
f6bcfd97
BP
3147 static const wxChar *prefixes[] =
3148 {
3149 _T("Hello"),
3150 _T("Hello, "),
3151 _T("Hello, world!"),
3152 _T("Hello, world!!!"),
3153 _T(""),
3154 _T("Goodbye"),
3155 _T("Hi"),
3156 };
3157
3158 for ( size_t n = 0; n < WXSIZEOF(prefixes); n++ )
3159 {
3160 wxString prefix = prefixes[n], rest;
3161 bool rc = s.StartsWith(prefix, &rest);
3162 printf("StartsWith('%s') = %s", prefix.c_str(), rc ? "TRUE" : "FALSE");
3163 if ( rc )
3164 {
3165 printf(" (the rest is '%s')\n", rest.c_str());
3166 }
3167 else
3168 {
3169 putchar('\n');
3170 }
3171 }
3172
299fcbfe
VZ
3173 puts("");
3174}
3175
f0f951fa
VZ
3176static void TestStringFormat()
3177{
3178 puts("*** Testing wxString formatting ***");
3179
3180 wxString s;
3181 s.Printf("%03d", 18);
3182
3183 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
3184 printf("Number 18: %s\n", s.c_str());
3185
3186 puts("");
3187}
3188
d71fa6fb
VZ
3189// returns "not found" for npos, value for all others
3190static wxString PosToString(size_t res)
3191{
3192 wxString s = res == wxString::npos ? wxString(_T("not found"))
3193 : wxString::Format(_T("%u"), res);
3194 return s;
3195}
3196
3197static void TestStringFind()
3198{
3199 puts("*** Testing wxString find() functions ***");
3200
3201 static const wxChar *strToFind = _T("ell");
3202 static const struct StringFindTest
3203 {
3204 const wxChar *str;
3205 size_t start,
3206 result; // of searching "ell" in str
3207 } findTestData[] =
3208 {
3209 { _T("Well, hello world"), 0, 1 },
3210 { _T("Well, hello world"), 6, 7 },
3211 { _T("Well, hello world"), 9, wxString::npos },
3212 };
3213
3214 for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
3215 {
3216 const StringFindTest& ft = findTestData[n];
3217 size_t res = wxString(ft.str).find(strToFind, ft.start);
3218
3219 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
3220 strToFind, ft.str, ft.start, PosToString(res).c_str());
3221
3222 size_t resTrue = ft.result;
3223 if ( res == resTrue )
3224 {
3225 puts(_T("(ok)"));
3226 }
3227 else
3228 {
3229 printf(_T("(ERROR: should be %s)\n"),
3230 PosToString(resTrue).c_str());
3231 }
3232 }
3233
3234 puts("");
3235}
3236
bbf8fc53
VZ
3237static void TestStringTokenizer()
3238{
3239 puts("*** Testing wxStringTokenizer ***");
3240
7c968cee
VZ
3241 static const wxChar *modeNames[] =
3242 {
3243 _T("default"),
3244 _T("return empty"),
3245 _T("return all empty"),
3246 _T("with delims"),
3247 _T("like strtok"),
3248 };
3249
bbf8fc53
VZ
3250 static const struct StringTokenizerTest
3251 {
7c968cee
VZ
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[] =
3257 {
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 },
bbf8fc53
VZ
3271 };
3272
3273 for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
3274 {
3275 const StringTokenizerTest& tt = tokenizerTestData[n];
7c968cee 3276 wxStringTokenizer tkz(tt.str, tt.delims, tt.mode);
bbf8fc53
VZ
3277
3278 size_t count = tkz.CountTokens();
7c968cee
VZ
3279 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
3280 MakePrintable(tt.str).c_str(),
bbf8fc53 3281 count,
7c968cee
VZ
3282 MakePrintable(tt.delims).c_str(),
3283 modeNames[tkz.GetMode()]);
bbf8fc53
VZ
3284 if ( count == tt.count )
3285 {
3286 puts(_T("(ok)"));
3287 }
3288 else
3289 {
3290 printf(_T("(ERROR: should be %u)\n"), tt.count);
3291
3292 continue;
3293 }
3294
7c968cee 3295 // if we emulate strtok(), check that we do it correctly
f6bcfd97 3296 wxChar *buf, *s = NULL, *last;
7c968cee
VZ
3297
3298 if ( tkz.GetMode() == wxTOKEN_STRTOK )
3299 {
3300 buf = new wxChar[wxStrlen(tt.str) + 1];
3301 wxStrcpy(buf, tt.str);
3302
3303 s = wxStrtok(buf, tt.delims, &last);
3304 }
3305 else
3306 {
3307 buf = NULL;
3308 }
3309
bbf8fc53
VZ
3310 // now show the tokens themselves
3311 size_t count2 = 0;
3312 while ( tkz.HasMoreTokens() )
3313 {
7c968cee
VZ
3314 wxString token = tkz.GetNextToken();
3315
3316 printf(_T("\ttoken %u: '%s'"),
bbf8fc53 3317 ++count2,
7c968cee
VZ
3318 MakePrintable(token).c_str());
3319
3320 if ( buf )
3321 {
3322 if ( token == s )
3323 {
3324 puts(" (ok)");
3325 }
3326 else
3327 {
3328 printf(" (ERROR: should be %s)\n", s);
3329 }
3330
3331 s = wxStrtok(NULL, tt.delims, &last);
3332 }
3333 else
3334 {
3335 // nothing to compare with
3336 puts("");
3337 }
bbf8fc53
VZ
3338 }
3339
3340 if ( count2 != count )
3341 {
7c968cee 3342 puts(_T("\tERROR: token count mismatch"));
bbf8fc53 3343 }
7c968cee
VZ
3344
3345 delete [] buf;
bbf8fc53
VZ
3346 }
3347
3348 puts("");
3349}
3350
f6bcfd97
BP
3351static void TestStringReplace()
3352{
3353 puts("*** Testing wxString::replace ***");
3354
3355 static const struct StringReplaceTestData
3356 {
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[] =
3362 {
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") },
3368 };
3369
3370 for ( size_t n = 0; n < WXSIZEOF(stringReplaceTestData); n++ )
3371 {
3372 const StringReplaceTestData data = stringReplaceTestData[n];
3373
3374 wxString original = data.original;
3375 original.replace(data.start, data.len, data.replacement);
3376
3377 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
3378 data.original, data.start, data.len, data.replacement,
3379 original.c_str());
3380
3381 if ( original == data.result )
3382 {
3383 puts("(ok)");
3384 }
3385 else
3386 {
3387 wxPrintf(_T("(ERROR: should be '%s')\n"), data.result);
3388 }
3389 }
3390
3391 puts("");
3392}
3393
9fc3ad34
VZ
3394#endif // TEST_STRINGS
3395
e87271f3
VZ
3396// ----------------------------------------------------------------------------
3397// entry point
3398// ----------------------------------------------------------------------------
3399
bbfa0322 3400int main(int argc, char **argv)
37667812
VZ
3401{
3402 if ( !wxInitialize() )
3403 {
3404 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
3405 }
3406
0de868d9
VZ
3407#ifdef TEST_USLEEP
3408 puts("Sleeping for 3 seconds... z-z-z-z-z...");
3409 wxUsleep(3000);
3410#endif // TEST_USLEEP
3411
d34bce84
VZ
3412#ifdef TEST_CMDLINE
3413 static const wxCmdLineEntryDesc cmdLineDesc[] =
3414 {
3415 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
3416 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
3417
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 },
1e245dc2 3421 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
d34bce84
VZ
3422
3423 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
3424 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
3425
3426 { wxCMD_LINE_NONE }
3427 };
3428
3429 wxCmdLineParser parser(cmdLineDesc, argc, argv);
3430
f6bcfd97
BP
3431 parser.AddOption("project_name", "", "full path to project file",
3432 wxCMD_LINE_VAL_STRING,
3433 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
3434
d34bce84
VZ
3435 switch ( parser.Parse() )
3436 {
3437 case -1:
3438 wxLogMessage("Help was given, terminating.");
3439 break;
3440
3441 case 0:
3442 ShowCmdLine(parser);
3443 break;
3444
3445 default:
3446 wxLogMessage("Syntax error detected, aborting.");
3447 break;
3448 }
3449#endif // TEST_CMDLINE
3450
9fc3ad34 3451#ifdef TEST_STRINGS
299fcbfe
VZ
3452 if ( 0 )
3453 {
3454 TestPChar();
3455 TestString();
3456 }
f6bcfd97 3457 TestStringSub();
f0f951fa
VZ
3458 if ( 0 )
3459 {
7c968cee 3460 TestStringConstruction();
d71fa6fb 3461 TestStringFormat();
bbf8fc53 3462 TestStringFind();
7c968cee 3463 TestStringTokenizer();
f6bcfd97 3464 TestStringReplace();
ee6e1b1d 3465 }
9fc3ad34
VZ
3466#endif // TEST_STRINGS
3467
e87271f3
VZ
3468#ifdef TEST_ARRAYS
3469 wxArrayString a1;
3470 a1.Add("tiger");
3471 a1.Add("cat");
3472 a1.Add("lion");
3473 a1.Add("dog");
3474 a1.Add("human");
3475 a1.Add("ape");
3476
3477 puts("*** Initially:");
3478
3479 PrintArray("a1", a1);
3480
3481 wxArrayString a2(a1);
3482 PrintArray("a2", a2);
3483
3484 wxSortedArrayString a3(a1);
3485 PrintArray("a3", a3);
3486
3487 puts("*** After deleting a string from a1");
3488 a1.Remove(2);
3489
3490 PrintArray("a1", a1);
3491 PrintArray("a2", a2);
3492 PrintArray("a3", a3);
3493
3494 puts("*** After reassigning a1 to a2 and a3");
3495 a3 = a2 = a1;
3496 PrintArray("a2", a2);
3497 PrintArray("a3", a3);
f6bcfd97
BP
3498
3499 puts("*** After sorting a1");
3500 a1.Sort();
3501 PrintArray("a1", a1);
3502
3503 puts("*** After sorting a1 in reverse order");
3504 a1.Sort(TRUE);
3505 PrintArray("a1", a1);
3506
3507 puts("*** After sorting a1 by the string length");
3508 a1.Sort(StringLenCompare);
3509 PrintArray("a1", a1);
3510
3511 TestArrayOfObjects();
e87271f3
VZ
3512#endif // TEST_ARRAYS
3513
1944c6bd
VZ
3514#ifdef TEST_DIR
3515 TestDirEnum();
3516#endif // TEST_DIR
3517
f6bcfd97
BP
3518#ifdef TEST_DLLLOADER
3519 TestDllLoad();
3520#endif // TEST_DLLLOADER
3521
d93c719a
VZ
3522#ifdef TEST_EXECUTE
3523 TestExecute();
3524#endif // TEST_EXECUTE
3525
ee6e1b1d
VZ
3526#ifdef TEST_FILECONF
3527 TestFileConfRead();
3528#endif // TEST_FILECONF
3529
f6bcfd97
BP
3530#ifdef TEST_LIST
3531 TestListCtor();
3532#endif // TEST_LIST
3533
378b05f7
VZ
3534#ifdef TEST_LOG
3535 wxString s;
3536 for ( size_t n = 0; n < 8000; n++ )
3537 {
3538 s << (char)('A' + (n % 26));
3539 }
3540
3541 wxString msg;
3542 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
3543
3544 // this one shouldn't be truncated
3545 printf(msg);
3546
3547 // but this one will because log functions use fixed size buffer
b568d04f
VZ
3548 // (note that it doesn't need '\n' at the end neither - will be added
3549 // by wxLog anyhow)
3550 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
378b05f7
VZ
3551#endif // TEST_LOG
3552
f6bcfd97 3553#ifdef TEST_FILE
3ca6a5f0
BP
3554 if ( 0 )
3555 TestFileRead();
f6bcfd97
BP
3556 TestTextFileRead();
3557#endif // TEST_FILE
3558
e87271f3 3559#ifdef TEST_THREADS
696e1ea0
VZ
3560 int nCPUs = wxThread::GetCPUCount();
3561 printf("This system has %d CPUs\n", nCPUs);
3562 if ( nCPUs != -1 )
3563 wxThread::SetConcurrency(nCPUs);
ef8d96c2 3564
9fc3ad34
VZ
3565 if ( argc > 1 && argv[1][0] == 't' )
3566 wxLog::AddTraceMask("thread");
b568d04f 3567
4c460b34 3568 if ( 1 )
2f02cb89 3569 TestDetachedThreads();
4c460b34 3570 if ( 1 )
2f02cb89 3571 TestJoinableThreads();
4c460b34 3572 if ( 1 )
2f02cb89
VZ
3573 TestThreadSuspend();
3574 if ( 1 )
3575 TestThreadDelete();
3576
e87271f3 3577#endif // TEST_THREADS
37667812 3578
b76b015e 3579#ifdef TEST_LONGLONG
2a310492
VZ
3580 // seed pseudo random generator
3581 srand((unsigned)time(NULL));
3582
b76b015e 3583 if ( 0 )
2a310492 3584 {
b76b015e 3585 TestSpeed();
2a310492 3586 }
2a310492
VZ
3587 if ( 0 )
3588 {
f6bcfd97 3589 TestMultiplication();
b76b015e 3590 TestDivision();
2a310492
VZ
3591 TestAddition();
3592 TestLongLongConversion();
3593 TestBitOperations();
3594 }
f6bcfd97 3595 TestLongLongComparison();
b76b015e
VZ
3596#endif // TEST_LONGLONG
3597
2c8e4738
VZ
3598#ifdef TEST_HASH
3599 TestHash();
3600#endif // TEST_HASH
3601
696e1ea0 3602#ifdef TEST_MIME
f6bcfd97
BP
3603 wxLog::AddTraceMask(_T("mime"));
3604 if ( 0 )
3605 TestMimeEnum();
3606 TestMimeOverride();
3607 TestMimeFilename();
696e1ea0
VZ
3608#endif // TEST_MIME
3609
89e60357
VZ
3610#ifdef TEST_INFO_FUNCTIONS
3611 TestOsInfo();
3612 TestUserInfo();
3613#endif // TEST_INFO_FUNCTIONS
3614
2c8e4738 3615#ifdef TEST_SOCKETS
ccdb23df 3616 if ( 0 )
8e907a13 3617 {
f6bcfd97 3618 TestSocketServer();
8e907a13 3619 TestSocketClient();
8e907a13 3620 TestProtocolFtp();
8dfea369 3621 }
f6bcfd97 3622 TestProtocolFtpUpload();
2c8e4738
VZ
3623#endif // TEST_SOCKETS
3624
83141d3a
VZ
3625#ifdef TEST_STREAMS
3626 TestMemoryStream();
3627#endif // TEST_STREAMS
3628
d31b7b68
VZ
3629#ifdef TEST_TIMER
3630 TestStopWatch();
3631#endif // TEST_TIMER
3632
3633#ifdef TEST_DATETIME
0de868d9 3634 if ( 0 )
299fcbfe 3635 {
9d9b7755
VZ
3636 TestTimeSet();
3637 TestTimeStatic();
3638 TestTimeRange();
3639 TestTimeZones();
3640 TestTimeTicks();
3641 TestTimeJDN();
3642 TestTimeDST();
3643 TestTimeWDays();
3644 TestTimeWNumber();
3645 TestTimeParse();
9d9b7755 3646 TestTimeArithmetics();
f6bcfd97
BP
3647 TestTimeHolidays();
3648 TestTimeFormat();
3ca6a5f0 3649 TestTimeMS();
f6bcfd97
BP
3650
3651 TestTimeZoneBug();
41acf5c0 3652 }
9d9b7755
VZ
3653 if ( 0 )
3654 TestInteractive();
d31b7b68 3655#endif // TEST_DATETIME
b76b015e 3656
f6bcfd97
BP
3657#ifdef TEST_VCARD
3658 if ( 0 )
3659 TestVCardRead();
3660 TestVCardWrite();
3661#endif // TEST_VCARD
3662
3663#ifdef TEST_WCHAR
3664 TestUtf8();
3665#endif // TEST_WCHAR
3666
3667#ifdef TEST_ZIP
3668 TestZipStreamRead();
3669#endif // TEST_ZIP
3670
3ca6a5f0
BP
3671#ifdef TEST_ZLIB
3672 if ( 0 )
3673 TestZlibStreamWrite();
3674 TestZlibStreamRead();
3675#endif // TEST_ZLIB
3676
37667812
VZ
3677 wxUninitialize();
3678
3679 return 0;
3680}
f6bcfd97 3681