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