]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
typo fixed
[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 #include <stdio.h>
23
24 #include "wx/string.h"
25 #include "wx/file.h"
26 #include "wx/app.h"
27 #include "wx/log.h"
28
29 // without this pragma, the stupid compiler precompiles #defines below so that
30 // changing them doesn't "take place" later!
31 #ifdef __VISUALC__
32 #pragma hdrstop
33 #endif
34
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
38
39 /*
40 A note about all these conditional compilation macros: this file is used
41 both as a test suite for various non-GUI wxWindows classes and as a
42 scratchpad for quick tests. So there are two compilation modes: if you
43 define TEST_ALL all tests are run, otherwise you may enable the individual
44 tests individually in the "#else" branch below.
45 */
46
47 // what to test (in alphabetic order)? uncomment the line below to do all tests
48 //#define TEST_ALL
49 #ifdef TEST_ALL
50 #define TEST_ARRAYS
51 #define TEST_CHARSET
52 #define TEST_CMDLINE
53 #define TEST_DATETIME
54 #define TEST_DIR
55 #define TEST_DLLLOADER
56 #define TEST_ENVIRON
57 #define TEST_EXECUTE
58 #define TEST_FILE
59 #define TEST_FILECONF
60 #define TEST_FILENAME
61 #define TEST_FILETIME
62 // #define TEST_FTP --FIXME! (RN)
63 #define TEST_HASH
64 #define TEST_HASHMAP
65 #define TEST_HASHSET
66 #define TEST_INFO_FUNCTIONS
67 #define TEST_LIST
68 #define TEST_LOCALE
69 #define TEST_LOG
70 #define TEST_LONGLONG
71 #define TEST_MIME
72 #define TEST_PATHLIST
73 #define TEST_ODBC
74 #define TEST_PRINTF
75 #define TEST_REGCONF
76 #define TEST_REGEX
77 #define TEST_REGISTRY
78 #define TEST_SCOPEGUARD
79 #define TEST_SNGLINST
80 // #define TEST_SOCKETS --FIXME! (RN)
81 #define TEST_STREAMS
82 #define TEST_STRINGS
83 #define TEST_TEXTSTREAM
84 #define TEST_THREADS
85 #define TEST_TIMER
86 #define TEST_UNICODE
87 // #define TEST_VCARD -- don't enable this (VZ)
88 // #define TEST_VOLUME --FIXME! (RN)
89 #define TEST_WCHAR
90 #define TEST_ZIP
91 #define TEST_ZLIB
92 #define TEST_GZIP
93
94 #undef TEST_ALL
95 static const bool TEST_ALL = true;
96 #else
97 #define TEST_FILENAME
98
99 static const bool TEST_ALL = false;
100 #endif
101
102 // some tests are interactive, define this to run them
103 #ifdef TEST_INTERACTIVE
104 #undef TEST_INTERACTIVE
105
106 static const bool TEST_INTERACTIVE = true;
107 #else
108 static const bool TEST_INTERACTIVE = false;
109 #endif
110
111 // ----------------------------------------------------------------------------
112 // test class for container objects
113 // ----------------------------------------------------------------------------
114
115 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
116
117 class Bar // Foo is already taken in the hash test
118 {
119 public:
120 Bar(const wxString& name) : m_name(name) { ms_bars++; }
121 Bar(const Bar& bar) : m_name(bar.m_name) { ms_bars++; }
122 ~Bar() { ms_bars--; }
123
124 static size_t GetNumber() { return ms_bars; }
125
126 const wxChar *GetName() const { return m_name; }
127
128 private:
129 wxString m_name;
130
131 static size_t ms_bars;
132 };
133
134 size_t Bar::ms_bars = 0;
135
136 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
137
138 // ============================================================================
139 // implementation
140 // ============================================================================
141
142 // ----------------------------------------------------------------------------
143 // helper functions
144 // ----------------------------------------------------------------------------
145
146 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
147
148 // replace TABs with \t and CRs with \n
149 static wxString MakePrintable(const wxChar *s)
150 {
151 wxString str(s);
152 (void)str.Replace(_T("\t"), _T("\\t"));
153 (void)str.Replace(_T("\n"), _T("\\n"));
154 (void)str.Replace(_T("\r"), _T("\\r"));
155
156 return str;
157 }
158
159 #endif // MakePrintable() is used
160
161 // ----------------------------------------------------------------------------
162 // wxFontMapper::CharsetToEncoding
163 // ----------------------------------------------------------------------------
164
165 #ifdef TEST_CHARSET
166
167 #include "wx/fontmap.h"
168
169 static void TestCharset()
170 {
171 static const wxChar *charsets[] =
172 {
173 // some vali charsets
174 _T("us-ascii "),
175 _T("iso8859-1 "),
176 _T("iso-8859-12 "),
177 _T("koi8-r "),
178 _T("utf-7 "),
179 _T("cp1250 "),
180 _T("windows-1252"),
181
182 // and now some bogus ones
183 _T(" "),
184 _T("cp1249 "),
185 _T("iso--8859-1 "),
186 _T("iso-8859-19 "),
187 };
188
189 for ( size_t n = 0; n < WXSIZEOF(charsets); n++ )
190 {
191 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charsets[n]);
192 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
193 charsets[n],
194 wxFontMapper::Get()->GetEncodingName(enc).c_str(),
195 wxFontMapper::Get()->GetEncodingDescription(enc).c_str());
196 }
197 }
198
199 #endif // TEST_CHARSET
200
201 // ----------------------------------------------------------------------------
202 // wxCmdLineParser
203 // ----------------------------------------------------------------------------
204
205 #ifdef TEST_CMDLINE
206
207 #include "wx/cmdline.h"
208 #include "wx/datetime.h"
209
210 #if wxUSE_CMDLINE_PARSER
211
212 static void ShowCmdLine(const wxCmdLineParser& parser)
213 {
214 wxString s = _T("Input files: ");
215
216 size_t count = parser.GetParamCount();
217 for ( size_t param = 0; param < count; param++ )
218 {
219 s << parser.GetParam(param) << ' ';
220 }
221
222 s << '\n'
223 << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
224 << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
225
226 wxString strVal;
227 long lVal;
228 wxDateTime dt;
229 if ( parser.Found(_T("o"), &strVal) )
230 s << _T("Output file:\t") << strVal << '\n';
231 if ( parser.Found(_T("i"), &strVal) )
232 s << _T("Input dir:\t") << strVal << '\n';
233 if ( parser.Found(_T("s"), &lVal) )
234 s << _T("Size:\t") << lVal << '\n';
235 if ( parser.Found(_T("d"), &dt) )
236 s << _T("Date:\t") << dt.FormatISODate() << '\n';
237 if ( parser.Found(_T("project_name"), &strVal) )
238 s << _T("Project:\t") << strVal << '\n';
239
240 wxLogMessage(s);
241 }
242
243 #endif // wxUSE_CMDLINE_PARSER
244
245 static void TestCmdLineConvert()
246 {
247 static const wxChar *cmdlines[] =
248 {
249 _T("arg1 arg2"),
250 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
251 _T("literal \\\" and \"\""),
252 };
253
254 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
255 {
256 const wxChar *cmdline = cmdlines[n];
257 wxPrintf(_T("Parsing: %s\n"), cmdline);
258 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
259
260 size_t count = args.GetCount();
261 wxPrintf(_T("\targc = %u\n"), count);
262 for ( size_t arg = 0; arg < count; arg++ )
263 {
264 wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str());
265 }
266 }
267 }
268
269 #endif // TEST_CMDLINE
270
271 // ----------------------------------------------------------------------------
272 // wxDir
273 // ----------------------------------------------------------------------------
274
275 #ifdef TEST_DIR
276
277 #include "wx/dir.h"
278
279 #ifdef __UNIX__
280 static const wxChar *ROOTDIR = _T("/");
281 static const wxChar *TESTDIR = _T("/usr/local/share");
282 #elif defined(__WXMSW__)
283 static const wxChar *ROOTDIR = _T("c:\\");
284 static const wxChar *TESTDIR = _T("d:\\");
285 #else
286 #error "don't know where the root directory is"
287 #endif
288
289 static void TestDirEnumHelper(wxDir& dir,
290 int flags = wxDIR_DEFAULT,
291 const wxString& filespec = wxEmptyString)
292 {
293 wxString filename;
294
295 if ( !dir.IsOpened() )
296 return;
297
298 bool cont = dir.GetFirst(&filename, filespec, flags);
299 while ( cont )
300 {
301 wxPrintf(_T("\t%s\n"), filename.c_str());
302
303 cont = dir.GetNext(&filename);
304 }
305
306 wxPuts(_T(""));
307 }
308
309 static void TestDirEnum()
310 {
311 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
312
313 wxString cwd = wxGetCwd();
314 if ( !wxDir::Exists(cwd) )
315 {
316 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
317 return;
318 }
319
320 wxDir dir(cwd);
321 if ( !dir.IsOpened() )
322 {
323 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
324 return;
325 }
326
327 wxPuts(_T("Enumerating everything in current directory:"));
328 TestDirEnumHelper(dir);
329
330 wxPuts(_T("Enumerating really everything in current directory:"));
331 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
332
333 wxPuts(_T("Enumerating object files in current directory:"));
334 TestDirEnumHelper(dir, wxDIR_DEFAULT, _T("*.o*"));
335
336 wxPuts(_T("Enumerating directories in current directory:"));
337 TestDirEnumHelper(dir, wxDIR_DIRS);
338
339 wxPuts(_T("Enumerating files in current directory:"));
340 TestDirEnumHelper(dir, wxDIR_FILES);
341
342 wxPuts(_T("Enumerating files including hidden in current directory:"));
343 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
344
345 dir.Open(ROOTDIR);
346
347 wxPuts(_T("Enumerating everything in root directory:"));
348 TestDirEnumHelper(dir, wxDIR_DEFAULT);
349
350 wxPuts(_T("Enumerating directories in root directory:"));
351 TestDirEnumHelper(dir, wxDIR_DIRS);
352
353 wxPuts(_T("Enumerating files in root directory:"));
354 TestDirEnumHelper(dir, wxDIR_FILES);
355
356 wxPuts(_T("Enumerating files including hidden in root directory:"));
357 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
358
359 wxPuts(_T("Enumerating files in non existing directory:"));
360 wxDir dirNo(_T("nosuchdir"));
361 TestDirEnumHelper(dirNo);
362 }
363
364 class DirPrintTraverser : public wxDirTraverser
365 {
366 public:
367 virtual wxDirTraverseResult OnFile(const wxString& filename)
368 {
369 return wxDIR_CONTINUE;
370 }
371
372 virtual wxDirTraverseResult OnDir(const wxString& dirname)
373 {
374 wxString path, name, ext;
375 wxSplitPath(dirname, &path, &name, &ext);
376
377 if ( !ext.empty() )
378 name << _T('.') << ext;
379
380 wxString indent;
381 for ( const wxChar *p = path.c_str(); *p; p++ )
382 {
383 if ( wxIsPathSeparator(*p) )
384 indent += _T(" ");
385 }
386
387 wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str());
388
389 return wxDIR_CONTINUE;
390 }
391 };
392
393 static void TestDirTraverse()
394 {
395 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
396
397 // enum all files
398 wxArrayString files;
399 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
400 wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR);
401 if ( n > 1 )
402 {
403 wxPrintf(_T("First one is '%s'\n"), files[0u].c_str());
404 wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str());
405 }
406
407 // enum again with custom traverser
408 wxPuts(_T("Now enumerating directories:"));
409 wxDir dir(TESTDIR);
410 DirPrintTraverser traverser;
411 dir.Traverse(traverser, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
412 }
413
414 static void TestDirExists()
415 {
416 wxPuts(_T("*** Testing wxDir::Exists() ***"));
417
418 static const wxChar *dirnames[] =
419 {
420 _T("."),
421 #if defined(__WXMSW__)
422 _T("c:"),
423 _T("c:\\"),
424 _T("\\\\share\\file"),
425 _T("c:\\dos"),
426 _T("c:\\dos\\"),
427 _T("c:\\dos\\\\"),
428 _T("c:\\autoexec.bat"),
429 #elif defined(__UNIX__)
430 _T("/"),
431 _T("//"),
432 _T("/usr/bin"),
433 _T("/usr//bin"),
434 _T("/usr///bin"),
435 #endif
436 };
437
438 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
439 {
440 wxPrintf(_T("%-40s: %s\n"),
441 dirnames[n],
442 wxDir::Exists(dirnames[n]) ? _T("exists")
443 : _T("doesn't exist"));
444 }
445 }
446
447 #endif // TEST_DIR
448
449 // ----------------------------------------------------------------------------
450 // wxDllLoader
451 // ----------------------------------------------------------------------------
452
453 #ifdef TEST_DLLLOADER
454
455 #include "wx/dynlib.h"
456
457 static void TestDllLoad()
458 {
459 #if defined(__WXMSW__)
460 static const wxChar *LIB_NAME = _T("kernel32.dll");
461 static const wxChar *FUNC_NAME = _T("lstrlenA");
462 #elif defined(__UNIX__)
463 // weird: using just libc.so does *not* work!
464 static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so");
465 static const wxChar *FUNC_NAME = _T("strlen");
466 #else
467 #error "don't know how to test wxDllLoader on this platform"
468 #endif
469
470 wxPuts(_T("*** testing wxDllLoader ***\n"));
471
472 wxDynamicLibrary lib(LIB_NAME);
473 if ( !lib.IsLoaded() )
474 {
475 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
476 }
477 else
478 {
479 typedef int (*wxStrlenType)(const char *);
480 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
481 if ( !pfnStrlen )
482 {
483 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
484 FUNC_NAME, LIB_NAME);
485 }
486 else
487 {
488 if ( pfnStrlen("foo") != 3 )
489 {
490 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
491 }
492 else
493 {
494 wxPuts(_T("... ok"));
495 }
496 }
497 }
498 }
499
500 #endif // TEST_DLLLOADER
501
502 // ----------------------------------------------------------------------------
503 // wxGet/SetEnv
504 // ----------------------------------------------------------------------------
505
506 #ifdef TEST_ENVIRON
507
508 #include "wx/utils.h"
509
510 static wxString MyGetEnv(const wxString& var)
511 {
512 wxString val;
513 if ( !wxGetEnv(var, &val) )
514 val = _T("<empty>");
515 else
516 val = wxString(_T('\'')) + val + _T('\'');
517
518 return val;
519 }
520
521 static void TestEnvironment()
522 {
523 const wxChar *var = _T("wxTestVar");
524
525 wxPuts(_T("*** testing environment access functions ***"));
526
527 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
528 wxSetEnv(var, _T("value for wxTestVar"));
529 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
530 wxSetEnv(var, _T("another value"));
531 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
532 wxUnsetEnv(var);
533 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
534 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
535 }
536
537 #endif // TEST_ENVIRON
538
539 // ----------------------------------------------------------------------------
540 // wxExecute
541 // ----------------------------------------------------------------------------
542
543 #ifdef TEST_EXECUTE
544
545 #include "wx/utils.h"
546
547 static void TestExecute()
548 {
549 wxPuts(_T("*** testing wxExecute ***"));
550
551 #ifdef __UNIX__
552 #define COMMAND "cat -n ../../Makefile" // "echo hi"
553 #define SHELL_COMMAND "echo hi from shell"
554 #define REDIRECT_COMMAND COMMAND // "date"
555 #elif defined(__WXMSW__)
556 #define COMMAND "command.com /c echo hi"
557 #define SHELL_COMMAND "echo hi"
558 #define REDIRECT_COMMAND COMMAND
559 #else
560 #error "no command to exec"
561 #endif // OS
562
563 wxPrintf(_T("Testing wxShell: "));
564 fflush(stdout);
565 if ( wxShell(_T(SHELL_COMMAND)) )
566 wxPuts(_T("Ok."));
567 else
568 wxPuts(_T("ERROR."));
569
570 wxPrintf(_T("Testing wxExecute: "));
571 fflush(stdout);
572 if ( wxExecute(_T(COMMAND), true /* sync */) == 0 )
573 wxPuts(_T("Ok."));
574 else
575 wxPuts(_T("ERROR."));
576
577 #if 0 // no, it doesn't work (yet?)
578 wxPrintf(_T("Testing async wxExecute: "));
579 fflush(stdout);
580 if ( wxExecute(COMMAND) != 0 )
581 wxPuts(_T("Ok (command launched)."));
582 else
583 wxPuts(_T("ERROR."));
584 #endif // 0
585
586 wxPrintf(_T("Testing wxExecute with redirection:\n"));
587 wxArrayString output;
588 if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 )
589 {
590 wxPuts(_T("ERROR."));
591 }
592 else
593 {
594 size_t count = output.GetCount();
595 for ( size_t n = 0; n < count; n++ )
596 {
597 wxPrintf(_T("\t%s\n"), output[n].c_str());
598 }
599
600 wxPuts(_T("Ok."));
601 }
602 }
603
604 #endif // TEST_EXECUTE
605
606 // ----------------------------------------------------------------------------
607 // file
608 // ----------------------------------------------------------------------------
609
610 #ifdef TEST_FILE
611
612 #include "wx/file.h"
613 #include "wx/ffile.h"
614 #include "wx/textfile.h"
615
616 static void TestFileRead()
617 {
618 wxPuts(_T("*** wxFile read test ***"));
619
620 wxFile file(_T("testdata.fc"));
621 if ( file.IsOpened() )
622 {
623 wxPrintf(_T("File length: %lu\n"), file.Length());
624
625 wxPuts(_T("File dump:\n----------"));
626
627 static const off_t len = 1024;
628 wxChar buf[len];
629 for ( ;; )
630 {
631 off_t nRead = file.Read(buf, len);
632 if ( nRead == wxInvalidOffset )
633 {
634 wxPrintf(_T("Failed to read the file."));
635 break;
636 }
637
638 fwrite(buf, nRead, 1, stdout);
639
640 if ( nRead < len )
641 break;
642 }
643
644 wxPuts(_T("----------"));
645 }
646 else
647 {
648 wxPrintf(_T("ERROR: can't open test file.\n"));
649 }
650
651 wxPuts(_T(""));
652 }
653
654 static void TestTextFileRead()
655 {
656 wxPuts(_T("*** wxTextFile read test ***"));
657
658 wxTextFile file(_T("testdata.fc"));
659 if ( file.Open() )
660 {
661 wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount());
662 wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str());
663
664 wxString s;
665
666 wxPuts(_T("\nDumping the entire file:"));
667 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
668 {
669 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
670 }
671 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
672
673 wxPuts(_T("\nAnd now backwards:"));
674 for ( s = file.GetLastLine();
675 file.GetCurrentLine() != 0;
676 s = file.GetPrevLine() )
677 {
678 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
679 }
680 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
681 }
682 else
683 {
684 wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName());
685 }
686
687 wxPuts(_T(""));
688 }
689
690 static void TestFileCopy()
691 {
692 wxPuts(_T("*** Testing wxCopyFile ***"));
693
694 static const wxChar *filename1 = _T("testdata.fc");
695 static const wxChar *filename2 = _T("test2");
696 if ( !wxCopyFile(filename1, filename2) )
697 {
698 wxPuts(_T("ERROR: failed to copy file"));
699 }
700 else
701 {
702 wxFFile f1(filename1, _T("rb")),
703 f2(filename2, _T("rb"));
704
705 if ( !f1.IsOpened() || !f2.IsOpened() )
706 {
707 wxPuts(_T("ERROR: failed to open file(s)"));
708 }
709 else
710 {
711 wxString s1, s2;
712 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
713 {
714 wxPuts(_T("ERROR: failed to read file(s)"));
715 }
716 else
717 {
718 if ( (s1.length() != s2.length()) ||
719 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
720 {
721 wxPuts(_T("ERROR: copy error!"));
722 }
723 else
724 {
725 wxPuts(_T("File was copied ok."));
726 }
727 }
728 }
729 }
730
731 if ( !wxRemoveFile(filename2) )
732 {
733 wxPuts(_T("ERROR: failed to remove the file"));
734 }
735
736 wxPuts(_T(""));
737 }
738
739 #endif // TEST_FILE
740
741 // ----------------------------------------------------------------------------
742 // wxFileConfig
743 // ----------------------------------------------------------------------------
744
745 #ifdef TEST_FILECONF
746
747 #include "wx/confbase.h"
748 #include "wx/fileconf.h"
749
750 static const struct FileConfTestData
751 {
752 const wxChar *name; // value name
753 const wxChar *value; // the value from the file
754 } fcTestData[] =
755 {
756 { _T("value1"), _T("one") },
757 { _T("value2"), _T("two") },
758 { _T("novalue"), _T("default") },
759 };
760
761 static void TestFileConfRead()
762 {
763 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
764
765 wxFileConfig fileconf(_T("test"), wxEmptyString,
766 _T("testdata.fc"), wxEmptyString,
767 wxCONFIG_USE_RELATIVE_PATH);
768
769 // test simple reading
770 wxPuts(_T("\nReading config file:"));
771 wxString defValue(_T("default")), value;
772 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
773 {
774 const FileConfTestData& data = fcTestData[n];
775 value = fileconf.Read(data.name, defValue);
776 wxPrintf(_T("\t%s = %s "), data.name, value.c_str());
777 if ( value == data.value )
778 {
779 wxPuts(_T("(ok)"));
780 }
781 else
782 {
783 wxPrintf(_T("(ERROR: should be %s)\n"), data.value);
784 }
785 }
786
787 // test enumerating the entries
788 wxPuts(_T("\nEnumerating all root entries:"));
789 long dummy;
790 wxString name;
791 bool cont = fileconf.GetFirstEntry(name, dummy);
792 while ( cont )
793 {
794 wxPrintf(_T("\t%s = %s\n"),
795 name.c_str(),
796 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
797
798 cont = fileconf.GetNextEntry(name, dummy);
799 }
800
801 static const wxChar *testEntry = _T("TestEntry");
802 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
803 fileconf.Write(testEntry, _T("A value"));
804 fileconf.DeleteEntry(testEntry);
805 wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n"));
806 }
807
808 #endif // TEST_FILECONF
809
810 // ----------------------------------------------------------------------------
811 // wxFileName
812 // ----------------------------------------------------------------------------
813
814 #ifdef TEST_FILENAME
815
816 #include "wx/filename.h"
817
818 static void DumpFileName(const wxChar *desc, const wxFileName& fn)
819 {
820 wxPuts(desc);
821
822 wxString full = fn.GetFullPath();
823
824 wxString vol, path, name, ext;
825 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
826
827 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
828 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
829
830 wxFileName::SplitPath(full, &path, &name, &ext);
831 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
832 path.c_str(), name.c_str(), ext.c_str());
833
834 wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str());
835 wxPrintf(_T("with volume: \t'%s'\n"),
836 fn.GetPath(wxPATH_GET_VOLUME).c_str());
837 wxPrintf(_T("with separator:\t'%s'\n"),
838 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
839 wxPrintf(_T("with both: \t'%s'\n"),
840 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
841
842 wxPuts(_T("The directories in the path are:"));
843 wxArrayString dirs = fn.GetDirs();
844 size_t count = dirs.GetCount();
845 for ( size_t n = 0; n < count; n++ )
846 {
847 wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str());
848 }
849 }
850
851 static struct FileNameInfo
852 {
853 const wxChar *fullname;
854 const wxChar *volume;
855 const wxChar *path;
856 const wxChar *name;
857 const wxChar *ext;
858 bool isAbsolute;
859 wxPathFormat format;
860 } filenames[] =
861 {
862 // Unix file names
863 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX },
864 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX },
865 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX },
866 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX },
867 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX },
868 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX },
869 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX },
870 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX },
871 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX },
872
873 // Windows file names
874 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
875 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS },
876 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
877 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
878 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS },
879 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
880 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
881
882 // wxFileName support for Mac file names is broken currently
883 #if 0
884 // Mac file names
885 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
886 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
887 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
888 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
889 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
890 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
891 #endif // 0
892
893 // VMS file names
894 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
895 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
896 };
897
898 static void TestFileNameConstruction()
899 {
900 wxPuts(_T("*** testing wxFileName construction ***"));
901
902 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
903 {
904 const FileNameInfo& fni = filenames[n];
905
906 wxFileName fn(fni.fullname, fni.format);
907
908 wxString fullname = fn.GetFullPath(fni.format);
909 if ( fullname != fni.fullname )
910 {
911 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni.fullname);
912 }
913
914 bool isAbsolute = fn.IsAbsolute(fni.format);
915 wxPrintf(_T("'%s' is %s (%s)\n\t"),
916 fullname.c_str(),
917 isAbsolute ? "absolute" : "relative",
918 isAbsolute == fni.isAbsolute ? "ok" : "ERROR");
919
920 if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) )
921 {
922 wxPuts(_T("ERROR (couldn't be normalized)"));
923 }
924 else
925 {
926 wxPrintf(_T("normalized: '%s'\n"), fn.GetFullPath(fni.format).c_str());
927 }
928 }
929
930 wxPuts(_T(""));
931 }
932
933 static void TestFileNameSplit()
934 {
935 wxPuts(_T("*** testing wxFileName splitting ***"));
936
937 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
938 {
939 const FileNameInfo& fni = filenames[n];
940 wxString volume, path, name, ext;
941 wxFileName::SplitPath(fni.fullname,
942 &volume, &path, &name, &ext, fni.format);
943
944 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
945 fni.fullname,
946 volume.c_str(), path.c_str(), name.c_str(), ext.c_str());
947
948 if ( volume != fni.volume )
949 wxPrintf(_T(" (ERROR: volume = '%s')"), fni.volume);
950 if ( path != fni.path )
951 wxPrintf(_T(" (ERROR: path = '%s')"), fni.path);
952 if ( name != fni.name )
953 wxPrintf(_T(" (ERROR: name = '%s')"), fni.name);
954 if ( ext != fni.ext )
955 wxPrintf(_T(" (ERROR: ext = '%s')"), fni.ext);
956
957 wxPuts(_T(""));
958 }
959 }
960
961 static void TestFileNameTemp()
962 {
963 wxPuts(_T("*** testing wxFileName temp file creation ***"));
964
965 static const wxChar *tmpprefixes[] =
966 {
967 _T(""),
968 _T("foo"),
969 _T(".."),
970 _T("../bar"),
971 #ifdef __UNIX__
972 _T("/tmp/foo"),
973 _T("/tmp/foo/bar"), // this one must be an error
974 #endif // __UNIX__
975 };
976
977 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
978 {
979 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
980 if ( path.empty() )
981 {
982 // "error" is not in upper case because it may be ok
983 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
984 }
985 else
986 {
987 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
988 tmpprefixes[n], path.c_str());
989
990 if ( !wxRemoveFile(path) )
991 {
992 wxLogWarning(_T("Failed to remove temp file '%s'"),
993 path.c_str());
994 }
995 }
996 }
997 }
998
999 static void TestFileNameMakeRelative()
1000 {
1001 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
1002
1003 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1004 {
1005 const FileNameInfo& fni = filenames[n];
1006
1007 wxFileName fn(fni.fullname, fni.format);
1008
1009 // choose the base dir of the same format
1010 wxString base;
1011 switch ( fni.format )
1012 {
1013 case wxPATH_UNIX:
1014 base = _T("/usr/bin/");
1015 break;
1016
1017 case wxPATH_DOS:
1018 base = _T("c:\\");
1019 break;
1020
1021 case wxPATH_MAC:
1022 case wxPATH_VMS:
1023 // TODO: I don't know how this is supposed to work there
1024 continue;
1025
1026 case wxPATH_NATIVE: // make gcc happy
1027 default:
1028 wxFAIL_MSG( _T("unexpected path format") );
1029 }
1030
1031 wxPrintf(_T("'%s' relative to '%s': "),
1032 fn.GetFullPath(fni.format).c_str(), base.c_str());
1033
1034 if ( !fn.MakeRelativeTo(base, fni.format) )
1035 {
1036 wxPuts(_T("unchanged"));
1037 }
1038 else
1039 {
1040 wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str());
1041 }
1042 }
1043 }
1044
1045 static void TestFileNameMakeAbsolute()
1046 {
1047 wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
1048
1049 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1050 {
1051 const FileNameInfo& fni = filenames[n];
1052 wxFileName fn(fni.fullname, fni.format);
1053
1054 wxPrintf(_T("'%s' absolutized: "),
1055 fn.GetFullPath(fni.format).c_str());
1056 fn.MakeAbsolute();
1057 wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str());
1058 }
1059
1060 wxPuts(_T(""));
1061 }
1062
1063 static void TestFileNameDirManip()
1064 {
1065 // TODO: test AppendDir(), RemoveDir(), ...
1066 }
1067
1068 static void TestFileNameComparison()
1069 {
1070 // TODO!
1071 }
1072
1073 static void TestFileNameOperations()
1074 {
1075 // TODO!
1076 }
1077
1078 static void TestFileNameCwd()
1079 {
1080 // TODO!
1081 }
1082
1083 #endif // TEST_FILENAME
1084
1085 // ----------------------------------------------------------------------------
1086 // wxFileName time functions
1087 // ----------------------------------------------------------------------------
1088
1089 #ifdef TEST_FILETIME
1090
1091 #include <wx/filename.h>
1092 #include <wx/datetime.h>
1093
1094 static void TestFileGetTimes()
1095 {
1096 wxFileName fn(_T("testdata.fc"));
1097
1098 wxDateTime dtAccess, dtMod, dtCreate;
1099 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
1100 {
1101 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1102 }
1103 else
1104 {
1105 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
1106
1107 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
1108 wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
1109 wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
1110 wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
1111 }
1112 }
1113
1114 static void TestFileSetTimes()
1115 {
1116 wxFileName fn(_T("testdata.fc"));
1117
1118 if ( !fn.Touch() )
1119 {
1120 wxPrintf(_T("ERROR: Touch() failed.\n"));
1121 }
1122 }
1123
1124 #endif // TEST_FILETIME
1125
1126 // ----------------------------------------------------------------------------
1127 // wxHashTable
1128 // ----------------------------------------------------------------------------
1129
1130 #ifdef TEST_HASH
1131
1132 #include "wx/hash.h"
1133
1134 struct Foo
1135 {
1136 Foo(int n_) { n = n_; count++; }
1137 ~Foo() { count--; }
1138
1139 int n;
1140
1141 static size_t count;
1142 };
1143
1144 size_t Foo::count = 0;
1145
1146 WX_DECLARE_LIST(Foo, wxListFoos);
1147 WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos);
1148
1149 #include "wx/listimpl.cpp"
1150
1151 WX_DEFINE_LIST(wxListFoos);
1152
1153 #include "wx/timer.h"
1154
1155 static void TestHash()
1156 {
1157 wxPuts(_T("*** Testing wxHashTable ***\n"));
1158 const int COUNT = 100;
1159
1160 wxStopWatch sw;
1161
1162 sw.Start();
1163
1164 {
1165 wxHashTable hash(wxKEY_INTEGER, 10), hash2(wxKEY_STRING);
1166 wxObject o;
1167 int i;
1168
1169 for ( i = 0; i < COUNT; ++i )
1170 hash.Put(i, &o + i);
1171
1172 hash.BeginFind();
1173 wxHashTable::compatibility_iterator it = hash.Next();
1174 i = 0;
1175
1176 while (it)
1177 {
1178 ++i;
1179 it = hash.Next();
1180 }
1181
1182 if (i != COUNT)
1183 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1184
1185 for ( i = 99; i >= 0; --i )
1186 if( hash.Get(i) != &o + i )
1187 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1188
1189 for ( i = 0; i < COUNT; ++i )
1190 hash.Put(i, &o + i + 20);
1191
1192 for ( i = 99; i >= 0; --i )
1193 if( hash.Get(i) != &o + i)
1194 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1195
1196 for ( i = 0; i < COUNT/2; ++i )
1197 if( hash.Delete(i) != &o + i)
1198 wxPuts(_T("Error in wxHashTable::Delete\n"));
1199
1200 for ( i = COUNT/2; i < COUNT; ++i )
1201 if( hash.Get(i) != &o + i)
1202 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1203
1204 for ( i = 0; i < COUNT/2; ++i )
1205 if( hash.Get(i) != &o + i + 20)
1206 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1207
1208 for ( i = 0; i < COUNT/2; ++i )
1209 if( hash.Delete(i) != &o + i + 20)
1210 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1211
1212 for ( i = 0; i < COUNT/2; ++i )
1213 if( hash.Get(i) != NULL)
1214 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1215
1216 hash2.Put(_T("foo"), &o + 1);
1217 hash2.Put(_T("bar"), &o + 2);
1218 hash2.Put(_T("baz"), &o + 3);
1219
1220 if (hash2.Get(_T("moo")) != NULL)
1221 wxPuts(_T("Error in wxHashTable::Get\n"));
1222
1223 if (hash2.Get(_T("bar")) != &o + 2)
1224 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1225
1226 hash2.Put(_T("bar"), &o + 0);
1227
1228 if (hash2.Get(_T("bar")) != &o + 2)
1229 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1230 }
1231
1232 // and now some corner-case testing; 3 and 13 hash to the same bucket
1233 {
1234 wxHashTable hash(wxKEY_INTEGER, 10);
1235 wxObject dummy;
1236
1237 hash.Put(3, &dummy);
1238 hash.Delete(3);
1239
1240 if (hash.Get(3) != NULL)
1241 wxPuts(_T("Corner case 1 failure\n"));
1242
1243 hash.Put(3, &dummy);
1244 hash.Put(13, &dummy);
1245 hash.Delete(3);
1246
1247 if (hash.Get(3) != NULL)
1248 wxPuts(_T("Corner case 2 failure\n"));
1249
1250 hash.Delete(13);
1251
1252 if (hash.Get(13) != NULL)
1253 wxPuts(_T("Corner case 3 failure\n"));
1254
1255 hash.Put(3, &dummy);
1256 hash.Put(13, &dummy);
1257 hash.Delete(13);
1258
1259 if (hash.Get(13) != NULL)
1260 wxPuts(_T("Corner case 4 failure\n"));
1261
1262 hash.Delete(3);
1263
1264 if (hash.Get(3) != NULL)
1265 wxPuts(_T("Corner case 5 failure\n"));
1266 }
1267
1268 {
1269 wxHashTable hash(wxKEY_INTEGER, 10);
1270 wxObject dummy;
1271
1272 hash.Put(3, 7, &dummy + 7);
1273 hash.Put(4, 8, &dummy + 8);
1274
1275 if (hash.Get(7) != NULL) wxPuts(_T("Key/Hash 1 failure\n"));
1276 if (hash.Get(3, 7) != &dummy + 7) wxPuts(_T("Key/Hash 2 failure\n"));
1277 if (hash.Get(4) != NULL) wxPuts(_T("Key/Hash 3 failure\n"));
1278 if (hash.Get(3) != NULL) wxPuts(_T("Key/Hash 4 failure\n"));
1279 if (hash.Get(8) != NULL) wxPuts(_T("Key/Hash 5 failure\n"));
1280 if (hash.Get(8, 4) != NULL) wxPuts(_T("Key/Hash 6 failure\n"));
1281
1282 if (hash.Delete(7) != NULL) wxPuts(_T("Key/Hash 7 failure\n"));
1283 if (hash.Delete(3) != NULL) wxPuts(_T("Key/Hash 8 failure\n"));
1284 if (hash.Delete(3, 7) != &dummy + 7) wxPuts(_T("Key/Hash 8 failure\n"));
1285 }
1286
1287 {
1288 wxHashFoos hash;
1289 hash.DeleteContents(true);
1290
1291 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1292 hash.GetCount(), Foo::count);
1293
1294 static const int hashTestData[] =
1295 {
1296 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1297 };
1298
1299 size_t n;
1300 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
1301 {
1302 hash.Put(hashTestData[n], n, new Foo(n));
1303 }
1304
1305 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1306 hash.GetCount(), Foo::count);
1307
1308 wxPuts(_T("Hash access test:"));
1309 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
1310 {
1311 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1312 hashTestData[n], n);
1313 Foo *foo = hash.Get(hashTestData[n], n);
1314 if ( !foo )
1315 {
1316 wxPrintf(_T("ERROR, not found.\n"));
1317 }
1318 else
1319 {
1320 wxPrintf(_T("%d (%s)\n"), foo->n,
1321 (size_t)foo->n == n ? "ok" : "ERROR");
1322 }
1323 }
1324
1325 wxPrintf(_T("\nTrying to get an element not in hash: "));
1326
1327 if ( hash.Get(1234) || hash.Get(1, 0) )
1328 {
1329 wxPuts(_T("ERROR: found!"));
1330 }
1331 else
1332 {
1333 wxPuts(_T("ok (not found)"));
1334 }
1335
1336 Foo* foo = hash.Delete(0);
1337
1338 wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count);
1339
1340 delete foo;
1341
1342 wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count);
1343 }
1344
1345 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count);
1346 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1347
1348 wxPrintf(_T("Time: %ld\n"), sw.Time());
1349 }
1350
1351 #endif // TEST_HASH
1352
1353 // ----------------------------------------------------------------------------
1354 // wxHashMap
1355 // ----------------------------------------------------------------------------
1356
1357 #ifdef TEST_HASHMAP
1358
1359 #include "wx/hashmap.h"
1360
1361 // test compilation of basic map types
1362 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash, wxPointerEqual, myPtrHashMap );
1363 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash, wxIntegerEqual, myLongHashMap );
1364 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash, wxIntegerEqual,
1365 myUnsignedHashMap );
1366 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash, wxIntegerEqual,
1367 myTestHashMap1 );
1368 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash, wxIntegerEqual,
1369 myTestHashMap2 );
1370 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash, wxIntegerEqual,
1371 myTestHashMap3 );
1372 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash, wxIntegerEqual,
1373 myTestHashMap4 );
1374
1375 // same as:
1376 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1377 // myStringHashMap );
1378 WX_DECLARE_STRING_HASH_MAP(wxString, myStringHashMap);
1379
1380 typedef myStringHashMap::iterator Itor;
1381
1382 static void TestHashMap()
1383 {
1384 wxPuts(_T("*** Testing wxHashMap ***\n"));
1385 myStringHashMap sh(0); // as small as possible
1386 wxString buf;
1387 size_t i;
1388 const size_t count = 10000;
1389
1390 // init with some data
1391 for( i = 0; i < count; ++i )
1392 {
1393 buf.Printf(wxT("%d"), i );
1394 sh[buf] = wxT("A") + buf + wxT("C");
1395 }
1396
1397 // test that insertion worked
1398 if( sh.size() != count )
1399 {
1400 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh.size(), count);
1401 }
1402
1403 for( i = 0; i < count; ++i )
1404 {
1405 buf.Printf(wxT("%d"), i );
1406 if( sh[buf] != wxT("A") + buf + wxT("C") )
1407 {
1408 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1409 return;
1410 }
1411 }
1412
1413 // check that iterators work
1414 Itor it;
1415 for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i )
1416 {
1417 if( i == count )
1418 {
1419 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1420 return;
1421 }
1422
1423 if( it->second != sh[it->first] )
1424 {
1425 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1426 return;
1427 }
1428 }
1429
1430 if( sh.size() != i )
1431 {
1432 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i, count);
1433 }
1434
1435 // test copy ctor, assignment operator
1436 myStringHashMap h1( sh ), h2( 0 );
1437 h2 = sh;
1438
1439 for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i )
1440 {
1441 if( h1[it->first] != it->second )
1442 {
1443 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it->first.c_str());
1444 }
1445
1446 if( h2[it->first] != it->second )
1447 {
1448 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it->first.c_str());
1449 }
1450 }
1451
1452 // other tests
1453 for( i = 0; i < count; ++i )
1454 {
1455 buf.Printf(wxT("%d"), i );
1456 size_t sz = sh.size();
1457
1458 // test find() and erase(it)
1459 if( i < 100 )
1460 {
1461 it = sh.find( buf );
1462 if( it != sh.end() )
1463 {
1464 sh.erase( it );
1465
1466 if( sh.find( buf ) != sh.end() )
1467 {
1468 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i);
1469 }
1470 }
1471 else
1472 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i);
1473 }
1474 else
1475 // test erase(key)
1476 {
1477 size_t c = sh.erase( buf );
1478 if( c != 1 )
1479 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1480
1481 if( sh.find( buf ) != sh.end() )
1482 {
1483 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i);
1484 }
1485 }
1486
1487 // count should decrease
1488 if( sh.size() != sz - 1 )
1489 {
1490 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1491 }
1492 }
1493
1494 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1495 }
1496
1497 #endif // TEST_HASHMAP
1498
1499 // ----------------------------------------------------------------------------
1500 // wxHashSet
1501 // ----------------------------------------------------------------------------
1502
1503 #ifdef TEST_HASHSET
1504
1505 #include "wx/hashset.h"
1506
1507 // test compilation of basic map types
1508 WX_DECLARE_HASH_SET( int*, wxPointerHash, wxPointerEqual, myPtrHashSet );
1509 WX_DECLARE_HASH_SET( long, wxIntegerHash, wxIntegerEqual, myLongHashSet );
1510 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash, wxIntegerEqual,
1511 myUnsignedHashSet );
1512 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash, wxIntegerEqual,
1513 myTestHashSet1 );
1514 WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual,
1515 myTestHashSet2 );
1516 WX_DECLARE_HASH_SET( short, wxIntegerHash, wxIntegerEqual,
1517 myTestHashSet3 );
1518 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash, wxIntegerEqual,
1519 myTestHashSet4 );
1520 WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual,
1521 myTestHashSet5 );
1522
1523 struct MyStruct
1524 {
1525 int* ptr;
1526 wxString str;
1527 };
1528
1529 class MyHash
1530 {
1531 public:
1532 unsigned long operator()(const MyStruct& s) const
1533 { return m_dummy(s.ptr); }
1534 MyHash& operator=(const MyHash&) { return *this; }
1535 private:
1536 wxPointerHash m_dummy;
1537 };
1538
1539 class MyEqual
1540 {
1541 public:
1542 bool operator()(const MyStruct& s1, const MyStruct& s2) const
1543 { return s1.ptr == s2.ptr; }
1544 MyEqual& operator=(const MyEqual&) { return *this; }
1545 };
1546
1547 WX_DECLARE_HASH_SET( MyStruct, MyHash, MyEqual, mySet );
1548
1549 typedef myTestHashSet5 wxStringHashSet;
1550
1551 static void TestHashSet()
1552 {
1553 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1554
1555 wxStringHashSet set1;
1556
1557 set1.insert( _T("abc") );
1558 set1.insert( _T("bbc") );
1559 set1.insert( _T("cbc") );
1560 set1.insert( _T("abc") );
1561
1562 if( set1.size() != 3 )
1563 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1564
1565 mySet set2;
1566 int dummy;
1567 MyStruct tmp;
1568
1569 tmp.ptr = &dummy; tmp.str = _T("ABC");
1570 set2.insert( tmp );
1571 tmp.ptr = &dummy + 1;
1572 set2.insert( tmp );
1573 tmp.ptr = &dummy; tmp.str = _T("CDE");
1574 set2.insert( tmp );
1575
1576 if( set2.size() != 2 )
1577 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1578
1579 mySet::iterator it = set2.find( tmp );
1580
1581 if( it == set2.end() )
1582 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1583 if( it->ptr != &dummy )
1584 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1585 if( it->str != _T("ABC") )
1586 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1587
1588 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1589 }
1590
1591 #endif // TEST_HASHSET
1592
1593 // ----------------------------------------------------------------------------
1594 // wxList
1595 // ----------------------------------------------------------------------------
1596
1597 #ifdef TEST_LIST
1598
1599 #include "wx/list.h"
1600
1601 WX_DECLARE_LIST(Bar, wxListBars);
1602 #include "wx/listimpl.cpp"
1603 WX_DEFINE_LIST(wxListBars);
1604
1605 WX_DECLARE_LIST(int, wxListInt);
1606 WX_DEFINE_LIST(wxListInt);
1607
1608 static void TestList()
1609 {
1610 wxPuts(_T("*** Testing wxList operations ***\n"));
1611 {
1612 wxListInt list1;
1613 int dummy[5];
1614 int i;
1615
1616 for ( i = 0; i < 5; ++i )
1617 list1.Append(dummy + i);
1618
1619 if ( list1.GetCount() != 5 )
1620 wxPuts(_T("Wrong number of items in list\n"));
1621
1622 if ( list1.Item(3)->GetData() != dummy + 3 )
1623 wxPuts(_T("Error in Item()\n"));
1624
1625 if ( !list1.Find(dummy + 4) )
1626 wxPuts(_T("Error in Find()\n"));
1627
1628 wxListInt::compatibility_iterator node = list1.GetFirst();
1629 i = 0;
1630
1631 while (node)
1632 {
1633 if ( node->GetData() != dummy + i )
1634 wxPuts(_T("Error in compatibility_iterator\n"));
1635 node = node->GetNext();
1636 ++i;
1637 }
1638
1639 if ( size_t(i) != list1.GetCount() )
1640 wxPuts(_T("Error in compatibility_iterator\n"));
1641
1642 list1.Insert(dummy + 0);
1643 list1.Insert(1, dummy + 1);
1644 list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2);
1645
1646 node = list1.GetFirst();
1647 i = 0;
1648
1649 while (i < 3)
1650 {
1651 int* t = node->GetData();
1652 if ( t != dummy + i )
1653 wxPuts(_T("Error in Insert\n"));
1654 node = node->GetNext();
1655 ++i;
1656 }
1657 }
1658
1659 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1660
1661 wxPuts(_T("*** Testing std::list operations ***\n"));
1662
1663 {
1664 wxListInt list1;
1665 wxListInt::iterator it, en;
1666 wxListInt::reverse_iterator rit, ren;
1667 int i;
1668 for ( i = 0; i < 5; ++i )
1669 list1.push_back(i + &i);
1670
1671 for ( it = list1.begin(), en = list1.end(), i = 0;
1672 it != en; ++it, ++i )
1673 if ( *it != i + &i )
1674 wxPuts(_T("Error in iterator\n"));
1675
1676 for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
1677 rit != ren; ++rit, --i )
1678 if ( *rit != i + &i )
1679 wxPuts(_T("Error in reverse_iterator\n"));
1680
1681 if ( *list1.rbegin() != *--list1.end() ||
1682 *list1.begin() != *--list1.rend() )
1683 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1684 if ( *list1.begin() != *--++list1.begin() ||
1685 *list1.rbegin() != *--++list1.rbegin() )
1686 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1687
1688 if ( list1.front() != &i || list1.back() != &i + 4 )
1689 wxPuts(_T("Error in front()/back()\n"));
1690
1691 list1.erase(list1.begin());
1692 list1.erase(--list1.end());
1693
1694 for ( it = list1.begin(), en = list1.end(), i = 1;
1695 it != en; ++it, ++i )
1696 if ( *it != i + &i )
1697 wxPuts(_T("Error in erase()\n"));
1698 }
1699
1700 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1701 }
1702
1703 static void TestListCtor()
1704 {
1705 wxPuts(_T("*** Testing wxList construction ***\n"));
1706
1707 {
1708 wxListBars list1;
1709 list1.Append(new Bar(_T("first")));
1710 list1.Append(new Bar(_T("second")));
1711
1712 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1713 list1.GetCount(), Bar::GetNumber());
1714
1715 wxListBars list2;
1716 list2 = list1;
1717
1718 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1719 list1.GetCount(), list2.GetCount(), Bar::GetNumber());
1720
1721 #if !wxUSE_STL
1722 list1.DeleteContents(true);
1723 #else
1724 WX_CLEAR_LIST(wxListBars, list1);
1725 #endif
1726 }
1727
1728 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1729 }
1730
1731 #endif // TEST_LIST
1732
1733 // ----------------------------------------------------------------------------
1734 // wxLocale
1735 // ----------------------------------------------------------------------------
1736
1737 #ifdef TEST_LOCALE
1738
1739 #include "wx/intl.h"
1740 #include "wx/utils.h" // for wxSetEnv
1741
1742 static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH);
1743
1744 // find the name of the language from its value
1745 static const wxChar *GetLangName(int lang)
1746 {
1747 static const wxChar *languageNames[] =
1748 {
1749 _T("DEFAULT"),
1750 _T("UNKNOWN"),
1751 _T("ABKHAZIAN"),
1752 _T("AFAR"),
1753 _T("AFRIKAANS"),
1754 _T("ALBANIAN"),
1755 _T("AMHARIC"),
1756 _T("ARABIC"),
1757 _T("ARABIC_ALGERIA"),
1758 _T("ARABIC_BAHRAIN"),
1759 _T("ARABIC_EGYPT"),
1760 _T("ARABIC_IRAQ"),
1761 _T("ARABIC_JORDAN"),
1762 _T("ARABIC_KUWAIT"),
1763 _T("ARABIC_LEBANON"),
1764 _T("ARABIC_LIBYA"),
1765 _T("ARABIC_MOROCCO"),
1766 _T("ARABIC_OMAN"),
1767 _T("ARABIC_QATAR"),
1768 _T("ARABIC_SAUDI_ARABIA"),
1769 _T("ARABIC_SUDAN"),
1770 _T("ARABIC_SYRIA"),
1771 _T("ARABIC_TUNISIA"),
1772 _T("ARABIC_UAE"),
1773 _T("ARABIC_YEMEN"),
1774 _T("ARMENIAN"),
1775 _T("ASSAMESE"),
1776 _T("AYMARA"),
1777 _T("AZERI"),
1778 _T("AZERI_CYRILLIC"),
1779 _T("AZERI_LATIN"),
1780 _T("BASHKIR"),
1781 _T("BASQUE"),
1782 _T("BELARUSIAN"),
1783 _T("BENGALI"),
1784 _T("BHUTANI"),
1785 _T("BIHARI"),
1786 _T("BISLAMA"),
1787 _T("BRETON"),
1788 _T("BULGARIAN"),
1789 _T("BURMESE"),
1790 _T("CAMBODIAN"),
1791 _T("CATALAN"),
1792 _T("CHINESE"),
1793 _T("CHINESE_SIMPLIFIED"),
1794 _T("CHINESE_TRADITIONAL"),
1795 _T("CHINESE_HONGKONG"),
1796 _T("CHINESE_MACAU"),
1797 _T("CHINESE_SINGAPORE"),
1798 _T("CHINESE_TAIWAN"),
1799 _T("CORSICAN"),
1800 _T("CROATIAN"),
1801 _T("CZECH"),
1802 _T("DANISH"),
1803 _T("DUTCH"),
1804 _T("DUTCH_BELGIAN"),
1805 _T("ENGLISH"),
1806 _T("ENGLISH_UK"),
1807 _T("ENGLISH_US"),
1808 _T("ENGLISH_AUSTRALIA"),
1809 _T("ENGLISH_BELIZE"),
1810 _T("ENGLISH_BOTSWANA"),
1811 _T("ENGLISH_CANADA"),
1812 _T("ENGLISH_CARIBBEAN"),
1813 _T("ENGLISH_DENMARK"),
1814 _T("ENGLISH_EIRE"),
1815 _T("ENGLISH_JAMAICA"),
1816 _T("ENGLISH_NEW_ZEALAND"),
1817 _T("ENGLISH_PHILIPPINES"),
1818 _T("ENGLISH_SOUTH_AFRICA"),
1819 _T("ENGLISH_TRINIDAD"),
1820 _T("ENGLISH_ZIMBABWE"),
1821 _T("ESPERANTO"),
1822 _T("ESTONIAN"),
1823 _T("FAEROESE"),
1824 _T("FARSI"),
1825 _T("FIJI"),
1826 _T("FINNISH"),
1827 _T("FRENCH"),
1828 _T("FRENCH_BELGIAN"),
1829 _T("FRENCH_CANADIAN"),
1830 _T("FRENCH_LUXEMBOURG"),
1831 _T("FRENCH_MONACO"),
1832 _T("FRENCH_SWISS"),
1833 _T("FRISIAN"),
1834 _T("GALICIAN"),
1835 _T("GEORGIAN"),
1836 _T("GERMAN"),
1837 _T("GERMAN_AUSTRIAN"),
1838 _T("GERMAN_BELGIUM"),
1839 _T("GERMAN_LIECHTENSTEIN"),
1840 _T("GERMAN_LUXEMBOURG"),
1841 _T("GERMAN_SWISS"),
1842 _T("GREEK"),
1843 _T("GREENLANDIC"),
1844 _T("GUARANI"),
1845 _T("GUJARATI"),
1846 _T("HAUSA"),
1847 _T("HEBREW"),
1848 _T("HINDI"),
1849 _T("HUNGARIAN"),
1850 _T("ICELANDIC"),
1851 _T("INDONESIAN"),
1852 _T("INTERLINGUA"),
1853 _T("INTERLINGUE"),
1854 _T("INUKTITUT"),
1855 _T("INUPIAK"),
1856 _T("IRISH"),
1857 _T("ITALIAN"),
1858 _T("ITALIAN_SWISS"),
1859 _T("JAPANESE"),
1860 _T("JAVANESE"),
1861 _T("KANNADA"),
1862 _T("KASHMIRI"),
1863 _T("KASHMIRI_INDIA"),
1864 _T("KAZAKH"),
1865 _T("KERNEWEK"),
1866 _T("KINYARWANDA"),
1867 _T("KIRGHIZ"),
1868 _T("KIRUNDI"),
1869 _T("KONKANI"),
1870 _T("KOREAN"),
1871 _T("KURDISH"),
1872 _T("LAOTHIAN"),
1873 _T("LATIN"),
1874 _T("LATVIAN"),
1875 _T("LINGALA"),
1876 _T("LITHUANIAN"),
1877 _T("MACEDONIAN"),
1878 _T("MALAGASY"),
1879 _T("MALAY"),
1880 _T("MALAYALAM"),
1881 _T("MALAY_BRUNEI_DARUSSALAM"),
1882 _T("MALAY_MALAYSIA"),
1883 _T("MALTESE"),
1884 _T("MANIPURI"),
1885 _T("MAORI"),
1886 _T("MARATHI"),
1887 _T("MOLDAVIAN"),
1888 _T("MONGOLIAN"),
1889 _T("NAURU"),
1890 _T("NEPALI"),
1891 _T("NEPALI_INDIA"),
1892 _T("NORWEGIAN_BOKMAL"),
1893 _T("NORWEGIAN_NYNORSK"),
1894 _T("OCCITAN"),
1895 _T("ORIYA"),
1896 _T("OROMO"),
1897 _T("PASHTO"),
1898 _T("POLISH"),
1899 _T("PORTUGUESE"),
1900 _T("PORTUGUESE_BRAZILIAN"),
1901 _T("PUNJABI"),
1902 _T("QUECHUA"),
1903 _T("RHAETO_ROMANCE"),
1904 _T("ROMANIAN"),
1905 _T("RUSSIAN"),
1906 _T("RUSSIAN_UKRAINE"),
1907 _T("SAMOAN"),
1908 _T("SANGHO"),
1909 _T("SANSKRIT"),
1910 _T("SCOTS_GAELIC"),
1911 _T("SERBIAN"),
1912 _T("SERBIAN_CYRILLIC"),
1913 _T("SERBIAN_LATIN"),
1914 _T("SERBO_CROATIAN"),
1915 _T("SESOTHO"),
1916 _T("SETSWANA"),
1917 _T("SHONA"),
1918 _T("SINDHI"),
1919 _T("SINHALESE"),
1920 _T("SISWATI"),
1921 _T("SLOVAK"),
1922 _T("SLOVENIAN"),
1923 _T("SOMALI"),
1924 _T("SPANISH"),
1925 _T("SPANISH_ARGENTINA"),
1926 _T("SPANISH_BOLIVIA"),
1927 _T("SPANISH_CHILE"),
1928 _T("SPANISH_COLOMBIA"),
1929 _T("SPANISH_COSTA_RICA"),
1930 _T("SPANISH_DOMINICAN_REPUBLIC"),
1931 _T("SPANISH_ECUADOR"),
1932 _T("SPANISH_EL_SALVADOR"),
1933 _T("SPANISH_GUATEMALA"),
1934 _T("SPANISH_HONDURAS"),
1935 _T("SPANISH_MEXICAN"),
1936 _T("SPANISH_MODERN"),
1937 _T("SPANISH_NICARAGUA"),
1938 _T("SPANISH_PANAMA"),
1939 _T("SPANISH_PARAGUAY"),
1940 _T("SPANISH_PERU"),
1941 _T("SPANISH_PUERTO_RICO"),
1942 _T("SPANISH_URUGUAY"),
1943 _T("SPANISH_US"),
1944 _T("SPANISH_VENEZUELA"),
1945 _T("SUNDANESE"),
1946 _T("SWAHILI"),
1947 _T("SWEDISH"),
1948 _T("SWEDISH_FINLAND"),
1949 _T("TAGALOG"),
1950 _T("TAJIK"),
1951 _T("TAMIL"),
1952 _T("TATAR"),
1953 _T("TELUGU"),
1954 _T("THAI"),
1955 _T("TIBETAN"),
1956 _T("TIGRINYA"),
1957 _T("TONGA"),
1958 _T("TSONGA"),
1959 _T("TURKISH"),
1960 _T("TURKMEN"),
1961 _T("TWI"),
1962 _T("UIGHUR"),
1963 _T("UKRAINIAN"),
1964 _T("URDU"),
1965 _T("URDU_INDIA"),
1966 _T("URDU_PAKISTAN"),
1967 _T("UZBEK"),
1968 _T("UZBEK_CYRILLIC"),
1969 _T("UZBEK_LATIN"),
1970 _T("VIETNAMESE"),
1971 _T("VOLAPUK"),
1972 _T("WELSH"),
1973 _T("WOLOF"),
1974 _T("XHOSA"),
1975 _T("YIDDISH"),
1976 _T("YORUBA"),
1977 _T("ZHUANG"),
1978 _T("ZULU"),
1979 };
1980
1981 if ( (size_t)lang < WXSIZEOF(languageNames) )
1982 return languageNames[lang];
1983 else
1984 return _T("INVALID");
1985 }
1986
1987 static void TestDefaultLang()
1988 {
1989 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1990
1991 static const wxChar *langStrings[] =
1992 {
1993 NULL, // system default
1994 _T("C"),
1995 _T("fr"),
1996 _T("fr_FR"),
1997 _T("en"),
1998 _T("en_GB"),
1999 _T("en_US"),
2000 _T("de_DE.iso88591"),
2001 _T("german"),
2002 _T("?"), // invalid lang spec
2003 _T("klingonese"), // I bet on some systems it does exist...
2004 };
2005
2006 wxPrintf(_T("The default system encoding is %s (%d)\n"),
2007 wxLocale::GetSystemEncodingName().c_str(),
2008 wxLocale::GetSystemEncoding());
2009
2010 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
2011 {
2012 const wxChar *langStr = langStrings[n];
2013 if ( langStr )
2014 {
2015 // FIXME: this doesn't do anything at all under Windows, we need
2016 // to create a new wxLocale!
2017 wxSetEnv(_T("LC_ALL"), langStr);
2018 }
2019
2020 int lang = gs_localeDefault.GetSystemLanguage();
2021 wxPrintf(_T("Locale for '%s' is %s.\n"),
2022 langStr ? langStr : _T("system default"), GetLangName(lang));
2023 }
2024 }
2025
2026 #endif // TEST_LOCALE
2027
2028 // ----------------------------------------------------------------------------
2029 // MIME types
2030 // ----------------------------------------------------------------------------
2031
2032 #ifdef TEST_MIME
2033
2034 #include "wx/mimetype.h"
2035
2036 static void TestMimeEnum()
2037 {
2038 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
2039
2040 wxArrayString mimetypes;
2041
2042 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
2043
2044 wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
2045
2046 wxArrayString exts;
2047 wxString desc;
2048
2049 for ( size_t n = 0; n < count; n++ )
2050 {
2051 wxFileType *filetype =
2052 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
2053 if ( !filetype )
2054 {
2055 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
2056 mimetypes[n].c_str());
2057 continue;
2058 }
2059
2060 filetype->GetDescription(&desc);
2061 filetype->GetExtensions(exts);
2062
2063 filetype->GetIcon(NULL);
2064
2065 wxString extsAll;
2066 for ( size_t e = 0; e < exts.GetCount(); e++ )
2067 {
2068 if ( e > 0 )
2069 extsAll << _T(", ");
2070 extsAll += exts[e];
2071 }
2072
2073 wxPrintf(_T("\t%s: %s (%s)\n"),
2074 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
2075 }
2076
2077 wxPuts(_T(""));
2078 }
2079
2080 static void TestMimeOverride()
2081 {
2082 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2083
2084 static const wxChar *mailcap = _T("/tmp/mailcap");
2085 static const wxChar *mimetypes = _T("/tmp/mime.types");
2086
2087 if ( wxFile::Exists(mailcap) )
2088 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2089 mailcap,
2090 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
2091 else
2092 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2093 mailcap);
2094
2095 if ( wxFile::Exists(mimetypes) )
2096 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2097 mimetypes,
2098 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
2099 else
2100 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2101 mimetypes);
2102
2103 wxPuts(_T(""));
2104 }
2105
2106 static void TestMimeFilename()
2107 {
2108 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2109
2110 static const wxChar *filenames[] =
2111 {
2112 _T("readme.txt"),
2113 _T("document.pdf"),
2114 _T("image.gif"),
2115 _T("picture.jpeg"),
2116 };
2117
2118 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
2119 {
2120 const wxString fname = filenames[n];
2121 wxString ext = fname.AfterLast(_T('.'));
2122 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
2123 if ( !ft )
2124 {
2125 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
2126 }
2127 else
2128 {
2129 wxString desc;
2130 if ( !ft->GetDescription(&desc) )
2131 desc = _T("<no description>");
2132
2133 wxString cmd;
2134 if ( !ft->GetOpenCommand(&cmd,
2135 wxFileType::MessageParameters(fname, _T(""))) )
2136 cmd = _T("<no command available>");
2137 else
2138 cmd = wxString(_T('"')) + cmd + _T('"');
2139
2140 wxPrintf(_T("To open %s (%s) do %s.\n"),
2141 fname.c_str(), desc.c_str(), cmd.c_str());
2142
2143 delete ft;
2144 }
2145 }
2146
2147 wxPuts(_T(""));
2148 }
2149
2150 static void TestMimeAssociate()
2151 {
2152 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2153
2154 wxFileTypeInfo ftInfo(
2155 _T("application/x-xyz"),
2156 _T("xyzview '%s'"), // open cmd
2157 _T(""), // print cmd
2158 _T("XYZ File"), // description
2159 _T(".xyz"), // extensions
2160 NULL // end of extensions
2161 );
2162 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
2163
2164 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
2165 if ( !ft )
2166 {
2167 wxPuts(_T("ERROR: failed to create association!"));
2168 }
2169 else
2170 {
2171 // TODO: read it back
2172 delete ft;
2173 }
2174
2175 wxPuts(_T(""));
2176 }
2177
2178 #endif // TEST_MIME
2179
2180 // ----------------------------------------------------------------------------
2181 // misc information functions
2182 // ----------------------------------------------------------------------------
2183
2184 #ifdef TEST_INFO_FUNCTIONS
2185
2186 #include "wx/utils.h"
2187
2188 static void TestDiskInfo()
2189 {
2190 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2191
2192 for ( ;; )
2193 {
2194 wxChar pathname[128];
2195 wxPrintf(_T("\nEnter a directory name: "));
2196 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
2197 break;
2198
2199 // kill the last '\n'
2200 pathname[wxStrlen(pathname) - 1] = 0;
2201
2202 wxLongLong total, free;
2203 if ( !wxGetDiskSpace(pathname, &total, &free) )
2204 {
2205 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2206 }
2207 else
2208 {
2209 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2210 (total / 1024).ToString().c_str(),
2211 (free / 1024).ToString().c_str(),
2212 pathname);
2213 }
2214 }
2215 }
2216
2217 static void TestOsInfo()
2218 {
2219 wxPuts(_T("*** Testing OS info functions ***\n"));
2220
2221 int major, minor;
2222 wxGetOsVersion(&major, &minor);
2223 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2224 wxGetOsDescription().c_str(), major, minor);
2225
2226 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2227
2228 wxPrintf(_T("Host name is %s (%s).\n"),
2229 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2230
2231 wxPuts(_T(""));
2232 }
2233
2234 static void TestUserInfo()
2235 {
2236 wxPuts(_T("*** Testing user info functions ***\n"));
2237
2238 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2239 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2240 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2241 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2242
2243 wxPuts(_T(""));
2244 }
2245
2246 #endif // TEST_INFO_FUNCTIONS
2247
2248 // ----------------------------------------------------------------------------
2249 // long long
2250 // ----------------------------------------------------------------------------
2251
2252 #ifdef TEST_LONGLONG
2253
2254 #include "wx/longlong.h"
2255 #include "wx/timer.h"
2256
2257 // make a 64 bit number from 4 16 bit ones
2258 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
2259
2260 // get a random 64 bit number
2261 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
2262
2263 static const long testLongs[] =
2264 {
2265 0,
2266 1,
2267 -1,
2268 LONG_MAX,
2269 LONG_MIN,
2270 0x1234,
2271 -0x1234
2272 };
2273
2274 #if wxUSE_LONGLONG_WX
2275 inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
2276 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
2277 inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
2278 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
2279 #endif // wxUSE_LONGLONG_WX
2280
2281 static void TestSpeed()
2282 {
2283 static const long max = 100000000;
2284 long n;
2285
2286 {
2287 wxStopWatch sw;
2288
2289 long l = 0;
2290 for ( n = 0; n < max; n++ )
2291 {
2292 l += n;
2293 }
2294
2295 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw.Time());
2296 }
2297
2298 #if wxUSE_LONGLONG_NATIVE
2299 {
2300 wxStopWatch sw;
2301
2302 wxLongLong_t l = 0;
2303 for ( n = 0; n < max; n++ )
2304 {
2305 l += n;
2306 }
2307
2308 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw.Time());
2309 }
2310 #endif // wxUSE_LONGLONG_NATIVE
2311
2312 {
2313 wxStopWatch sw;
2314
2315 wxLongLong l;
2316 for ( n = 0; n < max; n++ )
2317 {
2318 l += n;
2319 }
2320
2321 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw.Time());
2322 }
2323 }
2324
2325 static void TestLongLongConversion()
2326 {
2327 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2328
2329 wxLongLong a;
2330 size_t nTested = 0;
2331 for ( size_t n = 0; n < 100000; n++ )
2332 {
2333 a = RAND_LL();
2334
2335 #if wxUSE_LONGLONG_NATIVE
2336 wxLongLongNative b(a.GetHi(), a.GetLo());
2337
2338 wxASSERT_MSG( a == b, _T("conversions failure") );
2339 #else
2340 wxPuts(_T("Can't do it without native long long type, test skipped."));
2341
2342 return;
2343 #endif // wxUSE_LONGLONG_NATIVE
2344
2345 if ( !(nTested % 1000) )
2346 {
2347 putchar('.');
2348 fflush(stdout);
2349 }
2350
2351 nTested++;
2352 }
2353
2354 wxPuts(_T(" done!"));
2355 }
2356
2357 static void TestMultiplication()
2358 {
2359 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2360
2361 wxLongLong a, b;
2362 size_t nTested = 0;
2363 for ( size_t n = 0; n < 100000; n++ )
2364 {
2365 a = RAND_LL();
2366 b = RAND_LL();
2367
2368 #if wxUSE_LONGLONG_NATIVE
2369 wxLongLongNative aa(a.GetHi(), a.GetLo());
2370 wxLongLongNative bb(b.GetHi(), b.GetLo());
2371
2372 wxASSERT_MSG( a*b == aa*bb, _T("multiplication failure") );
2373 #else // !wxUSE_LONGLONG_NATIVE
2374 wxPuts(_T("Can't do it without native long long type, test skipped."));
2375
2376 return;
2377 #endif // wxUSE_LONGLONG_NATIVE
2378
2379 if ( !(nTested % 1000) )
2380 {
2381 putchar('.');
2382 fflush(stdout);
2383 }
2384
2385 nTested++;
2386 }
2387
2388 wxPuts(_T(" done!"));
2389 }
2390
2391 static void TestDivision()
2392 {
2393 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2394
2395 wxLongLong q, r;
2396 size_t nTested = 0;
2397 for ( size_t n = 0; n < 100000; n++ )
2398 {
2399 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2400 // multiplication will not overflow)
2401 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
2402
2403 // get a random (but non null) long (not wxLongLong for now) to divide
2404 // it with
2405 long l;
2406 do
2407 {
2408 l = rand();
2409 }
2410 while ( !l );
2411
2412 q = ll / l;
2413 r = ll % l;
2414
2415 #if wxUSE_LONGLONG_NATIVE
2416 wxLongLongNative m(ll.GetHi(), ll.GetLo());
2417
2418 wxLongLongNative p = m / l, s = m % l;
2419 wxASSERT_MSG( q == p && r == s, _T("division failure") );
2420 #else // !wxUSE_LONGLONG_NATIVE
2421 // verify the result
2422 wxASSERT_MSG( ll == q*l + r, "division failure" );
2423 #endif // wxUSE_LONGLONG_NATIVE
2424
2425 if ( !(nTested % 1000) )
2426 {
2427 putchar('.');
2428 fflush(stdout);
2429 }
2430
2431 nTested++;
2432 }
2433
2434 wxPuts(_T(" done!"));
2435 }
2436
2437 static void TestAddition()
2438 {
2439 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2440
2441 wxLongLong a, b, c;
2442 size_t nTested = 0;
2443 for ( size_t n = 0; n < 100000; n++ )
2444 {
2445 a = RAND_LL();
2446 b = RAND_LL();
2447 c = a + b;
2448
2449 #if wxUSE_LONGLONG_NATIVE
2450 wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
2451 wxLongLongNative(b.GetHi(), b.GetLo()),
2452 _T("addition failure") );
2453 #else // !wxUSE_LONGLONG_NATIVE
2454 wxASSERT_MSG( c - b == a, "addition failure" );
2455 #endif // wxUSE_LONGLONG_NATIVE
2456
2457 if ( !(nTested % 1000) )
2458 {
2459 putchar('.');
2460 fflush(stdout);
2461 }
2462
2463 nTested++;
2464 }
2465
2466 wxPuts(_T(" done!"));
2467 }
2468
2469 static void TestBitOperations()
2470 {
2471 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2472
2473 wxLongLong ll;
2474 size_t nTested = 0;
2475 for ( size_t n = 0; n < 100000; n++ )
2476 {
2477 ll = RAND_LL();
2478
2479 #if wxUSE_LONGLONG_NATIVE
2480 for ( size_t n = 0; n < 33; n++ )
2481 {
2482 }
2483 #else // !wxUSE_LONGLONG_NATIVE
2484 wxPuts(_T("Can't do it without native long long type, test skipped."));
2485
2486 return;
2487 #endif // wxUSE_LONGLONG_NATIVE
2488
2489 if ( !(nTested % 1000) )
2490 {
2491 putchar('.');
2492 fflush(stdout);
2493 }
2494
2495 nTested++;
2496 }
2497
2498 wxPuts(_T(" done!"));
2499 }
2500
2501 static void TestLongLongComparison()
2502 {
2503 #if wxUSE_LONGLONG_WX
2504 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
2505
2506 static const long ls[2] =
2507 {
2508 0x1234,
2509 -0x1234,
2510 };
2511
2512 wxLongLongWx lls[2];
2513 lls[0] = ls[0];
2514 lls[1] = ls[1];
2515
2516 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
2517 {
2518 bool res;
2519
2520 for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
2521 {
2522 res = lls[m] > testLongs[n];
2523 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
2524 ls[m], testLongs[n], res ? "true" : "false",
2525 res == (ls[m] > testLongs[n]) ? "ok" : "ERROR");
2526
2527 res = lls[m] < testLongs[n];
2528 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
2529 ls[m], testLongs[n], res ? "true" : "false",
2530 res == (ls[m] < testLongs[n]) ? "ok" : "ERROR");
2531
2532 res = lls[m] == testLongs[n];
2533 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
2534 ls[m], testLongs[n], res ? "true" : "false",
2535 res == (ls[m] == testLongs[n]) ? "ok" : "ERROR");
2536 }
2537 }
2538 #endif // wxUSE_LONGLONG_WX
2539 }
2540
2541 static void TestLongLongToString()
2542 {
2543 wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
2544
2545 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
2546 {
2547 wxLongLong ll = testLongs[n];
2548 wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str());
2549 }
2550
2551 wxLongLong ll(0x12345678, 0x87654321);
2552 wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str());
2553
2554 ll.Negate();
2555 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str());
2556 }
2557
2558 static void TestLongLongPrintf()
2559 {
2560 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2561
2562 #ifdef wxLongLongFmtSpec
2563 wxLongLong ll = wxLL(0x1234567890abcdef);
2564 wxString s = wxString::Format(_T("%") wxLongLongFmtSpec _T("x"), ll);
2565 wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
2566 s.c_str(), s == _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
2567 #else // !wxLongLongFmtSpec
2568 #error "wxLongLongFmtSpec not defined for this compiler/platform"
2569 #endif
2570 }
2571
2572 #undef MAKE_LL
2573 #undef RAND_LL
2574
2575 #endif // TEST_LONGLONG
2576
2577 // ----------------------------------------------------------------------------
2578 // path list
2579 // ----------------------------------------------------------------------------
2580
2581 #ifdef TEST_PATHLIST
2582
2583 #ifdef __UNIX__
2584 #define CMD_IN_PATH _T("ls")
2585 #else
2586 #define CMD_IN_PATH _T("command.com")
2587 #endif
2588
2589 static void TestPathList()
2590 {
2591 wxPuts(_T("*** Testing wxPathList ***\n"));
2592
2593 wxPathList pathlist;
2594 pathlist.AddEnvList(_T("PATH"));
2595 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
2596 if ( path.empty() )
2597 {
2598 wxPrintf(_T("ERROR: command not found in the path.\n"));
2599 }
2600 else
2601 {
2602 wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
2603 }
2604 }
2605
2606 #endif // TEST_PATHLIST
2607
2608 // ----------------------------------------------------------------------------
2609 // regular expressions
2610 // ----------------------------------------------------------------------------
2611
2612 #ifdef TEST_REGEX
2613
2614 #include "wx/regex.h"
2615
2616 static void TestRegExCompile()
2617 {
2618 wxPuts(_T("*** Testing RE compilation ***\n"));
2619
2620 static struct RegExCompTestData
2621 {
2622 const wxChar *pattern;
2623 bool correct;
2624 } regExCompTestData[] =
2625 {
2626 { _T("foo"), true },
2627 { _T("foo("), false },
2628 { _T("foo(bar"), false },
2629 { _T("foo(bar)"), true },
2630 { _T("foo["), false },
2631 { _T("foo[bar"), false },
2632 { _T("foo[bar]"), true },
2633 { _T("foo{"), true },
2634 { _T("foo{1"), false },
2635 { _T("foo{bar"), true },
2636 { _T("foo{1}"), true },
2637 { _T("foo{1,2}"), true },
2638 { _T("foo{bar}"), true },
2639 { _T("foo*"), true },
2640 { _T("foo**"), false },
2641 { _T("foo+"), true },
2642 { _T("foo++"), false },
2643 { _T("foo?"), true },
2644 { _T("foo??"), false },
2645 { _T("foo?+"), false },
2646 };
2647
2648 wxRegEx re;
2649 for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ )
2650 {
2651 const RegExCompTestData& data = regExCompTestData[n];
2652 bool ok = re.Compile(data.pattern);
2653
2654 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2655 data.pattern,
2656 ok ? _T("") : _T("not "),
2657 ok == data.correct ? _T("ok") : _T("ERROR"));
2658 }
2659 }
2660
2661 static void TestRegExMatch()
2662 {
2663 wxPuts(_T("*** Testing RE matching ***\n"));
2664
2665 static struct RegExMatchTestData
2666 {
2667 const wxChar *pattern;
2668 const wxChar *text;
2669 bool correct;
2670 } regExMatchTestData[] =
2671 {
2672 { _T("foo"), _T("bar"), false },
2673 { _T("foo"), _T("foobar"), true },
2674 { _T("^foo"), _T("foobar"), true },
2675 { _T("^foo"), _T("barfoo"), false },
2676 { _T("bar$"), _T("barbar"), true },
2677 { _T("bar$"), _T("barbar "), false },
2678 };
2679
2680 for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
2681 {
2682 const RegExMatchTestData& data = regExMatchTestData[n];
2683
2684 wxRegEx re(data.pattern);
2685 bool ok = re.Matches(data.text);
2686
2687 wxPrintf(_T("'%s' %s %s (%s)\n"),
2688 data.pattern,
2689 ok ? _T("matches") : _T("doesn't match"),
2690 data.text,
2691 ok == data.correct ? _T("ok") : _T("ERROR"));
2692 }
2693 }
2694
2695 static void TestRegExSubmatch()
2696 {
2697 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2698
2699 wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2700 if ( !re.IsValid() )
2701 {
2702 wxPuts(_T("ERROR: compilation failed."));
2703 return;
2704 }
2705
2706 wxString text = _T("Fri Jul 13 18:37:52 CEST 2001");
2707
2708 if ( !re.Matches(text) )
2709 {
2710 wxPuts(_T("ERROR: match expected."));
2711 }
2712 else
2713 {
2714 wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str());
2715
2716 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2717 re.GetMatch(text, 3).c_str(),
2718 re.GetMatch(text, 2).c_str(),
2719 re.GetMatch(text, 4).c_str(),
2720 re.GetMatch(text, 1).c_str());
2721 }
2722 }
2723
2724 static void TestRegExReplacement()
2725 {
2726 wxPuts(_T("*** Testing RE replacement ***"));
2727
2728 static struct RegExReplTestData
2729 {
2730 const wxChar *text;
2731 const wxChar *repl;
2732 const wxChar *result;
2733 size_t count;
2734 } regExReplTestData[] =
2735 {
2736 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2737 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2738 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2739 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2740 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2741 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2742 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2743 };
2744
2745 const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)");
2746 wxRegEx re(pattern);
2747
2748 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern);
2749
2750 for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ )
2751 {
2752 const RegExReplTestData& data = regExReplTestData[n];
2753
2754 wxString text = data.text;
2755 size_t nRepl = re.Replace(&text, data.repl);
2756
2757 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2758 data.text, data.repl,
2759 nRepl, nRepl == 1 ? _T("") : _T("es"),
2760 text.c_str());
2761 if ( text == data.result && nRepl == data.count )
2762 {
2763 wxPuts(_T("ok)"));
2764 }
2765 else
2766 {
2767 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2768 data.count, data.result);
2769 }
2770 }
2771 }
2772
2773 static void TestRegExInteractive()
2774 {
2775 wxPuts(_T("*** Testing RE interactively ***"));
2776
2777 for ( ;; )
2778 {
2779 wxChar pattern[128];
2780 wxPrintf(_T("\nEnter a pattern: "));
2781 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
2782 break;
2783
2784 // kill the last '\n'
2785 pattern[wxStrlen(pattern) - 1] = 0;
2786
2787 wxRegEx re;
2788 if ( !re.Compile(pattern) )
2789 {
2790 continue;
2791 }
2792
2793 wxChar text[128];
2794 for ( ;; )
2795 {
2796 wxPrintf(_T("Enter text to match: "));
2797 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
2798 break;
2799
2800 // kill the last '\n'
2801 text[wxStrlen(text) - 1] = 0;
2802
2803 if ( !re.Matches(text) )
2804 {
2805 wxPrintf(_T("No match.\n"));
2806 }
2807 else
2808 {
2809 wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
2810
2811 size_t start, len;
2812 for ( size_t n = 1; ; n++ )
2813 {
2814 if ( !re.GetMatch(&start, &len, n) )
2815 {
2816 break;
2817 }
2818
2819 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2820 n, wxString(text + start, len).c_str());
2821 }
2822 }
2823 }
2824 }
2825 }
2826
2827 #endif // TEST_REGEX
2828
2829 // ----------------------------------------------------------------------------
2830 // database
2831 // ----------------------------------------------------------------------------
2832
2833 #if !wxUSE_ODBC
2834 #undef TEST_ODBC
2835 #endif
2836
2837 #ifdef TEST_ODBC
2838
2839 #include <wx/db.h>
2840
2841 static void TestDbOpen()
2842 {
2843 HENV henv;
2844 wxDb db(henv);
2845 }
2846
2847 #endif // TEST_ODBC
2848
2849 // ----------------------------------------------------------------------------
2850 // printf() tests
2851 // ----------------------------------------------------------------------------
2852
2853 /*
2854 NB: this stuff was taken from the glibc test suite and modified to build
2855 in wxWindows: if I read the copyright below properly, this shouldn't
2856 be a problem
2857 */
2858
2859 #ifdef TEST_PRINTF
2860
2861 #ifdef wxTEST_PRINTF
2862 // use our functions from wxchar.cpp
2863 #undef wxPrintf
2864 #undef wxSprintf
2865
2866 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2867 // in the tests below
2868 int wxPrintf( const wxChar *format, ... );
2869 int wxSprintf( wxChar *str, const wxChar *format, ... );
2870 #endif
2871
2872 #include "wx/longlong.h"
2873
2874 #include <float.h>
2875
2876 static void rfg1 (void);
2877 static void rfg2 (void);
2878
2879
2880 static void
2881 fmtchk (const wxChar *fmt)
2882 {
2883 (void) wxPrintf(_T("%s:\t`"), fmt);
2884 (void) wxPrintf(fmt, 0x12);
2885 (void) wxPrintf(_T("'\n"));
2886 }
2887
2888 static void
2889 fmtst1chk (const wxChar *fmt)
2890 {
2891 (void) wxPrintf(_T("%s:\t`"), fmt);
2892 (void) wxPrintf(fmt, 4, 0x12);
2893 (void) wxPrintf(_T("'\n"));
2894 }
2895
2896 static void
2897 fmtst2chk (const wxChar *fmt)
2898 {
2899 (void) wxPrintf(_T("%s:\t`"), fmt);
2900 (void) wxPrintf(fmt, 4, 4, 0x12);
2901 (void) wxPrintf(_T("'\n"));
2902 }
2903
2904 /* This page is covered by the following copyright: */
2905
2906 /* (C) Copyright C E Chew
2907 *
2908 * Feel free to copy, use and distribute this software provided:
2909 *
2910 * 1. you do not pretend that you wrote it
2911 * 2. you leave this copyright notice intact.
2912 */
2913
2914 /*
2915 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2916 */
2917
2918 #define DEC -123
2919 #define INT 255
2920 #define UNS (~0)
2921
2922 /* Formatted Output Test
2923 *
2924 * This exercises the output formatting code.
2925 */
2926
2927 static void
2928 fp_test (void)
2929 {
2930 int i, j, k, l;
2931 wxChar buf[7];
2932 wxChar *prefix = buf;
2933 wxChar tp[20];
2934
2935 wxPuts(_T("\nFormatted output test"));
2936 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2937 wxStrcpy(prefix, _T("%"));
2938 for (i = 0; i < 2; i++) {
2939 for (j = 0; j < 2; j++) {
2940 for (k = 0; k < 2; k++) {
2941 for (l = 0; l < 2; l++) {
2942 wxStrcpy(prefix, _T("%"));
2943 if (i == 0) wxStrcat(prefix, _T("-"));
2944 if (j == 0) wxStrcat(prefix, _T("+"));
2945 if (k == 0) wxStrcat(prefix, _T("#"));
2946 if (l == 0) wxStrcat(prefix, _T("0"));
2947 wxPrintf(_T("%5s |"), prefix);
2948 wxStrcpy(tp, prefix);
2949 wxStrcat(tp, _T("6d |"));
2950 wxPrintf(tp, DEC);
2951 wxStrcpy(tp, prefix);
2952 wxStrcat(tp, _T("6o |"));
2953 wxPrintf(tp, INT);
2954 wxStrcpy(tp, prefix);
2955 wxStrcat(tp, _T("6x |"));
2956 wxPrintf(tp, INT);
2957 wxStrcpy(tp, prefix);
2958 wxStrcat(tp, _T("6X |"));
2959 wxPrintf(tp, INT);
2960 wxStrcpy(tp, prefix);
2961 wxStrcat(tp, _T("6u |"));
2962 wxPrintf(tp, UNS);
2963 wxPrintf(_T("\n"));
2964 }
2965 }
2966 }
2967 }
2968 wxPrintf(_T("%10s\n"), (wxChar *) NULL);
2969 wxPrintf(_T("%-10s\n"), (wxChar *) NULL);
2970 }
2971
2972 static void TestPrintf()
2973 {
2974 static wxChar shortstr[] = _T("Hi, Z.");
2975 static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
2976 I am ready for my first lesson today.");
2977 int result = 0;
2978
2979 fmtchk(_T("%.4x"));
2980 fmtchk(_T("%04x"));
2981 fmtchk(_T("%4.4x"));
2982 fmtchk(_T("%04.4x"));
2983 fmtchk(_T("%4.3x"));
2984 fmtchk(_T("%04.3x"));
2985
2986 fmtst1chk(_T("%.*x"));
2987 fmtst1chk(_T("%0*x"));
2988 fmtst2chk(_T("%*.*x"));
2989 fmtst2chk(_T("%0*.*x"));
2990
2991 wxPrintf(_T("bad format:\t\"%b\"\n"));
2992 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
2993
2994 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
2995 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
2996 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
2997 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
2998 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
2999 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
3000 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
3001 wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
3002 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
3003 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
3004
3005 wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr);
3006 wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr);
3007 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
3008 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
3009 wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar *)NULL);
3010 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
3011
3012 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
3013 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
3014 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
3015 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
3016 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
3017 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
3018 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
3019 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
3020 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
3021 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
3022 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
3023 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
3024
3025 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
3026 wxPrintf (_T(" %6.5f\n"), .1);
3027 wxPrintf (_T("x%5.4fx\n"), .5);
3028
3029 wxPrintf (_T("%#03x\n"), 1);
3030
3031 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
3032
3033 {
3034 double d = FLT_MIN;
3035 int niter = 17;
3036
3037 while (niter-- != 0)
3038 wxPrintf (_T("%.17e\n"), d / 2);
3039 fflush (stdout);
3040 }
3041
3042 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
3043
3044 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
3045 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
3046 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
3047 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
3048 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
3049 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
3050 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
3051 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
3052 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
3053 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
3054 #undef FORMAT
3055
3056 {
3057 wxChar buf[20];
3058 int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
3059
3060 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
3061 rc, WXSIZEOF(buf), buf);
3062 #if 0
3063 wxChar buf2[512];
3064 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
3065 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
3066 #endif
3067 }
3068
3069 fp_test ();
3070
3071 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
3072 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
3073 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
3074 wxPrintf (_T("%g should be 123.456\n"), 123.456);
3075 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
3076 wxPrintf (_T("%g should be 10\n"), 10.0);
3077 wxPrintf (_T("%g should be 0.02\n"), 0.02);
3078
3079 {
3080 double x=1.0;
3081 wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
3082 }
3083
3084 {
3085 wxChar buf[200];
3086
3087 wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
3088
3089 result |= wxStrcmp (buf,
3090 _T("onetwo three "));
3091
3092 wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
3093 }
3094
3095 #ifdef wxLongLong_t
3096 {
3097 wxChar buf[200];
3098
3099 wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
3100 #if 0
3101 // for some reason below line fails under Borland
3102 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
3103 #endif
3104
3105 if (wxStrcmp (buf, _T("40000000000")) != 0)
3106 {
3107 result = 1;
3108 wxPuts (_T("\tFAILED"));
3109 }
3110 wxPuts (_T(""));
3111 }
3112 #endif // wxLongLong_t
3113
3114 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
3115 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
3116
3117 wxPuts (_T("--- Should be no further output. ---"));
3118 rfg1 ();
3119 rfg2 ();
3120
3121 #if 0
3122 {
3123 wxChar bytes[7];
3124 wxChar buf[20];
3125
3126 memset (bytes, '\xff', sizeof bytes);
3127 wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
3128 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
3129 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
3130 {
3131 wxPuts (_T("%hhn overwrite more bytes"));
3132 result = 1;
3133 }
3134 if (bytes[3] != 3)
3135 {
3136 wxPuts (_T("%hhn wrote incorrect value"));
3137 result = 1;
3138 }
3139 }
3140 #endif
3141 }
3142
3143 static void
3144 rfg1 (void)
3145 {
3146 wxChar buf[100];
3147
3148 wxSprintf (buf, _T("%5.s"), _T("xyz"));
3149 if (wxStrcmp (buf, _T(" ")) != 0)
3150 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
3151 wxSprintf (buf, _T("%5.f"), 33.3);
3152 if (wxStrcmp (buf, _T(" 33")) != 0)
3153 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
3154 wxSprintf (buf, _T("%8.e"), 33.3e7);
3155 if (wxStrcmp (buf, _T(" 3e+08")) != 0)
3156 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
3157 wxSprintf (buf, _T("%8.E"), 33.3e7);
3158 if (wxStrcmp (buf, _T(" 3E+08")) != 0)
3159 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
3160 wxSprintf (buf, _T("%.g"), 33.3);
3161 if (wxStrcmp (buf, _T("3e+01")) != 0)
3162 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
3163 wxSprintf (buf, _T("%.G"), 33.3);
3164 if (wxStrcmp (buf, _T("3E+01")) != 0)
3165 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
3166 }
3167
3168 static void
3169 rfg2 (void)
3170 {
3171 int prec;
3172 wxChar buf[100];
3173
3174 prec = 0;
3175 wxSprintf (buf, _T("%.*g"), prec, 3.3);
3176 if (wxStrcmp (buf, _T("3")) != 0)
3177 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
3178 prec = 0;
3179 wxSprintf (buf, _T("%.*G"), prec, 3.3);
3180 if (wxStrcmp (buf, _T("3")) != 0)
3181 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
3182 prec = 0;
3183 wxSprintf (buf, _T("%7.*G"), prec, 3.33);
3184 if (wxStrcmp (buf, _T(" 3")) != 0)
3185 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
3186 prec = 3;
3187 wxSprintf (buf, _T("%04.*o"), prec, 33);
3188 if (wxStrcmp (buf, _T(" 041")) != 0)
3189 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
3190 prec = 7;
3191 wxSprintf (buf, _T("%09.*u"), prec, 33);
3192 if (wxStrcmp (buf, _T(" 0000033")) != 0)
3193 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
3194 prec = 3;
3195 wxSprintf (buf, _T("%04.*x"), prec, 33);
3196 if (wxStrcmp (buf, _T(" 021")) != 0)
3197 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
3198 prec = 3;
3199 wxSprintf (buf, _T("%04.*X"), prec, 33);
3200 if (wxStrcmp (buf, _T(" 021")) != 0)
3201 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
3202 }
3203
3204 #endif // TEST_PRINTF
3205
3206 // ----------------------------------------------------------------------------
3207 // registry and related stuff
3208 // ----------------------------------------------------------------------------
3209
3210 // this is for MSW only
3211 #ifndef __WXMSW__
3212 #undef TEST_REGCONF
3213 #undef TEST_REGISTRY
3214 #endif
3215
3216 #ifdef TEST_REGCONF
3217
3218 #include "wx/confbase.h"
3219 #include "wx/msw/regconf.h"
3220
3221 static void TestRegConfWrite()
3222 {
3223 wxConfig *config = new wxConfig("myapp");
3224 config->SetPath("/group1");
3225 config->Write("entry1", "foo");
3226 config->SetPath("/group2");
3227 config->Write("entry1", "bar");
3228 }
3229
3230 static void TestRegConfRead()
3231 {
3232 wxConfig *config = new wxConfig("myapp");
3233
3234 wxString str;
3235 long dummy;
3236 config->SetPath("/");
3237 puts("Enumerating / subgroups:");
3238 bool bCont = config->GetFirstGroup(str, dummy);
3239 while(bCont)
3240 {
3241 puts(str);
3242 bCont = config->GetNextGroup(str, dummy);
3243 }
3244 }
3245
3246 #endif // TEST_REGCONF
3247
3248 #ifdef TEST_REGISTRY
3249
3250 #include "wx/msw/registry.h"
3251
3252 // I chose this one because I liked its name, but it probably only exists under
3253 // NT
3254 static const wxChar *TESTKEY =
3255 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
3256
3257 static void TestRegistryRead()
3258 {
3259 wxPuts(_T("*** testing registry reading ***"));
3260
3261 wxRegKey key(TESTKEY);
3262 wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
3263 if ( !key.Open() )
3264 {
3265 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
3266
3267 return;
3268 }
3269
3270 size_t nSubKeys, nValues;
3271 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
3272 {
3273 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
3274 }
3275
3276 wxPrintf(_T("Enumerating values:\n"));
3277
3278 long dummy;
3279 wxString value;
3280 bool cont = key.GetFirstValue(value, dummy);
3281 while ( cont )
3282 {
3283 wxPrintf(_T("Value '%s': type "), value.c_str());
3284 switch ( key.GetValueType(value) )
3285 {
3286 case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
3287 case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
3288 case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
3289 case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
3290 case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
3291 case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
3292 default: wxPrintf(_T("other (unknown)")); break;
3293 }
3294
3295 wxPrintf(_T(", value = "));
3296 if ( key.IsNumericValue(value) )
3297 {
3298 long val;
3299 key.QueryValue(value, &val);
3300 wxPrintf(_T("%ld"), val);
3301 }
3302 else // string
3303 {
3304 wxString val;
3305 key.QueryValue(value, val);
3306 wxPrintf(_T("'%s'"), val.c_str());
3307
3308 key.QueryRawValue(value, val);
3309 wxPrintf(_T(" (raw value '%s')"), val.c_str());
3310 }
3311
3312 putchar('\n');
3313
3314 cont = key.GetNextValue(value, dummy);
3315 }
3316 }
3317
3318 static void TestRegistryAssociation()
3319 {
3320 /*
3321 The second call to deleteself genertaes an error message, with a
3322 messagebox saying .flo is crucial to system operation, while the .ddf
3323 call also fails, but with no error message
3324 */
3325
3326 wxRegKey key;
3327
3328 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3329 key.Create();
3330 key = _T("ddxf_auto_file") ;
3331 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3332 key.Create();
3333 key = _T("ddxf_auto_file") ;
3334 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3335 key.Create();
3336 key = _T("program,0") ;
3337 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3338 key.Create();
3339 key = _T("program \"%1\"") ;
3340
3341 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3342 key.DeleteSelf();
3343 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3344 key.DeleteSelf();
3345 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3346 key.DeleteSelf();
3347 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3348 key.DeleteSelf();
3349 }
3350
3351 #endif // TEST_REGISTRY
3352
3353 // ----------------------------------------------------------------------------
3354 // scope guard
3355 // ----------------------------------------------------------------------------
3356
3357 #ifdef TEST_SCOPEGUARD
3358
3359 #include "wx/scopeguard.h"
3360
3361 static void function0() { puts("function0()"); }
3362 static void function1(int n) { printf("function1(%d)\n", n); }
3363 static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
3364
3365 struct Object
3366 {
3367 void method0() { printf("method0()\n"); }
3368 void method1(int n) { printf("method1(%d)\n", n); }
3369 void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
3370 };
3371
3372 static void TestScopeGuard()
3373 {
3374 wxON_BLOCK_EXIT0(function0);
3375 wxON_BLOCK_EXIT1(function1, 17);
3376 wxON_BLOCK_EXIT2(function2, 3.14, 'p');
3377
3378 Object obj;
3379 wxON_BLOCK_EXIT_OBJ0(obj, &Object::method0);
3380 wxON_BLOCK_EXIT_OBJ1(obj, &Object::method1, 7);
3381 wxON_BLOCK_EXIT_OBJ2(obj, &Object::method2, 2.71, 'e');
3382
3383 wxScopeGuard dismissed = wxMakeGuard(function0);
3384 dismissed.Dismiss();
3385 }
3386
3387 #endif
3388
3389 // ----------------------------------------------------------------------------
3390 // sockets
3391 // ----------------------------------------------------------------------------
3392
3393 #ifdef TEST_SOCKETS
3394
3395 #include "wx/socket.h"
3396 #include "wx/protocol/protocol.h"
3397 #include "wx/protocol/http.h"
3398
3399 static void TestSocketServer()
3400 {
3401 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3402
3403 static const int PORT = 3000;
3404
3405 wxIPV4address addr;
3406 addr.Service(PORT);
3407
3408 wxSocketServer *server = new wxSocketServer(addr);
3409 if ( !server->Ok() )
3410 {
3411 wxPuts(_T("ERROR: failed to bind"));
3412
3413 return;
3414 }
3415
3416 bool quit = false;
3417 while ( !quit )
3418 {
3419 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
3420
3421 wxSocketBase *socket = server->Accept();
3422 if ( !socket )
3423 {
3424 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3425 break;
3426 }
3427
3428 wxPuts(_T("Server: got a client."));
3429
3430 server->SetTimeout(60); // 1 min
3431
3432 bool close = false;
3433 while ( !close && socket->IsConnected() )
3434 {
3435 wxString s;
3436 wxChar ch = _T('\0');
3437 for ( ;; )
3438 {
3439 if ( socket->Read(&ch, sizeof(ch)).Error() )
3440 {
3441 // don't log error if the client just close the connection
3442 if ( socket->IsConnected() )
3443 {
3444 wxPuts(_T("ERROR: in wxSocket::Read."));
3445 }
3446
3447 break;
3448 }
3449
3450 if ( ch == '\r' )
3451 continue;
3452
3453 if ( ch == '\n' )
3454 break;
3455
3456 s += ch;
3457 }
3458
3459 if ( ch != '\n' )
3460 {
3461 break;
3462 }
3463
3464 wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
3465 if ( s == _T("close") )
3466 {
3467 wxPuts(_T("Closing connection"));
3468
3469 close = true;
3470 }
3471 else if ( s == _T("quit") )
3472 {
3473 close =
3474 quit = true;
3475
3476 wxPuts(_T("Shutting down the server"));
3477 }
3478 else // not a special command
3479 {
3480 socket->Write(s.MakeUpper().c_str(), s.length());
3481 socket->Write("\r\n", 2);
3482 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
3483 }
3484 }
3485
3486 if ( !close )
3487 {
3488 wxPuts(_T("Server: lost a client unexpectedly."));
3489 }
3490
3491 socket->Destroy();
3492 }
3493
3494 // same as "delete server" but is consistent with GUI programs
3495 server->Destroy();
3496 }
3497
3498 static void TestSocketClient()
3499 {
3500 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3501
3502 static const wxChar *hostname = _T("www.wxwindows.org");
3503
3504 wxIPV4address addr;
3505 addr.Hostname(hostname);
3506 addr.Service(80);
3507
3508 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
3509
3510 wxSocketClient client;
3511 if ( !client.Connect(addr) )
3512 {
3513 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
3514 }
3515 else
3516 {
3517 wxPrintf(_T("--- Connected to %s:%u...\n"),
3518 addr.Hostname().c_str(), addr.Service());
3519
3520 wxChar buf[8192];
3521
3522 // could use simply "GET" here I suppose
3523 wxString cmdGet =
3524 wxString::Format(_T("GET http://%s/\r\n"), hostname);
3525 client.Write(cmdGet, cmdGet.length());
3526 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3527 MakePrintable(cmdGet).c_str());
3528 client.Read(buf, WXSIZEOF(buf));
3529 wxPrintf(_T("--- Server replied:\n%s"), buf);
3530 }
3531 }
3532
3533 #endif // TEST_SOCKETS
3534
3535 // ----------------------------------------------------------------------------
3536 // FTP
3537 // ----------------------------------------------------------------------------
3538
3539 #ifdef TEST_FTP
3540
3541 #include "wx/protocol/ftp.h"
3542
3543 static wxFTP ftp;
3544
3545 #define FTP_ANONYMOUS
3546
3547 #ifdef FTP_ANONYMOUS
3548 static const wxChar *directory = _T("/pub");
3549 static const wxChar *filename = _T("welcome.msg");
3550 #else
3551 static const wxChar *directory = _T("/etc");
3552 static const wxChar *filename = _T("issue");
3553 #endif
3554
3555 static bool TestFtpConnect()
3556 {
3557 wxPuts(_T("*** Testing FTP connect ***"));
3558
3559 #ifdef FTP_ANONYMOUS
3560 static const wxChar *hostname = _T("ftp.wxwindows.org");
3561
3562 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
3563 #else // !FTP_ANONYMOUS
3564 static const wxChar *hostname = "localhost";
3565
3566 wxChar user[256];
3567 wxFgets(user, WXSIZEOF(user), stdin);
3568 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
3569 ftp.SetUser(user);
3570
3571 wxChar password[256];
3572 wxPrintf(_T("Password for %s: "), password);
3573 wxFgets(password, WXSIZEOF(password), stdin);
3574 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
3575 ftp.SetPassword(password);
3576
3577 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
3578 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3579
3580 if ( !ftp.Connect(hostname) )
3581 {
3582 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
3583
3584 return false;
3585 }
3586 else
3587 {
3588 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3589 hostname, ftp.Pwd().c_str());
3590 }
3591
3592 return true;
3593 }
3594
3595 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3596 static void TestFtpWuFtpd()
3597 {
3598 wxFTP ftp;
3599 static const wxChar *hostname = _T("ftp.eudora.com");
3600 if ( !ftp.Connect(hostname) )
3601 {
3602 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
3603 }
3604 else
3605 {
3606 static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
3607 wxInputStream *in = ftp.GetInputStream(filename);
3608 if ( !in )
3609 {
3610 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
3611 }
3612 else
3613 {
3614 size_t size = in->GetSize();
3615 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
3616
3617 wxChar *data = new wxChar[size];
3618 if ( !in->Read(data, size) )
3619 {
3620 wxPuts(_T("ERROR: read error"));
3621 }
3622 else
3623 {
3624 wxPrintf(_T("Successfully retrieved the file.\n"));
3625 }
3626
3627 delete [] data;
3628 delete in;
3629 }
3630 }
3631 }
3632
3633 static void TestFtpList()
3634 {
3635 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3636
3637 // test CWD
3638 if ( !ftp.ChDir(directory) )
3639 {
3640 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
3641 }
3642
3643 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
3644
3645 // test NLIST and LIST
3646 wxArrayString files;
3647 if ( !ftp.GetFilesList(files) )
3648 {
3649 wxPuts(_T("ERROR: failed to get NLIST of files"));
3650 }
3651 else
3652 {
3653 wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
3654 size_t count = files.GetCount();
3655 for ( size_t n = 0; n < count; n++ )
3656 {
3657 wxPrintf(_T("\t%s\n"), files[n].c_str());
3658 }
3659 wxPuts(_T("End of the file list"));
3660 }
3661
3662 if ( !ftp.GetDirList(files) )
3663 {
3664 wxPuts(_T("ERROR: failed to get LIST of files"));
3665 }
3666 else
3667 {
3668 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
3669 size_t count = files.GetCount();
3670 for ( size_t n = 0; n < count; n++ )
3671 {
3672 wxPrintf(_T("\t%s\n"), files[n].c_str());
3673 }
3674 wxPuts(_T("End of the file list"));
3675 }
3676
3677 if ( !ftp.ChDir(_T("..")) )
3678 {
3679 wxPuts(_T("ERROR: failed to cd to .."));
3680 }
3681
3682 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
3683 }
3684
3685 static void TestFtpDownload()
3686 {
3687 wxPuts(_T("*** Testing wxFTP download ***\n"));
3688
3689 // test RETR
3690 wxInputStream *in = ftp.GetInputStream(filename);
3691 if ( !in )
3692 {
3693 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
3694 }
3695 else
3696 {
3697 size_t size = in->GetSize();
3698 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
3699 fflush(stdout);
3700
3701 wxChar *data = new wxChar[size];
3702 if ( !in->Read(data, size) )
3703 {
3704 wxPuts(_T("ERROR: read error"));
3705 }
3706 else
3707 {
3708 wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
3709 }
3710
3711 delete [] data;
3712 delete in;
3713 }
3714 }
3715
3716 static void TestFtpFileSize()
3717 {
3718 wxPuts(_T("*** Testing FTP SIZE command ***"));
3719
3720 if ( !ftp.ChDir(directory) )
3721 {
3722 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
3723 }
3724
3725 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
3726
3727 if ( ftp.FileExists(filename) )
3728 {
3729 int size = ftp.GetFileSize(filename);
3730 if ( size == -1 )
3731 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
3732 else
3733 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
3734 }
3735 else
3736 {
3737 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
3738 }
3739 }
3740
3741 static void TestFtpMisc()
3742 {
3743 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3744
3745 if ( ftp.SendCommand(_T("STAT")) != '2' )
3746 {
3747 wxPuts(_T("ERROR: STAT failed"));
3748 }
3749 else
3750 {
3751 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
3752 }
3753
3754 if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
3755 {
3756 wxPuts(_T("ERROR: HELP SITE failed"));
3757 }
3758 else
3759 {
3760 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3761 ftp.GetLastResult().c_str());
3762 }
3763 }
3764
3765 static void TestFtpInteractive()
3766 {
3767 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3768
3769 wxChar buf[128];
3770
3771 for ( ;; )
3772 {
3773 wxPrintf(_T("Enter FTP command: "));
3774 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
3775 break;
3776
3777 // kill the last '\n'
3778 buf[wxStrlen(buf) - 1] = 0;
3779
3780 // special handling of LIST and NLST as they require data connection
3781 wxString start(buf, 4);
3782 start.MakeUpper();
3783 if ( start == _T("LIST") || start == _T("NLST") )
3784 {
3785 wxString wildcard;
3786 if ( wxStrlen(buf) > 4 )
3787 wildcard = buf + 5;
3788
3789 wxArrayString files;
3790 if ( !ftp.GetList(files, wildcard, start == _T("LIST")) )
3791 {
3792 wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
3793 }
3794 else
3795 {
3796 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3797 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
3798 size_t count = files.GetCount();
3799 for ( size_t n = 0; n < count; n++ )
3800 {
3801 wxPrintf(_T("\t%s\n"), files[n].c_str());
3802 }
3803 wxPuts(_T("--- End of the file list"));
3804 }
3805 }
3806 else // !list
3807 {
3808 wxChar ch = ftp.SendCommand(buf);
3809 wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
3810 if ( ch )
3811 {
3812 wxPrintf(_T(" (return code %c)"), ch);
3813 }
3814
3815 wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
3816 }
3817 }
3818
3819 wxPuts(_T("\n*** done ***"));
3820 }
3821
3822 static void TestFtpUpload()
3823 {
3824 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3825
3826 // upload a file
3827 static const wxChar *file1 = _T("test1");
3828 static const wxChar *file2 = _T("test2");
3829 wxOutputStream *out = ftp.GetOutputStream(file1);
3830 if ( out )
3831 {
3832 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
3833 out->Write("First hello", 11);
3834 delete out;
3835 }
3836
3837 // send a command to check the remote file
3838 if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' )
3839 {
3840 wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
3841 }
3842 else
3843 {
3844 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3845 file1, ftp.GetLastResult().c_str());
3846 }
3847
3848 out = ftp.GetOutputStream(file2);
3849 if ( out )
3850 {
3851 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
3852 out->Write("Second hello", 12);
3853 delete out;
3854 }
3855 }
3856
3857 #endif // TEST_FTP
3858
3859 // ----------------------------------------------------------------------------
3860 // streams
3861 // ----------------------------------------------------------------------------
3862
3863 #ifdef TEST_STREAMS
3864
3865 #include "wx/wfstream.h"
3866 #include "wx/mstream.h"
3867
3868 static void TestFileStream()
3869 {
3870 wxPuts(_T("*** Testing wxFileInputStream ***"));
3871
3872 static const wxChar *filename = _T("testdata.fs");
3873 {
3874 wxFileOutputStream fsOut(filename);
3875 fsOut.Write("foo", 3);
3876 }
3877
3878 wxFileInputStream fsIn(filename);
3879 wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
3880 while ( !fsIn.Eof() )
3881 {
3882 putchar(fsIn.GetC());
3883 }
3884
3885 if ( !wxRemoveFile(filename) )
3886 {
3887 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename);
3888 }
3889
3890 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3891 }
3892
3893 static void TestMemoryStream()
3894 {
3895 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3896
3897 wxMemoryOutputStream memOutStream;
3898 wxPrintf(_T("Initially out stream offset: %lu\n"),
3899 (unsigned long)memOutStream.TellO());
3900
3901 for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
3902 {
3903 memOutStream.PutC(*p);
3904 }
3905
3906 wxPrintf(_T("Final out stream offset: %lu\n"),
3907 (unsigned long)memOutStream.TellO());
3908
3909 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3910
3911 wxChar buf[1024];
3912 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
3913
3914 wxMemoryInputStream memInpStream(buf, len);
3915 wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
3916 while ( !memInpStream.Eof() )
3917 {
3918 putchar(memInpStream.GetC());
3919 }
3920
3921 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3922 }
3923
3924 #endif // TEST_STREAMS
3925
3926 // ----------------------------------------------------------------------------
3927 // timers
3928 // ----------------------------------------------------------------------------
3929
3930 #ifdef TEST_TIMER
3931
3932 #include "wx/timer.h"
3933 #include "wx/utils.h"
3934
3935 static void TestStopWatch()
3936 {
3937 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3938
3939 wxStopWatch sw;
3940 sw.Pause();
3941 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3942 fflush(stdout);
3943 wxSleep(2);
3944 wxPrintf(_T("\t%ldms\n"), sw.Time());
3945
3946 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3947 fflush(stdout);
3948 sw.Resume();
3949 wxSleep(3);
3950 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
3951
3952 sw.Pause();
3953 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3954 fflush(stdout);
3955 wxSleep(2);
3956 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
3957
3958 sw.Resume();
3959 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3960 fflush(stdout);
3961 wxSleep(2);
3962 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
3963
3964 wxStopWatch sw2;
3965 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
3966 for ( size_t n = 0; n < 70; n++ )
3967 {
3968 sw2.Start();
3969
3970 for ( size_t m = 0; m < 100000; m++ )
3971 {
3972 if ( sw.Time() < 0 || sw2.Time() < 0 )
3973 {
3974 wxPuts(_T("\ntime is negative - ERROR!"));
3975 }
3976 }
3977
3978 putchar('.');
3979 fflush(stdout);
3980 }
3981
3982 wxPuts(_T(", ok."));
3983 }
3984
3985 #endif // TEST_TIMER
3986
3987 // ----------------------------------------------------------------------------
3988 // vCard support
3989 // ----------------------------------------------------------------------------
3990
3991 #ifdef TEST_VCARD
3992
3993 #include "wx/vcard.h"
3994
3995 static void DumpVObject(size_t level, const wxVCardObject& vcard)
3996 {
3997 void *cookie;
3998 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
3999 while ( vcObj )
4000 {
4001 wxPrintf(_T("%s%s"),
4002 wxString(_T('\t'), level).c_str(),
4003 vcObj->GetName().c_str());
4004
4005 wxString value;
4006 switch ( vcObj->GetType() )
4007 {
4008 case wxVCardObject::String:
4009 case wxVCardObject::UString:
4010 {
4011 wxString val;
4012 vcObj->GetValue(&val);
4013 value << _T('"') << val << _T('"');
4014 }
4015 break;
4016
4017 case wxVCardObject::Int:
4018 {
4019 unsigned int i;
4020 vcObj->GetValue(&i);
4021 value.Printf(_T("%u"), i);
4022 }
4023 break;
4024
4025 case wxVCardObject::Long:
4026 {
4027 unsigned long l;
4028 vcObj->GetValue(&l);
4029 value.Printf(_T("%lu"), l);
4030 }
4031 break;
4032
4033 case wxVCardObject::None:
4034 break;
4035
4036 case wxVCardObject::Object:
4037 value = _T("<node>");
4038 break;
4039
4040 default:
4041 value = _T("<unknown value type>");
4042 }
4043
4044 if ( !!value )
4045 wxPrintf(_T(" = %s"), value.c_str());
4046 putchar('\n');
4047
4048 DumpVObject(level + 1, *vcObj);
4049
4050 delete vcObj;
4051 vcObj = vcard.GetNextProp(&cookie);
4052 }
4053 }
4054
4055 static void DumpVCardAddresses(const wxVCard& vcard)
4056 {
4057 wxPuts(_T("\nShowing all addresses from vCard:\n"));
4058
4059 size_t nAdr = 0;
4060 void *cookie;
4061 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
4062 while ( addr )
4063 {
4064 wxString flagsStr;
4065 int flags = addr->GetFlags();
4066 if ( flags & wxVCardAddress::Domestic )
4067 {
4068 flagsStr << _T("domestic ");
4069 }
4070 if ( flags & wxVCardAddress::Intl )
4071 {
4072 flagsStr << _T("international ");
4073 }
4074 if ( flags & wxVCardAddress::Postal )
4075 {
4076 flagsStr << _T("postal ");
4077 }
4078 if ( flags & wxVCardAddress::Parcel )
4079 {
4080 flagsStr << _T("parcel ");
4081 }
4082 if ( flags & wxVCardAddress::Home )
4083 {
4084 flagsStr << _T("home ");
4085 }
4086 if ( flags & wxVCardAddress::Work )
4087 {
4088 flagsStr << _T("work ");
4089 }
4090
4091 wxPrintf(_T("Address %u:\n")
4092 "\tflags = %s\n"
4093 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
4094 ++nAdr,
4095 flagsStr.c_str(),
4096 addr->GetPostOffice().c_str(),
4097 addr->GetExtAddress().c_str(),
4098 addr->GetStreet().c_str(),
4099 addr->GetLocality().c_str(),
4100 addr->GetRegion().c_str(),
4101 addr->GetPostalCode().c_str(),
4102 addr->GetCountry().c_str()
4103 );
4104
4105 delete addr;
4106 addr = vcard.GetNextAddress(&cookie);
4107 }
4108 }
4109
4110 static void DumpVCardPhoneNumbers(const wxVCard& vcard)
4111 {
4112 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
4113
4114 size_t nPhone = 0;
4115 void *cookie;
4116 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
4117 while ( phone )
4118 {
4119 wxString flagsStr;
4120 int flags = phone->GetFlags();
4121 if ( flags & wxVCardPhoneNumber::Voice )
4122 {
4123 flagsStr << _T("voice ");
4124 }
4125 if ( flags & wxVCardPhoneNumber::Fax )
4126 {
4127 flagsStr << _T("fax ");
4128 }
4129 if ( flags & wxVCardPhoneNumber::Cellular )
4130 {
4131 flagsStr << _T("cellular ");
4132 }
4133 if ( flags & wxVCardPhoneNumber::Modem )
4134 {
4135 flagsStr << _T("modem ");
4136 }
4137 if ( flags & wxVCardPhoneNumber::Home )
4138 {
4139 flagsStr << _T("home ");
4140 }
4141 if ( flags & wxVCardPhoneNumber::Work )
4142 {
4143 flagsStr << _T("work ");
4144 }
4145
4146 wxPrintf(_T("Phone number %u:\n")
4147 "\tflags = %s\n"
4148 "\tvalue = %s\n",
4149 ++nPhone,
4150 flagsStr.c_str(),
4151 phone->GetNumber().c_str()
4152 );
4153
4154 delete phone;
4155 phone = vcard.GetNextPhoneNumber(&cookie);
4156 }
4157 }
4158
4159 static void TestVCardRead()
4160 {
4161 wxPuts(_T("*** Testing wxVCard reading ***\n"));
4162
4163 wxVCard vcard(_T("vcard.vcf"));
4164 if ( !vcard.IsOk() )
4165 {
4166 wxPuts(_T("ERROR: couldn't load vCard."));
4167 }
4168 else
4169 {
4170 // read individual vCard properties
4171 wxVCardObject *vcObj = vcard.GetProperty("FN");
4172 wxString value;
4173 if ( vcObj )
4174 {
4175 vcObj->GetValue(&value);
4176 delete vcObj;
4177 }
4178 else
4179 {
4180 value = _T("<none>");
4181 }
4182
4183 wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str());
4184
4185
4186 if ( !vcard.GetFullName(&value) )
4187 {
4188 value = _T("<none>");
4189 }
4190
4191 wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str());
4192
4193 // now show how to deal with multiply occuring properties
4194 DumpVCardAddresses(vcard);
4195 DumpVCardPhoneNumbers(vcard);
4196
4197 // and finally show all
4198 wxPuts(_T("\nNow dumping the entire vCard:\n")
4199 "-----------------------------\n");
4200
4201 DumpVObject(0, vcard);
4202 }
4203 }
4204
4205 static void TestVCardWrite()
4206 {
4207 wxPuts(_T("*** Testing wxVCard writing ***\n"));
4208
4209 wxVCard vcard;
4210 if ( !vcard.IsOk() )
4211 {
4212 wxPuts(_T("ERROR: couldn't create vCard."));
4213 }
4214 else
4215 {
4216 // set some fields
4217 vcard.SetName("Zeitlin", "Vadim");
4218 vcard.SetFullName("Vadim Zeitlin");
4219 vcard.SetOrganization("wxWindows", "R&D");
4220
4221 // just dump the vCard back
4222 wxPuts(_T("Entire vCard follows:\n"));
4223 wxPuts(vcard.Write());
4224 }
4225 }
4226
4227 #endif // TEST_VCARD
4228
4229 // ----------------------------------------------------------------------------
4230 // wxVolume tests
4231 // ----------------------------------------------------------------------------
4232
4233 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
4234 #undef TEST_VOLUME
4235 #endif
4236
4237 #ifdef TEST_VOLUME
4238
4239 #include "wx/volume.h"
4240
4241 static const wxChar *volumeKinds[] =
4242 {
4243 _T("floppy"),
4244 _T("hard disk"),
4245 _T("CD-ROM"),
4246 _T("DVD-ROM"),
4247 _T("network volume"),
4248 _T("other volume"),
4249 };
4250
4251 static void TestFSVolume()
4252 {
4253 wxPuts(_T("*** Testing wxFSVolume class ***"));
4254
4255 wxArrayString volumes = wxFSVolume::GetVolumes();
4256 size_t count = volumes.GetCount();
4257
4258 if ( !count )
4259 {
4260 wxPuts(_T("ERROR: no mounted volumes?"));
4261 return;
4262 }
4263
4264 wxPrintf(_T("%u mounted volumes found:\n"), count);
4265
4266 for ( size_t n = 0; n < count; n++ )
4267 {
4268 wxFSVolume vol(volumes[n]);
4269 if ( !vol.IsOk() )
4270 {
4271 wxPuts(_T("ERROR: couldn't create volume"));
4272 continue;
4273 }
4274
4275 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
4276 n + 1,
4277 vol.GetDisplayName().c_str(),
4278 vol.GetName().c_str(),
4279 volumeKinds[vol.GetKind()],
4280 vol.IsWritable() ? _T("rw") : _T("ro"),
4281 vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable")
4282 : _T("fixed"));
4283 }
4284 }
4285
4286 #endif // TEST_VOLUME
4287
4288 // ----------------------------------------------------------------------------
4289 // wide char and Unicode support
4290 // ----------------------------------------------------------------------------
4291
4292 #ifdef TEST_UNICODE
4293
4294 static void TestUnicodeToFromAscii()
4295 {
4296 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
4297
4298 static const char *msg = "Hello, world!";
4299 wxString s = wxString::FromAscii(msg);
4300
4301 wxPrintf(_T("Message in Unicode: %s\n"), s.c_str());
4302 printf("Message in ASCII: %s\n", (const char *)s.ToAscii());
4303
4304 wxPutchar(_T('\n'));
4305 }
4306
4307 #include "wx/textfile.h"
4308
4309 static void TestUnicodeTextFileRead()
4310 {
4311 wxPuts(_T("Testing wxTextFile in Unicode build\n"));
4312
4313 wxTextFile file;
4314 if ( file.Open(_T("testdata.fc"), wxConvLocal) )
4315 {
4316 const size_t count = file.GetLineCount();
4317 for ( size_t n = 0; n < count; n++ )
4318 {
4319 const wxString& s = file[n];
4320
4321 wxPrintf(_T("Line %u: \"%s\" (len %u, last char = '%c')\n"),
4322 (unsigned)n, s.c_str(), (unsigned)s.length(), s.Last());
4323 }
4324 }
4325 }
4326
4327 #endif // TEST_UNICODE
4328
4329 #ifdef TEST_WCHAR
4330
4331 #include "wx/strconv.h"
4332 #include "wx/fontenc.h"
4333 #include "wx/encconv.h"
4334 #include "wx/buffer.h"
4335
4336 static const unsigned char utf8koi8r[] =
4337 {
4338 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4339 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4340 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4341 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4342 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4343 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4344 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4345 };
4346
4347 static const unsigned char utf8iso8859_1[] =
4348 {
4349 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4350 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4351 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4352 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4353 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4354 };
4355
4356 static const unsigned char utf8Invalid[] =
4357 {
4358 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4359 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4360 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4361 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4362 0x6c, 0x61, 0x79, 0
4363 };
4364
4365 static const struct Utf8Data
4366 {
4367 const unsigned char *text;
4368 size_t len;
4369 const wxChar *charset;
4370 wxFontEncoding encoding;
4371 } utf8data[] =
4372 {
4373 { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
4374 { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 },
4375 { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
4376 };
4377
4378 static void TestUtf8()
4379 {
4380 wxPuts(_T("*** Testing UTF8 support ***\n"));
4381
4382 char buf[1024];
4383 wchar_t wbuf[1024];
4384
4385 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
4386 {
4387 const Utf8Data& u8d = utf8data[n];
4388 if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text,
4389 WXSIZEOF(wbuf)) == (size_t)-1 )
4390 {
4391 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4392 }
4393 else
4394 {
4395 wxCSConv conv(u8d.charset);
4396 if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 )
4397 {
4398 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset);
4399 }
4400 else
4401 {
4402 wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf);
4403 }
4404 }
4405
4406 wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text));
4407 if ( s.empty() )
4408 s = _T("<< conversion failed >>");
4409 wxPrintf(_T("String in current cset: %s\n"), s.c_str());
4410
4411 }
4412
4413 wxPuts(_T(""));
4414 }
4415
4416 static void TestEncodingConverter()
4417 {
4418 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4419
4420 // using wxEncodingConverter should give the same result as above
4421 char buf[1024];
4422 wchar_t wbuf[1024];
4423 if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r,
4424 WXSIZEOF(utf8koi8r)) == (size_t)-1 )
4425 {
4426 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4427 }
4428 else
4429 {
4430 wxEncodingConverter ec;
4431 ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
4432 ec.Convert(wbuf, buf);
4433 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf);
4434 }
4435
4436 wxPuts(_T(""));
4437 }
4438
4439 #endif // TEST_WCHAR
4440
4441 // ----------------------------------------------------------------------------
4442 // ZIP stream
4443 // ----------------------------------------------------------------------------
4444
4445 #ifdef TEST_ZIP
4446
4447 #include "wx/filesys.h"
4448 #include "wx/fs_zip.h"
4449 #include "wx/zipstrm.h"
4450
4451 static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
4452
4453 static void TestZipStreamRead()
4454 {
4455 wxPuts(_T("*** Testing ZIP reading ***\n"));
4456
4457 static const wxChar *filename = _T("foo");
4458 wxZipInputStream istr(TESTFILE_ZIP, filename);
4459 wxPrintf(_T("Archive size: %u\n"), istr.GetSize());
4460
4461 wxPrintf(_T("Dumping the file '%s':\n"), filename);
4462 while ( !istr.Eof() )
4463 {
4464 putchar(istr.GetC());
4465 fflush(stdout);
4466 }
4467
4468 wxPuts(_T("\n----- done ------"));
4469 }
4470
4471 static void DumpZipDirectory(wxFileSystem& fs,
4472 const wxString& dir,
4473 const wxString& indent)
4474 {
4475 wxString prefix = wxString::Format(_T("%s#zip:%s"),
4476 TESTFILE_ZIP, dir.c_str());
4477 wxString wildcard = prefix + _T("/*");
4478
4479 wxString dirname = fs.FindFirst(wildcard, wxDIR);
4480 while ( !dirname.empty() )
4481 {
4482 if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
4483 {
4484 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4485
4486 break;
4487 }
4488
4489 wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
4490
4491 DumpZipDirectory(fs, dirname,
4492 indent + wxString(_T(' '), 4));
4493
4494 dirname = fs.FindNext();
4495 }
4496
4497 wxString filename = fs.FindFirst(wildcard, wxFILE);
4498 while ( !filename.empty() )
4499 {
4500 if ( !filename.StartsWith(prefix, &filename) )
4501 {
4502 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4503
4504 break;
4505 }
4506
4507 wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
4508
4509 filename = fs.FindNext();
4510 }
4511 }
4512
4513 static void TestZipFileSystem()
4514 {
4515 wxPuts(_T("*** Testing ZIP file system ***\n"));
4516
4517 wxFileSystem::AddHandler(new wxZipFSHandler);
4518 wxFileSystem fs;
4519 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
4520
4521 DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
4522 }
4523
4524 #endif // TEST_ZIP
4525
4526 // ----------------------------------------------------------------------------
4527 // ZLIB stream
4528 // ----------------------------------------------------------------------------
4529
4530 #ifdef TEST_ZLIB
4531
4532 #include "wx/zstream.h"
4533 #include "wx/wfstream.h"
4534
4535 static const wxChar *FILENAME_GZ = _T("test.gz");
4536 static const wxChar *TEST_DATA = _T("hello and hello and hello and hello and hello");
4537
4538 static void TestZlibStreamWrite()
4539 {
4540 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4541
4542 wxFileOutputStream fileOutStream(FILENAME_GZ);
4543 wxZlibOutputStream ostr(fileOutStream);
4544 wxPrintf(_T("Compressing the test string... "));
4545 ostr.Write(TEST_DATA, wxStrlen(TEST_DATA) + 1);
4546 if ( !ostr )
4547 {
4548 wxPuts(_T("(ERROR: failed)"));
4549 }
4550 else
4551 {
4552 wxPuts(_T("(ok)"));
4553 }
4554
4555 wxPuts(_T("\n----- done ------"));
4556 }
4557
4558 static void TestZlibStreamRead()
4559 {
4560 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4561
4562 wxFileInputStream fileInStream(FILENAME_GZ);
4563 wxZlibInputStream istr(fileInStream);
4564 wxPrintf(_T("Archive size: %u\n"), istr.GetSize());
4565
4566 wxPuts(_T("Dumping the file:"));
4567 while ( !istr.Eof() )
4568 {
4569 putchar(istr.GetC());
4570 fflush(stdout);
4571 }
4572
4573 wxPuts(_T("\n----- done ------"));
4574 }
4575
4576 #endif // TEST_ZLIB
4577
4578 // ----------------------------------------------------------------------------
4579 // Gzip streams
4580 // ----------------------------------------------------------------------------
4581
4582 #ifdef TEST_GZIP
4583
4584 #include "wx/wfstream.h"
4585 #include "wx/gzstream.h"
4586 #include "wx/filename.h"
4587 #include "wx/txtstrm.h"
4588
4589 // Reads two input streams and verifies that they are the same (and non-emtpy)
4590 //
4591 void GzipVerify(wxInputStream &in1, wxInputStream &in2)
4592 {
4593 if (!in1 || !in2) {
4594 wxPuts(_T(" Can't verify"));
4595 return;
4596 }
4597
4598 const int BUFSIZE = 8192;
4599 wxCharBuffer buf1(BUFSIZE);
4600 wxCharBuffer buf2(BUFSIZE);
4601 bool none = true;
4602
4603 for (;;) {
4604 int n1 = in1.Read(buf1.data(), BUFSIZE).LastRead();
4605 int n2 = in2.Read(buf2.data(), BUFSIZE).LastRead();
4606
4607 if (n1 != n2 || (n1 && memcmp(buf1, buf2, n1) != 0) || (!n1 && none)) {
4608 wxPuts(_T(" Failure"));
4609 break;
4610 }
4611
4612 if (!n1) {
4613 wxPuts(_T(" Success"));
4614 break;
4615 }
4616
4617 none = false;
4618 }
4619
4620 while (in1.IsOk())
4621 in1.Read(buf1.data(), BUFSIZE);
4622 while (in2.IsOk())
4623 in2.Read(buf2.data(), BUFSIZE);
4624 }
4625
4626 // Write a gzip file and read it back.
4627 //
4628 void TestGzip()
4629 {
4630 wxPuts(_T("*** Testing gzip streams ***\n"));
4631
4632 const wxString testname = _T("gziptest");
4633 const wxString gzipname = testname + _T(".gz");
4634
4635 // write some random test data to a testfile
4636 wxPuts(_T("Writing random test data to ") + testname + _T("..."));
4637 {
4638 wxFFileOutputStream outstream(testname);
4639 wxTextOutputStream textout(outstream);
4640
4641 for (int i = 0; i < 1000 && outstream.Ok(); i++)
4642 textout << rand() << rand() << rand() << rand() << endl;
4643
4644 wxPuts(_T(" Done"));
4645 }
4646
4647 wxFileName fn(testname);
4648 wxDateTime dt = fn.GetModificationTime();
4649 wxFFileInputStream instream(testname);
4650
4651 // try writing a gzip file
4652 wxPuts(_T("Writing ") + gzipname + _T(" using wxGzipOutputStream..."));
4653 {
4654 wxFFileOutputStream outstream(gzipname);
4655 wxGzipOutputStream gzip(outstream, testname, dt);
4656
4657 if (!gzip.Write(instream))
4658 wxPuts(_T(" Failure"));
4659 else
4660 wxPuts(_T(" Success"));
4661 }
4662
4663 // try reading the gzip file
4664 wxPuts(_T("Reading ") + gzipname + _T(" using wxGzipInputStream..."));
4665 {
4666 instream.SeekI(0);
4667 wxFFileInputStream instream2(gzipname);
4668 wxGzipInputStream gzip(instream2);
4669 GzipVerify(instream, gzip);
4670
4671 if (gzip.GetName() != fn.GetFullName())
4672 wxPuts(gzipname + _T(" contains incorrect filename: ")
4673 + gzip.GetName());
4674 if (dt.IsValid() && gzip.GetDateTime() != dt)
4675 wxPuts(gzipname + _T(" contains incorrect timestamp: ")
4676 + gzip.GetDateTime().Format());
4677 }
4678
4679 #ifdef __UNIX__
4680 // then verify it using gzip program if it is in the path
4681 wxPuts(_T("Reading ") + gzipname + _T(" using gzip program..."));
4682 wxFFile file(popen((_T("gzip -d -c ") + gzipname).mb_str(), "r"));
4683 if (file.fp()) {
4684 wxFFileInputStream instream2(file);
4685 instream.SeekI(0);
4686 GzipVerify(instream, instream2);
4687 pclose(file.fp());
4688 file.Detach();
4689 }
4690
4691 // try reading a gzip created by gzip program
4692 wxPuts(_T("Reading output of gzip program using wxGzipInputStream..."));
4693 file.Attach(popen((_T("gzip -c ") + testname).mb_str(), "r"));
4694 if (file.fp()) {
4695 wxFFileInputStream instream2(file);
4696 wxGzipInputStream gzip(instream2);
4697 instream.SeekI(0);
4698 GzipVerify(instream, gzip);
4699 pclose(file.fp());
4700 file.Detach();
4701 }
4702 #endif
4703
4704 wxPuts(_T("\n--- Done gzip streams ---"));
4705 }
4706
4707 #endif // TEST_GZIP
4708
4709 // ----------------------------------------------------------------------------
4710 // date time
4711 // ----------------------------------------------------------------------------
4712
4713 #ifdef TEST_DATETIME
4714
4715 #include <math.h>
4716
4717 #include "wx/datetime.h"
4718
4719 // the test data
4720 struct Date
4721 {
4722 wxDateTime::wxDateTime_t day;
4723 wxDateTime::Month month;
4724 int year;
4725 wxDateTime::wxDateTime_t hour, min, sec;
4726 double jdn;
4727 wxDateTime::WeekDay wday;
4728 time_t gmticks, ticks;
4729
4730 void Init(const wxDateTime::Tm& tm)
4731 {
4732 day = tm.mday;
4733 month = tm.mon;
4734 year = tm.year;
4735 hour = tm.hour;
4736 min = tm.min;
4737 sec = tm.sec;
4738 jdn = 0.0;
4739 gmticks = ticks = -1;
4740 }
4741
4742 wxDateTime DT() const
4743 { return wxDateTime(day, month, year, hour, min, sec); }
4744
4745 bool SameDay(const wxDateTime::Tm& tm) const
4746 {
4747 return day == tm.mday && month == tm.mon && year == tm.year;
4748 }
4749
4750 wxString Format() const
4751 {
4752 wxString s;
4753 s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4754 hour, min, sec,
4755 wxDateTime::GetMonthName(month).c_str(),
4756 day,
4757 abs(wxDateTime::ConvertYearToBC(year)),
4758 year > 0 ? _T("AD") : _T("BC"));
4759 return s;
4760 }
4761
4762 wxString FormatDate() const
4763 {
4764 wxString s;
4765 s.Printf(_T("%02d-%s-%4d%s"),
4766 day,
4767 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
4768 abs(wxDateTime::ConvertYearToBC(year)),
4769 year > 0 ? _T("AD") : _T("BC"));
4770 return s;
4771 }
4772 };
4773
4774 static const Date testDates[] =
4775 {
4776 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
4777 { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 },
4778 { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 },
4779 { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 },
4780 { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 },
4781 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
4782 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
4783 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
4784 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
4785 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
4786 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
4787 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
4788 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
4789 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
4790 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
4791 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
4792 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
4793 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
4794 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
4795 };
4796
4797 // this test miscellaneous static wxDateTime functions
4798 static void TestTimeStatic()
4799 {
4800 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4801
4802 // some info about the current date
4803 int year = wxDateTime::GetCurrentYear();
4804 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4805 year,
4806 wxDateTime::IsLeapYear(year) ? "" : "not ",
4807 wxDateTime::GetNumberOfDays(year));
4808
4809 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
4810 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4811 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
4812 wxDateTime::GetMonthName(month).c_str(),
4813 wxDateTime::GetNumberOfDays(month));
4814
4815 // leap year logic
4816 static const size_t nYears = 5;
4817 static const size_t years[2][nYears] =
4818 {
4819 // first line: the years to test
4820 { 1990, 1976, 2000, 2030, 1984, },
4821
4822 // second line: true if leap, false otherwise
4823 { false, true, true, false, true }
4824 };
4825
4826 for ( size_t n = 0; n < nYears; n++ )
4827 {
4828 int year = years[0][n];
4829 bool should = years[1][n] != 0,
4830 is = wxDateTime::IsLeapYear(year);
4831
4832 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4833 year,
4834 is ? "" : "not ",
4835 should == is ? "ok" : "ERROR");
4836
4837 wxASSERT( should == wxDateTime::IsLeapYear(year) );
4838 }
4839 }
4840
4841 // test constructing wxDateTime objects
4842 static void TestTimeSet()
4843 {
4844 wxPuts(_T("\n*** wxDateTime construction test ***"));
4845
4846 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
4847 {
4848 const Date& d1 = testDates[n];
4849 wxDateTime dt = d1.DT();
4850
4851 Date d2;
4852 d2.Init(dt.GetTm());
4853
4854 wxString s1 = d1.Format(),
4855 s2 = d2.Format();
4856
4857 wxPrintf(_T("Date: %s == %s (%s)\n"),
4858 s1.c_str(), s2.c_str(),
4859 s1 == s2 ? _T("ok") : _T("ERROR"));
4860 }
4861 }
4862
4863 // test time zones stuff
4864 static void TestTimeZones()
4865 {
4866 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4867
4868 wxDateTime now = wxDateTime::Now();
4869
4870 wxPrintf(_T("Current GMT time:\t%s\n"), now.Format(_T("%c"), wxDateTime::GMT0).c_str());
4871 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0).c_str());
4872 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST).c_str());
4873 wxPrintf(_T("Current time in Paris:\t%s\n"), now.Format(_T("%c"), wxDateTime::CET).c_str());
4874 wxPrintf(_T(" Moscow:\t%s\n"), now.Format(_T("%c"), wxDateTime::MSK).c_str());
4875 wxPrintf(_T(" New York:\t%s\n"), now.Format(_T("%c"), wxDateTime::EST).c_str());
4876
4877 wxDateTime::Tm tm = now.GetTm();
4878 if ( wxDateTime(tm) != now )
4879 {
4880 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4881 wxDateTime(tm).Format().c_str(), now.Format().c_str());
4882 }
4883 }
4884
4885 // test some minimal support for the dates outside the standard range
4886 static void TestTimeRange()
4887 {
4888 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4889
4890 static const wxChar *fmt = _T("%d-%b-%Y %H:%M:%S");
4891
4892 wxPrintf(_T("Unix epoch:\t%s\n"),
4893 wxDateTime(2440587.5).Format(fmt).c_str());
4894 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4895 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
4896 wxPrintf(_T("JDN 0: \t%s\n"),
4897 wxDateTime(0.0).Format(fmt).c_str());
4898 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4899 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
4900 wxPrintf(_T("May 29, 2099:\t%s\n"),
4901 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
4902 }
4903
4904 static void TestTimeTicks()
4905 {
4906 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4907
4908 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
4909 {
4910 const Date& d = testDates[n];
4911 if ( d.ticks == -1 )
4912 continue;
4913
4914 wxDateTime dt = d.DT();
4915 long ticks = (dt.GetValue() / 1000).ToLong();
4916 wxPrintf(_T("Ticks of %s:\t% 10ld"), d.Format().c_str(), ticks);
4917 if ( ticks == d.ticks )
4918 {
4919 wxPuts(_T(" (ok)"));
4920 }
4921 else
4922 {
4923 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4924 (long)d.ticks, (long)(ticks - d.ticks));
4925 }
4926
4927 dt = d.DT().ToTimezone(wxDateTime::GMT0);
4928 ticks = (dt.GetValue() / 1000).ToLong();
4929 wxPrintf(_T("GMtks of %s:\t% 10ld"), d.Format().c_str(), ticks);
4930 if ( ticks == d.gmticks )
4931 {
4932 wxPuts(_T(" (ok)"));
4933 }
4934 else
4935 {
4936 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4937 (long)d.gmticks, (long)(ticks - d.gmticks));
4938 }
4939 }
4940
4941 wxPuts(_T(""));
4942 }
4943
4944 // test conversions to JDN &c
4945 static void TestTimeJDN()
4946 {
4947 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4948
4949 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
4950 {
4951 const Date& d = testDates[n];
4952 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
4953 double jdn = dt.GetJulianDayNumber();
4954
4955 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d.Format().c_str(), jdn);
4956 if ( jdn == d.jdn )
4957 {
4958 wxPuts(_T(" (ok)"));
4959 }
4960 else
4961 {
4962 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4963 d.jdn, jdn - d.jdn);
4964 }
4965 }
4966 }
4967
4968 // test week days computation
4969 static void TestTimeWDays()
4970 {
4971 wxPuts(_T("\n*** wxDateTime weekday test ***"));
4972
4973 // test GetWeekDay()
4974 size_t n;
4975 for ( n = 0; n < WXSIZEOF(testDates); n++ )
4976 {
4977 const Date& d = testDates[n];
4978 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
4979
4980 wxDateTime::WeekDay wday = dt.GetWeekDay();
4981 wxPrintf(_T("%s is: %s"),
4982 d.Format().c_str(),
4983 wxDateTime::GetWeekDayName(wday).c_str());
4984 if ( wday == d.wday )
4985 {
4986 wxPuts(_T(" (ok)"));
4987 }
4988 else
4989 {
4990 wxPrintf(_T(" (ERROR: should be %s)\n"),
4991 wxDateTime::GetWeekDayName(d.wday).c_str());
4992 }
4993 }
4994
4995 wxPuts(_T(""));
4996
4997 // test SetToWeekDay()
4998 struct WeekDateTestData
4999 {
5000 Date date; // the real date (precomputed)
5001 int nWeek; // its week index in the month
5002 wxDateTime::WeekDay wday; // the weekday
5003 wxDateTime::Month month; // the month
5004 int year; // and the year
5005
5006 wxString Format() const
5007 {
5008 wxString s, which;
5009 switch ( nWeek < -1 ? -nWeek : nWeek )
5010 {
5011 case 1: which = _T("first"); break;
5012 case 2: which = _T("second"); break;
5013 case 3: which = _T("third"); break;
5014 case 4: which = _T("fourth"); break;
5015 case 5: which = _T("fifth"); break;
5016
5017 case -1: which = _T("last"); break;
5018 }
5019
5020 if ( nWeek < -1 )
5021 {
5022 which += _T(" from end");
5023 }
5024
5025 s.Printf(_T("The %s %s of %s in %d"),
5026 which.c_str(),
5027 wxDateTime::GetWeekDayName(wday).c_str(),
5028 wxDateTime::GetMonthName(month).c_str(),
5029 year);
5030
5031 return s;
5032 }
5033 };
5034
5035 // the array data was generated by the following python program
5036 /*
5037 from DateTime import *
5038 from whrandom import *
5039 from string import *
5040
5041 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5042 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5043
5044 week = DateTimeDelta(7)
5045
5046 for n in range(20):
5047 year = randint(1900, 2100)
5048 month = randint(1, 12)
5049 day = randint(1, 28)
5050 dt = DateTime(year, month, day)
5051 wday = dt.day_of_week
5052
5053 countFromEnd = choice([-1, 1])
5054 weekNum = 0;
5055
5056 while dt.month is month:
5057 dt = dt - countFromEnd * week
5058 weekNum = weekNum + countFromEnd
5059
5060 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
5061
5062 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
5063 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
5064 */
5065
5066 static const WeekDateTestData weekDatesTestData[] =
5067 {
5068 { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
5069 { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
5070 { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
5071 { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
5072 { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
5073 { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
5074 { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
5075 { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
5076 { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
5077 { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
5078 { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
5079 { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
5080 { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
5081 { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
5082 { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
5083 { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
5084 { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
5085 { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
5086 { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
5087 { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 },
5088 };
5089
5090 static const wxChar *fmt = _T("%d-%b-%Y");
5091
5092 wxDateTime dt;
5093 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
5094 {
5095 const WeekDateTestData& wd = weekDatesTestData[n];
5096
5097 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
5098
5099 wxPrintf(_T("%s is %s"), wd.Format().c_str(), dt.Format(fmt).c_str());
5100
5101 const Date& d = wd.date;
5102 if ( d.SameDay(dt.GetTm()) )
5103 {
5104 wxPuts(_T(" (ok)"));
5105 }
5106 else
5107 {
5108 dt.Set(d.day, d.month, d.year);
5109
5110 wxPrintf(_T(" (ERROR: should be %s)\n"), dt.Format(fmt).c_str());
5111 }
5112 }
5113 }
5114
5115 // test the computation of (ISO) week numbers
5116 static void TestTimeWNumber()
5117 {
5118 wxPuts(_T("\n*** wxDateTime week number test ***"));
5119
5120 struct WeekNumberTestData
5121 {
5122 Date date; // the date
5123 wxDateTime::wxDateTime_t week; // the week number in the year
5124 wxDateTime::wxDateTime_t wmon; // the week number in the month
5125 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
5126 wxDateTime::wxDateTime_t dnum; // day number in the year
5127 };
5128
5129 // data generated with the following python script:
5130 /*
5131 from DateTime import *
5132 from whrandom import *
5133 from string import *
5134
5135 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5136 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5137
5138 def GetMonthWeek(dt):
5139 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
5140 if weekNumMonth < 0:
5141 weekNumMonth = weekNumMonth + 53
5142 return weekNumMonth
5143
5144 def GetLastSundayBefore(dt):
5145 if dt.iso_week[2] == 7:
5146 return dt
5147 else:
5148 return dt - DateTimeDelta(dt.iso_week[2])
5149
5150 for n in range(20):
5151 year = randint(1900, 2100)
5152 month = randint(1, 12)
5153 day = randint(1, 28)
5154 dt = DateTime(year, month, day)
5155 dayNum = dt.day_of_year
5156 weekNum = dt.iso_week[1]
5157 weekNumMonth = GetMonthWeek(dt)
5158
5159 weekNumMonth2 = 0
5160 dtSunday = GetLastSundayBefore(dt)
5161
5162 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
5163 weekNumMonth2 = weekNumMonth2 + 1
5164 dtSunday = dtSunday - DateTimeDelta(7)
5165
5166 data = { 'day': rjust(`day`, 2), \
5167 'month': monthNames[month - 1], \
5168 'year': year, \
5169 'weekNum': rjust(`weekNum`, 2), \
5170 'weekNumMonth': weekNumMonth, \
5171 'weekNumMonth2': weekNumMonth2, \
5172 'dayNum': rjust(`dayNum`, 3) }
5173
5174 print " { { %(day)s, "\
5175 "wxDateTime::%(month)s, "\
5176 "%(year)d }, "\
5177 "%(weekNum)s, "\
5178 "%(weekNumMonth)s, "\
5179 "%(weekNumMonth2)s, "\
5180 "%(dayNum)s }," % data
5181
5182 */
5183 static const WeekNumberTestData weekNumberTestDates[] =
5184 {
5185 { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 },
5186 { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 },
5187 { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 },
5188 { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 },
5189 { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 },
5190 { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 },
5191 { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 },
5192 { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 },
5193 { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 },
5194 { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 },
5195 { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 },
5196 { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 },
5197 { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 },
5198 { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 },
5199 { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 },
5200 { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 },
5201 { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 },
5202 { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 },
5203 { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 },
5204 { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 },
5205 };
5206
5207 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
5208 {
5209 const WeekNumberTestData& wn = weekNumberTestDates[n];
5210 const Date& d = wn.date;
5211
5212 wxDateTime dt = d.DT();
5213
5214 wxDateTime::wxDateTime_t
5215 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
5216 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
5217 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
5218 dnum = dt.GetDayOfYear();
5219
5220 wxPrintf(_T("%s: the day number is %d"), d.FormatDate().c_str(), dnum);
5221 if ( dnum == wn.dnum )
5222 {
5223 wxPrintf(_T(" (ok)"));
5224 }
5225 else
5226 {
5227 wxPrintf(_T(" (ERROR: should be %d)"), wn.dnum);
5228 }
5229
5230 wxPrintf(_T(", week in month = %d"), wmon);
5231 if ( wmon != wn.wmon )
5232 {
5233 wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon);
5234 }
5235
5236 wxPrintf(_T(" or %d"), wmon2);
5237 if ( wmon2 == wn.wmon2 )
5238 {
5239 wxPrintf(_T(" (ok)"));
5240 }
5241 else
5242 {
5243 wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon2);
5244 }
5245
5246 wxPrintf(_T(", week in year = %d"), week);
5247 if ( week != wn.week )
5248 {
5249 wxPrintf(_T(" (ERROR: should be %d)"), wn.week);
5250 }
5251
5252 wxPutchar(_T('\n'));
5253
5254 wxDateTime dt2(1, wxDateTime::Jan, d.year);
5255 dt2.SetToTheWeek(wn.week, dt.GetWeekDay());
5256 if ( dt2 != dt )
5257 {
5258 Date d2;
5259 d2.Init(dt2.GetTm());
5260 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
5261 d2.FormatDate().c_str());
5262 }
5263 }
5264 }
5265
5266 // test DST calculations
5267 static void TestTimeDST()
5268 {
5269 wxPuts(_T("\n*** wxDateTime DST test ***"));
5270
5271 wxPrintf(_T("DST is%s in effect now.\n\n"),
5272 wxDateTime::Now().IsDST() ? _T("") : _T(" not"));
5273
5274 // taken from http://www.energy.ca.gov/daylightsaving.html
5275 static const Date datesDST[2][2004 - 1900 + 1] =
5276 {
5277 {
5278 { 1, wxDateTime::Apr, 1990 },
5279 { 7, wxDateTime::Apr, 1991 },
5280 { 5, wxDateTime::Apr, 1992 },
5281 { 4, wxDateTime::Apr, 1993 },
5282 { 3, wxDateTime::Apr, 1994 },
5283 { 2, wxDateTime::Apr, 1995 },
5284 { 7, wxDateTime::Apr, 1996 },
5285 { 6, wxDateTime::Apr, 1997 },
5286 { 5, wxDateTime::Apr, 1998 },
5287 { 4, wxDateTime::Apr, 1999 },
5288 { 2, wxDateTime::Apr, 2000 },
5289 { 1, wxDateTime::Apr, 2001 },
5290 { 7, wxDateTime::Apr, 2002 },
5291 { 6, wxDateTime::Apr, 2003 },
5292 { 4, wxDateTime::Apr, 2004 },
5293 },
5294 {
5295 { 28, wxDateTime::Oct, 1990 },
5296 { 27, wxDateTime::Oct, 1991 },
5297 { 25, wxDateTime::Oct, 1992 },
5298 { 31, wxDateTime::Oct, 1993 },
5299 { 30, wxDateTime::Oct, 1994 },
5300 { 29, wxDateTime::Oct, 1995 },
5301 { 27, wxDateTime::Oct, 1996 },
5302 { 26, wxDateTime::Oct, 1997 },
5303 { 25, wxDateTime::Oct, 1998 },
5304 { 31, wxDateTime::Oct, 1999 },
5305 { 29, wxDateTime::Oct, 2000 },
5306 { 28, wxDateTime::Oct, 2001 },
5307 { 27, wxDateTime::Oct, 2002 },
5308 { 26, wxDateTime::Oct, 2003 },
5309 { 31, wxDateTime::Oct, 2004 },
5310 }
5311 };
5312
5313 int year;
5314 for ( year = 1990; year < 2005; year++ )
5315 {
5316 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
5317 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
5318
5319 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5320 year, dtBegin.Format().c_str(), dtEnd.Format().c_str());
5321
5322 size_t n = year - 1990;
5323 const Date& dBegin = datesDST[0][n];
5324 const Date& dEnd = datesDST[1][n];
5325
5326 if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) )
5327 {
5328 wxPuts(_T(" (ok)"));
5329 }
5330 else
5331 {
5332 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5333 wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day,
5334 wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day);
5335 }
5336 }
5337
5338 wxPuts(_T(""));
5339
5340 for ( year = 1990; year < 2005; year++ )
5341 {
5342 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5343 year,
5344 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
5345 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
5346 }
5347 }
5348
5349 // test wxDateTime -> text conversion
5350 static void TestTimeFormat()
5351 {
5352 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5353
5354 // some information may be lost during conversion, so store what kind
5355 // of info should we recover after a round trip
5356 enum CompareKind
5357 {
5358 CompareNone, // don't try comparing
5359 CompareBoth, // dates and times should be identical
5360 CompareDate, // dates only
5361 CompareTime // time only
5362 };
5363
5364 static const struct
5365 {
5366 CompareKind compareKind;
5367 const wxChar *format;
5368 } formatTestFormats[] =
5369 {
5370 { CompareBoth, _T("---> %c") },
5371 { CompareDate, _T("Date is %A, %d of %B, in year %Y") },
5372 { CompareBoth, _T("Date is %x, time is %X") },
5373 { CompareTime, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5374 { CompareNone, _T("The day of year: %j, the week of year: %W") },
5375 { CompareDate, _T("ISO date without separators: %Y%m%d") },
5376 };
5377
5378 static const Date formatTestDates[] =
5379 {
5380 { 29, wxDateTime::May, 1976, 18, 30, 00 },
5381 { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
5382 #if 0
5383 // this test can't work for other centuries because it uses two digit
5384 // years in formats, so don't even try it
5385 { 29, wxDateTime::May, 2076, 18, 30, 00 },
5386 { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
5387 { 01, wxDateTime::Jan, -52, 03, 16, 47 },
5388 #endif
5389 };
5390
5391 // an extra test (as it doesn't depend on date, don't do it in the loop)
5392 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5393
5394 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
5395 {
5396 wxPuts(_T(""));
5397
5398 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
5399 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
5400 {
5401 wxString s = dt.Format(formatTestFormats[n].format);
5402 wxPrintf(_T("%s"), s.c_str());
5403
5404 // what can we recover?
5405 int kind = formatTestFormats[n].compareKind;
5406
5407 // convert back
5408 wxDateTime dt2;
5409 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
5410 if ( !result )
5411 {
5412 // converion failed - should it have?
5413 if ( kind == CompareNone )
5414 wxPuts(_T(" (ok)"));
5415 else
5416 wxPuts(_T(" (ERROR: conversion back failed)"));
5417 }
5418 else if ( *result )
5419 {
5420 // should have parsed the entire string
5421 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5422 }
5423 else
5424 {
5425 bool equal = false; // suppress compilaer warning
5426 switch ( kind )
5427 {
5428 case CompareBoth:
5429 equal = dt2 == dt;
5430 break;
5431
5432 case CompareDate:
5433 equal = dt.IsSameDate(dt2);
5434 break;
5435
5436 case CompareTime:
5437 equal = dt.IsSameTime(dt2);
5438 break;
5439 }
5440
5441 if ( !equal )
5442 {
5443 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5444 dt2.Format().c_str(), dt.Format().c_str());
5445 }
5446 else
5447 {
5448 wxPuts(_T(" (ok)"));
5449 }
5450 }
5451 }
5452 }
5453 }
5454
5455 // test text -> wxDateTime conversion
5456 static void TestTimeParse()
5457 {
5458 wxPuts(_T("\n*** wxDateTime parse test ***"));
5459
5460 struct ParseTestData
5461 {
5462 const wxChar *format;
5463 Date date;
5464 bool good;
5465 };
5466
5467 static const ParseTestData parseTestDates[] =
5468 {
5469 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, true },
5470 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, true },
5471 };
5472
5473 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
5474 {
5475 const wxChar *format = parseTestDates[n].format;
5476
5477 wxPrintf(_T("%s => "), format);
5478
5479 wxDateTime dt;
5480 if ( dt.ParseRfc822Date(format) )
5481 {
5482 wxPrintf(_T("%s "), dt.Format().c_str());
5483
5484 if ( parseTestDates[n].good )
5485 {
5486 wxDateTime dtReal = parseTestDates[n].date.DT();
5487 if ( dt == dtReal )
5488 {
5489 wxPuts(_T("(ok)"));
5490 }
5491 else
5492 {
5493 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal.Format().c_str());
5494 }
5495 }
5496 else
5497 {
5498 wxPuts(_T("(ERROR: bad format)"));
5499 }
5500 }
5501 else
5502 {
5503 wxPrintf(_T("bad format (%s)\n"),
5504 parseTestDates[n].good ? "ERROR" : "ok");
5505 }
5506 }
5507 }
5508
5509 static void TestDateTimeInteractive()
5510 {
5511 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5512
5513 wxChar buf[128];
5514
5515 for ( ;; )
5516 {
5517 wxPrintf(_T("Enter a date: "));
5518 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
5519 break;
5520
5521 // kill the last '\n'
5522 buf[wxStrlen(buf) - 1] = 0;
5523
5524 wxDateTime dt;
5525 const wxChar *p = dt.ParseDate(buf);
5526 if ( !p )
5527 {
5528 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf);
5529
5530 continue;
5531 }
5532 else if ( *p )
5533 {
5534 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf);
5535 }
5536
5537 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5538 dt.Format(_T("%b %d, %Y")).c_str(),
5539 dt.GetDayOfYear(),
5540 dt.GetWeekOfMonth(wxDateTime::Monday_First),
5541 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
5542 dt.GetWeekOfYear(wxDateTime::Monday_First));
5543 }
5544
5545 wxPuts(_T("\n*** done ***"));
5546 }
5547
5548 static void TestTimeMS()
5549 {
5550 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5551
5552 wxDateTime dt1 = wxDateTime::Now(),
5553 dt2 = wxDateTime::UNow();
5554
5555 wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str());
5556 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
5557 wxPrintf(_T("Dummy loop: "));
5558 for ( int i = 0; i < 6000; i++ )
5559 {
5560 //for ( int j = 0; j < 10; j++ )
5561 {
5562 wxString s;
5563 s.Printf(_T("%g"), sqrt(i));
5564 }
5565
5566 if ( !(i % 100) )
5567 putchar('.');
5568 }
5569 wxPuts(_T(", done"));
5570
5571 dt1 = dt2;
5572 dt2 = wxDateTime::UNow();
5573 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
5574
5575 wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str());
5576
5577 wxPuts(_T("\n*** done ***"));
5578 }
5579
5580 static void TestTimeArithmetics()
5581 {
5582 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5583
5584 static const struct ArithmData
5585 {
5586 ArithmData(const wxDateSpan& sp, const wxChar *nam)
5587 : span(sp), name(nam) { }
5588
5589 wxDateSpan span;
5590 const wxChar *name;
5591 } testArithmData[] =
5592 {
5593 ArithmData(wxDateSpan::Day(), _T("day")),
5594 ArithmData(wxDateSpan::Week(), _T("week")),
5595 ArithmData(wxDateSpan::Month(), _T("month")),
5596 ArithmData(wxDateSpan::Year(), _T("year")),
5597 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5598 };
5599
5600 wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2;
5601
5602 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
5603 {
5604 wxDateSpan span = testArithmData[n].span;
5605 dt1 = dt + span;
5606 dt2 = dt - span;
5607
5608 const wxChar *name = testArithmData[n].name;
5609 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5610 dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(),
5611 dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str());
5612
5613 wxPrintf(_T("Going back: %s"), (dt1 - span).FormatISODate().c_str());
5614 if ( dt1 - span == dt )
5615 {
5616 wxPuts(_T(" (ok)"));
5617 }
5618 else
5619 {
5620 wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str());
5621 }
5622
5623 wxPrintf(_T("Going forward: %s"), (dt2 + span).FormatISODate().c_str());
5624 if ( dt2 + span == dt )
5625 {
5626 wxPuts(_T(" (ok)"));
5627 }
5628 else
5629 {
5630 wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str());
5631 }
5632
5633 wxPrintf(_T("Double increment: %s"), (dt2 + 2*span).FormatISODate().c_str());
5634 if ( dt2 + 2*span == dt1 )
5635 {
5636 wxPuts(_T(" (ok)"));
5637 }
5638 else
5639 {
5640 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2.FormatISODate().c_str());
5641 }
5642
5643 wxPuts(_T(""));
5644 }
5645 }
5646
5647 static void TestTimeHolidays()
5648 {
5649 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5650
5651 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
5652 wxDateTime dtStart(1, tm.mon, tm.year),
5653 dtEnd = dtStart.GetLastMonthDay();
5654
5655 wxDateTimeArray hol;
5656 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
5657
5658 const wxChar *format = _T("%d-%b-%Y (%a)");
5659
5660 wxPrintf(_T("All holidays between %s and %s:\n"),
5661 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
5662
5663 size_t count = hol.GetCount();
5664 for ( size_t n = 0; n < count; n++ )
5665 {
5666 wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str());
5667 }
5668
5669 wxPuts(_T(""));
5670 }
5671
5672 static void TestTimeZoneBug()
5673 {
5674 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5675
5676 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
5677 for ( int i = 0; i < 31; i++ )
5678 {
5679 wxPrintf(_T("Date %s: week day %s.\n"),
5680 date.Format(_T("%d-%m-%Y")).c_str(),
5681 date.GetWeekDayName(date.GetWeekDay()).c_str());
5682
5683 date += wxDateSpan::Day();
5684 }
5685
5686 wxPuts(_T(""));
5687 }
5688
5689 static void TestTimeSpanFormat()
5690 {
5691 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5692
5693 static const wxChar *formats[] =
5694 {
5695 _T("(default) %H:%M:%S"),
5696 _T("%E weeks and %D days"),
5697 _T("%l milliseconds"),
5698 _T("(with ms) %H:%M:%S:%l"),
5699 _T("100%% of minutes is %M"), // test "%%"
5700 _T("%D days and %H hours"),
5701 _T("or also %S seconds"),
5702 };
5703
5704 wxTimeSpan ts1(1, 2, 3, 4),
5705 ts2(111, 222, 333);
5706 for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
5707 {
5708 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5709 ts1.Format(formats[n]).c_str(),
5710 ts2.Format(formats[n]).c_str());
5711 }
5712
5713 wxPuts(_T(""));
5714 }
5715
5716 #endif // TEST_DATETIME
5717
5718 // ----------------------------------------------------------------------------
5719 // wxTextInput/OutputStream
5720 // ----------------------------------------------------------------------------
5721
5722 #ifdef TEST_TEXTSTREAM
5723
5724 #include "wx/txtstrm.h"
5725 #include "wx/wfstream.h"
5726
5727 static void TestTextInputStream()
5728 {
5729 wxPuts(_T("\n*** wxTextInputStream test ***"));
5730
5731 wxFileInputStream fsIn(_T("testdata.fc"));
5732 if ( !fsIn.Ok() )
5733 {
5734 wxPuts(_T("ERROR: couldn't open file."));
5735 }
5736 else
5737 {
5738 wxTextInputStream tis(fsIn);
5739
5740 size_t line = 1;
5741 for ( ;; )
5742 {
5743 const wxString s = tis.ReadLine();
5744
5745 // line could be non empty if the last line of the file isn't
5746 // terminated with EOL
5747 if ( fsIn.Eof() && s.empty() )
5748 break;
5749
5750 wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
5751 }
5752 }
5753 }
5754
5755 #endif // TEST_TEXTSTREAM
5756
5757 // ----------------------------------------------------------------------------
5758 // threads
5759 // ----------------------------------------------------------------------------
5760
5761 #ifdef TEST_THREADS
5762
5763 #include "wx/thread.h"
5764
5765 static size_t gs_counter = (size_t)-1;
5766 static wxCriticalSection gs_critsect;
5767 static wxSemaphore gs_cond;
5768
5769 class MyJoinableThread : public wxThread
5770 {
5771 public:
5772 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
5773 { m_n = n; Create(); }
5774
5775 // thread execution starts here
5776 virtual ExitCode Entry();
5777
5778 private:
5779 size_t m_n;
5780 };
5781
5782 wxThread::ExitCode MyJoinableThread::Entry()
5783 {
5784 unsigned long res = 1;
5785 for ( size_t n = 1; n < m_n; n++ )
5786 {
5787 res *= n;
5788
5789 // it's a loooong calculation :-)
5790 Sleep(100);
5791 }
5792
5793 return (ExitCode)res;
5794 }
5795
5796 class MyDetachedThread : public wxThread
5797 {
5798 public:
5799 MyDetachedThread(size_t n, wxChar ch)
5800 {
5801 m_n = n;
5802 m_ch = ch;
5803 m_cancelled = false;
5804
5805 Create();
5806 }
5807
5808 // thread execution starts here
5809 virtual ExitCode Entry();
5810
5811 // and stops here
5812 virtual void OnExit();
5813
5814 private:
5815 size_t m_n; // number of characters to write
5816 wxChar m_ch; // character to write
5817
5818 bool m_cancelled; // false if we exit normally
5819 };
5820
5821 wxThread::ExitCode MyDetachedThread::Entry()
5822 {
5823 {
5824 wxCriticalSectionLocker lock(gs_critsect);
5825 if ( gs_counter == (size_t)-1 )
5826 gs_counter = 1;
5827 else
5828 gs_counter++;
5829 }
5830
5831 for ( size_t n = 0; n < m_n; n++ )
5832 {
5833 if ( TestDestroy() )
5834 {
5835 m_cancelled = true;
5836
5837 break;
5838 }
5839
5840 putchar(m_ch);
5841 fflush(stdout);
5842
5843 wxThread::Sleep(100);
5844 }
5845
5846 return 0;
5847 }
5848
5849 void MyDetachedThread::OnExit()
5850 {
5851 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5852
5853 wxCriticalSectionLocker lock(gs_critsect);
5854 if ( !--gs_counter && !m_cancelled )
5855 gs_cond.Post();
5856 }
5857
5858 static void TestDetachedThreads()
5859 {
5860 wxPuts(_T("\n*** Testing detached threads ***"));
5861
5862 static const size_t nThreads = 3;
5863 MyDetachedThread *threads[nThreads];
5864 size_t n;
5865 for ( n = 0; n < nThreads; n++ )
5866 {
5867 threads[n] = new MyDetachedThread(10, 'A' + n);
5868 }
5869
5870 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
5871 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
5872
5873 for ( n = 0; n < nThreads; n++ )
5874 {
5875 threads[n]->Run();
5876 }
5877
5878 // wait until all threads terminate
5879 gs_cond.Wait();
5880
5881 wxPuts(_T(""));
5882 }
5883
5884 static void TestJoinableThreads()
5885 {
5886 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5887
5888 // calc 10! in the background
5889 MyJoinableThread thread(10);
5890 thread.Run();
5891
5892 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5893 (unsigned long)thread.Wait());
5894 }
5895
5896 static void TestThreadSuspend()
5897 {
5898 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5899
5900 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
5901
5902 thread->Run();
5903
5904 // this is for this demo only, in a real life program we'd use another
5905 // condition variable which would be signaled from wxThread::Entry() to
5906 // tell us that the thread really started running - but here just wait a
5907 // bit and hope that it will be enough (the problem is, of course, that
5908 // the thread might still not run when we call Pause() which will result
5909 // in an error)
5910 wxThread::Sleep(300);
5911
5912 for ( size_t n = 0; n < 3; n++ )
5913 {
5914 thread->Pause();
5915
5916 wxPuts(_T("\nThread suspended"));
5917 if ( n > 0 )
5918 {
5919 // don't sleep but resume immediately the first time
5920 wxThread::Sleep(300);
5921 }
5922 wxPuts(_T("Going to resume the thread"));
5923
5924 thread->Resume();
5925 }
5926
5927 wxPuts(_T("Waiting until it terminates now"));
5928
5929 // wait until the thread terminates
5930 gs_cond.Wait();
5931
5932 wxPuts(_T(""));
5933 }
5934
5935 static void TestThreadDelete()
5936 {
5937 // As above, using Sleep() is only for testing here - we must use some
5938 // synchronisation object instead to ensure that the thread is still
5939 // running when we delete it - deleting a detached thread which already
5940 // terminated will lead to a crash!
5941
5942 wxPuts(_T("\n*** Testing thread delete function ***"));
5943
5944 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
5945
5946 thread0->Delete();
5947
5948 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5949
5950 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
5951
5952 thread1->Run();
5953
5954 wxThread::Sleep(300);
5955
5956 thread1->Delete();
5957
5958 wxPuts(_T("\nDeleted a running thread."));
5959
5960 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
5961
5962 thread2->Run();
5963
5964 wxThread::Sleep(300);
5965
5966 thread2->Pause();
5967
5968 thread2->Delete();
5969
5970 wxPuts(_T("\nDeleted a sleeping thread."));
5971
5972 MyJoinableThread thread3(20);
5973 thread3.Run();
5974
5975 thread3.Delete();
5976
5977 wxPuts(_T("\nDeleted a joinable thread."));
5978
5979 MyJoinableThread thread4(2);
5980 thread4.Run();
5981
5982 wxThread::Sleep(300);
5983
5984 thread4.Delete();
5985
5986 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
5987
5988 wxPuts(_T(""));
5989 }
5990
5991 class MyWaitingThread : public wxThread
5992 {
5993 public:
5994 MyWaitingThread( wxMutex *mutex, wxCondition *condition )
5995 {
5996 m_mutex = mutex;
5997 m_condition = condition;
5998
5999 Create();
6000 }
6001
6002 virtual ExitCode Entry()
6003 {
6004 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
6005 fflush(stdout);
6006
6007 gs_cond.Post();
6008
6009 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
6010 fflush(stdout);
6011
6012 m_mutex->Lock();
6013 m_condition->Wait();
6014 m_mutex->Unlock();
6015
6016 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
6017 fflush(stdout);
6018
6019 return 0;
6020 }
6021
6022 private:
6023 wxMutex *m_mutex;
6024 wxCondition *m_condition;
6025 };
6026
6027 static void TestThreadConditions()
6028 {
6029 wxMutex mutex;
6030 wxCondition condition(mutex);
6031
6032 // otherwise its difficult to understand which log messages pertain to
6033 // which condition
6034 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
6035 // condition.GetId(), gs_cond.GetId());
6036
6037 // create and launch threads
6038 MyWaitingThread *threads[10];
6039
6040 size_t n;
6041 for ( n = 0; n < WXSIZEOF(threads); n++ )
6042 {
6043 threads[n] = new MyWaitingThread( &mutex, &condition );
6044 }
6045
6046 for ( n = 0; n < WXSIZEOF(threads); n++ )
6047 {
6048 threads[n]->Run();
6049 }
6050
6051 // wait until all threads run
6052 wxPuts(_T("Main thread is waiting for the other threads to start"));
6053 fflush(stdout);
6054
6055 size_t nRunning = 0;
6056 while ( nRunning < WXSIZEOF(threads) )
6057 {
6058 gs_cond.Wait();
6059
6060 nRunning++;
6061
6062 wxPrintf(_T("Main thread: %u already running\n"), nRunning);
6063 fflush(stdout);
6064 }
6065
6066 wxPuts(_T("Main thread: all threads started up."));
6067 fflush(stdout);
6068
6069 wxThread::Sleep(500);
6070
6071 #if 1
6072 // now wake one of them up
6073 wxPrintf(_T("Main thread: about to signal the condition.\n"));
6074 fflush(stdout);
6075 condition.Signal();
6076 #endif
6077
6078 wxThread::Sleep(200);
6079
6080 // wake all the (remaining) threads up, so that they can exit
6081 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
6082 fflush(stdout);
6083 condition.Broadcast();
6084
6085 // give them time to terminate (dirty!)
6086 wxThread::Sleep(500);
6087 }
6088
6089 #include "wx/utils.h"
6090
6091 class MyExecThread : public wxThread
6092 {
6093 public:
6094 MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
6095 m_command(command)
6096 {
6097 Create();
6098 }
6099
6100 virtual ExitCode Entry()
6101 {
6102 return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
6103 }
6104
6105 private:
6106 wxString m_command;
6107 };
6108
6109 static void TestThreadExec()
6110 {
6111 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
6112
6113 MyExecThread thread(_T("true"));
6114 thread.Run();
6115
6116 wxPrintf(_T("Main program exit code: %ld.\n"),
6117 wxExecute(_T("false"), wxEXEC_SYNC));
6118
6119 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
6120 }
6121
6122 // semaphore tests
6123 #include "wx/datetime.h"
6124
6125 class MySemaphoreThread : public wxThread
6126 {
6127 public:
6128 MySemaphoreThread(int i, wxSemaphore *sem)
6129 : wxThread(wxTHREAD_JOINABLE),
6130 m_sem(sem),
6131 m_i(i)
6132 {
6133 Create();
6134 }
6135
6136 virtual ExitCode Entry()
6137 {
6138 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
6139 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
6140
6141 m_sem->Wait();
6142
6143 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
6144 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
6145
6146 Sleep(1000);
6147
6148 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
6149 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
6150
6151 m_sem->Post();
6152
6153 return 0;
6154 }
6155
6156 private:
6157 wxSemaphore *m_sem;
6158 int m_i;
6159 };
6160
6161 WX_DEFINE_ARRAY(wxThread *, ArrayThreads);
6162
6163 static void TestSemaphore()
6164 {
6165 wxPuts(_T("*** Testing wxSemaphore class. ***"));
6166
6167 static const int SEM_LIMIT = 3;
6168
6169 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
6170 ArrayThreads threads;
6171
6172 for ( int i = 0; i < 3*SEM_LIMIT; i++ )
6173 {
6174 threads.Add(new MySemaphoreThread(i, &sem));
6175 threads.Last()->Run();
6176 }
6177
6178 for ( size_t n = 0; n < threads.GetCount(); n++ )
6179 {
6180 threads[n]->Wait();
6181 delete threads[n];
6182 }
6183 }
6184
6185 #endif // TEST_THREADS
6186
6187 // ----------------------------------------------------------------------------
6188 // arrays
6189 // ----------------------------------------------------------------------------
6190
6191 #ifdef TEST_ARRAYS
6192
6193 #include "wx/dynarray.h"
6194
6195 typedef unsigned short ushort;
6196
6197 static int MyStringCompare(wxString* s1, wxString* s2)
6198 {
6199 return wxStrcmp(s1->c_str(), s2->c_str());
6200 }
6201
6202 static int MyStringReverseCompare(wxString* s1, wxString* s2)
6203 {
6204 return -wxStrcmp(s1->c_str(), s2->c_str());
6205 }
6206
6207
6208 #define DefineCompare(name, T) \
6209 \
6210 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
6211 { \
6212 return first - second; \
6213 } \
6214 \
6215 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
6216 { \
6217 return *first - *second; \
6218 } \
6219 \
6220 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
6221 { \
6222 return *second - *first; \
6223 } \
6224
6225 DefineCompare(UShort, ushort);
6226 DefineCompare(Int, int);
6227
6228 // test compilation of all macros
6229 WX_DEFINE_ARRAY_SHORT(ushort, wxArrayUShort);
6230 WX_DEFINE_SORTED_ARRAY_SHORT(ushort, wxSortedArrayUShortNoCmp);
6231 WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort, UShortCompareValues, wxSortedArrayUShort);
6232 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt);
6233
6234 WX_DECLARE_OBJARRAY(Bar, ArrayBars);
6235 #include "wx/arrimpl.cpp"
6236 WX_DEFINE_OBJARRAY(ArrayBars);
6237
6238 static void PrintArray(const wxChar* name, const wxArrayString& array)
6239 {
6240 wxPrintf(_T("Dump of the array '%s'\n"), name);
6241
6242 size_t nCount = array.GetCount();
6243 for ( size_t n = 0; n < nCount; n++ )
6244 {
6245 wxPrintf(_T("\t%s[%u] = '%s'\n"), name, n, array[n].c_str());
6246 }
6247 }
6248
6249 static void PrintArray(const wxChar* name, const wxSortedArrayString& array)
6250 {
6251 wxPrintf(_T("Dump of the array '%s'\n"), name);
6252
6253 size_t nCount = array.GetCount();
6254 for ( size_t n = 0; n < nCount; n++ )
6255 {
6256 wxPrintf(_T("\t%s[%u] = '%s'\n"), name, n, array[n].c_str());
6257 }
6258 }
6259
6260 int wxCMPFUNC_CONV StringLenCompare(const wxString& first,
6261 const wxString& second)
6262 {
6263 return first.length() - second.length();
6264 }
6265
6266 #define TestArrayOf(name) \
6267 \
6268 static void PrintArray(const wxChar* name, const wxSortedArray##name & array) \
6269 { \
6270 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6271 \
6272 size_t nCount = array.GetCount(); \
6273 for ( size_t n = 0; n < nCount; n++ ) \
6274 { \
6275 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6276 } \
6277 } \
6278 \
6279 static void PrintArray(const wxChar* name, const wxArray##name & array) \
6280 { \
6281 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6282 \
6283 size_t nCount = array.GetCount(); \
6284 for ( size_t n = 0; n < nCount; n++ ) \
6285 { \
6286 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6287 } \
6288 } \
6289 \
6290 static void TestArrayOf ## name ## s() \
6291 { \
6292 wxPrintf(_T("*** Testing wxArray%s ***\n"), #name); \
6293 \
6294 wxArray##name a; \
6295 a.Add(1); \
6296 a.Add(17,2); \
6297 a.Add(5,3); \
6298 a.Add(3,4); \
6299 \
6300 wxPuts(_T("Initially:")); \
6301 PrintArray(_T("a"), a); \
6302 \
6303 wxPuts(_T("After sort:")); \
6304 a.Sort(name ## Compare); \
6305 PrintArray(_T("a"), a); \
6306 \
6307 wxPuts(_T("After reverse sort:")); \
6308 a.Sort(name ## RevCompare); \
6309 PrintArray(_T("a"), a); \
6310 \
6311 wxSortedArray##name b; \
6312 b.Add(1); \
6313 b.Add(17); \
6314 b.Add(5); \
6315 b.Add(3); \
6316 \
6317 wxPuts(_T("Sorted array initially:")); \
6318 PrintArray(_T("b"), b); \
6319 }
6320
6321 TestArrayOf(UShort);
6322 TestArrayOf(Int);
6323
6324 static void TestStlArray()
6325 {
6326 wxPuts(_T("*** Testing std::vector operations ***\n"));
6327
6328 {
6329 wxArrayInt list1;
6330 wxArrayInt::iterator it, en;
6331 wxArrayInt::reverse_iterator rit, ren;
6332 int i;
6333 for ( i = 0; i < 5; ++i )
6334 list1.push_back(i);
6335
6336 for ( it = list1.begin(), en = list1.end(), i = 0;
6337 it != en; ++it, ++i )
6338 if ( *it != i )
6339 wxPuts(_T("Error in iterator\n"));
6340
6341 for ( rit = list1.rbegin(), ren = list1.rend(), i = 4;
6342 rit != ren; ++rit, --i )
6343 if ( *rit != i )
6344 wxPuts(_T("Error in reverse_iterator\n"));
6345
6346 if ( *list1.rbegin() != *(list1.end()-1) ||
6347 *list1.begin() != *(list1.rend()-1) )
6348 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6349
6350 it = list1.begin()+1;
6351 rit = list1.rbegin()+1;
6352 if ( *list1.begin() != *(it-1) ||
6353 *list1.rbegin() != *(rit-1) )
6354 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6355
6356 if ( list1.front() != 0 || list1.back() != 4 )
6357 wxPuts(_T("Error in front()/back()\n"));
6358
6359 list1.erase(list1.begin());
6360 list1.erase(list1.end()-1);
6361
6362 for ( it = list1.begin(), en = list1.end(), i = 1;
6363 it != en; ++it, ++i )
6364 if ( *it != i )
6365 wxPuts(_T("Error in erase()\n"));
6366 }
6367
6368 wxPuts(_T("*** Testing std::vector operations finished ***\n"));
6369 }
6370
6371 static void TestArrayOfObjects()
6372 {
6373 wxPuts(_T("*** Testing wxObjArray ***\n"));
6374
6375 {
6376 ArrayBars bars;
6377 Bar bar(_T("second bar (two copies!)"));
6378
6379 wxPrintf(_T("Initially: %u objects in the array, %u objects total.\n"),
6380 bars.GetCount(), Bar::GetNumber());
6381
6382 bars.Add(new Bar(_T("first bar")));
6383 bars.Add(bar,2);
6384
6385 wxPrintf(_T("Now: %u objects in the array, %u objects total.\n"),
6386 bars.GetCount(), Bar::GetNumber());
6387
6388 bars.RemoveAt(1, bars.GetCount() - 1);
6389
6390 wxPrintf(_T("After removing all but first element: %u objects in the ")
6391 _T("array, %u objects total.\n"),
6392 bars.GetCount(), Bar::GetNumber());
6393
6394 bars.Empty();
6395
6396 wxPrintf(_T("After Empty(): %u objects in the array, %u objects total.\n"),
6397 bars.GetCount(), Bar::GetNumber());
6398 }
6399
6400 wxPrintf(_T("Finally: no more objects in the array, %u objects total.\n"),
6401 Bar::GetNumber());
6402 }
6403
6404 #endif // TEST_ARRAYS
6405
6406 // ----------------------------------------------------------------------------
6407 // strings
6408 // ----------------------------------------------------------------------------
6409
6410 #ifdef TEST_STRINGS
6411
6412 #include "wx/timer.h"
6413 #include "wx/tokenzr.h"
6414
6415 static void TestStringConstruction()
6416 {
6417 wxPuts(_T("*** Testing wxString constructores ***"));
6418
6419 #define TEST_CTOR(args, res) \
6420 { \
6421 wxString s args ; \
6422 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
6423 if ( s == res ) \
6424 { \
6425 wxPuts(_T("(ok)")); \
6426 } \
6427 else \
6428 { \
6429 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
6430 } \
6431 }
6432
6433 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
6434 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
6435 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
6436 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
6437
6438 static const wxChar *s = _T("?really!");
6439 const wxChar *start = wxStrchr(s, _T('r'));
6440 const wxChar *end = wxStrchr(s, _T('!'));
6441 TEST_CTOR((start, end), _T("really"));
6442
6443 wxPuts(_T(""));
6444 }
6445
6446 static void TestString()
6447 {
6448 wxStopWatch sw;
6449
6450 wxString a, b, c;
6451
6452 a.reserve (128);
6453 b.reserve (128);
6454 c.reserve (128);
6455
6456 for (int i = 0; i < 1000000; ++i)
6457 {
6458 a = _T("Hello");
6459 b = _T(" world");
6460 c = _T("! How'ya doin'?");
6461 a += b;
6462 a += c;
6463 c = _T("Hello world! What's up?");
6464 if (c != a)
6465 c = _T("Doh!");
6466 }
6467
6468 wxPrintf(_T("TestString elapsed time: %ld\n"), sw.Time());
6469 }
6470
6471 static void TestPChar()
6472 {
6473 wxStopWatch sw;
6474
6475 wxChar a [128];
6476 wxChar b [128];
6477 wxChar c [128];
6478
6479 for (int i = 0; i < 1000000; ++i)
6480 {
6481 wxStrcpy (a, _T("Hello"));
6482 wxStrcpy (b, _T(" world"));
6483 wxStrcpy (c, _T("! How'ya doin'?"));
6484 wxStrcat (a, b);
6485 wxStrcat (a, c);
6486 wxStrcpy (c, _T("Hello world! What's up?"));
6487 if (wxStrcmp (c, a) == 0)
6488 wxStrcpy (c, _T("Doh!"));
6489 }
6490
6491 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw.Time());
6492 }
6493
6494 static void TestStringSub()
6495 {
6496 wxString s(_T("Hello, world!"));
6497
6498 wxPuts(_T("*** Testing wxString substring extraction ***"));
6499
6500 wxPrintf(_T("String = '%s'\n"), s.c_str());
6501 wxPrintf(_T("Left(5) = '%s'\n"), s.Left(5).c_str());
6502 wxPrintf(_T("Right(6) = '%s'\n"), s.Right(6).c_str());
6503 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
6504 wxPrintf(_T("Mid(3) = '%s'\n"), s.Mid(3).c_str());
6505 wxPrintf(_T("substr(3, 5) = '%s'\n"), s.substr(3, 5).c_str());
6506 wxPrintf(_T("substr(3) = '%s'\n"), s.substr(3).c_str());
6507
6508 static const wxChar *prefixes[] =
6509 {
6510 _T("Hello"),
6511 _T("Hello, "),
6512 _T("Hello, world!"),
6513 _T("Hello, world!!!"),
6514 _T(""),
6515 _T("Goodbye"),
6516 _T("Hi"),
6517 };
6518
6519 for ( size_t n = 0; n < WXSIZEOF(prefixes); n++ )
6520 {
6521 wxString prefix = prefixes[n], rest;
6522 bool rc = s.StartsWith(prefix, &rest);
6523 wxPrintf(_T("StartsWith('%s') = %s"), prefix.c_str(), rc ? _T("true") : _T("false"));
6524 if ( rc )
6525 {
6526 wxPrintf(_T(" (the rest is '%s')\n"), rest.c_str());
6527 }
6528 else
6529 {
6530 putchar('\n');
6531 }
6532 }
6533
6534 wxPuts(_T(""));
6535 }
6536
6537 static void TestStringFormat()
6538 {
6539 wxPuts(_T("*** Testing wxString formatting ***"));
6540
6541 wxString s;
6542 s.Printf(_T("%03d"), 18);
6543
6544 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6545 wxPrintf(_T("Number 18: %s\n"), s.c_str());
6546
6547 wxPuts(_T(""));
6548 }
6549
6550 // returns "not found" for npos, value for all others
6551 static wxString PosToString(size_t res)
6552 {
6553 wxString s = res == wxString::npos ? wxString(_T("not found"))
6554 : wxString::Format(_T("%u"), res);
6555 return s;
6556 }
6557
6558 static void TestStringFind()
6559 {
6560 wxPuts(_T("*** Testing wxString find() functions ***"));
6561
6562 static const wxChar *strToFind = _T("ell");
6563 static const struct StringFindTest
6564 {
6565 const wxChar *str;
6566 size_t start,
6567 result; // of searching "ell" in str
6568 } findTestData[] =
6569 {
6570 { _T("Well, hello world"), 0, 1 },
6571 { _T("Well, hello world"), 6, 7 },
6572 { _T("Well, hello world"), 9, wxString::npos },
6573 };
6574
6575 for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
6576 {
6577 const StringFindTest& ft = findTestData[n];
6578 size_t res = wxString(ft.str).find(strToFind, ft.start);
6579
6580 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6581 strToFind, ft.str, ft.start, PosToString(res).c_str());
6582
6583 size_t resTrue = ft.result;
6584 if ( res == resTrue )
6585 {
6586 wxPuts(_T("(ok)"));
6587 }
6588 else
6589 {
6590 wxPrintf(_T("(ERROR: should be %s)\n"),
6591 PosToString(resTrue).c_str());
6592 }
6593 }
6594
6595 wxPuts(_T(""));
6596 }
6597
6598 static void TestStringTokenizer()
6599 {
6600 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6601
6602 static const wxChar *modeNames[] =
6603 {
6604 _T("default"),
6605 _T("return empty"),
6606 _T("return all empty"),
6607 _T("with delims"),
6608 _T("like strtok"),
6609 };
6610
6611 static const struct StringTokenizerTest
6612 {
6613 const wxChar *str; // string to tokenize
6614 const wxChar *delims; // delimiters to use
6615 size_t count; // count of token
6616 wxStringTokenizerMode mode; // how should we tokenize it
6617 } tokenizerTestData[] =
6618 {
6619 { _T(""), _T(" "), 0 },
6620 { _T("Hello, world"), _T(" "), 2 },
6621 { _T("Hello, world "), _T(" "), 2 },
6622 { _T("Hello, world"), _T(","), 2 },
6623 { _T("Hello, world!"), _T(",!"), 2 },
6624 { _T("Hello,, world!"), _T(",!"), 3 },
6625 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL },
6626 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6627 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 },
6628 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY },
6629 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL },
6630 { _T("01/02/99"), _T("/-"), 3 },
6631 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS },
6632 };
6633
6634 for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
6635 {
6636 const StringTokenizerTest& tt = tokenizerTestData[n];
6637 wxStringTokenizer tkz(tt.str, tt.delims, tt.mode);
6638
6639 size_t count = tkz.CountTokens();
6640 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6641 MakePrintable(tt.str).c_str(),
6642 count,
6643 MakePrintable(tt.delims).c_str(),
6644 modeNames[tkz.GetMode()]);
6645 if ( count == tt.count )
6646 {
6647 wxPuts(_T("(ok)"));
6648 }
6649 else
6650 {
6651 wxPrintf(_T("(ERROR: should be %u)\n"), tt.count);
6652
6653 continue;
6654 }
6655
6656 // if we emulate strtok(), check that we do it correctly
6657 wxChar *buf, *s = NULL, *last;
6658
6659 if ( tkz.GetMode() == wxTOKEN_STRTOK )
6660 {
6661 buf = new wxChar[wxStrlen(tt.str) + 1];
6662 wxStrcpy(buf, tt.str);
6663
6664 s = wxStrtok(buf, tt.delims, &last);
6665 }
6666 else
6667 {
6668 buf = NULL;
6669 }
6670
6671 // now show the tokens themselves
6672 size_t count2 = 0;
6673 while ( tkz.HasMoreTokens() )
6674 {
6675 wxString token = tkz.GetNextToken();
6676
6677 wxPrintf(_T("\ttoken %u: '%s'"),
6678 ++count2,
6679 MakePrintable(token).c_str());
6680
6681 if ( buf )
6682 {
6683 if ( token == s )
6684 {
6685 wxPuts(_T(" (ok)"));
6686 }
6687 else
6688 {
6689 wxPrintf(_T(" (ERROR: should be %s)\n"), s);
6690 }
6691
6692 s = wxStrtok(NULL, tt.delims, &last);
6693 }
6694 else
6695 {
6696 // nothing to compare with
6697 wxPuts(_T(""));
6698 }
6699 }
6700
6701 if ( count2 != count )
6702 {
6703 wxPuts(_T("\tERROR: token count mismatch"));
6704 }
6705
6706 delete [] buf;
6707 }
6708
6709 wxPuts(_T(""));
6710 }
6711
6712 static void TestStringReplace()
6713 {
6714 wxPuts(_T("*** Testing wxString::replace ***"));
6715
6716 static const struct StringReplaceTestData
6717 {
6718 const wxChar *original; // original test string
6719 size_t start, len; // the part to replace
6720 const wxChar *replacement; // the replacement string
6721 const wxChar *result; // and the expected result
6722 } stringReplaceTestData[] =
6723 {
6724 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6725 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6726 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6727 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6728 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6729 };
6730
6731 for ( size_t n = 0; n < WXSIZEOF(stringReplaceTestData); n++ )
6732 {
6733 const StringReplaceTestData data = stringReplaceTestData[n];
6734
6735 wxString original = data.original;
6736 original.replace(data.start, data.len, data.replacement);
6737
6738 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6739 data.original, data.start, data.len, data.replacement,
6740 original.c_str());
6741
6742 if ( original == data.result )
6743 {
6744 wxPuts(_T("(ok)"));
6745 }
6746 else
6747 {
6748 wxPrintf(_T("(ERROR: should be '%s')\n"), data.result);
6749 }
6750 }
6751
6752 wxPuts(_T(""));
6753 }
6754
6755 static void TestStringMatch()
6756 {
6757 wxPuts(_T("*** Testing wxString::Matches() ***"));
6758
6759 static const struct StringMatchTestData
6760 {
6761 const wxChar *text;
6762 const wxChar *wildcard;
6763 bool matches;
6764 } stringMatchTestData[] =
6765 {
6766 { _T("foobar"), _T("foo*"), 1 },
6767 { _T("foobar"), _T("*oo*"), 1 },
6768 { _T("foobar"), _T("*bar"), 1 },
6769 { _T("foobar"), _T("??????"), 1 },
6770 { _T("foobar"), _T("f??b*"), 1 },
6771 { _T("foobar"), _T("f?b*"), 0 },
6772 { _T("foobar"), _T("*goo*"), 0 },
6773 { _T("foobar"), _T("*foo"), 0 },
6774 { _T("foobarfoo"), _T("*foo"), 1 },
6775 { _T(""), _T("*"), 1 },
6776 { _T(""), _T("?"), 0 },
6777 };
6778
6779 for ( size_t n = 0; n < WXSIZEOF(stringMatchTestData); n++ )
6780 {
6781 const StringMatchTestData& data = stringMatchTestData[n];
6782 bool matches = wxString(data.text).Matches(data.wildcard);
6783 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6784 data.wildcard,
6785 matches ? _T("matches") : _T("doesn't match"),
6786 data.text,
6787 matches == data.matches ? _T("ok") : _T("ERROR"));
6788 }
6789
6790 wxPuts(_T(""));
6791 }
6792
6793 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6794 void ok(int line, bool ok, const wxString& msg = wxEmptyString)
6795 {
6796 if( !ok )
6797 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line) +
6798 _T(") ") + msg);
6799 }
6800
6801 void is(int line, const wxString& got, const wxString& expected,
6802 const wxString& msg = wxEmptyString)
6803 {
6804 bool isOk = got == expected;
6805 ok(line, isOk, msg);
6806 if( !isOk )
6807 {
6808 wxPuts(_T("Got: ") + got);
6809 wxPuts(_T("Expected: ") + expected);
6810 }
6811 }
6812
6813 #if 0
6814 void is(int line, const wxChar* got, const wxChar* expected,
6815 const wxString& msg = wxEmptyString)
6816 {
6817 bool isOk = wxStrcmp( got, expected ) == 0;
6818 ok(line, isOk, msg);
6819 if( !isOk )
6820 {
6821 wxPuts(_T("Got: ") + wxString(got));
6822 wxPuts(_T("Expected: ") + wxString(expected));
6823 }
6824 }
6825 #endif
6826
6827 void is(int line, const wxChar& got, const wxChar& expected,
6828 const wxString& msg = wxEmptyString)
6829 {
6830 bool isOk = got == expected;
6831 ok(line, isOk, msg);
6832 if( !isOk )
6833 {
6834 wxPuts(_T("Got: ") + got);
6835 wxPuts(_T("Expected: ") + expected);
6836 }
6837 }
6838
6839 void is(int line, size_t got, size_t expected,
6840 const wxString& msg = wxEmptyString)
6841 {
6842 bool isOk = got == expected;
6843 ok(line, isOk, msg);
6844 if( !isOk )
6845 {
6846 wxPuts(wxString::Format(_T("Got: %ld"), got));
6847 wxPuts(wxString::Format(_T("Expected: %ld"), expected));
6848 }
6849 }
6850
6851 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6852 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6853
6854 void TestStdString()
6855 {
6856 wxPuts(_T("*** Testing std::string operations ***\n"));
6857
6858 // test ctors
6859 wxString s1(_T("abcdefgh")),
6860 s2(_T("abcdefghijklm"), 8),
6861 s3(_T("abcdefghijklm")),
6862 s4(8, _T('a'));
6863 wxString s5(s1),
6864 s6(s3, 0, 8),
6865 s7(s3.begin(), s3.begin() + 8);
6866 wxString s8(s1, 4, 8), s9, s10, s11;
6867
6868 is( __LINE__, s1, _T("abcdefgh") );
6869 is( __LINE__, s2, s1 );
6870 is( __LINE__, s4, _T("aaaaaaaa") );
6871 is( __LINE__, s5, _T("abcdefgh") );
6872 is( __LINE__, s6, s1 );
6873 is( __LINE__, s7, s1 );
6874 is( __LINE__, s8, _T("efgh") );
6875
6876 // test append
6877 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc");
6878 s1.append(_T("def"));
6879 s2.append(_T("defgh"), 3);
6880 s3.append(wxString(_T("abcdef")), 3, 6);
6881 s4.append(s1);
6882 s5.append(3, _T('a'));
6883 s6.append(s1.begin() + 3, s1.end());
6884
6885 is( __LINE__, s1, _T("abcdef") );
6886 is( __LINE__, s2, _T("abcdef") );
6887 is( __LINE__, s3, _T("abcdef") );
6888 is( __LINE__, s4, _T("abcabcdef") );
6889 is( __LINE__, s5, _T("abcaaa") );
6890 is( __LINE__, s6, _T("abcdef") );
6891
6892 // test assign
6893 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc");
6894 s1.assign(_T("def"));
6895 s2.assign(_T("defgh"), 3);
6896 s3.assign(wxString(_T("abcdef")), 3, 6);
6897 s4.assign(s1);
6898 s5.assign(3, _T('a'));
6899 s6.assign(s1.begin() + 1, s1.end());
6900
6901 is( __LINE__, s1, _T("def") );
6902 is( __LINE__, s2, _T("def") );
6903 is( __LINE__, s3, _T("def") );
6904 is( __LINE__, s4, _T("def") );
6905 is( __LINE__, s5, _T("aaa") );
6906 is( __LINE__, s6, _T("ef") );
6907
6908 // test compare
6909 s1 = _T("abcdefgh");
6910 s2 = _T("abcdefgh");
6911 s3 = _T("abc");
6912 s4 = _T("abcdefghi");
6913 s5 = _T("aaa");
6914 s6 = _T("zzz");
6915 s7 = _T("zabcdefg");
6916
6917 ok( __LINE__, s1.compare(s2) == 0 );
6918 ok( __LINE__, s1.compare(s3) > 0 );
6919 ok( __LINE__, s1.compare(s4) < 0 );
6920 ok( __LINE__, s1.compare(s5) > 0 );
6921 ok( __LINE__, s1.compare(s6) < 0 );
6922 ok( __LINE__, s1.compare(1, 12, s1) > 0);
6923 ok( __LINE__, s1.compare(_T("abcdefgh")) == 0);
6924 ok( __LINE__, s1.compare(1, 7, _T("bcdefgh")) == 0);
6925 ok( __LINE__, s1.compare(1, 7, _T("bcdefgh"), 7) == 0);
6926
6927 // test erase
6928 s1.erase(1, 1);
6929 s2.erase(4, 12);
6930 wxString::iterator it = s3.erase(s3.begin() + 1);
6931 wxString::iterator it2 = s4.erase(s4.begin() + 4, s4.begin() + 6);
6932 wxString::iterator it3 = s7.erase(s7.begin() + 4, s7.begin() + 8);
6933
6934 is( __LINE__, s1, _T("acdefgh") );
6935 is( __LINE__, s2, _T("abcd") );
6936 is( __LINE__, s3, _T("ac") );
6937 is( __LINE__, s4, _T("abcdghi") );
6938 is( __LINE__, s7, _T("zabc") );
6939 is( __LINE__, *it, _T('c') );
6940 is( __LINE__, *it2, _T('g') );
6941 ok( __LINE__, it3 == s7.end() );
6942
6943 // find
6944 // 0 1 2
6945 // 01234567890123456789012345
6946 s1 = _T("abcdefgABCDEFGabcABCabcABC");
6947 s2 = _T("gAB");
6948
6949 is_nom( s1.find(_T('A')), 7u );
6950 is_nom( s1.find(_T('A'), 7), 7u );
6951 is_nom( s1.find(_T('Z')), wxString::npos );
6952 is_nom( s1.find(_T('C'), 22), 25u );
6953
6954 is_nom( s1.find(_T("gAB")), 6u );
6955 is_nom( s1.find(_T("gAB"), 7), wxString::npos );
6956 is_nom( s1.find(_T("gAB"), 6), 6u );
6957
6958 is_nom( s1.find(_T("gABZZZ"), 2, 3), 6u );
6959 is_nom( s1.find(_T("gABZZZ"), 7, 3), wxString::npos );
6960
6961 is_nom( s1.find(s2), 6u );
6962 is_nom( s1.find(s2, 7), wxString::npos );
6963 is_nom( s1.find(s2, 6), 6u );
6964
6965 // find_first_not_of
6966 // 0 1 2 3
6967 // 01234567890123456789012345678901234
6968 s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6969 s2 = _T("aaaaaa");
6970
6971 is_nom( s1.find_first_not_of(_T('a')), 6u );
6972 is_nom( s1.find_first_not_of(_T('a'), 7), 7u );
6973 is_nom( s2.find_first_not_of(_T('a')), wxString::npos );
6974
6975 is_nom( s1.find_first_not_of(_T("abde"), 4), 7u );
6976 is_nom( s1.find_first_not_of(_T("abde"), 7), 7u );
6977 is_nom( s1.find_first_not_of(_T("abcdefghijkl")), wxString::npos );
6978
6979 is_nom( s1.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
6980
6981 // find_first_of
6982 is_nom( s1.find_first_of(_T('c')), 7u );
6983 is_nom( s1.find_first_of(_T('v')), wxString::npos );
6984 is_nom( s1.find_first_of(_T('c'), 10), 24u );
6985
6986 is_nom( s1.find_first_of(_T("ijkl")), 13u );
6987 is_nom( s1.find_first_of(_T("ddcfg"), 17), 24u );
6988 is_nom( s1.find_first_of(_T("ddcfga"), 17, 5), 24u );
6989
6990 // find_last_not_of
6991 // 0 1 2 3
6992 // 01234567890123456789012345678901234
6993 s1 = _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6994 s2 = _T("aaaaaa");
6995
6996 is_nom( s2.find_last_not_of(_T('a')), wxString::npos );
6997 is_nom( s1.find_last_not_of(_T('d')), 33u );
6998 is_nom( s1.find_last_not_of(_T('d'), 25), 24u );
6999
7000 is_nom( s1.find_last_not_of(_T("bcd")), 22u );
7001 is_nom( s1.find_last_not_of(_T("abc"), 24), 16u );
7002
7003 is_nom( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
7004
7005 // find_last_of
7006 is_nom( s2.find_last_of(_T('c')), wxString::npos );
7007 is_nom( s1.find_last_of(_T('a')), 22u );
7008 is_nom( s1.find_last_of(_T('b'), 24), 23u );
7009
7010 is_nom( s1.find_last_of(_T("ijklm")), 16u );
7011 is_nom( s1.find_last_of(_T("ijklma"), 33, 4), 16u );
7012 is_nom( s1.find_last_of(_T("a"), 17), 17u );
7013
7014 // test insert
7015 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("aaaa");
7016 s9 = s10 = _T("cdefg");
7017
7018 s1.insert(1, _T("cc") );
7019 s2.insert(2, _T("cdef"), 3);
7020 s3.insert(2, s10);
7021 s4.insert(2, s10, 3, 7);
7022 s5.insert(1, 2, _T('c'));
7023 it = s6.insert(s6.begin() + 3, _T('X'));
7024 s7.insert(s7.begin(), s9.begin(), s9.end() - 1);
7025 s8.insert(s8.begin(), 2, _T('c'));
7026
7027 is( __LINE__, s1, _T("accaaa") );
7028 is( __LINE__, s2, _T("aacdeaa") );
7029 is( __LINE__, s3, _T("aacdefgaa") );
7030 is( __LINE__, s4, _T("aafgaa") );
7031 is( __LINE__, s5, _T("accaaa") );
7032 is( __LINE__, s6, _T("aaaXa") );
7033 is( __LINE__, s7, _T("cdefaaaa") );
7034 is( __LINE__, s8, _T("ccaaaa") );
7035
7036 s1 = s2 = s3 = _T("aaaa");
7037 s1.insert(0, _T("ccc"), 2);
7038 s2.insert(4, _T("ccc"), 2);
7039
7040 is( __LINE__, s1, _T("ccaaaa") );
7041 is( __LINE__, s2, _T("aaaacc") );
7042
7043 // test replace
7044 s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("QWERTYUIOP");
7045 s9 = s10 = _T("werty");
7046
7047 s1.replace(3, 4, _T("rtyu"));
7048 s1.replace(8, 7, _T("opopop"));
7049 s2.replace(10, 12, _T("WWWW"));
7050 s3.replace(1, 5, s9);
7051 s4.replace(1, 4, s9, 0, 4);
7052 s5.replace(1, 2, s9, 1, 12);
7053 s6.replace(0, 123, s9, 0, 123);
7054 s7.replace(2, 7, s9);
7055
7056 is( __LINE__, s1, _T("QWErtyuIopopop") );
7057 is( __LINE__, s2, _T("QWERTYUIOPWWWW") );
7058 is( __LINE__, s3, _T("QwertyUIOP") );
7059 is( __LINE__, s4, _T("QwertYUIOP") );
7060 is( __LINE__, s5, _T("QertyRTYUIOP") );
7061 is( __LINE__, s6, s9);
7062 is( __LINE__, s7, _T("QWwertyP") );
7063
7064 // rfind
7065 // 0 1 2
7066 // 01234567890123456789012345
7067 s1 = _T("abcdefgABCDEFGabcABCabcABC");
7068 s2 = _T("gAB");
7069 s3 = _T("ab");
7070
7071 is_nom( s1.rfind(_T('A')), 23u );
7072 is_nom( s1.rfind(_T('A'), 7), 7u );
7073 is_nom( s1.rfind(_T('Z')), wxString::npos );
7074 is_nom( s1.rfind(_T('C'), 22), 19u );
7075
7076 is_nom( s1.rfind(_T("cAB")), 22u );
7077 is_nom( s1.rfind(_T("cAB"), 15), wxString::npos );
7078 is_nom( s1.rfind(_T("cAB"), 21), 16u );
7079
7080 is_nom( s1.rfind(_T("gABZZZ"), 7, 3), 6u );
7081 is_nom( s1.rfind(_T("gABZZZ"), 5, 3), wxString::npos );
7082
7083 is_nom( s1.rfind(s2), 6u );
7084 is_nom( s1.rfind(s2, 5), wxString::npos );
7085 is_nom( s1.rfind(s2, 6), 6u );
7086 is_nom( s1.rfind(s3, 1), 0u );
7087
7088 // resize
7089 s1 = s2 = s3 = s4 = _T("abcABCdefDEF");
7090
7091 s1.resize( 12 );
7092 s2.resize( 10 );
7093 s3.resize( 14, _T(' ') );
7094 s4.resize( 14, _T('W') );
7095
7096 is_nom( s1, _T("abcABCdefDEF") );
7097 is_nom( s2, _T("abcABCdefD") );
7098 is_nom( s3, _T("abcABCdefDEF ") );
7099 is_nom( s4, _T("abcABCdefDEFWW") );
7100
7101 // substr
7102 s1 = _T("abcdefgABCDEFG");
7103
7104 is_nom( s1.substr( 0, 14 ), s1 );
7105 is_nom( s1.substr( 1, 13 ), _T("bcdefgABCDEFG") );
7106 is_nom( s1.substr( 1, 20 ), _T("bcdefgABCDEFG") );
7107 is_nom( s1.substr( 14, 30 ), _T("") );
7108
7109 wxPuts(_T("*** Testing std::string operations finished ***\n"));
7110 }
7111
7112 #endif // TEST_STRINGS
7113
7114 // ----------------------------------------------------------------------------
7115 // entry point
7116 // ----------------------------------------------------------------------------
7117
7118 #ifdef TEST_SNGLINST
7119 #include "wx/snglinst.h"
7120 #endif // TEST_SNGLINST
7121
7122 int main(int argc, char **argv)
7123 {
7124 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
7125
7126 wxInitializer initializer;
7127 if ( !initializer )
7128 {
7129 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
7130
7131 return -1;
7132 }
7133
7134 #ifdef TEST_SNGLINST
7135 wxSingleInstanceChecker checker;
7136 if ( checker.Create(_T(".wxconsole.lock")) )
7137 {
7138 if ( checker.IsAnotherRunning() )
7139 {
7140 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
7141
7142 return 1;
7143 }
7144
7145 // wait some time to give time to launch another instance
7146 wxPrintf(_T("Press \"Enter\" to continue..."));
7147 wxFgetc(stdin);
7148 }
7149 else // failed to create
7150 {
7151 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
7152 }
7153 #endif // TEST_SNGLINST
7154
7155 #ifdef TEST_CHARSET
7156 TestCharset();
7157 #endif // TEST_CHARSET
7158
7159 #ifdef TEST_CMDLINE
7160 TestCmdLineConvert();
7161
7162 #if wxUSE_CMDLINE_PARSER
7163 static const wxCmdLineEntryDesc cmdLineDesc[] =
7164 {
7165 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"),
7166 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
7167 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
7168 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
7169
7170 { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file") },
7171 { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("input dir") },
7172 { wxCMD_LINE_OPTION, _T("s"), _T("size"), _T("output block size"),
7173 wxCMD_LINE_VAL_NUMBER },
7174 { wxCMD_LINE_OPTION, _T("d"), _T("date"), _T("output file date"),
7175 wxCMD_LINE_VAL_DATE },
7176
7177 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"),
7178 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
7179
7180 { wxCMD_LINE_NONE }
7181 };
7182
7183 #if wxUSE_UNICODE
7184 wxChar **wargv = new wxChar *[argc + 1];
7185
7186 {
7187 int n;
7188
7189 for (n = 0; n < argc; n++ )
7190 {
7191 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
7192 wargv[n] = wxStrdup(warg);
7193 }
7194
7195 wargv[n] = NULL;
7196 }
7197
7198 #define argv wargv
7199 #endif // wxUSE_UNICODE
7200
7201 wxCmdLineParser parser(cmdLineDesc, argc, argv);
7202
7203 #if wxUSE_UNICODE
7204 {
7205 for ( int n = 0; n < argc; n++ )
7206 free(wargv[n]);
7207
7208 delete [] wargv;
7209 }
7210 #endif // wxUSE_UNICODE
7211
7212 parser.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
7213 wxCMD_LINE_VAL_STRING,
7214 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
7215
7216 switch ( parser.Parse() )
7217 {
7218 case -1:
7219 wxLogMessage(_T("Help was given, terminating."));
7220 break;
7221
7222 case 0:
7223 ShowCmdLine(parser);
7224 break;
7225
7226 default:
7227 wxLogMessage(_T("Syntax error detected, aborting."));
7228 break;
7229 }
7230 #endif // wxUSE_CMDLINE_PARSER
7231
7232 #endif // TEST_CMDLINE
7233
7234 #ifdef TEST_STRINGS
7235 if ( TEST_ALL )
7236 {
7237 TestPChar();
7238 TestString();
7239 TestStringSub();
7240 TestStringConstruction();
7241 TestStringFormat();
7242 TestStringFind();
7243 TestStringTokenizer();
7244 TestStringReplace();
7245 }
7246 else
7247 {
7248 TestStringMatch();
7249 }
7250
7251 TestStdString();
7252 #endif // TEST_STRINGS
7253
7254 #ifdef TEST_ARRAYS
7255 if ( 1 || TEST_ALL )
7256 {
7257 wxArrayString a1;
7258 a1.Add(_T("tiger"));
7259 a1.Add(_T("cat"));
7260 a1.Add(_T("lion"), 3);
7261 a1.Add(_T("dog"));
7262 a1.Add(_T("human"));
7263 a1.Add(_T("ape"));
7264
7265 wxPuts(_T("*** Initially:"));
7266
7267 PrintArray(_T("a1"), a1);
7268
7269 wxArrayString a2(a1);
7270 PrintArray(_T("a2"), a2);
7271
7272 #if !wxUSE_STL
7273 wxSortedArrayString a3(a1);
7274 #else
7275 wxSortedArrayString a3;
7276 for (wxArrayString::iterator it = a1.begin(), en = a1.end();
7277 it != en; ++it)
7278 a3.Add(*it);
7279 #endif
7280 PrintArray(_T("a3"), a3);
7281
7282 wxPuts(_T("*** After deleting three strings from a1"));
7283 a1.RemoveAt(2,3);
7284
7285 PrintArray(_T("a1"), a1);
7286 PrintArray(_T("a2"), a2);
7287 PrintArray(_T("a3"), a3);
7288
7289 #if !wxUSE_STL
7290 wxPuts(_T("*** After reassigning a1 to a2 and a3"));
7291 a3 = a2 = a1;
7292 PrintArray(_T("a2"), a2);
7293 PrintArray(_T("a3"), a3);
7294 #endif
7295
7296 wxPuts(_T("*** After sorting a1"));
7297 a1.Sort(false);
7298 PrintArray(_T("a1"), a1);
7299
7300 wxPuts(_T("*** After sorting a1 in reverse order"));
7301 a1.Sort(true);
7302 PrintArray(_T("a1"), a1);
7303
7304 #if !wxUSE_STL
7305 wxPuts(_T("*** After sorting a1 by the string length"));
7306 a1.Sort(&StringLenCompare);
7307 PrintArray(_T("a1"), a1);
7308 #endif
7309
7310 TestArrayOfObjects();
7311 TestArrayOfUShorts();
7312 }
7313
7314 TestArrayOfInts();
7315 TestStlArray();
7316 #endif // TEST_ARRAYS
7317
7318 #ifdef TEST_DIR
7319 if ( TEST_ALL )
7320 {
7321 TestDirExists();
7322 TestDirEnum();
7323 }
7324 TestDirTraverse();
7325 #endif // TEST_DIR
7326
7327 #ifdef TEST_DLLLOADER
7328 TestDllLoad();
7329 #endif // TEST_DLLLOADER
7330
7331 #ifdef TEST_ENVIRON
7332 TestEnvironment();
7333 #endif // TEST_ENVIRON
7334
7335 #ifdef TEST_EXECUTE
7336 TestExecute();
7337 #endif // TEST_EXECUTE
7338
7339 #ifdef TEST_FILECONF
7340 TestFileConfRead();
7341 #endif // TEST_FILECONF
7342
7343 #ifdef TEST_LIST
7344 TestListCtor();
7345 TestList();
7346 #endif // TEST_LIST
7347
7348 #ifdef TEST_LOCALE
7349 TestDefaultLang();
7350 #endif // TEST_LOCALE
7351
7352 #ifdef TEST_LOG
7353 wxPuts(_T("*** Testing wxLog ***"));
7354
7355 wxString s;
7356 for ( size_t n = 0; n < 8000; n++ )
7357 {
7358 s << (wxChar)(_T('A') + (n % 26));
7359 }
7360
7361 wxLogWarning(_T("The length of the string is %lu"),
7362 (unsigned long)s.length());
7363
7364 wxString msg;
7365 msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str());
7366
7367 // this one shouldn't be truncated
7368 wxPrintf(msg);
7369
7370 // but this one will because log functions use fixed size buffer
7371 // (note that it doesn't need '\n' at the end neither - will be added
7372 // by wxLog anyhow)
7373 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s.c_str());
7374 #endif // TEST_LOG
7375
7376 #ifdef TEST_FILE
7377 if ( TEST_ALL )
7378 {
7379 TestFileRead();
7380 TestTextFileRead();
7381 TestFileCopy();
7382 }
7383 #endif // TEST_FILE
7384
7385 #ifdef TEST_FILENAME
7386 if ( TEST_ALL )
7387 {
7388 TestFileNameConstruction();
7389 TestFileNameMakeRelative();
7390 TestFileNameMakeAbsolute();
7391 TestFileNameSplit();
7392 TestFileNameTemp();
7393 TestFileNameCwd();
7394 TestFileNameDirManip();
7395 TestFileNameComparison();
7396 TestFileNameOperations();
7397 }
7398 #endif // TEST_FILENAME
7399
7400 #ifdef TEST_FILETIME
7401 TestFileGetTimes();
7402 if ( 0 )
7403 TestFileSetTimes();
7404 #endif // TEST_FILETIME
7405
7406 #ifdef TEST_FTP
7407 wxLog::AddTraceMask(FTP_TRACE_MASK);
7408 if ( TestFtpConnect() )
7409 {
7410 if ( TEST_ALL )
7411 {
7412 TestFtpList();
7413 TestFtpDownload();
7414 TestFtpMisc();
7415 TestFtpFileSize();
7416 TestFtpUpload();
7417 }
7418
7419 if ( TEST_INTERACTIVE )
7420 TestFtpInteractive();
7421 }
7422 //else: connecting to the FTP server failed
7423
7424 if ( 0 )
7425 TestFtpWuFtpd();
7426 #endif // TEST_FTP
7427
7428 #ifdef TEST_LONGLONG
7429 // seed pseudo random generator
7430 srand((unsigned)time(NULL));
7431
7432 if ( 0 )
7433 {
7434 TestSpeed();
7435 }
7436
7437 if ( TEST_ALL )
7438 {
7439 TestMultiplication();
7440 TestDivision();
7441 TestAddition();
7442 TestLongLongConversion();
7443 TestBitOperations();
7444 TestLongLongComparison();
7445 TestLongLongToString();
7446 TestLongLongPrintf();
7447 }
7448 #endif // TEST_LONGLONG
7449
7450 #ifdef TEST_HASH
7451 TestHash();
7452 #endif // TEST_HASH
7453
7454 #ifdef TEST_HASHMAP
7455 TestHashMap();
7456 #endif // TEST_HASHMAP
7457
7458 #ifdef TEST_HASHSET
7459 TestHashSet();
7460 #endif // TEST_HASHSET
7461
7462 #ifdef TEST_MIME
7463 wxLog::AddTraceMask(_T("mime"));
7464 if ( TEST_ALL )
7465 {
7466 TestMimeEnum();
7467 TestMimeOverride();
7468 TestMimeAssociate();
7469 }
7470 TestMimeFilename();
7471 #endif // TEST_MIME
7472
7473 #ifdef TEST_INFO_FUNCTIONS
7474 if ( TEST_ALL )
7475 {
7476 TestOsInfo();
7477 TestUserInfo();
7478
7479 if ( TEST_INTERACTIVE )
7480 TestDiskInfo();
7481 }
7482 #endif // TEST_INFO_FUNCTIONS
7483
7484 #ifdef TEST_PATHLIST
7485 TestPathList();
7486 #endif // TEST_PATHLIST
7487
7488 #ifdef TEST_ODBC
7489 TestDbOpen();
7490 #endif // TEST_ODBC
7491
7492 #ifdef TEST_PRINTF
7493 TestPrintf();
7494 #endif // TEST_PRINTF
7495
7496 #ifdef TEST_REGCONF
7497 //TestRegConfWrite();
7498 TestRegConfRead();
7499 #endif // TEST_REGCONF
7500
7501 #ifdef TEST_REGEX
7502 // TODO: write a real test using src/regex/tests file
7503 if ( TEST_ALL )
7504 {
7505 TestRegExCompile();
7506 TestRegExMatch();
7507 TestRegExSubmatch();
7508 TestRegExReplacement();
7509
7510 if ( TEST_INTERACTIVE )
7511 TestRegExInteractive();
7512 }
7513 #endif // TEST_REGEX
7514
7515 #ifdef TEST_REGISTRY
7516 TestRegistryRead();
7517 TestRegistryAssociation();
7518 #endif // TEST_REGISTRY
7519
7520 #ifdef TEST_SOCKETS
7521 TestSocketServer();
7522 TestSocketClient();
7523 #endif // TEST_SOCKETS
7524
7525 #ifdef TEST_STREAMS
7526 if ( TEST_ALL )
7527 {
7528 TestFileStream();
7529 }
7530 TestMemoryStream();
7531 #endif // TEST_STREAMS
7532
7533 #ifdef TEST_TEXTSTREAM
7534 TestTextInputStream();
7535 #endif // TEST_TEXTSTREAM
7536
7537 #ifdef TEST_THREADS
7538 int nCPUs = wxThread::GetCPUCount();
7539 wxPrintf(_T("This system has %d CPUs\n"), nCPUs);
7540 if ( nCPUs != -1 )
7541 wxThread::SetConcurrency(nCPUs);
7542
7543 TestJoinableThreads();
7544
7545 if ( TEST_ALL )
7546 {
7547 TestJoinableThreads();
7548 TestDetachedThreads();
7549 TestThreadSuspend();
7550 TestThreadDelete();
7551 TestThreadConditions();
7552 TestThreadExec();
7553 TestSemaphore();
7554 }
7555 #endif // TEST_THREADS
7556
7557 #ifdef TEST_TIMER
7558 TestStopWatch();
7559 #endif // TEST_TIMER
7560
7561 #ifdef TEST_DATETIME
7562 if ( TEST_ALL )
7563 {
7564 TestTimeSet();
7565 TestTimeStatic();
7566 TestTimeRange();
7567 TestTimeZones();
7568 TestTimeTicks();
7569 TestTimeJDN();
7570 TestTimeDST();
7571 TestTimeWDays();
7572 TestTimeWNumber();
7573 TestTimeParse();
7574 TestTimeArithmetics();
7575 TestTimeHolidays();
7576 TestTimeFormat();
7577 TestTimeSpanFormat();
7578 TestTimeMS();
7579
7580 TestTimeZoneBug();
7581 }
7582
7583 if ( TEST_INTERACTIVE )
7584 TestDateTimeInteractive();
7585 #endif // TEST_DATETIME
7586
7587 #ifdef TEST_SCOPEGUARD
7588 TestScopeGuard();
7589 #endif
7590
7591 #ifdef TEST_USLEEP
7592 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
7593 wxUsleep(3000);
7594 #endif // TEST_USLEEP
7595
7596 #ifdef TEST_VCARD
7597 TestVCardRead();
7598 TestVCardWrite();
7599 #endif // TEST_VCARD
7600
7601 #ifdef TEST_VOLUME
7602 TestFSVolume();
7603 #endif // TEST_VOLUME
7604
7605 #ifdef TEST_UNICODE
7606 TestUnicodeTextFileRead();
7607 if ( TEST_ALL )
7608 {
7609 TestUnicodeToFromAscii();
7610 }
7611 #endif // TEST_UNICODE
7612
7613 #ifdef TEST_WCHAR
7614 TestUtf8();
7615 TestEncodingConverter();
7616 #endif // TEST_WCHAR
7617
7618 #ifdef TEST_ZIP
7619 TestZipStreamRead();
7620 TestZipFileSystem();
7621 #endif // TEST_ZIP
7622
7623 #ifdef TEST_ZLIB
7624 TestZlibStreamWrite();
7625 TestZlibStreamRead();
7626 #endif // TEST_ZLIB
7627
7628 #ifdef TEST_GZIP
7629 TestGzip();
7630 #endif
7631
7632 return 0;
7633 }
7634