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 // IMPORTANT NOTE FOR WXWIDGETS USERS:
13 // If you're a wxWidgets user and you're looking at this file to learn how to
14 // structure a wxWidgets console application, then you don't have much to learn.
15 // This application is used more for testing rather than as sample but
16 // basically the following simple block is enough for you to start your
17 // own console application:
20 int main(int argc, char **argv)
22 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
24 wxInitializer initializer;
27 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
31 static const wxCmdLineEntryDesc cmdLineDesc[] =
33 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
34 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
35 // ... your other command line options here...
40 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
41 switch ( parser.Parse() )
44 wxLogMessage(_T("Help was given, terminating."));
48 // everything is ok; proceed
52 wxLogMessage(_T("Syntax error detected, aborting."));
56 // do something useful here
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
75 #include "wx/string.h"
77 #include "wx/filename.h"
80 #include "wx/apptrait.h"
81 #include "wx/platinfo.h"
82 #include "wx/wxchar.h"
84 // without this pragma, the stupid compiler precompiles #defines below so that
85 // changing them doesn't "take place" later!
90 // ----------------------------------------------------------------------------
91 // conditional compilation
92 // ----------------------------------------------------------------------------
95 A note about all these conditional compilation macros: this file is used
96 both as a test suite for various non-GUI wxWidgets classes and as a
97 scratchpad for quick tests. So there are two compilation modes: if you
98 define TEST_ALL all tests are run, otherwise you may enable the individual
99 tests individually in the "#else" branch below.
102 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
103 // test, define it to 1 to do all tests.
109 #define TEST_DATETIME
114 #define TEST_FILECONF
115 #define TEST_FILENAME
116 #define TEST_FILETIME
118 #define TEST_INFO_FUNCTIONS
123 #define TEST_PATHLIST
127 #define TEST_REGISTRY
128 #define TEST_SCOPEGUARD
129 #define TEST_SNGLINST
130 // #define TEST_SOCKETS --FIXME! (RN)
131 #define TEST_STACKWALKER
132 #define TEST_STDPATHS
134 #define TEST_TEXTSTREAM
137 // #define TEST_VOLUME --FIXME! (RN)
140 #else // #if TEST_ALL
144 // some tests are interactive, define this to run them
145 #ifdef TEST_INTERACTIVE
146 #undef TEST_INTERACTIVE
148 #define TEST_INTERACTIVE 1
150 #define TEST_INTERACTIVE 0
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 #if defined(TEST_SOCKETS)
163 // replace TABs with \t and CRs with \n
164 static wxString
MakePrintable(const wxChar
*s
)
167 (void)str
.Replace(_T("\t"), _T("\\t"));
168 (void)str
.Replace(_T("\n"), _T("\\n"));
169 (void)str
.Replace(_T("\r"), _T("\\r"));
174 #endif // MakePrintable() is used
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
182 #include "wx/cmdline.h"
183 #include "wx/datetime.h"
185 #if wxUSE_CMDLINE_PARSER
187 static void ShowCmdLine(const wxCmdLineParser
& parser
)
189 wxString s
= _T("Command line parsed successfully:\nInput files: ");
191 size_t count
= parser
.GetParamCount();
192 for ( size_t param
= 0; param
< count
; param
++ )
194 s
<< parser
.GetParam(param
) << ' ';
198 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
199 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
205 if ( parser
.Found(_T("o"), &strVal
) )
206 s
<< _T("Output file:\t") << strVal
<< '\n';
207 if ( parser
.Found(_T("i"), &strVal
) )
208 s
<< _T("Input dir:\t") << strVal
<< '\n';
209 if ( parser
.Found(_T("s"), &lVal
) )
210 s
<< _T("Size:\t") << lVal
<< '\n';
211 if ( parser
.Found(_T("f"), &dVal
) )
212 s
<< _T("Double:\t") << dVal
<< '\n';
213 if ( parser
.Found(_T("d"), &dt
) )
214 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
215 if ( parser
.Found(_T("project_name"), &strVal
) )
216 s
<< _T("Project:\t") << strVal
<< '\n';
221 #endif // wxUSE_CMDLINE_PARSER
223 static void TestCmdLineConvert()
225 static const wxChar
*cmdlines
[] =
228 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
229 _T("literal \\\" and \"\""),
232 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
234 const wxChar
*cmdline
= cmdlines
[n
];
235 wxPrintf(_T("Parsing: %s\n"), cmdline
);
236 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
238 size_t count
= args
.GetCount();
239 wxPrintf(_T("\targc = %u\n"), count
);
240 for ( size_t arg
= 0; arg
< count
; arg
++ )
242 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
247 #endif // TEST_CMDLINE
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
258 static const wxChar
*ROOTDIR
= _T("/");
259 static const wxChar
*TESTDIR
= _T("/usr/local/share");
260 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
261 static const wxChar
*ROOTDIR
= _T("c:\\");
262 static const wxChar
*TESTDIR
= _T("d:\\");
264 #error "don't know where the root directory is"
267 static void TestDirEnumHelper(wxDir
& dir
,
268 int flags
= wxDIR_DEFAULT
,
269 const wxString
& filespec
= wxEmptyString
)
273 if ( !dir
.IsOpened() )
276 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
279 wxPrintf(_T("\t%s\n"), filename
.c_str());
281 cont
= dir
.GetNext(&filename
);
284 wxPuts(wxEmptyString
);
289 static void TestDirEnum()
291 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
293 wxString cwd
= wxGetCwd();
294 if ( !wxDir::Exists(cwd
) )
296 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
301 if ( !dir
.IsOpened() )
303 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
307 wxPuts(_T("Enumerating everything in current directory:"));
308 TestDirEnumHelper(dir
);
310 wxPuts(_T("Enumerating really everything in current directory:"));
311 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
313 wxPuts(_T("Enumerating object files in current directory:"));
314 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
316 wxPuts(_T("Enumerating directories in current directory:"));
317 TestDirEnumHelper(dir
, wxDIR_DIRS
);
319 wxPuts(_T("Enumerating files in current directory:"));
320 TestDirEnumHelper(dir
, wxDIR_FILES
);
322 wxPuts(_T("Enumerating files including hidden in current directory:"));
323 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
327 wxPuts(_T("Enumerating everything in root directory:"));
328 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
330 wxPuts(_T("Enumerating directories in root directory:"));
331 TestDirEnumHelper(dir
, wxDIR_DIRS
);
333 wxPuts(_T("Enumerating files in root directory:"));
334 TestDirEnumHelper(dir
, wxDIR_FILES
);
336 wxPuts(_T("Enumerating files including hidden in root directory:"));
337 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
339 wxPuts(_T("Enumerating files in non existing directory:"));
340 wxDir
dirNo(_T("nosuchdir"));
341 TestDirEnumHelper(dirNo
);
346 class DirPrintTraverser
: public wxDirTraverser
349 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
351 return wxDIR_CONTINUE
;
354 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
356 wxString path
, name
, ext
;
357 wxFileName::SplitPath(dirname
, &path
, &name
, &ext
);
360 name
<< _T('.') << ext
;
363 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
365 if ( wxIsPathSeparator(*p
) )
369 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
371 return wxDIR_CONTINUE
;
375 static void TestDirTraverse()
377 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
381 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
382 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
385 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
386 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
389 // enum again with custom traverser
390 wxPuts(_T("Now enumerating directories:"));
392 DirPrintTraverser traverser
;
393 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
398 static void TestDirExists()
400 wxPuts(_T("*** Testing wxDir::Exists() ***"));
402 static const wxChar
*dirnames
[] =
405 #if defined(__WXMSW__)
408 _T("\\\\share\\file"),
412 _T("c:\\autoexec.bat"),
413 #elif defined(__UNIX__)
422 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
424 wxPrintf(_T("%-40s: %s\n"),
426 wxDir::Exists(dirnames
[n
]) ? _T("exists")
427 : _T("doesn't exist"));
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
441 #include "wx/dynlib.h"
443 static void TestDllLoad()
445 #if defined(__WXMSW__)
446 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
447 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
448 #elif defined(__UNIX__)
449 // weird: using just libc.so does *not* work!
450 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
451 static const wxChar
*FUNC_NAME
= _T("strlen");
453 #error "don't know how to test wxDllLoader on this platform"
456 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
458 wxDynamicLibrary
lib(LIB_NAME
);
459 if ( !lib
.IsLoaded() )
461 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
465 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
466 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
469 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
470 FUNC_NAME
, LIB_NAME
);
474 wxPrintf(_T("Calling %s dynamically loaded from %s "),
475 FUNC_NAME
, LIB_NAME
);
477 if ( pfnStrlen("foo") != 3 )
479 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
483 wxPuts(_T("... ok"));
488 static const wxChar
*FUNC_NAME_AW
= _T("lstrlen");
490 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
492 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
493 if ( !pfnStrlenAorW
)
495 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
496 FUNC_NAME_AW
, LIB_NAME
);
500 if ( pfnStrlenAorW(_T("foobar")) != 6 )
502 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
509 #if defined(__WXMSW__) || defined(__UNIX__)
511 static void TestDllListLoaded()
513 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
515 puts("\nLoaded modules:");
516 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
517 const size_t count
= dlls
.GetCount();
518 for ( size_t n
= 0; n
< count
; ++n
)
520 const wxDynamicLibraryDetails
& details
= dlls
[n
];
521 printf("%-45s", (const char *)details
.GetPath().mb_str());
525 if ( details
.GetAddress(&addr
, &len
) )
527 printf(" %08lx:%08lx",
528 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
531 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
537 #endif // TEST_DYNLIB
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
545 #include "wx/utils.h"
547 static wxString
MyGetEnv(const wxString
& var
)
550 if ( !wxGetEnv(var
, &val
) )
553 val
= wxString(_T('\'')) + val
+ _T('\'');
558 static void TestEnvironment()
560 const wxChar
*var
= _T("wxTestVar");
562 wxPuts(_T("*** testing environment access functions ***"));
564 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
565 wxSetEnv(var
, _T("value for wxTestVar"));
566 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
567 wxSetEnv(var
, _T("another value"));
568 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
570 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
571 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
574 #endif // TEST_ENVIRON
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
583 #include "wx/ffile.h"
584 #include "wx/textfile.h"
586 static void TestFileRead()
588 wxPuts(_T("*** wxFile read test ***"));
590 wxFile
file(_T("testdata.fc"));
591 if ( file
.IsOpened() )
593 wxPrintf(_T("File length: %lu\n"), file
.Length());
595 wxPuts(_T("File dump:\n----------"));
597 static const size_t len
= 1024;
601 size_t nRead
= file
.Read(buf
, len
);
602 if ( nRead
== (size_t)wxInvalidOffset
)
604 wxPrintf(_T("Failed to read the file."));
608 fwrite(buf
, nRead
, 1, stdout
);
614 wxPuts(_T("----------"));
618 wxPrintf(_T("ERROR: can't open test file.\n"));
621 wxPuts(wxEmptyString
);
624 static void TestTextFileRead()
626 wxPuts(_T("*** wxTextFile read test ***"));
628 wxTextFile
file(_T("testdata.fc"));
631 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
632 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
636 wxPuts(_T("\nDumping the entire file:"));
637 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
639 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
641 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
643 wxPuts(_T("\nAnd now backwards:"));
644 for ( s
= file
.GetLastLine();
645 file
.GetCurrentLine() != 0;
646 s
= file
.GetPrevLine() )
648 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
650 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
654 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
657 wxPuts(wxEmptyString
);
660 static void TestFileCopy()
662 wxPuts(_T("*** Testing wxCopyFile ***"));
664 static const wxChar
*filename1
= _T("testdata.fc");
665 static const wxChar
*filename2
= _T("test2");
666 if ( !wxCopyFile(filename1
, filename2
) )
668 wxPuts(_T("ERROR: failed to copy file"));
672 wxFFile
f1(filename1
, _T("rb")),
673 f2(filename2
, _T("rb"));
675 if ( !f1
.IsOpened() || !f2
.IsOpened() )
677 wxPuts(_T("ERROR: failed to open file(s)"));
682 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
684 wxPuts(_T("ERROR: failed to read file(s)"));
688 if ( (s1
.length() != s2
.length()) ||
689 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
691 wxPuts(_T("ERROR: copy error!"));
695 wxPuts(_T("File was copied ok."));
701 if ( !wxRemoveFile(filename2
) )
703 wxPuts(_T("ERROR: failed to remove the file"));
706 wxPuts(wxEmptyString
);
709 static void TestTempFile()
711 wxPuts(_T("*** wxTempFile test ***"));
714 if ( tmpFile
.Open(_T("test2")) && tmpFile
.Write(_T("the answer is 42")) )
716 if ( tmpFile
.Commit() )
717 wxPuts(_T("File committed."));
719 wxPuts(_T("ERROR: could't commit temp file."));
721 wxRemoveFile(_T("test2"));
724 wxPuts(wxEmptyString
);
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
735 #include "wx/confbase.h"
736 #include "wx/fileconf.h"
738 static const struct FileConfTestData
740 const wxChar
*name
; // value name
741 const wxChar
*value
; // the value from the file
744 { _T("value1"), _T("one") },
745 { _T("value2"), _T("two") },
746 { _T("novalue"), _T("default") },
749 static void TestFileConfRead()
751 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
753 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
754 _T("testdata.fc"), wxEmptyString
,
755 wxCONFIG_USE_RELATIVE_PATH
);
757 // test simple reading
758 wxPuts(_T("\nReading config file:"));
759 wxString
defValue(_T("default")), value
;
760 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
762 const FileConfTestData
& data
= fcTestData
[n
];
763 value
= fileconf
.Read(data
.name
, defValue
);
764 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
765 if ( value
== data
.value
)
771 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
775 // test enumerating the entries
776 wxPuts(_T("\nEnumerating all root entries:"));
779 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
782 wxPrintf(_T("\t%s = %s\n"),
784 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
786 cont
= fileconf
.GetNextEntry(name
, dummy
);
789 static const wxChar
*testEntry
= _T("TestEntry");
790 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
791 fileconf
.Write(testEntry
, _T("A value"));
792 fileconf
.DeleteEntry(testEntry
);
793 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
796 #endif // TEST_FILECONF
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
804 #include "wx/filename.h"
807 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
811 wxString full
= fn
.GetFullPath();
813 wxString vol
, path
, name
, ext
;
814 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
816 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
817 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
819 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
820 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
821 path
.c_str(), name
.c_str(), ext
.c_str());
823 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
824 wxPrintf(_T("with volume: \t'%s'\n"),
825 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
826 wxPrintf(_T("with separator:\t'%s'\n"),
827 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
828 wxPrintf(_T("with both: \t'%s'\n"),
829 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
831 wxPuts(_T("The directories in the path are:"));
832 wxArrayString dirs
= fn
.GetDirs();
833 size_t count
= dirs
.GetCount();
834 for ( size_t n
= 0; n
< count
; n
++ )
836 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
841 static void TestFileNameTemp()
843 wxPuts(_T("*** testing wxFileName temp file creation ***"));
845 static const wxChar
*tmpprefixes
[] =
853 _T("/tmp/foo/bar"), // this one must be an error
857 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
859 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
862 // "error" is not in upper case because it may be ok
863 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
867 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
868 tmpprefixes
[n
], path
.c_str());
870 if ( !wxRemoveFile(path
) )
872 wxLogWarning(_T("Failed to remove temp file '%s'"),
879 static void TestFileNameDirManip()
881 // TODO: test AppendDir(), RemoveDir(), ...
884 static void TestFileNameComparison()
889 static void TestFileNameOperations()
894 static void TestFileNameCwd()
899 #endif // TEST_FILENAME
901 // ----------------------------------------------------------------------------
902 // wxFileName time functions
903 // ----------------------------------------------------------------------------
907 #include "wx/filename.h"
908 #include "wx/datetime.h"
910 static void TestFileGetTimes()
912 wxFileName
fn(_T("testdata.fc"));
914 wxDateTime dtAccess
, dtMod
, dtCreate
;
915 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
917 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
921 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
923 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
924 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
925 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
926 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
931 static void TestFileSetTimes()
933 wxFileName
fn(_T("testdata.fc"));
937 wxPrintf(_T("ERROR: Touch() failed.\n"));
942 #endif // TEST_FILETIME
944 // ----------------------------------------------------------------------------
946 // ----------------------------------------------------------------------------
951 #include "wx/utils.h" // for wxSetEnv
953 static wxLocale gs_localeDefault
;
954 // NOTE: don't init it here as it needs a wxAppTraits object
955 // and thus must be init-ed after creation of the wxInitializer
956 // class in the main()
958 // find the name of the language from its value
959 static const wxChar
*GetLangName(int lang
)
961 static const wxChar
*languageNames
[] =
971 _T("ARABIC_ALGERIA"),
972 _T("ARABIC_BAHRAIN"),
977 _T("ARABIC_LEBANON"),
979 _T("ARABIC_MOROCCO"),
982 _T("ARABIC_SAUDI_ARABIA"),
985 _T("ARABIC_TUNISIA"),
992 _T("AZERI_CYRILLIC"),
1007 _T("CHINESE_SIMPLIFIED"),
1008 _T("CHINESE_TRADITIONAL"),
1009 _T("CHINESE_HONGKONG"),
1010 _T("CHINESE_MACAU"),
1011 _T("CHINESE_SINGAPORE"),
1012 _T("CHINESE_TAIWAN"),
1018 _T("DUTCH_BELGIAN"),
1022 _T("ENGLISH_AUSTRALIA"),
1023 _T("ENGLISH_BELIZE"),
1024 _T("ENGLISH_BOTSWANA"),
1025 _T("ENGLISH_CANADA"),
1026 _T("ENGLISH_CARIBBEAN"),
1027 _T("ENGLISH_DENMARK"),
1029 _T("ENGLISH_JAMAICA"),
1030 _T("ENGLISH_NEW_ZEALAND"),
1031 _T("ENGLISH_PHILIPPINES"),
1032 _T("ENGLISH_SOUTH_AFRICA"),
1033 _T("ENGLISH_TRINIDAD"),
1034 _T("ENGLISH_ZIMBABWE"),
1042 _T("FRENCH_BELGIAN"),
1043 _T("FRENCH_CANADIAN"),
1044 _T("FRENCH_LUXEMBOURG"),
1045 _T("FRENCH_MONACO"),
1051 _T("GERMAN_AUSTRIAN"),
1052 _T("GERMAN_BELGIUM"),
1053 _T("GERMAN_LIECHTENSTEIN"),
1054 _T("GERMAN_LUXEMBOURG"),
1072 _T("ITALIAN_SWISS"),
1077 _T("KASHMIRI_INDIA"),
1095 _T("MALAY_BRUNEI_DARUSSALAM"),
1096 _T("MALAY_MALAYSIA"),
1106 _T("NORWEGIAN_BOKMAL"),
1107 _T("NORWEGIAN_NYNORSK"),
1114 _T("PORTUGUESE_BRAZILIAN"),
1117 _T("RHAETO_ROMANCE"),
1120 _T("RUSSIAN_UKRAINE"),
1126 _T("SERBIAN_CYRILLIC"),
1127 _T("SERBIAN_LATIN"),
1128 _T("SERBO_CROATIAN"),
1139 _T("SPANISH_ARGENTINA"),
1140 _T("SPANISH_BOLIVIA"),
1141 _T("SPANISH_CHILE"),
1142 _T("SPANISH_COLOMBIA"),
1143 _T("SPANISH_COSTA_RICA"),
1144 _T("SPANISH_DOMINICAN_REPUBLIC"),
1145 _T("SPANISH_ECUADOR"),
1146 _T("SPANISH_EL_SALVADOR"),
1147 _T("SPANISH_GUATEMALA"),
1148 _T("SPANISH_HONDURAS"),
1149 _T("SPANISH_MEXICAN"),
1150 _T("SPANISH_MODERN"),
1151 _T("SPANISH_NICARAGUA"),
1152 _T("SPANISH_PANAMA"),
1153 _T("SPANISH_PARAGUAY"),
1155 _T("SPANISH_PUERTO_RICO"),
1156 _T("SPANISH_URUGUAY"),
1158 _T("SPANISH_VENEZUELA"),
1162 _T("SWEDISH_FINLAND"),
1180 _T("URDU_PAKISTAN"),
1182 _T("UZBEK_CYRILLIC"),
1195 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1196 return languageNames
[lang
];
1198 return _T("INVALID");
1201 static void TestDefaultLang()
1203 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1205 gs_localeDefault
.Init(wxLANGUAGE_ENGLISH
);
1207 static const wxChar
*langStrings
[] =
1209 NULL
, // system default
1216 _T("de_DE.iso88591"),
1218 _T("?"), // invalid lang spec
1219 _T("klingonese"), // I bet on some systems it does exist...
1222 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1223 wxLocale::GetSystemEncodingName().c_str(),
1224 wxLocale::GetSystemEncoding());
1226 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1228 const wxChar
*langStr
= langStrings
[n
];
1231 // FIXME: this doesn't do anything at all under Windows, we need
1232 // to create a new wxLocale!
1233 wxSetEnv(_T("LC_ALL"), langStr
);
1236 int lang
= gs_localeDefault
.GetSystemLanguage();
1237 wxPrintf(_T("Locale for '%s' is %s.\n"),
1238 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1242 #endif // TEST_LOCALE
1244 // ----------------------------------------------------------------------------
1246 // ----------------------------------------------------------------------------
1250 #include "wx/mimetype.h"
1252 static void TestMimeEnum()
1254 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1256 wxArrayString mimetypes
;
1258 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1260 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1265 for ( size_t n
= 0; n
< count
; n
++ )
1267 wxFileType
*filetype
=
1268 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1271 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1272 mimetypes
[n
].c_str());
1276 filetype
->GetDescription(&desc
);
1277 filetype
->GetExtensions(exts
);
1279 filetype
->GetIcon(NULL
);
1282 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1285 extsAll
<< _T(", ");
1289 wxPrintf(_T("\t%s: %s (%s)\n"),
1290 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1293 wxPuts(wxEmptyString
);
1296 static void TestMimeFilename()
1298 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1300 static const wxChar
*filenames
[] =
1308 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1310 const wxString fname
= filenames
[n
];
1311 wxString ext
= fname
.AfterLast(_T('.'));
1312 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1315 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1320 if ( !ft
->GetDescription(&desc
) )
1321 desc
= _T("<no description>");
1324 if ( !ft
->GetOpenCommand(&cmd
,
1325 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1326 cmd
= _T("<no command available>");
1328 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1330 wxPrintf(_T("To open %s (%s) do %s.\n"),
1331 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1337 wxPuts(wxEmptyString
);
1340 // these tests were broken by wxMimeTypesManager changes, temporarily disabling
1343 static void TestMimeOverride()
1345 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1347 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1348 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1350 if ( wxFile::Exists(mailcap
) )
1351 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1353 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1355 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1358 if ( wxFile::Exists(mimetypes
) )
1359 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1361 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1363 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1366 wxPuts(wxEmptyString
);
1369 static void TestMimeAssociate()
1371 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1373 wxFileTypeInfo
ftInfo(
1374 _T("application/x-xyz"),
1375 _T("xyzview '%s'"), // open cmd
1376 _T(""), // print cmd
1377 _T("XYZ File"), // description
1378 _T(".xyz"), // extensions
1379 wxNullPtr
// end of extensions
1381 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1383 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1386 wxPuts(_T("ERROR: failed to create association!"));
1390 // TODO: read it back
1394 wxPuts(wxEmptyString
);
1401 // ----------------------------------------------------------------------------
1402 // module dependencies feature
1403 // ----------------------------------------------------------------------------
1407 #include "wx/module.h"
1409 class wxTestModule
: public wxModule
1412 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1413 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1416 class wxTestModuleA
: public wxTestModule
1421 DECLARE_DYNAMIC_CLASS(wxTestModuleA
)
1424 class wxTestModuleB
: public wxTestModule
1429 DECLARE_DYNAMIC_CLASS(wxTestModuleB
)
1432 class wxTestModuleC
: public wxTestModule
1437 DECLARE_DYNAMIC_CLASS(wxTestModuleC
)
1440 class wxTestModuleD
: public wxTestModule
1445 DECLARE_DYNAMIC_CLASS(wxTestModuleD
)
1448 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC
, wxModule
)
1449 wxTestModuleC::wxTestModuleC()
1451 AddDependency(CLASSINFO(wxTestModuleD
));
1454 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA
, wxModule
)
1455 wxTestModuleA::wxTestModuleA()
1457 AddDependency(CLASSINFO(wxTestModuleB
));
1458 AddDependency(CLASSINFO(wxTestModuleD
));
1461 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD
, wxModule
)
1462 wxTestModuleD::wxTestModuleD()
1466 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB
, wxModule
)
1467 wxTestModuleB::wxTestModuleB()
1469 AddDependency(CLASSINFO(wxTestModuleD
));
1470 AddDependency(CLASSINFO(wxTestModuleC
));
1473 #endif // TEST_MODULE
1475 // ----------------------------------------------------------------------------
1476 // misc information functions
1477 // ----------------------------------------------------------------------------
1479 #ifdef TEST_INFO_FUNCTIONS
1481 #include "wx/utils.h"
1483 #if TEST_INTERACTIVE
1484 static void TestDiskInfo()
1486 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1490 wxChar pathname
[128];
1491 wxPrintf(_T("\nEnter a directory name: "));
1492 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1495 // kill the last '\n'
1496 pathname
[wxStrlen(pathname
) - 1] = 0;
1498 wxLongLong total
, free
;
1499 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1501 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1505 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1506 (total
/ 1024).ToString().c_str(),
1507 (free
/ 1024).ToString().c_str(),
1512 #endif // TEST_INTERACTIVE
1514 static void TestOsInfo()
1516 wxPuts(_T("*** Testing OS info functions ***\n"));
1519 wxGetOsVersion(&major
, &minor
);
1520 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1521 wxGetOsDescription().c_str(), major
, minor
);
1523 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1525 wxPrintf(_T("Host name is %s (%s).\n"),
1526 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1528 wxPuts(wxEmptyString
);
1531 static void TestPlatformInfo()
1533 wxPuts(_T("*** Testing wxPlatformInfo functions ***\n"));
1535 // get this platform
1536 wxPlatformInfo plat
;
1538 wxPrintf(_T("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
1539 wxPrintf(_T("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
1540 wxPrintf(_T("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
1541 wxPrintf(_T("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
1542 wxPrintf(_T("Architecture is: %s\n"), plat
.GetArchName().c_str());
1543 wxPrintf(_T("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
1545 wxPuts(wxEmptyString
);
1548 static void TestUserInfo()
1550 wxPuts(_T("*** Testing user info functions ***\n"));
1552 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1553 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1554 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1555 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1557 wxPuts(wxEmptyString
);
1560 #endif // TEST_INFO_FUNCTIONS
1562 // ----------------------------------------------------------------------------
1564 // ----------------------------------------------------------------------------
1566 #ifdef TEST_PATHLIST
1569 #define CMD_IN_PATH _T("ls")
1571 #define CMD_IN_PATH _T("command.com")
1574 static void TestPathList()
1576 wxPuts(_T("*** Testing wxPathList ***\n"));
1578 wxPathList pathlist
;
1579 pathlist
.AddEnvList(_T("PATH"));
1580 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1583 wxPrintf(_T("ERROR: command not found in the path.\n"));
1587 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1591 #endif // TEST_PATHLIST
1593 // ----------------------------------------------------------------------------
1594 // regular expressions
1595 // ----------------------------------------------------------------------------
1597 #if defined TEST_REGEX && TEST_INTERACTIVE
1599 #include "wx/regex.h"
1601 static void TestRegExInteractive()
1603 wxPuts(_T("*** Testing RE interactively ***"));
1607 wxChar pattern
[128];
1608 wxPrintf(_T("\nEnter a pattern: "));
1609 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1612 // kill the last '\n'
1613 pattern
[wxStrlen(pattern
) - 1] = 0;
1616 if ( !re
.Compile(pattern
) )
1624 wxPrintf(_T("Enter text to match: "));
1625 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1628 // kill the last '\n'
1629 text
[wxStrlen(text
) - 1] = 0;
1631 if ( !re
.Matches(text
) )
1633 wxPrintf(_T("No match.\n"));
1637 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1640 for ( size_t n
= 1; ; n
++ )
1642 if ( !re
.GetMatch(&start
, &len
, n
) )
1647 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1648 n
, wxString(text
+ start
, len
).c_str());
1655 #endif // TEST_REGEX
1657 // ----------------------------------------------------------------------------
1659 // ----------------------------------------------------------------------------
1662 NB: this stuff was taken from the glibc test suite and modified to build
1663 in wxWidgets: if I read the copyright below properly, this shouldn't
1669 #ifdef wxTEST_PRINTF
1670 // use our functions from wxchar.cpp
1674 // NB: do _not_ use WX_ATTRIBUTE_PRINTF here, we have some invalid formats
1675 // in the tests below
1676 int wxPrintf( const wxChar
*format
, ... );
1677 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1680 #include "wx/longlong.h"
1684 static void rfg1 (void);
1685 static void rfg2 (void);
1689 fmtchk (const wxChar
*fmt
)
1691 (void) wxPrintf(_T("%s:\t`"), fmt
);
1692 (void) wxPrintf(fmt
, 0x12);
1693 (void) wxPrintf(_T("'\n"));
1697 fmtst1chk (const wxChar
*fmt
)
1699 (void) wxPrintf(_T("%s:\t`"), fmt
);
1700 (void) wxPrintf(fmt
, 4, 0x12);
1701 (void) wxPrintf(_T("'\n"));
1705 fmtst2chk (const wxChar
*fmt
)
1707 (void) wxPrintf(_T("%s:\t`"), fmt
);
1708 (void) wxPrintf(fmt
, 4, 4, 0x12);
1709 (void) wxPrintf(_T("'\n"));
1712 /* This page is covered by the following copyright: */
1714 /* (C) Copyright C E Chew
1716 * Feel free to copy, use and distribute this software provided:
1718 * 1. you do not pretend that you wrote it
1719 * 2. you leave this copyright notice intact.
1723 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1730 /* Formatted Output Test
1732 * This exercises the output formatting code.
1735 wxChar
*PointerNull
= NULL
;
1742 wxChar
*prefix
= buf
;
1745 wxPuts(_T("\nFormatted output test"));
1746 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1747 wxStrcpy(prefix
, _T("%"));
1748 for (i
= 0; i
< 2; i
++) {
1749 for (j
= 0; j
< 2; j
++) {
1750 for (k
= 0; k
< 2; k
++) {
1751 for (l
= 0; l
< 2; l
++) {
1752 wxStrcpy(prefix
, _T("%"));
1753 if (i
== 0) wxStrcat(prefix
, _T("-"));
1754 if (j
== 0) wxStrcat(prefix
, _T("+"));
1755 if (k
== 0) wxStrcat(prefix
, _T("#"));
1756 if (l
== 0) wxStrcat(prefix
, _T("0"));
1757 wxPrintf(_T("%5s |"), prefix
);
1758 wxStrcpy(tp
, prefix
);
1759 wxStrcat(tp
, _T("6d |"));
1761 wxStrcpy(tp
, prefix
);
1762 wxStrcat(tp
, _T("6o |"));
1764 wxStrcpy(tp
, prefix
);
1765 wxStrcat(tp
, _T("6x |"));
1767 wxStrcpy(tp
, prefix
);
1768 wxStrcat(tp
, _T("6X |"));
1770 wxStrcpy(tp
, prefix
);
1771 wxStrcat(tp
, _T("6u |"));
1778 wxPrintf(_T("%10s\n"), PointerNull
);
1779 wxPrintf(_T("%-10s\n"), PointerNull
);
1782 static void TestPrintf()
1784 static wxChar shortstr
[] = _T("Hi, Z.");
1785 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1786 I am ready for my first lesson today.");
1788 wxString test_format
;
1792 fmtchk(_T("%4.4x"));
1793 fmtchk(_T("%04.4x"));
1794 fmtchk(_T("%4.3x"));
1795 fmtchk(_T("%04.3x"));
1797 fmtst1chk(_T("%.*x"));
1798 fmtst1chk(_T("%0*x"));
1799 fmtst2chk(_T("%*.*x"));
1800 fmtst2chk(_T("%0*.*x"));
1802 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1803 wxPrintf(bad_format
.c_str());
1804 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1806 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1807 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1808 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1809 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1810 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1811 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1812 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1813 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1814 wxPrintf(test_format
.c_str(), -123456);
1815 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1816 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1818 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1819 wxPrintf(test_format
.c_str(), shortstr
);
1820 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1821 wxPrintf(test_format
.c_str(), shortstr
);
1822 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1823 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1824 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1825 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1827 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1828 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1829 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1830 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1831 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1832 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1833 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1834 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1835 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1836 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1837 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1838 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1840 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1841 wxPrintf (_T(" %6.5f\n"), .1);
1842 wxPrintf (_T("x%5.4fx\n"), .5);
1844 wxPrintf (_T("%#03x\n"), 1);
1846 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1852 while (niter
-- != 0)
1853 wxPrintf (_T("%.17e\n"), d
/ 2);
1858 // Open Watcom cause compiler error here
1859 // Error! E173: col(24) floating-point constant too small to represent
1860 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1863 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1864 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1865 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1866 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1867 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1868 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1869 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1870 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1871 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1872 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1877 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1879 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1880 rc
, WXSIZEOF(buf
), buf
);
1883 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1884 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1890 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1891 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1892 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1893 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1894 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1895 wxPrintf (_T("%g should be 10\n"), 10.0);
1896 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1900 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1906 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1908 result
|= wxStrcmp (buf
,
1909 _T("onetwo three "));
1911 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1918 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1920 // for some reason below line fails under Borland
1921 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1924 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1927 wxPuts (_T("\tFAILED"));
1929 wxUnusedVar(result
);
1930 wxPuts (wxEmptyString
);
1932 #endif // wxLongLong_t
1934 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1935 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1937 wxPuts (_T("--- Should be no further output. ---"));
1946 memset (bytes
, '\xff', sizeof bytes
);
1947 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1948 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1949 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1951 wxPuts (_T("%hhn overwrite more bytes"));
1956 wxPuts (_T("%hhn wrote incorrect value"));
1968 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1969 if (wxStrcmp (buf
, _T(" ")) != 0)
1970 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1971 wxSprintf (buf
, _T("%5.f"), 33.3);
1972 if (wxStrcmp (buf
, _T(" 33")) != 0)
1973 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1974 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1975 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1976 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1977 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1978 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1979 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1980 wxSprintf (buf
, _T("%.g"), 33.3);
1981 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1982 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1983 wxSprintf (buf
, _T("%.G"), 33.3);
1984 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1985 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1993 wxString test_format
;
1996 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
1997 if (wxStrcmp (buf
, _T("3")) != 0)
1998 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
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("%7.*G"), prec
, 3.33);
2005 if (wxStrcmp (buf
, _T(" 3")) != 0)
2006 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
2008 test_format
= _T("%04.*o");
2009 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2010 if (wxStrcmp (buf
, _T(" 041")) != 0)
2011 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
2013 test_format
= _T("%09.*u");
2014 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2015 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
2016 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
2018 test_format
= _T("%04.*x");
2019 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2020 if (wxStrcmp (buf
, _T(" 021")) != 0)
2021 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2023 test_format
= _T("%04.*X");
2024 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2025 if (wxStrcmp (buf
, _T(" 021")) != 0)
2026 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2029 #endif // TEST_PRINTF
2031 // ----------------------------------------------------------------------------
2032 // registry and related stuff
2033 // ----------------------------------------------------------------------------
2035 // this is for MSW only
2038 #undef TEST_REGISTRY
2043 #include "wx/confbase.h"
2044 #include "wx/msw/regconf.h"
2047 static void TestRegConfWrite()
2049 wxConfig
*config
= new wxConfig(_T("myapp"));
2050 config
->SetPath(_T("/group1"));
2051 config
->Write(_T("entry1"), _T("foo"));
2052 config
->SetPath(_T("/group2"));
2053 config
->Write(_T("entry1"), _T("bar"));
2057 static void TestRegConfRead()
2059 wxRegConfig
*config
= new wxRegConfig(_T("myapp"));
2063 config
->SetPath(_T("/"));
2064 wxPuts(_T("Enumerating / subgroups:"));
2065 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2069 bCont
= config
->GetNextGroup(str
, dummy
);
2073 #endif // TEST_REGCONF
2075 #ifdef TEST_REGISTRY
2077 #include "wx/msw/registry.h"
2079 // I chose this one because I liked its name, but it probably only exists under
2081 static const wxChar
*TESTKEY
=
2082 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2084 static void TestRegistryRead()
2086 wxPuts(_T("*** testing registry reading ***"));
2088 wxRegKey
key(TESTKEY
);
2089 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2092 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2097 size_t nSubKeys
, nValues
;
2098 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2100 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2103 wxPrintf(_T("Enumerating values:\n"));
2107 bool cont
= key
.GetFirstValue(value
, dummy
);
2110 wxPrintf(_T("Value '%s': type "), value
.c_str());
2111 switch ( key
.GetValueType(value
) )
2113 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2114 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2115 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2116 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2117 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2118 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2119 default: wxPrintf(_T("other (unknown)")); break;
2122 wxPrintf(_T(", value = "));
2123 if ( key
.IsNumericValue(value
) )
2126 key
.QueryValue(value
, &val
);
2127 wxPrintf(_T("%ld"), val
);
2132 key
.QueryValue(value
, val
);
2133 wxPrintf(_T("'%s'"), val
.c_str());
2135 key
.QueryRawValue(value
, val
);
2136 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2141 cont
= key
.GetNextValue(value
, dummy
);
2145 static void TestRegistryAssociation()
2148 The second call to deleteself genertaes an error message, with a
2149 messagebox saying .flo is crucial to system operation, while the .ddf
2150 call also fails, but with no error message
2155 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2157 key
= _T("ddxf_auto_file") ;
2158 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2160 key
= _T("ddxf_auto_file") ;
2161 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2163 key
= _T("program,0") ;
2164 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2166 key
= _T("program \"%1\"") ;
2168 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2170 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2172 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2174 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2178 #endif // TEST_REGISTRY
2180 // ----------------------------------------------------------------------------
2182 // ----------------------------------------------------------------------------
2184 #ifdef TEST_SCOPEGUARD
2186 #include "wx/scopeguard.h"
2188 static void function0() { puts("function0()"); }
2189 static void function1(int n
) { printf("function1(%d)\n", n
); }
2190 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2194 void method0() { printf("method0()\n"); }
2195 void method1(int n
) { printf("method1(%d)\n", n
); }
2196 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2199 static void TestScopeGuard()
2201 wxON_BLOCK_EXIT0(function0
);
2202 wxON_BLOCK_EXIT1(function1
, 17);
2203 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2206 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2207 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2208 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2210 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2211 dismissed
.Dismiss();
2216 // ----------------------------------------------------------------------------
2218 // ----------------------------------------------------------------------------
2222 #include "wx/socket.h"
2223 #include "wx/protocol/protocol.h"
2224 #include "wx/protocol/http.h"
2226 static void TestSocketServer()
2228 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2230 static const int PORT
= 3000;
2235 wxSocketServer
*server
= new wxSocketServer(addr
);
2236 if ( !server
->Ok() )
2238 wxPuts(_T("ERROR: failed to bind"));
2246 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2248 wxSocketBase
*socket
= server
->Accept();
2251 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2255 wxPuts(_T("Server: got a client."));
2257 server
->SetTimeout(60); // 1 min
2260 while ( !close
&& socket
->IsConnected() )
2263 wxChar ch
= _T('\0');
2266 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2268 // don't log error if the client just close the connection
2269 if ( socket
->IsConnected() )
2271 wxPuts(_T("ERROR: in wxSocket::Read."));
2291 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2292 if ( s
== _T("close") )
2294 wxPuts(_T("Closing connection"));
2298 else if ( s
== _T("quit") )
2303 wxPuts(_T("Shutting down the server"));
2305 else // not a special command
2307 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2308 socket
->Write("\r\n", 2);
2309 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2315 wxPuts(_T("Server: lost a client unexpectedly."));
2321 // same as "delete server" but is consistent with GUI programs
2325 static void TestSocketClient()
2327 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2329 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2332 addr
.Hostname(hostname
);
2335 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2337 wxSocketClient client
;
2338 if ( !client
.Connect(addr
) )
2340 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2344 wxPrintf(_T("--- Connected to %s:%u...\n"),
2345 addr
.Hostname().c_str(), addr
.Service());
2349 // could use simply "GET" here I suppose
2351 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2352 client
.Write(cmdGet
, cmdGet
.length());
2353 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2354 MakePrintable(cmdGet
).c_str());
2355 client
.Read(buf
, WXSIZEOF(buf
));
2356 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2360 #endif // TEST_SOCKETS
2362 // ----------------------------------------------------------------------------
2364 // ----------------------------------------------------------------------------
2368 #include "wx/protocol/ftp.h"
2369 #include "wx/protocol/log.h"
2371 #define FTP_ANONYMOUS
2375 #ifdef FTP_ANONYMOUS
2376 static const wxChar
*directory
= _T("/pub");
2377 static const wxChar
*filename
= _T("welcome.msg");
2379 static const wxChar
*directory
= _T("/etc");
2380 static const wxChar
*filename
= _T("issue");
2383 static bool TestFtpConnect()
2385 wxPuts(_T("*** Testing FTP connect ***"));
2387 #ifdef FTP_ANONYMOUS
2388 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2390 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2391 #else // !FTP_ANONYMOUS
2392 static const wxChar
*hostname
= "localhost";
2395 wxFgets(user
, WXSIZEOF(user
), stdin
);
2396 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2399 wxChar password
[256];
2400 wxPrintf(_T("Password for %s: "), password
);
2401 wxFgets(password
, WXSIZEOF(password
), stdin
);
2402 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2403 ftp
->SetPassword(password
);
2405 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2406 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2408 if ( !ftp
->Connect(hostname
) )
2410 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2416 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2417 hostname
, ftp
->Pwd().c_str());
2424 static void TestFtpList()
2426 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2429 if ( !ftp
->ChDir(directory
) )
2431 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2434 wxPrintf(_T("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2436 // test NLIST and LIST
2437 wxArrayString files
;
2438 if ( !ftp
->GetFilesList(files
) )
2440 wxPuts(_T("ERROR: failed to get NLIST of files"));
2444 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
->Pwd().c_str());
2445 size_t count
= files
.GetCount();
2446 for ( size_t n
= 0; n
< count
; n
++ )
2448 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2450 wxPuts(_T("End of the file list"));
2453 if ( !ftp
->GetDirList(files
) )
2455 wxPuts(_T("ERROR: failed to get LIST of files"));
2459 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
->Pwd().c_str());
2460 size_t count
= files
.GetCount();
2461 for ( size_t n
= 0; n
< count
; n
++ )
2463 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2465 wxPuts(_T("End of the file list"));
2468 if ( !ftp
->ChDir(_T("..")) )
2470 wxPuts(_T("ERROR: failed to cd to .."));
2473 wxPrintf(_T("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2476 static void TestFtpDownload()
2478 wxPuts(_T("*** Testing wxFTP download ***\n"));
2481 wxInputStream
*in
= ftp
->GetInputStream(filename
);
2484 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2488 size_t size
= in
->GetSize();
2489 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2492 wxChar
*data
= new wxChar
[size
];
2493 if ( !in
->Read(data
, size
) )
2495 wxPuts(_T("ERROR: read error"));
2499 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2507 static void TestFtpFileSize()
2509 wxPuts(_T("*** Testing FTP SIZE command ***"));
2511 if ( !ftp
->ChDir(directory
) )
2513 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2516 wxPrintf(_T("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2518 if ( ftp
->FileExists(filename
) )
2520 int size
= ftp
->GetFileSize(filename
);
2522 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2524 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2528 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2532 static void TestFtpMisc()
2534 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2536 if ( ftp
->SendCommand(_T("STAT")) != '2' )
2538 wxPuts(_T("ERROR: STAT failed"));
2542 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
->GetLastResult().c_str());
2545 if ( ftp
->SendCommand(_T("HELP SITE")) != '2' )
2547 wxPuts(_T("ERROR: HELP SITE failed"));
2551 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2552 ftp
->GetLastResult().c_str());
2556 #if TEST_INTERACTIVE
2558 static void TestFtpInteractive()
2560 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2566 wxPrintf(_T("Enter FTP command: "));
2567 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2570 // kill the last '\n'
2571 buf
[wxStrlen(buf
) - 1] = 0;
2573 // special handling of LIST and NLST as they require data connection
2574 wxString
start(buf
, 4);
2576 if ( start
== _T("LIST") || start
== _T("NLST") )
2579 if ( wxStrlen(buf
) > 4 )
2582 wxArrayString files
;
2583 if ( !ftp
->GetList(files
, wildcard
, start
== _T("LIST")) )
2585 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2589 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2590 start
.c_str(), wildcard
.c_str(), ftp
->Pwd().c_str());
2591 size_t count
= files
.GetCount();
2592 for ( size_t n
= 0; n
< count
; n
++ )
2594 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2596 wxPuts(_T("--- End of the file list"));
2601 wxChar ch
= ftp
->SendCommand(buf
);
2602 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2605 wxPrintf(_T(" (return code %c)"), ch
);
2608 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
->GetLastResult().c_str());
2612 wxPuts(_T("\n*** done ***"));
2615 #endif // TEST_INTERACTIVE
2617 static void TestFtpUpload()
2619 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2622 static const wxChar
*file1
= _T("test1");
2623 static const wxChar
*file2
= _T("test2");
2624 wxOutputStream
*out
= ftp
->GetOutputStream(file1
);
2627 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2628 out
->Write("First hello", 11);
2632 // send a command to check the remote file
2633 if ( ftp
->SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2635 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2639 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2640 file1
, ftp
->GetLastResult().c_str());
2643 out
= ftp
->GetOutputStream(file2
);
2646 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2647 out
->Write("Second hello", 12);
2654 // ----------------------------------------------------------------------------
2656 // ----------------------------------------------------------------------------
2658 #ifdef TEST_STACKWALKER
2660 #if wxUSE_STACKWALKER
2662 #include "wx/stackwalk.h"
2664 class StackDump
: public wxStackWalker
2667 StackDump(const char *argv0
)
2668 : wxStackWalker(argv0
)
2672 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
2674 wxPuts(_T("Stack dump:"));
2676 wxStackWalker::Walk(skip
, maxdepth
);
2680 virtual void OnStackFrame(const wxStackFrame
& frame
)
2682 printf("[%2d] ", (int) frame
.GetLevel());
2684 wxString name
= frame
.GetName();
2685 if ( !name
.empty() )
2687 printf("%-20.40s", (const char*)name
.mb_str());
2691 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2694 if ( frame
.HasSourceLocation() )
2697 (const char*)frame
.GetFileName().mb_str(),
2698 (int)frame
.GetLine());
2704 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2706 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
2707 (const char*)name
.mb_str(),
2708 (const char*)val
.mb_str());
2713 static void TestStackWalk(const char *argv0
)
2715 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2717 StackDump
dump(argv0
);
2721 #endif // wxUSE_STACKWALKER
2723 #endif // TEST_STACKWALKER
2725 // ----------------------------------------------------------------------------
2727 // ----------------------------------------------------------------------------
2729 #ifdef TEST_STDPATHS
2731 #include "wx/stdpaths.h"
2732 #include "wx/wxchar.h" // wxPrintf
2734 static void TestStandardPaths()
2736 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2738 wxTheApp
->SetAppName(_T("console"));
2740 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2741 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2742 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2743 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2744 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2745 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2746 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2747 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2748 wxPrintf(_T("Executable path:\t%s\n"), stdp
.GetExecutablePath().c_str());
2749 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2750 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2751 wxPrintf(_T("Localized res. dir:\t%s\n"),
2752 stdp
.GetLocalizedResourcesDir(_T("fr")).c_str());
2753 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2754 stdp
.GetLocalizedResourcesDir
2757 wxStandardPaths::ResourceCat_Messages
2761 #endif // TEST_STDPATHS
2763 // ----------------------------------------------------------------------------
2765 // ----------------------------------------------------------------------------
2769 #include "wx/wfstream.h"
2770 #include "wx/mstream.h"
2772 static void TestFileStream()
2774 wxPuts(_T("*** Testing wxFileInputStream ***"));
2776 static const wxString filename
= _T("testdata.fs");
2778 wxFileOutputStream
fsOut(filename
);
2779 fsOut
.Write("foo", 3);
2783 wxFileInputStream
fsIn(filename
);
2784 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2786 while ( (c
=fsIn
.GetC()) != wxEOF
)
2792 if ( !wxRemoveFile(filename
) )
2794 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2797 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2800 static void TestMemoryStream()
2802 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2804 wxMemoryOutputStream memOutStream
;
2805 wxPrintf(_T("Initially out stream offset: %lu\n"),
2806 (unsigned long)memOutStream
.TellO());
2808 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2810 memOutStream
.PutC(*p
);
2813 wxPrintf(_T("Final out stream offset: %lu\n"),
2814 (unsigned long)memOutStream
.TellO());
2816 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2819 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2821 wxMemoryInputStream
memInpStream(buf
, len
);
2822 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2824 while ( (c
=memInpStream
.GetC()) != wxEOF
)
2829 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2832 #endif // TEST_STREAMS
2834 // ----------------------------------------------------------------------------
2836 // ----------------------------------------------------------------------------
2840 #include "wx/stopwatch.h"
2841 #include "wx/utils.h"
2843 static void TestStopWatch()
2845 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2849 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2852 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2854 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2858 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2861 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2864 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2867 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2870 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2873 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2874 for ( size_t n
= 0; n
< 70; n
++ )
2878 for ( size_t m
= 0; m
< 100000; m
++ )
2880 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2882 wxPuts(_T("\ntime is negative - ERROR!"));
2890 wxPuts(_T(", ok."));
2893 #include "wx/timer.h"
2894 #include "wx/evtloop.h"
2898 wxPuts(_T("*** Testing wxTimer ***\n"));
2900 class MyTimer
: public wxTimer
2903 MyTimer() : wxTimer() { m_num
= 0; }
2905 virtual void Notify()
2907 wxPrintf(_T("%d"), m_num
++);
2912 wxPrintf(_T("... exiting the event loop"));
2915 wxEventLoop::GetActive()->Exit(0);
2916 wxPuts(_T(", ok."));
2929 timer1
.Start(100, true /* one shot */);
2931 timer1
.Start(100, true /* one shot */);
2939 #endif // TEST_TIMER
2941 // ----------------------------------------------------------------------------
2943 // ----------------------------------------------------------------------------
2945 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
2951 #include "wx/volume.h"
2953 static const wxChar
*volumeKinds
[] =
2959 _T("network volume"),
2963 static void TestFSVolume()
2965 wxPuts(_T("*** Testing wxFSVolume class ***"));
2967 wxArrayString volumes
= wxFSVolume::GetVolumes();
2968 size_t count
= volumes
.GetCount();
2972 wxPuts(_T("ERROR: no mounted volumes?"));
2976 wxPrintf(_T("%u mounted volumes found:\n"), count
);
2978 for ( size_t n
= 0; n
< count
; n
++ )
2980 wxFSVolume
vol(volumes
[n
]);
2983 wxPuts(_T("ERROR: couldn't create volume"));
2987 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
2989 vol
.GetDisplayName().c_str(),
2990 vol
.GetName().c_str(),
2991 volumeKinds
[vol
.GetKind()],
2992 vol
.IsWritable() ? _T("rw") : _T("ro"),
2993 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
2998 #endif // TEST_VOLUME
3000 // ----------------------------------------------------------------------------
3001 // wide char and Unicode support
3002 // ----------------------------------------------------------------------------
3006 #include "wx/strconv.h"
3007 #include "wx/fontenc.h"
3008 #include "wx/encconv.h"
3009 #include "wx/buffer.h"
3011 static const unsigned char utf8koi8r
[] =
3013 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3014 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3015 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3016 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3017 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3018 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3019 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3022 static const unsigned char utf8iso8859_1
[] =
3024 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3025 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3026 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3027 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3028 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3031 static const unsigned char utf8Invalid
[] =
3033 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3034 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3035 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3036 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3040 static const struct Utf8Data
3042 const unsigned char *text
;
3044 const wxChar
*charset
;
3045 wxFontEncoding encoding
;
3048 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3049 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3050 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3053 static void TestUtf8()
3055 wxPuts(_T("*** Testing UTF8 support ***\n"));
3060 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3062 const Utf8Data
& u8d
= utf8data
[n
];
3063 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3064 WXSIZEOF(wbuf
)) == (size_t)-1 )
3066 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3070 wxCSConv
conv(u8d
.charset
);
3071 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3073 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3077 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3081 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3083 s
= _T("<< conversion failed >>");
3084 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3088 wxPuts(wxEmptyString
);
3091 static void TestEncodingConverter()
3093 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3095 // using wxEncodingConverter should give the same result as above
3098 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3099 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3101 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3105 wxEncodingConverter ec
;
3106 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3107 ec
.Convert(wbuf
, buf
);
3108 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3111 wxPuts(wxEmptyString
);
3114 #endif // TEST_WCHAR
3116 // ----------------------------------------------------------------------------
3118 // ----------------------------------------------------------------------------
3122 #include "wx/filesys.h"
3123 #include "wx/fs_zip.h"
3124 #include "wx/zipstrm.h"
3126 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3128 static void TestZipStreamRead()
3130 wxPuts(_T("*** Testing ZIP reading ***\n"));
3132 static const wxString filename
= _T("foo");
3133 wxFFileInputStream
in(TESTFILE_ZIP
);
3134 wxZipInputStream
istr(in
);
3135 wxZipEntry
entry(filename
);
3136 istr
.OpenEntry(entry
);
3138 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3140 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3142 while ( (c
=istr
.GetC()) != wxEOF
)
3148 wxPuts(_T("\n----- done ------"));
3151 static void DumpZipDirectory(wxFileSystem
& fs
,
3152 const wxString
& dir
,
3153 const wxString
& indent
)
3155 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3156 TESTFILE_ZIP
, dir
.c_str());
3157 wxString wildcard
= prefix
+ _T("/*");
3159 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3160 while ( !dirname
.empty() )
3162 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3164 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3169 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3171 DumpZipDirectory(fs
, dirname
,
3172 indent
+ wxString(_T(' '), 4));
3174 dirname
= fs
.FindNext();
3177 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3178 while ( !filename
.empty() )
3180 if ( !filename
.StartsWith(prefix
, &filename
) )
3182 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3187 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3189 filename
= fs
.FindNext();
3193 static void TestZipFileSystem()
3195 wxPuts(_T("*** Testing ZIP file system ***\n"));
3197 wxFileSystem::AddHandler(new wxZipFSHandler
);
3199 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3201 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3206 // ----------------------------------------------------------------------------
3208 // ----------------------------------------------------------------------------
3210 #ifdef TEST_DATETIME
3212 #include "wx/math.h"
3213 #include "wx/datetime.h"
3215 // this test miscellaneous static wxDateTime functions
3219 static void TestTimeStatic()
3221 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3223 // some info about the current date
3224 int year
= wxDateTime::GetCurrentYear();
3225 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3227 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3228 wxDateTime::GetNumberOfDays(year
));
3230 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3231 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3232 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3233 wxDateTime::GetMonthName(month
).c_str(),
3234 wxDateTime::GetNumberOfDays(month
));
3237 // test time zones stuff
3238 static void TestTimeZones()
3240 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3242 wxDateTime now
= wxDateTime::Now();
3244 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3245 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3246 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3247 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3248 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3249 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3251 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3253 wxDateTime::Tm tm
= now
.GetTm();
3254 if ( wxDateTime(tm
) != now
)
3256 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3257 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3261 // test some minimal support for the dates outside the standard range
3262 static void TestTimeRange()
3264 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3266 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3268 wxPrintf(_T("Unix epoch:\t%s\n"),
3269 wxDateTime(2440587.5).Format(fmt
).c_str());
3270 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3271 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3272 wxPrintf(_T("JDN 0: \t%s\n"),
3273 wxDateTime(0.0).Format(fmt
).c_str());
3274 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3275 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3276 wxPrintf(_T("May 29, 2099:\t%s\n"),
3277 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3280 // test DST calculations
3281 static void TestTimeDST()
3283 wxPuts(_T("\n*** wxDateTime DST test ***"));
3285 wxPrintf(_T("DST is%s in effect now.\n\n"),
3286 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3288 for ( int year
= 1990; year
< 2005; year
++ )
3290 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3292 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3293 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3299 #if TEST_INTERACTIVE
3301 static void TestDateTimeInteractive()
3303 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3309 wxPrintf(_T("Enter a date: "));
3310 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3313 // kill the last '\n'
3314 buf
[wxStrlen(buf
) - 1] = 0;
3317 const wxChar
*p
= dt
.ParseDate(buf
);
3320 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3326 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3329 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3330 dt
.Format(_T("%b %d, %Y")).c_str(),
3332 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3333 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3334 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3337 wxPuts(_T("\n*** done ***"));
3340 #endif // TEST_INTERACTIVE
3344 static void TestTimeMS()
3346 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3348 wxDateTime dt1
= wxDateTime::Now(),
3349 dt2
= wxDateTime::UNow();
3351 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3352 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3353 wxPrintf(_T("Dummy loop: "));
3354 for ( int i
= 0; i
< 6000; i
++ )
3356 //for ( int j = 0; j < 10; j++ )
3359 s
.Printf(_T("%g"), sqrt((float)i
));
3365 wxPuts(_T(", done"));
3368 dt2
= wxDateTime::UNow();
3369 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3371 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3373 wxPuts(_T("\n*** done ***"));
3376 static void TestTimeHolidays()
3378 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3380 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3381 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3382 dtEnd
= dtStart
.GetLastMonthDay();
3384 wxDateTimeArray hol
;
3385 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3387 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3389 wxPrintf(_T("All holidays between %s and %s:\n"),
3390 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3392 size_t count
= hol
.GetCount();
3393 for ( size_t n
= 0; n
< count
; n
++ )
3395 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3398 wxPuts(wxEmptyString
);
3401 static void TestTimeZoneBug()
3403 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3405 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3406 for ( int i
= 0; i
< 31; i
++ )
3408 wxPrintf(_T("Date %s: week day %s.\n"),
3409 date
.Format(_T("%d-%m-%Y")).c_str(),
3410 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3412 date
+= wxDateSpan::Day();
3415 wxPuts(wxEmptyString
);
3418 static void TestTimeSpanFormat()
3420 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3422 static const wxChar
*formats
[] =
3424 _T("(default) %H:%M:%S"),
3425 _T("%E weeks and %D days"),
3426 _T("%l milliseconds"),
3427 _T("(with ms) %H:%M:%S:%l"),
3428 _T("100%% of minutes is %M"), // test "%%"
3429 _T("%D days and %H hours"),
3430 _T("or also %S seconds"),
3433 wxTimeSpan
ts1(1, 2, 3, 4),
3435 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3437 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3438 ts1
.Format(formats
[n
]).c_str(),
3439 ts2
.Format(formats
[n
]).c_str());
3442 wxPuts(wxEmptyString
);
3447 #endif // TEST_DATETIME
3449 // ----------------------------------------------------------------------------
3450 // wxTextInput/OutputStream
3451 // ----------------------------------------------------------------------------
3453 #ifdef TEST_TEXTSTREAM
3455 #include "wx/txtstrm.h"
3456 #include "wx/wfstream.h"
3458 static void TestTextInputStream()
3460 wxPuts(_T("\n*** wxTextInputStream test ***"));
3462 wxString filename
= _T("testdata.fc");
3463 wxFileInputStream
fsIn(filename
);
3466 wxPuts(_T("ERROR: couldn't open file."));
3470 wxTextInputStream
tis(fsIn
);
3475 const wxString s
= tis
.ReadLine();
3477 // line could be non empty if the last line of the file isn't
3478 // terminated with EOL
3479 if ( fsIn
.Eof() && s
.empty() )
3482 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3487 #endif // TEST_TEXTSTREAM
3489 // ----------------------------------------------------------------------------
3491 // ----------------------------------------------------------------------------
3495 #include "wx/thread.h"
3497 static size_t gs_counter
= (size_t)-1;
3498 static wxCriticalSection gs_critsect
;
3499 static wxSemaphore gs_cond
;
3501 class MyJoinableThread
: public wxThread
3504 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3505 { m_n
= n
; Create(); }
3507 // thread execution starts here
3508 virtual ExitCode
Entry();
3514 wxThread::ExitCode
MyJoinableThread::Entry()
3516 unsigned long res
= 1;
3517 for ( size_t n
= 1; n
< m_n
; n
++ )
3521 // it's a loooong calculation :-)
3525 return (ExitCode
)res
;
3528 class MyDetachedThread
: public wxThread
3531 MyDetachedThread(size_t n
, wxChar ch
)
3535 m_cancelled
= false;
3540 // thread execution starts here
3541 virtual ExitCode
Entry();
3544 virtual void OnExit();
3547 size_t m_n
; // number of characters to write
3548 wxChar m_ch
; // character to write
3550 bool m_cancelled
; // false if we exit normally
3553 wxThread::ExitCode
MyDetachedThread::Entry()
3556 wxCriticalSectionLocker
lock(gs_critsect
);
3557 if ( gs_counter
== (size_t)-1 )
3563 for ( size_t n
= 0; n
< m_n
; n
++ )
3565 if ( TestDestroy() )
3575 wxThread::Sleep(100);
3581 void MyDetachedThread::OnExit()
3583 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3585 wxCriticalSectionLocker
lock(gs_critsect
);
3586 if ( !--gs_counter
&& !m_cancelled
)
3590 static void TestDetachedThreads()
3592 wxPuts(_T("\n*** Testing detached threads ***"));
3594 static const size_t nThreads
= 3;
3595 MyDetachedThread
*threads
[nThreads
];
3597 for ( n
= 0; n
< nThreads
; n
++ )
3599 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3602 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3603 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3605 for ( n
= 0; n
< nThreads
; n
++ )
3610 // wait until all threads terminate
3613 wxPuts(wxEmptyString
);
3616 static void TestJoinableThreads()
3618 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3620 // calc 10! in the background
3621 MyJoinableThread
thread(10);
3624 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3625 (unsigned long)thread
.Wait());
3628 static void TestThreadSuspend()
3630 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3632 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3636 // this is for this demo only, in a real life program we'd use another
3637 // condition variable which would be signaled from wxThread::Entry() to
3638 // tell us that the thread really started running - but here just wait a
3639 // bit and hope that it will be enough (the problem is, of course, that
3640 // the thread might still not run when we call Pause() which will result
3642 wxThread::Sleep(300);
3644 for ( size_t n
= 0; n
< 3; n
++ )
3648 wxPuts(_T("\nThread suspended"));
3651 // don't sleep but resume immediately the first time
3652 wxThread::Sleep(300);
3654 wxPuts(_T("Going to resume the thread"));
3659 wxPuts(_T("Waiting until it terminates now"));
3661 // wait until the thread terminates
3664 wxPuts(wxEmptyString
);
3667 static void TestThreadDelete()
3669 // As above, using Sleep() is only for testing here - we must use some
3670 // synchronisation object instead to ensure that the thread is still
3671 // running when we delete it - deleting a detached thread which already
3672 // terminated will lead to a crash!
3674 wxPuts(_T("\n*** Testing thread delete function ***"));
3676 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3680 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3682 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3686 wxThread::Sleep(300);
3690 wxPuts(_T("\nDeleted a running thread."));
3692 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3696 wxThread::Sleep(300);
3702 wxPuts(_T("\nDeleted a sleeping thread."));
3704 MyJoinableThread
thread3(20);
3709 wxPuts(_T("\nDeleted a joinable thread."));
3711 MyJoinableThread
thread4(2);
3714 wxThread::Sleep(300);
3718 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3720 wxPuts(wxEmptyString
);
3723 class MyWaitingThread
: public wxThread
3726 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3729 m_condition
= condition
;
3734 virtual ExitCode
Entry()
3736 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3741 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3745 m_condition
->Wait();
3748 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3756 wxCondition
*m_condition
;
3759 static void TestThreadConditions()
3762 wxCondition
condition(mutex
);
3764 // otherwise its difficult to understand which log messages pertain to
3766 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3767 // condition.GetId(), gs_cond.GetId());
3769 // create and launch threads
3770 MyWaitingThread
*threads
[10];
3773 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3775 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
3778 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3783 // wait until all threads run
3784 wxPuts(_T("Main thread is waiting for the other threads to start"));
3787 size_t nRunning
= 0;
3788 while ( nRunning
< WXSIZEOF(threads
) )
3794 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
3798 wxPuts(_T("Main thread: all threads started up."));
3801 wxThread::Sleep(500);
3804 // now wake one of them up
3805 wxPrintf(_T("Main thread: about to signal the condition.\n"));
3810 wxThread::Sleep(200);
3812 // wake all the (remaining) threads up, so that they can exit
3813 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
3815 condition
.Broadcast();
3817 // give them time to terminate (dirty!)
3818 wxThread::Sleep(500);
3822 #include "wx/datetime.h"
3824 class MySemaphoreThread
: public wxThread
3827 MySemaphoreThread(int i
, wxSemaphore
*sem
)
3828 : wxThread(wxTHREAD_JOINABLE
),
3835 virtual ExitCode
Entry()
3837 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
3838 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3842 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
3843 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3847 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
3848 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3860 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
3862 static void TestSemaphore()
3864 wxPuts(_T("*** Testing wxSemaphore class. ***"));
3866 static const int SEM_LIMIT
= 3;
3868 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
3869 ArrayThreads threads
;
3871 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
3873 threads
.Add(new MySemaphoreThread(i
, &sem
));
3874 threads
.Last()->Run();
3877 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
3884 #endif // TEST_THREADS
3886 // ----------------------------------------------------------------------------
3888 // ----------------------------------------------------------------------------
3890 #ifdef TEST_SNGLINST
3891 #include "wx/snglinst.h"
3892 #endif // TEST_SNGLINST
3894 int main(int argc
, char **argv
)
3897 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
3902 for (n
= 0; n
< argc
; n
++ )
3904 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
3905 wxArgv
[n
] = wxStrdup(warg
);
3910 #else // !wxUSE_UNICODE
3912 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
3914 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
3916 wxInitializer initializer
;
3919 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
3924 #ifdef TEST_SNGLINST
3925 wxSingleInstanceChecker checker
;
3926 if ( checker
.Create(_T(".wxconsole.lock")) )
3928 if ( checker
.IsAnotherRunning() )
3930 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
3935 // wait some time to give time to launch another instance
3936 wxPrintf(_T("Press \"Enter\" to continue..."));
3939 else // failed to create
3941 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
3943 #endif // TEST_SNGLINST
3946 TestCmdLineConvert();
3948 #if wxUSE_CMDLINE_PARSER
3949 static const wxCmdLineEntryDesc cmdLineDesc
[] =
3951 { wxCMD_LINE_SWITCH
, "h", "help", "show this help message",
3952 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
3953 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
3954 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
3956 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
3957 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
3958 { wxCMD_LINE_OPTION
, "s", "size", "output block size",
3959 wxCMD_LINE_VAL_NUMBER
},
3960 { wxCMD_LINE_OPTION
, "d", "date", "output file date",
3961 wxCMD_LINE_VAL_DATE
},
3962 { wxCMD_LINE_OPTION
, "f", "double", "output double",
3963 wxCMD_LINE_VAL_DOUBLE
},
3965 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
3966 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
3971 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
3973 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
3974 wxCMD_LINE_VAL_STRING
,
3975 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
3977 switch ( parser
.Parse() )
3980 wxLogMessage(_T("Help was given, terminating."));
3984 ShowCmdLine(parser
);
3988 wxLogMessage(_T("Syntax error detected, aborting."));
3991 #endif // wxUSE_CMDLINE_PARSER
3993 #endif // TEST_CMDLINE
4005 TestDllListLoaded();
4006 #endif // TEST_DYNLIB
4010 #endif // TEST_ENVIRON
4012 #ifdef TEST_FILECONF
4014 #endif // TEST_FILECONF
4018 #endif // TEST_LOCALE
4021 wxPuts(_T("*** Testing wxLog ***"));
4024 for ( size_t n
= 0; n
< 8000; n
++ )
4026 s
<< (wxChar
)(_T('A') + (n
% 26));
4029 wxLogWarning(_T("The length of the string is %lu"),
4030 (unsigned long)s
.length());
4033 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4035 // this one shouldn't be truncated
4038 // but this one will because log functions use fixed size buffer
4039 // (note that it doesn't need '\n' at the end neither - will be added
4041 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4051 #ifdef TEST_FILENAME
4054 TestFileNameDirManip();
4055 TestFileNameComparison();
4056 TestFileNameOperations();
4057 #endif // TEST_FILENAME
4059 #ifdef TEST_FILETIME
4064 #endif // TEST_FILETIME
4067 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4069 // wxFTP cannot be a static variable as its ctor needs to access
4070 // wxWidgets internals after it has been initialized
4072 ftp
->SetLog(new wxProtocolLog(FTP_TRACE_MASK
));
4074 if ( TestFtpConnect() )
4084 #if TEST_INTERACTIVE
4085 //TestFtpInteractive();
4088 //else: connecting to the FTP server failed
4094 //wxLog::AddTraceMask(_T("mime"));
4098 TestMimeAssociate();
4103 #ifdef TEST_INFO_FUNCTIONS
4108 #if TEST_INTERACTIVE
4111 #endif // TEST_INFO_FUNCTIONS
4113 #ifdef TEST_PATHLIST
4115 #endif // TEST_PATHLIST
4119 #endif // TEST_PRINTF
4126 #endif // TEST_REGCONF
4128 #if defined TEST_REGEX && TEST_INTERACTIVE
4129 TestRegExInteractive();
4130 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4132 #ifdef TEST_REGISTRY
4134 TestRegistryAssociation();
4135 #endif // TEST_REGISTRY
4140 #endif // TEST_SOCKETS
4147 #endif // TEST_STREAMS
4149 #ifdef TEST_TEXTSTREAM
4150 TestTextInputStream();
4151 #endif // TEST_TEXTSTREAM
4154 int nCPUs
= wxThread::GetCPUCount();
4155 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4157 wxThread::SetConcurrency(nCPUs
);
4159 TestJoinableThreads();
4162 TestJoinableThreads();
4163 TestDetachedThreads();
4164 TestThreadSuspend();
4166 TestThreadConditions();
4169 #endif // TEST_THREADS
4174 #endif // TEST_TIMER
4176 #ifdef TEST_DATETIME
4183 TestTimeSpanFormat();
4189 #if TEST_INTERACTIVE
4190 TestDateTimeInteractive();
4192 #endif // TEST_DATETIME
4194 #ifdef TEST_SCOPEGUARD
4198 #ifdef TEST_STACKWALKER
4199 #if wxUSE_STACKWALKER
4200 TestStackWalk(argv
[0]);
4202 #endif // TEST_STACKWALKER
4204 #ifdef TEST_STDPATHS
4205 TestStandardPaths();
4209 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4211 #endif // TEST_USLEEP
4215 #endif // TEST_VOLUME
4219 TestEncodingConverter();
4220 #endif // TEST_WCHAR
4223 TestZipStreamRead();
4224 TestZipFileSystem();
4229 for ( int n
= 0; n
< argc
; n
++ )
4234 #endif // wxUSE_UNICODE