1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
24 #include "wx/string.h"
29 // without this pragma, the stupid compiler precompiles #defines below so that
30 // changing them doesn't "take place" later!
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
40 A note about all these conditional compilation macros: this file is used
41 both as a test suite for various non-GUI wxWidgets classes and as a
42 scratchpad for quick tests. So there are two compilation modes: if you
43 define TEST_ALL all tests are run, otherwise you may enable the individual
44 tests individually in the "#else" branch below.
47 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
48 // test, define it to 1 to do all tests.
63 // #define TEST_FTP --FIXME! (RN)
64 #define TEST_INFO_FUNCTIONS
75 #define TEST_SCOPEGUARD
77 // #define TEST_SOCKETS --FIXME! (RN)
78 #define TEST_STACKWALKER
81 #define TEST_TEXTSTREAM
84 // #define TEST_VCARD -- don't enable this (VZ)
85 // #define TEST_VOLUME --FIXME! (RN)
92 // some tests are interactive, define this to run them
93 #ifdef TEST_INTERACTIVE
94 #undef TEST_INTERACTIVE
96 #define TEST_INTERACTIVE 1
98 #define TEST_INTERACTIVE 0
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 #if defined(TEST_SOCKETS)
111 // replace TABs with \t and CRs with \n
112 static wxString
MakePrintable(const wxChar
*s
)
115 (void)str
.Replace(_T("\t"), _T("\\t"));
116 (void)str
.Replace(_T("\n"), _T("\\n"));
117 (void)str
.Replace(_T("\r"), _T("\\r"));
122 #endif // MakePrintable() is used
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
130 #include "wx/cmdline.h"
131 #include "wx/datetime.h"
133 #if wxUSE_CMDLINE_PARSER
135 static void ShowCmdLine(const wxCmdLineParser
& parser
)
137 wxString s
= _T("Input files: ");
139 size_t count
= parser
.GetParamCount();
140 for ( size_t param
= 0; param
< count
; param
++ )
142 s
<< parser
.GetParam(param
) << ' ';
146 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
147 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
152 if ( parser
.Found(_T("o"), &strVal
) )
153 s
<< _T("Output file:\t") << strVal
<< '\n';
154 if ( parser
.Found(_T("i"), &strVal
) )
155 s
<< _T("Input dir:\t") << strVal
<< '\n';
156 if ( parser
.Found(_T("s"), &lVal
) )
157 s
<< _T("Size:\t") << lVal
<< '\n';
158 if ( parser
.Found(_T("d"), &dt
) )
159 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
160 if ( parser
.Found(_T("project_name"), &strVal
) )
161 s
<< _T("Project:\t") << strVal
<< '\n';
166 #endif // wxUSE_CMDLINE_PARSER
168 static void TestCmdLineConvert()
170 static const wxChar
*cmdlines
[] =
173 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
174 _T("literal \\\" and \"\""),
177 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
179 const wxChar
*cmdline
= cmdlines
[n
];
180 wxPrintf(_T("Parsing: %s\n"), cmdline
);
181 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
183 size_t count
= args
.GetCount();
184 wxPrintf(_T("\targc = %u\n"), count
);
185 for ( size_t arg
= 0; arg
< count
; arg
++ )
187 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
192 #endif // TEST_CMDLINE
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
203 static const wxChar
*ROOTDIR
= _T("/");
204 static const wxChar
*TESTDIR
= _T("/usr/local/share");
205 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
206 static const wxChar
*ROOTDIR
= _T("c:\\");
207 static const wxChar
*TESTDIR
= _T("d:\\");
209 #error "don't know where the root directory is"
212 static void TestDirEnumHelper(wxDir
& dir
,
213 int flags
= wxDIR_DEFAULT
,
214 const wxString
& filespec
= wxEmptyString
)
218 if ( !dir
.IsOpened() )
221 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
224 wxPrintf(_T("\t%s\n"), filename
.c_str());
226 cont
= dir
.GetNext(&filename
);
229 wxPuts(wxEmptyString
);
234 static void TestDirEnum()
236 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
238 wxString cwd
= wxGetCwd();
239 if ( !wxDir::Exists(cwd
) )
241 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
246 if ( !dir
.IsOpened() )
248 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
252 wxPuts(_T("Enumerating everything in current directory:"));
253 TestDirEnumHelper(dir
);
255 wxPuts(_T("Enumerating really everything in current directory:"));
256 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
258 wxPuts(_T("Enumerating object files in current directory:"));
259 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
261 wxPuts(_T("Enumerating directories in current directory:"));
262 TestDirEnumHelper(dir
, wxDIR_DIRS
);
264 wxPuts(_T("Enumerating files in current directory:"));
265 TestDirEnumHelper(dir
, wxDIR_FILES
);
267 wxPuts(_T("Enumerating files including hidden in current directory:"));
268 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
272 wxPuts(_T("Enumerating everything in root directory:"));
273 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
275 wxPuts(_T("Enumerating directories in root directory:"));
276 TestDirEnumHelper(dir
, wxDIR_DIRS
);
278 wxPuts(_T("Enumerating files in root directory:"));
279 TestDirEnumHelper(dir
, wxDIR_FILES
);
281 wxPuts(_T("Enumerating files including hidden in root directory:"));
282 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
284 wxPuts(_T("Enumerating files in non existing directory:"));
285 wxDir
dirNo(_T("nosuchdir"));
286 TestDirEnumHelper(dirNo
);
291 class DirPrintTraverser
: public wxDirTraverser
294 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
296 return wxDIR_CONTINUE
;
299 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
301 wxString path
, name
, ext
;
302 wxSplitPath(dirname
, &path
, &name
, &ext
);
305 name
<< _T('.') << ext
;
308 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
310 if ( wxIsPathSeparator(*p
) )
314 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
316 return wxDIR_CONTINUE
;
320 static void TestDirTraverse()
322 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
326 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
327 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
330 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
331 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
334 // enum again with custom traverser
335 wxPuts(_T("Now enumerating directories:"));
337 DirPrintTraverser traverser
;
338 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
343 static void TestDirExists()
345 wxPuts(_T("*** Testing wxDir::Exists() ***"));
347 static const wxChar
*dirnames
[] =
350 #if defined(__WXMSW__)
353 _T("\\\\share\\file"),
357 _T("c:\\autoexec.bat"),
358 #elif defined(__UNIX__)
367 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
369 wxPrintf(_T("%-40s: %s\n"),
371 wxDir::Exists(dirnames
[n
]) ? _T("exists")
372 : _T("doesn't exist"));
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
386 #include "wx/dynlib.h"
388 static void TestDllLoad()
390 #if defined(__WXMSW__)
391 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
392 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
393 #elif defined(__UNIX__)
394 // weird: using just libc.so does *not* work!
395 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
396 static const wxChar
*FUNC_NAME
= _T("strlen");
398 #error "don't know how to test wxDllLoader on this platform"
401 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
403 wxDynamicLibrary
lib(LIB_NAME
);
404 if ( !lib
.IsLoaded() )
406 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
410 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
411 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
414 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
415 FUNC_NAME
, LIB_NAME
);
419 wxPrintf(_T("Calling %s dynamically loaded from %s "),
420 FUNC_NAME
, LIB_NAME
);
422 if ( pfnStrlen("foo") != 3 )
424 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
428 wxPuts(_T("... ok"));
433 static const wxChar
*FUNC_NAME_AW
= _T("lstrlen");
435 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
437 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
438 if ( !pfnStrlenAorW
)
440 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
441 FUNC_NAME_AW
, LIB_NAME
);
445 if ( pfnStrlenAorW(_T("foobar")) != 6 )
447 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
454 #if defined(__WXMSW__) || defined(__UNIX__)
456 static void TestDllListLoaded()
458 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
460 puts("\nLoaded modules:");
461 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
462 const size_t count
= dlls
.GetCount();
463 for ( size_t n
= 0; n
< count
; ++n
)
465 const wxDynamicLibraryDetails
& details
= dlls
[n
];
466 printf("%-45s", details
.GetPath().mb_str());
470 if ( details
.GetAddress(&addr
, &len
) )
472 printf(" %08lx:%08lx",
473 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
476 printf(" %s\n", details
.GetVersion().mb_str());
482 #endif // TEST_DYNLIB
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
490 #include "wx/utils.h"
492 static wxString
MyGetEnv(const wxString
& var
)
495 if ( !wxGetEnv(var
, &val
) )
498 val
= wxString(_T('\'')) + val
+ _T('\'');
503 static void TestEnvironment()
505 const wxChar
*var
= _T("wxTestVar");
507 wxPuts(_T("*** testing environment access functions ***"));
509 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
510 wxSetEnv(var
, _T("value for wxTestVar"));
511 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
512 wxSetEnv(var
, _T("another value"));
513 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
515 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
516 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
519 #endif // TEST_ENVIRON
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
527 #include "wx/utils.h"
529 static void TestExecute()
531 wxPuts(_T("*** testing wxExecute ***"));
534 #define COMMAND "cat -n ../../Makefile" // "echo hi"
535 #define SHELL_COMMAND "echo hi from shell"
536 #define REDIRECT_COMMAND COMMAND // "date"
537 #elif defined(__WXMSW__)
538 #define COMMAND "command.com /c echo hi"
539 #define SHELL_COMMAND "echo hi"
540 #define REDIRECT_COMMAND COMMAND
542 #error "no command to exec"
545 wxPrintf(_T("Testing wxShell: "));
547 if ( wxShell(_T(SHELL_COMMAND
)) )
550 wxPuts(_T("ERROR."));
552 wxPrintf(_T("Testing wxExecute: "));
554 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
557 wxPuts(_T("ERROR."));
559 #if 0 // no, it doesn't work (yet?)
560 wxPrintf(_T("Testing async wxExecute: "));
562 if ( wxExecute(COMMAND
) != 0 )
563 wxPuts(_T("Ok (command launched)."));
565 wxPuts(_T("ERROR."));
568 wxPrintf(_T("Testing wxExecute with redirection:\n"));
569 wxArrayString output
;
570 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
572 wxPuts(_T("ERROR."));
576 size_t count
= output
.GetCount();
577 for ( size_t n
= 0; n
< count
; n
++ )
579 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
586 #endif // TEST_EXECUTE
588 // ----------------------------------------------------------------------------
590 // ----------------------------------------------------------------------------
595 #include "wx/ffile.h"
596 #include "wx/textfile.h"
598 static void TestFileRead()
600 wxPuts(_T("*** wxFile read test ***"));
602 wxFile
file(_T("testdata.fc"));
603 if ( file
.IsOpened() )
605 wxPrintf(_T("File length: %lu\n"), file
.Length());
607 wxPuts(_T("File dump:\n----------"));
609 static const size_t len
= 1024;
613 size_t nRead
= file
.Read(buf
, len
);
614 if ( nRead
== (size_t)wxInvalidOffset
)
616 wxPrintf(_T("Failed to read the file."));
620 fwrite(buf
, nRead
, 1, stdout
);
626 wxPuts(_T("----------"));
630 wxPrintf(_T("ERROR: can't open test file.\n"));
633 wxPuts(wxEmptyString
);
636 static void TestTextFileRead()
638 wxPuts(_T("*** wxTextFile read test ***"));
640 wxTextFile
file(_T("testdata.fc"));
643 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
644 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
648 wxPuts(_T("\nDumping the entire file:"));
649 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
651 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
653 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
655 wxPuts(_T("\nAnd now backwards:"));
656 for ( s
= file
.GetLastLine();
657 file
.GetCurrentLine() != 0;
658 s
= file
.GetPrevLine() )
660 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
662 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
666 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
669 wxPuts(wxEmptyString
);
672 static void TestFileCopy()
674 wxPuts(_T("*** Testing wxCopyFile ***"));
676 static const wxChar
*filename1
= _T("testdata.fc");
677 static const wxChar
*filename2
= _T("test2");
678 if ( !wxCopyFile(filename1
, filename2
) )
680 wxPuts(_T("ERROR: failed to copy file"));
684 wxFFile
f1(filename1
, _T("rb")),
685 f2(filename2
, _T("rb"));
687 if ( !f1
.IsOpened() || !f2
.IsOpened() )
689 wxPuts(_T("ERROR: failed to open file(s)"));
694 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
696 wxPuts(_T("ERROR: failed to read file(s)"));
700 if ( (s1
.length() != s2
.length()) ||
701 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
703 wxPuts(_T("ERROR: copy error!"));
707 wxPuts(_T("File was copied ok."));
713 if ( !wxRemoveFile(filename2
) )
715 wxPuts(_T("ERROR: failed to remove the file"));
718 wxPuts(wxEmptyString
);
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
729 #include "wx/confbase.h"
730 #include "wx/fileconf.h"
732 static const struct FileConfTestData
734 const wxChar
*name
; // value name
735 const wxChar
*value
; // the value from the file
738 { _T("value1"), _T("one") },
739 { _T("value2"), _T("two") },
740 { _T("novalue"), _T("default") },
743 static void TestFileConfRead()
745 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
747 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
748 _T("testdata.fc"), wxEmptyString
,
749 wxCONFIG_USE_RELATIVE_PATH
);
751 // test simple reading
752 wxPuts(_T("\nReading config file:"));
753 wxString
defValue(_T("default")), value
;
754 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
756 const FileConfTestData
& data
= fcTestData
[n
];
757 value
= fileconf
.Read(data
.name
, defValue
);
758 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
759 if ( value
== data
.value
)
765 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
769 // test enumerating the entries
770 wxPuts(_T("\nEnumerating all root entries:"));
773 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
776 wxPrintf(_T("\t%s = %s\n"),
778 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
780 cont
= fileconf
.GetNextEntry(name
, dummy
);
783 static const wxChar
*testEntry
= _T("TestEntry");
784 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
785 fileconf
.Write(testEntry
, _T("A value"));
786 fileconf
.DeleteEntry(testEntry
);
787 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
790 #endif // TEST_FILECONF
792 // ----------------------------------------------------------------------------
794 // ----------------------------------------------------------------------------
798 #include "wx/filename.h"
801 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
805 wxString full
= fn
.GetFullPath();
807 wxString vol
, path
, name
, ext
;
808 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
810 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
811 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
813 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
814 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
815 path
.c_str(), name
.c_str(), ext
.c_str());
817 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
818 wxPrintf(_T("with volume: \t'%s'\n"),
819 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
820 wxPrintf(_T("with separator:\t'%s'\n"),
821 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
822 wxPrintf(_T("with both: \t'%s'\n"),
823 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
825 wxPuts(_T("The directories in the path are:"));
826 wxArrayString dirs
= fn
.GetDirs();
827 size_t count
= dirs
.GetCount();
828 for ( size_t n
= 0; n
< count
; n
++ )
830 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
835 static void TestFileNameTemp()
837 wxPuts(_T("*** testing wxFileName temp file creation ***"));
839 static const wxChar
*tmpprefixes
[] =
847 _T("/tmp/foo/bar"), // this one must be an error
851 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
853 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
856 // "error" is not in upper case because it may be ok
857 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
861 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
862 tmpprefixes
[n
], path
.c_str());
864 if ( !wxRemoveFile(path
) )
866 wxLogWarning(_T("Failed to remove temp file '%s'"),
873 static void TestFileNameDirManip()
875 // TODO: test AppendDir(), RemoveDir(), ...
878 static void TestFileNameComparison()
883 static void TestFileNameOperations()
888 static void TestFileNameCwd()
893 #endif // TEST_FILENAME
895 // ----------------------------------------------------------------------------
896 // wxFileName time functions
897 // ----------------------------------------------------------------------------
901 #include "wx/filename.h"
902 #include "wx/datetime.h"
904 static void TestFileGetTimes()
906 wxFileName
fn(_T("testdata.fc"));
908 wxDateTime dtAccess
, dtMod
, dtCreate
;
909 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
911 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
915 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
917 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
918 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
919 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
920 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
925 static void TestFileSetTimes()
927 wxFileName
fn(_T("testdata.fc"));
931 wxPrintf(_T("ERROR: Touch() failed.\n"));
936 #endif // TEST_FILETIME
938 // ----------------------------------------------------------------------------
940 // ----------------------------------------------------------------------------
945 #include "wx/utils.h" // for wxSetEnv
947 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
949 // find the name of the language from its value
950 static const wxChar
*GetLangName(int lang
)
952 static const wxChar
*languageNames
[] =
962 _T("ARABIC_ALGERIA"),
963 _T("ARABIC_BAHRAIN"),
968 _T("ARABIC_LEBANON"),
970 _T("ARABIC_MOROCCO"),
973 _T("ARABIC_SAUDI_ARABIA"),
976 _T("ARABIC_TUNISIA"),
983 _T("AZERI_CYRILLIC"),
998 _T("CHINESE_SIMPLIFIED"),
999 _T("CHINESE_TRADITIONAL"),
1000 _T("CHINESE_HONGKONG"),
1001 _T("CHINESE_MACAU"),
1002 _T("CHINESE_SINGAPORE"),
1003 _T("CHINESE_TAIWAN"),
1009 _T("DUTCH_BELGIAN"),
1013 _T("ENGLISH_AUSTRALIA"),
1014 _T("ENGLISH_BELIZE"),
1015 _T("ENGLISH_BOTSWANA"),
1016 _T("ENGLISH_CANADA"),
1017 _T("ENGLISH_CARIBBEAN"),
1018 _T("ENGLISH_DENMARK"),
1020 _T("ENGLISH_JAMAICA"),
1021 _T("ENGLISH_NEW_ZEALAND"),
1022 _T("ENGLISH_PHILIPPINES"),
1023 _T("ENGLISH_SOUTH_AFRICA"),
1024 _T("ENGLISH_TRINIDAD"),
1025 _T("ENGLISH_ZIMBABWE"),
1033 _T("FRENCH_BELGIAN"),
1034 _T("FRENCH_CANADIAN"),
1035 _T("FRENCH_LUXEMBOURG"),
1036 _T("FRENCH_MONACO"),
1042 _T("GERMAN_AUSTRIAN"),
1043 _T("GERMAN_BELGIUM"),
1044 _T("GERMAN_LIECHTENSTEIN"),
1045 _T("GERMAN_LUXEMBOURG"),
1063 _T("ITALIAN_SWISS"),
1068 _T("KASHMIRI_INDIA"),
1086 _T("MALAY_BRUNEI_DARUSSALAM"),
1087 _T("MALAY_MALAYSIA"),
1097 _T("NORWEGIAN_BOKMAL"),
1098 _T("NORWEGIAN_NYNORSK"),
1105 _T("PORTUGUESE_BRAZILIAN"),
1108 _T("RHAETO_ROMANCE"),
1111 _T("RUSSIAN_UKRAINE"),
1117 _T("SERBIAN_CYRILLIC"),
1118 _T("SERBIAN_LATIN"),
1119 _T("SERBO_CROATIAN"),
1130 _T("SPANISH_ARGENTINA"),
1131 _T("SPANISH_BOLIVIA"),
1132 _T("SPANISH_CHILE"),
1133 _T("SPANISH_COLOMBIA"),
1134 _T("SPANISH_COSTA_RICA"),
1135 _T("SPANISH_DOMINICAN_REPUBLIC"),
1136 _T("SPANISH_ECUADOR"),
1137 _T("SPANISH_EL_SALVADOR"),
1138 _T("SPANISH_GUATEMALA"),
1139 _T("SPANISH_HONDURAS"),
1140 _T("SPANISH_MEXICAN"),
1141 _T("SPANISH_MODERN"),
1142 _T("SPANISH_NICARAGUA"),
1143 _T("SPANISH_PANAMA"),
1144 _T("SPANISH_PARAGUAY"),
1146 _T("SPANISH_PUERTO_RICO"),
1147 _T("SPANISH_URUGUAY"),
1149 _T("SPANISH_VENEZUELA"),
1153 _T("SWEDISH_FINLAND"),
1171 _T("URDU_PAKISTAN"),
1173 _T("UZBEK_CYRILLIC"),
1186 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1187 return languageNames
[lang
];
1189 return _T("INVALID");
1192 static void TestDefaultLang()
1194 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1196 static const wxChar
*langStrings
[] =
1198 NULL
, // system default
1205 _T("de_DE.iso88591"),
1207 _T("?"), // invalid lang spec
1208 _T("klingonese"), // I bet on some systems it does exist...
1211 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1212 wxLocale::GetSystemEncodingName().c_str(),
1213 wxLocale::GetSystemEncoding());
1215 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1217 const wxChar
*langStr
= langStrings
[n
];
1220 // FIXME: this doesn't do anything at all under Windows, we need
1221 // to create a new wxLocale!
1222 wxSetEnv(_T("LC_ALL"), langStr
);
1225 int lang
= gs_localeDefault
.GetSystemLanguage();
1226 wxPrintf(_T("Locale for '%s' is %s.\n"),
1227 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1231 #endif // TEST_LOCALE
1233 // ----------------------------------------------------------------------------
1235 // ----------------------------------------------------------------------------
1239 #include "wx/mimetype.h"
1241 static void TestMimeEnum()
1243 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1245 wxArrayString mimetypes
;
1247 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1249 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1254 for ( size_t n
= 0; n
< count
; n
++ )
1256 wxFileType
*filetype
=
1257 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1260 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1261 mimetypes
[n
].c_str());
1265 filetype
->GetDescription(&desc
);
1266 filetype
->GetExtensions(exts
);
1268 filetype
->GetIcon(NULL
);
1271 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1274 extsAll
<< _T(", ");
1278 wxPrintf(_T("\t%s: %s (%s)\n"),
1279 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1282 wxPuts(wxEmptyString
);
1285 static void TestMimeOverride()
1287 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1289 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1290 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1292 if ( wxFile::Exists(mailcap
) )
1293 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1295 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1297 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1300 if ( wxFile::Exists(mimetypes
) )
1301 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1303 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1305 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1308 wxPuts(wxEmptyString
);
1311 static void TestMimeFilename()
1313 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1315 static const wxChar
*filenames
[] =
1323 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1325 const wxString fname
= filenames
[n
];
1326 wxString ext
= fname
.AfterLast(_T('.'));
1327 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1330 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1335 if ( !ft
->GetDescription(&desc
) )
1336 desc
= _T("<no description>");
1339 if ( !ft
->GetOpenCommand(&cmd
,
1340 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1341 cmd
= _T("<no command available>");
1343 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1345 wxPrintf(_T("To open %s (%s) do %s.\n"),
1346 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1352 wxPuts(wxEmptyString
);
1355 static void TestMimeAssociate()
1357 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1359 wxFileTypeInfo
ftInfo(
1360 _T("application/x-xyz"),
1361 _T("xyzview '%s'"), // open cmd
1362 _T(""), // print cmd
1363 _T("XYZ File"), // description
1364 _T(".xyz"), // extensions
1365 NULL
// end of extensions
1367 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1369 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1372 wxPuts(_T("ERROR: failed to create association!"));
1376 // TODO: read it back
1380 wxPuts(wxEmptyString
);
1385 // ----------------------------------------------------------------------------
1386 // module dependencies feature
1387 // ----------------------------------------------------------------------------
1391 #include "wx/module.h"
1393 class wxTestModule
: public wxModule
1396 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1397 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1400 class wxTestModuleA
: public wxTestModule
1405 DECLARE_DYNAMIC_CLASS(wxTestModuleA
)
1408 class wxTestModuleB
: public wxTestModule
1413 DECLARE_DYNAMIC_CLASS(wxTestModuleB
)
1416 class wxTestModuleC
: public wxTestModule
1421 DECLARE_DYNAMIC_CLASS(wxTestModuleC
)
1424 class wxTestModuleD
: public wxTestModule
1429 DECLARE_DYNAMIC_CLASS(wxTestModuleD
)
1432 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC
, wxModule
)
1433 wxTestModuleC::wxTestModuleC()
1435 AddDependency(CLASSINFO(wxTestModuleD
));
1438 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA
, wxModule
)
1439 wxTestModuleA::wxTestModuleA()
1441 AddDependency(CLASSINFO(wxTestModuleB
));
1442 AddDependency(CLASSINFO(wxTestModuleD
));
1445 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD
, wxModule
)
1446 wxTestModuleD::wxTestModuleD()
1450 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB
, wxModule
)
1451 wxTestModuleB::wxTestModuleB()
1453 AddDependency(CLASSINFO(wxTestModuleD
));
1454 AddDependency(CLASSINFO(wxTestModuleC
));
1457 #endif // TEST_MODULE
1459 // ----------------------------------------------------------------------------
1460 // misc information functions
1461 // ----------------------------------------------------------------------------
1463 #ifdef TEST_INFO_FUNCTIONS
1465 #include "wx/utils.h"
1467 static void TestDiskInfo()
1469 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1473 wxChar pathname
[128];
1474 wxPrintf(_T("\nEnter a directory name: "));
1475 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1478 // kill the last '\n'
1479 pathname
[wxStrlen(pathname
) - 1] = 0;
1481 wxLongLong total
, free
;
1482 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1484 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1488 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1489 (total
/ 1024).ToString().c_str(),
1490 (free
/ 1024).ToString().c_str(),
1496 static void TestOsInfo()
1498 wxPuts(_T("*** Testing OS info functions ***\n"));
1501 wxGetOsVersion(&major
, &minor
);
1502 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1503 wxGetOsDescription().c_str(), major
, minor
);
1505 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
1507 wxPrintf(_T("Host name is %s (%s).\n"),
1508 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1510 wxPuts(wxEmptyString
);
1513 static void TestUserInfo()
1515 wxPuts(_T("*** Testing user info functions ***\n"));
1517 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1518 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1519 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1520 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1522 wxPuts(wxEmptyString
);
1525 #endif // TEST_INFO_FUNCTIONS
1527 // ----------------------------------------------------------------------------
1529 // ----------------------------------------------------------------------------
1531 #ifdef TEST_PATHLIST
1534 #define CMD_IN_PATH _T("ls")
1536 #define CMD_IN_PATH _T("command.com")
1539 static void TestPathList()
1541 wxPuts(_T("*** Testing wxPathList ***\n"));
1543 wxPathList pathlist
;
1544 pathlist
.AddEnvList(_T("PATH"));
1545 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1548 wxPrintf(_T("ERROR: command not found in the path.\n"));
1552 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1556 #endif // TEST_PATHLIST
1558 // ----------------------------------------------------------------------------
1559 // regular expressions
1560 // ----------------------------------------------------------------------------
1564 #include "wx/regex.h"
1566 static void TestRegExInteractive()
1568 wxPuts(_T("*** Testing RE interactively ***"));
1572 wxChar pattern
[128];
1573 wxPrintf(_T("\nEnter a pattern: "));
1574 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1577 // kill the last '\n'
1578 pattern
[wxStrlen(pattern
) - 1] = 0;
1581 if ( !re
.Compile(pattern
) )
1589 wxPrintf(_T("Enter text to match: "));
1590 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1593 // kill the last '\n'
1594 text
[wxStrlen(text
) - 1] = 0;
1596 if ( !re
.Matches(text
) )
1598 wxPrintf(_T("No match.\n"));
1602 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1605 for ( size_t n
= 1; ; n
++ )
1607 if ( !re
.GetMatch(&start
, &len
, n
) )
1612 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1613 n
, wxString(text
+ start
, len
).c_str());
1620 #endif // TEST_REGEX
1622 // ----------------------------------------------------------------------------
1624 // ----------------------------------------------------------------------------
1634 static void TestDbOpen()
1642 // ----------------------------------------------------------------------------
1644 // ----------------------------------------------------------------------------
1647 NB: this stuff was taken from the glibc test suite and modified to build
1648 in wxWidgets: if I read the copyright below properly, this shouldn't
1654 #ifdef wxTEST_PRINTF
1655 // use our functions from wxchar.cpp
1659 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1660 // in the tests below
1661 int wxPrintf( const wxChar
*format
, ... );
1662 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1665 #include "wx/longlong.h"
1669 static void rfg1 (void);
1670 static void rfg2 (void);
1674 fmtchk (const wxChar
*fmt
)
1676 (void) wxPrintf(_T("%s:\t`"), fmt
);
1677 (void) wxPrintf(fmt
, 0x12);
1678 (void) wxPrintf(_T("'\n"));
1682 fmtst1chk (const wxChar
*fmt
)
1684 (void) wxPrintf(_T("%s:\t`"), fmt
);
1685 (void) wxPrintf(fmt
, 4, 0x12);
1686 (void) wxPrintf(_T("'\n"));
1690 fmtst2chk (const wxChar
*fmt
)
1692 (void) wxPrintf(_T("%s:\t`"), fmt
);
1693 (void) wxPrintf(fmt
, 4, 4, 0x12);
1694 (void) wxPrintf(_T("'\n"));
1697 /* This page is covered by the following copyright: */
1699 /* (C) Copyright C E Chew
1701 * Feel free to copy, use and distribute this software provided:
1703 * 1. you do not pretend that you wrote it
1704 * 2. you leave this copyright notice intact.
1708 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1715 /* Formatted Output Test
1717 * This exercises the output formatting code.
1720 wxChar
*PointerNull
= NULL
;
1727 wxChar
*prefix
= buf
;
1730 wxPuts(_T("\nFormatted output test"));
1731 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1732 wxStrcpy(prefix
, _T("%"));
1733 for (i
= 0; i
< 2; i
++) {
1734 for (j
= 0; j
< 2; j
++) {
1735 for (k
= 0; k
< 2; k
++) {
1736 for (l
= 0; l
< 2; l
++) {
1737 wxStrcpy(prefix
, _T("%"));
1738 if (i
== 0) wxStrcat(prefix
, _T("-"));
1739 if (j
== 0) wxStrcat(prefix
, _T("+"));
1740 if (k
== 0) wxStrcat(prefix
, _T("#"));
1741 if (l
== 0) wxStrcat(prefix
, _T("0"));
1742 wxPrintf(_T("%5s |"), prefix
);
1743 wxStrcpy(tp
, prefix
);
1744 wxStrcat(tp
, _T("6d |"));
1746 wxStrcpy(tp
, prefix
);
1747 wxStrcat(tp
, _T("6o |"));
1749 wxStrcpy(tp
, prefix
);
1750 wxStrcat(tp
, _T("6x |"));
1752 wxStrcpy(tp
, prefix
);
1753 wxStrcat(tp
, _T("6X |"));
1755 wxStrcpy(tp
, prefix
);
1756 wxStrcat(tp
, _T("6u |"));
1763 wxPrintf(_T("%10s\n"), PointerNull
);
1764 wxPrintf(_T("%-10s\n"), PointerNull
);
1767 static void TestPrintf()
1769 static wxChar shortstr
[] = _T("Hi, Z.");
1770 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1771 I am ready for my first lesson today.");
1773 wxString test_format
;
1777 fmtchk(_T("%4.4x"));
1778 fmtchk(_T("%04.4x"));
1779 fmtchk(_T("%4.3x"));
1780 fmtchk(_T("%04.3x"));
1782 fmtst1chk(_T("%.*x"));
1783 fmtst1chk(_T("%0*x"));
1784 fmtst2chk(_T("%*.*x"));
1785 fmtst2chk(_T("%0*.*x"));
1787 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1788 wxPrintf(bad_format
.c_str());
1789 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1791 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1792 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1793 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1794 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1795 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1796 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1797 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1798 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1799 wxPrintf(test_format
.c_str(), -123456);
1800 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1801 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1803 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1804 wxPrintf(test_format
.c_str(), shortstr
);
1805 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1806 wxPrintf(test_format
.c_str(), shortstr
);
1807 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1808 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1809 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1810 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1812 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1813 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1814 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1815 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1816 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1817 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1818 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1819 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1820 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1821 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1822 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1823 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1825 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1826 wxPrintf (_T(" %6.5f\n"), .1);
1827 wxPrintf (_T("x%5.4fx\n"), .5);
1829 wxPrintf (_T("%#03x\n"), 1);
1831 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1837 while (niter
-- != 0)
1838 wxPrintf (_T("%.17e\n"), d
/ 2);
1843 // Open Watcom cause compiler error here
1844 // Error! E173: col(24) floating-point constant too small to represent
1845 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1848 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1849 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1850 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1851 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1852 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1853 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1854 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1855 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1856 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1857 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1862 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1864 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1865 rc
, WXSIZEOF(buf
), buf
);
1868 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1869 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1875 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1876 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1877 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1878 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1879 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1880 wxPrintf (_T("%g should be 10\n"), 10.0);
1881 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1885 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1891 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1893 result
|= wxStrcmp (buf
,
1894 _T("onetwo three "));
1896 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1903 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1905 // for some reason below line fails under Borland
1906 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1909 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1912 wxPuts (_T("\tFAILED"));
1914 wxUnusedVar(result
);
1915 wxPuts (wxEmptyString
);
1917 #endif // wxLongLong_t
1919 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1920 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1922 wxPuts (_T("--- Should be no further output. ---"));
1931 memset (bytes
, '\xff', sizeof bytes
);
1932 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1933 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1934 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1936 wxPuts (_T("%hhn overwrite more bytes"));
1941 wxPuts (_T("%hhn wrote incorrect value"));
1953 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1954 if (wxStrcmp (buf
, _T(" ")) != 0)
1955 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1956 wxSprintf (buf
, _T("%5.f"), 33.3);
1957 if (wxStrcmp (buf
, _T(" 33")) != 0)
1958 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1959 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1960 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1961 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1962 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1963 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1964 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1965 wxSprintf (buf
, _T("%.g"), 33.3);
1966 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1967 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1968 wxSprintf (buf
, _T("%.G"), 33.3);
1969 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1970 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1978 wxString test_format
;
1981 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
1982 if (wxStrcmp (buf
, _T("3")) != 0)
1983 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1985 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
1986 if (wxStrcmp (buf
, _T("3")) != 0)
1987 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1989 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
1990 if (wxStrcmp (buf
, _T(" 3")) != 0)
1991 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
1993 test_format
= _T("%04.*o");
1994 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1995 if (wxStrcmp (buf
, _T(" 041")) != 0)
1996 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
1998 test_format
= _T("%09.*u");
1999 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2000 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
2001 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
2003 test_format
= _T("%04.*x");
2004 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2005 if (wxStrcmp (buf
, _T(" 021")) != 0)
2006 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2008 test_format
= _T("%04.*X");
2009 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2010 if (wxStrcmp (buf
, _T(" 021")) != 0)
2011 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2014 #endif // TEST_PRINTF
2016 // ----------------------------------------------------------------------------
2017 // registry and related stuff
2018 // ----------------------------------------------------------------------------
2020 // this is for MSW only
2023 #undef TEST_REGISTRY
2028 #include "wx/confbase.h"
2029 #include "wx/msw/regconf.h"
2032 static void TestRegConfWrite()
2034 wxConfig
*config
= new wxConfig(_T("myapp"));
2035 config
->SetPath(_T("/group1"));
2036 config
->Write(_T("entry1"), _T("foo"));
2037 config
->SetPath(_T("/group2"));
2038 config
->Write(_T("entry1"), _T("bar"));
2042 static void TestRegConfRead()
2044 wxConfig
*config
= new wxConfig(_T("myapp"));
2048 config
->SetPath(_T("/"));
2049 wxPuts(_T("Enumerating / subgroups:"));
2050 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2054 bCont
= config
->GetNextGroup(str
, dummy
);
2058 #endif // TEST_REGCONF
2060 #ifdef TEST_REGISTRY
2062 #include "wx/msw/registry.h"
2064 // I chose this one because I liked its name, but it probably only exists under
2066 static const wxChar
*TESTKEY
=
2067 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2069 static void TestRegistryRead()
2071 wxPuts(_T("*** testing registry reading ***"));
2073 wxRegKey
key(TESTKEY
);
2074 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2077 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2082 size_t nSubKeys
, nValues
;
2083 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2085 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2088 wxPrintf(_T("Enumerating values:\n"));
2092 bool cont
= key
.GetFirstValue(value
, dummy
);
2095 wxPrintf(_T("Value '%s': type "), value
.c_str());
2096 switch ( key
.GetValueType(value
) )
2098 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2099 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2100 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2101 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2102 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2103 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2104 default: wxPrintf(_T("other (unknown)")); break;
2107 wxPrintf(_T(", value = "));
2108 if ( key
.IsNumericValue(value
) )
2111 key
.QueryValue(value
, &val
);
2112 wxPrintf(_T("%ld"), val
);
2117 key
.QueryValue(value
, val
);
2118 wxPrintf(_T("'%s'"), val
.c_str());
2120 key
.QueryRawValue(value
, val
);
2121 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2126 cont
= key
.GetNextValue(value
, dummy
);
2130 static void TestRegistryAssociation()
2133 The second call to deleteself genertaes an error message, with a
2134 messagebox saying .flo is crucial to system operation, while the .ddf
2135 call also fails, but with no error message
2140 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2142 key
= _T("ddxf_auto_file") ;
2143 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2145 key
= _T("ddxf_auto_file") ;
2146 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2148 key
= _T("program,0") ;
2149 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2151 key
= _T("program \"%1\"") ;
2153 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2155 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2157 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2159 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2163 #endif // TEST_REGISTRY
2165 // ----------------------------------------------------------------------------
2167 // ----------------------------------------------------------------------------
2169 #ifdef TEST_SCOPEGUARD
2171 #include "wx/scopeguard.h"
2173 static void function0() { puts("function0()"); }
2174 static void function1(int n
) { printf("function1(%d)\n", n
); }
2175 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2179 void method0() { printf("method0()\n"); }
2180 void method1(int n
) { printf("method1(%d)\n", n
); }
2181 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2184 static void TestScopeGuard()
2186 wxON_BLOCK_EXIT0(function0
);
2187 wxON_BLOCK_EXIT1(function1
, 17);
2188 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2191 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2192 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2193 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2195 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2196 dismissed
.Dismiss();
2201 // ----------------------------------------------------------------------------
2203 // ----------------------------------------------------------------------------
2207 #include "wx/socket.h"
2208 #include "wx/protocol/protocol.h"
2209 #include "wx/protocol/http.h"
2211 static void TestSocketServer()
2213 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2215 static const int PORT
= 3000;
2220 wxSocketServer
*server
= new wxSocketServer(addr
);
2221 if ( !server
->Ok() )
2223 wxPuts(_T("ERROR: failed to bind"));
2231 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2233 wxSocketBase
*socket
= server
->Accept();
2236 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2240 wxPuts(_T("Server: got a client."));
2242 server
->SetTimeout(60); // 1 min
2245 while ( !close
&& socket
->IsConnected() )
2248 wxChar ch
= _T('\0');
2251 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2253 // don't log error if the client just close the connection
2254 if ( socket
->IsConnected() )
2256 wxPuts(_T("ERROR: in wxSocket::Read."));
2276 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2277 if ( s
== _T("close") )
2279 wxPuts(_T("Closing connection"));
2283 else if ( s
== _T("quit") )
2288 wxPuts(_T("Shutting down the server"));
2290 else // not a special command
2292 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2293 socket
->Write("\r\n", 2);
2294 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2300 wxPuts(_T("Server: lost a client unexpectedly."));
2306 // same as "delete server" but is consistent with GUI programs
2310 static void TestSocketClient()
2312 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2314 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2317 addr
.Hostname(hostname
);
2320 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2322 wxSocketClient client
;
2323 if ( !client
.Connect(addr
) )
2325 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2329 wxPrintf(_T("--- Connected to %s:%u...\n"),
2330 addr
.Hostname().c_str(), addr
.Service());
2334 // could use simply "GET" here I suppose
2336 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2337 client
.Write(cmdGet
, cmdGet
.length());
2338 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2339 MakePrintable(cmdGet
).c_str());
2340 client
.Read(buf
, WXSIZEOF(buf
));
2341 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2345 #endif // TEST_SOCKETS
2347 // ----------------------------------------------------------------------------
2349 // ----------------------------------------------------------------------------
2353 #include "wx/protocol/ftp.h"
2357 #define FTP_ANONYMOUS
2359 #ifdef FTP_ANONYMOUS
2360 static const wxChar
*directory
= _T("/pub");
2361 static const wxChar
*filename
= _T("welcome.msg");
2363 static const wxChar
*directory
= _T("/etc");
2364 static const wxChar
*filename
= _T("issue");
2367 static bool TestFtpConnect()
2369 wxPuts(_T("*** Testing FTP connect ***"));
2371 #ifdef FTP_ANONYMOUS
2372 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2374 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2375 #else // !FTP_ANONYMOUS
2376 static const wxChar
*hostname
= "localhost";
2379 wxFgets(user
, WXSIZEOF(user
), stdin
);
2380 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2383 wxChar password
[256];
2384 wxPrintf(_T("Password for %s: "), password
);
2385 wxFgets(password
, WXSIZEOF(password
), stdin
);
2386 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2387 ftp
.SetPassword(password
);
2389 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2390 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2392 if ( !ftp
.Connect(hostname
) )
2394 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2400 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2401 hostname
, ftp
.Pwd().c_str());
2408 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2409 static void TestFtpWuFtpd()
2412 static const wxChar
*hostname
= _T("ftp.eudora.com");
2413 if ( !ftp
.Connect(hostname
) )
2415 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2419 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2420 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2423 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2427 size_t size
= in
->GetSize();
2428 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2430 wxChar
*data
= new wxChar
[size
];
2431 if ( !in
->Read(data
, size
) )
2433 wxPuts(_T("ERROR: read error"));
2437 wxPrintf(_T("Successfully retrieved the file.\n"));
2446 static void TestFtpList()
2448 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2451 if ( !ftp
.ChDir(directory
) )
2453 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2456 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2458 // test NLIST and LIST
2459 wxArrayString files
;
2460 if ( !ftp
.GetFilesList(files
) )
2462 wxPuts(_T("ERROR: failed to get NLIST of files"));
2466 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2467 size_t count
= files
.GetCount();
2468 for ( size_t n
= 0; n
< count
; n
++ )
2470 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2472 wxPuts(_T("End of the file list"));
2475 if ( !ftp
.GetDirList(files
) )
2477 wxPuts(_T("ERROR: failed to get LIST of files"));
2481 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2482 size_t count
= files
.GetCount();
2483 for ( size_t n
= 0; n
< count
; n
++ )
2485 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2487 wxPuts(_T("End of the file list"));
2490 if ( !ftp
.ChDir(_T("..")) )
2492 wxPuts(_T("ERROR: failed to cd to .."));
2495 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2498 static void TestFtpDownload()
2500 wxPuts(_T("*** Testing wxFTP download ***\n"));
2503 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2506 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2510 size_t size
= in
->GetSize();
2511 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2514 wxChar
*data
= new wxChar
[size
];
2515 if ( !in
->Read(data
, size
) )
2517 wxPuts(_T("ERROR: read error"));
2521 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2529 static void TestFtpFileSize()
2531 wxPuts(_T("*** Testing FTP SIZE command ***"));
2533 if ( !ftp
.ChDir(directory
) )
2535 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2538 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2540 if ( ftp
.FileExists(filename
) )
2542 int size
= ftp
.GetFileSize(filename
);
2544 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2546 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2550 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2554 static void TestFtpMisc()
2556 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2558 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2560 wxPuts(_T("ERROR: STAT failed"));
2564 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2567 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2569 wxPuts(_T("ERROR: HELP SITE failed"));
2573 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2574 ftp
.GetLastResult().c_str());
2578 static void TestFtpInteractive()
2580 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2586 wxPrintf(_T("Enter FTP command: "));
2587 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2590 // kill the last '\n'
2591 buf
[wxStrlen(buf
) - 1] = 0;
2593 // special handling of LIST and NLST as they require data connection
2594 wxString
start(buf
, 4);
2596 if ( start
== _T("LIST") || start
== _T("NLST") )
2599 if ( wxStrlen(buf
) > 4 )
2602 wxArrayString files
;
2603 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2605 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2609 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2610 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2611 size_t count
= files
.GetCount();
2612 for ( size_t n
= 0; n
< count
; n
++ )
2614 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2616 wxPuts(_T("--- End of the file list"));
2621 wxChar ch
= ftp
.SendCommand(buf
);
2622 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2625 wxPrintf(_T(" (return code %c)"), ch
);
2628 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2632 wxPuts(_T("\n*** done ***"));
2635 static void TestFtpUpload()
2637 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2640 static const wxChar
*file1
= _T("test1");
2641 static const wxChar
*file2
= _T("test2");
2642 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2645 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2646 out
->Write("First hello", 11);
2650 // send a command to check the remote file
2651 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2653 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2657 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2658 file1
, ftp
.GetLastResult().c_str());
2661 out
= ftp
.GetOutputStream(file2
);
2664 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2665 out
->Write("Second hello", 12);
2672 // ----------------------------------------------------------------------------
2674 // ----------------------------------------------------------------------------
2676 #ifdef TEST_STACKWALKER
2678 #if wxUSE_STACKWALKER
2680 #include "wx/stackwalk.h"
2682 class StackDump
: public wxStackWalker
2685 StackDump(const char *argv0
)
2686 : wxStackWalker(argv0
)
2690 virtual void Walk(size_t skip
= 1)
2692 wxPuts(_T("Stack dump:"));
2694 wxStackWalker::Walk(skip
);
2698 virtual void OnStackFrame(const wxStackFrame
& frame
)
2700 printf("[%2d] ", frame
.GetLevel());
2702 wxString name
= frame
.GetName();
2703 if ( !name
.empty() )
2705 printf("%-20.40s", name
.mb_str());
2709 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2712 if ( frame
.HasSourceLocation() )
2715 frame
.GetFileName().mb_str(),
2722 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2724 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2729 static void TestStackWalk(const char *argv0
)
2731 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2733 StackDump
dump(argv0
);
2737 #endif // wxUSE_STACKWALKER
2739 #endif // TEST_STACKWALKER
2741 // ----------------------------------------------------------------------------
2743 // ----------------------------------------------------------------------------
2745 #ifdef TEST_STDPATHS
2747 #include "wx/stdpaths.h"
2749 static void TestStandardPaths()
2751 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2753 wxTheApp
->SetAppName(_T("console"));
2755 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2756 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2757 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2758 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2759 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2760 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2761 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2762 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2763 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2764 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2765 wxPrintf(_T("Localized res. dir:\t%s\n"),
2766 stdp
.GetLocalizedResourcesDir(_T("fr")).c_str());
2767 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2768 stdp
.GetLocalizedResourcesDir
2771 wxStandardPaths::ResourceCat_Messages
2775 #endif // TEST_STDPATHS
2777 // ----------------------------------------------------------------------------
2779 // ----------------------------------------------------------------------------
2783 #include "wx/wfstream.h"
2784 #include "wx/mstream.h"
2786 static void TestFileStream()
2788 wxPuts(_T("*** Testing wxFileInputStream ***"));
2790 static const wxString filename
= _T("testdata.fs");
2792 wxFileOutputStream
fsOut(filename
);
2793 fsOut
.Write("foo", 3);
2796 wxFileInputStream
fsIn(filename
);
2797 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2798 while ( !fsIn
.Eof() )
2800 wxPutchar(fsIn
.GetC());
2803 if ( !wxRemoveFile(filename
) )
2805 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2808 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2811 static void TestMemoryStream()
2813 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2815 wxMemoryOutputStream memOutStream
;
2816 wxPrintf(_T("Initially out stream offset: %lu\n"),
2817 (unsigned long)memOutStream
.TellO());
2819 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2821 memOutStream
.PutC(*p
);
2824 wxPrintf(_T("Final out stream offset: %lu\n"),
2825 (unsigned long)memOutStream
.TellO());
2827 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2830 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2832 wxMemoryInputStream
memInpStream(buf
, len
);
2833 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2834 while ( !memInpStream
.Eof() )
2836 wxPutchar(memInpStream
.GetC());
2839 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2842 #endif // TEST_STREAMS
2844 // ----------------------------------------------------------------------------
2846 // ----------------------------------------------------------------------------
2850 #include "wx/stopwatch.h"
2851 #include "wx/utils.h"
2853 static void TestStopWatch()
2855 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2859 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2862 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2864 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2868 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2871 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2874 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2877 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2880 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2883 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2884 for ( size_t n
= 0; n
< 70; n
++ )
2888 for ( size_t m
= 0; m
< 100000; m
++ )
2890 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2892 wxPuts(_T("\ntime is negative - ERROR!"));
2900 wxPuts(_T(", ok."));
2903 #endif // TEST_TIMER
2905 // ----------------------------------------------------------------------------
2907 // ----------------------------------------------------------------------------
2911 #include "wx/vcard.h"
2913 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2916 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2919 wxPrintf(_T("%s%s"),
2920 wxString(_T('\t'), level
).c_str(),
2921 vcObj
->GetName().c_str());
2924 switch ( vcObj
->GetType() )
2926 case wxVCardObject::String
:
2927 case wxVCardObject::UString
:
2930 vcObj
->GetValue(&val
);
2931 value
<< _T('"') << val
<< _T('"');
2935 case wxVCardObject::Int
:
2938 vcObj
->GetValue(&i
);
2939 value
.Printf(_T("%u"), i
);
2943 case wxVCardObject::Long
:
2946 vcObj
->GetValue(&l
);
2947 value
.Printf(_T("%lu"), l
);
2951 case wxVCardObject::None
:
2954 case wxVCardObject::Object
:
2955 value
= _T("<node>");
2959 value
= _T("<unknown value type>");
2963 wxPrintf(_T(" = %s"), value
.c_str());
2966 DumpVObject(level
+ 1, *vcObj
);
2969 vcObj
= vcard
.GetNextProp(&cookie
);
2973 static void DumpVCardAddresses(const wxVCard
& vcard
)
2975 wxPuts(_T("\nShowing all addresses from vCard:\n"));
2979 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
2983 int flags
= addr
->GetFlags();
2984 if ( flags
& wxVCardAddress::Domestic
)
2986 flagsStr
<< _T("domestic ");
2988 if ( flags
& wxVCardAddress::Intl
)
2990 flagsStr
<< _T("international ");
2992 if ( flags
& wxVCardAddress::Postal
)
2994 flagsStr
<< _T("postal ");
2996 if ( flags
& wxVCardAddress::Parcel
)
2998 flagsStr
<< _T("parcel ");
3000 if ( flags
& wxVCardAddress::Home
)
3002 flagsStr
<< _T("home ");
3004 if ( flags
& wxVCardAddress::Work
)
3006 flagsStr
<< _T("work ");
3009 wxPrintf(_T("Address %u:\n")
3011 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3014 addr
->GetPostOffice().c_str(),
3015 addr
->GetExtAddress().c_str(),
3016 addr
->GetStreet().c_str(),
3017 addr
->GetLocality().c_str(),
3018 addr
->GetRegion().c_str(),
3019 addr
->GetPostalCode().c_str(),
3020 addr
->GetCountry().c_str()
3024 addr
= vcard
.GetNextAddress(&cookie
);
3028 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
3030 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3034 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
3038 int flags
= phone
->GetFlags();
3039 if ( flags
& wxVCardPhoneNumber::Voice
)
3041 flagsStr
<< _T("voice ");
3043 if ( flags
& wxVCardPhoneNumber::Fax
)
3045 flagsStr
<< _T("fax ");
3047 if ( flags
& wxVCardPhoneNumber::Cellular
)
3049 flagsStr
<< _T("cellular ");
3051 if ( flags
& wxVCardPhoneNumber::Modem
)
3053 flagsStr
<< _T("modem ");
3055 if ( flags
& wxVCardPhoneNumber::Home
)
3057 flagsStr
<< _T("home ");
3059 if ( flags
& wxVCardPhoneNumber::Work
)
3061 flagsStr
<< _T("work ");
3064 wxPrintf(_T("Phone number %u:\n")
3069 phone
->GetNumber().c_str()
3073 phone
= vcard
.GetNextPhoneNumber(&cookie
);
3077 static void TestVCardRead()
3079 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3081 wxVCard
vcard(_T("vcard.vcf"));
3082 if ( !vcard
.IsOk() )
3084 wxPuts(_T("ERROR: couldn't load vCard."));
3088 // read individual vCard properties
3089 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3093 vcObj
->GetValue(&value
);
3098 value
= _T("<none>");
3101 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3104 if ( !vcard
.GetFullName(&value
) )
3106 value
= _T("<none>");
3109 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3111 // now show how to deal with multiply occurring properties
3112 DumpVCardAddresses(vcard
);
3113 DumpVCardPhoneNumbers(vcard
);
3115 // and finally show all
3116 wxPuts(_T("\nNow dumping the entire vCard:\n")
3117 "-----------------------------\n");
3119 DumpVObject(0, vcard
);
3123 static void TestVCardWrite()
3125 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3128 if ( !vcard
.IsOk() )
3130 wxPuts(_T("ERROR: couldn't create vCard."));
3135 vcard
.SetName("Zeitlin", "Vadim");
3136 vcard
.SetFullName("Vadim Zeitlin");
3137 vcard
.SetOrganization("wxWidgets", "R&D");
3139 // just dump the vCard back
3140 wxPuts(_T("Entire vCard follows:\n"));
3141 wxPuts(vcard
.Write());
3145 #endif // TEST_VCARD
3147 // ----------------------------------------------------------------------------
3149 // ----------------------------------------------------------------------------
3151 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3157 #include "wx/volume.h"
3159 static const wxChar
*volumeKinds
[] =
3165 _T("network volume"),
3169 static void TestFSVolume()
3171 wxPuts(_T("*** Testing wxFSVolume class ***"));
3173 wxArrayString volumes
= wxFSVolume::GetVolumes();
3174 size_t count
= volumes
.GetCount();
3178 wxPuts(_T("ERROR: no mounted volumes?"));
3182 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3184 for ( size_t n
= 0; n
< count
; n
++ )
3186 wxFSVolume
vol(volumes
[n
]);
3189 wxPuts(_T("ERROR: couldn't create volume"));
3193 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3195 vol
.GetDisplayName().c_str(),
3196 vol
.GetName().c_str(),
3197 volumeKinds
[vol
.GetKind()],
3198 vol
.IsWritable() ? _T("rw") : _T("ro"),
3199 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3204 #endif // TEST_VOLUME
3206 // ----------------------------------------------------------------------------
3207 // wide char and Unicode support
3208 // ----------------------------------------------------------------------------
3212 #include "wx/strconv.h"
3213 #include "wx/fontenc.h"
3214 #include "wx/encconv.h"
3215 #include "wx/buffer.h"
3217 static const unsigned char utf8koi8r
[] =
3219 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3220 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3221 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3222 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3223 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3224 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3225 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3228 static const unsigned char utf8iso8859_1
[] =
3230 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3231 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3232 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3233 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3234 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3237 static const unsigned char utf8Invalid
[] =
3239 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3240 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3241 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3242 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3246 static const struct Utf8Data
3248 const unsigned char *text
;
3250 const wxChar
*charset
;
3251 wxFontEncoding encoding
;
3254 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3255 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3256 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3259 static void TestUtf8()
3261 wxPuts(_T("*** Testing UTF8 support ***\n"));
3266 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3268 const Utf8Data
& u8d
= utf8data
[n
];
3269 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3270 WXSIZEOF(wbuf
)) == (size_t)-1 )
3272 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3276 wxCSConv
conv(u8d
.charset
);
3277 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3279 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3283 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3287 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3289 s
= _T("<< conversion failed >>");
3290 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3294 wxPuts(wxEmptyString
);
3297 static void TestEncodingConverter()
3299 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3301 // using wxEncodingConverter should give the same result as above
3304 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3305 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3307 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3311 wxEncodingConverter ec
;
3312 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3313 ec
.Convert(wbuf
, buf
);
3314 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3317 wxPuts(wxEmptyString
);
3320 #endif // TEST_WCHAR
3322 // ----------------------------------------------------------------------------
3324 // ----------------------------------------------------------------------------
3328 #include "wx/filesys.h"
3329 #include "wx/fs_zip.h"
3330 #include "wx/zipstrm.h"
3332 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3334 static void TestZipStreamRead()
3336 wxPuts(_T("*** Testing ZIP reading ***\n"));
3338 static const wxString filename
= _T("foo");
3339 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3340 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3342 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3343 while ( !istr
.Eof() )
3345 wxPutchar(istr
.GetC());
3349 wxPuts(_T("\n----- done ------"));
3352 static void DumpZipDirectory(wxFileSystem
& fs
,
3353 const wxString
& dir
,
3354 const wxString
& indent
)
3356 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3357 TESTFILE_ZIP
, dir
.c_str());
3358 wxString wildcard
= prefix
+ _T("/*");
3360 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3361 while ( !dirname
.empty() )
3363 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3365 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3370 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3372 DumpZipDirectory(fs
, dirname
,
3373 indent
+ wxString(_T(' '), 4));
3375 dirname
= fs
.FindNext();
3378 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3379 while ( !filename
.empty() )
3381 if ( !filename
.StartsWith(prefix
, &filename
) )
3383 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3388 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3390 filename
= fs
.FindNext();
3394 static void TestZipFileSystem()
3396 wxPuts(_T("*** Testing ZIP file system ***\n"));
3398 wxFileSystem::AddHandler(new wxZipFSHandler
);
3400 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3402 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3407 // ----------------------------------------------------------------------------
3409 // ----------------------------------------------------------------------------
3411 #ifdef TEST_DATETIME
3413 #include "wx/math.h"
3414 #include "wx/datetime.h"
3416 // this test miscellaneous static wxDateTime functions
3420 static void TestTimeStatic()
3422 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3424 // some info about the current date
3425 int year
= wxDateTime::GetCurrentYear();
3426 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3428 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3429 wxDateTime::GetNumberOfDays(year
));
3431 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3432 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3433 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3434 wxDateTime::GetMonthName(month
).c_str(),
3435 wxDateTime::GetNumberOfDays(month
));
3438 // test time zones stuff
3439 static void TestTimeZones()
3441 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3443 wxDateTime now
= wxDateTime::Now();
3445 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3446 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3447 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3448 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3449 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3450 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3452 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3454 wxDateTime::Tm tm
= now
.GetTm();
3455 if ( wxDateTime(tm
) != now
)
3457 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3458 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3462 // test some minimal support for the dates outside the standard range
3463 static void TestTimeRange()
3465 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3467 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3469 wxPrintf(_T("Unix epoch:\t%s\n"),
3470 wxDateTime(2440587.5).Format(fmt
).c_str());
3471 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3472 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3473 wxPrintf(_T("JDN 0: \t%s\n"),
3474 wxDateTime(0.0).Format(fmt
).c_str());
3475 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3476 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3477 wxPrintf(_T("May 29, 2099:\t%s\n"),
3478 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3481 // test DST calculations
3482 static void TestTimeDST()
3484 wxPuts(_T("\n*** wxDateTime DST test ***"));
3486 wxPrintf(_T("DST is%s in effect now.\n\n"),
3487 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3489 for ( int year
= 1990; year
< 2005; year
++ )
3491 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3493 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3494 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3500 #if TEST_INTERACTIVE
3502 static void TestDateTimeInteractive()
3504 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3510 wxPrintf(_T("Enter a date: "));
3511 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3514 // kill the last '\n'
3515 buf
[wxStrlen(buf
) - 1] = 0;
3518 const wxChar
*p
= dt
.ParseDate(buf
);
3521 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3527 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3530 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3531 dt
.Format(_T("%b %d, %Y")).c_str(),
3533 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3534 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3535 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3538 wxPuts(_T("\n*** done ***"));
3541 #endif // TEST_INTERACTIVE
3545 static void TestTimeMS()
3547 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3549 wxDateTime dt1
= wxDateTime::Now(),
3550 dt2
= wxDateTime::UNow();
3552 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3553 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3554 wxPrintf(_T("Dummy loop: "));
3555 for ( int i
= 0; i
< 6000; i
++ )
3557 //for ( int j = 0; j < 10; j++ )
3560 s
.Printf(_T("%g"), sqrt((float)i
));
3566 wxPuts(_T(", done"));
3569 dt2
= wxDateTime::UNow();
3570 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3572 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3574 wxPuts(_T("\n*** done ***"));
3577 static void TestTimeHolidays()
3579 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3581 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3582 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3583 dtEnd
= dtStart
.GetLastMonthDay();
3585 wxDateTimeArray hol
;
3586 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3588 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3590 wxPrintf(_T("All holidays between %s and %s:\n"),
3591 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3593 size_t count
= hol
.GetCount();
3594 for ( size_t n
= 0; n
< count
; n
++ )
3596 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3599 wxPuts(wxEmptyString
);
3602 static void TestTimeZoneBug()
3604 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3606 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3607 for ( int i
= 0; i
< 31; i
++ )
3609 wxPrintf(_T("Date %s: week day %s.\n"),
3610 date
.Format(_T("%d-%m-%Y")).c_str(),
3611 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3613 date
+= wxDateSpan::Day();
3616 wxPuts(wxEmptyString
);
3619 static void TestTimeSpanFormat()
3621 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3623 static const wxChar
*formats
[] =
3625 _T("(default) %H:%M:%S"),
3626 _T("%E weeks and %D days"),
3627 _T("%l milliseconds"),
3628 _T("(with ms) %H:%M:%S:%l"),
3629 _T("100%% of minutes is %M"), // test "%%"
3630 _T("%D days and %H hours"),
3631 _T("or also %S seconds"),
3634 wxTimeSpan
ts1(1, 2, 3, 4),
3636 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3638 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3639 ts1
.Format(formats
[n
]).c_str(),
3640 ts2
.Format(formats
[n
]).c_str());
3643 wxPuts(wxEmptyString
);
3648 #endif // TEST_DATETIME
3650 // ----------------------------------------------------------------------------
3651 // wxTextInput/OutputStream
3652 // ----------------------------------------------------------------------------
3654 #ifdef TEST_TEXTSTREAM
3656 #include "wx/txtstrm.h"
3657 #include "wx/wfstream.h"
3659 static void TestTextInputStream()
3661 wxPuts(_T("\n*** wxTextInputStream test ***"));
3663 wxString filename
= _T("testdata.fc");
3664 wxFileInputStream
fsIn(filename
);
3667 wxPuts(_T("ERROR: couldn't open file."));
3671 wxTextInputStream
tis(fsIn
);
3676 const wxString s
= tis
.ReadLine();
3678 // line could be non empty if the last line of the file isn't
3679 // terminated with EOL
3680 if ( fsIn
.Eof() && s
.empty() )
3683 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3688 #endif // TEST_TEXTSTREAM
3690 // ----------------------------------------------------------------------------
3692 // ----------------------------------------------------------------------------
3696 #include "wx/thread.h"
3698 static size_t gs_counter
= (size_t)-1;
3699 static wxCriticalSection gs_critsect
;
3700 static wxSemaphore gs_cond
;
3702 class MyJoinableThread
: public wxThread
3705 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3706 { m_n
= n
; Create(); }
3708 // thread execution starts here
3709 virtual ExitCode
Entry();
3715 wxThread::ExitCode
MyJoinableThread::Entry()
3717 unsigned long res
= 1;
3718 for ( size_t n
= 1; n
< m_n
; n
++ )
3722 // it's a loooong calculation :-)
3726 return (ExitCode
)res
;
3729 class MyDetachedThread
: public wxThread
3732 MyDetachedThread(size_t n
, wxChar ch
)
3736 m_cancelled
= false;
3741 // thread execution starts here
3742 virtual ExitCode
Entry();
3745 virtual void OnExit();
3748 size_t m_n
; // number of characters to write
3749 wxChar m_ch
; // character to write
3751 bool m_cancelled
; // false if we exit normally
3754 wxThread::ExitCode
MyDetachedThread::Entry()
3757 wxCriticalSectionLocker
lock(gs_critsect
);
3758 if ( gs_counter
== (size_t)-1 )
3764 for ( size_t n
= 0; n
< m_n
; n
++ )
3766 if ( TestDestroy() )
3776 wxThread::Sleep(100);
3782 void MyDetachedThread::OnExit()
3784 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3786 wxCriticalSectionLocker
lock(gs_critsect
);
3787 if ( !--gs_counter
&& !m_cancelled
)
3791 static void TestDetachedThreads()
3793 wxPuts(_T("\n*** Testing detached threads ***"));
3795 static const size_t nThreads
= 3;
3796 MyDetachedThread
*threads
[nThreads
];
3798 for ( n
= 0; n
< nThreads
; n
++ )
3800 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3803 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3804 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3806 for ( n
= 0; n
< nThreads
; n
++ )
3811 // wait until all threads terminate
3814 wxPuts(wxEmptyString
);
3817 static void TestJoinableThreads()
3819 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3821 // calc 10! in the background
3822 MyJoinableThread
thread(10);
3825 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3826 (unsigned long)thread
.Wait());
3829 static void TestThreadSuspend()
3831 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3833 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3837 // this is for this demo only, in a real life program we'd use another
3838 // condition variable which would be signaled from wxThread::Entry() to
3839 // tell us that the thread really started running - but here just wait a
3840 // bit and hope that it will be enough (the problem is, of course, that
3841 // the thread might still not run when we call Pause() which will result
3843 wxThread::Sleep(300);
3845 for ( size_t n
= 0; n
< 3; n
++ )
3849 wxPuts(_T("\nThread suspended"));
3852 // don't sleep but resume immediately the first time
3853 wxThread::Sleep(300);
3855 wxPuts(_T("Going to resume the thread"));
3860 wxPuts(_T("Waiting until it terminates now"));
3862 // wait until the thread terminates
3865 wxPuts(wxEmptyString
);
3868 static void TestThreadDelete()
3870 // As above, using Sleep() is only for testing here - we must use some
3871 // synchronisation object instead to ensure that the thread is still
3872 // running when we delete it - deleting a detached thread which already
3873 // terminated will lead to a crash!
3875 wxPuts(_T("\n*** Testing thread delete function ***"));
3877 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3881 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3883 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3887 wxThread::Sleep(300);
3891 wxPuts(_T("\nDeleted a running thread."));
3893 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3897 wxThread::Sleep(300);
3903 wxPuts(_T("\nDeleted a sleeping thread."));
3905 MyJoinableThread
thread3(20);
3910 wxPuts(_T("\nDeleted a joinable thread."));
3912 MyJoinableThread
thread4(2);
3915 wxThread::Sleep(300);
3919 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3921 wxPuts(wxEmptyString
);
3924 class MyWaitingThread
: public wxThread
3927 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3930 m_condition
= condition
;
3935 virtual ExitCode
Entry()
3937 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3942 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3946 m_condition
->Wait();
3949 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3957 wxCondition
*m_condition
;
3960 static void TestThreadConditions()
3963 wxCondition
condition(mutex
);
3965 // otherwise its difficult to understand which log messages pertain to
3967 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3968 // condition.GetId(), gs_cond.GetId());
3970 // create and launch threads
3971 MyWaitingThread
*threads
[10];
3974 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3976 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
3979 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3984 // wait until all threads run
3985 wxPuts(_T("Main thread is waiting for the other threads to start"));
3988 size_t nRunning
= 0;
3989 while ( nRunning
< WXSIZEOF(threads
) )
3995 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
3999 wxPuts(_T("Main thread: all threads started up."));
4002 wxThread::Sleep(500);
4005 // now wake one of them up
4006 wxPrintf(_T("Main thread: about to signal the condition.\n"));
4011 wxThread::Sleep(200);
4013 // wake all the (remaining) threads up, so that they can exit
4014 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
4016 condition
.Broadcast();
4018 // give them time to terminate (dirty!)
4019 wxThread::Sleep(500);
4022 #include "wx/utils.h"
4024 class MyExecThread
: public wxThread
4027 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
4033 virtual ExitCode
Entry()
4035 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
4042 static void TestThreadExec()
4044 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4046 MyExecThread
thread(_T("true"));
4049 wxPrintf(_T("Main program exit code: %ld.\n"),
4050 wxExecute(_T("false"), wxEXEC_SYNC
));
4052 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
4056 #include "wx/datetime.h"
4058 class MySemaphoreThread
: public wxThread
4061 MySemaphoreThread(int i
, wxSemaphore
*sem
)
4062 : wxThread(wxTHREAD_JOINABLE
),
4069 virtual ExitCode
Entry()
4071 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4072 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4076 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4077 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4081 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4082 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4094 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
4096 static void TestSemaphore()
4098 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4100 static const int SEM_LIMIT
= 3;
4102 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
4103 ArrayThreads threads
;
4105 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
4107 threads
.Add(new MySemaphoreThread(i
, &sem
));
4108 threads
.Last()->Run();
4111 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4118 #endif // TEST_THREADS
4120 // ----------------------------------------------------------------------------
4122 // ----------------------------------------------------------------------------
4124 #ifdef TEST_SNGLINST
4125 #include "wx/snglinst.h"
4126 #endif // TEST_SNGLINST
4128 int main(int argc
, char **argv
)
4131 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4136 for (n
= 0; n
< argc
; n
++ )
4138 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4139 wxArgv
[n
] = wxStrdup(warg
);
4144 #else // !wxUSE_UNICODE
4146 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4148 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4150 wxInitializer initializer
;
4153 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4158 #ifdef TEST_SNGLINST
4159 wxSingleInstanceChecker checker
;
4160 if ( checker
.Create(_T(".wxconsole.lock")) )
4162 if ( checker
.IsAnotherRunning() )
4164 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4169 // wait some time to give time to launch another instance
4170 wxPrintf(_T("Press \"Enter\" to continue..."));
4173 else // failed to create
4175 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4177 #endif // TEST_SNGLINST
4180 TestCmdLineConvert();
4182 #if wxUSE_CMDLINE_PARSER
4183 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4185 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
4186 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4187 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
4188 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
4190 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
4191 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
4192 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
4193 wxCMD_LINE_VAL_NUMBER
},
4194 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
4195 wxCMD_LINE_VAL_DATE
},
4197 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
4198 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4203 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4205 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4206 wxCMD_LINE_VAL_STRING
,
4207 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4209 switch ( parser
.Parse() )
4212 wxLogMessage(_T("Help was given, terminating."));
4216 ShowCmdLine(parser
);
4220 wxLogMessage(_T("Syntax error detected, aborting."));
4223 #endif // wxUSE_CMDLINE_PARSER
4225 #endif // TEST_CMDLINE
4237 TestDllListLoaded();
4238 #endif // TEST_DYNLIB
4242 #endif // TEST_ENVIRON
4246 #endif // TEST_EXECUTE
4248 #ifdef TEST_FILECONF
4250 #endif // TEST_FILECONF
4254 #endif // TEST_LOCALE
4257 wxPuts(_T("*** Testing wxLog ***"));
4260 for ( size_t n
= 0; n
< 8000; n
++ )
4262 s
<< (wxChar
)(_T('A') + (n
% 26));
4265 wxLogWarning(_T("The length of the string is %lu"),
4266 (unsigned long)s
.length());
4269 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4271 // this one shouldn't be truncated
4274 // but this one will because log functions use fixed size buffer
4275 // (note that it doesn't need '\n' at the end neither - will be added
4277 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4286 #ifdef TEST_FILENAME
4289 TestFileNameDirManip();
4290 TestFileNameComparison();
4291 TestFileNameOperations();
4292 #endif // TEST_FILENAME
4294 #ifdef TEST_FILETIME
4299 #endif // TEST_FILETIME
4302 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4303 if ( TestFtpConnect() )
4313 #if TEST_INTERACTIVE
4314 TestFtpInteractive();
4317 //else: connecting to the FTP server failed
4325 wxLog::AddTraceMask(_T("mime"));
4329 TestMimeAssociate();
4334 #ifdef TEST_INFO_FUNCTIONS
4339 #if TEST_INTERACTIVE
4343 #endif // TEST_INFO_FUNCTIONS
4345 #ifdef TEST_PATHLIST
4347 #endif // TEST_PATHLIST
4355 #endif // TEST_PRINTF
4362 #endif // TEST_REGCONF
4364 #if defined TEST_REGEX && TEST_INTERACTIVE
4365 TestRegExInteractive();
4366 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4368 #ifdef TEST_REGISTRY
4370 TestRegistryAssociation();
4371 #endif // TEST_REGISTRY
4376 #endif // TEST_SOCKETS
4383 #endif // TEST_STREAMS
4385 #ifdef TEST_TEXTSTREAM
4386 TestTextInputStream();
4387 #endif // TEST_TEXTSTREAM
4390 int nCPUs
= wxThread::GetCPUCount();
4391 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4393 wxThread::SetConcurrency(nCPUs
);
4395 TestJoinableThreads();
4398 TestJoinableThreads();
4399 TestDetachedThreads();
4400 TestThreadSuspend();
4402 TestThreadConditions();
4406 #endif // TEST_THREADS
4410 #endif // TEST_TIMER
4412 #ifdef TEST_DATETIME
4419 TestTimeSpanFormat();
4425 #if TEST_INTERACTIVE
4426 TestDateTimeInteractive();
4428 #endif // TEST_DATETIME
4430 #ifdef TEST_SCOPEGUARD
4434 #ifdef TEST_STACKWALKER
4435 #if wxUSE_STACKWALKER
4436 TestStackWalk(argv
[0]);
4438 #endif // TEST_STACKWALKER
4440 #ifdef TEST_STDPATHS
4441 TestStandardPaths();
4445 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4447 #endif // TEST_USLEEP
4452 #endif // TEST_VCARD
4456 #endif // TEST_VOLUME
4460 TestEncodingConverter();
4461 #endif // TEST_WCHAR
4464 TestZipStreamRead();
4465 TestZipFileSystem();
4470 for ( int n
= 0; n
< argc
; n
++ )
4475 #endif // wxUSE_UNICODE