1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
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 wxWindows 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.
58 #define TEST_DLLLOADER
65 // #define TEST_FTP --FIXME! (RN)
69 #define TEST_INFO_FUNCTIONS
80 #define TEST_SCOPEGUARD
82 // #define TEST_SOCKETS --FIXME! (RN)
85 #define TEST_TEXTSTREAM
89 // #define TEST_VCARD -- don't enable this (VZ)
90 // #define TEST_VOLUME --FIXME! (RN)
102 // some tests are interactive, define this to run them
103 #ifdef TEST_INTERACTIVE
104 #undef TEST_INTERACTIVE
106 #define TEST_INTERACTIVE 1
108 #define TEST_INTERACTIVE 0
111 // ----------------------------------------------------------------------------
112 // test class for container objects
113 // ----------------------------------------------------------------------------
115 #if defined(TEST_LIST)
117 class Bar
// Foo is already taken in the hash test
120 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
121 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
122 ~Bar() { ms_bars
--; }
124 static size_t GetNumber() { return ms_bars
; }
126 const wxChar
*GetName() const { return m_name
; }
131 static size_t ms_bars
;
134 size_t Bar::ms_bars
= 0;
136 #endif // defined(TEST_LIST)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
148 // replace TABs with \t and CRs with \n
149 static wxString
MakePrintable(const wxChar
*s
)
152 (void)str
.Replace(_T("\t"), _T("\\t"));
153 (void)str
.Replace(_T("\n"), _T("\\n"));
154 (void)str
.Replace(_T("\r"), _T("\\r"));
159 #endif // MakePrintable() is used
161 // ----------------------------------------------------------------------------
162 // wxFontMapper::CharsetToEncoding
163 // ----------------------------------------------------------------------------
167 #include "wx/fontmap.h"
169 static void TestCharset()
171 static const wxChar
*charsets
[] =
173 // some vali charsets
182 // and now some bogus ones
189 for ( size_t n
= 0; n
< WXSIZEOF(charsets
); n
++ )
191 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charsets
[n
]);
192 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
194 wxFontMapper::Get()->GetEncodingName(enc
).c_str(),
195 wxFontMapper::Get()->GetEncodingDescription(enc
).c_str());
199 #endif // TEST_CHARSET
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
207 #include "wx/cmdline.h"
208 #include "wx/datetime.h"
210 #if wxUSE_CMDLINE_PARSER
212 static void ShowCmdLine(const wxCmdLineParser
& parser
)
214 wxString s
= _T("Input files: ");
216 size_t count
= parser
.GetParamCount();
217 for ( size_t param
= 0; param
< count
; param
++ )
219 s
<< parser
.GetParam(param
) << ' ';
223 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
224 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
229 if ( parser
.Found(_T("o"), &strVal
) )
230 s
<< _T("Output file:\t") << strVal
<< '\n';
231 if ( parser
.Found(_T("i"), &strVal
) )
232 s
<< _T("Input dir:\t") << strVal
<< '\n';
233 if ( parser
.Found(_T("s"), &lVal
) )
234 s
<< _T("Size:\t") << lVal
<< '\n';
235 if ( parser
.Found(_T("d"), &dt
) )
236 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
237 if ( parser
.Found(_T("project_name"), &strVal
) )
238 s
<< _T("Project:\t") << strVal
<< '\n';
243 #endif // wxUSE_CMDLINE_PARSER
245 static void TestCmdLineConvert()
247 static const wxChar
*cmdlines
[] =
250 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
251 _T("literal \\\" and \"\""),
254 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
256 const wxChar
*cmdline
= cmdlines
[n
];
257 wxPrintf(_T("Parsing: %s\n"), cmdline
);
258 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
260 size_t count
= args
.GetCount();
261 wxPrintf(_T("\targc = %u\n"), count
);
262 for ( size_t arg
= 0; arg
< count
; arg
++ )
264 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
269 #endif // TEST_CMDLINE
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
280 static const wxChar
*ROOTDIR
= _T("/");
281 static const wxChar
*TESTDIR
= _T("/usr/local/share");
282 #elif defined(__WXMSW__)
283 static const wxChar
*ROOTDIR
= _T("c:\\");
284 static const wxChar
*TESTDIR
= _T("d:\\");
286 #error "don't know where the root directory is"
289 static void TestDirEnumHelper(wxDir
& dir
,
290 int flags
= wxDIR_DEFAULT
,
291 const wxString
& filespec
= wxEmptyString
)
295 if ( !dir
.IsOpened() )
298 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
301 wxPrintf(_T("\t%s\n"), filename
.c_str());
303 cont
= dir
.GetNext(&filename
);
306 wxPuts(wxEmptyString
);
309 static void TestDirEnum()
311 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
313 wxString cwd
= wxGetCwd();
314 if ( !wxDir::Exists(cwd
) )
316 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
321 if ( !dir
.IsOpened() )
323 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
327 wxPuts(_T("Enumerating everything in current directory:"));
328 TestDirEnumHelper(dir
);
330 wxPuts(_T("Enumerating really everything in current directory:"));
331 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
333 wxPuts(_T("Enumerating object files in current directory:"));
334 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
336 wxPuts(_T("Enumerating directories in current directory:"));
337 TestDirEnumHelper(dir
, wxDIR_DIRS
);
339 wxPuts(_T("Enumerating files in current directory:"));
340 TestDirEnumHelper(dir
, wxDIR_FILES
);
342 wxPuts(_T("Enumerating files including hidden in current directory:"));
343 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
347 wxPuts(_T("Enumerating everything in root directory:"));
348 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
350 wxPuts(_T("Enumerating directories in root directory:"));
351 TestDirEnumHelper(dir
, wxDIR_DIRS
);
353 wxPuts(_T("Enumerating files in root directory:"));
354 TestDirEnumHelper(dir
, wxDIR_FILES
);
356 wxPuts(_T("Enumerating files including hidden in root directory:"));
357 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
359 wxPuts(_T("Enumerating files in non existing directory:"));
360 wxDir
dirNo(_T("nosuchdir"));
361 TestDirEnumHelper(dirNo
);
364 class DirPrintTraverser
: public wxDirTraverser
367 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
369 return wxDIR_CONTINUE
;
372 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
374 wxString path
, name
, ext
;
375 wxSplitPath(dirname
, &path
, &name
, &ext
);
378 name
<< _T('.') << ext
;
381 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
383 if ( wxIsPathSeparator(*p
) )
387 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
389 return wxDIR_CONTINUE
;
393 static void TestDirTraverse()
395 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
399 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
400 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
403 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
404 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
407 // enum again with custom traverser
408 wxPuts(_T("Now enumerating directories:"));
410 DirPrintTraverser traverser
;
411 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
414 static void TestDirExists()
416 wxPuts(_T("*** Testing wxDir::Exists() ***"));
418 static const wxChar
*dirnames
[] =
421 #if defined(__WXMSW__)
424 _T("\\\\share\\file"),
428 _T("c:\\autoexec.bat"),
429 #elif defined(__UNIX__)
438 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
440 wxPrintf(_T("%-40s: %s\n"),
442 wxDir::Exists(dirnames
[n
]) ? _T("exists")
443 : _T("doesn't exist"));
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 #ifdef TEST_DLLLOADER
455 #include "wx/dynlib.h"
457 static void TestDllLoad()
459 #if defined(__WXMSW__)
460 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
461 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
462 #elif defined(__UNIX__)
463 // weird: using just libc.so does *not* work!
464 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
465 static const wxChar
*FUNC_NAME
= _T("strlen");
467 #error "don't know how to test wxDllLoader on this platform"
470 wxPuts(_T("*** testing wxDllLoader ***\n"));
472 wxDynamicLibrary
lib(LIB_NAME
);
473 if ( !lib
.IsLoaded() )
475 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
479 typedef int (*wxStrlenType
)(const char *);
480 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
483 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
484 FUNC_NAME
, LIB_NAME
);
488 if ( pfnStrlen("foo") != 3 )
490 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
494 wxPuts(_T("... ok"));
500 #endif // TEST_DLLLOADER
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
508 #include "wx/utils.h"
510 static wxString
MyGetEnv(const wxString
& var
)
513 if ( !wxGetEnv(var
, &val
) )
516 val
= wxString(_T('\'')) + val
+ _T('\'');
521 static void TestEnvironment()
523 const wxChar
*var
= _T("wxTestVar");
525 wxPuts(_T("*** testing environment access functions ***"));
527 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
528 wxSetEnv(var
, _T("value for wxTestVar"));
529 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
530 wxSetEnv(var
, _T("another value"));
531 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
533 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
534 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
537 #endif // TEST_ENVIRON
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
545 #include "wx/utils.h"
547 static void TestExecute()
549 wxPuts(_T("*** testing wxExecute ***"));
552 #define COMMAND "cat -n ../../Makefile" // "echo hi"
553 #define SHELL_COMMAND "echo hi from shell"
554 #define REDIRECT_COMMAND COMMAND // "date"
555 #elif defined(__WXMSW__)
556 #define COMMAND "command.com /c echo hi"
557 #define SHELL_COMMAND "echo hi"
558 #define REDIRECT_COMMAND COMMAND
560 #error "no command to exec"
563 wxPrintf(_T("Testing wxShell: "));
565 if ( wxShell(_T(SHELL_COMMAND
)) )
568 wxPuts(_T("ERROR."));
570 wxPrintf(_T("Testing wxExecute: "));
572 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
575 wxPuts(_T("ERROR."));
577 #if 0 // no, it doesn't work (yet?)
578 wxPrintf(_T("Testing async wxExecute: "));
580 if ( wxExecute(COMMAND
) != 0 )
581 wxPuts(_T("Ok (command launched)."));
583 wxPuts(_T("ERROR."));
586 wxPrintf(_T("Testing wxExecute with redirection:\n"));
587 wxArrayString output
;
588 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
590 wxPuts(_T("ERROR."));
594 size_t count
= output
.GetCount();
595 for ( size_t n
= 0; n
< count
; n
++ )
597 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
604 #endif // TEST_EXECUTE
606 // ----------------------------------------------------------------------------
608 // ----------------------------------------------------------------------------
613 #include "wx/ffile.h"
614 #include "wx/textfile.h"
616 static void TestFileRead()
618 wxPuts(_T("*** wxFile read test ***"));
620 wxFile
file(_T("testdata.fc"));
621 if ( file
.IsOpened() )
623 wxPrintf(_T("File length: %lu\n"), file
.Length());
625 wxPuts(_T("File dump:\n----------"));
627 static const off_t len
= 1024;
631 off_t nRead
= file
.Read(buf
, len
);
632 if ( nRead
== wxInvalidOffset
)
634 wxPrintf(_T("Failed to read the file."));
638 fwrite(buf
, nRead
, 1, stdout
);
644 wxPuts(_T("----------"));
648 wxPrintf(_T("ERROR: can't open test file.\n"));
651 wxPuts(wxEmptyString
);
654 static void TestTextFileRead()
656 wxPuts(_T("*** wxTextFile read test ***"));
658 wxTextFile
file(_T("testdata.fc"));
661 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
662 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
666 wxPuts(_T("\nDumping the entire file:"));
667 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
669 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
671 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
673 wxPuts(_T("\nAnd now backwards:"));
674 for ( s
= file
.GetLastLine();
675 file
.GetCurrentLine() != 0;
676 s
= file
.GetPrevLine() )
678 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
680 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
684 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
687 wxPuts(wxEmptyString
);
690 static void TestFileCopy()
692 wxPuts(_T("*** Testing wxCopyFile ***"));
694 static const wxChar
*filename1
= _T("testdata.fc");
695 static const wxChar
*filename2
= _T("test2");
696 if ( !wxCopyFile(filename1
, filename2
) )
698 wxPuts(_T("ERROR: failed to copy file"));
702 wxFFile
f1(filename1
, _T("rb")),
703 f2(filename2
, _T("rb"));
705 if ( !f1
.IsOpened() || !f2
.IsOpened() )
707 wxPuts(_T("ERROR: failed to open file(s)"));
712 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
714 wxPuts(_T("ERROR: failed to read file(s)"));
718 if ( (s1
.length() != s2
.length()) ||
719 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
721 wxPuts(_T("ERROR: copy error!"));
725 wxPuts(_T("File was copied ok."));
731 if ( !wxRemoveFile(filename2
) )
733 wxPuts(_T("ERROR: failed to remove the file"));
736 wxPuts(wxEmptyString
);
741 // ----------------------------------------------------------------------------
743 // ----------------------------------------------------------------------------
747 #include "wx/confbase.h"
748 #include "wx/fileconf.h"
750 static const struct FileConfTestData
752 const wxChar
*name
; // value name
753 const wxChar
*value
; // the value from the file
756 { _T("value1"), _T("one") },
757 { _T("value2"), _T("two") },
758 { _T("novalue"), _T("default") },
761 static void TestFileConfRead()
763 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
765 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
766 _T("testdata.fc"), wxEmptyString
,
767 wxCONFIG_USE_RELATIVE_PATH
);
769 // test simple reading
770 wxPuts(_T("\nReading config file:"));
771 wxString
defValue(_T("default")), value
;
772 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
774 const FileConfTestData
& data
= fcTestData
[n
];
775 value
= fileconf
.Read(data
.name
, defValue
);
776 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
777 if ( value
== data
.value
)
783 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
787 // test enumerating the entries
788 wxPuts(_T("\nEnumerating all root entries:"));
791 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
794 wxPrintf(_T("\t%s = %s\n"),
796 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
798 cont
= fileconf
.GetNextEntry(name
, dummy
);
801 static const wxChar
*testEntry
= _T("TestEntry");
802 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
803 fileconf
.Write(testEntry
, _T("A value"));
804 fileconf
.DeleteEntry(testEntry
);
805 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
808 #endif // TEST_FILECONF
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
816 #include "wx/filename.h"
819 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
823 wxString full
= fn
.GetFullPath();
825 wxString vol
, path
, name
, ext
;
826 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
828 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
829 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
831 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
832 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
833 path
.c_str(), name
.c_str(), ext
.c_str());
835 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
836 wxPrintf(_T("with volume: \t'%s'\n"),
837 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
838 wxPrintf(_T("with separator:\t'%s'\n"),
839 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
840 wxPrintf(_T("with both: \t'%s'\n"),
841 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
843 wxPuts(_T("The directories in the path are:"));
844 wxArrayString dirs
= fn
.GetDirs();
845 size_t count
= dirs
.GetCount();
846 for ( size_t n
= 0; n
< count
; n
++ )
848 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
853 static struct FileNameInfo
855 const wxChar
*fullname
;
856 const wxChar
*volume
;
865 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
866 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
867 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
868 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
869 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
870 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
871 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
872 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
873 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
875 // Windows file names
876 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
877 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
878 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
879 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
880 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
881 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
882 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
884 // wxFileName support for Mac file names is broken currently
887 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
888 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
889 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
890 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
891 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
892 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
896 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
897 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
900 static void TestFileNameConstruction()
902 wxPuts(_T("*** testing wxFileName construction ***"));
904 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
906 const FileNameInfo
& fni
= filenames
[n
];
908 wxFileName
fn(fni
.fullname
, fni
.format
);
910 wxString fullname
= fn
.GetFullPath(fni
.format
);
911 if ( fullname
!= fni
.fullname
)
913 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni
.fullname
);
916 bool isAbsolute
= fn
.IsAbsolute(fni
.format
);
917 wxPrintf(_T("'%s' is %s (%s)\n\t"),
919 isAbsolute
? "absolute" : "relative",
920 isAbsolute
== fni
.isAbsolute
? "ok" : "ERROR");
922 if ( !fn
.Normalize(wxPATH_NORM_ALL
, wxEmptyString
, fni
.format
) )
924 wxPuts(_T("ERROR (couldn't be normalized)"));
928 wxPrintf(_T("normalized: '%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
932 wxPuts(wxEmptyString
);
935 static void TestFileNameSplit()
937 wxPuts(_T("*** testing wxFileName splitting ***"));
939 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
941 const FileNameInfo
& fni
= filenames
[n
];
942 wxString volume
, path
, name
, ext
;
943 wxFileName::SplitPath(fni
.fullname
,
944 &volume
, &path
, &name
, &ext
, fni
.format
);
946 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
948 volume
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
950 if ( volume
!= fni
.volume
)
951 wxPrintf(_T(" (ERROR: volume = '%s')"), fni
.volume
);
952 if ( path
!= fni
.path
)
953 wxPrintf(_T(" (ERROR: path = '%s')"), fni
.path
);
954 if ( name
!= fni
.name
)
955 wxPrintf(_T(" (ERROR: name = '%s')"), fni
.name
);
956 if ( ext
!= fni
.ext
)
957 wxPrintf(_T(" (ERROR: ext = '%s')"), fni
.ext
);
959 wxPuts(wxEmptyString
);
963 static void TestFileNameTemp()
965 wxPuts(_T("*** testing wxFileName temp file creation ***"));
967 static const wxChar
*tmpprefixes
[] =
975 _T("/tmp/foo/bar"), // this one must be an error
979 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
981 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
984 // "error" is not in upper case because it may be ok
985 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
989 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
990 tmpprefixes
[n
], path
.c_str());
992 if ( !wxRemoveFile(path
) )
994 wxLogWarning(_T("Failed to remove temp file '%s'"),
1001 static void TestFileNameMakeRelative()
1003 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
1005 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1007 const FileNameInfo
& fni
= filenames
[n
];
1009 wxFileName
fn(fni
.fullname
, fni
.format
);
1011 // choose the base dir of the same format
1013 switch ( fni
.format
)
1016 base
= _T("/usr/bin/");
1025 // TODO: I don't know how this is supposed to work there
1028 case wxPATH_NATIVE
: // make gcc happy
1030 wxFAIL_MSG( _T("unexpected path format") );
1033 wxPrintf(_T("'%s' relative to '%s': "),
1034 fn
.GetFullPath(fni
.format
).c_str(), base
.c_str());
1036 if ( !fn
.MakeRelativeTo(base
, fni
.format
) )
1038 wxPuts(_T("unchanged"));
1042 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1047 static void TestFileNameMakeAbsolute()
1049 wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
1051 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1053 const FileNameInfo
& fni
= filenames
[n
];
1054 wxFileName
fn(fni
.fullname
, fni
.format
);
1056 wxPrintf(_T("'%s' absolutized: "),
1057 fn
.GetFullPath(fni
.format
).c_str());
1059 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1062 wxPuts(wxEmptyString
);
1065 static void TestFileNameDirManip()
1067 // TODO: test AppendDir(), RemoveDir(), ...
1070 static void TestFileNameComparison()
1075 static void TestFileNameOperations()
1080 static void TestFileNameCwd()
1085 #endif // TEST_FILENAME
1087 // ----------------------------------------------------------------------------
1088 // wxFileName time functions
1089 // ----------------------------------------------------------------------------
1091 #ifdef TEST_FILETIME
1093 #include <wx/filename.h>
1094 #include <wx/datetime.h>
1096 static void TestFileGetTimes()
1098 wxFileName
fn(_T("testdata.fc"));
1100 wxDateTime dtAccess
, dtMod
, dtCreate
;
1101 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
1103 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1107 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
1109 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
1110 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
1111 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
1112 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
1117 static void TestFileSetTimes()
1119 wxFileName
fn(_T("testdata.fc"));
1123 wxPrintf(_T("ERROR: Touch() failed.\n"));
1128 #endif // TEST_FILETIME
1130 // ----------------------------------------------------------------------------
1132 // ----------------------------------------------------------------------------
1136 #include "wx/hash.h"
1140 Foo(int n_
) { n
= n_
; count
++; }
1145 static size_t count
;
1148 size_t Foo::count
= 0;
1150 WX_DECLARE_LIST(Foo
, wxListFoos
);
1151 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
1153 #include "wx/listimpl.cpp"
1155 WX_DEFINE_LIST(wxListFoos
);
1157 #include "wx/timer.h"
1159 static void TestHash()
1161 wxPuts(_T("*** Testing wxHashTable ***\n"));
1162 const int COUNT
= 100;
1169 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
1173 for ( i
= 0; i
< COUNT
; ++i
)
1174 hash
.Put(i
, &o
+ i
);
1177 wxHashTable::compatibility_iterator it
= hash
.Next();
1187 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1189 for ( i
= 99; i
>= 0; --i
)
1190 if( hash
.Get(i
) != &o
+ i
)
1191 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1193 for ( i
= 0; i
< COUNT
; ++i
)
1194 hash
.Put(i
, &o
+ i
+ 20);
1196 for ( i
= 99; i
>= 0; --i
)
1197 if( hash
.Get(i
) != &o
+ i
)
1198 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1200 for ( i
= 0; i
< COUNT
/2; ++i
)
1201 if( hash
.Delete(i
) != &o
+ i
)
1202 wxPuts(_T("Error in wxHashTable::Delete\n"));
1204 for ( i
= COUNT
/2; i
< COUNT
; ++i
)
1205 if( hash
.Get(i
) != &o
+ i
)
1206 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1208 for ( i
= 0; i
< COUNT
/2; ++i
)
1209 if( hash
.Get(i
) != &o
+ i
+ 20)
1210 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1212 for ( i
= 0; i
< COUNT
/2; ++i
)
1213 if( hash
.Delete(i
) != &o
+ i
+ 20)
1214 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1216 for ( i
= 0; i
< COUNT
/2; ++i
)
1217 if( hash
.Get(i
) != NULL
)
1218 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1220 hash2
.Put(_T("foo"), &o
+ 1);
1221 hash2
.Put(_T("bar"), &o
+ 2);
1222 hash2
.Put(_T("baz"), &o
+ 3);
1224 if (hash2
.Get(_T("moo")) != NULL
)
1225 wxPuts(_T("Error in wxHashTable::Get\n"));
1227 if (hash2
.Get(_T("bar")) != &o
+ 2)
1228 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1230 hash2
.Put(_T("bar"), &o
+ 0);
1232 if (hash2
.Get(_T("bar")) != &o
+ 2)
1233 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1236 // and now some corner-case testing; 3 and 13 hash to the same bucket
1238 wxHashTable
hash(wxKEY_INTEGER
, 10);
1241 hash
.Put(3, &dummy
);
1244 if (hash
.Get(3) != NULL
)
1245 wxPuts(_T("Corner case 1 failure\n"));
1247 hash
.Put(3, &dummy
);
1248 hash
.Put(13, &dummy
);
1251 if (hash
.Get(3) != NULL
)
1252 wxPuts(_T("Corner case 2 failure\n"));
1256 if (hash
.Get(13) != NULL
)
1257 wxPuts(_T("Corner case 3 failure\n"));
1259 hash
.Put(3, &dummy
);
1260 hash
.Put(13, &dummy
);
1263 if (hash
.Get(13) != NULL
)
1264 wxPuts(_T("Corner case 4 failure\n"));
1268 if (hash
.Get(3) != NULL
)
1269 wxPuts(_T("Corner case 5 failure\n"));
1273 wxHashTable
hash(wxKEY_INTEGER
, 10);
1276 hash
.Put(3, 7, &dummy
+ 7);
1277 hash
.Put(4, 8, &dummy
+ 8);
1279 if (hash
.Get(7) != NULL
) wxPuts(_T("Key/Hash 1 failure\n"));
1280 if (hash
.Get(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 2 failure\n"));
1281 if (hash
.Get(4) != NULL
) wxPuts(_T("Key/Hash 3 failure\n"));
1282 if (hash
.Get(3) != NULL
) wxPuts(_T("Key/Hash 4 failure\n"));
1283 if (hash
.Get(8) != NULL
) wxPuts(_T("Key/Hash 5 failure\n"));
1284 if (hash
.Get(8, 4) != NULL
) wxPuts(_T("Key/Hash 6 failure\n"));
1286 if (hash
.Delete(7) != NULL
) wxPuts(_T("Key/Hash 7 failure\n"));
1287 if (hash
.Delete(3) != NULL
) wxPuts(_T("Key/Hash 8 failure\n"));
1288 if (hash
.Delete(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 8 failure\n"));
1293 hash
.DeleteContents(true);
1295 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1296 hash
.GetCount(), Foo::count
);
1298 static const int hashTestData
[] =
1300 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1304 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1306 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
1309 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1310 hash
.GetCount(), Foo::count
);
1312 wxPuts(_T("Hash access test:"));
1313 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1315 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1316 hashTestData
[n
], n
);
1317 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
1320 wxPrintf(_T("ERROR, not found.\n"));
1324 wxPrintf(_T("%d (%s)\n"), foo
->n
,
1325 (size_t)foo
->n
== n
? "ok" : "ERROR");
1329 wxPrintf(_T("\nTrying to get an element not in hash: "));
1331 if ( hash
.Get(1234) || hash
.Get(1, 0) )
1333 wxPuts(_T("ERROR: found!"));
1337 wxPuts(_T("ok (not found)"));
1340 Foo
* foo
= hash
.Delete(0);
1342 wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count
);
1346 wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count
);
1349 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count
);
1350 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1352 wxPrintf(_T("Time: %ld\n"), sw
.Time());
1357 // ----------------------------------------------------------------------------
1359 // ----------------------------------------------------------------------------
1363 #include "wx/hashmap.h"
1365 // test compilation of basic map types
1366 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
1367 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
1368 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
1369 myUnsignedHashMap
);
1370 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1372 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1374 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1376 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1380 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1381 // myStringHashMap );
1382 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
1384 typedef myStringHashMap::iterator Itor
;
1386 static void TestHashMap()
1388 wxPuts(_T("*** Testing wxHashMap ***\n"));
1389 myStringHashMap
sh(0); // as small as possible
1392 const size_t count
= 10000;
1394 // init with some data
1395 for( i
= 0; i
< count
; ++i
)
1397 buf
.Printf(wxT("%d"), i
);
1398 sh
[buf
] = wxT("A") + buf
+ wxT("C");
1401 // test that insertion worked
1402 if( sh
.size() != count
)
1404 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh
.size(), count
);
1407 for( i
= 0; i
< count
; ++i
)
1409 buf
.Printf(wxT("%d"), i
);
1410 if( sh
[buf
] != wxT("A") + buf
+ wxT("C") )
1412 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1417 // check that iterators work
1419 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1423 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1427 if( it
->second
!= sh
[it
->first
] )
1429 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1434 if( sh
.size() != i
)
1436 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i
, count
);
1439 // test copy ctor, assignment operator
1440 myStringHashMap
h1( sh
), h2( 0 );
1443 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1445 if( h1
[it
->first
] != it
->second
)
1447 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it
->first
.c_str());
1450 if( h2
[it
->first
] != it
->second
)
1452 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it
->first
.c_str());
1457 for( i
= 0; i
< count
; ++i
)
1459 buf
.Printf(wxT("%d"), i
);
1460 size_t sz
= sh
.size();
1462 // test find() and erase(it)
1465 it
= sh
.find( buf
);
1466 if( it
!= sh
.end() )
1470 if( sh
.find( buf
) != sh
.end() )
1472 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1476 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i
);
1481 size_t c
= sh
.erase( buf
);
1483 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1485 if( sh
.find( buf
) != sh
.end() )
1487 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1491 // count should decrease
1492 if( sh
.size() != sz
- 1 )
1494 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1498 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1501 #endif // TEST_HASHMAP
1503 // ----------------------------------------------------------------------------
1505 // ----------------------------------------------------------------------------
1509 #include "wx/hashset.h"
1511 // test compilation of basic map types
1512 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
1513 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
1514 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
1515 myUnsignedHashSet
);
1516 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
1518 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
1520 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
1522 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
1524 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
1536 unsigned long operator()(const MyStruct
& s
) const
1537 { return m_dummy(s
.ptr
); }
1538 MyHash
& operator=(const MyHash
&) { return *this; }
1540 wxPointerHash m_dummy
;
1546 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
1547 { return s1
.ptr
== s2
.ptr
; }
1548 MyEqual
& operator=(const MyEqual
&) { return *this; }
1551 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
1553 typedef myTestHashSet5 wxStringHashSet
;
1555 static void TestHashSet()
1557 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1559 wxStringHashSet set1
;
1561 set1
.insert( _T("abc") );
1562 set1
.insert( _T("bbc") );
1563 set1
.insert( _T("cbc") );
1564 set1
.insert( _T("abc") );
1566 if( set1
.size() != 3 )
1567 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1573 tmp
.ptr
= &dummy
; tmp
.str
= _T("ABC");
1575 tmp
.ptr
= &dummy
+ 1;
1577 tmp
.ptr
= &dummy
; tmp
.str
= _T("CDE");
1580 if( set2
.size() != 2 )
1581 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1583 mySet::iterator it
= set2
.find( tmp
);
1585 if( it
== set2
.end() )
1586 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1587 if( it
->ptr
!= &dummy
)
1588 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1589 if( it
->str
!= _T("ABC") )
1590 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1592 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1595 #endif // TEST_HASHSET
1597 // ----------------------------------------------------------------------------
1599 // ----------------------------------------------------------------------------
1603 #include "wx/list.h"
1605 WX_DECLARE_LIST(Bar
, wxListBars
);
1606 #include "wx/listimpl.cpp"
1607 WX_DEFINE_LIST(wxListBars
);
1609 WX_DECLARE_LIST(int, wxListInt
);
1610 WX_DEFINE_LIST(wxListInt
);
1612 static void TestList()
1614 wxPuts(_T("*** Testing wxList operations ***\n"));
1620 for ( i
= 0; i
< 5; ++i
)
1621 list1
.Append(dummy
+ i
);
1623 if ( list1
.GetCount() != 5 )
1624 wxPuts(_T("Wrong number of items in list\n"));
1626 if ( list1
.Item(3)->GetData() != dummy
+ 3 )
1627 wxPuts(_T("Error in Item()\n"));
1629 if ( !list1
.Find(dummy
+ 4) )
1630 wxPuts(_T("Error in Find()\n"));
1632 wxListInt::compatibility_iterator node
= list1
.GetFirst();
1637 if ( node
->GetData() != dummy
+ i
)
1638 wxPuts(_T("Error in compatibility_iterator\n"));
1639 node
= node
->GetNext();
1643 if ( size_t(i
) != list1
.GetCount() )
1644 wxPuts(_T("Error in compatibility_iterator\n"));
1646 list1
.Insert(dummy
+ 0);
1647 list1
.Insert(1, dummy
+ 1);
1648 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
1650 node
= list1
.GetFirst();
1655 int* t
= node
->GetData();
1656 if ( t
!= dummy
+ i
)
1657 wxPuts(_T("Error in Insert\n"));
1658 node
= node
->GetNext();
1663 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1665 wxPuts(_T("*** Testing std::list operations ***\n"));
1669 wxListInt::iterator it
, en
;
1670 wxListInt::reverse_iterator rit
, ren
;
1672 for ( i
= 0; i
< 5; ++i
)
1673 list1
.push_back(i
+ &i
);
1675 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
1676 it
!= en
; ++it
, ++i
)
1677 if ( *it
!= i
+ &i
)
1678 wxPuts(_T("Error in iterator\n"));
1680 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
1681 rit
!= ren
; ++rit
, --i
)
1682 if ( *rit
!= i
+ &i
)
1683 wxPuts(_T("Error in reverse_iterator\n"));
1685 if ( *list1
.rbegin() != *--list1
.end() ||
1686 *list1
.begin() != *--list1
.rend() )
1687 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1688 if ( *list1
.begin() != *--++list1
.begin() ||
1689 *list1
.rbegin() != *--++list1
.rbegin() )
1690 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1692 if ( list1
.front() != &i
|| list1
.back() != &i
+ 4 )
1693 wxPuts(_T("Error in front()/back()\n"));
1695 list1
.erase(list1
.begin());
1696 list1
.erase(--list1
.end());
1698 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
1699 it
!= en
; ++it
, ++i
)
1700 if ( *it
!= i
+ &i
)
1701 wxPuts(_T("Error in erase()\n"));
1704 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1707 static void TestListCtor()
1709 wxPuts(_T("*** Testing wxList construction ***\n"));
1713 list1
.Append(new Bar(_T("first")));
1714 list1
.Append(new Bar(_T("second")));
1716 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1717 list1
.GetCount(), Bar::GetNumber());
1722 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1723 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
1726 list1
.DeleteContents(true);
1728 WX_CLEAR_LIST(wxListBars
, list1
);
1732 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1737 // ----------------------------------------------------------------------------
1739 // ----------------------------------------------------------------------------
1743 #include "wx/intl.h"
1744 #include "wx/utils.h" // for wxSetEnv
1746 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
1748 // find the name of the language from its value
1749 static const wxChar
*GetLangName(int lang
)
1751 static const wxChar
*languageNames
[] =
1761 _T("ARABIC_ALGERIA"),
1762 _T("ARABIC_BAHRAIN"),
1765 _T("ARABIC_JORDAN"),
1766 _T("ARABIC_KUWAIT"),
1767 _T("ARABIC_LEBANON"),
1769 _T("ARABIC_MOROCCO"),
1772 _T("ARABIC_SAUDI_ARABIA"),
1775 _T("ARABIC_TUNISIA"),
1782 _T("AZERI_CYRILLIC"),
1797 _T("CHINESE_SIMPLIFIED"),
1798 _T("CHINESE_TRADITIONAL"),
1799 _T("CHINESE_HONGKONG"),
1800 _T("CHINESE_MACAU"),
1801 _T("CHINESE_SINGAPORE"),
1802 _T("CHINESE_TAIWAN"),
1808 _T("DUTCH_BELGIAN"),
1812 _T("ENGLISH_AUSTRALIA"),
1813 _T("ENGLISH_BELIZE"),
1814 _T("ENGLISH_BOTSWANA"),
1815 _T("ENGLISH_CANADA"),
1816 _T("ENGLISH_CARIBBEAN"),
1817 _T("ENGLISH_DENMARK"),
1819 _T("ENGLISH_JAMAICA"),
1820 _T("ENGLISH_NEW_ZEALAND"),
1821 _T("ENGLISH_PHILIPPINES"),
1822 _T("ENGLISH_SOUTH_AFRICA"),
1823 _T("ENGLISH_TRINIDAD"),
1824 _T("ENGLISH_ZIMBABWE"),
1832 _T("FRENCH_BELGIAN"),
1833 _T("FRENCH_CANADIAN"),
1834 _T("FRENCH_LUXEMBOURG"),
1835 _T("FRENCH_MONACO"),
1841 _T("GERMAN_AUSTRIAN"),
1842 _T("GERMAN_BELGIUM"),
1843 _T("GERMAN_LIECHTENSTEIN"),
1844 _T("GERMAN_LUXEMBOURG"),
1862 _T("ITALIAN_SWISS"),
1867 _T("KASHMIRI_INDIA"),
1885 _T("MALAY_BRUNEI_DARUSSALAM"),
1886 _T("MALAY_MALAYSIA"),
1896 _T("NORWEGIAN_BOKMAL"),
1897 _T("NORWEGIAN_NYNORSK"),
1904 _T("PORTUGUESE_BRAZILIAN"),
1907 _T("RHAETO_ROMANCE"),
1910 _T("RUSSIAN_UKRAINE"),
1916 _T("SERBIAN_CYRILLIC"),
1917 _T("SERBIAN_LATIN"),
1918 _T("SERBO_CROATIAN"),
1929 _T("SPANISH_ARGENTINA"),
1930 _T("SPANISH_BOLIVIA"),
1931 _T("SPANISH_CHILE"),
1932 _T("SPANISH_COLOMBIA"),
1933 _T("SPANISH_COSTA_RICA"),
1934 _T("SPANISH_DOMINICAN_REPUBLIC"),
1935 _T("SPANISH_ECUADOR"),
1936 _T("SPANISH_EL_SALVADOR"),
1937 _T("SPANISH_GUATEMALA"),
1938 _T("SPANISH_HONDURAS"),
1939 _T("SPANISH_MEXICAN"),
1940 _T("SPANISH_MODERN"),
1941 _T("SPANISH_NICARAGUA"),
1942 _T("SPANISH_PANAMA"),
1943 _T("SPANISH_PARAGUAY"),
1945 _T("SPANISH_PUERTO_RICO"),
1946 _T("SPANISH_URUGUAY"),
1948 _T("SPANISH_VENEZUELA"),
1952 _T("SWEDISH_FINLAND"),
1970 _T("URDU_PAKISTAN"),
1972 _T("UZBEK_CYRILLIC"),
1985 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1986 return languageNames
[lang
];
1988 return _T("INVALID");
1991 static void TestDefaultLang()
1993 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1995 static const wxChar
*langStrings
[] =
1997 NULL
, // system default
2004 _T("de_DE.iso88591"),
2006 _T("?"), // invalid lang spec
2007 _T("klingonese"), // I bet on some systems it does exist...
2010 wxPrintf(_T("The default system encoding is %s (%d)\n"),
2011 wxLocale::GetSystemEncodingName().c_str(),
2012 wxLocale::GetSystemEncoding());
2014 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
2016 const wxChar
*langStr
= langStrings
[n
];
2019 // FIXME: this doesn't do anything at all under Windows, we need
2020 // to create a new wxLocale!
2021 wxSetEnv(_T("LC_ALL"), langStr
);
2024 int lang
= gs_localeDefault
.GetSystemLanguage();
2025 wxPrintf(_T("Locale for '%s' is %s.\n"),
2026 langStr
? langStr
: _T("system default"), GetLangName(lang
));
2030 #endif // TEST_LOCALE
2032 // ----------------------------------------------------------------------------
2034 // ----------------------------------------------------------------------------
2038 #include "wx/mimetype.h"
2040 static void TestMimeEnum()
2042 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
2044 wxArrayString mimetypes
;
2046 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
2048 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
2053 for ( size_t n
= 0; n
< count
; n
++ )
2055 wxFileType
*filetype
=
2056 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
2059 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
2060 mimetypes
[n
].c_str());
2064 filetype
->GetDescription(&desc
);
2065 filetype
->GetExtensions(exts
);
2067 filetype
->GetIcon(NULL
);
2070 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
2073 extsAll
<< _T(", ");
2077 wxPrintf(_T("\t%s: %s (%s)\n"),
2078 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
2081 wxPuts(wxEmptyString
);
2084 static void TestMimeOverride()
2086 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2088 static const wxChar
*mailcap
= _T("/tmp/mailcap");
2089 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
2091 if ( wxFile::Exists(mailcap
) )
2092 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2094 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
2096 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2099 if ( wxFile::Exists(mimetypes
) )
2100 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2102 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
2104 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2107 wxPuts(wxEmptyString
);
2110 static void TestMimeFilename()
2112 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2114 static const wxChar
*filenames
[] =
2122 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
2124 const wxString fname
= filenames
[n
];
2125 wxString ext
= fname
.AfterLast(_T('.'));
2126 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
2129 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
2134 if ( !ft
->GetDescription(&desc
) )
2135 desc
= _T("<no description>");
2138 if ( !ft
->GetOpenCommand(&cmd
,
2139 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
2140 cmd
= _T("<no command available>");
2142 cmd
= wxString(_T('"')) + cmd
+ _T('"');
2144 wxPrintf(_T("To open %s (%s) do %s.\n"),
2145 fname
.c_str(), desc
.c_str(), cmd
.c_str());
2151 wxPuts(wxEmptyString
);
2154 static void TestMimeAssociate()
2156 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2158 wxFileTypeInfo
ftInfo(
2159 _T("application/x-xyz"),
2160 _T("xyzview '%s'"), // open cmd
2161 _T(""), // print cmd
2162 _T("XYZ File"), // description
2163 _T(".xyz"), // extensions
2164 NULL
// end of extensions
2166 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
2168 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
2171 wxPuts(_T("ERROR: failed to create association!"));
2175 // TODO: read it back
2179 wxPuts(wxEmptyString
);
2184 // ----------------------------------------------------------------------------
2185 // misc information functions
2186 // ----------------------------------------------------------------------------
2188 #ifdef TEST_INFO_FUNCTIONS
2190 #include "wx/utils.h"
2192 static void TestDiskInfo()
2194 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2198 wxChar pathname
[128];
2199 wxPrintf(_T("\nEnter a directory name: "));
2200 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
2203 // kill the last '\n'
2204 pathname
[wxStrlen(pathname
) - 1] = 0;
2206 wxLongLong total
, free
;
2207 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
2209 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2213 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2214 (total
/ 1024).ToString().c_str(),
2215 (free
/ 1024).ToString().c_str(),
2221 static void TestOsInfo()
2223 wxPuts(_T("*** Testing OS info functions ***\n"));
2226 wxGetOsVersion(&major
, &minor
);
2227 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2228 wxGetOsDescription().c_str(), major
, minor
);
2230 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2232 wxPrintf(_T("Host name is %s (%s).\n"),
2233 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2235 wxPuts(wxEmptyString
);
2238 static void TestUserInfo()
2240 wxPuts(_T("*** Testing user info functions ***\n"));
2242 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2243 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2244 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2245 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2247 wxPuts(wxEmptyString
);
2250 #endif // TEST_INFO_FUNCTIONS
2252 // ----------------------------------------------------------------------------
2254 // ----------------------------------------------------------------------------
2256 #ifdef TEST_PATHLIST
2259 #define CMD_IN_PATH _T("ls")
2261 #define CMD_IN_PATH _T("command.com")
2264 static void TestPathList()
2266 wxPuts(_T("*** Testing wxPathList ***\n"));
2268 wxPathList pathlist
;
2269 pathlist
.AddEnvList(_T("PATH"));
2270 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
2273 wxPrintf(_T("ERROR: command not found in the path.\n"));
2277 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
2281 #endif // TEST_PATHLIST
2283 // ----------------------------------------------------------------------------
2284 // regular expressions
2285 // ----------------------------------------------------------------------------
2289 #include "wx/regex.h"
2291 static void TestRegExCompile()
2293 wxPuts(_T("*** Testing RE compilation ***\n"));
2295 static struct RegExCompTestData
2297 const wxChar
*pattern
;
2299 } regExCompTestData
[] =
2301 { _T("foo"), true },
2302 { _T("foo("), false },
2303 { _T("foo(bar"), false },
2304 { _T("foo(bar)"), true },
2305 { _T("foo["), false },
2306 { _T("foo[bar"), false },
2307 { _T("foo[bar]"), true },
2308 { _T("foo{"), true },
2309 { _T("foo{1"), false },
2310 { _T("foo{bar"), true },
2311 { _T("foo{1}"), true },
2312 { _T("foo{1,2}"), true },
2313 { _T("foo{bar}"), true },
2314 { _T("foo*"), true },
2315 { _T("foo**"), false },
2316 { _T("foo+"), true },
2317 { _T("foo++"), false },
2318 { _T("foo?"), true },
2319 { _T("foo??"), false },
2320 { _T("foo?+"), false },
2324 for ( size_t n
= 0; n
< WXSIZEOF(regExCompTestData
); n
++ )
2326 const RegExCompTestData
& data
= regExCompTestData
[n
];
2327 bool ok
= re
.Compile(data
.pattern
);
2329 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2331 ok
? wxEmptyString
: _T("not "),
2332 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2336 static void TestRegExMatch()
2338 wxPuts(_T("*** Testing RE matching ***\n"));
2340 static struct RegExMatchTestData
2342 const wxChar
*pattern
;
2345 } regExMatchTestData
[] =
2347 { _T("foo"), _T("bar"), false },
2348 { _T("foo"), _T("foobar"), true },
2349 { _T("^foo"), _T("foobar"), true },
2350 { _T("^foo"), _T("barfoo"), false },
2351 { _T("bar$"), _T("barbar"), true },
2352 { _T("bar$"), _T("barbar "), false },
2355 for ( size_t n
= 0; n
< WXSIZEOF(regExMatchTestData
); n
++ )
2357 const RegExMatchTestData
& data
= regExMatchTestData
[n
];
2359 wxRegEx
re(data
.pattern
);
2360 bool ok
= re
.Matches(data
.text
);
2362 wxPrintf(_T("'%s' %s %s (%s)\n"),
2364 ok
? _T("matches") : _T("doesn't match"),
2366 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2370 static void TestRegExSubmatch()
2372 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2374 wxRegEx
re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2375 if ( !re
.IsValid() )
2377 wxPuts(_T("ERROR: compilation failed."));
2381 wxString text
= _T("Fri Jul 13 18:37:52 CEST 2001");
2383 if ( !re
.Matches(text
) )
2385 wxPuts(_T("ERROR: match expected."));
2389 wxPrintf(_T("Entire match: %s\n"), re
.GetMatch(text
).c_str());
2391 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2392 re
.GetMatch(text
, 3).c_str(),
2393 re
.GetMatch(text
, 2).c_str(),
2394 re
.GetMatch(text
, 4).c_str(),
2395 re
.GetMatch(text
, 1).c_str());
2399 static void TestRegExReplacement()
2401 wxPuts(_T("*** Testing RE replacement ***"));
2403 static struct RegExReplTestData
2407 const wxChar
*result
;
2409 } regExReplTestData
[] =
2411 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2412 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2413 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2414 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2415 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2416 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2417 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2420 const wxChar
*pattern
= _T("([a-z]+)[^0-9]*([0-9]+)");
2421 wxRegEx
re(pattern
);
2423 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern
);
2425 for ( size_t n
= 0; n
< WXSIZEOF(regExReplTestData
); n
++ )
2427 const RegExReplTestData
& data
= regExReplTestData
[n
];
2429 wxString text
= data
.text
;
2430 size_t nRepl
= re
.Replace(&text
, data
.repl
);
2432 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2433 data
.text
, data
.repl
,
2434 nRepl
, nRepl
== 1 ? wxEmptyString
: _T("es"),
2436 if ( text
== data
.result
&& nRepl
== data
.count
)
2442 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2443 data
.count
, data
.result
);
2448 static void TestRegExInteractive()
2450 wxPuts(_T("*** Testing RE interactively ***"));
2454 wxChar pattern
[128];
2455 wxPrintf(_T("\nEnter a pattern: "));
2456 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
2459 // kill the last '\n'
2460 pattern
[wxStrlen(pattern
) - 1] = 0;
2463 if ( !re
.Compile(pattern
) )
2471 wxPrintf(_T("Enter text to match: "));
2472 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
2475 // kill the last '\n'
2476 text
[wxStrlen(text
) - 1] = 0;
2478 if ( !re
.Matches(text
) )
2480 wxPrintf(_T("No match.\n"));
2484 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
2487 for ( size_t n
= 1; ; n
++ )
2489 if ( !re
.GetMatch(&start
, &len
, n
) )
2494 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2495 n
, wxString(text
+ start
, len
).c_str());
2502 #endif // TEST_REGEX
2504 // ----------------------------------------------------------------------------
2506 // ----------------------------------------------------------------------------
2516 static void TestDbOpen()
2524 // ----------------------------------------------------------------------------
2526 // ----------------------------------------------------------------------------
2529 NB: this stuff was taken from the glibc test suite and modified to build
2530 in wxWindows: if I read the copyright below properly, this shouldn't
2536 #ifdef wxTEST_PRINTF
2537 // use our functions from wxchar.cpp
2541 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2542 // in the tests below
2543 int wxPrintf( const wxChar
*format
, ... );
2544 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
2547 #include "wx/longlong.h"
2551 static void rfg1 (void);
2552 static void rfg2 (void);
2556 fmtchk (const wxChar
*fmt
)
2558 (void) wxPrintf(_T("%s:\t`"), fmt
);
2559 (void) wxPrintf(fmt
, 0x12);
2560 (void) wxPrintf(_T("'\n"));
2564 fmtst1chk (const wxChar
*fmt
)
2566 (void) wxPrintf(_T("%s:\t`"), fmt
);
2567 (void) wxPrintf(fmt
, 4, 0x12);
2568 (void) wxPrintf(_T("'\n"));
2572 fmtst2chk (const wxChar
*fmt
)
2574 (void) wxPrintf(_T("%s:\t`"), fmt
);
2575 (void) wxPrintf(fmt
, 4, 4, 0x12);
2576 (void) wxPrintf(_T("'\n"));
2579 /* This page is covered by the following copyright: */
2581 /* (C) Copyright C E Chew
2583 * Feel free to copy, use and distribute this software provided:
2585 * 1. you do not pretend that you wrote it
2586 * 2. you leave this copyright notice intact.
2590 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2597 /* Formatted Output Test
2599 * This exercises the output formatting code.
2602 wxChar
*PointerNull
= NULL
;
2609 wxChar
*prefix
= buf
;
2612 wxPuts(_T("\nFormatted output test"));
2613 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2614 wxStrcpy(prefix
, _T("%"));
2615 for (i
= 0; i
< 2; i
++) {
2616 for (j
= 0; j
< 2; j
++) {
2617 for (k
= 0; k
< 2; k
++) {
2618 for (l
= 0; l
< 2; l
++) {
2619 wxStrcpy(prefix
, _T("%"));
2620 if (i
== 0) wxStrcat(prefix
, _T("-"));
2621 if (j
== 0) wxStrcat(prefix
, _T("+"));
2622 if (k
== 0) wxStrcat(prefix
, _T("#"));
2623 if (l
== 0) wxStrcat(prefix
, _T("0"));
2624 wxPrintf(_T("%5s |"), prefix
);
2625 wxStrcpy(tp
, prefix
);
2626 wxStrcat(tp
, _T("6d |"));
2628 wxStrcpy(tp
, prefix
);
2629 wxStrcat(tp
, _T("6o |"));
2631 wxStrcpy(tp
, prefix
);
2632 wxStrcat(tp
, _T("6x |"));
2634 wxStrcpy(tp
, prefix
);
2635 wxStrcat(tp
, _T("6X |"));
2637 wxStrcpy(tp
, prefix
);
2638 wxStrcat(tp
, _T("6u |"));
2645 wxPrintf(_T("%10s\n"), PointerNull
);
2646 wxPrintf(_T("%-10s\n"), PointerNull
);
2649 static void TestPrintf()
2651 static wxChar shortstr
[] = _T("Hi, Z.");
2652 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
2653 I am ready for my first lesson today.");
2655 wxString test_format
;
2659 fmtchk(_T("%4.4x"));
2660 fmtchk(_T("%04.4x"));
2661 fmtchk(_T("%4.3x"));
2662 fmtchk(_T("%04.3x"));
2664 fmtst1chk(_T("%.*x"));
2665 fmtst1chk(_T("%0*x"));
2666 fmtst2chk(_T("%*.*x"));
2667 fmtst2chk(_T("%0*.*x"));
2669 wxString bad_format
= _T("bad format:\t\"%b\"\n");
2670 wxPrintf(bad_format
.c_str());
2671 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
2673 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
2674 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
2675 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
2676 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
2677 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
2678 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
2679 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
2680 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
2681 wxPrintf(test_format
.c_str(), -123456);
2682 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
2683 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
2685 test_format
= _T("zero-padded string:\t\"%010s\"\n");
2686 wxPrintf(test_format
.c_str(), shortstr
);
2687 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
2688 wxPrintf(test_format
.c_str(), shortstr
);
2689 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
2690 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
2691 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
2692 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
2694 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
2695 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
2696 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
2697 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
2698 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
2699 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
2700 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
2701 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
2702 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
2703 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
2704 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
2705 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
2707 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
2708 wxPrintf (_T(" %6.5f\n"), .1);
2709 wxPrintf (_T("x%5.4fx\n"), .5);
2711 wxPrintf (_T("%#03x\n"), 1);
2713 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
2719 while (niter
-- != 0)
2720 wxPrintf (_T("%.17e\n"), d
/ 2);
2725 // Open Watcom cause compiler error here
2726 // Error! E173: col(24) floating-point constant too small to represent
2727 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
2730 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
2731 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
2732 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
2733 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
2734 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
2735 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
2736 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
2737 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
2738 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
2739 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
2744 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
2746 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
2747 rc
, WXSIZEOF(buf
), buf
);
2750 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
2751 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
2757 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
2758 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
2759 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
2760 wxPrintf (_T("%g should be 123.456\n"), 123.456);
2761 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
2762 wxPrintf (_T("%g should be 10\n"), 10.0);
2763 wxPrintf (_T("%g should be 0.02\n"), 0.02);
2767 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
2773 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
2775 result
|= wxStrcmp (buf
,
2776 _T("onetwo three "));
2778 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
2785 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
2787 // for some reason below line fails under Borland
2788 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
2791 if (wxStrcmp (buf
, _T("40000000000")) != 0)
2794 wxPuts (_T("\tFAILED"));
2796 wxUnusedVar(result
);
2797 wxPuts (wxEmptyString
);
2799 #endif // wxLongLong_t
2801 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
2802 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
2804 wxPuts (_T("--- Should be no further output. ---"));
2813 memset (bytes
, '\xff', sizeof bytes
);
2814 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
2815 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
2816 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
2818 wxPuts (_T("%hhn overwrite more bytes"));
2823 wxPuts (_T("%hhn wrote incorrect value"));
2835 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
2836 if (wxStrcmp (buf
, _T(" ")) != 0)
2837 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
2838 wxSprintf (buf
, _T("%5.f"), 33.3);
2839 if (wxStrcmp (buf
, _T(" 33")) != 0)
2840 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
2841 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
2842 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
2843 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
2844 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
2845 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
2846 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
2847 wxSprintf (buf
, _T("%.g"), 33.3);
2848 if (wxStrcmp (buf
, _T("3e+01")) != 0)
2849 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
2850 wxSprintf (buf
, _T("%.G"), 33.3);
2851 if (wxStrcmp (buf
, _T("3E+01")) != 0)
2852 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
2860 wxString test_format
;
2863 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
2864 if (wxStrcmp (buf
, _T("3")) != 0)
2865 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2867 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
2868 if (wxStrcmp (buf
, _T("3")) != 0)
2869 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
2871 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
2872 if (wxStrcmp (buf
, _T(" 3")) != 0)
2873 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
2875 test_format
= _T("%04.*o");
2876 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2877 if (wxStrcmp (buf
, _T(" 041")) != 0)
2878 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
2880 test_format
= _T("%09.*u");
2881 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2882 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
2883 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
2885 test_format
= _T("%04.*x");
2886 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2887 if (wxStrcmp (buf
, _T(" 021")) != 0)
2888 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2890 test_format
= _T("%04.*X");
2891 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2892 if (wxStrcmp (buf
, _T(" 021")) != 0)
2893 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
2896 #endif // TEST_PRINTF
2898 // ----------------------------------------------------------------------------
2899 // registry and related stuff
2900 // ----------------------------------------------------------------------------
2902 // this is for MSW only
2905 #undef TEST_REGISTRY
2910 #include "wx/confbase.h"
2911 #include "wx/msw/regconf.h"
2914 static void TestRegConfWrite()
2916 wxConfig
*config
= new wxConfig(_T("myapp"));
2917 config
->SetPath(_T("/group1"));
2918 config
->Write(_T("entry1"), _T("foo"));
2919 config
->SetPath(_T("/group2"));
2920 config
->Write(_T("entry1"), _T("bar"));
2924 static void TestRegConfRead()
2926 wxConfig
*config
= new wxConfig(_T("myapp"));
2930 config
->SetPath(_T("/"));
2931 wxPuts(_T("Enumerating / subgroups:"));
2932 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2936 bCont
= config
->GetNextGroup(str
, dummy
);
2940 #endif // TEST_REGCONF
2942 #ifdef TEST_REGISTRY
2944 #include "wx/msw/registry.h"
2946 // I chose this one because I liked its name, but it probably only exists under
2948 static const wxChar
*TESTKEY
=
2949 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2951 static void TestRegistryRead()
2953 wxPuts(_T("*** testing registry reading ***"));
2955 wxRegKey
key(TESTKEY
);
2956 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
2959 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2964 size_t nSubKeys
, nValues
;
2965 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2967 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2970 wxPrintf(_T("Enumerating values:\n"));
2974 bool cont
= key
.GetFirstValue(value
, dummy
);
2977 wxPrintf(_T("Value '%s': type "), value
.c_str());
2978 switch ( key
.GetValueType(value
) )
2980 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
2981 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
2982 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
2983 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
2984 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
2985 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
2986 default: wxPrintf(_T("other (unknown)")); break;
2989 wxPrintf(_T(", value = "));
2990 if ( key
.IsNumericValue(value
) )
2993 key
.QueryValue(value
, &val
);
2994 wxPrintf(_T("%ld"), val
);
2999 key
.QueryValue(value
, val
);
3000 wxPrintf(_T("'%s'"), val
.c_str());
3002 key
.QueryRawValue(value
, val
);
3003 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
3008 cont
= key
.GetNextValue(value
, dummy
);
3012 static void TestRegistryAssociation()
3015 The second call to deleteself genertaes an error message, with a
3016 messagebox saying .flo is crucial to system operation, while the .ddf
3017 call also fails, but with no error message
3022 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3024 key
= _T("ddxf_auto_file") ;
3025 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3027 key
= _T("ddxf_auto_file") ;
3028 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3030 key
= _T("program,0") ;
3031 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3033 key
= _T("program \"%1\"") ;
3035 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3037 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3039 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3041 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3045 #endif // TEST_REGISTRY
3047 // ----------------------------------------------------------------------------
3049 // ----------------------------------------------------------------------------
3051 #ifdef TEST_SCOPEGUARD
3053 #include "wx/scopeguard.h"
3055 static void function0() { puts("function0()"); }
3056 static void function1(int n
) { printf("function1(%d)\n", n
); }
3057 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
3061 void method0() { printf("method0()\n"); }
3062 void method1(int n
) { printf("method1(%d)\n", n
); }
3063 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
3066 static void TestScopeGuard()
3068 wxON_BLOCK_EXIT0(function0
);
3069 wxON_BLOCK_EXIT1(function1
, 17);
3070 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
3073 wxON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
3074 wxON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
3075 wxON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
3077 wxScopeGuard dismissed
= wxMakeGuard(function0
);
3078 dismissed
.Dismiss();
3083 // ----------------------------------------------------------------------------
3085 // ----------------------------------------------------------------------------
3089 #include "wx/socket.h"
3090 #include "wx/protocol/protocol.h"
3091 #include "wx/protocol/http.h"
3093 static void TestSocketServer()
3095 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3097 static const int PORT
= 3000;
3102 wxSocketServer
*server
= new wxSocketServer(addr
);
3103 if ( !server
->Ok() )
3105 wxPuts(_T("ERROR: failed to bind"));
3113 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
3115 wxSocketBase
*socket
= server
->Accept();
3118 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3122 wxPuts(_T("Server: got a client."));
3124 server
->SetTimeout(60); // 1 min
3127 while ( !close
&& socket
->IsConnected() )
3130 wxChar ch
= _T('\0');
3133 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
3135 // don't log error if the client just close the connection
3136 if ( socket
->IsConnected() )
3138 wxPuts(_T("ERROR: in wxSocket::Read."));
3158 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
3159 if ( s
== _T("close") )
3161 wxPuts(_T("Closing connection"));
3165 else if ( s
== _T("quit") )
3170 wxPuts(_T("Shutting down the server"));
3172 else // not a special command
3174 socket
->Write(s
.MakeUpper().c_str(), s
.length());
3175 socket
->Write("\r\n", 2);
3176 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
3182 wxPuts(_T("Server: lost a client unexpectedly."));
3188 // same as "delete server" but is consistent with GUI programs
3192 static void TestSocketClient()
3194 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3196 static const wxChar
*hostname
= _T("www.wxwindows.org");
3199 addr
.Hostname(hostname
);
3202 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
3204 wxSocketClient client
;
3205 if ( !client
.Connect(addr
) )
3207 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3211 wxPrintf(_T("--- Connected to %s:%u...\n"),
3212 addr
.Hostname().c_str(), addr
.Service());
3216 // could use simply "GET" here I suppose
3218 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
3219 client
.Write(cmdGet
, cmdGet
.length());
3220 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3221 MakePrintable(cmdGet
).c_str());
3222 client
.Read(buf
, WXSIZEOF(buf
));
3223 wxPrintf(_T("--- Server replied:\n%s"), buf
);
3227 #endif // TEST_SOCKETS
3229 // ----------------------------------------------------------------------------
3231 // ----------------------------------------------------------------------------
3235 #include "wx/protocol/ftp.h"
3239 #define FTP_ANONYMOUS
3241 #ifdef FTP_ANONYMOUS
3242 static const wxChar
*directory
= _T("/pub");
3243 static const wxChar
*filename
= _T("welcome.msg");
3245 static const wxChar
*directory
= _T("/etc");
3246 static const wxChar
*filename
= _T("issue");
3249 static bool TestFtpConnect()
3251 wxPuts(_T("*** Testing FTP connect ***"));
3253 #ifdef FTP_ANONYMOUS
3254 static const wxChar
*hostname
= _T("ftp.wxwindows.org");
3256 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
3257 #else // !FTP_ANONYMOUS
3258 static const wxChar
*hostname
= "localhost";
3261 wxFgets(user
, WXSIZEOF(user
), stdin
);
3262 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
3265 wxChar password
[256];
3266 wxPrintf(_T("Password for %s: "), password
);
3267 wxFgets(password
, WXSIZEOF(password
), stdin
);
3268 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
3269 ftp
.SetPassword(password
);
3271 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
3272 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3274 if ( !ftp
.Connect(hostname
) )
3276 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3282 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3283 hostname
, ftp
.Pwd().c_str());
3289 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3290 static void TestFtpWuFtpd()
3293 static const wxChar
*hostname
= _T("ftp.eudora.com");
3294 if ( !ftp
.Connect(hostname
) )
3296 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3300 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
3301 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3304 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3308 size_t size
= in
->GetSize();
3309 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3311 wxChar
*data
= new wxChar
[size
];
3312 if ( !in
->Read(data
, size
) )
3314 wxPuts(_T("ERROR: read error"));
3318 wxPrintf(_T("Successfully retrieved the file.\n"));
3327 static void TestFtpList()
3329 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3332 if ( !ftp
.ChDir(directory
) )
3334 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3337 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3339 // test NLIST and LIST
3340 wxArrayString files
;
3341 if ( !ftp
.GetFilesList(files
) )
3343 wxPuts(_T("ERROR: failed to get NLIST of files"));
3347 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
3348 size_t count
= files
.GetCount();
3349 for ( size_t n
= 0; n
< count
; n
++ )
3351 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3353 wxPuts(_T("End of the file list"));
3356 if ( !ftp
.GetDirList(files
) )
3358 wxPuts(_T("ERROR: failed to get LIST of files"));
3362 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
3363 size_t count
= files
.GetCount();
3364 for ( size_t n
= 0; n
< count
; n
++ )
3366 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3368 wxPuts(_T("End of the file list"));
3371 if ( !ftp
.ChDir(_T("..")) )
3373 wxPuts(_T("ERROR: failed to cd to .."));
3376 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3379 static void TestFtpDownload()
3381 wxPuts(_T("*** Testing wxFTP download ***\n"));
3384 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3387 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3391 size_t size
= in
->GetSize();
3392 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3395 wxChar
*data
= new wxChar
[size
];
3396 if ( !in
->Read(data
, size
) )
3398 wxPuts(_T("ERROR: read error"));
3402 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
3410 static void TestFtpFileSize()
3412 wxPuts(_T("*** Testing FTP SIZE command ***"));
3414 if ( !ftp
.ChDir(directory
) )
3416 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3419 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3421 if ( ftp
.FileExists(filename
) )
3423 int size
= ftp
.GetFileSize(filename
);
3425 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
3427 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
3431 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
3435 static void TestFtpMisc()
3437 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3439 if ( ftp
.SendCommand(_T("STAT")) != '2' )
3441 wxPuts(_T("ERROR: STAT failed"));
3445 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
3448 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
3450 wxPuts(_T("ERROR: HELP SITE failed"));
3454 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3455 ftp
.GetLastResult().c_str());
3459 static void TestFtpInteractive()
3461 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3467 wxPrintf(_T("Enter FTP command: "));
3468 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3471 // kill the last '\n'
3472 buf
[wxStrlen(buf
) - 1] = 0;
3474 // special handling of LIST and NLST as they require data connection
3475 wxString
start(buf
, 4);
3477 if ( start
== _T("LIST") || start
== _T("NLST") )
3480 if ( wxStrlen(buf
) > 4 )
3483 wxArrayString files
;
3484 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
3486 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
3490 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3491 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
3492 size_t count
= files
.GetCount();
3493 for ( size_t n
= 0; n
< count
; n
++ )
3495 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3497 wxPuts(_T("--- End of the file list"));
3502 wxChar ch
= ftp
.SendCommand(buf
);
3503 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
3506 wxPrintf(_T(" (return code %c)"), ch
);
3509 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
3513 wxPuts(_T("\n*** done ***"));
3516 static void TestFtpUpload()
3518 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3521 static const wxChar
*file1
= _T("test1");
3522 static const wxChar
*file2
= _T("test2");
3523 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
3526 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3527 out
->Write("First hello", 11);
3531 // send a command to check the remote file
3532 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
3534 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
3538 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3539 file1
, ftp
.GetLastResult().c_str());
3542 out
= ftp
.GetOutputStream(file2
);
3545 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3546 out
->Write("Second hello", 12);
3553 // ----------------------------------------------------------------------------
3555 // ----------------------------------------------------------------------------
3559 #include "wx/wfstream.h"
3560 #include "wx/mstream.h"
3562 static void TestFileStream()
3564 wxPuts(_T("*** Testing wxFileInputStream ***"));
3566 static const wxString filename
= _T("testdata.fs");
3568 wxFileOutputStream
fsOut(filename
);
3569 fsOut
.Write("foo", 3);
3572 wxFileInputStream
fsIn(filename
);
3573 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
3574 while ( !fsIn
.Eof() )
3576 wxPutchar(fsIn
.GetC());
3579 if ( !wxRemoveFile(filename
) )
3581 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
3584 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3587 static void TestMemoryStream()
3589 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3591 wxMemoryOutputStream memOutStream
;
3592 wxPrintf(_T("Initially out stream offset: %lu\n"),
3593 (unsigned long)memOutStream
.TellO());
3595 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
3597 memOutStream
.PutC(*p
);
3600 wxPrintf(_T("Final out stream offset: %lu\n"),
3601 (unsigned long)memOutStream
.TellO());
3603 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3606 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
3608 wxMemoryInputStream
memInpStream(buf
, len
);
3609 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
3610 while ( !memInpStream
.Eof() )
3612 wxPutchar(memInpStream
.GetC());
3615 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3618 #endif // TEST_STREAMS
3620 // ----------------------------------------------------------------------------
3622 // ----------------------------------------------------------------------------
3626 #include "wx/timer.h"
3627 #include "wx/utils.h"
3629 static void TestStopWatch()
3631 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3635 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3638 wxPrintf(_T("\t%ldms\n"), sw
.Time());
3640 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3644 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3647 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3650 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3653 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3656 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3659 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
3660 for ( size_t n
= 0; n
< 70; n
++ )
3664 for ( size_t m
= 0; m
< 100000; m
++ )
3666 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
3668 wxPuts(_T("\ntime is negative - ERROR!"));
3676 wxPuts(_T(", ok."));
3679 #endif // TEST_TIMER
3681 // ----------------------------------------------------------------------------
3683 // ----------------------------------------------------------------------------
3687 #include "wx/vcard.h"
3689 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
3692 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
3695 wxPrintf(_T("%s%s"),
3696 wxString(_T('\t'), level
).c_str(),
3697 vcObj
->GetName().c_str());
3700 switch ( vcObj
->GetType() )
3702 case wxVCardObject::String
:
3703 case wxVCardObject::UString
:
3706 vcObj
->GetValue(&val
);
3707 value
<< _T('"') << val
<< _T('"');
3711 case wxVCardObject::Int
:
3714 vcObj
->GetValue(&i
);
3715 value
.Printf(_T("%u"), i
);
3719 case wxVCardObject::Long
:
3722 vcObj
->GetValue(&l
);
3723 value
.Printf(_T("%lu"), l
);
3727 case wxVCardObject::None
:
3730 case wxVCardObject::Object
:
3731 value
= _T("<node>");
3735 value
= _T("<unknown value type>");
3739 wxPrintf(_T(" = %s"), value
.c_str());
3742 DumpVObject(level
+ 1, *vcObj
);
3745 vcObj
= vcard
.GetNextProp(&cookie
);
3749 static void DumpVCardAddresses(const wxVCard
& vcard
)
3751 wxPuts(_T("\nShowing all addresses from vCard:\n"));
3755 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
3759 int flags
= addr
->GetFlags();
3760 if ( flags
& wxVCardAddress::Domestic
)
3762 flagsStr
<< _T("domestic ");
3764 if ( flags
& wxVCardAddress::Intl
)
3766 flagsStr
<< _T("international ");
3768 if ( flags
& wxVCardAddress::Postal
)
3770 flagsStr
<< _T("postal ");
3772 if ( flags
& wxVCardAddress::Parcel
)
3774 flagsStr
<< _T("parcel ");
3776 if ( flags
& wxVCardAddress::Home
)
3778 flagsStr
<< _T("home ");
3780 if ( flags
& wxVCardAddress::Work
)
3782 flagsStr
<< _T("work ");
3785 wxPrintf(_T("Address %u:\n")
3787 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3790 addr
->GetPostOffice().c_str(),
3791 addr
->GetExtAddress().c_str(),
3792 addr
->GetStreet().c_str(),
3793 addr
->GetLocality().c_str(),
3794 addr
->GetRegion().c_str(),
3795 addr
->GetPostalCode().c_str(),
3796 addr
->GetCountry().c_str()
3800 addr
= vcard
.GetNextAddress(&cookie
);
3804 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
3806 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3810 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
3814 int flags
= phone
->GetFlags();
3815 if ( flags
& wxVCardPhoneNumber::Voice
)
3817 flagsStr
<< _T("voice ");
3819 if ( flags
& wxVCardPhoneNumber::Fax
)
3821 flagsStr
<< _T("fax ");
3823 if ( flags
& wxVCardPhoneNumber::Cellular
)
3825 flagsStr
<< _T("cellular ");
3827 if ( flags
& wxVCardPhoneNumber::Modem
)
3829 flagsStr
<< _T("modem ");
3831 if ( flags
& wxVCardPhoneNumber::Home
)
3833 flagsStr
<< _T("home ");
3835 if ( flags
& wxVCardPhoneNumber::Work
)
3837 flagsStr
<< _T("work ");
3840 wxPrintf(_T("Phone number %u:\n")
3845 phone
->GetNumber().c_str()
3849 phone
= vcard
.GetNextPhoneNumber(&cookie
);
3853 static void TestVCardRead()
3855 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3857 wxVCard
vcard(_T("vcard.vcf"));
3858 if ( !vcard
.IsOk() )
3860 wxPuts(_T("ERROR: couldn't load vCard."));
3864 // read individual vCard properties
3865 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
3869 vcObj
->GetValue(&value
);
3874 value
= _T("<none>");
3877 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
3880 if ( !vcard
.GetFullName(&value
) )
3882 value
= _T("<none>");
3885 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
3887 // now show how to deal with multiply occuring properties
3888 DumpVCardAddresses(vcard
);
3889 DumpVCardPhoneNumbers(vcard
);
3891 // and finally show all
3892 wxPuts(_T("\nNow dumping the entire vCard:\n")
3893 "-----------------------------\n");
3895 DumpVObject(0, vcard
);
3899 static void TestVCardWrite()
3901 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3904 if ( !vcard
.IsOk() )
3906 wxPuts(_T("ERROR: couldn't create vCard."));
3911 vcard
.SetName("Zeitlin", "Vadim");
3912 vcard
.SetFullName("Vadim Zeitlin");
3913 vcard
.SetOrganization("wxWindows", "R&D");
3915 // just dump the vCard back
3916 wxPuts(_T("Entire vCard follows:\n"));
3917 wxPuts(vcard
.Write());
3921 #endif // TEST_VCARD
3923 // ----------------------------------------------------------------------------
3925 // ----------------------------------------------------------------------------
3927 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3933 #include "wx/volume.h"
3935 static const wxChar
*volumeKinds
[] =
3941 _T("network volume"),
3945 static void TestFSVolume()
3947 wxPuts(_T("*** Testing wxFSVolume class ***"));
3949 wxArrayString volumes
= wxFSVolume::GetVolumes();
3950 size_t count
= volumes
.GetCount();
3954 wxPuts(_T("ERROR: no mounted volumes?"));
3958 wxPrintf(_T("%u mounted volumes found:\n"), count
);
3960 for ( size_t n
= 0; n
< count
; n
++ )
3962 wxFSVolume
vol(volumes
[n
]);
3965 wxPuts(_T("ERROR: couldn't create volume"));
3969 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3971 vol
.GetDisplayName().c_str(),
3972 vol
.GetName().c_str(),
3973 volumeKinds
[vol
.GetKind()],
3974 vol
.IsWritable() ? _T("rw") : _T("ro"),
3975 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
3980 #endif // TEST_VOLUME
3982 // ----------------------------------------------------------------------------
3983 // wide char and Unicode support
3984 // ----------------------------------------------------------------------------
3988 static void TestUnicodeToFromAscii()
3990 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
3992 static const char *msg
= "Hello, world!";
3993 wxString s
= wxString::FromAscii(msg
);
3995 wxPrintf(_T("Message in Unicode: %s\n"), s
.c_str());
3996 printf("Message in ASCII: %s\n", (const char *)s
.ToAscii());
3998 wxPutchar(_T('\n'));
4001 #include "wx/textfile.h"
4003 static void TestUnicodeTextFileRead()
4005 wxPuts(_T("Testing wxTextFile in Unicode build\n"));
4008 if ( file
.Open(_T("testdata.fc"), wxConvLocal
) )
4010 const size_t count
= file
.GetLineCount();
4011 for ( size_t n
= 0; n
< count
; n
++ )
4013 const wxString
& s
= file
[n
];
4015 wxPrintf(_T("Line %u: \"%s\" (len %u, last char = '%c')\n"),
4016 (unsigned)n
, s
.c_str(), (unsigned)s
.length(), s
.Last());
4021 #endif // TEST_UNICODE
4025 #include "wx/strconv.h"
4026 #include "wx/fontenc.h"
4027 #include "wx/encconv.h"
4028 #include "wx/buffer.h"
4030 static const unsigned char utf8koi8r
[] =
4032 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4033 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4034 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4035 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4036 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4037 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4038 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4041 static const unsigned char utf8iso8859_1
[] =
4043 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4044 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4045 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4046 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4047 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4050 static const unsigned char utf8Invalid
[] =
4052 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4053 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4054 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4055 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4059 static const struct Utf8Data
4061 const unsigned char *text
;
4063 const wxChar
*charset
;
4064 wxFontEncoding encoding
;
4067 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4068 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
4069 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4072 static void TestUtf8()
4074 wxPuts(_T("*** Testing UTF8 support ***\n"));
4079 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
4081 const Utf8Data
& u8d
= utf8data
[n
];
4082 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
4083 WXSIZEOF(wbuf
)) == (size_t)-1 )
4085 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4089 wxCSConv
conv(u8d
.charset
);
4090 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
4092 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
4096 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
4100 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
4102 s
= _T("<< conversion failed >>");
4103 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
4107 wxPuts(wxEmptyString
);
4110 static void TestEncodingConverter()
4112 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4114 // using wxEncodingConverter should give the same result as above
4117 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
4118 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
4120 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4124 wxEncodingConverter ec
;
4125 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
4126 ec
.Convert(wbuf
, buf
);
4127 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
4130 wxPuts(wxEmptyString
);
4133 #endif // TEST_WCHAR
4135 // ----------------------------------------------------------------------------
4137 // ----------------------------------------------------------------------------
4141 #include "wx/filesys.h"
4142 #include "wx/fs_zip.h"
4143 #include "wx/zipstrm.h"
4145 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
4147 static void TestZipStreamRead()
4149 wxPuts(_T("*** Testing ZIP reading ***\n"));
4151 static const wxString filename
= _T("foo");
4152 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
4153 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4155 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
4156 while ( !istr
.Eof() )
4158 wxPutchar(istr
.GetC());
4162 wxPuts(_T("\n----- done ------"));
4165 static void DumpZipDirectory(wxFileSystem
& fs
,
4166 const wxString
& dir
,
4167 const wxString
& indent
)
4169 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
4170 TESTFILE_ZIP
, dir
.c_str());
4171 wxString wildcard
= prefix
+ _T("/*");
4173 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
4174 while ( !dirname
.empty() )
4176 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
4178 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4183 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
4185 DumpZipDirectory(fs
, dirname
,
4186 indent
+ wxString(_T(' '), 4));
4188 dirname
= fs
.FindNext();
4191 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
4192 while ( !filename
.empty() )
4194 if ( !filename
.StartsWith(prefix
, &filename
) )
4196 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4201 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
4203 filename
= fs
.FindNext();
4207 static void TestZipFileSystem()
4209 wxPuts(_T("*** Testing ZIP file system ***\n"));
4211 wxFileSystem::AddHandler(new wxZipFSHandler
);
4213 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
4215 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
4220 // ----------------------------------------------------------------------------
4222 // ----------------------------------------------------------------------------
4226 #include "wx/zstream.h"
4227 #include "wx/wfstream.h"
4229 static const wxString FILENAME_GZ
= _T("test.gz");
4230 static const wxChar
*TEST_DATA
= _T("hello and hello and hello and hello and hello");
4232 static void TestZlibStreamWrite()
4234 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4236 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
4237 wxZlibOutputStream
ostr(fileOutStream
);
4238 wxPrintf(_T("Compressing the test string... "));
4239 ostr
.Write(TEST_DATA
, wxStrlen(TEST_DATA
) + 1);
4242 wxPuts(_T("(ERROR: failed)"));
4249 wxPuts(_T("\n----- done ------"));
4252 static void TestZlibStreamRead()
4254 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4256 wxFileInputStream
fileInStream(FILENAME_GZ
);
4257 wxZlibInputStream
istr(fileInStream
);
4258 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4260 wxPuts(_T("Dumping the file:"));
4261 while ( !istr
.Eof() )
4263 wxPutchar(istr
.GetC());
4267 wxPuts(_T("\n----- done ------"));
4272 // ----------------------------------------------------------------------------
4274 // ----------------------------------------------------------------------------
4278 #include "wx/wfstream.h"
4279 #include "wx/gzstream.h"
4280 #include "wx/filename.h"
4281 #include "wx/txtstrm.h"
4283 // Reads two input streams and verifies that they are the same (and non-emtpy)
4285 void GzipVerify(wxInputStream
&in1
, wxInputStream
&in2
)
4288 wxPuts(_T(" Can't verify"));
4292 const int BUFSIZE
= 8192;
4293 wxCharBuffer
buf1(BUFSIZE
);
4294 wxCharBuffer
buf2(BUFSIZE
);
4299 int n1
= in1
.Read(buf1
.data(), BUFSIZE
).LastRead();
4300 int n2
= in2
.Read(buf2
.data(), BUFSIZE
).LastRead();
4302 if (n1
!= n2
|| (n1
&& memcmp(buf1
, buf2
, n1
) != 0) || (!n1
&& none
)) {
4303 wxPuts(_T(" Failure"));
4308 wxPuts(_T(" Success"));
4316 in1
.Read(buf1
.data(), BUFSIZE
);
4318 in2
.Read(buf2
.data(), BUFSIZE
);
4321 // Write a gzip file and read it back.
4325 wxPuts(_T("*** Testing gzip streams ***\n"));
4327 const wxString testname
= _T("gziptest");
4328 const wxString gzipname
= testname
+ _T(".gz");
4330 // write some random test data to a testfile
4331 wxPuts(_T("Writing random test data to ") + testname
+ _T("..."));
4333 wxFFileOutputStream
outstream(testname
);
4334 wxTextOutputStream
textout(outstream
);
4336 for (int i
= 0; i
< 1000 && outstream
.Ok(); i
++)
4337 textout
<< rand() << rand() << rand() << rand() << endl
;
4339 wxPuts(_T(" Done"));
4342 wxFileName
fn(testname
);
4343 wxDateTime dt
= fn
.GetModificationTime();
4344 wxFFileInputStream
instream(testname
);
4346 // try writing a gzip file
4347 wxPuts(_T("Writing ") + gzipname
+ _T(" using wxGzipOutputStream..."));
4349 wxFFileOutputStream
outstream(gzipname
);
4350 wxGzipOutputStream
gzip(outstream
, testname
, dt
);
4352 if (!gzip
.Write(instream
))
4353 wxPuts(_T(" Failure"));
4355 wxPuts(_T(" Success"));
4358 // try reading the gzip file
4359 wxPuts(_T("Reading ") + gzipname
+ _T(" using wxGzipInputStream..."));
4362 wxFFileInputStream
instream2(gzipname
);
4363 wxGzipInputStream
gzip(instream2
);
4364 GzipVerify(instream
, gzip
);
4366 if (gzip
.GetName() != fn
.GetFullName())
4367 wxPuts(gzipname
+ _T(" contains incorrect filename: ")
4369 if (dt
.IsValid() && gzip
.GetDateTime() != dt
)
4370 wxPuts(gzipname
+ _T(" contains incorrect timestamp: ")
4371 + gzip
.GetDateTime().Format());
4375 // then verify it using gzip program if it is in the path
4376 wxPuts(_T("Reading ") + gzipname
+ _T(" using gzip program..."));
4377 wxFFile
file(popen((_T("gzip -d -c ") + gzipname
).mb_str(), "r"));
4379 wxFFileInputStream
instream2(file
);
4381 GzipVerify(instream
, instream2
);
4386 // try reading a gzip created by gzip program
4387 wxPuts(_T("Reading output of gzip program using wxGzipInputStream..."));
4388 file
.Attach(popen((_T("gzip -c ") + testname
).mb_str(), "r"));
4390 wxFFileInputStream
instream2(file
);
4391 wxGzipInputStream
gzip(instream2
);
4393 GzipVerify(instream
, gzip
);
4399 wxPuts(_T("\n--- Done gzip streams ---"));
4404 // ----------------------------------------------------------------------------
4406 // ----------------------------------------------------------------------------
4408 #ifdef TEST_DATETIME
4412 #include "wx/datetime.h"
4417 wxDateTime::wxDateTime_t day
;
4418 wxDateTime::Month month
;
4420 wxDateTime::wxDateTime_t hour
, min
, sec
;
4422 wxDateTime::WeekDay wday
;
4423 time_t gmticks
, ticks
;
4425 void Init(const wxDateTime::Tm
& tm
)
4434 gmticks
= ticks
= -1;
4437 wxDateTime
DT() const
4438 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
4440 bool SameDay(const wxDateTime::Tm
& tm
) const
4442 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
4445 wxString
Format() const
4448 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4450 wxDateTime::GetMonthName(month
).c_str(),
4452 abs(wxDateTime::ConvertYearToBC(year
)),
4453 year
> 0 ? _T("AD") : _T("BC"));
4457 wxString
FormatDate() const
4460 s
.Printf(_T("%02d-%s-%4d%s"),
4462 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4463 abs(wxDateTime::ConvertYearToBC(year
)),
4464 year
> 0 ? _T("AD") : _T("BC"));
4469 static const Date testDates
[] =
4471 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
4472 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
4473 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
4474 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
4475 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
4476 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
4477 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
4478 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
4479 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
4480 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
4481 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
4482 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
4483 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
4484 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
4485 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
4486 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
4487 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
4488 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
4489 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
4492 // this test miscellaneous static wxDateTime functions
4493 static void TestTimeStatic()
4495 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4497 // some info about the current date
4498 int year
= wxDateTime::GetCurrentYear();
4499 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4501 wxDateTime::IsLeapYear(year
) ? "" : "not ",
4502 wxDateTime::GetNumberOfDays(year
));
4504 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
4505 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4506 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4507 wxDateTime::GetMonthName(month
).c_str(),
4508 wxDateTime::GetNumberOfDays(month
));
4511 static const size_t nYears
= 5;
4512 static const size_t years
[2][nYears
] =
4514 // first line: the years to test
4515 { 1990, 1976, 2000, 2030, 1984, },
4517 // second line: true if leap, false otherwise
4518 { false, true, true, false, true }
4521 for ( size_t n
= 0; n
< nYears
; n
++ )
4523 int year
= years
[0][n
];
4524 bool should
= years
[1][n
] != 0,
4525 is
= wxDateTime::IsLeapYear(year
);
4527 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4530 should
== is
? "ok" : "ERROR");
4532 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
4536 // test constructing wxDateTime objects
4537 static void TestTimeSet()
4539 wxPuts(_T("\n*** wxDateTime construction test ***"));
4541 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4543 const Date
& d1
= testDates
[n
];
4544 wxDateTime dt
= d1
.DT();
4547 d2
.Init(dt
.GetTm());
4549 wxString s1
= d1
.Format(),
4552 wxPrintf(_T("Date: %s == %s (%s)\n"),
4553 s1
.c_str(), s2
.c_str(),
4554 s1
== s2
? _T("ok") : _T("ERROR"));
4558 // test time zones stuff
4559 static void TestTimeZones()
4561 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4563 wxDateTime now
= wxDateTime::Now();
4565 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
4566 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
4567 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
4568 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
4569 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
4570 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
4572 wxDateTime::Tm tm
= now
.GetTm();
4573 if ( wxDateTime(tm
) != now
)
4575 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4576 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
4580 // test some minimal support for the dates outside the standard range
4581 static void TestTimeRange()
4583 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4585 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
4587 wxPrintf(_T("Unix epoch:\t%s\n"),
4588 wxDateTime(2440587.5).Format(fmt
).c_str());
4589 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4590 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
4591 wxPrintf(_T("JDN 0: \t%s\n"),
4592 wxDateTime(0.0).Format(fmt
).c_str());
4593 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4594 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
4595 wxPrintf(_T("May 29, 2099:\t%s\n"),
4596 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
4599 static void TestTimeTicks()
4601 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4603 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4605 const Date
& d
= testDates
[n
];
4606 if ( d
.ticks
== -1 )
4609 wxDateTime dt
= d
.DT();
4610 long ticks
= (dt
.GetValue() / 1000).ToLong();
4611 wxPrintf(_T("Ticks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4612 if ( ticks
== d
.ticks
)
4614 wxPuts(_T(" (ok)"));
4618 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4619 (long)d
.ticks
, (long)(ticks
- d
.ticks
));
4622 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
4623 ticks
= (dt
.GetValue() / 1000).ToLong();
4624 wxPrintf(_T("GMtks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4625 if ( ticks
== d
.gmticks
)
4627 wxPuts(_T(" (ok)"));
4631 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4632 (long)d
.gmticks
, (long)(ticks
- d
.gmticks
));
4636 wxPuts(wxEmptyString
);
4639 // test conversions to JDN &c
4640 static void TestTimeJDN()
4642 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4644 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4646 const Date
& d
= testDates
[n
];
4647 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4648 double jdn
= dt
.GetJulianDayNumber();
4650 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d
.Format().c_str(), jdn
);
4653 wxPuts(_T(" (ok)"));
4657 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4658 d
.jdn
, jdn
- d
.jdn
);
4663 // test week days computation
4664 static void TestTimeWDays()
4666 wxPuts(_T("\n*** wxDateTime weekday test ***"));
4668 // test GetWeekDay()
4670 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4672 const Date
& d
= testDates
[n
];
4673 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4675 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
4676 wxPrintf(_T("%s is: %s"),
4678 wxDateTime::GetWeekDayName(wday
).c_str());
4679 if ( wday
== d
.wday
)
4681 wxPuts(_T(" (ok)"));
4685 wxPrintf(_T(" (ERROR: should be %s)\n"),
4686 wxDateTime::GetWeekDayName(d
.wday
).c_str());
4690 wxPuts(wxEmptyString
);
4692 // test SetToWeekDay()
4693 struct WeekDateTestData
4695 Date date
; // the real date (precomputed)
4696 int nWeek
; // its week index in the month
4697 wxDateTime::WeekDay wday
; // the weekday
4698 wxDateTime::Month month
; // the month
4699 int year
; // and the year
4701 wxString
Format() const
4704 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
4706 case 1: which
= _T("first"); break;
4707 case 2: which
= _T("second"); break;
4708 case 3: which
= _T("third"); break;
4709 case 4: which
= _T("fourth"); break;
4710 case 5: which
= _T("fifth"); break;
4712 case -1: which
= _T("last"); break;
4717 which
+= _T(" from end");
4720 s
.Printf(_T("The %s %s of %s in %d"),
4722 wxDateTime::GetWeekDayName(wday
).c_str(),
4723 wxDateTime::GetMonthName(month
).c_str(),
4730 // the array data was generated by the following python program
4732 from DateTime import *
4733 from whrandom import *
4734 from string import *
4736 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4737 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4739 week = DateTimeDelta(7)
4742 year = randint(1900, 2100)
4743 month = randint(1, 12)
4744 day = randint(1, 28)
4745 dt = DateTime(year, month, day)
4746 wday = dt.day_of_week
4748 countFromEnd = choice([-1, 1])
4751 while dt.month is month:
4752 dt = dt - countFromEnd * week
4753 weekNum = weekNum + countFromEnd
4755 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
4757 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
4758 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
4761 static const WeekDateTestData weekDatesTestData
[] =
4763 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
4764 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
4765 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
4766 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
4767 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
4768 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
4769 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
4770 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
4771 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
4772 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
4773 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
4774 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
4775 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
4776 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
4777 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
4778 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
4779 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
4780 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
4781 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
4782 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
4785 static const wxChar
*fmt
= _T("%d-%b-%Y");
4788 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
4790 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
4792 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
4794 wxPrintf(_T("%s is %s"), wd
.Format().c_str(), dt
.Format(fmt
).c_str());
4796 const Date
& d
= wd
.date
;
4797 if ( d
.SameDay(dt
.GetTm()) )
4799 wxPuts(_T(" (ok)"));
4803 dt
.Set(d
.day
, d
.month
, d
.year
);
4805 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.Format(fmt
).c_str());
4810 // test the computation of (ISO) week numbers
4811 static void TestTimeWNumber()
4813 wxPuts(_T("\n*** wxDateTime week number test ***"));
4815 struct WeekNumberTestData
4817 Date date
; // the date
4818 wxDateTime::wxDateTime_t week
; // the week number in the year
4819 wxDateTime::wxDateTime_t wmon
; // the week number in the month
4820 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
4821 wxDateTime::wxDateTime_t dnum
; // day number in the year
4824 // data generated with the following python script:
4826 from DateTime import *
4827 from whrandom import *
4828 from string import *
4830 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4831 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4833 def GetMonthWeek(dt):
4834 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
4835 if weekNumMonth < 0:
4836 weekNumMonth = weekNumMonth + 53
4839 def GetLastSundayBefore(dt):
4840 if dt.iso_week[2] == 7:
4843 return dt - DateTimeDelta(dt.iso_week[2])
4846 year = randint(1900, 2100)
4847 month = randint(1, 12)
4848 day = randint(1, 28)
4849 dt = DateTime(year, month, day)
4850 dayNum = dt.day_of_year
4851 weekNum = dt.iso_week[1]
4852 weekNumMonth = GetMonthWeek(dt)
4855 dtSunday = GetLastSundayBefore(dt)
4857 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
4858 weekNumMonth2 = weekNumMonth2 + 1
4859 dtSunday = dtSunday - DateTimeDelta(7)
4861 data = { 'day': rjust(`day`, 2), \
4862 'month': monthNames[month - 1], \
4864 'weekNum': rjust(`weekNum`, 2), \
4865 'weekNumMonth': weekNumMonth, \
4866 'weekNumMonth2': weekNumMonth2, \
4867 'dayNum': rjust(`dayNum`, 3) }
4869 print " { { %(day)s, "\
4870 "wxDateTime::%(month)s, "\
4873 "%(weekNumMonth)s, "\
4874 "%(weekNumMonth2)s, "\
4875 "%(dayNum)s }," % data
4878 static const WeekNumberTestData weekNumberTestDates
[] =
4880 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
4881 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
4882 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
4883 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
4884 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
4885 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
4886 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
4887 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
4888 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
4889 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
4890 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
4891 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
4892 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
4893 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
4894 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
4895 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
4896 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
4897 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
4898 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
4899 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
4902 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
4904 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
4905 const Date
& d
= wn
.date
;
4907 wxDateTime dt
= d
.DT();
4909 wxDateTime::wxDateTime_t
4910 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
4911 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
4912 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
4913 dnum
= dt
.GetDayOfYear();
4915 wxPrintf(_T("%s: the day number is %d"), d
.FormatDate().c_str(), dnum
);
4916 if ( dnum
== wn
.dnum
)
4918 wxPrintf(_T(" (ok)"));
4922 wxPrintf(_T(" (ERROR: should be %d)"), wn
.dnum
);
4925 wxPrintf(_T(", week in month = %d"), wmon
);
4926 if ( wmon
!= wn
.wmon
)
4928 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon
);
4931 wxPrintf(_T(" or %d"), wmon2
);
4932 if ( wmon2
== wn
.wmon2
)
4934 wxPrintf(_T(" (ok)"));
4938 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon2
);
4941 wxPrintf(_T(", week in year = %d"), week
);
4942 if ( week
!= wn
.week
)
4944 wxPrintf(_T(" (ERROR: should be %d)"), wn
.week
);
4947 wxPutchar(_T('\n'));
4949 wxDateTime
dt2(1, wxDateTime::Jan
, d
.year
);
4950 dt2
.SetToTheWeek(wn
.week
, dt
.GetWeekDay());
4954 d2
.Init(dt2
.GetTm());
4955 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
4956 d2
.FormatDate().c_str());
4961 // test DST calculations
4962 static void TestTimeDST()
4964 wxPuts(_T("\n*** wxDateTime DST test ***"));
4966 wxPrintf(_T("DST is%s in effect now.\n\n"),
4967 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
4969 // taken from http://www.energy.ca.gov/daylightsaving.html
4970 static const Date datesDST
[2][2004 - 1900 + 1] =
4973 { 1, wxDateTime::Apr
, 1990 },
4974 { 7, wxDateTime::Apr
, 1991 },
4975 { 5, wxDateTime::Apr
, 1992 },
4976 { 4, wxDateTime::Apr
, 1993 },
4977 { 3, wxDateTime::Apr
, 1994 },
4978 { 2, wxDateTime::Apr
, 1995 },
4979 { 7, wxDateTime::Apr
, 1996 },
4980 { 6, wxDateTime::Apr
, 1997 },
4981 { 5, wxDateTime::Apr
, 1998 },
4982 { 4, wxDateTime::Apr
, 1999 },
4983 { 2, wxDateTime::Apr
, 2000 },
4984 { 1, wxDateTime::Apr
, 2001 },
4985 { 7, wxDateTime::Apr
, 2002 },
4986 { 6, wxDateTime::Apr
, 2003 },
4987 { 4, wxDateTime::Apr
, 2004 },
4990 { 28, wxDateTime::Oct
, 1990 },
4991 { 27, wxDateTime::Oct
, 1991 },
4992 { 25, wxDateTime::Oct
, 1992 },
4993 { 31, wxDateTime::Oct
, 1993 },
4994 { 30, wxDateTime::Oct
, 1994 },
4995 { 29, wxDateTime::Oct
, 1995 },
4996 { 27, wxDateTime::Oct
, 1996 },
4997 { 26, wxDateTime::Oct
, 1997 },
4998 { 25, wxDateTime::Oct
, 1998 },
4999 { 31, wxDateTime::Oct
, 1999 },
5000 { 29, wxDateTime::Oct
, 2000 },
5001 { 28, wxDateTime::Oct
, 2001 },
5002 { 27, wxDateTime::Oct
, 2002 },
5003 { 26, wxDateTime::Oct
, 2003 },
5004 { 31, wxDateTime::Oct
, 2004 },
5009 for ( year
= 1990; year
< 2005; year
++ )
5011 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
5012 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
5014 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5015 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
5017 size_t n
= year
- 1990;
5018 const Date
& dBegin
= datesDST
[0][n
];
5019 const Date
& dEnd
= datesDST
[1][n
];
5021 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
5023 wxPuts(_T(" (ok)"));
5027 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5028 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
5029 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
5033 wxPuts(wxEmptyString
);
5035 for ( year
= 1990; year
< 2005; year
++ )
5037 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5039 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
5040 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
5044 // test wxDateTime -> text conversion
5045 static void TestTimeFormat()
5047 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5049 // some information may be lost during conversion, so store what kind
5050 // of info should we recover after a round trip
5053 CompareNone
, // don't try comparing
5054 CompareBoth
, // dates and times should be identical
5055 CompareDate
, // dates only
5056 CompareTime
// time only
5061 CompareKind compareKind
;
5062 const wxChar
*format
;
5063 } formatTestFormats
[] =
5065 { CompareBoth
, _T("---> %c") },
5066 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
5067 { CompareBoth
, _T("Date is %x, time is %X") },
5068 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5069 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
5070 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
5073 static const Date formatTestDates
[] =
5075 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
5076 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
5078 // this test can't work for other centuries because it uses two digit
5079 // years in formats, so don't even try it
5080 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
5081 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
5082 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
5086 // an extra test (as it doesn't depend on date, don't do it in the loop)
5087 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5089 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
5091 wxPuts(wxEmptyString
);
5093 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
5094 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
5096 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
5097 wxPrintf(_T("%s"), s
.c_str());
5099 // what can we recover?
5100 int kind
= formatTestFormats
[n
].compareKind
;
5104 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
5107 // converion failed - should it have?
5108 if ( kind
== CompareNone
)
5109 wxPuts(_T(" (ok)"));
5111 wxPuts(_T(" (ERROR: conversion back failed)"));
5115 // should have parsed the entire string
5116 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5120 bool equal
= false; // suppress compilaer warning
5128 equal
= dt
.IsSameDate(dt2
);
5132 equal
= dt
.IsSameTime(dt2
);
5138 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5139 dt2
.Format().c_str(), dt
.Format().c_str());
5143 wxPuts(_T(" (ok)"));
5150 // test text -> wxDateTime conversion
5151 static void TestTimeParse()
5153 wxPuts(_T("\n*** wxDateTime parse test ***"));
5155 struct ParseTestData
5157 const wxChar
*format
;
5162 static const ParseTestData parseTestDates
[] =
5164 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, true },
5165 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, true },
5168 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
5170 const wxChar
*format
= parseTestDates
[n
].format
;
5172 wxPrintf(_T("%s => "), format
);
5175 if ( dt
.ParseRfc822Date(format
) )
5177 wxPrintf(_T("%s "), dt
.Format().c_str());
5179 if ( parseTestDates
[n
].good
)
5181 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
5188 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal
.Format().c_str());
5193 wxPuts(_T("(ERROR: bad format)"));
5198 wxPrintf(_T("bad format (%s)\n"),
5199 parseTestDates
[n
].good
? "ERROR" : "ok");
5204 static void TestDateTimeInteractive()
5206 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5212 wxPrintf(_T("Enter a date: "));
5213 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
5216 // kill the last '\n'
5217 buf
[wxStrlen(buf
) - 1] = 0;
5220 const wxChar
*p
= dt
.ParseDate(buf
);
5223 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
5229 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
5232 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5233 dt
.Format(_T("%b %d, %Y")).c_str(),
5235 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5236 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5237 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
5240 wxPuts(_T("\n*** done ***"));
5243 static void TestTimeMS()
5245 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5247 wxDateTime dt1
= wxDateTime::Now(),
5248 dt2
= wxDateTime::UNow();
5250 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
5251 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5252 wxPrintf(_T("Dummy loop: "));
5253 for ( int i
= 0; i
< 6000; i
++ )
5255 //for ( int j = 0; j < 10; j++ )
5258 s
.Printf(_T("%g"), sqrt(i
));
5264 wxPuts(_T(", done"));
5267 dt2
= wxDateTime::UNow();
5268 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5270 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
5272 wxPuts(_T("\n*** done ***"));
5275 static void TestTimeArithmetics()
5277 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5279 static const struct ArithmData
5281 ArithmData(const wxDateSpan
& sp
, const wxChar
*nam
)
5282 : span(sp
), name(nam
) { }
5286 } testArithmData
[] =
5288 ArithmData(wxDateSpan::Day(), _T("day")),
5289 ArithmData(wxDateSpan::Week(), _T("week")),
5290 ArithmData(wxDateSpan::Month(), _T("month")),
5291 ArithmData(wxDateSpan::Year(), _T("year")),
5292 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5295 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
5297 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
5299 wxDateSpan span
= testArithmData
[n
].span
;
5303 const wxChar
*name
= testArithmData
[n
].name
;
5304 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5305 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
5306 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
5308 wxPrintf(_T("Going back: %s"), (dt1
- span
).FormatISODate().c_str());
5309 if ( dt1
- span
== dt
)
5311 wxPuts(_T(" (ok)"));
5315 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5318 wxPrintf(_T("Going forward: %s"), (dt2
+ span
).FormatISODate().c_str());
5319 if ( dt2
+ span
== dt
)
5321 wxPuts(_T(" (ok)"));
5325 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5328 wxPrintf(_T("Double increment: %s"), (dt2
+ 2*span
).FormatISODate().c_str());
5329 if ( dt2
+ 2*span
== dt1
)
5331 wxPuts(_T(" (ok)"));
5335 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2
.FormatISODate().c_str());
5338 wxPuts(wxEmptyString
);
5342 static void TestTimeHolidays()
5344 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5346 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
5347 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
5348 dtEnd
= dtStart
.GetLastMonthDay();
5350 wxDateTimeArray hol
;
5351 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
5353 const wxChar
*format
= _T("%d-%b-%Y (%a)");
5355 wxPrintf(_T("All holidays between %s and %s:\n"),
5356 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
5358 size_t count
= hol
.GetCount();
5359 for ( size_t n
= 0; n
< count
; n
++ )
5361 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
5364 wxPuts(wxEmptyString
);
5367 static void TestTimeZoneBug()
5369 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5371 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
5372 for ( int i
= 0; i
< 31; i
++ )
5374 wxPrintf(_T("Date %s: week day %s.\n"),
5375 date
.Format(_T("%d-%m-%Y")).c_str(),
5376 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
5378 date
+= wxDateSpan::Day();
5381 wxPuts(wxEmptyString
);
5384 static void TestTimeSpanFormat()
5386 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5388 static const wxChar
*formats
[] =
5390 _T("(default) %H:%M:%S"),
5391 _T("%E weeks and %D days"),
5392 _T("%l milliseconds"),
5393 _T("(with ms) %H:%M:%S:%l"),
5394 _T("100%% of minutes is %M"), // test "%%"
5395 _T("%D days and %H hours"),
5396 _T("or also %S seconds"),
5399 wxTimeSpan
ts1(1, 2, 3, 4),
5401 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
5403 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5404 ts1
.Format(formats
[n
]).c_str(),
5405 ts2
.Format(formats
[n
]).c_str());
5408 wxPuts(wxEmptyString
);
5411 #endif // TEST_DATETIME
5413 // ----------------------------------------------------------------------------
5414 // wxTextInput/OutputStream
5415 // ----------------------------------------------------------------------------
5417 #ifdef TEST_TEXTSTREAM
5419 #include "wx/txtstrm.h"
5420 #include "wx/wfstream.h"
5422 static void TestTextInputStream()
5424 wxPuts(_T("\n*** wxTextInputStream test ***"));
5426 wxString filename
= _T("testdata.fc");
5427 wxFileInputStream
fsIn(filename
);
5430 wxPuts(_T("ERROR: couldn't open file."));
5434 wxTextInputStream
tis(fsIn
);
5439 const wxString s
= tis
.ReadLine();
5441 // line could be non empty if the last line of the file isn't
5442 // terminated with EOL
5443 if ( fsIn
.Eof() && s
.empty() )
5446 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
5451 #endif // TEST_TEXTSTREAM
5453 // ----------------------------------------------------------------------------
5455 // ----------------------------------------------------------------------------
5459 #include "wx/thread.h"
5461 static size_t gs_counter
= (size_t)-1;
5462 static wxCriticalSection gs_critsect
;
5463 static wxSemaphore gs_cond
;
5465 class MyJoinableThread
: public wxThread
5468 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
5469 { m_n
= n
; Create(); }
5471 // thread execution starts here
5472 virtual ExitCode
Entry();
5478 wxThread::ExitCode
MyJoinableThread::Entry()
5480 unsigned long res
= 1;
5481 for ( size_t n
= 1; n
< m_n
; n
++ )
5485 // it's a loooong calculation :-)
5489 return (ExitCode
)res
;
5492 class MyDetachedThread
: public wxThread
5495 MyDetachedThread(size_t n
, wxChar ch
)
5499 m_cancelled
= false;
5504 // thread execution starts here
5505 virtual ExitCode
Entry();
5508 virtual void OnExit();
5511 size_t m_n
; // number of characters to write
5512 wxChar m_ch
; // character to write
5514 bool m_cancelled
; // false if we exit normally
5517 wxThread::ExitCode
MyDetachedThread::Entry()
5520 wxCriticalSectionLocker
lock(gs_critsect
);
5521 if ( gs_counter
== (size_t)-1 )
5527 for ( size_t n
= 0; n
< m_n
; n
++ )
5529 if ( TestDestroy() )
5539 wxThread::Sleep(100);
5545 void MyDetachedThread::OnExit()
5547 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5549 wxCriticalSectionLocker
lock(gs_critsect
);
5550 if ( !--gs_counter
&& !m_cancelled
)
5554 static void TestDetachedThreads()
5556 wxPuts(_T("\n*** Testing detached threads ***"));
5558 static const size_t nThreads
= 3;
5559 MyDetachedThread
*threads
[nThreads
];
5561 for ( n
= 0; n
< nThreads
; n
++ )
5563 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
5566 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
5567 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
5569 for ( n
= 0; n
< nThreads
; n
++ )
5574 // wait until all threads terminate
5577 wxPuts(wxEmptyString
);
5580 static void TestJoinableThreads()
5582 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5584 // calc 10! in the background
5585 MyJoinableThread
thread(10);
5588 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5589 (unsigned long)thread
.Wait());
5592 static void TestThreadSuspend()
5594 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5596 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
5600 // this is for this demo only, in a real life program we'd use another
5601 // condition variable which would be signaled from wxThread::Entry() to
5602 // tell us that the thread really started running - but here just wait a
5603 // bit and hope that it will be enough (the problem is, of course, that
5604 // the thread might still not run when we call Pause() which will result
5606 wxThread::Sleep(300);
5608 for ( size_t n
= 0; n
< 3; n
++ )
5612 wxPuts(_T("\nThread suspended"));
5615 // don't sleep but resume immediately the first time
5616 wxThread::Sleep(300);
5618 wxPuts(_T("Going to resume the thread"));
5623 wxPuts(_T("Waiting until it terminates now"));
5625 // wait until the thread terminates
5628 wxPuts(wxEmptyString
);
5631 static void TestThreadDelete()
5633 // As above, using Sleep() is only for testing here - we must use some
5634 // synchronisation object instead to ensure that the thread is still
5635 // running when we delete it - deleting a detached thread which already
5636 // terminated will lead to a crash!
5638 wxPuts(_T("\n*** Testing thread delete function ***"));
5640 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
5644 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5646 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
5650 wxThread::Sleep(300);
5654 wxPuts(_T("\nDeleted a running thread."));
5656 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
5660 wxThread::Sleep(300);
5666 wxPuts(_T("\nDeleted a sleeping thread."));
5668 MyJoinableThread
thread3(20);
5673 wxPuts(_T("\nDeleted a joinable thread."));
5675 MyJoinableThread
thread4(2);
5678 wxThread::Sleep(300);
5682 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
5684 wxPuts(wxEmptyString
);
5687 class MyWaitingThread
: public wxThread
5690 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
5693 m_condition
= condition
;
5698 virtual ExitCode
Entry()
5700 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
5705 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
5709 m_condition
->Wait();
5712 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
5720 wxCondition
*m_condition
;
5723 static void TestThreadConditions()
5726 wxCondition
condition(mutex
);
5728 // otherwise its difficult to understand which log messages pertain to
5730 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
5731 // condition.GetId(), gs_cond.GetId());
5733 // create and launch threads
5734 MyWaitingThread
*threads
[10];
5737 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5739 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
5742 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5747 // wait until all threads run
5748 wxPuts(_T("Main thread is waiting for the other threads to start"));
5751 size_t nRunning
= 0;
5752 while ( nRunning
< WXSIZEOF(threads
) )
5758 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
5762 wxPuts(_T("Main thread: all threads started up."));
5765 wxThread::Sleep(500);
5768 // now wake one of them up
5769 wxPrintf(_T("Main thread: about to signal the condition.\n"));
5774 wxThread::Sleep(200);
5776 // wake all the (remaining) threads up, so that they can exit
5777 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
5779 condition
.Broadcast();
5781 // give them time to terminate (dirty!)
5782 wxThread::Sleep(500);
5785 #include "wx/utils.h"
5787 class MyExecThread
: public wxThread
5790 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
5796 virtual ExitCode
Entry()
5798 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
5805 static void TestThreadExec()
5807 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
5809 MyExecThread
thread(_T("true"));
5812 wxPrintf(_T("Main program exit code: %ld.\n"),
5813 wxExecute(_T("false"), wxEXEC_SYNC
));
5815 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
5819 #include "wx/datetime.h"
5821 class MySemaphoreThread
: public wxThread
5824 MySemaphoreThread(int i
, wxSemaphore
*sem
)
5825 : wxThread(wxTHREAD_JOINABLE
),
5832 virtual ExitCode
Entry()
5834 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
5835 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5839 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
5840 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5844 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
5845 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5857 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
5859 static void TestSemaphore()
5861 wxPuts(_T("*** Testing wxSemaphore class. ***"));
5863 static const int SEM_LIMIT
= 3;
5865 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
5866 ArrayThreads threads
;
5868 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
5870 threads
.Add(new MySemaphoreThread(i
, &sem
));
5871 threads
.Last()->Run();
5874 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
5881 #endif // TEST_THREADS
5883 // ----------------------------------------------------------------------------
5885 // ----------------------------------------------------------------------------
5889 #include "wx/timer.h"
5890 #include "wx/tokenzr.h"
5892 static void TestStringConstruction()
5894 wxPuts(_T("*** Testing wxString constructores ***"));
5896 #define TEST_CTOR(args, res) \
5899 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
5902 wxPuts(_T("(ok)")); \
5906 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
5910 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
5911 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
5912 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
5913 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
5915 static const wxChar
*s
= _T("?really!");
5916 const wxChar
*start
= wxStrchr(s
, _T('r'));
5917 const wxChar
*end
= wxStrchr(s
, _T('!'));
5918 TEST_CTOR((start
, end
), _T("really"));
5920 wxPuts(wxEmptyString
);
5923 static void TestString()
5933 for (int i
= 0; i
< 1000000; ++i
)
5937 c
= _T("! How'ya doin'?");
5940 c
= _T("Hello world! What's up?");
5945 wxPrintf(_T("TestString elapsed time: %ld\n"), sw
.Time());
5948 static void TestPChar()
5956 for (int i
= 0; i
< 1000000; ++i
)
5958 wxStrcpy (a
, _T("Hello"));
5959 wxStrcpy (b
, _T(" world"));
5960 wxStrcpy (c
, _T("! How'ya doin'?"));
5963 wxStrcpy (c
, _T("Hello world! What's up?"));
5964 if (wxStrcmp (c
, a
) == 0)
5965 wxStrcpy (c
, _T("Doh!"));
5968 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw
.Time());
5971 static void TestStringSub()
5973 wxString
s(_T("Hello, world!"));
5975 wxPuts(_T("*** Testing wxString substring extraction ***"));
5977 wxPrintf(_T("String = '%s'\n"), s
.c_str());
5978 wxPrintf(_T("Left(5) = '%s'\n"), s
.Left(5).c_str());
5979 wxPrintf(_T("Right(6) = '%s'\n"), s
.Right(6).c_str());
5980 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
5981 wxPrintf(_T("Mid(3) = '%s'\n"), s
.Mid(3).c_str());
5982 wxPrintf(_T("substr(3, 5) = '%s'\n"), s
.substr(3, 5).c_str());
5983 wxPrintf(_T("substr(3) = '%s'\n"), s
.substr(3).c_str());
5985 static const wxChar
*prefixes
[] =
5989 _T("Hello, world!"),
5990 _T("Hello, world!!!"),
5996 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
5998 wxString prefix
= prefixes
[n
], rest
;
5999 bool rc
= s
.StartsWith(prefix
, &rest
);
6000 wxPrintf(_T("StartsWith('%s') = %s"), prefix
.c_str(), rc
? _T("true") : _T("false"));
6003 wxPrintf(_T(" (the rest is '%s')\n"), rest
.c_str());
6011 wxPuts(wxEmptyString
);
6014 static void TestStringFormat()
6016 wxPuts(_T("*** Testing wxString formatting ***"));
6019 s
.Printf(_T("%03d"), 18);
6021 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6022 wxPrintf(_T("Number 18: %s\n"), s
.c_str());
6024 wxPuts(wxEmptyString
);
6027 // returns "not found" for npos, value for all others
6028 static wxString
PosToString(size_t res
)
6030 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
6031 : wxString::Format(_T("%u"), res
);
6035 static void TestStringFind()
6037 wxPuts(_T("*** Testing wxString find() functions ***"));
6039 static const wxChar
*strToFind
= _T("ell");
6040 static const struct StringFindTest
6044 result
; // of searching "ell" in str
6047 { _T("Well, hello world"), 0, 1 },
6048 { _T("Well, hello world"), 6, 7 },
6049 { _T("Well, hello world"), 9, wxString::npos
},
6052 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
6054 const StringFindTest
& ft
= findTestData
[n
];
6055 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
6057 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6058 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
6060 size_t resTrue
= ft
.result
;
6061 if ( res
== resTrue
)
6067 wxPrintf(_T("(ERROR: should be %s)\n"),
6068 PosToString(resTrue
).c_str());
6072 wxPuts(wxEmptyString
);
6075 static void TestStringTokenizer()
6077 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6079 static const wxChar
*modeNames
[] =
6083 _T("return all empty"),
6088 static const struct StringTokenizerTest
6090 const wxChar
*str
; // string to tokenize
6091 const wxChar
*delims
; // delimiters to use
6092 size_t count
; // count of token
6093 wxStringTokenizerMode mode
; // how should we tokenize it
6094 } tokenizerTestData
[] =
6096 { _T(""), _T(" "), 0 },
6097 { _T("Hello, world"), _T(" "), 2 },
6098 { _T("Hello, world "), _T(" "), 2 },
6099 { _T("Hello, world"), _T(","), 2 },
6100 { _T("Hello, world!"), _T(",!"), 2 },
6101 { _T("Hello,, world!"), _T(",!"), 3 },
6102 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
6103 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6104 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
6105 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
6106 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
6107 { _T("01/02/99"), _T("/-"), 3 },
6108 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
6111 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
6113 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
6114 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
6116 size_t count
= tkz
.CountTokens();
6117 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6118 MakePrintable(tt
.str
).c_str(),
6120 MakePrintable(tt
.delims
).c_str(),
6121 modeNames
[tkz
.GetMode()]);
6122 if ( count
== tt
.count
)
6128 wxPrintf(_T("(ERROR: should be %u)\n"), tt
.count
);
6133 // if we emulate strtok(), check that we do it correctly
6134 wxChar
*buf
, *s
= NULL
, *last
;
6136 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
6138 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
6139 wxStrcpy(buf
, tt
.str
);
6141 s
= wxStrtok(buf
, tt
.delims
, &last
);
6148 // now show the tokens themselves
6150 while ( tkz
.HasMoreTokens() )
6152 wxString token
= tkz
.GetNextToken();
6154 wxPrintf(_T("\ttoken %u: '%s'"),
6156 MakePrintable(token
).c_str());
6162 wxPuts(_T(" (ok)"));
6166 wxPrintf(_T(" (ERROR: should be %s)\n"), s
);
6169 s
= wxStrtok(NULL
, tt
.delims
, &last
);
6173 // nothing to compare with
6174 wxPuts(wxEmptyString
);
6178 if ( count2
!= count
)
6180 wxPuts(_T("\tERROR: token count mismatch"));
6186 wxPuts(wxEmptyString
);
6189 static void TestStringReplace()
6191 wxPuts(_T("*** Testing wxString::replace ***"));
6193 static const struct StringReplaceTestData
6195 const wxChar
*original
; // original test string
6196 size_t start
, len
; // the part to replace
6197 const wxChar
*replacement
; // the replacement string
6198 const wxChar
*result
; // and the expected result
6199 } stringReplaceTestData
[] =
6201 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6202 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6203 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6204 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6205 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6208 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
6210 const StringReplaceTestData data
= stringReplaceTestData
[n
];
6212 wxString original
= data
.original
;
6213 original
.replace(data
.start
, data
.len
, data
.replacement
);
6215 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6216 data
.original
, data
.start
, data
.len
, data
.replacement
,
6219 if ( original
== data
.result
)
6225 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
6229 wxPuts(wxEmptyString
);
6233 static void TestStringMatch()
6235 wxPuts(_T("*** Testing wxString::Matches() ***"));
6237 static const struct StringMatchTestData
6240 const wxChar
*wildcard
;
6242 } stringMatchTestData
[] =
6244 { _T("foobar"), _T("foo*"), 1 },
6245 { _T("foobar"), _T("*oo*"), 1 },
6246 { _T("foobar"), _T("*bar"), 1 },
6247 { _T("foobar"), _T("??????"), 1 },
6248 { _T("foobar"), _T("f??b*"), 1 },
6249 { _T("foobar"), _T("f?b*"), 0 },
6250 { _T("foobar"), _T("*goo*"), 0 },
6251 { _T("foobar"), _T("*foo"), 0 },
6252 { _T("foobarfoo"), _T("*foo"), 1 },
6253 { _T(""), _T("*"), 1 },
6254 { _T(""), _T("?"), 0 },
6257 for ( size_t n
= 0; n
< WXSIZEOF(stringMatchTestData
); n
++ )
6259 const StringMatchTestData
& data
= stringMatchTestData
[n
];
6260 bool matches
= wxString(data
.text
).Matches(data
.wildcard
);
6261 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6263 matches
? _T("matches") : _T("doesn't match"),
6265 matches
== data
.matches
? _T("ok") : _T("ERROR"));
6268 wxPuts(wxEmptyString
);
6272 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6273 void ok(int line
, bool ok
, const wxString
& msg
= wxEmptyString
)
6276 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line
) +
6280 void is(int line
, const wxString
& got
, const wxString
& expected
,
6281 const wxString
& msg
= wxEmptyString
)
6283 bool isOk
= got
== expected
;
6284 ok(line
, isOk
, msg
);
6287 wxPuts(_T("Got: ") + got
);
6288 wxPuts(_T("Expected: ") + expected
);
6293 void is(int line
, const wxChar
* got
, const wxChar
* expected
,
6294 const wxString
& msg
= wxEmptyString
)
6296 bool isOk
= wxStrcmp( got
, expected
) == 0;
6297 ok(line
, isOk
, msg
);
6300 wxPuts(_T("Got: ") + wxString(got
));
6301 wxPuts(_T("Expected: ") + wxString(expected
));
6306 void is(int line
, const wxChar
& got
, const wxChar
& expected
,
6307 const wxString
& msg
= wxEmptyString
)
6309 bool isOk
= got
== expected
;
6310 ok(line
, isOk
, msg
);
6313 wxPuts(_T("Got: ") + got
);
6314 wxPuts(_T("Expected: ") + expected
);
6318 void is(int line
, size_t got
, size_t expected
,
6319 const wxString
& msg
= wxEmptyString
)
6321 bool isOk
= got
== expected
;
6322 ok(line
, isOk
, msg
);
6325 wxPuts(wxString::Format(_T("Got: %d"), got
));
6326 wxPuts(wxString::Format(_T("Expected: %d"), expected
));
6330 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6331 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6333 void TestStdString()
6335 wxPuts(_T("*** Testing std::string operations ***\n"));
6338 wxString
s1(_T("abcdefgh")),
6339 s2(_T("abcdefghijklm"), 8),
6340 s3(_T("abcdefghijklm")),
6344 s7(s3
.begin(), s3
.begin() + 8);
6345 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
6347 is( __LINE__
, s1
, _T("abcdefgh") );
6348 is( __LINE__
, s2
, s1
);
6349 is( __LINE__
, s4
, _T("aaaaaaaa") );
6350 is( __LINE__
, s5
, _T("abcdefgh") );
6351 is( __LINE__
, s6
, s1
);
6352 is( __LINE__
, s7
, s1
);
6353 is( __LINE__
, s8
, _T("efgh") );
6356 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6357 s1
.append(_T("def"));
6358 s2
.append(_T("defgh"), 3);
6359 s3
.append(wxString(_T("abcdef")), 3, 6);
6361 s5
.append(3, _T('a'));
6362 s6
.append(s1
.begin() + 3, s1
.end());
6364 is( __LINE__
, s1
, _T("abcdef") );
6365 is( __LINE__
, s2
, _T("abcdef") );
6366 is( __LINE__
, s3
, _T("abcdef") );
6367 is( __LINE__
, s4
, _T("abcabcdef") );
6368 is( __LINE__
, s5
, _T("abcaaa") );
6369 is( __LINE__
, s6
, _T("abcdef") );
6372 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6373 s1
.assign(_T("def"));
6374 s2
.assign(_T("defgh"), 3);
6375 s3
.assign(wxString(_T("abcdef")), 3, 6);
6377 s5
.assign(3, _T('a'));
6378 s6
.assign(s1
.begin() + 1, s1
.end());
6380 is( __LINE__
, s1
, _T("def") );
6381 is( __LINE__
, s2
, _T("def") );
6382 is( __LINE__
, s3
, _T("def") );
6383 is( __LINE__
, s4
, _T("def") );
6384 is( __LINE__
, s5
, _T("aaa") );
6385 is( __LINE__
, s6
, _T("ef") );
6388 s1
= _T("abcdefgh");
6389 s2
= _T("abcdefgh");
6391 s4
= _T("abcdefghi");
6394 s7
= _T("zabcdefg");
6396 ok( __LINE__
, s1
.compare(s2
) == 0 );
6397 ok( __LINE__
, s1
.compare(s3
) > 0 );
6398 ok( __LINE__
, s1
.compare(s4
) < 0 );
6399 ok( __LINE__
, s1
.compare(s5
) > 0 );
6400 ok( __LINE__
, s1
.compare(s6
) < 0 );
6401 ok( __LINE__
, s1
.compare(1, 12, s1
) > 0);
6402 ok( __LINE__
, s1
.compare(_T("abcdefgh")) == 0);
6403 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh")) == 0);
6404 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
6409 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
6410 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
6411 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
6413 is( __LINE__
, s1
, _T("acdefgh") );
6414 is( __LINE__
, s2
, _T("abcd") );
6415 is( __LINE__
, s3
, _T("ac") );
6416 is( __LINE__
, s4
, _T("abcdghi") );
6417 is( __LINE__
, s7
, _T("zabc") );
6418 is( __LINE__
, *it
, _T('c') );
6419 is( __LINE__
, *it2
, _T('g') );
6420 ok( __LINE__
, it3
== s7
.end() );
6424 // 01234567890123456789012345
6425 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6428 is_nom( s1
.find(_T('A')), 7u );
6429 is_nom( s1
.find(_T('A'), 7), 7u );
6430 is_nom( s1
.find(_T('Z')), wxString::npos
);
6431 is_nom( s1
.find(_T('C'), 22), 25u );
6433 is_nom( s1
.find(_T("gAB")), 6u );
6434 is_nom( s1
.find(_T("gAB"), 7), wxString::npos
);
6435 is_nom( s1
.find(_T("gAB"), 6), 6u );
6437 is_nom( s1
.find(_T("gABZZZ"), 2, 3), 6u );
6438 is_nom( s1
.find(_T("gABZZZ"), 7, 3), wxString::npos
);
6440 is_nom( s1
.find(s2
), 6u );
6441 is_nom( s1
.find(s2
, 7), wxString::npos
);
6442 is_nom( s1
.find(s2
, 6), 6u );
6444 // find_first_not_of
6446 // 01234567890123456789012345678901234
6447 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6450 is_nom( s1
.find_first_not_of(_T('a')), 6u );
6451 is_nom( s1
.find_first_not_of(_T('a'), 7), 7u );
6452 is_nom( s2
.find_first_not_of(_T('a')), wxString::npos
);
6454 is_nom( s1
.find_first_not_of(_T("abde"), 4), 7u );
6455 is_nom( s1
.find_first_not_of(_T("abde"), 7), 7u );
6456 is_nom( s1
.find_first_not_of(_T("abcdefghijkl")), wxString::npos
);
6458 is_nom( s1
.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
6461 is_nom( s1
.find_first_of(_T('c')), 7u );
6462 is_nom( s1
.find_first_of(_T('v')), wxString::npos
);
6463 is_nom( s1
.find_first_of(_T('c'), 10), 24u );
6465 is_nom( s1
.find_first_of(_T("ijkl")), 13u );
6466 is_nom( s1
.find_first_of(_T("ddcfg"), 17), 24u );
6467 is_nom( s1
.find_first_of(_T("ddcfga"), 17, 5), 24u );
6471 // 01234567890123456789012345678901234
6472 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6475 is_nom( s2
.find_last_not_of(_T('a')), wxString::npos
);
6476 is_nom( s1
.find_last_not_of(_T('d')), 33u );
6477 is_nom( s1
.find_last_not_of(_T('d'), 25), 24u );
6479 is_nom( s1
.find_last_not_of(_T("bcd")), 22u );
6480 is_nom( s1
.find_last_not_of(_T("abc"), 24), 16u );
6482 is_nom( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
6485 is_nom( s2
.find_last_of(_T('c')), wxString::npos
);
6486 is_nom( s1
.find_last_of(_T('a')), 22u );
6487 is_nom( s1
.find_last_of(_T('b'), 24), 23u );
6489 is_nom( s1
.find_last_of(_T("ijklm")), 16u );
6490 is_nom( s1
.find_last_of(_T("ijklma"), 33, 4), 16u );
6491 is_nom( s1
.find_last_of(_T("a"), 17), 17u );
6494 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
6495 s9
= s10
= _T("cdefg");
6497 s1
.insert(1, _T("cc") );
6498 s2
.insert(2, _T("cdef"), 3);
6500 s4
.insert(2, s10
, 3, 7);
6501 s5
.insert(1, 2, _T('c'));
6502 it
= s6
.insert(s6
.begin() + 3, _T('X'));
6503 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
6504 s8
.insert(s8
.begin(), 2, _T('c'));
6506 is( __LINE__
, s1
, _T("accaaa") );
6507 is( __LINE__
, s2
, _T("aacdeaa") );
6508 is( __LINE__
, s3
, _T("aacdefgaa") );
6509 is( __LINE__
, s4
, _T("aafgaa") );
6510 is( __LINE__
, s5
, _T("accaaa") );
6511 is( __LINE__
, s6
, _T("aaaXa") );
6512 is( __LINE__
, s7
, _T("cdefaaaa") );
6513 is( __LINE__
, s8
, _T("ccaaaa") );
6515 s1
= s2
= s3
= _T("aaaa");
6516 s1
.insert(0, _T("ccc"), 2);
6517 s2
.insert(4, _T("ccc"), 2);
6519 is( __LINE__
, s1
, _T("ccaaaa") );
6520 is( __LINE__
, s2
, _T("aaaacc") );
6523 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
6524 s9
= s10
= _T("werty");
6526 s1
.replace(3, 4, _T("rtyu"));
6527 s1
.replace(8, 7, _T("opopop"));
6528 s2
.replace(10, 12, _T("WWWW"));
6529 s3
.replace(1, 5, s9
);
6530 s4
.replace(1, 4, s9
, 0, 4);
6531 s5
.replace(1, 2, s9
, 1, 12);
6532 s6
.replace(0, 123, s9
, 0, 123);
6533 s7
.replace(2, 7, s9
);
6535 is( __LINE__
, s1
, _T("QWErtyuIopopop") );
6536 is( __LINE__
, s2
, _T("QWERTYUIOPWWWW") );
6537 is( __LINE__
, s3
, _T("QwertyUIOP") );
6538 is( __LINE__
, s4
, _T("QwertYUIOP") );
6539 is( __LINE__
, s5
, _T("QertyRTYUIOP") );
6540 is( __LINE__
, s6
, s9
);
6541 is( __LINE__
, s7
, _T("QWwertyP") );
6545 // 01234567890123456789012345
6546 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6550 is_nom( s1
.rfind(_T('A')), 23u );
6551 is_nom( s1
.rfind(_T('A'), 7), 7u );
6552 is_nom( s1
.rfind(_T('Z')), wxString::npos
);
6553 is_nom( s1
.rfind(_T('C'), 22), 19u );
6555 is_nom( s1
.rfind(_T("cAB")), 22u );
6556 is_nom( s1
.rfind(_T("cAB"), 15), wxString::npos
);
6557 is_nom( s1
.rfind(_T("cAB"), 21), 16u );
6559 is_nom( s1
.rfind(_T("gABZZZ"), 7, 3), 6u );
6560 is_nom( s1
.rfind(_T("gABZZZ"), 5, 3), wxString::npos
);
6562 is_nom( s1
.rfind(s2
), 6u );
6563 is_nom( s1
.rfind(s2
, 5), wxString::npos
);
6564 is_nom( s1
.rfind(s2
, 6), 6u );
6565 is_nom( s1
.rfind(s3
, 1), 0u );
6568 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
6572 s3
.resize( 14, _T(' ') );
6573 s4
.resize( 14, _T('W') );
6575 is_nom( s1
, _T("abcABCdefDEF") );
6576 is_nom( s2
, _T("abcABCdefD") );
6577 is_nom( s3
, _T("abcABCdefDEF ") );
6578 is_nom( s4
, _T("abcABCdefDEFWW") );
6581 s1
= _T("abcdefgABCDEFG");
6583 is_nom( s1
.substr( 0, 14 ), s1
);
6584 is_nom( s1
.substr( 1, 13 ), _T("bcdefgABCDEFG") );
6585 is_nom( s1
.substr( 1, 20 ), _T("bcdefgABCDEFG") );
6586 is_nom( s1
.substr( 14, 30 ), _T("") );
6588 wxPuts(_T("*** Testing std::string operations finished ***\n"));
6591 #endif // TEST_STRINGS
6593 // ----------------------------------------------------------------------------
6595 // ----------------------------------------------------------------------------
6597 #ifdef TEST_SNGLINST
6598 #include "wx/snglinst.h"
6599 #endif // TEST_SNGLINST
6601 int main(int argc
, char **argv
)
6603 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
6605 wxInitializer initializer
;
6608 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
6613 #ifdef TEST_SNGLINST
6614 wxSingleInstanceChecker checker
;
6615 if ( checker
.Create(_T(".wxconsole.lock")) )
6617 if ( checker
.IsAnotherRunning() )
6619 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
6624 // wait some time to give time to launch another instance
6625 wxPrintf(_T("Press \"Enter\" to continue..."));
6628 else // failed to create
6630 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
6632 #endif // TEST_SNGLINST
6636 #endif // TEST_CHARSET
6639 TestCmdLineConvert();
6641 #if wxUSE_CMDLINE_PARSER
6642 static const wxCmdLineEntryDesc cmdLineDesc
[] =
6644 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
6645 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
6646 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
6647 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
6649 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
6650 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
6651 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
6652 wxCMD_LINE_VAL_NUMBER
},
6653 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
6654 wxCMD_LINE_VAL_DATE
},
6656 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
6657 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
6663 wxChar
**wargv
= new wxChar
*[argc
+ 1];
6668 for (n
= 0; n
< argc
; n
++ )
6670 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
6671 wargv
[n
] = wxStrdup(warg
);
6678 #endif // wxUSE_UNICODE
6680 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
6684 for ( int n
= 0; n
< argc
; n
++ )
6689 #endif // wxUSE_UNICODE
6691 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
6692 wxCMD_LINE_VAL_STRING
,
6693 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
6695 switch ( parser
.Parse() )
6698 wxLogMessage(_T("Help was given, terminating."));
6702 ShowCmdLine(parser
);
6706 wxLogMessage(_T("Syntax error detected, aborting."));
6709 #endif // wxUSE_CMDLINE_PARSER
6711 #endif // TEST_CMDLINE
6718 TestStringConstruction();
6721 TestStringTokenizer();
6722 TestStringReplace();
6728 #endif // TEST_STRINGS
6738 #ifdef TEST_DLLLOADER
6740 #endif // TEST_DLLLOADER
6744 #endif // TEST_ENVIRON
6748 #endif // TEST_EXECUTE
6750 #ifdef TEST_FILECONF
6752 #endif // TEST_FILECONF
6761 #endif // TEST_LOCALE
6764 wxPuts(_T("*** Testing wxLog ***"));
6767 for ( size_t n
= 0; n
< 8000; n
++ )
6769 s
<< (wxChar
)(_T('A') + (n
% 26));
6772 wxLogWarning(_T("The length of the string is %lu"),
6773 (unsigned long)s
.length());
6776 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
6778 // this one shouldn't be truncated
6781 // but this one will because log functions use fixed size buffer
6782 // (note that it doesn't need '\n' at the end neither - will be added
6784 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
6793 #ifdef TEST_FILENAME
6794 TestFileNameConstruction();
6795 TestFileNameMakeRelative();
6796 TestFileNameMakeAbsolute();
6797 TestFileNameSplit();
6800 TestFileNameDirManip();
6801 TestFileNameComparison();
6802 TestFileNameOperations();
6803 #endif // TEST_FILENAME
6805 #ifdef TEST_FILETIME
6810 #endif // TEST_FILETIME
6813 wxLog::AddTraceMask(FTP_TRACE_MASK
);
6814 if ( TestFtpConnect() )
6824 #if TEST_INTERACTIVE
6825 TestFtpInteractive();
6828 //else: connecting to the FTP server failed
6841 #endif // TEST_HASHMAP
6845 #endif // TEST_HASHSET
6848 wxLog::AddTraceMask(_T("mime"));
6852 TestMimeAssociate();
6857 #ifdef TEST_INFO_FUNCTIONS
6862 #if TEST_INTERACTIVE
6866 #endif // TEST_INFO_FUNCTIONS
6868 #ifdef TEST_PATHLIST
6870 #endif // TEST_PATHLIST
6878 #endif // TEST_PRINTF
6885 #endif // TEST_REGCONF
6888 // TODO: write a real test using src/regex/tests file
6892 TestRegExSubmatch();
6893 TestRegExReplacement();
6895 #if TEST_INTERACTIVE
6896 TestRegExInteractive();
6899 #endif // TEST_REGEX
6901 #ifdef TEST_REGISTRY
6903 TestRegistryAssociation();
6904 #endif // TEST_REGISTRY
6909 #endif // TEST_SOCKETS
6916 #endif // TEST_STREAMS
6918 #ifdef TEST_TEXTSTREAM
6919 TestTextInputStream();
6920 #endif // TEST_TEXTSTREAM
6923 int nCPUs
= wxThread::GetCPUCount();
6924 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
6926 wxThread::SetConcurrency(nCPUs
);
6928 TestJoinableThreads();
6931 TestJoinableThreads();
6932 TestDetachedThreads();
6933 TestThreadSuspend();
6935 TestThreadConditions();
6939 #endif // TEST_THREADS
6943 #endif // TEST_TIMER
6945 #ifdef TEST_DATETIME
6957 TestTimeArithmetics();
6960 TestTimeSpanFormat();
6966 #if TEST_INTERACTIVE
6967 TestDateTimeInteractive();
6969 #endif // TEST_DATETIME
6971 #ifdef TEST_SCOPEGUARD
6976 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
6978 #endif // TEST_USLEEP
6983 #endif // TEST_VCARD
6987 #endif // TEST_VOLUME
6990 TestUnicodeTextFileRead();
6992 TestUnicodeToFromAscii();
6994 #endif // TEST_UNICODE
6998 TestEncodingConverter();
6999 #endif // TEST_WCHAR
7002 TestZipStreamRead();
7003 TestZipFileSystem();
7007 TestZlibStreamWrite();
7008 TestZlibStreamRead();