1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
24 #include "wx/string.h"
29 // without this pragma, the stupid compiler precompiles #defines below so that
30 // changing them doesn't "take place" later!
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
40 A note about all these conditional compilation macros: this file is used
41 both as a test suite for various non-GUI wxWidgets classes and as a
42 scratchpad for quick tests. So there are two compilation modes: if you
43 define TEST_ALL all tests are run, otherwise you may enable the individual
44 tests individually in the "#else" branch below.
47 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
48 // test, define it to 1 to do all tests.
63 // #define TEST_FTP --FIXME! (RN)
64 #define TEST_INFO_FUNCTIONS
74 #define TEST_SCOPEGUARD
76 // #define TEST_SOCKETS --FIXME! (RN)
77 #define TEST_STACKWALKER
80 #define TEST_TEXTSTREAM
83 // #define TEST_VCARD -- don't enable this (VZ)
84 // #define TEST_VOLUME --FIXME! (RN)
91 // some tests are interactive, define this to run them
92 #ifdef TEST_INTERACTIVE
93 #undef TEST_INTERACTIVE
95 #define TEST_INTERACTIVE 1
97 #define TEST_INTERACTIVE 0
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 #if defined(TEST_SOCKETS)
110 // replace TABs with \t and CRs with \n
111 static wxString
MakePrintable(const wxChar
*s
)
114 (void)str
.Replace(_T("\t"), _T("\\t"));
115 (void)str
.Replace(_T("\n"), _T("\\n"));
116 (void)str
.Replace(_T("\r"), _T("\\r"));
121 #endif // MakePrintable() is used
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
129 #include "wx/cmdline.h"
130 #include "wx/datetime.h"
132 #if wxUSE_CMDLINE_PARSER
134 static void ShowCmdLine(const wxCmdLineParser
& parser
)
136 wxString s
= _T("Input files: ");
138 size_t count
= parser
.GetParamCount();
139 for ( size_t param
= 0; param
< count
; param
++ )
141 s
<< parser
.GetParam(param
) << ' ';
145 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
146 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
151 if ( parser
.Found(_T("o"), &strVal
) )
152 s
<< _T("Output file:\t") << strVal
<< '\n';
153 if ( parser
.Found(_T("i"), &strVal
) )
154 s
<< _T("Input dir:\t") << strVal
<< '\n';
155 if ( parser
.Found(_T("s"), &lVal
) )
156 s
<< _T("Size:\t") << lVal
<< '\n';
157 if ( parser
.Found(_T("d"), &dt
) )
158 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
159 if ( parser
.Found(_T("project_name"), &strVal
) )
160 s
<< _T("Project:\t") << strVal
<< '\n';
165 #endif // wxUSE_CMDLINE_PARSER
167 static void TestCmdLineConvert()
169 static const wxChar
*cmdlines
[] =
172 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
173 _T("literal \\\" and \"\""),
176 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
178 const wxChar
*cmdline
= cmdlines
[n
];
179 wxPrintf(_T("Parsing: %s\n"), cmdline
);
180 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
182 size_t count
= args
.GetCount();
183 wxPrintf(_T("\targc = %u\n"), count
);
184 for ( size_t arg
= 0; arg
< count
; arg
++ )
186 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
191 #endif // TEST_CMDLINE
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
202 static const wxChar
*ROOTDIR
= _T("/");
203 static const wxChar
*TESTDIR
= _T("/usr/local/share");
204 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
205 static const wxChar
*ROOTDIR
= _T("c:\\");
206 static const wxChar
*TESTDIR
= _T("d:\\");
208 #error "don't know where the root directory is"
211 static void TestDirEnumHelper(wxDir
& dir
,
212 int flags
= wxDIR_DEFAULT
,
213 const wxString
& filespec
= wxEmptyString
)
217 if ( !dir
.IsOpened() )
220 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
223 wxPrintf(_T("\t%s\n"), filename
.c_str());
225 cont
= dir
.GetNext(&filename
);
228 wxPuts(wxEmptyString
);
233 static void TestDirEnum()
235 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
237 wxString cwd
= wxGetCwd();
238 if ( !wxDir::Exists(cwd
) )
240 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
245 if ( !dir
.IsOpened() )
247 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
251 wxPuts(_T("Enumerating everything in current directory:"));
252 TestDirEnumHelper(dir
);
254 wxPuts(_T("Enumerating really everything in current directory:"));
255 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
257 wxPuts(_T("Enumerating object files in current directory:"));
258 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
260 wxPuts(_T("Enumerating directories in current directory:"));
261 TestDirEnumHelper(dir
, wxDIR_DIRS
);
263 wxPuts(_T("Enumerating files in current directory:"));
264 TestDirEnumHelper(dir
, wxDIR_FILES
);
266 wxPuts(_T("Enumerating files including hidden in current directory:"));
267 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
271 wxPuts(_T("Enumerating everything in root directory:"));
272 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
274 wxPuts(_T("Enumerating directories in root directory:"));
275 TestDirEnumHelper(dir
, wxDIR_DIRS
);
277 wxPuts(_T("Enumerating files in root directory:"));
278 TestDirEnumHelper(dir
, wxDIR_FILES
);
280 wxPuts(_T("Enumerating files including hidden in root directory:"));
281 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
283 wxPuts(_T("Enumerating files in non existing directory:"));
284 wxDir
dirNo(_T("nosuchdir"));
285 TestDirEnumHelper(dirNo
);
290 class DirPrintTraverser
: public wxDirTraverser
293 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
295 return wxDIR_CONTINUE
;
298 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
300 wxString path
, name
, ext
;
301 wxSplitPath(dirname
, &path
, &name
, &ext
);
304 name
<< _T('.') << ext
;
307 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
309 if ( wxIsPathSeparator(*p
) )
313 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
315 return wxDIR_CONTINUE
;
319 static void TestDirTraverse()
321 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
325 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
326 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
329 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
330 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
333 // enum again with custom traverser
334 wxPuts(_T("Now enumerating directories:"));
336 DirPrintTraverser traverser
;
337 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
342 static void TestDirExists()
344 wxPuts(_T("*** Testing wxDir::Exists() ***"));
346 static const wxChar
*dirnames
[] =
349 #if defined(__WXMSW__)
352 _T("\\\\share\\file"),
356 _T("c:\\autoexec.bat"),
357 #elif defined(__UNIX__)
366 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
368 wxPrintf(_T("%-40s: %s\n"),
370 wxDir::Exists(dirnames
[n
]) ? _T("exists")
371 : _T("doesn't exist"));
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
385 #include "wx/dynlib.h"
387 static void TestDllLoad()
389 #if defined(__WXMSW__)
390 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
391 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
392 #elif defined(__UNIX__)
393 // weird: using just libc.so does *not* work!
394 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
395 static const wxChar
*FUNC_NAME
= _T("strlen");
397 #error "don't know how to test wxDllLoader on this platform"
400 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
402 wxDynamicLibrary
lib(LIB_NAME
);
403 if ( !lib
.IsLoaded() )
405 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
409 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
410 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
413 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
414 FUNC_NAME
, LIB_NAME
);
418 wxPrintf(_T("Calling %s dynamically loaded from %s "),
419 FUNC_NAME
, LIB_NAME
);
421 if ( pfnStrlen("foo") != 3 )
423 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
427 wxPuts(_T("... ok"));
432 static const wxChar
*FUNC_NAME_AW
= _T("lstrlen");
434 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
436 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
437 if ( !pfnStrlenAorW
)
439 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
440 FUNC_NAME_AW
, LIB_NAME
);
444 if ( pfnStrlenAorW(_T("foobar")) != 6 )
446 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
453 #if defined(__WXMSW__) || defined(__UNIX__)
455 static void TestDllListLoaded()
457 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
459 puts("\nLoaded modules:");
460 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
461 const size_t count
= dlls
.GetCount();
462 for ( size_t n
= 0; n
< count
; ++n
)
464 const wxDynamicLibraryDetails
& details
= dlls
[n
];
465 printf("%-45s", details
.GetPath().mb_str());
469 if ( details
.GetAddress(&addr
, &len
) )
471 printf(" %08lx:%08lx",
472 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
475 printf(" %s\n", details
.GetVersion().mb_str());
481 #endif // TEST_DYNLIB
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
489 #include "wx/utils.h"
491 static wxString
MyGetEnv(const wxString
& var
)
494 if ( !wxGetEnv(var
, &val
) )
497 val
= wxString(_T('\'')) + val
+ _T('\'');
502 static void TestEnvironment()
504 const wxChar
*var
= _T("wxTestVar");
506 wxPuts(_T("*** testing environment access functions ***"));
508 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
509 wxSetEnv(var
, _T("value for wxTestVar"));
510 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
511 wxSetEnv(var
, _T("another value"));
512 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
514 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
515 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
518 #endif // TEST_ENVIRON
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
526 #include "wx/utils.h"
528 static void TestExecute()
530 wxPuts(_T("*** testing wxExecute ***"));
533 #define COMMAND "cat -n ../../Makefile" // "echo hi"
534 #define SHELL_COMMAND "echo hi from shell"
535 #define REDIRECT_COMMAND COMMAND // "date"
536 #elif defined(__WXMSW__)
537 #define COMMAND "command.com /c echo hi"
538 #define SHELL_COMMAND "echo hi"
539 #define REDIRECT_COMMAND COMMAND
541 #error "no command to exec"
544 wxPrintf(_T("Testing wxShell: "));
546 if ( wxShell(_T(SHELL_COMMAND
)) )
549 wxPuts(_T("ERROR."));
551 wxPrintf(_T("Testing wxExecute: "));
553 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
556 wxPuts(_T("ERROR."));
558 #if 0 // no, it doesn't work (yet?)
559 wxPrintf(_T("Testing async wxExecute: "));
561 if ( wxExecute(COMMAND
) != 0 )
562 wxPuts(_T("Ok (command launched)."));
564 wxPuts(_T("ERROR."));
567 wxPrintf(_T("Testing wxExecute with redirection:\n"));
568 wxArrayString output
;
569 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
571 wxPuts(_T("ERROR."));
575 size_t count
= output
.GetCount();
576 for ( size_t n
= 0; n
< count
; n
++ )
578 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
585 #endif // TEST_EXECUTE
587 // ----------------------------------------------------------------------------
589 // ----------------------------------------------------------------------------
594 #include "wx/ffile.h"
595 #include "wx/textfile.h"
597 static void TestFileRead()
599 wxPuts(_T("*** wxFile read test ***"));
601 wxFile
file(_T("testdata.fc"));
602 if ( file
.IsOpened() )
604 wxPrintf(_T("File length: %lu\n"), file
.Length());
606 wxPuts(_T("File dump:\n----------"));
608 static const size_t len
= 1024;
612 size_t nRead
= file
.Read(buf
, len
);
613 if ( nRead
== (size_t)wxInvalidOffset
)
615 wxPrintf(_T("Failed to read the file."));
619 fwrite(buf
, nRead
, 1, stdout
);
625 wxPuts(_T("----------"));
629 wxPrintf(_T("ERROR: can't open test file.\n"));
632 wxPuts(wxEmptyString
);
635 static void TestTextFileRead()
637 wxPuts(_T("*** wxTextFile read test ***"));
639 wxTextFile
file(_T("testdata.fc"));
642 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
643 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
647 wxPuts(_T("\nDumping the entire file:"));
648 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
650 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
652 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
654 wxPuts(_T("\nAnd now backwards:"));
655 for ( s
= file
.GetLastLine();
656 file
.GetCurrentLine() != 0;
657 s
= file
.GetPrevLine() )
659 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
661 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
665 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
668 wxPuts(wxEmptyString
);
671 static void TestFileCopy()
673 wxPuts(_T("*** Testing wxCopyFile ***"));
675 static const wxChar
*filename1
= _T("testdata.fc");
676 static const wxChar
*filename2
= _T("test2");
677 if ( !wxCopyFile(filename1
, filename2
) )
679 wxPuts(_T("ERROR: failed to copy file"));
683 wxFFile
f1(filename1
, _T("rb")),
684 f2(filename2
, _T("rb"));
686 if ( !f1
.IsOpened() || !f2
.IsOpened() )
688 wxPuts(_T("ERROR: failed to open file(s)"));
693 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
695 wxPuts(_T("ERROR: failed to read file(s)"));
699 if ( (s1
.length() != s2
.length()) ||
700 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
702 wxPuts(_T("ERROR: copy error!"));
706 wxPuts(_T("File was copied ok."));
712 if ( !wxRemoveFile(filename2
) )
714 wxPuts(_T("ERROR: failed to remove the file"));
717 wxPuts(wxEmptyString
);
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
728 #include "wx/confbase.h"
729 #include "wx/fileconf.h"
731 static const struct FileConfTestData
733 const wxChar
*name
; // value name
734 const wxChar
*value
; // the value from the file
737 { _T("value1"), _T("one") },
738 { _T("value2"), _T("two") },
739 { _T("novalue"), _T("default") },
742 static void TestFileConfRead()
744 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
746 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
747 _T("testdata.fc"), wxEmptyString
,
748 wxCONFIG_USE_RELATIVE_PATH
);
750 // test simple reading
751 wxPuts(_T("\nReading config file:"));
752 wxString
defValue(_T("default")), value
;
753 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
755 const FileConfTestData
& data
= fcTestData
[n
];
756 value
= fileconf
.Read(data
.name
, defValue
);
757 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
758 if ( value
== data
.value
)
764 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
768 // test enumerating the entries
769 wxPuts(_T("\nEnumerating all root entries:"));
772 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
775 wxPrintf(_T("\t%s = %s\n"),
777 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
779 cont
= fileconf
.GetNextEntry(name
, dummy
);
782 static const wxChar
*testEntry
= _T("TestEntry");
783 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
784 fileconf
.Write(testEntry
, _T("A value"));
785 fileconf
.DeleteEntry(testEntry
);
786 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
789 #endif // TEST_FILECONF
791 // ----------------------------------------------------------------------------
793 // ----------------------------------------------------------------------------
797 #include "wx/filename.h"
800 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
804 wxString full
= fn
.GetFullPath();
806 wxString vol
, path
, name
, ext
;
807 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
809 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
810 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
812 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
813 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
814 path
.c_str(), name
.c_str(), ext
.c_str());
816 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
817 wxPrintf(_T("with volume: \t'%s'\n"),
818 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
819 wxPrintf(_T("with separator:\t'%s'\n"),
820 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
821 wxPrintf(_T("with both: \t'%s'\n"),
822 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
824 wxPuts(_T("The directories in the path are:"));
825 wxArrayString dirs
= fn
.GetDirs();
826 size_t count
= dirs
.GetCount();
827 for ( size_t n
= 0; n
< count
; n
++ )
829 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
834 static void TestFileNameTemp()
836 wxPuts(_T("*** testing wxFileName temp file creation ***"));
838 static const wxChar
*tmpprefixes
[] =
846 _T("/tmp/foo/bar"), // this one must be an error
850 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
852 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
855 // "error" is not in upper case because it may be ok
856 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
860 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
861 tmpprefixes
[n
], path
.c_str());
863 if ( !wxRemoveFile(path
) )
865 wxLogWarning(_T("Failed to remove temp file '%s'"),
872 static void TestFileNameDirManip()
874 // TODO: test AppendDir(), RemoveDir(), ...
877 static void TestFileNameComparison()
882 static void TestFileNameOperations()
887 static void TestFileNameCwd()
892 #endif // TEST_FILENAME
894 // ----------------------------------------------------------------------------
895 // wxFileName time functions
896 // ----------------------------------------------------------------------------
900 #include "wx/filename.h"
901 #include "wx/datetime.h"
903 static void TestFileGetTimes()
905 wxFileName
fn(_T("testdata.fc"));
907 wxDateTime dtAccess
, dtMod
, dtCreate
;
908 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
910 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
914 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
916 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
917 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
918 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
919 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
924 static void TestFileSetTimes()
926 wxFileName
fn(_T("testdata.fc"));
930 wxPrintf(_T("ERROR: Touch() failed.\n"));
935 #endif // TEST_FILETIME
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
944 #include "wx/utils.h" // for wxSetEnv
946 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
948 // find the name of the language from its value
949 static const wxChar
*GetLangName(int lang
)
951 static const wxChar
*languageNames
[] =
961 _T("ARABIC_ALGERIA"),
962 _T("ARABIC_BAHRAIN"),
967 _T("ARABIC_LEBANON"),
969 _T("ARABIC_MOROCCO"),
972 _T("ARABIC_SAUDI_ARABIA"),
975 _T("ARABIC_TUNISIA"),
982 _T("AZERI_CYRILLIC"),
997 _T("CHINESE_SIMPLIFIED"),
998 _T("CHINESE_TRADITIONAL"),
999 _T("CHINESE_HONGKONG"),
1000 _T("CHINESE_MACAU"),
1001 _T("CHINESE_SINGAPORE"),
1002 _T("CHINESE_TAIWAN"),
1008 _T("DUTCH_BELGIAN"),
1012 _T("ENGLISH_AUSTRALIA"),
1013 _T("ENGLISH_BELIZE"),
1014 _T("ENGLISH_BOTSWANA"),
1015 _T("ENGLISH_CANADA"),
1016 _T("ENGLISH_CARIBBEAN"),
1017 _T("ENGLISH_DENMARK"),
1019 _T("ENGLISH_JAMAICA"),
1020 _T("ENGLISH_NEW_ZEALAND"),
1021 _T("ENGLISH_PHILIPPINES"),
1022 _T("ENGLISH_SOUTH_AFRICA"),
1023 _T("ENGLISH_TRINIDAD"),
1024 _T("ENGLISH_ZIMBABWE"),
1032 _T("FRENCH_BELGIAN"),
1033 _T("FRENCH_CANADIAN"),
1034 _T("FRENCH_LUXEMBOURG"),
1035 _T("FRENCH_MONACO"),
1041 _T("GERMAN_AUSTRIAN"),
1042 _T("GERMAN_BELGIUM"),
1043 _T("GERMAN_LIECHTENSTEIN"),
1044 _T("GERMAN_LUXEMBOURG"),
1062 _T("ITALIAN_SWISS"),
1067 _T("KASHMIRI_INDIA"),
1085 _T("MALAY_BRUNEI_DARUSSALAM"),
1086 _T("MALAY_MALAYSIA"),
1096 _T("NORWEGIAN_BOKMAL"),
1097 _T("NORWEGIAN_NYNORSK"),
1104 _T("PORTUGUESE_BRAZILIAN"),
1107 _T("RHAETO_ROMANCE"),
1110 _T("RUSSIAN_UKRAINE"),
1116 _T("SERBIAN_CYRILLIC"),
1117 _T("SERBIAN_LATIN"),
1118 _T("SERBO_CROATIAN"),
1129 _T("SPANISH_ARGENTINA"),
1130 _T("SPANISH_BOLIVIA"),
1131 _T("SPANISH_CHILE"),
1132 _T("SPANISH_COLOMBIA"),
1133 _T("SPANISH_COSTA_RICA"),
1134 _T("SPANISH_DOMINICAN_REPUBLIC"),
1135 _T("SPANISH_ECUADOR"),
1136 _T("SPANISH_EL_SALVADOR"),
1137 _T("SPANISH_GUATEMALA"),
1138 _T("SPANISH_HONDURAS"),
1139 _T("SPANISH_MEXICAN"),
1140 _T("SPANISH_MODERN"),
1141 _T("SPANISH_NICARAGUA"),
1142 _T("SPANISH_PANAMA"),
1143 _T("SPANISH_PARAGUAY"),
1145 _T("SPANISH_PUERTO_RICO"),
1146 _T("SPANISH_URUGUAY"),
1148 _T("SPANISH_VENEZUELA"),
1152 _T("SWEDISH_FINLAND"),
1170 _T("URDU_PAKISTAN"),
1172 _T("UZBEK_CYRILLIC"),
1185 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1186 return languageNames
[lang
];
1188 return _T("INVALID");
1191 static void TestDefaultLang()
1193 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1195 static const wxChar
*langStrings
[] =
1197 NULL
, // system default
1204 _T("de_DE.iso88591"),
1206 _T("?"), // invalid lang spec
1207 _T("klingonese"), // I bet on some systems it does exist...
1210 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1211 wxLocale::GetSystemEncodingName().c_str(),
1212 wxLocale::GetSystemEncoding());
1214 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1216 const wxChar
*langStr
= langStrings
[n
];
1219 // FIXME: this doesn't do anything at all under Windows, we need
1220 // to create a new wxLocale!
1221 wxSetEnv(_T("LC_ALL"), langStr
);
1224 int lang
= gs_localeDefault
.GetSystemLanguage();
1225 wxPrintf(_T("Locale for '%s' is %s.\n"),
1226 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1230 #endif // TEST_LOCALE
1232 // ----------------------------------------------------------------------------
1234 // ----------------------------------------------------------------------------
1238 #include "wx/mimetype.h"
1240 static void TestMimeEnum()
1242 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1244 wxArrayString mimetypes
;
1246 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1248 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1253 for ( size_t n
= 0; n
< count
; n
++ )
1255 wxFileType
*filetype
=
1256 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1259 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1260 mimetypes
[n
].c_str());
1264 filetype
->GetDescription(&desc
);
1265 filetype
->GetExtensions(exts
);
1267 filetype
->GetIcon(NULL
);
1270 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1273 extsAll
<< _T(", ");
1277 wxPrintf(_T("\t%s: %s (%s)\n"),
1278 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1281 wxPuts(wxEmptyString
);
1284 static void TestMimeOverride()
1286 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1288 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1289 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1291 if ( wxFile::Exists(mailcap
) )
1292 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1294 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1296 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1299 if ( wxFile::Exists(mimetypes
) )
1300 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1302 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1304 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1307 wxPuts(wxEmptyString
);
1310 static void TestMimeFilename()
1312 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1314 static const wxChar
*filenames
[] =
1322 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1324 const wxString fname
= filenames
[n
];
1325 wxString ext
= fname
.AfterLast(_T('.'));
1326 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1329 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1334 if ( !ft
->GetDescription(&desc
) )
1335 desc
= _T("<no description>");
1338 if ( !ft
->GetOpenCommand(&cmd
,
1339 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1340 cmd
= _T("<no command available>");
1342 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1344 wxPrintf(_T("To open %s (%s) do %s.\n"),
1345 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1351 wxPuts(wxEmptyString
);
1354 static void TestMimeAssociate()
1356 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1358 wxFileTypeInfo
ftInfo(
1359 _T("application/x-xyz"),
1360 _T("xyzview '%s'"), // open cmd
1361 _T(""), // print cmd
1362 _T("XYZ File"), // description
1363 _T(".xyz"), // extensions
1364 NULL
// end of extensions
1366 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1368 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1371 wxPuts(_T("ERROR: failed to create association!"));
1375 // TODO: read it back
1379 wxPuts(wxEmptyString
);
1384 // ----------------------------------------------------------------------------
1385 // misc information functions
1386 // ----------------------------------------------------------------------------
1388 #ifdef TEST_INFO_FUNCTIONS
1390 #include "wx/utils.h"
1392 static void TestDiskInfo()
1394 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1398 wxChar pathname
[128];
1399 wxPrintf(_T("\nEnter a directory name: "));
1400 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1403 // kill the last '\n'
1404 pathname
[wxStrlen(pathname
) - 1] = 0;
1406 wxLongLong total
, free
;
1407 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1409 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1413 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1414 (total
/ 1024).ToString().c_str(),
1415 (free
/ 1024).ToString().c_str(),
1421 static void TestOsInfo()
1423 wxPuts(_T("*** Testing OS info functions ***\n"));
1426 wxGetOsVersion(&major
, &minor
);
1427 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1428 wxGetOsDescription().c_str(), major
, minor
);
1430 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
1432 wxPrintf(_T("Host name is %s (%s).\n"),
1433 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1435 wxPuts(wxEmptyString
);
1438 static void TestUserInfo()
1440 wxPuts(_T("*** Testing user info functions ***\n"));
1442 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1443 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1444 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1445 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1447 wxPuts(wxEmptyString
);
1450 #endif // TEST_INFO_FUNCTIONS
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 #ifdef TEST_PATHLIST
1459 #define CMD_IN_PATH _T("ls")
1461 #define CMD_IN_PATH _T("command.com")
1464 static void TestPathList()
1466 wxPuts(_T("*** Testing wxPathList ***\n"));
1468 wxPathList pathlist
;
1469 pathlist
.AddEnvList(_T("PATH"));
1470 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1473 wxPrintf(_T("ERROR: command not found in the path.\n"));
1477 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1481 #endif // TEST_PATHLIST
1483 // ----------------------------------------------------------------------------
1484 // regular expressions
1485 // ----------------------------------------------------------------------------
1489 #include "wx/regex.h"
1491 static void TestRegExInteractive()
1493 wxPuts(_T("*** Testing RE interactively ***"));
1497 wxChar pattern
[128];
1498 wxPrintf(_T("\nEnter a pattern: "));
1499 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1502 // kill the last '\n'
1503 pattern
[wxStrlen(pattern
) - 1] = 0;
1506 if ( !re
.Compile(pattern
) )
1514 wxPrintf(_T("Enter text to match: "));
1515 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1518 // kill the last '\n'
1519 text
[wxStrlen(text
) - 1] = 0;
1521 if ( !re
.Matches(text
) )
1523 wxPrintf(_T("No match.\n"));
1527 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1530 for ( size_t n
= 1; ; n
++ )
1532 if ( !re
.GetMatch(&start
, &len
, n
) )
1537 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1538 n
, wxString(text
+ start
, len
).c_str());
1545 #endif // TEST_REGEX
1547 // ----------------------------------------------------------------------------
1549 // ----------------------------------------------------------------------------
1559 static void TestDbOpen()
1567 // ----------------------------------------------------------------------------
1569 // ----------------------------------------------------------------------------
1572 NB: this stuff was taken from the glibc test suite and modified to build
1573 in wxWidgets: if I read the copyright below properly, this shouldn't
1579 #ifdef wxTEST_PRINTF
1580 // use our functions from wxchar.cpp
1584 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1585 // in the tests below
1586 int wxPrintf( const wxChar
*format
, ... );
1587 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1590 #include "wx/longlong.h"
1594 static void rfg1 (void);
1595 static void rfg2 (void);
1599 fmtchk (const wxChar
*fmt
)
1601 (void) wxPrintf(_T("%s:\t`"), fmt
);
1602 (void) wxPrintf(fmt
, 0x12);
1603 (void) wxPrintf(_T("'\n"));
1607 fmtst1chk (const wxChar
*fmt
)
1609 (void) wxPrintf(_T("%s:\t`"), fmt
);
1610 (void) wxPrintf(fmt
, 4, 0x12);
1611 (void) wxPrintf(_T("'\n"));
1615 fmtst2chk (const wxChar
*fmt
)
1617 (void) wxPrintf(_T("%s:\t`"), fmt
);
1618 (void) wxPrintf(fmt
, 4, 4, 0x12);
1619 (void) wxPrintf(_T("'\n"));
1622 /* This page is covered by the following copyright: */
1624 /* (C) Copyright C E Chew
1626 * Feel free to copy, use and distribute this software provided:
1628 * 1. you do not pretend that you wrote it
1629 * 2. you leave this copyright notice intact.
1633 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1640 /* Formatted Output Test
1642 * This exercises the output formatting code.
1645 wxChar
*PointerNull
= NULL
;
1652 wxChar
*prefix
= buf
;
1655 wxPuts(_T("\nFormatted output test"));
1656 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1657 wxStrcpy(prefix
, _T("%"));
1658 for (i
= 0; i
< 2; i
++) {
1659 for (j
= 0; j
< 2; j
++) {
1660 for (k
= 0; k
< 2; k
++) {
1661 for (l
= 0; l
< 2; l
++) {
1662 wxStrcpy(prefix
, _T("%"));
1663 if (i
== 0) wxStrcat(prefix
, _T("-"));
1664 if (j
== 0) wxStrcat(prefix
, _T("+"));
1665 if (k
== 0) wxStrcat(prefix
, _T("#"));
1666 if (l
== 0) wxStrcat(prefix
, _T("0"));
1667 wxPrintf(_T("%5s |"), prefix
);
1668 wxStrcpy(tp
, prefix
);
1669 wxStrcat(tp
, _T("6d |"));
1671 wxStrcpy(tp
, prefix
);
1672 wxStrcat(tp
, _T("6o |"));
1674 wxStrcpy(tp
, prefix
);
1675 wxStrcat(tp
, _T("6x |"));
1677 wxStrcpy(tp
, prefix
);
1678 wxStrcat(tp
, _T("6X |"));
1680 wxStrcpy(tp
, prefix
);
1681 wxStrcat(tp
, _T("6u |"));
1688 wxPrintf(_T("%10s\n"), PointerNull
);
1689 wxPrintf(_T("%-10s\n"), PointerNull
);
1692 static void TestPrintf()
1694 static wxChar shortstr
[] = _T("Hi, Z.");
1695 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1696 I am ready for my first lesson today.");
1698 wxString test_format
;
1702 fmtchk(_T("%4.4x"));
1703 fmtchk(_T("%04.4x"));
1704 fmtchk(_T("%4.3x"));
1705 fmtchk(_T("%04.3x"));
1707 fmtst1chk(_T("%.*x"));
1708 fmtst1chk(_T("%0*x"));
1709 fmtst2chk(_T("%*.*x"));
1710 fmtst2chk(_T("%0*.*x"));
1712 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1713 wxPrintf(bad_format
.c_str());
1714 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1716 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1717 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1718 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1719 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1720 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1721 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1722 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1723 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1724 wxPrintf(test_format
.c_str(), -123456);
1725 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1726 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1728 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1729 wxPrintf(test_format
.c_str(), shortstr
);
1730 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1731 wxPrintf(test_format
.c_str(), shortstr
);
1732 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1733 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1734 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1735 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1737 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1738 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1739 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1740 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1741 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1742 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1743 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1744 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1745 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1746 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1747 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1748 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1750 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1751 wxPrintf (_T(" %6.5f\n"), .1);
1752 wxPrintf (_T("x%5.4fx\n"), .5);
1754 wxPrintf (_T("%#03x\n"), 1);
1756 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1762 while (niter
-- != 0)
1763 wxPrintf (_T("%.17e\n"), d
/ 2);
1768 // Open Watcom cause compiler error here
1769 // Error! E173: col(24) floating-point constant too small to represent
1770 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1773 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1774 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1775 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1776 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1777 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1778 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1779 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1780 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1781 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1782 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1787 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1789 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1790 rc
, WXSIZEOF(buf
), buf
);
1793 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1794 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1800 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1801 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1802 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1803 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1804 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1805 wxPrintf (_T("%g should be 10\n"), 10.0);
1806 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1810 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1816 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1818 result
|= wxStrcmp (buf
,
1819 _T("onetwo three "));
1821 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1828 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1830 // for some reason below line fails under Borland
1831 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1834 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1837 wxPuts (_T("\tFAILED"));
1839 wxUnusedVar(result
);
1840 wxPuts (wxEmptyString
);
1842 #endif // wxLongLong_t
1844 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1845 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1847 wxPuts (_T("--- Should be no further output. ---"));
1856 memset (bytes
, '\xff', sizeof bytes
);
1857 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1858 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1859 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1861 wxPuts (_T("%hhn overwrite more bytes"));
1866 wxPuts (_T("%hhn wrote incorrect value"));
1878 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1879 if (wxStrcmp (buf
, _T(" ")) != 0)
1880 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1881 wxSprintf (buf
, _T("%5.f"), 33.3);
1882 if (wxStrcmp (buf
, _T(" 33")) != 0)
1883 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1884 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1885 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1886 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1887 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1888 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1889 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1890 wxSprintf (buf
, _T("%.g"), 33.3);
1891 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1892 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1893 wxSprintf (buf
, _T("%.G"), 33.3);
1894 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1895 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1903 wxString test_format
;
1906 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
1907 if (wxStrcmp (buf
, _T("3")) != 0)
1908 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1910 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
1911 if (wxStrcmp (buf
, _T("3")) != 0)
1912 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1914 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
1915 if (wxStrcmp (buf
, _T(" 3")) != 0)
1916 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
1918 test_format
= _T("%04.*o");
1919 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1920 if (wxStrcmp (buf
, _T(" 041")) != 0)
1921 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
1923 test_format
= _T("%09.*u");
1924 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1925 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
1926 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
1928 test_format
= _T("%04.*x");
1929 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1930 if (wxStrcmp (buf
, _T(" 021")) != 0)
1931 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1933 test_format
= _T("%04.*X");
1934 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1935 if (wxStrcmp (buf
, _T(" 021")) != 0)
1936 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1939 #endif // TEST_PRINTF
1941 // ----------------------------------------------------------------------------
1942 // registry and related stuff
1943 // ----------------------------------------------------------------------------
1945 // this is for MSW only
1948 #undef TEST_REGISTRY
1953 #include "wx/confbase.h"
1954 #include "wx/msw/regconf.h"
1957 static void TestRegConfWrite()
1959 wxConfig
*config
= new wxConfig(_T("myapp"));
1960 config
->SetPath(_T("/group1"));
1961 config
->Write(_T("entry1"), _T("foo"));
1962 config
->SetPath(_T("/group2"));
1963 config
->Write(_T("entry1"), _T("bar"));
1967 static void TestRegConfRead()
1969 wxConfig
*config
= new wxConfig(_T("myapp"));
1973 config
->SetPath(_T("/"));
1974 wxPuts(_T("Enumerating / subgroups:"));
1975 bool bCont
= config
->GetFirstGroup(str
, dummy
);
1979 bCont
= config
->GetNextGroup(str
, dummy
);
1983 #endif // TEST_REGCONF
1985 #ifdef TEST_REGISTRY
1987 #include "wx/msw/registry.h"
1989 // I chose this one because I liked its name, but it probably only exists under
1991 static const wxChar
*TESTKEY
=
1992 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
1994 static void TestRegistryRead()
1996 wxPuts(_T("*** testing registry reading ***"));
1998 wxRegKey
key(TESTKEY
);
1999 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2002 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2007 size_t nSubKeys
, nValues
;
2008 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2010 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2013 wxPrintf(_T("Enumerating values:\n"));
2017 bool cont
= key
.GetFirstValue(value
, dummy
);
2020 wxPrintf(_T("Value '%s': type "), value
.c_str());
2021 switch ( key
.GetValueType(value
) )
2023 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2024 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2025 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2026 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2027 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2028 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2029 default: wxPrintf(_T("other (unknown)")); break;
2032 wxPrintf(_T(", value = "));
2033 if ( key
.IsNumericValue(value
) )
2036 key
.QueryValue(value
, &val
);
2037 wxPrintf(_T("%ld"), val
);
2042 key
.QueryValue(value
, val
);
2043 wxPrintf(_T("'%s'"), val
.c_str());
2045 key
.QueryRawValue(value
, val
);
2046 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2051 cont
= key
.GetNextValue(value
, dummy
);
2055 static void TestRegistryAssociation()
2058 The second call to deleteself genertaes an error message, with a
2059 messagebox saying .flo is crucial to system operation, while the .ddf
2060 call also fails, but with no error message
2065 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2067 key
= _T("ddxf_auto_file") ;
2068 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2070 key
= _T("ddxf_auto_file") ;
2071 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2073 key
= _T("program,0") ;
2074 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2076 key
= _T("program \"%1\"") ;
2078 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2080 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2082 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2084 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2088 #endif // TEST_REGISTRY
2090 // ----------------------------------------------------------------------------
2092 // ----------------------------------------------------------------------------
2094 #ifdef TEST_SCOPEGUARD
2096 #include "wx/scopeguard.h"
2098 static void function0() { puts("function0()"); }
2099 static void function1(int n
) { printf("function1(%d)\n", n
); }
2100 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2104 void method0() { printf("method0()\n"); }
2105 void method1(int n
) { printf("method1(%d)\n", n
); }
2106 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2109 static void TestScopeGuard()
2111 wxON_BLOCK_EXIT0(function0
);
2112 wxON_BLOCK_EXIT1(function1
, 17);
2113 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2116 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2117 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2118 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2120 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2121 dismissed
.Dismiss();
2126 // ----------------------------------------------------------------------------
2128 // ----------------------------------------------------------------------------
2132 #include "wx/socket.h"
2133 #include "wx/protocol/protocol.h"
2134 #include "wx/protocol/http.h"
2136 static void TestSocketServer()
2138 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2140 static const int PORT
= 3000;
2145 wxSocketServer
*server
= new wxSocketServer(addr
);
2146 if ( !server
->Ok() )
2148 wxPuts(_T("ERROR: failed to bind"));
2156 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2158 wxSocketBase
*socket
= server
->Accept();
2161 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2165 wxPuts(_T("Server: got a client."));
2167 server
->SetTimeout(60); // 1 min
2170 while ( !close
&& socket
->IsConnected() )
2173 wxChar ch
= _T('\0');
2176 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2178 // don't log error if the client just close the connection
2179 if ( socket
->IsConnected() )
2181 wxPuts(_T("ERROR: in wxSocket::Read."));
2201 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2202 if ( s
== _T("close") )
2204 wxPuts(_T("Closing connection"));
2208 else if ( s
== _T("quit") )
2213 wxPuts(_T("Shutting down the server"));
2215 else // not a special command
2217 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2218 socket
->Write("\r\n", 2);
2219 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2225 wxPuts(_T("Server: lost a client unexpectedly."));
2231 // same as "delete server" but is consistent with GUI programs
2235 static void TestSocketClient()
2237 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2239 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2242 addr
.Hostname(hostname
);
2245 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2247 wxSocketClient client
;
2248 if ( !client
.Connect(addr
) )
2250 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2254 wxPrintf(_T("--- Connected to %s:%u...\n"),
2255 addr
.Hostname().c_str(), addr
.Service());
2259 // could use simply "GET" here I suppose
2261 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2262 client
.Write(cmdGet
, cmdGet
.length());
2263 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2264 MakePrintable(cmdGet
).c_str());
2265 client
.Read(buf
, WXSIZEOF(buf
));
2266 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2270 #endif // TEST_SOCKETS
2272 // ----------------------------------------------------------------------------
2274 // ----------------------------------------------------------------------------
2278 #include "wx/protocol/ftp.h"
2282 #define FTP_ANONYMOUS
2284 #ifdef FTP_ANONYMOUS
2285 static const wxChar
*directory
= _T("/pub");
2286 static const wxChar
*filename
= _T("welcome.msg");
2288 static const wxChar
*directory
= _T("/etc");
2289 static const wxChar
*filename
= _T("issue");
2292 static bool TestFtpConnect()
2294 wxPuts(_T("*** Testing FTP connect ***"));
2296 #ifdef FTP_ANONYMOUS
2297 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2299 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2300 #else // !FTP_ANONYMOUS
2301 static const wxChar
*hostname
= "localhost";
2304 wxFgets(user
, WXSIZEOF(user
), stdin
);
2305 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2308 wxChar password
[256];
2309 wxPrintf(_T("Password for %s: "), password
);
2310 wxFgets(password
, WXSIZEOF(password
), stdin
);
2311 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2312 ftp
.SetPassword(password
);
2314 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2315 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2317 if ( !ftp
.Connect(hostname
) )
2319 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2325 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2326 hostname
, ftp
.Pwd().c_str());
2333 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2334 static void TestFtpWuFtpd()
2337 static const wxChar
*hostname
= _T("ftp.eudora.com");
2338 if ( !ftp
.Connect(hostname
) )
2340 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2344 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2345 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2348 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2352 size_t size
= in
->GetSize();
2353 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2355 wxChar
*data
= new wxChar
[size
];
2356 if ( !in
->Read(data
, size
) )
2358 wxPuts(_T("ERROR: read error"));
2362 wxPrintf(_T("Successfully retrieved the file.\n"));
2371 static void TestFtpList()
2373 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2376 if ( !ftp
.ChDir(directory
) )
2378 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2381 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2383 // test NLIST and LIST
2384 wxArrayString files
;
2385 if ( !ftp
.GetFilesList(files
) )
2387 wxPuts(_T("ERROR: failed to get NLIST of files"));
2391 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2392 size_t count
= files
.GetCount();
2393 for ( size_t n
= 0; n
< count
; n
++ )
2395 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2397 wxPuts(_T("End of the file list"));
2400 if ( !ftp
.GetDirList(files
) )
2402 wxPuts(_T("ERROR: failed to get LIST of files"));
2406 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2407 size_t count
= files
.GetCount();
2408 for ( size_t n
= 0; n
< count
; n
++ )
2410 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2412 wxPuts(_T("End of the file list"));
2415 if ( !ftp
.ChDir(_T("..")) )
2417 wxPuts(_T("ERROR: failed to cd to .."));
2420 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2423 static void TestFtpDownload()
2425 wxPuts(_T("*** Testing wxFTP download ***\n"));
2428 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2431 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2435 size_t size
= in
->GetSize();
2436 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2439 wxChar
*data
= new wxChar
[size
];
2440 if ( !in
->Read(data
, size
) )
2442 wxPuts(_T("ERROR: read error"));
2446 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2454 static void TestFtpFileSize()
2456 wxPuts(_T("*** Testing FTP SIZE command ***"));
2458 if ( !ftp
.ChDir(directory
) )
2460 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2463 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2465 if ( ftp
.FileExists(filename
) )
2467 int size
= ftp
.GetFileSize(filename
);
2469 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2471 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2475 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2479 static void TestFtpMisc()
2481 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2483 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2485 wxPuts(_T("ERROR: STAT failed"));
2489 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2492 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2494 wxPuts(_T("ERROR: HELP SITE failed"));
2498 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2499 ftp
.GetLastResult().c_str());
2503 static void TestFtpInteractive()
2505 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2511 wxPrintf(_T("Enter FTP command: "));
2512 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2515 // kill the last '\n'
2516 buf
[wxStrlen(buf
) - 1] = 0;
2518 // special handling of LIST and NLST as they require data connection
2519 wxString
start(buf
, 4);
2521 if ( start
== _T("LIST") || start
== _T("NLST") )
2524 if ( wxStrlen(buf
) > 4 )
2527 wxArrayString files
;
2528 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2530 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2534 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2535 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2536 size_t count
= files
.GetCount();
2537 for ( size_t n
= 0; n
< count
; n
++ )
2539 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2541 wxPuts(_T("--- End of the file list"));
2546 wxChar ch
= ftp
.SendCommand(buf
);
2547 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2550 wxPrintf(_T(" (return code %c)"), ch
);
2553 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2557 wxPuts(_T("\n*** done ***"));
2560 static void TestFtpUpload()
2562 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2565 static const wxChar
*file1
= _T("test1");
2566 static const wxChar
*file2
= _T("test2");
2567 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2570 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2571 out
->Write("First hello", 11);
2575 // send a command to check the remote file
2576 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2578 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2582 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2583 file1
, ftp
.GetLastResult().c_str());
2586 out
= ftp
.GetOutputStream(file2
);
2589 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2590 out
->Write("Second hello", 12);
2597 // ----------------------------------------------------------------------------
2599 // ----------------------------------------------------------------------------
2601 #ifdef TEST_STACKWALKER
2603 #if wxUSE_STACKWALKER
2605 #include "wx/stackwalk.h"
2607 class StackDump
: public wxStackWalker
2610 StackDump(const char *argv0
)
2611 : wxStackWalker(argv0
)
2615 virtual void Walk(size_t skip
= 1)
2617 wxPuts(_T("Stack dump:"));
2619 wxStackWalker::Walk(skip
);
2623 virtual void OnStackFrame(const wxStackFrame
& frame
)
2625 printf("[%2d] ", frame
.GetLevel());
2627 wxString name
= frame
.GetName();
2628 if ( !name
.empty() )
2630 printf("%-20.40s", name
.mb_str());
2634 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2637 if ( frame
.HasSourceLocation() )
2640 frame
.GetFileName().mb_str(),
2647 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2649 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2654 static void TestStackWalk(const char *argv0
)
2656 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2658 StackDump
dump(argv0
);
2662 #endif // wxUSE_STACKWALKER
2664 #endif // TEST_STACKWALKER
2666 // ----------------------------------------------------------------------------
2668 // ----------------------------------------------------------------------------
2670 #ifdef TEST_STDPATHS
2672 #include "wx/stdpaths.h"
2674 static void TestStandardPaths()
2676 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2678 wxTheApp
->SetAppName(_T("console"));
2680 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2681 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2682 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2683 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2684 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2685 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2686 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2687 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2688 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2689 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2690 wxPrintf(_T("Localized res. dir:\t%s\n"),
2691 stdp
.GetLocalizedResourcesDir(_T("fr")).c_str());
2692 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2693 stdp
.GetLocalizedResourcesDir
2696 wxStandardPaths::ResourceCat_Messages
2700 #endif // TEST_STDPATHS
2702 // ----------------------------------------------------------------------------
2704 // ----------------------------------------------------------------------------
2708 #include "wx/wfstream.h"
2709 #include "wx/mstream.h"
2711 static void TestFileStream()
2713 wxPuts(_T("*** Testing wxFileInputStream ***"));
2715 static const wxString filename
= _T("testdata.fs");
2717 wxFileOutputStream
fsOut(filename
);
2718 fsOut
.Write("foo", 3);
2721 wxFileInputStream
fsIn(filename
);
2722 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2723 while ( !fsIn
.Eof() )
2725 wxPutchar(fsIn
.GetC());
2728 if ( !wxRemoveFile(filename
) )
2730 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2733 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2736 static void TestMemoryStream()
2738 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2740 wxMemoryOutputStream memOutStream
;
2741 wxPrintf(_T("Initially out stream offset: %lu\n"),
2742 (unsigned long)memOutStream
.TellO());
2744 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2746 memOutStream
.PutC(*p
);
2749 wxPrintf(_T("Final out stream offset: %lu\n"),
2750 (unsigned long)memOutStream
.TellO());
2752 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2755 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2757 wxMemoryInputStream
memInpStream(buf
, len
);
2758 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2759 while ( !memInpStream
.Eof() )
2761 wxPutchar(memInpStream
.GetC());
2764 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2767 #endif // TEST_STREAMS
2769 // ----------------------------------------------------------------------------
2771 // ----------------------------------------------------------------------------
2775 #include "wx/stopwatch.h"
2776 #include "wx/utils.h"
2778 static void TestStopWatch()
2780 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2784 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2787 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2789 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2793 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2796 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2799 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2802 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2805 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2808 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2809 for ( size_t n
= 0; n
< 70; n
++ )
2813 for ( size_t m
= 0; m
< 100000; m
++ )
2815 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2817 wxPuts(_T("\ntime is negative - ERROR!"));
2825 wxPuts(_T(", ok."));
2828 #endif // TEST_TIMER
2830 // ----------------------------------------------------------------------------
2832 // ----------------------------------------------------------------------------
2836 #include "wx/vcard.h"
2838 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2841 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2844 wxPrintf(_T("%s%s"),
2845 wxString(_T('\t'), level
).c_str(),
2846 vcObj
->GetName().c_str());
2849 switch ( vcObj
->GetType() )
2851 case wxVCardObject::String
:
2852 case wxVCardObject::UString
:
2855 vcObj
->GetValue(&val
);
2856 value
<< _T('"') << val
<< _T('"');
2860 case wxVCardObject::Int
:
2863 vcObj
->GetValue(&i
);
2864 value
.Printf(_T("%u"), i
);
2868 case wxVCardObject::Long
:
2871 vcObj
->GetValue(&l
);
2872 value
.Printf(_T("%lu"), l
);
2876 case wxVCardObject::None
:
2879 case wxVCardObject::Object
:
2880 value
= _T("<node>");
2884 value
= _T("<unknown value type>");
2888 wxPrintf(_T(" = %s"), value
.c_str());
2891 DumpVObject(level
+ 1, *vcObj
);
2894 vcObj
= vcard
.GetNextProp(&cookie
);
2898 static void DumpVCardAddresses(const wxVCard
& vcard
)
2900 wxPuts(_T("\nShowing all addresses from vCard:\n"));
2904 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
2908 int flags
= addr
->GetFlags();
2909 if ( flags
& wxVCardAddress::Domestic
)
2911 flagsStr
<< _T("domestic ");
2913 if ( flags
& wxVCardAddress::Intl
)
2915 flagsStr
<< _T("international ");
2917 if ( flags
& wxVCardAddress::Postal
)
2919 flagsStr
<< _T("postal ");
2921 if ( flags
& wxVCardAddress::Parcel
)
2923 flagsStr
<< _T("parcel ");
2925 if ( flags
& wxVCardAddress::Home
)
2927 flagsStr
<< _T("home ");
2929 if ( flags
& wxVCardAddress::Work
)
2931 flagsStr
<< _T("work ");
2934 wxPrintf(_T("Address %u:\n")
2936 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
2939 addr
->GetPostOffice().c_str(),
2940 addr
->GetExtAddress().c_str(),
2941 addr
->GetStreet().c_str(),
2942 addr
->GetLocality().c_str(),
2943 addr
->GetRegion().c_str(),
2944 addr
->GetPostalCode().c_str(),
2945 addr
->GetCountry().c_str()
2949 addr
= vcard
.GetNextAddress(&cookie
);
2953 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
2955 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
2959 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
2963 int flags
= phone
->GetFlags();
2964 if ( flags
& wxVCardPhoneNumber::Voice
)
2966 flagsStr
<< _T("voice ");
2968 if ( flags
& wxVCardPhoneNumber::Fax
)
2970 flagsStr
<< _T("fax ");
2972 if ( flags
& wxVCardPhoneNumber::Cellular
)
2974 flagsStr
<< _T("cellular ");
2976 if ( flags
& wxVCardPhoneNumber::Modem
)
2978 flagsStr
<< _T("modem ");
2980 if ( flags
& wxVCardPhoneNumber::Home
)
2982 flagsStr
<< _T("home ");
2984 if ( flags
& wxVCardPhoneNumber::Work
)
2986 flagsStr
<< _T("work ");
2989 wxPrintf(_T("Phone number %u:\n")
2994 phone
->GetNumber().c_str()
2998 phone
= vcard
.GetNextPhoneNumber(&cookie
);
3002 static void TestVCardRead()
3004 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3006 wxVCard
vcard(_T("vcard.vcf"));
3007 if ( !vcard
.IsOk() )
3009 wxPuts(_T("ERROR: couldn't load vCard."));
3013 // read individual vCard properties
3014 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3018 vcObj
->GetValue(&value
);
3023 value
= _T("<none>");
3026 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3029 if ( !vcard
.GetFullName(&value
) )
3031 value
= _T("<none>");
3034 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3036 // now show how to deal with multiply occurring properties
3037 DumpVCardAddresses(vcard
);
3038 DumpVCardPhoneNumbers(vcard
);
3040 // and finally show all
3041 wxPuts(_T("\nNow dumping the entire vCard:\n")
3042 "-----------------------------\n");
3044 DumpVObject(0, vcard
);
3048 static void TestVCardWrite()
3050 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3053 if ( !vcard
.IsOk() )
3055 wxPuts(_T("ERROR: couldn't create vCard."));
3060 vcard
.SetName("Zeitlin", "Vadim");
3061 vcard
.SetFullName("Vadim Zeitlin");
3062 vcard
.SetOrganization("wxWidgets", "R&D");
3064 // just dump the vCard back
3065 wxPuts(_T("Entire vCard follows:\n"));
3066 wxPuts(vcard
.Write());
3070 #endif // TEST_VCARD
3072 // ----------------------------------------------------------------------------
3074 // ----------------------------------------------------------------------------
3076 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3082 #include "wx/volume.h"
3084 static const wxChar
*volumeKinds
[] =
3090 _T("network volume"),
3094 static void TestFSVolume()
3096 wxPuts(_T("*** Testing wxFSVolume class ***"));
3098 wxArrayString volumes
= wxFSVolume::GetVolumes();
3099 size_t count
= volumes
.GetCount();
3103 wxPuts(_T("ERROR: no mounted volumes?"));
3107 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3109 for ( size_t n
= 0; n
< count
; n
++ )
3111 wxFSVolume
vol(volumes
[n
]);
3114 wxPuts(_T("ERROR: couldn't create volume"));
3118 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3120 vol
.GetDisplayName().c_str(),
3121 vol
.GetName().c_str(),
3122 volumeKinds
[vol
.GetKind()],
3123 vol
.IsWritable() ? _T("rw") : _T("ro"),
3124 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3129 #endif // TEST_VOLUME
3131 // ----------------------------------------------------------------------------
3132 // wide char and Unicode support
3133 // ----------------------------------------------------------------------------
3137 #include "wx/strconv.h"
3138 #include "wx/fontenc.h"
3139 #include "wx/encconv.h"
3140 #include "wx/buffer.h"
3142 static const unsigned char utf8koi8r
[] =
3144 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3145 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3146 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3147 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3148 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3149 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3150 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3153 static const unsigned char utf8iso8859_1
[] =
3155 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3156 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3157 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3158 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3159 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3162 static const unsigned char utf8Invalid
[] =
3164 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3165 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3166 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3167 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3171 static const struct Utf8Data
3173 const unsigned char *text
;
3175 const wxChar
*charset
;
3176 wxFontEncoding encoding
;
3179 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3180 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3181 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3184 static void TestUtf8()
3186 wxPuts(_T("*** Testing UTF8 support ***\n"));
3191 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3193 const Utf8Data
& u8d
= utf8data
[n
];
3194 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3195 WXSIZEOF(wbuf
)) == (size_t)-1 )
3197 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3201 wxCSConv
conv(u8d
.charset
);
3202 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3204 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3208 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3212 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3214 s
= _T("<< conversion failed >>");
3215 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3219 wxPuts(wxEmptyString
);
3222 static void TestEncodingConverter()
3224 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3226 // using wxEncodingConverter should give the same result as above
3229 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3230 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3232 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3236 wxEncodingConverter ec
;
3237 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3238 ec
.Convert(wbuf
, buf
);
3239 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3242 wxPuts(wxEmptyString
);
3245 #endif // TEST_WCHAR
3247 // ----------------------------------------------------------------------------
3249 // ----------------------------------------------------------------------------
3253 #include "wx/filesys.h"
3254 #include "wx/fs_zip.h"
3255 #include "wx/zipstrm.h"
3257 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3259 static void TestZipStreamRead()
3261 wxPuts(_T("*** Testing ZIP reading ***\n"));
3263 static const wxString filename
= _T("foo");
3264 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3265 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3267 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3268 while ( !istr
.Eof() )
3270 wxPutchar(istr
.GetC());
3274 wxPuts(_T("\n----- done ------"));
3277 static void DumpZipDirectory(wxFileSystem
& fs
,
3278 const wxString
& dir
,
3279 const wxString
& indent
)
3281 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3282 TESTFILE_ZIP
, dir
.c_str());
3283 wxString wildcard
= prefix
+ _T("/*");
3285 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3286 while ( !dirname
.empty() )
3288 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3290 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3295 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3297 DumpZipDirectory(fs
, dirname
,
3298 indent
+ wxString(_T(' '), 4));
3300 dirname
= fs
.FindNext();
3303 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3304 while ( !filename
.empty() )
3306 if ( !filename
.StartsWith(prefix
, &filename
) )
3308 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3313 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3315 filename
= fs
.FindNext();
3319 static void TestZipFileSystem()
3321 wxPuts(_T("*** Testing ZIP file system ***\n"));
3323 wxFileSystem::AddHandler(new wxZipFSHandler
);
3325 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3327 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3332 // ----------------------------------------------------------------------------
3334 // ----------------------------------------------------------------------------
3336 #ifdef TEST_DATETIME
3338 #include "wx/math.h"
3339 #include "wx/datetime.h"
3341 // this test miscellaneous static wxDateTime functions
3345 static void TestTimeStatic()
3347 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3349 // some info about the current date
3350 int year
= wxDateTime::GetCurrentYear();
3351 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3353 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3354 wxDateTime::GetNumberOfDays(year
));
3356 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3357 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3358 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3359 wxDateTime::GetMonthName(month
).c_str(),
3360 wxDateTime::GetNumberOfDays(month
));
3363 // test time zones stuff
3364 static void TestTimeZones()
3366 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3368 wxDateTime now
= wxDateTime::Now();
3370 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3371 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3372 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3373 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3374 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3375 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3377 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3379 wxDateTime::Tm tm
= now
.GetTm();
3380 if ( wxDateTime(tm
) != now
)
3382 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3383 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3387 // test some minimal support for the dates outside the standard range
3388 static void TestTimeRange()
3390 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3392 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3394 wxPrintf(_T("Unix epoch:\t%s\n"),
3395 wxDateTime(2440587.5).Format(fmt
).c_str());
3396 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3397 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3398 wxPrintf(_T("JDN 0: \t%s\n"),
3399 wxDateTime(0.0).Format(fmt
).c_str());
3400 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3401 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3402 wxPrintf(_T("May 29, 2099:\t%s\n"),
3403 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3406 // test DST calculations
3407 static void TestTimeDST()
3409 wxPuts(_T("\n*** wxDateTime DST test ***"));
3411 wxPrintf(_T("DST is%s in effect now.\n\n"),
3412 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3414 for ( int year
= 1990; year
< 2005; year
++ )
3416 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3418 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3419 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3425 #if TEST_INTERACTIVE
3427 static void TestDateTimeInteractive()
3429 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3435 wxPrintf(_T("Enter a date: "));
3436 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3439 // kill the last '\n'
3440 buf
[wxStrlen(buf
) - 1] = 0;
3443 const wxChar
*p
= dt
.ParseDate(buf
);
3446 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3452 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3455 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3456 dt
.Format(_T("%b %d, %Y")).c_str(),
3458 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3459 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3460 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3463 wxPuts(_T("\n*** done ***"));
3466 #endif // TEST_INTERACTIVE
3470 static void TestTimeMS()
3472 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3474 wxDateTime dt1
= wxDateTime::Now(),
3475 dt2
= wxDateTime::UNow();
3477 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3478 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3479 wxPrintf(_T("Dummy loop: "));
3480 for ( int i
= 0; i
< 6000; i
++ )
3482 //for ( int j = 0; j < 10; j++ )
3485 s
.Printf(_T("%g"), sqrt((float)i
));
3491 wxPuts(_T(", done"));
3494 dt2
= wxDateTime::UNow();
3495 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3497 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3499 wxPuts(_T("\n*** done ***"));
3502 static void TestTimeHolidays()
3504 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3506 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3507 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3508 dtEnd
= dtStart
.GetLastMonthDay();
3510 wxDateTimeArray hol
;
3511 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3513 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3515 wxPrintf(_T("All holidays between %s and %s:\n"),
3516 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3518 size_t count
= hol
.GetCount();
3519 for ( size_t n
= 0; n
< count
; n
++ )
3521 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3524 wxPuts(wxEmptyString
);
3527 static void TestTimeZoneBug()
3529 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3531 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3532 for ( int i
= 0; i
< 31; i
++ )
3534 wxPrintf(_T("Date %s: week day %s.\n"),
3535 date
.Format(_T("%d-%m-%Y")).c_str(),
3536 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3538 date
+= wxDateSpan::Day();
3541 wxPuts(wxEmptyString
);
3544 static void TestTimeSpanFormat()
3546 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3548 static const wxChar
*formats
[] =
3550 _T("(default) %H:%M:%S"),
3551 _T("%E weeks and %D days"),
3552 _T("%l milliseconds"),
3553 _T("(with ms) %H:%M:%S:%l"),
3554 _T("100%% of minutes is %M"), // test "%%"
3555 _T("%D days and %H hours"),
3556 _T("or also %S seconds"),
3559 wxTimeSpan
ts1(1, 2, 3, 4),
3561 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3563 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3564 ts1
.Format(formats
[n
]).c_str(),
3565 ts2
.Format(formats
[n
]).c_str());
3568 wxPuts(wxEmptyString
);
3573 #endif // TEST_DATETIME
3575 // ----------------------------------------------------------------------------
3576 // wxTextInput/OutputStream
3577 // ----------------------------------------------------------------------------
3579 #ifdef TEST_TEXTSTREAM
3581 #include "wx/txtstrm.h"
3582 #include "wx/wfstream.h"
3584 static void TestTextInputStream()
3586 wxPuts(_T("\n*** wxTextInputStream test ***"));
3588 wxString filename
= _T("testdata.fc");
3589 wxFileInputStream
fsIn(filename
);
3592 wxPuts(_T("ERROR: couldn't open file."));
3596 wxTextInputStream
tis(fsIn
);
3601 const wxString s
= tis
.ReadLine();
3603 // line could be non empty if the last line of the file isn't
3604 // terminated with EOL
3605 if ( fsIn
.Eof() && s
.empty() )
3608 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3613 #endif // TEST_TEXTSTREAM
3615 // ----------------------------------------------------------------------------
3617 // ----------------------------------------------------------------------------
3621 #include "wx/thread.h"
3623 static size_t gs_counter
= (size_t)-1;
3624 static wxCriticalSection gs_critsect
;
3625 static wxSemaphore gs_cond
;
3627 class MyJoinableThread
: public wxThread
3630 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3631 { m_n
= n
; Create(); }
3633 // thread execution starts here
3634 virtual ExitCode
Entry();
3640 wxThread::ExitCode
MyJoinableThread::Entry()
3642 unsigned long res
= 1;
3643 for ( size_t n
= 1; n
< m_n
; n
++ )
3647 // it's a loooong calculation :-)
3651 return (ExitCode
)res
;
3654 class MyDetachedThread
: public wxThread
3657 MyDetachedThread(size_t n
, wxChar ch
)
3661 m_cancelled
= false;
3666 // thread execution starts here
3667 virtual ExitCode
Entry();
3670 virtual void OnExit();
3673 size_t m_n
; // number of characters to write
3674 wxChar m_ch
; // character to write
3676 bool m_cancelled
; // false if we exit normally
3679 wxThread::ExitCode
MyDetachedThread::Entry()
3682 wxCriticalSectionLocker
lock(gs_critsect
);
3683 if ( gs_counter
== (size_t)-1 )
3689 for ( size_t n
= 0; n
< m_n
; n
++ )
3691 if ( TestDestroy() )
3701 wxThread::Sleep(100);
3707 void MyDetachedThread::OnExit()
3709 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3711 wxCriticalSectionLocker
lock(gs_critsect
);
3712 if ( !--gs_counter
&& !m_cancelled
)
3716 static void TestDetachedThreads()
3718 wxPuts(_T("\n*** Testing detached threads ***"));
3720 static const size_t nThreads
= 3;
3721 MyDetachedThread
*threads
[nThreads
];
3723 for ( n
= 0; n
< nThreads
; n
++ )
3725 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3728 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3729 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3731 for ( n
= 0; n
< nThreads
; n
++ )
3736 // wait until all threads terminate
3739 wxPuts(wxEmptyString
);
3742 static void TestJoinableThreads()
3744 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3746 // calc 10! in the background
3747 MyJoinableThread
thread(10);
3750 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3751 (unsigned long)thread
.Wait());
3754 static void TestThreadSuspend()
3756 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3758 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3762 // this is for this demo only, in a real life program we'd use another
3763 // condition variable which would be signaled from wxThread::Entry() to
3764 // tell us that the thread really started running - but here just wait a
3765 // bit and hope that it will be enough (the problem is, of course, that
3766 // the thread might still not run when we call Pause() which will result
3768 wxThread::Sleep(300);
3770 for ( size_t n
= 0; n
< 3; n
++ )
3774 wxPuts(_T("\nThread suspended"));
3777 // don't sleep but resume immediately the first time
3778 wxThread::Sleep(300);
3780 wxPuts(_T("Going to resume the thread"));
3785 wxPuts(_T("Waiting until it terminates now"));
3787 // wait until the thread terminates
3790 wxPuts(wxEmptyString
);
3793 static void TestThreadDelete()
3795 // As above, using Sleep() is only for testing here - we must use some
3796 // synchronisation object instead to ensure that the thread is still
3797 // running when we delete it - deleting a detached thread which already
3798 // terminated will lead to a crash!
3800 wxPuts(_T("\n*** Testing thread delete function ***"));
3802 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3806 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3808 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3812 wxThread::Sleep(300);
3816 wxPuts(_T("\nDeleted a running thread."));
3818 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3822 wxThread::Sleep(300);
3828 wxPuts(_T("\nDeleted a sleeping thread."));
3830 MyJoinableThread
thread3(20);
3835 wxPuts(_T("\nDeleted a joinable thread."));
3837 MyJoinableThread
thread4(2);
3840 wxThread::Sleep(300);
3844 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3846 wxPuts(wxEmptyString
);
3849 class MyWaitingThread
: public wxThread
3852 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3855 m_condition
= condition
;
3860 virtual ExitCode
Entry()
3862 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3867 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3871 m_condition
->Wait();
3874 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3882 wxCondition
*m_condition
;
3885 static void TestThreadConditions()
3888 wxCondition
condition(mutex
);
3890 // otherwise its difficult to understand which log messages pertain to
3892 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3893 // condition.GetId(), gs_cond.GetId());
3895 // create and launch threads
3896 MyWaitingThread
*threads
[10];
3899 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3901 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
3904 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3909 // wait until all threads run
3910 wxPuts(_T("Main thread is waiting for the other threads to start"));
3913 size_t nRunning
= 0;
3914 while ( nRunning
< WXSIZEOF(threads
) )
3920 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
3924 wxPuts(_T("Main thread: all threads started up."));
3927 wxThread::Sleep(500);
3930 // now wake one of them up
3931 wxPrintf(_T("Main thread: about to signal the condition.\n"));
3936 wxThread::Sleep(200);
3938 // wake all the (remaining) threads up, so that they can exit
3939 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
3941 condition
.Broadcast();
3943 // give them time to terminate (dirty!)
3944 wxThread::Sleep(500);
3947 #include "wx/utils.h"
3949 class MyExecThread
: public wxThread
3952 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
3958 virtual ExitCode
Entry()
3960 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
3967 static void TestThreadExec()
3969 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
3971 MyExecThread
thread(_T("true"));
3974 wxPrintf(_T("Main program exit code: %ld.\n"),
3975 wxExecute(_T("false"), wxEXEC_SYNC
));
3977 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
3981 #include "wx/datetime.h"
3983 class MySemaphoreThread
: public wxThread
3986 MySemaphoreThread(int i
, wxSemaphore
*sem
)
3987 : wxThread(wxTHREAD_JOINABLE
),
3994 virtual ExitCode
Entry()
3996 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
3997 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4001 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4002 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4006 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4007 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
4019 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
4021 static void TestSemaphore()
4023 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4025 static const int SEM_LIMIT
= 3;
4027 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
4028 ArrayThreads threads
;
4030 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
4032 threads
.Add(new MySemaphoreThread(i
, &sem
));
4033 threads
.Last()->Run();
4036 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4043 #endif // TEST_THREADS
4045 // ----------------------------------------------------------------------------
4047 // ----------------------------------------------------------------------------
4049 #ifdef TEST_SNGLINST
4050 #include "wx/snglinst.h"
4051 #endif // TEST_SNGLINST
4053 int main(int argc
, char **argv
)
4056 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4061 for (n
= 0; n
< argc
; n
++ )
4063 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4064 wxArgv
[n
] = wxStrdup(warg
);
4069 #else // !wxUSE_UNICODE
4071 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4073 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4075 wxInitializer initializer
;
4078 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4083 #ifdef TEST_SNGLINST
4084 wxSingleInstanceChecker checker
;
4085 if ( checker
.Create(_T(".wxconsole.lock")) )
4087 if ( checker
.IsAnotherRunning() )
4089 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4094 // wait some time to give time to launch another instance
4095 wxPrintf(_T("Press \"Enter\" to continue..."));
4098 else // failed to create
4100 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4102 #endif // TEST_SNGLINST
4105 TestCmdLineConvert();
4107 #if wxUSE_CMDLINE_PARSER
4108 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4110 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
4111 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4112 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
4113 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
4115 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
4116 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
4117 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
4118 wxCMD_LINE_VAL_NUMBER
},
4119 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
4120 wxCMD_LINE_VAL_DATE
},
4122 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
4123 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4128 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4130 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4131 wxCMD_LINE_VAL_STRING
,
4132 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4134 switch ( parser
.Parse() )
4137 wxLogMessage(_T("Help was given, terminating."));
4141 ShowCmdLine(parser
);
4145 wxLogMessage(_T("Syntax error detected, aborting."));
4148 #endif // wxUSE_CMDLINE_PARSER
4150 #endif // TEST_CMDLINE
4162 TestDllListLoaded();
4163 #endif // TEST_DYNLIB
4167 #endif // TEST_ENVIRON
4171 #endif // TEST_EXECUTE
4173 #ifdef TEST_FILECONF
4175 #endif // TEST_FILECONF
4179 #endif // TEST_LOCALE
4182 wxPuts(_T("*** Testing wxLog ***"));
4185 for ( size_t n
= 0; n
< 8000; n
++ )
4187 s
<< (wxChar
)(_T('A') + (n
% 26));
4190 wxLogWarning(_T("The length of the string is %lu"),
4191 (unsigned long)s
.length());
4194 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4196 // this one shouldn't be truncated
4199 // but this one will because log functions use fixed size buffer
4200 // (note that it doesn't need '\n' at the end neither - will be added
4202 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4211 #ifdef TEST_FILENAME
4214 TestFileNameDirManip();
4215 TestFileNameComparison();
4216 TestFileNameOperations();
4217 #endif // TEST_FILENAME
4219 #ifdef TEST_FILETIME
4224 #endif // TEST_FILETIME
4227 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4228 if ( TestFtpConnect() )
4238 #if TEST_INTERACTIVE
4239 TestFtpInteractive();
4242 //else: connecting to the FTP server failed
4250 wxLog::AddTraceMask(_T("mime"));
4254 TestMimeAssociate();
4259 #ifdef TEST_INFO_FUNCTIONS
4264 #if TEST_INTERACTIVE
4268 #endif // TEST_INFO_FUNCTIONS
4270 #ifdef TEST_PATHLIST
4272 #endif // TEST_PATHLIST
4280 #endif // TEST_PRINTF
4287 #endif // TEST_REGCONF
4289 #if defined TEST_REGEX && TEST_INTERACTIVE
4290 TestRegExInteractive();
4291 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4293 #ifdef TEST_REGISTRY
4295 TestRegistryAssociation();
4296 #endif // TEST_REGISTRY
4301 #endif // TEST_SOCKETS
4308 #endif // TEST_STREAMS
4310 #ifdef TEST_TEXTSTREAM
4311 TestTextInputStream();
4312 #endif // TEST_TEXTSTREAM
4315 int nCPUs
= wxThread::GetCPUCount();
4316 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4318 wxThread::SetConcurrency(nCPUs
);
4320 TestJoinableThreads();
4323 TestJoinableThreads();
4324 TestDetachedThreads();
4325 TestThreadSuspend();
4327 TestThreadConditions();
4331 #endif // TEST_THREADS
4335 #endif // TEST_TIMER
4337 #ifdef TEST_DATETIME
4344 TestTimeSpanFormat();
4350 #if TEST_INTERACTIVE
4351 TestDateTimeInteractive();
4353 #endif // TEST_DATETIME
4355 #ifdef TEST_SCOPEGUARD
4359 #ifdef TEST_STACKWALKER
4360 #if wxUSE_STACKWALKER
4361 TestStackWalk(argv
[0]);
4363 #endif // TEST_STACKWALKER
4365 #ifdef TEST_STDPATHS
4366 TestStandardPaths();
4370 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4372 #endif // TEST_USLEEP
4377 #endif // TEST_VCARD
4381 #endif // TEST_VOLUME
4385 TestEncodingConverter();
4386 #endif // TEST_WCHAR
4389 TestZipStreamRead();
4390 TestZipFileSystem();
4395 for ( int n
= 0; n
< argc
; n
++ )
4400 #endif // wxUSE_UNICODE