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.
59 #define TEST_DLLLOADER
66 // #define TEST_FTP --FIXME! (RN)
70 #define TEST_INFO_FUNCTIONS
82 #define TEST_SCOPEGUARD
84 // #define TEST_SOCKETS --FIXME! (RN)
87 #define TEST_TEXTSTREAM
91 // #define TEST_VCARD -- don't enable this (VZ)
92 // #define TEST_VOLUME --FIXME! (RN)
100 #define TEST_FILENAME
104 // some tests are interactive, define this to run them
105 #ifdef TEST_INTERACTIVE
106 #undef TEST_INTERACTIVE
108 #define TEST_INTERACTIVE 1
110 #define TEST_INTERACTIVE 0
113 // ----------------------------------------------------------------------------
114 // test class for container objects
115 // ----------------------------------------------------------------------------
117 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
119 class Bar
// Foo is already taken in the hash test
122 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
123 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
124 ~Bar() { ms_bars
--; }
126 static size_t GetNumber() { return ms_bars
; }
128 const wxChar
*GetName() const { return m_name
; }
133 static size_t ms_bars
;
136 size_t Bar::ms_bars
= 0;
138 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
150 // replace TABs with \t and CRs with \n
151 static wxString
MakePrintable(const wxChar
*s
)
154 (void)str
.Replace(_T("\t"), _T("\\t"));
155 (void)str
.Replace(_T("\n"), _T("\\n"));
156 (void)str
.Replace(_T("\r"), _T("\\r"));
161 #endif // MakePrintable() is used
163 // ----------------------------------------------------------------------------
164 // wxFontMapper::CharsetToEncoding
165 // ----------------------------------------------------------------------------
169 #include "wx/fontmap.h"
171 static void TestCharset()
173 static const wxChar
*charsets
[] =
175 // some vali charsets
184 // and now some bogus ones
191 for ( size_t n
= 0; n
< WXSIZEOF(charsets
); n
++ )
193 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charsets
[n
]);
194 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
196 wxFontMapper::Get()->GetEncodingName(enc
).c_str(),
197 wxFontMapper::Get()->GetEncodingDescription(enc
).c_str());
201 #endif // TEST_CHARSET
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
209 #include "wx/cmdline.h"
210 #include "wx/datetime.h"
212 #if wxUSE_CMDLINE_PARSER
214 static void ShowCmdLine(const wxCmdLineParser
& parser
)
216 wxString s
= _T("Input files: ");
218 size_t count
= parser
.GetParamCount();
219 for ( size_t param
= 0; param
< count
; param
++ )
221 s
<< parser
.GetParam(param
) << ' ';
225 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
226 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
231 if ( parser
.Found(_T("o"), &strVal
) )
232 s
<< _T("Output file:\t") << strVal
<< '\n';
233 if ( parser
.Found(_T("i"), &strVal
) )
234 s
<< _T("Input dir:\t") << strVal
<< '\n';
235 if ( parser
.Found(_T("s"), &lVal
) )
236 s
<< _T("Size:\t") << lVal
<< '\n';
237 if ( parser
.Found(_T("d"), &dt
) )
238 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
239 if ( parser
.Found(_T("project_name"), &strVal
) )
240 s
<< _T("Project:\t") << strVal
<< '\n';
245 #endif // wxUSE_CMDLINE_PARSER
247 static void TestCmdLineConvert()
249 static const wxChar
*cmdlines
[] =
252 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
253 _T("literal \\\" and \"\""),
256 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
258 const wxChar
*cmdline
= cmdlines
[n
];
259 wxPrintf(_T("Parsing: %s\n"), cmdline
);
260 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
262 size_t count
= args
.GetCount();
263 wxPrintf(_T("\targc = %u\n"), count
);
264 for ( size_t arg
= 0; arg
< count
; arg
++ )
266 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
271 #endif // TEST_CMDLINE
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
282 static const wxChar
*ROOTDIR
= _T("/");
283 static const wxChar
*TESTDIR
= _T("/usr/local/share");
284 #elif defined(__WXMSW__)
285 static const wxChar
*ROOTDIR
= _T("c:\\");
286 static const wxChar
*TESTDIR
= _T("d:\\");
288 #error "don't know where the root directory is"
291 static void TestDirEnumHelper(wxDir
& dir
,
292 int flags
= wxDIR_DEFAULT
,
293 const wxString
& filespec
= wxEmptyString
)
297 if ( !dir
.IsOpened() )
300 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
303 wxPrintf(_T("\t%s\n"), filename
.c_str());
305 cont
= dir
.GetNext(&filename
);
308 wxPuts(wxEmptyString
);
311 static void TestDirEnum()
313 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
315 wxString cwd
= wxGetCwd();
316 if ( !wxDir::Exists(cwd
) )
318 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
323 if ( !dir
.IsOpened() )
325 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
329 wxPuts(_T("Enumerating everything in current directory:"));
330 TestDirEnumHelper(dir
);
332 wxPuts(_T("Enumerating really everything in current directory:"));
333 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
335 wxPuts(_T("Enumerating object files in current directory:"));
336 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
338 wxPuts(_T("Enumerating directories in current directory:"));
339 TestDirEnumHelper(dir
, wxDIR_DIRS
);
341 wxPuts(_T("Enumerating files in current directory:"));
342 TestDirEnumHelper(dir
, wxDIR_FILES
);
344 wxPuts(_T("Enumerating files including hidden in current directory:"));
345 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
349 wxPuts(_T("Enumerating everything in root directory:"));
350 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
352 wxPuts(_T("Enumerating directories in root directory:"));
353 TestDirEnumHelper(dir
, wxDIR_DIRS
);
355 wxPuts(_T("Enumerating files in root directory:"));
356 TestDirEnumHelper(dir
, wxDIR_FILES
);
358 wxPuts(_T("Enumerating files including hidden in root directory:"));
359 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
361 wxPuts(_T("Enumerating files in non existing directory:"));
362 wxDir
dirNo(_T("nosuchdir"));
363 TestDirEnumHelper(dirNo
);
366 class DirPrintTraverser
: public wxDirTraverser
369 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
371 return wxDIR_CONTINUE
;
374 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
376 wxString path
, name
, ext
;
377 wxSplitPath(dirname
, &path
, &name
, &ext
);
380 name
<< _T('.') << ext
;
383 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
385 if ( wxIsPathSeparator(*p
) )
389 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
391 return wxDIR_CONTINUE
;
395 static void TestDirTraverse()
397 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
401 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
402 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
405 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
406 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
409 // enum again with custom traverser
410 wxPuts(_T("Now enumerating directories:"));
412 DirPrintTraverser traverser
;
413 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
416 static void TestDirExists()
418 wxPuts(_T("*** Testing wxDir::Exists() ***"));
420 static const wxChar
*dirnames
[] =
423 #if defined(__WXMSW__)
426 _T("\\\\share\\file"),
430 _T("c:\\autoexec.bat"),
431 #elif defined(__UNIX__)
440 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
442 wxPrintf(_T("%-40s: %s\n"),
444 wxDir::Exists(dirnames
[n
]) ? _T("exists")
445 : _T("doesn't exist"));
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 #ifdef TEST_DLLLOADER
457 #include "wx/dynlib.h"
459 static void TestDllLoad()
461 #if defined(__WXMSW__)
462 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
463 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
464 #elif defined(__UNIX__)
465 // weird: using just libc.so does *not* work!
466 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
467 static const wxChar
*FUNC_NAME
= _T("strlen");
469 #error "don't know how to test wxDllLoader on this platform"
472 wxPuts(_T("*** testing wxDllLoader ***\n"));
474 wxDynamicLibrary
lib(LIB_NAME
);
475 if ( !lib
.IsLoaded() )
477 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
481 typedef int (*wxStrlenType
)(const char *);
482 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
485 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
486 FUNC_NAME
, LIB_NAME
);
490 if ( pfnStrlen("foo") != 3 )
492 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
496 wxPuts(_T("... ok"));
502 #endif // TEST_DLLLOADER
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
510 #include "wx/utils.h"
512 static wxString
MyGetEnv(const wxString
& var
)
515 if ( !wxGetEnv(var
, &val
) )
518 val
= wxString(_T('\'')) + val
+ _T('\'');
523 static void TestEnvironment()
525 const wxChar
*var
= _T("wxTestVar");
527 wxPuts(_T("*** testing environment access functions ***"));
529 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
530 wxSetEnv(var
, _T("value for wxTestVar"));
531 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
532 wxSetEnv(var
, _T("another value"));
533 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
535 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
536 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
539 #endif // TEST_ENVIRON
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
547 #include "wx/utils.h"
549 static void TestExecute()
551 wxPuts(_T("*** testing wxExecute ***"));
554 #define COMMAND "cat -n ../../Makefile" // "echo hi"
555 #define SHELL_COMMAND "echo hi from shell"
556 #define REDIRECT_COMMAND COMMAND // "date"
557 #elif defined(__WXMSW__)
558 #define COMMAND "command.com /c echo hi"
559 #define SHELL_COMMAND "echo hi"
560 #define REDIRECT_COMMAND COMMAND
562 #error "no command to exec"
565 wxPrintf(_T("Testing wxShell: "));
567 if ( wxShell(_T(SHELL_COMMAND
)) )
570 wxPuts(_T("ERROR."));
572 wxPrintf(_T("Testing wxExecute: "));
574 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
577 wxPuts(_T("ERROR."));
579 #if 0 // no, it doesn't work (yet?)
580 wxPrintf(_T("Testing async wxExecute: "));
582 if ( wxExecute(COMMAND
) != 0 )
583 wxPuts(_T("Ok (command launched)."));
585 wxPuts(_T("ERROR."));
588 wxPrintf(_T("Testing wxExecute with redirection:\n"));
589 wxArrayString output
;
590 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
592 wxPuts(_T("ERROR."));
596 size_t count
= output
.GetCount();
597 for ( size_t n
= 0; n
< count
; n
++ )
599 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
606 #endif // TEST_EXECUTE
608 // ----------------------------------------------------------------------------
610 // ----------------------------------------------------------------------------
615 #include "wx/ffile.h"
616 #include "wx/textfile.h"
618 static void TestFileRead()
620 wxPuts(_T("*** wxFile read test ***"));
622 wxFile
file(_T("testdata.fc"));
623 if ( file
.IsOpened() )
625 wxPrintf(_T("File length: %lu\n"), file
.Length());
627 wxPuts(_T("File dump:\n----------"));
629 static const off_t len
= 1024;
633 off_t nRead
= file
.Read(buf
, len
);
634 if ( nRead
== wxInvalidOffset
)
636 wxPrintf(_T("Failed to read the file."));
640 fwrite(buf
, nRead
, 1, stdout
);
646 wxPuts(_T("----------"));
650 wxPrintf(_T("ERROR: can't open test file.\n"));
653 wxPuts(wxEmptyString
);
656 static void TestTextFileRead()
658 wxPuts(_T("*** wxTextFile read test ***"));
660 wxTextFile
file(_T("testdata.fc"));
663 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
664 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
668 wxPuts(_T("\nDumping the entire file:"));
669 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
671 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
673 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
675 wxPuts(_T("\nAnd now backwards:"));
676 for ( s
= file
.GetLastLine();
677 file
.GetCurrentLine() != 0;
678 s
= file
.GetPrevLine() )
680 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
682 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
686 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
689 wxPuts(wxEmptyString
);
692 static void TestFileCopy()
694 wxPuts(_T("*** Testing wxCopyFile ***"));
696 static const wxChar
*filename1
= _T("testdata.fc");
697 static const wxChar
*filename2
= _T("test2");
698 if ( !wxCopyFile(filename1
, filename2
) )
700 wxPuts(_T("ERROR: failed to copy file"));
704 wxFFile
f1(filename1
, _T("rb")),
705 f2(filename2
, _T("rb"));
707 if ( !f1
.IsOpened() || !f2
.IsOpened() )
709 wxPuts(_T("ERROR: failed to open file(s)"));
714 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
716 wxPuts(_T("ERROR: failed to read file(s)"));
720 if ( (s1
.length() != s2
.length()) ||
721 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
723 wxPuts(_T("ERROR: copy error!"));
727 wxPuts(_T("File was copied ok."));
733 if ( !wxRemoveFile(filename2
) )
735 wxPuts(_T("ERROR: failed to remove the file"));
738 wxPuts(wxEmptyString
);
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
749 #include "wx/confbase.h"
750 #include "wx/fileconf.h"
752 static const struct FileConfTestData
754 const wxChar
*name
; // value name
755 const wxChar
*value
; // the value from the file
758 { _T("value1"), _T("one") },
759 { _T("value2"), _T("two") },
760 { _T("novalue"), _T("default") },
763 static void TestFileConfRead()
765 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
767 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
768 _T("testdata.fc"), wxEmptyString
,
769 wxCONFIG_USE_RELATIVE_PATH
);
771 // test simple reading
772 wxPuts(_T("\nReading config file:"));
773 wxString
defValue(_T("default")), value
;
774 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
776 const FileConfTestData
& data
= fcTestData
[n
];
777 value
= fileconf
.Read(data
.name
, defValue
);
778 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
779 if ( value
== data
.value
)
785 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
789 // test enumerating the entries
790 wxPuts(_T("\nEnumerating all root entries:"));
793 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
796 wxPrintf(_T("\t%s = %s\n"),
798 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
800 cont
= fileconf
.GetNextEntry(name
, dummy
);
803 static const wxChar
*testEntry
= _T("TestEntry");
804 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
805 fileconf
.Write(testEntry
, _T("A value"));
806 fileconf
.DeleteEntry(testEntry
);
807 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
810 #endif // TEST_FILECONF
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
818 #include "wx/filename.h"
821 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
825 wxString full
= fn
.GetFullPath();
827 wxString vol
, path
, name
, ext
;
828 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
830 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
831 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
833 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
834 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
835 path
.c_str(), name
.c_str(), ext
.c_str());
837 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
838 wxPrintf(_T("with volume: \t'%s'\n"),
839 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
840 wxPrintf(_T("with separator:\t'%s'\n"),
841 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
842 wxPrintf(_T("with both: \t'%s'\n"),
843 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
845 wxPuts(_T("The directories in the path are:"));
846 wxArrayString dirs
= fn
.GetDirs();
847 size_t count
= dirs
.GetCount();
848 for ( size_t n
= 0; n
< count
; n
++ )
850 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
855 static struct FileNameInfo
857 const wxChar
*fullname
;
858 const wxChar
*volume
;
867 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
868 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
869 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
870 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
871 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
872 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
873 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
874 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
875 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
877 // Windows file names
878 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
879 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
880 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
881 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
882 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
883 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
884 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
886 // wxFileName support for Mac file names is broken currently
889 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
890 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
891 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
892 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
893 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
894 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
898 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
899 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
902 static void TestFileNameConstruction()
904 wxPuts(_T("*** testing wxFileName construction ***"));
906 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
908 const FileNameInfo
& fni
= filenames
[n
];
910 wxFileName
fn(fni
.fullname
, fni
.format
);
912 wxString fullname
= fn
.GetFullPath(fni
.format
);
913 if ( fullname
!= fni
.fullname
)
915 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni
.fullname
);
918 bool isAbsolute
= fn
.IsAbsolute(fni
.format
);
919 wxPrintf(_T("'%s' is %s (%s)\n\t"),
921 isAbsolute
? "absolute" : "relative",
922 isAbsolute
== fni
.isAbsolute
? "ok" : "ERROR");
924 if ( !fn
.Normalize(wxPATH_NORM_ALL
, wxEmptyString
, fni
.format
) )
926 wxPuts(_T("ERROR (couldn't be normalized)"));
930 wxPrintf(_T("normalized: '%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
934 wxPuts(wxEmptyString
);
937 static void TestFileNameSplit()
939 wxPuts(_T("*** testing wxFileName splitting ***"));
941 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
943 const FileNameInfo
& fni
= filenames
[n
];
944 wxString volume
, path
, name
, ext
;
945 wxFileName::SplitPath(fni
.fullname
,
946 &volume
, &path
, &name
, &ext
, fni
.format
);
948 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
950 volume
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
952 if ( volume
!= fni
.volume
)
953 wxPrintf(_T(" (ERROR: volume = '%s')"), fni
.volume
);
954 if ( path
!= fni
.path
)
955 wxPrintf(_T(" (ERROR: path = '%s')"), fni
.path
);
956 if ( name
!= fni
.name
)
957 wxPrintf(_T(" (ERROR: name = '%s')"), fni
.name
);
958 if ( ext
!= fni
.ext
)
959 wxPrintf(_T(" (ERROR: ext = '%s')"), fni
.ext
);
961 wxPuts(wxEmptyString
);
965 static void TestFileNameTemp()
967 wxPuts(_T("*** testing wxFileName temp file creation ***"));
969 static const wxChar
*tmpprefixes
[] =
977 _T("/tmp/foo/bar"), // this one must be an error
981 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
983 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
986 // "error" is not in upper case because it may be ok
987 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
991 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
992 tmpprefixes
[n
], path
.c_str());
994 if ( !wxRemoveFile(path
) )
996 wxLogWarning(_T("Failed to remove temp file '%s'"),
1003 static void TestFileNameMakeRelative()
1005 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
1007 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1009 const FileNameInfo
& fni
= filenames
[n
];
1011 wxFileName
fn(fni
.fullname
, fni
.format
);
1013 // choose the base dir of the same format
1015 switch ( fni
.format
)
1018 base
= _T("/usr/bin/");
1027 // TODO: I don't know how this is supposed to work there
1030 case wxPATH_NATIVE
: // make gcc happy
1032 wxFAIL_MSG( _T("unexpected path format") );
1035 wxPrintf(_T("'%s' relative to '%s': "),
1036 fn
.GetFullPath(fni
.format
).c_str(), base
.c_str());
1038 if ( !fn
.MakeRelativeTo(base
, fni
.format
) )
1040 wxPuts(_T("unchanged"));
1044 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1049 static void TestFileNameMakeAbsolute()
1051 wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
1053 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1055 const FileNameInfo
& fni
= filenames
[n
];
1056 wxFileName
fn(fni
.fullname
, fni
.format
);
1058 wxPrintf(_T("'%s' absolutized: "),
1059 fn
.GetFullPath(fni
.format
).c_str());
1061 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1064 wxPuts(wxEmptyString
);
1067 static void TestFileNameDirManip()
1069 // TODO: test AppendDir(), RemoveDir(), ...
1072 static void TestFileNameComparison()
1077 static void TestFileNameOperations()
1082 static void TestFileNameCwd()
1087 #endif // TEST_FILENAME
1089 // ----------------------------------------------------------------------------
1090 // wxFileName time functions
1091 // ----------------------------------------------------------------------------
1093 #ifdef TEST_FILETIME
1095 #include <wx/filename.h>
1096 #include <wx/datetime.h>
1098 static void TestFileGetTimes()
1100 wxFileName
fn(_T("testdata.fc"));
1102 wxDateTime dtAccess
, dtMod
, dtCreate
;
1103 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
1105 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1109 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
1111 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
1112 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
1113 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
1114 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
1119 static void TestFileSetTimes()
1121 wxFileName
fn(_T("testdata.fc"));
1125 wxPrintf(_T("ERROR: Touch() failed.\n"));
1130 #endif // TEST_FILETIME
1132 // ----------------------------------------------------------------------------
1134 // ----------------------------------------------------------------------------
1138 #include "wx/hash.h"
1142 Foo(int n_
) { n
= n_
; count
++; }
1147 static size_t count
;
1150 size_t Foo::count
= 0;
1152 WX_DECLARE_LIST(Foo
, wxListFoos
);
1153 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
1155 #include "wx/listimpl.cpp"
1157 WX_DEFINE_LIST(wxListFoos
);
1159 #include "wx/timer.h"
1161 static void TestHash()
1163 wxPuts(_T("*** Testing wxHashTable ***\n"));
1164 const int COUNT
= 100;
1171 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
1175 for ( i
= 0; i
< COUNT
; ++i
)
1176 hash
.Put(i
, &o
+ i
);
1179 wxHashTable::compatibility_iterator it
= hash
.Next();
1189 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1191 for ( i
= 99; i
>= 0; --i
)
1192 if( hash
.Get(i
) != &o
+ i
)
1193 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1195 for ( i
= 0; i
< COUNT
; ++i
)
1196 hash
.Put(i
, &o
+ i
+ 20);
1198 for ( i
= 99; i
>= 0; --i
)
1199 if( hash
.Get(i
) != &o
+ i
)
1200 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1202 for ( i
= 0; i
< COUNT
/2; ++i
)
1203 if( hash
.Delete(i
) != &o
+ i
)
1204 wxPuts(_T("Error in wxHashTable::Delete\n"));
1206 for ( i
= COUNT
/2; i
< COUNT
; ++i
)
1207 if( hash
.Get(i
) != &o
+ i
)
1208 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1210 for ( i
= 0; i
< COUNT
/2; ++i
)
1211 if( hash
.Get(i
) != &o
+ i
+ 20)
1212 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1214 for ( i
= 0; i
< COUNT
/2; ++i
)
1215 if( hash
.Delete(i
) != &o
+ i
+ 20)
1216 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1218 for ( i
= 0; i
< COUNT
/2; ++i
)
1219 if( hash
.Get(i
) != NULL
)
1220 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1222 hash2
.Put(_T("foo"), &o
+ 1);
1223 hash2
.Put(_T("bar"), &o
+ 2);
1224 hash2
.Put(_T("baz"), &o
+ 3);
1226 if (hash2
.Get(_T("moo")) != NULL
)
1227 wxPuts(_T("Error in wxHashTable::Get\n"));
1229 if (hash2
.Get(_T("bar")) != &o
+ 2)
1230 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1232 hash2
.Put(_T("bar"), &o
+ 0);
1234 if (hash2
.Get(_T("bar")) != &o
+ 2)
1235 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1238 // and now some corner-case testing; 3 and 13 hash to the same bucket
1240 wxHashTable
hash(wxKEY_INTEGER
, 10);
1243 hash
.Put(3, &dummy
);
1246 if (hash
.Get(3) != NULL
)
1247 wxPuts(_T("Corner case 1 failure\n"));
1249 hash
.Put(3, &dummy
);
1250 hash
.Put(13, &dummy
);
1253 if (hash
.Get(3) != NULL
)
1254 wxPuts(_T("Corner case 2 failure\n"));
1258 if (hash
.Get(13) != NULL
)
1259 wxPuts(_T("Corner case 3 failure\n"));
1261 hash
.Put(3, &dummy
);
1262 hash
.Put(13, &dummy
);
1265 if (hash
.Get(13) != NULL
)
1266 wxPuts(_T("Corner case 4 failure\n"));
1270 if (hash
.Get(3) != NULL
)
1271 wxPuts(_T("Corner case 5 failure\n"));
1275 wxHashTable
hash(wxKEY_INTEGER
, 10);
1278 hash
.Put(3, 7, &dummy
+ 7);
1279 hash
.Put(4, 8, &dummy
+ 8);
1281 if (hash
.Get(7) != NULL
) wxPuts(_T("Key/Hash 1 failure\n"));
1282 if (hash
.Get(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 2 failure\n"));
1283 if (hash
.Get(4) != NULL
) wxPuts(_T("Key/Hash 3 failure\n"));
1284 if (hash
.Get(3) != NULL
) wxPuts(_T("Key/Hash 4 failure\n"));
1285 if (hash
.Get(8) != NULL
) wxPuts(_T("Key/Hash 5 failure\n"));
1286 if (hash
.Get(8, 4) != NULL
) wxPuts(_T("Key/Hash 6 failure\n"));
1288 if (hash
.Delete(7) != NULL
) wxPuts(_T("Key/Hash 7 failure\n"));
1289 if (hash
.Delete(3) != NULL
) wxPuts(_T("Key/Hash 8 failure\n"));
1290 if (hash
.Delete(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 8 failure\n"));
1295 hash
.DeleteContents(true);
1297 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1298 hash
.GetCount(), Foo::count
);
1300 static const int hashTestData
[] =
1302 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1306 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1308 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
1311 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1312 hash
.GetCount(), Foo::count
);
1314 wxPuts(_T("Hash access test:"));
1315 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1317 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1318 hashTestData
[n
], n
);
1319 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
1322 wxPrintf(_T("ERROR, not found.\n"));
1326 wxPrintf(_T("%d (%s)\n"), foo
->n
,
1327 (size_t)foo
->n
== n
? "ok" : "ERROR");
1331 wxPrintf(_T("\nTrying to get an element not in hash: "));
1333 if ( hash
.Get(1234) || hash
.Get(1, 0) )
1335 wxPuts(_T("ERROR: found!"));
1339 wxPuts(_T("ok (not found)"));
1342 Foo
* foo
= hash
.Delete(0);
1344 wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count
);
1348 wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count
);
1351 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count
);
1352 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1354 wxPrintf(_T("Time: %ld\n"), sw
.Time());
1359 // ----------------------------------------------------------------------------
1361 // ----------------------------------------------------------------------------
1365 #include "wx/hashmap.h"
1367 // test compilation of basic map types
1368 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
1369 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
1370 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
1371 myUnsignedHashMap
);
1372 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1374 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1376 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1378 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1382 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1383 // myStringHashMap );
1384 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
1386 typedef myStringHashMap::iterator Itor
;
1388 static void TestHashMap()
1390 wxPuts(_T("*** Testing wxHashMap ***\n"));
1391 myStringHashMap
sh(0); // as small as possible
1394 const size_t count
= 10000;
1396 // init with some data
1397 for( i
= 0; i
< count
; ++i
)
1399 buf
.Printf(wxT("%d"), i
);
1400 sh
[buf
] = wxT("A") + buf
+ wxT("C");
1403 // test that insertion worked
1404 if( sh
.size() != count
)
1406 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh
.size(), count
);
1409 for( i
= 0; i
< count
; ++i
)
1411 buf
.Printf(wxT("%d"), i
);
1412 if( sh
[buf
] != wxT("A") + buf
+ wxT("C") )
1414 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1419 // check that iterators work
1421 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1425 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1429 if( it
->second
!= sh
[it
->first
] )
1431 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1436 if( sh
.size() != i
)
1438 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i
, count
);
1441 // test copy ctor, assignment operator
1442 myStringHashMap
h1( sh
), h2( 0 );
1445 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1447 if( h1
[it
->first
] != it
->second
)
1449 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it
->first
.c_str());
1452 if( h2
[it
->first
] != it
->second
)
1454 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it
->first
.c_str());
1459 for( i
= 0; i
< count
; ++i
)
1461 buf
.Printf(wxT("%d"), i
);
1462 size_t sz
= sh
.size();
1464 // test find() and erase(it)
1467 it
= sh
.find( buf
);
1468 if( it
!= sh
.end() )
1472 if( sh
.find( buf
) != sh
.end() )
1474 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1478 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i
);
1483 size_t c
= sh
.erase( buf
);
1485 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1487 if( sh
.find( buf
) != sh
.end() )
1489 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1493 // count should decrease
1494 if( sh
.size() != sz
- 1 )
1496 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1500 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1503 #endif // TEST_HASHMAP
1505 // ----------------------------------------------------------------------------
1507 // ----------------------------------------------------------------------------
1511 #include "wx/hashset.h"
1513 // test compilation of basic map types
1514 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
1515 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
1516 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
1517 myUnsignedHashSet
);
1518 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
1520 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
1522 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
1524 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
1526 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
1538 unsigned long operator()(const MyStruct
& s
) const
1539 { return m_dummy(s
.ptr
); }
1540 MyHash
& operator=(const MyHash
&) { return *this; }
1542 wxPointerHash m_dummy
;
1548 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
1549 { return s1
.ptr
== s2
.ptr
; }
1550 MyEqual
& operator=(const MyEqual
&) { return *this; }
1553 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
1555 typedef myTestHashSet5 wxStringHashSet
;
1557 static void TestHashSet()
1559 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1561 wxStringHashSet set1
;
1563 set1
.insert( _T("abc") );
1564 set1
.insert( _T("bbc") );
1565 set1
.insert( _T("cbc") );
1566 set1
.insert( _T("abc") );
1568 if( set1
.size() != 3 )
1569 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1575 tmp
.ptr
= &dummy
; tmp
.str
= _T("ABC");
1577 tmp
.ptr
= &dummy
+ 1;
1579 tmp
.ptr
= &dummy
; tmp
.str
= _T("CDE");
1582 if( set2
.size() != 2 )
1583 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1585 mySet::iterator it
= set2
.find( tmp
);
1587 if( it
== set2
.end() )
1588 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1589 if( it
->ptr
!= &dummy
)
1590 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1591 if( it
->str
!= _T("ABC") )
1592 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1594 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1597 #endif // TEST_HASHSET
1599 // ----------------------------------------------------------------------------
1601 // ----------------------------------------------------------------------------
1605 #include "wx/list.h"
1607 WX_DECLARE_LIST(Bar
, wxListBars
);
1608 #include "wx/listimpl.cpp"
1609 WX_DEFINE_LIST(wxListBars
);
1611 WX_DECLARE_LIST(int, wxListInt
);
1612 WX_DEFINE_LIST(wxListInt
);
1614 static void TestList()
1616 wxPuts(_T("*** Testing wxList operations ***\n"));
1622 for ( i
= 0; i
< 5; ++i
)
1623 list1
.Append(dummy
+ i
);
1625 if ( list1
.GetCount() != 5 )
1626 wxPuts(_T("Wrong number of items in list\n"));
1628 if ( list1
.Item(3)->GetData() != dummy
+ 3 )
1629 wxPuts(_T("Error in Item()\n"));
1631 if ( !list1
.Find(dummy
+ 4) )
1632 wxPuts(_T("Error in Find()\n"));
1634 wxListInt::compatibility_iterator node
= list1
.GetFirst();
1639 if ( node
->GetData() != dummy
+ i
)
1640 wxPuts(_T("Error in compatibility_iterator\n"));
1641 node
= node
->GetNext();
1645 if ( size_t(i
) != list1
.GetCount() )
1646 wxPuts(_T("Error in compatibility_iterator\n"));
1648 list1
.Insert(dummy
+ 0);
1649 list1
.Insert(1, dummy
+ 1);
1650 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
1652 node
= list1
.GetFirst();
1657 int* t
= node
->GetData();
1658 if ( t
!= dummy
+ i
)
1659 wxPuts(_T("Error in Insert\n"));
1660 node
= node
->GetNext();
1665 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1667 wxPuts(_T("*** Testing std::list operations ***\n"));
1671 wxListInt::iterator it
, en
;
1672 wxListInt::reverse_iterator rit
, ren
;
1674 for ( i
= 0; i
< 5; ++i
)
1675 list1
.push_back(i
+ &i
);
1677 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
1678 it
!= en
; ++it
, ++i
)
1679 if ( *it
!= i
+ &i
)
1680 wxPuts(_T("Error in iterator\n"));
1682 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
1683 rit
!= ren
; ++rit
, --i
)
1684 if ( *rit
!= i
+ &i
)
1685 wxPuts(_T("Error in reverse_iterator\n"));
1687 if ( *list1
.rbegin() != *--list1
.end() ||
1688 *list1
.begin() != *--list1
.rend() )
1689 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1690 if ( *list1
.begin() != *--++list1
.begin() ||
1691 *list1
.rbegin() != *--++list1
.rbegin() )
1692 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1694 if ( list1
.front() != &i
|| list1
.back() != &i
+ 4 )
1695 wxPuts(_T("Error in front()/back()\n"));
1697 list1
.erase(list1
.begin());
1698 list1
.erase(--list1
.end());
1700 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
1701 it
!= en
; ++it
, ++i
)
1702 if ( *it
!= i
+ &i
)
1703 wxPuts(_T("Error in erase()\n"));
1706 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1709 static void TestListCtor()
1711 wxPuts(_T("*** Testing wxList construction ***\n"));
1715 list1
.Append(new Bar(_T("first")));
1716 list1
.Append(new Bar(_T("second")));
1718 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1719 list1
.GetCount(), Bar::GetNumber());
1724 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1725 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
1728 list1
.DeleteContents(true);
1730 WX_CLEAR_LIST(wxListBars
, list1
);
1734 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1739 // ----------------------------------------------------------------------------
1741 // ----------------------------------------------------------------------------
1745 #include "wx/intl.h"
1746 #include "wx/utils.h" // for wxSetEnv
1748 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
1750 // find the name of the language from its value
1751 static const wxChar
*GetLangName(int lang
)
1753 static const wxChar
*languageNames
[] =
1763 _T("ARABIC_ALGERIA"),
1764 _T("ARABIC_BAHRAIN"),
1767 _T("ARABIC_JORDAN"),
1768 _T("ARABIC_KUWAIT"),
1769 _T("ARABIC_LEBANON"),
1771 _T("ARABIC_MOROCCO"),
1774 _T("ARABIC_SAUDI_ARABIA"),
1777 _T("ARABIC_TUNISIA"),
1784 _T("AZERI_CYRILLIC"),
1799 _T("CHINESE_SIMPLIFIED"),
1800 _T("CHINESE_TRADITIONAL"),
1801 _T("CHINESE_HONGKONG"),
1802 _T("CHINESE_MACAU"),
1803 _T("CHINESE_SINGAPORE"),
1804 _T("CHINESE_TAIWAN"),
1810 _T("DUTCH_BELGIAN"),
1814 _T("ENGLISH_AUSTRALIA"),
1815 _T("ENGLISH_BELIZE"),
1816 _T("ENGLISH_BOTSWANA"),
1817 _T("ENGLISH_CANADA"),
1818 _T("ENGLISH_CARIBBEAN"),
1819 _T("ENGLISH_DENMARK"),
1821 _T("ENGLISH_JAMAICA"),
1822 _T("ENGLISH_NEW_ZEALAND"),
1823 _T("ENGLISH_PHILIPPINES"),
1824 _T("ENGLISH_SOUTH_AFRICA"),
1825 _T("ENGLISH_TRINIDAD"),
1826 _T("ENGLISH_ZIMBABWE"),
1834 _T("FRENCH_BELGIAN"),
1835 _T("FRENCH_CANADIAN"),
1836 _T("FRENCH_LUXEMBOURG"),
1837 _T("FRENCH_MONACO"),
1843 _T("GERMAN_AUSTRIAN"),
1844 _T("GERMAN_BELGIUM"),
1845 _T("GERMAN_LIECHTENSTEIN"),
1846 _T("GERMAN_LUXEMBOURG"),
1864 _T("ITALIAN_SWISS"),
1869 _T("KASHMIRI_INDIA"),
1887 _T("MALAY_BRUNEI_DARUSSALAM"),
1888 _T("MALAY_MALAYSIA"),
1898 _T("NORWEGIAN_BOKMAL"),
1899 _T("NORWEGIAN_NYNORSK"),
1906 _T("PORTUGUESE_BRAZILIAN"),
1909 _T("RHAETO_ROMANCE"),
1912 _T("RUSSIAN_UKRAINE"),
1918 _T("SERBIAN_CYRILLIC"),
1919 _T("SERBIAN_LATIN"),
1920 _T("SERBO_CROATIAN"),
1931 _T("SPANISH_ARGENTINA"),
1932 _T("SPANISH_BOLIVIA"),
1933 _T("SPANISH_CHILE"),
1934 _T("SPANISH_COLOMBIA"),
1935 _T("SPANISH_COSTA_RICA"),
1936 _T("SPANISH_DOMINICAN_REPUBLIC"),
1937 _T("SPANISH_ECUADOR"),
1938 _T("SPANISH_EL_SALVADOR"),
1939 _T("SPANISH_GUATEMALA"),
1940 _T("SPANISH_HONDURAS"),
1941 _T("SPANISH_MEXICAN"),
1942 _T("SPANISH_MODERN"),
1943 _T("SPANISH_NICARAGUA"),
1944 _T("SPANISH_PANAMA"),
1945 _T("SPANISH_PARAGUAY"),
1947 _T("SPANISH_PUERTO_RICO"),
1948 _T("SPANISH_URUGUAY"),
1950 _T("SPANISH_VENEZUELA"),
1954 _T("SWEDISH_FINLAND"),
1972 _T("URDU_PAKISTAN"),
1974 _T("UZBEK_CYRILLIC"),
1987 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1988 return languageNames
[lang
];
1990 return _T("INVALID");
1993 static void TestDefaultLang()
1995 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1997 static const wxChar
*langStrings
[] =
1999 NULL
, // system default
2006 _T("de_DE.iso88591"),
2008 _T("?"), // invalid lang spec
2009 _T("klingonese"), // I bet on some systems it does exist...
2012 wxPrintf(_T("The default system encoding is %s (%d)\n"),
2013 wxLocale::GetSystemEncodingName().c_str(),
2014 wxLocale::GetSystemEncoding());
2016 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
2018 const wxChar
*langStr
= langStrings
[n
];
2021 // FIXME: this doesn't do anything at all under Windows, we need
2022 // to create a new wxLocale!
2023 wxSetEnv(_T("LC_ALL"), langStr
);
2026 int lang
= gs_localeDefault
.GetSystemLanguage();
2027 wxPrintf(_T("Locale for '%s' is %s.\n"),
2028 langStr
? langStr
: _T("system default"), GetLangName(lang
));
2032 #endif // TEST_LOCALE
2034 // ----------------------------------------------------------------------------
2036 // ----------------------------------------------------------------------------
2040 #include "wx/mimetype.h"
2042 static void TestMimeEnum()
2044 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
2046 wxArrayString mimetypes
;
2048 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
2050 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
2055 for ( size_t n
= 0; n
< count
; n
++ )
2057 wxFileType
*filetype
=
2058 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
2061 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
2062 mimetypes
[n
].c_str());
2066 filetype
->GetDescription(&desc
);
2067 filetype
->GetExtensions(exts
);
2069 filetype
->GetIcon(NULL
);
2072 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
2075 extsAll
<< _T(", ");
2079 wxPrintf(_T("\t%s: %s (%s)\n"),
2080 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
2083 wxPuts(wxEmptyString
);
2086 static void TestMimeOverride()
2088 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2090 static const wxChar
*mailcap
= _T("/tmp/mailcap");
2091 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
2093 if ( wxFile::Exists(mailcap
) )
2094 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2096 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
2098 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2101 if ( wxFile::Exists(mimetypes
) )
2102 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2104 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
2106 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2109 wxPuts(wxEmptyString
);
2112 static void TestMimeFilename()
2114 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2116 static const wxChar
*filenames
[] =
2124 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
2126 const wxString fname
= filenames
[n
];
2127 wxString ext
= fname
.AfterLast(_T('.'));
2128 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
2131 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
2136 if ( !ft
->GetDescription(&desc
) )
2137 desc
= _T("<no description>");
2140 if ( !ft
->GetOpenCommand(&cmd
,
2141 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
2142 cmd
= _T("<no command available>");
2144 cmd
= wxString(_T('"')) + cmd
+ _T('"');
2146 wxPrintf(_T("To open %s (%s) do %s.\n"),
2147 fname
.c_str(), desc
.c_str(), cmd
.c_str());
2153 wxPuts(wxEmptyString
);
2156 static void TestMimeAssociate()
2158 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2160 wxFileTypeInfo
ftInfo(
2161 _T("application/x-xyz"),
2162 _T("xyzview '%s'"), // open cmd
2163 _T(""), // print cmd
2164 _T("XYZ File"), // description
2165 _T(".xyz"), // extensions
2166 NULL
// end of extensions
2168 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
2170 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
2173 wxPuts(_T("ERROR: failed to create association!"));
2177 // TODO: read it back
2181 wxPuts(wxEmptyString
);
2186 // ----------------------------------------------------------------------------
2187 // misc information functions
2188 // ----------------------------------------------------------------------------
2190 #ifdef TEST_INFO_FUNCTIONS
2192 #include "wx/utils.h"
2194 static void TestDiskInfo()
2196 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2200 wxChar pathname
[128];
2201 wxPrintf(_T("\nEnter a directory name: "));
2202 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
2205 // kill the last '\n'
2206 pathname
[wxStrlen(pathname
) - 1] = 0;
2208 wxLongLong total
, free
;
2209 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
2211 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2215 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2216 (total
/ 1024).ToString().c_str(),
2217 (free
/ 1024).ToString().c_str(),
2223 static void TestOsInfo()
2225 wxPuts(_T("*** Testing OS info functions ***\n"));
2228 wxGetOsVersion(&major
, &minor
);
2229 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2230 wxGetOsDescription().c_str(), major
, minor
);
2232 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2234 wxPrintf(_T("Host name is %s (%s).\n"),
2235 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2237 wxPuts(wxEmptyString
);
2240 static void TestUserInfo()
2242 wxPuts(_T("*** Testing user info functions ***\n"));
2244 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2245 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2246 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2247 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2249 wxPuts(wxEmptyString
);
2252 #endif // TEST_INFO_FUNCTIONS
2254 // ----------------------------------------------------------------------------
2256 // ----------------------------------------------------------------------------
2258 #ifdef TEST_LONGLONG
2260 #include "wx/longlong.h"
2261 #include "wx/timer.h"
2263 // make a 64 bit number from 4 16 bit ones
2264 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
2266 // get a random 64 bit number
2267 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
2269 static const long testLongs
[] =
2280 #if wxUSE_LONGLONG_WX
2281 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
2282 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2283 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
2284 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2285 #endif // wxUSE_LONGLONG_WX
2288 static void TestSpeed()
2290 static const long max
= 100000000;
2297 for ( n
= 0; n
< max
; n
++ )
2303 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw
.Time());
2306 #if wxUSE_LONGLONG_NATIVE
2311 for ( n
= 0; n
< max
; n
++ )
2317 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw
.Time());
2319 #endif // wxUSE_LONGLONG_NATIVE
2325 for ( n
= 0; n
< max
; n
++ )
2330 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw
.Time());
2335 static void TestLongLongConversion()
2337 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2340 for ( size_t n
= 0; n
< 100000; n
++ )
2342 #if wxUSE_LONGLONG_NATIVE
2343 wxLongLong a
= RAND_LL();
2345 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
2348 wxPuts( _T("conversions failure") );
2350 wxPuts(_T("Can't do it without native long long type, test skipped."));
2353 #endif // wxUSE_LONGLONG_NATIVE
2355 if ( !(nTested
% 1000) )
2364 wxPuts(_T(" done!"));
2367 static void TestMultiplication()
2369 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2372 for ( size_t n
= 0; n
< 100000; n
++ )
2374 #if wxUSE_LONGLONG_NATIVE
2375 wxLongLong a
= RAND_LL();
2376 wxLongLong b
= RAND_LL();
2378 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
2379 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
2382 wxPuts( _T("multiplication failure") );
2383 #else // !wxUSE_LONGLONG_NATIVE
2384 wxPuts(_T("Can't do it without native long long type, test skipped."));
2387 #endif // wxUSE_LONGLONG_NATIVE
2389 if ( !(nTested
% 1000) )
2398 wxPuts(_T(" done!"));
2401 static void TestDivision()
2403 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2407 for ( size_t n
= 0; n
< 100000; n
++ )
2409 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2410 // multiplication will not overflow)
2411 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
2413 // get a random (but non null) long (not wxLongLong for now) to divide
2425 #if wxUSE_LONGLONG_NATIVE
2426 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
2428 wxLongLongNative p
= m
/ l
, s
= m
% l
;
2430 if(q
!= p
|| r
!= s
)
2431 wxPuts( _T("division failure") );
2432 #else // !wxUSE_LONGLONG_NATIVE
2433 // verify the result
2434 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
2435 #endif // wxUSE_LONGLONG_NATIVE
2437 if ( !(nTested
% 1000) )
2446 wxPuts(_T(" done!"));
2449 static void TestAddition()
2451 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2455 for ( size_t n
= 0; n
< 100000; n
++ )
2461 #if wxUSE_LONGLONG_NATIVE
2462 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
2463 wxLongLongNative(b
.GetHi(), b
.GetLo()),
2464 _T("addition failure") );
2465 #else // !wxUSE_LONGLONG_NATIVE
2466 wxASSERT_MSG( c
- b
== a
, "addition failure" );
2467 #endif // wxUSE_LONGLONG_NATIVE
2469 if ( !(nTested
% 1000) )
2478 wxPuts(_T(" done!"));
2481 static void TestBitOperations()
2483 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2487 for ( size_t n
= 0; n
< 100000; n
++ )
2491 #if wxUSE_LONGLONG_NATIVE
2492 for ( size_t n
= 0; n
< 33; n
++ )
2495 #else // !wxUSE_LONGLONG_NATIVE
2496 wxPuts(_T("Can't do it without native long long type, test skipped."));
2499 #endif // wxUSE_LONGLONG_NATIVE
2501 if ( !(nTested
% 1000) )
2510 wxPuts(_T(" done!"));
2513 static void TestLongLongComparison()
2515 #if wxUSE_LONGLONG_WX
2516 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
2518 static const long ls
[2] =
2524 wxLongLongWx lls
[2];
2528 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2532 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
2534 res
= lls
[m
] > testLongs
[n
];
2535 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
2536 ls
[m
], testLongs
[n
], res
? "true" : "false",
2537 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
2539 res
= lls
[m
] < testLongs
[n
];
2540 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
2541 ls
[m
], testLongs
[n
], res
? "true" : "false",
2542 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
2544 res
= lls
[m
] == testLongs
[n
];
2545 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
2546 ls
[m
], testLongs
[n
], res
? "true" : "false",
2547 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
2550 #endif // wxUSE_LONGLONG_WX
2553 static void TestLongLongToString()
2555 wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
2557 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2559 wxLongLong ll
= testLongs
[n
];
2560 wxPrintf(_T("%ld == %s\n"), testLongs
[n
], ll
.ToString().c_str());
2563 wxLongLong
ll(0x12345678, 0x87654321);
2564 wxPrintf(_T("0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2567 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2570 static void TestLongLongPrintf()
2572 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2574 #ifdef wxLongLongFmtSpec
2576 wxLongLong ll
= wxLL(0x1234567890abcdef);
2577 wxString s
= wxString::Format(_T("%") wxLongLongFmtSpec
_T("x"), ll
);
2579 wxString s
= _T("MinGW compiler does not allow wxLongLong in '...'");
2581 wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
2582 s
.c_str(), s
== _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
2583 #else // !wxLongLongFmtSpec
2584 #error "wxLongLongFmtSpec not defined for this compiler/platform"
2591 #endif // TEST_LONGLONG
2593 // ----------------------------------------------------------------------------
2595 // ----------------------------------------------------------------------------
2597 #ifdef TEST_PATHLIST
2600 #define CMD_IN_PATH _T("ls")
2602 #define CMD_IN_PATH _T("command.com")
2605 static void TestPathList()
2607 wxPuts(_T("*** Testing wxPathList ***\n"));
2609 wxPathList pathlist
;
2610 pathlist
.AddEnvList(_T("PATH"));
2611 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
2614 wxPrintf(_T("ERROR: command not found in the path.\n"));
2618 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
2622 #endif // TEST_PATHLIST
2624 // ----------------------------------------------------------------------------
2625 // regular expressions
2626 // ----------------------------------------------------------------------------
2630 #include "wx/regex.h"
2632 static void TestRegExCompile()
2634 wxPuts(_T("*** Testing RE compilation ***\n"));
2636 static struct RegExCompTestData
2638 const wxChar
*pattern
;
2640 } regExCompTestData
[] =
2642 { _T("foo"), true },
2643 { _T("foo("), false },
2644 { _T("foo(bar"), false },
2645 { _T("foo(bar)"), true },
2646 { _T("foo["), false },
2647 { _T("foo[bar"), false },
2648 { _T("foo[bar]"), true },
2649 { _T("foo{"), true },
2650 { _T("foo{1"), false },
2651 { _T("foo{bar"), true },
2652 { _T("foo{1}"), true },
2653 { _T("foo{1,2}"), true },
2654 { _T("foo{bar}"), true },
2655 { _T("foo*"), true },
2656 { _T("foo**"), false },
2657 { _T("foo+"), true },
2658 { _T("foo++"), false },
2659 { _T("foo?"), true },
2660 { _T("foo??"), false },
2661 { _T("foo?+"), false },
2665 for ( size_t n
= 0; n
< WXSIZEOF(regExCompTestData
); n
++ )
2667 const RegExCompTestData
& data
= regExCompTestData
[n
];
2668 bool ok
= re
.Compile(data
.pattern
);
2670 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2672 ok
? wxEmptyString
: _T("not "),
2673 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2677 static void TestRegExMatch()
2679 wxPuts(_T("*** Testing RE matching ***\n"));
2681 static struct RegExMatchTestData
2683 const wxChar
*pattern
;
2686 } regExMatchTestData
[] =
2688 { _T("foo"), _T("bar"), false },
2689 { _T("foo"), _T("foobar"), true },
2690 { _T("^foo"), _T("foobar"), true },
2691 { _T("^foo"), _T("barfoo"), false },
2692 { _T("bar$"), _T("barbar"), true },
2693 { _T("bar$"), _T("barbar "), false },
2696 for ( size_t n
= 0; n
< WXSIZEOF(regExMatchTestData
); n
++ )
2698 const RegExMatchTestData
& data
= regExMatchTestData
[n
];
2700 wxRegEx
re(data
.pattern
);
2701 bool ok
= re
.Matches(data
.text
);
2703 wxPrintf(_T("'%s' %s %s (%s)\n"),
2705 ok
? _T("matches") : _T("doesn't match"),
2707 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2711 static void TestRegExSubmatch()
2713 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2715 wxRegEx
re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2716 if ( !re
.IsValid() )
2718 wxPuts(_T("ERROR: compilation failed."));
2722 wxString text
= _T("Fri Jul 13 18:37:52 CEST 2001");
2724 if ( !re
.Matches(text
) )
2726 wxPuts(_T("ERROR: match expected."));
2730 wxPrintf(_T("Entire match: %s\n"), re
.GetMatch(text
).c_str());
2732 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2733 re
.GetMatch(text
, 3).c_str(),
2734 re
.GetMatch(text
, 2).c_str(),
2735 re
.GetMatch(text
, 4).c_str(),
2736 re
.GetMatch(text
, 1).c_str());
2740 static void TestRegExReplacement()
2742 wxPuts(_T("*** Testing RE replacement ***"));
2744 static struct RegExReplTestData
2748 const wxChar
*result
;
2750 } regExReplTestData
[] =
2752 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2753 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2754 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2755 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2756 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2757 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2758 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2761 const wxChar
*pattern
= _T("([a-z]+)[^0-9]*([0-9]+)");
2762 wxRegEx
re(pattern
);
2764 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern
);
2766 for ( size_t n
= 0; n
< WXSIZEOF(regExReplTestData
); n
++ )
2768 const RegExReplTestData
& data
= regExReplTestData
[n
];
2770 wxString text
= data
.text
;
2771 size_t nRepl
= re
.Replace(&text
, data
.repl
);
2773 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2774 data
.text
, data
.repl
,
2775 nRepl
, nRepl
== 1 ? wxEmptyString
: _T("es"),
2777 if ( text
== data
.result
&& nRepl
== data
.count
)
2783 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2784 data
.count
, data
.result
);
2789 static void TestRegExInteractive()
2791 wxPuts(_T("*** Testing RE interactively ***"));
2795 wxChar pattern
[128];
2796 wxPrintf(_T("\nEnter a pattern: "));
2797 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
2800 // kill the last '\n'
2801 pattern
[wxStrlen(pattern
) - 1] = 0;
2804 if ( !re
.Compile(pattern
) )
2812 wxPrintf(_T("Enter text to match: "));
2813 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
2816 // kill the last '\n'
2817 text
[wxStrlen(text
) - 1] = 0;
2819 if ( !re
.Matches(text
) )
2821 wxPrintf(_T("No match.\n"));
2825 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
2828 for ( size_t n
= 1; ; n
++ )
2830 if ( !re
.GetMatch(&start
, &len
, n
) )
2835 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2836 n
, wxString(text
+ start
, len
).c_str());
2843 #endif // TEST_REGEX
2845 // ----------------------------------------------------------------------------
2847 // ----------------------------------------------------------------------------
2857 static void TestDbOpen()
2865 // ----------------------------------------------------------------------------
2867 // ----------------------------------------------------------------------------
2870 NB: this stuff was taken from the glibc test suite and modified to build
2871 in wxWindows: if I read the copyright below properly, this shouldn't
2877 #ifdef wxTEST_PRINTF
2878 // use our functions from wxchar.cpp
2882 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2883 // in the tests below
2884 int wxPrintf( const wxChar
*format
, ... );
2885 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
2888 #include "wx/longlong.h"
2892 static void rfg1 (void);
2893 static void rfg2 (void);
2897 fmtchk (const wxChar
*fmt
)
2899 (void) wxPrintf(_T("%s:\t`"), fmt
);
2900 (void) wxPrintf(fmt
, 0x12);
2901 (void) wxPrintf(_T("'\n"));
2905 fmtst1chk (const wxChar
*fmt
)
2907 (void) wxPrintf(_T("%s:\t`"), fmt
);
2908 (void) wxPrintf(fmt
, 4, 0x12);
2909 (void) wxPrintf(_T("'\n"));
2913 fmtst2chk (const wxChar
*fmt
)
2915 (void) wxPrintf(_T("%s:\t`"), fmt
);
2916 (void) wxPrintf(fmt
, 4, 4, 0x12);
2917 (void) wxPrintf(_T("'\n"));
2920 /* This page is covered by the following copyright: */
2922 /* (C) Copyright C E Chew
2924 * Feel free to copy, use and distribute this software provided:
2926 * 1. you do not pretend that you wrote it
2927 * 2. you leave this copyright notice intact.
2931 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2938 /* Formatted Output Test
2940 * This exercises the output formatting code.
2943 wxChar
*PointerNull
= NULL
;
2950 wxChar
*prefix
= buf
;
2953 wxPuts(_T("\nFormatted output test"));
2954 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2955 wxStrcpy(prefix
, _T("%"));
2956 for (i
= 0; i
< 2; i
++) {
2957 for (j
= 0; j
< 2; j
++) {
2958 for (k
= 0; k
< 2; k
++) {
2959 for (l
= 0; l
< 2; l
++) {
2960 wxStrcpy(prefix
, _T("%"));
2961 if (i
== 0) wxStrcat(prefix
, _T("-"));
2962 if (j
== 0) wxStrcat(prefix
, _T("+"));
2963 if (k
== 0) wxStrcat(prefix
, _T("#"));
2964 if (l
== 0) wxStrcat(prefix
, _T("0"));
2965 wxPrintf(_T("%5s |"), prefix
);
2966 wxStrcpy(tp
, prefix
);
2967 wxStrcat(tp
, _T("6d |"));
2969 wxStrcpy(tp
, prefix
);
2970 wxStrcat(tp
, _T("6o |"));
2972 wxStrcpy(tp
, prefix
);
2973 wxStrcat(tp
, _T("6x |"));
2975 wxStrcpy(tp
, prefix
);
2976 wxStrcat(tp
, _T("6X |"));
2978 wxStrcpy(tp
, prefix
);
2979 wxStrcat(tp
, _T("6u |"));
2986 wxPrintf(_T("%10s\n"), PointerNull
);
2987 wxPrintf(_T("%-10s\n"), PointerNull
);
2990 static void TestPrintf()
2992 static wxChar shortstr
[] = _T("Hi, Z.");
2993 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
2994 I am ready for my first lesson today.");
2996 wxString test_format
;
3000 fmtchk(_T("%4.4x"));
3001 fmtchk(_T("%04.4x"));
3002 fmtchk(_T("%4.3x"));
3003 fmtchk(_T("%04.3x"));
3005 fmtst1chk(_T("%.*x"));
3006 fmtst1chk(_T("%0*x"));
3007 fmtst2chk(_T("%*.*x"));
3008 fmtst2chk(_T("%0*.*x"));
3010 wxString bad_format
= _T("bad format:\t\"%b\"\n");
3011 wxPrintf(bad_format
.c_str());
3012 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
3014 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
3015 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
3016 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
3017 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
3018 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
3019 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
3020 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
3021 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
3022 wxPrintf(test_format
.c_str(), -123456);
3023 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
3024 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
3026 test_format
= _T("zero-padded string:\t\"%010s\"\n");
3027 wxPrintf(test_format
.c_str(), shortstr
);
3028 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
3029 wxPrintf(test_format
.c_str(), shortstr
);
3030 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
3031 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
3032 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
3033 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
3035 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
3036 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
3037 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
3038 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
3039 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
3040 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
3041 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
3042 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
3043 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
3044 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
3045 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
3046 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
3048 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
3049 wxPrintf (_T(" %6.5f\n"), .1);
3050 wxPrintf (_T("x%5.4fx\n"), .5);
3052 wxPrintf (_T("%#03x\n"), 1);
3054 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
3060 while (niter
-- != 0)
3061 wxPrintf (_T("%.17e\n"), d
/ 2);
3066 // Open Watcom cause compiler error here
3067 // Error! E173: col(24) floating-point constant too small to represent
3068 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
3071 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
3072 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
3073 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
3074 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
3075 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
3076 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
3077 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
3078 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
3079 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
3080 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
3085 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
3087 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
3088 rc
, WXSIZEOF(buf
), buf
);
3091 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
3092 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
3098 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
3099 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
3100 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
3101 wxPrintf (_T("%g should be 123.456\n"), 123.456);
3102 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
3103 wxPrintf (_T("%g should be 10\n"), 10.0);
3104 wxPrintf (_T("%g should be 0.02\n"), 0.02);
3108 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
3114 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
3116 result
|= wxStrcmp (buf
,
3117 _T("onetwo three "));
3119 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
3126 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
3128 // for some reason below line fails under Borland
3129 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
3132 if (wxStrcmp (buf
, _T("40000000000")) != 0)
3135 wxPuts (_T("\tFAILED"));
3137 wxUnusedVar(result
);
3138 wxPuts (wxEmptyString
);
3140 #endif // wxLongLong_t
3142 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
3143 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
3145 wxPuts (_T("--- Should be no further output. ---"));
3154 memset (bytes
, '\xff', sizeof bytes
);
3155 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
3156 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
3157 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
3159 wxPuts (_T("%hhn overwrite more bytes"));
3164 wxPuts (_T("%hhn wrote incorrect value"));
3176 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
3177 if (wxStrcmp (buf
, _T(" ")) != 0)
3178 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
3179 wxSprintf (buf
, _T("%5.f"), 33.3);
3180 if (wxStrcmp (buf
, _T(" 33")) != 0)
3181 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
3182 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
3183 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
3184 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
3185 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
3186 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
3187 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
3188 wxSprintf (buf
, _T("%.g"), 33.3);
3189 if (wxStrcmp (buf
, _T("3e+01")) != 0)
3190 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
3191 wxSprintf (buf
, _T("%.G"), 33.3);
3192 if (wxStrcmp (buf
, _T("3E+01")) != 0)
3193 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
3201 wxString test_format
;
3204 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
3205 if (wxStrcmp (buf
, _T("3")) != 0)
3206 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3208 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
3209 if (wxStrcmp (buf
, _T("3")) != 0)
3210 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3212 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
3213 if (wxStrcmp (buf
, _T(" 3")) != 0)
3214 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
3216 test_format
= _T("%04.*o");
3217 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3218 if (wxStrcmp (buf
, _T(" 041")) != 0)
3219 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
3221 test_format
= _T("%09.*u");
3222 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3223 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
3224 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
3226 test_format
= _T("%04.*x");
3227 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3228 if (wxStrcmp (buf
, _T(" 021")) != 0)
3229 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3231 test_format
= _T("%04.*X");
3232 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3233 if (wxStrcmp (buf
, _T(" 021")) != 0)
3234 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3237 #endif // TEST_PRINTF
3239 // ----------------------------------------------------------------------------
3240 // registry and related stuff
3241 // ----------------------------------------------------------------------------
3243 // this is for MSW only
3246 #undef TEST_REGISTRY
3251 #include "wx/confbase.h"
3252 #include "wx/msw/regconf.h"
3255 static void TestRegConfWrite()
3257 wxConfig
*config
= new wxConfig(_T("myapp"));
3258 config
->SetPath(_T("/group1"));
3259 config
->Write(_T("entry1"), _T("foo"));
3260 config
->SetPath(_T("/group2"));
3261 config
->Write(_T("entry1"), _T("bar"));
3265 static void TestRegConfRead()
3267 wxConfig
*config
= new wxConfig(_T("myapp"));
3271 config
->SetPath(_T("/"));
3272 wxPuts(_T("Enumerating / subgroups:"));
3273 bool bCont
= config
->GetFirstGroup(str
, dummy
);
3277 bCont
= config
->GetNextGroup(str
, dummy
);
3281 #endif // TEST_REGCONF
3283 #ifdef TEST_REGISTRY
3285 #include "wx/msw/registry.h"
3287 // I chose this one because I liked its name, but it probably only exists under
3289 static const wxChar
*TESTKEY
=
3290 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
3292 static void TestRegistryRead()
3294 wxPuts(_T("*** testing registry reading ***"));
3296 wxRegKey
key(TESTKEY
);
3297 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
3300 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
3305 size_t nSubKeys
, nValues
;
3306 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
3308 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
3311 wxPrintf(_T("Enumerating values:\n"));
3315 bool cont
= key
.GetFirstValue(value
, dummy
);
3318 wxPrintf(_T("Value '%s': type "), value
.c_str());
3319 switch ( key
.GetValueType(value
) )
3321 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
3322 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
3323 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
3324 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
3325 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
3326 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
3327 default: wxPrintf(_T("other (unknown)")); break;
3330 wxPrintf(_T(", value = "));
3331 if ( key
.IsNumericValue(value
) )
3334 key
.QueryValue(value
, &val
);
3335 wxPrintf(_T("%ld"), val
);
3340 key
.QueryValue(value
, val
);
3341 wxPrintf(_T("'%s'"), val
.c_str());
3343 key
.QueryRawValue(value
, val
);
3344 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
3349 cont
= key
.GetNextValue(value
, dummy
);
3353 static void TestRegistryAssociation()
3356 The second call to deleteself genertaes an error message, with a
3357 messagebox saying .flo is crucial to system operation, while the .ddf
3358 call also fails, but with no error message
3363 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3365 key
= _T("ddxf_auto_file") ;
3366 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3368 key
= _T("ddxf_auto_file") ;
3369 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3371 key
= _T("program,0") ;
3372 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3374 key
= _T("program \"%1\"") ;
3376 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3378 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3380 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3382 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3386 #endif // TEST_REGISTRY
3388 // ----------------------------------------------------------------------------
3390 // ----------------------------------------------------------------------------
3392 #ifdef TEST_SCOPEGUARD
3394 #include "wx/scopeguard.h"
3396 static void function0() { puts("function0()"); }
3397 static void function1(int n
) { printf("function1(%d)\n", n
); }
3398 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
3402 void method0() { printf("method0()\n"); }
3403 void method1(int n
) { printf("method1(%d)\n", n
); }
3404 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
3407 static void TestScopeGuard()
3409 wxON_BLOCK_EXIT0(function0
);
3410 wxON_BLOCK_EXIT1(function1
, 17);
3411 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
3414 wxON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
3415 wxON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
3416 wxON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
3418 wxScopeGuard dismissed
= wxMakeGuard(function0
);
3419 dismissed
.Dismiss();
3424 // ----------------------------------------------------------------------------
3426 // ----------------------------------------------------------------------------
3430 #include "wx/socket.h"
3431 #include "wx/protocol/protocol.h"
3432 #include "wx/protocol/http.h"
3434 static void TestSocketServer()
3436 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3438 static const int PORT
= 3000;
3443 wxSocketServer
*server
= new wxSocketServer(addr
);
3444 if ( !server
->Ok() )
3446 wxPuts(_T("ERROR: failed to bind"));
3454 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
3456 wxSocketBase
*socket
= server
->Accept();
3459 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3463 wxPuts(_T("Server: got a client."));
3465 server
->SetTimeout(60); // 1 min
3468 while ( !close
&& socket
->IsConnected() )
3471 wxChar ch
= _T('\0');
3474 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
3476 // don't log error if the client just close the connection
3477 if ( socket
->IsConnected() )
3479 wxPuts(_T("ERROR: in wxSocket::Read."));
3499 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
3500 if ( s
== _T("close") )
3502 wxPuts(_T("Closing connection"));
3506 else if ( s
== _T("quit") )
3511 wxPuts(_T("Shutting down the server"));
3513 else // not a special command
3515 socket
->Write(s
.MakeUpper().c_str(), s
.length());
3516 socket
->Write("\r\n", 2);
3517 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
3523 wxPuts(_T("Server: lost a client unexpectedly."));
3529 // same as "delete server" but is consistent with GUI programs
3533 static void TestSocketClient()
3535 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3537 static const wxChar
*hostname
= _T("www.wxwindows.org");
3540 addr
.Hostname(hostname
);
3543 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
3545 wxSocketClient client
;
3546 if ( !client
.Connect(addr
) )
3548 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3552 wxPrintf(_T("--- Connected to %s:%u...\n"),
3553 addr
.Hostname().c_str(), addr
.Service());
3557 // could use simply "GET" here I suppose
3559 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
3560 client
.Write(cmdGet
, cmdGet
.length());
3561 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3562 MakePrintable(cmdGet
).c_str());
3563 client
.Read(buf
, WXSIZEOF(buf
));
3564 wxPrintf(_T("--- Server replied:\n%s"), buf
);
3568 #endif // TEST_SOCKETS
3570 // ----------------------------------------------------------------------------
3572 // ----------------------------------------------------------------------------
3576 #include "wx/protocol/ftp.h"
3580 #define FTP_ANONYMOUS
3582 #ifdef FTP_ANONYMOUS
3583 static const wxChar
*directory
= _T("/pub");
3584 static const wxChar
*filename
= _T("welcome.msg");
3586 static const wxChar
*directory
= _T("/etc");
3587 static const wxChar
*filename
= _T("issue");
3590 static bool TestFtpConnect()
3592 wxPuts(_T("*** Testing FTP connect ***"));
3594 #ifdef FTP_ANONYMOUS
3595 static const wxChar
*hostname
= _T("ftp.wxwindows.org");
3597 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
3598 #else // !FTP_ANONYMOUS
3599 static const wxChar
*hostname
= "localhost";
3602 wxFgets(user
, WXSIZEOF(user
), stdin
);
3603 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
3606 wxChar password
[256];
3607 wxPrintf(_T("Password for %s: "), password
);
3608 wxFgets(password
, WXSIZEOF(password
), stdin
);
3609 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
3610 ftp
.SetPassword(password
);
3612 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
3613 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3615 if ( !ftp
.Connect(hostname
) )
3617 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3623 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3624 hostname
, ftp
.Pwd().c_str());
3630 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3631 static void TestFtpWuFtpd()
3634 static const wxChar
*hostname
= _T("ftp.eudora.com");
3635 if ( !ftp
.Connect(hostname
) )
3637 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3641 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
3642 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3645 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3649 size_t size
= in
->GetSize();
3650 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3652 wxChar
*data
= new wxChar
[size
];
3653 if ( !in
->Read(data
, size
) )
3655 wxPuts(_T("ERROR: read error"));
3659 wxPrintf(_T("Successfully retrieved the file.\n"));
3668 static void TestFtpList()
3670 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3673 if ( !ftp
.ChDir(directory
) )
3675 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3678 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3680 // test NLIST and LIST
3681 wxArrayString files
;
3682 if ( !ftp
.GetFilesList(files
) )
3684 wxPuts(_T("ERROR: failed to get NLIST of files"));
3688 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
3689 size_t count
= files
.GetCount();
3690 for ( size_t n
= 0; n
< count
; n
++ )
3692 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3694 wxPuts(_T("End of the file list"));
3697 if ( !ftp
.GetDirList(files
) )
3699 wxPuts(_T("ERROR: failed to get LIST of files"));
3703 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
3704 size_t count
= files
.GetCount();
3705 for ( size_t n
= 0; n
< count
; n
++ )
3707 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3709 wxPuts(_T("End of the file list"));
3712 if ( !ftp
.ChDir(_T("..")) )
3714 wxPuts(_T("ERROR: failed to cd to .."));
3717 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3720 static void TestFtpDownload()
3722 wxPuts(_T("*** Testing wxFTP download ***\n"));
3725 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3728 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3732 size_t size
= in
->GetSize();
3733 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3736 wxChar
*data
= new wxChar
[size
];
3737 if ( !in
->Read(data
, size
) )
3739 wxPuts(_T("ERROR: read error"));
3743 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
3751 static void TestFtpFileSize()
3753 wxPuts(_T("*** Testing FTP SIZE command ***"));
3755 if ( !ftp
.ChDir(directory
) )
3757 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3760 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3762 if ( ftp
.FileExists(filename
) )
3764 int size
= ftp
.GetFileSize(filename
);
3766 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
3768 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
3772 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
3776 static void TestFtpMisc()
3778 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3780 if ( ftp
.SendCommand(_T("STAT")) != '2' )
3782 wxPuts(_T("ERROR: STAT failed"));
3786 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
3789 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
3791 wxPuts(_T("ERROR: HELP SITE failed"));
3795 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3796 ftp
.GetLastResult().c_str());
3800 static void TestFtpInteractive()
3802 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3808 wxPrintf(_T("Enter FTP command: "));
3809 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3812 // kill the last '\n'
3813 buf
[wxStrlen(buf
) - 1] = 0;
3815 // special handling of LIST and NLST as they require data connection
3816 wxString
start(buf
, 4);
3818 if ( start
== _T("LIST") || start
== _T("NLST") )
3821 if ( wxStrlen(buf
) > 4 )
3824 wxArrayString files
;
3825 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
3827 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
3831 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3832 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
3833 size_t count
= files
.GetCount();
3834 for ( size_t n
= 0; n
< count
; n
++ )
3836 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3838 wxPuts(_T("--- End of the file list"));
3843 wxChar ch
= ftp
.SendCommand(buf
);
3844 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
3847 wxPrintf(_T(" (return code %c)"), ch
);
3850 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
3854 wxPuts(_T("\n*** done ***"));
3857 static void TestFtpUpload()
3859 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3862 static const wxChar
*file1
= _T("test1");
3863 static const wxChar
*file2
= _T("test2");
3864 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
3867 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3868 out
->Write("First hello", 11);
3872 // send a command to check the remote file
3873 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
3875 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
3879 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3880 file1
, ftp
.GetLastResult().c_str());
3883 out
= ftp
.GetOutputStream(file2
);
3886 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3887 out
->Write("Second hello", 12);
3894 // ----------------------------------------------------------------------------
3896 // ----------------------------------------------------------------------------
3900 #include "wx/wfstream.h"
3901 #include "wx/mstream.h"
3903 static void TestFileStream()
3905 wxPuts(_T("*** Testing wxFileInputStream ***"));
3907 static const wxString filename
= _T("testdata.fs");
3909 wxFileOutputStream
fsOut(filename
);
3910 fsOut
.Write("foo", 3);
3913 wxFileInputStream
fsIn(filename
);
3914 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
3915 while ( !fsIn
.Eof() )
3917 wxPutchar(fsIn
.GetC());
3920 if ( !wxRemoveFile(filename
) )
3922 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
3925 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3928 static void TestMemoryStream()
3930 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3932 wxMemoryOutputStream memOutStream
;
3933 wxPrintf(_T("Initially out stream offset: %lu\n"),
3934 (unsigned long)memOutStream
.TellO());
3936 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
3938 memOutStream
.PutC(*p
);
3941 wxPrintf(_T("Final out stream offset: %lu\n"),
3942 (unsigned long)memOutStream
.TellO());
3944 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3947 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
3949 wxMemoryInputStream
memInpStream(buf
, len
);
3950 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
3951 while ( !memInpStream
.Eof() )
3953 wxPutchar(memInpStream
.GetC());
3956 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3959 #endif // TEST_STREAMS
3961 // ----------------------------------------------------------------------------
3963 // ----------------------------------------------------------------------------
3967 #include "wx/timer.h"
3968 #include "wx/utils.h"
3970 static void TestStopWatch()
3972 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3976 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3979 wxPrintf(_T("\t%ldms\n"), sw
.Time());
3981 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3985 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3988 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3991 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3994 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3997 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
4000 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
4001 for ( size_t n
= 0; n
< 70; n
++ )
4005 for ( size_t m
= 0; m
< 100000; m
++ )
4007 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
4009 wxPuts(_T("\ntime is negative - ERROR!"));
4017 wxPuts(_T(", ok."));
4020 #endif // TEST_TIMER
4022 // ----------------------------------------------------------------------------
4024 // ----------------------------------------------------------------------------
4028 #include "wx/vcard.h"
4030 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
4033 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
4036 wxPrintf(_T("%s%s"),
4037 wxString(_T('\t'), level
).c_str(),
4038 vcObj
->GetName().c_str());
4041 switch ( vcObj
->GetType() )
4043 case wxVCardObject::String
:
4044 case wxVCardObject::UString
:
4047 vcObj
->GetValue(&val
);
4048 value
<< _T('"') << val
<< _T('"');
4052 case wxVCardObject::Int
:
4055 vcObj
->GetValue(&i
);
4056 value
.Printf(_T("%u"), i
);
4060 case wxVCardObject::Long
:
4063 vcObj
->GetValue(&l
);
4064 value
.Printf(_T("%lu"), l
);
4068 case wxVCardObject::None
:
4071 case wxVCardObject::Object
:
4072 value
= _T("<node>");
4076 value
= _T("<unknown value type>");
4080 wxPrintf(_T(" = %s"), value
.c_str());
4083 DumpVObject(level
+ 1, *vcObj
);
4086 vcObj
= vcard
.GetNextProp(&cookie
);
4090 static void DumpVCardAddresses(const wxVCard
& vcard
)
4092 wxPuts(_T("\nShowing all addresses from vCard:\n"));
4096 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
4100 int flags
= addr
->GetFlags();
4101 if ( flags
& wxVCardAddress::Domestic
)
4103 flagsStr
<< _T("domestic ");
4105 if ( flags
& wxVCardAddress::Intl
)
4107 flagsStr
<< _T("international ");
4109 if ( flags
& wxVCardAddress::Postal
)
4111 flagsStr
<< _T("postal ");
4113 if ( flags
& wxVCardAddress::Parcel
)
4115 flagsStr
<< _T("parcel ");
4117 if ( flags
& wxVCardAddress::Home
)
4119 flagsStr
<< _T("home ");
4121 if ( flags
& wxVCardAddress::Work
)
4123 flagsStr
<< _T("work ");
4126 wxPrintf(_T("Address %u:\n")
4128 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
4131 addr
->GetPostOffice().c_str(),
4132 addr
->GetExtAddress().c_str(),
4133 addr
->GetStreet().c_str(),
4134 addr
->GetLocality().c_str(),
4135 addr
->GetRegion().c_str(),
4136 addr
->GetPostalCode().c_str(),
4137 addr
->GetCountry().c_str()
4141 addr
= vcard
.GetNextAddress(&cookie
);
4145 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
4147 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
4151 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
4155 int flags
= phone
->GetFlags();
4156 if ( flags
& wxVCardPhoneNumber::Voice
)
4158 flagsStr
<< _T("voice ");
4160 if ( flags
& wxVCardPhoneNumber::Fax
)
4162 flagsStr
<< _T("fax ");
4164 if ( flags
& wxVCardPhoneNumber::Cellular
)
4166 flagsStr
<< _T("cellular ");
4168 if ( flags
& wxVCardPhoneNumber::Modem
)
4170 flagsStr
<< _T("modem ");
4172 if ( flags
& wxVCardPhoneNumber::Home
)
4174 flagsStr
<< _T("home ");
4176 if ( flags
& wxVCardPhoneNumber::Work
)
4178 flagsStr
<< _T("work ");
4181 wxPrintf(_T("Phone number %u:\n")
4186 phone
->GetNumber().c_str()
4190 phone
= vcard
.GetNextPhoneNumber(&cookie
);
4194 static void TestVCardRead()
4196 wxPuts(_T("*** Testing wxVCard reading ***\n"));
4198 wxVCard
vcard(_T("vcard.vcf"));
4199 if ( !vcard
.IsOk() )
4201 wxPuts(_T("ERROR: couldn't load vCard."));
4205 // read individual vCard properties
4206 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
4210 vcObj
->GetValue(&value
);
4215 value
= _T("<none>");
4218 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
4221 if ( !vcard
.GetFullName(&value
) )
4223 value
= _T("<none>");
4226 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
4228 // now show how to deal with multiply occuring properties
4229 DumpVCardAddresses(vcard
);
4230 DumpVCardPhoneNumbers(vcard
);
4232 // and finally show all
4233 wxPuts(_T("\nNow dumping the entire vCard:\n")
4234 "-----------------------------\n");
4236 DumpVObject(0, vcard
);
4240 static void TestVCardWrite()
4242 wxPuts(_T("*** Testing wxVCard writing ***\n"));
4245 if ( !vcard
.IsOk() )
4247 wxPuts(_T("ERROR: couldn't create vCard."));
4252 vcard
.SetName("Zeitlin", "Vadim");
4253 vcard
.SetFullName("Vadim Zeitlin");
4254 vcard
.SetOrganization("wxWindows", "R&D");
4256 // just dump the vCard back
4257 wxPuts(_T("Entire vCard follows:\n"));
4258 wxPuts(vcard
.Write());
4262 #endif // TEST_VCARD
4264 // ----------------------------------------------------------------------------
4266 // ----------------------------------------------------------------------------
4268 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
4274 #include "wx/volume.h"
4276 static const wxChar
*volumeKinds
[] =
4282 _T("network volume"),
4286 static void TestFSVolume()
4288 wxPuts(_T("*** Testing wxFSVolume class ***"));
4290 wxArrayString volumes
= wxFSVolume::GetVolumes();
4291 size_t count
= volumes
.GetCount();
4295 wxPuts(_T("ERROR: no mounted volumes?"));
4299 wxPrintf(_T("%u mounted volumes found:\n"), count
);
4301 for ( size_t n
= 0; n
< count
; n
++ )
4303 wxFSVolume
vol(volumes
[n
]);
4306 wxPuts(_T("ERROR: couldn't create volume"));
4310 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
4312 vol
.GetDisplayName().c_str(),
4313 vol
.GetName().c_str(),
4314 volumeKinds
[vol
.GetKind()],
4315 vol
.IsWritable() ? _T("rw") : _T("ro"),
4316 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
4321 #endif // TEST_VOLUME
4323 // ----------------------------------------------------------------------------
4324 // wide char and Unicode support
4325 // ----------------------------------------------------------------------------
4329 static void TestUnicodeToFromAscii()
4331 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
4333 static const char *msg
= "Hello, world!";
4334 wxString s
= wxString::FromAscii(msg
);
4336 wxPrintf(_T("Message in Unicode: %s\n"), s
.c_str());
4337 printf("Message in ASCII: %s\n", (const char *)s
.ToAscii());
4339 wxPutchar(_T('\n'));
4342 #include "wx/textfile.h"
4344 static void TestUnicodeTextFileRead()
4346 wxPuts(_T("Testing wxTextFile in Unicode build\n"));
4349 if ( file
.Open(_T("testdata.fc"), wxConvLocal
) )
4351 const size_t count
= file
.GetLineCount();
4352 for ( size_t n
= 0; n
< count
; n
++ )
4354 const wxString
& s
= file
[n
];
4356 wxPrintf(_T("Line %u: \"%s\" (len %u, last char = '%c')\n"),
4357 (unsigned)n
, s
.c_str(), (unsigned)s
.length(), s
.Last());
4362 #endif // TEST_UNICODE
4366 #include "wx/strconv.h"
4367 #include "wx/fontenc.h"
4368 #include "wx/encconv.h"
4369 #include "wx/buffer.h"
4371 static const unsigned char utf8koi8r
[] =
4373 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4374 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4375 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4376 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4377 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4378 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4379 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4382 static const unsigned char utf8iso8859_1
[] =
4384 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4385 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4386 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4387 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4388 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4391 static const unsigned char utf8Invalid
[] =
4393 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4394 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4395 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4396 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4400 static const struct Utf8Data
4402 const unsigned char *text
;
4404 const wxChar
*charset
;
4405 wxFontEncoding encoding
;
4408 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4409 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
4410 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4413 static void TestUtf8()
4415 wxPuts(_T("*** Testing UTF8 support ***\n"));
4420 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
4422 const Utf8Data
& u8d
= utf8data
[n
];
4423 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
4424 WXSIZEOF(wbuf
)) == (size_t)-1 )
4426 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4430 wxCSConv
conv(u8d
.charset
);
4431 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
4433 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
4437 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
4441 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
4443 s
= _T("<< conversion failed >>");
4444 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
4448 wxPuts(wxEmptyString
);
4451 static void TestEncodingConverter()
4453 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4455 // using wxEncodingConverter should give the same result as above
4458 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
4459 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
4461 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4465 wxEncodingConverter ec
;
4466 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
4467 ec
.Convert(wbuf
, buf
);
4468 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
4471 wxPuts(wxEmptyString
);
4474 #endif // TEST_WCHAR
4476 // ----------------------------------------------------------------------------
4478 // ----------------------------------------------------------------------------
4482 #include "wx/filesys.h"
4483 #include "wx/fs_zip.h"
4484 #include "wx/zipstrm.h"
4486 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
4488 static void TestZipStreamRead()
4490 wxPuts(_T("*** Testing ZIP reading ***\n"));
4492 static const wxString filename
= _T("foo");
4493 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
4494 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4496 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
4497 while ( !istr
.Eof() )
4499 wxPutchar(istr
.GetC());
4503 wxPuts(_T("\n----- done ------"));
4506 static void DumpZipDirectory(wxFileSystem
& fs
,
4507 const wxString
& dir
,
4508 const wxString
& indent
)
4510 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
4511 TESTFILE_ZIP
, dir
.c_str());
4512 wxString wildcard
= prefix
+ _T("/*");
4514 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
4515 while ( !dirname
.empty() )
4517 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
4519 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4524 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
4526 DumpZipDirectory(fs
, dirname
,
4527 indent
+ wxString(_T(' '), 4));
4529 dirname
= fs
.FindNext();
4532 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
4533 while ( !filename
.empty() )
4535 if ( !filename
.StartsWith(prefix
, &filename
) )
4537 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4542 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
4544 filename
= fs
.FindNext();
4548 static void TestZipFileSystem()
4550 wxPuts(_T("*** Testing ZIP file system ***\n"));
4552 wxFileSystem::AddHandler(new wxZipFSHandler
);
4554 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
4556 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
4561 // ----------------------------------------------------------------------------
4563 // ----------------------------------------------------------------------------
4567 #include "wx/zstream.h"
4568 #include "wx/wfstream.h"
4570 static const wxString FILENAME_GZ
= _T("test.gz");
4571 static const wxChar
*TEST_DATA
= _T("hello and hello and hello and hello and hello");
4573 static void TestZlibStreamWrite()
4575 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4577 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
4578 wxZlibOutputStream
ostr(fileOutStream
);
4579 wxPrintf(_T("Compressing the test string... "));
4580 ostr
.Write(TEST_DATA
, wxStrlen(TEST_DATA
) + 1);
4583 wxPuts(_T("(ERROR: failed)"));
4590 wxPuts(_T("\n----- done ------"));
4593 static void TestZlibStreamRead()
4595 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4597 wxFileInputStream
fileInStream(FILENAME_GZ
);
4598 wxZlibInputStream
istr(fileInStream
);
4599 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4601 wxPuts(_T("Dumping the file:"));
4602 while ( !istr
.Eof() )
4604 wxPutchar(istr
.GetC());
4608 wxPuts(_T("\n----- done ------"));
4613 // ----------------------------------------------------------------------------
4615 // ----------------------------------------------------------------------------
4619 #include "wx/wfstream.h"
4620 #include "wx/gzstream.h"
4621 #include "wx/filename.h"
4622 #include "wx/txtstrm.h"
4624 // Reads two input streams and verifies that they are the same (and non-emtpy)
4626 void GzipVerify(wxInputStream
&in1
, wxInputStream
&in2
)
4629 wxPuts(_T(" Can't verify"));
4633 const int BUFSIZE
= 8192;
4634 wxCharBuffer
buf1(BUFSIZE
);
4635 wxCharBuffer
buf2(BUFSIZE
);
4640 int n1
= in1
.Read(buf1
.data(), BUFSIZE
).LastRead();
4641 int n2
= in2
.Read(buf2
.data(), BUFSIZE
).LastRead();
4643 if (n1
!= n2
|| (n1
&& memcmp(buf1
, buf2
, n1
) != 0) || (!n1
&& none
)) {
4644 wxPuts(_T(" Failure"));
4649 wxPuts(_T(" Success"));
4657 in1
.Read(buf1
.data(), BUFSIZE
);
4659 in2
.Read(buf2
.data(), BUFSIZE
);
4662 // Write a gzip file and read it back.
4666 wxPuts(_T("*** Testing gzip streams ***\n"));
4668 const wxString testname
= _T("gziptest");
4669 const wxString gzipname
= testname
+ _T(".gz");
4671 // write some random test data to a testfile
4672 wxPuts(_T("Writing random test data to ") + testname
+ _T("..."));
4674 wxFFileOutputStream
outstream(testname
);
4675 wxTextOutputStream
textout(outstream
);
4677 for (int i
= 0; i
< 1000 && outstream
.Ok(); i
++)
4678 textout
<< rand() << rand() << rand() << rand() << endl
;
4680 wxPuts(_T(" Done"));
4683 wxFileName
fn(testname
);
4684 wxDateTime dt
= fn
.GetModificationTime();
4685 wxFFileInputStream
instream(testname
);
4687 // try writing a gzip file
4688 wxPuts(_T("Writing ") + gzipname
+ _T(" using wxGzipOutputStream..."));
4690 wxFFileOutputStream
outstream(gzipname
);
4691 wxGzipOutputStream
gzip(outstream
, testname
, dt
);
4693 if (!gzip
.Write(instream
))
4694 wxPuts(_T(" Failure"));
4696 wxPuts(_T(" Success"));
4699 // try reading the gzip file
4700 wxPuts(_T("Reading ") + gzipname
+ _T(" using wxGzipInputStream..."));
4703 wxFFileInputStream
instream2(gzipname
);
4704 wxGzipInputStream
gzip(instream2
);
4705 GzipVerify(instream
, gzip
);
4707 if (gzip
.GetName() != fn
.GetFullName())
4708 wxPuts(gzipname
+ _T(" contains incorrect filename: ")
4710 if (dt
.IsValid() && gzip
.GetDateTime() != dt
)
4711 wxPuts(gzipname
+ _T(" contains incorrect timestamp: ")
4712 + gzip
.GetDateTime().Format());
4716 // then verify it using gzip program if it is in the path
4717 wxPuts(_T("Reading ") + gzipname
+ _T(" using gzip program..."));
4718 wxFFile
file(popen((_T("gzip -d -c ") + gzipname
).mb_str(), "r"));
4720 wxFFileInputStream
instream2(file
);
4722 GzipVerify(instream
, instream2
);
4727 // try reading a gzip created by gzip program
4728 wxPuts(_T("Reading output of gzip program using wxGzipInputStream..."));
4729 file
.Attach(popen((_T("gzip -c ") + testname
).mb_str(), "r"));
4731 wxFFileInputStream
instream2(file
);
4732 wxGzipInputStream
gzip(instream2
);
4734 GzipVerify(instream
, gzip
);
4740 wxPuts(_T("\n--- Done gzip streams ---"));
4745 // ----------------------------------------------------------------------------
4747 // ----------------------------------------------------------------------------
4749 #ifdef TEST_DATETIME
4753 #include "wx/datetime.h"
4758 wxDateTime::wxDateTime_t day
;
4759 wxDateTime::Month month
;
4761 wxDateTime::wxDateTime_t hour
, min
, sec
;
4763 wxDateTime::WeekDay wday
;
4764 time_t gmticks
, ticks
;
4766 void Init(const wxDateTime::Tm
& tm
)
4775 gmticks
= ticks
= -1;
4778 wxDateTime
DT() const
4779 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
4781 bool SameDay(const wxDateTime::Tm
& tm
) const
4783 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
4786 wxString
Format() const
4789 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4791 wxDateTime::GetMonthName(month
).c_str(),
4793 abs(wxDateTime::ConvertYearToBC(year
)),
4794 year
> 0 ? _T("AD") : _T("BC"));
4798 wxString
FormatDate() const
4801 s
.Printf(_T("%02d-%s-%4d%s"),
4803 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4804 abs(wxDateTime::ConvertYearToBC(year
)),
4805 year
> 0 ? _T("AD") : _T("BC"));
4810 static const Date testDates
[] =
4812 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
4813 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
4814 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
4815 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
4816 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
4817 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
4818 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
4819 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
4820 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
4821 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
4822 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
4823 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
4824 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
4825 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
4826 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
4827 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
4828 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
4829 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
4830 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
4833 // this test miscellaneous static wxDateTime functions
4834 static void TestTimeStatic()
4836 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4838 // some info about the current date
4839 int year
= wxDateTime::GetCurrentYear();
4840 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4842 wxDateTime::IsLeapYear(year
) ? "" : "not ",
4843 wxDateTime::GetNumberOfDays(year
));
4845 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
4846 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4847 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4848 wxDateTime::GetMonthName(month
).c_str(),
4849 wxDateTime::GetNumberOfDays(month
));
4852 static const size_t nYears
= 5;
4853 static const size_t years
[2][nYears
] =
4855 // first line: the years to test
4856 { 1990, 1976, 2000, 2030, 1984, },
4858 // second line: true if leap, false otherwise
4859 { false, true, true, false, true }
4862 for ( size_t n
= 0; n
< nYears
; n
++ )
4864 int year
= years
[0][n
];
4865 bool should
= years
[1][n
] != 0,
4866 is
= wxDateTime::IsLeapYear(year
);
4868 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4871 should
== is
? "ok" : "ERROR");
4873 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
4877 // test constructing wxDateTime objects
4878 static void TestTimeSet()
4880 wxPuts(_T("\n*** wxDateTime construction test ***"));
4882 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4884 const Date
& d1
= testDates
[n
];
4885 wxDateTime dt
= d1
.DT();
4888 d2
.Init(dt
.GetTm());
4890 wxString s1
= d1
.Format(),
4893 wxPrintf(_T("Date: %s == %s (%s)\n"),
4894 s1
.c_str(), s2
.c_str(),
4895 s1
== s2
? _T("ok") : _T("ERROR"));
4899 // test time zones stuff
4900 static void TestTimeZones()
4902 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4904 wxDateTime now
= wxDateTime::Now();
4906 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
4907 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
4908 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
4909 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
4910 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
4911 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
4913 wxDateTime::Tm tm
= now
.GetTm();
4914 if ( wxDateTime(tm
) != now
)
4916 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4917 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
4921 // test some minimal support for the dates outside the standard range
4922 static void TestTimeRange()
4924 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4926 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
4928 wxPrintf(_T("Unix epoch:\t%s\n"),
4929 wxDateTime(2440587.5).Format(fmt
).c_str());
4930 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4931 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
4932 wxPrintf(_T("JDN 0: \t%s\n"),
4933 wxDateTime(0.0).Format(fmt
).c_str());
4934 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4935 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
4936 wxPrintf(_T("May 29, 2099:\t%s\n"),
4937 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
4940 static void TestTimeTicks()
4942 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4944 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4946 const Date
& d
= testDates
[n
];
4947 if ( d
.ticks
== -1 )
4950 wxDateTime dt
= d
.DT();
4951 long ticks
= (dt
.GetValue() / 1000).ToLong();
4952 wxPrintf(_T("Ticks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4953 if ( ticks
== d
.ticks
)
4955 wxPuts(_T(" (ok)"));
4959 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4960 (long)d
.ticks
, (long)(ticks
- d
.ticks
));
4963 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
4964 ticks
= (dt
.GetValue() / 1000).ToLong();
4965 wxPrintf(_T("GMtks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4966 if ( ticks
== d
.gmticks
)
4968 wxPuts(_T(" (ok)"));
4972 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4973 (long)d
.gmticks
, (long)(ticks
- d
.gmticks
));
4977 wxPuts(wxEmptyString
);
4980 // test conversions to JDN &c
4981 static void TestTimeJDN()
4983 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4985 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4987 const Date
& d
= testDates
[n
];
4988 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4989 double jdn
= dt
.GetJulianDayNumber();
4991 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d
.Format().c_str(), jdn
);
4994 wxPuts(_T(" (ok)"));
4998 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4999 d
.jdn
, jdn
- d
.jdn
);
5004 // test week days computation
5005 static void TestTimeWDays()
5007 wxPuts(_T("\n*** wxDateTime weekday test ***"));
5009 // test GetWeekDay()
5011 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
5013 const Date
& d
= testDates
[n
];
5014 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
5016 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
5017 wxPrintf(_T("%s is: %s"),
5019 wxDateTime::GetWeekDayName(wday
).c_str());
5020 if ( wday
== d
.wday
)
5022 wxPuts(_T(" (ok)"));
5026 wxPrintf(_T(" (ERROR: should be %s)\n"),
5027 wxDateTime::GetWeekDayName(d
.wday
).c_str());
5031 wxPuts(wxEmptyString
);
5033 // test SetToWeekDay()
5034 struct WeekDateTestData
5036 Date date
; // the real date (precomputed)
5037 int nWeek
; // its week index in the month
5038 wxDateTime::WeekDay wday
; // the weekday
5039 wxDateTime::Month month
; // the month
5040 int year
; // and the year
5042 wxString
Format() const
5045 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
5047 case 1: which
= _T("first"); break;
5048 case 2: which
= _T("second"); break;
5049 case 3: which
= _T("third"); break;
5050 case 4: which
= _T("fourth"); break;
5051 case 5: which
= _T("fifth"); break;
5053 case -1: which
= _T("last"); break;
5058 which
+= _T(" from end");
5061 s
.Printf(_T("The %s %s of %s in %d"),
5063 wxDateTime::GetWeekDayName(wday
).c_str(),
5064 wxDateTime::GetMonthName(month
).c_str(),
5071 // the array data was generated by the following python program
5073 from DateTime import *
5074 from whrandom import *
5075 from string import *
5077 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5078 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5080 week = DateTimeDelta(7)
5083 year = randint(1900, 2100)
5084 month = randint(1, 12)
5085 day = randint(1, 28)
5086 dt = DateTime(year, month, day)
5087 wday = dt.day_of_week
5089 countFromEnd = choice([-1, 1])
5092 while dt.month is month:
5093 dt = dt - countFromEnd * week
5094 weekNum = weekNum + countFromEnd
5096 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
5098 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
5099 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
5102 static const WeekDateTestData weekDatesTestData
[] =
5104 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
5105 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
5106 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
5107 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
5108 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
5109 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
5110 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
5111 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
5112 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
5113 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
5114 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
5115 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
5116 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
5117 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
5118 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
5119 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
5120 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
5121 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
5122 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
5123 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
5126 static const wxChar
*fmt
= _T("%d-%b-%Y");
5129 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
5131 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
5133 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
5135 wxPrintf(_T("%s is %s"), wd
.Format().c_str(), dt
.Format(fmt
).c_str());
5137 const Date
& d
= wd
.date
;
5138 if ( d
.SameDay(dt
.GetTm()) )
5140 wxPuts(_T(" (ok)"));
5144 dt
.Set(d
.day
, d
.month
, d
.year
);
5146 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.Format(fmt
).c_str());
5151 // test the computation of (ISO) week numbers
5152 static void TestTimeWNumber()
5154 wxPuts(_T("\n*** wxDateTime week number test ***"));
5156 struct WeekNumberTestData
5158 Date date
; // the date
5159 wxDateTime::wxDateTime_t week
; // the week number in the year
5160 wxDateTime::wxDateTime_t wmon
; // the week number in the month
5161 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
5162 wxDateTime::wxDateTime_t dnum
; // day number in the year
5165 // data generated with the following python script:
5167 from DateTime import *
5168 from whrandom import *
5169 from string import *
5171 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5172 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5174 def GetMonthWeek(dt):
5175 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
5176 if weekNumMonth < 0:
5177 weekNumMonth = weekNumMonth + 53
5180 def GetLastSundayBefore(dt):
5181 if dt.iso_week[2] == 7:
5184 return dt - DateTimeDelta(dt.iso_week[2])
5187 year = randint(1900, 2100)
5188 month = randint(1, 12)
5189 day = randint(1, 28)
5190 dt = DateTime(year, month, day)
5191 dayNum = dt.day_of_year
5192 weekNum = dt.iso_week[1]
5193 weekNumMonth = GetMonthWeek(dt)
5196 dtSunday = GetLastSundayBefore(dt)
5198 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
5199 weekNumMonth2 = weekNumMonth2 + 1
5200 dtSunday = dtSunday - DateTimeDelta(7)
5202 data = { 'day': rjust(`day`, 2), \
5203 'month': monthNames[month - 1], \
5205 'weekNum': rjust(`weekNum`, 2), \
5206 'weekNumMonth': weekNumMonth, \
5207 'weekNumMonth2': weekNumMonth2, \
5208 'dayNum': rjust(`dayNum`, 3) }
5210 print " { { %(day)s, "\
5211 "wxDateTime::%(month)s, "\
5214 "%(weekNumMonth)s, "\
5215 "%(weekNumMonth2)s, "\
5216 "%(dayNum)s }," % data
5219 static const WeekNumberTestData weekNumberTestDates
[] =
5221 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
5222 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
5223 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
5224 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
5225 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
5226 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
5227 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
5228 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
5229 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
5230 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
5231 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
5232 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
5233 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
5234 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
5235 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
5236 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
5237 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
5238 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
5239 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
5240 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
5243 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
5245 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
5246 const Date
& d
= wn
.date
;
5248 wxDateTime dt
= d
.DT();
5250 wxDateTime::wxDateTime_t
5251 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
5252 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5253 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5254 dnum
= dt
.GetDayOfYear();
5256 wxPrintf(_T("%s: the day number is %d"), d
.FormatDate().c_str(), dnum
);
5257 if ( dnum
== wn
.dnum
)
5259 wxPrintf(_T(" (ok)"));
5263 wxPrintf(_T(" (ERROR: should be %d)"), wn
.dnum
);
5266 wxPrintf(_T(", week in month = %d"), wmon
);
5267 if ( wmon
!= wn
.wmon
)
5269 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon
);
5272 wxPrintf(_T(" or %d"), wmon2
);
5273 if ( wmon2
== wn
.wmon2
)
5275 wxPrintf(_T(" (ok)"));
5279 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon2
);
5282 wxPrintf(_T(", week in year = %d"), week
);
5283 if ( week
!= wn
.week
)
5285 wxPrintf(_T(" (ERROR: should be %d)"), wn
.week
);
5288 wxPutchar(_T('\n'));
5290 wxDateTime
dt2(1, wxDateTime::Jan
, d
.year
);
5291 dt2
.SetToTheWeek(wn
.week
, dt
.GetWeekDay());
5295 d2
.Init(dt2
.GetTm());
5296 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
5297 d2
.FormatDate().c_str());
5302 // test DST calculations
5303 static void TestTimeDST()
5305 wxPuts(_T("\n*** wxDateTime DST test ***"));
5307 wxPrintf(_T("DST is%s in effect now.\n\n"),
5308 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
5310 // taken from http://www.energy.ca.gov/daylightsaving.html
5311 static const Date datesDST
[2][2004 - 1900 + 1] =
5314 { 1, wxDateTime::Apr
, 1990 },
5315 { 7, wxDateTime::Apr
, 1991 },
5316 { 5, wxDateTime::Apr
, 1992 },
5317 { 4, wxDateTime::Apr
, 1993 },
5318 { 3, wxDateTime::Apr
, 1994 },
5319 { 2, wxDateTime::Apr
, 1995 },
5320 { 7, wxDateTime::Apr
, 1996 },
5321 { 6, wxDateTime::Apr
, 1997 },
5322 { 5, wxDateTime::Apr
, 1998 },
5323 { 4, wxDateTime::Apr
, 1999 },
5324 { 2, wxDateTime::Apr
, 2000 },
5325 { 1, wxDateTime::Apr
, 2001 },
5326 { 7, wxDateTime::Apr
, 2002 },
5327 { 6, wxDateTime::Apr
, 2003 },
5328 { 4, wxDateTime::Apr
, 2004 },
5331 { 28, wxDateTime::Oct
, 1990 },
5332 { 27, wxDateTime::Oct
, 1991 },
5333 { 25, wxDateTime::Oct
, 1992 },
5334 { 31, wxDateTime::Oct
, 1993 },
5335 { 30, wxDateTime::Oct
, 1994 },
5336 { 29, wxDateTime::Oct
, 1995 },
5337 { 27, wxDateTime::Oct
, 1996 },
5338 { 26, wxDateTime::Oct
, 1997 },
5339 { 25, wxDateTime::Oct
, 1998 },
5340 { 31, wxDateTime::Oct
, 1999 },
5341 { 29, wxDateTime::Oct
, 2000 },
5342 { 28, wxDateTime::Oct
, 2001 },
5343 { 27, wxDateTime::Oct
, 2002 },
5344 { 26, wxDateTime::Oct
, 2003 },
5345 { 31, wxDateTime::Oct
, 2004 },
5350 for ( year
= 1990; year
< 2005; year
++ )
5352 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
5353 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
5355 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5356 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
5358 size_t n
= year
- 1990;
5359 const Date
& dBegin
= datesDST
[0][n
];
5360 const Date
& dEnd
= datesDST
[1][n
];
5362 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
5364 wxPuts(_T(" (ok)"));
5368 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5369 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
5370 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
5374 wxPuts(wxEmptyString
);
5376 for ( year
= 1990; year
< 2005; year
++ )
5378 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5380 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
5381 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
5385 // test wxDateTime -> text conversion
5386 static void TestTimeFormat()
5388 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5390 // some information may be lost during conversion, so store what kind
5391 // of info should we recover after a round trip
5394 CompareNone
, // don't try comparing
5395 CompareBoth
, // dates and times should be identical
5396 CompareDate
, // dates only
5397 CompareTime
// time only
5402 CompareKind compareKind
;
5403 const wxChar
*format
;
5404 } formatTestFormats
[] =
5406 { CompareBoth
, _T("---> %c") },
5407 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
5408 { CompareBoth
, _T("Date is %x, time is %X") },
5409 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5410 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
5411 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
5414 static const Date formatTestDates
[] =
5416 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
5417 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
5419 // this test can't work for other centuries because it uses two digit
5420 // years in formats, so don't even try it
5421 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
5422 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
5423 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
5427 // an extra test (as it doesn't depend on date, don't do it in the loop)
5428 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5430 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
5432 wxPuts(wxEmptyString
);
5434 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
5435 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
5437 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
5438 wxPrintf(_T("%s"), s
.c_str());
5440 // what can we recover?
5441 int kind
= formatTestFormats
[n
].compareKind
;
5445 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
5448 // converion failed - should it have?
5449 if ( kind
== CompareNone
)
5450 wxPuts(_T(" (ok)"));
5452 wxPuts(_T(" (ERROR: conversion back failed)"));
5456 // should have parsed the entire string
5457 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5461 bool equal
= false; // suppress compilaer warning
5469 equal
= dt
.IsSameDate(dt2
);
5473 equal
= dt
.IsSameTime(dt2
);
5479 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5480 dt2
.Format().c_str(), dt
.Format().c_str());
5484 wxPuts(_T(" (ok)"));
5491 // test text -> wxDateTime conversion
5492 static void TestTimeParse()
5494 wxPuts(_T("\n*** wxDateTime parse test ***"));
5496 struct ParseTestData
5498 const wxChar
*format
;
5503 static const ParseTestData parseTestDates
[] =
5505 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, true },
5506 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, true },
5509 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
5511 const wxChar
*format
= parseTestDates
[n
].format
;
5513 wxPrintf(_T("%s => "), format
);
5516 if ( dt
.ParseRfc822Date(format
) )
5518 wxPrintf(_T("%s "), dt
.Format().c_str());
5520 if ( parseTestDates
[n
].good
)
5522 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
5529 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal
.Format().c_str());
5534 wxPuts(_T("(ERROR: bad format)"));
5539 wxPrintf(_T("bad format (%s)\n"),
5540 parseTestDates
[n
].good
? "ERROR" : "ok");
5545 static void TestDateTimeInteractive()
5547 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5553 wxPrintf(_T("Enter a date: "));
5554 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
5557 // kill the last '\n'
5558 buf
[wxStrlen(buf
) - 1] = 0;
5561 const wxChar
*p
= dt
.ParseDate(buf
);
5564 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
5570 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
5573 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5574 dt
.Format(_T("%b %d, %Y")).c_str(),
5576 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5577 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5578 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
5581 wxPuts(_T("\n*** done ***"));
5584 static void TestTimeMS()
5586 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5588 wxDateTime dt1
= wxDateTime::Now(),
5589 dt2
= wxDateTime::UNow();
5591 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
5592 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5593 wxPrintf(_T("Dummy loop: "));
5594 for ( int i
= 0; i
< 6000; i
++ )
5596 //for ( int j = 0; j < 10; j++ )
5599 s
.Printf(_T("%g"), sqrt(i
));
5605 wxPuts(_T(", done"));
5608 dt2
= wxDateTime::UNow();
5609 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5611 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
5613 wxPuts(_T("\n*** done ***"));
5616 static void TestTimeArithmetics()
5618 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5620 static const struct ArithmData
5622 ArithmData(const wxDateSpan
& sp
, const wxChar
*nam
)
5623 : span(sp
), name(nam
) { }
5627 } testArithmData
[] =
5629 ArithmData(wxDateSpan::Day(), _T("day")),
5630 ArithmData(wxDateSpan::Week(), _T("week")),
5631 ArithmData(wxDateSpan::Month(), _T("month")),
5632 ArithmData(wxDateSpan::Year(), _T("year")),
5633 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5636 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
5638 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
5640 wxDateSpan span
= testArithmData
[n
].span
;
5644 const wxChar
*name
= testArithmData
[n
].name
;
5645 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5646 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
5647 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
5649 wxPrintf(_T("Going back: %s"), (dt1
- span
).FormatISODate().c_str());
5650 if ( dt1
- span
== dt
)
5652 wxPuts(_T(" (ok)"));
5656 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5659 wxPrintf(_T("Going forward: %s"), (dt2
+ span
).FormatISODate().c_str());
5660 if ( dt2
+ span
== dt
)
5662 wxPuts(_T(" (ok)"));
5666 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5669 wxPrintf(_T("Double increment: %s"), (dt2
+ 2*span
).FormatISODate().c_str());
5670 if ( dt2
+ 2*span
== dt1
)
5672 wxPuts(_T(" (ok)"));
5676 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2
.FormatISODate().c_str());
5679 wxPuts(wxEmptyString
);
5683 static void TestTimeHolidays()
5685 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5687 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
5688 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
5689 dtEnd
= dtStart
.GetLastMonthDay();
5691 wxDateTimeArray hol
;
5692 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
5694 const wxChar
*format
= _T("%d-%b-%Y (%a)");
5696 wxPrintf(_T("All holidays between %s and %s:\n"),
5697 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
5699 size_t count
= hol
.GetCount();
5700 for ( size_t n
= 0; n
< count
; n
++ )
5702 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
5705 wxPuts(wxEmptyString
);
5708 static void TestTimeZoneBug()
5710 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5712 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
5713 for ( int i
= 0; i
< 31; i
++ )
5715 wxPrintf(_T("Date %s: week day %s.\n"),
5716 date
.Format(_T("%d-%m-%Y")).c_str(),
5717 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
5719 date
+= wxDateSpan::Day();
5722 wxPuts(wxEmptyString
);
5725 static void TestTimeSpanFormat()
5727 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5729 static const wxChar
*formats
[] =
5731 _T("(default) %H:%M:%S"),
5732 _T("%E weeks and %D days"),
5733 _T("%l milliseconds"),
5734 _T("(with ms) %H:%M:%S:%l"),
5735 _T("100%% of minutes is %M"), // test "%%"
5736 _T("%D days and %H hours"),
5737 _T("or also %S seconds"),
5740 wxTimeSpan
ts1(1, 2, 3, 4),
5742 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
5744 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5745 ts1
.Format(formats
[n
]).c_str(),
5746 ts2
.Format(formats
[n
]).c_str());
5749 wxPuts(wxEmptyString
);
5752 #endif // TEST_DATETIME
5754 // ----------------------------------------------------------------------------
5755 // wxTextInput/OutputStream
5756 // ----------------------------------------------------------------------------
5758 #ifdef TEST_TEXTSTREAM
5760 #include "wx/txtstrm.h"
5761 #include "wx/wfstream.h"
5763 static void TestTextInputStream()
5765 wxPuts(_T("\n*** wxTextInputStream test ***"));
5767 wxString filename
= _T("testdata.fc");
5768 wxFileInputStream
fsIn(filename
);
5771 wxPuts(_T("ERROR: couldn't open file."));
5775 wxTextInputStream
tis(fsIn
);
5780 const wxString s
= tis
.ReadLine();
5782 // line could be non empty if the last line of the file isn't
5783 // terminated with EOL
5784 if ( fsIn
.Eof() && s
.empty() )
5787 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
5792 #endif // TEST_TEXTSTREAM
5794 // ----------------------------------------------------------------------------
5796 // ----------------------------------------------------------------------------
5800 #include "wx/thread.h"
5802 static size_t gs_counter
= (size_t)-1;
5803 static wxCriticalSection gs_critsect
;
5804 static wxSemaphore gs_cond
;
5806 class MyJoinableThread
: public wxThread
5809 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
5810 { m_n
= n
; Create(); }
5812 // thread execution starts here
5813 virtual ExitCode
Entry();
5819 wxThread::ExitCode
MyJoinableThread::Entry()
5821 unsigned long res
= 1;
5822 for ( size_t n
= 1; n
< m_n
; n
++ )
5826 // it's a loooong calculation :-)
5830 return (ExitCode
)res
;
5833 class MyDetachedThread
: public wxThread
5836 MyDetachedThread(size_t n
, wxChar ch
)
5840 m_cancelled
= false;
5845 // thread execution starts here
5846 virtual ExitCode
Entry();
5849 virtual void OnExit();
5852 size_t m_n
; // number of characters to write
5853 wxChar m_ch
; // character to write
5855 bool m_cancelled
; // false if we exit normally
5858 wxThread::ExitCode
MyDetachedThread::Entry()
5861 wxCriticalSectionLocker
lock(gs_critsect
);
5862 if ( gs_counter
== (size_t)-1 )
5868 for ( size_t n
= 0; n
< m_n
; n
++ )
5870 if ( TestDestroy() )
5880 wxThread::Sleep(100);
5886 void MyDetachedThread::OnExit()
5888 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5890 wxCriticalSectionLocker
lock(gs_critsect
);
5891 if ( !--gs_counter
&& !m_cancelled
)
5895 static void TestDetachedThreads()
5897 wxPuts(_T("\n*** Testing detached threads ***"));
5899 static const size_t nThreads
= 3;
5900 MyDetachedThread
*threads
[nThreads
];
5902 for ( n
= 0; n
< nThreads
; n
++ )
5904 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
5907 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
5908 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
5910 for ( n
= 0; n
< nThreads
; n
++ )
5915 // wait until all threads terminate
5918 wxPuts(wxEmptyString
);
5921 static void TestJoinableThreads()
5923 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5925 // calc 10! in the background
5926 MyJoinableThread
thread(10);
5929 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5930 (unsigned long)thread
.Wait());
5933 static void TestThreadSuspend()
5935 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5937 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
5941 // this is for this demo only, in a real life program we'd use another
5942 // condition variable which would be signaled from wxThread::Entry() to
5943 // tell us that the thread really started running - but here just wait a
5944 // bit and hope that it will be enough (the problem is, of course, that
5945 // the thread might still not run when we call Pause() which will result
5947 wxThread::Sleep(300);
5949 for ( size_t n
= 0; n
< 3; n
++ )
5953 wxPuts(_T("\nThread suspended"));
5956 // don't sleep but resume immediately the first time
5957 wxThread::Sleep(300);
5959 wxPuts(_T("Going to resume the thread"));
5964 wxPuts(_T("Waiting until it terminates now"));
5966 // wait until the thread terminates
5969 wxPuts(wxEmptyString
);
5972 static void TestThreadDelete()
5974 // As above, using Sleep() is only for testing here - we must use some
5975 // synchronisation object instead to ensure that the thread is still
5976 // running when we delete it - deleting a detached thread which already
5977 // terminated will lead to a crash!
5979 wxPuts(_T("\n*** Testing thread delete function ***"));
5981 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
5985 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5987 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
5991 wxThread::Sleep(300);
5995 wxPuts(_T("\nDeleted a running thread."));
5997 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
6001 wxThread::Sleep(300);
6007 wxPuts(_T("\nDeleted a sleeping thread."));
6009 MyJoinableThread
thread3(20);
6014 wxPuts(_T("\nDeleted a joinable thread."));
6016 MyJoinableThread
thread4(2);
6019 wxThread::Sleep(300);
6023 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
6025 wxPuts(wxEmptyString
);
6028 class MyWaitingThread
: public wxThread
6031 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
6034 m_condition
= condition
;
6039 virtual ExitCode
Entry()
6041 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
6046 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
6050 m_condition
->Wait();
6053 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
6061 wxCondition
*m_condition
;
6064 static void TestThreadConditions()
6067 wxCondition
condition(mutex
);
6069 // otherwise its difficult to understand which log messages pertain to
6071 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
6072 // condition.GetId(), gs_cond.GetId());
6074 // create and launch threads
6075 MyWaitingThread
*threads
[10];
6078 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
6080 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
6083 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
6088 // wait until all threads run
6089 wxPuts(_T("Main thread is waiting for the other threads to start"));
6092 size_t nRunning
= 0;
6093 while ( nRunning
< WXSIZEOF(threads
) )
6099 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
6103 wxPuts(_T("Main thread: all threads started up."));
6106 wxThread::Sleep(500);
6109 // now wake one of them up
6110 wxPrintf(_T("Main thread: about to signal the condition.\n"));
6115 wxThread::Sleep(200);
6117 // wake all the (remaining) threads up, so that they can exit
6118 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
6120 condition
.Broadcast();
6122 // give them time to terminate (dirty!)
6123 wxThread::Sleep(500);
6126 #include "wx/utils.h"
6128 class MyExecThread
: public wxThread
6131 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
6137 virtual ExitCode
Entry()
6139 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
6146 static void TestThreadExec()
6148 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
6150 MyExecThread
thread(_T("true"));
6153 wxPrintf(_T("Main program exit code: %ld.\n"),
6154 wxExecute(_T("false"), wxEXEC_SYNC
));
6156 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
6160 #include "wx/datetime.h"
6162 class MySemaphoreThread
: public wxThread
6165 MySemaphoreThread(int i
, wxSemaphore
*sem
)
6166 : wxThread(wxTHREAD_JOINABLE
),
6173 virtual ExitCode
Entry()
6175 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
6176 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6180 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
6181 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6185 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
6186 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6198 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
6200 static void TestSemaphore()
6202 wxPuts(_T("*** Testing wxSemaphore class. ***"));
6204 static const int SEM_LIMIT
= 3;
6206 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
6207 ArrayThreads threads
;
6209 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
6211 threads
.Add(new MySemaphoreThread(i
, &sem
));
6212 threads
.Last()->Run();
6215 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
6222 #endif // TEST_THREADS
6224 // ----------------------------------------------------------------------------
6226 // ----------------------------------------------------------------------------
6230 #include "wx/dynarray.h"
6232 typedef unsigned short ushort
;
6234 #define DefineCompare(name, T) \
6236 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
6238 return first - second; \
6241 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
6243 return *first - *second; \
6246 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
6248 return *second - *first; \
6251 DefineCompare(UShort, ushort);
6252 DefineCompare(Int
, int);
6254 // test compilation of all macros
6255 WX_DEFINE_ARRAY_SHORT(ushort
, wxArrayUShort
);
6256 WX_DEFINE_SORTED_ARRAY_SHORT(ushort
, wxSortedArrayUShortNoCmp
);
6257 WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort
, UShortCompareValues
, wxSortedArrayUShort
);
6258 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues
, wxSortedArrayInt
);
6260 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
6261 #include "wx/arrimpl.cpp"
6262 WX_DEFINE_OBJARRAY(ArrayBars
);
6264 static void PrintArray(const wxChar
* name
, const wxArrayString
& array
)
6266 wxPrintf(_T("Dump of the array '%s'\n"), name
);
6268 size_t nCount
= array
.GetCount();
6269 for ( size_t n
= 0; n
< nCount
; n
++ )
6271 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
6275 static void PrintArray(const wxChar
* name
, const wxSortedArrayString
& array
)
6277 wxPrintf(_T("Dump of the array '%s'\n"), name
);
6279 size_t nCount
= array
.GetCount();
6280 for ( size_t n
= 0; n
< nCount
; n
++ )
6282 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
6286 int wxCMPFUNC_CONV
StringLenCompare(const wxString
& first
,
6287 const wxString
& second
)
6289 return first
.length() - second
.length();
6292 #define TestArrayOf(name) \
6294 static void PrintArray(const wxChar* name, const wxSortedArray##name & array) \
6296 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6298 size_t nCount = array.GetCount(); \
6299 for ( size_t n = 0; n < nCount; n++ ) \
6301 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6305 static void PrintArray(const wxChar* name, const wxArray##name & array) \
6307 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6309 size_t nCount = array.GetCount(); \
6310 for ( size_t n = 0; n < nCount; n++ ) \
6312 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6316 static void TestArrayOf ## name ## s() \
6318 wxPrintf(_T("*** Testing wxArray%s ***\n"), #name); \
6326 wxPuts(_T("Initially:")); \
6327 PrintArray(_T("a"), a); \
6329 wxPuts(_T("After sort:")); \
6330 a.Sort(name ## Compare); \
6331 PrintArray(_T("a"), a); \
6333 wxPuts(_T("After reverse sort:")); \
6334 a.Sort(name ## RevCompare); \
6335 PrintArray(_T("a"), a); \
6337 wxSortedArray##name b; \
6343 wxPuts(_T("Sorted array initially:")); \
6344 PrintArray(_T("b"), b); \
6347 TestArrayOf(UShort
);
6350 static void TestStlArray()
6352 wxPuts(_T("*** Testing std::vector operations ***\n"));
6356 wxArrayInt::iterator it
, en
;
6357 wxArrayInt::reverse_iterator rit
, ren
;
6359 for ( i
= 0; i
< 5; ++i
)
6362 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
6363 it
!= en
; ++it
, ++i
)
6365 wxPuts(_T("Error in iterator\n"));
6367 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
6368 rit
!= ren
; ++rit
, --i
)
6370 wxPuts(_T("Error in reverse_iterator\n"));
6372 if ( *list1
.rbegin() != *(list1
.end()-1) ||
6373 *list1
.begin() != *(list1
.rend()-1) )
6374 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6376 it
= list1
.begin()+1;
6377 rit
= list1
.rbegin()+1;
6378 if ( *list1
.begin() != *(it
-1) ||
6379 *list1
.rbegin() != *(rit
-1) )
6380 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6382 if ( list1
.front() != 0 || list1
.back() != 4 )
6383 wxPuts(_T("Error in front()/back()\n"));
6385 list1
.erase(list1
.begin());
6386 list1
.erase(list1
.end()-1);
6388 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
6389 it
!= en
; ++it
, ++i
)
6391 wxPuts(_T("Error in erase()\n"));
6394 wxPuts(_T("*** Testing std::vector operations finished ***\n"));
6397 static void TestArrayOfObjects()
6399 wxPuts(_T("*** Testing wxObjArray ***\n"));
6403 Bar
bar(_T("second bar (two copies!)"));
6405 wxPrintf(_T("Initially: %u objects in the array, %u objects total.\n"),
6406 bars
.GetCount(), Bar::GetNumber());
6408 bars
.Add(new Bar(_T("first bar")));
6411 wxPrintf(_T("Now: %u objects in the array, %u objects total.\n"),
6412 bars
.GetCount(), Bar::GetNumber());
6414 bars
.RemoveAt(1, bars
.GetCount() - 1);
6416 wxPrintf(_T("After removing all but first element: %u objects in the ")
6417 _T("array, %u objects total.\n"),
6418 bars
.GetCount(), Bar::GetNumber());
6422 wxPrintf(_T("After Empty(): %u objects in the array, %u objects total.\n"),
6423 bars
.GetCount(), Bar::GetNumber());
6426 wxPrintf(_T("Finally: no more objects in the array, %u objects total.\n"),
6430 #endif // TEST_ARRAYS
6432 // ----------------------------------------------------------------------------
6434 // ----------------------------------------------------------------------------
6438 #include "wx/timer.h"
6439 #include "wx/tokenzr.h"
6441 static void TestStringConstruction()
6443 wxPuts(_T("*** Testing wxString constructores ***"));
6445 #define TEST_CTOR(args, res) \
6448 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
6451 wxPuts(_T("(ok)")); \
6455 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
6459 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
6460 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
6461 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
6462 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
6464 static const wxChar
*s
= _T("?really!");
6465 const wxChar
*start
= wxStrchr(s
, _T('r'));
6466 const wxChar
*end
= wxStrchr(s
, _T('!'));
6467 TEST_CTOR((start
, end
), _T("really"));
6469 wxPuts(wxEmptyString
);
6472 static void TestString()
6482 for (int i
= 0; i
< 1000000; ++i
)
6486 c
= _T("! How'ya doin'?");
6489 c
= _T("Hello world! What's up?");
6494 wxPrintf(_T("TestString elapsed time: %ld\n"), sw
.Time());
6497 static void TestPChar()
6505 for (int i
= 0; i
< 1000000; ++i
)
6507 wxStrcpy (a
, _T("Hello"));
6508 wxStrcpy (b
, _T(" world"));
6509 wxStrcpy (c
, _T("! How'ya doin'?"));
6512 wxStrcpy (c
, _T("Hello world! What's up?"));
6513 if (wxStrcmp (c
, a
) == 0)
6514 wxStrcpy (c
, _T("Doh!"));
6517 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw
.Time());
6520 static void TestStringSub()
6522 wxString
s(_T("Hello, world!"));
6524 wxPuts(_T("*** Testing wxString substring extraction ***"));
6526 wxPrintf(_T("String = '%s'\n"), s
.c_str());
6527 wxPrintf(_T("Left(5) = '%s'\n"), s
.Left(5).c_str());
6528 wxPrintf(_T("Right(6) = '%s'\n"), s
.Right(6).c_str());
6529 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
6530 wxPrintf(_T("Mid(3) = '%s'\n"), s
.Mid(3).c_str());
6531 wxPrintf(_T("substr(3, 5) = '%s'\n"), s
.substr(3, 5).c_str());
6532 wxPrintf(_T("substr(3) = '%s'\n"), s
.substr(3).c_str());
6534 static const wxChar
*prefixes
[] =
6538 _T("Hello, world!"),
6539 _T("Hello, world!!!"),
6545 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
6547 wxString prefix
= prefixes
[n
], rest
;
6548 bool rc
= s
.StartsWith(prefix
, &rest
);
6549 wxPrintf(_T("StartsWith('%s') = %s"), prefix
.c_str(), rc
? _T("true") : _T("false"));
6552 wxPrintf(_T(" (the rest is '%s')\n"), rest
.c_str());
6560 wxPuts(wxEmptyString
);
6563 static void TestStringFormat()
6565 wxPuts(_T("*** Testing wxString formatting ***"));
6568 s
.Printf(_T("%03d"), 18);
6570 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6571 wxPrintf(_T("Number 18: %s\n"), s
.c_str());
6573 wxPuts(wxEmptyString
);
6576 // returns "not found" for npos, value for all others
6577 static wxString
PosToString(size_t res
)
6579 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
6580 : wxString::Format(_T("%u"), res
);
6584 static void TestStringFind()
6586 wxPuts(_T("*** Testing wxString find() functions ***"));
6588 static const wxChar
*strToFind
= _T("ell");
6589 static const struct StringFindTest
6593 result
; // of searching "ell" in str
6596 { _T("Well, hello world"), 0, 1 },
6597 { _T("Well, hello world"), 6, 7 },
6598 { _T("Well, hello world"), 9, wxString::npos
},
6601 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
6603 const StringFindTest
& ft
= findTestData
[n
];
6604 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
6606 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6607 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
6609 size_t resTrue
= ft
.result
;
6610 if ( res
== resTrue
)
6616 wxPrintf(_T("(ERROR: should be %s)\n"),
6617 PosToString(resTrue
).c_str());
6621 wxPuts(wxEmptyString
);
6624 static void TestStringTokenizer()
6626 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6628 static const wxChar
*modeNames
[] =
6632 _T("return all empty"),
6637 static const struct StringTokenizerTest
6639 const wxChar
*str
; // string to tokenize
6640 const wxChar
*delims
; // delimiters to use
6641 size_t count
; // count of token
6642 wxStringTokenizerMode mode
; // how should we tokenize it
6643 } tokenizerTestData
[] =
6645 { _T(""), _T(" "), 0 },
6646 { _T("Hello, world"), _T(" "), 2 },
6647 { _T("Hello, world "), _T(" "), 2 },
6648 { _T("Hello, world"), _T(","), 2 },
6649 { _T("Hello, world!"), _T(",!"), 2 },
6650 { _T("Hello,, world!"), _T(",!"), 3 },
6651 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
6652 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6653 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
6654 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
6655 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
6656 { _T("01/02/99"), _T("/-"), 3 },
6657 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
6660 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
6662 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
6663 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
6665 size_t count
= tkz
.CountTokens();
6666 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6667 MakePrintable(tt
.str
).c_str(),
6669 MakePrintable(tt
.delims
).c_str(),
6670 modeNames
[tkz
.GetMode()]);
6671 if ( count
== tt
.count
)
6677 wxPrintf(_T("(ERROR: should be %u)\n"), tt
.count
);
6682 // if we emulate strtok(), check that we do it correctly
6683 wxChar
*buf
, *s
= NULL
, *last
;
6685 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
6687 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
6688 wxStrcpy(buf
, tt
.str
);
6690 s
= wxStrtok(buf
, tt
.delims
, &last
);
6697 // now show the tokens themselves
6699 while ( tkz
.HasMoreTokens() )
6701 wxString token
= tkz
.GetNextToken();
6703 wxPrintf(_T("\ttoken %u: '%s'"),
6705 MakePrintable(token
).c_str());
6711 wxPuts(_T(" (ok)"));
6715 wxPrintf(_T(" (ERROR: should be %s)\n"), s
);
6718 s
= wxStrtok(NULL
, tt
.delims
, &last
);
6722 // nothing to compare with
6723 wxPuts(wxEmptyString
);
6727 if ( count2
!= count
)
6729 wxPuts(_T("\tERROR: token count mismatch"));
6735 wxPuts(wxEmptyString
);
6738 static void TestStringReplace()
6740 wxPuts(_T("*** Testing wxString::replace ***"));
6742 static const struct StringReplaceTestData
6744 const wxChar
*original
; // original test string
6745 size_t start
, len
; // the part to replace
6746 const wxChar
*replacement
; // the replacement string
6747 const wxChar
*result
; // and the expected result
6748 } stringReplaceTestData
[] =
6750 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6751 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6752 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6753 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6754 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6757 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
6759 const StringReplaceTestData data
= stringReplaceTestData
[n
];
6761 wxString original
= data
.original
;
6762 original
.replace(data
.start
, data
.len
, data
.replacement
);
6764 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6765 data
.original
, data
.start
, data
.len
, data
.replacement
,
6768 if ( original
== data
.result
)
6774 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
6778 wxPuts(wxEmptyString
);
6782 static void TestStringMatch()
6784 wxPuts(_T("*** Testing wxString::Matches() ***"));
6786 static const struct StringMatchTestData
6789 const wxChar
*wildcard
;
6791 } stringMatchTestData
[] =
6793 { _T("foobar"), _T("foo*"), 1 },
6794 { _T("foobar"), _T("*oo*"), 1 },
6795 { _T("foobar"), _T("*bar"), 1 },
6796 { _T("foobar"), _T("??????"), 1 },
6797 { _T("foobar"), _T("f??b*"), 1 },
6798 { _T("foobar"), _T("f?b*"), 0 },
6799 { _T("foobar"), _T("*goo*"), 0 },
6800 { _T("foobar"), _T("*foo"), 0 },
6801 { _T("foobarfoo"), _T("*foo"), 1 },
6802 { _T(""), _T("*"), 1 },
6803 { _T(""), _T("?"), 0 },
6806 for ( size_t n
= 0; n
< WXSIZEOF(stringMatchTestData
); n
++ )
6808 const StringMatchTestData
& data
= stringMatchTestData
[n
];
6809 bool matches
= wxString(data
.text
).Matches(data
.wildcard
);
6810 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6812 matches
? _T("matches") : _T("doesn't match"),
6814 matches
== data
.matches
? _T("ok") : _T("ERROR"));
6817 wxPuts(wxEmptyString
);
6821 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6822 void ok(int line
, bool ok
, const wxString
& msg
= wxEmptyString
)
6825 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line
) +
6829 void is(int line
, const wxString
& got
, const wxString
& expected
,
6830 const wxString
& msg
= wxEmptyString
)
6832 bool isOk
= got
== expected
;
6833 ok(line
, isOk
, msg
);
6836 wxPuts(_T("Got: ") + got
);
6837 wxPuts(_T("Expected: ") + expected
);
6842 void is(int line
, const wxChar
* got
, const wxChar
* expected
,
6843 const wxString
& msg
= wxEmptyString
)
6845 bool isOk
= wxStrcmp( got
, expected
) == 0;
6846 ok(line
, isOk
, msg
);
6849 wxPuts(_T("Got: ") + wxString(got
));
6850 wxPuts(_T("Expected: ") + wxString(expected
));
6855 void is(int line
, const wxChar
& got
, const wxChar
& expected
,
6856 const wxString
& msg
= wxEmptyString
)
6858 bool isOk
= got
== expected
;
6859 ok(line
, isOk
, msg
);
6862 wxPuts(_T("Got: ") + got
);
6863 wxPuts(_T("Expected: ") + expected
);
6867 void is(int line
, size_t got
, size_t expected
,
6868 const wxString
& msg
= wxEmptyString
)
6870 bool isOk
= got
== expected
;
6871 ok(line
, isOk
, msg
);
6874 wxPuts(wxString::Format(_T("Got: %d"), got
));
6875 wxPuts(wxString::Format(_T("Expected: %d"), expected
));
6879 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6880 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6882 void TestStdString()
6884 wxPuts(_T("*** Testing std::string operations ***\n"));
6887 wxString
s1(_T("abcdefgh")),
6888 s2(_T("abcdefghijklm"), 8),
6889 s3(_T("abcdefghijklm")),
6893 s7(s3
.begin(), s3
.begin() + 8);
6894 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
6896 is( __LINE__
, s1
, _T("abcdefgh") );
6897 is( __LINE__
, s2
, s1
);
6898 is( __LINE__
, s4
, _T("aaaaaaaa") );
6899 is( __LINE__
, s5
, _T("abcdefgh") );
6900 is( __LINE__
, s6
, s1
);
6901 is( __LINE__
, s7
, s1
);
6902 is( __LINE__
, s8
, _T("efgh") );
6905 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6906 s1
.append(_T("def"));
6907 s2
.append(_T("defgh"), 3);
6908 s3
.append(wxString(_T("abcdef")), 3, 6);
6910 s5
.append(3, _T('a'));
6911 s6
.append(s1
.begin() + 3, s1
.end());
6913 is( __LINE__
, s1
, _T("abcdef") );
6914 is( __LINE__
, s2
, _T("abcdef") );
6915 is( __LINE__
, s3
, _T("abcdef") );
6916 is( __LINE__
, s4
, _T("abcabcdef") );
6917 is( __LINE__
, s5
, _T("abcaaa") );
6918 is( __LINE__
, s6
, _T("abcdef") );
6921 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6922 s1
.assign(_T("def"));
6923 s2
.assign(_T("defgh"), 3);
6924 s3
.assign(wxString(_T("abcdef")), 3, 6);
6926 s5
.assign(3, _T('a'));
6927 s6
.assign(s1
.begin() + 1, s1
.end());
6929 is( __LINE__
, s1
, _T("def") );
6930 is( __LINE__
, s2
, _T("def") );
6931 is( __LINE__
, s3
, _T("def") );
6932 is( __LINE__
, s4
, _T("def") );
6933 is( __LINE__
, s5
, _T("aaa") );
6934 is( __LINE__
, s6
, _T("ef") );
6937 s1
= _T("abcdefgh");
6938 s2
= _T("abcdefgh");
6940 s4
= _T("abcdefghi");
6943 s7
= _T("zabcdefg");
6945 ok( __LINE__
, s1
.compare(s2
) == 0 );
6946 ok( __LINE__
, s1
.compare(s3
) > 0 );
6947 ok( __LINE__
, s1
.compare(s4
) < 0 );
6948 ok( __LINE__
, s1
.compare(s5
) > 0 );
6949 ok( __LINE__
, s1
.compare(s6
) < 0 );
6950 ok( __LINE__
, s1
.compare(1, 12, s1
) > 0);
6951 ok( __LINE__
, s1
.compare(_T("abcdefgh")) == 0);
6952 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh")) == 0);
6953 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
6958 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
6959 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
6960 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
6962 is( __LINE__
, s1
, _T("acdefgh") );
6963 is( __LINE__
, s2
, _T("abcd") );
6964 is( __LINE__
, s3
, _T("ac") );
6965 is( __LINE__
, s4
, _T("abcdghi") );
6966 is( __LINE__
, s7
, _T("zabc") );
6967 is( __LINE__
, *it
, _T('c') );
6968 is( __LINE__
, *it2
, _T('g') );
6969 ok( __LINE__
, it3
== s7
.end() );
6973 // 01234567890123456789012345
6974 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6977 is_nom( s1
.find(_T('A')), 7u );
6978 is_nom( s1
.find(_T('A'), 7), 7u );
6979 is_nom( s1
.find(_T('Z')), wxString::npos
);
6980 is_nom( s1
.find(_T('C'), 22), 25u );
6982 is_nom( s1
.find(_T("gAB")), 6u );
6983 is_nom( s1
.find(_T("gAB"), 7), wxString::npos
);
6984 is_nom( s1
.find(_T("gAB"), 6), 6u );
6986 is_nom( s1
.find(_T("gABZZZ"), 2, 3), 6u );
6987 is_nom( s1
.find(_T("gABZZZ"), 7, 3), wxString::npos
);
6989 is_nom( s1
.find(s2
), 6u );
6990 is_nom( s1
.find(s2
, 7), wxString::npos
);
6991 is_nom( s1
.find(s2
, 6), 6u );
6993 // find_first_not_of
6995 // 01234567890123456789012345678901234
6996 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6999 is_nom( s1
.find_first_not_of(_T('a')), 6u );
7000 is_nom( s1
.find_first_not_of(_T('a'), 7), 7u );
7001 is_nom( s2
.find_first_not_of(_T('a')), wxString::npos
);
7003 is_nom( s1
.find_first_not_of(_T("abde"), 4), 7u );
7004 is_nom( s1
.find_first_not_of(_T("abde"), 7), 7u );
7005 is_nom( s1
.find_first_not_of(_T("abcdefghijkl")), wxString::npos
);
7007 is_nom( s1
.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
7010 is_nom( s1
.find_first_of(_T('c')), 7u );
7011 is_nom( s1
.find_first_of(_T('v')), wxString::npos
);
7012 is_nom( s1
.find_first_of(_T('c'), 10), 24u );
7014 is_nom( s1
.find_first_of(_T("ijkl")), 13u );
7015 is_nom( s1
.find_first_of(_T("ddcfg"), 17), 24u );
7016 is_nom( s1
.find_first_of(_T("ddcfga"), 17, 5), 24u );
7020 // 01234567890123456789012345678901234
7021 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
7024 is_nom( s2
.find_last_not_of(_T('a')), wxString::npos
);
7025 is_nom( s1
.find_last_not_of(_T('d')), 33u );
7026 is_nom( s1
.find_last_not_of(_T('d'), 25), 24u );
7028 is_nom( s1
.find_last_not_of(_T("bcd")), 22u );
7029 is_nom( s1
.find_last_not_of(_T("abc"), 24), 16u );
7031 is_nom( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
7034 is_nom( s2
.find_last_of(_T('c')), wxString::npos
);
7035 is_nom( s1
.find_last_of(_T('a')), 22u );
7036 is_nom( s1
.find_last_of(_T('b'), 24), 23u );
7038 is_nom( s1
.find_last_of(_T("ijklm")), 16u );
7039 is_nom( s1
.find_last_of(_T("ijklma"), 33, 4), 16u );
7040 is_nom( s1
.find_last_of(_T("a"), 17), 17u );
7043 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
7044 s9
= s10
= _T("cdefg");
7046 s1
.insert(1, _T("cc") );
7047 s2
.insert(2, _T("cdef"), 3);
7049 s4
.insert(2, s10
, 3, 7);
7050 s5
.insert(1, 2, _T('c'));
7051 it
= s6
.insert(s6
.begin() + 3, _T('X'));
7052 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
7053 s8
.insert(s8
.begin(), 2, _T('c'));
7055 is( __LINE__
, s1
, _T("accaaa") );
7056 is( __LINE__
, s2
, _T("aacdeaa") );
7057 is( __LINE__
, s3
, _T("aacdefgaa") );
7058 is( __LINE__
, s4
, _T("aafgaa") );
7059 is( __LINE__
, s5
, _T("accaaa") );
7060 is( __LINE__
, s6
, _T("aaaXa") );
7061 is( __LINE__
, s7
, _T("cdefaaaa") );
7062 is( __LINE__
, s8
, _T("ccaaaa") );
7064 s1
= s2
= s3
= _T("aaaa");
7065 s1
.insert(0, _T("ccc"), 2);
7066 s2
.insert(4, _T("ccc"), 2);
7068 is( __LINE__
, s1
, _T("ccaaaa") );
7069 is( __LINE__
, s2
, _T("aaaacc") );
7072 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
7073 s9
= s10
= _T("werty");
7075 s1
.replace(3, 4, _T("rtyu"));
7076 s1
.replace(8, 7, _T("opopop"));
7077 s2
.replace(10, 12, _T("WWWW"));
7078 s3
.replace(1, 5, s9
);
7079 s4
.replace(1, 4, s9
, 0, 4);
7080 s5
.replace(1, 2, s9
, 1, 12);
7081 s6
.replace(0, 123, s9
, 0, 123);
7082 s7
.replace(2, 7, s9
);
7084 is( __LINE__
, s1
, _T("QWErtyuIopopop") );
7085 is( __LINE__
, s2
, _T("QWERTYUIOPWWWW") );
7086 is( __LINE__
, s3
, _T("QwertyUIOP") );
7087 is( __LINE__
, s4
, _T("QwertYUIOP") );
7088 is( __LINE__
, s5
, _T("QertyRTYUIOP") );
7089 is( __LINE__
, s6
, s9
);
7090 is( __LINE__
, s7
, _T("QWwertyP") );
7094 // 01234567890123456789012345
7095 s1
= _T("abcdefgABCDEFGabcABCabcABC");
7099 is_nom( s1
.rfind(_T('A')), 23u );
7100 is_nom( s1
.rfind(_T('A'), 7), 7u );
7101 is_nom( s1
.rfind(_T('Z')), wxString::npos
);
7102 is_nom( s1
.rfind(_T('C'), 22), 19u );
7104 is_nom( s1
.rfind(_T("cAB")), 22u );
7105 is_nom( s1
.rfind(_T("cAB"), 15), wxString::npos
);
7106 is_nom( s1
.rfind(_T("cAB"), 21), 16u );
7108 is_nom( s1
.rfind(_T("gABZZZ"), 7, 3), 6u );
7109 is_nom( s1
.rfind(_T("gABZZZ"), 5, 3), wxString::npos
);
7111 is_nom( s1
.rfind(s2
), 6u );
7112 is_nom( s1
.rfind(s2
, 5), wxString::npos
);
7113 is_nom( s1
.rfind(s2
, 6), 6u );
7114 is_nom( s1
.rfind(s3
, 1), 0u );
7117 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
7121 s3
.resize( 14, _T(' ') );
7122 s4
.resize( 14, _T('W') );
7124 is_nom( s1
, _T("abcABCdefDEF") );
7125 is_nom( s2
, _T("abcABCdefD") );
7126 is_nom( s3
, _T("abcABCdefDEF ") );
7127 is_nom( s4
, _T("abcABCdefDEFWW") );
7130 s1
= _T("abcdefgABCDEFG");
7132 is_nom( s1
.substr( 0, 14 ), s1
);
7133 is_nom( s1
.substr( 1, 13 ), _T("bcdefgABCDEFG") );
7134 is_nom( s1
.substr( 1, 20 ), _T("bcdefgABCDEFG") );
7135 is_nom( s1
.substr( 14, 30 ), _T("") );
7137 wxPuts(_T("*** Testing std::string operations finished ***\n"));
7140 #endif // TEST_STRINGS
7142 // ----------------------------------------------------------------------------
7144 // ----------------------------------------------------------------------------
7146 #ifdef TEST_SNGLINST
7147 #include "wx/snglinst.h"
7148 #endif // TEST_SNGLINST
7150 int main(int argc
, char **argv
)
7152 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
7154 wxInitializer initializer
;
7157 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
7162 #ifdef TEST_SNGLINST
7163 wxSingleInstanceChecker checker
;
7164 if ( checker
.Create(_T(".wxconsole.lock")) )
7166 if ( checker
.IsAnotherRunning() )
7168 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
7173 // wait some time to give time to launch another instance
7174 wxPrintf(_T("Press \"Enter\" to continue..."));
7177 else // failed to create
7179 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
7181 #endif // TEST_SNGLINST
7185 #endif // TEST_CHARSET
7188 TestCmdLineConvert();
7190 #if wxUSE_CMDLINE_PARSER
7191 static const wxCmdLineEntryDesc cmdLineDesc
[] =
7193 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
7194 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
7195 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
7196 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
7198 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
7199 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
7200 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
7201 wxCMD_LINE_VAL_NUMBER
},
7202 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
7203 wxCMD_LINE_VAL_DATE
},
7205 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
7206 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
7212 wxChar
**wargv
= new wxChar
*[argc
+ 1];
7217 for (n
= 0; n
< argc
; n
++ )
7219 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
7220 wargv
[n
] = wxStrdup(warg
);
7227 #endif // wxUSE_UNICODE
7229 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
7233 for ( int n
= 0; n
< argc
; n
++ )
7238 #endif // wxUSE_UNICODE
7240 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
7241 wxCMD_LINE_VAL_STRING
,
7242 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
7244 switch ( parser
.Parse() )
7247 wxLogMessage(_T("Help was given, terminating."));
7251 ShowCmdLine(parser
);
7255 wxLogMessage(_T("Syntax error detected, aborting."));
7258 #endif // wxUSE_CMDLINE_PARSER
7260 #endif // TEST_CMDLINE
7267 TestStringConstruction();
7270 TestStringTokenizer();
7271 TestStringReplace();
7277 #endif // TEST_STRINGS
7282 a1
.Add(_T("tiger"));
7284 a1
.Add(_T("lion"), 3);
7286 a1
.Add(_T("human"));
7289 wxPuts(_T("*** Initially:"));
7291 PrintArray(_T("a1"), a1
);
7293 wxArrayString
a2(a1
);
7294 PrintArray(_T("a2"), a2
);
7297 wxSortedArrayString
a3(a1
);
7299 wxSortedArrayString a3
;
7300 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end();
7304 PrintArray(_T("a3"), a3
);
7306 wxPuts(_T("*** After deleting three strings from a1"));
7309 PrintArray(_T("a1"), a1
);
7310 PrintArray(_T("a2"), a2
);
7311 PrintArray(_T("a3"), a3
);
7314 wxPuts(_T("*** After reassigning a1 to a2 and a3"));
7316 PrintArray(_T("a2"), a2
);
7317 PrintArray(_T("a3"), a3
);
7320 wxPuts(_T("*** After sorting a1"));
7322 PrintArray(_T("a1"), a1
);
7324 wxPuts(_T("*** After sorting a1 in reverse order"));
7326 PrintArray(_T("a1"), a1
);
7329 wxPuts(_T("*** After sorting a1 by the string length"));
7330 a1
.Sort(&StringLenCompare
);
7331 PrintArray(_T("a1"), a1
);
7334 TestArrayOfObjects();
7335 TestArrayOfUShorts();
7340 #endif // TEST_ARRAYS
7350 #ifdef TEST_DLLLOADER
7352 #endif // TEST_DLLLOADER
7356 #endif // TEST_ENVIRON
7360 #endif // TEST_EXECUTE
7362 #ifdef TEST_FILECONF
7364 #endif // TEST_FILECONF
7373 #endif // TEST_LOCALE
7376 wxPuts(_T("*** Testing wxLog ***"));
7379 for ( size_t n
= 0; n
< 8000; n
++ )
7381 s
<< (wxChar
)(_T('A') + (n
% 26));
7384 wxLogWarning(_T("The length of the string is %lu"),
7385 (unsigned long)s
.length());
7388 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
7390 // this one shouldn't be truncated
7393 // but this one will because log functions use fixed size buffer
7394 // (note that it doesn't need '\n' at the end neither - will be added
7396 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
7405 #ifdef TEST_FILENAME
7406 TestFileNameConstruction();
7407 TestFileNameMakeRelative();
7408 TestFileNameMakeAbsolute();
7409 TestFileNameSplit();
7412 TestFileNameDirManip();
7413 TestFileNameComparison();
7414 TestFileNameOperations();
7415 #endif // TEST_FILENAME
7417 #ifdef TEST_FILETIME
7422 #endif // TEST_FILETIME
7425 wxLog::AddTraceMask(FTP_TRACE_MASK
);
7426 if ( TestFtpConnect() )
7436 #if TEST_INTERACTIVE
7437 TestFtpInteractive();
7440 //else: connecting to the FTP server failed
7447 #ifdef TEST_LONGLONG
7448 // seed pseudo random generator
7449 srand((unsigned)time(NULL
));
7456 TestMultiplication();
7459 TestLongLongConversion();
7460 TestBitOperations();
7461 TestLongLongComparison();
7462 TestLongLongToString();
7463 TestLongLongPrintf();
7465 #endif // TEST_LONGLONG
7473 #endif // TEST_HASHMAP
7477 #endif // TEST_HASHSET
7480 wxLog::AddTraceMask(_T("mime"));
7484 TestMimeAssociate();
7489 #ifdef TEST_INFO_FUNCTIONS
7494 #if TEST_INTERACTIVE
7498 #endif // TEST_INFO_FUNCTIONS
7500 #ifdef TEST_PATHLIST
7502 #endif // TEST_PATHLIST
7510 #endif // TEST_PRINTF
7517 #endif // TEST_REGCONF
7520 // TODO: write a real test using src/regex/tests file
7524 TestRegExSubmatch();
7525 TestRegExReplacement();
7527 #if TEST_INTERACTIVE
7528 TestRegExInteractive();
7531 #endif // TEST_REGEX
7533 #ifdef TEST_REGISTRY
7535 TestRegistryAssociation();
7536 #endif // TEST_REGISTRY
7541 #endif // TEST_SOCKETS
7548 #endif // TEST_STREAMS
7550 #ifdef TEST_TEXTSTREAM
7551 TestTextInputStream();
7552 #endif // TEST_TEXTSTREAM
7555 int nCPUs
= wxThread::GetCPUCount();
7556 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
7558 wxThread::SetConcurrency(nCPUs
);
7560 TestJoinableThreads();
7563 TestJoinableThreads();
7564 TestDetachedThreads();
7565 TestThreadSuspend();
7567 TestThreadConditions();
7571 #endif // TEST_THREADS
7575 #endif // TEST_TIMER
7577 #ifdef TEST_DATETIME
7589 TestTimeArithmetics();
7592 TestTimeSpanFormat();
7598 #if TEST_INTERACTIVE
7599 TestDateTimeInteractive();
7601 #endif // TEST_DATETIME
7603 #ifdef TEST_SCOPEGUARD
7608 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
7610 #endif // TEST_USLEEP
7615 #endif // TEST_VCARD
7619 #endif // TEST_VOLUME
7622 TestUnicodeTextFileRead();
7624 TestUnicodeToFromAscii();
7626 #endif // TEST_UNICODE
7630 TestEncodingConverter();
7631 #endif // TEST_WCHAR
7634 TestZipStreamRead();
7635 TestZipFileSystem();
7639 TestZlibStreamWrite();
7640 TestZlibStreamRead();