1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam 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.
56 #define TEST_DLLLOADER
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)
88 #define TEST_STACKWALKER
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__)
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
);
231 static void TestDirEnum()
233 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
235 wxString cwd
= wxGetCwd();
236 if ( !wxDir::Exists(cwd
) )
238 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
243 if ( !dir
.IsOpened() )
245 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
249 wxPuts(_T("Enumerating everything in current directory:"));
250 TestDirEnumHelper(dir
);
252 wxPuts(_T("Enumerating really everything in current directory:"));
253 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
255 wxPuts(_T("Enumerating object files in current directory:"));
256 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
258 wxPuts(_T("Enumerating directories in current directory:"));
259 TestDirEnumHelper(dir
, wxDIR_DIRS
);
261 wxPuts(_T("Enumerating files in current directory:"));
262 TestDirEnumHelper(dir
, wxDIR_FILES
);
264 wxPuts(_T("Enumerating files including hidden in current directory:"));
265 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
269 wxPuts(_T("Enumerating everything in root directory:"));
270 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
272 wxPuts(_T("Enumerating directories in root directory:"));
273 TestDirEnumHelper(dir
, wxDIR_DIRS
);
275 wxPuts(_T("Enumerating files in root directory:"));
276 TestDirEnumHelper(dir
, wxDIR_FILES
);
278 wxPuts(_T("Enumerating files including hidden in root directory:"));
279 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
281 wxPuts(_T("Enumerating files in non existing directory:"));
282 wxDir
dirNo(_T("nosuchdir"));
283 TestDirEnumHelper(dirNo
);
286 class DirPrintTraverser
: public wxDirTraverser
289 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
291 return wxDIR_CONTINUE
;
294 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
296 wxString path
, name
, ext
;
297 wxSplitPath(dirname
, &path
, &name
, &ext
);
300 name
<< _T('.') << ext
;
303 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
305 if ( wxIsPathSeparator(*p
) )
309 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
311 return wxDIR_CONTINUE
;
315 static void TestDirTraverse()
317 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
321 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
322 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
325 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
326 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
329 // enum again with custom traverser
330 wxPuts(_T("Now enumerating directories:"));
332 DirPrintTraverser traverser
;
333 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
336 static void TestDirExists()
338 wxPuts(_T("*** Testing wxDir::Exists() ***"));
340 static const wxChar
*dirnames
[] =
343 #if defined(__WXMSW__)
346 _T("\\\\share\\file"),
350 _T("c:\\autoexec.bat"),
351 #elif defined(__UNIX__)
360 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
362 wxPrintf(_T("%-40s: %s\n"),
364 wxDir::Exists(dirnames
[n
]) ? _T("exists")
365 : _T("doesn't exist"));
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 #ifdef TEST_DLLLOADER
377 #include "wx/dynlib.h"
379 static void TestDllLoad()
381 #if defined(__WXMSW__)
382 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
383 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
384 #elif defined(__UNIX__)
385 // weird: using just libc.so does *not* work!
386 static const wxChar
*LIB_NAME
= _T("/lib/libc.so.6");
387 static const wxChar
*FUNC_NAME
= _T("strlen");
389 #error "don't know how to test wxDllLoader on this platform"
392 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
394 wxDynamicLibrary
lib(LIB_NAME
);
395 if ( !lib
.IsLoaded() )
397 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
401 typedef int (*wxStrlenType
)(const char *);
402 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
405 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
406 FUNC_NAME
, LIB_NAME
);
410 wxPrintf(_T("Calling %s dynamically loaded from %s "),
411 FUNC_NAME
, LIB_NAME
);
413 if ( pfnStrlen("foo") != 3 )
415 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
419 wxPuts(_T("... ok"));
425 #if defined(__WXMSW__) || defined(__UNIX__)
427 static void TestDllListLoaded()
429 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
431 puts("\nLoaded modules:");
432 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
433 const size_t count
= dlls
.GetCount();
434 for ( size_t n
= 0; n
< count
; ++n
)
436 const wxDynamicLibraryDetails
& details
= dlls
[n
];
437 printf("%-45s", details
.GetPath().mb_str());
441 if ( details
.GetAddress(&addr
, &len
) )
443 printf(" %08lx:%08lx",
444 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
447 printf(" %s\n", details
.GetVersion().mb_str());
453 #endif // TEST_DLLLOADER
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
461 #include "wx/utils.h"
463 static wxString
MyGetEnv(const wxString
& var
)
466 if ( !wxGetEnv(var
, &val
) )
469 val
= wxString(_T('\'')) + val
+ _T('\'');
474 static void TestEnvironment()
476 const wxChar
*var
= _T("wxTestVar");
478 wxPuts(_T("*** testing environment access functions ***"));
480 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
481 wxSetEnv(var
, _T("value for wxTestVar"));
482 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
483 wxSetEnv(var
, _T("another value"));
484 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
486 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
487 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
490 #endif // TEST_ENVIRON
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
498 #include "wx/utils.h"
500 static void TestExecute()
502 wxPuts(_T("*** testing wxExecute ***"));
505 #define COMMAND "cat -n ../../Makefile" // "echo hi"
506 #define SHELL_COMMAND "echo hi from shell"
507 #define REDIRECT_COMMAND COMMAND // "date"
508 #elif defined(__WXMSW__)
509 #define COMMAND "command.com /c echo hi"
510 #define SHELL_COMMAND "echo hi"
511 #define REDIRECT_COMMAND COMMAND
513 #error "no command to exec"
516 wxPrintf(_T("Testing wxShell: "));
518 if ( wxShell(_T(SHELL_COMMAND
)) )
521 wxPuts(_T("ERROR."));
523 wxPrintf(_T("Testing wxExecute: "));
525 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
528 wxPuts(_T("ERROR."));
530 #if 0 // no, it doesn't work (yet?)
531 wxPrintf(_T("Testing async wxExecute: "));
533 if ( wxExecute(COMMAND
) != 0 )
534 wxPuts(_T("Ok (command launched)."));
536 wxPuts(_T("ERROR."));
539 wxPrintf(_T("Testing wxExecute with redirection:\n"));
540 wxArrayString output
;
541 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
543 wxPuts(_T("ERROR."));
547 size_t count
= output
.GetCount();
548 for ( size_t n
= 0; n
< count
; n
++ )
550 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
557 #endif // TEST_EXECUTE
559 // ----------------------------------------------------------------------------
561 // ----------------------------------------------------------------------------
566 #include "wx/ffile.h"
567 #include "wx/textfile.h"
569 static void TestFileRead()
571 wxPuts(_T("*** wxFile read test ***"));
573 wxFile
file(_T("testdata.fc"));
574 if ( file
.IsOpened() )
576 wxPrintf(_T("File length: %lu\n"), file
.Length());
578 wxPuts(_T("File dump:\n----------"));
580 static const size_t len
= 1024;
584 size_t nRead
= file
.Read(buf
, len
);
585 if ( nRead
== (size_t)wxInvalidOffset
)
587 wxPrintf(_T("Failed to read the file."));
591 fwrite(buf
, nRead
, 1, stdout
);
597 wxPuts(_T("----------"));
601 wxPrintf(_T("ERROR: can't open test file.\n"));
604 wxPuts(wxEmptyString
);
607 static void TestTextFileRead()
609 wxPuts(_T("*** wxTextFile read test ***"));
611 wxTextFile
file(_T("testdata.fc"));
614 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
615 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
619 wxPuts(_T("\nDumping the entire file:"));
620 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
622 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
624 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
626 wxPuts(_T("\nAnd now backwards:"));
627 for ( s
= file
.GetLastLine();
628 file
.GetCurrentLine() != 0;
629 s
= file
.GetPrevLine() )
631 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
633 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
637 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
640 wxPuts(wxEmptyString
);
643 static void TestFileCopy()
645 wxPuts(_T("*** Testing wxCopyFile ***"));
647 static const wxChar
*filename1
= _T("testdata.fc");
648 static const wxChar
*filename2
= _T("test2");
649 if ( !wxCopyFile(filename1
, filename2
) )
651 wxPuts(_T("ERROR: failed to copy file"));
655 wxFFile
f1(filename1
, _T("rb")),
656 f2(filename2
, _T("rb"));
658 if ( !f1
.IsOpened() || !f2
.IsOpened() )
660 wxPuts(_T("ERROR: failed to open file(s)"));
665 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
667 wxPuts(_T("ERROR: failed to read file(s)"));
671 if ( (s1
.length() != s2
.length()) ||
672 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
674 wxPuts(_T("ERROR: copy error!"));
678 wxPuts(_T("File was copied ok."));
684 if ( !wxRemoveFile(filename2
) )
686 wxPuts(_T("ERROR: failed to remove the file"));
689 wxPuts(wxEmptyString
);
694 // ----------------------------------------------------------------------------
696 // ----------------------------------------------------------------------------
700 #include "wx/confbase.h"
701 #include "wx/fileconf.h"
703 static const struct FileConfTestData
705 const wxChar
*name
; // value name
706 const wxChar
*value
; // the value from the file
709 { _T("value1"), _T("one") },
710 { _T("value2"), _T("two") },
711 { _T("novalue"), _T("default") },
714 static void TestFileConfRead()
716 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
718 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
719 _T("testdata.fc"), wxEmptyString
,
720 wxCONFIG_USE_RELATIVE_PATH
);
722 // test simple reading
723 wxPuts(_T("\nReading config file:"));
724 wxString
defValue(_T("default")), value
;
725 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
727 const FileConfTestData
& data
= fcTestData
[n
];
728 value
= fileconf
.Read(data
.name
, defValue
);
729 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
730 if ( value
== data
.value
)
736 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
740 // test enumerating the entries
741 wxPuts(_T("\nEnumerating all root entries:"));
744 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
747 wxPrintf(_T("\t%s = %s\n"),
749 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
751 cont
= fileconf
.GetNextEntry(name
, dummy
);
754 static const wxChar
*testEntry
= _T("TestEntry");
755 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
756 fileconf
.Write(testEntry
, _T("A value"));
757 fileconf
.DeleteEntry(testEntry
);
758 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
761 #endif // TEST_FILECONF
763 // ----------------------------------------------------------------------------
765 // ----------------------------------------------------------------------------
769 #include "wx/filename.h"
772 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
776 wxString full
= fn
.GetFullPath();
778 wxString vol
, path
, name
, ext
;
779 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
781 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
782 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
784 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
785 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
786 path
.c_str(), name
.c_str(), ext
.c_str());
788 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
789 wxPrintf(_T("with volume: \t'%s'\n"),
790 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
791 wxPrintf(_T("with separator:\t'%s'\n"),
792 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
793 wxPrintf(_T("with both: \t'%s'\n"),
794 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
796 wxPuts(_T("The directories in the path are:"));
797 wxArrayString dirs
= fn
.GetDirs();
798 size_t count
= dirs
.GetCount();
799 for ( size_t n
= 0; n
< count
; n
++ )
801 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
806 static void TestFileNameTemp()
808 wxPuts(_T("*** testing wxFileName temp file creation ***"));
810 static const wxChar
*tmpprefixes
[] =
818 _T("/tmp/foo/bar"), // this one must be an error
822 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
824 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
827 // "error" is not in upper case because it may be ok
828 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
832 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
833 tmpprefixes
[n
], path
.c_str());
835 if ( !wxRemoveFile(path
) )
837 wxLogWarning(_T("Failed to remove temp file '%s'"),
844 static void TestFileNameDirManip()
846 // TODO: test AppendDir(), RemoveDir(), ...
849 static void TestFileNameComparison()
854 static void TestFileNameOperations()
859 static void TestFileNameCwd()
864 #endif // TEST_FILENAME
866 // ----------------------------------------------------------------------------
867 // wxFileName time functions
868 // ----------------------------------------------------------------------------
872 #include <wx/filename.h>
873 #include <wx/datetime.h>
875 static void TestFileGetTimes()
877 wxFileName
fn(_T("testdata.fc"));
879 wxDateTime dtAccess
, dtMod
, dtCreate
;
880 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
882 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
886 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
888 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
889 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
890 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
891 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
896 static void TestFileSetTimes()
898 wxFileName
fn(_T("testdata.fc"));
902 wxPrintf(_T("ERROR: Touch() failed.\n"));
907 #endif // TEST_FILETIME
909 // ----------------------------------------------------------------------------
911 // ----------------------------------------------------------------------------
916 #include "wx/utils.h" // for wxSetEnv
918 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
920 // find the name of the language from its value
921 static const wxChar
*GetLangName(int lang
)
923 static const wxChar
*languageNames
[] =
933 _T("ARABIC_ALGERIA"),
934 _T("ARABIC_BAHRAIN"),
939 _T("ARABIC_LEBANON"),
941 _T("ARABIC_MOROCCO"),
944 _T("ARABIC_SAUDI_ARABIA"),
947 _T("ARABIC_TUNISIA"),
954 _T("AZERI_CYRILLIC"),
969 _T("CHINESE_SIMPLIFIED"),
970 _T("CHINESE_TRADITIONAL"),
971 _T("CHINESE_HONGKONG"),
973 _T("CHINESE_SINGAPORE"),
974 _T("CHINESE_TAIWAN"),
984 _T("ENGLISH_AUSTRALIA"),
985 _T("ENGLISH_BELIZE"),
986 _T("ENGLISH_BOTSWANA"),
987 _T("ENGLISH_CANADA"),
988 _T("ENGLISH_CARIBBEAN"),
989 _T("ENGLISH_DENMARK"),
991 _T("ENGLISH_JAMAICA"),
992 _T("ENGLISH_NEW_ZEALAND"),
993 _T("ENGLISH_PHILIPPINES"),
994 _T("ENGLISH_SOUTH_AFRICA"),
995 _T("ENGLISH_TRINIDAD"),
996 _T("ENGLISH_ZIMBABWE"),
1004 _T("FRENCH_BELGIAN"),
1005 _T("FRENCH_CANADIAN"),
1006 _T("FRENCH_LUXEMBOURG"),
1007 _T("FRENCH_MONACO"),
1013 _T("GERMAN_AUSTRIAN"),
1014 _T("GERMAN_BELGIUM"),
1015 _T("GERMAN_LIECHTENSTEIN"),
1016 _T("GERMAN_LUXEMBOURG"),
1034 _T("ITALIAN_SWISS"),
1039 _T("KASHMIRI_INDIA"),
1057 _T("MALAY_BRUNEI_DARUSSALAM"),
1058 _T("MALAY_MALAYSIA"),
1068 _T("NORWEGIAN_BOKMAL"),
1069 _T("NORWEGIAN_NYNORSK"),
1076 _T("PORTUGUESE_BRAZILIAN"),
1079 _T("RHAETO_ROMANCE"),
1082 _T("RUSSIAN_UKRAINE"),
1088 _T("SERBIAN_CYRILLIC"),
1089 _T("SERBIAN_LATIN"),
1090 _T("SERBO_CROATIAN"),
1101 _T("SPANISH_ARGENTINA"),
1102 _T("SPANISH_BOLIVIA"),
1103 _T("SPANISH_CHILE"),
1104 _T("SPANISH_COLOMBIA"),
1105 _T("SPANISH_COSTA_RICA"),
1106 _T("SPANISH_DOMINICAN_REPUBLIC"),
1107 _T("SPANISH_ECUADOR"),
1108 _T("SPANISH_EL_SALVADOR"),
1109 _T("SPANISH_GUATEMALA"),
1110 _T("SPANISH_HONDURAS"),
1111 _T("SPANISH_MEXICAN"),
1112 _T("SPANISH_MODERN"),
1113 _T("SPANISH_NICARAGUA"),
1114 _T("SPANISH_PANAMA"),
1115 _T("SPANISH_PARAGUAY"),
1117 _T("SPANISH_PUERTO_RICO"),
1118 _T("SPANISH_URUGUAY"),
1120 _T("SPANISH_VENEZUELA"),
1124 _T("SWEDISH_FINLAND"),
1142 _T("URDU_PAKISTAN"),
1144 _T("UZBEK_CYRILLIC"),
1157 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1158 return languageNames
[lang
];
1160 return _T("INVALID");
1163 static void TestDefaultLang()
1165 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1167 static const wxChar
*langStrings
[] =
1169 NULL
, // system default
1176 _T("de_DE.iso88591"),
1178 _T("?"), // invalid lang spec
1179 _T("klingonese"), // I bet on some systems it does exist...
1182 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1183 wxLocale::GetSystemEncodingName().c_str(),
1184 wxLocale::GetSystemEncoding());
1186 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1188 const wxChar
*langStr
= langStrings
[n
];
1191 // FIXME: this doesn't do anything at all under Windows, we need
1192 // to create a new wxLocale!
1193 wxSetEnv(_T("LC_ALL"), langStr
);
1196 int lang
= gs_localeDefault
.GetSystemLanguage();
1197 wxPrintf(_T("Locale for '%s' is %s.\n"),
1198 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1202 #endif // TEST_LOCALE
1204 // ----------------------------------------------------------------------------
1206 // ----------------------------------------------------------------------------
1210 #include "wx/mimetype.h"
1212 static void TestMimeEnum()
1214 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1216 wxArrayString mimetypes
;
1218 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1220 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1225 for ( size_t n
= 0; n
< count
; n
++ )
1227 wxFileType
*filetype
=
1228 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1231 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1232 mimetypes
[n
].c_str());
1236 filetype
->GetDescription(&desc
);
1237 filetype
->GetExtensions(exts
);
1239 filetype
->GetIcon(NULL
);
1242 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1245 extsAll
<< _T(", ");
1249 wxPrintf(_T("\t%s: %s (%s)\n"),
1250 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1253 wxPuts(wxEmptyString
);
1256 static void TestMimeOverride()
1258 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1260 static const wxChar
*mailcap
= _T("/tmp/mailcap");
1261 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
1263 if ( wxFile::Exists(mailcap
) )
1264 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1266 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
1268 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1271 if ( wxFile::Exists(mimetypes
) )
1272 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1274 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
1276 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1279 wxPuts(wxEmptyString
);
1282 static void TestMimeFilename()
1284 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1286 static const wxChar
*filenames
[] =
1294 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1296 const wxString fname
= filenames
[n
];
1297 wxString ext
= fname
.AfterLast(_T('.'));
1298 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1301 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1306 if ( !ft
->GetDescription(&desc
) )
1307 desc
= _T("<no description>");
1310 if ( !ft
->GetOpenCommand(&cmd
,
1311 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1312 cmd
= _T("<no command available>");
1314 cmd
= wxString(_T('"')) + cmd
+ _T('"');
1316 wxPrintf(_T("To open %s (%s) do %s.\n"),
1317 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1323 wxPuts(wxEmptyString
);
1326 static void TestMimeAssociate()
1328 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1330 wxFileTypeInfo
ftInfo(
1331 _T("application/x-xyz"),
1332 _T("xyzview '%s'"), // open cmd
1333 _T(""), // print cmd
1334 _T("XYZ File"), // description
1335 _T(".xyz"), // extensions
1336 NULL
// end of extensions
1338 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
1340 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1343 wxPuts(_T("ERROR: failed to create association!"));
1347 // TODO: read it back
1351 wxPuts(wxEmptyString
);
1356 // ----------------------------------------------------------------------------
1357 // misc information functions
1358 // ----------------------------------------------------------------------------
1360 #ifdef TEST_INFO_FUNCTIONS
1362 #include "wx/utils.h"
1364 static void TestDiskInfo()
1366 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1370 wxChar pathname
[128];
1371 wxPrintf(_T("\nEnter a directory name: "));
1372 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1375 // kill the last '\n'
1376 pathname
[wxStrlen(pathname
) - 1] = 0;
1378 wxLongLong total
, free
;
1379 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1381 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1385 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1386 (total
/ 1024).ToString().c_str(),
1387 (free
/ 1024).ToString().c_str(),
1393 static void TestOsInfo()
1395 wxPuts(_T("*** Testing OS info functions ***\n"));
1398 wxGetOsVersion(&major
, &minor
);
1399 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1400 wxGetOsDescription().c_str(), major
, minor
);
1402 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
1404 wxPrintf(_T("Host name is %s (%s).\n"),
1405 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1407 wxPuts(wxEmptyString
);
1410 static void TestUserInfo()
1412 wxPuts(_T("*** Testing user info functions ***\n"));
1414 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1415 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1416 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1417 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1419 wxPuts(wxEmptyString
);
1422 #endif // TEST_INFO_FUNCTIONS
1424 // ----------------------------------------------------------------------------
1426 // ----------------------------------------------------------------------------
1428 #ifdef TEST_PATHLIST
1431 #define CMD_IN_PATH _T("ls")
1433 #define CMD_IN_PATH _T("command.com")
1436 static void TestPathList()
1438 wxPuts(_T("*** Testing wxPathList ***\n"));
1440 wxPathList pathlist
;
1441 pathlist
.AddEnvList(_T("PATH"));
1442 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1445 wxPrintf(_T("ERROR: command not found in the path.\n"));
1449 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
1453 #endif // TEST_PATHLIST
1455 // ----------------------------------------------------------------------------
1456 // regular expressions
1457 // ----------------------------------------------------------------------------
1461 #include "wx/regex.h"
1463 static void TestRegExInteractive()
1465 wxPuts(_T("*** Testing RE interactively ***"));
1469 wxChar pattern
[128];
1470 wxPrintf(_T("\nEnter a pattern: "));
1471 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1474 // kill the last '\n'
1475 pattern
[wxStrlen(pattern
) - 1] = 0;
1478 if ( !re
.Compile(pattern
) )
1486 wxPrintf(_T("Enter text to match: "));
1487 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1490 // kill the last '\n'
1491 text
[wxStrlen(text
) - 1] = 0;
1493 if ( !re
.Matches(text
) )
1495 wxPrintf(_T("No match.\n"));
1499 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1502 for ( size_t n
= 1; ; n
++ )
1504 if ( !re
.GetMatch(&start
, &len
, n
) )
1509 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1510 n
, wxString(text
+ start
, len
).c_str());
1517 #endif // TEST_REGEX
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1531 static void TestDbOpen()
1539 // ----------------------------------------------------------------------------
1541 // ----------------------------------------------------------------------------
1544 NB: this stuff was taken from the glibc test suite and modified to build
1545 in wxWidgets: if I read the copyright below properly, this shouldn't
1551 #ifdef wxTEST_PRINTF
1552 // use our functions from wxchar.cpp
1556 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1557 // in the tests below
1558 int wxPrintf( const wxChar
*format
, ... );
1559 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1562 #include "wx/longlong.h"
1566 static void rfg1 (void);
1567 static void rfg2 (void);
1571 fmtchk (const wxChar
*fmt
)
1573 (void) wxPrintf(_T("%s:\t`"), fmt
);
1574 (void) wxPrintf(fmt
, 0x12);
1575 (void) wxPrintf(_T("'\n"));
1579 fmtst1chk (const wxChar
*fmt
)
1581 (void) wxPrintf(_T("%s:\t`"), fmt
);
1582 (void) wxPrintf(fmt
, 4, 0x12);
1583 (void) wxPrintf(_T("'\n"));
1587 fmtst2chk (const wxChar
*fmt
)
1589 (void) wxPrintf(_T("%s:\t`"), fmt
);
1590 (void) wxPrintf(fmt
, 4, 4, 0x12);
1591 (void) wxPrintf(_T("'\n"));
1594 /* This page is covered by the following copyright: */
1596 /* (C) Copyright C E Chew
1598 * Feel free to copy, use and distribute this software provided:
1600 * 1. you do not pretend that you wrote it
1601 * 2. you leave this copyright notice intact.
1605 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1612 /* Formatted Output Test
1614 * This exercises the output formatting code.
1617 wxChar
*PointerNull
= NULL
;
1624 wxChar
*prefix
= buf
;
1627 wxPuts(_T("\nFormatted output test"));
1628 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1629 wxStrcpy(prefix
, _T("%"));
1630 for (i
= 0; i
< 2; i
++) {
1631 for (j
= 0; j
< 2; j
++) {
1632 for (k
= 0; k
< 2; k
++) {
1633 for (l
= 0; l
< 2; l
++) {
1634 wxStrcpy(prefix
, _T("%"));
1635 if (i
== 0) wxStrcat(prefix
, _T("-"));
1636 if (j
== 0) wxStrcat(prefix
, _T("+"));
1637 if (k
== 0) wxStrcat(prefix
, _T("#"));
1638 if (l
== 0) wxStrcat(prefix
, _T("0"));
1639 wxPrintf(_T("%5s |"), prefix
);
1640 wxStrcpy(tp
, prefix
);
1641 wxStrcat(tp
, _T("6d |"));
1643 wxStrcpy(tp
, prefix
);
1644 wxStrcat(tp
, _T("6o |"));
1646 wxStrcpy(tp
, prefix
);
1647 wxStrcat(tp
, _T("6x |"));
1649 wxStrcpy(tp
, prefix
);
1650 wxStrcat(tp
, _T("6X |"));
1652 wxStrcpy(tp
, prefix
);
1653 wxStrcat(tp
, _T("6u |"));
1660 wxPrintf(_T("%10s\n"), PointerNull
);
1661 wxPrintf(_T("%-10s\n"), PointerNull
);
1664 static void TestPrintf()
1666 static wxChar shortstr
[] = _T("Hi, Z.");
1667 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
1668 I am ready for my first lesson today.");
1670 wxString test_format
;
1674 fmtchk(_T("%4.4x"));
1675 fmtchk(_T("%04.4x"));
1676 fmtchk(_T("%4.3x"));
1677 fmtchk(_T("%04.3x"));
1679 fmtst1chk(_T("%.*x"));
1680 fmtst1chk(_T("%0*x"));
1681 fmtst2chk(_T("%*.*x"));
1682 fmtst2chk(_T("%0*.*x"));
1684 wxString bad_format
= _T("bad format:\t\"%b\"\n");
1685 wxPrintf(bad_format
.c_str());
1686 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1688 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1689 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1690 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1691 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1692 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1693 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1694 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1695 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1696 wxPrintf(test_format
.c_str(), -123456);
1697 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1698 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1700 test_format
= _T("zero-padded string:\t\"%010s\"\n");
1701 wxPrintf(test_format
.c_str(), shortstr
);
1702 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
1703 wxPrintf(test_format
.c_str(), shortstr
);
1704 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
1705 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1706 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
1707 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
1709 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1710 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1711 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1712 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
1713 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1714 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1715 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1716 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1717 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1718 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1719 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1720 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
1722 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1723 wxPrintf (_T(" %6.5f\n"), .1);
1724 wxPrintf (_T("x%5.4fx\n"), .5);
1726 wxPrintf (_T("%#03x\n"), 1);
1728 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1734 while (niter
-- != 0)
1735 wxPrintf (_T("%.17e\n"), d
/ 2);
1740 // Open Watcom cause compiler error here
1741 // Error! E173: col(24) floating-point constant too small to represent
1742 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1745 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1746 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1747 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1748 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1749 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1750 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1751 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1752 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1753 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1754 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1759 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
1761 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1762 rc
, WXSIZEOF(buf
), buf
);
1765 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1766 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1772 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1773 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1774 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1775 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1776 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1777 wxPrintf (_T("%g should be 10\n"), 10.0);
1778 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1782 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1788 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1790 result
|= wxStrcmp (buf
,
1791 _T("onetwo three "));
1793 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
1800 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
1802 // for some reason below line fails under Borland
1803 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1806 if (wxStrcmp (buf
, _T("40000000000")) != 0)
1809 wxPuts (_T("\tFAILED"));
1811 wxUnusedVar(result
);
1812 wxPuts (wxEmptyString
);
1814 #endif // wxLongLong_t
1816 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1817 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1819 wxPuts (_T("--- Should be no further output. ---"));
1828 memset (bytes
, '\xff', sizeof bytes
);
1829 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
1830 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1831 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1833 wxPuts (_T("%hhn overwrite more bytes"));
1838 wxPuts (_T("%hhn wrote incorrect value"));
1850 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
1851 if (wxStrcmp (buf
, _T(" ")) != 0)
1852 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
1853 wxSprintf (buf
, _T("%5.f"), 33.3);
1854 if (wxStrcmp (buf
, _T(" 33")) != 0)
1855 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
1856 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
1857 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
1858 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
1859 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
1860 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
1861 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
1862 wxSprintf (buf
, _T("%.g"), 33.3);
1863 if (wxStrcmp (buf
, _T("3e+01")) != 0)
1864 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
1865 wxSprintf (buf
, _T("%.G"), 33.3);
1866 if (wxStrcmp (buf
, _T("3E+01")) != 0)
1867 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
1875 wxString test_format
;
1878 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
1879 if (wxStrcmp (buf
, _T("3")) != 0)
1880 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1882 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
1883 if (wxStrcmp (buf
, _T("3")) != 0)
1884 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
1886 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
1887 if (wxStrcmp (buf
, _T(" 3")) != 0)
1888 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
1890 test_format
= _T("%04.*o");
1891 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1892 if (wxStrcmp (buf
, _T(" 041")) != 0)
1893 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
1895 test_format
= _T("%09.*u");
1896 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1897 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
1898 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
1900 test_format
= _T("%04.*x");
1901 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1902 if (wxStrcmp (buf
, _T(" 021")) != 0)
1903 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1905 test_format
= _T("%04.*X");
1906 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
1907 if (wxStrcmp (buf
, _T(" 021")) != 0)
1908 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
1911 #endif // TEST_PRINTF
1913 // ----------------------------------------------------------------------------
1914 // registry and related stuff
1915 // ----------------------------------------------------------------------------
1917 // this is for MSW only
1920 #undef TEST_REGISTRY
1925 #include "wx/confbase.h"
1926 #include "wx/msw/regconf.h"
1929 static void TestRegConfWrite()
1931 wxConfig
*config
= new wxConfig(_T("myapp"));
1932 config
->SetPath(_T("/group1"));
1933 config
->Write(_T("entry1"), _T("foo"));
1934 config
->SetPath(_T("/group2"));
1935 config
->Write(_T("entry1"), _T("bar"));
1939 static void TestRegConfRead()
1941 wxConfig
*config
= new wxConfig(_T("myapp"));
1945 config
->SetPath(_T("/"));
1946 wxPuts(_T("Enumerating / subgroups:"));
1947 bool bCont
= config
->GetFirstGroup(str
, dummy
);
1951 bCont
= config
->GetNextGroup(str
, dummy
);
1955 #endif // TEST_REGCONF
1957 #ifdef TEST_REGISTRY
1959 #include "wx/msw/registry.h"
1961 // I chose this one because I liked its name, but it probably only exists under
1963 static const wxChar
*TESTKEY
=
1964 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
1966 static void TestRegistryRead()
1968 wxPuts(_T("*** testing registry reading ***"));
1970 wxRegKey
key(TESTKEY
);
1971 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
1974 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
1979 size_t nSubKeys
, nValues
;
1980 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
1982 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
1985 wxPrintf(_T("Enumerating values:\n"));
1989 bool cont
= key
.GetFirstValue(value
, dummy
);
1992 wxPrintf(_T("Value '%s': type "), value
.c_str());
1993 switch ( key
.GetValueType(value
) )
1995 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
1996 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
1997 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
1998 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
1999 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2000 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2001 default: wxPrintf(_T("other (unknown)")); break;
2004 wxPrintf(_T(", value = "));
2005 if ( key
.IsNumericValue(value
) )
2008 key
.QueryValue(value
, &val
);
2009 wxPrintf(_T("%ld"), val
);
2014 key
.QueryValue(value
, val
);
2015 wxPrintf(_T("'%s'"), val
.c_str());
2017 key
.QueryRawValue(value
, val
);
2018 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
2023 cont
= key
.GetNextValue(value
, dummy
);
2027 static void TestRegistryAssociation()
2030 The second call to deleteself genertaes an error message, with a
2031 messagebox saying .flo is crucial to system operation, while the .ddf
2032 call also fails, but with no error message
2037 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2039 key
= _T("ddxf_auto_file") ;
2040 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2042 key
= _T("ddxf_auto_file") ;
2043 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2045 key
= _T("program,0") ;
2046 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2048 key
= _T("program \"%1\"") ;
2050 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2052 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2054 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2056 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2060 #endif // TEST_REGISTRY
2062 // ----------------------------------------------------------------------------
2064 // ----------------------------------------------------------------------------
2066 #ifdef TEST_SCOPEGUARD
2068 #include "wx/scopeguard.h"
2070 static void function0() { puts("function0()"); }
2071 static void function1(int n
) { printf("function1(%d)\n", n
); }
2072 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2076 void method0() { printf("method0()\n"); }
2077 void method1(int n
) { printf("method1(%d)\n", n
); }
2078 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2081 static void TestScopeGuard()
2083 wxON_BLOCK_EXIT0(function0
);
2084 wxON_BLOCK_EXIT1(function1
, 17);
2085 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2088 wxON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
2089 wxON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
2090 wxON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
2092 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2093 dismissed
.Dismiss();
2098 // ----------------------------------------------------------------------------
2100 // ----------------------------------------------------------------------------
2104 #include "wx/socket.h"
2105 #include "wx/protocol/protocol.h"
2106 #include "wx/protocol/http.h"
2108 static void TestSocketServer()
2110 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2112 static const int PORT
= 3000;
2117 wxSocketServer
*server
= new wxSocketServer(addr
);
2118 if ( !server
->Ok() )
2120 wxPuts(_T("ERROR: failed to bind"));
2128 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
2130 wxSocketBase
*socket
= server
->Accept();
2133 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2137 wxPuts(_T("Server: got a client."));
2139 server
->SetTimeout(60); // 1 min
2142 while ( !close
&& socket
->IsConnected() )
2145 wxChar ch
= _T('\0');
2148 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2150 // don't log error if the client just close the connection
2151 if ( socket
->IsConnected() )
2153 wxPuts(_T("ERROR: in wxSocket::Read."));
2173 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
2174 if ( s
== _T("close") )
2176 wxPuts(_T("Closing connection"));
2180 else if ( s
== _T("quit") )
2185 wxPuts(_T("Shutting down the server"));
2187 else // not a special command
2189 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2190 socket
->Write("\r\n", 2);
2191 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
2197 wxPuts(_T("Server: lost a client unexpectedly."));
2203 // same as "delete server" but is consistent with GUI programs
2207 static void TestSocketClient()
2209 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2211 static const wxChar
*hostname
= _T("www.wxwidgets.org");
2214 addr
.Hostname(hostname
);
2217 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
2219 wxSocketClient client
;
2220 if ( !client
.Connect(addr
) )
2222 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2226 wxPrintf(_T("--- Connected to %s:%u...\n"),
2227 addr
.Hostname().c_str(), addr
.Service());
2231 // could use simply "GET" here I suppose
2233 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
2234 client
.Write(cmdGet
, cmdGet
.length());
2235 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2236 MakePrintable(cmdGet
).c_str());
2237 client
.Read(buf
, WXSIZEOF(buf
));
2238 wxPrintf(_T("--- Server replied:\n%s"), buf
);
2242 #endif // TEST_SOCKETS
2244 // ----------------------------------------------------------------------------
2246 // ----------------------------------------------------------------------------
2250 #include "wx/protocol/ftp.h"
2254 #define FTP_ANONYMOUS
2256 #ifdef FTP_ANONYMOUS
2257 static const wxChar
*directory
= _T("/pub");
2258 static const wxChar
*filename
= _T("welcome.msg");
2260 static const wxChar
*directory
= _T("/etc");
2261 static const wxChar
*filename
= _T("issue");
2264 static bool TestFtpConnect()
2266 wxPuts(_T("*** Testing FTP connect ***"));
2268 #ifdef FTP_ANONYMOUS
2269 static const wxChar
*hostname
= _T("ftp.wxwidgets.org");
2271 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2272 #else // !FTP_ANONYMOUS
2273 static const wxChar
*hostname
= "localhost";
2276 wxFgets(user
, WXSIZEOF(user
), stdin
);
2277 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2280 wxChar password
[256];
2281 wxPrintf(_T("Password for %s: "), password
);
2282 wxFgets(password
, WXSIZEOF(password
), stdin
);
2283 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2284 ftp
.SetPassword(password
);
2286 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2287 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2289 if ( !ftp
.Connect(hostname
) )
2291 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2297 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2298 hostname
, ftp
.Pwd().c_str());
2305 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2306 static void TestFtpWuFtpd()
2309 static const wxChar
*hostname
= _T("ftp.eudora.com");
2310 if ( !ftp
.Connect(hostname
) )
2312 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
2316 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
2317 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2320 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2324 size_t size
= in
->GetSize();
2325 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2327 wxChar
*data
= new wxChar
[size
];
2328 if ( !in
->Read(data
, size
) )
2330 wxPuts(_T("ERROR: read error"));
2334 wxPrintf(_T("Successfully retrieved the file.\n"));
2343 static void TestFtpList()
2345 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2348 if ( !ftp
.ChDir(directory
) )
2350 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2353 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2355 // test NLIST and LIST
2356 wxArrayString files
;
2357 if ( !ftp
.GetFilesList(files
) )
2359 wxPuts(_T("ERROR: failed to get NLIST of files"));
2363 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
2364 size_t count
= files
.GetCount();
2365 for ( size_t n
= 0; n
< count
; n
++ )
2367 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2369 wxPuts(_T("End of the file list"));
2372 if ( !ftp
.GetDirList(files
) )
2374 wxPuts(_T("ERROR: failed to get LIST of files"));
2378 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
2379 size_t count
= files
.GetCount();
2380 for ( size_t n
= 0; n
< count
; n
++ )
2382 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2384 wxPuts(_T("End of the file list"));
2387 if ( !ftp
.ChDir(_T("..")) )
2389 wxPuts(_T("ERROR: failed to cd to .."));
2392 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2395 static void TestFtpDownload()
2397 wxPuts(_T("*** Testing wxFTP download ***\n"));
2400 wxInputStream
*in
= ftp
.GetInputStream(filename
);
2403 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
2407 size_t size
= in
->GetSize();
2408 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
2411 wxChar
*data
= new wxChar
[size
];
2412 if ( !in
->Read(data
, size
) )
2414 wxPuts(_T("ERROR: read error"));
2418 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
2426 static void TestFtpFileSize()
2428 wxPuts(_T("*** Testing FTP SIZE command ***"));
2430 if ( !ftp
.ChDir(directory
) )
2432 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
2435 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
2437 if ( ftp
.FileExists(filename
) )
2439 int size
= ftp
.GetFileSize(filename
);
2441 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
2443 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
2447 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
2451 static void TestFtpMisc()
2453 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2455 if ( ftp
.SendCommand(_T("STAT")) != '2' )
2457 wxPuts(_T("ERROR: STAT failed"));
2461 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
2464 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
2466 wxPuts(_T("ERROR: HELP SITE failed"));
2470 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2471 ftp
.GetLastResult().c_str());
2475 static void TestFtpInteractive()
2477 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2483 wxPrintf(_T("Enter FTP command: "));
2484 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2487 // kill the last '\n'
2488 buf
[wxStrlen(buf
) - 1] = 0;
2490 // special handling of LIST and NLST as they require data connection
2491 wxString
start(buf
, 4);
2493 if ( start
== _T("LIST") || start
== _T("NLST") )
2496 if ( wxStrlen(buf
) > 4 )
2499 wxArrayString files
;
2500 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
2502 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
2506 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2507 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
2508 size_t count
= files
.GetCount();
2509 for ( size_t n
= 0; n
< count
; n
++ )
2511 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
2513 wxPuts(_T("--- End of the file list"));
2518 wxChar ch
= ftp
.SendCommand(buf
);
2519 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
2522 wxPrintf(_T(" (return code %c)"), ch
);
2525 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
2529 wxPuts(_T("\n*** done ***"));
2532 static void TestFtpUpload()
2534 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2537 static const wxChar
*file1
= _T("test1");
2538 static const wxChar
*file2
= _T("test2");
2539 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
2542 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2543 out
->Write("First hello", 11);
2547 // send a command to check the remote file
2548 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
2550 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
2554 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2555 file1
, ftp
.GetLastResult().c_str());
2558 out
= ftp
.GetOutputStream(file2
);
2561 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
2562 out
->Write("Second hello", 12);
2569 // ----------------------------------------------------------------------------
2571 // ----------------------------------------------------------------------------
2573 #ifdef TEST_STACKWALKER
2575 #if wxUSE_STACKWALKER
2577 #include "wx/stackwalk.h"
2579 class StackDump
: public wxStackWalker
2582 StackDump(const char *argv0
)
2583 : wxStackWalker(argv0
)
2587 virtual void Walk(size_t skip
= 1)
2589 wxPuts(_T("Stack dump:"));
2591 wxStackWalker::Walk(skip
);
2595 virtual void OnStackFrame(const wxStackFrame
& frame
)
2597 printf("[%2d] ", frame
.GetLevel());
2599 wxString name
= frame
.GetName();
2600 if ( !name
.empty() )
2602 printf("%-20.40s", name
.mb_str());
2606 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2609 if ( frame
.HasSourceLocation() )
2612 frame
.GetFileName().mb_str(),
2619 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2621 printf("\t%s %s = %s\n", type
.mb_str(), name
.mb_str(), val
.mb_str());
2626 static void TestStackWalk(const char *argv0
)
2628 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2630 StackDump
dump(argv0
);
2634 #endif // wxUSE_STACKWALKER
2636 #endif // TEST_STACKWALKER
2638 // ----------------------------------------------------------------------------
2640 // ----------------------------------------------------------------------------
2642 #ifdef TEST_STDPATHS
2644 #include "wx/stdpaths.h"
2646 static void TestStandardPaths()
2648 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2650 wxTheApp
->SetAppName(_T("console"));
2652 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2653 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2654 wxPrintf(_T("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2655 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2656 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2657 wxPrintf(_T("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2658 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2659 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2662 #endif // TEST_STDPATHS
2664 // ----------------------------------------------------------------------------
2666 // ----------------------------------------------------------------------------
2670 #include "wx/wfstream.h"
2671 #include "wx/mstream.h"
2673 static void TestFileStream()
2675 wxPuts(_T("*** Testing wxFileInputStream ***"));
2677 static const wxString filename
= _T("testdata.fs");
2679 wxFileOutputStream
fsOut(filename
);
2680 fsOut
.Write("foo", 3);
2683 wxFileInputStream
fsIn(filename
);
2684 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
2685 while ( !fsIn
.Eof() )
2687 wxPutchar(fsIn
.GetC());
2690 if ( !wxRemoveFile(filename
) )
2692 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2695 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2698 static void TestMemoryStream()
2700 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2702 wxMemoryOutputStream memOutStream
;
2703 wxPrintf(_T("Initially out stream offset: %lu\n"),
2704 (unsigned long)memOutStream
.TellO());
2706 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
2708 memOutStream
.PutC(*p
);
2711 wxPrintf(_T("Final out stream offset: %lu\n"),
2712 (unsigned long)memOutStream
.TellO());
2714 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2717 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2719 wxMemoryInputStream
memInpStream(buf
, len
);
2720 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
2721 while ( !memInpStream
.Eof() )
2723 wxPutchar(memInpStream
.GetC());
2726 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2729 #endif // TEST_STREAMS
2731 // ----------------------------------------------------------------------------
2733 // ----------------------------------------------------------------------------
2737 #include "wx/stopwatch.h"
2738 #include "wx/utils.h"
2740 static void TestStopWatch()
2742 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2746 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2749 wxPrintf(_T("\t%ldms\n"), sw
.Time());
2751 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2755 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2758 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2761 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2764 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2767 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
2770 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2771 for ( size_t n
= 0; n
< 70; n
++ )
2775 for ( size_t m
= 0; m
< 100000; m
++ )
2777 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2779 wxPuts(_T("\ntime is negative - ERROR!"));
2787 wxPuts(_T(", ok."));
2790 #endif // TEST_TIMER
2792 // ----------------------------------------------------------------------------
2794 // ----------------------------------------------------------------------------
2798 #include "wx/vcard.h"
2800 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
2803 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
2806 wxPrintf(_T("%s%s"),
2807 wxString(_T('\t'), level
).c_str(),
2808 vcObj
->GetName().c_str());
2811 switch ( vcObj
->GetType() )
2813 case wxVCardObject::String
:
2814 case wxVCardObject::UString
:
2817 vcObj
->GetValue(&val
);
2818 value
<< _T('"') << val
<< _T('"');
2822 case wxVCardObject::Int
:
2825 vcObj
->GetValue(&i
);
2826 value
.Printf(_T("%u"), i
);
2830 case wxVCardObject::Long
:
2833 vcObj
->GetValue(&l
);
2834 value
.Printf(_T("%lu"), l
);
2838 case wxVCardObject::None
:
2841 case wxVCardObject::Object
:
2842 value
= _T("<node>");
2846 value
= _T("<unknown value type>");
2850 wxPrintf(_T(" = %s"), value
.c_str());
2853 DumpVObject(level
+ 1, *vcObj
);
2856 vcObj
= vcard
.GetNextProp(&cookie
);
2860 static void DumpVCardAddresses(const wxVCard
& vcard
)
2862 wxPuts(_T("\nShowing all addresses from vCard:\n"));
2866 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
2870 int flags
= addr
->GetFlags();
2871 if ( flags
& wxVCardAddress::Domestic
)
2873 flagsStr
<< _T("domestic ");
2875 if ( flags
& wxVCardAddress::Intl
)
2877 flagsStr
<< _T("international ");
2879 if ( flags
& wxVCardAddress::Postal
)
2881 flagsStr
<< _T("postal ");
2883 if ( flags
& wxVCardAddress::Parcel
)
2885 flagsStr
<< _T("parcel ");
2887 if ( flags
& wxVCardAddress::Home
)
2889 flagsStr
<< _T("home ");
2891 if ( flags
& wxVCardAddress::Work
)
2893 flagsStr
<< _T("work ");
2896 wxPrintf(_T("Address %u:\n")
2898 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
2901 addr
->GetPostOffice().c_str(),
2902 addr
->GetExtAddress().c_str(),
2903 addr
->GetStreet().c_str(),
2904 addr
->GetLocality().c_str(),
2905 addr
->GetRegion().c_str(),
2906 addr
->GetPostalCode().c_str(),
2907 addr
->GetCountry().c_str()
2911 addr
= vcard
.GetNextAddress(&cookie
);
2915 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
2917 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
2921 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
2925 int flags
= phone
->GetFlags();
2926 if ( flags
& wxVCardPhoneNumber::Voice
)
2928 flagsStr
<< _T("voice ");
2930 if ( flags
& wxVCardPhoneNumber::Fax
)
2932 flagsStr
<< _T("fax ");
2934 if ( flags
& wxVCardPhoneNumber::Cellular
)
2936 flagsStr
<< _T("cellular ");
2938 if ( flags
& wxVCardPhoneNumber::Modem
)
2940 flagsStr
<< _T("modem ");
2942 if ( flags
& wxVCardPhoneNumber::Home
)
2944 flagsStr
<< _T("home ");
2946 if ( flags
& wxVCardPhoneNumber::Work
)
2948 flagsStr
<< _T("work ");
2951 wxPrintf(_T("Phone number %u:\n")
2956 phone
->GetNumber().c_str()
2960 phone
= vcard
.GetNextPhoneNumber(&cookie
);
2964 static void TestVCardRead()
2966 wxPuts(_T("*** Testing wxVCard reading ***\n"));
2968 wxVCard
vcard(_T("vcard.vcf"));
2969 if ( !vcard
.IsOk() )
2971 wxPuts(_T("ERROR: couldn't load vCard."));
2975 // read individual vCard properties
2976 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
2980 vcObj
->GetValue(&value
);
2985 value
= _T("<none>");
2988 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
2991 if ( !vcard
.GetFullName(&value
) )
2993 value
= _T("<none>");
2996 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
2998 // now show how to deal with multiply occuring properties
2999 DumpVCardAddresses(vcard
);
3000 DumpVCardPhoneNumbers(vcard
);
3002 // and finally show all
3003 wxPuts(_T("\nNow dumping the entire vCard:\n")
3004 "-----------------------------\n");
3006 DumpVObject(0, vcard
);
3010 static void TestVCardWrite()
3012 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3015 if ( !vcard
.IsOk() )
3017 wxPuts(_T("ERROR: couldn't create vCard."));
3022 vcard
.SetName("Zeitlin", "Vadim");
3023 vcard
.SetFullName("Vadim Zeitlin");
3024 vcard
.SetOrganization("wxWidgets", "R&D");
3026 // just dump the vCard back
3027 wxPuts(_T("Entire vCard follows:\n"));
3028 wxPuts(vcard
.Write());
3032 #endif // TEST_VCARD
3034 // ----------------------------------------------------------------------------
3036 // ----------------------------------------------------------------------------
3038 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3044 #include "wx/volume.h"
3046 static const wxChar
*volumeKinds
[] =
3052 _T("network volume"),
3056 static void TestFSVolume()
3058 wxPuts(_T("*** Testing wxFSVolume class ***"));
3060 wxArrayString volumes
= wxFSVolume::GetVolumes();
3061 size_t count
= volumes
.GetCount();
3065 wxPuts(_T("ERROR: no mounted volumes?"));
3069 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3071 for ( size_t n
= 0; n
< count
; n
++ )
3073 wxFSVolume
vol(volumes
[n
]);
3076 wxPuts(_T("ERROR: couldn't create volume"));
3080 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3082 vol
.GetDisplayName().c_str(),
3083 vol
.GetName().c_str(),
3084 volumeKinds
[vol
.GetKind()],
3085 vol
.IsWritable() ? _T("rw") : _T("ro"),
3086 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3091 #endif // TEST_VOLUME
3093 // ----------------------------------------------------------------------------
3094 // wide char and Unicode support
3095 // ----------------------------------------------------------------------------
3099 #include "wx/strconv.h"
3100 #include "wx/fontenc.h"
3101 #include "wx/encconv.h"
3102 #include "wx/buffer.h"
3104 static const unsigned char utf8koi8r
[] =
3106 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3107 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3108 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3109 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3110 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3111 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3112 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3115 static const unsigned char utf8iso8859_1
[] =
3117 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3118 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3119 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3120 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3121 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3124 static const unsigned char utf8Invalid
[] =
3126 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3127 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3128 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3129 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3133 static const struct Utf8Data
3135 const unsigned char *text
;
3137 const wxChar
*charset
;
3138 wxFontEncoding encoding
;
3141 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3142 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
3143 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
3146 static void TestUtf8()
3148 wxPuts(_T("*** Testing UTF8 support ***\n"));
3153 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
3155 const Utf8Data
& u8d
= utf8data
[n
];
3156 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
3157 WXSIZEOF(wbuf
)) == (size_t)-1 )
3159 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3163 wxCSConv
conv(u8d
.charset
);
3164 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
3166 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
3170 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
3174 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
3176 s
= _T("<< conversion failed >>");
3177 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
3181 wxPuts(wxEmptyString
);
3184 static void TestEncodingConverter()
3186 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3188 // using wxEncodingConverter should give the same result as above
3191 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
3192 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
3194 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3198 wxEncodingConverter ec
;
3199 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
3200 ec
.Convert(wbuf
, buf
);
3201 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
3204 wxPuts(wxEmptyString
);
3207 #endif // TEST_WCHAR
3209 // ----------------------------------------------------------------------------
3211 // ----------------------------------------------------------------------------
3215 #include "wx/filesys.h"
3216 #include "wx/fs_zip.h"
3217 #include "wx/zipstrm.h"
3219 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
3221 static void TestZipStreamRead()
3223 wxPuts(_T("*** Testing ZIP reading ***\n"));
3225 static const wxString filename
= _T("foo");
3226 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
3227 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
3229 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
3230 while ( !istr
.Eof() )
3232 wxPutchar(istr
.GetC());
3236 wxPuts(_T("\n----- done ------"));
3239 static void DumpZipDirectory(wxFileSystem
& fs
,
3240 const wxString
& dir
,
3241 const wxString
& indent
)
3243 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
3244 TESTFILE_ZIP
, dir
.c_str());
3245 wxString wildcard
= prefix
+ _T("/*");
3247 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
3248 while ( !dirname
.empty() )
3250 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
3252 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3257 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
3259 DumpZipDirectory(fs
, dirname
,
3260 indent
+ wxString(_T(' '), 4));
3262 dirname
= fs
.FindNext();
3265 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
3266 while ( !filename
.empty() )
3268 if ( !filename
.StartsWith(prefix
, &filename
) )
3270 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3275 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
3277 filename
= fs
.FindNext();
3281 static void TestZipFileSystem()
3283 wxPuts(_T("*** Testing ZIP file system ***\n"));
3285 wxFileSystem::AddHandler(new wxZipFSHandler
);
3287 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
3289 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
3294 // ----------------------------------------------------------------------------
3296 // ----------------------------------------------------------------------------
3298 #ifdef TEST_DATETIME
3300 #include "wx/math.h"
3301 #include "wx/datetime.h"
3303 // this test miscellaneous static wxDateTime functions
3307 static void TestTimeStatic()
3309 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3311 // some info about the current date
3312 int year
= wxDateTime::GetCurrentYear();
3313 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3315 wxDateTime::IsLeapYear(year
) ? "" : "not ",
3316 wxDateTime::GetNumberOfDays(year
));
3318 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
3319 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3320 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
3321 wxDateTime::GetMonthName(month
).c_str(),
3322 wxDateTime::GetNumberOfDays(month
));
3325 // test time zones stuff
3326 static void TestTimeZones()
3328 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3330 wxDateTime now
= wxDateTime::Now();
3332 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
3333 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
3334 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
3335 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
3336 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
3337 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
3339 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3341 wxDateTime::Tm tm
= now
.GetTm();
3342 if ( wxDateTime(tm
) != now
)
3344 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3345 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
3349 // test some minimal support for the dates outside the standard range
3350 static void TestTimeRange()
3352 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3354 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
3356 wxPrintf(_T("Unix epoch:\t%s\n"),
3357 wxDateTime(2440587.5).Format(fmt
).c_str());
3358 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3359 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
3360 wxPrintf(_T("JDN 0: \t%s\n"),
3361 wxDateTime(0.0).Format(fmt
).c_str());
3362 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3363 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
3364 wxPrintf(_T("May 29, 2099:\t%s\n"),
3365 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
3368 // test DST calculations
3369 static void TestTimeDST()
3371 wxPuts(_T("\n*** wxDateTime DST test ***"));
3373 wxPrintf(_T("DST is%s in effect now.\n\n"),
3374 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
3376 for ( int year
= 1990; year
< 2005; year
++ )
3378 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3380 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
3381 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
3387 #if TEST_INTERACTIVE
3389 static void TestDateTimeInteractive()
3391 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3397 wxPrintf(_T("Enter a date: "));
3398 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3401 // kill the last '\n'
3402 buf
[wxStrlen(buf
) - 1] = 0;
3405 const wxChar
*p
= dt
.ParseDate(buf
);
3408 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
3414 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
3417 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3418 dt
.Format(_T("%b %d, %Y")).c_str(),
3420 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3421 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3422 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3425 wxPuts(_T("\n*** done ***"));
3428 #endif // TEST_INTERACTIVE
3432 static void TestTimeMS()
3434 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3436 wxDateTime dt1
= wxDateTime::Now(),
3437 dt2
= wxDateTime::UNow();
3439 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
3440 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3441 wxPrintf(_T("Dummy loop: "));
3442 for ( int i
= 0; i
< 6000; i
++ )
3444 //for ( int j = 0; j < 10; j++ )
3447 s
.Printf(_T("%g"), sqrt((float)i
));
3453 wxPuts(_T(", done"));
3456 dt2
= wxDateTime::UNow();
3457 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
3459 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
3461 wxPuts(_T("\n*** done ***"));
3464 static void TestTimeHolidays()
3466 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3468 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
3469 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
3470 dtEnd
= dtStart
.GetLastMonthDay();
3472 wxDateTimeArray hol
;
3473 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
3475 const wxChar
*format
= _T("%d-%b-%Y (%a)");
3477 wxPrintf(_T("All holidays between %s and %s:\n"),
3478 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
3480 size_t count
= hol
.GetCount();
3481 for ( size_t n
= 0; n
< count
; n
++ )
3483 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
3486 wxPuts(wxEmptyString
);
3489 static void TestTimeZoneBug()
3491 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3493 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
3494 for ( int i
= 0; i
< 31; i
++ )
3496 wxPrintf(_T("Date %s: week day %s.\n"),
3497 date
.Format(_T("%d-%m-%Y")).c_str(),
3498 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
3500 date
+= wxDateSpan::Day();
3503 wxPuts(wxEmptyString
);
3506 static void TestTimeSpanFormat()
3508 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3510 static const wxChar
*formats
[] =
3512 _T("(default) %H:%M:%S"),
3513 _T("%E weeks and %D days"),
3514 _T("%l milliseconds"),
3515 _T("(with ms) %H:%M:%S:%l"),
3516 _T("100%% of minutes is %M"), // test "%%"
3517 _T("%D days and %H hours"),
3518 _T("or also %S seconds"),
3521 wxTimeSpan
ts1(1, 2, 3, 4),
3523 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
3525 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3526 ts1
.Format(formats
[n
]).c_str(),
3527 ts2
.Format(formats
[n
]).c_str());
3530 wxPuts(wxEmptyString
);
3535 #endif // TEST_DATETIME
3537 // ----------------------------------------------------------------------------
3538 // wxTextInput/OutputStream
3539 // ----------------------------------------------------------------------------
3541 #ifdef TEST_TEXTSTREAM
3543 #include "wx/txtstrm.h"
3544 #include "wx/wfstream.h"
3546 static void TestTextInputStream()
3548 wxPuts(_T("\n*** wxTextInputStream test ***"));
3550 wxString filename
= _T("testdata.fc");
3551 wxFileInputStream
fsIn(filename
);
3554 wxPuts(_T("ERROR: couldn't open file."));
3558 wxTextInputStream
tis(fsIn
);
3563 const wxString s
= tis
.ReadLine();
3565 // line could be non empty if the last line of the file isn't
3566 // terminated with EOL
3567 if ( fsIn
.Eof() && s
.empty() )
3570 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
3575 #endif // TEST_TEXTSTREAM
3577 // ----------------------------------------------------------------------------
3579 // ----------------------------------------------------------------------------
3583 #include "wx/thread.h"
3585 static size_t gs_counter
= (size_t)-1;
3586 static wxCriticalSection gs_critsect
;
3587 static wxSemaphore gs_cond
;
3589 class MyJoinableThread
: public wxThread
3592 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
3593 { m_n
= n
; Create(); }
3595 // thread execution starts here
3596 virtual ExitCode
Entry();
3602 wxThread::ExitCode
MyJoinableThread::Entry()
3604 unsigned long res
= 1;
3605 for ( size_t n
= 1; n
< m_n
; n
++ )
3609 // it's a loooong calculation :-)
3613 return (ExitCode
)res
;
3616 class MyDetachedThread
: public wxThread
3619 MyDetachedThread(size_t n
, wxChar ch
)
3623 m_cancelled
= false;
3628 // thread execution starts here
3629 virtual ExitCode
Entry();
3632 virtual void OnExit();
3635 size_t m_n
; // number of characters to write
3636 wxChar m_ch
; // character to write
3638 bool m_cancelled
; // false if we exit normally
3641 wxThread::ExitCode
MyDetachedThread::Entry()
3644 wxCriticalSectionLocker
lock(gs_critsect
);
3645 if ( gs_counter
== (size_t)-1 )
3651 for ( size_t n
= 0; n
< m_n
; n
++ )
3653 if ( TestDestroy() )
3663 wxThread::Sleep(100);
3669 void MyDetachedThread::OnExit()
3671 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3673 wxCriticalSectionLocker
lock(gs_critsect
);
3674 if ( !--gs_counter
&& !m_cancelled
)
3678 static void TestDetachedThreads()
3680 wxPuts(_T("\n*** Testing detached threads ***"));
3682 static const size_t nThreads
= 3;
3683 MyDetachedThread
*threads
[nThreads
];
3685 for ( n
= 0; n
< nThreads
; n
++ )
3687 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
3690 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
3691 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
3693 for ( n
= 0; n
< nThreads
; n
++ )
3698 // wait until all threads terminate
3701 wxPuts(wxEmptyString
);
3704 static void TestJoinableThreads()
3706 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3708 // calc 10! in the background
3709 MyJoinableThread
thread(10);
3712 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3713 (unsigned long)thread
.Wait());
3716 static void TestThreadSuspend()
3718 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3720 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
3724 // this is for this demo only, in a real life program we'd use another
3725 // condition variable which would be signaled from wxThread::Entry() to
3726 // tell us that the thread really started running - but here just wait a
3727 // bit and hope that it will be enough (the problem is, of course, that
3728 // the thread might still not run when we call Pause() which will result
3730 wxThread::Sleep(300);
3732 for ( size_t n
= 0; n
< 3; n
++ )
3736 wxPuts(_T("\nThread suspended"));
3739 // don't sleep but resume immediately the first time
3740 wxThread::Sleep(300);
3742 wxPuts(_T("Going to resume the thread"));
3747 wxPuts(_T("Waiting until it terminates now"));
3749 // wait until the thread terminates
3752 wxPuts(wxEmptyString
);
3755 static void TestThreadDelete()
3757 // As above, using Sleep() is only for testing here - we must use some
3758 // synchronisation object instead to ensure that the thread is still
3759 // running when we delete it - deleting a detached thread which already
3760 // terminated will lead to a crash!
3762 wxPuts(_T("\n*** Testing thread delete function ***"));
3764 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
3768 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3770 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
3774 wxThread::Sleep(300);
3778 wxPuts(_T("\nDeleted a running thread."));
3780 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
3784 wxThread::Sleep(300);
3790 wxPuts(_T("\nDeleted a sleeping thread."));
3792 MyJoinableThread
thread3(20);
3797 wxPuts(_T("\nDeleted a joinable thread."));
3799 MyJoinableThread
thread4(2);
3802 wxThread::Sleep(300);
3806 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
3808 wxPuts(wxEmptyString
);
3811 class MyWaitingThread
: public wxThread
3814 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
3817 m_condition
= condition
;
3822 virtual ExitCode
Entry()
3824 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
3829 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
3833 m_condition
->Wait();
3836 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
3844 wxCondition
*m_condition
;
3847 static void TestThreadConditions()
3850 wxCondition
condition(mutex
);
3852 // otherwise its difficult to understand which log messages pertain to
3854 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
3855 // condition.GetId(), gs_cond.GetId());
3857 // create and launch threads
3858 MyWaitingThread
*threads
[10];
3861 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3863 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
3866 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
3871 // wait until all threads run
3872 wxPuts(_T("Main thread is waiting for the other threads to start"));
3875 size_t nRunning
= 0;
3876 while ( nRunning
< WXSIZEOF(threads
) )
3882 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
3886 wxPuts(_T("Main thread: all threads started up."));
3889 wxThread::Sleep(500);
3892 // now wake one of them up
3893 wxPrintf(_T("Main thread: about to signal the condition.\n"));
3898 wxThread::Sleep(200);
3900 // wake all the (remaining) threads up, so that they can exit
3901 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
3903 condition
.Broadcast();
3905 // give them time to terminate (dirty!)
3906 wxThread::Sleep(500);
3909 #include "wx/utils.h"
3911 class MyExecThread
: public wxThread
3914 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
3920 virtual ExitCode
Entry()
3922 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
3929 static void TestThreadExec()
3931 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
3933 MyExecThread
thread(_T("true"));
3936 wxPrintf(_T("Main program exit code: %ld.\n"),
3937 wxExecute(_T("false"), wxEXEC_SYNC
));
3939 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
3943 #include "wx/datetime.h"
3945 class MySemaphoreThread
: public wxThread
3948 MySemaphoreThread(int i
, wxSemaphore
*sem
)
3949 : wxThread(wxTHREAD_JOINABLE
),
3956 virtual ExitCode
Entry()
3958 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
3959 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3963 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
3964 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3968 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
3969 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
3981 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
3983 static void TestSemaphore()
3985 wxPuts(_T("*** Testing wxSemaphore class. ***"));
3987 static const int SEM_LIMIT
= 3;
3989 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
3990 ArrayThreads threads
;
3992 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
3994 threads
.Add(new MySemaphoreThread(i
, &sem
));
3995 threads
.Last()->Run();
3998 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
4005 #endif // TEST_THREADS
4007 // ----------------------------------------------------------------------------
4009 // ----------------------------------------------------------------------------
4011 #ifdef TEST_SNGLINST
4012 #include "wx/snglinst.h"
4013 #endif // TEST_SNGLINST
4015 int main(int argc
, char **argv
)
4018 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
4023 for (n
= 0; n
< argc
; n
++ )
4025 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
4026 wxArgv
[n
] = wxStrdup(warg
);
4031 #else // !wxUSE_UNICODE
4033 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4035 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
4037 wxInitializer initializer
;
4040 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
4045 #ifdef TEST_SNGLINST
4046 wxSingleInstanceChecker checker
;
4047 if ( checker
.Create(_T(".wxconsole.lock")) )
4049 if ( checker
.IsAnotherRunning() )
4051 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4056 // wait some time to give time to launch another instance
4057 wxPrintf(_T("Press \"Enter\" to continue..."));
4060 else // failed to create
4062 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4064 #endif // TEST_SNGLINST
4067 TestCmdLineConvert();
4069 #if wxUSE_CMDLINE_PARSER
4070 static const wxCmdLineEntryDesc cmdLineDesc
[] =
4072 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
4073 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
4074 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
4075 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
4077 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
4078 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
4079 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
4080 wxCMD_LINE_VAL_NUMBER
},
4081 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
4082 wxCMD_LINE_VAL_DATE
},
4084 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
4085 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
4090 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
4092 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4093 wxCMD_LINE_VAL_STRING
,
4094 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
4096 switch ( parser
.Parse() )
4099 wxLogMessage(_T("Help was given, terminating."));
4103 ShowCmdLine(parser
);
4107 wxLogMessage(_T("Syntax error detected, aborting."));
4110 #endif // wxUSE_CMDLINE_PARSER
4112 #endif // TEST_CMDLINE
4122 #ifdef TEST_DLLLOADER
4124 TestDllListLoaded();
4125 #endif // TEST_DLLLOADER
4129 #endif // TEST_ENVIRON
4133 #endif // TEST_EXECUTE
4135 #ifdef TEST_FILECONF
4137 #endif // TEST_FILECONF
4141 #endif // TEST_LOCALE
4144 wxPuts(_T("*** Testing wxLog ***"));
4147 for ( size_t n
= 0; n
< 8000; n
++ )
4149 s
<< (wxChar
)(_T('A') + (n
% 26));
4152 wxLogWarning(_T("The length of the string is %lu"),
4153 (unsigned long)s
.length());
4156 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
4158 // this one shouldn't be truncated
4161 // but this one will because log functions use fixed size buffer
4162 // (note that it doesn't need '\n' at the end neither - will be added
4164 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
4173 #ifdef TEST_FILENAME
4176 TestFileNameDirManip();
4177 TestFileNameComparison();
4178 TestFileNameOperations();
4179 #endif // TEST_FILENAME
4181 #ifdef TEST_FILETIME
4186 #endif // TEST_FILETIME
4189 wxLog::AddTraceMask(FTP_TRACE_MASK
);
4190 if ( TestFtpConnect() )
4200 #if TEST_INTERACTIVE
4201 TestFtpInteractive();
4204 //else: connecting to the FTP server failed
4212 wxLog::AddTraceMask(_T("mime"));
4216 TestMimeAssociate();
4221 #ifdef TEST_INFO_FUNCTIONS
4226 #if TEST_INTERACTIVE
4230 #endif // TEST_INFO_FUNCTIONS
4232 #ifdef TEST_PATHLIST
4234 #endif // TEST_PATHLIST
4242 #endif // TEST_PRINTF
4249 #endif // TEST_REGCONF
4251 #if defined TEST_REGEX && TEST_INTERACTIVE
4252 TestRegExInteractive();
4253 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4255 #ifdef TEST_REGISTRY
4257 TestRegistryAssociation();
4258 #endif // TEST_REGISTRY
4263 #endif // TEST_SOCKETS
4270 #endif // TEST_STREAMS
4272 #ifdef TEST_TEXTSTREAM
4273 TestTextInputStream();
4274 #endif // TEST_TEXTSTREAM
4277 int nCPUs
= wxThread::GetCPUCount();
4278 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
4280 wxThread::SetConcurrency(nCPUs
);
4282 TestJoinableThreads();
4285 TestJoinableThreads();
4286 TestDetachedThreads();
4287 TestThreadSuspend();
4289 TestThreadConditions();
4293 #endif // TEST_THREADS
4297 #endif // TEST_TIMER
4299 #ifdef TEST_DATETIME
4306 TestTimeSpanFormat();
4312 #if TEST_INTERACTIVE
4313 TestDateTimeInteractive();
4315 #endif // TEST_DATETIME
4317 #ifdef TEST_SCOPEGUARD
4321 #ifdef TEST_STACKWALKER
4322 #if wxUSE_STACKWALKER
4323 TestStackWalk(argv
[0]);
4325 #endif // TEST_STACKWALKER
4327 #ifdef TEST_STDPATHS
4328 TestStandardPaths();
4332 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4334 #endif // TEST_USLEEP
4339 #endif // TEST_VCARD
4343 #endif // TEST_VOLUME
4347 TestEncodingConverter();
4348 #endif // TEST_WCHAR
4351 TestZipStreamRead();
4352 TestZipFileSystem();
4357 for ( int n
= 0; n
< argc
; n
++ )
4362 #endif // wxUSE_UNICODE