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
74 #define TEST_SCOPEGUARD
76 // #define TEST_SOCKETS --FIXME! (RN)
77 #define TEST_STACKWALKER
80 #define TEST_TEXTSTREAM
83 // #define TEST_VCARD -- don't enable this (VZ)
84 // #define TEST_VOLUME --FIXME! (RN)
91 // some tests are interactive, define this to run them
92 #ifdef TEST_INTERACTIVE
93 #undef TEST_INTERACTIVE
95 #define TEST_INTERACTIVE 1
97 #define TEST_INTERACTIVE 0
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 #if defined(TEST_SOCKETS)
110 // replace TABs with \t and CRs with \n
111 static wxString
MakePrintable(const wxChar
*s
)
114 (void)str
.Replace(_T("\t"), _T("\\t"));
115 (void)str
.Replace(_T("\n"), _T("\\n"));
116 (void)str
.Replace(_T("\r"), _T("\\r"));
121 #endif // MakePrintable() is used
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
129 #include "wx/cmdline.h"
130 #include "wx/datetime.h"
132 #if wxUSE_CMDLINE_PARSER
134 static void ShowCmdLine(const wxCmdLineParser
& parser
)
136 wxString s
= _T("Input files: ");
138 size_t count
= parser
.GetParamCount();
139 for ( size_t param
= 0; param
< count
; param
++ )
141 s
<< parser
.GetParam(param
) << ' ';
145 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
146 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
151 if ( parser
.Found(_T("o"), &strVal
) )
152 s
<< _T("Output file:\t") << strVal
<< '\n';
153 if ( parser
.Found(_T("i"), &strVal
) )
154 s
<< _T("Input dir:\t") << strVal
<< '\n';
155 if ( parser
.Found(_T("s"), &lVal
) )
156 s
<< _T("Size:\t") << lVal
<< '\n';
157 if ( parser
.Found(_T("d"), &dt
) )
158 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
159 if ( parser
.Found(_T("project_name"), &strVal
) )
160 s
<< _T("Project:\t") << strVal
<< '\n';
165 #endif // wxUSE_CMDLINE_PARSER
167 static void TestCmdLineConvert()
169 static const wxChar
*cmdlines
[] =
172 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
173 _T("literal \\\" and \"\""),
176 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
178 const wxChar
*cmdline
= cmdlines
[n
];
179 wxPrintf(_T("Parsing: %s\n"), cmdline
);
180 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
182 size_t count
= args
.GetCount();
183 wxPrintf(_T("\targc = %u\n"), count
);
184 for ( size_t arg
= 0; arg
< count
; arg
++ )
186 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
191 #endif // TEST_CMDLINE
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
202 static const wxChar
*ROOTDIR
= _T("/");
203 static const wxChar
*TESTDIR
= _T("/usr/local/share");
204 #elif defined(__WXMSW__) || defined(__DOS__)
205 static const wxChar
*ROOTDIR
= _T("c:\\");
206 static const wxChar
*TESTDIR
= _T("d:\\");
208 #error "don't know where the root directory is"
211 static void TestDirEnumHelper(wxDir
& dir
,
212 int flags
= wxDIR_DEFAULT
,
213 const wxString
& filespec
= wxEmptyString
)
217 if ( !dir
.IsOpened() )
220 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
223 wxPrintf(_T("\t%s\n"), filename
.c_str());
225 cont
= dir
.GetNext(&filename
);
228 wxPuts(wxEmptyString
);
231 static void TestDirEnum()
233 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
235 wxString cwd
= wxGetCwd();
236 if ( !wxDir::Exists(cwd
) )
238 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
243 if ( !dir
.IsOpened() )
245 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
249 wxPuts(_T("Enumerating everything in current directory:"));
250 TestDirEnumHelper(dir
);
252 wxPuts(_T("Enumerating really everything in current directory:"));
253 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
255 wxPuts(_T("Enumerating object files in current directory:"));
256 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
258 wxPuts(_T("Enumerating directories in current directory:"));
259 TestDirEnumHelper(dir
, wxDIR_DIRS
);
261 wxPuts(_T("Enumerating files in current directory:"));
262 TestDirEnumHelper(dir
, wxDIR_FILES
);
264 wxPuts(_T("Enumerating files including hidden in current directory:"));
265 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
269 wxPuts(_T("Enumerating everything in root directory:"));
270 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
272 wxPuts(_T("Enumerating directories in root directory:"));
273 TestDirEnumHelper(dir
, wxDIR_DIRS
);
275 wxPuts(_T("Enumerating files in root directory:"));
276 TestDirEnumHelper(dir
, wxDIR_FILES
);
278 wxPuts(_T("Enumerating files including hidden in root directory:"));
279 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
281 wxPuts(_T("Enumerating files in non existing directory:"));
282 wxDir
dirNo(_T("nosuchdir"));
283 TestDirEnumHelper(dirNo
);
286 class DirPrintTraverser
: public wxDirTraverser
289 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
291 return wxDIR_CONTINUE
;
294 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
296 wxString path
, name
, ext
;
297 wxSplitPath(dirname
, &path
, &name
, &ext
);
300 name
<< _T('.') << ext
;
303 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
305 if ( wxIsPathSeparator(*p
) )
309 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
311 return wxDIR_CONTINUE
;
315 static void TestDirTraverse()
317 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
321 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
322 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
325 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
326 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
329 // enum again with custom traverser
330 wxPuts(_T("Now enumerating directories:"));
332 DirPrintTraverser traverser
;
333 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
336 static void TestDirExists()
338 wxPuts(_T("*** Testing wxDir::Exists() ***"));
340 static const wxChar
*dirnames
[] =
343 #if defined(__WXMSW__)
346 _T("\\\\share\\file"),
350 _T("c:\\autoexec.bat"),
351 #elif defined(__UNIX__)
360 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
362 wxPrintf(_T("%-40s: %s\n"),
364 wxDir::Exists(dirnames
[n
]) ? _T("exists")
365 : _T("doesn't exist"));
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
377 #include "wx/dynlib.h"
379 static void TestDllLoad()
381 #if defined(__WXMSW__)
382 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
383 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
384 #elif defined(__UNIX__)
385 // weird: using just libc.so does *not* work!
386 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
387 static const wxChar
*FUNC_NAME
= _T("strlen");
389 #error "don't know how to test wxDllLoader on this platform"
392 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
394 wxDynamicLibrary
lib(LIB_NAME
);
395 if ( !lib
.IsLoaded() )
397 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
401 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
402 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
405 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
406 FUNC_NAME
, LIB_NAME
);
410 wxPrintf(_T("Calling %s dynamically loaded from %s "),
411 FUNC_NAME
, LIB_NAME
);
413 if ( pfnStrlen("foo") != 3 )
415 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
419 wxPuts(_T("... ok"));
424 static const wxChar
*FUNC_NAME_AW
= _T("lstrlen");
426 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
428 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
429 if ( !pfnStrlenAorW
)
431 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
432 FUNC_NAME_AW
, LIB_NAME
);
436 if ( pfnStrlenAorW(_T("foobar")) != 6 )
438 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
445 #if defined(__WXMSW__) || defined(__UNIX__)
447 static void TestDllListLoaded()
449 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
451 puts("\nLoaded modules:");
452 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
453 const size_t count
= dlls
.GetCount();
454 for ( size_t n
= 0; n
< count
; ++n
)
456 const wxDynamicLibraryDetails
& details
= dlls
[n
];
457 printf("%-45s", details
.GetPath().mb_str());
461 if ( details
.GetAddress(&addr
, &len
) )
463 printf(" %08lx:%08lx",
464 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
467 printf(" %s\n", details
.GetVersion().mb_str());
473 #endif // TEST_DYNLIB
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
481 #include "wx/utils.h"
483 static wxString
MyGetEnv(const wxString
& var
)
486 if ( !wxGetEnv(var
, &val
) )
489 val
= wxString(_T('\'')) + val
+ _T('\'');
494 static void TestEnvironment()
496 const wxChar
*var
= _T("wxTestVar");
498 wxPuts(_T("*** testing environment access functions ***"));
500 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
501 wxSetEnv(var
, _T("value for wxTestVar"));
502 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
503 wxSetEnv(var
, _T("another value"));
504 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
506 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
507 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
510 #endif // TEST_ENVIRON
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
518 #include "wx/utils.h"
520 static void TestExecute()
522 wxPuts(_T("*** testing wxExecute ***"));
525 #define COMMAND "cat -n ../../Makefile" // "echo hi"
526 #define SHELL_COMMAND "echo hi from shell"
527 #define REDIRECT_COMMAND COMMAND // "date"
528 #elif defined(__WXMSW__)
529 #define COMMAND "command.com /c echo hi"
530 #define SHELL_COMMAND "echo hi"
531 #define REDIRECT_COMMAND COMMAND
533 #error "no command to exec"
536 wxPrintf(_T("Testing wxShell: "));
538 if ( wxShell(_T(SHELL_COMMAND
)) )
541 wxPuts(_T("ERROR."));
543 wxPrintf(_T("Testing wxExecute: "));
545 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
548 wxPuts(_T("ERROR."));
550 #if 0 // no, it doesn't work (yet?)
551 wxPrintf(_T("Testing async wxExecute: "));
553 if ( wxExecute(COMMAND
) != 0 )
554 wxPuts(_T("Ok (command launched)."));
556 wxPuts(_T("ERROR."));
559 wxPrintf(_T("Testing wxExecute with redirection:\n"));
560 wxArrayString output
;
561 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
563 wxPuts(_T("ERROR."));
567 size_t count
= output
.GetCount();
568 for ( size_t n
= 0; n
< count
; n
++ )
570 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
577 #endif // TEST_EXECUTE
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
586 #include "wx/ffile.h"
587 #include "wx/textfile.h"
589 static void TestFileRead()
591 wxPuts(_T("*** wxFile read test ***"));
593 wxFile
file(_T("testdata.fc"));
594 if ( file
.IsOpened() )
596 wxPrintf(_T("File length: %lu\n"), file
.Length());
598 wxPuts(_T("File dump:\n----------"));
600 static const size_t len
= 1024;
604 size_t nRead
= file
.Read(buf
, len
);
605 if ( nRead
== (size_t)wxInvalidOffset
)
607 wxPrintf(_T("Failed to read the file."));
611 fwrite(buf
, nRead
, 1, stdout
);
617 wxPuts(_T("----------"));
621 wxPrintf(_T("ERROR: can't open test file.\n"));
624 wxPuts(wxEmptyString
);
627 static void TestTextFileRead()
629 wxPuts(_T("*** wxTextFile read test ***"));
631 wxTextFile
file(_T("testdata.fc"));
634 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
635 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
639 wxPuts(_T("\nDumping the entire file:"));
640 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
642 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
644 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
646 wxPuts(_T("\nAnd now backwards:"));
647 for ( s
= file
.GetLastLine();
648 file
.GetCurrentLine() != 0;
649 s
= file
.GetPrevLine() )
651 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
653 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
657 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
660 wxPuts(wxEmptyString
);
663 static void TestFileCopy()
665 wxPuts(_T("*** Testing wxCopyFile ***"));
667 static const wxChar
*filename1
= _T("testdata.fc");
668 static const wxChar
*filename2
= _T("test2");
669 if ( !wxCopyFile(filename1
, filename2
) )
671 wxPuts(_T("ERROR: failed to copy file"));
675 wxFFile
f1(filename1
, _T("rb")),
676 f2(filename2
, _T("rb"));
678 if ( !f1
.IsOpened() || !f2
.IsOpened() )
680 wxPuts(_T("ERROR: failed to open file(s)"));
685 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
687 wxPuts(_T("ERROR: failed to read file(s)"));
691 if ( (s1
.length() != s2
.length()) ||
692 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
694 wxPuts(_T("ERROR: copy error!"));
698 wxPuts(_T("File was copied ok."));
704 if ( !wxRemoveFile(filename2
) )
706 wxPuts(_T("ERROR: failed to remove the file"));
709 wxPuts(wxEmptyString
);
714 // ----------------------------------------------------------------------------
716 // ----------------------------------------------------------------------------
720 #include "wx/confbase.h"
721 #include "wx/fileconf.h"
723 static const struct FileConfTestData
725 const wxChar
*name
; // value name
726 const wxChar
*value
; // the value from the file
729 { _T("value1"), _T("one") },
730 { _T("value2"), _T("two") },
731 { _T("novalue"), _T("default") },
734 static void TestFileConfRead()
736 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
738 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
739 _T("testdata.fc"), wxEmptyString
,
740 wxCONFIG_USE_RELATIVE_PATH
);
742 // test simple reading
743 wxPuts(_T("\nReading config file:"));
744 wxString
defValue(_T("default")), value
;
745 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
747 const FileConfTestData
& data
= fcTestData
[n
];
748 value
= fileconf
.Read(data
.name
, defValue
);
749 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
750 if ( value
== data
.value
)
756 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
760 // test enumerating the entries
761 wxPuts(_T("\nEnumerating all root entries:"));
764 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
767 wxPrintf(_T("\t%s = %s\n"),
769 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
771 cont
= fileconf
.GetNextEntry(name
, dummy
);
774 static const wxChar
*testEntry
= _T("TestEntry");
775 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
776 fileconf
.Write(testEntry
, _T("A value"));
777 fileconf
.DeleteEntry(testEntry
);
778 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
781 #endif // TEST_FILECONF
783 // ----------------------------------------------------------------------------
785 // ----------------------------------------------------------------------------
789 #include "wx/filename.h"
792 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
796 wxString full
= fn
.GetFullPath();
798 wxString vol
, path
, name
, ext
;
799 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
801 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
802 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
804 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
805 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
806 path
.c_str(), name
.c_str(), ext
.c_str());
808 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
809 wxPrintf(_T("with volume: \t'%s'\n"),
810 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
811 wxPrintf(_T("with separator:\t'%s'\n"),
812 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
813 wxPrintf(_T("with both: \t'%s'\n"),
814 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
816 wxPuts(_T("The directories in the path are:"));
817 wxArrayString dirs
= fn
.GetDirs();
818 size_t count
= dirs
.GetCount();
819 for ( size_t n
= 0; n
< count
; n
++ )
821 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
826 static void TestFileNameTemp()
828 wxPuts(_T("*** testing wxFileName temp file creation ***"));
830 static const wxChar
*tmpprefixes
[] =
838 _T("/tmp/foo/bar"), // this one must be an error
842 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
844 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
847 // "error" is not in upper case because it may be ok
848 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
852 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
853 tmpprefixes
[n
], path
.c_str());
855 if ( !wxRemoveFile(path
) )
857 wxLogWarning(_T("Failed to remove temp file '%s'"),
864 static void TestFileNameDirManip()
866 // TODO: test AppendDir(), RemoveDir(), ...
869 static void TestFileNameComparison()
874 static void TestFileNameOperations()
879 static void TestFileNameCwd()
884 #endif // TEST_FILENAME
886 // ----------------------------------------------------------------------------
887 // wxFileName time functions
888 // ----------------------------------------------------------------------------
892 #include <wx/filename.h>
893 #include <wx/datetime.h>
895 static void TestFileGetTimes()
897 wxFileName
fn(_T("testdata.fc"));
899 wxDateTime dtAccess
, dtMod
, dtCreate
;
900 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
902 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
906 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
908 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
909 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
910 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
911 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
916 static void TestFileSetTimes()
918 wxFileName
fn(_T("testdata.fc"));
922 wxPrintf(_T("ERROR: Touch() failed.\n"));
927 #endif // TEST_FILETIME
929 // ----------------------------------------------------------------------------
931 // ----------------------------------------------------------------------------
936 #include "wx/utils.h" // for wxSetEnv
938 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
940 // find the name of the language from its value
941 static const wxChar
*GetLangName(int lang
)
943 static const wxChar
*languageNames
[] =
953 _T("ARABIC_ALGERIA"),
954 _T("ARABIC_BAHRAIN"),
959 _T("ARABIC_LEBANON"),
961 _T("ARABIC_MOROCCO"),
964 _T("ARABIC_SAUDI_ARABIA"),
967 _T("ARABIC_TUNISIA"),
974 _T("AZERI_CYRILLIC"),
989 _T("CHINESE_SIMPLIFIED"),
990 _T("CHINESE_TRADITIONAL"),
991 _T("CHINESE_HONGKONG"),
993 _T("CHINESE_SINGAPORE"),
994 _T("CHINESE_TAIWAN"),
1000 _T("DUTCH_BELGIAN"),
1004 _T("ENGLISH_AUSTRALIA"),
1005 _T("ENGLISH_BELIZE"),
1006 _T("ENGLISH_BOTSWANA"),
1007 _T("ENGLISH_CANADA"),
1008 _T("ENGLISH_CARIBBEAN"),
1009 _T("ENGLISH_DENMARK"),
1011 _T("ENGLISH_JAMAICA"),
1012 _T("ENGLISH_NEW_ZEALAND"),
1013 _T("ENGLISH_PHILIPPINES"),
1014 _T("ENGLISH_SOUTH_AFRICA"),
1015 _T("ENGLISH_TRINIDAD"),
1016 _T("ENGLISH_ZIMBABWE"),
1024 _T("FRENCH_BELGIAN"),
1025 _T("FRENCH_CANADIAN"),
1026 _T("FRENCH_LUXEMBOURG"),
1027 _T("FRENCH_MONACO"),
1033 _T("GERMAN_AUSTRIAN"),
1034 _T("GERMAN_BELGIUM"),
1035 _T("GERMAN_LIECHTENSTEIN"),
1036 _T("GERMAN_LUXEMBOURG"),
1054 _T("ITALIAN_SWISS"),
1059 _T("KASHMIRI_INDIA"),
1077 _T("MALAY_BRUNEI_DARUSSALAM"),
1078 _T("MALAY_MALAYSIA"),
1088 _T("NORWEGIAN_BOKMAL"),
1089 _T("NORWEGIAN_NYNORSK"),
1096 _T("PORTUGUESE_BRAZILIAN"),
1099 _T("RHAETO_ROMANCE"),
1102 _T("RUSSIAN_UKRAINE"),
1108 _T("SERBIAN_CYRILLIC"),
1109 _T("SERBIAN_LATIN"),
1110 _T("SERBO_CROATIAN"),
1121 _T("SPANISH_ARGENTINA"),
1122 _T("SPANISH_BOLIVIA"),
1123 _T("SPANISH_CHILE"),
1124 _T("SPANISH_COLOMBIA"),
1125 _T("SPANISH_COSTA_RICA"),
1126 _T("SPANISH_DOMINICAN_REPUBLIC"),
1127 _T("SPANISH_ECUADOR"),
1128 _T("SPANISH_EL_SALVADOR"),
1129 _T("SPANISH_GUATEMALA"),
1130 _T("SPANISH_HONDURAS"),
1131 _T("SPANISH_MEXICAN"),
1132 _T("SPANISH_MODERN"),
1133 _T("SPANISH_NICARAGUA"),
1134 _T("SPANISH_PANAMA"),
1135 _T("SPANISH_PARAGUAY"),
1137 _T("SPANISH_PUERTO_RICO"),
1138 _T("SPANISH_URUGUAY"),
1140 _T("SPANISH_VENEZUELA"),
1144 _T("SWEDISH_FINLAND"),
1162 _T("URDU_PAKISTAN"),
1164 _T("UZBEK_CYRILLIC"),
1177 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1178 return languageNames
[lang
];
1180 return _T("INVALID");
1183 static void TestDefaultLang()
1185 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1187 static const wxChar
*langStrings
[] =
1189 NULL
, // system default
1196 _T("de_DE.iso88591"),
1198 _T("?"), // invalid lang spec
1199 _T("klingonese"), // I bet on some systems it does exist...
1202 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1203 wxLocale::GetSystemEncodingName().c_str(),
1204 wxLocale::GetSystemEncoding());
1206 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1208 const wxChar
*langStr
= langStrings
[n
];
1211 // FIXME: this doesn't do anything at all under Windows, we need
1212 // to create a new wxLocale!
1213 wxSetEnv(_T("LC_ALL"), langStr
);
1216 int lang
= gs_localeDefault
.GetSystemLanguage();
1217 wxPrintf(_T("Locale for '%s' is %s.\n"),
1218 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1222 #endif // TEST_LOCALE
1224 // ----------------------------------------------------------------------------
1226 // ----------------------------------------------------------------------------
1230 #include "wx/mimetype.h"
1232 static void TestMimeEnum()
1234 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1236 wxArrayString mimetypes
;
1238 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1240 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1245 for ( size_t n
= 0; n
< count
; n
++ )
1247 wxFileType
*filetype
=
1248 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1251 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1252 mimetypes
[n
].c_str());
1256 filetype
->GetDescription(&desc
);
1257 filetype
->GetExtensions(exts
);
1259 filetype
->GetIcon(NULL
);
1262 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1265 extsAll
<< _T(", ");
1269 wxPrintf(_T("\t%s: %s (%s)\n"),
1270 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1273 wxPuts(wxEmptyString
);
1276 static void TestMimeOverride()
1278 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1280 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1281 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1283 if ( wxFile::Exists(mailcap
) )
1284 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1286 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1288 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1291 if ( wxFile::Exists(mimetypes
) )
1292 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1294 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1296 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1299 wxPuts(wxEmptyString
);
1302 static void TestMimeFilename()
1304 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1306 static const wxChar
*filenames
[] =
1314 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1316 const wxString fname
= filenames
[n
];
1317 wxString ext
= fname
.AfterLast(_T('.'));
1318 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1321 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1326 if ( !ft
->GetDescription(&desc
) )
1327 desc
= _T("<no description>");
1330 if ( !ft
->GetOpenCommand(&cmd
,
1331 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1332 cmd
= _T("<no command available>");
1334 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1336 wxPrintf(_T("To open %s (%s) do %s.\n"),
1337 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1343 wxPuts(wxEmptyString
);
1346 static void TestMimeAssociate()
1348 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1350 wxFileTypeInfo
ftInfo(
1351 _T("application/x-xyz"),
1352 _T("xyzview '%s'"), // open cmd
1353 _T(""), // print cmd
1354 _T("XYZ File"), // description
1355 _T(".xyz"), // extensions
1356 NULL
// end of extensions
1358 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1360 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1363 wxPuts(_T("ERROR: failed to create association!"));
1367 // TODO: read it back
1371 wxPuts(wxEmptyString
);
1376 // ----------------------------------------------------------------------------
1377 // misc information functions
1378 // ----------------------------------------------------------------------------
1380 #ifdef TEST_INFO_FUNCTIONS
1382 #include "wx/utils.h"
1384 static void TestDiskInfo()
1386 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1390 wxChar pathname
[128];
1391 wxPrintf(_T("\nEnter a directory name: "));
1392 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1395 // kill the last '\n'
1396 pathname
[wxStrlen(pathname
) - 1] = 0;
1398 wxLongLong total
, free
;
1399 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1401 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1405 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1406 (total
/ 1024).ToString().c_str(),
1407 (free
/ 1024).ToString().c_str(),
1413 static void TestOsInfo()
1415 wxPuts(_T("*** Testing OS info functions ***\n"));
1418 wxGetOsVersion(&major
, &minor
);
1419 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1420 wxGetOsDescription().c_str(), major
, minor
);
1422 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
1424 wxPrintf(_T("Host name is %s (%s).\n"),
1425 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1427 wxPuts(wxEmptyString
);
1430 static void TestUserInfo()
1432 wxPuts(_T("*** Testing user info functions ***\n"));
1434 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1435 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1436 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1437 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1439 wxPuts(wxEmptyString
);
1442 #endif // TEST_INFO_FUNCTIONS
1444 // ----------------------------------------------------------------------------
1446 // ----------------------------------------------------------------------------
1448 #ifdef TEST_PATHLIST
1451 #define CMD_IN_PATH _T("ls")
1453 #define CMD_IN_PATH _T("command.com")
1456 static void TestPathList()
1458 wxPuts(_T("*** Testing wxPathList ***\n"));
1460 wxPathList pathlist
;
1461 pathlist
.AddEnvList(_T("PATH"));
1462 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1465 wxPrintf(_T("ERROR: command not found in the path.\n"));
1469 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1473 #endif // TEST_PATHLIST
1475 // ----------------------------------------------------------------------------
1476 // regular expressions
1477 // ----------------------------------------------------------------------------
1481 #include "wx/regex.h"
1483 static void TestRegExInteractive()
1485 wxPuts(_T("*** Testing RE interactively ***"));
1489 wxChar pattern
[128];
1490 wxPrintf(_T("\nEnter a pattern: "));
1491 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1494 // kill the last '\n'
1495 pattern
[wxStrlen(pattern
) - 1] = 0;
1498 if ( !re
.Compile(pattern
) )
1506 wxPrintf(_T("Enter text to match: "));
1507 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1510 // kill the last '\n'
1511 text
[wxStrlen(text
) - 1] = 0;
1513 if ( !re
.Matches(text
) )
1515 wxPrintf(_T("No match.\n"));
1519 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1522 for ( size_t n
= 1; ; n
++ )
1524 if ( !re
.GetMatch(&start
, &len
, n
) )
1529 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1530 n
, wxString(text
+ start
, len
).c_str());
1537 #endif // TEST_REGEX
1539 // ----------------------------------------------------------------------------
1541 // ----------------------------------------------------------------------------
1551 static void TestDbOpen()
1559 // ----------------------------------------------------------------------------
1561 // ----------------------------------------------------------------------------
1564 NB: this stuff was taken from the glibc test suite and modified to build
1565 in wxWidgets: if I read the copyright below properly, this shouldn't
1571 #ifdef wxTEST_PRINTF
1572 // use our functions from wxchar.cpp
1576 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1577 // in the tests below
1578 int wxPrintf( const wxChar
*format
, ... );
1579 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1582 #include "wx/longlong.h"
1586 static void rfg1 (void);
1587 static void rfg2 (void);
1591 fmtchk (const wxChar
*fmt
)
1593 (void) wxPrintf(_T("%s:\t`"), fmt
);
1594 (void) wxPrintf(fmt
, 0x12);
1595 (void) wxPrintf(_T("'\n"));
1599 fmtst1chk (const wxChar
*fmt
)
1601 (void) wxPrintf(_T("%s:\t`"), fmt
);
1602 (void) wxPrintf(fmt
, 4, 0x12);
1603 (void) wxPrintf(_T("'\n"));
1607 fmtst2chk (const wxChar
*fmt
)
1609 (void) wxPrintf(_T("%s:\t`"), fmt
);
1610 (void) wxPrintf(fmt
, 4, 4, 0x12);
1611 (void) wxPrintf(_T("'\n"));
1614 /* This page is covered by the following copyright: */
1616 /* (C) Copyright C E Chew
1618 * Feel free to copy, use and distribute this software provided:
1620 * 1. you do not pretend that you wrote it
1621 * 2. you leave this copyright notice intact.
1625 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1632 /* Formatted Output Test
1634 * This exercises the output formatting code.
1637 wxChar
*PointerNull
= NULL
;
1644 wxChar
*prefix
= buf
;
1647 wxPuts(_T("\nFormatted output test"));
1648 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1649 wxStrcpy(prefix
, _T("%"));
1650 for (i
= 0; i
< 2; i
++) {
1651 for (j
= 0; j
< 2; j
++) {
1652 for (k
= 0; k
< 2; k
++) {
1653 for (l
= 0; l
< 2; l
++) {
1654 wxStrcpy(prefix
, _T("%"));
1655 if (i
== 0) wxStrcat(prefix
, _T("-"));
1656 if (j
== 0) wxStrcat(prefix
, _T("+"));
1657 if (k
== 0) wxStrcat(prefix
, _T("#"));
1658 if (l
== 0) wxStrcat(prefix
, _T("0"));
1659 wxPrintf(_T("%5s |"), prefix
);
1660 wxStrcpy(tp
, prefix
);
1661 wxStrcat(tp
, _T("6d |"));
1663 wxStrcpy(tp
, prefix
);
1664 wxStrcat(tp
, _T("6o |"));
1666 wxStrcpy(tp
, prefix
);
1667 wxStrcat(tp
, _T("6x |"));
1669 wxStrcpy(tp
, prefix
);
1670 wxStrcat(tp
, _T("6X |"));
1672 wxStrcpy(tp
, prefix
);
1673 wxStrcat(tp
, _T("6u |"));
1680 wxPrintf(_T("%10s\n"), PointerNull
);
1681 wxPrintf(_T("%-10s\n"), PointerNull
);
1684 static void TestPrintf()
1686 static wxChar shortstr
[] = _T("Hi, Z.");
1687 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1688 I am ready for my first lesson today.");
1690 wxString test_format
;
1694 fmtchk(_T("%4.4x"));
1695 fmtchk(_T("%04.4x"));
1696 fmtchk(_T("%4.3x"));
1697 fmtchk(_T("%04.3x"));
1699 fmtst1chk(_T("%.*x"));
1700 fmtst1chk(_T("%0*x"));
1701 fmtst2chk(_T("%*.*x"));
1702 fmtst2chk(_T("%0*.*x"));
1704 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1705 wxPrintf(bad_format
.c_str());
1706 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1708 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1709 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1710 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1711 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1712 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1713 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1714 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1715 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1716 wxPrintf(test_format
.c_str(), -123456);
1717 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1718 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1720 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1721 wxPrintf(test_format
.c_str(), shortstr
);
1722 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1723 wxPrintf(test_format
.c_str(), shortstr
);
1724 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1725 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1726 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1727 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1729 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1730 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1731 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1732 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1733 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1734 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1735 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1736 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1737 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1738 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1739 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1740 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1742 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1743 wxPrintf (_T(" %6.5f\n"), .1);
1744 wxPrintf (_T("x%5.4fx\n"), .5);
1746 wxPrintf (_T("%#03x\n"), 1);
1748 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1754 while (niter
-- != 0)
1755 wxPrintf (_T("%.17e\n"), d
/ 2);
1760 // Open Watcom cause compiler error here
1761 // Error! E173: col(24) floating-point constant too small to represent
1762 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1765 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1766 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1767 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1768 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1769 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1770 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1771 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1772 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1773 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1774 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1779 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1781 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1782 rc
, WXSIZEOF(buf
), buf
);
1785 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1786 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1792 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1793 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1794 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1795 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1796 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1797 wxPrintf (_T("%g should be 10\n"), 10.0);
1798 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1802 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1808 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1810 result
|= wxStrcmp (buf
,
1811 _T("onetwo three "));
1813 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1820 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1822 // for some reason below line fails under Borland
1823 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1826 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1829 wxPuts (_T("\tFAILED"));
1831 wxUnusedVar(result
);
1832 wxPuts (wxEmptyString
);
1834 #endif // wxLongLong_t
1836 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1837 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1839 wxPuts (_T("--- Should be no further output. ---"));
1848 memset (bytes
, '\xff', sizeof bytes
);
1849 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1850 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1851 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1853 wxPuts (_T("%hhn overwrite more bytes"));
1858 wxPuts (_T("%hhn wrote incorrect value"));
1870 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1871 if (wxStrcmp (buf
, _T(" ")) != 0)
1872 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1873 wxSprintf (buf
, _T("%5.f"), 33.3);
1874 if (wxStrcmp (buf
, _T(" 33")) != 0)
1875 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1876 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1877 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1878 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1879 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1880 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1881 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1882 wxSprintf (buf
, _T("%.g"), 33.3);
1883 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1884 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1885 wxSprintf (buf
, _T("%.G"), 33.3);
1886 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1887 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1895 wxString test_format
;
1898 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
1899 if (wxStrcmp (buf
, _T("3")) != 0)
1900 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1902 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
1903 if (wxStrcmp (buf
, _T("3")) != 0)
1904 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1906 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
1907 if (wxStrcmp (buf
, _T(" 3")) != 0)
1908 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
1910 test_format
= _T("%04.*o");
1911 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1912 if (wxStrcmp (buf
, _T(" 041")) != 0)
1913 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
1915 test_format
= _T("%09.*u");
1916 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1917 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
1918 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
1920 test_format
= _T("%04.*x");
1921 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1922 if (wxStrcmp (buf
, _T(" 021")) != 0)
1923 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1925 test_format
= _T("%04.*X");
1926 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1927 if (wxStrcmp (buf
, _T(" 021")) != 0)
1928 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1931 #endif // TEST_PRINTF
1933 // ----------------------------------------------------------------------------
1934 // registry and related stuff
1935 // ----------------------------------------------------------------------------
1937 // this is for MSW only
1940 #undef TEST_REGISTRY
1945 #include "wx/confbase.h"
1946 #include "wx/msw/regconf.h"
1949 static void TestRegConfWrite()
1951 wxConfig
*config
= new wxConfig(_T("myapp"));
1952 config
->SetPath(_T("/group1"));
1953 config
->Write(_T("entry1"), _T("foo"));
1954 config
->SetPath(_T("/group2"));
1955 config
->Write(_T("entry1"), _T("bar"));
1959 static void TestRegConfRead()
1961 wxConfig
*config
= new wxConfig(_T("myapp"));
1965 config
->SetPath(_T("/"));
1966 wxPuts(_T("Enumerating / subgroups:"));
1967 bool bCont
= config
->GetFirstGroup(str
, dummy
);
1971 bCont
= config
->GetNextGroup(str
, dummy
);
1975 #endif // TEST_REGCONF
1977 #ifdef TEST_REGISTRY
1979 #include "wx/msw/registry.h"
1981 // I chose this one because I liked its name, but it probably only exists under
1983 static const wxChar
*TESTKEY
=
1984 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
1986 static void TestRegistryRead()
1988 wxPuts(_T("*** testing registry reading ***"));
1990 wxRegKey
key(TESTKEY
);
1991 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
1994 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
1999 size_t nSubKeys
, nValues
;
2000 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2002 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2005 wxPrintf(_T("Enumerating values:\n"));
2009 bool cont
= key
.GetFirstValue(value
, dummy
);
2012 wxPrintf(_T("Value '%s': type "), value
.c_str());
2013 switch ( key
.GetValueType(value
) )
2015 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2016 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2017 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2018 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2019 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2020 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2021 default: wxPrintf(_T("other (unknown)")); break;
2024 wxPrintf(_T(", value = "));
2025 if ( key
.IsNumericValue(value
) )
2028 key
.QueryValue(value
, &val
);
2029 wxPrintf(_T("%ld"), val
);
2034 key
.QueryValue(value
, val
);
2035 wxPrintf(_T("'%s'"), val
.c_str());
2037 key
.QueryRawValue(value
, val
);
2038 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2043 cont
= key
.GetNextValue(value
, dummy
);
2047 static void TestRegistryAssociation()
2050 The second call to deleteself genertaes an error message, with a
2051 messagebox saying .flo is crucial to system operation, while the .ddf
2052 call also fails, but with no error message
2057 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2059 key
= _T("ddxf_auto_file") ;
2060 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2062 key
= _T("ddxf_auto_file") ;
2063 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2065 key
= _T("program,0") ;
2066 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2068 key
= _T("program \"%1\"") ;
2070 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2072 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2074 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2076 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2080 #endif // TEST_REGISTRY
2082 // ----------------------------------------------------------------------------
2084 // ----------------------------------------------------------------------------
2086 #ifdef TEST_SCOPEGUARD
2088 #include "wx/scopeguard.h"
2090 static void function0() { puts("function0()"); }
2091 static void function1(int n
) { printf("function1(%d)\n", n
); }
2092 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2096 void method0() { printf("method0()\n"); }
2097 void method1(int n
) { printf("method1(%d)\n", n
); }
2098 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2101 static void TestScopeGuard()
2103 wxON_BLOCK_EXIT0(function0
);
2104 wxON_BLOCK_EXIT1(function1
, 17);
2105 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2108 wxON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
2109 wxON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
2110 wxON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
2112 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2113 dismissed
.Dismiss();
2118 // ----------------------------------------------------------------------------
2120 // ----------------------------------------------------------------------------
2124 #include "wx/socket.h"
2125 #include "wx/protocol/protocol.h"
2126 #include "wx/protocol/http.h"
2128 static void TestSocketServer()
2130 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2132 static const int PORT
= 3000;
2137 wxSocketServer
*server
= new wxSocketServer(addr
);
2138 if ( !server
->Ok() )
2140 wxPuts(_T("ERROR: failed to bind"));
2148 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2150 wxSocketBase
*socket
= server
->Accept();
2153 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2157 wxPuts(_T("Server: got a client."));
2159 server
->SetTimeout(60); // 1 min
2162 while ( !close
&& socket
->IsConnected() )
2165 wxChar ch
= _T('\0');
2168 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2170 // don't log error if the client just close the connection
2171 if ( socket
->IsConnected() )
2173 wxPuts(_T("ERROR: in wxSocket::Read."));
2193 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2194 if ( s
== _T("close") )
2196 wxPuts(_T("Closing connection"));
2200 else if ( s
== _T("quit") )
2205 wxPuts(_T("Shutting down the server"));
2207 else // not a special command
2209 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2210 socket
->Write("\r\n", 2);
2211 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2217 wxPuts(_T("Server: lost a client unexpectedly."));
2223 // same as "delete server" but is consistent with GUI programs
2227 static void TestSocketClient()
2229 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2231 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2234 addr
.Hostname(hostname
);
2237 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2239 wxSocketClient client
;
2240 if ( !client
.Connect(addr
) )
2242 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2246 wxPrintf(_T("--- Connected to %s:%u...\n"),
2247 addr
.Hostname().c_str(), addr
.Service());
2251 // could use simply "GET" here I suppose
2253 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2254 client
.Write(cmdGet
, cmdGet
.length());
2255 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2256 MakePrintable(cmdGet
).c_str());
2257 client
.Read(buf
, WXSIZEOF(buf
));
2258 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2262 #endif // TEST_SOCKETS
2264 // ----------------------------------------------------------------------------
2266 // ----------------------------------------------------------------------------
2270 #include "wx/protocol/ftp.h"
2274 #define FTP_ANONYMOUS
2276 #ifdef FTP_ANONYMOUS
2277 static const wxChar
*directory
= _T("/pub");
2278 static const wxChar
*filename
= _T("welcome.msg");
2280 static const wxChar
*directory
= _T("/etc");
2281 static const wxChar
*filename
= _T("issue");
2284 static bool TestFtpConnect()
2286 wxPuts(_T("*** Testing FTP connect ***"));
2288 #ifdef FTP_ANONYMOUS
2289 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2291 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2292 #else // !FTP_ANONYMOUS
2293 static const wxChar
*hostname
= "localhost";
2296 wxFgets(user
, WXSIZEOF(user
), stdin
);
2297 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2300 wxChar password
[256];
2301 wxPrintf(_T("Password for %s: "), password
);
2302 wxFgets(password
, WXSIZEOF(password
), stdin
);
2303 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2304 ftp
.SetPassword(password
);
2306 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2307 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2309 if ( !ftp
.Connect(hostname
) )
2311 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2317 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2318 hostname
, ftp
.Pwd().c_str());
2325 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2326 static void TestFtpWuFtpd()
2329 static const wxChar
*hostname
= _T("ftp.eudora.com");
2330 if ( !ftp
.Connect(hostname
) )
2332 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2336 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2337 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2340 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2344 size_t size
= in
->GetSize();
2345 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2347 wxChar
*data
= new wxChar
[size
];
2348 if ( !in
->Read(data
, size
) )
2350 wxPuts(_T("ERROR: read error"));
2354 wxPrintf(_T("Successfully retrieved the file.\n"));
2363 static void TestFtpList()
2365 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2368 if ( !ftp
.ChDir(directory
) )
2370 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2373 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2375 // test NLIST and LIST
2376 wxArrayString files
;
2377 if ( !ftp
.GetFilesList(files
) )
2379 wxPuts(_T("ERROR: failed to get NLIST of files"));
2383 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2384 size_t count
= files
.GetCount();
2385 for ( size_t n
= 0; n
< count
; n
++ )
2387 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2389 wxPuts(_T("End of the file list"));
2392 if ( !ftp
.GetDirList(files
) )
2394 wxPuts(_T("ERROR: failed to get LIST of files"));
2398 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2399 size_t count
= files
.GetCount();
2400 for ( size_t n
= 0; n
< count
; n
++ )
2402 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2404 wxPuts(_T("End of the file list"));
2407 if ( !ftp
.ChDir(_T("..")) )
2409 wxPuts(_T("ERROR: failed to cd to .."));
2412 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2415 static void TestFtpDownload()
2417 wxPuts(_T("*** Testing wxFTP download ***\n"));
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
);
2431 wxChar
*data
= new wxChar
[size
];
2432 if ( !in
->Read(data
, size
) )
2434 wxPuts(_T("ERROR: read error"));
2438 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2446 static void TestFtpFileSize()
2448 wxPuts(_T("*** Testing FTP SIZE command ***"));
2450 if ( !ftp
.ChDir(directory
) )
2452 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2455 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2457 if ( ftp
.FileExists(filename
) )
2459 int size
= ftp
.GetFileSize(filename
);
2461 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2463 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2467 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2471 static void TestFtpMisc()
2473 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2475 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2477 wxPuts(_T("ERROR: STAT failed"));
2481 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2484 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2486 wxPuts(_T("ERROR: HELP SITE failed"));
2490 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2491 ftp
.GetLastResult().c_str());
2495 static void TestFtpInteractive()
2497 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2503 wxPrintf(_T("Enter FTP command: "));
2504 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2507 // kill the last '\n'
2508 buf
[wxStrlen(buf
) - 1] = 0;
2510 // special handling of LIST and NLST as they require data connection
2511 wxString
start(buf
, 4);
2513 if ( start
== _T("LIST") || start
== _T("NLST") )
2516 if ( wxStrlen(buf
) > 4 )
2519 wxArrayString files
;
2520 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2522 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2526 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2527 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2528 size_t count
= files
.GetCount();
2529 for ( size_t n
= 0; n
< count
; n
++ )
2531 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2533 wxPuts(_T("--- End of the file list"));
2538 wxChar ch
= ftp
.SendCommand(buf
);
2539 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2542 wxPrintf(_T(" (return code %c)"), ch
);
2545 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2549 wxPuts(_T("\n*** done ***"));
2552 static void TestFtpUpload()
2554 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2557 static const wxChar
*file1
= _T("test1");
2558 static const wxChar
*file2
= _T("test2");
2559 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2562 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2563 out
->Write("First hello", 11);
2567 // send a command to check the remote file
2568 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2570 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2574 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2575 file1
, ftp
.GetLastResult().c_str());
2578 out
= ftp
.GetOutputStream(file2
);
2581 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2582 out
->Write("Second hello", 12);
2589 // ----------------------------------------------------------------------------
2591 // ----------------------------------------------------------------------------
2593 #ifdef TEST_STACKWALKER
2595 #if wxUSE_STACKWALKER
2597 #include "wx/stackwalk.h"
2599 class StackDump
: public wxStackWalker
2602 StackDump(const char *argv0
)
2603 : wxStackWalker(argv0
)
2607 virtual void Walk(size_t skip
= 1)
2609 wxPuts(_T("Stack dump:"));
2611 wxStackWalker::Walk(skip
);
2615 virtual void OnStackFrame(const wxStackFrame
& frame
)
2617 printf("[%2d] ", frame
.GetLevel());
2619 wxString name
= frame
.GetName();
2620 if ( !name
.empty() )
2622 printf("%-20.40s", name
.mb_str());
2626 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2629 if ( frame
.HasSourceLocation() )
2632 frame
.GetFileName().mb_str(),
2639 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2641 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2646 static void TestStackWalk(const char *argv0
)
2648 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2650 StackDump
dump(argv0
);
2654 #endif // wxUSE_STACKWALKER
2656 #endif // TEST_STACKWALKER
2658 // ----------------------------------------------------------------------------
2660 // ----------------------------------------------------------------------------
2662 #ifdef TEST_STDPATHS
2664 #include "wx/stdpaths.h"
2666 static void TestStandardPaths()
2668 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2670 wxTheApp
->SetAppName(_T("console"));
2672 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2673 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2674 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2675 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2676 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2677 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2678 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2679 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2682 #endif // TEST_STDPATHS
2684 // ----------------------------------------------------------------------------
2686 // ----------------------------------------------------------------------------
2690 #include "wx/wfstream.h"
2691 #include "wx/mstream.h"
2693 static void TestFileStream()
2695 wxPuts(_T("*** Testing wxFileInputStream ***"));
2697 static const wxString filename
= _T("testdata.fs");
2699 wxFileOutputStream
fsOut(filename
);
2700 fsOut
.Write("foo", 3);
2703 wxFileInputStream
fsIn(filename
);
2704 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2705 while ( !fsIn
.Eof() )
2707 wxPutchar(fsIn
.GetC());
2710 if ( !wxRemoveFile(filename
) )
2712 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2715 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2718 static void TestMemoryStream()
2720 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2722 wxMemoryOutputStream memOutStream
;
2723 wxPrintf(_T("Initially out stream offset: %lu\n"),
2724 (unsigned long)memOutStream
.TellO());
2726 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2728 memOutStream
.PutC(*p
);
2731 wxPrintf(_T("Final out stream offset: %lu\n"),
2732 (unsigned long)memOutStream
.TellO());
2734 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2737 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2739 wxMemoryInputStream
memInpStream(buf
, len
);
2740 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2741 while ( !memInpStream
.Eof() )
2743 wxPutchar(memInpStream
.GetC());
2746 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2749 #endif // TEST_STREAMS
2751 // ----------------------------------------------------------------------------
2753 // ----------------------------------------------------------------------------
2757 #include "wx/stopwatch.h"
2758 #include "wx/utils.h"
2760 static void TestStopWatch()
2762 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2766 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2769 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2771 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2775 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2778 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2781 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2784 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2787 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2790 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2791 for ( size_t n
= 0; n
< 70; n
++ )
2795 for ( size_t m
= 0; m
< 100000; m
++ )
2797 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2799 wxPuts(_T("\ntime is negative - ERROR!"));
2807 wxPuts(_T(", ok."));
2810 #endif // TEST_TIMER
2812 // ----------------------------------------------------------------------------
2814 // ----------------------------------------------------------------------------
2818 #include "wx/vcard.h"
2820 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2823 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2826 wxPrintf(_T("%s%s"),
2827 wxString(_T('\t'), level
).c_str(),
2828 vcObj
->GetName().c_str());
2831 switch ( vcObj
->GetType() )
2833 case wxVCardObject::String
:
2834 case wxVCardObject::UString
:
2837 vcObj
->GetValue(&val
);
2838 value
<< _T('"') << val
<< _T('"');
2842 case wxVCardObject::Int
:
2845 vcObj
->GetValue(&i
);
2846 value
.Printf(_T("%u"), i
);
2850 case wxVCardObject::Long
:
2853 vcObj
->GetValue(&l
);
2854 value
.Printf(_T("%lu"), l
);
2858 case wxVCardObject::None
:
2861 case wxVCardObject::Object
:
2862 value
= _T("<node>");
2866 value
= _T("<unknown value type>");
2870 wxPrintf(_T(" = %s"), value
.c_str());
2873 DumpVObject(level
+ 1, *vcObj
);
2876 vcObj
= vcard
.GetNextProp(&cookie
);
2880 static void DumpVCardAddresses(const wxVCard
& vcard
)
2882 wxPuts(_T("\nShowing all addresses from vCard:\n"));
2886 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
2890 int flags
= addr
->GetFlags();
2891 if ( flags
& wxVCardAddress::Domestic
)
2893 flagsStr
<< _T("domestic ");
2895 if ( flags
& wxVCardAddress::Intl
)
2897 flagsStr
<< _T("international ");
2899 if ( flags
& wxVCardAddress::Postal
)
2901 flagsStr
<< _T("postal ");
2903 if ( flags
& wxVCardAddress::Parcel
)
2905 flagsStr
<< _T("parcel ");
2907 if ( flags
& wxVCardAddress::Home
)
2909 flagsStr
<< _T("home ");
2911 if ( flags
& wxVCardAddress::Work
)
2913 flagsStr
<< _T("work ");
2916 wxPrintf(_T("Address %u:\n")
2918 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
2921 addr
->GetPostOffice().c_str(),
2922 addr
->GetExtAddress().c_str(),
2923 addr
->GetStreet().c_str(),
2924 addr
->GetLocality().c_str(),
2925 addr
->GetRegion().c_str(),
2926 addr
->GetPostalCode().c_str(),
2927 addr
->GetCountry().c_str()
2931 addr
= vcard
.GetNextAddress(&cookie
);
2935 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
2937 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
2941 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
2945 int flags
= phone
->GetFlags();
2946 if ( flags
& wxVCardPhoneNumber::Voice
)
2948 flagsStr
<< _T("voice ");
2950 if ( flags
& wxVCardPhoneNumber::Fax
)
2952 flagsStr
<< _T("fax ");
2954 if ( flags
& wxVCardPhoneNumber::Cellular
)
2956 flagsStr
<< _T("cellular ");
2958 if ( flags
& wxVCardPhoneNumber::Modem
)
2960 flagsStr
<< _T("modem ");
2962 if ( flags
& wxVCardPhoneNumber::Home
)
2964 flagsStr
<< _T("home ");
2966 if ( flags
& wxVCardPhoneNumber::Work
)
2968 flagsStr
<< _T("work ");
2971 wxPrintf(_T("Phone number %u:\n")
2976 phone
->GetNumber().c_str()
2980 phone
= vcard
.GetNextPhoneNumber(&cookie
);
2984 static void TestVCardRead()
2986 wxPuts(_T("*** Testing wxVCard reading ***\n"));
2988 wxVCard
vcard(_T("vcard.vcf"));
2989 if ( !vcard
.IsOk() )
2991 wxPuts(_T("ERROR: couldn't load vCard."));
2995 // read individual vCard properties
2996 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3000 vcObj
->GetValue(&value
);
3005 value
= _T("<none>");
3008 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3011 if ( !vcard
.GetFullName(&value
) )
3013 value
= _T("<none>");
3016 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3018 // now show how to deal with multiply occurring properties
3019 DumpVCardAddresses(vcard
);
3020 DumpVCardPhoneNumbers(vcard
);
3022 // and finally show all
3023 wxPuts(_T("\nNow dumping the entire vCard:\n")
3024 "-----------------------------\n");
3026 DumpVObject(0, vcard
);
3030 static void TestVCardWrite()
3032 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3035 if ( !vcard
.IsOk() )
3037 wxPuts(_T("ERROR: couldn't create vCard."));
3042 vcard
.SetName("Zeitlin", "Vadim");
3043 vcard
.SetFullName("Vadim Zeitlin");
3044 vcard
.SetOrganization("wxWidgets", "R&D");
3046 // just dump the vCard back
3047 wxPuts(_T("Entire vCard follows:\n"));
3048 wxPuts(vcard
.Write());
3052 #endif // TEST_VCARD
3054 // ----------------------------------------------------------------------------
3056 // ----------------------------------------------------------------------------
3058 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3064 #include "wx/volume.h"
3066 static const wxChar
*volumeKinds
[] =
3072 _T("network volume"),
3076 static void TestFSVolume()
3078 wxPuts(_T("*** Testing wxFSVolume class ***"));
3080 wxArrayString volumes
= wxFSVolume::GetVolumes();
3081 size_t count
= volumes
.GetCount();
3085 wxPuts(_T("ERROR: no mounted volumes?"));
3089 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3091 for ( size_t n
= 0; n
< count
; n
++ )
3093 wxFSVolume
vol(volumes
[n
]);
3096 wxPuts(_T("ERROR: couldn't create volume"));
3100 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3102 vol
.GetDisplayName().c_str(),
3103 vol
.GetName().c_str(),
3104 volumeKinds
[vol
.GetKind()],
3105 vol
.IsWritable() ? _T("rw") : _T("ro"),
3106 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3111 #endif // TEST_VOLUME
3113 // ----------------------------------------------------------------------------
3114 // wide char and Unicode support
3115 // ----------------------------------------------------------------------------
3119 #include "wx/strconv.h"
3120 #include "wx/fontenc.h"
3121 #include "wx/encconv.h"
3122 #include "wx/buffer.h"
3124 static const unsigned char utf8koi8r
[] =
3126 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3127 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3128 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3129 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3130 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3131 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3132 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3135 static const unsigned char utf8iso8859_1
[] =
3137 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3138 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3139 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3140 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3141 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3144 static const unsigned char utf8Invalid
[] =
3146 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3147 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3148 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3149 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3153 static const struct Utf8Data
3155 const unsigned char *text
;
3157 const wxChar
*charset
;
3158 wxFontEncoding encoding
;
3161 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3162 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3163 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3166 static void TestUtf8()
3168 wxPuts(_T("*** Testing UTF8 support ***\n"));
3173 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3175 const Utf8Data
& u8d
= utf8data
[n
];
3176 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3177 WXSIZEOF(wbuf
)) == (size_t)-1 )
3179 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3183 wxCSConv
conv(u8d
.charset
);
3184 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3186 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3190 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3194 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3196 s
= _T("<< conversion failed >>");
3197 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3201 wxPuts(wxEmptyString
);
3204 static void TestEncodingConverter()
3206 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3208 // using wxEncodingConverter should give the same result as above
3211 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3212 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3214 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3218 wxEncodingConverter ec
;
3219 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3220 ec
.Convert(wbuf
, buf
);
3221 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3224 wxPuts(wxEmptyString
);
3227 #endif // TEST_WCHAR
3229 // ----------------------------------------------------------------------------
3231 // ----------------------------------------------------------------------------
3235 #include "wx/filesys.h"
3236 #include "wx/fs_zip.h"
3237 #include "wx/zipstrm.h"
3239 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3241 static void TestZipStreamRead()
3243 wxPuts(_T("*** Testing ZIP reading ***\n"));
3245 static const wxString filename
= _T("foo");
3246 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3247 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3249 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3250 while ( !istr
.Eof() )
3252 wxPutchar(istr
.GetC());
3256 wxPuts(_T("\n----- done ------"));
3259 static void DumpZipDirectory(wxFileSystem
& fs
,
3260 const wxString
& dir
,
3261 const wxString
& indent
)
3263 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3264 TESTFILE_ZIP
, dir
.c_str());
3265 wxString wildcard
= prefix
+ _T("/*");
3267 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3268 while ( !dirname
.empty() )
3270 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3272 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3277 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3279 DumpZipDirectory(fs
, dirname
,
3280 indent
+ wxString(_T(' '), 4));
3282 dirname
= fs
.FindNext();
3285 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3286 while ( !filename
.empty() )
3288 if ( !filename
.StartsWith(prefix
, &filename
) )
3290 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3295 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3297 filename
= fs
.FindNext();
3301 static void TestZipFileSystem()
3303 wxPuts(_T("*** Testing ZIP file system ***\n"));
3305 wxFileSystem::AddHandler(new wxZipFSHandler
);
3307 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3309 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3314 // ----------------------------------------------------------------------------
3316 // ----------------------------------------------------------------------------
3318 #ifdef TEST_DATETIME
3320 #include "wx/math.h"
3321 #include "wx/datetime.h"
3323 // this test miscellaneous static wxDateTime functions
3327 static void TestTimeStatic()
3329 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3331 // some info about the current date
3332 int year
= wxDateTime::GetCurrentYear();
3333 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3335 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3336 wxDateTime::GetNumberOfDays(year
));
3338 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3339 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3340 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3341 wxDateTime::GetMonthName(month
).c_str(),
3342 wxDateTime::GetNumberOfDays(month
));
3345 // test time zones stuff
3346 static void TestTimeZones()
3348 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3350 wxDateTime now
= wxDateTime::Now();
3352 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3353 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3354 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3355 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3356 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3357 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3359 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3361 wxDateTime::Tm tm
= now
.GetTm();
3362 if ( wxDateTime(tm
) != now
)
3364 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3365 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3369 // test some minimal support for the dates outside the standard range
3370 static void TestTimeRange()
3372 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3374 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3376 wxPrintf(_T("Unix epoch:\t%s\n"),
3377 wxDateTime(2440587.5).Format(fmt
).c_str());
3378 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3379 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3380 wxPrintf(_T("JDN 0: \t%s\n"),
3381 wxDateTime(0.0).Format(fmt
).c_str());
3382 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3383 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3384 wxPrintf(_T("May 29, 2099:\t%s\n"),
3385 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3388 // test DST calculations
3389 static void TestTimeDST()
3391 wxPuts(_T("\n*** wxDateTime DST test ***"));
3393 wxPrintf(_T("DST is%s in effect now.\n\n"),
3394 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3396 for ( int year
= 1990; year
< 2005; year
++ )
3398 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3400 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3401 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3407 #if TEST_INTERACTIVE
3409 static void TestDateTimeInteractive()
3411 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3417 wxPrintf(_T("Enter a date: "));
3418 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3421 // kill the last '\n'
3422 buf
[wxStrlen(buf
) - 1] = 0;
3425 const wxChar
*p
= dt
.ParseDate(buf
);
3428 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3434 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3437 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3438 dt
.Format(_T("%b %d, %Y")).c_str(),
3440 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3441 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3442 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3445 wxPuts(_T("\n*** done ***"));
3448 #endif // TEST_INTERACTIVE
3452 static void TestTimeMS()
3454 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3456 wxDateTime dt1
= wxDateTime::Now(),
3457 dt2
= wxDateTime::UNow();
3459 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3460 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3461 wxPrintf(_T("Dummy loop: "));
3462 for ( int i
= 0; i
< 6000; i
++ )
3464 //for ( int j = 0; j < 10; j++ )
3467 s
.Printf(_T("%g"), sqrt((float)i
));
3473 wxPuts(_T(", done"));
3476 dt2
= wxDateTime::UNow();
3477 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3479 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3481 wxPuts(_T("\n*** done ***"));
3484 static void TestTimeHolidays()
3486 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3488 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3489 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3490 dtEnd
= dtStart
.GetLastMonthDay();
3492 wxDateTimeArray hol
;
3493 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3495 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3497 wxPrintf(_T("All holidays between %s and %s:\n"),
3498 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3500 size_t count
= hol
.GetCount();
3501 for ( size_t n
= 0; n
< count
; n
++ )
3503 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3506 wxPuts(wxEmptyString
);
3509 static void TestTimeZoneBug()
3511 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3513 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3514 for ( int i
= 0; i
< 31; i
++ )
3516 wxPrintf(_T("Date %s: week day %s.\n"),
3517 date
.Format(_T("%d-%m-%Y")).c_str(),
3518 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3520 date
+= wxDateSpan::Day();
3523 wxPuts(wxEmptyString
);
3526 static void TestTimeSpanFormat()
3528 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3530 static const wxChar
*formats
[] =
3532 _T("(default) %H:%M:%S"),
3533 _T("%E weeks and %D days"),
3534 _T("%l milliseconds"),
3535 _T("(with ms) %H:%M:%S:%l"),
3536 _T("100%% of minutes is %M"), // test "%%"
3537 _T("%D days and %H hours"),
3538 _T("or also %S seconds"),
3541 wxTimeSpan
ts1(1, 2, 3, 4),
3543 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3545 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3546 ts1
.Format(formats
[n
]).c_str(),
3547 ts2
.Format(formats
[n
]).c_str());
3550 wxPuts(wxEmptyString
);
3555 #endif // TEST_DATETIME
3557 // ----------------------------------------------------------------------------
3558 // wxTextInput/OutputStream
3559 // ----------------------------------------------------------------------------
3561 #ifdef TEST_TEXTSTREAM
3563 #include "wx/txtstrm.h"
3564 #include "wx/wfstream.h"
3566 static void TestTextInputStream()
3568 wxPuts(_T("\n*** wxTextInputStream test ***"));
3570 wxString filename
= _T("testdata.fc");
3571 wxFileInputStream
fsIn(filename
);
3574 wxPuts(_T("ERROR: couldn't open file."));
3578 wxTextInputStream
tis(fsIn
);
3583 const wxString s
= tis
.ReadLine();
3585 // line could be non empty if the last line of the file isn't
3586 // terminated with EOL
3587 if ( fsIn
.Eof() && s
.empty() )
3590 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3595 #endif // TEST_TEXTSTREAM
3597 // ----------------------------------------------------------------------------
3599 // ----------------------------------------------------------------------------
3603 #include "wx/thread.h"
3605 static size_t gs_counter
= (size_t)-1;
3606 static wxCriticalSection gs_critsect
;
3607 static wxSemaphore gs_cond
;
3609 class MyJoinableThread
: public wxThread
3612 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3613 { m_n
= n
; Create(); }
3615 // thread execution starts here
3616 virtual ExitCode
Entry();
3622 wxThread::ExitCode
MyJoinableThread::Entry()
3624 unsigned long res
= 1;
3625 for ( size_t n
= 1; n
< m_n
; n
++ )
3629 // it's a loooong calculation :-)
3633 return (ExitCode
)res
;
3636 class MyDetachedThread
: public wxThread
3639 MyDetachedThread(size_t n
, wxChar ch
)
3643 m_cancelled
= false;
3648 // thread execution starts here
3649 virtual ExitCode
Entry();
3652 virtual void OnExit();
3655 size_t m_n
; // number of characters to write
3656 wxChar m_ch
; // character to write
3658 bool m_cancelled
; // false if we exit normally
3661 wxThread::ExitCode
MyDetachedThread::Entry()
3664 wxCriticalSectionLocker
lock(gs_critsect
);
3665 if ( gs_counter
== (size_t)-1 )
3671 for ( size_t n
= 0; n
< m_n
; n
++ )
3673 if ( TestDestroy() )
3683 wxThread::Sleep(100);
3689 void MyDetachedThread::OnExit()
3691 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3693 wxCriticalSectionLocker
lock(gs_critsect
);
3694 if ( !--gs_counter
&& !m_cancelled
)
3698 static void TestDetachedThreads()
3700 wxPuts(_T("\n*** Testing detached threads ***"));
3702 static const size_t nThreads
= 3;
3703 MyDetachedThread
*threads
[nThreads
];
3705 for ( n
= 0; n
< nThreads
; n
++ )
3707 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3710 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3711 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3713 for ( n
= 0; n
< nThreads
; n
++ )
3718 // wait until all threads terminate
3721 wxPuts(wxEmptyString
);
3724 static void TestJoinableThreads()
3726 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3728 // calc 10! in the background
3729 MyJoinableThread
thread(10);
3732 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3733 (unsigned long)thread
.Wait());
3736 static void TestThreadSuspend()
3738 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3740 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3744 // this is for this demo only, in a real life program we'd use another
3745 // condition variable which would be signaled from wxThread::Entry() to
3746 // tell us that the thread really started running - but here just wait a
3747 // bit and hope that it will be enough (the problem is, of course, that
3748 // the thread might still not run when we call Pause() which will result
3750 wxThread::Sleep(300);
3752 for ( size_t n
= 0; n
< 3; n
++ )
3756 wxPuts(_T("\nThread suspended"));
3759 // don't sleep but resume immediately the first time
3760 wxThread::Sleep(300);
3762 wxPuts(_T("Going to resume the thread"));
3767 wxPuts(_T("Waiting until it terminates now"));
3769 // wait until the thread terminates
3772 wxPuts(wxEmptyString
);
3775 static void TestThreadDelete()
3777 // As above, using Sleep() is only for testing here - we must use some
3778 // synchronisation object instead to ensure that the thread is still
3779 // running when we delete it - deleting a detached thread which already
3780 // terminated will lead to a crash!
3782 wxPuts(_T("\n*** Testing thread delete function ***"));
3784 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3788 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3790 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3794 wxThread::Sleep(300);
3798 wxPuts(_T("\nDeleted a running thread."));
3800 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3804 wxThread::Sleep(300);
3810 wxPuts(_T("\nDeleted a sleeping thread."));
3812 MyJoinableThread
thread3(20);
3817 wxPuts(_T("\nDeleted a joinable thread."));
3819 MyJoinableThread
thread4(2);
3822 wxThread::Sleep(300);
3826 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3828 wxPuts(wxEmptyString
);
3831 class MyWaitingThread
: public wxThread
3834 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3837 m_condition
= condition
;
3842 virtual ExitCode
Entry()
3844 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3849 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3853 m_condition
->Wait();
3856 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3864 wxCondition
*m_condition
;
3867 static void TestThreadConditions()
3870 wxCondition
condition(mutex
);
3872 // otherwise its difficult to understand which log messages pertain to
3874 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3875 // condition.GetId(), gs_cond.GetId());
3877 // create and launch threads
3878 MyWaitingThread
*threads
[10];
3881 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3883 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
3886 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3891 // wait until all threads run
3892 wxPuts(_T("Main thread is waiting for the other threads to start"));
3895 size_t nRunning
= 0;
3896 while ( nRunning
< WXSIZEOF(threads
) )
3902 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
3906 wxPuts(_T("Main thread: all threads started up."));
3909 wxThread::Sleep(500);
3912 // now wake one of them up
3913 wxPrintf(_T("Main thread: about to signal the condition.\n"));
3918 wxThread::Sleep(200);
3920 // wake all the (remaining) threads up, so that they can exit
3921 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
3923 condition
.Broadcast();
3925 // give them time to terminate (dirty!)
3926 wxThread::Sleep(500);
3929 #include "wx/utils.h"
3931 class MyExecThread
: public wxThread
3934 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
3940 virtual ExitCode
Entry()
3942 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
3949 static void TestThreadExec()
3951 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
3953 MyExecThread
thread(_T("true"));
3956 wxPrintf(_T("Main program exit code: %ld.\n"),
3957 wxExecute(_T("false"), wxEXEC_SYNC
));
3959 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
3963 #include "wx/datetime.h"
3965 class MySemaphoreThread
: public wxThread
3968 MySemaphoreThread(int i
, wxSemaphore
*sem
)
3969 : wxThread(wxTHREAD_JOINABLE
),
3976 virtual ExitCode
Entry()
3978 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
3979 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3983 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
3984 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3988 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
3989 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4001 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
4003 static void TestSemaphore()
4005 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4007 static const int SEM_LIMIT
= 3;
4009 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
4010 ArrayThreads threads
;
4012 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
4014 threads
.Add(new MySemaphoreThread(i
, &sem
));
4015 threads
.Last()->Run();
4018 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4025 #endif // TEST_THREADS
4027 // ----------------------------------------------------------------------------
4029 // ----------------------------------------------------------------------------
4031 #ifdef TEST_SNGLINST
4032 #include "wx/snglinst.h"
4033 #endif // TEST_SNGLINST
4035 int main(int argc
, char **argv
)
4038 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4043 for (n
= 0; n
< argc
; n
++ )
4045 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4046 wxArgv
[n
] = wxStrdup(warg
);
4051 #else // !wxUSE_UNICODE
4053 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4055 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4057 wxInitializer initializer
;
4060 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4065 #ifdef TEST_SNGLINST
4066 wxSingleInstanceChecker checker
;
4067 if ( checker
.Create(_T(".wxconsole.lock")) )
4069 if ( checker
.IsAnotherRunning() )
4071 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4076 // wait some time to give time to launch another instance
4077 wxPrintf(_T("Press \"Enter\" to continue..."));
4080 else // failed to create
4082 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4084 #endif // TEST_SNGLINST
4087 TestCmdLineConvert();
4089 #if wxUSE_CMDLINE_PARSER
4090 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4092 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
4093 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4094 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
4095 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
4097 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
4098 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
4099 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
4100 wxCMD_LINE_VAL_NUMBER
},
4101 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
4102 wxCMD_LINE_VAL_DATE
},
4104 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
4105 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4110 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4112 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4113 wxCMD_LINE_VAL_STRING
,
4114 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4116 switch ( parser
.Parse() )
4119 wxLogMessage(_T("Help was given, terminating."));
4123 ShowCmdLine(parser
);
4127 wxLogMessage(_T("Syntax error detected, aborting."));
4130 #endif // wxUSE_CMDLINE_PARSER
4132 #endif // TEST_CMDLINE
4144 TestDllListLoaded();
4145 #endif // TEST_DYNLIB
4149 #endif // TEST_ENVIRON
4153 #endif // TEST_EXECUTE
4155 #ifdef TEST_FILECONF
4157 #endif // TEST_FILECONF
4161 #endif // TEST_LOCALE
4164 wxPuts(_T("*** Testing wxLog ***"));
4167 for ( size_t n
= 0; n
< 8000; n
++ )
4169 s
<< (wxChar
)(_T('A') + (n
% 26));
4172 wxLogWarning(_T("The length of the string is %lu"),
4173 (unsigned long)s
.length());
4176 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4178 // this one shouldn't be truncated
4181 // but this one will because log functions use fixed size buffer
4182 // (note that it doesn't need '\n' at the end neither - will be added
4184 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4193 #ifdef TEST_FILENAME
4196 TestFileNameDirManip();
4197 TestFileNameComparison();
4198 TestFileNameOperations();
4199 #endif // TEST_FILENAME
4201 #ifdef TEST_FILETIME
4206 #endif // TEST_FILETIME
4209 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4210 if ( TestFtpConnect() )
4220 #if TEST_INTERACTIVE
4221 TestFtpInteractive();
4224 //else: connecting to the FTP server failed
4232 wxLog::AddTraceMask(_T("mime"));
4236 TestMimeAssociate();
4241 #ifdef TEST_INFO_FUNCTIONS
4246 #if TEST_INTERACTIVE
4250 #endif // TEST_INFO_FUNCTIONS
4252 #ifdef TEST_PATHLIST
4254 #endif // TEST_PATHLIST
4262 #endif // TEST_PRINTF
4269 #endif // TEST_REGCONF
4271 #if defined TEST_REGEX && TEST_INTERACTIVE
4272 TestRegExInteractive();
4273 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4275 #ifdef TEST_REGISTRY
4277 TestRegistryAssociation();
4278 #endif // TEST_REGISTRY
4283 #endif // TEST_SOCKETS
4290 #endif // TEST_STREAMS
4292 #ifdef TEST_TEXTSTREAM
4293 TestTextInputStream();
4294 #endif // TEST_TEXTSTREAM
4297 int nCPUs
= wxThread::GetCPUCount();
4298 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4300 wxThread::SetConcurrency(nCPUs
);
4302 TestJoinableThreads();
4305 TestJoinableThreads();
4306 TestDetachedThreads();
4307 TestThreadSuspend();
4309 TestThreadConditions();
4313 #endif // TEST_THREADS
4317 #endif // TEST_TIMER
4319 #ifdef TEST_DATETIME
4326 TestTimeSpanFormat();
4332 #if TEST_INTERACTIVE
4333 TestDateTimeInteractive();
4335 #endif // TEST_DATETIME
4337 #ifdef TEST_SCOPEGUARD
4341 #ifdef TEST_STACKWALKER
4342 #if wxUSE_STACKWALKER
4343 TestStackWalk(argv
[0]);
4345 #endif // TEST_STACKWALKER
4347 #ifdef TEST_STDPATHS
4348 TestStandardPaths();
4352 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4354 #endif // TEST_USLEEP
4359 #endif // TEST_VCARD
4363 #endif // TEST_VOLUME
4367 TestEncodingConverter();
4368 #endif // TEST_WCHAR
4371 TestZipStreamRead();
4372 TestZipFileSystem();
4377 for ( int n
= 0; n
< argc
; n
++ )
4382 #endif // wxUSE_UNICODE