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