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