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