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