fix printf() argument type in GetOsInfo()
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
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 wxWidgets 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)? Define TEST_ALL to 0 to do a single
48 // test, define it to 1 to do all tests.
49 #define TEST_ALL 0
50
51
52 #if TEST_ALL
53 #define TEST_CMDLINE
54 #define TEST_DATETIME
55 #define TEST_DIR
56 #define TEST_DYNLIB
57 #define TEST_ENVIRON
58 #define TEST_EXECUTE
59 #define TEST_FILE
60 #define TEST_FILECONF
61 #define TEST_FILENAME
62 #define TEST_FILETIME
63 // #define TEST_FTP --FIXME! (RN)
64 #define TEST_INFO_FUNCTIONS
65 #define TEST_LOCALE
66 #define TEST_LOG
67 #define TEST_MIME
68 #define TEST_MODULE
69 #define TEST_PATHLIST
70 #define TEST_ODBC
71 #define TEST_PRINTF
72 #define TEST_REGCONF
73 #define TEST_REGEX
74 #define TEST_REGISTRY
75 #define TEST_SCOPEGUARD
76 #define TEST_SNGLINST
77 // #define TEST_SOCKETS --FIXME! (RN)
78 #define TEST_STACKWALKER
79 #define TEST_STDPATHS
80 #define TEST_STREAMS
81 #define TEST_TEXTSTREAM
82 #define TEST_THREADS
83 #define TEST_TIMER
84 // #define TEST_VCARD -- don't enable this (VZ)
85 // #define TEST_VOLUME --FIXME! (RN)
86 #define TEST_WCHAR
87 #define TEST_ZIP
88 #else // #if TEST_ALL
89 #define TEST_MODULE
90 #endif
91
92 // some tests are interactive, define this to run them
93 #ifdef TEST_INTERACTIVE
94 #undef TEST_INTERACTIVE
95
96 #define TEST_INTERACTIVE 1
97 #else
98 #define TEST_INTERACTIVE 0
99 #endif
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ----------------------------------------------------------------------------
106 // helper functions
107 // ----------------------------------------------------------------------------
108
109 #if defined(TEST_SOCKETS)
110
111 // replace TABs with \t and CRs with \n
112 static wxString MakePrintable(const wxChar *s)
113 {
114 wxString str(s);
115 (void)str.Replace(_T("\t"), _T("\\t"));
116 (void)str.Replace(_T("\n"), _T("\\n"));
117 (void)str.Replace(_T("\r"), _T("\\r"));
118
119 return str;
120 }
121
122 #endif // MakePrintable() is used
123
124 // ----------------------------------------------------------------------------
125 // wxCmdLineParser
126 // ----------------------------------------------------------------------------
127
128 #ifdef TEST_CMDLINE
129
130 #include "wx/cmdline.h"
131 #include "wx/datetime.h"
132
133 #if wxUSE_CMDLINE_PARSER
134
135 static void ShowCmdLine(const wxCmdLineParser& parser)
136 {
137 wxString s = _T("Input files: ");
138
139 size_t count = parser.GetParamCount();
140 for ( size_t param = 0; param < count; param++ )
141 {
142 s << parser.GetParam(param) << ' ';
143 }
144
145 s << '\n'
146 << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
147 << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
148
149 wxString strVal;
150 long lVal;
151 wxDateTime dt;
152 if ( parser.Found(_T("o"), &strVal) )
153 s << _T("Output file:\t") << strVal << '\n';
154 if ( parser.Found(_T("i"), &strVal) )
155 s << _T("Input dir:\t") << strVal << '\n';
156 if ( parser.Found(_T("s"), &lVal) )
157 s << _T("Size:\t") << lVal << '\n';
158 if ( parser.Found(_T("d"), &dt) )
159 s << _T("Date:\t") << dt.FormatISODate() << '\n';
160 if ( parser.Found(_T("project_name"), &strVal) )
161 s << _T("Project:\t") << strVal << '\n';
162
163 wxLogMessage(s);
164 }
165
166 #endif // wxUSE_CMDLINE_PARSER
167
168 static void TestCmdLineConvert()
169 {
170 static const wxChar *cmdlines[] =
171 {
172 _T("arg1 arg2"),
173 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
174 _T("literal \\\" and \"\""),
175 };
176
177 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
178 {
179 const wxChar *cmdline = cmdlines[n];
180 wxPrintf(_T("Parsing: %s\n"), cmdline);
181 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
182
183 size_t count = args.GetCount();
184 wxPrintf(_T("\targc = %u\n"), count);
185 for ( size_t arg = 0; arg < count; arg++ )
186 {
187 wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str());
188 }
189 }
190 }
191
192 #endif // TEST_CMDLINE
193
194 // ----------------------------------------------------------------------------
195 // wxDir
196 // ----------------------------------------------------------------------------
197
198 #ifdef TEST_DIR
199
200 #include "wx/dir.h"
201
202 #ifdef __UNIX__
203 static const wxChar *ROOTDIR = _T("/");
204 static const wxChar *TESTDIR = _T("/usr/local/share");
205 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
206 static const wxChar *ROOTDIR = _T("c:\\");
207 static const wxChar *TESTDIR = _T("d:\\");
208 #else
209 #error "don't know where the root directory is"
210 #endif
211
212 static void TestDirEnumHelper(wxDir& dir,
213 int flags = wxDIR_DEFAULT,
214 const wxString& filespec = wxEmptyString)
215 {
216 wxString filename;
217
218 if ( !dir.IsOpened() )
219 return;
220
221 bool cont = dir.GetFirst(&filename, filespec, flags);
222 while ( cont )
223 {
224 wxPrintf(_T("\t%s\n"), filename.c_str());
225
226 cont = dir.GetNext(&filename);
227 }
228
229 wxPuts(wxEmptyString);
230 }
231
232 #if TEST_ALL
233
234 static void TestDirEnum()
235 {
236 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
237
238 wxString cwd = wxGetCwd();
239 if ( !wxDir::Exists(cwd) )
240 {
241 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
242 return;
243 }
244
245 wxDir dir(cwd);
246 if ( !dir.IsOpened() )
247 {
248 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
249 return;
250 }
251
252 wxPuts(_T("Enumerating everything in current directory:"));
253 TestDirEnumHelper(dir);
254
255 wxPuts(_T("Enumerating really everything in current directory:"));
256 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
257
258 wxPuts(_T("Enumerating object files in current directory:"));
259 TestDirEnumHelper(dir, wxDIR_DEFAULT, _T("*.o*"));
260
261 wxPuts(_T("Enumerating directories in current directory:"));
262 TestDirEnumHelper(dir, wxDIR_DIRS);
263
264 wxPuts(_T("Enumerating files in current directory:"));
265 TestDirEnumHelper(dir, wxDIR_FILES);
266
267 wxPuts(_T("Enumerating files including hidden in current directory:"));
268 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
269
270 dir.Open(ROOTDIR);
271
272 wxPuts(_T("Enumerating everything in root directory:"));
273 TestDirEnumHelper(dir, wxDIR_DEFAULT);
274
275 wxPuts(_T("Enumerating directories in root directory:"));
276 TestDirEnumHelper(dir, wxDIR_DIRS);
277
278 wxPuts(_T("Enumerating files in root directory:"));
279 TestDirEnumHelper(dir, wxDIR_FILES);
280
281 wxPuts(_T("Enumerating files including hidden in root directory:"));
282 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
283
284 wxPuts(_T("Enumerating files in non existing directory:"));
285 wxDir dirNo(_T("nosuchdir"));
286 TestDirEnumHelper(dirNo);
287 }
288
289 #endif // TEST_ALL
290
291 class DirPrintTraverser : public wxDirTraverser
292 {
293 public:
294 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
295 {
296 return wxDIR_CONTINUE;
297 }
298
299 virtual wxDirTraverseResult OnDir(const wxString& dirname)
300 {
301 wxString path, name, ext;
302 wxSplitPath(dirname, &path, &name, &ext);
303
304 if ( !ext.empty() )
305 name << _T('.') << ext;
306
307 wxString indent;
308 for ( const wxChar *p = path.c_str(); *p; p++ )
309 {
310 if ( wxIsPathSeparator(*p) )
311 indent += _T(" ");
312 }
313
314 wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str());
315
316 return wxDIR_CONTINUE;
317 }
318 };
319
320 static void TestDirTraverse()
321 {
322 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
323
324 // enum all files
325 wxArrayString files;
326 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
327 wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR);
328 if ( n > 1 )
329 {
330 wxPrintf(_T("First one is '%s'\n"), files[0u].c_str());
331 wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str());
332 }
333
334 // enum again with custom traverser
335 wxPuts(_T("Now enumerating directories:"));
336 wxDir dir(TESTDIR);
337 DirPrintTraverser traverser;
338 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
339 }
340
341 #if TEST_ALL
342
343 static void TestDirExists()
344 {
345 wxPuts(_T("*** Testing wxDir::Exists() ***"));
346
347 static const wxChar *dirnames[] =
348 {
349 _T("."),
350 #if defined(__WXMSW__)
351 _T("c:"),
352 _T("c:\\"),
353 _T("\\\\share\\file"),
354 _T("c:\\dos"),
355 _T("c:\\dos\\"),
356 _T("c:\\dos\\\\"),
357 _T("c:\\autoexec.bat"),
358 #elif defined(__UNIX__)
359 _T("/"),
360 _T("//"),
361 _T("/usr/bin"),
362 _T("/usr//bin"),
363 _T("/usr///bin"),
364 #endif
365 };
366
367 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
368 {
369 wxPrintf(_T("%-40s: %s\n"),
370 dirnames[n],
371 wxDir::Exists(dirnames[n]) ? _T("exists")
372 : _T("doesn't exist"));
373 }
374 }
375
376 #endif // TEST_ALL
377
378 #endif // TEST_DIR
379
380 // ----------------------------------------------------------------------------
381 // wxDllLoader
382 // ----------------------------------------------------------------------------
383
384 #ifdef TEST_DYNLIB
385
386 #include "wx/dynlib.h"
387
388 static void TestDllLoad()
389 {
390 #if defined(__WXMSW__)
391 static const wxChar *LIB_NAME = _T("kernel32.dll");
392 static const wxChar *FUNC_NAME = _T("lstrlenA");
393 #elif defined(__UNIX__)
394 // weird: using just libc.so does *not* work!
395 static const wxChar *LIB_NAME = _T("/lib/libc.so.6");
396 static const wxChar *FUNC_NAME = _T("strlen");
397 #else
398 #error "don't know how to test wxDllLoader on this platform"
399 #endif
400
401 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
402
403 wxDynamicLibrary lib(LIB_NAME);
404 if ( !lib.IsLoaded() )
405 {
406 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
407 }
408 else
409 {
410 typedef int (wxSTDCALL *wxStrlenType)(const char *);
411 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
412 if ( !pfnStrlen )
413 {
414 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
415 FUNC_NAME, LIB_NAME);
416 }
417 else
418 {
419 wxPrintf(_T("Calling %s dynamically loaded from %s "),
420 FUNC_NAME, LIB_NAME);
421
422 if ( pfnStrlen("foo") != 3 )
423 {
424 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
425 }
426 else
427 {
428 wxPuts(_T("... ok"));
429 }
430 }
431
432 #ifdef __WXMSW__
433 static const wxChar *FUNC_NAME_AW = _T("lstrlen");
434
435 typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
436 wxStrlenTypeAorW
437 pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
438 if ( !pfnStrlenAorW )
439 {
440 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
441 FUNC_NAME_AW, LIB_NAME);
442 }
443 else
444 {
445 if ( pfnStrlenAorW(_T("foobar")) != 6 )
446 {
447 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
448 }
449 }
450 #endif // __WXMSW__
451 }
452 }
453
454 #if defined(__WXMSW__) || defined(__UNIX__)
455
456 static void TestDllListLoaded()
457 {
458 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
459
460 puts("\nLoaded modules:");
461 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
462 const size_t count = dlls.GetCount();
463 for ( size_t n = 0; n < count; ++n )
464 {
465 const wxDynamicLibraryDetails& details = dlls[n];
466 printf("%-45s", details.GetPath().mb_str());
467
468 void *addr;
469 size_t len;
470 if ( details.GetAddress(&addr, &len) )
471 {
472 printf(" %08lx:%08lx",
473 (unsigned long)addr, (unsigned long)((char *)addr + len));
474 }
475
476 printf(" %s\n", details.GetVersion().mb_str());
477 }
478 }
479
480 #endif
481
482 #endif // TEST_DYNLIB
483
484 // ----------------------------------------------------------------------------
485 // wxGet/SetEnv
486 // ----------------------------------------------------------------------------
487
488 #ifdef TEST_ENVIRON
489
490 #include "wx/utils.h"
491
492 static wxString MyGetEnv(const wxString& var)
493 {
494 wxString val;
495 if ( !wxGetEnv(var, &val) )
496 val = _T("<empty>");
497 else
498 val = wxString(_T('\'')) + val + _T('\'');
499
500 return val;
501 }
502
503 static void TestEnvironment()
504 {
505 const wxChar *var = _T("wxTestVar");
506
507 wxPuts(_T("*** testing environment access functions ***"));
508
509 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
510 wxSetEnv(var, _T("value for wxTestVar"));
511 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
512 wxSetEnv(var, _T("another value"));
513 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
514 wxUnsetEnv(var);
515 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
516 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
517 }
518
519 #endif // TEST_ENVIRON
520
521 // ----------------------------------------------------------------------------
522 // wxExecute
523 // ----------------------------------------------------------------------------
524
525 #ifdef TEST_EXECUTE
526
527 #include "wx/utils.h"
528
529 static void TestExecute()
530 {
531 wxPuts(_T("*** testing wxExecute ***"));
532
533 #ifdef __UNIX__
534 #define COMMAND "cat -n ../../Makefile" // "echo hi"
535 #define SHELL_COMMAND "echo hi from shell"
536 #define REDIRECT_COMMAND COMMAND // "date"
537 #elif defined(__WXMSW__)
538 #define COMMAND "command.com /c echo hi"
539 #define SHELL_COMMAND "echo hi"
540 #define REDIRECT_COMMAND COMMAND
541 #else
542 #error "no command to exec"
543 #endif // OS
544
545 wxPrintf(_T("Testing wxShell: "));
546 fflush(stdout);
547 if ( wxShell(_T(SHELL_COMMAND)) )
548 wxPuts(_T("Ok."));
549 else
550 wxPuts(_T("ERROR."));
551
552 wxPrintf(_T("Testing wxExecute: "));
553 fflush(stdout);
554 if ( wxExecute(_T(COMMAND), true /* sync */) == 0 )
555 wxPuts(_T("Ok."));
556 else
557 wxPuts(_T("ERROR."));
558
559 #if 0 // no, it doesn't work (yet?)
560 wxPrintf(_T("Testing async wxExecute: "));
561 fflush(stdout);
562 if ( wxExecute(COMMAND) != 0 )
563 wxPuts(_T("Ok (command launched)."));
564 else
565 wxPuts(_T("ERROR."));
566 #endif // 0
567
568 wxPrintf(_T("Testing wxExecute with redirection:\n"));
569 wxArrayString output;
570 if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 )
571 {
572 wxPuts(_T("ERROR."));
573 }
574 else
575 {
576 size_t count = output.GetCount();
577 for ( size_t n = 0; n < count; n++ )
578 {
579 wxPrintf(_T("\t%s\n"), output[n].c_str());
580 }
581
582 wxPuts(_T("Ok."));
583 }
584 }
585
586 #endif // TEST_EXECUTE
587
588 // ----------------------------------------------------------------------------
589 // file
590 // ----------------------------------------------------------------------------
591
592 #ifdef TEST_FILE
593
594 #include "wx/file.h"
595 #include "wx/ffile.h"
596 #include "wx/textfile.h"
597
598 static void TestFileRead()
599 {
600 wxPuts(_T("*** wxFile read test ***"));
601
602 wxFile file(_T("testdata.fc"));
603 if ( file.IsOpened() )
604 {
605 wxPrintf(_T("File length: %lu\n"), file.Length());
606
607 wxPuts(_T("File dump:\n----------"));
608
609 static const size_t len = 1024;
610 wxChar buf[len];
611 for ( ;; )
612 {
613 size_t nRead = file.Read(buf, len);
614 if ( nRead == (size_t)wxInvalidOffset )
615 {
616 wxPrintf(_T("Failed to read the file."));
617 break;
618 }
619
620 fwrite(buf, nRead, 1, stdout);
621
622 if ( nRead < len )
623 break;
624 }
625
626 wxPuts(_T("----------"));
627 }
628 else
629 {
630 wxPrintf(_T("ERROR: can't open test file.\n"));
631 }
632
633 wxPuts(wxEmptyString);
634 }
635
636 static void TestTextFileRead()
637 {
638 wxPuts(_T("*** wxTextFile read test ***"));
639
640 wxTextFile file(_T("testdata.fc"));
641 if ( file.Open() )
642 {
643 wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount());
644 wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str());
645
646 wxString s;
647
648 wxPuts(_T("\nDumping the entire file:"));
649 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
650 {
651 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
652 }
653 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
654
655 wxPuts(_T("\nAnd now backwards:"));
656 for ( s = file.GetLastLine();
657 file.GetCurrentLine() != 0;
658 s = file.GetPrevLine() )
659 {
660 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
661 }
662 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
663 }
664 else
665 {
666 wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName());
667 }
668
669 wxPuts(wxEmptyString);
670 }
671
672 static void TestFileCopy()
673 {
674 wxPuts(_T("*** Testing wxCopyFile ***"));
675
676 static const wxChar *filename1 = _T("testdata.fc");
677 static const wxChar *filename2 = _T("test2");
678 if ( !wxCopyFile(filename1, filename2) )
679 {
680 wxPuts(_T("ERROR: failed to copy file"));
681 }
682 else
683 {
684 wxFFile f1(filename1, _T("rb")),
685 f2(filename2, _T("rb"));
686
687 if ( !f1.IsOpened() || !f2.IsOpened() )
688 {
689 wxPuts(_T("ERROR: failed to open file(s)"));
690 }
691 else
692 {
693 wxString s1, s2;
694 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
695 {
696 wxPuts(_T("ERROR: failed to read file(s)"));
697 }
698 else
699 {
700 if ( (s1.length() != s2.length()) ||
701 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
702 {
703 wxPuts(_T("ERROR: copy error!"));
704 }
705 else
706 {
707 wxPuts(_T("File was copied ok."));
708 }
709 }
710 }
711 }
712
713 if ( !wxRemoveFile(filename2) )
714 {
715 wxPuts(_T("ERROR: failed to remove the file"));
716 }
717
718 wxPuts(wxEmptyString);
719 }
720
721 #endif // TEST_FILE
722
723 // ----------------------------------------------------------------------------
724 // wxFileConfig
725 // ----------------------------------------------------------------------------
726
727 #ifdef TEST_FILECONF
728
729 #include "wx/confbase.h"
730 #include "wx/fileconf.h"
731
732 static const struct FileConfTestData
733 {
734 const wxChar *name; // value name
735 const wxChar *value; // the value from the file
736 } fcTestData[] =
737 {
738 { _T("value1"), _T("one") },
739 { _T("value2"), _T("two") },
740 { _T("novalue"), _T("default") },
741 };
742
743 static void TestFileConfRead()
744 {
745 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
746
747 wxFileConfig fileconf(_T("test"), wxEmptyString,
748 _T("testdata.fc"), wxEmptyString,
749 wxCONFIG_USE_RELATIVE_PATH);
750
751 // test simple reading
752 wxPuts(_T("\nReading config file:"));
753 wxString defValue(_T("default")), value;
754 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
755 {
756 const FileConfTestData& data = fcTestData[n];
757 value = fileconf.Read(data.name, defValue);
758 wxPrintf(_T("\t%s = %s "), data.name, value.c_str());
759 if ( value == data.value )
760 {
761 wxPuts(_T("(ok)"));
762 }
763 else
764 {
765 wxPrintf(_T("(ERROR: should be %s)\n"), data.value);
766 }
767 }
768
769 // test enumerating the entries
770 wxPuts(_T("\nEnumerating all root entries:"));
771 long dummy;
772 wxString name;
773 bool cont = fileconf.GetFirstEntry(name, dummy);
774 while ( cont )
775 {
776 wxPrintf(_T("\t%s = %s\n"),
777 name.c_str(),
778 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
779
780 cont = fileconf.GetNextEntry(name, dummy);
781 }
782
783 static const wxChar *testEntry = _T("TestEntry");
784 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
785 fileconf.Write(testEntry, _T("A value"));
786 fileconf.DeleteEntry(testEntry);
787 wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n"));
788 }
789
790 #endif // TEST_FILECONF
791
792 // ----------------------------------------------------------------------------
793 // wxFileName
794 // ----------------------------------------------------------------------------
795
796 #ifdef TEST_FILENAME
797
798 #include "wx/filename.h"
799
800 #if 0
801 static void DumpFileName(const wxChar *desc, const wxFileName& fn)
802 {
803 wxPuts(desc);
804
805 wxString full = fn.GetFullPath();
806
807 wxString vol, path, name, ext;
808 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
809
810 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
811 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
812
813 wxFileName::SplitPath(full, &path, &name, &ext);
814 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
815 path.c_str(), name.c_str(), ext.c_str());
816
817 wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str());
818 wxPrintf(_T("with volume: \t'%s'\n"),
819 fn.GetPath(wxPATH_GET_VOLUME).c_str());
820 wxPrintf(_T("with separator:\t'%s'\n"),
821 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
822 wxPrintf(_T("with both: \t'%s'\n"),
823 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
824
825 wxPuts(_T("The directories in the path are:"));
826 wxArrayString dirs = fn.GetDirs();
827 size_t count = dirs.GetCount();
828 for ( size_t n = 0; n < count; n++ )
829 {
830 wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str());
831 }
832 }
833 #endif
834
835 static void TestFileNameTemp()
836 {
837 wxPuts(_T("*** testing wxFileName temp file creation ***"));
838
839 static const wxChar *tmpprefixes[] =
840 {
841 _T(""),
842 _T("foo"),
843 _T(".."),
844 _T("../bar"),
845 #ifdef __UNIX__
846 _T("/tmp/foo"),
847 _T("/tmp/foo/bar"), // this one must be an error
848 #endif // __UNIX__
849 };
850
851 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
852 {
853 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
854 if ( path.empty() )
855 {
856 // "error" is not in upper case because it may be ok
857 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
858 }
859 else
860 {
861 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
862 tmpprefixes[n], path.c_str());
863
864 if ( !wxRemoveFile(path) )
865 {
866 wxLogWarning(_T("Failed to remove temp file '%s'"),
867 path.c_str());
868 }
869 }
870 }
871 }
872
873 static void TestFileNameDirManip()
874 {
875 // TODO: test AppendDir(), RemoveDir(), ...
876 }
877
878 static void TestFileNameComparison()
879 {
880 // TODO!
881 }
882
883 static void TestFileNameOperations()
884 {
885 // TODO!
886 }
887
888 static void TestFileNameCwd()
889 {
890 // TODO!
891 }
892
893 #endif // TEST_FILENAME
894
895 // ----------------------------------------------------------------------------
896 // wxFileName time functions
897 // ----------------------------------------------------------------------------
898
899 #ifdef TEST_FILETIME
900
901 #include "wx/filename.h"
902 #include "wx/datetime.h"
903
904 static void TestFileGetTimes()
905 {
906 wxFileName fn(_T("testdata.fc"));
907
908 wxDateTime dtAccess, dtMod, dtCreate;
909 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
910 {
911 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
912 }
913 else
914 {
915 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
916
917 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
918 wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
919 wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
920 wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
921 }
922 }
923
924 #if 0
925 static void TestFileSetTimes()
926 {
927 wxFileName fn(_T("testdata.fc"));
928
929 if ( !fn.Touch() )
930 {
931 wxPrintf(_T("ERROR: Touch() failed.\n"));
932 }
933 }
934 #endif
935
936 #endif // TEST_FILETIME
937
938 // ----------------------------------------------------------------------------
939 // wxLocale
940 // ----------------------------------------------------------------------------
941
942 #ifdef TEST_LOCALE
943
944 #include "wx/intl.h"
945 #include "wx/utils.h" // for wxSetEnv
946
947 static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH);
948
949 // find the name of the language from its value
950 static const wxChar *GetLangName(int lang)
951 {
952 static const wxChar *languageNames[] =
953 {
954 _T("DEFAULT"),
955 _T("UNKNOWN"),
956 _T("ABKHAZIAN"),
957 _T("AFAR"),
958 _T("AFRIKAANS"),
959 _T("ALBANIAN"),
960 _T("AMHARIC"),
961 _T("ARABIC"),
962 _T("ARABIC_ALGERIA"),
963 _T("ARABIC_BAHRAIN"),
964 _T("ARABIC_EGYPT"),
965 _T("ARABIC_IRAQ"),
966 _T("ARABIC_JORDAN"),
967 _T("ARABIC_KUWAIT"),
968 _T("ARABIC_LEBANON"),
969 _T("ARABIC_LIBYA"),
970 _T("ARABIC_MOROCCO"),
971 _T("ARABIC_OMAN"),
972 _T("ARABIC_QATAR"),
973 _T("ARABIC_SAUDI_ARABIA"),
974 _T("ARABIC_SUDAN"),
975 _T("ARABIC_SYRIA"),
976 _T("ARABIC_TUNISIA"),
977 _T("ARABIC_UAE"),
978 _T("ARABIC_YEMEN"),
979 _T("ARMENIAN"),
980 _T("ASSAMESE"),
981 _T("AYMARA"),
982 _T("AZERI"),
983 _T("AZERI_CYRILLIC"),
984 _T("AZERI_LATIN"),
985 _T("BASHKIR"),
986 _T("BASQUE"),
987 _T("BELARUSIAN"),
988 _T("BENGALI"),
989 _T("BHUTANI"),
990 _T("BIHARI"),
991 _T("BISLAMA"),
992 _T("BRETON"),
993 _T("BULGARIAN"),
994 _T("BURMESE"),
995 _T("CAMBODIAN"),
996 _T("CATALAN"),
997 _T("CHINESE"),
998 _T("CHINESE_SIMPLIFIED"),
999 _T("CHINESE_TRADITIONAL"),
1000 _T("CHINESE_HONGKONG"),
1001 _T("CHINESE_MACAU"),
1002 _T("CHINESE_SINGAPORE"),
1003 _T("CHINESE_TAIWAN"),
1004 _T("CORSICAN"),
1005 _T("CROATIAN"),
1006 _T("CZECH"),
1007 _T("DANISH"),
1008 _T("DUTCH"),
1009 _T("DUTCH_BELGIAN"),
1010 _T("ENGLISH"),
1011 _T("ENGLISH_UK"),
1012 _T("ENGLISH_US"),
1013 _T("ENGLISH_AUSTRALIA"),
1014 _T("ENGLISH_BELIZE"),
1015 _T("ENGLISH_BOTSWANA"),
1016 _T("ENGLISH_CANADA"),
1017 _T("ENGLISH_CARIBBEAN"),
1018 _T("ENGLISH_DENMARK"),
1019 _T("ENGLISH_EIRE"),
1020 _T("ENGLISH_JAMAICA"),
1021 _T("ENGLISH_NEW_ZEALAND"),
1022 _T("ENGLISH_PHILIPPINES"),
1023 _T("ENGLISH_SOUTH_AFRICA"),
1024 _T("ENGLISH_TRINIDAD"),
1025 _T("ENGLISH_ZIMBABWE"),
1026 _T("ESPERANTO"),
1027 _T("ESTONIAN"),
1028 _T("FAEROESE"),
1029 _T("FARSI"),
1030 _T("FIJI"),
1031 _T("FINNISH"),
1032 _T("FRENCH"),
1033 _T("FRENCH_BELGIAN"),
1034 _T("FRENCH_CANADIAN"),
1035 _T("FRENCH_LUXEMBOURG"),
1036 _T("FRENCH_MONACO"),
1037 _T("FRENCH_SWISS"),
1038 _T("FRISIAN"),
1039 _T("GALICIAN"),
1040 _T("GEORGIAN"),
1041 _T("GERMAN"),
1042 _T("GERMAN_AUSTRIAN"),
1043 _T("GERMAN_BELGIUM"),
1044 _T("GERMAN_LIECHTENSTEIN"),
1045 _T("GERMAN_LUXEMBOURG"),
1046 _T("GERMAN_SWISS"),
1047 _T("GREEK"),
1048 _T("GREENLANDIC"),
1049 _T("GUARANI"),
1050 _T("GUJARATI"),
1051 _T("HAUSA"),
1052 _T("HEBREW"),
1053 _T("HINDI"),
1054 _T("HUNGARIAN"),
1055 _T("ICELANDIC"),
1056 _T("INDONESIAN"),
1057 _T("INTERLINGUA"),
1058 _T("INTERLINGUE"),
1059 _T("INUKTITUT"),
1060 _T("INUPIAK"),
1061 _T("IRISH"),
1062 _T("ITALIAN"),
1063 _T("ITALIAN_SWISS"),
1064 _T("JAPANESE"),
1065 _T("JAVANESE"),
1066 _T("KANNADA"),
1067 _T("KASHMIRI"),
1068 _T("KASHMIRI_INDIA"),
1069 _T("KAZAKH"),
1070 _T("KERNEWEK"),
1071 _T("KINYARWANDA"),
1072 _T("KIRGHIZ"),
1073 _T("KIRUNDI"),
1074 _T("KONKANI"),
1075 _T("KOREAN"),
1076 _T("KURDISH"),
1077 _T("LAOTHIAN"),
1078 _T("LATIN"),
1079 _T("LATVIAN"),
1080 _T("LINGALA"),
1081 _T("LITHUANIAN"),
1082 _T("MACEDONIAN"),
1083 _T("MALAGASY"),
1084 _T("MALAY"),
1085 _T("MALAYALAM"),
1086 _T("MALAY_BRUNEI_DARUSSALAM"),
1087 _T("MALAY_MALAYSIA"),
1088 _T("MALTESE"),
1089 _T("MANIPURI"),
1090 _T("MAORI"),
1091 _T("MARATHI"),
1092 _T("MOLDAVIAN"),
1093 _T("MONGOLIAN"),
1094 _T("NAURU"),
1095 _T("NEPALI"),
1096 _T("NEPALI_INDIA"),
1097 _T("NORWEGIAN_BOKMAL"),
1098 _T("NORWEGIAN_NYNORSK"),
1099 _T("OCCITAN"),
1100 _T("ORIYA"),
1101 _T("OROMO"),
1102 _T("PASHTO"),
1103 _T("POLISH"),
1104 _T("PORTUGUESE"),
1105 _T("PORTUGUESE_BRAZILIAN"),
1106 _T("PUNJABI"),
1107 _T("QUECHUA"),
1108 _T("RHAETO_ROMANCE"),
1109 _T("ROMANIAN"),
1110 _T("RUSSIAN"),
1111 _T("RUSSIAN_UKRAINE"),
1112 _T("SAMOAN"),
1113 _T("SANGHO"),
1114 _T("SANSKRIT"),
1115 _T("SCOTS_GAELIC"),
1116 _T("SERBIAN"),
1117 _T("SERBIAN_CYRILLIC"),
1118 _T("SERBIAN_LATIN"),
1119 _T("SERBO_CROATIAN"),
1120 _T("SESOTHO"),
1121 _T("SETSWANA"),
1122 _T("SHONA"),
1123 _T("SINDHI"),
1124 _T("SINHALESE"),
1125 _T("SISWATI"),
1126 _T("SLOVAK"),
1127 _T("SLOVENIAN"),
1128 _T("SOMALI"),
1129 _T("SPANISH"),
1130 _T("SPANISH_ARGENTINA"),
1131 _T("SPANISH_BOLIVIA"),
1132 _T("SPANISH_CHILE"),
1133 _T("SPANISH_COLOMBIA"),
1134 _T("SPANISH_COSTA_RICA"),
1135 _T("SPANISH_DOMINICAN_REPUBLIC"),
1136 _T("SPANISH_ECUADOR"),
1137 _T("SPANISH_EL_SALVADOR"),
1138 _T("SPANISH_GUATEMALA"),
1139 _T("SPANISH_HONDURAS"),
1140 _T("SPANISH_MEXICAN"),
1141 _T("SPANISH_MODERN"),
1142 _T("SPANISH_NICARAGUA"),
1143 _T("SPANISH_PANAMA"),
1144 _T("SPANISH_PARAGUAY"),
1145 _T("SPANISH_PERU"),
1146 _T("SPANISH_PUERTO_RICO"),
1147 _T("SPANISH_URUGUAY"),
1148 _T("SPANISH_US"),
1149 _T("SPANISH_VENEZUELA"),
1150 _T("SUNDANESE"),
1151 _T("SWAHILI"),
1152 _T("SWEDISH"),
1153 _T("SWEDISH_FINLAND"),
1154 _T("TAGALOG"),
1155 _T("TAJIK"),
1156 _T("TAMIL"),
1157 _T("TATAR"),
1158 _T("TELUGU"),
1159 _T("THAI"),
1160 _T("TIBETAN"),
1161 _T("TIGRINYA"),
1162 _T("TONGA"),
1163 _T("TSONGA"),
1164 _T("TURKISH"),
1165 _T("TURKMEN"),
1166 _T("TWI"),
1167 _T("UIGHUR"),
1168 _T("UKRAINIAN"),
1169 _T("URDU"),
1170 _T("URDU_INDIA"),
1171 _T("URDU_PAKISTAN"),
1172 _T("UZBEK"),
1173 _T("UZBEK_CYRILLIC"),
1174 _T("UZBEK_LATIN"),
1175 _T("VIETNAMESE"),
1176 _T("VOLAPUK"),
1177 _T("WELSH"),
1178 _T("WOLOF"),
1179 _T("XHOSA"),
1180 _T("YIDDISH"),
1181 _T("YORUBA"),
1182 _T("ZHUANG"),
1183 _T("ZULU"),
1184 };
1185
1186 if ( (size_t)lang < WXSIZEOF(languageNames) )
1187 return languageNames[lang];
1188 else
1189 return _T("INVALID");
1190 }
1191
1192 static void TestDefaultLang()
1193 {
1194 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1195
1196 static const wxChar *langStrings[] =
1197 {
1198 NULL, // system default
1199 _T("C"),
1200 _T("fr"),
1201 _T("fr_FR"),
1202 _T("en"),
1203 _T("en_GB"),
1204 _T("en_US"),
1205 _T("de_DE.iso88591"),
1206 _T("german"),
1207 _T("?"), // invalid lang spec
1208 _T("klingonese"), // I bet on some systems it does exist...
1209 };
1210
1211 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1212 wxLocale::GetSystemEncodingName().c_str(),
1213 wxLocale::GetSystemEncoding());
1214
1215 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1216 {
1217 const wxChar *langStr = langStrings[n];
1218 if ( langStr )
1219 {
1220 // FIXME: this doesn't do anything at all under Windows, we need
1221 // to create a new wxLocale!
1222 wxSetEnv(_T("LC_ALL"), langStr);
1223 }
1224
1225 int lang = gs_localeDefault.GetSystemLanguage();
1226 wxPrintf(_T("Locale for '%s' is %s.\n"),
1227 langStr ? langStr : _T("system default"), GetLangName(lang));
1228 }
1229 }
1230
1231 #endif // TEST_LOCALE
1232
1233 // ----------------------------------------------------------------------------
1234 // MIME types
1235 // ----------------------------------------------------------------------------
1236
1237 #ifdef TEST_MIME
1238
1239 #include "wx/mimetype.h"
1240
1241 static void TestMimeEnum()
1242 {
1243 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1244
1245 wxArrayString mimetypes;
1246
1247 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
1248
1249 wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
1250
1251 wxArrayString exts;
1252 wxString desc;
1253
1254 for ( size_t n = 0; n < count; n++ )
1255 {
1256 wxFileType *filetype =
1257 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
1258 if ( !filetype )
1259 {
1260 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1261 mimetypes[n].c_str());
1262 continue;
1263 }
1264
1265 filetype->GetDescription(&desc);
1266 filetype->GetExtensions(exts);
1267
1268 filetype->GetIcon(NULL);
1269
1270 wxString extsAll;
1271 for ( size_t e = 0; e < exts.GetCount(); e++ )
1272 {
1273 if ( e > 0 )
1274 extsAll << _T(", ");
1275 extsAll += exts[e];
1276 }
1277
1278 wxPrintf(_T("\t%s: %s (%s)\n"),
1279 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
1280 }
1281
1282 wxPuts(wxEmptyString);
1283 }
1284
1285 static void TestMimeOverride()
1286 {
1287 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1288
1289 static const wxChar *mailcap = _T("/tmp/mailcap");
1290 static const wxChar *mimetypes = _T("/tmp/mime.types");
1291
1292 if ( wxFile::Exists(mailcap) )
1293 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1294 mailcap,
1295 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
1296 else
1297 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1298 mailcap);
1299
1300 if ( wxFile::Exists(mimetypes) )
1301 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1302 mimetypes,
1303 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
1304 else
1305 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1306 mimetypes);
1307
1308 wxPuts(wxEmptyString);
1309 }
1310
1311 static void TestMimeFilename()
1312 {
1313 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1314
1315 static const wxChar *filenames[] =
1316 {
1317 _T("readme.txt"),
1318 _T("document.pdf"),
1319 _T("image.gif"),
1320 _T("picture.jpeg"),
1321 };
1322
1323 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1324 {
1325 const wxString fname = filenames[n];
1326 wxString ext = fname.AfterLast(_T('.'));
1327 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
1328 if ( !ft )
1329 {
1330 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1331 }
1332 else
1333 {
1334 wxString desc;
1335 if ( !ft->GetDescription(&desc) )
1336 desc = _T("<no description>");
1337
1338 wxString cmd;
1339 if ( !ft->GetOpenCommand(&cmd,
1340 wxFileType::MessageParameters(fname, wxEmptyString)) )
1341 cmd = _T("<no command available>");
1342 else
1343 cmd = wxString(_T('"')) + cmd + _T('"');
1344
1345 wxPrintf(_T("To open %s (%s) do %s.\n"),
1346 fname.c_str(), desc.c_str(), cmd.c_str());
1347
1348 delete ft;
1349 }
1350 }
1351
1352 wxPuts(wxEmptyString);
1353 }
1354
1355 static void TestMimeAssociate()
1356 {
1357 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1358
1359 wxFileTypeInfo ftInfo(
1360 _T("application/x-xyz"),
1361 _T("xyzview '%s'"), // open cmd
1362 _T(""), // print cmd
1363 _T("XYZ File"), // description
1364 _T(".xyz"), // extensions
1365 NULL // end of extensions
1366 );
1367 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
1368
1369 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
1370 if ( !ft )
1371 {
1372 wxPuts(_T("ERROR: failed to create association!"));
1373 }
1374 else
1375 {
1376 // TODO: read it back
1377 delete ft;
1378 }
1379
1380 wxPuts(wxEmptyString);
1381 }
1382
1383 #endif // TEST_MIME
1384
1385 // ----------------------------------------------------------------------------
1386 // module dependencies feature
1387 // ----------------------------------------------------------------------------
1388
1389 #ifdef TEST_MODULE
1390
1391 #include "wx/module.h"
1392
1393 class wxTestModule : public wxModule
1394 {
1395 protected:
1396 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1397 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1398 };
1399
1400 class wxTestModuleA : public wxTestModule
1401 {
1402 public:
1403 wxTestModuleA();
1404 private:
1405 DECLARE_DYNAMIC_CLASS(wxTestModuleA)
1406 };
1407
1408 class wxTestModuleB : public wxTestModule
1409 {
1410 public:
1411 wxTestModuleB();
1412 private:
1413 DECLARE_DYNAMIC_CLASS(wxTestModuleB)
1414 };
1415
1416 class wxTestModuleC : public wxTestModule
1417 {
1418 public:
1419 wxTestModuleC();
1420 private:
1421 DECLARE_DYNAMIC_CLASS(wxTestModuleC)
1422 };
1423
1424 class wxTestModuleD : public wxTestModule
1425 {
1426 public:
1427 wxTestModuleD();
1428 private:
1429 DECLARE_DYNAMIC_CLASS(wxTestModuleD)
1430 };
1431
1432 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC, wxModule)
1433 wxTestModuleC::wxTestModuleC()
1434 {
1435 AddDependency(CLASSINFO(wxTestModuleD));
1436 }
1437
1438 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA, wxModule)
1439 wxTestModuleA::wxTestModuleA()
1440 {
1441 AddDependency(CLASSINFO(wxTestModuleB));
1442 AddDependency(CLASSINFO(wxTestModuleD));
1443 }
1444
1445 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD, wxModule)
1446 wxTestModuleD::wxTestModuleD()
1447 {
1448 }
1449
1450 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB, wxModule)
1451 wxTestModuleB::wxTestModuleB()
1452 {
1453 AddDependency(CLASSINFO(wxTestModuleD));
1454 AddDependency(CLASSINFO(wxTestModuleC));
1455 }
1456
1457 #endif // TEST_MODULE
1458
1459 // ----------------------------------------------------------------------------
1460 // misc information functions
1461 // ----------------------------------------------------------------------------
1462
1463 #ifdef TEST_INFO_FUNCTIONS
1464
1465 #include "wx/utils.h"
1466
1467 static void TestDiskInfo()
1468 {
1469 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1470
1471 for ( ;; )
1472 {
1473 wxChar pathname[128];
1474 wxPrintf(_T("\nEnter a directory name: "));
1475 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
1476 break;
1477
1478 // kill the last '\n'
1479 pathname[wxStrlen(pathname) - 1] = 0;
1480
1481 wxLongLong total, free;
1482 if ( !wxGetDiskSpace(pathname, &total, &free) )
1483 {
1484 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1485 }
1486 else
1487 {
1488 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1489 (total / 1024).ToString().c_str(),
1490 (free / 1024).ToString().c_str(),
1491 pathname);
1492 }
1493 }
1494 }
1495
1496 static void TestOsInfo()
1497 {
1498 wxPuts(_T("*** Testing OS info functions ***\n"));
1499
1500 int major, minor;
1501 wxGetOsVersion(&major, &minor);
1502 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1503 wxGetOsDescription().c_str(), major, minor);
1504
1505 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1506
1507 wxPrintf(_T("Host name is %s (%s).\n"),
1508 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1509
1510 wxPuts(wxEmptyString);
1511 }
1512
1513 static void TestUserInfo()
1514 {
1515 wxPuts(_T("*** Testing user info functions ***\n"));
1516
1517 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1518 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1519 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1520 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1521
1522 wxPuts(wxEmptyString);
1523 }
1524
1525 #endif // TEST_INFO_FUNCTIONS
1526
1527 // ----------------------------------------------------------------------------
1528 // path list
1529 // ----------------------------------------------------------------------------
1530
1531 #ifdef TEST_PATHLIST
1532
1533 #ifdef __UNIX__
1534 #define CMD_IN_PATH _T("ls")
1535 #else
1536 #define CMD_IN_PATH _T("command.com")
1537 #endif
1538
1539 static void TestPathList()
1540 {
1541 wxPuts(_T("*** Testing wxPathList ***\n"));
1542
1543 wxPathList pathlist;
1544 pathlist.AddEnvList(_T("PATH"));
1545 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
1546 if ( path.empty() )
1547 {
1548 wxPrintf(_T("ERROR: command not found in the path.\n"));
1549 }
1550 else
1551 {
1552 wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
1553 }
1554 }
1555
1556 #endif // TEST_PATHLIST
1557
1558 // ----------------------------------------------------------------------------
1559 // regular expressions
1560 // ----------------------------------------------------------------------------
1561
1562 #ifdef TEST_REGEX
1563
1564 #include "wx/regex.h"
1565
1566 static void TestRegExInteractive()
1567 {
1568 wxPuts(_T("*** Testing RE interactively ***"));
1569
1570 for ( ;; )
1571 {
1572 wxChar pattern[128];
1573 wxPrintf(_T("\nEnter a pattern: "));
1574 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
1575 break;
1576
1577 // kill the last '\n'
1578 pattern[wxStrlen(pattern) - 1] = 0;
1579
1580 wxRegEx re;
1581 if ( !re.Compile(pattern) )
1582 {
1583 continue;
1584 }
1585
1586 wxChar text[128];
1587 for ( ;; )
1588 {
1589 wxPrintf(_T("Enter text to match: "));
1590 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
1591 break;
1592
1593 // kill the last '\n'
1594 text[wxStrlen(text) - 1] = 0;
1595
1596 if ( !re.Matches(text) )
1597 {
1598 wxPrintf(_T("No match.\n"));
1599 }
1600 else
1601 {
1602 wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
1603
1604 size_t start, len;
1605 for ( size_t n = 1; ; n++ )
1606 {
1607 if ( !re.GetMatch(&start, &len, n) )
1608 {
1609 break;
1610 }
1611
1612 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1613 n, wxString(text + start, len).c_str());
1614 }
1615 }
1616 }
1617 }
1618 }
1619
1620 #endif // TEST_REGEX
1621
1622 // ----------------------------------------------------------------------------
1623 // database
1624 // ----------------------------------------------------------------------------
1625
1626 #if !wxUSE_ODBC
1627 #undef TEST_ODBC
1628 #endif
1629
1630 #ifdef TEST_ODBC
1631
1632 #include "wx/db.h"
1633
1634 static void TestDbOpen()
1635 {
1636 HENV henv;
1637 wxDb db(henv);
1638 }
1639
1640 #endif // TEST_ODBC
1641
1642 // ----------------------------------------------------------------------------
1643 // printf() tests
1644 // ----------------------------------------------------------------------------
1645
1646 /*
1647 NB: this stuff was taken from the glibc test suite and modified to build
1648 in wxWidgets: if I read the copyright below properly, this shouldn't
1649 be a problem
1650 */
1651
1652 #ifdef TEST_PRINTF
1653
1654 #ifdef wxTEST_PRINTF
1655 // use our functions from wxchar.cpp
1656 #undef wxPrintf
1657 #undef wxSprintf
1658
1659 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1660 // in the tests below
1661 int wxPrintf( const wxChar *format, ... );
1662 int wxSprintf( wxChar *str, const wxChar *format, ... );
1663 #endif
1664
1665 #include "wx/longlong.h"
1666
1667 #include <float.h>
1668
1669 static void rfg1 (void);
1670 static void rfg2 (void);
1671
1672
1673 static void
1674 fmtchk (const wxChar *fmt)
1675 {
1676 (void) wxPrintf(_T("%s:\t`"), fmt);
1677 (void) wxPrintf(fmt, 0x12);
1678 (void) wxPrintf(_T("'\n"));
1679 }
1680
1681 static void
1682 fmtst1chk (const wxChar *fmt)
1683 {
1684 (void) wxPrintf(_T("%s:\t`"), fmt);
1685 (void) wxPrintf(fmt, 4, 0x12);
1686 (void) wxPrintf(_T("'\n"));
1687 }
1688
1689 static void
1690 fmtst2chk (const wxChar *fmt)
1691 {
1692 (void) wxPrintf(_T("%s:\t`"), fmt);
1693 (void) wxPrintf(fmt, 4, 4, 0x12);
1694 (void) wxPrintf(_T("'\n"));
1695 }
1696
1697 /* This page is covered by the following copyright: */
1698
1699 /* (C) Copyright C E Chew
1700 *
1701 * Feel free to copy, use and distribute this software provided:
1702 *
1703 * 1. you do not pretend that you wrote it
1704 * 2. you leave this copyright notice intact.
1705 */
1706
1707 /*
1708 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1709 */
1710
1711 #define DEC -123
1712 #define INT 255
1713 #define UNS (~0)
1714
1715 /* Formatted Output Test
1716 *
1717 * This exercises the output formatting code.
1718 */
1719
1720 wxChar *PointerNull = NULL;
1721
1722 static void
1723 fp_test (void)
1724 {
1725 int i, j, k, l;
1726 wxChar buf[7];
1727 wxChar *prefix = buf;
1728 wxChar tp[20];
1729
1730 wxPuts(_T("\nFormatted output test"));
1731 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1732 wxStrcpy(prefix, _T("%"));
1733 for (i = 0; i < 2; i++) {
1734 for (j = 0; j < 2; j++) {
1735 for (k = 0; k < 2; k++) {
1736 for (l = 0; l < 2; l++) {
1737 wxStrcpy(prefix, _T("%"));
1738 if (i == 0) wxStrcat(prefix, _T("-"));
1739 if (j == 0) wxStrcat(prefix, _T("+"));
1740 if (k == 0) wxStrcat(prefix, _T("#"));
1741 if (l == 0) wxStrcat(prefix, _T("0"));
1742 wxPrintf(_T("%5s |"), prefix);
1743 wxStrcpy(tp, prefix);
1744 wxStrcat(tp, _T("6d |"));
1745 wxPrintf(tp, DEC);
1746 wxStrcpy(tp, prefix);
1747 wxStrcat(tp, _T("6o |"));
1748 wxPrintf(tp, INT);
1749 wxStrcpy(tp, prefix);
1750 wxStrcat(tp, _T("6x |"));
1751 wxPrintf(tp, INT);
1752 wxStrcpy(tp, prefix);
1753 wxStrcat(tp, _T("6X |"));
1754 wxPrintf(tp, INT);
1755 wxStrcpy(tp, prefix);
1756 wxStrcat(tp, _T("6u |"));
1757 wxPrintf(tp, UNS);
1758 wxPrintf(_T("\n"));
1759 }
1760 }
1761 }
1762 }
1763 wxPrintf(_T("%10s\n"), PointerNull);
1764 wxPrintf(_T("%-10s\n"), PointerNull);
1765 }
1766
1767 static void TestPrintf()
1768 {
1769 static wxChar shortstr[] = _T("Hi, Z.");
1770 static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
1771 I am ready for my first lesson today.");
1772 int result = 0;
1773 wxString test_format;
1774
1775 fmtchk(_T("%.4x"));
1776 fmtchk(_T("%04x"));
1777 fmtchk(_T("%4.4x"));
1778 fmtchk(_T("%04.4x"));
1779 fmtchk(_T("%4.3x"));
1780 fmtchk(_T("%04.3x"));
1781
1782 fmtst1chk(_T("%.*x"));
1783 fmtst1chk(_T("%0*x"));
1784 fmtst2chk(_T("%*.*x"));
1785 fmtst2chk(_T("%0*.*x"));
1786
1787 wxString bad_format = _T("bad format:\t\"%b\"\n");
1788 wxPrintf(bad_format.c_str());
1789 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
1790
1791 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1792 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1793 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1794 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1795 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1796 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1797 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1798 test_format = _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1799 wxPrintf(test_format.c_str(), -123456);
1800 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1801 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1802
1803 test_format = _T("zero-padded string:\t\"%010s\"\n");
1804 wxPrintf(test_format.c_str(), shortstr);
1805 test_format = _T("left-adjusted Z string:\t\"%-010s\"\n");
1806 wxPrintf(test_format.c_str(), shortstr);
1807 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
1808 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
1809 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull);
1810 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
1811
1812 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1813 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1814 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1815 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
1816 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1817 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1818 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1819 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1820 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1821 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1822 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1823 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
1824
1825 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1826 wxPrintf (_T(" %6.5f\n"), .1);
1827 wxPrintf (_T("x%5.4fx\n"), .5);
1828
1829 wxPrintf (_T("%#03x\n"), 1);
1830
1831 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1832
1833 {
1834 double d = FLT_MIN;
1835 int niter = 17;
1836
1837 while (niter-- != 0)
1838 wxPrintf (_T("%.17e\n"), d / 2);
1839 fflush (stdout);
1840 }
1841
1842 #ifndef __WATCOMC__
1843 // Open Watcom cause compiler error here
1844 // Error! E173: col(24) floating-point constant too small to represent
1845 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1846 #endif
1847
1848 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1849 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
1850 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
1851 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
1852 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
1853 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
1854 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
1855 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
1856 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
1857 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
1858 #undef FORMAT
1859
1860 {
1861 wxChar buf[20];
1862 int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
1863
1864 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1865 rc, WXSIZEOF(buf), buf);
1866 #if 0
1867 wxChar buf2[512];
1868 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1869 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
1870 #endif
1871 }
1872
1873 fp_test ();
1874
1875 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1876 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1877 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1878 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1879 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1880 wxPrintf (_T("%g should be 10\n"), 10.0);
1881 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1882
1883 {
1884 double x=1.0;
1885 wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
1886 }
1887
1888 {
1889 wxChar buf[200];
1890
1891 wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1892
1893 result |= wxStrcmp (buf,
1894 _T("onetwo three "));
1895
1896 wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
1897 }
1898
1899 #ifdef wxLongLong_t
1900 {
1901 wxChar buf[200];
1902
1903 wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
1904 #if 0
1905 // for some reason below line fails under Borland
1906 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
1907 #endif
1908
1909 if (wxStrcmp (buf, _T("40000000000")) != 0)
1910 {
1911 result = 1;
1912 wxPuts (_T("\tFAILED"));
1913 }
1914 wxUnusedVar(result);
1915 wxPuts (wxEmptyString);
1916 }
1917 #endif // wxLongLong_t
1918
1919 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
1920 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
1921
1922 wxPuts (_T("--- Should be no further output. ---"));
1923 rfg1 ();
1924 rfg2 ();
1925
1926 #if 0
1927 {
1928 wxChar bytes[7];
1929 wxChar buf[20];
1930
1931 memset (bytes, '\xff', sizeof bytes);
1932 wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
1933 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
1934 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
1935 {
1936 wxPuts (_T("%hhn overwrite more bytes"));
1937 result = 1;
1938 }
1939 if (bytes[3] != 3)
1940 {
1941 wxPuts (_T("%hhn wrote incorrect value"));
1942 result = 1;
1943 }
1944 }
1945 #endif
1946 }
1947
1948 static void
1949 rfg1 (void)
1950 {
1951 wxChar buf[100];
1952
1953 wxSprintf (buf, _T("%5.s"), _T("xyz"));
1954 if (wxStrcmp (buf, _T(" ")) != 0)
1955 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
1956 wxSprintf (buf, _T("%5.f"), 33.3);
1957 if (wxStrcmp (buf, _T(" 33")) != 0)
1958 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
1959 wxSprintf (buf, _T("%8.e"), 33.3e7);
1960 if (wxStrcmp (buf, _T(" 3e+08")) != 0)
1961 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
1962 wxSprintf (buf, _T("%8.E"), 33.3e7);
1963 if (wxStrcmp (buf, _T(" 3E+08")) != 0)
1964 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
1965 wxSprintf (buf, _T("%.g"), 33.3);
1966 if (wxStrcmp (buf, _T("3e+01")) != 0)
1967 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
1968 wxSprintf (buf, _T("%.G"), 33.3);
1969 if (wxStrcmp (buf, _T("3E+01")) != 0)
1970 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
1971 }
1972
1973 static void
1974 rfg2 (void)
1975 {
1976 int prec;
1977 wxChar buf[100];
1978 wxString test_format;
1979
1980 prec = 0;
1981 wxSprintf (buf, _T("%.*g"), prec, 3.3);
1982 if (wxStrcmp (buf, _T("3")) != 0)
1983 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
1984 prec = 0;
1985 wxSprintf (buf, _T("%.*G"), prec, 3.3);
1986 if (wxStrcmp (buf, _T("3")) != 0)
1987 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
1988 prec = 0;
1989 wxSprintf (buf, _T("%7.*G"), prec, 3.33);
1990 if (wxStrcmp (buf, _T(" 3")) != 0)
1991 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
1992 prec = 3;
1993 test_format = _T("%04.*o");
1994 wxSprintf (buf, test_format.c_str(), prec, 33);
1995 if (wxStrcmp (buf, _T(" 041")) != 0)
1996 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
1997 prec = 7;
1998 test_format = _T("%09.*u");
1999 wxSprintf (buf, test_format.c_str(), prec, 33);
2000 if (wxStrcmp (buf, _T(" 0000033")) != 0)
2001 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
2002 prec = 3;
2003 test_format = _T("%04.*x");
2004 wxSprintf (buf, test_format.c_str(), prec, 33);
2005 if (wxStrcmp (buf, _T(" 021")) != 0)
2006 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2007 prec = 3;
2008 test_format = _T("%04.*X");
2009 wxSprintf (buf, test_format.c_str(), prec, 33);
2010 if (wxStrcmp (buf, _T(" 021")) != 0)
2011 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2012 }
2013
2014 #endif // TEST_PRINTF
2015
2016 // ----------------------------------------------------------------------------
2017 // registry and related stuff
2018 // ----------------------------------------------------------------------------
2019
2020 // this is for MSW only
2021 #ifndef __WXMSW__
2022 #undef TEST_REGCONF
2023 #undef TEST_REGISTRY
2024 #endif
2025
2026 #ifdef TEST_REGCONF
2027
2028 #include "wx/confbase.h"
2029 #include "wx/msw/regconf.h"
2030
2031 #if 0
2032 static void TestRegConfWrite()
2033 {
2034 wxConfig *config = new wxConfig(_T("myapp"));
2035 config->SetPath(_T("/group1"));
2036 config->Write(_T("entry1"), _T("foo"));
2037 config->SetPath(_T("/group2"));
2038 config->Write(_T("entry1"), _T("bar"));
2039 }
2040 #endif
2041
2042 static void TestRegConfRead()
2043 {
2044 wxConfig *config = new wxConfig(_T("myapp"));
2045
2046 wxString str;
2047 long dummy;
2048 config->SetPath(_T("/"));
2049 wxPuts(_T("Enumerating / subgroups:"));
2050 bool bCont = config->GetFirstGroup(str, dummy);
2051 while(bCont)
2052 {
2053 wxPuts(str);
2054 bCont = config->GetNextGroup(str, dummy);
2055 }
2056 }
2057
2058 #endif // TEST_REGCONF
2059
2060 #ifdef TEST_REGISTRY
2061
2062 #include "wx/msw/registry.h"
2063
2064 // I chose this one because I liked its name, but it probably only exists under
2065 // NT
2066 static const wxChar *TESTKEY =
2067 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2068
2069 static void TestRegistryRead()
2070 {
2071 wxPuts(_T("*** testing registry reading ***"));
2072
2073 wxRegKey key(TESTKEY);
2074 wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
2075 if ( !key.Open() )
2076 {
2077 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2078
2079 return;
2080 }
2081
2082 size_t nSubKeys, nValues;
2083 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2084 {
2085 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
2086 }
2087
2088 wxPrintf(_T("Enumerating values:\n"));
2089
2090 long dummy;
2091 wxString value;
2092 bool cont = key.GetFirstValue(value, dummy);
2093 while ( cont )
2094 {
2095 wxPrintf(_T("Value '%s': type "), value.c_str());
2096 switch ( key.GetValueType(value) )
2097 {
2098 case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
2099 case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
2100 case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
2101 case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
2102 case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
2103 case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
2104 default: wxPrintf(_T("other (unknown)")); break;
2105 }
2106
2107 wxPrintf(_T(", value = "));
2108 if ( key.IsNumericValue(value) )
2109 {
2110 long val;
2111 key.QueryValue(value, &val);
2112 wxPrintf(_T("%ld"), val);
2113 }
2114 else // string
2115 {
2116 wxString val;
2117 key.QueryValue(value, val);
2118 wxPrintf(_T("'%s'"), val.c_str());
2119
2120 key.QueryRawValue(value, val);
2121 wxPrintf(_T(" (raw value '%s')"), val.c_str());
2122 }
2123
2124 wxPutchar('\n');
2125
2126 cont = key.GetNextValue(value, dummy);
2127 }
2128 }
2129
2130 static void TestRegistryAssociation()
2131 {
2132 /*
2133 The second call to deleteself genertaes an error message, with a
2134 messagebox saying .flo is crucial to system operation, while the .ddf
2135 call also fails, but with no error message
2136 */
2137
2138 wxRegKey key;
2139
2140 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2141 key.Create();
2142 key = _T("ddxf_auto_file") ;
2143 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2144 key.Create();
2145 key = _T("ddxf_auto_file") ;
2146 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2147 key.Create();
2148 key = _T("program,0") ;
2149 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2150 key.Create();
2151 key = _T("program \"%1\"") ;
2152
2153 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2154 key.DeleteSelf();
2155 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2156 key.DeleteSelf();
2157 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2158 key.DeleteSelf();
2159 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2160 key.DeleteSelf();
2161 }
2162
2163 #endif // TEST_REGISTRY
2164
2165 // ----------------------------------------------------------------------------
2166 // scope guard
2167 // ----------------------------------------------------------------------------
2168
2169 #ifdef TEST_SCOPEGUARD
2170
2171 #include "wx/scopeguard.h"
2172
2173 static void function0() { puts("function0()"); }
2174 static void function1(int n) { printf("function1(%d)\n", n); }
2175 static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
2176
2177 struct Object
2178 {
2179 void method0() { printf("method0()\n"); }
2180 void method1(int n) { printf("method1(%d)\n", n); }
2181 void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
2182 };
2183
2184 static void TestScopeGuard()
2185 {
2186 wxON_BLOCK_EXIT0(function0);
2187 wxON_BLOCK_EXIT1(function1, 17);
2188 wxON_BLOCK_EXIT2(function2, 3.14, 'p');
2189
2190 Object obj;
2191 wxON_BLOCK_EXIT_OBJ0(obj, Object::method0);
2192 wxON_BLOCK_EXIT_OBJ1(obj, Object::method1, 7);
2193 wxON_BLOCK_EXIT_OBJ2(obj, Object::method2, 2.71, 'e');
2194
2195 wxScopeGuard dismissed = wxMakeGuard(function0);
2196 dismissed.Dismiss();
2197 }
2198
2199 #endif
2200
2201 // ----------------------------------------------------------------------------
2202 // sockets
2203 // ----------------------------------------------------------------------------
2204
2205 #ifdef TEST_SOCKETS
2206
2207 #include "wx/socket.h"
2208 #include "wx/protocol/protocol.h"
2209 #include "wx/protocol/http.h"
2210
2211 static void TestSocketServer()
2212 {
2213 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2214
2215 static const int PORT = 3000;
2216
2217 wxIPV4address addr;
2218 addr.Service(PORT);
2219
2220 wxSocketServer *server = new wxSocketServer(addr);
2221 if ( !server->Ok() )
2222 {
2223 wxPuts(_T("ERROR: failed to bind"));
2224
2225 return;
2226 }
2227
2228 bool quit = false;
2229 while ( !quit )
2230 {
2231 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
2232
2233 wxSocketBase *socket = server->Accept();
2234 if ( !socket )
2235 {
2236 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2237 break;
2238 }
2239
2240 wxPuts(_T("Server: got a client."));
2241
2242 server->SetTimeout(60); // 1 min
2243
2244 bool close = false;
2245 while ( !close && socket->IsConnected() )
2246 {
2247 wxString s;
2248 wxChar ch = _T('\0');
2249 for ( ;; )
2250 {
2251 if ( socket->Read(&ch, sizeof(ch)).Error() )
2252 {
2253 // don't log error if the client just close the connection
2254 if ( socket->IsConnected() )
2255 {
2256 wxPuts(_T("ERROR: in wxSocket::Read."));
2257 }
2258
2259 break;
2260 }
2261
2262 if ( ch == '\r' )
2263 continue;
2264
2265 if ( ch == '\n' )
2266 break;
2267
2268 s += ch;
2269 }
2270
2271 if ( ch != '\n' )
2272 {
2273 break;
2274 }
2275
2276 wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
2277 if ( s == _T("close") )
2278 {
2279 wxPuts(_T("Closing connection"));
2280
2281 close = true;
2282 }
2283 else if ( s == _T("quit") )
2284 {
2285 close =
2286 quit = true;
2287
2288 wxPuts(_T("Shutting down the server"));
2289 }
2290 else // not a special command
2291 {
2292 socket->Write(s.MakeUpper().c_str(), s.length());
2293 socket->Write("\r\n", 2);
2294 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
2295 }
2296 }
2297
2298 if ( !close )
2299 {
2300 wxPuts(_T("Server: lost a client unexpectedly."));
2301 }
2302
2303 socket->Destroy();
2304 }
2305
2306 // same as "delete server" but is consistent with GUI programs
2307 server->Destroy();
2308 }
2309
2310 static void TestSocketClient()
2311 {
2312 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2313
2314 static const wxChar *hostname = _T("www.wxwidgets.org");
2315
2316 wxIPV4address addr;
2317 addr.Hostname(hostname);
2318 addr.Service(80);
2319
2320 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
2321
2322 wxSocketClient client;
2323 if ( !client.Connect(addr) )
2324 {
2325 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2326 }
2327 else
2328 {
2329 wxPrintf(_T("--- Connected to %s:%u...\n"),
2330 addr.Hostname().c_str(), addr.Service());
2331
2332 wxChar buf[8192];
2333
2334 // could use simply "GET" here I suppose
2335 wxString cmdGet =
2336 wxString::Format(_T("GET http://%s/\r\n"), hostname);
2337 client.Write(cmdGet, cmdGet.length());
2338 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2339 MakePrintable(cmdGet).c_str());
2340 client.Read(buf, WXSIZEOF(buf));
2341 wxPrintf(_T("--- Server replied:\n%s"), buf);
2342 }
2343 }
2344
2345 #endif // TEST_SOCKETS
2346
2347 // ----------------------------------------------------------------------------
2348 // FTP
2349 // ----------------------------------------------------------------------------
2350
2351 #ifdef TEST_FTP
2352
2353 #include "wx/protocol/ftp.h"
2354
2355 static wxFTP ftp;
2356
2357 #define FTP_ANONYMOUS
2358
2359 #ifdef FTP_ANONYMOUS
2360 static const wxChar *directory = _T("/pub");
2361 static const wxChar *filename = _T("welcome.msg");
2362 #else
2363 static const wxChar *directory = _T("/etc");
2364 static const wxChar *filename = _T("issue");
2365 #endif
2366
2367 static bool TestFtpConnect()
2368 {
2369 wxPuts(_T("*** Testing FTP connect ***"));
2370
2371 #ifdef FTP_ANONYMOUS
2372 static const wxChar *hostname = _T("ftp.wxwidgets.org");
2373
2374 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
2375 #else // !FTP_ANONYMOUS
2376 static const wxChar *hostname = "localhost";
2377
2378 wxChar user[256];
2379 wxFgets(user, WXSIZEOF(user), stdin);
2380 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
2381 ftp.SetUser(user);
2382
2383 wxChar password[256];
2384 wxPrintf(_T("Password for %s: "), password);
2385 wxFgets(password, WXSIZEOF(password), stdin);
2386 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
2387 ftp.SetPassword(password);
2388
2389 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
2390 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2391
2392 if ( !ftp.Connect(hostname) )
2393 {
2394 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2395
2396 return false;
2397 }
2398 else
2399 {
2400 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2401 hostname, ftp.Pwd().c_str());
2402 ftp.Close();
2403 }
2404
2405 return true;
2406 }
2407
2408 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2409 static void TestFtpWuFtpd()
2410 {
2411 wxFTP ftp;
2412 static const wxChar *hostname = _T("ftp.eudora.com");
2413 if ( !ftp.Connect(hostname) )
2414 {
2415 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2416 }
2417 else
2418 {
2419 static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
2420 wxInputStream *in = ftp.GetInputStream(filename);
2421 if ( !in )
2422 {
2423 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
2424 }
2425 else
2426 {
2427 size_t size = in->GetSize();
2428 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
2429
2430 wxChar *data = new wxChar[size];
2431 if ( !in->Read(data, size) )
2432 {
2433 wxPuts(_T("ERROR: read error"));
2434 }
2435 else
2436 {
2437 wxPrintf(_T("Successfully retrieved the file.\n"));
2438 }
2439
2440 delete [] data;
2441 delete in;
2442 }
2443 }
2444 }
2445
2446 static void TestFtpList()
2447 {
2448 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2449
2450 // test CWD
2451 if ( !ftp.ChDir(directory) )
2452 {
2453 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
2454 }
2455
2456 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2457
2458 // test NLIST and LIST
2459 wxArrayString files;
2460 if ( !ftp.GetFilesList(files) )
2461 {
2462 wxPuts(_T("ERROR: failed to get NLIST of files"));
2463 }
2464 else
2465 {
2466 wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
2467 size_t count = files.GetCount();
2468 for ( size_t n = 0; n < count; n++ )
2469 {
2470 wxPrintf(_T("\t%s\n"), files[n].c_str());
2471 }
2472 wxPuts(_T("End of the file list"));
2473 }
2474
2475 if ( !ftp.GetDirList(files) )
2476 {
2477 wxPuts(_T("ERROR: failed to get LIST of files"));
2478 }
2479 else
2480 {
2481 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
2482 size_t count = files.GetCount();
2483 for ( size_t n = 0; n < count; n++ )
2484 {
2485 wxPrintf(_T("\t%s\n"), files[n].c_str());
2486 }
2487 wxPuts(_T("End of the file list"));
2488 }
2489
2490 if ( !ftp.ChDir(_T("..")) )
2491 {
2492 wxPuts(_T("ERROR: failed to cd to .."));
2493 }
2494
2495 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2496 }
2497
2498 static void TestFtpDownload()
2499 {
2500 wxPuts(_T("*** Testing wxFTP download ***\n"));
2501
2502 // test RETR
2503 wxInputStream *in = ftp.GetInputStream(filename);
2504 if ( !in )
2505 {
2506 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
2507 }
2508 else
2509 {
2510 size_t size = in->GetSize();
2511 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
2512 fflush(stdout);
2513
2514 wxChar *data = new wxChar[size];
2515 if ( !in->Read(data, size) )
2516 {
2517 wxPuts(_T("ERROR: read error"));
2518 }
2519 else
2520 {
2521 wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
2522 }
2523
2524 delete [] data;
2525 delete in;
2526 }
2527 }
2528
2529 static void TestFtpFileSize()
2530 {
2531 wxPuts(_T("*** Testing FTP SIZE command ***"));
2532
2533 if ( !ftp.ChDir(directory) )
2534 {
2535 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
2536 }
2537
2538 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2539
2540 if ( ftp.FileExists(filename) )
2541 {
2542 int size = ftp.GetFileSize(filename);
2543 if ( size == -1 )
2544 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
2545 else
2546 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
2547 }
2548 else
2549 {
2550 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
2551 }
2552 }
2553
2554 static void TestFtpMisc()
2555 {
2556 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2557
2558 if ( ftp.SendCommand(_T("STAT")) != '2' )
2559 {
2560 wxPuts(_T("ERROR: STAT failed"));
2561 }
2562 else
2563 {
2564 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
2565 }
2566
2567 if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
2568 {
2569 wxPuts(_T("ERROR: HELP SITE failed"));
2570 }
2571 else
2572 {
2573 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2574 ftp.GetLastResult().c_str());
2575 }
2576 }
2577
2578 static void TestFtpInteractive()
2579 {
2580 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2581
2582 wxChar buf[128];
2583
2584 for ( ;; )
2585 {
2586 wxPrintf(_T("Enter FTP command: "));
2587 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
2588 break;
2589
2590 // kill the last '\n'
2591 buf[wxStrlen(buf) - 1] = 0;
2592
2593 // special handling of LIST and NLST as they require data connection
2594 wxString start(buf, 4);
2595 start.MakeUpper();
2596 if ( start == _T("LIST") || start == _T("NLST") )
2597 {
2598 wxString wildcard;
2599 if ( wxStrlen(buf) > 4 )
2600 wildcard = buf + 5;
2601
2602 wxArrayString files;
2603 if ( !ftp.GetList(files, wildcard, start == _T("LIST")) )
2604 {
2605 wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
2606 }
2607 else
2608 {
2609 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2610 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
2611 size_t count = files.GetCount();
2612 for ( size_t n = 0; n < count; n++ )
2613 {
2614 wxPrintf(_T("\t%s\n"), files[n].c_str());
2615 }
2616 wxPuts(_T("--- End of the file list"));
2617 }
2618 }
2619 else // !list
2620 {
2621 wxChar ch = ftp.SendCommand(buf);
2622 wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
2623 if ( ch )
2624 {
2625 wxPrintf(_T(" (return code %c)"), ch);
2626 }
2627
2628 wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
2629 }
2630 }
2631
2632 wxPuts(_T("\n*** done ***"));
2633 }
2634
2635 static void TestFtpUpload()
2636 {
2637 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2638
2639 // upload a file
2640 static const wxChar *file1 = _T("test1");
2641 static const wxChar *file2 = _T("test2");
2642 wxOutputStream *out = ftp.GetOutputStream(file1);
2643 if ( out )
2644 {
2645 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
2646 out->Write("First hello", 11);
2647 delete out;
2648 }
2649
2650 // send a command to check the remote file
2651 if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' )
2652 {
2653 wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
2654 }
2655 else
2656 {
2657 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2658 file1, ftp.GetLastResult().c_str());
2659 }
2660
2661 out = ftp.GetOutputStream(file2);
2662 if ( out )
2663 {
2664 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
2665 out->Write("Second hello", 12);
2666 delete out;
2667 }
2668 }
2669
2670 #endif // TEST_FTP
2671
2672 // ----------------------------------------------------------------------------
2673 // stack backtrace
2674 // ----------------------------------------------------------------------------
2675
2676 #ifdef TEST_STACKWALKER
2677
2678 #if wxUSE_STACKWALKER
2679
2680 #include "wx/stackwalk.h"
2681
2682 class StackDump : public wxStackWalker
2683 {
2684 public:
2685 StackDump(const char *argv0)
2686 : wxStackWalker(argv0)
2687 {
2688 }
2689
2690 virtual void Walk(size_t skip = 1)
2691 {
2692 wxPuts(_T("Stack dump:"));
2693
2694 wxStackWalker::Walk(skip);
2695 }
2696
2697 protected:
2698 virtual void OnStackFrame(const wxStackFrame& frame)
2699 {
2700 printf("[%2d] ", frame.GetLevel());
2701
2702 wxString name = frame.GetName();
2703 if ( !name.empty() )
2704 {
2705 printf("%-20.40s", name.mb_str());
2706 }
2707 else
2708 {
2709 printf("0x%08lx", (unsigned long)frame.GetAddress());
2710 }
2711
2712 if ( frame.HasSourceLocation() )
2713 {
2714 printf("\t%s:%d",
2715 frame.GetFileName().mb_str(),
2716 frame.GetLine());
2717 }
2718
2719 puts("");
2720
2721 wxString type, val;
2722 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
2723 {
2724 printf("\t%s %s = %s\n", type.mb_str(), name.mb_str(), val.mb_str());
2725 }
2726 }
2727 };
2728
2729 static void TestStackWalk(const char *argv0)
2730 {
2731 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2732
2733 StackDump dump(argv0);
2734 dump.Walk();
2735 }
2736
2737 #endif // wxUSE_STACKWALKER
2738
2739 #endif // TEST_STACKWALKER
2740
2741 // ----------------------------------------------------------------------------
2742 // standard paths
2743 // ----------------------------------------------------------------------------
2744
2745 #ifdef TEST_STDPATHS
2746
2747 #include "wx/stdpaths.h"
2748
2749 static void TestStandardPaths()
2750 {
2751 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2752
2753 wxTheApp->SetAppName(_T("console"));
2754
2755 wxStandardPathsBase& stdp = wxStandardPaths::Get();
2756 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
2757 wxPrintf(_T("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
2758 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
2759 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
2760 wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
2761 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
2762 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
2763 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
2764 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
2765 wxPrintf(_T("Localized res. dir:\t%s\n"),
2766 stdp.GetLocalizedResourcesDir(_T("fr")).c_str());
2767 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2768 stdp.GetLocalizedResourcesDir
2769 (
2770 _T("fr"),
2771 wxStandardPaths::ResourceCat_Messages
2772 ).c_str());
2773 }
2774
2775 #endif // TEST_STDPATHS
2776
2777 // ----------------------------------------------------------------------------
2778 // streams
2779 // ----------------------------------------------------------------------------
2780
2781 #ifdef TEST_STREAMS
2782
2783 #include "wx/wfstream.h"
2784 #include "wx/mstream.h"
2785
2786 static void TestFileStream()
2787 {
2788 wxPuts(_T("*** Testing wxFileInputStream ***"));
2789
2790 static const wxString filename = _T("testdata.fs");
2791 {
2792 wxFileOutputStream fsOut(filename);
2793 fsOut.Write("foo", 3);
2794 }
2795
2796 wxFileInputStream fsIn(filename);
2797 wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
2798 while ( !fsIn.Eof() )
2799 {
2800 wxPutchar(fsIn.GetC());
2801 }
2802
2803 if ( !wxRemoveFile(filename) )
2804 {
2805 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename.c_str());
2806 }
2807
2808 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2809 }
2810
2811 static void TestMemoryStream()
2812 {
2813 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2814
2815 wxMemoryOutputStream memOutStream;
2816 wxPrintf(_T("Initially out stream offset: %lu\n"),
2817 (unsigned long)memOutStream.TellO());
2818
2819 for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
2820 {
2821 memOutStream.PutC(*p);
2822 }
2823
2824 wxPrintf(_T("Final out stream offset: %lu\n"),
2825 (unsigned long)memOutStream.TellO());
2826
2827 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2828
2829 wxChar buf[1024];
2830 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
2831
2832 wxMemoryInputStream memInpStream(buf, len);
2833 wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
2834 while ( !memInpStream.Eof() )
2835 {
2836 wxPutchar(memInpStream.GetC());
2837 }
2838
2839 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2840 }
2841
2842 #endif // TEST_STREAMS
2843
2844 // ----------------------------------------------------------------------------
2845 // timers
2846 // ----------------------------------------------------------------------------
2847
2848 #ifdef TEST_TIMER
2849
2850 #include "wx/stopwatch.h"
2851 #include "wx/utils.h"
2852
2853 static void TestStopWatch()
2854 {
2855 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2856
2857 wxStopWatch sw;
2858 sw.Pause();
2859 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2860 fflush(stdout);
2861 wxSleep(2);
2862 wxPrintf(_T("\t%ldms\n"), sw.Time());
2863
2864 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2865 fflush(stdout);
2866 sw.Resume();
2867 wxSleep(3);
2868 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2869
2870 sw.Pause();
2871 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2872 fflush(stdout);
2873 wxSleep(2);
2874 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2875
2876 sw.Resume();
2877 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2878 fflush(stdout);
2879 wxSleep(2);
2880 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2881
2882 wxStopWatch sw2;
2883 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2884 for ( size_t n = 0; n < 70; n++ )
2885 {
2886 sw2.Start();
2887
2888 for ( size_t m = 0; m < 100000; m++ )
2889 {
2890 if ( sw.Time() < 0 || sw2.Time() < 0 )
2891 {
2892 wxPuts(_T("\ntime is negative - ERROR!"));
2893 }
2894 }
2895
2896 wxPutchar('.');
2897 fflush(stdout);
2898 }
2899
2900 wxPuts(_T(", ok."));
2901 }
2902
2903 #endif // TEST_TIMER
2904
2905 // ----------------------------------------------------------------------------
2906 // vCard support
2907 // ----------------------------------------------------------------------------
2908
2909 #ifdef TEST_VCARD
2910
2911 #include "wx/vcard.h"
2912
2913 static void DumpVObject(size_t level, const wxVCardObject& vcard)
2914 {
2915 void *cookie;
2916 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
2917 while ( vcObj )
2918 {
2919 wxPrintf(_T("%s%s"),
2920 wxString(_T('\t'), level).c_str(),
2921 vcObj->GetName().c_str());
2922
2923 wxString value;
2924 switch ( vcObj->GetType() )
2925 {
2926 case wxVCardObject::String:
2927 case wxVCardObject::UString:
2928 {
2929 wxString val;
2930 vcObj->GetValue(&val);
2931 value << _T('"') << val << _T('"');
2932 }
2933 break;
2934
2935 case wxVCardObject::Int:
2936 {
2937 unsigned int i;
2938 vcObj->GetValue(&i);
2939 value.Printf(_T("%u"), i);
2940 }
2941 break;
2942
2943 case wxVCardObject::Long:
2944 {
2945 unsigned long l;
2946 vcObj->GetValue(&l);
2947 value.Printf(_T("%lu"), l);
2948 }
2949 break;
2950
2951 case wxVCardObject::None:
2952 break;
2953
2954 case wxVCardObject::Object:
2955 value = _T("<node>");
2956 break;
2957
2958 default:
2959 value = _T("<unknown value type>");
2960 }
2961
2962 if ( !!value )
2963 wxPrintf(_T(" = %s"), value.c_str());
2964 wxPutchar('\n');
2965
2966 DumpVObject(level + 1, *vcObj);
2967
2968 delete vcObj;
2969 vcObj = vcard.GetNextProp(&cookie);
2970 }
2971 }
2972
2973 static void DumpVCardAddresses(const wxVCard& vcard)
2974 {
2975 wxPuts(_T("\nShowing all addresses from vCard:\n"));
2976
2977 size_t nAdr = 0;
2978 void *cookie;
2979 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
2980 while ( addr )
2981 {
2982 wxString flagsStr;
2983 int flags = addr->GetFlags();
2984 if ( flags & wxVCardAddress::Domestic )
2985 {
2986 flagsStr << _T("domestic ");
2987 }
2988 if ( flags & wxVCardAddress::Intl )
2989 {
2990 flagsStr << _T("international ");
2991 }
2992 if ( flags & wxVCardAddress::Postal )
2993 {
2994 flagsStr << _T("postal ");
2995 }
2996 if ( flags & wxVCardAddress::Parcel )
2997 {
2998 flagsStr << _T("parcel ");
2999 }
3000 if ( flags & wxVCardAddress::Home )
3001 {
3002 flagsStr << _T("home ");
3003 }
3004 if ( flags & wxVCardAddress::Work )
3005 {
3006 flagsStr << _T("work ");
3007 }
3008
3009 wxPrintf(_T("Address %u:\n")
3010 "\tflags = %s\n"
3011 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3012 ++nAdr,
3013 flagsStr.c_str(),
3014 addr->GetPostOffice().c_str(),
3015 addr->GetExtAddress().c_str(),
3016 addr->GetStreet().c_str(),
3017 addr->GetLocality().c_str(),
3018 addr->GetRegion().c_str(),
3019 addr->GetPostalCode().c_str(),
3020 addr->GetCountry().c_str()
3021 );
3022
3023 delete addr;
3024 addr = vcard.GetNextAddress(&cookie);
3025 }
3026 }
3027
3028 static void DumpVCardPhoneNumbers(const wxVCard& vcard)
3029 {
3030 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3031
3032 size_t nPhone = 0;
3033 void *cookie;
3034 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
3035 while ( phone )
3036 {
3037 wxString flagsStr;
3038 int flags = phone->GetFlags();
3039 if ( flags & wxVCardPhoneNumber::Voice )
3040 {
3041 flagsStr << _T("voice ");
3042 }
3043 if ( flags & wxVCardPhoneNumber::Fax )
3044 {
3045 flagsStr << _T("fax ");
3046 }
3047 if ( flags & wxVCardPhoneNumber::Cellular )
3048 {
3049 flagsStr << _T("cellular ");
3050 }
3051 if ( flags & wxVCardPhoneNumber::Modem )
3052 {
3053 flagsStr << _T("modem ");
3054 }
3055 if ( flags & wxVCardPhoneNumber::Home )
3056 {
3057 flagsStr << _T("home ");
3058 }
3059 if ( flags & wxVCardPhoneNumber::Work )
3060 {
3061 flagsStr << _T("work ");
3062 }
3063
3064 wxPrintf(_T("Phone number %u:\n")
3065 "\tflags = %s\n"
3066 "\tvalue = %s\n",
3067 ++nPhone,
3068 flagsStr.c_str(),
3069 phone->GetNumber().c_str()
3070 );
3071
3072 delete phone;
3073 phone = vcard.GetNextPhoneNumber(&cookie);
3074 }
3075 }
3076
3077 static void TestVCardRead()
3078 {
3079 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3080
3081 wxVCard vcard(_T("vcard.vcf"));
3082 if ( !vcard.IsOk() )
3083 {
3084 wxPuts(_T("ERROR: couldn't load vCard."));
3085 }
3086 else
3087 {
3088 // read individual vCard properties
3089 wxVCardObject *vcObj = vcard.GetProperty("FN");
3090 wxString value;
3091 if ( vcObj )
3092 {
3093 vcObj->GetValue(&value);
3094 delete vcObj;
3095 }
3096 else
3097 {
3098 value = _T("<none>");
3099 }
3100
3101 wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str());
3102
3103
3104 if ( !vcard.GetFullName(&value) )
3105 {
3106 value = _T("<none>");
3107 }
3108
3109 wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str());
3110
3111 // now show how to deal with multiply occurring properties
3112 DumpVCardAddresses(vcard);
3113 DumpVCardPhoneNumbers(vcard);
3114
3115 // and finally show all
3116 wxPuts(_T("\nNow dumping the entire vCard:\n")
3117 "-----------------------------\n");
3118
3119 DumpVObject(0, vcard);
3120 }
3121 }
3122
3123 static void TestVCardWrite()
3124 {
3125 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3126
3127 wxVCard vcard;
3128 if ( !vcard.IsOk() )
3129 {
3130 wxPuts(_T("ERROR: couldn't create vCard."));
3131 }
3132 else
3133 {
3134 // set some fields
3135 vcard.SetName("Zeitlin", "Vadim");
3136 vcard.SetFullName("Vadim Zeitlin");
3137 vcard.SetOrganization("wxWidgets", "R&D");
3138
3139 // just dump the vCard back
3140 wxPuts(_T("Entire vCard follows:\n"));
3141 wxPuts(vcard.Write());
3142 }
3143 }
3144
3145 #endif // TEST_VCARD
3146
3147 // ----------------------------------------------------------------------------
3148 // wxVolume tests
3149 // ----------------------------------------------------------------------------
3150
3151 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3152 #undef TEST_VOLUME
3153 #endif
3154
3155 #ifdef TEST_VOLUME
3156
3157 #include "wx/volume.h"
3158
3159 static const wxChar *volumeKinds[] =
3160 {
3161 _T("floppy"),
3162 _T("hard disk"),
3163 _T("CD-ROM"),
3164 _T("DVD-ROM"),
3165 _T("network volume"),
3166 _T("other volume"),
3167 };
3168
3169 static void TestFSVolume()
3170 {
3171 wxPuts(_T("*** Testing wxFSVolume class ***"));
3172
3173 wxArrayString volumes = wxFSVolume::GetVolumes();
3174 size_t count = volumes.GetCount();
3175
3176 if ( !count )
3177 {
3178 wxPuts(_T("ERROR: no mounted volumes?"));
3179 return;
3180 }
3181
3182 wxPrintf(_T("%u mounted volumes found:\n"), count);
3183
3184 for ( size_t n = 0; n < count; n++ )
3185 {
3186 wxFSVolume vol(volumes[n]);
3187 if ( !vol.IsOk() )
3188 {
3189 wxPuts(_T("ERROR: couldn't create volume"));
3190 continue;
3191 }
3192
3193 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3194 n + 1,
3195 vol.GetDisplayName().c_str(),
3196 vol.GetName().c_str(),
3197 volumeKinds[vol.GetKind()],
3198 vol.IsWritable() ? _T("rw") : _T("ro"),
3199 vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable")
3200 : _T("fixed"));
3201 }
3202 }
3203
3204 #endif // TEST_VOLUME
3205
3206 // ----------------------------------------------------------------------------
3207 // wide char and Unicode support
3208 // ----------------------------------------------------------------------------
3209
3210 #ifdef TEST_WCHAR
3211
3212 #include "wx/strconv.h"
3213 #include "wx/fontenc.h"
3214 #include "wx/encconv.h"
3215 #include "wx/buffer.h"
3216
3217 static const unsigned char utf8koi8r[] =
3218 {
3219 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3220 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3221 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3222 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3223 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3224 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3225 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3226 };
3227
3228 static const unsigned char utf8iso8859_1[] =
3229 {
3230 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3231 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3232 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3233 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3234 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3235 };
3236
3237 static const unsigned char utf8Invalid[] =
3238 {
3239 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3240 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3241 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3242 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3243 0x6c, 0x61, 0x79, 0
3244 };
3245
3246 static const struct Utf8Data
3247 {
3248 const unsigned char *text;
3249 size_t len;
3250 const wxChar *charset;
3251 wxFontEncoding encoding;
3252 } utf8data[] =
3253 {
3254 { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3255 { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 },
3256 { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3257 };
3258
3259 static void TestUtf8()
3260 {
3261 wxPuts(_T("*** Testing UTF8 support ***\n"));
3262
3263 char buf[1024];
3264 wchar_t wbuf[1024];
3265
3266 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
3267 {
3268 const Utf8Data& u8d = utf8data[n];
3269 if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text,
3270 WXSIZEOF(wbuf)) == (size_t)-1 )
3271 {
3272 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3273 }
3274 else
3275 {
3276 wxCSConv conv(u8d.charset);
3277 if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 )
3278 {
3279 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset);
3280 }
3281 else
3282 {
3283 wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf);
3284 }
3285 }
3286
3287 wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text));
3288 if ( s.empty() )
3289 s = _T("<< conversion failed >>");
3290 wxPrintf(_T("String in current cset: %s\n"), s.c_str());
3291
3292 }
3293
3294 wxPuts(wxEmptyString);
3295 }
3296
3297 static void TestEncodingConverter()
3298 {
3299 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3300
3301 // using wxEncodingConverter should give the same result as above
3302 char buf[1024];
3303 wchar_t wbuf[1024];
3304 if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r,
3305 WXSIZEOF(utf8koi8r)) == (size_t)-1 )
3306 {
3307 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3308 }
3309 else
3310 {
3311 wxEncodingConverter ec;
3312 ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
3313 ec.Convert(wbuf, buf);
3314 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf);
3315 }
3316
3317 wxPuts(wxEmptyString);
3318 }
3319
3320 #endif // TEST_WCHAR
3321
3322 // ----------------------------------------------------------------------------
3323 // ZIP stream
3324 // ----------------------------------------------------------------------------
3325
3326 #ifdef TEST_ZIP
3327
3328 #include "wx/filesys.h"
3329 #include "wx/fs_zip.h"
3330 #include "wx/zipstrm.h"
3331
3332 static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
3333
3334 static void TestZipStreamRead()
3335 {
3336 wxPuts(_T("*** Testing ZIP reading ***\n"));
3337
3338 static const wxString filename = _T("foo");
3339 wxZipInputStream istr(TESTFILE_ZIP, filename);
3340 wxPrintf(_T("Archive size: %u\n"), istr.GetSize());
3341
3342 wxPrintf(_T("Dumping the file '%s':\n"), filename.c_str());
3343 while ( !istr.Eof() )
3344 {
3345 wxPutchar(istr.GetC());
3346 fflush(stdout);
3347 }
3348
3349 wxPuts(_T("\n----- done ------"));
3350 }
3351
3352 static void DumpZipDirectory(wxFileSystem& fs,
3353 const wxString& dir,
3354 const wxString& indent)
3355 {
3356 wxString prefix = wxString::Format(_T("%s#zip:%s"),
3357 TESTFILE_ZIP, dir.c_str());
3358 wxString wildcard = prefix + _T("/*");
3359
3360 wxString dirname = fs.FindFirst(wildcard, wxDIR);
3361 while ( !dirname.empty() )
3362 {
3363 if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
3364 {
3365 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3366
3367 break;
3368 }
3369
3370 wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
3371
3372 DumpZipDirectory(fs, dirname,
3373 indent + wxString(_T(' '), 4));
3374
3375 dirname = fs.FindNext();
3376 }
3377
3378 wxString filename = fs.FindFirst(wildcard, wxFILE);
3379 while ( !filename.empty() )
3380 {
3381 if ( !filename.StartsWith(prefix, &filename) )
3382 {
3383 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3384
3385 break;
3386 }
3387
3388 wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
3389
3390 filename = fs.FindNext();
3391 }
3392 }
3393
3394 static void TestZipFileSystem()
3395 {
3396 wxPuts(_T("*** Testing ZIP file system ***\n"));
3397
3398 wxFileSystem::AddHandler(new wxZipFSHandler);
3399 wxFileSystem fs;
3400 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
3401
3402 DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
3403 }
3404
3405 #endif // TEST_ZIP
3406
3407 // ----------------------------------------------------------------------------
3408 // date time
3409 // ----------------------------------------------------------------------------
3410
3411 #ifdef TEST_DATETIME
3412
3413 #include "wx/math.h"
3414 #include "wx/datetime.h"
3415
3416 // this test miscellaneous static wxDateTime functions
3417
3418 #if TEST_ALL
3419
3420 static void TestTimeStatic()
3421 {
3422 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3423
3424 // some info about the current date
3425 int year = wxDateTime::GetCurrentYear();
3426 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3427 year,
3428 wxDateTime::IsLeapYear(year) ? "" : "not ",
3429 wxDateTime::GetNumberOfDays(year));
3430
3431 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
3432 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3433 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
3434 wxDateTime::GetMonthName(month).c_str(),
3435 wxDateTime::GetNumberOfDays(month));
3436 }
3437
3438 // test time zones stuff
3439 static void TestTimeZones()
3440 {
3441 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3442
3443 wxDateTime now = wxDateTime::Now();
3444
3445 wxPrintf(_T("Current GMT time:\t%s\n"), now.Format(_T("%c"), wxDateTime::GMT0).c_str());
3446 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0).c_str());
3447 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST).c_str());
3448 wxPrintf(_T("Current time in Paris:\t%s\n"), now.Format(_T("%c"), wxDateTime::CET).c_str());
3449 wxPrintf(_T(" Moscow:\t%s\n"), now.Format(_T("%c"), wxDateTime::MSK).c_str());
3450 wxPrintf(_T(" New York:\t%s\n"), now.Format(_T("%c"), wxDateTime::EST).c_str());
3451
3452 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3453
3454 wxDateTime::Tm tm = now.GetTm();
3455 if ( wxDateTime(tm) != now )
3456 {
3457 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3458 wxDateTime(tm).Format().c_str(), now.Format().c_str());
3459 }
3460 }
3461
3462 // test some minimal support for the dates outside the standard range
3463 static void TestTimeRange()
3464 {
3465 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3466
3467 static const wxChar *fmt = _T("%d-%b-%Y %H:%M:%S");
3468
3469 wxPrintf(_T("Unix epoch:\t%s\n"),
3470 wxDateTime(2440587.5).Format(fmt).c_str());
3471 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3472 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
3473 wxPrintf(_T("JDN 0: \t%s\n"),
3474 wxDateTime(0.0).Format(fmt).c_str());
3475 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3476 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
3477 wxPrintf(_T("May 29, 2099:\t%s\n"),
3478 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
3479 }
3480
3481 // test DST calculations
3482 static void TestTimeDST()
3483 {
3484 wxPuts(_T("\n*** wxDateTime DST test ***"));
3485
3486 wxPrintf(_T("DST is%s in effect now.\n\n"),
3487 wxDateTime::Now().IsDST() ? wxEmptyString : _T(" not"));
3488
3489 for ( int year = 1990; year < 2005; year++ )
3490 {
3491 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3492 year,
3493 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
3494 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
3495 }
3496 }
3497
3498 #endif // TEST_ALL
3499
3500 #if TEST_INTERACTIVE
3501
3502 static void TestDateTimeInteractive()
3503 {
3504 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3505
3506 wxChar buf[128];
3507
3508 for ( ;; )
3509 {
3510 wxPrintf(_T("Enter a date: "));
3511 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
3512 break;
3513
3514 // kill the last '\n'
3515 buf[wxStrlen(buf) - 1] = 0;
3516
3517 wxDateTime dt;
3518 const wxChar *p = dt.ParseDate(buf);
3519 if ( !p )
3520 {
3521 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf);
3522
3523 continue;
3524 }
3525 else if ( *p )
3526 {
3527 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf);
3528 }
3529
3530 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3531 dt.Format(_T("%b %d, %Y")).c_str(),
3532 dt.GetDayOfYear(),
3533 dt.GetWeekOfMonth(wxDateTime::Monday_First),
3534 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
3535 dt.GetWeekOfYear(wxDateTime::Monday_First));
3536 }
3537
3538 wxPuts(_T("\n*** done ***"));
3539 }
3540
3541 #endif // TEST_INTERACTIVE
3542
3543 #if TEST_ALL
3544
3545 static void TestTimeMS()
3546 {
3547 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3548
3549 wxDateTime dt1 = wxDateTime::Now(),
3550 dt2 = wxDateTime::UNow();
3551
3552 wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str());
3553 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3554 wxPrintf(_T("Dummy loop: "));
3555 for ( int i = 0; i < 6000; i++ )
3556 {
3557 //for ( int j = 0; j < 10; j++ )
3558 {
3559 wxString s;
3560 s.Printf(_T("%g"), sqrt((float)i));
3561 }
3562
3563 if ( !(i % 100) )
3564 wxPutchar('.');
3565 }
3566 wxPuts(_T(", done"));
3567
3568 dt1 = dt2;
3569 dt2 = wxDateTime::UNow();
3570 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3571
3572 wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str());
3573
3574 wxPuts(_T("\n*** done ***"));
3575 }
3576
3577 static void TestTimeHolidays()
3578 {
3579 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3580
3581 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
3582 wxDateTime dtStart(1, tm.mon, tm.year),
3583 dtEnd = dtStart.GetLastMonthDay();
3584
3585 wxDateTimeArray hol;
3586 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
3587
3588 const wxChar *format = _T("%d-%b-%Y (%a)");
3589
3590 wxPrintf(_T("All holidays between %s and %s:\n"),
3591 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
3592
3593 size_t count = hol.GetCount();
3594 for ( size_t n = 0; n < count; n++ )
3595 {
3596 wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str());
3597 }
3598
3599 wxPuts(wxEmptyString);
3600 }
3601
3602 static void TestTimeZoneBug()
3603 {
3604 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3605
3606 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
3607 for ( int i = 0; i < 31; i++ )
3608 {
3609 wxPrintf(_T("Date %s: week day %s.\n"),
3610 date.Format(_T("%d-%m-%Y")).c_str(),
3611 date.GetWeekDayName(date.GetWeekDay()).c_str());
3612
3613 date += wxDateSpan::Day();
3614 }
3615
3616 wxPuts(wxEmptyString);
3617 }
3618
3619 static void TestTimeSpanFormat()
3620 {
3621 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3622
3623 static const wxChar *formats[] =
3624 {
3625 _T("(default) %H:%M:%S"),
3626 _T("%E weeks and %D days"),
3627 _T("%l milliseconds"),
3628 _T("(with ms) %H:%M:%S:%l"),
3629 _T("100%% of minutes is %M"), // test "%%"
3630 _T("%D days and %H hours"),
3631 _T("or also %S seconds"),
3632 };
3633
3634 wxTimeSpan ts1(1, 2, 3, 4),
3635 ts2(111, 222, 333);
3636 for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
3637 {
3638 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3639 ts1.Format(formats[n]).c_str(),
3640 ts2.Format(formats[n]).c_str());
3641 }
3642
3643 wxPuts(wxEmptyString);
3644 }
3645
3646 #endif // TEST_ALL
3647
3648 #endif // TEST_DATETIME
3649
3650 // ----------------------------------------------------------------------------
3651 // wxTextInput/OutputStream
3652 // ----------------------------------------------------------------------------
3653
3654 #ifdef TEST_TEXTSTREAM
3655
3656 #include "wx/txtstrm.h"
3657 #include "wx/wfstream.h"
3658
3659 static void TestTextInputStream()
3660 {
3661 wxPuts(_T("\n*** wxTextInputStream test ***"));
3662
3663 wxString filename = _T("testdata.fc");
3664 wxFileInputStream fsIn(filename);
3665 if ( !fsIn.Ok() )
3666 {
3667 wxPuts(_T("ERROR: couldn't open file."));
3668 }
3669 else
3670 {
3671 wxTextInputStream tis(fsIn);
3672
3673 size_t line = 1;
3674 for ( ;; )
3675 {
3676 const wxString s = tis.ReadLine();
3677
3678 // line could be non empty if the last line of the file isn't
3679 // terminated with EOL
3680 if ( fsIn.Eof() && s.empty() )
3681 break;
3682
3683 wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
3684 }
3685 }
3686 }
3687
3688 #endif // TEST_TEXTSTREAM
3689
3690 // ----------------------------------------------------------------------------
3691 // threads
3692 // ----------------------------------------------------------------------------
3693
3694 #ifdef TEST_THREADS
3695
3696 #include "wx/thread.h"
3697
3698 static size_t gs_counter = (size_t)-1;
3699 static wxCriticalSection gs_critsect;
3700 static wxSemaphore gs_cond;
3701
3702 class MyJoinableThread : public wxThread
3703 {
3704 public:
3705 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
3706 { m_n = n; Create(); }
3707
3708 // thread execution starts here
3709 virtual ExitCode Entry();
3710
3711 private:
3712 size_t m_n;
3713 };
3714
3715 wxThread::ExitCode MyJoinableThread::Entry()
3716 {
3717 unsigned long res = 1;
3718 for ( size_t n = 1; n < m_n; n++ )
3719 {
3720 res *= n;
3721
3722 // it's a loooong calculation :-)
3723 Sleep(100);
3724 }
3725
3726 return (ExitCode)res;
3727 }
3728
3729 class MyDetachedThread : public wxThread
3730 {
3731 public:
3732 MyDetachedThread(size_t n, wxChar ch)
3733 {
3734 m_n = n;
3735 m_ch = ch;
3736 m_cancelled = false;
3737
3738 Create();
3739 }
3740
3741 // thread execution starts here
3742 virtual ExitCode Entry();
3743
3744 // and stops here
3745 virtual void OnExit();
3746
3747 private:
3748 size_t m_n; // number of characters to write
3749 wxChar m_ch; // character to write
3750
3751 bool m_cancelled; // false if we exit normally
3752 };
3753
3754 wxThread::ExitCode MyDetachedThread::Entry()
3755 {
3756 {
3757 wxCriticalSectionLocker lock(gs_critsect);
3758 if ( gs_counter == (size_t)-1 )
3759 gs_counter = 1;
3760 else
3761 gs_counter++;
3762 }
3763
3764 for ( size_t n = 0; n < m_n; n++ )
3765 {
3766 if ( TestDestroy() )
3767 {
3768 m_cancelled = true;
3769
3770 break;
3771 }
3772
3773 wxPutchar(m_ch);
3774 fflush(stdout);
3775
3776 wxThread::Sleep(100);
3777 }
3778
3779 return 0;
3780 }
3781
3782 void MyDetachedThread::OnExit()
3783 {
3784 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3785
3786 wxCriticalSectionLocker lock(gs_critsect);
3787 if ( !--gs_counter && !m_cancelled )
3788 gs_cond.Post();
3789 }
3790
3791 static void TestDetachedThreads()
3792 {
3793 wxPuts(_T("\n*** Testing detached threads ***"));
3794
3795 static const size_t nThreads = 3;
3796 MyDetachedThread *threads[nThreads];
3797 size_t n;
3798 for ( n = 0; n < nThreads; n++ )
3799 {
3800 threads[n] = new MyDetachedThread(10, 'A' + n);
3801 }
3802
3803 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
3804 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
3805
3806 for ( n = 0; n < nThreads; n++ )
3807 {
3808 threads[n]->Run();
3809 }
3810
3811 // wait until all threads terminate
3812 gs_cond.Wait();
3813
3814 wxPuts(wxEmptyString);
3815 }
3816
3817 static void TestJoinableThreads()
3818 {
3819 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3820
3821 // calc 10! in the background
3822 MyJoinableThread thread(10);
3823 thread.Run();
3824
3825 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3826 (unsigned long)thread.Wait());
3827 }
3828
3829 static void TestThreadSuspend()
3830 {
3831 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3832
3833 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
3834
3835 thread->Run();
3836
3837 // this is for this demo only, in a real life program we'd use another
3838 // condition variable which would be signaled from wxThread::Entry() to
3839 // tell us that the thread really started running - but here just wait a
3840 // bit and hope that it will be enough (the problem is, of course, that
3841 // the thread might still not run when we call Pause() which will result
3842 // in an error)
3843 wxThread::Sleep(300);
3844
3845 for ( size_t n = 0; n < 3; n++ )
3846 {
3847 thread->Pause();
3848
3849 wxPuts(_T("\nThread suspended"));
3850 if ( n > 0 )
3851 {
3852 // don't sleep but resume immediately the first time
3853 wxThread::Sleep(300);
3854 }
3855 wxPuts(_T("Going to resume the thread"));
3856
3857 thread->Resume();
3858 }
3859
3860 wxPuts(_T("Waiting until it terminates now"));
3861
3862 // wait until the thread terminates
3863 gs_cond.Wait();
3864
3865 wxPuts(wxEmptyString);
3866 }
3867
3868 static void TestThreadDelete()
3869 {
3870 // As above, using Sleep() is only for testing here - we must use some
3871 // synchronisation object instead to ensure that the thread is still
3872 // running when we delete it - deleting a detached thread which already
3873 // terminated will lead to a crash!
3874
3875 wxPuts(_T("\n*** Testing thread delete function ***"));
3876
3877 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
3878
3879 thread0->Delete();
3880
3881 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3882
3883 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
3884
3885 thread1->Run();
3886
3887 wxThread::Sleep(300);
3888
3889 thread1->Delete();
3890
3891 wxPuts(_T("\nDeleted a running thread."));
3892
3893 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
3894
3895 thread2->Run();
3896
3897 wxThread::Sleep(300);
3898
3899 thread2->Pause();
3900
3901 thread2->Delete();
3902
3903 wxPuts(_T("\nDeleted a sleeping thread."));
3904
3905 MyJoinableThread thread3(20);
3906 thread3.Run();
3907
3908 thread3.Delete();
3909
3910 wxPuts(_T("\nDeleted a joinable thread."));
3911
3912 MyJoinableThread thread4(2);
3913 thread4.Run();
3914
3915 wxThread::Sleep(300);
3916
3917 thread4.Delete();
3918
3919 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3920
3921 wxPuts(wxEmptyString);
3922 }
3923
3924 class MyWaitingThread : public wxThread
3925 {
3926 public:
3927 MyWaitingThread( wxMutex *mutex, wxCondition *condition )
3928 {
3929 m_mutex = mutex;
3930 m_condition = condition;
3931
3932 Create();
3933 }
3934
3935 virtual ExitCode Entry()
3936 {
3937 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3938 fflush(stdout);
3939
3940 gs_cond.Post();
3941
3942 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3943 fflush(stdout);
3944
3945 m_mutex->Lock();
3946 m_condition->Wait();
3947 m_mutex->Unlock();
3948
3949 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3950 fflush(stdout);
3951
3952 return 0;
3953 }
3954
3955 private:
3956 wxMutex *m_mutex;
3957 wxCondition *m_condition;
3958 };
3959
3960 static void TestThreadConditions()
3961 {
3962 wxMutex mutex;
3963 wxCondition condition(mutex);
3964
3965 // otherwise its difficult to understand which log messages pertain to
3966 // which condition
3967 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3968 // condition.GetId(), gs_cond.GetId());
3969
3970 // create and launch threads
3971 MyWaitingThread *threads[10];
3972
3973 size_t n;
3974 for ( n = 0; n < WXSIZEOF(threads); n++ )
3975 {
3976 threads[n] = new MyWaitingThread( &mutex, &condition );
3977 }
3978
3979 for ( n = 0; n < WXSIZEOF(threads); n++ )
3980 {
3981 threads[n]->Run();
3982 }
3983
3984 // wait until all threads run
3985 wxPuts(_T("Main thread is waiting for the other threads to start"));
3986 fflush(stdout);
3987
3988 size_t nRunning = 0;
3989 while ( nRunning < WXSIZEOF(threads) )
3990 {
3991 gs_cond.Wait();
3992
3993 nRunning++;
3994
3995 wxPrintf(_T("Main thread: %u already running\n"), nRunning);
3996 fflush(stdout);
3997 }
3998
3999 wxPuts(_T("Main thread: all threads started up."));
4000 fflush(stdout);
4001
4002 wxThread::Sleep(500);
4003
4004 #if 1
4005 // now wake one of them up
4006 wxPrintf(_T("Main thread: about to signal the condition.\n"));
4007 fflush(stdout);
4008 condition.Signal();
4009 #endif
4010
4011 wxThread::Sleep(200);
4012
4013 // wake all the (remaining) threads up, so that they can exit
4014 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
4015 fflush(stdout);
4016 condition.Broadcast();
4017
4018 // give them time to terminate (dirty!)
4019 wxThread::Sleep(500);
4020 }
4021
4022 #include "wx/utils.h"
4023
4024 class MyExecThread : public wxThread
4025 {
4026 public:
4027 MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
4028 m_command(command)
4029 {
4030 Create();
4031 }
4032
4033 virtual ExitCode Entry()
4034 {
4035 return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
4036 }
4037
4038 private:
4039 wxString m_command;
4040 };
4041
4042 static void TestThreadExec()
4043 {
4044 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4045
4046 MyExecThread thread(_T("true"));
4047 thread.Run();
4048
4049 wxPrintf(_T("Main program exit code: %ld.\n"),
4050 wxExecute(_T("false"), wxEXEC_SYNC));
4051
4052 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
4053 }
4054
4055 // semaphore tests
4056 #include "wx/datetime.h"
4057
4058 class MySemaphoreThread : public wxThread
4059 {
4060 public:
4061 MySemaphoreThread(int i, wxSemaphore *sem)
4062 : wxThread(wxTHREAD_JOINABLE),
4063 m_sem(sem),
4064 m_i(i)
4065 {
4066 Create();
4067 }
4068
4069 virtual ExitCode Entry()
4070 {
4071 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4072 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4073
4074 m_sem->Wait();
4075
4076 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4077 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4078
4079 Sleep(1000);
4080
4081 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4082 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4083
4084 m_sem->Post();
4085
4086 return 0;
4087 }
4088
4089 private:
4090 wxSemaphore *m_sem;
4091 int m_i;
4092 };
4093
4094 WX_DEFINE_ARRAY_PTR(wxThread *, ArrayThreads);
4095
4096 static void TestSemaphore()
4097 {
4098 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4099
4100 static const int SEM_LIMIT = 3;
4101
4102 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
4103 ArrayThreads threads;
4104
4105 for ( int i = 0; i < 3*SEM_LIMIT; i++ )
4106 {
4107 threads.Add(new MySemaphoreThread(i, &sem));
4108 threads.Last()->Run();
4109 }
4110
4111 for ( size_t n = 0; n < threads.GetCount(); n++ )
4112 {
4113 threads[n]->Wait();
4114 delete threads[n];
4115 }
4116 }
4117
4118 #endif // TEST_THREADS
4119
4120 // ----------------------------------------------------------------------------
4121 // entry point
4122 // ----------------------------------------------------------------------------
4123
4124 #ifdef TEST_SNGLINST
4125 #include "wx/snglinst.h"
4126 #endif // TEST_SNGLINST
4127
4128 int main(int argc, char **argv)
4129 {
4130 #if wxUSE_UNICODE
4131 wxChar **wxArgv = new wxChar *[argc + 1];
4132
4133 {
4134 int n;
4135
4136 for (n = 0; n < argc; n++ )
4137 {
4138 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
4139 wxArgv[n] = wxStrdup(warg);
4140 }
4141
4142 wxArgv[n] = NULL;
4143 }
4144 #else // !wxUSE_UNICODE
4145 #define wxArgv argv
4146 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4147
4148 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
4149
4150 wxInitializer initializer;
4151 if ( !initializer )
4152 {
4153 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
4154
4155 return -1;
4156 }
4157
4158 #ifdef TEST_SNGLINST
4159 wxSingleInstanceChecker checker;
4160 if ( checker.Create(_T(".wxconsole.lock")) )
4161 {
4162 if ( checker.IsAnotherRunning() )
4163 {
4164 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4165
4166 return 1;
4167 }
4168
4169 // wait some time to give time to launch another instance
4170 wxPrintf(_T("Press \"Enter\" to continue..."));
4171 wxFgetc(stdin);
4172 }
4173 else // failed to create
4174 {
4175 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4176 }
4177 #endif // TEST_SNGLINST
4178
4179 #ifdef TEST_CMDLINE
4180 TestCmdLineConvert();
4181
4182 #if wxUSE_CMDLINE_PARSER
4183 static const wxCmdLineEntryDesc cmdLineDesc[] =
4184 {
4185 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"),
4186 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
4187 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
4188 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
4189
4190 { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file") },
4191 { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("input dir") },
4192 { wxCMD_LINE_OPTION, _T("s"), _T("size"), _T("output block size"),
4193 wxCMD_LINE_VAL_NUMBER },
4194 { wxCMD_LINE_OPTION, _T("d"), _T("date"), _T("output file date"),
4195 wxCMD_LINE_VAL_DATE },
4196
4197 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"),
4198 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
4199
4200 { wxCMD_LINE_NONE }
4201 };
4202
4203 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
4204
4205 parser.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4206 wxCMD_LINE_VAL_STRING,
4207 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
4208
4209 switch ( parser.Parse() )
4210 {
4211 case -1:
4212 wxLogMessage(_T("Help was given, terminating."));
4213 break;
4214
4215 case 0:
4216 ShowCmdLine(parser);
4217 break;
4218
4219 default:
4220 wxLogMessage(_T("Syntax error detected, aborting."));
4221 break;
4222 }
4223 #endif // wxUSE_CMDLINE_PARSER
4224
4225 #endif // TEST_CMDLINE
4226
4227 #ifdef TEST_DIR
4228 #if TEST_ALL
4229 TestDirExists();
4230 TestDirEnum();
4231 #endif
4232 TestDirTraverse();
4233 #endif // TEST_DIR
4234
4235 #ifdef TEST_DYNLIB
4236 TestDllLoad();
4237 TestDllListLoaded();
4238 #endif // TEST_DYNLIB
4239
4240 #ifdef TEST_ENVIRON
4241 TestEnvironment();
4242 #endif // TEST_ENVIRON
4243
4244 #ifdef TEST_EXECUTE
4245 TestExecute();
4246 #endif // TEST_EXECUTE
4247
4248 #ifdef TEST_FILECONF
4249 TestFileConfRead();
4250 #endif // TEST_FILECONF
4251
4252 #ifdef TEST_LOCALE
4253 TestDefaultLang();
4254 #endif // TEST_LOCALE
4255
4256 #ifdef TEST_LOG
4257 wxPuts(_T("*** Testing wxLog ***"));
4258
4259 wxString s;
4260 for ( size_t n = 0; n < 8000; n++ )
4261 {
4262 s << (wxChar)(_T('A') + (n % 26));
4263 }
4264
4265 wxLogWarning(_T("The length of the string is %lu"),
4266 (unsigned long)s.length());
4267
4268 wxString msg;
4269 msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str());
4270
4271 // this one shouldn't be truncated
4272 wxPrintf(msg);
4273
4274 // but this one will because log functions use fixed size buffer
4275 // (note that it doesn't need '\n' at the end neither - will be added
4276 // by wxLog anyhow)
4277 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s.c_str());
4278 #endif // TEST_LOG
4279
4280 #ifdef TEST_FILE
4281 TestFileRead();
4282 TestTextFileRead();
4283 TestFileCopy();
4284 #endif // TEST_FILE
4285
4286 #ifdef TEST_FILENAME
4287 TestFileNameTemp();
4288 TestFileNameCwd();
4289 TestFileNameDirManip();
4290 TestFileNameComparison();
4291 TestFileNameOperations();
4292 #endif // TEST_FILENAME
4293
4294 #ifdef TEST_FILETIME
4295 TestFileGetTimes();
4296 #if 0
4297 TestFileSetTimes();
4298 #endif
4299 #endif // TEST_FILETIME
4300
4301 #ifdef TEST_FTP
4302 wxLog::AddTraceMask(FTP_TRACE_MASK);
4303 if ( TestFtpConnect() )
4304 {
4305 #if TEST_ALL
4306 TestFtpList();
4307 TestFtpDownload();
4308 TestFtpMisc();
4309 TestFtpFileSize();
4310 TestFtpUpload();
4311 #endif // TEST_ALL
4312
4313 #if TEST_INTERACTIVE
4314 TestFtpInteractive();
4315 #endif
4316 }
4317 //else: connecting to the FTP server failed
4318
4319 #if 0
4320 TestFtpWuFtpd();
4321 #endif
4322 #endif // TEST_FTP
4323
4324 #ifdef TEST_MIME
4325 wxLog::AddTraceMask(_T("mime"));
4326 #if TEST_ALL
4327 TestMimeEnum();
4328 TestMimeOverride();
4329 TestMimeAssociate();
4330 #endif
4331 TestMimeFilename();
4332 #endif // TEST_MIME
4333
4334 #ifdef TEST_INFO_FUNCTIONS
4335 #if TEST_ALL
4336 TestOsInfo();
4337 TestUserInfo();
4338
4339 #if TEST_INTERACTIVE
4340 TestDiskInfo();
4341 #endif
4342 #endif
4343 #endif // TEST_INFO_FUNCTIONS
4344
4345 #ifdef TEST_PATHLIST
4346 TestPathList();
4347 #endif // TEST_PATHLIST
4348
4349 #ifdef TEST_ODBC
4350 TestDbOpen();
4351 #endif // TEST_ODBC
4352
4353 #ifdef TEST_PRINTF
4354 TestPrintf();
4355 #endif // TEST_PRINTF
4356
4357 #ifdef TEST_REGCONF
4358 #if 0
4359 TestRegConfWrite();
4360 #endif
4361 TestRegConfRead();
4362 #endif // TEST_REGCONF
4363
4364 #if defined TEST_REGEX && TEST_INTERACTIVE
4365 TestRegExInteractive();
4366 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4367
4368 #ifdef TEST_REGISTRY
4369 TestRegistryRead();
4370 TestRegistryAssociation();
4371 #endif // TEST_REGISTRY
4372
4373 #ifdef TEST_SOCKETS
4374 TestSocketServer();
4375 TestSocketClient();
4376 #endif // TEST_SOCKETS
4377
4378 #ifdef TEST_STREAMS
4379 #if TEST_ALL
4380 TestFileStream();
4381 #endif
4382 TestMemoryStream();
4383 #endif // TEST_STREAMS
4384
4385 #ifdef TEST_TEXTSTREAM
4386 TestTextInputStream();
4387 #endif // TEST_TEXTSTREAM
4388
4389 #ifdef TEST_THREADS
4390 int nCPUs = wxThread::GetCPUCount();
4391 wxPrintf(_T("This system has %d CPUs\n"), nCPUs);
4392 if ( nCPUs != -1 )
4393 wxThread::SetConcurrency(nCPUs);
4394
4395 TestJoinableThreads();
4396
4397 #if TEST_ALL
4398 TestJoinableThreads();
4399 TestDetachedThreads();
4400 TestThreadSuspend();
4401 TestThreadDelete();
4402 TestThreadConditions();
4403 TestThreadExec();
4404 TestSemaphore();
4405 #endif
4406 #endif // TEST_THREADS
4407
4408 #ifdef TEST_TIMER
4409 TestStopWatch();
4410 #endif // TEST_TIMER
4411
4412 #ifdef TEST_DATETIME
4413 #if TEST_ALL
4414 TestTimeStatic();
4415 TestTimeRange();
4416 TestTimeZones();
4417 TestTimeDST();
4418 TestTimeHolidays();
4419 TestTimeSpanFormat();
4420 TestTimeMS();
4421
4422 TestTimeZoneBug();
4423 #endif
4424
4425 #if TEST_INTERACTIVE
4426 TestDateTimeInteractive();
4427 #endif
4428 #endif // TEST_DATETIME
4429
4430 #ifdef TEST_SCOPEGUARD
4431 TestScopeGuard();
4432 #endif
4433
4434 #ifdef TEST_STACKWALKER
4435 #if wxUSE_STACKWALKER
4436 TestStackWalk(argv[0]);
4437 #endif
4438 #endif // TEST_STACKWALKER
4439
4440 #ifdef TEST_STDPATHS
4441 TestStandardPaths();
4442 #endif
4443
4444 #ifdef TEST_USLEEP
4445 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4446 wxUsleep(3000);
4447 #endif // TEST_USLEEP
4448
4449 #ifdef TEST_VCARD
4450 TestVCardRead();
4451 TestVCardWrite();
4452 #endif // TEST_VCARD
4453
4454 #ifdef TEST_VOLUME
4455 TestFSVolume();
4456 #endif // TEST_VOLUME
4457
4458 #ifdef TEST_WCHAR
4459 TestUtf8();
4460 TestEncodingConverter();
4461 #endif // TEST_WCHAR
4462
4463 #ifdef TEST_ZIP
4464 TestZipStreamRead();
4465 TestZipFileSystem();
4466 #endif // TEST_ZIP
4467
4468 #if wxUSE_UNICODE
4469 {
4470 for ( int n = 0; n < argc; n++ )
4471 free(wxArgv[n]);
4472
4473 delete [] wxArgv;
4474 }
4475 #endif // wxUSE_UNICODE
4476
4477 wxUnusedVar(argc);
4478 wxUnusedVar(argv);
4479 return 0;
4480 }