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