]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
051b4e253db50cf0dc8e27a2237436dd14ed9692
[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 "wx/defs.h"
21
22 #if wxUSE_GUI
23 #error "This sample can't be compiled in GUI mode."
24 #endif // wxUSE_GUI
25
26 #include <stdio.h>
27
28 #include "wx/string.h"
29 #include "wx/file.h"
30 #include "wx/app.h"
31
32 // without this pragma, the stupid compiler precompiles #defines below so that
33 // changing them doesn't "take place" later!
34 #ifdef __VISUALC__
35 #pragma hdrstop
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
41
42 // what to test (in alphabetic order)? uncomment the line below to do all tests
43 //#define TEST_ALL
44 #ifdef TEST_ALL
45 #define TEST_ARRAYS
46 #define TEST_CHARSET
47 #define TEST_CMDLINE
48 #define TEST_DATETIME
49 #define TEST_DIR
50 #define TEST_DLLLOADER
51 #define TEST_ENVIRON
52 #define TEST_EXECUTE
53 #define TEST_FILE
54 #define TEST_FILECONF
55 #define TEST_FILENAME
56 #define TEST_FILETIME
57 #define TEST_FTP
58 #define TEST_HASH
59 #define TEST_INFO_FUNCTIONS
60 #define TEST_LIST
61 #define TEST_LOCALE
62 #define TEST_LOG
63 #define TEST_LONGLONG
64 #define TEST_MIME
65 #define TEST_PATHLIST
66 #define TEST_REGCONF
67 #define TEST_REGEX
68 #define TEST_REGISTRY
69 #define TEST_SNGLINST
70 #define TEST_SOCKETS
71 #define TEST_STREAMS
72 #define TEST_STRINGS
73 #define TEST_THREADS
74 #define TEST_TIMER
75 // #define TEST_VCARD -- don't enable this (VZ)
76 #define TEST_WCHAR
77 #define TEST_ZIP
78 #define TEST_ZLIB
79 #else
80 #define TEST_FILENAME
81 #endif
82
83 #ifdef TEST_SNGLINST
84 #include "wx/snglinst.h"
85 #endif // TEST_SNGLINST
86
87 // ----------------------------------------------------------------------------
88 // test class for container objects
89 // ----------------------------------------------------------------------------
90
91 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
92
93 class Bar // Foo is already taken in the hash test
94 {
95 public:
96 Bar(const wxString& name) : m_name(name) { ms_bars++; }
97 ~Bar() { ms_bars--; }
98
99 static size_t GetNumber() { return ms_bars; }
100
101 const char *GetName() const { return m_name; }
102
103 private:
104 wxString m_name;
105
106 static size_t ms_bars;
107 };
108
109 size_t Bar::ms_bars = 0;
110
111 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // helper functions
119 // ----------------------------------------------------------------------------
120
121 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
122
123 // replace TABs with \t and CRs with \n
124 static wxString MakePrintable(const wxChar *s)
125 {
126 wxString str(s);
127 (void)str.Replace(_T("\t"), _T("\\t"));
128 (void)str.Replace(_T("\n"), _T("\\n"));
129 (void)str.Replace(_T("\r"), _T("\\r"));
130
131 return str;
132 }
133
134 #endif // MakePrintable() is used
135
136 // ----------------------------------------------------------------------------
137 // wxFontMapper::CharsetToEncoding
138 // ----------------------------------------------------------------------------
139
140 #ifdef TEST_CHARSET
141
142 #include "wx/fontmap.h"
143
144 static void TestCharset()
145 {
146 static const wxChar *charsets[] =
147 {
148 // some vali charsets
149 _T("us-ascii "),
150 _T("iso8859-1 "),
151 _T("iso-8859-12 "),
152 _T("koi8-r "),
153 _T("utf-7 "),
154 _T("cp1250 "),
155 _T("windows-1252"),
156
157 // and now some bogus ones
158 _T(" "),
159 _T("cp1249 "),
160 _T("iso--8859-1 "),
161 _T("iso-8859-19 "),
162 };
163
164 for ( size_t n = 0; n < WXSIZEOF(charsets); n++ )
165 {
166 wxFontEncoding enc = wxTheFontMapper->CharsetToEncoding(charsets[n]);
167 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
168 charsets[n],
169 wxTheFontMapper->GetEncodingName(enc).c_str(),
170 wxTheFontMapper->GetEncodingDescription(enc).c_str());
171 }
172 }
173
174 #endif // TEST_CHARSET
175
176 // ----------------------------------------------------------------------------
177 // wxCmdLineParser
178 // ----------------------------------------------------------------------------
179
180 #ifdef TEST_CMDLINE
181
182 #include "wx/cmdline.h"
183 #include "wx/datetime.h"
184
185 #if wxUSE_CMDLINE_PARSER
186
187 static void ShowCmdLine(const wxCmdLineParser& parser)
188 {
189 wxString s = "Input files: ";
190
191 size_t count = parser.GetParamCount();
192 for ( size_t param = 0; param < count; param++ )
193 {
194 s << parser.GetParam(param) << ' ';
195 }
196
197 s << '\n'
198 << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n'
199 << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n';
200
201 wxString strVal;
202 long lVal;
203 wxDateTime dt;
204 if ( parser.Found("o", &strVal) )
205 s << "Output file:\t" << strVal << '\n';
206 if ( parser.Found("i", &strVal) )
207 s << "Input dir:\t" << strVal << '\n';
208 if ( parser.Found("s", &lVal) )
209 s << "Size:\t" << lVal << '\n';
210 if ( parser.Found("d", &dt) )
211 s << "Date:\t" << dt.FormatISODate() << '\n';
212 if ( parser.Found("project_name", &strVal) )
213 s << "Project:\t" << strVal << '\n';
214
215 wxLogMessage(s);
216 }
217
218 #endif // wxUSE_CMDLINE_PARSER
219
220 static void TestCmdLineConvert()
221 {
222 static const char *cmdlines[] =
223 {
224 "arg1 arg2",
225 "-a \"-bstring 1\" -c\"string 2\" \"string 3\"",
226 "literal \\\" and \"\"",
227 };
228
229 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
230 {
231 const char *cmdline = cmdlines[n];
232 printf("Parsing: %s\n", cmdline);
233 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
234
235 size_t count = args.GetCount();
236 printf("\targc = %u\n", count);
237 for ( size_t arg = 0; arg < count; arg++ )
238 {
239 printf("\targv[%u] = %s\n", arg, args[arg]);
240 }
241 }
242 }
243
244 #endif // TEST_CMDLINE
245
246 // ----------------------------------------------------------------------------
247 // wxDir
248 // ----------------------------------------------------------------------------
249
250 #ifdef TEST_DIR
251
252 #include "wx/dir.h"
253
254 #ifdef __UNIX__
255 static const wxChar *ROOTDIR = _T("/");
256 static const wxChar *TESTDIR = _T("/usr");
257 #elif defined(__WXMSW__)
258 static const wxChar *ROOTDIR = _T("c:\\");
259 static const wxChar *TESTDIR = _T("d:\\");
260 #else
261 #error "don't know where the root directory is"
262 #endif
263
264 static void TestDirEnumHelper(wxDir& dir,
265 int flags = wxDIR_DEFAULT,
266 const wxString& filespec = wxEmptyString)
267 {
268 wxString filename;
269
270 if ( !dir.IsOpened() )
271 return;
272
273 bool cont = dir.GetFirst(&filename, filespec, flags);
274 while ( cont )
275 {
276 printf("\t%s\n", filename.c_str());
277
278 cont = dir.GetNext(&filename);
279 }
280
281 puts("");
282 }
283
284 static void TestDirEnum()
285 {
286 puts("*** Testing wxDir::GetFirst/GetNext ***");
287
288 wxDir dir(wxGetCwd());
289
290 puts("Enumerating everything in current directory:");
291 TestDirEnumHelper(dir);
292
293 puts("Enumerating really everything in current directory:");
294 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
295
296 puts("Enumerating object files in current directory:");
297 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o");
298
299 puts("Enumerating directories in current directory:");
300 TestDirEnumHelper(dir, wxDIR_DIRS);
301
302 puts("Enumerating files in current directory:");
303 TestDirEnumHelper(dir, wxDIR_FILES);
304
305 puts("Enumerating files including hidden in current directory:");
306 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
307
308 dir.Open(ROOTDIR);
309
310 puts("Enumerating everything in root directory:");
311 TestDirEnumHelper(dir, wxDIR_DEFAULT);
312
313 puts("Enumerating directories in root directory:");
314 TestDirEnumHelper(dir, wxDIR_DIRS);
315
316 puts("Enumerating files in root directory:");
317 TestDirEnumHelper(dir, wxDIR_FILES);
318
319 puts("Enumerating files including hidden in root directory:");
320 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
321
322 puts("Enumerating files in non existing directory:");
323 wxDir dirNo("nosuchdir");
324 TestDirEnumHelper(dirNo);
325 }
326
327 class DirPrintTraverser : public wxDirTraverser
328 {
329 public:
330 virtual wxDirTraverseResult OnFile(const wxString& filename)
331 {
332 return wxDIR_CONTINUE;
333 }
334
335 virtual wxDirTraverseResult OnDir(const wxString& dirname)
336 {
337 wxString path, name, ext;
338 wxSplitPath(dirname, &path, &name, &ext);
339
340 if ( !ext.empty() )
341 name << _T('.') << ext;
342
343 wxString indent;
344 for ( const wxChar *p = path.c_str(); *p; p++ )
345 {
346 if ( wxIsPathSeparator(*p) )
347 indent += _T(" ");
348 }
349
350 printf("%s%s\n", indent.c_str(), name.c_str());
351
352 return wxDIR_CONTINUE;
353 }
354 };
355
356 static void TestDirTraverse()
357 {
358 puts("*** Testing wxDir::Traverse() ***");
359
360 // enum all files
361 wxArrayString files;
362 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
363 printf("There are %u files under '%s'\n", n, TESTDIR);
364 if ( n > 1 )
365 {
366 printf("First one is '%s'\n", files[0u].c_str());
367 printf(" last one is '%s'\n", files[n - 1].c_str());
368 }
369
370 // enum again with custom traverser
371 wxDir dir(TESTDIR);
372 DirPrintTraverser traverser;
373 dir.Traverse(traverser, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
374 }
375
376 #endif // TEST_DIR
377
378 // ----------------------------------------------------------------------------
379 // wxDllLoader
380 // ----------------------------------------------------------------------------
381
382 #ifdef TEST_DLLLOADER
383
384 #include "wx/dynlib.h"
385
386 static void TestDllLoad()
387 {
388 #if defined(__WXMSW__)
389 static const wxChar *LIB_NAME = _T("kernel32.dll");
390 static const wxChar *FUNC_NAME = _T("lstrlenA");
391 #elif defined(__UNIX__)
392 // weird: using just libc.so does *not* work!
393 static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so");
394 static const wxChar *FUNC_NAME = _T("strlen");
395 #else
396 #error "don't know how to test wxDllLoader on this platform"
397 #endif
398
399 puts("*** testing wxDllLoader ***\n");
400
401 wxDllType dllHandle = wxDllLoader::LoadLibrary(LIB_NAME);
402 if ( !dllHandle )
403 {
404 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
405 }
406 else
407 {
408 typedef int (*strlenType)(const char *);
409 strlenType pfnStrlen = (strlenType)wxDllLoader::GetSymbol(dllHandle, FUNC_NAME);
410 if ( !pfnStrlen )
411 {
412 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
413 FUNC_NAME, LIB_NAME);
414 }
415 else
416 {
417 if ( pfnStrlen("foo") != 3 )
418 {
419 wxPrintf(_T("ERROR: loaded function is not strlen()!\n"));
420 }
421 else
422 {
423 puts("... ok");
424 }
425 }
426
427 wxDllLoader::UnloadLibrary(dllHandle);
428 }
429 }
430
431 #endif // TEST_DLLLOADER
432
433 // ----------------------------------------------------------------------------
434 // wxGet/SetEnv
435 // ----------------------------------------------------------------------------
436
437 #ifdef TEST_ENVIRON
438
439 #include "wx/utils.h"
440
441 static wxString MyGetEnv(const wxString& var)
442 {
443 wxString val;
444 if ( !wxGetEnv(var, &val) )
445 val = _T("<empty>");
446 else
447 val = wxString(_T('\'')) + val + _T('\'');
448
449 return val;
450 }
451
452 static void TestEnvironment()
453 {
454 const wxChar *var = _T("wxTestVar");
455
456 puts("*** testing environment access functions ***");
457
458 printf("Initially getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
459 wxSetEnv(var, _T("value for wxTestVar"));
460 printf("After wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
461 wxSetEnv(var, _T("another value"));
462 printf("After 2nd wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
463 wxUnsetEnv(var);
464 printf("After wxUnsetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
465 printf("PATH = %s\n", MyGetEnv(_T("PATH")).c_str());
466 }
467
468 #endif // TEST_ENVIRON
469
470 // ----------------------------------------------------------------------------
471 // wxExecute
472 // ----------------------------------------------------------------------------
473
474 #ifdef TEST_EXECUTE
475
476 #include "wx/utils.h"
477
478 static void TestExecute()
479 {
480 puts("*** testing wxExecute ***");
481
482 #ifdef __UNIX__
483 #define COMMAND "cat -n ../../Makefile" // "echo hi"
484 #define SHELL_COMMAND "echo hi from shell"
485 #define REDIRECT_COMMAND COMMAND // "date"
486 #elif defined(__WXMSW__)
487 #define COMMAND "command.com -c 'echo hi'"
488 #define SHELL_COMMAND "echo hi"
489 #define REDIRECT_COMMAND COMMAND
490 #else
491 #error "no command to exec"
492 #endif // OS
493
494 printf("Testing wxShell: ");
495 fflush(stdout);
496 if ( wxShell(SHELL_COMMAND) )
497 puts("Ok.");
498 else
499 puts("ERROR.");
500
501 printf("Testing wxExecute: ");
502 fflush(stdout);
503 if ( wxExecute(COMMAND, TRUE /* sync */) == 0 )
504 puts("Ok.");
505 else
506 puts("ERROR.");
507
508 #if 0 // no, it doesn't work (yet?)
509 printf("Testing async wxExecute: ");
510 fflush(stdout);
511 if ( wxExecute(COMMAND) != 0 )
512 puts("Ok (command launched).");
513 else
514 puts("ERROR.");
515 #endif // 0
516
517 printf("Testing wxExecute with redirection:\n");
518 wxArrayString output;
519 if ( wxExecute(REDIRECT_COMMAND, output) != 0 )
520 {
521 puts("ERROR.");
522 }
523 else
524 {
525 size_t count = output.GetCount();
526 for ( size_t n = 0; n < count; n++ )
527 {
528 printf("\t%s\n", output[n].c_str());
529 }
530
531 puts("Ok.");
532 }
533 }
534
535 #endif // TEST_EXECUTE
536
537 // ----------------------------------------------------------------------------
538 // file
539 // ----------------------------------------------------------------------------
540
541 #ifdef TEST_FILE
542
543 #include "wx/file.h"
544 #include "wx/ffile.h"
545 #include "wx/textfile.h"
546
547 static void TestFileRead()
548 {
549 puts("*** wxFile read test ***");
550
551 wxFile file(_T("testdata.fc"));
552 if ( file.IsOpened() )
553 {
554 printf("File length: %lu\n", file.Length());
555
556 puts("File dump:\n----------");
557
558 static const off_t len = 1024;
559 char buf[len];
560 for ( ;; )
561 {
562 off_t nRead = file.Read(buf, len);
563 if ( nRead == wxInvalidOffset )
564 {
565 printf("Failed to read the file.");
566 break;
567 }
568
569 fwrite(buf, nRead, 1, stdout);
570
571 if ( nRead < len )
572 break;
573 }
574
575 puts("----------");
576 }
577 else
578 {
579 printf("ERROR: can't open test file.\n");
580 }
581
582 puts("");
583 }
584
585 static void TestTextFileRead()
586 {
587 puts("*** wxTextFile read test ***");
588
589 wxTextFile file(_T("testdata.fc"));
590 if ( file.Open() )
591 {
592 printf("Number of lines: %u\n", file.GetLineCount());
593 printf("Last line: '%s'\n", file.GetLastLine().c_str());
594
595 wxString s;
596
597 puts("\nDumping the entire file:");
598 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
599 {
600 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
601 }
602 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
603
604 puts("\nAnd now backwards:");
605 for ( s = file.GetLastLine();
606 file.GetCurrentLine() != 0;
607 s = file.GetPrevLine() )
608 {
609 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
610 }
611 printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str());
612 }
613 else
614 {
615 printf("ERROR: can't open '%s'\n", file.GetName());
616 }
617
618 puts("");
619 }
620
621 static void TestFileCopy()
622 {
623 puts("*** Testing wxCopyFile ***");
624
625 static const wxChar *filename1 = _T("testdata.fc");
626 static const wxChar *filename2 = _T("test2");
627 if ( !wxCopyFile(filename1, filename2) )
628 {
629 puts("ERROR: failed to copy file");
630 }
631 else
632 {
633 wxFFile f1(filename1, "rb"),
634 f2(filename2, "rb");
635
636 if ( !f1.IsOpened() || !f2.IsOpened() )
637 {
638 puts("ERROR: failed to open file(s)");
639 }
640 else
641 {
642 wxString s1, s2;
643 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
644 {
645 puts("ERROR: failed to read file(s)");
646 }
647 else
648 {
649 if ( (s1.length() != s2.length()) ||
650 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
651 {
652 puts("ERROR: copy error!");
653 }
654 else
655 {
656 puts("File was copied ok.");
657 }
658 }
659 }
660 }
661
662 if ( !wxRemoveFile(filename2) )
663 {
664 puts("ERROR: failed to remove the file");
665 }
666
667 puts("");
668 }
669
670 #endif // TEST_FILE
671
672 // ----------------------------------------------------------------------------
673 // wxFileConfig
674 // ----------------------------------------------------------------------------
675
676 #ifdef TEST_FILECONF
677
678 #include "wx/confbase.h"
679 #include "wx/fileconf.h"
680
681 static const struct FileConfTestData
682 {
683 const wxChar *name; // value name
684 const wxChar *value; // the value from the file
685 } fcTestData[] =
686 {
687 { _T("value1"), _T("one") },
688 { _T("value2"), _T("two") },
689 { _T("novalue"), _T("default") },
690 };
691
692 static void TestFileConfRead()
693 {
694 puts("*** testing wxFileConfig loading/reading ***");
695
696 wxFileConfig fileconf(_T("test"), wxEmptyString,
697 _T("testdata.fc"), wxEmptyString,
698 wxCONFIG_USE_RELATIVE_PATH);
699
700 // test simple reading
701 puts("\nReading config file:");
702 wxString defValue(_T("default")), value;
703 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
704 {
705 const FileConfTestData& data = fcTestData[n];
706 value = fileconf.Read(data.name, defValue);
707 printf("\t%s = %s ", data.name, value.c_str());
708 if ( value == data.value )
709 {
710 puts("(ok)");
711 }
712 else
713 {
714 printf("(ERROR: should be %s)\n", data.value);
715 }
716 }
717
718 // test enumerating the entries
719 puts("\nEnumerating all root entries:");
720 long dummy;
721 wxString name;
722 bool cont = fileconf.GetFirstEntry(name, dummy);
723 while ( cont )
724 {
725 printf("\t%s = %s\n",
726 name.c_str(),
727 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
728
729 cont = fileconf.GetNextEntry(name, dummy);
730 }
731 }
732
733 #endif // TEST_FILECONF
734
735 // ----------------------------------------------------------------------------
736 // wxFileName
737 // ----------------------------------------------------------------------------
738
739 #ifdef TEST_FILENAME
740
741 #include "wx/filename.h"
742
743 static struct FileNameInfo
744 {
745 const wxChar *fullname;
746 const wxChar *volume;
747 const wxChar *path;
748 const wxChar *name;
749 const wxChar *ext;
750 bool isAbsolute;
751 wxPathFormat format;
752 } filenames[] =
753 {
754 // Unix file names
755 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), TRUE, wxPATH_UNIX },
756 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), TRUE, wxPATH_UNIX },
757 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), TRUE, wxPATH_UNIX },
758 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), FALSE, wxPATH_UNIX },
759 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
760 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), TRUE, wxPATH_UNIX },
761 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), TRUE, wxPATH_UNIX },
762 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
763 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), TRUE, wxPATH_UNIX },
764
765 // Windows file names
766 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
767 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
768 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
769 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
770 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), TRUE, wxPATH_DOS },
771 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
772
773 // Mac file names
774 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
775 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), FALSE, wxPATH_MAC },
776 { _T(":File"), _T(""), _T(""), _T("File"), _T(""), FALSE, wxPATH_MAC },
777 { _T("File"), _T(""), _T(""), _T("File"), _T(""), FALSE, wxPATH_MAC },
778
779 // VMS file names
780 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), TRUE, wxPATH_VMS },
781 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), FALSE, wxPATH_VMS },
782 };
783
784 static void TestFileNameConstruction()
785 {
786 puts("*** testing wxFileName construction ***");
787
788 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
789 {
790 const FileNameInfo& fni = filenames[n];
791
792 wxFileName fn(fni.fullname, fni.format);
793
794 wxString fullname = fn.GetFullPath(fni.format);
795 if ( fullname != fni.fullname )
796 {
797 printf("ERROR: fullname should be '%s'\n", fni.fullname);
798 }
799
800 bool isAbsolute = fn.IsAbsolute(fni.format);
801 printf("'%s' is %s (%s)\n\t",
802 fullname.c_str(),
803 isAbsolute ? "absolute" : "relative",
804 isAbsolute == fni.isAbsolute ? "ok" : "ERROR");
805
806 if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) )
807 {
808 puts("ERROR (couldn't be normalized)");
809 }
810 else
811 {
812 printf("normalized: '%s'\n", fn.GetFullPath(fni.format).c_str());
813 }
814 }
815
816 puts("");
817 }
818
819 static void TestFileNameSplit()
820 {
821 puts("*** testing wxFileName splitting ***");
822
823 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
824 {
825 const FileNameInfo& fni = filenames[n];
826 wxString volume, path, name, ext;
827 wxFileName::SplitPath(fni.fullname,
828 &volume, &path, &name, &ext, fni.format);
829
830 printf("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'",
831 fni.fullname,
832 volume.c_str(), path.c_str(), name.c_str(), ext.c_str());
833
834 if ( volume != fni.volume )
835 printf(" (ERROR: volume = '%s')", fni.volume);
836 if ( path != fni.path )
837 printf(" (ERROR: path = '%s')", fni.path);
838 if ( name != fni.name )
839 printf(" (ERROR: name = '%s')", fni.name);
840 if ( ext != fni.ext )
841 printf(" (ERROR: ext = '%s')", fni.ext);
842
843 puts("");
844 }
845 }
846
847 static void TestFileNameTemp()
848 {
849 puts("*** testing wxFileName temp file creation ***");
850
851 static const char *tmpprefixes[] =
852 {
853 "foo",
854 "/tmp/foo",
855 "..",
856 "../bar",
857 "/tmp/foo/bar", // this one must be an error
858 };
859
860 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
861 {
862 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
863 if ( !path.empty() )
864 {
865 printf("Prefix '%s'\t-> temp file '%s'\n",
866 tmpprefixes[n], path.c_str());
867
868 if ( !wxRemoveFile(path) )
869 {
870 wxLogWarning("Failed to remove temp file '%s'", path.c_str());
871 }
872 }
873 }
874 }
875
876 static void TestFileNameComparison()
877 {
878 // TODO!
879 }
880
881 static void TestFileNameOperations()
882 {
883 // TODO!
884 }
885
886 static void TestFileNameCwd()
887 {
888 // TODO!
889 }
890
891 #endif // TEST_FILENAME
892
893 // ----------------------------------------------------------------------------
894 // wxFileName time functions
895 // ----------------------------------------------------------------------------
896
897 #ifdef TEST_FILETIME
898
899 #include <wx/filename.h>
900 #include <wx/datetime.h>
901
902 static void TestFileGetTimes()
903 {
904 wxFileName fn(_T("testdata.fc"));
905
906 wxDateTime dtAccess, dtMod, dtChange;
907 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtChange) )
908 {
909 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
910 }
911 else
912 {
913 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
914
915 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
916 wxPrintf(_T("Access: \t%s\n"), dtAccess.Format(fmt).c_str());
917 wxPrintf(_T("Mod/creation:\t%s\n"), dtMod.Format(fmt).c_str());
918 wxPrintf(_T("Change: \t%s\n"), dtChange.Format(fmt).c_str());
919 }
920 }
921
922 static void TestFileSetTimes()
923 {
924 wxFileName fn(_T("testdata.fc"));
925
926 wxDateTime dtAccess, dtMod, dtChange;
927 if ( !fn.Touch() )
928 {
929 wxPrintf(_T("ERROR: Touch() failed.\n"));
930 }
931 }
932
933 #endif // TEST_FILETIME
934
935 // ----------------------------------------------------------------------------
936 // wxHashTable
937 // ----------------------------------------------------------------------------
938
939 #ifdef TEST_HASH
940
941 #include "wx/hash.h"
942
943 struct Foo
944 {
945 Foo(int n_) { n = n_; count++; }
946 ~Foo() { count--; }
947
948 int n;
949
950 static size_t count;
951 };
952
953 size_t Foo::count = 0;
954
955 WX_DECLARE_LIST(Foo, wxListFoos);
956 WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos);
957
958 #include "wx/listimpl.cpp"
959
960 WX_DEFINE_LIST(wxListFoos);
961
962 static void TestHash()
963 {
964 puts("*** Testing wxHashTable ***\n");
965
966 {
967 wxHashFoos hash;
968 hash.DeleteContents(TRUE);
969
970 printf("Hash created: %u foos in hash, %u foos totally\n",
971 hash.GetCount(), Foo::count);
972
973 static const int hashTestData[] =
974 {
975 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
976 };
977
978 size_t n;
979 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
980 {
981 hash.Put(hashTestData[n], n, new Foo(n));
982 }
983
984 printf("Hash filled: %u foos in hash, %u foos totally\n",
985 hash.GetCount(), Foo::count);
986
987 puts("Hash access test:");
988 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
989 {
990 printf("\tGetting element with key %d, value %d: ",
991 hashTestData[n], n);
992 Foo *foo = hash.Get(hashTestData[n], n);
993 if ( !foo )
994 {
995 printf("ERROR, not found.\n");
996 }
997 else
998 {
999 printf("%d (%s)\n", foo->n,
1000 (size_t)foo->n == n ? "ok" : "ERROR");
1001 }
1002 }
1003
1004 printf("\nTrying to get an element not in hash: ");
1005
1006 if ( hash.Get(1234) || hash.Get(1, 0) )
1007 {
1008 puts("ERROR: found!");
1009 }
1010 else
1011 {
1012 puts("ok (not found)");
1013 }
1014 }
1015
1016 printf("Hash destroyed: %u foos left\n", Foo::count);
1017 }
1018
1019 #endif // TEST_HASH
1020
1021 // ----------------------------------------------------------------------------
1022 // wxList
1023 // ----------------------------------------------------------------------------
1024
1025 #ifdef TEST_LIST
1026
1027 #include "wx/list.h"
1028
1029 WX_DECLARE_LIST(Bar, wxListBars);
1030 #include "wx/listimpl.cpp"
1031 WX_DEFINE_LIST(wxListBars);
1032
1033 static void TestListCtor()
1034 {
1035 puts("*** Testing wxList construction ***\n");
1036
1037 {
1038 wxListBars list1;
1039 list1.Append(new Bar(_T("first")));
1040 list1.Append(new Bar(_T("second")));
1041
1042 printf("After 1st list creation: %u objects in the list, %u objects total.\n",
1043 list1.GetCount(), Bar::GetNumber());
1044
1045 wxListBars list2;
1046 list2 = list1;
1047
1048 printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n",
1049 list1.GetCount(), list2.GetCount(), Bar::GetNumber());
1050
1051 list1.DeleteContents(TRUE);
1052 }
1053
1054 printf("After list destruction: %u objects left.\n", Bar::GetNumber());
1055 }
1056
1057 #endif // TEST_LIST
1058
1059 // ----------------------------------------------------------------------------
1060 // wxLocale
1061 // ----------------------------------------------------------------------------
1062
1063 #ifdef TEST_LOCALE
1064
1065 #include "wx/intl.h"
1066 #include "wx/utils.h" // for wxSetEnv
1067
1068 static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH);
1069
1070 // find the name of the language from its value
1071 static const char *GetLangName(int lang)
1072 {
1073 static const char *languageNames[] =
1074 {
1075 "DEFAULT",
1076 "UNKNOWN",
1077 "ABKHAZIAN",
1078 "AFAR",
1079 "AFRIKAANS",
1080 "ALBANIAN",
1081 "AMHARIC",
1082 "ARABIC",
1083 "ARABIC_ALGERIA",
1084 "ARABIC_BAHRAIN",
1085 "ARABIC_EGYPT",
1086 "ARABIC_IRAQ",
1087 "ARABIC_JORDAN",
1088 "ARABIC_KUWAIT",
1089 "ARABIC_LEBANON",
1090 "ARABIC_LIBYA",
1091 "ARABIC_MOROCCO",
1092 "ARABIC_OMAN",
1093 "ARABIC_QATAR",
1094 "ARABIC_SAUDI_ARABIA",
1095 "ARABIC_SUDAN",
1096 "ARABIC_SYRIA",
1097 "ARABIC_TUNISIA",
1098 "ARABIC_UAE",
1099 "ARABIC_YEMEN",
1100 "ARMENIAN",
1101 "ASSAMESE",
1102 "AYMARA",
1103 "AZERI",
1104 "AZERI_CYRILLIC",
1105 "AZERI_LATIN",
1106 "BASHKIR",
1107 "BASQUE",
1108 "BELARUSIAN",
1109 "BENGALI",
1110 "BHUTANI",
1111 "BIHARI",
1112 "BISLAMA",
1113 "BRETON",
1114 "BULGARIAN",
1115 "BURMESE",
1116 "CAMBODIAN",
1117 "CATALAN",
1118 "CHINESE",
1119 "CHINESE_SIMPLIFIED",
1120 "CHINESE_TRADITIONAL",
1121 "CHINESE_HONGKONG",
1122 "CHINESE_MACAU",
1123 "CHINESE_SINGAPORE",
1124 "CHINESE_TAIWAN",
1125 "CORSICAN",
1126 "CROATIAN",
1127 "CZECH",
1128 "DANISH",
1129 "DUTCH",
1130 "DUTCH_BELGIAN",
1131 "ENGLISH",
1132 "ENGLISH_UK",
1133 "ENGLISH_US",
1134 "ENGLISH_AUSTRALIA",
1135 "ENGLISH_BELIZE",
1136 "ENGLISH_BOTSWANA",
1137 "ENGLISH_CANADA",
1138 "ENGLISH_CARIBBEAN",
1139 "ENGLISH_DENMARK",
1140 "ENGLISH_EIRE",
1141 "ENGLISH_JAMAICA",
1142 "ENGLISH_NEW_ZEALAND",
1143 "ENGLISH_PHILIPPINES",
1144 "ENGLISH_SOUTH_AFRICA",
1145 "ENGLISH_TRINIDAD",
1146 "ENGLISH_ZIMBABWE",
1147 "ESPERANTO",
1148 "ESTONIAN",
1149 "FAEROESE",
1150 "FARSI",
1151 "FIJI",
1152 "FINNISH",
1153 "FRENCH",
1154 "FRENCH_BELGIAN",
1155 "FRENCH_CANADIAN",
1156 "FRENCH_LUXEMBOURG",
1157 "FRENCH_MONACO",
1158 "FRENCH_SWISS",
1159 "FRISIAN",
1160 "GALICIAN",
1161 "GEORGIAN",
1162 "GERMAN",
1163 "GERMAN_AUSTRIAN",
1164 "GERMAN_BELGIUM",
1165 "GERMAN_LIECHTENSTEIN",
1166 "GERMAN_LUXEMBOURG",
1167 "GERMAN_SWISS",
1168 "GREEK",
1169 "GREENLANDIC",
1170 "GUARANI",
1171 "GUJARATI",
1172 "HAUSA",
1173 "HEBREW",
1174 "HINDI",
1175 "HUNGARIAN",
1176 "ICELANDIC",
1177 "INDONESIAN",
1178 "INTERLINGUA",
1179 "INTERLINGUE",
1180 "INUKTITUT",
1181 "INUPIAK",
1182 "IRISH",
1183 "ITALIAN",
1184 "ITALIAN_SWISS",
1185 "JAPANESE",
1186 "JAVANESE",
1187 "KANNADA",
1188 "KASHMIRI",
1189 "KASHMIRI_INDIA",
1190 "KAZAKH",
1191 "KERNEWEK",
1192 "KINYARWANDA",
1193 "KIRGHIZ",
1194 "KIRUNDI",
1195 "KONKANI",
1196 "KOREAN",
1197 "KURDISH",
1198 "LAOTHIAN",
1199 "LATIN",
1200 "LATVIAN",
1201 "LINGALA",
1202 "LITHUANIAN",
1203 "MACEDONIAN",
1204 "MALAGASY",
1205 "MALAY",
1206 "MALAYALAM",
1207 "MALAY_BRUNEI_DARUSSALAM",
1208 "MALAY_MALAYSIA",
1209 "MALTESE",
1210 "MANIPURI",
1211 "MAORI",
1212 "MARATHI",
1213 "MOLDAVIAN",
1214 "MONGOLIAN",
1215 "NAURU",
1216 "NEPALI",
1217 "NEPALI_INDIA",
1218 "NORWEGIAN_BOKMAL",
1219 "NORWEGIAN_NYNORSK",
1220 "OCCITAN",
1221 "ORIYA",
1222 "OROMO",
1223 "PASHTO",
1224 "POLISH",
1225 "PORTUGUESE",
1226 "PORTUGUESE_BRAZILIAN",
1227 "PUNJABI",
1228 "QUECHUA",
1229 "RHAETO_ROMANCE",
1230 "ROMANIAN",
1231 "RUSSIAN",
1232 "RUSSIAN_UKRAINE",
1233 "SAMOAN",
1234 "SANGHO",
1235 "SANSKRIT",
1236 "SCOTS_GAELIC",
1237 "SERBIAN",
1238 "SERBIAN_CYRILLIC",
1239 "SERBIAN_LATIN",
1240 "SERBO_CROATIAN",
1241 "SESOTHO",
1242 "SETSWANA",
1243 "SHONA",
1244 "SINDHI",
1245 "SINHALESE",
1246 "SISWATI",
1247 "SLOVAK",
1248 "SLOVENIAN",
1249 "SOMALI",
1250 "SPANISH",
1251 "SPANISH_ARGENTINA",
1252 "SPANISH_BOLIVIA",
1253 "SPANISH_CHILE",
1254 "SPANISH_COLOMBIA",
1255 "SPANISH_COSTA_RICA",
1256 "SPANISH_DOMINICAN_REPUBLIC",
1257 "SPANISH_ECUADOR",
1258 "SPANISH_EL_SALVADOR",
1259 "SPANISH_GUATEMALA",
1260 "SPANISH_HONDURAS",
1261 "SPANISH_MEXICAN",
1262 "SPANISH_MODERN",
1263 "SPANISH_NICARAGUA",
1264 "SPANISH_PANAMA",
1265 "SPANISH_PARAGUAY",
1266 "SPANISH_PERU",
1267 "SPANISH_PUERTO_RICO",
1268 "SPANISH_URUGUAY",
1269 "SPANISH_US",
1270 "SPANISH_VENEZUELA",
1271 "SUNDANESE",
1272 "SWAHILI",
1273 "SWEDISH",
1274 "SWEDISH_FINLAND",
1275 "TAGALOG",
1276 "TAJIK",
1277 "TAMIL",
1278 "TATAR",
1279 "TELUGU",
1280 "THAI",
1281 "TIBETAN",
1282 "TIGRINYA",
1283 "TONGA",
1284 "TSONGA",
1285 "TURKISH",
1286 "TURKMEN",
1287 "TWI",
1288 "UIGHUR",
1289 "UKRAINIAN",
1290 "URDU",
1291 "URDU_INDIA",
1292 "URDU_PAKISTAN",
1293 "UZBEK",
1294 "UZBEK_CYRILLIC",
1295 "UZBEK_LATIN",
1296 "VIETNAMESE",
1297 "VOLAPUK",
1298 "WELSH",
1299 "WOLOF",
1300 "XHOSA",
1301 "YIDDISH",
1302 "YORUBA",
1303 "ZHUANG",
1304 "ZULU",
1305 };
1306
1307 if ( (size_t)lang < WXSIZEOF(languageNames) )
1308 return languageNames[lang];
1309 else
1310 return "INVALID";
1311 }
1312
1313 static void TestDefaultLang()
1314 {
1315 puts("*** Testing wxLocale::GetSystemLanguage ***");
1316
1317 static const wxChar *langStrings[] =
1318 {
1319 NULL, // system default
1320 _T("C"),
1321 _T("fr"),
1322 _T("fr_FR"),
1323 _T("en"),
1324 _T("en_GB"),
1325 _T("en_US"),
1326 _T("de_DE.iso88591"),
1327 _T("german"),
1328 _T("?"), // invalid lang spec
1329 _T("klingonese"), // I bet on some systems it does exist...
1330 };
1331
1332 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1333 wxLocale::GetSystemEncodingName().c_str(),
1334 wxLocale::GetSystemEncoding());
1335
1336 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1337 {
1338 const char *langStr = langStrings[n];
1339 if ( langStr )
1340 {
1341 // FIXME: this doesn't do anything at all under Windows, we need
1342 // to create a new wxLocale!
1343 wxSetEnv(_T("LC_ALL"), langStr);
1344 }
1345
1346 int lang = gs_localeDefault.GetSystemLanguage();
1347 printf("Locale for '%s' is %s.\n",
1348 langStr ? langStr : "system default", GetLangName(lang));
1349 }
1350 }
1351
1352 #endif // TEST_LOCALE
1353
1354 // ----------------------------------------------------------------------------
1355 // MIME types
1356 // ----------------------------------------------------------------------------
1357
1358 #ifdef TEST_MIME
1359
1360 #include "wx/mimetype.h"
1361
1362 static void TestMimeEnum()
1363 {
1364 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1365
1366 wxArrayString mimetypes;
1367
1368 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
1369
1370 printf("*** All %u known filetypes: ***\n", count);
1371
1372 wxArrayString exts;
1373 wxString desc;
1374
1375 for ( size_t n = 0; n < count; n++ )
1376 {
1377 wxFileType *filetype =
1378 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
1379 if ( !filetype )
1380 {
1381 printf("nothing known about the filetype '%s'!\n",
1382 mimetypes[n].c_str());
1383 continue;
1384 }
1385
1386 filetype->GetDescription(&desc);
1387 filetype->GetExtensions(exts);
1388
1389 filetype->GetIcon(NULL);
1390
1391 wxString extsAll;
1392 for ( size_t e = 0; e < exts.GetCount(); e++ )
1393 {
1394 if ( e > 0 )
1395 extsAll << _T(", ");
1396 extsAll += exts[e];
1397 }
1398
1399 printf("\t%s: %s (%s)\n",
1400 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
1401 }
1402
1403 puts("");
1404 }
1405
1406 static void TestMimeOverride()
1407 {
1408 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1409
1410 static const wxChar *mailcap = _T("/tmp/mailcap");
1411 static const wxChar *mimetypes = _T("/tmp/mime.types");
1412
1413 if ( wxFile::Exists(mailcap) )
1414 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1415 mailcap,
1416 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
1417 else
1418 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1419 mailcap);
1420
1421 if ( wxFile::Exists(mimetypes) )
1422 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1423 mimetypes,
1424 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
1425 else
1426 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1427 mimetypes);
1428
1429 puts("");
1430 }
1431
1432 static void TestMimeFilename()
1433 {
1434 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1435
1436 static const wxChar *filenames[] =
1437 {
1438 _T("readme.txt"),
1439 _T("document.pdf"),
1440 _T("image.gif"),
1441 };
1442
1443 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1444 {
1445 const wxString fname = filenames[n];
1446 wxString ext = fname.AfterLast(_T('.'));
1447 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
1448 if ( !ft )
1449 {
1450 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1451 }
1452 else
1453 {
1454 wxString desc;
1455 if ( !ft->GetDescription(&desc) )
1456 desc = _T("<no description>");
1457
1458 wxString cmd;
1459 if ( !ft->GetOpenCommand(&cmd,
1460 wxFileType::MessageParameters(fname, _T(""))) )
1461 cmd = _T("<no command available>");
1462
1463 wxPrintf(_T("To open %s (%s) do '%s'.\n"),
1464 fname.c_str(), desc.c_str(), cmd.c_str());
1465
1466 delete ft;
1467 }
1468 }
1469
1470 puts("");
1471 }
1472
1473 static void TestMimeAssociate()
1474 {
1475 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1476
1477 wxFileTypeInfo ftInfo(
1478 _T("application/x-xyz"),
1479 _T("xyzview '%s'"), // open cmd
1480 _T(""), // print cmd
1481 _T("XYZ File") // description
1482 _T(".xyz"), // extensions
1483 NULL // end of extensions
1484 );
1485 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
1486
1487 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
1488 if ( !ft )
1489 {
1490 wxPuts(_T("ERROR: failed to create association!"));
1491 }
1492 else
1493 {
1494 // TODO: read it back
1495 delete ft;
1496 }
1497
1498 puts("");
1499 }
1500
1501 #endif // TEST_MIME
1502
1503 // ----------------------------------------------------------------------------
1504 // misc information functions
1505 // ----------------------------------------------------------------------------
1506
1507 #ifdef TEST_INFO_FUNCTIONS
1508
1509 #include "wx/utils.h"
1510
1511 static void TestDiskInfo()
1512 {
1513 puts("*** Testing wxGetDiskSpace() ***");
1514
1515 for ( ;; )
1516 {
1517 char pathname[128];
1518 printf("\nEnter a directory name: ");
1519 if ( !fgets(pathname, WXSIZEOF(pathname), stdin) )
1520 break;
1521
1522 // kill the last '\n'
1523 pathname[strlen(pathname) - 1] = 0;
1524
1525 wxLongLong total, free;
1526 if ( !wxGetDiskSpace(pathname, &total, &free) )
1527 {
1528 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1529 }
1530 else
1531 {
1532 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1533 (total / 1024).ToString().c_str(),
1534 (free / 1024).ToString().c_str(),
1535 pathname);
1536 }
1537 }
1538 }
1539
1540 static void TestOsInfo()
1541 {
1542 puts("*** Testing OS info functions ***\n");
1543
1544 int major, minor;
1545 wxGetOsVersion(&major, &minor);
1546 printf("Running under: %s, version %d.%d\n",
1547 wxGetOsDescription().c_str(), major, minor);
1548
1549 printf("%ld free bytes of memory left.\n", wxGetFreeMemory());
1550
1551 printf("Host name is %s (%s).\n",
1552 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1553
1554 puts("");
1555 }
1556
1557 static void TestUserInfo()
1558 {
1559 puts("*** Testing user info functions ***\n");
1560
1561 printf("User id is:\t%s\n", wxGetUserId().c_str());
1562 printf("User name is:\t%s\n", wxGetUserName().c_str());
1563 printf("Home dir is:\t%s\n", wxGetHomeDir().c_str());
1564 printf("Email address:\t%s\n", wxGetEmailAddress().c_str());
1565
1566 puts("");
1567 }
1568
1569 #endif // TEST_INFO_FUNCTIONS
1570
1571 // ----------------------------------------------------------------------------
1572 // long long
1573 // ----------------------------------------------------------------------------
1574
1575 #ifdef TEST_LONGLONG
1576
1577 #include "wx/longlong.h"
1578 #include "wx/timer.h"
1579
1580 // make a 64 bit number from 4 16 bit ones
1581 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
1582
1583 // get a random 64 bit number
1584 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
1585
1586 static const long testLongs[] =
1587 {
1588 0,
1589 1,
1590 -1,
1591 LONG_MAX,
1592 LONG_MIN,
1593 0x1234,
1594 -0x1234
1595 };
1596
1597 #if wxUSE_LONGLONG_WX
1598 inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
1599 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
1600 inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
1601 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
1602 #endif // wxUSE_LONGLONG_WX
1603
1604 static void TestSpeed()
1605 {
1606 static const long max = 100000000;
1607 long n;
1608
1609 {
1610 wxStopWatch sw;
1611
1612 long l = 0;
1613 for ( n = 0; n < max; n++ )
1614 {
1615 l += n;
1616 }
1617
1618 printf("Summing longs took %ld milliseconds.\n", sw.Time());
1619 }
1620
1621 #if wxUSE_LONGLONG_NATIVE
1622 {
1623 wxStopWatch sw;
1624
1625 wxLongLong_t l = 0;
1626 for ( n = 0; n < max; n++ )
1627 {
1628 l += n;
1629 }
1630
1631 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time());
1632 }
1633 #endif // wxUSE_LONGLONG_NATIVE
1634
1635 {
1636 wxStopWatch sw;
1637
1638 wxLongLong l;
1639 for ( n = 0; n < max; n++ )
1640 {
1641 l += n;
1642 }
1643
1644 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
1645 }
1646 }
1647
1648 static void TestLongLongConversion()
1649 {
1650 puts("*** Testing wxLongLong conversions ***\n");
1651
1652 wxLongLong a;
1653 size_t nTested = 0;
1654 for ( size_t n = 0; n < 100000; n++ )
1655 {
1656 a = RAND_LL();
1657
1658 #if wxUSE_LONGLONG_NATIVE
1659 wxLongLongNative b(a.GetHi(), a.GetLo());
1660
1661 wxASSERT_MSG( a == b, "conversions failure" );
1662 #else
1663 puts("Can't do it without native long long type, test skipped.");
1664
1665 return;
1666 #endif // wxUSE_LONGLONG_NATIVE
1667
1668 if ( !(nTested % 1000) )
1669 {
1670 putchar('.');
1671 fflush(stdout);
1672 }
1673
1674 nTested++;
1675 }
1676
1677 puts(" done!");
1678 }
1679
1680 static void TestMultiplication()
1681 {
1682 puts("*** Testing wxLongLong multiplication ***\n");
1683
1684 wxLongLong a, b;
1685 size_t nTested = 0;
1686 for ( size_t n = 0; n < 100000; n++ )
1687 {
1688 a = RAND_LL();
1689 b = RAND_LL();
1690
1691 #if wxUSE_LONGLONG_NATIVE
1692 wxLongLongNative aa(a.GetHi(), a.GetLo());
1693 wxLongLongNative bb(b.GetHi(), b.GetLo());
1694
1695 wxASSERT_MSG( a*b == aa*bb, "multiplication failure" );
1696 #else // !wxUSE_LONGLONG_NATIVE
1697 puts("Can't do it without native long long type, test skipped.");
1698
1699 return;
1700 #endif // wxUSE_LONGLONG_NATIVE
1701
1702 if ( !(nTested % 1000) )
1703 {
1704 putchar('.');
1705 fflush(stdout);
1706 }
1707
1708 nTested++;
1709 }
1710
1711 puts(" done!");
1712 }
1713
1714 static void TestDivision()
1715 {
1716 puts("*** Testing wxLongLong division ***\n");
1717
1718 wxLongLong q, r;
1719 size_t nTested = 0;
1720 for ( size_t n = 0; n < 100000; n++ )
1721 {
1722 // get a random wxLongLong (shifting by 12 the MSB ensures that the
1723 // multiplication will not overflow)
1724 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
1725
1726 // get a random long (not wxLongLong for now) to divide it with
1727 long l = rand();
1728 q = ll / l;
1729 r = ll % l;
1730
1731 #if wxUSE_LONGLONG_NATIVE
1732 wxLongLongNative m(ll.GetHi(), ll.GetLo());
1733
1734 wxLongLongNative p = m / l, s = m % l;
1735 wxASSERT_MSG( q == p && r == s, "division failure" );
1736 #else // !wxUSE_LONGLONG_NATIVE
1737 // verify the result
1738 wxASSERT_MSG( ll == q*l + r, "division failure" );
1739 #endif // wxUSE_LONGLONG_NATIVE
1740
1741 if ( !(nTested % 1000) )
1742 {
1743 putchar('.');
1744 fflush(stdout);
1745 }
1746
1747 nTested++;
1748 }
1749
1750 puts(" done!");
1751 }
1752
1753 static void TestAddition()
1754 {
1755 puts("*** Testing wxLongLong addition ***\n");
1756
1757 wxLongLong a, b, c;
1758 size_t nTested = 0;
1759 for ( size_t n = 0; n < 100000; n++ )
1760 {
1761 a = RAND_LL();
1762 b = RAND_LL();
1763 c = a + b;
1764
1765 #if wxUSE_LONGLONG_NATIVE
1766 wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
1767 wxLongLongNative(b.GetHi(), b.GetLo()),
1768 "addition failure" );
1769 #else // !wxUSE_LONGLONG_NATIVE
1770 wxASSERT_MSG( c - b == a, "addition failure" );
1771 #endif // wxUSE_LONGLONG_NATIVE
1772
1773 if ( !(nTested % 1000) )
1774 {
1775 putchar('.');
1776 fflush(stdout);
1777 }
1778
1779 nTested++;
1780 }
1781
1782 puts(" done!");
1783 }
1784
1785 static void TestBitOperations()
1786 {
1787 puts("*** Testing wxLongLong bit operation ***\n");
1788
1789 wxLongLong ll;
1790 size_t nTested = 0;
1791 for ( size_t n = 0; n < 100000; n++ )
1792 {
1793 ll = RAND_LL();
1794
1795 #if wxUSE_LONGLONG_NATIVE
1796 for ( size_t n = 0; n < 33; n++ )
1797 {
1798 }
1799 #else // !wxUSE_LONGLONG_NATIVE
1800 puts("Can't do it without native long long type, test skipped.");
1801
1802 return;
1803 #endif // wxUSE_LONGLONG_NATIVE
1804
1805 if ( !(nTested % 1000) )
1806 {
1807 putchar('.');
1808 fflush(stdout);
1809 }
1810
1811 nTested++;
1812 }
1813
1814 puts(" done!");
1815 }
1816
1817 static void TestLongLongComparison()
1818 {
1819 #if wxUSE_LONGLONG_WX
1820 puts("*** Testing wxLongLong comparison ***\n");
1821
1822 static const long ls[2] =
1823 {
1824 0x1234,
1825 -0x1234,
1826 };
1827
1828 wxLongLongWx lls[2];
1829 lls[0] = ls[0];
1830 lls[1] = ls[1];
1831
1832 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
1833 {
1834 bool res;
1835
1836 for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
1837 {
1838 res = lls[m] > testLongs[n];
1839 printf("0x%lx > 0x%lx is %s (%s)\n",
1840 ls[m], testLongs[n], res ? "true" : "false",
1841 res == (ls[m] > testLongs[n]) ? "ok" : "ERROR");
1842
1843 res = lls[m] < testLongs[n];
1844 printf("0x%lx < 0x%lx is %s (%s)\n",
1845 ls[m], testLongs[n], res ? "true" : "false",
1846 res == (ls[m] < testLongs[n]) ? "ok" : "ERROR");
1847
1848 res = lls[m] == testLongs[n];
1849 printf("0x%lx == 0x%lx is %s (%s)\n",
1850 ls[m], testLongs[n], res ? "true" : "false",
1851 res == (ls[m] == testLongs[n]) ? "ok" : "ERROR");
1852 }
1853 }
1854 #endif // wxUSE_LONGLONG_WX
1855 }
1856
1857 static void TestLongLongPrint()
1858 {
1859 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
1860
1861 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
1862 {
1863 wxLongLong ll = testLongs[n];
1864 wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str());
1865 }
1866
1867 wxLongLong ll(0x12345678, 0x87654321);
1868 wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str());
1869
1870 ll.Negate();
1871 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str());
1872 }
1873
1874 #undef MAKE_LL
1875 #undef RAND_LL
1876
1877 #endif // TEST_LONGLONG
1878
1879 // ----------------------------------------------------------------------------
1880 // path list
1881 // ----------------------------------------------------------------------------
1882
1883 #ifdef TEST_PATHLIST
1884
1885 static void TestPathList()
1886 {
1887 puts("*** Testing wxPathList ***\n");
1888
1889 wxPathList pathlist;
1890 pathlist.AddEnvList("PATH");
1891 wxString path = pathlist.FindValidPath("ls");
1892 if ( path.empty() )
1893 {
1894 printf("ERROR: command not found in the path.\n");
1895 }
1896 else
1897 {
1898 printf("Command found in the path as '%s'.\n", path.c_str());
1899 }
1900 }
1901
1902 #endif // TEST_PATHLIST
1903
1904 // ----------------------------------------------------------------------------
1905 // regular expressions
1906 // ----------------------------------------------------------------------------
1907
1908 #ifdef TEST_REGEX
1909
1910 #include "wx/regex.h"
1911
1912 static void TestRegExCompile()
1913 {
1914 wxPuts(_T("*** Testing RE compilation ***\n"));
1915
1916 static struct RegExCompTestData
1917 {
1918 const wxChar *pattern;
1919 bool correct;
1920 } regExCompTestData[] =
1921 {
1922 { _T("foo"), TRUE },
1923 { _T("foo("), FALSE },
1924 { _T("foo(bar"), FALSE },
1925 { _T("foo(bar)"), TRUE },
1926 { _T("foo["), FALSE },
1927 { _T("foo[bar"), FALSE },
1928 { _T("foo[bar]"), TRUE },
1929 { _T("foo{"), TRUE },
1930 { _T("foo{1"), FALSE },
1931 { _T("foo{bar"), TRUE },
1932 { _T("foo{1}"), TRUE },
1933 { _T("foo{1,2}"), TRUE },
1934 { _T("foo{bar}"), TRUE },
1935 { _T("foo*"), TRUE },
1936 { _T("foo**"), FALSE },
1937 { _T("foo+"), TRUE },
1938 { _T("foo++"), FALSE },
1939 { _T("foo?"), TRUE },
1940 { _T("foo??"), FALSE },
1941 { _T("foo?+"), FALSE },
1942 };
1943
1944 wxRegEx re;
1945 for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ )
1946 {
1947 const RegExCompTestData& data = regExCompTestData[n];
1948 bool ok = re.Compile(data.pattern);
1949
1950 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
1951 data.pattern,
1952 ok ? _T("") : _T("not "),
1953 ok == data.correct ? _T("ok") : _T("ERROR"));
1954 }
1955 }
1956
1957 static void TestRegExMatch()
1958 {
1959 wxPuts(_T("*** Testing RE matching ***\n"));
1960
1961 static struct RegExMatchTestData
1962 {
1963 const wxChar *pattern;
1964 const wxChar *text;
1965 bool correct;
1966 } regExMatchTestData[] =
1967 {
1968 { _T("foo"), _T("bar"), FALSE },
1969 { _T("foo"), _T("foobar"), TRUE },
1970 { _T("^foo"), _T("foobar"), TRUE },
1971 { _T("^foo"), _T("barfoo"), FALSE },
1972 { _T("bar$"), _T("barbar"), TRUE },
1973 { _T("bar$"), _T("barbar "), FALSE },
1974 };
1975
1976 for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
1977 {
1978 const RegExMatchTestData& data = regExMatchTestData[n];
1979
1980 wxRegEx re(data.pattern);
1981 bool ok = re.Matches(data.text);
1982
1983 wxPrintf(_T("'%s' %s %s (%s)\n"),
1984 data.pattern,
1985 ok ? _T("matches") : _T("doesn't match"),
1986 data.text,
1987 ok == data.correct ? _T("ok") : _T("ERROR"));
1988 }
1989 }
1990
1991 static void TestRegExSubmatch()
1992 {
1993 wxPuts(_T("*** Testing RE subexpressions ***\n"));
1994
1995 wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
1996 if ( !re.IsValid() )
1997 {
1998 wxPuts(_T("ERROR: compilation failed."));
1999 return;
2000 }
2001
2002 wxString text = _T("Fri Jul 13 18:37:52 CEST 2001");
2003
2004 if ( !re.Matches(text) )
2005 {
2006 wxPuts(_T("ERROR: match expected."));
2007 }
2008 else
2009 {
2010 wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str());
2011
2012 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2013 re.GetMatch(text, 3).c_str(),
2014 re.GetMatch(text, 2).c_str(),
2015 re.GetMatch(text, 4).c_str(),
2016 re.GetMatch(text, 1).c_str());
2017 }
2018 }
2019
2020 static void TestRegExReplacement()
2021 {
2022 wxPuts(_T("*** Testing RE replacement ***"));
2023
2024 static struct RegExReplTestData
2025 {
2026 const wxChar *text;
2027 const wxChar *repl;
2028 const wxChar *result;
2029 size_t count;
2030 } regExReplTestData[] =
2031 {
2032 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2033 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2034 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2035 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2036 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2037 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2038 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2039 };
2040
2041 const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)");
2042 wxRegEx re = pattern;
2043
2044 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern);
2045
2046 for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ )
2047 {
2048 const RegExReplTestData& data = regExReplTestData[n];
2049
2050 wxString text = data.text;
2051 size_t nRepl = re.Replace(&text, data.repl);
2052
2053 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2054 data.text, data.repl,
2055 nRepl, nRepl == 1 ? _T("") : _T("es"),
2056 text.c_str());
2057 if ( text == data.result && nRepl == data.count )
2058 {
2059 wxPuts(_T("ok)"));
2060 }
2061 else
2062 {
2063 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2064 data.count, data.result);
2065 }
2066 }
2067 }
2068
2069 static void TestRegExInteractive()
2070 {
2071 wxPuts(_T("*** Testing RE interactively ***"));
2072
2073 for ( ;; )
2074 {
2075 char pattern[128];
2076 printf("\nEnter a pattern: ");
2077 if ( !fgets(pattern, WXSIZEOF(pattern), stdin) )
2078 break;
2079
2080 // kill the last '\n'
2081 pattern[strlen(pattern) - 1] = 0;
2082
2083 wxRegEx re;
2084 if ( !re.Compile(pattern) )
2085 {
2086 continue;
2087 }
2088
2089 char text[128];
2090 for ( ;; )
2091 {
2092 printf("Enter text to match: ");
2093 if ( !fgets(text, WXSIZEOF(text), stdin) )
2094 break;
2095
2096 // kill the last '\n'
2097 text[strlen(text) - 1] = 0;
2098
2099 if ( !re.Matches(text) )
2100 {
2101 printf("No match.\n");
2102 }
2103 else
2104 {
2105 printf("Pattern matches at '%s'\n", re.GetMatch(text).c_str());
2106
2107 size_t start, len;
2108 for ( size_t n = 1; ; n++ )
2109 {
2110 if ( !re.GetMatch(&start, &len, n) )
2111 {
2112 break;
2113 }
2114
2115 printf("Subexpr %u matched '%s'\n",
2116 n, wxString(text + start, len).c_str());
2117 }
2118 }
2119 }
2120 }
2121 }
2122
2123 #endif // TEST_REGEX
2124
2125 // ----------------------------------------------------------------------------
2126 // registry and related stuff
2127 // ----------------------------------------------------------------------------
2128
2129 // this is for MSW only
2130 #ifndef __WXMSW__
2131 #undef TEST_REGCONF
2132 #undef TEST_REGISTRY
2133 #endif
2134
2135 #ifdef TEST_REGCONF
2136
2137 #include "wx/confbase.h"
2138 #include "wx/msw/regconf.h"
2139
2140 static void TestRegConfWrite()
2141 {
2142 wxRegConfig regconf(_T("console"), _T("wxwindows"));
2143 regconf.Write(_T("Hello"), wxString(_T("world")));
2144 }
2145
2146 #endif // TEST_REGCONF
2147
2148 #ifdef TEST_REGISTRY
2149
2150 #include "wx/msw/registry.h"
2151
2152 // I chose this one because I liked its name, but it probably only exists under
2153 // NT
2154 static const wxChar *TESTKEY =
2155 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2156
2157 static void TestRegistryRead()
2158 {
2159 puts("*** testing registry reading ***");
2160
2161 wxRegKey key(TESTKEY);
2162 printf("The test key name is '%s'.\n", key.GetName().c_str());
2163 if ( !key.Open() )
2164 {
2165 puts("ERROR: test key can't be opened, aborting test.");
2166
2167 return;
2168 }
2169
2170 size_t nSubKeys, nValues;
2171 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2172 {
2173 printf("It has %u subkeys and %u values.\n", nSubKeys, nValues);
2174 }
2175
2176 printf("Enumerating values:\n");
2177
2178 long dummy;
2179 wxString value;
2180 bool cont = key.GetFirstValue(value, dummy);
2181 while ( cont )
2182 {
2183 printf("Value '%s': type ", value.c_str());
2184 switch ( key.GetValueType(value) )
2185 {
2186 case wxRegKey::Type_None: printf("ERROR (none)"); break;
2187 case wxRegKey::Type_String: printf("SZ"); break;
2188 case wxRegKey::Type_Expand_String: printf("EXPAND_SZ"); break;
2189 case wxRegKey::Type_Binary: printf("BINARY"); break;
2190 case wxRegKey::Type_Dword: printf("DWORD"); break;
2191 case wxRegKey::Type_Multi_String: printf("MULTI_SZ"); break;
2192 default: printf("other (unknown)"); break;
2193 }
2194
2195 printf(", value = ");
2196 if ( key.IsNumericValue(value) )
2197 {
2198 long val;
2199 key.QueryValue(value, &val);
2200 printf("%ld", val);
2201 }
2202 else // string
2203 {
2204 wxString val;
2205 key.QueryValue(value, val);
2206 printf("'%s'", val.c_str());
2207
2208 key.QueryRawValue(value, val);
2209 printf(" (raw value '%s')", val.c_str());
2210 }
2211
2212 putchar('\n');
2213
2214 cont = key.GetNextValue(value, dummy);
2215 }
2216 }
2217
2218 static void TestRegistryAssociation()
2219 {
2220 /*
2221 The second call to deleteself genertaes an error message, with a
2222 messagebox saying .flo is crucial to system operation, while the .ddf
2223 call also fails, but with no error message
2224 */
2225
2226 wxRegKey key;
2227
2228 key.SetName("HKEY_CLASSES_ROOT\\.ddf" );
2229 key.Create();
2230 key = "ddxf_auto_file" ;
2231 key.SetName("HKEY_CLASSES_ROOT\\.flo" );
2232 key.Create();
2233 key = "ddxf_auto_file" ;
2234 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon");
2235 key.Create();
2236 key = "program,0" ;
2237 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command");
2238 key.Create();
2239 key = "program \"%1\"" ;
2240
2241 key.SetName("HKEY_CLASSES_ROOT\\.ddf" );
2242 key.DeleteSelf();
2243 key.SetName("HKEY_CLASSES_ROOT\\.flo" );
2244 key.DeleteSelf();
2245 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon");
2246 key.DeleteSelf();
2247 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command");
2248 key.DeleteSelf();
2249 }
2250
2251 #endif // TEST_REGISTRY
2252
2253 // ----------------------------------------------------------------------------
2254 // sockets
2255 // ----------------------------------------------------------------------------
2256
2257 #ifdef TEST_SOCKETS
2258
2259 #include "wx/socket.h"
2260 #include "wx/protocol/protocol.h"
2261 #include "wx/protocol/http.h"
2262
2263 static void TestSocketServer()
2264 {
2265 puts("*** Testing wxSocketServer ***\n");
2266
2267 static const int PORT = 3000;
2268
2269 wxIPV4address addr;
2270 addr.Service(PORT);
2271
2272 wxSocketServer *server = new wxSocketServer(addr);
2273 if ( !server->Ok() )
2274 {
2275 puts("ERROR: failed to bind");
2276
2277 return;
2278 }
2279
2280 for ( ;; )
2281 {
2282 printf("Server: waiting for connection on port %d...\n", PORT);
2283
2284 wxSocketBase *socket = server->Accept();
2285 if ( !socket )
2286 {
2287 puts("ERROR: wxSocketServer::Accept() failed.");
2288 break;
2289 }
2290
2291 puts("Server: got a client.");
2292
2293 server->SetTimeout(60); // 1 min
2294
2295 while ( socket->IsConnected() )
2296 {
2297 wxString s;
2298 char ch = '\0';
2299 for ( ;; )
2300 {
2301 if ( socket->Read(&ch, sizeof(ch)).Error() )
2302 {
2303 // don't log error if the client just close the connection
2304 if ( socket->IsConnected() )
2305 {
2306 puts("ERROR: in wxSocket::Read.");
2307 }
2308
2309 break;
2310 }
2311
2312 if ( ch == '\r' )
2313 continue;
2314
2315 if ( ch == '\n' )
2316 break;
2317
2318 s += ch;
2319 }
2320
2321 if ( ch != '\n' )
2322 {
2323 break;
2324 }
2325
2326 printf("Server: got '%s'.\n", s.c_str());
2327 if ( s == _T("bye") )
2328 {
2329 delete socket;
2330
2331 break;
2332 }
2333
2334 socket->Write(s.MakeUpper().c_str(), s.length());
2335 socket->Write("\r\n", 2);
2336 printf("Server: wrote '%s'.\n", s.c_str());
2337 }
2338
2339 puts("Server: lost a client.");
2340
2341 socket->Destroy();
2342 }
2343
2344 // same as "delete server" but is consistent with GUI programs
2345 server->Destroy();
2346 }
2347
2348 static void TestSocketClient()
2349 {
2350 puts("*** Testing wxSocketClient ***\n");
2351
2352 static const char *hostname = "www.wxwindows.org";
2353
2354 wxIPV4address addr;
2355 addr.Hostname(hostname);
2356 addr.Service(80);
2357
2358 printf("--- Attempting to connect to %s:80...\n", hostname);
2359
2360 wxSocketClient client;
2361 if ( !client.Connect(addr) )
2362 {
2363 printf("ERROR: failed to connect to %s\n", hostname);
2364 }
2365 else
2366 {
2367 printf("--- Connected to %s:%u...\n",
2368 addr.Hostname().c_str(), addr.Service());
2369
2370 char buf[8192];
2371
2372 // could use simply "GET" here I suppose
2373 wxString cmdGet =
2374 wxString::Format("GET http://%s/\r\n", hostname);
2375 client.Write(cmdGet, cmdGet.length());
2376 printf("--- Sent command '%s' to the server\n",
2377 MakePrintable(cmdGet).c_str());
2378 client.Read(buf, WXSIZEOF(buf));
2379 printf("--- Server replied:\n%s", buf);
2380 }
2381 }
2382
2383 #endif // TEST_SOCKETS
2384
2385 // ----------------------------------------------------------------------------
2386 // FTP
2387 // ----------------------------------------------------------------------------
2388
2389 #ifdef TEST_FTP
2390
2391 #include "wx/protocol/ftp.h"
2392
2393 static wxFTP ftp;
2394
2395 #define FTP_ANONYMOUS
2396
2397 #ifdef FTP_ANONYMOUS
2398 static const char *directory = "/pub";
2399 static const char *filename = "welcome.msg";
2400 #else
2401 static const char *directory = "/etc";
2402 static const char *filename = "issue";
2403 #endif
2404
2405 static bool TestFtpConnect()
2406 {
2407 puts("*** Testing FTP connect ***");
2408
2409 #ifdef FTP_ANONYMOUS
2410 static const char *hostname = "ftp.wxwindows.org";
2411
2412 printf("--- Attempting to connect to %s:21 anonymously...\n", hostname);
2413 #else // !FTP_ANONYMOUS
2414 static const char *hostname = "localhost";
2415
2416 char user[256];
2417 fgets(user, WXSIZEOF(user), stdin);
2418 user[strlen(user) - 1] = '\0'; // chop off '\n'
2419 ftp.SetUser(user);
2420
2421 char password[256];
2422 printf("Password for %s: ", password);
2423 fgets(password, WXSIZEOF(password), stdin);
2424 password[strlen(password) - 1] = '\0'; // chop off '\n'
2425 ftp.SetPassword(password);
2426
2427 printf("--- Attempting to connect to %s:21 as %s...\n", hostname, user);
2428 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2429
2430 if ( !ftp.Connect(hostname) )
2431 {
2432 printf("ERROR: failed to connect to %s\n", hostname);
2433
2434 return FALSE;
2435 }
2436 else
2437 {
2438 printf("--- Connected to %s, current directory is '%s'\n",
2439 hostname, ftp.Pwd().c_str());
2440 }
2441
2442 return TRUE;
2443 }
2444
2445 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2446 static void TestFtpWuFtpd()
2447 {
2448 wxFTP ftp;
2449 static const char *hostname = "ftp.eudora.com";
2450 if ( !ftp.Connect(hostname) )
2451 {
2452 printf("ERROR: failed to connect to %s\n", hostname);
2453 }
2454 else
2455 {
2456 static const char *filename = "eudora/pubs/draft-gellens-submit-09.txt";
2457 wxInputStream *in = ftp.GetInputStream(filename);
2458 if ( !in )
2459 {
2460 printf("ERROR: couldn't get input stream for %s\n", filename);
2461 }
2462 else
2463 {
2464 size_t size = in->StreamSize();
2465 printf("Reading file %s (%u bytes)...", filename, size);
2466
2467 char *data = new char[size];
2468 if ( !in->Read(data, size) )
2469 {
2470 puts("ERROR: read error");
2471 }
2472 else
2473 {
2474 printf("Successfully retrieved the file.\n");
2475 }
2476
2477 delete [] data;
2478 delete in;
2479 }
2480 }
2481 }
2482
2483 static void TestFtpList()
2484 {
2485 puts("*** Testing wxFTP file listing ***\n");
2486
2487 // test CWD
2488 if ( !ftp.ChDir(directory) )
2489 {
2490 printf("ERROR: failed to cd to %s\n", directory);
2491 }
2492
2493 printf("Current directory is '%s'\n", ftp.Pwd().c_str());
2494
2495 // test NLIST and LIST
2496 wxArrayString files;
2497 if ( !ftp.GetFilesList(files) )
2498 {
2499 puts("ERROR: failed to get NLIST of files");
2500 }
2501 else
2502 {
2503 printf("Brief list of files under '%s':\n", ftp.Pwd().c_str());
2504 size_t count = files.GetCount();
2505 for ( size_t n = 0; n < count; n++ )
2506 {
2507 printf("\t%s\n", files[n].c_str());
2508 }
2509 puts("End of the file list");
2510 }
2511
2512 if ( !ftp.GetDirList(files) )
2513 {
2514 puts("ERROR: failed to get LIST of files");
2515 }
2516 else
2517 {
2518 printf("Detailed list of files under '%s':\n", ftp.Pwd().c_str());
2519 size_t count = files.GetCount();
2520 for ( size_t n = 0; n < count; n++ )
2521 {
2522 printf("\t%s\n", files[n].c_str());
2523 }
2524 puts("End of the file list");
2525 }
2526
2527 if ( !ftp.ChDir(_T("..")) )
2528 {
2529 puts("ERROR: failed to cd to ..");
2530 }
2531
2532 printf("Current directory is '%s'\n", ftp.Pwd().c_str());
2533 }
2534
2535 static void TestFtpDownload()
2536 {
2537 puts("*** Testing wxFTP download ***\n");
2538
2539 // test RETR
2540 wxInputStream *in = ftp.GetInputStream(filename);
2541 if ( !in )
2542 {
2543 printf("ERROR: couldn't get input stream for %s\n", filename);
2544 }
2545 else
2546 {
2547 size_t size = in->StreamSize();
2548 printf("Reading file %s (%u bytes)...", filename, size);
2549 fflush(stdout);
2550
2551 char *data = new char[size];
2552 if ( !in->Read(data, size) )
2553 {
2554 puts("ERROR: read error");
2555 }
2556 else
2557 {
2558 printf("\nContents of %s:\n%s\n", filename, data);
2559 }
2560
2561 delete [] data;
2562 delete in;
2563 }
2564 }
2565
2566 static void TestFtpFileSize()
2567 {
2568 puts("*** Testing FTP SIZE command ***");
2569
2570 if ( !ftp.ChDir(directory) )
2571 {
2572 printf("ERROR: failed to cd to %s\n", directory);
2573 }
2574
2575 printf("Current directory is '%s'\n", ftp.Pwd().c_str());
2576
2577 if ( ftp.FileExists(filename) )
2578 {
2579 int size = ftp.GetFileSize(filename);
2580 if ( size == -1 )
2581 printf("ERROR: couldn't get size of '%s'\n", filename);
2582 else
2583 printf("Size of '%s' is %d bytes.\n", filename, size);
2584 }
2585 else
2586 {
2587 printf("ERROR: '%s' doesn't exist\n", filename);
2588 }
2589 }
2590
2591 static void TestFtpMisc()
2592 {
2593 puts("*** Testing miscellaneous wxFTP functions ***");
2594
2595 if ( ftp.SendCommand("STAT") != '2' )
2596 {
2597 puts("ERROR: STAT failed");
2598 }
2599 else
2600 {
2601 printf("STAT returned:\n\n%s\n", ftp.GetLastResult().c_str());
2602 }
2603
2604 if ( ftp.SendCommand("HELP SITE") != '2' )
2605 {
2606 puts("ERROR: HELP SITE failed");
2607 }
2608 else
2609 {
2610 printf("The list of site-specific commands:\n\n%s\n",
2611 ftp.GetLastResult().c_str());
2612 }
2613 }
2614
2615 static void TestFtpInteractive()
2616 {
2617 puts("\n*** Interactive wxFTP test ***");
2618
2619 char buf[128];
2620
2621 for ( ;; )
2622 {
2623 printf("Enter FTP command: ");
2624 if ( !fgets(buf, WXSIZEOF(buf), stdin) )
2625 break;
2626
2627 // kill the last '\n'
2628 buf[strlen(buf) - 1] = 0;
2629
2630 // special handling of LIST and NLST as they require data connection
2631 wxString start(buf, 4);
2632 start.MakeUpper();
2633 if ( start == "LIST" || start == "NLST" )
2634 {
2635 wxString wildcard;
2636 if ( strlen(buf) > 4 )
2637 wildcard = buf + 5;
2638
2639 wxArrayString files;
2640 if ( !ftp.GetList(files, wildcard, start == "LIST") )
2641 {
2642 printf("ERROR: failed to get %s of files\n", start.c_str());
2643 }
2644 else
2645 {
2646 printf("--- %s of '%s' under '%s':\n",
2647 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
2648 size_t count = files.GetCount();
2649 for ( size_t n = 0; n < count; n++ )
2650 {
2651 printf("\t%s\n", files[n].c_str());
2652 }
2653 puts("--- End of the file list");
2654 }
2655 }
2656 else // !list
2657 {
2658 char ch = ftp.SendCommand(buf);
2659 printf("Command %s", ch ? "succeeded" : "failed");
2660 if ( ch )
2661 {
2662 printf(" (return code %c)", ch);
2663 }
2664
2665 printf(", server reply:\n%s\n\n", ftp.GetLastResult().c_str());
2666 }
2667 }
2668
2669 puts("\n*** done ***");
2670 }
2671
2672 static void TestFtpUpload()
2673 {
2674 puts("*** Testing wxFTP uploading ***\n");
2675
2676 // upload a file
2677 static const char *file1 = "test1";
2678 static const char *file2 = "test2";
2679 wxOutputStream *out = ftp.GetOutputStream(file1);
2680 if ( out )
2681 {
2682 printf("--- Uploading to %s ---\n", file1);
2683 out->Write("First hello", 11);
2684 delete out;
2685 }
2686
2687 // send a command to check the remote file
2688 if ( ftp.SendCommand(wxString("STAT ") + file1) != '2' )
2689 {
2690 printf("ERROR: STAT %s failed\n", file1);
2691 }
2692 else
2693 {
2694 printf("STAT %s returned:\n\n%s\n",
2695 file1, ftp.GetLastResult().c_str());
2696 }
2697
2698 out = ftp.GetOutputStream(file2);
2699 if ( out )
2700 {
2701 printf("--- Uploading to %s ---\n", file1);
2702 out->Write("Second hello", 12);
2703 delete out;
2704 }
2705 }
2706
2707 #endif // TEST_FTP
2708
2709 // ----------------------------------------------------------------------------
2710 // streams
2711 // ----------------------------------------------------------------------------
2712
2713 #ifdef TEST_STREAMS
2714
2715 #include "wx/wfstream.h"
2716 #include "wx/mstream.h"
2717
2718 static void TestFileStream()
2719 {
2720 puts("*** Testing wxFileInputStream ***");
2721
2722 static const wxChar *filename = _T("testdata.fs");
2723 {
2724 wxFileOutputStream fsOut(filename);
2725 fsOut.Write("foo", 3);
2726 }
2727
2728 wxFileInputStream fsIn(filename);
2729 printf("File stream size: %u\n", fsIn.GetSize());
2730 while ( !fsIn.Eof() )
2731 {
2732 putchar(fsIn.GetC());
2733 }
2734
2735 if ( !wxRemoveFile(filename) )
2736 {
2737 printf("ERROR: failed to remove the file '%s'.\n", filename);
2738 }
2739
2740 puts("\n*** wxFileInputStream test done ***");
2741 }
2742
2743 static void TestMemoryStream()
2744 {
2745 puts("*** Testing wxMemoryInputStream ***");
2746
2747 wxChar buf[1024];
2748 wxStrncpy(buf, _T("Hello, stream!"), WXSIZEOF(buf));
2749
2750 wxMemoryInputStream memInpStream(buf, wxStrlen(buf));
2751 printf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
2752 while ( !memInpStream.Eof() )
2753 {
2754 putchar(memInpStream.GetC());
2755 }
2756
2757 puts("\n*** wxMemoryInputStream test done ***");
2758 }
2759
2760 #endif // TEST_STREAMS
2761
2762 // ----------------------------------------------------------------------------
2763 // timers
2764 // ----------------------------------------------------------------------------
2765
2766 #ifdef TEST_TIMER
2767
2768 #include "wx/timer.h"
2769 #include "wx/utils.h"
2770
2771 static void TestStopWatch()
2772 {
2773 puts("*** Testing wxStopWatch ***\n");
2774
2775 wxStopWatch sw;
2776 printf("Sleeping 3 seconds...");
2777 wxSleep(3);
2778 printf("\telapsed time: %ldms\n", sw.Time());
2779
2780 sw.Pause();
2781 printf("Sleeping 2 more seconds...");
2782 wxSleep(2);
2783 printf("\telapsed time: %ldms\n", sw.Time());
2784
2785 sw.Resume();
2786 printf("And 3 more seconds...");
2787 wxSleep(3);
2788 printf("\telapsed time: %ldms\n", sw.Time());
2789
2790 wxStopWatch sw2;
2791 puts("\nChecking for 'backwards clock' bug...");
2792 for ( size_t n = 0; n < 70; n++ )
2793 {
2794 sw2.Start();
2795
2796 for ( size_t m = 0; m < 100000; m++ )
2797 {
2798 if ( sw.Time() < 0 || sw2.Time() < 0 )
2799 {
2800 puts("\ntime is negative - ERROR!");
2801 }
2802 }
2803
2804 putchar('.');
2805 }
2806
2807 puts(", ok.");
2808 }
2809
2810 #endif // TEST_TIMER
2811
2812 // ----------------------------------------------------------------------------
2813 // vCard support
2814 // ----------------------------------------------------------------------------
2815
2816 #ifdef TEST_VCARD
2817
2818 #include "wx/vcard.h"
2819
2820 static void DumpVObject(size_t level, const wxVCardObject& vcard)
2821 {
2822 void *cookie;
2823 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
2824 while ( vcObj )
2825 {
2826 printf("%s%s",
2827 wxString(_T('\t'), level).c_str(),
2828 vcObj->GetName().c_str());
2829
2830 wxString value;
2831 switch ( vcObj->GetType() )
2832 {
2833 case wxVCardObject::String:
2834 case wxVCardObject::UString:
2835 {
2836 wxString val;
2837 vcObj->GetValue(&val);
2838 value << _T('"') << val << _T('"');
2839 }
2840 break;
2841
2842 case wxVCardObject::Int:
2843 {
2844 unsigned int i;
2845 vcObj->GetValue(&i);
2846 value.Printf(_T("%u"), i);
2847 }
2848 break;
2849
2850 case wxVCardObject::Long:
2851 {
2852 unsigned long l;
2853 vcObj->GetValue(&l);
2854 value.Printf(_T("%lu"), l);
2855 }
2856 break;
2857
2858 case wxVCardObject::None:
2859 break;
2860
2861 case wxVCardObject::Object:
2862 value = _T("<node>");
2863 break;
2864
2865 default:
2866 value = _T("<unknown value type>");
2867 }
2868
2869 if ( !!value )
2870 printf(" = %s", value.c_str());
2871 putchar('\n');
2872
2873 DumpVObject(level + 1, *vcObj);
2874
2875 delete vcObj;
2876 vcObj = vcard.GetNextProp(&cookie);
2877 }
2878 }
2879
2880 static void DumpVCardAddresses(const wxVCard& vcard)
2881 {
2882 puts("\nShowing all addresses from vCard:\n");
2883
2884 size_t nAdr = 0;
2885 void *cookie;
2886 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
2887 while ( addr )
2888 {
2889 wxString flagsStr;
2890 int flags = addr->GetFlags();
2891 if ( flags & wxVCardAddress::Domestic )
2892 {
2893 flagsStr << _T("domestic ");
2894 }
2895 if ( flags & wxVCardAddress::Intl )
2896 {
2897 flagsStr << _T("international ");
2898 }
2899 if ( flags & wxVCardAddress::Postal )
2900 {
2901 flagsStr << _T("postal ");
2902 }
2903 if ( flags & wxVCardAddress::Parcel )
2904 {
2905 flagsStr << _T("parcel ");
2906 }
2907 if ( flags & wxVCardAddress::Home )
2908 {
2909 flagsStr << _T("home ");
2910 }
2911 if ( flags & wxVCardAddress::Work )
2912 {
2913 flagsStr << _T("work ");
2914 }
2915
2916 printf("Address %u:\n"
2917 "\tflags = %s\n"
2918 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
2919 ++nAdr,
2920 flagsStr.c_str(),
2921 addr->GetPostOffice().c_str(),
2922 addr->GetExtAddress().c_str(),
2923 addr->GetStreet().c_str(),
2924 addr->GetLocality().c_str(),
2925 addr->GetRegion().c_str(),
2926 addr->GetPostalCode().c_str(),
2927 addr->GetCountry().c_str()
2928 );
2929
2930 delete addr;
2931 addr = vcard.GetNextAddress(&cookie);
2932 }
2933 }
2934
2935 static void DumpVCardPhoneNumbers(const wxVCard& vcard)
2936 {
2937 puts("\nShowing all phone numbers from vCard:\n");
2938
2939 size_t nPhone = 0;
2940 void *cookie;
2941 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
2942 while ( phone )
2943 {
2944 wxString flagsStr;
2945 int flags = phone->GetFlags();
2946 if ( flags & wxVCardPhoneNumber::Voice )
2947 {
2948 flagsStr << _T("voice ");
2949 }
2950 if ( flags & wxVCardPhoneNumber::Fax )
2951 {
2952 flagsStr << _T("fax ");
2953 }
2954 if ( flags & wxVCardPhoneNumber::Cellular )
2955 {
2956 flagsStr << _T("cellular ");
2957 }
2958 if ( flags & wxVCardPhoneNumber::Modem )
2959 {
2960 flagsStr << _T("modem ");
2961 }
2962 if ( flags & wxVCardPhoneNumber::Home )
2963 {
2964 flagsStr << _T("home ");
2965 }
2966 if ( flags & wxVCardPhoneNumber::Work )
2967 {
2968 flagsStr << _T("work ");
2969 }
2970
2971 printf("Phone number %u:\n"
2972 "\tflags = %s\n"
2973 "\tvalue = %s\n",
2974 ++nPhone,
2975 flagsStr.c_str(),
2976 phone->GetNumber().c_str()
2977 );
2978
2979 delete phone;
2980 phone = vcard.GetNextPhoneNumber(&cookie);
2981 }
2982 }
2983
2984 static void TestVCardRead()
2985 {
2986 puts("*** Testing wxVCard reading ***\n");
2987
2988 wxVCard vcard(_T("vcard.vcf"));
2989 if ( !vcard.IsOk() )
2990 {
2991 puts("ERROR: couldn't load vCard.");
2992 }
2993 else
2994 {
2995 // read individual vCard properties
2996 wxVCardObject *vcObj = vcard.GetProperty("FN");
2997 wxString value;
2998 if ( vcObj )
2999 {
3000 vcObj->GetValue(&value);
3001 delete vcObj;
3002 }
3003 else
3004 {
3005 value = _T("<none>");
3006 }
3007
3008 printf("Full name retrieved directly: %s\n", value.c_str());
3009
3010
3011 if ( !vcard.GetFullName(&value) )
3012 {
3013 value = _T("<none>");
3014 }
3015
3016 printf("Full name from wxVCard API: %s\n", value.c_str());
3017
3018 // now show how to deal with multiply occuring properties
3019 DumpVCardAddresses(vcard);
3020 DumpVCardPhoneNumbers(vcard);
3021
3022 // and finally show all
3023 puts("\nNow dumping the entire vCard:\n"
3024 "-----------------------------\n");
3025
3026 DumpVObject(0, vcard);
3027 }
3028 }
3029
3030 static void TestVCardWrite()
3031 {
3032 puts("*** Testing wxVCard writing ***\n");
3033
3034 wxVCard vcard;
3035 if ( !vcard.IsOk() )
3036 {
3037 puts("ERROR: couldn't create vCard.");
3038 }
3039 else
3040 {
3041 // set some fields
3042 vcard.SetName("Zeitlin", "Vadim");
3043 vcard.SetFullName("Vadim Zeitlin");
3044 vcard.SetOrganization("wxWindows", "R&D");
3045
3046 // just dump the vCard back
3047 puts("Entire vCard follows:\n");
3048 puts(vcard.Write());
3049 }
3050 }
3051
3052 #endif // TEST_VCARD
3053
3054 // ----------------------------------------------------------------------------
3055 // wide char (Unicode) support
3056 // ----------------------------------------------------------------------------
3057
3058 #ifdef TEST_WCHAR
3059
3060 #include "wx/strconv.h"
3061 #include "wx/fontenc.h"
3062 #include "wx/encconv.h"
3063 #include "wx/buffer.h"
3064
3065 static void TestUtf8()
3066 {
3067 puts("*** Testing UTF8 support ***\n");
3068
3069 static const char textInUtf8[] =
3070 {
3071 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3072 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3073 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3074 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3075 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3076 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3077 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3078 };
3079
3080 char buf[1024];
3081 wchar_t wbuf[1024];
3082 if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
3083 {
3084 puts("ERROR: UTF-8 decoding failed.");
3085 }
3086 else
3087 {
3088 // using wxEncodingConverter
3089 #if 0
3090 wxEncodingConverter ec;
3091 ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
3092 ec.Convert(wbuf, buf);
3093 #else // using wxCSConv
3094 wxCSConv conv(_T("koi8-r"));
3095 if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 )
3096 {
3097 puts("ERROR: conversion to KOI8-R failed.");
3098 }
3099 else
3100 #endif
3101
3102 printf("The resulting string (in koi8-r): %s\n", buf);
3103 }
3104 }
3105
3106 #endif // TEST_WCHAR
3107
3108 // ----------------------------------------------------------------------------
3109 // ZIP stream
3110 // ----------------------------------------------------------------------------
3111
3112 #ifdef TEST_ZIP
3113
3114 #include "wx/filesys.h"
3115 #include "wx/fs_zip.h"
3116 #include "wx/zipstrm.h"
3117
3118 static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
3119
3120 static void TestZipStreamRead()
3121 {
3122 puts("*** Testing ZIP reading ***\n");
3123
3124 static const wxChar *filename = _T("foo");
3125 wxZipInputStream istr(TESTFILE_ZIP, filename);
3126 printf("Archive size: %u\n", istr.GetSize());
3127
3128 printf("Dumping the file '%s':\n", filename);
3129 while ( !istr.Eof() )
3130 {
3131 putchar(istr.GetC());
3132 fflush(stdout);
3133 }
3134
3135 puts("\n----- done ------");
3136 }
3137
3138 static void DumpZipDirectory(wxFileSystem& fs,
3139 const wxString& dir,
3140 const wxString& indent)
3141 {
3142 wxString prefix = wxString::Format(_T("%s#zip:%s"),
3143 TESTFILE_ZIP, dir.c_str());
3144 wxString wildcard = prefix + _T("/*");
3145
3146 wxString dirname = fs.FindFirst(wildcard, wxDIR);
3147 while ( !dirname.empty() )
3148 {
3149 if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
3150 {
3151 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3152
3153 break;
3154 }
3155
3156 wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
3157
3158 DumpZipDirectory(fs, dirname,
3159 indent + wxString(_T(' '), 4));
3160
3161 dirname = fs.FindNext();
3162 }
3163
3164 wxString filename = fs.FindFirst(wildcard, wxFILE);
3165 while ( !filename.empty() )
3166 {
3167 if ( !filename.StartsWith(prefix, &filename) )
3168 {
3169 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3170
3171 break;
3172 }
3173
3174 wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
3175
3176 filename = fs.FindNext();
3177 }
3178 }
3179
3180 static void TestZipFileSystem()
3181 {
3182 puts("*** Testing ZIP file system ***\n");
3183
3184 wxFileSystem::AddHandler(new wxZipFSHandler);
3185 wxFileSystem fs;
3186 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
3187
3188 DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
3189 }
3190
3191 #endif // TEST_ZIP
3192
3193 // ----------------------------------------------------------------------------
3194 // ZLIB stream
3195 // ----------------------------------------------------------------------------
3196
3197 #ifdef TEST_ZLIB
3198
3199 #include "wx/zstream.h"
3200 #include "wx/wfstream.h"
3201
3202 static const wxChar *FILENAME_GZ = _T("test.gz");
3203 static const char *TEST_DATA = "hello and hello again";
3204
3205 static void TestZlibStreamWrite()
3206 {
3207 puts("*** Testing Zlib stream reading ***\n");
3208
3209 wxFileOutputStream fileOutStream(FILENAME_GZ);
3210 wxZlibOutputStream ostr(fileOutStream, 0);
3211 printf("Compressing the test string... ");
3212 ostr.Write(TEST_DATA, sizeof(TEST_DATA));
3213 if ( !ostr )
3214 {
3215 puts("(ERROR: failed)");
3216 }
3217 else
3218 {
3219 puts("(ok)");
3220 }
3221
3222 puts("\n----- done ------");
3223 }
3224
3225 static void TestZlibStreamRead()
3226 {
3227 puts("*** Testing Zlib stream reading ***\n");
3228
3229 wxFileInputStream fileInStream(FILENAME_GZ);
3230 wxZlibInputStream istr(fileInStream);
3231 printf("Archive size: %u\n", istr.GetSize());
3232
3233 puts("Dumping the file:");
3234 while ( !istr.Eof() )
3235 {
3236 putchar(istr.GetC());
3237 fflush(stdout);
3238 }
3239
3240 puts("\n----- done ------");
3241 }
3242
3243 #endif // TEST_ZLIB
3244
3245 // ----------------------------------------------------------------------------
3246 // date time
3247 // ----------------------------------------------------------------------------
3248
3249 #ifdef TEST_DATETIME
3250
3251 #include <math.h>
3252
3253 #include "wx/date.h"
3254 #include "wx/datetime.h"
3255
3256 // the test data
3257 struct Date
3258 {
3259 wxDateTime::wxDateTime_t day;
3260 wxDateTime::Month month;
3261 int year;
3262 wxDateTime::wxDateTime_t hour, min, sec;
3263 double jdn;
3264 wxDateTime::WeekDay wday;
3265 time_t gmticks, ticks;
3266
3267 void Init(const wxDateTime::Tm& tm)
3268 {
3269 day = tm.mday;
3270 month = tm.mon;
3271 year = tm.year;
3272 hour = tm.hour;
3273 min = tm.min;
3274 sec = tm.sec;
3275 jdn = 0.0;
3276 gmticks = ticks = -1;
3277 }
3278
3279 wxDateTime DT() const
3280 { return wxDateTime(day, month, year, hour, min, sec); }
3281
3282 bool SameDay(const wxDateTime::Tm& tm) const
3283 {
3284 return day == tm.mday && month == tm.mon && year == tm.year;
3285 }
3286
3287 wxString Format() const
3288 {
3289 wxString s;
3290 s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
3291 hour, min, sec,
3292 wxDateTime::GetMonthName(month).c_str(),
3293 day,
3294 abs(wxDateTime::ConvertYearToBC(year)),
3295 year > 0 ? "AD" : "BC");
3296 return s;
3297 }
3298
3299 wxString FormatDate() const
3300 {
3301 wxString s;
3302 s.Printf("%02d-%s-%4d%s",
3303 day,
3304 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
3305 abs(wxDateTime::ConvertYearToBC(year)),
3306 year > 0 ? "AD" : "BC");
3307 return s;
3308 }
3309 };
3310
3311 static const Date testDates[] =
3312 {
3313 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
3314 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
3315 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
3316 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
3317 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
3318 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
3319 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
3320 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
3321 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
3322 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
3323 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
3324 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
3325 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
3326 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
3327 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
3328 };
3329
3330 // this test miscellaneous static wxDateTime functions
3331 static void TestTimeStatic()
3332 {
3333 puts("\n*** wxDateTime static methods test ***");
3334
3335 // some info about the current date
3336 int year = wxDateTime::GetCurrentYear();
3337 printf("Current year %d is %sa leap one and has %d days.\n",
3338 year,
3339 wxDateTime::IsLeapYear(year) ? "" : "not ",
3340 wxDateTime::GetNumberOfDays(year));
3341
3342 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
3343 printf("Current month is '%s' ('%s') and it has %d days\n",
3344 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
3345 wxDateTime::GetMonthName(month).c_str(),
3346 wxDateTime::GetNumberOfDays(month));
3347
3348 // leap year logic
3349 static const size_t nYears = 5;
3350 static const size_t years[2][nYears] =
3351 {
3352 // first line: the years to test
3353 { 1990, 1976, 2000, 2030, 1984, },
3354
3355 // second line: TRUE if leap, FALSE otherwise
3356 { FALSE, TRUE, TRUE, FALSE, TRUE }
3357 };
3358
3359 for ( size_t n = 0; n < nYears; n++ )
3360 {
3361 int year = years[0][n];
3362 bool should = years[1][n] != 0,
3363 is = wxDateTime::IsLeapYear(year);
3364
3365 printf("Year %d is %sa leap year (%s)\n",
3366 year,
3367 is ? "" : "not ",
3368 should == is ? "ok" : "ERROR");
3369
3370 wxASSERT( should == wxDateTime::IsLeapYear(year) );
3371 }
3372 }
3373
3374 // test constructing wxDateTime objects
3375 static void TestTimeSet()
3376 {
3377 puts("\n*** wxDateTime construction test ***");
3378
3379 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
3380 {
3381 const Date& d1 = testDates[n];
3382 wxDateTime dt = d1.DT();
3383
3384 Date d2;
3385 d2.Init(dt.GetTm());
3386
3387 wxString s1 = d1.Format(),
3388 s2 = d2.Format();
3389
3390 printf("Date: %s == %s (%s)\n",
3391 s1.c_str(), s2.c_str(),
3392 s1 == s2 ? "ok" : "ERROR");
3393 }
3394 }
3395
3396 // test time zones stuff
3397 static void TestTimeZones()
3398 {
3399 puts("\n*** wxDateTime timezone test ***");
3400
3401 wxDateTime now = wxDateTime::Now();
3402
3403 printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
3404 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
3405 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
3406 printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
3407 printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
3408 printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
3409
3410 wxDateTime::Tm tm = now.GetTm();
3411 if ( wxDateTime(tm) != now )
3412 {
3413 printf("ERROR: got %s instead of %s\n",
3414 wxDateTime(tm).Format().c_str(), now.Format().c_str());
3415 }
3416 }
3417
3418 // test some minimal support for the dates outside the standard range
3419 static void TestTimeRange()
3420 {
3421 puts("\n*** wxDateTime out-of-standard-range dates test ***");
3422
3423 static const char *fmt = "%d-%b-%Y %H:%M:%S";
3424
3425 printf("Unix epoch:\t%s\n",
3426 wxDateTime(2440587.5).Format(fmt).c_str());
3427 printf("Feb 29, 0: \t%s\n",
3428 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
3429 printf("JDN 0: \t%s\n",
3430 wxDateTime(0.0).Format(fmt).c_str());
3431 printf("Jan 1, 1AD:\t%s\n",
3432 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
3433 printf("May 29, 2099:\t%s\n",
3434 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
3435 }
3436
3437 static void TestTimeTicks()
3438 {
3439 puts("\n*** wxDateTime ticks test ***");
3440
3441 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
3442 {
3443 const Date& d = testDates[n];
3444 if ( d.ticks == -1 )
3445 continue;
3446
3447 wxDateTime dt = d.DT();
3448 long ticks = (dt.GetValue() / 1000).ToLong();
3449 printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
3450 if ( ticks == d.ticks )
3451 {
3452 puts(" (ok)");
3453 }
3454 else
3455 {
3456 printf(" (ERROR: should be %ld, delta = %ld)\n",
3457 d.ticks, ticks - d.ticks);
3458 }
3459
3460 dt = d.DT().ToTimezone(wxDateTime::GMT0);
3461 ticks = (dt.GetValue() / 1000).ToLong();
3462 printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
3463 if ( ticks == d.gmticks )
3464 {
3465 puts(" (ok)");
3466 }
3467 else
3468 {
3469 printf(" (ERROR: should be %ld, delta = %ld)\n",
3470 d.gmticks, ticks - d.gmticks);
3471 }
3472 }
3473
3474 puts("");
3475 }
3476
3477 // test conversions to JDN &c
3478 static void TestTimeJDN()
3479 {
3480 puts("\n*** wxDateTime to JDN test ***");
3481
3482 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
3483 {
3484 const Date& d = testDates[n];
3485 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
3486 double jdn = dt.GetJulianDayNumber();
3487
3488 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
3489 if ( jdn == d.jdn )
3490 {
3491 puts(" (ok)");
3492 }
3493 else
3494 {
3495 printf(" (ERROR: should be %f, delta = %f)\n",
3496 d.jdn, jdn - d.jdn);
3497 }
3498 }
3499 }
3500
3501 // test week days computation
3502 static void TestTimeWDays()
3503 {
3504 puts("\n*** wxDateTime weekday test ***");
3505
3506 // test GetWeekDay()
3507 size_t n;
3508 for ( n = 0; n < WXSIZEOF(testDates); n++ )
3509 {
3510 const Date& d = testDates[n];
3511 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
3512
3513 wxDateTime::WeekDay wday = dt.GetWeekDay();
3514 printf("%s is: %s",
3515 d.Format().c_str(),
3516 wxDateTime::GetWeekDayName(wday).c_str());
3517 if ( wday == d.wday )
3518 {
3519 puts(" (ok)");
3520 }
3521 else
3522 {
3523 printf(" (ERROR: should be %s)\n",
3524 wxDateTime::GetWeekDayName(d.wday).c_str());
3525 }
3526 }
3527
3528 puts("");
3529
3530 // test SetToWeekDay()
3531 struct WeekDateTestData
3532 {
3533 Date date; // the real date (precomputed)
3534 int nWeek; // its week index in the month
3535 wxDateTime::WeekDay wday; // the weekday
3536 wxDateTime::Month month; // the month
3537 int year; // and the year
3538
3539 wxString Format() const
3540 {
3541 wxString s, which;
3542 switch ( nWeek < -1 ? -nWeek : nWeek )
3543 {
3544 case 1: which = "first"; break;
3545 case 2: which = "second"; break;
3546 case 3: which = "third"; break;
3547 case 4: which = "fourth"; break;
3548 case 5: which = "fifth"; break;
3549
3550 case -1: which = "last"; break;
3551 }
3552
3553 if ( nWeek < -1 )
3554 {
3555 which += " from end";
3556 }
3557
3558 s.Printf("The %s %s of %s in %d",
3559 which.c_str(),
3560 wxDateTime::GetWeekDayName(wday).c_str(),
3561 wxDateTime::GetMonthName(month).c_str(),
3562 year);
3563
3564 return s;
3565 }
3566 };
3567
3568 // the array data was generated by the following python program
3569 /*
3570 from DateTime import *
3571 from whrandom import *
3572 from string import *
3573
3574 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
3575 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
3576
3577 week = DateTimeDelta(7)
3578
3579 for n in range(20):
3580 year = randint(1900, 2100)
3581 month = randint(1, 12)
3582 day = randint(1, 28)
3583 dt = DateTime(year, month, day)
3584 wday = dt.day_of_week
3585
3586 countFromEnd = choice([-1, 1])
3587 weekNum = 0;
3588
3589 while dt.month is month:
3590 dt = dt - countFromEnd * week
3591 weekNum = weekNum + countFromEnd
3592
3593 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
3594
3595 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
3596 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
3597 */
3598
3599 static const WeekDateTestData weekDatesTestData[] =
3600 {
3601 { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
3602 { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
3603 { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
3604 { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
3605 { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
3606 { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
3607 { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
3608 { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
3609 { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
3610 { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
3611 { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
3612 { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
3613 { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
3614 { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
3615 { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
3616 { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
3617 { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
3618 { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
3619 { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
3620 { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 },
3621 };
3622
3623 static const char *fmt = "%d-%b-%Y";
3624
3625 wxDateTime dt;
3626 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
3627 {
3628 const WeekDateTestData& wd = weekDatesTestData[n];
3629
3630 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
3631
3632 printf("%s is %s", wd.Format().c_str(), dt.Format(fmt).c_str());
3633
3634 const Date& d = wd.date;
3635 if ( d.SameDay(dt.GetTm()) )
3636 {
3637 puts(" (ok)");
3638 }
3639 else
3640 {
3641 dt.Set(d.day, d.month, d.year);
3642
3643 printf(" (ERROR: should be %s)\n", dt.Format(fmt).c_str());
3644 }
3645 }
3646 }
3647
3648 // test the computation of (ISO) week numbers
3649 static void TestTimeWNumber()
3650 {
3651 puts("\n*** wxDateTime week number test ***");
3652
3653 struct WeekNumberTestData
3654 {
3655 Date date; // the date
3656 wxDateTime::wxDateTime_t week; // the week number in the year
3657 wxDateTime::wxDateTime_t wmon; // the week number in the month
3658 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
3659 wxDateTime::wxDateTime_t dnum; // day number in the year
3660 };
3661
3662 // data generated with the following python script:
3663 /*
3664 from DateTime import *
3665 from whrandom import *
3666 from string import *
3667
3668 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
3669 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
3670
3671 def GetMonthWeek(dt):
3672 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
3673 if weekNumMonth < 0:
3674 weekNumMonth = weekNumMonth + 53
3675 return weekNumMonth
3676
3677 def GetLastSundayBefore(dt):
3678 if dt.iso_week[2] == 7:
3679 return dt
3680 else:
3681 return dt - DateTimeDelta(dt.iso_week[2])
3682
3683 for n in range(20):
3684 year = randint(1900, 2100)
3685 month = randint(1, 12)
3686 day = randint(1, 28)
3687 dt = DateTime(year, month, day)
3688 dayNum = dt.day_of_year
3689 weekNum = dt.iso_week[1]
3690 weekNumMonth = GetMonthWeek(dt)
3691
3692 weekNumMonth2 = 0
3693 dtSunday = GetLastSundayBefore(dt)
3694
3695 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
3696 weekNumMonth2 = weekNumMonth2 + 1
3697 dtSunday = dtSunday - DateTimeDelta(7)
3698
3699 data = { 'day': rjust(`day`, 2), \
3700 'month': monthNames[month - 1], \
3701 'year': year, \
3702 'weekNum': rjust(`weekNum`, 2), \
3703 'weekNumMonth': weekNumMonth, \
3704 'weekNumMonth2': weekNumMonth2, \
3705 'dayNum': rjust(`dayNum`, 3) }
3706
3707 print " { { %(day)s, "\
3708 "wxDateTime::%(month)s, "\
3709 "%(year)d }, "\
3710 "%(weekNum)s, "\
3711 "%(weekNumMonth)s, "\
3712 "%(weekNumMonth2)s, "\
3713 "%(dayNum)s }," % data
3714
3715 */
3716 static const WeekNumberTestData weekNumberTestDates[] =
3717 {
3718 { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 },
3719 { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 },
3720 { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 },
3721 { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 },
3722 { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 },
3723 { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 },
3724 { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 },
3725 { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 },
3726 { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 },
3727 { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 },
3728 { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 },
3729 { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 },
3730 { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 },
3731 { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 },
3732 { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 },
3733 { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 },
3734 { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 },
3735 { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 },
3736 { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 },
3737 { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 },
3738 };
3739
3740 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
3741 {
3742 const WeekNumberTestData& wn = weekNumberTestDates[n];
3743 const Date& d = wn.date;
3744
3745 wxDateTime dt = d.DT();
3746
3747 wxDateTime::wxDateTime_t
3748 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
3749 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
3750 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
3751 dnum = dt.GetDayOfYear();
3752
3753 printf("%s: the day number is %d",
3754 d.FormatDate().c_str(), dnum);
3755 if ( dnum == wn.dnum )
3756 {
3757 printf(" (ok)");
3758 }
3759 else
3760 {
3761 printf(" (ERROR: should be %d)", wn.dnum);
3762 }
3763
3764 printf(", week in month is %d", wmon);
3765 if ( wmon == wn.wmon )
3766 {
3767 printf(" (ok)");
3768 }
3769 else
3770 {
3771 printf(" (ERROR: should be %d)", wn.wmon);
3772 }
3773
3774 printf(" or %d", wmon2);
3775 if ( wmon2 == wn.wmon2 )
3776 {
3777 printf(" (ok)");
3778 }
3779 else
3780 {
3781 printf(" (ERROR: should be %d)", wn.wmon2);
3782 }
3783
3784 printf(", week in year is %d", week);
3785 if ( week == wn.week )
3786 {
3787 puts(" (ok)");
3788 }
3789 else
3790 {
3791 printf(" (ERROR: should be %d)\n", wn.week);
3792 }
3793 }
3794 }
3795
3796 // test DST calculations
3797 static void TestTimeDST()
3798 {
3799 puts("\n*** wxDateTime DST test ***");
3800
3801 printf("DST is%s in effect now.\n\n",
3802 wxDateTime::Now().IsDST() ? "" : " not");
3803
3804 // taken from http://www.energy.ca.gov/daylightsaving.html
3805 static const Date datesDST[2][2004 - 1900 + 1] =
3806 {
3807 {
3808 { 1, wxDateTime::Apr, 1990 },
3809 { 7, wxDateTime::Apr, 1991 },
3810 { 5, wxDateTime::Apr, 1992 },
3811 { 4, wxDateTime::Apr, 1993 },
3812 { 3, wxDateTime::Apr, 1994 },
3813 { 2, wxDateTime::Apr, 1995 },
3814 { 7, wxDateTime::Apr, 1996 },
3815 { 6, wxDateTime::Apr, 1997 },
3816 { 5, wxDateTime::Apr, 1998 },
3817 { 4, wxDateTime::Apr, 1999 },
3818 { 2, wxDateTime::Apr, 2000 },
3819 { 1, wxDateTime::Apr, 2001 },
3820 { 7, wxDateTime::Apr, 2002 },
3821 { 6, wxDateTime::Apr, 2003 },
3822 { 4, wxDateTime::Apr, 2004 },
3823 },
3824 {
3825 { 28, wxDateTime::Oct, 1990 },
3826 { 27, wxDateTime::Oct, 1991 },
3827 { 25, wxDateTime::Oct, 1992 },
3828 { 31, wxDateTime::Oct, 1993 },
3829 { 30, wxDateTime::Oct, 1994 },
3830 { 29, wxDateTime::Oct, 1995 },
3831 { 27, wxDateTime::Oct, 1996 },
3832 { 26, wxDateTime::Oct, 1997 },
3833 { 25, wxDateTime::Oct, 1998 },
3834 { 31, wxDateTime::Oct, 1999 },
3835 { 29, wxDateTime::Oct, 2000 },
3836 { 28, wxDateTime::Oct, 2001 },
3837 { 27, wxDateTime::Oct, 2002 },
3838 { 26, wxDateTime::Oct, 2003 },
3839 { 31, wxDateTime::Oct, 2004 },
3840 }
3841 };
3842
3843 int year;
3844 for ( year = 1990; year < 2005; year++ )
3845 {
3846 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
3847 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
3848
3849 printf("DST period in the US for year %d: from %s to %s",
3850 year, dtBegin.Format().c_str(), dtEnd.Format().c_str());
3851
3852 size_t n = year - 1990;
3853 const Date& dBegin = datesDST[0][n];
3854 const Date& dEnd = datesDST[1][n];
3855
3856 if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) )
3857 {
3858 puts(" (ok)");
3859 }
3860 else
3861 {
3862 printf(" (ERROR: should be %s %d to %s %d)\n",
3863 wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day,
3864 wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day);
3865 }
3866 }
3867
3868 puts("");
3869
3870 for ( year = 1990; year < 2005; year++ )
3871 {
3872 printf("DST period in Europe for year %d: from %s to %s\n",
3873 year,
3874 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
3875 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
3876 }
3877 }
3878
3879 // test wxDateTime -> text conversion
3880 static void TestTimeFormat()
3881 {
3882 puts("\n*** wxDateTime formatting test ***");
3883
3884 // some information may be lost during conversion, so store what kind
3885 // of info should we recover after a round trip
3886 enum CompareKind
3887 {
3888 CompareNone, // don't try comparing
3889 CompareBoth, // dates and times should be identical
3890 CompareDate, // dates only
3891 CompareTime // time only
3892 };
3893
3894 static const struct
3895 {
3896 CompareKind compareKind;
3897 const char *format;
3898 } formatTestFormats[] =
3899 {
3900 { CompareBoth, "---> %c" },
3901 { CompareDate, "Date is %A, %d of %B, in year %Y" },
3902 { CompareBoth, "Date is %x, time is %X" },
3903 { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
3904 { CompareNone, "The day of year: %j, the week of year: %W" },
3905 { CompareDate, "ISO date without separators: %4Y%2m%2d" },
3906 };
3907
3908 static const Date formatTestDates[] =
3909 {
3910 { 29, wxDateTime::May, 1976, 18, 30, 00 },
3911 { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
3912 #if 0
3913 // this test can't work for other centuries because it uses two digit
3914 // years in formats, so don't even try it
3915 { 29, wxDateTime::May, 2076, 18, 30, 00 },
3916 { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
3917 { 01, wxDateTime::Jan, -52, 03, 16, 47 },
3918 #endif
3919 };
3920
3921 // an extra test (as it doesn't depend on date, don't do it in the loop)
3922 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
3923
3924 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
3925 {
3926 puts("");
3927
3928 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
3929 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
3930 {
3931 wxString s = dt.Format(formatTestFormats[n].format);
3932 printf("%s", s.c_str());
3933
3934 // what can we recover?
3935 int kind = formatTestFormats[n].compareKind;
3936
3937 // convert back
3938 wxDateTime dt2;
3939 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
3940 if ( !result )
3941 {
3942 // converion failed - should it have?
3943 if ( kind == CompareNone )
3944 puts(" (ok)");
3945 else
3946 puts(" (ERROR: conversion back failed)");
3947 }
3948 else if ( *result )
3949 {
3950 // should have parsed the entire string
3951 puts(" (ERROR: conversion back stopped too soon)");
3952 }
3953 else
3954 {
3955 bool equal = FALSE; // suppress compilaer warning
3956 switch ( kind )
3957 {
3958 case CompareBoth:
3959 equal = dt2 == dt;
3960 break;
3961
3962 case CompareDate:
3963 equal = dt.IsSameDate(dt2);
3964 break;
3965
3966 case CompareTime:
3967 equal = dt.IsSameTime(dt2);
3968 break;
3969 }
3970
3971 if ( !equal )
3972 {
3973 printf(" (ERROR: got back '%s' instead of '%s')\n",
3974 dt2.Format().c_str(), dt.Format().c_str());
3975 }
3976 else
3977 {
3978 puts(" (ok)");
3979 }
3980 }
3981 }
3982 }
3983 }
3984
3985 // test text -> wxDateTime conversion
3986 static void TestTimeParse()
3987 {
3988 puts("\n*** wxDateTime parse test ***");
3989
3990 struct ParseTestData
3991 {
3992 const char *format;
3993 Date date;
3994 bool good;
3995 };
3996
3997 static const ParseTestData parseTestDates[] =
3998 {
3999 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE },
4000 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE },
4001 };
4002
4003 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
4004 {
4005 const char *format = parseTestDates[n].format;
4006
4007 printf("%s => ", format);
4008
4009 wxDateTime dt;
4010 if ( dt.ParseRfc822Date(format) )
4011 {
4012 printf("%s ", dt.Format().c_str());
4013
4014 if ( parseTestDates[n].good )
4015 {
4016 wxDateTime dtReal = parseTestDates[n].date.DT();
4017 if ( dt == dtReal )
4018 {
4019 puts("(ok)");
4020 }
4021 else
4022 {
4023 printf("(ERROR: should be %s)\n", dtReal.Format().c_str());
4024 }
4025 }
4026 else
4027 {
4028 puts("(ERROR: bad format)");
4029 }
4030 }
4031 else
4032 {
4033 printf("bad format (%s)\n",
4034 parseTestDates[n].good ? "ERROR" : "ok");
4035 }
4036 }
4037 }
4038
4039 static void TestDateTimeInteractive()
4040 {
4041 puts("\n*** interactive wxDateTime tests ***");
4042
4043 char buf[128];
4044
4045 for ( ;; )
4046 {
4047 printf("Enter a date: ");
4048 if ( !fgets(buf, WXSIZEOF(buf), stdin) )
4049 break;
4050
4051 // kill the last '\n'
4052 buf[strlen(buf) - 1] = 0;
4053
4054 wxDateTime dt;
4055 const char *p = dt.ParseDate(buf);
4056 if ( !p )
4057 {
4058 printf("ERROR: failed to parse the date '%s'.\n", buf);
4059
4060 continue;
4061 }
4062 else if ( *p )
4063 {
4064 printf("WARNING: parsed only first %u characters.\n", p - buf);
4065 }
4066
4067 printf("%s: day %u, week of month %u/%u, week of year %u\n",
4068 dt.Format("%b %d, %Y").c_str(),
4069 dt.GetDayOfYear(),
4070 dt.GetWeekOfMonth(wxDateTime::Monday_First),
4071 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
4072 dt.GetWeekOfYear(wxDateTime::Monday_First));
4073 }
4074
4075 puts("\n*** done ***");
4076 }
4077
4078 static void TestTimeMS()
4079 {
4080 puts("*** testing millisecond-resolution support in wxDateTime ***");
4081
4082 wxDateTime dt1 = wxDateTime::Now(),
4083 dt2 = wxDateTime::UNow();
4084
4085 printf("Now = %s\n", dt1.Format("%H:%M:%S:%l").c_str());
4086 printf("UNow = %s\n", dt2.Format("%H:%M:%S:%l").c_str());
4087 printf("Dummy loop: ");
4088 for ( int i = 0; i < 6000; i++ )
4089 {
4090 //for ( int j = 0; j < 10; j++ )
4091 {
4092 wxString s;
4093 s.Printf("%g", sqrt(i));
4094 }
4095
4096 if ( !(i % 100) )
4097 putchar('.');
4098 }
4099 puts(", done");
4100
4101 dt1 = dt2;
4102 dt2 = wxDateTime::UNow();
4103 printf("UNow = %s\n", dt2.Format("%H:%M:%S:%l").c_str());
4104
4105 printf("Loop executed in %s ms\n", (dt2 - dt1).Format("%l").c_str());
4106
4107 puts("\n*** done ***");
4108 }
4109
4110 static void TestTimeArithmetics()
4111 {
4112 puts("\n*** testing arithmetic operations on wxDateTime ***");
4113
4114 static const struct ArithmData
4115 {
4116 ArithmData(const wxDateSpan& sp, const char *nam)
4117 : span(sp), name(nam) { }
4118
4119 wxDateSpan span;
4120 const char *name;
4121 } testArithmData[] =
4122 {
4123 ArithmData(wxDateSpan::Day(), "day"),
4124 ArithmData(wxDateSpan::Week(), "week"),
4125 ArithmData(wxDateSpan::Month(), "month"),
4126 ArithmData(wxDateSpan::Year(), "year"),
4127 ArithmData(wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days"),
4128 };
4129
4130 wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2;
4131
4132 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
4133 {
4134 wxDateSpan span = testArithmData[n].span;
4135 dt1 = dt + span;
4136 dt2 = dt - span;
4137
4138 const char *name = testArithmData[n].name;
4139 printf("%s + %s = %s, %s - %s = %s\n",
4140 dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(),
4141 dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str());
4142
4143 printf("Going back: %s", (dt1 - span).FormatISODate().c_str());
4144 if ( dt1 - span == dt )
4145 {
4146 puts(" (ok)");
4147 }
4148 else
4149 {
4150 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
4151 }
4152
4153 printf("Going forward: %s", (dt2 + span).FormatISODate().c_str());
4154 if ( dt2 + span == dt )
4155 {
4156 puts(" (ok)");
4157 }
4158 else
4159 {
4160 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
4161 }
4162
4163 printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str());
4164 if ( dt2 + 2*span == dt1 )
4165 {
4166 puts(" (ok)");
4167 }
4168 else
4169 {
4170 printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str());
4171 }
4172
4173 puts("");
4174 }
4175 }
4176
4177 static void TestTimeHolidays()
4178 {
4179 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
4180
4181 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
4182 wxDateTime dtStart(1, tm.mon, tm.year),
4183 dtEnd = dtStart.GetLastMonthDay();
4184
4185 wxDateTimeArray hol;
4186 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
4187
4188 const wxChar *format = "%d-%b-%Y (%a)";
4189
4190 printf("All holidays between %s and %s:\n",
4191 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
4192
4193 size_t count = hol.GetCount();
4194 for ( size_t n = 0; n < count; n++ )
4195 {
4196 printf("\t%s\n", hol[n].Format(format).c_str());
4197 }
4198
4199 puts("");
4200 }
4201
4202 static void TestTimeZoneBug()
4203 {
4204 puts("\n*** testing for DST/timezone bug ***\n");
4205
4206 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
4207 for ( int i = 0; i < 31; i++ )
4208 {
4209 printf("Date %s: week day %s.\n",
4210 date.Format(_T("%d-%m-%Y")).c_str(),
4211 date.GetWeekDayName(date.GetWeekDay()).c_str());
4212
4213 date += wxDateSpan::Day();
4214 }
4215
4216 puts("");
4217 }
4218
4219 static void TestTimeSpanFormat()
4220 {
4221 puts("\n*** wxTimeSpan tests ***");
4222
4223 static const char *formats[] =
4224 {
4225 _T("(default) %H:%M:%S"),
4226 _T("%E weeks and %D days"),
4227 _T("%l milliseconds"),
4228 _T("(with ms) %H:%M:%S:%l"),
4229 _T("100%% of minutes is %M"), // test "%%"
4230 _T("%D days and %H hours"),
4231 _T("or also %S seconds"),
4232 };
4233
4234 wxTimeSpan ts1(1, 2, 3, 4),
4235 ts2(111, 222, 333);
4236 for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
4237 {
4238 printf("ts1 = %s\tts2 = %s\n",
4239 ts1.Format(formats[n]).c_str(),
4240 ts2.Format(formats[n]).c_str());
4241 }
4242
4243 puts("");
4244 }
4245
4246 #if 0
4247
4248 // test compatibility with the old wxDate/wxTime classes
4249 static void TestTimeCompatibility()
4250 {
4251 puts("\n*** wxDateTime compatibility test ***");
4252
4253 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
4254 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
4255
4256 double jdnNow = wxDateTime::Now().GetJDN();
4257 long jdnMidnight = (long)(jdnNow - 0.5);
4258 printf("wxDate for today: %s\n", wxDate(jdnMidnight).FormatDate().c_str());
4259
4260 jdnMidnight = wxDate().Set().GetJulianDate();
4261 printf("wxDateTime for today: %s\n",
4262 wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str());
4263
4264 int flags = wxEUROPEAN;//wxFULL;
4265 wxDate date;
4266 date.Set();
4267 printf("Today is %s\n", date.FormatDate(flags).c_str());
4268 for ( int n = 0; n < 7; n++ )
4269 {
4270 printf("Previous %s is %s\n",
4271 wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n),
4272 date.Previous(n + 1).FormatDate(flags).c_str());
4273 }
4274 }
4275
4276 #endif // 0
4277
4278 #endif // TEST_DATETIME
4279
4280 // ----------------------------------------------------------------------------
4281 // threads
4282 // ----------------------------------------------------------------------------
4283
4284 #ifdef TEST_THREADS
4285
4286 #include "wx/thread.h"
4287
4288 static size_t gs_counter = (size_t)-1;
4289 static wxCriticalSection gs_critsect;
4290 static wxCondition gs_cond;
4291
4292 class MyJoinableThread : public wxThread
4293 {
4294 public:
4295 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
4296 { m_n = n; Create(); }
4297
4298 // thread execution starts here
4299 virtual ExitCode Entry();
4300
4301 private:
4302 size_t m_n;
4303 };
4304
4305 wxThread::ExitCode MyJoinableThread::Entry()
4306 {
4307 unsigned long res = 1;
4308 for ( size_t n = 1; n < m_n; n++ )
4309 {
4310 res *= n;
4311
4312 // it's a loooong calculation :-)
4313 Sleep(100);
4314 }
4315
4316 return (ExitCode)res;
4317 }
4318
4319 class MyDetachedThread : public wxThread
4320 {
4321 public:
4322 MyDetachedThread(size_t n, char ch)
4323 {
4324 m_n = n;
4325 m_ch = ch;
4326 m_cancelled = FALSE;
4327
4328 Create();
4329 }
4330
4331 // thread execution starts here
4332 virtual ExitCode Entry();
4333
4334 // and stops here
4335 virtual void OnExit();
4336
4337 private:
4338 size_t m_n; // number of characters to write
4339 char m_ch; // character to write
4340
4341 bool m_cancelled; // FALSE if we exit normally
4342 };
4343
4344 wxThread::ExitCode MyDetachedThread::Entry()
4345 {
4346 {
4347 wxCriticalSectionLocker lock(gs_critsect);
4348 if ( gs_counter == (size_t)-1 )
4349 gs_counter = 1;
4350 else
4351 gs_counter++;
4352 }
4353
4354 for ( size_t n = 0; n < m_n; n++ )
4355 {
4356 if ( TestDestroy() )
4357 {
4358 m_cancelled = TRUE;
4359
4360 break;
4361 }
4362
4363 putchar(m_ch);
4364 fflush(stdout);
4365
4366 wxThread::Sleep(100);
4367 }
4368
4369 return 0;
4370 }
4371
4372 void MyDetachedThread::OnExit()
4373 {
4374 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
4375
4376 wxCriticalSectionLocker lock(gs_critsect);
4377 if ( !--gs_counter && !m_cancelled )
4378 gs_cond.Signal();
4379 }
4380
4381 void TestDetachedThreads()
4382 {
4383 puts("\n*** Testing detached threads ***");
4384
4385 static const size_t nThreads = 3;
4386 MyDetachedThread *threads[nThreads];
4387 size_t n;
4388 for ( n = 0; n < nThreads; n++ )
4389 {
4390 threads[n] = new MyDetachedThread(10, 'A' + n);
4391 }
4392
4393 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
4394 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
4395
4396 for ( n = 0; n < nThreads; n++ )
4397 {
4398 threads[n]->Run();
4399 }
4400
4401 // wait until all threads terminate
4402 gs_cond.Wait();
4403
4404 puts("");
4405 }
4406
4407 void TestJoinableThreads()
4408 {
4409 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
4410
4411 // calc 10! in the background
4412 MyJoinableThread thread(10);
4413 thread.Run();
4414
4415 printf("\nThread terminated with exit code %lu.\n",
4416 (unsigned long)thread.Wait());
4417 }
4418
4419 void TestThreadSuspend()
4420 {
4421 puts("\n*** Testing thread suspend/resume functions ***");
4422
4423 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
4424
4425 thread->Run();
4426
4427 // this is for this demo only, in a real life program we'd use another
4428 // condition variable which would be signaled from wxThread::Entry() to
4429 // tell us that the thread really started running - but here just wait a
4430 // bit and hope that it will be enough (the problem is, of course, that
4431 // the thread might still not run when we call Pause() which will result
4432 // in an error)
4433 wxThread::Sleep(300);
4434
4435 for ( size_t n = 0; n < 3; n++ )
4436 {
4437 thread->Pause();
4438
4439 puts("\nThread suspended");
4440 if ( n > 0 )
4441 {
4442 // don't sleep but resume immediately the first time
4443 wxThread::Sleep(300);
4444 }
4445 puts("Going to resume the thread");
4446
4447 thread->Resume();
4448 }
4449
4450 puts("Waiting until it terminates now");
4451
4452 // wait until the thread terminates
4453 gs_cond.Wait();
4454
4455 puts("");
4456 }
4457
4458 void TestThreadDelete()
4459 {
4460 // As above, using Sleep() is only for testing here - we must use some
4461 // synchronisation object instead to ensure that the thread is still
4462 // running when we delete it - deleting a detached thread which already
4463 // terminated will lead to a crash!
4464
4465 puts("\n*** Testing thread delete function ***");
4466
4467 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
4468
4469 thread0->Delete();
4470
4471 puts("\nDeleted a thread which didn't start to run yet.");
4472
4473 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
4474
4475 thread1->Run();
4476
4477 wxThread::Sleep(300);
4478
4479 thread1->Delete();
4480
4481 puts("\nDeleted a running thread.");
4482
4483 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
4484
4485 thread2->Run();
4486
4487 wxThread::Sleep(300);
4488
4489 thread2->Pause();
4490
4491 thread2->Delete();
4492
4493 puts("\nDeleted a sleeping thread.");
4494
4495 MyJoinableThread thread3(20);
4496 thread3.Run();
4497
4498 thread3.Delete();
4499
4500 puts("\nDeleted a joinable thread.");
4501
4502 MyJoinableThread thread4(2);
4503 thread4.Run();
4504
4505 wxThread::Sleep(300);
4506
4507 thread4.Delete();
4508
4509 puts("\nDeleted a joinable thread which already terminated.");
4510
4511 puts("");
4512 }
4513
4514 #endif // TEST_THREADS
4515
4516 // ----------------------------------------------------------------------------
4517 // arrays
4518 // ----------------------------------------------------------------------------
4519
4520 #ifdef TEST_ARRAYS
4521
4522 static void PrintArray(const char* name, const wxArrayString& array)
4523 {
4524 printf("Dump of the array '%s'\n", name);
4525
4526 size_t nCount = array.GetCount();
4527 for ( size_t n = 0; n < nCount; n++ )
4528 {
4529 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
4530 }
4531 }
4532
4533 static void PrintArray(const char* name, const wxArrayInt& array)
4534 {
4535 printf("Dump of the array '%s'\n", name);
4536
4537 size_t nCount = array.GetCount();
4538 for ( size_t n = 0; n < nCount; n++ )
4539 {
4540 printf("\t%s[%u] = %d\n", name, n, array[n]);
4541 }
4542 }
4543
4544 int wxCMPFUNC_CONV StringLenCompare(const wxString& first,
4545 const wxString& second)
4546 {
4547 return first.length() - second.length();
4548 }
4549
4550 int wxCMPFUNC_CONV IntCompare(int *first,
4551 int *second)
4552 {
4553 return *first - *second;
4554 }
4555
4556 int wxCMPFUNC_CONV IntRevCompare(int *first,
4557 int *second)
4558 {
4559 return *second - *first;
4560 }
4561
4562 static void TestArrayOfInts()
4563 {
4564 puts("*** Testing wxArrayInt ***\n");
4565
4566 wxArrayInt a;
4567 a.Add(1);
4568 a.Add(17);
4569 a.Add(5);
4570 a.Add(3);
4571
4572 puts("Initially:");
4573 PrintArray("a", a);
4574
4575 puts("After sort:");
4576 a.Sort(IntCompare);
4577 PrintArray("a", a);
4578
4579 puts("After reverse sort:");
4580 a.Sort(IntRevCompare);
4581 PrintArray("a", a);
4582 }
4583
4584 #include "wx/dynarray.h"
4585
4586 WX_DECLARE_OBJARRAY(Bar, ArrayBars);
4587 #include "wx/arrimpl.cpp"
4588 WX_DEFINE_OBJARRAY(ArrayBars);
4589
4590 static void TestArrayOfObjects()
4591 {
4592 puts("*** Testing wxObjArray ***\n");
4593
4594 {
4595 ArrayBars bars;
4596 Bar bar("second bar");
4597
4598 printf("Initially: %u objects in the array, %u objects total.\n",
4599 bars.GetCount(), Bar::GetNumber());
4600
4601 bars.Add(new Bar("first bar"));
4602 bars.Add(bar);
4603
4604 printf("Now: %u objects in the array, %u objects total.\n",
4605 bars.GetCount(), Bar::GetNumber());
4606
4607 bars.Empty();
4608
4609 printf("After Empty(): %u objects in the array, %u objects total.\n",
4610 bars.GetCount(), Bar::GetNumber());
4611 }
4612
4613 printf("Finally: no more objects in the array, %u objects total.\n",
4614 Bar::GetNumber());
4615 }
4616
4617 #endif // TEST_ARRAYS
4618
4619 // ----------------------------------------------------------------------------
4620 // strings
4621 // ----------------------------------------------------------------------------
4622
4623 #ifdef TEST_STRINGS
4624
4625 #include "wx/timer.h"
4626 #include "wx/tokenzr.h"
4627
4628 static void TestStringConstruction()
4629 {
4630 puts("*** Testing wxString constructores ***");
4631
4632 #define TEST_CTOR(args, res) \
4633 { \
4634 wxString s args ; \
4635 printf("wxString%s = %s ", #args, s.c_str()); \
4636 if ( s == res ) \
4637 { \
4638 puts("(ok)"); \
4639 } \
4640 else \
4641 { \
4642 printf("(ERROR: should be %s)\n", res); \
4643 } \
4644 }
4645
4646 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
4647 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
4648 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
4649 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
4650
4651 static const wxChar *s = _T("?really!");
4652 const wxChar *start = wxStrchr(s, _T('r'));
4653 const wxChar *end = wxStrchr(s, _T('!'));
4654 TEST_CTOR((start, end), _T("really"));
4655
4656 puts("");
4657 }
4658
4659 static void TestString()
4660 {
4661 wxStopWatch sw;
4662
4663 wxString a, b, c;
4664
4665 a.reserve (128);
4666 b.reserve (128);
4667 c.reserve (128);
4668
4669 for (int i = 0; i < 1000000; ++i)
4670 {
4671 a = "Hello";
4672 b = " world";
4673 c = "! How'ya doin'?";
4674 a += b;
4675 a += c;
4676 c = "Hello world! What's up?";
4677 if (c != a)
4678 c = "Doh!";
4679 }
4680
4681 printf ("TestString elapsed time: %ld\n", sw.Time());
4682 }
4683
4684 static void TestPChar()
4685 {
4686 wxStopWatch sw;
4687
4688 char a [128];
4689 char b [128];
4690 char c [128];
4691
4692 for (int i = 0; i < 1000000; ++i)
4693 {
4694 strcpy (a, "Hello");
4695 strcpy (b, " world");
4696 strcpy (c, "! How'ya doin'?");
4697 strcat (a, b);
4698 strcat (a, c);
4699 strcpy (c, "Hello world! What's up?");
4700 if (strcmp (c, a) == 0)
4701 strcpy (c, "Doh!");
4702 }
4703
4704 printf ("TestPChar elapsed time: %ld\n", sw.Time());
4705 }
4706
4707 static void TestStringSub()
4708 {
4709 wxString s("Hello, world!");
4710
4711 puts("*** Testing wxString substring extraction ***");
4712
4713 printf("String = '%s'\n", s.c_str());
4714 printf("Left(5) = '%s'\n", s.Left(5).c_str());
4715 printf("Right(6) = '%s'\n", s.Right(6).c_str());
4716 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
4717 printf("Mid(3) = '%s'\n", s.Mid(3).c_str());
4718 printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str());
4719 printf("substr(3) = '%s'\n", s.substr(3).c_str());
4720
4721 static const wxChar *prefixes[] =
4722 {
4723 _T("Hello"),
4724 _T("Hello, "),
4725 _T("Hello, world!"),
4726 _T("Hello, world!!!"),
4727 _T(""),
4728 _T("Goodbye"),
4729 _T("Hi"),
4730 };
4731
4732 for ( size_t n = 0; n < WXSIZEOF(prefixes); n++ )
4733 {
4734 wxString prefix = prefixes[n], rest;
4735 bool rc = s.StartsWith(prefix, &rest);
4736 printf("StartsWith('%s') = %s", prefix.c_str(), rc ? "TRUE" : "FALSE");
4737 if ( rc )
4738 {
4739 printf(" (the rest is '%s')\n", rest.c_str());
4740 }
4741 else
4742 {
4743 putchar('\n');
4744 }
4745 }
4746
4747 puts("");
4748 }
4749
4750 static void TestStringFormat()
4751 {
4752 puts("*** Testing wxString formatting ***");
4753
4754 wxString s;
4755 s.Printf("%03d", 18);
4756
4757 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
4758 printf("Number 18: %s\n", s.c_str());
4759
4760 puts("");
4761 }
4762
4763 // returns "not found" for npos, value for all others
4764 static wxString PosToString(size_t res)
4765 {
4766 wxString s = res == wxString::npos ? wxString(_T("not found"))
4767 : wxString::Format(_T("%u"), res);
4768 return s;
4769 }
4770
4771 static void TestStringFind()
4772 {
4773 puts("*** Testing wxString find() functions ***");
4774
4775 static const wxChar *strToFind = _T("ell");
4776 static const struct StringFindTest
4777 {
4778 const wxChar *str;
4779 size_t start,
4780 result; // of searching "ell" in str
4781 } findTestData[] =
4782 {
4783 { _T("Well, hello world"), 0, 1 },
4784 { _T("Well, hello world"), 6, 7 },
4785 { _T("Well, hello world"), 9, wxString::npos },
4786 };
4787
4788 for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
4789 {
4790 const StringFindTest& ft = findTestData[n];
4791 size_t res = wxString(ft.str).find(strToFind, ft.start);
4792
4793 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
4794 strToFind, ft.str, ft.start, PosToString(res).c_str());
4795
4796 size_t resTrue = ft.result;
4797 if ( res == resTrue )
4798 {
4799 puts(_T("(ok)"));
4800 }
4801 else
4802 {
4803 printf(_T("(ERROR: should be %s)\n"),
4804 PosToString(resTrue).c_str());
4805 }
4806 }
4807
4808 puts("");
4809 }
4810
4811 static void TestStringTokenizer()
4812 {
4813 puts("*** Testing wxStringTokenizer ***");
4814
4815 static const wxChar *modeNames[] =
4816 {
4817 _T("default"),
4818 _T("return empty"),
4819 _T("return all empty"),
4820 _T("with delims"),
4821 _T("like strtok"),
4822 };
4823
4824 static const struct StringTokenizerTest
4825 {
4826 const wxChar *str; // string to tokenize
4827 const wxChar *delims; // delimiters to use
4828 size_t count; // count of token
4829 wxStringTokenizerMode mode; // how should we tokenize it
4830 } tokenizerTestData[] =
4831 {
4832 { _T(""), _T(" "), 0 },
4833 { _T("Hello, world"), _T(" "), 2 },
4834 { _T("Hello, world "), _T(" "), 2 },
4835 { _T("Hello, world"), _T(","), 2 },
4836 { _T("Hello, world!"), _T(",!"), 2 },
4837 { _T("Hello,, world!"), _T(",!"), 3 },
4838 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL },
4839 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
4840 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 },
4841 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY },
4842 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL },
4843 { _T("01/02/99"), _T("/-"), 3 },
4844 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS },
4845 };
4846
4847 for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
4848 {
4849 const StringTokenizerTest& tt = tokenizerTestData[n];
4850 wxStringTokenizer tkz(tt.str, tt.delims, tt.mode);
4851
4852 size_t count = tkz.CountTokens();
4853 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
4854 MakePrintable(tt.str).c_str(),
4855 count,
4856 MakePrintable(tt.delims).c_str(),
4857 modeNames[tkz.GetMode()]);
4858 if ( count == tt.count )
4859 {
4860 puts(_T("(ok)"));
4861 }
4862 else
4863 {
4864 printf(_T("(ERROR: should be %u)\n"), tt.count);
4865
4866 continue;
4867 }
4868
4869 // if we emulate strtok(), check that we do it correctly
4870 wxChar *buf, *s = NULL, *last;
4871
4872 if ( tkz.GetMode() == wxTOKEN_STRTOK )
4873 {
4874 buf = new wxChar[wxStrlen(tt.str) + 1];
4875 wxStrcpy(buf, tt.str);
4876
4877 s = wxStrtok(buf, tt.delims, &last);
4878 }
4879 else
4880 {
4881 buf = NULL;
4882 }
4883
4884 // now show the tokens themselves
4885 size_t count2 = 0;
4886 while ( tkz.HasMoreTokens() )
4887 {
4888 wxString token = tkz.GetNextToken();
4889
4890 printf(_T("\ttoken %u: '%s'"),
4891 ++count2,
4892 MakePrintable(token).c_str());
4893
4894 if ( buf )
4895 {
4896 if ( token == s )
4897 {
4898 puts(" (ok)");
4899 }
4900 else
4901 {
4902 printf(" (ERROR: should be %s)\n", s);
4903 }
4904
4905 s = wxStrtok(NULL, tt.delims, &last);
4906 }
4907 else
4908 {
4909 // nothing to compare with
4910 puts("");
4911 }
4912 }
4913
4914 if ( count2 != count )
4915 {
4916 puts(_T("\tERROR: token count mismatch"));
4917 }
4918
4919 delete [] buf;
4920 }
4921
4922 puts("");
4923 }
4924
4925 static void TestStringReplace()
4926 {
4927 puts("*** Testing wxString::replace ***");
4928
4929 static const struct StringReplaceTestData
4930 {
4931 const wxChar *original; // original test string
4932 size_t start, len; // the part to replace
4933 const wxChar *replacement; // the replacement string
4934 const wxChar *result; // and the expected result
4935 } stringReplaceTestData[] =
4936 {
4937 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
4938 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
4939 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
4940 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
4941 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
4942 };
4943
4944 for ( size_t n = 0; n < WXSIZEOF(stringReplaceTestData); n++ )
4945 {
4946 const StringReplaceTestData data = stringReplaceTestData[n];
4947
4948 wxString original = data.original;
4949 original.replace(data.start, data.len, data.replacement);
4950
4951 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
4952 data.original, data.start, data.len, data.replacement,
4953 original.c_str());
4954
4955 if ( original == data.result )
4956 {
4957 puts("(ok)");
4958 }
4959 else
4960 {
4961 wxPrintf(_T("(ERROR: should be '%s')\n"), data.result);
4962 }
4963 }
4964
4965 puts("");
4966 }
4967
4968 static void TestStringMatch()
4969 {
4970 wxPuts(_T("*** Testing wxString::Matches() ***"));
4971
4972 static const struct StringMatchTestData
4973 {
4974 const wxChar *text;
4975 const wxChar *wildcard;
4976 bool matches;
4977 } stringMatchTestData[] =
4978 {
4979 { _T("foobar"), _T("foo*"), 1 },
4980 { _T("foobar"), _T("*oo*"), 1 },
4981 { _T("foobar"), _T("*bar"), 1 },
4982 { _T("foobar"), _T("??????"), 1 },
4983 { _T("foobar"), _T("f??b*"), 1 },
4984 { _T("foobar"), _T("f?b*"), 0 },
4985 { _T("foobar"), _T("*goo*"), 0 },
4986 { _T("foobar"), _T("*foo"), 0 },
4987 { _T("foobarfoo"), _T("*foo"), 1 },
4988 { _T(""), _T("*"), 1 },
4989 { _T(""), _T("?"), 0 },
4990 };
4991
4992 for ( size_t n = 0; n < WXSIZEOF(stringMatchTestData); n++ )
4993 {
4994 const StringMatchTestData& data = stringMatchTestData[n];
4995 bool matches = wxString(data.text).Matches(data.wildcard);
4996 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
4997 data.wildcard,
4998 matches ? _T("matches") : _T("doesn't match"),
4999 data.text,
5000 matches == data.matches ? _T("ok") : _T("ERROR"));
5001 }
5002
5003 wxPuts(_T(""));
5004 }
5005
5006 #endif // TEST_STRINGS
5007
5008 // ----------------------------------------------------------------------------
5009 // entry point
5010 // ----------------------------------------------------------------------------
5011
5012 int main(int argc, char **argv)
5013 {
5014 wxInitializer initializer;
5015 if ( !initializer )
5016 {
5017 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
5018
5019 return -1;
5020 }
5021
5022 #ifdef TEST_SNGLINST
5023 wxSingleInstanceChecker checker;
5024 if ( checker.Create(_T(".wxconsole.lock")) )
5025 {
5026 if ( checker.IsAnotherRunning() )
5027 {
5028 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
5029
5030 return 1;
5031 }
5032
5033 // wait some time to give time to launch another instance
5034 wxPrintf(_T("Press \"Enter\" to continue..."));
5035 wxFgetc(stdin);
5036 }
5037 else // failed to create
5038 {
5039 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
5040 }
5041 #endif // TEST_SNGLINST
5042
5043 #ifdef TEST_CHARSET
5044 TestCharset();
5045 #endif // TEST_CHARSET
5046
5047 #ifdef TEST_CMDLINE
5048 TestCmdLineConvert();
5049
5050 #if wxUSE_CMDLINE_PARSER
5051 static const wxCmdLineEntryDesc cmdLineDesc[] =
5052 {
5053 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), "show this help message",
5054 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
5055 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
5056 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
5057
5058 { wxCMD_LINE_OPTION, "o", "output", "output file" },
5059 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
5060 { wxCMD_LINE_OPTION, "s", "size", "output block size",
5061 wxCMD_LINE_VAL_NUMBER },
5062 { wxCMD_LINE_OPTION, "d", "date", "output file date",
5063 wxCMD_LINE_VAL_DATE },
5064
5065 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
5066 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
5067
5068 { wxCMD_LINE_NONE }
5069 };
5070
5071 wxCmdLineParser parser(cmdLineDesc, argc, argv);
5072
5073 parser.AddOption("project_name", "", "full path to project file",
5074 wxCMD_LINE_VAL_STRING,
5075 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
5076
5077 switch ( parser.Parse() )
5078 {
5079 case -1:
5080 wxLogMessage("Help was given, terminating.");
5081 break;
5082
5083 case 0:
5084 ShowCmdLine(parser);
5085 break;
5086
5087 default:
5088 wxLogMessage("Syntax error detected, aborting.");
5089 break;
5090 }
5091 #endif // wxUSE_CMDLINE_PARSER
5092
5093 #endif // TEST_CMDLINE
5094
5095 #ifdef TEST_STRINGS
5096 if ( 0 )
5097 {
5098 TestPChar();
5099 TestString();
5100 TestStringSub();
5101 TestStringConstruction();
5102 TestStringFormat();
5103 TestStringFind();
5104 TestStringTokenizer();
5105 TestStringReplace();
5106 }
5107 TestStringMatch();
5108 #endif // TEST_STRINGS
5109
5110 #ifdef TEST_ARRAYS
5111 if ( 0 )
5112 {
5113 wxArrayString a1;
5114 a1.Add("tiger");
5115 a1.Add("cat");
5116 a1.Add("lion");
5117 a1.Add("dog");
5118 a1.Add("human");
5119 a1.Add("ape");
5120
5121 puts("*** Initially:");
5122
5123 PrintArray("a1", a1);
5124
5125 wxArrayString a2(a1);
5126 PrintArray("a2", a2);
5127
5128 wxSortedArrayString a3(a1);
5129 PrintArray("a3", a3);
5130
5131 puts("*** After deleting a string from a1");
5132 a1.Remove(2);
5133
5134 PrintArray("a1", a1);
5135 PrintArray("a2", a2);
5136 PrintArray("a3", a3);
5137
5138 puts("*** After reassigning a1 to a2 and a3");
5139 a3 = a2 = a1;
5140 PrintArray("a2", a2);
5141 PrintArray("a3", a3);
5142
5143 puts("*** After sorting a1");
5144 a1.Sort();
5145 PrintArray("a1", a1);
5146
5147 puts("*** After sorting a1 in reverse order");
5148 a1.Sort(TRUE);
5149 PrintArray("a1", a1);
5150
5151 puts("*** After sorting a1 by the string length");
5152 a1.Sort(StringLenCompare);
5153 PrintArray("a1", a1);
5154
5155 TestArrayOfObjects();
5156 }
5157 TestArrayOfInts();
5158 #endif // TEST_ARRAYS
5159
5160 #ifdef TEST_DIR
5161 if ( 0 )
5162 TestDirEnum();
5163 TestDirTraverse();
5164 #endif // TEST_DIR
5165
5166 #ifdef TEST_DLLLOADER
5167 TestDllLoad();
5168 #endif // TEST_DLLLOADER
5169
5170 #ifdef TEST_ENVIRON
5171 TestEnvironment();
5172 #endif // TEST_ENVIRON
5173
5174 #ifdef TEST_EXECUTE
5175 TestExecute();
5176 #endif // TEST_EXECUTE
5177
5178 #ifdef TEST_FILECONF
5179 TestFileConfRead();
5180 #endif // TEST_FILECONF
5181
5182 #ifdef TEST_LIST
5183 TestListCtor();
5184 #endif // TEST_LIST
5185
5186 #ifdef TEST_LOCALE
5187 TestDefaultLang();
5188 #endif // TEST_LOCALE
5189
5190 #ifdef TEST_LOG
5191 wxString s;
5192 for ( size_t n = 0; n < 8000; n++ )
5193 {
5194 s << (char)('A' + (n % 26));
5195 }
5196
5197 wxString msg;
5198 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
5199
5200 // this one shouldn't be truncated
5201 printf(msg);
5202
5203 // but this one will because log functions use fixed size buffer
5204 // (note that it doesn't need '\n' at the end neither - will be added
5205 // by wxLog anyhow)
5206 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
5207 #endif // TEST_LOG
5208
5209 #ifdef TEST_FILE
5210 if ( 0 )
5211 {
5212 TestFileRead();
5213 TestTextFileRead();
5214 }
5215 TestFileCopy();
5216 #endif // TEST_FILE
5217
5218 #ifdef TEST_FILENAME
5219 TestFileNameTemp();
5220 if ( 0 )
5221 {
5222 TestFileNameConstruction();
5223 TestFileNameSplit();
5224 TestFileNameCwd();
5225 TestFileNameComparison();
5226 TestFileNameOperations();
5227 }
5228 #endif // TEST_FILENAME
5229
5230 #ifdef TEST_FILETIME
5231 TestFileGetTimes();
5232 TestFileSetTimes();
5233 #endif // TEST_FILETIME
5234
5235 #ifdef TEST_FTP
5236 wxLog::AddTraceMask(FTP_TRACE_MASK);
5237 if ( TestFtpConnect() )
5238 {
5239 TestFtpFileSize();
5240 if ( 0 )
5241 {
5242 TestFtpList();
5243 TestFtpDownload();
5244 TestFtpMisc();
5245 TestFtpUpload();
5246 }
5247 if ( 0 )
5248 TestFtpInteractive();
5249 }
5250 //else: connecting to the FTP server failed
5251
5252 if ( 0 )
5253 TestFtpWuFtpd();
5254 #endif // TEST_FTP
5255
5256 #ifdef TEST_THREADS
5257 int nCPUs = wxThread::GetCPUCount();
5258 printf("This system has %d CPUs\n", nCPUs);
5259 if ( nCPUs != -1 )
5260 wxThread::SetConcurrency(nCPUs);
5261
5262 if ( argc > 1 && argv[1][0] == 't' )
5263 wxLog::AddTraceMask("thread");
5264
5265 if ( 1 )
5266 TestDetachedThreads();
5267 if ( 1 )
5268 TestJoinableThreads();
5269 if ( 1 )
5270 TestThreadSuspend();
5271 if ( 1 )
5272 TestThreadDelete();
5273
5274 #endif // TEST_THREADS
5275
5276 #ifdef TEST_LONGLONG
5277 // seed pseudo random generator
5278 srand((unsigned)time(NULL));
5279
5280 if ( 0 )
5281 {
5282 TestSpeed();
5283 }
5284 if ( 0 )
5285 {
5286 TestMultiplication();
5287 TestDivision();
5288 TestAddition();
5289 TestLongLongConversion();
5290 TestBitOperations();
5291 TestLongLongComparison();
5292 }
5293 TestLongLongPrint();
5294 #endif // TEST_LONGLONG
5295
5296 #ifdef TEST_HASH
5297 TestHash();
5298 #endif // TEST_HASH
5299
5300 #ifdef TEST_MIME
5301 wxLog::AddTraceMask(_T("mime"));
5302 if ( 1 )
5303 {
5304 TestMimeEnum();
5305 TestMimeOverride();
5306 TestMimeFilename();
5307 }
5308 else
5309 TestMimeAssociate();
5310 #endif // TEST_MIME
5311
5312 #ifdef TEST_INFO_FUNCTIONS
5313 TestDiskInfo();
5314 if ( 0 )
5315 {
5316 TestOsInfo();
5317 TestUserInfo();
5318 }
5319 #endif // TEST_INFO_FUNCTIONS
5320
5321 #ifdef TEST_PATHLIST
5322 TestPathList();
5323 #endif // TEST_PATHLIST
5324
5325 #ifdef TEST_REGCONF
5326 TestRegConfWrite();
5327 #endif // TEST_REGCONF
5328
5329 #ifdef TEST_REGEX
5330 // TODO: write a real test using src/regex/tests file
5331 if ( 0 )
5332 {
5333 TestRegExCompile();
5334 TestRegExMatch();
5335 TestRegExSubmatch();
5336 TestRegExInteractive();
5337 }
5338 TestRegExReplacement();
5339 #endif // TEST_REGEX
5340
5341 #ifdef TEST_REGISTRY
5342 if ( 0 )
5343 TestRegistryRead();
5344 TestRegistryAssociation();
5345 #endif // TEST_REGISTRY
5346
5347 #ifdef TEST_SOCKETS
5348 if ( 0 )
5349 {
5350 TestSocketServer();
5351 }
5352 TestSocketClient();
5353 #endif // TEST_SOCKETS
5354
5355 #ifdef TEST_STREAMS
5356 if ( 0 )
5357 TestFileStream();
5358 TestMemoryStream();
5359 #endif // TEST_STREAMS
5360
5361 #ifdef TEST_TIMER
5362 TestStopWatch();
5363 #endif // TEST_TIMER
5364
5365 #ifdef TEST_DATETIME
5366 if ( 0 )
5367 {
5368 TestTimeSet();
5369 TestTimeStatic();
5370 TestTimeRange();
5371 TestTimeZones();
5372 TestTimeTicks();
5373 TestTimeJDN();
5374 TestTimeDST();
5375 TestTimeWDays();
5376 TestTimeWNumber();
5377 TestTimeParse();
5378 TestTimeArithmetics();
5379 TestTimeHolidays();
5380 TestTimeFormat();
5381 TestTimeMS();
5382
5383 TestTimeZoneBug();
5384 }
5385 TestTimeSpanFormat();
5386 if ( 0 )
5387 TestDateTimeInteractive();
5388 #endif // TEST_DATETIME
5389
5390 #ifdef TEST_USLEEP
5391 puts("Sleeping for 3 seconds... z-z-z-z-z...");
5392 wxUsleep(3000);
5393 #endif // TEST_USLEEP
5394
5395 #ifdef TEST_VCARD
5396 if ( 0 )
5397 TestVCardRead();
5398 TestVCardWrite();
5399 #endif // TEST_VCARD
5400
5401 #ifdef TEST_WCHAR
5402 TestUtf8();
5403 #endif // TEST_WCHAR
5404
5405 #ifdef TEST_ZIP
5406 if ( 0 )
5407 TestZipStreamRead();
5408 TestZipFileSystem();
5409 #endif // TEST_ZIP
5410
5411 #ifdef TEST_ZLIB
5412 if ( 0 )
5413 TestZlibStreamWrite();
5414 TestZlibStreamRead();
5415 #endif // TEST_ZLIB
5416
5417 return 0;
5418 }
5419