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"
28 #include "wx/apptrait.h"
29 #include "wx/platinfo.h"
31 // without this pragma, the stupid compiler precompiles #defines below so that
32 // changing them doesn't "take place" later!
37 // ----------------------------------------------------------------------------
38 // conditional compilation
39 // ----------------------------------------------------------------------------
42 A note about all these conditional compilation macros: this file is used
43 both as a test suite for various non-GUI wxWidgets classes and as a
44 scratchpad for quick tests. So there are two compilation modes: if you
45 define TEST_ALL all tests are run, otherwise you may enable the individual
46 tests individually in the "#else" branch below.
49 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
50 // test, define it to 1 to do all tests.
65 // #define TEST_FTP --FIXME! (RN)
66 #define TEST_INFO_FUNCTIONS
77 #define TEST_SCOPEGUARD
79 // #define TEST_SOCKETS --FIXME! (RN)
80 #define TEST_STACKWALKER
83 #define TEST_TEXTSTREAM
86 // #define TEST_VCARD -- don't enable this (VZ)
87 // #define TEST_VOLUME --FIXME! (RN)
91 #define TEST_INFO_FUNCTIONS
94 // some tests are interactive, define this to run them
95 #ifdef TEST_INTERACTIVE
96 #undef TEST_INTERACTIVE
98 #define TEST_INTERACTIVE 1
100 #define TEST_INTERACTIVE 0
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 #if defined(TEST_SOCKETS)
113 // replace TABs with \t and CRs with \n
114 static wxString
MakePrintable(const wxChar
*s
)
117 (void)str
.Replace(_T("\t"), _T("\\t"));
118 (void)str
.Replace(_T("\n"), _T("\\n"));
119 (void)str
.Replace(_T("\r"), _T("\\r"));
124 #endif // MakePrintable() is used
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
132 #include "wx/cmdline.h"
133 #include "wx/datetime.h"
135 #if wxUSE_CMDLINE_PARSER
137 static void ShowCmdLine(const wxCmdLineParser
& parser
)
139 wxString s
= _T("Input files: ");
141 size_t count
= parser
.GetParamCount();
142 for ( size_t param
= 0; param
< count
; param
++ )
144 s
<< parser
.GetParam(param
) << ' ';
148 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
149 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
154 if ( parser
.Found(_T("o"), &strVal
) )
155 s
<< _T("Output file:\t") << strVal
<< '\n';
156 if ( parser
.Found(_T("i"), &strVal
) )
157 s
<< _T("Input dir:\t") << strVal
<< '\n';
158 if ( parser
.Found(_T("s"), &lVal
) )
159 s
<< _T("Size:\t") << lVal
<< '\n';
160 if ( parser
.Found(_T("d"), &dt
) )
161 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
162 if ( parser
.Found(_T("project_name"), &strVal
) )
163 s
<< _T("Project:\t") << strVal
<< '\n';
168 #endif // wxUSE_CMDLINE_PARSER
170 static void TestCmdLineConvert()
172 static const wxChar
*cmdlines
[] =
175 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
176 _T("literal \\\" and \"\""),
179 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
181 const wxChar
*cmdline
= cmdlines
[n
];
182 wxPrintf(_T("Parsing: %s\n"), cmdline
);
183 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
185 size_t count
= args
.GetCount();
186 wxPrintf(_T("\targc = %u\n"), count
);
187 for ( size_t arg
= 0; arg
< count
; arg
++ )
189 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
194 #endif // TEST_CMDLINE
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
205 static const wxChar
*ROOTDIR
= _T("/");
206 static const wxChar
*TESTDIR
= _T("/usr/local/share");
207 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
208 static const wxChar
*ROOTDIR
= _T("c:\\");
209 static const wxChar
*TESTDIR
= _T("d:\\");
211 #error "don't know where the root directory is"
214 static void TestDirEnumHelper(wxDir
& dir
,
215 int flags
= wxDIR_DEFAULT
,
216 const wxString
& filespec
= wxEmptyString
)
220 if ( !dir
.IsOpened() )
223 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
226 wxPrintf(_T("\t%s\n"), filename
.c_str());
228 cont
= dir
.GetNext(&filename
);
231 wxPuts(wxEmptyString
);
236 static void TestDirEnum()
238 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
240 wxString cwd
= wxGetCwd();
241 if ( !wxDir::Exists(cwd
) )
243 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
248 if ( !dir
.IsOpened() )
250 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
254 wxPuts(_T("Enumerating everything in current directory:"));
255 TestDirEnumHelper(dir
);
257 wxPuts(_T("Enumerating really everything in current directory:"));
258 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
260 wxPuts(_T("Enumerating object files in current directory:"));
261 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
263 wxPuts(_T("Enumerating directories in current directory:"));
264 TestDirEnumHelper(dir
, wxDIR_DIRS
);
266 wxPuts(_T("Enumerating files in current directory:"));
267 TestDirEnumHelper(dir
, wxDIR_FILES
);
269 wxPuts(_T("Enumerating files including hidden in current directory:"));
270 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
274 wxPuts(_T("Enumerating everything in root directory:"));
275 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
277 wxPuts(_T("Enumerating directories in root directory:"));
278 TestDirEnumHelper(dir
, wxDIR_DIRS
);
280 wxPuts(_T("Enumerating files in root directory:"));
281 TestDirEnumHelper(dir
, wxDIR_FILES
);
283 wxPuts(_T("Enumerating files including hidden in root directory:"));
284 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
286 wxPuts(_T("Enumerating files in non existing directory:"));
287 wxDir
dirNo(_T("nosuchdir"));
288 TestDirEnumHelper(dirNo
);
293 class DirPrintTraverser
: public wxDirTraverser
296 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
298 return wxDIR_CONTINUE
;
301 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
303 wxString path
, name
, ext
;
304 wxSplitPath(dirname
, &path
, &name
, &ext
);
307 name
<< _T('.') << ext
;
310 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
312 if ( wxIsPathSeparator(*p
) )
316 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
318 return wxDIR_CONTINUE
;
322 static void TestDirTraverse()
324 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
328 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
329 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
332 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
333 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
336 // enum again with custom traverser
337 wxPuts(_T("Now enumerating directories:"));
339 DirPrintTraverser traverser
;
340 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
345 static void TestDirExists()
347 wxPuts(_T("*** Testing wxDir::Exists() ***"));
349 static const wxChar
*dirnames
[] =
352 #if defined(__WXMSW__)
355 _T("\\\\share\\file"),
359 _T("c:\\autoexec.bat"),
360 #elif defined(__UNIX__)
369 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
371 wxPrintf(_T("%-40s: %s\n"),
373 wxDir::Exists(dirnames
[n
]) ? _T("exists")
374 : _T("doesn't exist"));
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
388 #include "wx/dynlib.h"
390 static void TestDllLoad()
392 #if defined(__WXMSW__)
393 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
394 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
395 #elif defined(__UNIX__)
396 // weird: using just libc.so does *not* work!
397 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
398 static const wxChar
*FUNC_NAME
= _T("strlen");
400 #error "don't know how to test wxDllLoader on this platform"
403 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
405 wxDynamicLibrary
lib(LIB_NAME
);
406 if ( !lib
.IsLoaded() )
408 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
412 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
413 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
416 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
417 FUNC_NAME
, LIB_NAME
);
421 wxPrintf(_T("Calling %s dynamically loaded from %s "),
422 FUNC_NAME
, LIB_NAME
);
424 if ( pfnStrlen("foo") != 3 )
426 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
430 wxPuts(_T("... ok"));
435 static const wxChar
*FUNC_NAME_AW
= _T("lstrlen");
437 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
439 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
440 if ( !pfnStrlenAorW
)
442 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
443 FUNC_NAME_AW
, LIB_NAME
);
447 if ( pfnStrlenAorW(_T("foobar")) != 6 )
449 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
456 #if defined(__WXMSW__) || defined(__UNIX__)
458 static void TestDllListLoaded()
460 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
462 puts("\nLoaded modules:");
463 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
464 const size_t count
= dlls
.GetCount();
465 for ( size_t n
= 0; n
< count
; ++n
)
467 const wxDynamicLibraryDetails
& details
= dlls
[n
];
468 printf("%-45s", details
.GetPath().mb_str());
472 if ( details
.GetAddress(&addr
, &len
) )
474 printf(" %08lx:%08lx",
475 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
478 printf(" %s\n", details
.GetVersion().mb_str());
484 #endif // TEST_DYNLIB
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
492 #include "wx/utils.h"
494 static wxString
MyGetEnv(const wxString
& var
)
497 if ( !wxGetEnv(var
, &val
) )
500 val
= wxString(_T('\'')) + val
+ _T('\'');
505 static void TestEnvironment()
507 const wxChar
*var
= _T("wxTestVar");
509 wxPuts(_T("*** testing environment access functions ***"));
511 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
512 wxSetEnv(var
, _T("value for wxTestVar"));
513 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
514 wxSetEnv(var
, _T("another value"));
515 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
517 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
518 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
521 #endif // TEST_ENVIRON
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
529 #include "wx/utils.h"
531 static void TestExecute()
533 wxPuts(_T("*** testing wxExecute ***"));
536 #define COMMAND "cat -n ../../Makefile" // "echo hi"
537 #define SHELL_COMMAND "echo hi from shell"
538 #define REDIRECT_COMMAND COMMAND // "date"
539 #elif defined(__WXMSW__)
540 #define COMMAND "command.com /c echo hi"
541 #define SHELL_COMMAND "echo hi"
542 #define REDIRECT_COMMAND COMMAND
544 #error "no command to exec"
547 wxPrintf(_T("Testing wxShell: "));
549 if ( wxShell(_T(SHELL_COMMAND
)) )
552 wxPuts(_T("ERROR."));
554 wxPrintf(_T("Testing wxExecute: "));
556 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
559 wxPuts(_T("ERROR."));
561 #if 0 // no, it doesn't work (yet?)
562 wxPrintf(_T("Testing async wxExecute: "));
564 if ( wxExecute(COMMAND
) != 0 )
565 wxPuts(_T("Ok (command launched)."));
567 wxPuts(_T("ERROR."));
570 wxPrintf(_T("Testing wxExecute with redirection:\n"));
571 wxArrayString output
;
572 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
574 wxPuts(_T("ERROR."));
578 size_t count
= output
.GetCount();
579 for ( size_t n
= 0; n
< count
; n
++ )
581 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
588 #endif // TEST_EXECUTE
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
597 #include "wx/ffile.h"
598 #include "wx/textfile.h"
600 static void TestFileRead()
602 wxPuts(_T("*** wxFile read test ***"));
604 wxFile
file(_T("testdata.fc"));
605 if ( file
.IsOpened() )
607 wxPrintf(_T("File length: %lu\n"), file
.Length());
609 wxPuts(_T("File dump:\n----------"));
611 static const size_t len
= 1024;
615 size_t nRead
= file
.Read(buf
, len
);
616 if ( nRead
== (size_t)wxInvalidOffset
)
618 wxPrintf(_T("Failed to read the file."));
622 fwrite(buf
, nRead
, 1, stdout
);
628 wxPuts(_T("----------"));
632 wxPrintf(_T("ERROR: can't open test file.\n"));
635 wxPuts(wxEmptyString
);
638 static void TestTextFileRead()
640 wxPuts(_T("*** wxTextFile read test ***"));
642 wxTextFile
file(_T("testdata.fc"));
645 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
646 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
650 wxPuts(_T("\nDumping the entire file:"));
651 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
653 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
655 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
657 wxPuts(_T("\nAnd now backwards:"));
658 for ( s
= file
.GetLastLine();
659 file
.GetCurrentLine() != 0;
660 s
= file
.GetPrevLine() )
662 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
664 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
668 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
671 wxPuts(wxEmptyString
);
674 static void TestFileCopy()
676 wxPuts(_T("*** Testing wxCopyFile ***"));
678 static const wxChar
*filename1
= _T("testdata.fc");
679 static const wxChar
*filename2
= _T("test2");
680 if ( !wxCopyFile(filename1
, filename2
) )
682 wxPuts(_T("ERROR: failed to copy file"));
686 wxFFile
f1(filename1
, _T("rb")),
687 f2(filename2
, _T("rb"));
689 if ( !f1
.IsOpened() || !f2
.IsOpened() )
691 wxPuts(_T("ERROR: failed to open file(s)"));
696 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
698 wxPuts(_T("ERROR: failed to read file(s)"));
702 if ( (s1
.length() != s2
.length()) ||
703 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
705 wxPuts(_T("ERROR: copy error!"));
709 wxPuts(_T("File was copied ok."));
715 if ( !wxRemoveFile(filename2
) )
717 wxPuts(_T("ERROR: failed to remove the file"));
720 wxPuts(wxEmptyString
);
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
731 #include "wx/confbase.h"
732 #include "wx/fileconf.h"
734 static const struct FileConfTestData
736 const wxChar
*name
; // value name
737 const wxChar
*value
; // the value from the file
740 { _T("value1"), _T("one") },
741 { _T("value2"), _T("two") },
742 { _T("novalue"), _T("default") },
745 static void TestFileConfRead()
747 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
749 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
750 _T("testdata.fc"), wxEmptyString
,
751 wxCONFIG_USE_RELATIVE_PATH
);
753 // test simple reading
754 wxPuts(_T("\nReading config file:"));
755 wxString
defValue(_T("default")), value
;
756 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
758 const FileConfTestData
& data
= fcTestData
[n
];
759 value
= fileconf
.Read(data
.name
, defValue
);
760 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
761 if ( value
== data
.value
)
767 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
771 // test enumerating the entries
772 wxPuts(_T("\nEnumerating all root entries:"));
775 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
778 wxPrintf(_T("\t%s = %s\n"),
780 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
782 cont
= fileconf
.GetNextEntry(name
, dummy
);
785 static const wxChar
*testEntry
= _T("TestEntry");
786 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
787 fileconf
.Write(testEntry
, _T("A value"));
788 fileconf
.DeleteEntry(testEntry
);
789 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
792 #endif // TEST_FILECONF
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
800 #include "wx/filename.h"
803 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
807 wxString full
= fn
.GetFullPath();
809 wxString vol
, path
, name
, ext
;
810 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
812 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
813 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
815 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
816 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
817 path
.c_str(), name
.c_str(), ext
.c_str());
819 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
820 wxPrintf(_T("with volume: \t'%s'\n"),
821 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
822 wxPrintf(_T("with separator:\t'%s'\n"),
823 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
824 wxPrintf(_T("with both: \t'%s'\n"),
825 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
827 wxPuts(_T("The directories in the path are:"));
828 wxArrayString dirs
= fn
.GetDirs();
829 size_t count
= dirs
.GetCount();
830 for ( size_t n
= 0; n
< count
; n
++ )
832 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
837 static void TestFileNameTemp()
839 wxPuts(_T("*** testing wxFileName temp file creation ***"));
841 static const wxChar
*tmpprefixes
[] =
849 _T("/tmp/foo/bar"), // this one must be an error
853 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
855 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
858 // "error" is not in upper case because it may be ok
859 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
863 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
864 tmpprefixes
[n
], path
.c_str());
866 if ( !wxRemoveFile(path
) )
868 wxLogWarning(_T("Failed to remove temp file '%s'"),
875 static void TestFileNameDirManip()
877 // TODO: test AppendDir(), RemoveDir(), ...
880 static void TestFileNameComparison()
885 static void TestFileNameOperations()
890 static void TestFileNameCwd()
895 #endif // TEST_FILENAME
897 // ----------------------------------------------------------------------------
898 // wxFileName time functions
899 // ----------------------------------------------------------------------------
903 #include "wx/filename.h"
904 #include "wx/datetime.h"
906 static void TestFileGetTimes()
908 wxFileName
fn(_T("testdata.fc"));
910 wxDateTime dtAccess
, dtMod
, dtCreate
;
911 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
913 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
917 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
919 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
920 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
921 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
922 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
927 static void TestFileSetTimes()
929 wxFileName
fn(_T("testdata.fc"));
933 wxPrintf(_T("ERROR: Touch() failed.\n"));
938 #endif // TEST_FILETIME
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
947 #include "wx/utils.h" // for wxSetEnv
949 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
951 // find the name of the language from its value
952 static const wxChar
*GetLangName(int lang
)
954 static const wxChar
*languageNames
[] =
964 _T("ARABIC_ALGERIA"),
965 _T("ARABIC_BAHRAIN"),
970 _T("ARABIC_LEBANON"),
972 _T("ARABIC_MOROCCO"),
975 _T("ARABIC_SAUDI_ARABIA"),
978 _T("ARABIC_TUNISIA"),
985 _T("AZERI_CYRILLIC"),
1000 _T("CHINESE_SIMPLIFIED"),
1001 _T("CHINESE_TRADITIONAL"),
1002 _T("CHINESE_HONGKONG"),
1003 _T("CHINESE_MACAU"),
1004 _T("CHINESE_SINGAPORE"),
1005 _T("CHINESE_TAIWAN"),
1011 _T("DUTCH_BELGIAN"),
1015 _T("ENGLISH_AUSTRALIA"),
1016 _T("ENGLISH_BELIZE"),
1017 _T("ENGLISH_BOTSWANA"),
1018 _T("ENGLISH_CANADA"),
1019 _T("ENGLISH_CARIBBEAN"),
1020 _T("ENGLISH_DENMARK"),
1022 _T("ENGLISH_JAMAICA"),
1023 _T("ENGLISH_NEW_ZEALAND"),
1024 _T("ENGLISH_PHILIPPINES"),
1025 _T("ENGLISH_SOUTH_AFRICA"),
1026 _T("ENGLISH_TRINIDAD"),
1027 _T("ENGLISH_ZIMBABWE"),
1035 _T("FRENCH_BELGIAN"),
1036 _T("FRENCH_CANADIAN"),
1037 _T("FRENCH_LUXEMBOURG"),
1038 _T("FRENCH_MONACO"),
1044 _T("GERMAN_AUSTRIAN"),
1045 _T("GERMAN_BELGIUM"),
1046 _T("GERMAN_LIECHTENSTEIN"),
1047 _T("GERMAN_LUXEMBOURG"),
1065 _T("ITALIAN_SWISS"),
1070 _T("KASHMIRI_INDIA"),
1088 _T("MALAY_BRUNEI_DARUSSALAM"),
1089 _T("MALAY_MALAYSIA"),
1099 _T("NORWEGIAN_BOKMAL"),
1100 _T("NORWEGIAN_NYNORSK"),
1107 _T("PORTUGUESE_BRAZILIAN"),
1110 _T("RHAETO_ROMANCE"),
1113 _T("RUSSIAN_UKRAINE"),
1119 _T("SERBIAN_CYRILLIC"),
1120 _T("SERBIAN_LATIN"),
1121 _T("SERBO_CROATIAN"),
1132 _T("SPANISH_ARGENTINA"),
1133 _T("SPANISH_BOLIVIA"),
1134 _T("SPANISH_CHILE"),
1135 _T("SPANISH_COLOMBIA"),
1136 _T("SPANISH_COSTA_RICA"),
1137 _T("SPANISH_DOMINICAN_REPUBLIC"),
1138 _T("SPANISH_ECUADOR"),
1139 _T("SPANISH_EL_SALVADOR"),
1140 _T("SPANISH_GUATEMALA"),
1141 _T("SPANISH_HONDURAS"),
1142 _T("SPANISH_MEXICAN"),
1143 _T("SPANISH_MODERN"),
1144 _T("SPANISH_NICARAGUA"),
1145 _T("SPANISH_PANAMA"),
1146 _T("SPANISH_PARAGUAY"),
1148 _T("SPANISH_PUERTO_RICO"),
1149 _T("SPANISH_URUGUAY"),
1151 _T("SPANISH_VENEZUELA"),
1155 _T("SWEDISH_FINLAND"),
1173 _T("URDU_PAKISTAN"),
1175 _T("UZBEK_CYRILLIC"),
1188 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1189 return languageNames
[lang
];
1191 return _T("INVALID");
1194 static void TestDefaultLang()
1196 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1198 static const wxChar
*langStrings
[] =
1200 NULL
, // system default
1207 _T("de_DE.iso88591"),
1209 _T("?"), // invalid lang spec
1210 _T("klingonese"), // I bet on some systems it does exist...
1213 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1214 wxLocale::GetSystemEncodingName().c_str(),
1215 wxLocale::GetSystemEncoding());
1217 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1219 const wxChar
*langStr
= langStrings
[n
];
1222 // FIXME: this doesn't do anything at all under Windows, we need
1223 // to create a new wxLocale!
1224 wxSetEnv(_T("LC_ALL"), langStr
);
1227 int lang
= gs_localeDefault
.GetSystemLanguage();
1228 wxPrintf(_T("Locale for '%s' is %s.\n"),
1229 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1233 #endif // TEST_LOCALE
1235 // ----------------------------------------------------------------------------
1237 // ----------------------------------------------------------------------------
1241 #include "wx/mimetype.h"
1243 static void TestMimeEnum()
1245 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1247 wxArrayString mimetypes
;
1249 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1251 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1256 for ( size_t n
= 0; n
< count
; n
++ )
1258 wxFileType
*filetype
=
1259 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1262 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1263 mimetypes
[n
].c_str());
1267 filetype
->GetDescription(&desc
);
1268 filetype
->GetExtensions(exts
);
1270 filetype
->GetIcon(NULL
);
1273 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1276 extsAll
<< _T(", ");
1280 wxPrintf(_T("\t%s: %s (%s)\n"),
1281 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1284 wxPuts(wxEmptyString
);
1287 static void TestMimeOverride()
1289 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1291 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1292 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1294 if ( wxFile::Exists(mailcap
) )
1295 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1297 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1299 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1302 if ( wxFile::Exists(mimetypes
) )
1303 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1305 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1307 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1310 wxPuts(wxEmptyString
);
1313 static void TestMimeFilename()
1315 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1317 static const wxChar
*filenames
[] =
1325 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1327 const wxString fname
= filenames
[n
];
1328 wxString ext
= fname
.AfterLast(_T('.'));
1329 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1332 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1337 if ( !ft
->GetDescription(&desc
) )
1338 desc
= _T("<no description>");
1341 if ( !ft
->GetOpenCommand(&cmd
,
1342 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1343 cmd
= _T("<no command available>");
1345 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1347 wxPrintf(_T("To open %s (%s) do %s.\n"),
1348 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1354 wxPuts(wxEmptyString
);
1357 static void TestMimeAssociate()
1359 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1361 wxFileTypeInfo
ftInfo(
1362 _T("application/x-xyz"),
1363 _T("xyzview '%s'"), // open cmd
1364 _T(""), // print cmd
1365 _T("XYZ File"), // description
1366 _T(".xyz"), // extensions
1367 NULL
// end of extensions
1369 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1371 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1374 wxPuts(_T("ERROR: failed to create association!"));
1378 // TODO: read it back
1382 wxPuts(wxEmptyString
);
1387 // ----------------------------------------------------------------------------
1388 // module dependencies feature
1389 // ----------------------------------------------------------------------------
1393 #include "wx/module.h"
1395 class wxTestModule
: public wxModule
1398 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1399 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1402 class wxTestModuleA
: public wxTestModule
1407 DECLARE_DYNAMIC_CLASS(wxTestModuleA
)
1410 class wxTestModuleB
: public wxTestModule
1415 DECLARE_DYNAMIC_CLASS(wxTestModuleB
)
1418 class wxTestModuleC
: public wxTestModule
1423 DECLARE_DYNAMIC_CLASS(wxTestModuleC
)
1426 class wxTestModuleD
: public wxTestModule
1431 DECLARE_DYNAMIC_CLASS(wxTestModuleD
)
1434 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC
, wxModule
)
1435 wxTestModuleC::wxTestModuleC()
1437 AddDependency(CLASSINFO(wxTestModuleD
));
1440 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA
, wxModule
)
1441 wxTestModuleA::wxTestModuleA()
1443 AddDependency(CLASSINFO(wxTestModuleB
));
1444 AddDependency(CLASSINFO(wxTestModuleD
));
1447 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD
, wxModule
)
1448 wxTestModuleD::wxTestModuleD()
1452 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB
, wxModule
)
1453 wxTestModuleB::wxTestModuleB()
1455 AddDependency(CLASSINFO(wxTestModuleD
));
1456 AddDependency(CLASSINFO(wxTestModuleC
));
1459 #endif // TEST_MODULE
1461 // ----------------------------------------------------------------------------
1462 // misc information functions
1463 // ----------------------------------------------------------------------------
1465 #ifdef TEST_INFO_FUNCTIONS
1467 #include "wx/utils.h"
1469 #if TEST_INTERACTIVE
1470 static void TestDiskInfo()
1472 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1476 wxChar pathname
[128];
1477 wxPrintf(_T("\nEnter a directory name: "));
1478 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1481 // kill the last '\n'
1482 pathname
[wxStrlen(pathname
) - 1] = 0;
1484 wxLongLong total
, free
;
1485 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1487 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1491 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1492 (total
/ 1024).ToString().c_str(),
1493 (free
/ 1024).ToString().c_str(),
1498 #endif // TEST_INTERACTIVE
1500 static void TestOsInfo()
1502 wxPuts(_T("*** Testing OS info functions ***\n"));
1505 wxGetOsVersion(&major
, &minor
);
1506 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1507 wxGetOsDescription().c_str(), major
, minor
);
1509 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1511 wxPrintf(_T("Host name is %s (%s).\n"),
1512 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1514 wxPuts(wxEmptyString
);
1517 static void TestPlatformInfo()
1519 wxPuts(_T("*** Testing wxPlatformInfo functions ***\n"));
1521 // get this platform
1522 wxPlatformInfo plat
;
1524 wxPrintf(_T("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
1525 wxPrintf(_T("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
1526 wxPrintf(_T("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
1527 wxPrintf(_T("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
1528 wxPrintf(_T("Architecture is: %s\n"), plat
.GetArchName().c_str());
1529 wxPrintf(_T("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
1531 wxPuts(wxEmptyString
);
1534 static void TestUserInfo()
1536 wxPuts(_T("*** Testing user info functions ***\n"));
1538 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1539 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1540 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1541 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1543 wxPuts(wxEmptyString
);
1546 #endif // TEST_INFO_FUNCTIONS
1548 // ----------------------------------------------------------------------------
1550 // ----------------------------------------------------------------------------
1552 #ifdef TEST_PATHLIST
1555 #define CMD_IN_PATH _T("ls")
1557 #define CMD_IN_PATH _T("command.com")
1560 static void TestPathList()
1562 wxPuts(_T("*** Testing wxPathList ***\n"));
1564 wxPathList pathlist
;
1565 pathlist
.AddEnvList(_T("PATH"));
1566 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1569 wxPrintf(_T("ERROR: command not found in the path.\n"));
1573 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1577 #endif // TEST_PATHLIST
1579 // ----------------------------------------------------------------------------
1580 // regular expressions
1581 // ----------------------------------------------------------------------------
1585 #include "wx/regex.h"
1587 static void TestRegExInteractive()
1589 wxPuts(_T("*** Testing RE interactively ***"));
1593 wxChar pattern
[128];
1594 wxPrintf(_T("\nEnter a pattern: "));
1595 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1598 // kill the last '\n'
1599 pattern
[wxStrlen(pattern
) - 1] = 0;
1602 if ( !re
.Compile(pattern
) )
1610 wxPrintf(_T("Enter text to match: "));
1611 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1614 // kill the last '\n'
1615 text
[wxStrlen(text
) - 1] = 0;
1617 if ( !re
.Matches(text
) )
1619 wxPrintf(_T("No match.\n"));
1623 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1626 for ( size_t n
= 1; ; n
++ )
1628 if ( !re
.GetMatch(&start
, &len
, n
) )
1633 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1634 n
, wxString(text
+ start
, len
).c_str());
1641 #endif // TEST_REGEX
1643 // ----------------------------------------------------------------------------
1645 // ----------------------------------------------------------------------------
1655 static void TestDbOpen()
1663 // ----------------------------------------------------------------------------
1665 // ----------------------------------------------------------------------------
1668 NB: this stuff was taken from the glibc test suite and modified to build
1669 in wxWidgets: if I read the copyright below properly, this shouldn't
1675 #ifdef wxTEST_PRINTF
1676 // use our functions from wxchar.cpp
1680 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1681 // in the tests below
1682 int wxPrintf( const wxChar
*format
, ... );
1683 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1686 #include "wx/longlong.h"
1690 static void rfg1 (void);
1691 static void rfg2 (void);
1695 fmtchk (const wxChar
*fmt
)
1697 (void) wxPrintf(_T("%s:\t`"), fmt
);
1698 (void) wxPrintf(fmt
, 0x12);
1699 (void) wxPrintf(_T("'\n"));
1703 fmtst1chk (const wxChar
*fmt
)
1705 (void) wxPrintf(_T("%s:\t`"), fmt
);
1706 (void) wxPrintf(fmt
, 4, 0x12);
1707 (void) wxPrintf(_T("'\n"));
1711 fmtst2chk (const wxChar
*fmt
)
1713 (void) wxPrintf(_T("%s:\t`"), fmt
);
1714 (void) wxPrintf(fmt
, 4, 4, 0x12);
1715 (void) wxPrintf(_T("'\n"));
1718 /* This page is covered by the following copyright: */
1720 /* (C) Copyright C E Chew
1722 * Feel free to copy, use and distribute this software provided:
1724 * 1. you do not pretend that you wrote it
1725 * 2. you leave this copyright notice intact.
1729 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1736 /* Formatted Output Test
1738 * This exercises the output formatting code.
1741 wxChar
*PointerNull
= NULL
;
1748 wxChar
*prefix
= buf
;
1751 wxPuts(_T("\nFormatted output test"));
1752 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1753 wxStrcpy(prefix
, _T("%"));
1754 for (i
= 0; i
< 2; i
++) {
1755 for (j
= 0; j
< 2; j
++) {
1756 for (k
= 0; k
< 2; k
++) {
1757 for (l
= 0; l
< 2; l
++) {
1758 wxStrcpy(prefix
, _T("%"));
1759 if (i
== 0) wxStrcat(prefix
, _T("-"));
1760 if (j
== 0) wxStrcat(prefix
, _T("+"));
1761 if (k
== 0) wxStrcat(prefix
, _T("#"));
1762 if (l
== 0) wxStrcat(prefix
, _T("0"));
1763 wxPrintf(_T("%5s |"), prefix
);
1764 wxStrcpy(tp
, prefix
);
1765 wxStrcat(tp
, _T("6d |"));
1767 wxStrcpy(tp
, prefix
);
1768 wxStrcat(tp
, _T("6o |"));
1770 wxStrcpy(tp
, prefix
);
1771 wxStrcat(tp
, _T("6x |"));
1773 wxStrcpy(tp
, prefix
);
1774 wxStrcat(tp
, _T("6X |"));
1776 wxStrcpy(tp
, prefix
);
1777 wxStrcat(tp
, _T("6u |"));
1784 wxPrintf(_T("%10s\n"), PointerNull
);
1785 wxPrintf(_T("%-10s\n"), PointerNull
);
1788 static void TestPrintf()
1790 static wxChar shortstr
[] = _T("Hi, Z.");
1791 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1792 I am ready for my first lesson today.");
1794 wxString test_format
;
1798 fmtchk(_T("%4.4x"));
1799 fmtchk(_T("%04.4x"));
1800 fmtchk(_T("%4.3x"));
1801 fmtchk(_T("%04.3x"));
1803 fmtst1chk(_T("%.*x"));
1804 fmtst1chk(_T("%0*x"));
1805 fmtst2chk(_T("%*.*x"));
1806 fmtst2chk(_T("%0*.*x"));
1808 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1809 wxPrintf(bad_format
.c_str());
1810 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1812 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1813 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1814 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1815 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1816 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1817 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1818 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1819 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1820 wxPrintf(test_format
.c_str(), -123456);
1821 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1822 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1824 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1825 wxPrintf(test_format
.c_str(), shortstr
);
1826 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1827 wxPrintf(test_format
.c_str(), shortstr
);
1828 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1829 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1830 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1831 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1833 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1834 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1835 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1836 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1837 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1838 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1839 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1840 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1841 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1842 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1843 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1844 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1846 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1847 wxPrintf (_T(" %6.5f\n"), .1);
1848 wxPrintf (_T("x%5.4fx\n"), .5);
1850 wxPrintf (_T("%#03x\n"), 1);
1852 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1858 while (niter
-- != 0)
1859 wxPrintf (_T("%.17e\n"), d
/ 2);
1864 // Open Watcom cause compiler error here
1865 // Error! E173: col(24) floating-point constant too small to represent
1866 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1869 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1870 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1871 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1872 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1873 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1874 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1875 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1876 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1877 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1878 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1883 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1885 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1886 rc
, WXSIZEOF(buf
), buf
);
1889 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1890 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1896 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1897 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1898 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1899 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1900 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1901 wxPrintf (_T("%g should be 10\n"), 10.0);
1902 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1906 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1912 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1914 result
|= wxStrcmp (buf
,
1915 _T("onetwo three "));
1917 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1924 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1926 // for some reason below line fails under Borland
1927 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1930 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1933 wxPuts (_T("\tFAILED"));
1935 wxUnusedVar(result
);
1936 wxPuts (wxEmptyString
);
1938 #endif // wxLongLong_t
1940 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1941 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1943 wxPuts (_T("--- Should be no further output. ---"));
1952 memset (bytes
, '\xff', sizeof bytes
);
1953 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1954 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1955 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1957 wxPuts (_T("%hhn overwrite more bytes"));
1962 wxPuts (_T("%hhn wrote incorrect value"));
1974 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1975 if (wxStrcmp (buf
, _T(" ")) != 0)
1976 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1977 wxSprintf (buf
, _T("%5.f"), 33.3);
1978 if (wxStrcmp (buf
, _T(" 33")) != 0)
1979 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1980 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1981 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1982 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1983 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1984 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1985 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1986 wxSprintf (buf
, _T("%.g"), 33.3);
1987 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1988 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1989 wxSprintf (buf
, _T("%.G"), 33.3);
1990 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1991 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1999 wxString test_format
;
2002 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
2003 if (wxStrcmp (buf
, _T("3")) != 0)
2004 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2006 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
2007 if (wxStrcmp (buf
, _T("3")) != 0)
2008 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2010 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
2011 if (wxStrcmp (buf
, _T(" 3")) != 0)
2012 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
2014 test_format
= _T("%04.*o");
2015 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2016 if (wxStrcmp (buf
, _T(" 041")) != 0)
2017 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
2019 test_format
= _T("%09.*u");
2020 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2021 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
2022 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
2024 test_format
= _T("%04.*x");
2025 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2026 if (wxStrcmp (buf
, _T(" 021")) != 0)
2027 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2029 test_format
= _T("%04.*X");
2030 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2031 if (wxStrcmp (buf
, _T(" 021")) != 0)
2032 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2035 #endif // TEST_PRINTF
2037 // ----------------------------------------------------------------------------
2038 // registry and related stuff
2039 // ----------------------------------------------------------------------------
2041 // this is for MSW only
2044 #undef TEST_REGISTRY
2049 #include "wx/confbase.h"
2050 #include "wx/msw/regconf.h"
2053 static void TestRegConfWrite()
2055 wxConfig
*config
= new wxConfig(_T("myapp"));
2056 config
->SetPath(_T("/group1"));
2057 config
->Write(_T("entry1"), _T("foo"));
2058 config
->SetPath(_T("/group2"));
2059 config
->Write(_T("entry1"), _T("bar"));
2063 static void TestRegConfRead()
2065 wxConfig
*config
= new wxConfig(_T("myapp"));
2069 config
->SetPath(_T("/"));
2070 wxPuts(_T("Enumerating / subgroups:"));
2071 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2075 bCont
= config
->GetNextGroup(str
, dummy
);
2079 #endif // TEST_REGCONF
2081 #ifdef TEST_REGISTRY
2083 #include "wx/msw/registry.h"
2085 // I chose this one because I liked its name, but it probably only exists under
2087 static const wxChar
*TESTKEY
=
2088 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2090 static void TestRegistryRead()
2092 wxPuts(_T("*** testing registry reading ***"));
2094 wxRegKey
key(TESTKEY
);
2095 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2098 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2103 size_t nSubKeys
, nValues
;
2104 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2106 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2109 wxPrintf(_T("Enumerating values:\n"));
2113 bool cont
= key
.GetFirstValue(value
, dummy
);
2116 wxPrintf(_T("Value '%s': type "), value
.c_str());
2117 switch ( key
.GetValueType(value
) )
2119 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2120 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2121 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2122 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2123 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2124 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2125 default: wxPrintf(_T("other (unknown)")); break;
2128 wxPrintf(_T(", value = "));
2129 if ( key
.IsNumericValue(value
) )
2132 key
.QueryValue(value
, &val
);
2133 wxPrintf(_T("%ld"), val
);
2138 key
.QueryValue(value
, val
);
2139 wxPrintf(_T("'%s'"), val
.c_str());
2141 key
.QueryRawValue(value
, val
);
2142 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2147 cont
= key
.GetNextValue(value
, dummy
);
2151 static void TestRegistryAssociation()
2154 The second call to deleteself genertaes an error message, with a
2155 messagebox saying .flo is crucial to system operation, while the .ddf
2156 call also fails, but with no error message
2161 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2163 key
= _T("ddxf_auto_file") ;
2164 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2166 key
= _T("ddxf_auto_file") ;
2167 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2169 key
= _T("program,0") ;
2170 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2172 key
= _T("program \"%1\"") ;
2174 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2176 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2178 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2180 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2184 #endif // TEST_REGISTRY
2186 // ----------------------------------------------------------------------------
2188 // ----------------------------------------------------------------------------
2190 #ifdef TEST_SCOPEGUARD
2192 #include "wx/scopeguard.h"
2194 static void function0() { puts("function0()"); }
2195 static void function1(int n
) { printf("function1(%d)\n", n
); }
2196 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2200 void method0() { printf("method0()\n"); }
2201 void method1(int n
) { printf("method1(%d)\n", n
); }
2202 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2205 static void TestScopeGuard()
2207 wxON_BLOCK_EXIT0(function0
);
2208 wxON_BLOCK_EXIT1(function1
, 17);
2209 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2212 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2213 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2214 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2216 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2217 dismissed
.Dismiss();
2222 // ----------------------------------------------------------------------------
2224 // ----------------------------------------------------------------------------
2228 #include "wx/socket.h"
2229 #include "wx/protocol/protocol.h"
2230 #include "wx/protocol/http.h"
2232 static void TestSocketServer()
2234 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2236 static const int PORT
= 3000;
2241 wxSocketServer
*server
= new wxSocketServer(addr
);
2242 if ( !server
->Ok() )
2244 wxPuts(_T("ERROR: failed to bind"));
2252 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2254 wxSocketBase
*socket
= server
->Accept();
2257 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2261 wxPuts(_T("Server: got a client."));
2263 server
->SetTimeout(60); // 1 min
2266 while ( !close
&& socket
->IsConnected() )
2269 wxChar ch
= _T('\0');
2272 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2274 // don't log error if the client just close the connection
2275 if ( socket
->IsConnected() )
2277 wxPuts(_T("ERROR: in wxSocket::Read."));
2297 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2298 if ( s
== _T("close") )
2300 wxPuts(_T("Closing connection"));
2304 else if ( s
== _T("quit") )
2309 wxPuts(_T("Shutting down the server"));
2311 else // not a special command
2313 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2314 socket
->Write("\r\n", 2);
2315 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2321 wxPuts(_T("Server: lost a client unexpectedly."));
2327 // same as "delete server" but is consistent with GUI programs
2331 static void TestSocketClient()
2333 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2335 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2338 addr
.Hostname(hostname
);
2341 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2343 wxSocketClient client
;
2344 if ( !client
.Connect(addr
) )
2346 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2350 wxPrintf(_T("--- Connected to %s:%u...\n"),
2351 addr
.Hostname().c_str(), addr
.Service());
2355 // could use simply "GET" here I suppose
2357 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2358 client
.Write(cmdGet
, cmdGet
.length());
2359 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2360 MakePrintable(cmdGet
).c_str());
2361 client
.Read(buf
, WXSIZEOF(buf
));
2362 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2366 #endif // TEST_SOCKETS
2368 // ----------------------------------------------------------------------------
2370 // ----------------------------------------------------------------------------
2374 #include "wx/protocol/ftp.h"
2378 #define FTP_ANONYMOUS
2380 #ifdef FTP_ANONYMOUS
2381 static const wxChar
*directory
= _T("/pub");
2382 static const wxChar
*filename
= _T("welcome.msg");
2384 static const wxChar
*directory
= _T("/etc");
2385 static const wxChar
*filename
= _T("issue");
2388 static bool TestFtpConnect()
2390 wxPuts(_T("*** Testing FTP connect ***"));
2392 #ifdef FTP_ANONYMOUS
2393 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2395 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2396 #else // !FTP_ANONYMOUS
2397 static const wxChar
*hostname
= "localhost";
2400 wxFgets(user
, WXSIZEOF(user
), stdin
);
2401 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2404 wxChar password
[256];
2405 wxPrintf(_T("Password for %s: "), password
);
2406 wxFgets(password
, WXSIZEOF(password
), stdin
);
2407 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2408 ftp
.SetPassword(password
);
2410 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2411 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2413 if ( !ftp
.Connect(hostname
) )
2415 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2421 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2422 hostname
, ftp
.Pwd().c_str());
2429 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2430 static void TestFtpWuFtpd()
2433 static const wxChar
*hostname
= _T("ftp.eudora.com");
2434 if ( !ftp
.Connect(hostname
) )
2436 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2440 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2441 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2444 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2448 size_t size
= in
->GetSize();
2449 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2451 wxChar
*data
= new wxChar
[size
];
2452 if ( !in
->Read(data
, size
) )
2454 wxPuts(_T("ERROR: read error"));
2458 wxPrintf(_T("Successfully retrieved the file.\n"));
2467 static void TestFtpList()
2469 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2472 if ( !ftp
.ChDir(directory
) )
2474 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2477 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2479 // test NLIST and LIST
2480 wxArrayString files
;
2481 if ( !ftp
.GetFilesList(files
) )
2483 wxPuts(_T("ERROR: failed to get NLIST of files"));
2487 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2488 size_t count
= files
.GetCount();
2489 for ( size_t n
= 0; n
< count
; n
++ )
2491 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2493 wxPuts(_T("End of the file list"));
2496 if ( !ftp
.GetDirList(files
) )
2498 wxPuts(_T("ERROR: failed to get LIST of files"));
2502 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2503 size_t count
= files
.GetCount();
2504 for ( size_t n
= 0; n
< count
; n
++ )
2506 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2508 wxPuts(_T("End of the file list"));
2511 if ( !ftp
.ChDir(_T("..")) )
2513 wxPuts(_T("ERROR: failed to cd to .."));
2516 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2519 static void TestFtpDownload()
2521 wxPuts(_T("*** Testing wxFTP download ***\n"));
2524 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2527 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2531 size_t size
= in
->GetSize();
2532 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2535 wxChar
*data
= new wxChar
[size
];
2536 if ( !in
->Read(data
, size
) )
2538 wxPuts(_T("ERROR: read error"));
2542 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2550 static void TestFtpFileSize()
2552 wxPuts(_T("*** Testing FTP SIZE command ***"));
2554 if ( !ftp
.ChDir(directory
) )
2556 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2559 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2561 if ( ftp
.FileExists(filename
) )
2563 int size
= ftp
.GetFileSize(filename
);
2565 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2567 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2571 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2575 static void TestFtpMisc()
2577 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2579 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2581 wxPuts(_T("ERROR: STAT failed"));
2585 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2588 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2590 wxPuts(_T("ERROR: HELP SITE failed"));
2594 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2595 ftp
.GetLastResult().c_str());
2599 static void TestFtpInteractive()
2601 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2607 wxPrintf(_T("Enter FTP command: "));
2608 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2611 // kill the last '\n'
2612 buf
[wxStrlen(buf
) - 1] = 0;
2614 // special handling of LIST and NLST as they require data connection
2615 wxString
start(buf
, 4);
2617 if ( start
== _T("LIST") || start
== _T("NLST") )
2620 if ( wxStrlen(buf
) > 4 )
2623 wxArrayString files
;
2624 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2626 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2630 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2631 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2632 size_t count
= files
.GetCount();
2633 for ( size_t n
= 0; n
< count
; n
++ )
2635 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2637 wxPuts(_T("--- End of the file list"));
2642 wxChar ch
= ftp
.SendCommand(buf
);
2643 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2646 wxPrintf(_T(" (return code %c)"), ch
);
2649 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2653 wxPuts(_T("\n*** done ***"));
2656 static void TestFtpUpload()
2658 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2661 static const wxChar
*file1
= _T("test1");
2662 static const wxChar
*file2
= _T("test2");
2663 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2666 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2667 out
->Write("First hello", 11);
2671 // send a command to check the remote file
2672 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2674 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2678 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2679 file1
, ftp
.GetLastResult().c_str());
2682 out
= ftp
.GetOutputStream(file2
);
2685 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2686 out
->Write("Second hello", 12);
2693 // ----------------------------------------------------------------------------
2695 // ----------------------------------------------------------------------------
2697 #ifdef TEST_STACKWALKER
2699 #if wxUSE_STACKWALKER
2701 #include "wx/stackwalk.h"
2703 class StackDump
: public wxStackWalker
2706 StackDump(const char *argv0
)
2707 : wxStackWalker(argv0
)
2711 virtual void Walk(size_t skip
= 1)
2713 wxPuts(_T("Stack dump:"));
2715 wxStackWalker::Walk(skip
);
2719 virtual void OnStackFrame(const wxStackFrame
& frame
)
2721 printf("[%2d] ", frame
.GetLevel());
2723 wxString name
= frame
.GetName();
2724 if ( !name
.empty() )
2726 printf("%-20.40s", name
.mb_str());
2730 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2733 if ( frame
.HasSourceLocation() )
2736 frame
.GetFileName().mb_str(),
2743 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2745 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2750 static void TestStackWalk(const char *argv0
)
2752 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2754 StackDump
dump(argv0
);
2758 #endif // wxUSE_STACKWALKER
2760 #endif // TEST_STACKWALKER
2762 // ----------------------------------------------------------------------------
2764 // ----------------------------------------------------------------------------
2766 #ifdef TEST_STDPATHS
2768 #include "wx/stdpaths.h"
2770 static void TestStandardPaths()
2772 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2774 wxTheApp
->SetAppName(_T("console"));
2776 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2777 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2778 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2779 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2780 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2781 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2782 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2783 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2784 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2785 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2786 wxPrintf(_T("Localized res. dir:\t%s\n"),
2787 stdp
.GetLocalizedResourcesDir(_T("fr")).c_str());
2788 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2789 stdp
.GetLocalizedResourcesDir
2792 wxStandardPaths::ResourceCat_Messages
2796 #endif // TEST_STDPATHS
2798 // ----------------------------------------------------------------------------
2800 // ----------------------------------------------------------------------------
2804 #include "wx/wfstream.h"
2805 #include "wx/mstream.h"
2807 static void TestFileStream()
2809 wxPuts(_T("*** Testing wxFileInputStream ***"));
2811 static const wxString filename
= _T("testdata.fs");
2813 wxFileOutputStream
fsOut(filename
);
2814 fsOut
.Write("foo", 3);
2817 wxFileInputStream
fsIn(filename
);
2818 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2819 while ( !fsIn
.Eof() )
2821 wxPutchar(fsIn
.GetC());
2824 if ( !wxRemoveFile(filename
) )
2826 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2829 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2832 static void TestMemoryStream()
2834 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2836 wxMemoryOutputStream memOutStream
;
2837 wxPrintf(_T("Initially out stream offset: %lu\n"),
2838 (unsigned long)memOutStream
.TellO());
2840 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2842 memOutStream
.PutC(*p
);
2845 wxPrintf(_T("Final out stream offset: %lu\n"),
2846 (unsigned long)memOutStream
.TellO());
2848 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2851 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2853 wxMemoryInputStream
memInpStream(buf
, len
);
2854 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2855 while ( !memInpStream
.Eof() )
2857 wxPutchar(memInpStream
.GetC());
2860 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2863 #endif // TEST_STREAMS
2865 // ----------------------------------------------------------------------------
2867 // ----------------------------------------------------------------------------
2871 #include "wx/stopwatch.h"
2872 #include "wx/utils.h"
2874 static void TestStopWatch()
2876 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2880 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2883 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2885 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2889 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2892 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2895 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2898 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2901 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2904 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2905 for ( size_t n
= 0; n
< 70; n
++ )
2909 for ( size_t m
= 0; m
< 100000; m
++ )
2911 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2913 wxPuts(_T("\ntime is negative - ERROR!"));
2921 wxPuts(_T(", ok."));
2924 #endif // TEST_TIMER
2926 // ----------------------------------------------------------------------------
2928 // ----------------------------------------------------------------------------
2932 #include "wx/vcard.h"
2934 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2937 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2940 wxPrintf(_T("%s%s"),
2941 wxString(_T('\t'), level
).c_str(),
2942 vcObj
->GetName().c_str());
2945 switch ( vcObj
->GetType() )
2947 case wxVCardObject::String
:
2948 case wxVCardObject::UString
:
2951 vcObj
->GetValue(&val
);
2952 value
<< _T('"') << val
<< _T('"');
2956 case wxVCardObject::Int
:
2959 vcObj
->GetValue(&i
);
2960 value
.Printf(_T("%u"), i
);
2964 case wxVCardObject::Long
:
2967 vcObj
->GetValue(&l
);
2968 value
.Printf(_T("%lu"), l
);
2972 case wxVCardObject::None
:
2975 case wxVCardObject::Object
:
2976 value
= _T("<node>");
2980 value
= _T("<unknown value type>");
2984 wxPrintf(_T(" = %s"), value
.c_str());
2987 DumpVObject(level
+ 1, *vcObj
);
2990 vcObj
= vcard
.GetNextProp(&cookie
);
2994 static void DumpVCardAddresses(const wxVCard
& vcard
)
2996 wxPuts(_T("\nShowing all addresses from vCard:\n"));
3000 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
3004 int flags
= addr
->GetFlags();
3005 if ( flags
& wxVCardAddress::Domestic
)
3007 flagsStr
<< _T("domestic ");
3009 if ( flags
& wxVCardAddress::Intl
)
3011 flagsStr
<< _T("international ");
3013 if ( flags
& wxVCardAddress::Postal
)
3015 flagsStr
<< _T("postal ");
3017 if ( flags
& wxVCardAddress::Parcel
)
3019 flagsStr
<< _T("parcel ");
3021 if ( flags
& wxVCardAddress::Home
)
3023 flagsStr
<< _T("home ");
3025 if ( flags
& wxVCardAddress::Work
)
3027 flagsStr
<< _T("work ");
3030 wxPrintf(_T("Address %u:\n")
3032 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3035 addr
->GetPostOffice().c_str(),
3036 addr
->GetExtAddress().c_str(),
3037 addr
->GetStreet().c_str(),
3038 addr
->GetLocality().c_str(),
3039 addr
->GetRegion().c_str(),
3040 addr
->GetPostalCode().c_str(),
3041 addr
->GetCountry().c_str()
3045 addr
= vcard
.GetNextAddress(&cookie
);
3049 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
3051 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3055 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
3059 int flags
= phone
->GetFlags();
3060 if ( flags
& wxVCardPhoneNumber::Voice
)
3062 flagsStr
<< _T("voice ");
3064 if ( flags
& wxVCardPhoneNumber::Fax
)
3066 flagsStr
<< _T("fax ");
3068 if ( flags
& wxVCardPhoneNumber::Cellular
)
3070 flagsStr
<< _T("cellular ");
3072 if ( flags
& wxVCardPhoneNumber::Modem
)
3074 flagsStr
<< _T("modem ");
3076 if ( flags
& wxVCardPhoneNumber::Home
)
3078 flagsStr
<< _T("home ");
3080 if ( flags
& wxVCardPhoneNumber::Work
)
3082 flagsStr
<< _T("work ");
3085 wxPrintf(_T("Phone number %u:\n")
3090 phone
->GetNumber().c_str()
3094 phone
= vcard
.GetNextPhoneNumber(&cookie
);
3098 static void TestVCardRead()
3100 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3102 wxVCard
vcard(_T("vcard.vcf"));
3103 if ( !vcard
.IsOk() )
3105 wxPuts(_T("ERROR: couldn't load vCard."));
3109 // read individual vCard properties
3110 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3114 vcObj
->GetValue(&value
);
3119 value
= _T("<none>");
3122 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3125 if ( !vcard
.GetFullName(&value
) )
3127 value
= _T("<none>");
3130 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3132 // now show how to deal with multiply occurring properties
3133 DumpVCardAddresses(vcard
);
3134 DumpVCardPhoneNumbers(vcard
);
3136 // and finally show all
3137 wxPuts(_T("\nNow dumping the entire vCard:\n")
3138 "-----------------------------\n");
3140 DumpVObject(0, vcard
);
3144 static void TestVCardWrite()
3146 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3149 if ( !vcard
.IsOk() )
3151 wxPuts(_T("ERROR: couldn't create vCard."));
3156 vcard
.SetName("Zeitlin", "Vadim");
3157 vcard
.SetFullName("Vadim Zeitlin");
3158 vcard
.SetOrganization("wxWidgets", "R&D");
3160 // just dump the vCard back
3161 wxPuts(_T("Entire vCard follows:\n"));
3162 wxPuts(vcard
.Write());
3166 #endif // TEST_VCARD
3168 // ----------------------------------------------------------------------------
3170 // ----------------------------------------------------------------------------
3172 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3178 #include "wx/volume.h"
3180 static const wxChar
*volumeKinds
[] =
3186 _T("network volume"),
3190 static void TestFSVolume()
3192 wxPuts(_T("*** Testing wxFSVolume class ***"));
3194 wxArrayString volumes
= wxFSVolume::GetVolumes();
3195 size_t count
= volumes
.GetCount();
3199 wxPuts(_T("ERROR: no mounted volumes?"));
3203 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3205 for ( size_t n
= 0; n
< count
; n
++ )
3207 wxFSVolume
vol(volumes
[n
]);
3210 wxPuts(_T("ERROR: couldn't create volume"));
3214 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3216 vol
.GetDisplayName().c_str(),
3217 vol
.GetName().c_str(),
3218 volumeKinds
[vol
.GetKind()],
3219 vol
.IsWritable() ? _T("rw") : _T("ro"),
3220 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3225 #endif // TEST_VOLUME
3227 // ----------------------------------------------------------------------------
3228 // wide char and Unicode support
3229 // ----------------------------------------------------------------------------
3233 #include "wx/strconv.h"
3234 #include "wx/fontenc.h"
3235 #include "wx/encconv.h"
3236 #include "wx/buffer.h"
3238 static const unsigned char utf8koi8r
[] =
3240 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3241 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3242 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3243 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3244 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3245 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3246 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3249 static const unsigned char utf8iso8859_1
[] =
3251 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3252 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3253 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3254 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3255 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3258 static const unsigned char utf8Invalid
[] =
3260 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3261 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3262 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3263 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3267 static const struct Utf8Data
3269 const unsigned char *text
;
3271 const wxChar
*charset
;
3272 wxFontEncoding encoding
;
3275 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3276 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3277 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3280 static void TestUtf8()
3282 wxPuts(_T("*** Testing UTF8 support ***\n"));
3287 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3289 const Utf8Data
& u8d
= utf8data
[n
];
3290 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3291 WXSIZEOF(wbuf
)) == (size_t)-1 )
3293 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3297 wxCSConv
conv(u8d
.charset
);
3298 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3300 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3304 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3308 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3310 s
= _T("<< conversion failed >>");
3311 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3315 wxPuts(wxEmptyString
);
3318 static void TestEncodingConverter()
3320 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3322 // using wxEncodingConverter should give the same result as above
3325 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3326 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3328 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3332 wxEncodingConverter ec
;
3333 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3334 ec
.Convert(wbuf
, buf
);
3335 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3338 wxPuts(wxEmptyString
);
3341 #endif // TEST_WCHAR
3343 // ----------------------------------------------------------------------------
3345 // ----------------------------------------------------------------------------
3349 #include "wx/filesys.h"
3350 #include "wx/fs_zip.h"
3351 #include "wx/zipstrm.h"
3353 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3355 static void TestZipStreamRead()
3357 wxPuts(_T("*** Testing ZIP reading ***\n"));
3359 static const wxString filename
= _T("foo");
3360 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3361 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3363 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3364 while ( !istr
.Eof() )
3366 wxPutchar(istr
.GetC());
3370 wxPuts(_T("\n----- done ------"));
3373 static void DumpZipDirectory(wxFileSystem
& fs
,
3374 const wxString
& dir
,
3375 const wxString
& indent
)
3377 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3378 TESTFILE_ZIP
, dir
.c_str());
3379 wxString wildcard
= prefix
+ _T("/*");
3381 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3382 while ( !dirname
.empty() )
3384 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3386 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3391 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3393 DumpZipDirectory(fs
, dirname
,
3394 indent
+ wxString(_T(' '), 4));
3396 dirname
= fs
.FindNext();
3399 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3400 while ( !filename
.empty() )
3402 if ( !filename
.StartsWith(prefix
, &filename
) )
3404 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3409 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3411 filename
= fs
.FindNext();
3415 static void TestZipFileSystem()
3417 wxPuts(_T("*** Testing ZIP file system ***\n"));
3419 wxFileSystem::AddHandler(new wxZipFSHandler
);
3421 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3423 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3428 // ----------------------------------------------------------------------------
3430 // ----------------------------------------------------------------------------
3432 #ifdef TEST_DATETIME
3434 #include "wx/math.h"
3435 #include "wx/datetime.h"
3437 // this test miscellaneous static wxDateTime functions
3441 static void TestTimeStatic()
3443 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3445 // some info about the current date
3446 int year
= wxDateTime::GetCurrentYear();
3447 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3449 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3450 wxDateTime::GetNumberOfDays(year
));
3452 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3453 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3454 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3455 wxDateTime::GetMonthName(month
).c_str(),
3456 wxDateTime::GetNumberOfDays(month
));
3459 // test time zones stuff
3460 static void TestTimeZones()
3462 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3464 wxDateTime now
= wxDateTime::Now();
3466 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3467 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3468 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3469 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3470 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3471 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3473 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3475 wxDateTime::Tm tm
= now
.GetTm();
3476 if ( wxDateTime(tm
) != now
)
3478 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3479 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3483 // test some minimal support for the dates outside the standard range
3484 static void TestTimeRange()
3486 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3488 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3490 wxPrintf(_T("Unix epoch:\t%s\n"),
3491 wxDateTime(2440587.5).Format(fmt
).c_str());
3492 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3493 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3494 wxPrintf(_T("JDN 0: \t%s\n"),
3495 wxDateTime(0.0).Format(fmt
).c_str());
3496 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3497 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3498 wxPrintf(_T("May 29, 2099:\t%s\n"),
3499 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3502 // test DST calculations
3503 static void TestTimeDST()
3505 wxPuts(_T("\n*** wxDateTime DST test ***"));
3507 wxPrintf(_T("DST is%s in effect now.\n\n"),
3508 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3510 for ( int year
= 1990; year
< 2005; year
++ )
3512 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3514 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3515 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3521 #if TEST_INTERACTIVE
3523 static void TestDateTimeInteractive()
3525 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3531 wxPrintf(_T("Enter a date: "));
3532 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3535 // kill the last '\n'
3536 buf
[wxStrlen(buf
) - 1] = 0;
3539 const wxChar
*p
= dt
.ParseDate(buf
);
3542 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3548 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3551 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3552 dt
.Format(_T("%b %d, %Y")).c_str(),
3554 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3555 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3556 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3559 wxPuts(_T("\n*** done ***"));
3562 #endif // TEST_INTERACTIVE
3566 static void TestTimeMS()
3568 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3570 wxDateTime dt1
= wxDateTime::Now(),
3571 dt2
= wxDateTime::UNow();
3573 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3574 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3575 wxPrintf(_T("Dummy loop: "));
3576 for ( int i
= 0; i
< 6000; i
++ )
3578 //for ( int j = 0; j < 10; j++ )
3581 s
.Printf(_T("%g"), sqrt((float)i
));
3587 wxPuts(_T(", done"));
3590 dt2
= wxDateTime::UNow();
3591 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3593 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3595 wxPuts(_T("\n*** done ***"));
3598 static void TestTimeHolidays()
3600 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3602 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3603 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3604 dtEnd
= dtStart
.GetLastMonthDay();
3606 wxDateTimeArray hol
;
3607 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3609 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3611 wxPrintf(_T("All holidays between %s and %s:\n"),
3612 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3614 size_t count
= hol
.GetCount();
3615 for ( size_t n
= 0; n
< count
; n
++ )
3617 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3620 wxPuts(wxEmptyString
);
3623 static void TestTimeZoneBug()
3625 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3627 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3628 for ( int i
= 0; i
< 31; i
++ )
3630 wxPrintf(_T("Date %s: week day %s.\n"),
3631 date
.Format(_T("%d-%m-%Y")).c_str(),
3632 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3634 date
+= wxDateSpan::Day();
3637 wxPuts(wxEmptyString
);
3640 static void TestTimeSpanFormat()
3642 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3644 static const wxChar
*formats
[] =
3646 _T("(default) %H:%M:%S"),
3647 _T("%E weeks and %D days"),
3648 _T("%l milliseconds"),
3649 _T("(with ms) %H:%M:%S:%l"),
3650 _T("100%% of minutes is %M"), // test "%%"
3651 _T("%D days and %H hours"),
3652 _T("or also %S seconds"),
3655 wxTimeSpan
ts1(1, 2, 3, 4),
3657 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3659 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3660 ts1
.Format(formats
[n
]).c_str(),
3661 ts2
.Format(formats
[n
]).c_str());
3664 wxPuts(wxEmptyString
);
3669 #endif // TEST_DATETIME
3671 // ----------------------------------------------------------------------------
3672 // wxTextInput/OutputStream
3673 // ----------------------------------------------------------------------------
3675 #ifdef TEST_TEXTSTREAM
3677 #include "wx/txtstrm.h"
3678 #include "wx/wfstream.h"
3680 static void TestTextInputStream()
3682 wxPuts(_T("\n*** wxTextInputStream test ***"));
3684 wxString filename
= _T("testdata.fc");
3685 wxFileInputStream
fsIn(filename
);
3688 wxPuts(_T("ERROR: couldn't open file."));
3692 wxTextInputStream
tis(fsIn
);
3697 const wxString s
= tis
.ReadLine();
3699 // line could be non empty if the last line of the file isn't
3700 // terminated with EOL
3701 if ( fsIn
.Eof() && s
.empty() )
3704 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3709 #endif // TEST_TEXTSTREAM
3711 // ----------------------------------------------------------------------------
3713 // ----------------------------------------------------------------------------
3717 #include "wx/thread.h"
3719 static size_t gs_counter
= (size_t)-1;
3720 static wxCriticalSection gs_critsect
;
3721 static wxSemaphore gs_cond
;
3723 class MyJoinableThread
: public wxThread
3726 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3727 { m_n
= n
; Create(); }
3729 // thread execution starts here
3730 virtual ExitCode
Entry();
3736 wxThread::ExitCode
MyJoinableThread::Entry()
3738 unsigned long res
= 1;
3739 for ( size_t n
= 1; n
< m_n
; n
++ )
3743 // it's a loooong calculation :-)
3747 return (ExitCode
)res
;
3750 class MyDetachedThread
: public wxThread
3753 MyDetachedThread(size_t n
, wxChar ch
)
3757 m_cancelled
= false;
3762 // thread execution starts here
3763 virtual ExitCode
Entry();
3766 virtual void OnExit();
3769 size_t m_n
; // number of characters to write
3770 wxChar m_ch
; // character to write
3772 bool m_cancelled
; // false if we exit normally
3775 wxThread::ExitCode
MyDetachedThread::Entry()
3778 wxCriticalSectionLocker
lock(gs_critsect
);
3779 if ( gs_counter
== (size_t)-1 )
3785 for ( size_t n
= 0; n
< m_n
; n
++ )
3787 if ( TestDestroy() )
3797 wxThread::Sleep(100);
3803 void MyDetachedThread::OnExit()
3805 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3807 wxCriticalSectionLocker
lock(gs_critsect
);
3808 if ( !--gs_counter
&& !m_cancelled
)
3812 static void TestDetachedThreads()
3814 wxPuts(_T("\n*** Testing detached threads ***"));
3816 static const size_t nThreads
= 3;
3817 MyDetachedThread
*threads
[nThreads
];
3819 for ( n
= 0; n
< nThreads
; n
++ )
3821 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3824 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3825 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3827 for ( n
= 0; n
< nThreads
; n
++ )
3832 // wait until all threads terminate
3835 wxPuts(wxEmptyString
);
3838 static void TestJoinableThreads()
3840 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3842 // calc 10! in the background
3843 MyJoinableThread
thread(10);
3846 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3847 (unsigned long)thread
.Wait());
3850 static void TestThreadSuspend()
3852 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3854 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3858 // this is for this demo only, in a real life program we'd use another
3859 // condition variable which would be signaled from wxThread::Entry() to
3860 // tell us that the thread really started running - but here just wait a
3861 // bit and hope that it will be enough (the problem is, of course, that
3862 // the thread might still not run when we call Pause() which will result
3864 wxThread::Sleep(300);
3866 for ( size_t n
= 0; n
< 3; n
++ )
3870 wxPuts(_T("\nThread suspended"));
3873 // don't sleep but resume immediately the first time
3874 wxThread::Sleep(300);
3876 wxPuts(_T("Going to resume the thread"));
3881 wxPuts(_T("Waiting until it terminates now"));
3883 // wait until the thread terminates
3886 wxPuts(wxEmptyString
);
3889 static void TestThreadDelete()
3891 // As above, using Sleep() is only for testing here - we must use some
3892 // synchronisation object instead to ensure that the thread is still
3893 // running when we delete it - deleting a detached thread which already
3894 // terminated will lead to a crash!
3896 wxPuts(_T("\n*** Testing thread delete function ***"));
3898 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3902 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3904 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3908 wxThread::Sleep(300);
3912 wxPuts(_T("\nDeleted a running thread."));
3914 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3918 wxThread::Sleep(300);
3924 wxPuts(_T("\nDeleted a sleeping thread."));
3926 MyJoinableThread
thread3(20);
3931 wxPuts(_T("\nDeleted a joinable thread."));
3933 MyJoinableThread
thread4(2);
3936 wxThread::Sleep(300);
3940 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3942 wxPuts(wxEmptyString
);
3945 class MyWaitingThread
: public wxThread
3948 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3951 m_condition
= condition
;
3956 virtual ExitCode
Entry()
3958 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3963 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3967 m_condition
->Wait();
3970 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3978 wxCondition
*m_condition
;
3981 static void TestThreadConditions()
3984 wxCondition
condition(mutex
);
3986 // otherwise its difficult to understand which log messages pertain to
3988 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3989 // condition.GetId(), gs_cond.GetId());
3991 // create and launch threads
3992 MyWaitingThread
*threads
[10];
3995 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3997 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
4000 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
4005 // wait until all threads run
4006 wxPuts(_T("Main thread is waiting for the other threads to start"));
4009 size_t nRunning
= 0;
4010 while ( nRunning
< WXSIZEOF(threads
) )
4016 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
4020 wxPuts(_T("Main thread: all threads started up."));
4023 wxThread::Sleep(500);
4026 // now wake one of them up
4027 wxPrintf(_T("Main thread: about to signal the condition.\n"));
4032 wxThread::Sleep(200);
4034 // wake all the (remaining) threads up, so that they can exit
4035 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
4037 condition
.Broadcast();
4039 // give them time to terminate (dirty!)
4040 wxThread::Sleep(500);
4043 #include "wx/utils.h"
4045 class MyExecThread
: public wxThread
4048 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
4054 virtual ExitCode
Entry()
4056 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
4063 static void TestThreadExec()
4065 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4067 MyExecThread
thread(_T("true"));
4070 wxPrintf(_T("Main program exit code: %ld.\n"),
4071 wxExecute(_T("false"), wxEXEC_SYNC
));
4073 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
4077 #include "wx/datetime.h"
4079 class MySemaphoreThread
: public wxThread
4082 MySemaphoreThread(int i
, wxSemaphore
*sem
)
4083 : wxThread(wxTHREAD_JOINABLE
),
4090 virtual ExitCode
Entry()
4092 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4093 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4097 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4098 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4102 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4103 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4115 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
4117 static void TestSemaphore()
4119 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4121 static const int SEM_LIMIT
= 3;
4123 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
4124 ArrayThreads threads
;
4126 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
4128 threads
.Add(new MySemaphoreThread(i
, &sem
));
4129 threads
.Last()->Run();
4132 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4139 #endif // TEST_THREADS
4141 // ----------------------------------------------------------------------------
4143 // ----------------------------------------------------------------------------
4145 #ifdef TEST_SNGLINST
4146 #include "wx/snglinst.h"
4147 #endif // TEST_SNGLINST
4149 int main(int argc
, char **argv
)
4152 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4157 for (n
= 0; n
< argc
; n
++ )
4159 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4160 wxArgv
[n
] = wxStrdup(warg
);
4165 #else // !wxUSE_UNICODE
4167 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4169 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4171 wxInitializer initializer
;
4174 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4179 #ifdef TEST_SNGLINST
4180 wxSingleInstanceChecker checker
;
4181 if ( checker
.Create(_T(".wxconsole.lock")) )
4183 if ( checker
.IsAnotherRunning() )
4185 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4190 // wait some time to give time to launch another instance
4191 wxPrintf(_T("Press \"Enter\" to continue..."));
4194 else // failed to create
4196 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4198 #endif // TEST_SNGLINST
4201 TestCmdLineConvert();
4203 #if wxUSE_CMDLINE_PARSER
4204 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4206 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
4207 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4208 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
4209 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
4211 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
4212 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
4213 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
4214 wxCMD_LINE_VAL_NUMBER
},
4215 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
4216 wxCMD_LINE_VAL_DATE
},
4218 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
4219 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4224 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4226 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4227 wxCMD_LINE_VAL_STRING
,
4228 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4230 switch ( parser
.Parse() )
4233 wxLogMessage(_T("Help was given, terminating."));
4237 ShowCmdLine(parser
);
4241 wxLogMessage(_T("Syntax error detected, aborting."));
4244 #endif // wxUSE_CMDLINE_PARSER
4246 #endif // TEST_CMDLINE
4258 TestDllListLoaded();
4259 #endif // TEST_DYNLIB
4263 #endif // TEST_ENVIRON
4267 #endif // TEST_EXECUTE
4269 #ifdef TEST_FILECONF
4271 #endif // TEST_FILECONF
4275 #endif // TEST_LOCALE
4278 wxPuts(_T("*** Testing wxLog ***"));
4281 for ( size_t n
= 0; n
< 8000; n
++ )
4283 s
<< (wxChar
)(_T('A') + (n
% 26));
4286 wxLogWarning(_T("The length of the string is %lu"),
4287 (unsigned long)s
.length());
4290 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4292 // this one shouldn't be truncated
4295 // but this one will because log functions use fixed size buffer
4296 // (note that it doesn't need '\n' at the end neither - will be added
4298 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4307 #ifdef TEST_FILENAME
4310 TestFileNameDirManip();
4311 TestFileNameComparison();
4312 TestFileNameOperations();
4313 #endif // TEST_FILENAME
4315 #ifdef TEST_FILETIME
4320 #endif // TEST_FILETIME
4323 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4324 if ( TestFtpConnect() )
4334 #if TEST_INTERACTIVE
4335 TestFtpInteractive();
4338 //else: connecting to the FTP server failed
4346 wxLog::AddTraceMask(_T("mime"));
4350 TestMimeAssociate();
4355 #ifdef TEST_INFO_FUNCTIONS
4360 #if TEST_INTERACTIVE
4363 #endif // TEST_INFO_FUNCTIONS
4365 #ifdef TEST_PATHLIST
4367 #endif // TEST_PATHLIST
4375 #endif // TEST_PRINTF
4382 #endif // TEST_REGCONF
4384 #if defined TEST_REGEX && TEST_INTERACTIVE
4385 TestRegExInteractive();
4386 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4388 #ifdef TEST_REGISTRY
4390 TestRegistryAssociation();
4391 #endif // TEST_REGISTRY
4396 #endif // TEST_SOCKETS
4403 #endif // TEST_STREAMS
4405 #ifdef TEST_TEXTSTREAM
4406 TestTextInputStream();
4407 #endif // TEST_TEXTSTREAM
4410 int nCPUs
= wxThread::GetCPUCount();
4411 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4413 wxThread::SetConcurrency(nCPUs
);
4415 TestJoinableThreads();
4418 TestJoinableThreads();
4419 TestDetachedThreads();
4420 TestThreadSuspend();
4422 TestThreadConditions();
4426 #endif // TEST_THREADS
4430 #endif // TEST_TIMER
4432 #ifdef TEST_DATETIME
4439 TestTimeSpanFormat();
4445 #if TEST_INTERACTIVE
4446 TestDateTimeInteractive();
4448 #endif // TEST_DATETIME
4450 #ifdef TEST_SCOPEGUARD
4454 #ifdef TEST_STACKWALKER
4455 #if wxUSE_STACKWALKER
4456 TestStackWalk(argv
[0]);
4458 #endif // TEST_STACKWALKER
4460 #ifdef TEST_STDPATHS
4461 TestStandardPaths();
4465 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4467 #endif // TEST_USLEEP
4472 #endif // TEST_VCARD
4476 #endif // TEST_VOLUME
4480 TestEncodingConverter();
4481 #endif // TEST_WCHAR
4484 TestZipStreamRead();
4485 TestZipFileSystem();
4490 for ( int n
= 0; n
< argc
; n
++ )
4495 #endif // wxUSE_UNICODE