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"
30 #include "wx/wxchar.h"
32 // without this pragma, the stupid compiler precompiles #defines below so that
33 // changing them doesn't "take place" later!
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
43 A note about all these conditional compilation macros: this file is used
44 both as a test suite for various non-GUI wxWidgets classes and as a
45 scratchpad for quick tests. So there are two compilation modes: if you
46 define TEST_ALL all tests are run, otherwise you may enable the individual
47 tests individually in the "#else" branch below.
50 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
51 // test, define it to 1 to do all tests.
66 // #define TEST_FTP --FIXME! (RN)
67 #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)
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("Command line parsed successfully:\nInput 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
);
723 static void TestTempFile()
725 wxPuts(_T("*** wxTempFile test ***"));
728 if ( tmpFile
.Open(_T("test2")) && tmpFile
.Write(_T("the answer is 42")) )
730 if ( tmpFile
.Commit() )
731 wxPuts(_T("File committed."));
733 wxPuts(_T("ERROR: could't commit temp file."));
735 wxRemoveFile(_T("test2"));
738 wxPuts(wxEmptyString
);
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
749 #include "wx/confbase.h"
750 #include "wx/fileconf.h"
752 static const struct FileConfTestData
754 const wxChar
*name
; // value name
755 const wxChar
*value
; // the value from the file
758 { _T("value1"), _T("one") },
759 { _T("value2"), _T("two") },
760 { _T("novalue"), _T("default") },
763 static void TestFileConfRead()
765 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
767 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
768 _T("testdata.fc"), wxEmptyString
,
769 wxCONFIG_USE_RELATIVE_PATH
);
771 // test simple reading
772 wxPuts(_T("\nReading config file:"));
773 wxString
defValue(_T("default")), value
;
774 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
776 const FileConfTestData
& data
= fcTestData
[n
];
777 value
= fileconf
.Read(data
.name
, defValue
);
778 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
779 if ( value
== data
.value
)
785 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
789 // test enumerating the entries
790 wxPuts(_T("\nEnumerating all root entries:"));
793 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
796 wxPrintf(_T("\t%s = %s\n"),
798 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
800 cont
= fileconf
.GetNextEntry(name
, dummy
);
803 static const wxChar
*testEntry
= _T("TestEntry");
804 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
805 fileconf
.Write(testEntry
, _T("A value"));
806 fileconf
.DeleteEntry(testEntry
);
807 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
810 #endif // TEST_FILECONF
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
818 #include "wx/filename.h"
821 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
825 wxString full
= fn
.GetFullPath();
827 wxString vol
, path
, name
, ext
;
828 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
830 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
831 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
833 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
834 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
835 path
.c_str(), name
.c_str(), ext
.c_str());
837 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
838 wxPrintf(_T("with volume: \t'%s'\n"),
839 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
840 wxPrintf(_T("with separator:\t'%s'\n"),
841 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
842 wxPrintf(_T("with both: \t'%s'\n"),
843 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
845 wxPuts(_T("The directories in the path are:"));
846 wxArrayString dirs
= fn
.GetDirs();
847 size_t count
= dirs
.GetCount();
848 for ( size_t n
= 0; n
< count
; n
++ )
850 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
855 static void TestFileNameTemp()
857 wxPuts(_T("*** testing wxFileName temp file creation ***"));
859 static const wxChar
*tmpprefixes
[] =
867 _T("/tmp/foo/bar"), // this one must be an error
871 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
873 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
876 // "error" is not in upper case because it may be ok
877 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
881 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
882 tmpprefixes
[n
], path
.c_str());
884 if ( !wxRemoveFile(path
) )
886 wxLogWarning(_T("Failed to remove temp file '%s'"),
893 static void TestFileNameDirManip()
895 // TODO: test AppendDir(), RemoveDir(), ...
898 static void TestFileNameComparison()
903 static void TestFileNameOperations()
908 static void TestFileNameCwd()
913 #endif // TEST_FILENAME
915 // ----------------------------------------------------------------------------
916 // wxFileName time functions
917 // ----------------------------------------------------------------------------
921 #include "wx/filename.h"
922 #include "wx/datetime.h"
924 static void TestFileGetTimes()
926 wxFileName
fn(_T("testdata.fc"));
928 wxDateTime dtAccess
, dtMod
, dtCreate
;
929 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
931 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
935 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
937 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
938 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
939 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
940 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
945 static void TestFileSetTimes()
947 wxFileName
fn(_T("testdata.fc"));
951 wxPrintf(_T("ERROR: Touch() failed.\n"));
956 #endif // TEST_FILETIME
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
965 #include "wx/utils.h" // for wxSetEnv
967 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
969 // find the name of the language from its value
970 static const wxChar
*GetLangName(int lang
)
972 static const wxChar
*languageNames
[] =
982 _T("ARABIC_ALGERIA"),
983 _T("ARABIC_BAHRAIN"),
988 _T("ARABIC_LEBANON"),
990 _T("ARABIC_MOROCCO"),
993 _T("ARABIC_SAUDI_ARABIA"),
996 _T("ARABIC_TUNISIA"),
1003 _T("AZERI_CYRILLIC"),
1018 _T("CHINESE_SIMPLIFIED"),
1019 _T("CHINESE_TRADITIONAL"),
1020 _T("CHINESE_HONGKONG"),
1021 _T("CHINESE_MACAU"),
1022 _T("CHINESE_SINGAPORE"),
1023 _T("CHINESE_TAIWAN"),
1029 _T("DUTCH_BELGIAN"),
1033 _T("ENGLISH_AUSTRALIA"),
1034 _T("ENGLISH_BELIZE"),
1035 _T("ENGLISH_BOTSWANA"),
1036 _T("ENGLISH_CANADA"),
1037 _T("ENGLISH_CARIBBEAN"),
1038 _T("ENGLISH_DENMARK"),
1040 _T("ENGLISH_JAMAICA"),
1041 _T("ENGLISH_NEW_ZEALAND"),
1042 _T("ENGLISH_PHILIPPINES"),
1043 _T("ENGLISH_SOUTH_AFRICA"),
1044 _T("ENGLISH_TRINIDAD"),
1045 _T("ENGLISH_ZIMBABWE"),
1053 _T("FRENCH_BELGIAN"),
1054 _T("FRENCH_CANADIAN"),
1055 _T("FRENCH_LUXEMBOURG"),
1056 _T("FRENCH_MONACO"),
1062 _T("GERMAN_AUSTRIAN"),
1063 _T("GERMAN_BELGIUM"),
1064 _T("GERMAN_LIECHTENSTEIN"),
1065 _T("GERMAN_LUXEMBOURG"),
1083 _T("ITALIAN_SWISS"),
1088 _T("KASHMIRI_INDIA"),
1106 _T("MALAY_BRUNEI_DARUSSALAM"),
1107 _T("MALAY_MALAYSIA"),
1117 _T("NORWEGIAN_BOKMAL"),
1118 _T("NORWEGIAN_NYNORSK"),
1125 _T("PORTUGUESE_BRAZILIAN"),
1128 _T("RHAETO_ROMANCE"),
1131 _T("RUSSIAN_UKRAINE"),
1137 _T("SERBIAN_CYRILLIC"),
1138 _T("SERBIAN_LATIN"),
1139 _T("SERBO_CROATIAN"),
1150 _T("SPANISH_ARGENTINA"),
1151 _T("SPANISH_BOLIVIA"),
1152 _T("SPANISH_CHILE"),
1153 _T("SPANISH_COLOMBIA"),
1154 _T("SPANISH_COSTA_RICA"),
1155 _T("SPANISH_DOMINICAN_REPUBLIC"),
1156 _T("SPANISH_ECUADOR"),
1157 _T("SPANISH_EL_SALVADOR"),
1158 _T("SPANISH_GUATEMALA"),
1159 _T("SPANISH_HONDURAS"),
1160 _T("SPANISH_MEXICAN"),
1161 _T("SPANISH_MODERN"),
1162 _T("SPANISH_NICARAGUA"),
1163 _T("SPANISH_PANAMA"),
1164 _T("SPANISH_PARAGUAY"),
1166 _T("SPANISH_PUERTO_RICO"),
1167 _T("SPANISH_URUGUAY"),
1169 _T("SPANISH_VENEZUELA"),
1173 _T("SWEDISH_FINLAND"),
1191 _T("URDU_PAKISTAN"),
1193 _T("UZBEK_CYRILLIC"),
1206 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1207 return languageNames
[lang
];
1209 return _T("INVALID");
1212 static void TestDefaultLang()
1214 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1216 static const wxChar
*langStrings
[] =
1218 NULL
, // system default
1225 _T("de_DE.iso88591"),
1227 _T("?"), // invalid lang spec
1228 _T("klingonese"), // I bet on some systems it does exist...
1231 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1232 wxLocale::GetSystemEncodingName().c_str(),
1233 wxLocale::GetSystemEncoding());
1235 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1237 const wxChar
*langStr
= langStrings
[n
];
1240 // FIXME: this doesn't do anything at all under Windows, we need
1241 // to create a new wxLocale!
1242 wxSetEnv(_T("LC_ALL"), langStr
);
1245 int lang
= gs_localeDefault
.GetSystemLanguage();
1246 wxPrintf(_T("Locale for '%s' is %s.\n"),
1247 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1251 #endif // TEST_LOCALE
1253 // ----------------------------------------------------------------------------
1255 // ----------------------------------------------------------------------------
1259 #include "wx/mimetype.h"
1261 static void TestMimeEnum()
1263 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1265 wxArrayString mimetypes
;
1267 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1269 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1274 for ( size_t n
= 0; n
< count
; n
++ )
1276 wxFileType
*filetype
=
1277 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1280 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1281 mimetypes
[n
].c_str());
1285 filetype
->GetDescription(&desc
);
1286 filetype
->GetExtensions(exts
);
1288 filetype
->GetIcon(NULL
);
1291 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1294 extsAll
<< _T(", ");
1298 wxPrintf(_T("\t%s: %s (%s)\n"),
1299 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1302 wxPuts(wxEmptyString
);
1305 static void TestMimeOverride()
1307 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1309 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1310 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1312 if ( wxFile::Exists(mailcap
) )
1313 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1315 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1317 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1320 if ( wxFile::Exists(mimetypes
) )
1321 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1323 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1325 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1328 wxPuts(wxEmptyString
);
1331 static void TestMimeFilename()
1333 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1335 static const wxChar
*filenames
[] =
1343 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1345 const wxString fname
= filenames
[n
];
1346 wxString ext
= fname
.AfterLast(_T('.'));
1347 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1350 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1355 if ( !ft
->GetDescription(&desc
) )
1356 desc
= _T("<no description>");
1359 if ( !ft
->GetOpenCommand(&cmd
,
1360 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1361 cmd
= _T("<no command available>");
1363 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1365 wxPrintf(_T("To open %s (%s) do %s.\n"),
1366 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1372 wxPuts(wxEmptyString
);
1375 static void TestMimeAssociate()
1377 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1379 wxFileTypeInfo
ftInfo(
1380 _T("application/x-xyz"),
1381 _T("xyzview '%s'"), // open cmd
1382 _T(""), // print cmd
1383 _T("XYZ File"), // description
1384 _T(".xyz"), // extensions
1385 wxNullPtr
// end of extensions
1387 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1389 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1392 wxPuts(_T("ERROR: failed to create association!"));
1396 // TODO: read it back
1400 wxPuts(wxEmptyString
);
1405 // ----------------------------------------------------------------------------
1406 // module dependencies feature
1407 // ----------------------------------------------------------------------------
1411 #include "wx/module.h"
1413 class wxTestModule
: public wxModule
1416 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1417 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1420 class wxTestModuleA
: public wxTestModule
1425 DECLARE_DYNAMIC_CLASS(wxTestModuleA
)
1428 class wxTestModuleB
: public wxTestModule
1433 DECLARE_DYNAMIC_CLASS(wxTestModuleB
)
1436 class wxTestModuleC
: public wxTestModule
1441 DECLARE_DYNAMIC_CLASS(wxTestModuleC
)
1444 class wxTestModuleD
: public wxTestModule
1449 DECLARE_DYNAMIC_CLASS(wxTestModuleD
)
1452 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC
, wxModule
)
1453 wxTestModuleC::wxTestModuleC()
1455 AddDependency(CLASSINFO(wxTestModuleD
));
1458 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA
, wxModule
)
1459 wxTestModuleA::wxTestModuleA()
1461 AddDependency(CLASSINFO(wxTestModuleB
));
1462 AddDependency(CLASSINFO(wxTestModuleD
));
1465 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD
, wxModule
)
1466 wxTestModuleD::wxTestModuleD()
1470 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB
, wxModule
)
1471 wxTestModuleB::wxTestModuleB()
1473 AddDependency(CLASSINFO(wxTestModuleD
));
1474 AddDependency(CLASSINFO(wxTestModuleC
));
1477 #endif // TEST_MODULE
1479 // ----------------------------------------------------------------------------
1480 // misc information functions
1481 // ----------------------------------------------------------------------------
1483 #ifdef TEST_INFO_FUNCTIONS
1485 #include "wx/utils.h"
1487 #if TEST_INTERACTIVE
1488 static void TestDiskInfo()
1490 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1494 wxChar pathname
[128];
1495 wxPrintf(_T("\nEnter a directory name: "));
1496 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1499 // kill the last '\n'
1500 pathname
[wxStrlen(pathname
) - 1] = 0;
1502 wxLongLong total
, free
;
1503 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1505 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1509 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1510 (total
/ 1024).ToString().c_str(),
1511 (free
/ 1024).ToString().c_str(),
1516 #endif // TEST_INTERACTIVE
1518 static void TestOsInfo()
1520 wxPuts(_T("*** Testing OS info functions ***\n"));
1523 wxGetOsVersion(&major
, &minor
);
1524 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1525 wxGetOsDescription().c_str(), major
, minor
);
1527 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1529 wxPrintf(_T("Host name is %s (%s).\n"),
1530 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1532 wxPuts(wxEmptyString
);
1535 static void TestPlatformInfo()
1537 wxPuts(_T("*** Testing wxPlatformInfo functions ***\n"));
1539 // get this platform
1540 wxPlatformInfo plat
;
1542 wxPrintf(_T("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
1543 wxPrintf(_T("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
1544 wxPrintf(_T("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
1545 wxPrintf(_T("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
1546 wxPrintf(_T("Architecture is: %s\n"), plat
.GetArchName().c_str());
1547 wxPrintf(_T("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
1549 wxPuts(wxEmptyString
);
1552 static void TestUserInfo()
1554 wxPuts(_T("*** Testing user info functions ***\n"));
1556 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1557 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1558 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1559 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1561 wxPuts(wxEmptyString
);
1564 #endif // TEST_INFO_FUNCTIONS
1566 // ----------------------------------------------------------------------------
1568 // ----------------------------------------------------------------------------
1570 #ifdef TEST_PATHLIST
1573 #define CMD_IN_PATH _T("ls")
1575 #define CMD_IN_PATH _T("command.com")
1578 static void TestPathList()
1580 wxPuts(_T("*** Testing wxPathList ***\n"));
1582 wxPathList pathlist
;
1583 pathlist
.AddEnvList(_T("PATH"));
1584 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1587 wxPrintf(_T("ERROR: command not found in the path.\n"));
1591 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1595 #endif // TEST_PATHLIST
1597 // ----------------------------------------------------------------------------
1598 // regular expressions
1599 // ----------------------------------------------------------------------------
1603 #include "wx/regex.h"
1605 static void TestRegExInteractive()
1607 wxPuts(_T("*** Testing RE interactively ***"));
1611 wxChar pattern
[128];
1612 wxPrintf(_T("\nEnter a pattern: "));
1613 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1616 // kill the last '\n'
1617 pattern
[wxStrlen(pattern
) - 1] = 0;
1620 if ( !re
.Compile(pattern
) )
1628 wxPrintf(_T("Enter text to match: "));
1629 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1632 // kill the last '\n'
1633 text
[wxStrlen(text
) - 1] = 0;
1635 if ( !re
.Matches(text
) )
1637 wxPrintf(_T("No match.\n"));
1641 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1644 for ( size_t n
= 1; ; n
++ )
1646 if ( !re
.GetMatch(&start
, &len
, n
) )
1651 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1652 n
, wxString(text
+ start
, len
).c_str());
1659 #endif // TEST_REGEX
1661 // ----------------------------------------------------------------------------
1663 // ----------------------------------------------------------------------------
1666 NB: this stuff was taken from the glibc test suite and modified to build
1667 in wxWidgets: if I read the copyright below properly, this shouldn't
1673 #ifdef wxTEST_PRINTF
1674 // use our functions from wxchar.cpp
1678 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1679 // in the tests below
1680 int wxPrintf( const wxChar
*format
, ... );
1681 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1684 #include "wx/longlong.h"
1688 static void rfg1 (void);
1689 static void rfg2 (void);
1693 fmtchk (const wxChar
*fmt
)
1695 (void) wxPrintf(_T("%s:\t`"), fmt
);
1696 (void) wxPrintf(fmt
, 0x12);
1697 (void) wxPrintf(_T("'\n"));
1701 fmtst1chk (const wxChar
*fmt
)
1703 (void) wxPrintf(_T("%s:\t`"), fmt
);
1704 (void) wxPrintf(fmt
, 4, 0x12);
1705 (void) wxPrintf(_T("'\n"));
1709 fmtst2chk (const wxChar
*fmt
)
1711 (void) wxPrintf(_T("%s:\t`"), fmt
);
1712 (void) wxPrintf(fmt
, 4, 4, 0x12);
1713 (void) wxPrintf(_T("'\n"));
1716 /* This page is covered by the following copyright: */
1718 /* (C) Copyright C E Chew
1720 * Feel free to copy, use and distribute this software provided:
1722 * 1. you do not pretend that you wrote it
1723 * 2. you leave this copyright notice intact.
1727 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1734 /* Formatted Output Test
1736 * This exercises the output formatting code.
1739 wxChar
*PointerNull
= NULL
;
1746 wxChar
*prefix
= buf
;
1749 wxPuts(_T("\nFormatted output test"));
1750 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1751 wxStrcpy(prefix
, _T("%"));
1752 for (i
= 0; i
< 2; i
++) {
1753 for (j
= 0; j
< 2; j
++) {
1754 for (k
= 0; k
< 2; k
++) {
1755 for (l
= 0; l
< 2; l
++) {
1756 wxStrcpy(prefix
, _T("%"));
1757 if (i
== 0) wxStrcat(prefix
, _T("-"));
1758 if (j
== 0) wxStrcat(prefix
, _T("+"));
1759 if (k
== 0) wxStrcat(prefix
, _T("#"));
1760 if (l
== 0) wxStrcat(prefix
, _T("0"));
1761 wxPrintf(_T("%5s |"), prefix
);
1762 wxStrcpy(tp
, prefix
);
1763 wxStrcat(tp
, _T("6d |"));
1765 wxStrcpy(tp
, prefix
);
1766 wxStrcat(tp
, _T("6o |"));
1768 wxStrcpy(tp
, prefix
);
1769 wxStrcat(tp
, _T("6x |"));
1771 wxStrcpy(tp
, prefix
);
1772 wxStrcat(tp
, _T("6X |"));
1774 wxStrcpy(tp
, prefix
);
1775 wxStrcat(tp
, _T("6u |"));
1782 wxPrintf(_T("%10s\n"), PointerNull
);
1783 wxPrintf(_T("%-10s\n"), PointerNull
);
1786 static void TestPrintf()
1788 static wxChar shortstr
[] = _T("Hi, Z.");
1789 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1790 I am ready for my first lesson today.");
1792 wxString test_format
;
1796 fmtchk(_T("%4.4x"));
1797 fmtchk(_T("%04.4x"));
1798 fmtchk(_T("%4.3x"));
1799 fmtchk(_T("%04.3x"));
1801 fmtst1chk(_T("%.*x"));
1802 fmtst1chk(_T("%0*x"));
1803 fmtst2chk(_T("%*.*x"));
1804 fmtst2chk(_T("%0*.*x"));
1806 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1807 wxPrintf(bad_format
.c_str());
1808 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1810 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1811 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1812 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1813 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1814 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1815 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1816 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1817 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1818 wxPrintf(test_format
.c_str(), -123456);
1819 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1820 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1822 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1823 wxPrintf(test_format
.c_str(), shortstr
);
1824 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1825 wxPrintf(test_format
.c_str(), shortstr
);
1826 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1827 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1828 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1829 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1831 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1832 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1833 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1834 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1835 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1836 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1837 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1838 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1839 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1840 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1841 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1842 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1844 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1845 wxPrintf (_T(" %6.5f\n"), .1);
1846 wxPrintf (_T("x%5.4fx\n"), .5);
1848 wxPrintf (_T("%#03x\n"), 1);
1850 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1856 while (niter
-- != 0)
1857 wxPrintf (_T("%.17e\n"), d
/ 2);
1862 // Open Watcom cause compiler error here
1863 // Error! E173: col(24) floating-point constant too small to represent
1864 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1867 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1868 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1869 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1870 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1871 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1872 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1873 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1874 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1875 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1876 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1881 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1883 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1884 rc
, WXSIZEOF(buf
), buf
);
1887 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1888 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1894 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1895 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1896 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1897 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1898 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1899 wxPrintf (_T("%g should be 10\n"), 10.0);
1900 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1904 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1910 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1912 result
|= wxStrcmp (buf
,
1913 _T("onetwo three "));
1915 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1922 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1924 // for some reason below line fails under Borland
1925 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1928 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1931 wxPuts (_T("\tFAILED"));
1933 wxUnusedVar(result
);
1934 wxPuts (wxEmptyString
);
1936 #endif // wxLongLong_t
1938 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1939 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1941 wxPuts (_T("--- Should be no further output. ---"));
1950 memset (bytes
, '\xff', sizeof bytes
);
1951 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1952 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1953 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1955 wxPuts (_T("%hhn overwrite more bytes"));
1960 wxPuts (_T("%hhn wrote incorrect value"));
1972 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1973 if (wxStrcmp (buf
, _T(" ")) != 0)
1974 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1975 wxSprintf (buf
, _T("%5.f"), 33.3);
1976 if (wxStrcmp (buf
, _T(" 33")) != 0)
1977 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1978 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1979 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1980 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1981 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1982 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1983 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1984 wxSprintf (buf
, _T("%.g"), 33.3);
1985 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1986 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1987 wxSprintf (buf
, _T("%.G"), 33.3);
1988 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1989 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1997 wxString test_format
;
2000 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
2001 if (wxStrcmp (buf
, _T("3")) != 0)
2002 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2004 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
2005 if (wxStrcmp (buf
, _T("3")) != 0)
2006 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2008 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
2009 if (wxStrcmp (buf
, _T(" 3")) != 0)
2010 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
2012 test_format
= _T("%04.*o");
2013 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2014 if (wxStrcmp (buf
, _T(" 041")) != 0)
2015 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
2017 test_format
= _T("%09.*u");
2018 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2019 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
2020 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
2022 test_format
= _T("%04.*x");
2023 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2024 if (wxStrcmp (buf
, _T(" 021")) != 0)
2025 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2027 test_format
= _T("%04.*X");
2028 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2029 if (wxStrcmp (buf
, _T(" 021")) != 0)
2030 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2033 #endif // TEST_PRINTF
2035 // ----------------------------------------------------------------------------
2036 // registry and related stuff
2037 // ----------------------------------------------------------------------------
2039 // this is for MSW only
2042 #undef TEST_REGISTRY
2047 #include "wx/confbase.h"
2048 #include "wx/msw/regconf.h"
2051 static void TestRegConfWrite()
2053 wxConfig
*config
= new wxConfig(_T("myapp"));
2054 config
->SetPath(_T("/group1"));
2055 config
->Write(_T("entry1"), _T("foo"));
2056 config
->SetPath(_T("/group2"));
2057 config
->Write(_T("entry1"), _T("bar"));
2061 static void TestRegConfRead()
2063 wxConfig
*config
= new wxConfig(_T("myapp"));
2067 config
->SetPath(_T("/"));
2068 wxPuts(_T("Enumerating / subgroups:"));
2069 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2073 bCont
= config
->GetNextGroup(str
, dummy
);
2077 #endif // TEST_REGCONF
2079 #ifdef TEST_REGISTRY
2081 #include "wx/msw/registry.h"
2083 // I chose this one because I liked its name, but it probably only exists under
2085 static const wxChar
*TESTKEY
=
2086 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2088 static void TestRegistryRead()
2090 wxPuts(_T("*** testing registry reading ***"));
2092 wxRegKey
key(TESTKEY
);
2093 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2096 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2101 size_t nSubKeys
, nValues
;
2102 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2104 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2107 wxPrintf(_T("Enumerating values:\n"));
2111 bool cont
= key
.GetFirstValue(value
, dummy
);
2114 wxPrintf(_T("Value '%s': type "), value
.c_str());
2115 switch ( key
.GetValueType(value
) )
2117 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2118 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2119 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2120 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2121 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2122 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2123 default: wxPrintf(_T("other (unknown)")); break;
2126 wxPrintf(_T(", value = "));
2127 if ( key
.IsNumericValue(value
) )
2130 key
.QueryValue(value
, &val
);
2131 wxPrintf(_T("%ld"), val
);
2136 key
.QueryValue(value
, val
);
2137 wxPrintf(_T("'%s'"), val
.c_str());
2139 key
.QueryRawValue(value
, val
);
2140 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2145 cont
= key
.GetNextValue(value
, dummy
);
2149 static void TestRegistryAssociation()
2152 The second call to deleteself genertaes an error message, with a
2153 messagebox saying .flo is crucial to system operation, while the .ddf
2154 call also fails, but with no error message
2159 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2161 key
= _T("ddxf_auto_file") ;
2162 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2164 key
= _T("ddxf_auto_file") ;
2165 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2167 key
= _T("program,0") ;
2168 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2170 key
= _T("program \"%1\"") ;
2172 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2174 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2176 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2178 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2182 #endif // TEST_REGISTRY
2184 // ----------------------------------------------------------------------------
2186 // ----------------------------------------------------------------------------
2188 #ifdef TEST_SCOPEGUARD
2190 #include "wx/scopeguard.h"
2192 static void function0() { puts("function0()"); }
2193 static void function1(int n
) { printf("function1(%d)\n", n
); }
2194 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2198 void method0() { printf("method0()\n"); }
2199 void method1(int n
) { printf("method1(%d)\n", n
); }
2200 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2203 static void TestScopeGuard()
2205 wxON_BLOCK_EXIT0(function0
);
2206 wxON_BLOCK_EXIT1(function1
, 17);
2207 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2210 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2211 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2212 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2214 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2215 dismissed
.Dismiss();
2220 // ----------------------------------------------------------------------------
2222 // ----------------------------------------------------------------------------
2226 #include "wx/socket.h"
2227 #include "wx/protocol/protocol.h"
2228 #include "wx/protocol/http.h"
2230 static void TestSocketServer()
2232 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2234 static const int PORT
= 3000;
2239 wxSocketServer
*server
= new wxSocketServer(addr
);
2240 if ( !server
->Ok() )
2242 wxPuts(_T("ERROR: failed to bind"));
2250 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2252 wxSocketBase
*socket
= server
->Accept();
2255 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2259 wxPuts(_T("Server: got a client."));
2261 server
->SetTimeout(60); // 1 min
2264 while ( !close
&& socket
->IsConnected() )
2267 wxChar ch
= _T('\0');
2270 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2272 // don't log error if the client just close the connection
2273 if ( socket
->IsConnected() )
2275 wxPuts(_T("ERROR: in wxSocket::Read."));
2295 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2296 if ( s
== _T("close") )
2298 wxPuts(_T("Closing connection"));
2302 else if ( s
== _T("quit") )
2307 wxPuts(_T("Shutting down the server"));
2309 else // not a special command
2311 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2312 socket
->Write("\r\n", 2);
2313 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2319 wxPuts(_T("Server: lost a client unexpectedly."));
2325 // same as "delete server" but is consistent with GUI programs
2329 static void TestSocketClient()
2331 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2333 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2336 addr
.Hostname(hostname
);
2339 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2341 wxSocketClient client
;
2342 if ( !client
.Connect(addr
) )
2344 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2348 wxPrintf(_T("--- Connected to %s:%u...\n"),
2349 addr
.Hostname().c_str(), addr
.Service());
2353 // could use simply "GET" here I suppose
2355 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2356 client
.Write(cmdGet
, cmdGet
.length());
2357 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2358 MakePrintable(cmdGet
).c_str());
2359 client
.Read(buf
, WXSIZEOF(buf
));
2360 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2364 #endif // TEST_SOCKETS
2366 // ----------------------------------------------------------------------------
2368 // ----------------------------------------------------------------------------
2372 #include "wx/protocol/ftp.h"
2376 #define FTP_ANONYMOUS
2378 #ifdef FTP_ANONYMOUS
2379 static const wxChar
*directory
= _T("/pub");
2380 static const wxChar
*filename
= _T("welcome.msg");
2382 static const wxChar
*directory
= _T("/etc");
2383 static const wxChar
*filename
= _T("issue");
2386 static bool TestFtpConnect()
2388 wxPuts(_T("*** Testing FTP connect ***"));
2390 #ifdef FTP_ANONYMOUS
2391 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2393 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2394 #else // !FTP_ANONYMOUS
2395 static const wxChar
*hostname
= "localhost";
2398 wxFgets(user
, WXSIZEOF(user
), stdin
);
2399 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2402 wxChar password
[256];
2403 wxPrintf(_T("Password for %s: "), password
);
2404 wxFgets(password
, WXSIZEOF(password
), stdin
);
2405 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2406 ftp
.SetPassword(password
);
2408 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2409 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2411 if ( !ftp
.Connect(hostname
) )
2413 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2419 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2420 hostname
, ftp
.Pwd().c_str());
2427 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2428 static void TestFtpWuFtpd()
2431 static const wxChar
*hostname
= _T("ftp.eudora.com");
2432 if ( !ftp
.Connect(hostname
) )
2434 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2438 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2439 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2442 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2446 size_t size
= in
->GetSize();
2447 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2449 wxChar
*data
= new wxChar
[size
];
2450 if ( !in
->Read(data
, size
) )
2452 wxPuts(_T("ERROR: read error"));
2456 wxPrintf(_T("Successfully retrieved the file.\n"));
2465 static void TestFtpList()
2467 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2470 if ( !ftp
.ChDir(directory
) )
2472 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2475 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2477 // test NLIST and LIST
2478 wxArrayString files
;
2479 if ( !ftp
.GetFilesList(files
) )
2481 wxPuts(_T("ERROR: failed to get NLIST of files"));
2485 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2486 size_t count
= files
.GetCount();
2487 for ( size_t n
= 0; n
< count
; n
++ )
2489 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2491 wxPuts(_T("End of the file list"));
2494 if ( !ftp
.GetDirList(files
) )
2496 wxPuts(_T("ERROR: failed to get LIST of files"));
2500 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2501 size_t count
= files
.GetCount();
2502 for ( size_t n
= 0; n
< count
; n
++ )
2504 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2506 wxPuts(_T("End of the file list"));
2509 if ( !ftp
.ChDir(_T("..")) )
2511 wxPuts(_T("ERROR: failed to cd to .."));
2514 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2517 static void TestFtpDownload()
2519 wxPuts(_T("*** Testing wxFTP download ***\n"));
2522 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2525 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2529 size_t size
= in
->GetSize();
2530 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2533 wxChar
*data
= new wxChar
[size
];
2534 if ( !in
->Read(data
, size
) )
2536 wxPuts(_T("ERROR: read error"));
2540 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2548 static void TestFtpFileSize()
2550 wxPuts(_T("*** Testing FTP SIZE command ***"));
2552 if ( !ftp
.ChDir(directory
) )
2554 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2557 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2559 if ( ftp
.FileExists(filename
) )
2561 int size
= ftp
.GetFileSize(filename
);
2563 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2565 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2569 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2573 static void TestFtpMisc()
2575 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2577 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2579 wxPuts(_T("ERROR: STAT failed"));
2583 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2586 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2588 wxPuts(_T("ERROR: HELP SITE failed"));
2592 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2593 ftp
.GetLastResult().c_str());
2597 static void TestFtpInteractive()
2599 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2605 wxPrintf(_T("Enter FTP command: "));
2606 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2609 // kill the last '\n'
2610 buf
[wxStrlen(buf
) - 1] = 0;
2612 // special handling of LIST and NLST as they require data connection
2613 wxString
start(buf
, 4);
2615 if ( start
== _T("LIST") || start
== _T("NLST") )
2618 if ( wxStrlen(buf
) > 4 )
2621 wxArrayString files
;
2622 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2624 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2628 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2629 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2630 size_t count
= files
.GetCount();
2631 for ( size_t n
= 0; n
< count
; n
++ )
2633 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2635 wxPuts(_T("--- End of the file list"));
2640 wxChar ch
= ftp
.SendCommand(buf
);
2641 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2644 wxPrintf(_T(" (return code %c)"), ch
);
2647 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2651 wxPuts(_T("\n*** done ***"));
2654 static void TestFtpUpload()
2656 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2659 static const wxChar
*file1
= _T("test1");
2660 static const wxChar
*file2
= _T("test2");
2661 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2664 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2665 out
->Write("First hello", 11);
2669 // send a command to check the remote file
2670 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2672 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2676 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2677 file1
, ftp
.GetLastResult().c_str());
2680 out
= ftp
.GetOutputStream(file2
);
2683 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2684 out
->Write("Second hello", 12);
2691 // ----------------------------------------------------------------------------
2693 // ----------------------------------------------------------------------------
2695 #ifdef TEST_STACKWALKER
2697 #if wxUSE_STACKWALKER
2699 #include "wx/stackwalk.h"
2701 class StackDump
: public wxStackWalker
2704 StackDump(const char *argv0
)
2705 : wxStackWalker(argv0
)
2709 virtual void Walk(size_t skip
= 1)
2711 wxPuts(_T("Stack dump:"));
2713 wxStackWalker::Walk(skip
);
2717 virtual void OnStackFrame(const wxStackFrame
& frame
)
2719 printf("[%2d] ", frame
.GetLevel());
2721 wxString name
= frame
.GetName();
2722 if ( !name
.empty() )
2724 printf("%-20.40s", name
.mb_str());
2728 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2731 if ( frame
.HasSourceLocation() )
2734 frame
.GetFileName().mb_str(),
2741 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2743 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2748 static void TestStackWalk(const char *argv0
)
2750 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2752 StackDump
dump(argv0
);
2756 #endif // wxUSE_STACKWALKER
2758 #endif // TEST_STACKWALKER
2760 // ----------------------------------------------------------------------------
2762 // ----------------------------------------------------------------------------
2764 #ifdef TEST_STDPATHS
2766 #include "wx/stdpaths.h"
2767 #include "wx/wxchar.h" // wxPrintf
2769 static void TestStandardPaths()
2771 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2773 wxTheApp
->SetAppName(_T("console"));
2775 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2776 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2777 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2778 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2779 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2780 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2781 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2782 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2783 wxPrintf(_T("Executable path:\t%s\n"), stdp
.GetExecutablePath().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);
2818 wxFileInputStream
fsIn(filename
);
2819 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2820 while ( !fsIn
.Eof() )
2822 wxPutchar(fsIn
.GetC());
2826 if ( !wxRemoveFile(filename
) )
2828 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2831 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2834 static void TestMemoryStream()
2836 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2838 wxMemoryOutputStream memOutStream
;
2839 wxPrintf(_T("Initially out stream offset: %lu\n"),
2840 (unsigned long)memOutStream
.TellO());
2842 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2844 memOutStream
.PutC(*p
);
2847 wxPrintf(_T("Final out stream offset: %lu\n"),
2848 (unsigned long)memOutStream
.TellO());
2850 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2853 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2855 wxMemoryInputStream
memInpStream(buf
, len
);
2856 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2857 while ( !memInpStream
.Eof() )
2859 wxPutchar(memInpStream
.GetC());
2862 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2865 #endif // TEST_STREAMS
2867 // ----------------------------------------------------------------------------
2869 // ----------------------------------------------------------------------------
2873 #include "wx/stopwatch.h"
2874 #include "wx/utils.h"
2876 static void TestStopWatch()
2878 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2882 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2885 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2887 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2891 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2894 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2897 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2900 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2903 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2906 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2907 for ( size_t n
= 0; n
< 70; n
++ )
2911 for ( size_t m
= 0; m
< 100000; m
++ )
2913 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2915 wxPuts(_T("\ntime is negative - ERROR!"));
2923 wxPuts(_T(", ok."));
2926 #include "wx/timer.h"
2927 #include "wx/evtloop.h"
2931 wxPuts(_T("*** Testing wxTimer ***\n"));
2933 class MyTimer
: public wxTimer
2936 MyTimer() : wxTimer() { m_num
= 0; }
2938 virtual void Notify()
2940 wxPrintf(_T("%d"), m_num
++);
2945 wxPrintf(_T("... exiting the event loop"));
2948 wxEventLoop::GetActive()->Exit(0);
2949 wxPuts(_T(", ok."));
2962 timer1
.Start(100, true /* one shot */);
2964 timer1
.Start(100, true /* one shot */);
2972 #endif // TEST_TIMER
2974 // ----------------------------------------------------------------------------
2976 // ----------------------------------------------------------------------------
2980 #include "wx/vcard.h"
2982 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2985 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2988 wxPrintf(_T("%s%s"),
2989 wxString(_T('\t'), level
).c_str(),
2990 vcObj
->GetName().c_str());
2993 switch ( vcObj
->GetType() )
2995 case wxVCardObject::String
:
2996 case wxVCardObject::UString
:
2999 vcObj
->GetValue(&val
);
3000 value
<< _T('"') << val
<< _T('"');
3004 case wxVCardObject::Int
:
3007 vcObj
->GetValue(&i
);
3008 value
.Printf(_T("%u"), i
);
3012 case wxVCardObject::Long
:
3015 vcObj
->GetValue(&l
);
3016 value
.Printf(_T("%lu"), l
);
3020 case wxVCardObject::None
:
3023 case wxVCardObject::Object
:
3024 value
= _T("<node>");
3028 value
= _T("<unknown value type>");
3032 wxPrintf(_T(" = %s"), value
.c_str());
3035 DumpVObject(level
+ 1, *vcObj
);
3038 vcObj
= vcard
.GetNextProp(&cookie
);
3042 static void DumpVCardAddresses(const wxVCard
& vcard
)
3044 wxPuts(_T("\nShowing all addresses from vCard:\n"));
3048 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
3052 int flags
= addr
->GetFlags();
3053 if ( flags
& wxVCardAddress::Domestic
)
3055 flagsStr
<< _T("domestic ");
3057 if ( flags
& wxVCardAddress::Intl
)
3059 flagsStr
<< _T("international ");
3061 if ( flags
& wxVCardAddress::Postal
)
3063 flagsStr
<< _T("postal ");
3065 if ( flags
& wxVCardAddress::Parcel
)
3067 flagsStr
<< _T("parcel ");
3069 if ( flags
& wxVCardAddress::Home
)
3071 flagsStr
<< _T("home ");
3073 if ( flags
& wxVCardAddress::Work
)
3075 flagsStr
<< _T("work ");
3078 wxPrintf(_T("Address %u:\n")
3080 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3083 addr
->GetPostOffice().c_str(),
3084 addr
->GetExtAddress().c_str(),
3085 addr
->GetStreet().c_str(),
3086 addr
->GetLocality().c_str(),
3087 addr
->GetRegion().c_str(),
3088 addr
->GetPostalCode().c_str(),
3089 addr
->GetCountry().c_str()
3093 addr
= vcard
.GetNextAddress(&cookie
);
3097 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
3099 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3103 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
3107 int flags
= phone
->GetFlags();
3108 if ( flags
& wxVCardPhoneNumber::Voice
)
3110 flagsStr
<< _T("voice ");
3112 if ( flags
& wxVCardPhoneNumber::Fax
)
3114 flagsStr
<< _T("fax ");
3116 if ( flags
& wxVCardPhoneNumber::Cellular
)
3118 flagsStr
<< _T("cellular ");
3120 if ( flags
& wxVCardPhoneNumber::Modem
)
3122 flagsStr
<< _T("modem ");
3124 if ( flags
& wxVCardPhoneNumber::Home
)
3126 flagsStr
<< _T("home ");
3128 if ( flags
& wxVCardPhoneNumber::Work
)
3130 flagsStr
<< _T("work ");
3133 wxPrintf(_T("Phone number %u:\n")
3138 phone
->GetNumber().c_str()
3142 phone
= vcard
.GetNextPhoneNumber(&cookie
);
3146 static void TestVCardRead()
3148 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3150 wxVCard
vcard(_T("vcard.vcf"));
3151 if ( !vcard
.IsOk() )
3153 wxPuts(_T("ERROR: couldn't load vCard."));
3157 // read individual vCard properties
3158 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3162 vcObj
->GetValue(&value
);
3167 value
= _T("<none>");
3170 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3173 if ( !vcard
.GetFullName(&value
) )
3175 value
= _T("<none>");
3178 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3180 // now show how to deal with multiply occurring properties
3181 DumpVCardAddresses(vcard
);
3182 DumpVCardPhoneNumbers(vcard
);
3184 // and finally show all
3185 wxPuts(_T("\nNow dumping the entire vCard:\n")
3186 "-----------------------------\n");
3188 DumpVObject(0, vcard
);
3192 static void TestVCardWrite()
3194 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3197 if ( !vcard
.IsOk() )
3199 wxPuts(_T("ERROR: couldn't create vCard."));
3204 vcard
.SetName("Zeitlin", "Vadim");
3205 vcard
.SetFullName("Vadim Zeitlin");
3206 vcard
.SetOrganization("wxWidgets", "R&D");
3208 // just dump the vCard back
3209 wxPuts(_T("Entire vCard follows:\n"));
3210 wxPuts(vcard
.Write());
3214 #endif // TEST_VCARD
3216 // ----------------------------------------------------------------------------
3218 // ----------------------------------------------------------------------------
3220 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3226 #include "wx/volume.h"
3228 static const wxChar
*volumeKinds
[] =
3234 _T("network volume"),
3238 static void TestFSVolume()
3240 wxPuts(_T("*** Testing wxFSVolume class ***"));
3242 wxArrayString volumes
= wxFSVolume::GetVolumes();
3243 size_t count
= volumes
.GetCount();
3247 wxPuts(_T("ERROR: no mounted volumes?"));
3251 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3253 for ( size_t n
= 0; n
< count
; n
++ )
3255 wxFSVolume
vol(volumes
[n
]);
3258 wxPuts(_T("ERROR: couldn't create volume"));
3262 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3264 vol
.GetDisplayName().c_str(),
3265 vol
.GetName().c_str(),
3266 volumeKinds
[vol
.GetKind()],
3267 vol
.IsWritable() ? _T("rw") : _T("ro"),
3268 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3273 #endif // TEST_VOLUME
3275 // ----------------------------------------------------------------------------
3276 // wide char and Unicode support
3277 // ----------------------------------------------------------------------------
3281 #include "wx/strconv.h"
3282 #include "wx/fontenc.h"
3283 #include "wx/encconv.h"
3284 #include "wx/buffer.h"
3286 static const unsigned char utf8koi8r
[] =
3288 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3289 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3290 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3291 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3292 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3293 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3294 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3297 static const unsigned char utf8iso8859_1
[] =
3299 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3300 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3301 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3302 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3303 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3306 static const unsigned char utf8Invalid
[] =
3308 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3309 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3310 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3311 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3315 static const struct Utf8Data
3317 const unsigned char *text
;
3319 const wxChar
*charset
;
3320 wxFontEncoding encoding
;
3323 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3324 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3325 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3328 static void TestUtf8()
3330 wxPuts(_T("*** Testing UTF8 support ***\n"));
3335 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3337 const Utf8Data
& u8d
= utf8data
[n
];
3338 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3339 WXSIZEOF(wbuf
)) == (size_t)-1 )
3341 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3345 wxCSConv
conv(u8d
.charset
);
3346 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3348 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3352 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3356 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3358 s
= _T("<< conversion failed >>");
3359 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3363 wxPuts(wxEmptyString
);
3366 static void TestEncodingConverter()
3368 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3370 // using wxEncodingConverter should give the same result as above
3373 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3374 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3376 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3380 wxEncodingConverter ec
;
3381 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3382 ec
.Convert(wbuf
, buf
);
3383 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3386 wxPuts(wxEmptyString
);
3389 #endif // TEST_WCHAR
3391 // ----------------------------------------------------------------------------
3393 // ----------------------------------------------------------------------------
3397 #include "wx/filesys.h"
3398 #include "wx/fs_zip.h"
3399 #include "wx/zipstrm.h"
3401 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3403 static void TestZipStreamRead()
3405 wxPuts(_T("*** Testing ZIP reading ***\n"));
3407 static const wxString filename
= _T("foo");
3408 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3409 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3411 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3412 while ( !istr
.Eof() )
3414 wxPutchar(istr
.GetC());
3418 wxPuts(_T("\n----- done ------"));
3421 static void DumpZipDirectory(wxFileSystem
& fs
,
3422 const wxString
& dir
,
3423 const wxString
& indent
)
3425 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3426 TESTFILE_ZIP
, dir
.c_str());
3427 wxString wildcard
= prefix
+ _T("/*");
3429 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3430 while ( !dirname
.empty() )
3432 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3434 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3439 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3441 DumpZipDirectory(fs
, dirname
,
3442 indent
+ wxString(_T(' '), 4));
3444 dirname
= fs
.FindNext();
3447 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3448 while ( !filename
.empty() )
3450 if ( !filename
.StartsWith(prefix
, &filename
) )
3452 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3457 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3459 filename
= fs
.FindNext();
3463 static void TestZipFileSystem()
3465 wxPuts(_T("*** Testing ZIP file system ***\n"));
3467 wxFileSystem::AddHandler(new wxZipFSHandler
);
3469 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3471 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3476 // ----------------------------------------------------------------------------
3478 // ----------------------------------------------------------------------------
3480 #ifdef TEST_DATETIME
3482 #include "wx/math.h"
3483 #include "wx/datetime.h"
3485 // this test miscellaneous static wxDateTime functions
3489 static void TestTimeStatic()
3491 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3493 // some info about the current date
3494 int year
= wxDateTime::GetCurrentYear();
3495 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3497 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3498 wxDateTime::GetNumberOfDays(year
));
3500 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3501 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3502 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3503 wxDateTime::GetMonthName(month
).c_str(),
3504 wxDateTime::GetNumberOfDays(month
));
3507 // test time zones stuff
3508 static void TestTimeZones()
3510 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3512 wxDateTime now
= wxDateTime::Now();
3514 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3515 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3516 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3517 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3518 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3519 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3521 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3523 wxDateTime::Tm tm
= now
.GetTm();
3524 if ( wxDateTime(tm
) != now
)
3526 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3527 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3531 // test some minimal support for the dates outside the standard range
3532 static void TestTimeRange()
3534 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3536 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3538 wxPrintf(_T("Unix epoch:\t%s\n"),
3539 wxDateTime(2440587.5).Format(fmt
).c_str());
3540 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3541 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3542 wxPrintf(_T("JDN 0: \t%s\n"),
3543 wxDateTime(0.0).Format(fmt
).c_str());
3544 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3545 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3546 wxPrintf(_T("May 29, 2099:\t%s\n"),
3547 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3550 // test DST calculations
3551 static void TestTimeDST()
3553 wxPuts(_T("\n*** wxDateTime DST test ***"));
3555 wxPrintf(_T("DST is%s in effect now.\n\n"),
3556 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3558 for ( int year
= 1990; year
< 2005; year
++ )
3560 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3562 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3563 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3569 #if TEST_INTERACTIVE
3571 static void TestDateTimeInteractive()
3573 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3579 wxPrintf(_T("Enter a date: "));
3580 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3583 // kill the last '\n'
3584 buf
[wxStrlen(buf
) - 1] = 0;
3587 const wxChar
*p
= dt
.ParseDate(buf
);
3590 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3596 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3599 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3600 dt
.Format(_T("%b %d, %Y")).c_str(),
3602 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3603 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3604 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3607 wxPuts(_T("\n*** done ***"));
3610 #endif // TEST_INTERACTIVE
3614 static void TestTimeMS()
3616 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3618 wxDateTime dt1
= wxDateTime::Now(),
3619 dt2
= wxDateTime::UNow();
3621 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3622 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3623 wxPrintf(_T("Dummy loop: "));
3624 for ( int i
= 0; i
< 6000; i
++ )
3626 //for ( int j = 0; j < 10; j++ )
3629 s
.Printf(_T("%g"), sqrt((float)i
));
3635 wxPuts(_T(", done"));
3638 dt2
= wxDateTime::UNow();
3639 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3641 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3643 wxPuts(_T("\n*** done ***"));
3646 static void TestTimeHolidays()
3648 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3650 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3651 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3652 dtEnd
= dtStart
.GetLastMonthDay();
3654 wxDateTimeArray hol
;
3655 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3657 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3659 wxPrintf(_T("All holidays between %s and %s:\n"),
3660 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3662 size_t count
= hol
.GetCount();
3663 for ( size_t n
= 0; n
< count
; n
++ )
3665 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3668 wxPuts(wxEmptyString
);
3671 static void TestTimeZoneBug()
3673 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3675 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3676 for ( int i
= 0; i
< 31; i
++ )
3678 wxPrintf(_T("Date %s: week day %s.\n"),
3679 date
.Format(_T("%d-%m-%Y")).c_str(),
3680 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3682 date
+= wxDateSpan::Day();
3685 wxPuts(wxEmptyString
);
3688 static void TestTimeSpanFormat()
3690 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3692 static const wxChar
*formats
[] =
3694 _T("(default) %H:%M:%S"),
3695 _T("%E weeks and %D days"),
3696 _T("%l milliseconds"),
3697 _T("(with ms) %H:%M:%S:%l"),
3698 _T("100%% of minutes is %M"), // test "%%"
3699 _T("%D days and %H hours"),
3700 _T("or also %S seconds"),
3703 wxTimeSpan
ts1(1, 2, 3, 4),
3705 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3707 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3708 ts1
.Format(formats
[n
]).c_str(),
3709 ts2
.Format(formats
[n
]).c_str());
3712 wxPuts(wxEmptyString
);
3717 #endif // TEST_DATETIME
3719 // ----------------------------------------------------------------------------
3720 // wxTextInput/OutputStream
3721 // ----------------------------------------------------------------------------
3723 #ifdef TEST_TEXTSTREAM
3725 #include "wx/txtstrm.h"
3726 #include "wx/wfstream.h"
3728 static void TestTextInputStream()
3730 wxPuts(_T("\n*** wxTextInputStream test ***"));
3732 wxString filename
= _T("testdata.fc");
3733 wxFileInputStream
fsIn(filename
);
3736 wxPuts(_T("ERROR: couldn't open file."));
3740 wxTextInputStream
tis(fsIn
);
3745 const wxString s
= tis
.ReadLine();
3747 // line could be non empty if the last line of the file isn't
3748 // terminated with EOL
3749 if ( fsIn
.Eof() && s
.empty() )
3752 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3757 #endif // TEST_TEXTSTREAM
3759 // ----------------------------------------------------------------------------
3761 // ----------------------------------------------------------------------------
3765 #include "wx/thread.h"
3767 static size_t gs_counter
= (size_t)-1;
3768 static wxCriticalSection gs_critsect
;
3769 static wxSemaphore gs_cond
;
3771 class MyJoinableThread
: public wxThread
3774 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3775 { m_n
= n
; Create(); }
3777 // thread execution starts here
3778 virtual ExitCode
Entry();
3784 wxThread::ExitCode
MyJoinableThread::Entry()
3786 unsigned long res
= 1;
3787 for ( size_t n
= 1; n
< m_n
; n
++ )
3791 // it's a loooong calculation :-)
3795 return (ExitCode
)res
;
3798 class MyDetachedThread
: public wxThread
3801 MyDetachedThread(size_t n
, wxChar ch
)
3805 m_cancelled
= false;
3810 // thread execution starts here
3811 virtual ExitCode
Entry();
3814 virtual void OnExit();
3817 size_t m_n
; // number of characters to write
3818 wxChar m_ch
; // character to write
3820 bool m_cancelled
; // false if we exit normally
3823 wxThread::ExitCode
MyDetachedThread::Entry()
3826 wxCriticalSectionLocker
lock(gs_critsect
);
3827 if ( gs_counter
== (size_t)-1 )
3833 for ( size_t n
= 0; n
< m_n
; n
++ )
3835 if ( TestDestroy() )
3845 wxThread::Sleep(100);
3851 void MyDetachedThread::OnExit()
3853 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3855 wxCriticalSectionLocker
lock(gs_critsect
);
3856 if ( !--gs_counter
&& !m_cancelled
)
3860 static void TestDetachedThreads()
3862 wxPuts(_T("\n*** Testing detached threads ***"));
3864 static const size_t nThreads
= 3;
3865 MyDetachedThread
*threads
[nThreads
];
3867 for ( n
= 0; n
< nThreads
; n
++ )
3869 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3872 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3873 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3875 for ( n
= 0; n
< nThreads
; n
++ )
3880 // wait until all threads terminate
3883 wxPuts(wxEmptyString
);
3886 static void TestJoinableThreads()
3888 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3890 // calc 10! in the background
3891 MyJoinableThread
thread(10);
3894 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3895 (unsigned long)thread
.Wait());
3898 static void TestThreadSuspend()
3900 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3902 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3906 // this is for this demo only, in a real life program we'd use another
3907 // condition variable which would be signaled from wxThread::Entry() to
3908 // tell us that the thread really started running - but here just wait a
3909 // bit and hope that it will be enough (the problem is, of course, that
3910 // the thread might still not run when we call Pause() which will result
3912 wxThread::Sleep(300);
3914 for ( size_t n
= 0; n
< 3; n
++ )
3918 wxPuts(_T("\nThread suspended"));
3921 // don't sleep but resume immediately the first time
3922 wxThread::Sleep(300);
3924 wxPuts(_T("Going to resume the thread"));
3929 wxPuts(_T("Waiting until it terminates now"));
3931 // wait until the thread terminates
3934 wxPuts(wxEmptyString
);
3937 static void TestThreadDelete()
3939 // As above, using Sleep() is only for testing here - we must use some
3940 // synchronisation object instead to ensure that the thread is still
3941 // running when we delete it - deleting a detached thread which already
3942 // terminated will lead to a crash!
3944 wxPuts(_T("\n*** Testing thread delete function ***"));
3946 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3950 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3952 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3956 wxThread::Sleep(300);
3960 wxPuts(_T("\nDeleted a running thread."));
3962 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3966 wxThread::Sleep(300);
3972 wxPuts(_T("\nDeleted a sleeping thread."));
3974 MyJoinableThread
thread3(20);
3979 wxPuts(_T("\nDeleted a joinable thread."));
3981 MyJoinableThread
thread4(2);
3984 wxThread::Sleep(300);
3988 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3990 wxPuts(wxEmptyString
);
3993 class MyWaitingThread
: public wxThread
3996 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3999 m_condition
= condition
;
4004 virtual ExitCode
Entry()
4006 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
4011 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
4015 m_condition
->Wait();
4018 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
4026 wxCondition
*m_condition
;
4029 static void TestThreadConditions()
4032 wxCondition
condition(mutex
);
4034 // otherwise its difficult to understand which log messages pertain to
4036 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
4037 // condition.GetId(), gs_cond.GetId());
4039 // create and launch threads
4040 MyWaitingThread
*threads
[10];
4043 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
4045 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
4048 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
4053 // wait until all threads run
4054 wxPuts(_T("Main thread is waiting for the other threads to start"));
4057 size_t nRunning
= 0;
4058 while ( nRunning
< WXSIZEOF(threads
) )
4064 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
4068 wxPuts(_T("Main thread: all threads started up."));
4071 wxThread::Sleep(500);
4074 // now wake one of them up
4075 wxPrintf(_T("Main thread: about to signal the condition.\n"));
4080 wxThread::Sleep(200);
4082 // wake all the (remaining) threads up, so that they can exit
4083 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
4085 condition
.Broadcast();
4087 // give them time to terminate (dirty!)
4088 wxThread::Sleep(500);
4091 #include "wx/utils.h"
4093 class MyExecThread
: public wxThread
4096 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
4102 virtual ExitCode
Entry()
4104 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
4111 static void TestThreadExec()
4113 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4115 MyExecThread
thread(_T("true"));
4118 wxPrintf(_T("Main program exit code: %ld.\n"),
4119 wxExecute(_T("false"), wxEXEC_SYNC
));
4121 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
4125 #include "wx/datetime.h"
4127 class MySemaphoreThread
: public wxThread
4130 MySemaphoreThread(int i
, wxSemaphore
*sem
)
4131 : wxThread(wxTHREAD_JOINABLE
),
4138 virtual ExitCode
Entry()
4140 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4141 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4145 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4146 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4150 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4151 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4163 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
4165 static void TestSemaphore()
4167 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4169 static const int SEM_LIMIT
= 3;
4171 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
4172 ArrayThreads threads
;
4174 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
4176 threads
.Add(new MySemaphoreThread(i
, &sem
));
4177 threads
.Last()->Run();
4180 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4187 #endif // TEST_THREADS
4189 // ----------------------------------------------------------------------------
4191 // ----------------------------------------------------------------------------
4193 #ifdef TEST_SNGLINST
4194 #include "wx/snglinst.h"
4195 #endif // TEST_SNGLINST
4197 int main(int argc
, char **argv
)
4200 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4205 for (n
= 0; n
< argc
; n
++ )
4207 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4208 wxArgv
[n
] = wxStrdup(warg
);
4213 #else // !wxUSE_UNICODE
4215 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4217 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4219 wxInitializer initializer
;
4222 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4227 #ifdef TEST_SNGLINST
4228 wxSingleInstanceChecker checker
;
4229 if ( checker
.Create(_T(".wxconsole.lock")) )
4231 if ( checker
.IsAnotherRunning() )
4233 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4238 // wait some time to give time to launch another instance
4239 wxPrintf(_T("Press \"Enter\" to continue..."));
4242 else // failed to create
4244 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4246 #endif // TEST_SNGLINST
4249 TestCmdLineConvert();
4251 #if wxUSE_CMDLINE_PARSER
4252 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4254 { wxCMD_LINE_SWITCH
, "h", "help", "show this help message",
4255 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4256 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
4257 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
4259 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
4260 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
4261 { wxCMD_LINE_OPTION
, "s", "size", "output block size",
4262 wxCMD_LINE_VAL_NUMBER
},
4263 { wxCMD_LINE_OPTION
, "d", "date", "output file date",
4264 wxCMD_LINE_VAL_DATE
},
4266 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
4267 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4272 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4274 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4275 wxCMD_LINE_VAL_STRING
,
4276 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4278 switch ( parser
.Parse() )
4281 wxLogMessage(_T("Help was given, terminating."));
4285 ShowCmdLine(parser
);
4289 wxLogMessage(_T("Syntax error detected, aborting."));
4292 #endif // wxUSE_CMDLINE_PARSER
4294 #endif // TEST_CMDLINE
4306 TestDllListLoaded();
4307 #endif // TEST_DYNLIB
4311 #endif // TEST_ENVIRON
4315 #endif // TEST_EXECUTE
4317 #ifdef TEST_FILECONF
4319 #endif // TEST_FILECONF
4323 #endif // TEST_LOCALE
4326 wxPuts(_T("*** Testing wxLog ***"));
4329 for ( size_t n
= 0; n
< 8000; n
++ )
4331 s
<< (wxChar
)(_T('A') + (n
% 26));
4334 wxLogWarning(_T("The length of the string is %lu"),
4335 (unsigned long)s
.length());
4338 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4340 // this one shouldn't be truncated
4343 // but this one will because log functions use fixed size buffer
4344 // (note that it doesn't need '\n' at the end neither - will be added
4346 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4356 #ifdef TEST_FILENAME
4359 TestFileNameDirManip();
4360 TestFileNameComparison();
4361 TestFileNameOperations();
4362 #endif // TEST_FILENAME
4364 #ifdef TEST_FILETIME
4369 #endif // TEST_FILETIME
4372 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4373 if ( TestFtpConnect() )
4383 #if TEST_INTERACTIVE
4384 TestFtpInteractive();
4387 //else: connecting to the FTP server failed
4395 //wxLog::AddTraceMask(_T("mime"));
4398 // TestMimeAssociate();
4402 #ifdef TEST_INFO_FUNCTIONS
4407 #if TEST_INTERACTIVE
4410 #endif // TEST_INFO_FUNCTIONS
4412 #ifdef TEST_PATHLIST
4414 #endif // TEST_PATHLIST
4418 #endif // TEST_PRINTF
4425 #endif // TEST_REGCONF
4427 #if defined TEST_REGEX && TEST_INTERACTIVE
4428 TestRegExInteractive();
4429 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4431 #ifdef TEST_REGISTRY
4433 TestRegistryAssociation();
4434 #endif // TEST_REGISTRY
4439 #endif // TEST_SOCKETS
4446 #endif // TEST_STREAMS
4448 #ifdef TEST_TEXTSTREAM
4449 TestTextInputStream();
4450 #endif // TEST_TEXTSTREAM
4453 int nCPUs
= wxThread::GetCPUCount();
4454 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4456 wxThread::SetConcurrency(nCPUs
);
4458 TestJoinableThreads();
4461 TestJoinableThreads();
4462 TestDetachedThreads();
4463 TestThreadSuspend();
4465 TestThreadConditions();
4469 #endif // TEST_THREADS
4474 #endif // TEST_TIMER
4476 #ifdef TEST_DATETIME
4483 TestTimeSpanFormat();
4489 #if TEST_INTERACTIVE
4490 TestDateTimeInteractive();
4492 #endif // TEST_DATETIME
4494 #ifdef TEST_SCOPEGUARD
4498 #ifdef TEST_STACKWALKER
4499 #if wxUSE_STACKWALKER
4500 TestStackWalk(argv
[0]);
4502 #endif // TEST_STACKWALKER
4504 #ifdef TEST_STDPATHS
4505 TestStandardPaths();
4509 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4511 #endif // TEST_USLEEP
4516 #endif // TEST_VCARD
4520 #endif // TEST_VOLUME
4524 TestEncodingConverter();
4525 #endif // TEST_WCHAR
4528 TestZipStreamRead();
4529 TestZipFileSystem();
4534 for ( int n
= 0; n
< argc
; n
++ )
4539 #endif // wxUSE_UNICODE