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