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(wxT("Help was given, terminating."));
48 // everything is ok; proceed
52 wxLogMessage(wxT("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(wxT("\t"), wxT("\\t"));
168 (void)str
.Replace(wxT("\n"), wxT("\\n"));
169 (void)str
.Replace(wxT("\r"), wxT("\\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
= wxT("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 << wxT("Verbose:\t") << (parser
.Found(wxT("v")) ? wxT("yes") : wxT("no")) << '\n'
199 << wxT("Quiet:\t") << (parser
.Found(wxT("q")) ? wxT("yes") : wxT("no")) << '\n';
205 if ( parser
.Found(wxT("o"), &strVal
) )
206 s
<< wxT("Output file:\t") << strVal
<< '\n';
207 if ( parser
.Found(wxT("i"), &strVal
) )
208 s
<< wxT("Input dir:\t") << strVal
<< '\n';
209 if ( parser
.Found(wxT("s"), &lVal
) )
210 s
<< wxT("Size:\t") << lVal
<< '\n';
211 if ( parser
.Found(wxT("f"), &dVal
) )
212 s
<< wxT("Double:\t") << dVal
<< '\n';
213 if ( parser
.Found(wxT("d"), &dt
) )
214 s
<< wxT("Date:\t") << dt
.FormatISODate() << '\n';
215 if ( parser
.Found(wxT("project_name"), &strVal
) )
216 s
<< wxT("Project:\t") << strVal
<< '\n';
221 #endif // wxUSE_CMDLINE_PARSER
223 static void TestCmdLineConvert()
225 static const wxChar
*cmdlines
[] =
228 wxT("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
229 wxT("literal \\\" and \"\""),
232 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
234 const wxChar
*cmdline
= cmdlines
[n
];
235 wxPrintf(wxT("Parsing: %s\n"), cmdline
);
236 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
238 size_t count
= args
.GetCount();
239 wxPrintf(wxT("\targc = %u\n"), count
);
240 for ( size_t arg
= 0; arg
< count
; arg
++ )
242 wxPrintf(wxT("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
247 #endif // TEST_CMDLINE
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
258 static const wxChar
*ROOTDIR
= wxT("/");
259 static const wxChar
*TESTDIR
= wxT("/usr/local/share");
260 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
261 static const wxChar
*ROOTDIR
= wxT("c:\\");
262 static const wxChar
*TESTDIR
= wxT("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(wxT("\t%s\n"), filename
.c_str());
281 cont
= dir
.GetNext(&filename
);
284 wxPuts(wxEmptyString
);
289 static void TestDirEnum()
291 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
293 wxString cwd
= wxGetCwd();
294 if ( !wxDir::Exists(cwd
) )
296 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
301 if ( !dir
.IsOpened() )
303 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
307 wxPuts(wxT("Enumerating everything in current directory:"));
308 TestDirEnumHelper(dir
);
310 wxPuts(wxT("Enumerating really everything in current directory:"));
311 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
313 wxPuts(wxT("Enumerating object files in current directory:"));
314 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, wxT("*.o*"));
316 wxPuts(wxT("Enumerating directories in current directory:"));
317 TestDirEnumHelper(dir
, wxDIR_DIRS
);
319 wxPuts(wxT("Enumerating files in current directory:"));
320 TestDirEnumHelper(dir
, wxDIR_FILES
);
322 wxPuts(wxT("Enumerating files including hidden in current directory:"));
323 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
327 wxPuts(wxT("Enumerating everything in root directory:"));
328 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
330 wxPuts(wxT("Enumerating directories in root directory:"));
331 TestDirEnumHelper(dir
, wxDIR_DIRS
);
333 wxPuts(wxT("Enumerating files in root directory:"));
334 TestDirEnumHelper(dir
, wxDIR_FILES
);
336 wxPuts(wxT("Enumerating files including hidden in root directory:"));
337 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
339 wxPuts(wxT("Enumerating files in non existing directory:"));
340 wxDir
dirNo(wxT("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
<< wxT('.') << ext
;
363 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
365 if ( wxIsPathSeparator(*p
) )
369 wxPrintf(wxT("%s%s\n"), indent
.c_str(), name
.c_str());
371 return wxDIR_CONTINUE
;
375 static void TestDirTraverse()
377 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
381 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
382 wxPrintf(wxT("There are %u files under '%s'\n"), n
, TESTDIR
);
385 wxPrintf(wxT("First one is '%s'\n"), files
[0u].c_str());
386 wxPrintf(wxT(" last one is '%s'\n"), files
[n
- 1].c_str());
389 // enum again with custom traverser
390 wxPuts(wxT("Now enumerating directories:"));
392 DirPrintTraverser traverser
;
393 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
398 static void TestDirExists()
400 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
402 static const wxChar
*dirnames
[] =
405 #if defined(__WXMSW__)
408 wxT("\\\\share\\file"),
412 wxT("c:\\autoexec.bat"),
413 #elif defined(__UNIX__)
422 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
424 wxPrintf(wxT("%-40s: %s\n"),
426 wxDir::Exists(dirnames
[n
]) ? wxT("exists")
427 : wxT("doesn't exist"));
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
441 #include "wx/dynlib.h"
443 static void TestDllLoad()
445 #if defined(__WXMSW__)
446 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
447 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
448 #elif defined(__UNIX__)
449 // weird: using just libc.so does *not* work!
450 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
451 static const wxChar
*FUNC_NAME
= wxT("strlen");
453 #error "don't know how to test wxDllLoader on this platform"
456 wxPuts(wxT("*** testing basic wxDynamicLibrary functions ***\n"));
458 wxDynamicLibrary
lib(LIB_NAME
);
459 if ( !lib
.IsLoaded() )
461 wxPrintf(wxT("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(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
470 FUNC_NAME
, LIB_NAME
);
474 wxPrintf(wxT("Calling %s dynamically loaded from %s "),
475 FUNC_NAME
, LIB_NAME
);
477 if ( pfnStrlen("foo") != 3 )
479 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
483 wxPuts(wxT("... ok"));
488 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
490 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
492 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
493 if ( !pfnStrlenAorW
)
495 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
496 FUNC_NAME_AW
, LIB_NAME
);
500 if ( pfnStrlenAorW(wxT("foobar")) != 6 )
502 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
509 #if defined(__WXMSW__) || defined(__UNIX__)
511 static void TestDllListLoaded()
513 wxPuts(wxT("*** 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());
523 void *addr
wxDUMMY_INITIALIZE(NULL
);
524 size_t len
wxDUMMY_INITIALIZE(0);
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
) )
551 val
= wxT("<empty>");
553 val
= wxString(wxT('\'')) + val
+ wxT('\'');
558 static void TestEnvironment()
560 const wxChar
*var
= wxT("wxTestVar");
562 wxPuts(wxT("*** testing environment access functions ***"));
564 wxPrintf(wxT("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
565 wxSetEnv(var
, wxT("value for wxTestVar"));
566 wxPrintf(wxT("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
567 wxSetEnv(var
, wxT("another value"));
568 wxPrintf(wxT("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
570 wxPrintf(wxT("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
571 wxPrintf(wxT("PATH = %s\n"), MyGetEnv(wxT("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(wxT("*** wxFile read test ***"));
590 wxFile
file(wxT("testdata.fc"));
591 if ( file
.IsOpened() )
593 wxPrintf(wxT("File length: %lu\n"), file
.Length());
595 wxPuts(wxT("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(wxT("Failed to read the file."));
608 fwrite(buf
, nRead
, 1, stdout
);
614 wxPuts(wxT("----------"));
618 wxPrintf(wxT("ERROR: can't open test file.\n"));
621 wxPuts(wxEmptyString
);
624 static void TestTextFileRead()
626 wxPuts(wxT("*** wxTextFile read test ***"));
628 wxTextFile
file(wxT("testdata.fc"));
631 wxPrintf(wxT("Number of lines: %u\n"), file
.GetLineCount());
632 wxPrintf(wxT("Last line: '%s'\n"), file
.GetLastLine().c_str());
636 wxPuts(wxT("\nDumping the entire file:"));
637 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
639 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
641 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
643 wxPuts(wxT("\nAnd now backwards:"));
644 for ( s
= file
.GetLastLine();
645 file
.GetCurrentLine() != 0;
646 s
= file
.GetPrevLine() )
648 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
650 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
654 wxPrintf(wxT("ERROR: can't open '%s'\n"), file
.GetName());
657 wxPuts(wxEmptyString
);
660 static void TestFileCopy()
662 wxPuts(wxT("*** Testing wxCopyFile ***"));
664 static const wxChar
*filename1
= wxT("testdata.fc");
665 static const wxChar
*filename2
= wxT("test2");
666 if ( !wxCopyFile(filename1
, filename2
) )
668 wxPuts(wxT("ERROR: failed to copy file"));
672 wxFFile
f1(filename1
, wxT("rb")),
673 f2(filename2
, wxT("rb"));
675 if ( !f1
.IsOpened() || !f2
.IsOpened() )
677 wxPuts(wxT("ERROR: failed to open file(s)"));
682 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
684 wxPuts(wxT("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(wxT("ERROR: copy error!"));
695 wxPuts(wxT("File was copied ok."));
701 if ( !wxRemoveFile(filename2
) )
703 wxPuts(wxT("ERROR: failed to remove the file"));
706 wxPuts(wxEmptyString
);
709 static void TestTempFile()
711 wxPuts(wxT("*** wxTempFile test ***"));
714 if ( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) )
716 if ( tmpFile
.Commit() )
717 wxPuts(wxT("File committed."));
719 wxPuts(wxT("ERROR: could't commit temp file."));
721 wxRemoveFile(wxT("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 { wxT("value1"), wxT("one") },
745 { wxT("value2"), wxT("two") },
746 { wxT("novalue"), wxT("default") },
749 static void TestFileConfRead()
751 wxPuts(wxT("*** testing wxFileConfig loading/reading ***"));
753 wxFileConfig
fileconf(wxT("test"), wxEmptyString
,
754 wxT("testdata.fc"), wxEmptyString
,
755 wxCONFIG_USE_RELATIVE_PATH
);
757 // test simple reading
758 wxPuts(wxT("\nReading config file:"));
759 wxString
defValue(wxT("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(wxT("\t%s = %s "), data
.name
, value
.c_str());
765 if ( value
== data
.value
)
771 wxPrintf(wxT("(ERROR: should be %s)\n"), data
.value
);
775 // test enumerating the entries
776 wxPuts(wxT("\nEnumerating all root entries:"));
779 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
782 wxPrintf(wxT("\t%s = %s\n"),
784 fileconf
.Read(name
.c_str(), wxT("ERROR")).c_str());
786 cont
= fileconf
.GetNextEntry(name
, dummy
);
789 static const wxChar
*testEntry
= wxT("TestEntry");
790 wxPrintf(wxT("\nTesting deletion of newly created \"Test\" entry: "));
791 fileconf
.Write(testEntry
, wxT("A value"));
792 fileconf
.DeleteEntry(testEntry
);
793 wxPrintf(fileconf
.HasEntry(testEntry
) ? wxT("ERROR\n") : wxT("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(wxT("'%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(wxT("or\t\t-> path '%s', name '%s', ext '%s'\n"),
821 path
.c_str(), name
.c_str(), ext
.c_str());
823 wxPrintf(wxT("path is also:\t'%s'\n"), fn
.GetPath().c_str());
824 wxPrintf(wxT("with volume: \t'%s'\n"),
825 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
826 wxPrintf(wxT("with separator:\t'%s'\n"),
827 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
828 wxPrintf(wxT("with both: \t'%s'\n"),
829 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
831 wxPuts(wxT("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(wxT("\t%u: %s\n"), n
, dirs
[n
].c_str());
841 static void TestFileNameTemp()
843 wxPuts(wxT("*** testing wxFileName temp file creation ***"));
845 static const wxChar
*tmpprefixes
[] =
853 wxT("/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(wxT("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
867 wxPrintf(wxT("Prefix '%s'\t-> temp file '%s'\n"),
868 tmpprefixes
[n
], path
.c_str());
870 if ( !wxRemoveFile(path
) )
872 wxLogWarning(wxT("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(wxT("testdata.fc"));
914 wxDateTime dtAccess
, dtMod
, dtCreate
;
915 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
917 wxPrintf(wxT("ERROR: GetTimes() failed.\n"));
921 static const wxChar
*fmt
= wxT("%Y-%b-%d %H:%M:%S");
923 wxPrintf(wxT("File times for '%s':\n"), fn
.GetFullPath().c_str());
924 wxPrintf(wxT("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
925 wxPrintf(wxT("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
926 wxPrintf(wxT("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
931 static void TestFileSetTimes()
933 wxFileName
fn(wxT("testdata.fc"));
937 wxPrintf(wxT("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 wxT("ARABIC_ALGERIA"),
972 wxT("ARABIC_BAHRAIN"),
975 wxT("ARABIC_JORDAN"),
976 wxT("ARABIC_KUWAIT"),
977 wxT("ARABIC_LEBANON"),
979 wxT("ARABIC_MOROCCO"),
982 wxT("ARABIC_SAUDI_ARABIA"),
985 wxT("ARABIC_TUNISIA"),
992 wxT("AZERI_CYRILLIC"),
1007 wxT("CHINESE_SIMPLIFIED"),
1008 wxT("CHINESE_TRADITIONAL"),
1009 wxT("CHINESE_HONGKONG"),
1010 wxT("CHINESE_MACAU"),
1011 wxT("CHINESE_SINGAPORE"),
1012 wxT("CHINESE_TAIWAN"),
1018 wxT("DUTCH_BELGIAN"),
1022 wxT("ENGLISH_AUSTRALIA"),
1023 wxT("ENGLISH_BELIZE"),
1024 wxT("ENGLISH_BOTSWANA"),
1025 wxT("ENGLISH_CANADA"),
1026 wxT("ENGLISH_CARIBBEAN"),
1027 wxT("ENGLISH_DENMARK"),
1028 wxT("ENGLISH_EIRE"),
1029 wxT("ENGLISH_JAMAICA"),
1030 wxT("ENGLISH_NEW_ZEALAND"),
1031 wxT("ENGLISH_PHILIPPINES"),
1032 wxT("ENGLISH_SOUTH_AFRICA"),
1033 wxT("ENGLISH_TRINIDAD"),
1034 wxT("ENGLISH_ZIMBABWE"),
1042 wxT("FRENCH_BELGIAN"),
1043 wxT("FRENCH_CANADIAN"),
1044 wxT("FRENCH_LUXEMBOURG"),
1045 wxT("FRENCH_MONACO"),
1046 wxT("FRENCH_SWISS"),
1051 wxT("GERMAN_AUSTRIAN"),
1052 wxT("GERMAN_BELGIUM"),
1053 wxT("GERMAN_LIECHTENSTEIN"),
1054 wxT("GERMAN_LUXEMBOURG"),
1055 wxT("GERMAN_SWISS"),
1072 wxT("ITALIAN_SWISS"),
1077 wxT("KASHMIRI_INDIA"),
1095 wxT("MALAY_BRUNEI_DARUSSALAM"),
1096 wxT("MALAY_MALAYSIA"),
1105 wxT("NEPALI_INDIA"),
1106 wxT("NORWEGIAN_BOKMAL"),
1107 wxT("NORWEGIAN_NYNORSK"),
1114 wxT("PORTUGUESE_BRAZILIAN"),
1117 wxT("RHAETO_ROMANCE"),
1120 wxT("RUSSIAN_UKRAINE"),
1124 wxT("SCOTS_GAELIC"),
1126 wxT("SERBIAN_CYRILLIC"),
1127 wxT("SERBIAN_LATIN"),
1128 wxT("SERBO_CROATIAN"),
1139 wxT("SPANISH_ARGENTINA"),
1140 wxT("SPANISH_BOLIVIA"),
1141 wxT("SPANISH_CHILE"),
1142 wxT("SPANISH_COLOMBIA"),
1143 wxT("SPANISH_COSTA_RICA"),
1144 wxT("SPANISH_DOMINICAN_REPUBLIC"),
1145 wxT("SPANISH_ECUADOR"),
1146 wxT("SPANISH_EL_SALVADOR"),
1147 wxT("SPANISH_GUATEMALA"),
1148 wxT("SPANISH_HONDURAS"),
1149 wxT("SPANISH_MEXICAN"),
1150 wxT("SPANISH_MODERN"),
1151 wxT("SPANISH_NICARAGUA"),
1152 wxT("SPANISH_PANAMA"),
1153 wxT("SPANISH_PARAGUAY"),
1154 wxT("SPANISH_PERU"),
1155 wxT("SPANISH_PUERTO_RICO"),
1156 wxT("SPANISH_URUGUAY"),
1158 wxT("SPANISH_VENEZUELA"),
1162 wxT("SWEDISH_FINLAND"),
1180 wxT("URDU_PAKISTAN"),
1182 wxT("UZBEK_CYRILLIC"),
1195 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1196 return languageNames
[lang
];
1198 return wxT("INVALID");
1201 static void TestDefaultLang()
1203 wxPuts(wxT("*** Testing wxLocale::GetSystemLanguage ***"));
1205 gs_localeDefault
.Init(wxLANGUAGE_ENGLISH
);
1207 static const wxChar
*langStrings
[] =
1209 NULL
, // system default
1216 wxT("de_DE.iso88591"),
1218 wxT("?"), // invalid lang spec
1219 wxT("klingonese"), // I bet on some systems it does exist...
1222 wxPrintf(wxT("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(wxT("LC_ALL"), langStr
);
1236 int lang
= gs_localeDefault
.GetSystemLanguage();
1237 wxPrintf(wxT("Locale for '%s' is %s.\n"),
1238 langStr
? langStr
: wxT("system default"), GetLangName(lang
));
1242 #endif // TEST_LOCALE
1244 // ----------------------------------------------------------------------------
1246 // ----------------------------------------------------------------------------
1250 #include "wx/mimetype.h"
1252 static void TestMimeEnum()
1254 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1256 wxArrayString mimetypes
;
1258 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1260 wxPrintf(wxT("*** 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(wxT("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
<< wxT(", ");
1289 wxPrintf(wxT("\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(wxT("*** Testing MIME type from filename query ***\n"));
1300 static const wxChar
*filenames
[] =
1303 wxT("document.pdf"),
1305 wxT("picture.jpeg"),
1308 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1310 const wxString fname
= filenames
[n
];
1311 wxString ext
= fname
.AfterLast(wxT('.'));
1312 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1315 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1320 if ( !ft
->GetDescription(&desc
) )
1321 desc
= wxT("<no description>");
1324 if ( !ft
->GetOpenCommand(&cmd
,
1325 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1326 cmd
= wxT("<no command available>");
1328 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
1330 wxPrintf(wxT("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(wxT("*** Testing wxMimeTypesManager additional files loading ***\n"));
1347 static const wxChar
*mailcap
= wxT("/tmp/mailcap");
1348 static const wxChar
*mimetypes
= wxT("/tmp/mime.types");
1350 if ( wxFile::Exists(mailcap
) )
1351 wxPrintf(wxT("Loading mailcap from '%s': %s\n"),
1353 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? wxT("ok") : wxT("ERROR"));
1355 wxPrintf(wxT("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1358 if ( wxFile::Exists(mimetypes
) )
1359 wxPrintf(wxT("Loading mime.types from '%s': %s\n"),
1361 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? wxT("ok") : wxT("ERROR"));
1363 wxPrintf(wxT("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1366 wxPuts(wxEmptyString
);
1369 static void TestMimeAssociate()
1371 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
1373 wxFileTypeInfo
ftInfo(
1374 wxT("application/x-xyz"),
1375 wxT("xyzview '%s'"), // open cmd
1376 wxT(""), // print cmd
1377 wxT("XYZ File"), // description
1378 wxT(".xyz"), // extensions
1379 wxNullPtr
// end of extensions
1381 ftInfo
.SetShortDesc(wxT("XYZFile")); // used under Win32 only
1383 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1386 wxPuts(wxT("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(wxT("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1413 virtual void OnExit() { wxPrintf(wxT("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(wxT("*** Testing wxGetDiskSpace() ***"));
1490 wxChar pathname
[128];
1491 wxPrintf(wxT("\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(wxT("ERROR: wxGetDiskSpace failed."));
1505 wxPrintf(wxT("%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(wxT("*** Testing OS info functions ***\n"));
1519 wxGetOsVersion(&major
, &minor
);
1520 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
1521 wxGetOsDescription().c_str(), major
, minor
);
1523 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1525 wxPrintf(wxT("Host name is %s (%s).\n"),
1526 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1528 wxPuts(wxEmptyString
);
1531 static void TestPlatformInfo()
1533 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
1535 // get this platform
1536 wxPlatformInfo plat
;
1538 wxPrintf(wxT("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
1539 wxPrintf(wxT("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
1540 wxPrintf(wxT("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
1541 wxPrintf(wxT("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
1542 wxPrintf(wxT("Architecture is: %s\n"), plat
.GetArchName().c_str());
1543 wxPrintf(wxT("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
1545 wxPuts(wxEmptyString
);
1548 static void TestUserInfo()
1550 wxPuts(wxT("*** Testing user info functions ***\n"));
1552 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
1553 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
1554 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1555 wxPrintf(wxT("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 wxT("ls")
1571 #define CMD_IN_PATH wxT("command.com")
1574 static void TestPathList()
1576 wxPuts(wxT("*** Testing wxPathList ***\n"));
1578 wxPathList pathlist
;
1579 pathlist
.AddEnvList(wxT("PATH"));
1580 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1583 wxPrintf(wxT("ERROR: command not found in the path.\n"));
1587 wxPrintf(wxT("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(wxT("*** Testing RE interactively ***"));
1607 wxChar pattern
[128];
1608 wxPrintf(wxT("\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(wxT("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(wxT("No match.\n"));
1637 wxPrintf(wxT("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(wxT("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(wxT("%s:\t`"), fmt
);
1692 (void) wxPrintf(fmt
, 0x12);
1693 (void) wxPrintf(wxT("'\n"));
1697 fmtst1chk (const wxChar
*fmt
)
1699 (void) wxPrintf(wxT("%s:\t`"), fmt
);
1700 (void) wxPrintf(fmt
, 4, 0x12);
1701 (void) wxPrintf(wxT("'\n"));
1705 fmtst2chk (const wxChar
*fmt
)
1707 (void) wxPrintf(wxT("%s:\t`"), fmt
);
1708 (void) wxPrintf(fmt
, 4, 4, 0x12);
1709 (void) wxPrintf(wxT("'\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(wxT("\nFormatted output test"));
1746 wxPrintf(wxT("prefix 6d 6o 6x 6X 6u\n"));
1747 wxStrcpy(prefix
, wxT("%"));
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
, wxT("%"));
1753 if (i
== 0) wxStrcat(prefix
, wxT("-"));
1754 if (j
== 0) wxStrcat(prefix
, wxT("+"));
1755 if (k
== 0) wxStrcat(prefix
, wxT("#"));
1756 if (l
== 0) wxStrcat(prefix
, wxT("0"));
1757 wxPrintf(wxT("%5s |"), prefix
);
1758 wxStrcpy(tp
, prefix
);
1759 wxStrcat(tp
, wxT("6d |"));
1761 wxStrcpy(tp
, prefix
);
1762 wxStrcat(tp
, wxT("6o |"));
1764 wxStrcpy(tp
, prefix
);
1765 wxStrcat(tp
, wxT("6x |"));
1767 wxStrcpy(tp
, prefix
);
1768 wxStrcat(tp
, wxT("6X |"));
1770 wxStrcpy(tp
, prefix
);
1771 wxStrcat(tp
, wxT("6u |"));
1773 wxPrintf(wxT("\n"));
1778 wxPrintf(wxT("%10s\n"), PointerNull
);
1779 wxPrintf(wxT("%-10s\n"), PointerNull
);
1782 static void TestPrintf()
1784 static wxChar shortstr
[] = wxT("Hi, Z.");
1785 static wxChar longstr
[] = wxT("Good morning, Doctor Chandra. This is Hal. \
1786 I am ready for my first lesson today.");
1788 wxString test_format
;
1790 fmtchk(wxT("%.4x"));
1791 fmtchk(wxT("%04x"));
1792 fmtchk(wxT("%4.4x"));
1793 fmtchk(wxT("%04.4x"));
1794 fmtchk(wxT("%4.3x"));
1795 fmtchk(wxT("%04.3x"));
1797 fmtst1chk(wxT("%.*x"));
1798 fmtst1chk(wxT("%0*x"));
1799 fmtst2chk(wxT("%*.*x"));
1800 fmtst2chk(wxT("%0*.*x"));
1802 wxString bad_format
= wxT("bad format:\t\"%b\"\n");
1803 wxPrintf(bad_format
.c_str());
1804 wxPrintf(wxT("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1806 wxPrintf(wxT("decimal negative:\t\"%d\"\n"), -2345);
1807 wxPrintf(wxT("octal negative:\t\"%o\"\n"), -2345);
1808 wxPrintf(wxT("hex negative:\t\"%x\"\n"), -2345);
1809 wxPrintf(wxT("long decimal number:\t\"%ld\"\n"), -123456L);
1810 wxPrintf(wxT("long octal negative:\t\"%lo\"\n"), -2345L);
1811 wxPrintf(wxT("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1812 wxPrintf(wxT("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1813 test_format
= wxT("left-adjusted ZLDN:\t\"%-010ld\"\n");
1814 wxPrintf(test_format
.c_str(), -123456);
1815 wxPrintf(wxT("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1816 wxPrintf(wxT("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1818 test_format
= wxT("zero-padded string:\t\"%010s\"\n");
1819 wxPrintf(test_format
.c_str(), shortstr
);
1820 test_format
= wxT("left-adjusted Z string:\t\"%-010s\"\n");
1821 wxPrintf(test_format
.c_str(), shortstr
);
1822 wxPrintf(wxT("space-padded string:\t\"%10s\"\n"), shortstr
);
1823 wxPrintf(wxT("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1824 wxPrintf(wxT("null string:\t\"%s\"\n"), PointerNull
);
1825 wxPrintf(wxT("limited string:\t\"%.22s\"\n"), longstr
);
1827 wxPrintf(wxT("e-style >= 1:\t\"%e\"\n"), 12.34);
1828 wxPrintf(wxT("e-style >= .1:\t\"%e\"\n"), 0.1234);
1829 wxPrintf(wxT("e-style < .1:\t\"%e\"\n"), 0.001234);
1830 wxPrintf(wxT("e-style big:\t\"%.60e\"\n"), 1e20
);
1831 wxPrintf(wxT("e-style == .1:\t\"%e\"\n"), 0.1);
1832 wxPrintf(wxT("f-style >= 1:\t\"%f\"\n"), 12.34);
1833 wxPrintf(wxT("f-style >= .1:\t\"%f\"\n"), 0.1234);
1834 wxPrintf(wxT("f-style < .1:\t\"%f\"\n"), 0.001234);
1835 wxPrintf(wxT("g-style >= 1:\t\"%g\"\n"), 12.34);
1836 wxPrintf(wxT("g-style >= .1:\t\"%g\"\n"), 0.1234);
1837 wxPrintf(wxT("g-style < .1:\t\"%g\"\n"), 0.001234);
1838 wxPrintf(wxT("g-style big:\t\"%.60g\"\n"), 1e20
);
1840 wxPrintf (wxT(" %6.5f\n"), .099999999860301614);
1841 wxPrintf (wxT(" %6.5f\n"), .1);
1842 wxPrintf (wxT("x%5.4fx\n"), .5);
1844 wxPrintf (wxT("%#03x\n"), 1);
1846 //wxPrintf (wxT("something really insane: %.10000f\n"), 1.0);
1852 while (niter
-- != 0)
1853 wxPrintf (wxT("%.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 (wxT("%15.5e\n"), 4.9406564584124654e-324);
1863 #define FORMAT wxT("|%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
), wxT("%30s"), wxT("foo"));
1879 wxPrintf(wxT("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 (wxT("%e should be 1.234568e+06\n"), 1234567.8);
1891 wxPrintf (wxT("%f should be 1234567.800000\n"), 1234567.8);
1892 wxPrintf (wxT("%g should be 1.23457e+06\n"), 1234567.8);
1893 wxPrintf (wxT("%g should be 123.456\n"), 123.456);
1894 wxPrintf (wxT("%g should be 1e+06\n"), 1000000.0);
1895 wxPrintf (wxT("%g should be 10\n"), 10.0);
1896 wxPrintf (wxT("%g should be 0.02\n"), 0.02);
1900 wxPrintf(wxT("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1906 wxSprintf(buf
,wxT("%*s%*s%*s"),-1,wxT("one"),-20,wxT("two"),-30,wxT("three"));
1908 result
|= wxStrcmp (buf
,
1909 wxT("onetwo three "));
1911 wxPuts (result
!= 0 ? wxT("Test failed!") : wxT("Test ok."));
1918 wxSprintf(buf
, "%07" wxLongLongFmtSpec
"o", wxLL(040000000000));
1920 // for some reason below line fails under Borland
1921 wxPrintf (wxT("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1924 if (wxStrcmp (buf
, wxT("40000000000")) != 0)
1927 wxPuts (wxT("\tFAILED"));
1929 wxUnusedVar(result
);
1930 wxPuts (wxEmptyString
);
1932 #endif // wxLongLong_t
1934 wxPrintf (wxT("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1935 wxPrintf (wxT("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1937 wxPuts (wxT("--- Should be no further output. ---"));
1946 memset (bytes
, '\xff', sizeof bytes
);
1947 wxSprintf (buf
, wxT("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 (wxT("%hhn overwrite more bytes"));
1956 wxPuts (wxT("%hhn wrote incorrect value"));
1968 wxSprintf (buf
, wxT("%5.s"), wxT("xyz"));
1969 if (wxStrcmp (buf
, wxT(" ")) != 0)
1970 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" "));
1971 wxSprintf (buf
, wxT("%5.f"), 33.3);
1972 if (wxStrcmp (buf
, wxT(" 33")) != 0)
1973 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 33"));
1974 wxSprintf (buf
, wxT("%8.e"), 33.3e7
);
1975 if (wxStrcmp (buf
, wxT(" 3e+08")) != 0)
1976 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3e+08"));
1977 wxSprintf (buf
, wxT("%8.E"), 33.3e7
);
1978 if (wxStrcmp (buf
, wxT(" 3E+08")) != 0)
1979 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3E+08"));
1980 wxSprintf (buf
, wxT("%.g"), 33.3);
1981 if (wxStrcmp (buf
, wxT("3e+01")) != 0)
1982 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3e+01"));
1983 wxSprintf (buf
, wxT("%.G"), 33.3);
1984 if (wxStrcmp (buf
, wxT("3E+01")) != 0)
1985 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3E+01"));
1993 wxString test_format
;
1996 wxSprintf (buf
, wxT("%.*g"), prec
, 3.3);
1997 if (wxStrcmp (buf
, wxT("3")) != 0)
1998 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3"));
2000 wxSprintf (buf
, wxT("%.*G"), prec
, 3.3);
2001 if (wxStrcmp (buf
, wxT("3")) != 0)
2002 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3"));
2004 wxSprintf (buf
, wxT("%7.*G"), prec
, 3.33);
2005 if (wxStrcmp (buf
, wxT(" 3")) != 0)
2006 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3"));
2008 test_format
= wxT("%04.*o");
2009 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2010 if (wxStrcmp (buf
, wxT(" 041")) != 0)
2011 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 041"));
2013 test_format
= wxT("%09.*u");
2014 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2015 if (wxStrcmp (buf
, wxT(" 0000033")) != 0)
2016 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 0000033"));
2018 test_format
= wxT("%04.*x");
2019 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2020 if (wxStrcmp (buf
, wxT(" 021")) != 0)
2021 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 021"));
2023 test_format
= wxT("%04.*X");
2024 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2025 if (wxStrcmp (buf
, wxT(" 021")) != 0)
2026 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 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(wxT("myapp"));
2050 config
->SetPath(wxT("/group1"));
2051 config
->Write(wxT("entry1"), wxT("foo"));
2052 config
->SetPath(wxT("/group2"));
2053 config
->Write(wxT("entry1"), wxT("bar"));
2057 static void TestRegConfRead()
2059 wxRegConfig
*config
= new wxRegConfig(wxT("myapp"));
2063 config
->SetPath(wxT("/"));
2064 wxPuts(wxT("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 wxT("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2084 static void TestRegistryRead()
2086 wxPuts(wxT("*** testing registry reading ***"));
2088 wxRegKey
key(TESTKEY
);
2089 wxPrintf(wxT("The test key name is '%s'.\n"), key
.GetName().c_str());
2092 wxPuts(wxT("ERROR: test key can't be opened, aborting test."));
2097 size_t nSubKeys
, nValues
;
2098 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2100 wxPrintf(wxT("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2103 wxPrintf(wxT("Enumerating values:\n"));
2107 bool cont
= key
.GetFirstValue(value
, dummy
);
2110 wxPrintf(wxT("Value '%s': type "), value
.c_str());
2111 switch ( key
.GetValueType(value
) )
2113 case wxRegKey::Type_None
: wxPrintf(wxT("ERROR (none)")); break;
2114 case wxRegKey::Type_String
: wxPrintf(wxT("SZ")); break;
2115 case wxRegKey::Type_Expand_String
: wxPrintf(wxT("EXPAND_SZ")); break;
2116 case wxRegKey::Type_Binary
: wxPrintf(wxT("BINARY")); break;
2117 case wxRegKey::Type_Dword
: wxPrintf(wxT("DWORD")); break;
2118 case wxRegKey::Type_Multi_String
: wxPrintf(wxT("MULTI_SZ")); break;
2119 default: wxPrintf(wxT("other (unknown)")); break;
2122 wxPrintf(wxT(", value = "));
2123 if ( key
.IsNumericValue(value
) )
2126 key
.QueryValue(value
, &val
);
2127 wxPrintf(wxT("%ld"), val
);
2132 key
.QueryValue(value
, val
);
2133 wxPrintf(wxT("'%s'"), val
.c_str());
2135 key
.QueryRawValue(value
, val
);
2136 wxPrintf(wxT(" (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(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2157 key
= wxT("ddxf_auto_file") ;
2158 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2160 key
= wxT("ddxf_auto_file") ;
2161 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2163 key
= wxT("program,0") ;
2164 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2166 key
= wxT("program \"%1\"") ;
2168 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2170 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2172 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2174 key
.SetName(wxT("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(wxT("*** Testing wxSocketServer ***\n"));
2230 static const int PORT
= 3000;
2235 wxSocketServer
*server
= new wxSocketServer(addr
);
2236 if ( !server
->Ok() )
2238 wxPuts(wxT("ERROR: failed to bind"));
2246 wxPrintf(wxT("Server: waiting for connection on port %d...\n"), PORT
);
2248 wxSocketBase
*socket
= server
->Accept();
2251 wxPuts(wxT("ERROR: wxSocketServer::Accept() failed."));
2255 wxPuts(wxT("Server: got a client."));
2257 server
->SetTimeout(60); // 1 min
2260 while ( !close
&& socket
->IsConnected() )
2263 wxChar ch
= wxT('\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(wxT("ERROR: in wxSocket::Read."));
2291 wxPrintf(wxT("Server: got '%s'.\n"), s
.c_str());
2292 if ( s
== wxT("close") )
2294 wxPuts(wxT("Closing connection"));
2298 else if ( s
== wxT("quit") )
2303 wxPuts(wxT("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(wxT("Server: wrote '%s'.\n"), s
.c_str());
2315 wxPuts(wxT("Server: lost a client unexpectedly."));
2321 // same as "delete server" but is consistent with GUI programs
2325 static void TestSocketClient()
2327 wxPuts(wxT("*** Testing wxSocketClient ***\n"));
2329 static const wxChar
*hostname
= wxT("www.wxwidgets.org");
2332 addr
.Hostname(hostname
);
2335 wxPrintf(wxT("--- Attempting to connect to %s:80...\n"), hostname
);
2337 wxSocketClient client
;
2338 if ( !client
.Connect(addr
) )
2340 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
2344 wxPrintf(wxT("--- Connected to %s:%u...\n"),
2345 addr
.Hostname().c_str(), addr
.Service());
2349 // could use simply "GET" here I suppose
2351 wxString::Format(wxT("GET http://%s/\r\n"), hostname
);
2352 client
.Write(cmdGet
, cmdGet
.length());
2353 wxPrintf(wxT("--- Sent command '%s' to the server\n"),
2354 MakePrintable(cmdGet
).c_str());
2355 client
.Read(buf
, WXSIZEOF(buf
));
2356 wxPrintf(wxT("--- 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
= wxT("/pub");
2377 static const wxChar
*filename
= wxT("welcome.msg");
2379 static const wxChar
*directory
= wxT("/etc");
2380 static const wxChar
*filename
= wxT("issue");
2383 static bool TestFtpConnect()
2385 wxPuts(wxT("*** Testing FTP connect ***"));
2387 #ifdef FTP_ANONYMOUS
2388 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
2390 wxPrintf(wxT("--- 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(wxT("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(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2406 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2408 if ( !ftp
->Connect(hostname
) )
2410 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
2416 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
2417 hostname
, ftp
->Pwd().c_str());
2424 static void TestFtpList()
2426 wxPuts(wxT("*** Testing wxFTP file listing ***\n"));
2429 if ( !ftp
->ChDir(directory
) )
2431 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory
);
2434 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2436 // test NLIST and LIST
2437 wxArrayString files
;
2438 if ( !ftp
->GetFilesList(files
) )
2440 wxPuts(wxT("ERROR: failed to get NLIST of files"));
2444 wxPrintf(wxT("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(wxT("\t%s\n"), files
[n
].c_str());
2450 wxPuts(wxT("End of the file list"));
2453 if ( !ftp
->GetDirList(files
) )
2455 wxPuts(wxT("ERROR: failed to get LIST of files"));
2459 wxPrintf(wxT("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(wxT("\t%s\n"), files
[n
].c_str());
2465 wxPuts(wxT("End of the file list"));
2468 if ( !ftp
->ChDir(wxT("..")) )
2470 wxPuts(wxT("ERROR: failed to cd to .."));
2473 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2476 static void TestFtpDownload()
2478 wxPuts(wxT("*** Testing wxFTP download ***\n"));
2481 wxInputStream
*in
= ftp
->GetInputStream(filename
);
2484 wxPrintf(wxT("ERROR: couldn't get input stream for %s\n"), filename
);
2488 size_t size
= in
->GetSize();
2489 wxPrintf(wxT("Reading file %s (%u bytes)..."), filename
, size
);
2492 wxChar
*data
= new wxChar
[size
];
2493 if ( !in
->Read(data
, size
) )
2495 wxPuts(wxT("ERROR: read error"));
2499 wxPrintf(wxT("\nContents of %s:\n%s\n"), filename
, data
);
2507 static void TestFtpFileSize()
2509 wxPuts(wxT("*** Testing FTP SIZE command ***"));
2511 if ( !ftp
->ChDir(directory
) )
2513 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory
);
2516 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2518 if ( ftp
->FileExists(filename
) )
2520 int size
= ftp
->GetFileSize(filename
);
2522 wxPrintf(wxT("ERROR: couldn't get size of '%s'\n"), filename
);
2524 wxPrintf(wxT("Size of '%s' is %d bytes.\n"), filename
, size
);
2528 wxPrintf(wxT("ERROR: '%s' doesn't exist\n"), filename
);
2532 static void TestFtpMisc()
2534 wxPuts(wxT("*** Testing miscellaneous wxFTP functions ***"));
2536 if ( ftp
->SendCommand(wxT("STAT")) != '2' )
2538 wxPuts(wxT("ERROR: STAT failed"));
2542 wxPrintf(wxT("STAT returned:\n\n%s\n"), ftp
->GetLastResult().c_str());
2545 if ( ftp
->SendCommand(wxT("HELP SITE")) != '2' )
2547 wxPuts(wxT("ERROR: HELP SITE failed"));
2551 wxPrintf(wxT("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(wxT("\n*** Interactive wxFTP test ***"));
2566 wxPrintf(wxT("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
== wxT("LIST") || start
== wxT("NLST") )
2579 if ( wxStrlen(buf
) > 4 )
2582 wxArrayString files
;
2583 if ( !ftp
->GetList(files
, wildcard
, start
== wxT("LIST")) )
2585 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
2589 wxPrintf(wxT("--- %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(wxT("\t%s\n"), files
[n
].c_str());
2596 wxPuts(wxT("--- End of the file list"));
2601 wxChar ch
= ftp
->SendCommand(buf
);
2602 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
2605 wxPrintf(wxT(" (return code %c)"), ch
);
2608 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
->GetLastResult().c_str());
2612 wxPuts(wxT("\n*** done ***"));
2615 #endif // TEST_INTERACTIVE
2617 static void TestFtpUpload()
2619 wxPuts(wxT("*** Testing wxFTP uploading ***\n"));
2622 static const wxChar
*file1
= wxT("test1");
2623 static const wxChar
*file2
= wxT("test2");
2624 wxOutputStream
*out
= ftp
->GetOutputStream(file1
);
2627 wxPrintf(wxT("--- 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(wxT("STAT ")) + file1
) != '2' )
2635 wxPrintf(wxT("ERROR: STAT %s failed\n"), file1
);
2639 wxPrintf(wxT("STAT %s returned:\n\n%s\n"),
2640 file1
, ftp
->GetLastResult().c_str());
2643 out
= ftp
->GetOutputStream(file2
);
2646 wxPrintf(wxT("--- 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(wxT("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(wxT("*** 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(wxT("*** Testing wxStandardPaths ***\n"));
2738 wxTheApp
->SetAppName(wxT("console"));
2740 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2741 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2742 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2743 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2744 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2745 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2746 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2747 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2748 wxPrintf(wxT("Executable path:\t%s\n"), stdp
.GetExecutablePath().c_str());
2749 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2750 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2751 wxPrintf(wxT("Localized res. dir:\t%s\n"),
2752 stdp
.GetLocalizedResourcesDir(wxT("fr")).c_str());
2753 wxPrintf(wxT("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(wxT("*** Testing wxFileInputStream ***"));
2776 static const wxString filename
= wxT("testdata.fs");
2778 wxFileOutputStream
fsOut(filename
);
2779 fsOut
.Write("foo", 3);
2783 wxFileInputStream
fsIn(filename
);
2784 wxPrintf(wxT("File stream size: %u\n"), fsIn
.GetSize());
2786 while ( (c
=fsIn
.GetC()) != wxEOF
)
2792 if ( !wxRemoveFile(filename
) )
2794 wxPrintf(wxT("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2797 wxPuts(wxT("\n*** wxFileInputStream test done ***"));
2800 static void TestMemoryStream()
2802 wxPuts(wxT("*** Testing wxMemoryOutputStream ***"));
2804 wxMemoryOutputStream memOutStream
;
2805 wxPrintf(wxT("Initially out stream offset: %lu\n"),
2806 (unsigned long)memOutStream
.TellO());
2808 for ( const wxChar
*p
= wxT("Hello, stream!"); *p
; p
++ )
2810 memOutStream
.PutC(*p
);
2813 wxPrintf(wxT("Final out stream offset: %lu\n"),
2814 (unsigned long)memOutStream
.TellO());
2816 wxPuts(wxT("*** Testing wxMemoryInputStream ***"));
2819 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2821 wxMemoryInputStream
memInpStream(buf
, len
);
2822 wxPrintf(wxT("Memory stream size: %u\n"), memInpStream
.GetSize());
2824 while ( (c
=memInpStream
.GetC()) != wxEOF
)
2829 wxPuts(wxT("\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(wxT("*** Testing wxStopWatch ***\n"));
2849 wxPrintf(wxT("Initially paused, after 2 seconds time is..."));
2852 wxPrintf(wxT("\t%ldms\n"), sw
.Time());
2854 wxPrintf(wxT("Resuming stopwatch and sleeping 3 seconds..."));
2858 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2861 wxPrintf(wxT("Pausing agan and sleeping 2 more seconds..."));
2864 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2867 wxPrintf(wxT("Finally resuming and sleeping 2 more seconds..."));
2870 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2873 wxPuts(wxT("\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(wxT("\ntime is negative - ERROR!"));
2890 wxPuts(wxT(", ok."));
2893 #include "wx/timer.h"
2894 #include "wx/evtloop.h"
2898 wxPuts(wxT("*** Testing wxTimer ***\n"));
2900 class MyTimer
: public wxTimer
2903 MyTimer() : wxTimer() { m_num
= 0; }
2905 virtual void Notify()
2907 wxPrintf(wxT("%d"), m_num
++);
2912 wxPrintf(wxT("... exiting the event loop"));
2915 wxEventLoop::GetActive()->Exit(0);
2916 wxPuts(wxT(", 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 wxT("network volume"),
2960 wxT("other volume"),
2963 static void TestFSVolume()
2965 wxPuts(wxT("*** Testing wxFSVolume class ***"));
2967 wxArrayString volumes
= wxFSVolume::GetVolumes();
2968 size_t count
= volumes
.GetCount();
2972 wxPuts(wxT("ERROR: no mounted volumes?"));
2976 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
2978 for ( size_t n
= 0; n
< count
; n
++ )
2980 wxFSVolume
vol(volumes
[n
]);
2983 wxPuts(wxT("ERROR: couldn't create volume"));
2987 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
2989 vol
.GetDisplayName().c_str(),
2990 vol
.GetName().c_str(),
2991 volumeKinds
[vol
.GetKind()],
2992 vol
.IsWritable() ? wxT("rw") : wxT("ro"),
2993 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? wxT("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
), wxT("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3049 { utf8koi8r
, WXSIZEOF(utf8koi8r
), wxT("koi8-r"), wxFONTENCODING_KOI8
},
3050 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), wxT("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3053 static void TestUtf8()
3055 wxPuts(wxT("*** 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(wxT("ERROR: UTF-8 decoding failed."));
3070 wxCSConv
conv(u8d
.charset
);
3071 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3073 wxPrintf(wxT("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3077 wxPrintf(wxT("String in %s: %s\n"), u8d
.charset
, buf
);
3081 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3083 s
= wxT("<< conversion failed >>");
3084 wxPrintf(wxT("String in current cset: %s\n"), s
.c_str());
3088 wxPuts(wxEmptyString
);
3091 static void TestEncodingConverter()
3093 wxPuts(wxT("*** 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(wxT("ERROR: UTF-8 decoding failed."));
3105 wxEncodingConverter ec
;
3106 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3107 ec
.Convert(wbuf
, buf
);
3108 wxPrintf(wxT("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
= wxT("testdata.zip");
3128 static void TestZipStreamRead()
3130 wxPuts(wxT("*** Testing ZIP reading ***\n"));
3132 static const wxString filename
= wxT("foo");
3133 wxFFileInputStream
in(TESTFILE_ZIP
);
3134 wxZipInputStream
istr(in
);
3135 wxZipEntry
entry(filename
);
3136 istr
.OpenEntry(entry
);
3138 wxPrintf(wxT("Archive size: %u\n"), istr
.GetSize());
3140 wxPrintf(wxT("Dumping the file '%s':\n"), filename
.c_str());
3142 while ( (c
=istr
.GetC()) != wxEOF
)
3148 wxPuts(wxT("\n----- done ------"));
3151 static void DumpZipDirectory(wxFileSystem
& fs
,
3152 const wxString
& dir
,
3153 const wxString
& indent
)
3155 wxString prefix
= wxString::Format(wxT("%s#zip:%s"),
3156 TESTFILE_ZIP
, dir
.c_str());
3157 wxString wildcard
= prefix
+ wxT("/*");
3159 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3160 while ( !dirname
.empty() )
3162 if ( !dirname
.StartsWith(prefix
+ wxT('/'), &dirname
) )
3164 wxPrintf(wxT("ERROR: unexpected wxFileSystem::FindNext result\n"));
3169 wxPrintf(wxT("%s%s\n"), indent
.c_str(), dirname
.c_str());
3171 DumpZipDirectory(fs
, dirname
,
3172 indent
+ wxString(wxT(' '), 4));
3174 dirname
= fs
.FindNext();
3177 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3178 while ( !filename
.empty() )
3180 if ( !filename
.StartsWith(prefix
, &filename
) )
3182 wxPrintf(wxT("ERROR: unexpected wxFileSystem::FindNext result\n"));
3187 wxPrintf(wxT("%s%s\n"), indent
.c_str(), filename
.c_str());
3189 filename
= fs
.FindNext();
3193 static void TestZipFileSystem()
3195 wxPuts(wxT("*** Testing ZIP file system ***\n"));
3197 wxFileSystem::AddHandler(new wxZipFSHandler
);
3199 wxPrintf(wxT("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3201 DumpZipDirectory(fs
, wxT(""), wxString(wxT(' '), 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(wxT("\n*** wxDateTime static methods test ***"));
3223 // some info about the current date
3224 int year
= wxDateTime::GetCurrentYear();
3225 wxPrintf(wxT("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(wxT("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(wxT("\n*** wxDateTime timezone test ***"));
3242 wxDateTime now
= wxDateTime::Now();
3244 wxPrintf(wxT("Current GMT time:\t%s\n"), now
.Format(wxT("%c"), wxDateTime::GMT0
).c_str());
3245 wxPrintf(wxT("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(wxT("%c"), wxDateTime::GMT0
).c_str());
3246 wxPrintf(wxT("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(wxT("%c"), wxDateTime::EST
).c_str());
3247 wxPrintf(wxT("Current time in Paris:\t%s\n"), now
.Format(wxT("%c"), wxDateTime::CET
).c_str());
3248 wxPrintf(wxT(" Moscow:\t%s\n"), now
.Format(wxT("%c"), wxDateTime::MSK
).c_str());
3249 wxPrintf(wxT(" New York:\t%s\n"), now
.Format(wxT("%c"), wxDateTime::EST
).c_str());
3251 wxPrintf(wxT("%s\n"), wxDateTime::Now().Format(wxT("Our timezone is %Z")).c_str());
3253 wxDateTime::Tm tm
= now
.GetTm();
3254 if ( wxDateTime(tm
) != now
)
3256 wxPrintf(wxT("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(wxT("\n*** wxDateTime out-of-standard-range dates test ***"));
3266 static const wxChar
*fmt
= wxT("%d-%b-%Y %H:%M:%S");
3268 wxPrintf(wxT("Unix epoch:\t%s\n"),
3269 wxDateTime(2440587.5).Format(fmt
).c_str());
3270 wxPrintf(wxT("Feb 29, 0: \t%s\n"),
3271 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3272 wxPrintf(wxT("JDN 0: \t%s\n"),
3273 wxDateTime(0.0).Format(fmt
).c_str());
3274 wxPrintf(wxT("Jan 1, 1AD:\t%s\n"),
3275 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3276 wxPrintf(wxT("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(wxT("\n*** wxDateTime DST test ***"));
3285 wxPrintf(wxT("DST is%s in effect now.\n\n"),
3286 wxDateTime::Now().IsDST() ? wxEmptyString
: wxT(" not"));
3288 for ( int year
= 1990; year
< 2005; year
++ )
3290 wxPrintf(wxT("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(wxT("\n*** interactive wxDateTime tests ***"));
3309 wxPrintf(wxT("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(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
3326 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
3329 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
3330 dt
.Format(wxT("%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(wxT("\n*** done ***"));
3340 #endif // TEST_INTERACTIVE
3344 static void TestTimeMS()
3346 wxPuts(wxT("*** testing millisecond-resolution support in wxDateTime ***"));
3348 wxDateTime dt1
= wxDateTime::Now(),
3349 dt2
= wxDateTime::UNow();
3351 wxPrintf(wxT("Now = %s\n"), dt1
.Format(wxT("%H:%M:%S:%l")).c_str());
3352 wxPrintf(wxT("UNow = %s\n"), dt2
.Format(wxT("%H:%M:%S:%l")).c_str());
3353 wxPrintf(wxT("Dummy loop: "));
3354 for ( int i
= 0; i
< 6000; i
++ )
3356 //for ( int j = 0; j < 10; j++ )
3359 s
.Printf(wxT("%g"), sqrt((float)i
));
3365 wxPuts(wxT(", done"));
3368 dt2
= wxDateTime::UNow();
3369 wxPrintf(wxT("UNow = %s\n"), dt2
.Format(wxT("%H:%M:%S:%l")).c_str());
3371 wxPrintf(wxT("Loop executed in %s ms\n"), (dt2
- dt1
).Format(wxT("%l")).c_str());
3373 wxPuts(wxT("\n*** done ***"));
3376 static void TestTimeHolidays()
3378 wxPuts(wxT("\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
= wxT("%d-%b-%Y (%a)");
3389 wxPrintf(wxT("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(wxT("\t%s\n"), hol
[n
].Format(format
).c_str());
3398 wxPuts(wxEmptyString
);
3401 static void TestTimeZoneBug()
3403 wxPuts(wxT("\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(wxT("Date %s: week day %s.\n"),
3409 date
.Format(wxT("%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(wxT("\n*** wxTimeSpan tests ***"));
3422 static const wxChar
*formats
[] =
3424 wxT("(default) %H:%M:%S"),
3425 wxT("%E weeks and %D days"),
3426 wxT("%l milliseconds"),
3427 wxT("(with ms) %H:%M:%S:%l"),
3428 wxT("100%% of minutes is %M"), // test "%%"
3429 wxT("%D days and %H hours"),
3430 wxT("or also %S seconds"),
3433 wxTimeSpan
ts1(1, 2, 3, 4),
3435 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3437 wxPrintf(wxT("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(wxT("\n*** wxTextInputStream test ***"));
3462 wxString filename
= wxT("testdata.fc");
3463 wxFileInputStream
fsIn(filename
);
3466 wxPuts(wxT("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(wxT("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(wxT("thread"), wxT("Thread %ld is in OnExit"), GetId());
3585 wxCriticalSectionLocker
lock(gs_critsect
);
3586 if ( !--gs_counter
&& !m_cancelled
)
3590 static void TestDetachedThreads()
3592 wxPuts(wxT("\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(wxT("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3620 // calc 10! in the background
3621 MyJoinableThread
thread(10);
3624 wxPrintf(wxT("\nThread terminated with exit code %lu.\n"),
3625 (unsigned long)thread
.Wait());
3628 static void TestThreadSuspend()
3630 wxPuts(wxT("\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(wxT("\nThread suspended"));
3651 // don't sleep but resume immediately the first time
3652 wxThread::Sleep(300);
3654 wxPuts(wxT("Going to resume the thread"));
3659 wxPuts(wxT("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(wxT("\n*** Testing thread delete function ***"));
3676 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3680 wxPuts(wxT("\nDeleted a thread which didn't start to run yet."));
3682 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3686 wxThread::Sleep(300);
3690 wxPuts(wxT("\nDeleted a running thread."));
3692 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3696 wxThread::Sleep(300);
3702 wxPuts(wxT("\nDeleted a sleeping thread."));
3704 MyJoinableThread
thread3(20);
3709 wxPuts(wxT("\nDeleted a joinable thread."));
3711 MyJoinableThread
thread4(2);
3714 wxThread::Sleep(300);
3718 wxPuts(wxT("\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(wxT("Thread %lu has started running.\n"), GetId());
3741 wxPrintf(wxT("Thread %lu starts to wait...\n"), GetId());
3745 m_condition
->Wait();
3748 wxPrintf(wxT("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(wxT("thread"), wxT("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(wxT("Main thread is waiting for the other threads to start"));
3787 size_t nRunning
= 0;
3788 while ( nRunning
< WXSIZEOF(threads
) )
3794 wxPrintf(wxT("Main thread: %u already running\n"), nRunning
);
3798 wxPuts(wxT("Main thread: all threads started up."));
3801 wxThread::Sleep(500);
3804 // now wake one of them up
3805 wxPrintf(wxT("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(wxT("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(wxT("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
3838 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3842 wxPrintf(wxT("%s: Thread #%d (%ld) acquired the semaphore.\n"),
3843 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3847 wxPrintf(wxT("%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(wxT("*** 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(wxT(".wxconsole.lock")) )
3928 if ( checker
.IsAnotherRunning() )
3930 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
3935 // wait some time to give time to launch another instance
3936 wxPrintf(wxT("Press \"Enter\" to continue..."));
3939 else // failed to create
3941 wxPrintf(wxT("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(wxT("project_name"), wxT(""), wxT("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(wxT("Help was given, terminating."));
3984 ShowCmdLine(parser
);
3988 wxLogMessage(wxT("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(wxT("*** Testing wxLog ***"));
4024 for ( size_t n
= 0; n
< 8000; n
++ )
4026 s
<< (wxChar
)(wxT('A') + (n
% 26));
4029 wxLogWarning(wxT("The length of the string is %lu"),
4030 (unsigned long)s
.length());
4033 msg
.Printf(wxT("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(wxT("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(wxT("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(wxT("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(wxT("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