1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
24 #include "wx/string.h"
29 // without this pragma, the stupid compiler precompiles #defines below so that
30 // changing them doesn't "take place" later!
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
40 A note about all these conditional compilation macros: this file is used
41 both as a test suite for various non-GUI wxWindows classes and as a
42 scratchpad for quick tests. So there are two compilation modes: if you
43 define TEST_ALL all tests are run, otherwise you may enable the individual
44 tests individually in the "#else" branch below.
47 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
48 // test, define it to 1 to do all tests.
58 #define TEST_DLLLOADER
65 // #define TEST_FTP --FIXME! (RN)
69 #define TEST_INFO_FUNCTIONS
81 #define TEST_SCOPEGUARD
83 // #define TEST_SOCKETS --FIXME! (RN)
86 #define TEST_TEXTSTREAM
90 // #define TEST_VCARD -- don't enable this (VZ)
91 // #define TEST_VOLUME --FIXME! (RN)
103 // some tests are interactive, define this to run them
104 #ifdef TEST_INTERACTIVE
105 #undef TEST_INTERACTIVE
107 #define TEST_INTERACTIVE 1
109 #define TEST_INTERACTIVE 0
112 // ----------------------------------------------------------------------------
113 // test class for container objects
114 // ----------------------------------------------------------------------------
116 #if defined(TEST_LIST)
118 class Bar
// Foo is already taken in the hash test
121 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
122 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
123 ~Bar() { ms_bars
--; }
125 static size_t GetNumber() { return ms_bars
; }
127 const wxChar
*GetName() const { return m_name
; }
132 static size_t ms_bars
;
135 size_t Bar::ms_bars
= 0;
137 #endif // defined(TEST_LIST)
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
149 // replace TABs with \t and CRs with \n
150 static wxString
MakePrintable(const wxChar
*s
)
153 (void)str
.Replace(_T("\t"), _T("\\t"));
154 (void)str
.Replace(_T("\n"), _T("\\n"));
155 (void)str
.Replace(_T("\r"), _T("\\r"));
160 #endif // MakePrintable() is used
162 // ----------------------------------------------------------------------------
163 // wxFontMapper::CharsetToEncoding
164 // ----------------------------------------------------------------------------
168 #include "wx/fontmap.h"
170 static void TestCharset()
172 static const wxChar
*charsets
[] =
174 // some vali charsets
183 // and now some bogus ones
190 for ( size_t n
= 0; n
< WXSIZEOF(charsets
); n
++ )
192 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charsets
[n
]);
193 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
195 wxFontMapper::Get()->GetEncodingName(enc
).c_str(),
196 wxFontMapper::Get()->GetEncodingDescription(enc
).c_str());
200 #endif // TEST_CHARSET
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
208 #include "wx/cmdline.h"
209 #include "wx/datetime.h"
211 #if wxUSE_CMDLINE_PARSER
213 static void ShowCmdLine(const wxCmdLineParser
& parser
)
215 wxString s
= _T("Input files: ");
217 size_t count
= parser
.GetParamCount();
218 for ( size_t param
= 0; param
< count
; param
++ )
220 s
<< parser
.GetParam(param
) << ' ';
224 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
225 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
230 if ( parser
.Found(_T("o"), &strVal
) )
231 s
<< _T("Output file:\t") << strVal
<< '\n';
232 if ( parser
.Found(_T("i"), &strVal
) )
233 s
<< _T("Input dir:\t") << strVal
<< '\n';
234 if ( parser
.Found(_T("s"), &lVal
) )
235 s
<< _T("Size:\t") << lVal
<< '\n';
236 if ( parser
.Found(_T("d"), &dt
) )
237 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
238 if ( parser
.Found(_T("project_name"), &strVal
) )
239 s
<< _T("Project:\t") << strVal
<< '\n';
244 #endif // wxUSE_CMDLINE_PARSER
246 static void TestCmdLineConvert()
248 static const wxChar
*cmdlines
[] =
251 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
252 _T("literal \\\" and \"\""),
255 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
257 const wxChar
*cmdline
= cmdlines
[n
];
258 wxPrintf(_T("Parsing: %s\n"), cmdline
);
259 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
261 size_t count
= args
.GetCount();
262 wxPrintf(_T("\targc = %u\n"), count
);
263 for ( size_t arg
= 0; arg
< count
; arg
++ )
265 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
270 #endif // TEST_CMDLINE
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
281 static const wxChar
*ROOTDIR
= _T("/");
282 static const wxChar
*TESTDIR
= _T("/usr/local/share");
283 #elif defined(__WXMSW__)
284 static const wxChar
*ROOTDIR
= _T("c:\\");
285 static const wxChar
*TESTDIR
= _T("d:\\");
287 #error "don't know where the root directory is"
290 static void TestDirEnumHelper(wxDir
& dir
,
291 int flags
= wxDIR_DEFAULT
,
292 const wxString
& filespec
= wxEmptyString
)
296 if ( !dir
.IsOpened() )
299 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
302 wxPrintf(_T("\t%s\n"), filename
.c_str());
304 cont
= dir
.GetNext(&filename
);
307 wxPuts(wxEmptyString
);
310 static void TestDirEnum()
312 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
314 wxString cwd
= wxGetCwd();
315 if ( !wxDir::Exists(cwd
) )
317 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
322 if ( !dir
.IsOpened() )
324 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
328 wxPuts(_T("Enumerating everything in current directory:"));
329 TestDirEnumHelper(dir
);
331 wxPuts(_T("Enumerating really everything in current directory:"));
332 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
334 wxPuts(_T("Enumerating object files in current directory:"));
335 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
337 wxPuts(_T("Enumerating directories in current directory:"));
338 TestDirEnumHelper(dir
, wxDIR_DIRS
);
340 wxPuts(_T("Enumerating files in current directory:"));
341 TestDirEnumHelper(dir
, wxDIR_FILES
);
343 wxPuts(_T("Enumerating files including hidden in current directory:"));
344 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
348 wxPuts(_T("Enumerating everything in root directory:"));
349 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
351 wxPuts(_T("Enumerating directories in root directory:"));
352 TestDirEnumHelper(dir
, wxDIR_DIRS
);
354 wxPuts(_T("Enumerating files in root directory:"));
355 TestDirEnumHelper(dir
, wxDIR_FILES
);
357 wxPuts(_T("Enumerating files including hidden in root directory:"));
358 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
360 wxPuts(_T("Enumerating files in non existing directory:"));
361 wxDir
dirNo(_T("nosuchdir"));
362 TestDirEnumHelper(dirNo
);
365 class DirPrintTraverser
: public wxDirTraverser
368 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
370 return wxDIR_CONTINUE
;
373 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
375 wxString path
, name
, ext
;
376 wxSplitPath(dirname
, &path
, &name
, &ext
);
379 name
<< _T('.') << ext
;
382 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
384 if ( wxIsPathSeparator(*p
) )
388 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
390 return wxDIR_CONTINUE
;
394 static void TestDirTraverse()
396 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
400 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
401 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
404 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
405 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
408 // enum again with custom traverser
409 wxPuts(_T("Now enumerating directories:"));
411 DirPrintTraverser traverser
;
412 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
415 static void TestDirExists()
417 wxPuts(_T("*** Testing wxDir::Exists() ***"));
419 static const wxChar
*dirnames
[] =
422 #if defined(__WXMSW__)
425 _T("\\\\share\\file"),
429 _T("c:\\autoexec.bat"),
430 #elif defined(__UNIX__)
439 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
441 wxPrintf(_T("%-40s: %s\n"),
443 wxDir::Exists(dirnames
[n
]) ? _T("exists")
444 : _T("doesn't exist"));
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 #ifdef TEST_DLLLOADER
456 #include "wx/dynlib.h"
458 static void TestDllLoad()
460 #if defined(__WXMSW__)
461 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
462 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
463 #elif defined(__UNIX__)
464 // weird: using just libc.so does *not* work!
465 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
466 static const wxChar
*FUNC_NAME
= _T("strlen");
468 #error "don't know how to test wxDllLoader on this platform"
471 wxPuts(_T("*** testing wxDllLoader ***\n"));
473 wxDynamicLibrary
lib(LIB_NAME
);
474 if ( !lib
.IsLoaded() )
476 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
480 typedef int (*wxStrlenType
)(const char *);
481 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
484 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
485 FUNC_NAME
, LIB_NAME
);
489 if ( pfnStrlen("foo") != 3 )
491 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
495 wxPuts(_T("... ok"));
501 #endif // TEST_DLLLOADER
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
509 #include "wx/utils.h"
511 static wxString
MyGetEnv(const wxString
& var
)
514 if ( !wxGetEnv(var
, &val
) )
517 val
= wxString(_T('\'')) + val
+ _T('\'');
522 static void TestEnvironment()
524 const wxChar
*var
= _T("wxTestVar");
526 wxPuts(_T("*** testing environment access functions ***"));
528 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
529 wxSetEnv(var
, _T("value for wxTestVar"));
530 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
531 wxSetEnv(var
, _T("another value"));
532 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
534 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
535 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
538 #endif // TEST_ENVIRON
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
546 #include "wx/utils.h"
548 static void TestExecute()
550 wxPuts(_T("*** testing wxExecute ***"));
553 #define COMMAND "cat -n ../../Makefile" // "echo hi"
554 #define SHELL_COMMAND "echo hi from shell"
555 #define REDIRECT_COMMAND COMMAND // "date"
556 #elif defined(__WXMSW__)
557 #define COMMAND "command.com /c echo hi"
558 #define SHELL_COMMAND "echo hi"
559 #define REDIRECT_COMMAND COMMAND
561 #error "no command to exec"
564 wxPrintf(_T("Testing wxShell: "));
566 if ( wxShell(_T(SHELL_COMMAND
)) )
569 wxPuts(_T("ERROR."));
571 wxPrintf(_T("Testing wxExecute: "));
573 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
576 wxPuts(_T("ERROR."));
578 #if 0 // no, it doesn't work (yet?)
579 wxPrintf(_T("Testing async wxExecute: "));
581 if ( wxExecute(COMMAND
) != 0 )
582 wxPuts(_T("Ok (command launched)."));
584 wxPuts(_T("ERROR."));
587 wxPrintf(_T("Testing wxExecute with redirection:\n"));
588 wxArrayString output
;
589 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
591 wxPuts(_T("ERROR."));
595 size_t count
= output
.GetCount();
596 for ( size_t n
= 0; n
< count
; n
++ )
598 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
605 #endif // TEST_EXECUTE
607 // ----------------------------------------------------------------------------
609 // ----------------------------------------------------------------------------
614 #include "wx/ffile.h"
615 #include "wx/textfile.h"
617 static void TestFileRead()
619 wxPuts(_T("*** wxFile read test ***"));
621 wxFile
file(_T("testdata.fc"));
622 if ( file
.IsOpened() )
624 wxPrintf(_T("File length: %lu\n"), file
.Length());
626 wxPuts(_T("File dump:\n----------"));
628 static const off_t len
= 1024;
632 off_t nRead
= file
.Read(buf
, len
);
633 if ( nRead
== wxInvalidOffset
)
635 wxPrintf(_T("Failed to read the file."));
639 fwrite(buf
, nRead
, 1, stdout
);
645 wxPuts(_T("----------"));
649 wxPrintf(_T("ERROR: can't open test file.\n"));
652 wxPuts(wxEmptyString
);
655 static void TestTextFileRead()
657 wxPuts(_T("*** wxTextFile read test ***"));
659 wxTextFile
file(_T("testdata.fc"));
662 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
663 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
667 wxPuts(_T("\nDumping the entire file:"));
668 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
670 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
672 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
674 wxPuts(_T("\nAnd now backwards:"));
675 for ( s
= file
.GetLastLine();
676 file
.GetCurrentLine() != 0;
677 s
= file
.GetPrevLine() )
679 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
681 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
685 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
688 wxPuts(wxEmptyString
);
691 static void TestFileCopy()
693 wxPuts(_T("*** Testing wxCopyFile ***"));
695 static const wxChar
*filename1
= _T("testdata.fc");
696 static const wxChar
*filename2
= _T("test2");
697 if ( !wxCopyFile(filename1
, filename2
) )
699 wxPuts(_T("ERROR: failed to copy file"));
703 wxFFile
f1(filename1
, _T("rb")),
704 f2(filename2
, _T("rb"));
706 if ( !f1
.IsOpened() || !f2
.IsOpened() )
708 wxPuts(_T("ERROR: failed to open file(s)"));
713 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
715 wxPuts(_T("ERROR: failed to read file(s)"));
719 if ( (s1
.length() != s2
.length()) ||
720 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
722 wxPuts(_T("ERROR: copy error!"));
726 wxPuts(_T("File was copied ok."));
732 if ( !wxRemoveFile(filename2
) )
734 wxPuts(_T("ERROR: failed to remove the file"));
737 wxPuts(wxEmptyString
);
742 // ----------------------------------------------------------------------------
744 // ----------------------------------------------------------------------------
748 #include "wx/confbase.h"
749 #include "wx/fileconf.h"
751 static const struct FileConfTestData
753 const wxChar
*name
; // value name
754 const wxChar
*value
; // the value from the file
757 { _T("value1"), _T("one") },
758 { _T("value2"), _T("two") },
759 { _T("novalue"), _T("default") },
762 static void TestFileConfRead()
764 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
766 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
767 _T("testdata.fc"), wxEmptyString
,
768 wxCONFIG_USE_RELATIVE_PATH
);
770 // test simple reading
771 wxPuts(_T("\nReading config file:"));
772 wxString
defValue(_T("default")), value
;
773 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
775 const FileConfTestData
& data
= fcTestData
[n
];
776 value
= fileconf
.Read(data
.name
, defValue
);
777 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
778 if ( value
== data
.value
)
784 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
788 // test enumerating the entries
789 wxPuts(_T("\nEnumerating all root entries:"));
792 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
795 wxPrintf(_T("\t%s = %s\n"),
797 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
799 cont
= fileconf
.GetNextEntry(name
, dummy
);
802 static const wxChar
*testEntry
= _T("TestEntry");
803 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
804 fileconf
.Write(testEntry
, _T("A value"));
805 fileconf
.DeleteEntry(testEntry
);
806 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
809 #endif // TEST_FILECONF
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
817 #include "wx/filename.h"
820 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
824 wxString full
= fn
.GetFullPath();
826 wxString vol
, path
, name
, ext
;
827 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
829 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
830 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
832 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
833 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
834 path
.c_str(), name
.c_str(), ext
.c_str());
836 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
837 wxPrintf(_T("with volume: \t'%s'\n"),
838 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
839 wxPrintf(_T("with separator:\t'%s'\n"),
840 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
841 wxPrintf(_T("with both: \t'%s'\n"),
842 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
844 wxPuts(_T("The directories in the path are:"));
845 wxArrayString dirs
= fn
.GetDirs();
846 size_t count
= dirs
.GetCount();
847 for ( size_t n
= 0; n
< count
; n
++ )
849 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
854 static struct FileNameInfo
856 const wxChar
*fullname
;
857 const wxChar
*volume
;
866 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
867 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
868 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
869 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
870 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
871 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
872 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
873 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
874 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
876 // Windows file names
877 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
878 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
879 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
880 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
881 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
882 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
883 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
885 // wxFileName support for Mac file names is broken currently
888 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
889 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
890 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
891 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
892 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
893 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
897 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
898 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
901 static void TestFileNameConstruction()
903 wxPuts(_T("*** testing wxFileName construction ***"));
905 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
907 const FileNameInfo
& fni
= filenames
[n
];
909 wxFileName
fn(fni
.fullname
, fni
.format
);
911 wxString fullname
= fn
.GetFullPath(fni
.format
);
912 if ( fullname
!= fni
.fullname
)
914 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni
.fullname
);
917 bool isAbsolute
= fn
.IsAbsolute(fni
.format
);
918 wxPrintf(_T("'%s' is %s (%s)\n\t"),
920 isAbsolute
? "absolute" : "relative",
921 isAbsolute
== fni
.isAbsolute
? "ok" : "ERROR");
923 if ( !fn
.Normalize(wxPATH_NORM_ALL
, wxEmptyString
, fni
.format
) )
925 wxPuts(_T("ERROR (couldn't be normalized)"));
929 wxPrintf(_T("normalized: '%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
933 wxPuts(wxEmptyString
);
936 static void TestFileNameSplit()
938 wxPuts(_T("*** testing wxFileName splitting ***"));
940 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
942 const FileNameInfo
& fni
= filenames
[n
];
943 wxString volume
, path
, name
, ext
;
944 wxFileName::SplitPath(fni
.fullname
,
945 &volume
, &path
, &name
, &ext
, fni
.format
);
947 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
949 volume
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
951 if ( volume
!= fni
.volume
)
952 wxPrintf(_T(" (ERROR: volume = '%s')"), fni
.volume
);
953 if ( path
!= fni
.path
)
954 wxPrintf(_T(" (ERROR: path = '%s')"), fni
.path
);
955 if ( name
!= fni
.name
)
956 wxPrintf(_T(" (ERROR: name = '%s')"), fni
.name
);
957 if ( ext
!= fni
.ext
)
958 wxPrintf(_T(" (ERROR: ext = '%s')"), fni
.ext
);
960 wxPuts(wxEmptyString
);
964 static void TestFileNameTemp()
966 wxPuts(_T("*** testing wxFileName temp file creation ***"));
968 static const wxChar
*tmpprefixes
[] =
976 _T("/tmp/foo/bar"), // this one must be an error
980 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
982 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
985 // "error" is not in upper case because it may be ok
986 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
990 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
991 tmpprefixes
[n
], path
.c_str());
993 if ( !wxRemoveFile(path
) )
995 wxLogWarning(_T("Failed to remove temp file '%s'"),
1002 static void TestFileNameMakeRelative()
1004 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
1006 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1008 const FileNameInfo
& fni
= filenames
[n
];
1010 wxFileName
fn(fni
.fullname
, fni
.format
);
1012 // choose the base dir of the same format
1014 switch ( fni
.format
)
1017 base
= _T("/usr/bin/");
1026 // TODO: I don't know how this is supposed to work there
1029 case wxPATH_NATIVE
: // make gcc happy
1031 wxFAIL_MSG( _T("unexpected path format") );
1034 wxPrintf(_T("'%s' relative to '%s': "),
1035 fn
.GetFullPath(fni
.format
).c_str(), base
.c_str());
1037 if ( !fn
.MakeRelativeTo(base
, fni
.format
) )
1039 wxPuts(_T("unchanged"));
1043 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1048 static void TestFileNameMakeAbsolute()
1050 wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
1052 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1054 const FileNameInfo
& fni
= filenames
[n
];
1055 wxFileName
fn(fni
.fullname
, fni
.format
);
1057 wxPrintf(_T("'%s' absolutized: "),
1058 fn
.GetFullPath(fni
.format
).c_str());
1060 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1063 wxPuts(wxEmptyString
);
1066 static void TestFileNameDirManip()
1068 // TODO: test AppendDir(), RemoveDir(), ...
1071 static void TestFileNameComparison()
1076 static void TestFileNameOperations()
1081 static void TestFileNameCwd()
1086 #endif // TEST_FILENAME
1088 // ----------------------------------------------------------------------------
1089 // wxFileName time functions
1090 // ----------------------------------------------------------------------------
1092 #ifdef TEST_FILETIME
1094 #include <wx/filename.h>
1095 #include <wx/datetime.h>
1097 static void TestFileGetTimes()
1099 wxFileName
fn(_T("testdata.fc"));
1101 wxDateTime dtAccess
, dtMod
, dtCreate
;
1102 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
1104 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1108 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
1110 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
1111 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
1112 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
1113 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
1118 static void TestFileSetTimes()
1120 wxFileName
fn(_T("testdata.fc"));
1124 wxPrintf(_T("ERROR: Touch() failed.\n"));
1129 #endif // TEST_FILETIME
1131 // ----------------------------------------------------------------------------
1133 // ----------------------------------------------------------------------------
1137 #include "wx/hash.h"
1141 Foo(int n_
) { n
= n_
; count
++; }
1146 static size_t count
;
1149 size_t Foo::count
= 0;
1151 WX_DECLARE_LIST(Foo
, wxListFoos
);
1152 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
1154 #include "wx/listimpl.cpp"
1156 WX_DEFINE_LIST(wxListFoos
);
1158 #include "wx/timer.h"
1160 static void TestHash()
1162 wxPuts(_T("*** Testing wxHashTable ***\n"));
1163 const int COUNT
= 100;
1170 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
1174 for ( i
= 0; i
< COUNT
; ++i
)
1175 hash
.Put(i
, &o
+ i
);
1178 wxHashTable::compatibility_iterator it
= hash
.Next();
1188 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1190 for ( i
= 99; i
>= 0; --i
)
1191 if( hash
.Get(i
) != &o
+ i
)
1192 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1194 for ( i
= 0; i
< COUNT
; ++i
)
1195 hash
.Put(i
, &o
+ i
+ 20);
1197 for ( i
= 99; i
>= 0; --i
)
1198 if( hash
.Get(i
) != &o
+ i
)
1199 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1201 for ( i
= 0; i
< COUNT
/2; ++i
)
1202 if( hash
.Delete(i
) != &o
+ i
)
1203 wxPuts(_T("Error in wxHashTable::Delete\n"));
1205 for ( i
= COUNT
/2; i
< COUNT
; ++i
)
1206 if( hash
.Get(i
) != &o
+ i
)
1207 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1209 for ( i
= 0; i
< COUNT
/2; ++i
)
1210 if( hash
.Get(i
) != &o
+ i
+ 20)
1211 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1213 for ( i
= 0; i
< COUNT
/2; ++i
)
1214 if( hash
.Delete(i
) != &o
+ i
+ 20)
1215 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1217 for ( i
= 0; i
< COUNT
/2; ++i
)
1218 if( hash
.Get(i
) != NULL
)
1219 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1221 hash2
.Put(_T("foo"), &o
+ 1);
1222 hash2
.Put(_T("bar"), &o
+ 2);
1223 hash2
.Put(_T("baz"), &o
+ 3);
1225 if (hash2
.Get(_T("moo")) != NULL
)
1226 wxPuts(_T("Error in wxHashTable::Get\n"));
1228 if (hash2
.Get(_T("bar")) != &o
+ 2)
1229 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1231 hash2
.Put(_T("bar"), &o
+ 0);
1233 if (hash2
.Get(_T("bar")) != &o
+ 2)
1234 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1237 // and now some corner-case testing; 3 and 13 hash to the same bucket
1239 wxHashTable
hash(wxKEY_INTEGER
, 10);
1242 hash
.Put(3, &dummy
);
1245 if (hash
.Get(3) != NULL
)
1246 wxPuts(_T("Corner case 1 failure\n"));
1248 hash
.Put(3, &dummy
);
1249 hash
.Put(13, &dummy
);
1252 if (hash
.Get(3) != NULL
)
1253 wxPuts(_T("Corner case 2 failure\n"));
1257 if (hash
.Get(13) != NULL
)
1258 wxPuts(_T("Corner case 3 failure\n"));
1260 hash
.Put(3, &dummy
);
1261 hash
.Put(13, &dummy
);
1264 if (hash
.Get(13) != NULL
)
1265 wxPuts(_T("Corner case 4 failure\n"));
1269 if (hash
.Get(3) != NULL
)
1270 wxPuts(_T("Corner case 5 failure\n"));
1274 wxHashTable
hash(wxKEY_INTEGER
, 10);
1277 hash
.Put(3, 7, &dummy
+ 7);
1278 hash
.Put(4, 8, &dummy
+ 8);
1280 if (hash
.Get(7) != NULL
) wxPuts(_T("Key/Hash 1 failure\n"));
1281 if (hash
.Get(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 2 failure\n"));
1282 if (hash
.Get(4) != NULL
) wxPuts(_T("Key/Hash 3 failure\n"));
1283 if (hash
.Get(3) != NULL
) wxPuts(_T("Key/Hash 4 failure\n"));
1284 if (hash
.Get(8) != NULL
) wxPuts(_T("Key/Hash 5 failure\n"));
1285 if (hash
.Get(8, 4) != NULL
) wxPuts(_T("Key/Hash 6 failure\n"));
1287 if (hash
.Delete(7) != NULL
) wxPuts(_T("Key/Hash 7 failure\n"));
1288 if (hash
.Delete(3) != NULL
) wxPuts(_T("Key/Hash 8 failure\n"));
1289 if (hash
.Delete(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 8 failure\n"));
1294 hash
.DeleteContents(true);
1296 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1297 hash
.GetCount(), Foo::count
);
1299 static const int hashTestData
[] =
1301 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1305 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1307 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
1310 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1311 hash
.GetCount(), Foo::count
);
1313 wxPuts(_T("Hash access test:"));
1314 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1316 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1317 hashTestData
[n
], n
);
1318 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
1321 wxPrintf(_T("ERROR, not found.\n"));
1325 wxPrintf(_T("%d (%s)\n"), foo
->n
,
1326 (size_t)foo
->n
== n
? "ok" : "ERROR");
1330 wxPrintf(_T("\nTrying to get an element not in hash: "));
1332 if ( hash
.Get(1234) || hash
.Get(1, 0) )
1334 wxPuts(_T("ERROR: found!"));
1338 wxPuts(_T("ok (not found)"));
1341 Foo
* foo
= hash
.Delete(0);
1343 wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count
);
1347 wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count
);
1350 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count
);
1351 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1353 wxPrintf(_T("Time: %ld\n"), sw
.Time());
1358 // ----------------------------------------------------------------------------
1360 // ----------------------------------------------------------------------------
1364 #include "wx/hashmap.h"
1366 // test compilation of basic map types
1367 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
1368 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
1369 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
1370 myUnsignedHashMap
);
1371 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1373 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1375 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1377 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1381 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1382 // myStringHashMap );
1383 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
1385 typedef myStringHashMap::iterator Itor
;
1387 static void TestHashMap()
1389 wxPuts(_T("*** Testing wxHashMap ***\n"));
1390 myStringHashMap
sh(0); // as small as possible
1393 const size_t count
= 10000;
1395 // init with some data
1396 for( i
= 0; i
< count
; ++i
)
1398 buf
.Printf(wxT("%d"), i
);
1399 sh
[buf
] = wxT("A") + buf
+ wxT("C");
1402 // test that insertion worked
1403 if( sh
.size() != count
)
1405 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh
.size(), count
);
1408 for( i
= 0; i
< count
; ++i
)
1410 buf
.Printf(wxT("%d"), i
);
1411 if( sh
[buf
] != wxT("A") + buf
+ wxT("C") )
1413 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1418 // check that iterators work
1420 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1424 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1428 if( it
->second
!= sh
[it
->first
] )
1430 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1435 if( sh
.size() != i
)
1437 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i
, count
);
1440 // test copy ctor, assignment operator
1441 myStringHashMap
h1( sh
), h2( 0 );
1444 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1446 if( h1
[it
->first
] != it
->second
)
1448 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it
->first
.c_str());
1451 if( h2
[it
->first
] != it
->second
)
1453 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it
->first
.c_str());
1458 for( i
= 0; i
< count
; ++i
)
1460 buf
.Printf(wxT("%d"), i
);
1461 size_t sz
= sh
.size();
1463 // test find() and erase(it)
1466 it
= sh
.find( buf
);
1467 if( it
!= sh
.end() )
1471 if( sh
.find( buf
) != sh
.end() )
1473 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1477 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i
);
1482 size_t c
= sh
.erase( buf
);
1484 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1486 if( sh
.find( buf
) != sh
.end() )
1488 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1492 // count should decrease
1493 if( sh
.size() != sz
- 1 )
1495 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1499 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1502 #endif // TEST_HASHMAP
1504 // ----------------------------------------------------------------------------
1506 // ----------------------------------------------------------------------------
1510 #include "wx/hashset.h"
1512 // test compilation of basic map types
1513 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
1514 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
1515 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
1516 myUnsignedHashSet
);
1517 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
1519 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
1521 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
1523 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
1525 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
1537 unsigned long operator()(const MyStruct
& s
) const
1538 { return m_dummy(s
.ptr
); }
1539 MyHash
& operator=(const MyHash
&) { return *this; }
1541 wxPointerHash m_dummy
;
1547 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
1548 { return s1
.ptr
== s2
.ptr
; }
1549 MyEqual
& operator=(const MyEqual
&) { return *this; }
1552 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
1554 typedef myTestHashSet5 wxStringHashSet
;
1556 static void TestHashSet()
1558 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1560 wxStringHashSet set1
;
1562 set1
.insert( _T("abc") );
1563 set1
.insert( _T("bbc") );
1564 set1
.insert( _T("cbc") );
1565 set1
.insert( _T("abc") );
1567 if( set1
.size() != 3 )
1568 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1574 tmp
.ptr
= &dummy
; tmp
.str
= _T("ABC");
1576 tmp
.ptr
= &dummy
+ 1;
1578 tmp
.ptr
= &dummy
; tmp
.str
= _T("CDE");
1581 if( set2
.size() != 2 )
1582 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1584 mySet::iterator it
= set2
.find( tmp
);
1586 if( it
== set2
.end() )
1587 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1588 if( it
->ptr
!= &dummy
)
1589 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1590 if( it
->str
!= _T("ABC") )
1591 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1593 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1596 #endif // TEST_HASHSET
1598 // ----------------------------------------------------------------------------
1600 // ----------------------------------------------------------------------------
1604 #include "wx/list.h"
1606 WX_DECLARE_LIST(Bar
, wxListBars
);
1607 #include "wx/listimpl.cpp"
1608 WX_DEFINE_LIST(wxListBars
);
1610 WX_DECLARE_LIST(int, wxListInt
);
1611 WX_DEFINE_LIST(wxListInt
);
1613 static void TestList()
1615 wxPuts(_T("*** Testing wxList operations ***\n"));
1621 for ( i
= 0; i
< 5; ++i
)
1622 list1
.Append(dummy
+ i
);
1624 if ( list1
.GetCount() != 5 )
1625 wxPuts(_T("Wrong number of items in list\n"));
1627 if ( list1
.Item(3)->GetData() != dummy
+ 3 )
1628 wxPuts(_T("Error in Item()\n"));
1630 if ( !list1
.Find(dummy
+ 4) )
1631 wxPuts(_T("Error in Find()\n"));
1633 wxListInt::compatibility_iterator node
= list1
.GetFirst();
1638 if ( node
->GetData() != dummy
+ i
)
1639 wxPuts(_T("Error in compatibility_iterator\n"));
1640 node
= node
->GetNext();
1644 if ( size_t(i
) != list1
.GetCount() )
1645 wxPuts(_T("Error in compatibility_iterator\n"));
1647 list1
.Insert(dummy
+ 0);
1648 list1
.Insert(1, dummy
+ 1);
1649 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
1651 node
= list1
.GetFirst();
1656 int* t
= node
->GetData();
1657 if ( t
!= dummy
+ i
)
1658 wxPuts(_T("Error in Insert\n"));
1659 node
= node
->GetNext();
1664 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1666 wxPuts(_T("*** Testing std::list operations ***\n"));
1670 wxListInt::iterator it
, en
;
1671 wxListInt::reverse_iterator rit
, ren
;
1673 for ( i
= 0; i
< 5; ++i
)
1674 list1
.push_back(i
+ &i
);
1676 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
1677 it
!= en
; ++it
, ++i
)
1678 if ( *it
!= i
+ &i
)
1679 wxPuts(_T("Error in iterator\n"));
1681 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
1682 rit
!= ren
; ++rit
, --i
)
1683 if ( *rit
!= i
+ &i
)
1684 wxPuts(_T("Error in reverse_iterator\n"));
1686 if ( *list1
.rbegin() != *--list1
.end() ||
1687 *list1
.begin() != *--list1
.rend() )
1688 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1689 if ( *list1
.begin() != *--++list1
.begin() ||
1690 *list1
.rbegin() != *--++list1
.rbegin() )
1691 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1693 if ( list1
.front() != &i
|| list1
.back() != &i
+ 4 )
1694 wxPuts(_T("Error in front()/back()\n"));
1696 list1
.erase(list1
.begin());
1697 list1
.erase(--list1
.end());
1699 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
1700 it
!= en
; ++it
, ++i
)
1701 if ( *it
!= i
+ &i
)
1702 wxPuts(_T("Error in erase()\n"));
1705 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1708 static void TestListCtor()
1710 wxPuts(_T("*** Testing wxList construction ***\n"));
1714 list1
.Append(new Bar(_T("first")));
1715 list1
.Append(new Bar(_T("second")));
1717 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1718 list1
.GetCount(), Bar::GetNumber());
1723 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1724 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
1727 list1
.DeleteContents(true);
1729 WX_CLEAR_LIST(wxListBars
, list1
);
1733 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1738 // ----------------------------------------------------------------------------
1740 // ----------------------------------------------------------------------------
1744 #include "wx/intl.h"
1745 #include "wx/utils.h" // for wxSetEnv
1747 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
1749 // find the name of the language from its value
1750 static const wxChar
*GetLangName(int lang
)
1752 static const wxChar
*languageNames
[] =
1762 _T("ARABIC_ALGERIA"),
1763 _T("ARABIC_BAHRAIN"),
1766 _T("ARABIC_JORDAN"),
1767 _T("ARABIC_KUWAIT"),
1768 _T("ARABIC_LEBANON"),
1770 _T("ARABIC_MOROCCO"),
1773 _T("ARABIC_SAUDI_ARABIA"),
1776 _T("ARABIC_TUNISIA"),
1783 _T("AZERI_CYRILLIC"),
1798 _T("CHINESE_SIMPLIFIED"),
1799 _T("CHINESE_TRADITIONAL"),
1800 _T("CHINESE_HONGKONG"),
1801 _T("CHINESE_MACAU"),
1802 _T("CHINESE_SINGAPORE"),
1803 _T("CHINESE_TAIWAN"),
1809 _T("DUTCH_BELGIAN"),
1813 _T("ENGLISH_AUSTRALIA"),
1814 _T("ENGLISH_BELIZE"),
1815 _T("ENGLISH_BOTSWANA"),
1816 _T("ENGLISH_CANADA"),
1817 _T("ENGLISH_CARIBBEAN"),
1818 _T("ENGLISH_DENMARK"),
1820 _T("ENGLISH_JAMAICA"),
1821 _T("ENGLISH_NEW_ZEALAND"),
1822 _T("ENGLISH_PHILIPPINES"),
1823 _T("ENGLISH_SOUTH_AFRICA"),
1824 _T("ENGLISH_TRINIDAD"),
1825 _T("ENGLISH_ZIMBABWE"),
1833 _T("FRENCH_BELGIAN"),
1834 _T("FRENCH_CANADIAN"),
1835 _T("FRENCH_LUXEMBOURG"),
1836 _T("FRENCH_MONACO"),
1842 _T("GERMAN_AUSTRIAN"),
1843 _T("GERMAN_BELGIUM"),
1844 _T("GERMAN_LIECHTENSTEIN"),
1845 _T("GERMAN_LUXEMBOURG"),
1863 _T("ITALIAN_SWISS"),
1868 _T("KASHMIRI_INDIA"),
1886 _T("MALAY_BRUNEI_DARUSSALAM"),
1887 _T("MALAY_MALAYSIA"),
1897 _T("NORWEGIAN_BOKMAL"),
1898 _T("NORWEGIAN_NYNORSK"),
1905 _T("PORTUGUESE_BRAZILIAN"),
1908 _T("RHAETO_ROMANCE"),
1911 _T("RUSSIAN_UKRAINE"),
1917 _T("SERBIAN_CYRILLIC"),
1918 _T("SERBIAN_LATIN"),
1919 _T("SERBO_CROATIAN"),
1930 _T("SPANISH_ARGENTINA"),
1931 _T("SPANISH_BOLIVIA"),
1932 _T("SPANISH_CHILE"),
1933 _T("SPANISH_COLOMBIA"),
1934 _T("SPANISH_COSTA_RICA"),
1935 _T("SPANISH_DOMINICAN_REPUBLIC"),
1936 _T("SPANISH_ECUADOR"),
1937 _T("SPANISH_EL_SALVADOR"),
1938 _T("SPANISH_GUATEMALA"),
1939 _T("SPANISH_HONDURAS"),
1940 _T("SPANISH_MEXICAN"),
1941 _T("SPANISH_MODERN"),
1942 _T("SPANISH_NICARAGUA"),
1943 _T("SPANISH_PANAMA"),
1944 _T("SPANISH_PARAGUAY"),
1946 _T("SPANISH_PUERTO_RICO"),
1947 _T("SPANISH_URUGUAY"),
1949 _T("SPANISH_VENEZUELA"),
1953 _T("SWEDISH_FINLAND"),
1971 _T("URDU_PAKISTAN"),
1973 _T("UZBEK_CYRILLIC"),
1986 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1987 return languageNames
[lang
];
1989 return _T("INVALID");
1992 static void TestDefaultLang()
1994 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1996 static const wxChar
*langStrings
[] =
1998 NULL
, // system default
2005 _T("de_DE.iso88591"),
2007 _T("?"), // invalid lang spec
2008 _T("klingonese"), // I bet on some systems it does exist...
2011 wxPrintf(_T("The default system encoding is %s (%d)\n"),
2012 wxLocale::GetSystemEncodingName().c_str(),
2013 wxLocale::GetSystemEncoding());
2015 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
2017 const wxChar
*langStr
= langStrings
[n
];
2020 // FIXME: this doesn't do anything at all under Windows, we need
2021 // to create a new wxLocale!
2022 wxSetEnv(_T("LC_ALL"), langStr
);
2025 int lang
= gs_localeDefault
.GetSystemLanguage();
2026 wxPrintf(_T("Locale for '%s' is %s.\n"),
2027 langStr
? langStr
: _T("system default"), GetLangName(lang
));
2031 #endif // TEST_LOCALE
2033 // ----------------------------------------------------------------------------
2035 // ----------------------------------------------------------------------------
2039 #include "wx/mimetype.h"
2041 static void TestMimeEnum()
2043 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
2045 wxArrayString mimetypes
;
2047 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
2049 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
2054 for ( size_t n
= 0; n
< count
; n
++ )
2056 wxFileType
*filetype
=
2057 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
2060 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
2061 mimetypes
[n
].c_str());
2065 filetype
->GetDescription(&desc
);
2066 filetype
->GetExtensions(exts
);
2068 filetype
->GetIcon(NULL
);
2071 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
2074 extsAll
<< _T(", ");
2078 wxPrintf(_T("\t%s: %s (%s)\n"),
2079 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
2082 wxPuts(wxEmptyString
);
2085 static void TestMimeOverride()
2087 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2089 static const wxChar
*mailcap
= _T("/tmp/mailcap");
2090 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
2092 if ( wxFile::Exists(mailcap
) )
2093 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2095 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
2097 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2100 if ( wxFile::Exists(mimetypes
) )
2101 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2103 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
2105 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2108 wxPuts(wxEmptyString
);
2111 static void TestMimeFilename()
2113 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2115 static const wxChar
*filenames
[] =
2123 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
2125 const wxString fname
= filenames
[n
];
2126 wxString ext
= fname
.AfterLast(_T('.'));
2127 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
2130 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
2135 if ( !ft
->GetDescription(&desc
) )
2136 desc
= _T("<no description>");
2139 if ( !ft
->GetOpenCommand(&cmd
,
2140 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
2141 cmd
= _T("<no command available>");
2143 cmd
= wxString(_T('"')) + cmd
+ _T('"');
2145 wxPrintf(_T("To open %s (%s) do %s.\n"),
2146 fname
.c_str(), desc
.c_str(), cmd
.c_str());
2152 wxPuts(wxEmptyString
);
2155 static void TestMimeAssociate()
2157 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2159 wxFileTypeInfo
ftInfo(
2160 _T("application/x-xyz"),
2161 _T("xyzview '%s'"), // open cmd
2162 _T(""), // print cmd
2163 _T("XYZ File"), // description
2164 _T(".xyz"), // extensions
2165 NULL
// end of extensions
2167 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
2169 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
2172 wxPuts(_T("ERROR: failed to create association!"));
2176 // TODO: read it back
2180 wxPuts(wxEmptyString
);
2185 // ----------------------------------------------------------------------------
2186 // misc information functions
2187 // ----------------------------------------------------------------------------
2189 #ifdef TEST_INFO_FUNCTIONS
2191 #include "wx/utils.h"
2193 static void TestDiskInfo()
2195 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2199 wxChar pathname
[128];
2200 wxPrintf(_T("\nEnter a directory name: "));
2201 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
2204 // kill the last '\n'
2205 pathname
[wxStrlen(pathname
) - 1] = 0;
2207 wxLongLong total
, free
;
2208 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
2210 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2214 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2215 (total
/ 1024).ToString().c_str(),
2216 (free
/ 1024).ToString().c_str(),
2222 static void TestOsInfo()
2224 wxPuts(_T("*** Testing OS info functions ***\n"));
2227 wxGetOsVersion(&major
, &minor
);
2228 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2229 wxGetOsDescription().c_str(), major
, minor
);
2231 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2233 wxPrintf(_T("Host name is %s (%s).\n"),
2234 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2236 wxPuts(wxEmptyString
);
2239 static void TestUserInfo()
2241 wxPuts(_T("*** Testing user info functions ***\n"));
2243 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2244 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2245 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2246 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2248 wxPuts(wxEmptyString
);
2251 #endif // TEST_INFO_FUNCTIONS
2253 // ----------------------------------------------------------------------------
2255 // ----------------------------------------------------------------------------
2257 #ifdef TEST_LONGLONG
2259 #include "wx/longlong.h"
2260 #include "wx/timer.h"
2262 // make a 64 bit number from 4 16 bit ones
2263 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
2265 // get a random 64 bit number
2266 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
2268 static const long testLongs
[] =
2279 #if wxUSE_LONGLONG_WX
2280 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
2281 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2282 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
2283 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2284 #endif // wxUSE_LONGLONG_WX
2287 static void TestSpeed()
2289 static const long max
= 100000000;
2296 for ( n
= 0; n
< max
; n
++ )
2302 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw
.Time());
2305 #if wxUSE_LONGLONG_NATIVE
2310 for ( n
= 0; n
< max
; n
++ )
2316 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw
.Time());
2318 #endif // wxUSE_LONGLONG_NATIVE
2324 for ( n
= 0; n
< max
; n
++ )
2329 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw
.Time());
2334 static void TestLongLongConversion()
2336 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2339 for ( size_t n
= 0; n
< 100000; n
++ )
2341 #if wxUSE_LONGLONG_NATIVE
2342 wxLongLong a
= RAND_LL();
2344 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
2347 wxPuts( _T("conversions failure") );
2349 wxPuts(_T("Can't do it without native long long type, test skipped."));
2352 #endif // wxUSE_LONGLONG_NATIVE
2354 if ( !(nTested
% 1000) )
2363 wxPuts(_T(" done!"));
2366 static void TestMultiplication()
2368 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2371 for ( size_t n
= 0; n
< 100000; n
++ )
2373 #if wxUSE_LONGLONG_NATIVE
2374 wxLongLong a
= RAND_LL();
2375 wxLongLong b
= RAND_LL();
2377 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
2378 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
2381 wxPuts( _T("multiplication failure") );
2382 #else // !wxUSE_LONGLONG_NATIVE
2383 wxPuts(_T("Can't do it without native long long type, test skipped."));
2386 #endif // wxUSE_LONGLONG_NATIVE
2388 if ( !(nTested
% 1000) )
2397 wxPuts(_T(" done!"));
2400 static void TestDivision()
2402 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2406 for ( size_t n
= 0; n
< 100000; n
++ )
2408 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2409 // multiplication will not overflow)
2410 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
2412 // get a random (but non null) long (not wxLongLong for now) to divide
2424 #if wxUSE_LONGLONG_NATIVE
2425 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
2427 wxLongLongNative p
= m
/ l
, s
= m
% l
;
2429 if(q
!= p
|| r
!= s
)
2430 wxPuts( _T("division failure") );
2431 #else // !wxUSE_LONGLONG_NATIVE
2432 // verify the result
2433 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
2434 #endif // wxUSE_LONGLONG_NATIVE
2436 if ( !(nTested
% 1000) )
2445 wxPuts(_T(" done!"));
2448 static void TestAddition()
2450 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2454 for ( size_t n
= 0; n
< 100000; n
++ )
2460 #if wxUSE_LONGLONG_NATIVE
2461 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
2462 wxLongLongNative(b
.GetHi(), b
.GetLo()),
2463 _T("addition failure") );
2464 #else // !wxUSE_LONGLONG_NATIVE
2465 wxASSERT_MSG( c
- b
== a
, "addition failure" );
2466 #endif // wxUSE_LONGLONG_NATIVE
2468 if ( !(nTested
% 1000) )
2477 wxPuts(_T(" done!"));
2480 static void TestBitOperations()
2482 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2486 for ( size_t n
= 0; n
< 100000; n
++ )
2490 #if wxUSE_LONGLONG_NATIVE
2491 for ( size_t n
= 0; n
< 33; n
++ )
2494 #else // !wxUSE_LONGLONG_NATIVE
2495 wxPuts(_T("Can't do it without native long long type, test skipped."));
2498 #endif // wxUSE_LONGLONG_NATIVE
2500 if ( !(nTested
% 1000) )
2509 wxPuts(_T(" done!"));
2512 static void TestLongLongComparison()
2514 #if wxUSE_LONGLONG_WX
2515 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
2517 static const long ls
[2] =
2523 wxLongLongWx lls
[2];
2527 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2531 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
2533 res
= lls
[m
] > testLongs
[n
];
2534 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
2535 ls
[m
], testLongs
[n
], res
? "true" : "false",
2536 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
2538 res
= lls
[m
] < testLongs
[n
];
2539 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
2540 ls
[m
], testLongs
[n
], res
? "true" : "false",
2541 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
2543 res
= lls
[m
] == testLongs
[n
];
2544 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
2545 ls
[m
], testLongs
[n
], res
? "true" : "false",
2546 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
2549 #endif // wxUSE_LONGLONG_WX
2552 static void TestLongLongToString()
2554 wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
2556 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2558 wxLongLong ll
= testLongs
[n
];
2559 wxPrintf(_T("%ld == %s\n"), testLongs
[n
], ll
.ToString().c_str());
2562 wxLongLong
ll(0x12345678, 0x87654321);
2563 wxPrintf(_T("0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2566 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2569 static void TestLongLongPrintf()
2571 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2573 #ifdef wxLongLongFmtSpec
2575 wxLongLong ll
= wxLL(0x1234567890abcdef);
2576 wxString s
= wxString::Format(_T("%") wxLongLongFmtSpec
_T("x"), ll
);
2578 wxString s
= _T("MinGW compiler does not allow wxLongLong in '...'");
2580 wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
2581 s
.c_str(), s
== _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
2582 #else // !wxLongLongFmtSpec
2583 #error "wxLongLongFmtSpec not defined for this compiler/platform"
2590 #endif // TEST_LONGLONG
2592 // ----------------------------------------------------------------------------
2594 // ----------------------------------------------------------------------------
2596 #ifdef TEST_PATHLIST
2599 #define CMD_IN_PATH _T("ls")
2601 #define CMD_IN_PATH _T("command.com")
2604 static void TestPathList()
2606 wxPuts(_T("*** Testing wxPathList ***\n"));
2608 wxPathList pathlist
;
2609 pathlist
.AddEnvList(_T("PATH"));
2610 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
2613 wxPrintf(_T("ERROR: command not found in the path.\n"));
2617 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
2621 #endif // TEST_PATHLIST
2623 // ----------------------------------------------------------------------------
2624 // regular expressions
2625 // ----------------------------------------------------------------------------
2629 #include "wx/regex.h"
2631 static void TestRegExCompile()
2633 wxPuts(_T("*** Testing RE compilation ***\n"));
2635 static struct RegExCompTestData
2637 const wxChar
*pattern
;
2639 } regExCompTestData
[] =
2641 { _T("foo"), true },
2642 { _T("foo("), false },
2643 { _T("foo(bar"), false },
2644 { _T("foo(bar)"), true },
2645 { _T("foo["), false },
2646 { _T("foo[bar"), false },
2647 { _T("foo[bar]"), true },
2648 { _T("foo{"), true },
2649 { _T("foo{1"), false },
2650 { _T("foo{bar"), true },
2651 { _T("foo{1}"), true },
2652 { _T("foo{1,2}"), true },
2653 { _T("foo{bar}"), true },
2654 { _T("foo*"), true },
2655 { _T("foo**"), false },
2656 { _T("foo+"), true },
2657 { _T("foo++"), false },
2658 { _T("foo?"), true },
2659 { _T("foo??"), false },
2660 { _T("foo?+"), false },
2664 for ( size_t n
= 0; n
< WXSIZEOF(regExCompTestData
); n
++ )
2666 const RegExCompTestData
& data
= regExCompTestData
[n
];
2667 bool ok
= re
.Compile(data
.pattern
);
2669 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2671 ok
? wxEmptyString
: _T("not "),
2672 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2676 static void TestRegExMatch()
2678 wxPuts(_T("*** Testing RE matching ***\n"));
2680 static struct RegExMatchTestData
2682 const wxChar
*pattern
;
2685 } regExMatchTestData
[] =
2687 { _T("foo"), _T("bar"), false },
2688 { _T("foo"), _T("foobar"), true },
2689 { _T("^foo"), _T("foobar"), true },
2690 { _T("^foo"), _T("barfoo"), false },
2691 { _T("bar$"), _T("barbar"), true },
2692 { _T("bar$"), _T("barbar "), false },
2695 for ( size_t n
= 0; n
< WXSIZEOF(regExMatchTestData
); n
++ )
2697 const RegExMatchTestData
& data
= regExMatchTestData
[n
];
2699 wxRegEx
re(data
.pattern
);
2700 bool ok
= re
.Matches(data
.text
);
2702 wxPrintf(_T("'%s' %s %s (%s)\n"),
2704 ok
? _T("matches") : _T("doesn't match"),
2706 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2710 static void TestRegExSubmatch()
2712 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2714 wxRegEx
re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2715 if ( !re
.IsValid() )
2717 wxPuts(_T("ERROR: compilation failed."));
2721 wxString text
= _T("Fri Jul 13 18:37:52 CEST 2001");
2723 if ( !re
.Matches(text
) )
2725 wxPuts(_T("ERROR: match expected."));
2729 wxPrintf(_T("Entire match: %s\n"), re
.GetMatch(text
).c_str());
2731 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2732 re
.GetMatch(text
, 3).c_str(),
2733 re
.GetMatch(text
, 2).c_str(),
2734 re
.GetMatch(text
, 4).c_str(),
2735 re
.GetMatch(text
, 1).c_str());
2739 static void TestRegExReplacement()
2741 wxPuts(_T("*** Testing RE replacement ***"));
2743 static struct RegExReplTestData
2747 const wxChar
*result
;
2749 } regExReplTestData
[] =
2751 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2752 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2753 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2754 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2755 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2756 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2757 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2760 const wxChar
*pattern
= _T("([a-z]+)[^0-9]*([0-9]+)");
2761 wxRegEx
re(pattern
);
2763 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern
);
2765 for ( size_t n
= 0; n
< WXSIZEOF(regExReplTestData
); n
++ )
2767 const RegExReplTestData
& data
= regExReplTestData
[n
];
2769 wxString text
= data
.text
;
2770 size_t nRepl
= re
.Replace(&text
, data
.repl
);
2772 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2773 data
.text
, data
.repl
,
2774 nRepl
, nRepl
== 1 ? wxEmptyString
: _T("es"),
2776 if ( text
== data
.result
&& nRepl
== data
.count
)
2782 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2783 data
.count
, data
.result
);
2788 static void TestRegExInteractive()
2790 wxPuts(_T("*** Testing RE interactively ***"));
2794 wxChar pattern
[128];
2795 wxPrintf(_T("\nEnter a pattern: "));
2796 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
2799 // kill the last '\n'
2800 pattern
[wxStrlen(pattern
) - 1] = 0;
2803 if ( !re
.Compile(pattern
) )
2811 wxPrintf(_T("Enter text to match: "));
2812 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
2815 // kill the last '\n'
2816 text
[wxStrlen(text
) - 1] = 0;
2818 if ( !re
.Matches(text
) )
2820 wxPrintf(_T("No match.\n"));
2824 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
2827 for ( size_t n
= 1; ; n
++ )
2829 if ( !re
.GetMatch(&start
, &len
, n
) )
2834 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2835 n
, wxString(text
+ start
, len
).c_str());
2842 #endif // TEST_REGEX
2844 // ----------------------------------------------------------------------------
2846 // ----------------------------------------------------------------------------
2856 static void TestDbOpen()
2864 // ----------------------------------------------------------------------------
2866 // ----------------------------------------------------------------------------
2869 NB: this stuff was taken from the glibc test suite and modified to build
2870 in wxWindows: if I read the copyright below properly, this shouldn't
2876 #ifdef wxTEST_PRINTF
2877 // use our functions from wxchar.cpp
2881 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2882 // in the tests below
2883 int wxPrintf( const wxChar
*format
, ... );
2884 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
2887 #include "wx/longlong.h"
2891 static void rfg1 (void);
2892 static void rfg2 (void);
2896 fmtchk (const wxChar
*fmt
)
2898 (void) wxPrintf(_T("%s:\t`"), fmt
);
2899 (void) wxPrintf(fmt
, 0x12);
2900 (void) wxPrintf(_T("'\n"));
2904 fmtst1chk (const wxChar
*fmt
)
2906 (void) wxPrintf(_T("%s:\t`"), fmt
);
2907 (void) wxPrintf(fmt
, 4, 0x12);
2908 (void) wxPrintf(_T("'\n"));
2912 fmtst2chk (const wxChar
*fmt
)
2914 (void) wxPrintf(_T("%s:\t`"), fmt
);
2915 (void) wxPrintf(fmt
, 4, 4, 0x12);
2916 (void) wxPrintf(_T("'\n"));
2919 /* This page is covered by the following copyright: */
2921 /* (C) Copyright C E Chew
2923 * Feel free to copy, use and distribute this software provided:
2925 * 1. you do not pretend that you wrote it
2926 * 2. you leave this copyright notice intact.
2930 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2937 /* Formatted Output Test
2939 * This exercises the output formatting code.
2942 wxChar
*PointerNull
= NULL
;
2949 wxChar
*prefix
= buf
;
2952 wxPuts(_T("\nFormatted output test"));
2953 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2954 wxStrcpy(prefix
, _T("%"));
2955 for (i
= 0; i
< 2; i
++) {
2956 for (j
= 0; j
< 2; j
++) {
2957 for (k
= 0; k
< 2; k
++) {
2958 for (l
= 0; l
< 2; l
++) {
2959 wxStrcpy(prefix
, _T("%"));
2960 if (i
== 0) wxStrcat(prefix
, _T("-"));
2961 if (j
== 0) wxStrcat(prefix
, _T("+"));
2962 if (k
== 0) wxStrcat(prefix
, _T("#"));
2963 if (l
== 0) wxStrcat(prefix
, _T("0"));
2964 wxPrintf(_T("%5s |"), prefix
);
2965 wxStrcpy(tp
, prefix
);
2966 wxStrcat(tp
, _T("6d |"));
2968 wxStrcpy(tp
, prefix
);
2969 wxStrcat(tp
, _T("6o |"));
2971 wxStrcpy(tp
, prefix
);
2972 wxStrcat(tp
, _T("6x |"));
2974 wxStrcpy(tp
, prefix
);
2975 wxStrcat(tp
, _T("6X |"));
2977 wxStrcpy(tp
, prefix
);
2978 wxStrcat(tp
, _T("6u |"));
2985 wxPrintf(_T("%10s\n"), PointerNull
);
2986 wxPrintf(_T("%-10s\n"), PointerNull
);
2989 static void TestPrintf()
2991 static wxChar shortstr
[] = _T("Hi, Z.");
2992 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
2993 I am ready for my first lesson today.");
2995 wxString test_format
;
2999 fmtchk(_T("%4.4x"));
3000 fmtchk(_T("%04.4x"));
3001 fmtchk(_T("%4.3x"));
3002 fmtchk(_T("%04.3x"));
3004 fmtst1chk(_T("%.*x"));
3005 fmtst1chk(_T("%0*x"));
3006 fmtst2chk(_T("%*.*x"));
3007 fmtst2chk(_T("%0*.*x"));
3009 wxString bad_format
= _T("bad format:\t\"%b\"\n");
3010 wxPrintf(bad_format
.c_str());
3011 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
3013 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
3014 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
3015 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
3016 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
3017 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
3018 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
3019 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
3020 test_format
= _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
3021 wxPrintf(test_format
.c_str(), -123456);
3022 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
3023 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
3025 test_format
= _T("zero-padded string:\t\"%010s\"\n");
3026 wxPrintf(test_format
.c_str(), shortstr
);
3027 test_format
= _T("left-adjusted Z string:\t\"%-010s\"\n");
3028 wxPrintf(test_format
.c_str(), shortstr
);
3029 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
3030 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
3031 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull
);
3032 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
3034 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
3035 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
3036 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
3037 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
3038 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
3039 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
3040 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
3041 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
3042 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
3043 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
3044 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
3045 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
3047 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
3048 wxPrintf (_T(" %6.5f\n"), .1);
3049 wxPrintf (_T("x%5.4fx\n"), .5);
3051 wxPrintf (_T("%#03x\n"), 1);
3053 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
3059 while (niter
-- != 0)
3060 wxPrintf (_T("%.17e\n"), d
/ 2);
3065 // Open Watcom cause compiler error here
3066 // Error! E173: col(24) floating-point constant too small to represent
3067 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
3070 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
3071 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
3072 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
3073 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
3074 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
3075 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
3076 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
3077 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
3078 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
3079 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
3084 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
3086 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
3087 rc
, WXSIZEOF(buf
), buf
);
3090 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
3091 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
3097 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
3098 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
3099 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
3100 wxPrintf (_T("%g should be 123.456\n"), 123.456);
3101 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
3102 wxPrintf (_T("%g should be 10\n"), 10.0);
3103 wxPrintf (_T("%g should be 0.02\n"), 0.02);
3107 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
3113 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
3115 result
|= wxStrcmp (buf
,
3116 _T("onetwo three "));
3118 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
3125 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
3127 // for some reason below line fails under Borland
3128 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
3131 if (wxStrcmp (buf
, _T("40000000000")) != 0)
3134 wxPuts (_T("\tFAILED"));
3136 wxUnusedVar(result
);
3137 wxPuts (wxEmptyString
);
3139 #endif // wxLongLong_t
3141 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
3142 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
3144 wxPuts (_T("--- Should be no further output. ---"));
3153 memset (bytes
, '\xff', sizeof bytes
);
3154 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
3155 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
3156 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
3158 wxPuts (_T("%hhn overwrite more bytes"));
3163 wxPuts (_T("%hhn wrote incorrect value"));
3175 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
3176 if (wxStrcmp (buf
, _T(" ")) != 0)
3177 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
3178 wxSprintf (buf
, _T("%5.f"), 33.3);
3179 if (wxStrcmp (buf
, _T(" 33")) != 0)
3180 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
3181 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
3182 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
3183 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
3184 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
3185 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
3186 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
3187 wxSprintf (buf
, _T("%.g"), 33.3);
3188 if (wxStrcmp (buf
, _T("3e+01")) != 0)
3189 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
3190 wxSprintf (buf
, _T("%.G"), 33.3);
3191 if (wxStrcmp (buf
, _T("3E+01")) != 0)
3192 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
3200 wxString test_format
;
3203 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
3204 if (wxStrcmp (buf
, _T("3")) != 0)
3205 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3207 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
3208 if (wxStrcmp (buf
, _T("3")) != 0)
3209 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3211 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
3212 if (wxStrcmp (buf
, _T(" 3")) != 0)
3213 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
3215 test_format
= _T("%04.*o");
3216 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3217 if (wxStrcmp (buf
, _T(" 041")) != 0)
3218 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
3220 test_format
= _T("%09.*u");
3221 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3222 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
3223 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
3225 test_format
= _T("%04.*x");
3226 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3227 if (wxStrcmp (buf
, _T(" 021")) != 0)
3228 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3230 test_format
= _T("%04.*X");
3231 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
3232 if (wxStrcmp (buf
, _T(" 021")) != 0)
3233 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3236 #endif // TEST_PRINTF
3238 // ----------------------------------------------------------------------------
3239 // registry and related stuff
3240 // ----------------------------------------------------------------------------
3242 // this is for MSW only
3245 #undef TEST_REGISTRY
3250 #include "wx/confbase.h"
3251 #include "wx/msw/regconf.h"
3254 static void TestRegConfWrite()
3256 wxConfig
*config
= new wxConfig(_T("myapp"));
3257 config
->SetPath(_T("/group1"));
3258 config
->Write(_T("entry1"), _T("foo"));
3259 config
->SetPath(_T("/group2"));
3260 config
->Write(_T("entry1"), _T("bar"));
3264 static void TestRegConfRead()
3266 wxConfig
*config
= new wxConfig(_T("myapp"));
3270 config
->SetPath(_T("/"));
3271 wxPuts(_T("Enumerating / subgroups:"));
3272 bool bCont
= config
->GetFirstGroup(str
, dummy
);
3276 bCont
= config
->GetNextGroup(str
, dummy
);
3280 #endif // TEST_REGCONF
3282 #ifdef TEST_REGISTRY
3284 #include "wx/msw/registry.h"
3286 // I chose this one because I liked its name, but it probably only exists under
3288 static const wxChar
*TESTKEY
=
3289 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
3291 static void TestRegistryRead()
3293 wxPuts(_T("*** testing registry reading ***"));
3295 wxRegKey
key(TESTKEY
);
3296 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
3299 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
3304 size_t nSubKeys
, nValues
;
3305 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
3307 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
3310 wxPrintf(_T("Enumerating values:\n"));
3314 bool cont
= key
.GetFirstValue(value
, dummy
);
3317 wxPrintf(_T("Value '%s': type "), value
.c_str());
3318 switch ( key
.GetValueType(value
) )
3320 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
3321 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
3322 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
3323 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
3324 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
3325 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
3326 default: wxPrintf(_T("other (unknown)")); break;
3329 wxPrintf(_T(", value = "));
3330 if ( key
.IsNumericValue(value
) )
3333 key
.QueryValue(value
, &val
);
3334 wxPrintf(_T("%ld"), val
);
3339 key
.QueryValue(value
, val
);
3340 wxPrintf(_T("'%s'"), val
.c_str());
3342 key
.QueryRawValue(value
, val
);
3343 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
3348 cont
= key
.GetNextValue(value
, dummy
);
3352 static void TestRegistryAssociation()
3355 The second call to deleteself genertaes an error message, with a
3356 messagebox saying .flo is crucial to system operation, while the .ddf
3357 call also fails, but with no error message
3362 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3364 key
= _T("ddxf_auto_file") ;
3365 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3367 key
= _T("ddxf_auto_file") ;
3368 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3370 key
= _T("program,0") ;
3371 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3373 key
= _T("program \"%1\"") ;
3375 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3377 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3379 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3381 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3385 #endif // TEST_REGISTRY
3387 // ----------------------------------------------------------------------------
3389 // ----------------------------------------------------------------------------
3391 #ifdef TEST_SCOPEGUARD
3393 #include "wx/scopeguard.h"
3395 static void function0() { puts("function0()"); }
3396 static void function1(int n
) { printf("function1(%d)\n", n
); }
3397 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
3401 void method0() { printf("method0()\n"); }
3402 void method1(int n
) { printf("method1(%d)\n", n
); }
3403 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
3406 static void TestScopeGuard()
3408 wxON_BLOCK_EXIT0(function0
);
3409 wxON_BLOCK_EXIT1(function1
, 17);
3410 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
3413 wxON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
3414 wxON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
3415 wxON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
3417 wxScopeGuard dismissed
= wxMakeGuard(function0
);
3418 dismissed
.Dismiss();
3423 // ----------------------------------------------------------------------------
3425 // ----------------------------------------------------------------------------
3429 #include "wx/socket.h"
3430 #include "wx/protocol/protocol.h"
3431 #include "wx/protocol/http.h"
3433 static void TestSocketServer()
3435 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3437 static const int PORT
= 3000;
3442 wxSocketServer
*server
= new wxSocketServer(addr
);
3443 if ( !server
->Ok() )
3445 wxPuts(_T("ERROR: failed to bind"));
3453 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
3455 wxSocketBase
*socket
= server
->Accept();
3458 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3462 wxPuts(_T("Server: got a client."));
3464 server
->SetTimeout(60); // 1 min
3467 while ( !close
&& socket
->IsConnected() )
3470 wxChar ch
= _T('\0');
3473 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
3475 // don't log error if the client just close the connection
3476 if ( socket
->IsConnected() )
3478 wxPuts(_T("ERROR: in wxSocket::Read."));
3498 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
3499 if ( s
== _T("close") )
3501 wxPuts(_T("Closing connection"));
3505 else if ( s
== _T("quit") )
3510 wxPuts(_T("Shutting down the server"));
3512 else // not a special command
3514 socket
->Write(s
.MakeUpper().c_str(), s
.length());
3515 socket
->Write("\r\n", 2);
3516 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
3522 wxPuts(_T("Server: lost a client unexpectedly."));
3528 // same as "delete server" but is consistent with GUI programs
3532 static void TestSocketClient()
3534 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3536 static const wxChar
*hostname
= _T("www.wxwindows.org");
3539 addr
.Hostname(hostname
);
3542 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
3544 wxSocketClient client
;
3545 if ( !client
.Connect(addr
) )
3547 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3551 wxPrintf(_T("--- Connected to %s:%u...\n"),
3552 addr
.Hostname().c_str(), addr
.Service());
3556 // could use simply "GET" here I suppose
3558 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
3559 client
.Write(cmdGet
, cmdGet
.length());
3560 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3561 MakePrintable(cmdGet
).c_str());
3562 client
.Read(buf
, WXSIZEOF(buf
));
3563 wxPrintf(_T("--- Server replied:\n%s"), buf
);
3567 #endif // TEST_SOCKETS
3569 // ----------------------------------------------------------------------------
3571 // ----------------------------------------------------------------------------
3575 #include "wx/protocol/ftp.h"
3579 #define FTP_ANONYMOUS
3581 #ifdef FTP_ANONYMOUS
3582 static const wxChar
*directory
= _T("/pub");
3583 static const wxChar
*filename
= _T("welcome.msg");
3585 static const wxChar
*directory
= _T("/etc");
3586 static const wxChar
*filename
= _T("issue");
3589 static bool TestFtpConnect()
3591 wxPuts(_T("*** Testing FTP connect ***"));
3593 #ifdef FTP_ANONYMOUS
3594 static const wxChar
*hostname
= _T("ftp.wxwindows.org");
3596 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
3597 #else // !FTP_ANONYMOUS
3598 static const wxChar
*hostname
= "localhost";
3601 wxFgets(user
, WXSIZEOF(user
), stdin
);
3602 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
3605 wxChar password
[256];
3606 wxPrintf(_T("Password for %s: "), password
);
3607 wxFgets(password
, WXSIZEOF(password
), stdin
);
3608 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
3609 ftp
.SetPassword(password
);
3611 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
3612 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3614 if ( !ftp
.Connect(hostname
) )
3616 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3622 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3623 hostname
, ftp
.Pwd().c_str());
3629 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3630 static void TestFtpWuFtpd()
3633 static const wxChar
*hostname
= _T("ftp.eudora.com");
3634 if ( !ftp
.Connect(hostname
) )
3636 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3640 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
3641 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3644 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3648 size_t size
= in
->GetSize();
3649 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3651 wxChar
*data
= new wxChar
[size
];
3652 if ( !in
->Read(data
, size
) )
3654 wxPuts(_T("ERROR: read error"));
3658 wxPrintf(_T("Successfully retrieved the file.\n"));
3667 static void TestFtpList()
3669 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3672 if ( !ftp
.ChDir(directory
) )
3674 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3677 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3679 // test NLIST and LIST
3680 wxArrayString files
;
3681 if ( !ftp
.GetFilesList(files
) )
3683 wxPuts(_T("ERROR: failed to get NLIST of files"));
3687 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
3688 size_t count
= files
.GetCount();
3689 for ( size_t n
= 0; n
< count
; n
++ )
3691 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3693 wxPuts(_T("End of the file list"));
3696 if ( !ftp
.GetDirList(files
) )
3698 wxPuts(_T("ERROR: failed to get LIST of files"));
3702 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
3703 size_t count
= files
.GetCount();
3704 for ( size_t n
= 0; n
< count
; n
++ )
3706 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3708 wxPuts(_T("End of the file list"));
3711 if ( !ftp
.ChDir(_T("..")) )
3713 wxPuts(_T("ERROR: failed to cd to .."));
3716 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3719 static void TestFtpDownload()
3721 wxPuts(_T("*** Testing wxFTP download ***\n"));
3724 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3727 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3731 size_t size
= in
->GetSize();
3732 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3735 wxChar
*data
= new wxChar
[size
];
3736 if ( !in
->Read(data
, size
) )
3738 wxPuts(_T("ERROR: read error"));
3742 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
3750 static void TestFtpFileSize()
3752 wxPuts(_T("*** Testing FTP SIZE command ***"));
3754 if ( !ftp
.ChDir(directory
) )
3756 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3759 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3761 if ( ftp
.FileExists(filename
) )
3763 int size
= ftp
.GetFileSize(filename
);
3765 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
3767 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
3771 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
3775 static void TestFtpMisc()
3777 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3779 if ( ftp
.SendCommand(_T("STAT")) != '2' )
3781 wxPuts(_T("ERROR: STAT failed"));
3785 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
3788 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
3790 wxPuts(_T("ERROR: HELP SITE failed"));
3794 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3795 ftp
.GetLastResult().c_str());
3799 static void TestFtpInteractive()
3801 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3807 wxPrintf(_T("Enter FTP command: "));
3808 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3811 // kill the last '\n'
3812 buf
[wxStrlen(buf
) - 1] = 0;
3814 // special handling of LIST and NLST as they require data connection
3815 wxString
start(buf
, 4);
3817 if ( start
== _T("LIST") || start
== _T("NLST") )
3820 if ( wxStrlen(buf
) > 4 )
3823 wxArrayString files
;
3824 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
3826 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
3830 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3831 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
3832 size_t count
= files
.GetCount();
3833 for ( size_t n
= 0; n
< count
; n
++ )
3835 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3837 wxPuts(_T("--- End of the file list"));
3842 wxChar ch
= ftp
.SendCommand(buf
);
3843 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
3846 wxPrintf(_T(" (return code %c)"), ch
);
3849 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
3853 wxPuts(_T("\n*** done ***"));
3856 static void TestFtpUpload()
3858 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3861 static const wxChar
*file1
= _T("test1");
3862 static const wxChar
*file2
= _T("test2");
3863 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
3866 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3867 out
->Write("First hello", 11);
3871 // send a command to check the remote file
3872 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
3874 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
3878 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3879 file1
, ftp
.GetLastResult().c_str());
3882 out
= ftp
.GetOutputStream(file2
);
3885 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3886 out
->Write("Second hello", 12);
3893 // ----------------------------------------------------------------------------
3895 // ----------------------------------------------------------------------------
3899 #include "wx/wfstream.h"
3900 #include "wx/mstream.h"
3902 static void TestFileStream()
3904 wxPuts(_T("*** Testing wxFileInputStream ***"));
3906 static const wxString filename
= _T("testdata.fs");
3908 wxFileOutputStream
fsOut(filename
);
3909 fsOut
.Write("foo", 3);
3912 wxFileInputStream
fsIn(filename
);
3913 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
3914 while ( !fsIn
.Eof() )
3916 wxPutchar(fsIn
.GetC());
3919 if ( !wxRemoveFile(filename
) )
3921 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
3924 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3927 static void TestMemoryStream()
3929 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3931 wxMemoryOutputStream memOutStream
;
3932 wxPrintf(_T("Initially out stream offset: %lu\n"),
3933 (unsigned long)memOutStream
.TellO());
3935 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
3937 memOutStream
.PutC(*p
);
3940 wxPrintf(_T("Final out stream offset: %lu\n"),
3941 (unsigned long)memOutStream
.TellO());
3943 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3946 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
3948 wxMemoryInputStream
memInpStream(buf
, len
);
3949 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
3950 while ( !memInpStream
.Eof() )
3952 wxPutchar(memInpStream
.GetC());
3955 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3958 #endif // TEST_STREAMS
3960 // ----------------------------------------------------------------------------
3962 // ----------------------------------------------------------------------------
3966 #include "wx/timer.h"
3967 #include "wx/utils.h"
3969 static void TestStopWatch()
3971 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3975 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3978 wxPrintf(_T("\t%ldms\n"), sw
.Time());
3980 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3984 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3987 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3990 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3993 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3996 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3999 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
4000 for ( size_t n
= 0; n
< 70; n
++ )
4004 for ( size_t m
= 0; m
< 100000; m
++ )
4006 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
4008 wxPuts(_T("\ntime is negative - ERROR!"));
4016 wxPuts(_T(", ok."));
4019 #endif // TEST_TIMER
4021 // ----------------------------------------------------------------------------
4023 // ----------------------------------------------------------------------------
4027 #include "wx/vcard.h"
4029 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
4032 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
4035 wxPrintf(_T("%s%s"),
4036 wxString(_T('\t'), level
).c_str(),
4037 vcObj
->GetName().c_str());
4040 switch ( vcObj
->GetType() )
4042 case wxVCardObject::String
:
4043 case wxVCardObject::UString
:
4046 vcObj
->GetValue(&val
);
4047 value
<< _T('"') << val
<< _T('"');
4051 case wxVCardObject::Int
:
4054 vcObj
->GetValue(&i
);
4055 value
.Printf(_T("%u"), i
);
4059 case wxVCardObject::Long
:
4062 vcObj
->GetValue(&l
);
4063 value
.Printf(_T("%lu"), l
);
4067 case wxVCardObject::None
:
4070 case wxVCardObject::Object
:
4071 value
= _T("<node>");
4075 value
= _T("<unknown value type>");
4079 wxPrintf(_T(" = %s"), value
.c_str());
4082 DumpVObject(level
+ 1, *vcObj
);
4085 vcObj
= vcard
.GetNextProp(&cookie
);
4089 static void DumpVCardAddresses(const wxVCard
& vcard
)
4091 wxPuts(_T("\nShowing all addresses from vCard:\n"));
4095 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
4099 int flags
= addr
->GetFlags();
4100 if ( flags
& wxVCardAddress::Domestic
)
4102 flagsStr
<< _T("domestic ");
4104 if ( flags
& wxVCardAddress::Intl
)
4106 flagsStr
<< _T("international ");
4108 if ( flags
& wxVCardAddress::Postal
)
4110 flagsStr
<< _T("postal ");
4112 if ( flags
& wxVCardAddress::Parcel
)
4114 flagsStr
<< _T("parcel ");
4116 if ( flags
& wxVCardAddress::Home
)
4118 flagsStr
<< _T("home ");
4120 if ( flags
& wxVCardAddress::Work
)
4122 flagsStr
<< _T("work ");
4125 wxPrintf(_T("Address %u:\n")
4127 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
4130 addr
->GetPostOffice().c_str(),
4131 addr
->GetExtAddress().c_str(),
4132 addr
->GetStreet().c_str(),
4133 addr
->GetLocality().c_str(),
4134 addr
->GetRegion().c_str(),
4135 addr
->GetPostalCode().c_str(),
4136 addr
->GetCountry().c_str()
4140 addr
= vcard
.GetNextAddress(&cookie
);
4144 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
4146 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
4150 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
4154 int flags
= phone
->GetFlags();
4155 if ( flags
& wxVCardPhoneNumber::Voice
)
4157 flagsStr
<< _T("voice ");
4159 if ( flags
& wxVCardPhoneNumber::Fax
)
4161 flagsStr
<< _T("fax ");
4163 if ( flags
& wxVCardPhoneNumber::Cellular
)
4165 flagsStr
<< _T("cellular ");
4167 if ( flags
& wxVCardPhoneNumber::Modem
)
4169 flagsStr
<< _T("modem ");
4171 if ( flags
& wxVCardPhoneNumber::Home
)
4173 flagsStr
<< _T("home ");
4175 if ( flags
& wxVCardPhoneNumber::Work
)
4177 flagsStr
<< _T("work ");
4180 wxPrintf(_T("Phone number %u:\n")
4185 phone
->GetNumber().c_str()
4189 phone
= vcard
.GetNextPhoneNumber(&cookie
);
4193 static void TestVCardRead()
4195 wxPuts(_T("*** Testing wxVCard reading ***\n"));
4197 wxVCard
vcard(_T("vcard.vcf"));
4198 if ( !vcard
.IsOk() )
4200 wxPuts(_T("ERROR: couldn't load vCard."));
4204 // read individual vCard properties
4205 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
4209 vcObj
->GetValue(&value
);
4214 value
= _T("<none>");
4217 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
4220 if ( !vcard
.GetFullName(&value
) )
4222 value
= _T("<none>");
4225 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
4227 // now show how to deal with multiply occuring properties
4228 DumpVCardAddresses(vcard
);
4229 DumpVCardPhoneNumbers(vcard
);
4231 // and finally show all
4232 wxPuts(_T("\nNow dumping the entire vCard:\n")
4233 "-----------------------------\n");
4235 DumpVObject(0, vcard
);
4239 static void TestVCardWrite()
4241 wxPuts(_T("*** Testing wxVCard writing ***\n"));
4244 if ( !vcard
.IsOk() )
4246 wxPuts(_T("ERROR: couldn't create vCard."));
4251 vcard
.SetName("Zeitlin", "Vadim");
4252 vcard
.SetFullName("Vadim Zeitlin");
4253 vcard
.SetOrganization("wxWindows", "R&D");
4255 // just dump the vCard back
4256 wxPuts(_T("Entire vCard follows:\n"));
4257 wxPuts(vcard
.Write());
4261 #endif // TEST_VCARD
4263 // ----------------------------------------------------------------------------
4265 // ----------------------------------------------------------------------------
4267 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
4273 #include "wx/volume.h"
4275 static const wxChar
*volumeKinds
[] =
4281 _T("network volume"),
4285 static void TestFSVolume()
4287 wxPuts(_T("*** Testing wxFSVolume class ***"));
4289 wxArrayString volumes
= wxFSVolume::GetVolumes();
4290 size_t count
= volumes
.GetCount();
4294 wxPuts(_T("ERROR: no mounted volumes?"));
4298 wxPrintf(_T("%u mounted volumes found:\n"), count
);
4300 for ( size_t n
= 0; n
< count
; n
++ )
4302 wxFSVolume
vol(volumes
[n
]);
4305 wxPuts(_T("ERROR: couldn't create volume"));
4309 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
4311 vol
.GetDisplayName().c_str(),
4312 vol
.GetName().c_str(),
4313 volumeKinds
[vol
.GetKind()],
4314 vol
.IsWritable() ? _T("rw") : _T("ro"),
4315 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
4320 #endif // TEST_VOLUME
4322 // ----------------------------------------------------------------------------
4323 // wide char and Unicode support
4324 // ----------------------------------------------------------------------------
4328 static void TestUnicodeToFromAscii()
4330 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
4332 static const char *msg
= "Hello, world!";
4333 wxString s
= wxString::FromAscii(msg
);
4335 wxPrintf(_T("Message in Unicode: %s\n"), s
.c_str());
4336 printf("Message in ASCII: %s\n", (const char *)s
.ToAscii());
4338 wxPutchar(_T('\n'));
4341 #include "wx/textfile.h"
4343 static void TestUnicodeTextFileRead()
4345 wxPuts(_T("Testing wxTextFile in Unicode build\n"));
4348 if ( file
.Open(_T("testdata.fc"), wxConvLocal
) )
4350 const size_t count
= file
.GetLineCount();
4351 for ( size_t n
= 0; n
< count
; n
++ )
4353 const wxString
& s
= file
[n
];
4355 wxPrintf(_T("Line %u: \"%s\" (len %u, last char = '%c')\n"),
4356 (unsigned)n
, s
.c_str(), (unsigned)s
.length(), s
.Last());
4361 #endif // TEST_UNICODE
4365 #include "wx/strconv.h"
4366 #include "wx/fontenc.h"
4367 #include "wx/encconv.h"
4368 #include "wx/buffer.h"
4370 static const unsigned char utf8koi8r
[] =
4372 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4373 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4374 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4375 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4376 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4377 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4378 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4381 static const unsigned char utf8iso8859_1
[] =
4383 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4384 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4385 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4386 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4387 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4390 static const unsigned char utf8Invalid
[] =
4392 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4393 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4394 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4395 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4399 static const struct Utf8Data
4401 const unsigned char *text
;
4403 const wxChar
*charset
;
4404 wxFontEncoding encoding
;
4407 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4408 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
4409 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4412 static void TestUtf8()
4414 wxPuts(_T("*** Testing UTF8 support ***\n"));
4419 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
4421 const Utf8Data
& u8d
= utf8data
[n
];
4422 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
4423 WXSIZEOF(wbuf
)) == (size_t)-1 )
4425 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4429 wxCSConv
conv(u8d
.charset
);
4430 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
4432 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
4436 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
4440 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
4442 s
= _T("<< conversion failed >>");
4443 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
4447 wxPuts(wxEmptyString
);
4450 static void TestEncodingConverter()
4452 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4454 // using wxEncodingConverter should give the same result as above
4457 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
4458 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
4460 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4464 wxEncodingConverter ec
;
4465 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
4466 ec
.Convert(wbuf
, buf
);
4467 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
4470 wxPuts(wxEmptyString
);
4473 #endif // TEST_WCHAR
4475 // ----------------------------------------------------------------------------
4477 // ----------------------------------------------------------------------------
4481 #include "wx/filesys.h"
4482 #include "wx/fs_zip.h"
4483 #include "wx/zipstrm.h"
4485 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
4487 static void TestZipStreamRead()
4489 wxPuts(_T("*** Testing ZIP reading ***\n"));
4491 static const wxString filename
= _T("foo");
4492 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
4493 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4495 wxPrintf(_T("Dumping the file '%s':\n"), filename
.c_str());
4496 while ( !istr
.Eof() )
4498 wxPutchar(istr
.GetC());
4502 wxPuts(_T("\n----- done ------"));
4505 static void DumpZipDirectory(wxFileSystem
& fs
,
4506 const wxString
& dir
,
4507 const wxString
& indent
)
4509 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
4510 TESTFILE_ZIP
, dir
.c_str());
4511 wxString wildcard
= prefix
+ _T("/*");
4513 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
4514 while ( !dirname
.empty() )
4516 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
4518 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4523 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
4525 DumpZipDirectory(fs
, dirname
,
4526 indent
+ wxString(_T(' '), 4));
4528 dirname
= fs
.FindNext();
4531 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
4532 while ( !filename
.empty() )
4534 if ( !filename
.StartsWith(prefix
, &filename
) )
4536 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4541 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
4543 filename
= fs
.FindNext();
4547 static void TestZipFileSystem()
4549 wxPuts(_T("*** Testing ZIP file system ***\n"));
4551 wxFileSystem::AddHandler(new wxZipFSHandler
);
4553 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
4555 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
4560 // ----------------------------------------------------------------------------
4562 // ----------------------------------------------------------------------------
4566 #include "wx/zstream.h"
4567 #include "wx/wfstream.h"
4569 static const wxString FILENAME_GZ
= _T("test.gz");
4570 static const wxChar
*TEST_DATA
= _T("hello and hello and hello and hello and hello");
4572 static void TestZlibStreamWrite()
4574 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4576 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
4577 wxZlibOutputStream
ostr(fileOutStream
);
4578 wxPrintf(_T("Compressing the test string... "));
4579 ostr
.Write(TEST_DATA
, wxStrlen(TEST_DATA
) + 1);
4582 wxPuts(_T("(ERROR: failed)"));
4589 wxPuts(_T("\n----- done ------"));
4592 static void TestZlibStreamRead()
4594 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4596 wxFileInputStream
fileInStream(FILENAME_GZ
);
4597 wxZlibInputStream
istr(fileInStream
);
4598 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4600 wxPuts(_T("Dumping the file:"));
4601 while ( !istr
.Eof() )
4603 wxPutchar(istr
.GetC());
4607 wxPuts(_T("\n----- done ------"));
4612 // ----------------------------------------------------------------------------
4614 // ----------------------------------------------------------------------------
4618 #include "wx/wfstream.h"
4619 #include "wx/gzstream.h"
4620 #include "wx/filename.h"
4621 #include "wx/txtstrm.h"
4623 // Reads two input streams and verifies that they are the same (and non-emtpy)
4625 void GzipVerify(wxInputStream
&in1
, wxInputStream
&in2
)
4628 wxPuts(_T(" Can't verify"));
4632 const int BUFSIZE
= 8192;
4633 wxCharBuffer
buf1(BUFSIZE
);
4634 wxCharBuffer
buf2(BUFSIZE
);
4639 int n1
= in1
.Read(buf1
.data(), BUFSIZE
).LastRead();
4640 int n2
= in2
.Read(buf2
.data(), BUFSIZE
).LastRead();
4642 if (n1
!= n2
|| (n1
&& memcmp(buf1
, buf2
, n1
) != 0) || (!n1
&& none
)) {
4643 wxPuts(_T(" Failure"));
4648 wxPuts(_T(" Success"));
4656 in1
.Read(buf1
.data(), BUFSIZE
);
4658 in2
.Read(buf2
.data(), BUFSIZE
);
4661 // Write a gzip file and read it back.
4665 wxPuts(_T("*** Testing gzip streams ***\n"));
4667 const wxString testname
= _T("gziptest");
4668 const wxString gzipname
= testname
+ _T(".gz");
4670 // write some random test data to a testfile
4671 wxPuts(_T("Writing random test data to ") + testname
+ _T("..."));
4673 wxFFileOutputStream
outstream(testname
);
4674 wxTextOutputStream
textout(outstream
);
4676 for (int i
= 0; i
< 1000 && outstream
.Ok(); i
++)
4677 textout
<< rand() << rand() << rand() << rand() << endl
;
4679 wxPuts(_T(" Done"));
4682 wxFileName
fn(testname
);
4683 wxDateTime dt
= fn
.GetModificationTime();
4684 wxFFileInputStream
instream(testname
);
4686 // try writing a gzip file
4687 wxPuts(_T("Writing ") + gzipname
+ _T(" using wxGzipOutputStream..."));
4689 wxFFileOutputStream
outstream(gzipname
);
4690 wxGzipOutputStream
gzip(outstream
, testname
, dt
);
4692 if (!gzip
.Write(instream
))
4693 wxPuts(_T(" Failure"));
4695 wxPuts(_T(" Success"));
4698 // try reading the gzip file
4699 wxPuts(_T("Reading ") + gzipname
+ _T(" using wxGzipInputStream..."));
4702 wxFFileInputStream
instream2(gzipname
);
4703 wxGzipInputStream
gzip(instream2
);
4704 GzipVerify(instream
, gzip
);
4706 if (gzip
.GetName() != fn
.GetFullName())
4707 wxPuts(gzipname
+ _T(" contains incorrect filename: ")
4709 if (dt
.IsValid() && gzip
.GetDateTime() != dt
)
4710 wxPuts(gzipname
+ _T(" contains incorrect timestamp: ")
4711 + gzip
.GetDateTime().Format());
4715 // then verify it using gzip program if it is in the path
4716 wxPuts(_T("Reading ") + gzipname
+ _T(" using gzip program..."));
4717 wxFFile
file(popen((_T("gzip -d -c ") + gzipname
).mb_str(), "r"));
4719 wxFFileInputStream
instream2(file
);
4721 GzipVerify(instream
, instream2
);
4726 // try reading a gzip created by gzip program
4727 wxPuts(_T("Reading output of gzip program using wxGzipInputStream..."));
4728 file
.Attach(popen((_T("gzip -c ") + testname
).mb_str(), "r"));
4730 wxFFileInputStream
instream2(file
);
4731 wxGzipInputStream
gzip(instream2
);
4733 GzipVerify(instream
, gzip
);
4739 wxPuts(_T("\n--- Done gzip streams ---"));
4744 // ----------------------------------------------------------------------------
4746 // ----------------------------------------------------------------------------
4748 #ifdef TEST_DATETIME
4752 #include "wx/datetime.h"
4757 wxDateTime::wxDateTime_t day
;
4758 wxDateTime::Month month
;
4760 wxDateTime::wxDateTime_t hour
, min
, sec
;
4762 wxDateTime::WeekDay wday
;
4763 time_t gmticks
, ticks
;
4765 void Init(const wxDateTime::Tm
& tm
)
4774 gmticks
= ticks
= -1;
4777 wxDateTime
DT() const
4778 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
4780 bool SameDay(const wxDateTime::Tm
& tm
) const
4782 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
4785 wxString
Format() const
4788 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4790 wxDateTime::GetMonthName(month
).c_str(),
4792 abs(wxDateTime::ConvertYearToBC(year
)),
4793 year
> 0 ? _T("AD") : _T("BC"));
4797 wxString
FormatDate() const
4800 s
.Printf(_T("%02d-%s-%4d%s"),
4802 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4803 abs(wxDateTime::ConvertYearToBC(year
)),
4804 year
> 0 ? _T("AD") : _T("BC"));
4809 static const Date testDates
[] =
4811 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
4812 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
4813 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
4814 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
4815 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
4816 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
4817 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
4818 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
4819 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
4820 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
4821 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
4822 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
4823 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
4824 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
4825 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
4826 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
4827 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
4828 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
4829 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
4832 // this test miscellaneous static wxDateTime functions
4833 static void TestTimeStatic()
4835 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4837 // some info about the current date
4838 int year
= wxDateTime::GetCurrentYear();
4839 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4841 wxDateTime::IsLeapYear(year
) ? "" : "not ",
4842 wxDateTime::GetNumberOfDays(year
));
4844 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
4845 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4846 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4847 wxDateTime::GetMonthName(month
).c_str(),
4848 wxDateTime::GetNumberOfDays(month
));
4851 static const size_t nYears
= 5;
4852 static const size_t years
[2][nYears
] =
4854 // first line: the years to test
4855 { 1990, 1976, 2000, 2030, 1984, },
4857 // second line: true if leap, false otherwise
4858 { false, true, true, false, true }
4861 for ( size_t n
= 0; n
< nYears
; n
++ )
4863 int year
= years
[0][n
];
4864 bool should
= years
[1][n
] != 0,
4865 is
= wxDateTime::IsLeapYear(year
);
4867 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4870 should
== is
? "ok" : "ERROR");
4872 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
4876 // test constructing wxDateTime objects
4877 static void TestTimeSet()
4879 wxPuts(_T("\n*** wxDateTime construction test ***"));
4881 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4883 const Date
& d1
= testDates
[n
];
4884 wxDateTime dt
= d1
.DT();
4887 d2
.Init(dt
.GetTm());
4889 wxString s1
= d1
.Format(),
4892 wxPrintf(_T("Date: %s == %s (%s)\n"),
4893 s1
.c_str(), s2
.c_str(),
4894 s1
== s2
? _T("ok") : _T("ERROR"));
4898 // test time zones stuff
4899 static void TestTimeZones()
4901 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4903 wxDateTime now
= wxDateTime::Now();
4905 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
4906 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
4907 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
4908 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
4909 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
4910 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
4912 wxDateTime::Tm tm
= now
.GetTm();
4913 if ( wxDateTime(tm
) != now
)
4915 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4916 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
4920 // test some minimal support for the dates outside the standard range
4921 static void TestTimeRange()
4923 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4925 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
4927 wxPrintf(_T("Unix epoch:\t%s\n"),
4928 wxDateTime(2440587.5).Format(fmt
).c_str());
4929 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4930 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
4931 wxPrintf(_T("JDN 0: \t%s\n"),
4932 wxDateTime(0.0).Format(fmt
).c_str());
4933 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4934 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
4935 wxPrintf(_T("May 29, 2099:\t%s\n"),
4936 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
4939 static void TestTimeTicks()
4941 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4943 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4945 const Date
& d
= testDates
[n
];
4946 if ( d
.ticks
== -1 )
4949 wxDateTime dt
= d
.DT();
4950 long ticks
= (dt
.GetValue() / 1000).ToLong();
4951 wxPrintf(_T("Ticks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4952 if ( ticks
== d
.ticks
)
4954 wxPuts(_T(" (ok)"));
4958 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4959 (long)d
.ticks
, (long)(ticks
- d
.ticks
));
4962 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
4963 ticks
= (dt
.GetValue() / 1000).ToLong();
4964 wxPrintf(_T("GMtks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4965 if ( ticks
== d
.gmticks
)
4967 wxPuts(_T(" (ok)"));
4971 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4972 (long)d
.gmticks
, (long)(ticks
- d
.gmticks
));
4976 wxPuts(wxEmptyString
);
4979 // test conversions to JDN &c
4980 static void TestTimeJDN()
4982 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4984 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4986 const Date
& d
= testDates
[n
];
4987 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4988 double jdn
= dt
.GetJulianDayNumber();
4990 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d
.Format().c_str(), jdn
);
4993 wxPuts(_T(" (ok)"));
4997 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4998 d
.jdn
, jdn
- d
.jdn
);
5003 // test week days computation
5004 static void TestTimeWDays()
5006 wxPuts(_T("\n*** wxDateTime weekday test ***"));
5008 // test GetWeekDay()
5010 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
5012 const Date
& d
= testDates
[n
];
5013 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
5015 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
5016 wxPrintf(_T("%s is: %s"),
5018 wxDateTime::GetWeekDayName(wday
).c_str());
5019 if ( wday
== d
.wday
)
5021 wxPuts(_T(" (ok)"));
5025 wxPrintf(_T(" (ERROR: should be %s)\n"),
5026 wxDateTime::GetWeekDayName(d
.wday
).c_str());
5030 wxPuts(wxEmptyString
);
5032 // test SetToWeekDay()
5033 struct WeekDateTestData
5035 Date date
; // the real date (precomputed)
5036 int nWeek
; // its week index in the month
5037 wxDateTime::WeekDay wday
; // the weekday
5038 wxDateTime::Month month
; // the month
5039 int year
; // and the year
5041 wxString
Format() const
5044 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
5046 case 1: which
= _T("first"); break;
5047 case 2: which
= _T("second"); break;
5048 case 3: which
= _T("third"); break;
5049 case 4: which
= _T("fourth"); break;
5050 case 5: which
= _T("fifth"); break;
5052 case -1: which
= _T("last"); break;
5057 which
+= _T(" from end");
5060 s
.Printf(_T("The %s %s of %s in %d"),
5062 wxDateTime::GetWeekDayName(wday
).c_str(),
5063 wxDateTime::GetMonthName(month
).c_str(),
5070 // the array data was generated by the following python program
5072 from DateTime import *
5073 from whrandom import *
5074 from string import *
5076 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5077 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5079 week = DateTimeDelta(7)
5082 year = randint(1900, 2100)
5083 month = randint(1, 12)
5084 day = randint(1, 28)
5085 dt = DateTime(year, month, day)
5086 wday = dt.day_of_week
5088 countFromEnd = choice([-1, 1])
5091 while dt.month is month:
5092 dt = dt - countFromEnd * week
5093 weekNum = weekNum + countFromEnd
5095 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
5097 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
5098 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
5101 static const WeekDateTestData weekDatesTestData
[] =
5103 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
5104 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
5105 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
5106 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
5107 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
5108 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
5109 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
5110 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
5111 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
5112 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
5113 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
5114 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
5115 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
5116 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
5117 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
5118 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
5119 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
5120 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
5121 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
5122 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
5125 static const wxChar
*fmt
= _T("%d-%b-%Y");
5128 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
5130 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
5132 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
5134 wxPrintf(_T("%s is %s"), wd
.Format().c_str(), dt
.Format(fmt
).c_str());
5136 const Date
& d
= wd
.date
;
5137 if ( d
.SameDay(dt
.GetTm()) )
5139 wxPuts(_T(" (ok)"));
5143 dt
.Set(d
.day
, d
.month
, d
.year
);
5145 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.Format(fmt
).c_str());
5150 // test the computation of (ISO) week numbers
5151 static void TestTimeWNumber()
5153 wxPuts(_T("\n*** wxDateTime week number test ***"));
5155 struct WeekNumberTestData
5157 Date date
; // the date
5158 wxDateTime::wxDateTime_t week
; // the week number in the year
5159 wxDateTime::wxDateTime_t wmon
; // the week number in the month
5160 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
5161 wxDateTime::wxDateTime_t dnum
; // day number in the year
5164 // data generated with the following python script:
5166 from DateTime import *
5167 from whrandom import *
5168 from string import *
5170 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
5171 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
5173 def GetMonthWeek(dt):
5174 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
5175 if weekNumMonth < 0:
5176 weekNumMonth = weekNumMonth + 53
5179 def GetLastSundayBefore(dt):
5180 if dt.iso_week[2] == 7:
5183 return dt - DateTimeDelta(dt.iso_week[2])
5186 year = randint(1900, 2100)
5187 month = randint(1, 12)
5188 day = randint(1, 28)
5189 dt = DateTime(year, month, day)
5190 dayNum = dt.day_of_year
5191 weekNum = dt.iso_week[1]
5192 weekNumMonth = GetMonthWeek(dt)
5195 dtSunday = GetLastSundayBefore(dt)
5197 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
5198 weekNumMonth2 = weekNumMonth2 + 1
5199 dtSunday = dtSunday - DateTimeDelta(7)
5201 data = { 'day': rjust(`day`, 2), \
5202 'month': monthNames[month - 1], \
5204 'weekNum': rjust(`weekNum`, 2), \
5205 'weekNumMonth': weekNumMonth, \
5206 'weekNumMonth2': weekNumMonth2, \
5207 'dayNum': rjust(`dayNum`, 3) }
5209 print " { { %(day)s, "\
5210 "wxDateTime::%(month)s, "\
5213 "%(weekNumMonth)s, "\
5214 "%(weekNumMonth2)s, "\
5215 "%(dayNum)s }," % data
5218 static const WeekNumberTestData weekNumberTestDates
[] =
5220 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
5221 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
5222 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
5223 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
5224 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
5225 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
5226 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
5227 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
5228 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
5229 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
5230 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
5231 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
5232 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
5233 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
5234 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
5235 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
5236 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
5237 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
5238 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
5239 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
5242 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
5244 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
5245 const Date
& d
= wn
.date
;
5247 wxDateTime dt
= d
.DT();
5249 wxDateTime::wxDateTime_t
5250 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
5251 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5252 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5253 dnum
= dt
.GetDayOfYear();
5255 wxPrintf(_T("%s: the day number is %d"), d
.FormatDate().c_str(), dnum
);
5256 if ( dnum
== wn
.dnum
)
5258 wxPrintf(_T(" (ok)"));
5262 wxPrintf(_T(" (ERROR: should be %d)"), wn
.dnum
);
5265 wxPrintf(_T(", week in month = %d"), wmon
);
5266 if ( wmon
!= wn
.wmon
)
5268 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon
);
5271 wxPrintf(_T(" or %d"), wmon2
);
5272 if ( wmon2
== wn
.wmon2
)
5274 wxPrintf(_T(" (ok)"));
5278 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon2
);
5281 wxPrintf(_T(", week in year = %d"), week
);
5282 if ( week
!= wn
.week
)
5284 wxPrintf(_T(" (ERROR: should be %d)"), wn
.week
);
5287 wxPutchar(_T('\n'));
5289 wxDateTime
dt2(1, wxDateTime::Jan
, d
.year
);
5290 dt2
.SetToTheWeek(wn
.week
, dt
.GetWeekDay());
5294 d2
.Init(dt2
.GetTm());
5295 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
5296 d2
.FormatDate().c_str());
5301 // test DST calculations
5302 static void TestTimeDST()
5304 wxPuts(_T("\n*** wxDateTime DST test ***"));
5306 wxPrintf(_T("DST is%s in effect now.\n\n"),
5307 wxDateTime::Now().IsDST() ? wxEmptyString
: _T(" not"));
5309 // taken from http://www.energy.ca.gov/daylightsaving.html
5310 static const Date datesDST
[2][2004 - 1900 + 1] =
5313 { 1, wxDateTime::Apr
, 1990 },
5314 { 7, wxDateTime::Apr
, 1991 },
5315 { 5, wxDateTime::Apr
, 1992 },
5316 { 4, wxDateTime::Apr
, 1993 },
5317 { 3, wxDateTime::Apr
, 1994 },
5318 { 2, wxDateTime::Apr
, 1995 },
5319 { 7, wxDateTime::Apr
, 1996 },
5320 { 6, wxDateTime::Apr
, 1997 },
5321 { 5, wxDateTime::Apr
, 1998 },
5322 { 4, wxDateTime::Apr
, 1999 },
5323 { 2, wxDateTime::Apr
, 2000 },
5324 { 1, wxDateTime::Apr
, 2001 },
5325 { 7, wxDateTime::Apr
, 2002 },
5326 { 6, wxDateTime::Apr
, 2003 },
5327 { 4, wxDateTime::Apr
, 2004 },
5330 { 28, wxDateTime::Oct
, 1990 },
5331 { 27, wxDateTime::Oct
, 1991 },
5332 { 25, wxDateTime::Oct
, 1992 },
5333 { 31, wxDateTime::Oct
, 1993 },
5334 { 30, wxDateTime::Oct
, 1994 },
5335 { 29, wxDateTime::Oct
, 1995 },
5336 { 27, wxDateTime::Oct
, 1996 },
5337 { 26, wxDateTime::Oct
, 1997 },
5338 { 25, wxDateTime::Oct
, 1998 },
5339 { 31, wxDateTime::Oct
, 1999 },
5340 { 29, wxDateTime::Oct
, 2000 },
5341 { 28, wxDateTime::Oct
, 2001 },
5342 { 27, wxDateTime::Oct
, 2002 },
5343 { 26, wxDateTime::Oct
, 2003 },
5344 { 31, wxDateTime::Oct
, 2004 },
5349 for ( year
= 1990; year
< 2005; year
++ )
5351 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
5352 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
5354 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5355 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
5357 size_t n
= year
- 1990;
5358 const Date
& dBegin
= datesDST
[0][n
];
5359 const Date
& dEnd
= datesDST
[1][n
];
5361 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
5363 wxPuts(_T(" (ok)"));
5367 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5368 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
5369 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
5373 wxPuts(wxEmptyString
);
5375 for ( year
= 1990; year
< 2005; year
++ )
5377 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5379 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
5380 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
5384 // test wxDateTime -> text conversion
5385 static void TestTimeFormat()
5387 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5389 // some information may be lost during conversion, so store what kind
5390 // of info should we recover after a round trip
5393 CompareNone
, // don't try comparing
5394 CompareBoth
, // dates and times should be identical
5395 CompareDate
, // dates only
5396 CompareTime
// time only
5401 CompareKind compareKind
;
5402 const wxChar
*format
;
5403 } formatTestFormats
[] =
5405 { CompareBoth
, _T("---> %c") },
5406 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
5407 { CompareBoth
, _T("Date is %x, time is %X") },
5408 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5409 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
5410 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
5413 static const Date formatTestDates
[] =
5415 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
5416 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
5418 // this test can't work for other centuries because it uses two digit
5419 // years in formats, so don't even try it
5420 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
5421 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
5422 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
5426 // an extra test (as it doesn't depend on date, don't do it in the loop)
5427 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5429 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
5431 wxPuts(wxEmptyString
);
5433 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
5434 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
5436 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
5437 wxPrintf(_T("%s"), s
.c_str());
5439 // what can we recover?
5440 int kind
= formatTestFormats
[n
].compareKind
;
5444 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
5447 // converion failed - should it have?
5448 if ( kind
== CompareNone
)
5449 wxPuts(_T(" (ok)"));
5451 wxPuts(_T(" (ERROR: conversion back failed)"));
5455 // should have parsed the entire string
5456 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5460 bool equal
= false; // suppress compilaer warning
5468 equal
= dt
.IsSameDate(dt2
);
5472 equal
= dt
.IsSameTime(dt2
);
5478 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5479 dt2
.Format().c_str(), dt
.Format().c_str());
5483 wxPuts(_T(" (ok)"));
5490 // test text -> wxDateTime conversion
5491 static void TestTimeParse()
5493 wxPuts(_T("\n*** wxDateTime parse test ***"));
5495 struct ParseTestData
5497 const wxChar
*format
;
5502 static const ParseTestData parseTestDates
[] =
5504 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, true },
5505 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, true },
5508 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
5510 const wxChar
*format
= parseTestDates
[n
].format
;
5512 wxPrintf(_T("%s => "), format
);
5515 if ( dt
.ParseRfc822Date(format
) )
5517 wxPrintf(_T("%s "), dt
.Format().c_str());
5519 if ( parseTestDates
[n
].good
)
5521 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
5528 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal
.Format().c_str());
5533 wxPuts(_T("(ERROR: bad format)"));
5538 wxPrintf(_T("bad format (%s)\n"),
5539 parseTestDates
[n
].good
? "ERROR" : "ok");
5544 static void TestDateTimeInteractive()
5546 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5552 wxPrintf(_T("Enter a date: "));
5553 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
5556 // kill the last '\n'
5557 buf
[wxStrlen(buf
) - 1] = 0;
5560 const wxChar
*p
= dt
.ParseDate(buf
);
5563 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
5569 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
5572 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5573 dt
.Format(_T("%b %d, %Y")).c_str(),
5575 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5576 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5577 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
5580 wxPuts(_T("\n*** done ***"));
5583 static void TestTimeMS()
5585 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5587 wxDateTime dt1
= wxDateTime::Now(),
5588 dt2
= wxDateTime::UNow();
5590 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
5591 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5592 wxPrintf(_T("Dummy loop: "));
5593 for ( int i
= 0; i
< 6000; i
++ )
5595 //for ( int j = 0; j < 10; j++ )
5598 s
.Printf(_T("%g"), sqrt(i
));
5604 wxPuts(_T(", done"));
5607 dt2
= wxDateTime::UNow();
5608 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5610 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
5612 wxPuts(_T("\n*** done ***"));
5615 static void TestTimeArithmetics()
5617 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5619 static const struct ArithmData
5621 ArithmData(const wxDateSpan
& sp
, const wxChar
*nam
)
5622 : span(sp
), name(nam
) { }
5626 } testArithmData
[] =
5628 ArithmData(wxDateSpan::Day(), _T("day")),
5629 ArithmData(wxDateSpan::Week(), _T("week")),
5630 ArithmData(wxDateSpan::Month(), _T("month")),
5631 ArithmData(wxDateSpan::Year(), _T("year")),
5632 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5635 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
5637 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
5639 wxDateSpan span
= testArithmData
[n
].span
;
5643 const wxChar
*name
= testArithmData
[n
].name
;
5644 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5645 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
5646 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
5648 wxPrintf(_T("Going back: %s"), (dt1
- span
).FormatISODate().c_str());
5649 if ( dt1
- span
== dt
)
5651 wxPuts(_T(" (ok)"));
5655 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5658 wxPrintf(_T("Going forward: %s"), (dt2
+ span
).FormatISODate().c_str());
5659 if ( dt2
+ span
== dt
)
5661 wxPuts(_T(" (ok)"));
5665 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5668 wxPrintf(_T("Double increment: %s"), (dt2
+ 2*span
).FormatISODate().c_str());
5669 if ( dt2
+ 2*span
== dt1
)
5671 wxPuts(_T(" (ok)"));
5675 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2
.FormatISODate().c_str());
5678 wxPuts(wxEmptyString
);
5682 static void TestTimeHolidays()
5684 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5686 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
5687 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
5688 dtEnd
= dtStart
.GetLastMonthDay();
5690 wxDateTimeArray hol
;
5691 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
5693 const wxChar
*format
= _T("%d-%b-%Y (%a)");
5695 wxPrintf(_T("All holidays between %s and %s:\n"),
5696 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
5698 size_t count
= hol
.GetCount();
5699 for ( size_t n
= 0; n
< count
; n
++ )
5701 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
5704 wxPuts(wxEmptyString
);
5707 static void TestTimeZoneBug()
5709 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5711 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
5712 for ( int i
= 0; i
< 31; i
++ )
5714 wxPrintf(_T("Date %s: week day %s.\n"),
5715 date
.Format(_T("%d-%m-%Y")).c_str(),
5716 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
5718 date
+= wxDateSpan::Day();
5721 wxPuts(wxEmptyString
);
5724 static void TestTimeSpanFormat()
5726 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5728 static const wxChar
*formats
[] =
5730 _T("(default) %H:%M:%S"),
5731 _T("%E weeks and %D days"),
5732 _T("%l milliseconds"),
5733 _T("(with ms) %H:%M:%S:%l"),
5734 _T("100%% of minutes is %M"), // test "%%"
5735 _T("%D days and %H hours"),
5736 _T("or also %S seconds"),
5739 wxTimeSpan
ts1(1, 2, 3, 4),
5741 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
5743 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5744 ts1
.Format(formats
[n
]).c_str(),
5745 ts2
.Format(formats
[n
]).c_str());
5748 wxPuts(wxEmptyString
);
5751 #endif // TEST_DATETIME
5753 // ----------------------------------------------------------------------------
5754 // wxTextInput/OutputStream
5755 // ----------------------------------------------------------------------------
5757 #ifdef TEST_TEXTSTREAM
5759 #include "wx/txtstrm.h"
5760 #include "wx/wfstream.h"
5762 static void TestTextInputStream()
5764 wxPuts(_T("\n*** wxTextInputStream test ***"));
5766 wxString filename
= _T("testdata.fc");
5767 wxFileInputStream
fsIn(filename
);
5770 wxPuts(_T("ERROR: couldn't open file."));
5774 wxTextInputStream
tis(fsIn
);
5779 const wxString s
= tis
.ReadLine();
5781 // line could be non empty if the last line of the file isn't
5782 // terminated with EOL
5783 if ( fsIn
.Eof() && s
.empty() )
5786 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
5791 #endif // TEST_TEXTSTREAM
5793 // ----------------------------------------------------------------------------
5795 // ----------------------------------------------------------------------------
5799 #include "wx/thread.h"
5801 static size_t gs_counter
= (size_t)-1;
5802 static wxCriticalSection gs_critsect
;
5803 static wxSemaphore gs_cond
;
5805 class MyJoinableThread
: public wxThread
5808 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
5809 { m_n
= n
; Create(); }
5811 // thread execution starts here
5812 virtual ExitCode
Entry();
5818 wxThread::ExitCode
MyJoinableThread::Entry()
5820 unsigned long res
= 1;
5821 for ( size_t n
= 1; n
< m_n
; n
++ )
5825 // it's a loooong calculation :-)
5829 return (ExitCode
)res
;
5832 class MyDetachedThread
: public wxThread
5835 MyDetachedThread(size_t n
, wxChar ch
)
5839 m_cancelled
= false;
5844 // thread execution starts here
5845 virtual ExitCode
Entry();
5848 virtual void OnExit();
5851 size_t m_n
; // number of characters to write
5852 wxChar m_ch
; // character to write
5854 bool m_cancelled
; // false if we exit normally
5857 wxThread::ExitCode
MyDetachedThread::Entry()
5860 wxCriticalSectionLocker
lock(gs_critsect
);
5861 if ( gs_counter
== (size_t)-1 )
5867 for ( size_t n
= 0; n
< m_n
; n
++ )
5869 if ( TestDestroy() )
5879 wxThread::Sleep(100);
5885 void MyDetachedThread::OnExit()
5887 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5889 wxCriticalSectionLocker
lock(gs_critsect
);
5890 if ( !--gs_counter
&& !m_cancelled
)
5894 static void TestDetachedThreads()
5896 wxPuts(_T("\n*** Testing detached threads ***"));
5898 static const size_t nThreads
= 3;
5899 MyDetachedThread
*threads
[nThreads
];
5901 for ( n
= 0; n
< nThreads
; n
++ )
5903 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
5906 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
5907 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
5909 for ( n
= 0; n
< nThreads
; n
++ )
5914 // wait until all threads terminate
5917 wxPuts(wxEmptyString
);
5920 static void TestJoinableThreads()
5922 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5924 // calc 10! in the background
5925 MyJoinableThread
thread(10);
5928 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5929 (unsigned long)thread
.Wait());
5932 static void TestThreadSuspend()
5934 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5936 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
5940 // this is for this demo only, in a real life program we'd use another
5941 // condition variable which would be signaled from wxThread::Entry() to
5942 // tell us that the thread really started running - but here just wait a
5943 // bit and hope that it will be enough (the problem is, of course, that
5944 // the thread might still not run when we call Pause() which will result
5946 wxThread::Sleep(300);
5948 for ( size_t n
= 0; n
< 3; n
++ )
5952 wxPuts(_T("\nThread suspended"));
5955 // don't sleep but resume immediately the first time
5956 wxThread::Sleep(300);
5958 wxPuts(_T("Going to resume the thread"));
5963 wxPuts(_T("Waiting until it terminates now"));
5965 // wait until the thread terminates
5968 wxPuts(wxEmptyString
);
5971 static void TestThreadDelete()
5973 // As above, using Sleep() is only for testing here - we must use some
5974 // synchronisation object instead to ensure that the thread is still
5975 // running when we delete it - deleting a detached thread which already
5976 // terminated will lead to a crash!
5978 wxPuts(_T("\n*** Testing thread delete function ***"));
5980 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
5984 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5986 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
5990 wxThread::Sleep(300);
5994 wxPuts(_T("\nDeleted a running thread."));
5996 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
6000 wxThread::Sleep(300);
6006 wxPuts(_T("\nDeleted a sleeping thread."));
6008 MyJoinableThread
thread3(20);
6013 wxPuts(_T("\nDeleted a joinable thread."));
6015 MyJoinableThread
thread4(2);
6018 wxThread::Sleep(300);
6022 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
6024 wxPuts(wxEmptyString
);
6027 class MyWaitingThread
: public wxThread
6030 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
6033 m_condition
= condition
;
6038 virtual ExitCode
Entry()
6040 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
6045 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
6049 m_condition
->Wait();
6052 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
6060 wxCondition
*m_condition
;
6063 static void TestThreadConditions()
6066 wxCondition
condition(mutex
);
6068 // otherwise its difficult to understand which log messages pertain to
6070 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
6071 // condition.GetId(), gs_cond.GetId());
6073 // create and launch threads
6074 MyWaitingThread
*threads
[10];
6077 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
6079 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
6082 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
6087 // wait until all threads run
6088 wxPuts(_T("Main thread is waiting for the other threads to start"));
6091 size_t nRunning
= 0;
6092 while ( nRunning
< WXSIZEOF(threads
) )
6098 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
6102 wxPuts(_T("Main thread: all threads started up."));
6105 wxThread::Sleep(500);
6108 // now wake one of them up
6109 wxPrintf(_T("Main thread: about to signal the condition.\n"));
6114 wxThread::Sleep(200);
6116 // wake all the (remaining) threads up, so that they can exit
6117 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
6119 condition
.Broadcast();
6121 // give them time to terminate (dirty!)
6122 wxThread::Sleep(500);
6125 #include "wx/utils.h"
6127 class MyExecThread
: public wxThread
6130 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
6136 virtual ExitCode
Entry()
6138 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
6145 static void TestThreadExec()
6147 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
6149 MyExecThread
thread(_T("true"));
6152 wxPrintf(_T("Main program exit code: %ld.\n"),
6153 wxExecute(_T("false"), wxEXEC_SYNC
));
6155 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
6159 #include "wx/datetime.h"
6161 class MySemaphoreThread
: public wxThread
6164 MySemaphoreThread(int i
, wxSemaphore
*sem
)
6165 : wxThread(wxTHREAD_JOINABLE
),
6172 virtual ExitCode
Entry()
6174 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
6175 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6179 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
6180 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6184 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
6185 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6197 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
6199 static void TestSemaphore()
6201 wxPuts(_T("*** Testing wxSemaphore class. ***"));
6203 static const int SEM_LIMIT
= 3;
6205 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
6206 ArrayThreads threads
;
6208 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
6210 threads
.Add(new MySemaphoreThread(i
, &sem
));
6211 threads
.Last()->Run();
6214 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
6221 #endif // TEST_THREADS
6223 // ----------------------------------------------------------------------------
6225 // ----------------------------------------------------------------------------
6229 #include "wx/timer.h"
6230 #include "wx/tokenzr.h"
6232 static void TestStringConstruction()
6234 wxPuts(_T("*** Testing wxString constructores ***"));
6236 #define TEST_CTOR(args, res) \
6239 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
6242 wxPuts(_T("(ok)")); \
6246 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
6250 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
6251 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
6252 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
6253 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
6255 static const wxChar
*s
= _T("?really!");
6256 const wxChar
*start
= wxStrchr(s
, _T('r'));
6257 const wxChar
*end
= wxStrchr(s
, _T('!'));
6258 TEST_CTOR((start
, end
), _T("really"));
6260 wxPuts(wxEmptyString
);
6263 static void TestString()
6273 for (int i
= 0; i
< 1000000; ++i
)
6277 c
= _T("! How'ya doin'?");
6280 c
= _T("Hello world! What's up?");
6285 wxPrintf(_T("TestString elapsed time: %ld\n"), sw
.Time());
6288 static void TestPChar()
6296 for (int i
= 0; i
< 1000000; ++i
)
6298 wxStrcpy (a
, _T("Hello"));
6299 wxStrcpy (b
, _T(" world"));
6300 wxStrcpy (c
, _T("! How'ya doin'?"));
6303 wxStrcpy (c
, _T("Hello world! What's up?"));
6304 if (wxStrcmp (c
, a
) == 0)
6305 wxStrcpy (c
, _T("Doh!"));
6308 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw
.Time());
6311 static void TestStringSub()
6313 wxString
s(_T("Hello, world!"));
6315 wxPuts(_T("*** Testing wxString substring extraction ***"));
6317 wxPrintf(_T("String = '%s'\n"), s
.c_str());
6318 wxPrintf(_T("Left(5) = '%s'\n"), s
.Left(5).c_str());
6319 wxPrintf(_T("Right(6) = '%s'\n"), s
.Right(6).c_str());
6320 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
6321 wxPrintf(_T("Mid(3) = '%s'\n"), s
.Mid(3).c_str());
6322 wxPrintf(_T("substr(3, 5) = '%s'\n"), s
.substr(3, 5).c_str());
6323 wxPrintf(_T("substr(3) = '%s'\n"), s
.substr(3).c_str());
6325 static const wxChar
*prefixes
[] =
6329 _T("Hello, world!"),
6330 _T("Hello, world!!!"),
6336 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
6338 wxString prefix
= prefixes
[n
], rest
;
6339 bool rc
= s
.StartsWith(prefix
, &rest
);
6340 wxPrintf(_T("StartsWith('%s') = %s"), prefix
.c_str(), rc
? _T("true") : _T("false"));
6343 wxPrintf(_T(" (the rest is '%s')\n"), rest
.c_str());
6351 wxPuts(wxEmptyString
);
6354 static void TestStringFormat()
6356 wxPuts(_T("*** Testing wxString formatting ***"));
6359 s
.Printf(_T("%03d"), 18);
6361 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6362 wxPrintf(_T("Number 18: %s\n"), s
.c_str());
6364 wxPuts(wxEmptyString
);
6367 // returns "not found" for npos, value for all others
6368 static wxString
PosToString(size_t res
)
6370 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
6371 : wxString::Format(_T("%u"), res
);
6375 static void TestStringFind()
6377 wxPuts(_T("*** Testing wxString find() functions ***"));
6379 static const wxChar
*strToFind
= _T("ell");
6380 static const struct StringFindTest
6384 result
; // of searching "ell" in str
6387 { _T("Well, hello world"), 0, 1 },
6388 { _T("Well, hello world"), 6, 7 },
6389 { _T("Well, hello world"), 9, wxString::npos
},
6392 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
6394 const StringFindTest
& ft
= findTestData
[n
];
6395 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
6397 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6398 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
6400 size_t resTrue
= ft
.result
;
6401 if ( res
== resTrue
)
6407 wxPrintf(_T("(ERROR: should be %s)\n"),
6408 PosToString(resTrue
).c_str());
6412 wxPuts(wxEmptyString
);
6415 static void TestStringTokenizer()
6417 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6419 static const wxChar
*modeNames
[] =
6423 _T("return all empty"),
6428 static const struct StringTokenizerTest
6430 const wxChar
*str
; // string to tokenize
6431 const wxChar
*delims
; // delimiters to use
6432 size_t count
; // count of token
6433 wxStringTokenizerMode mode
; // how should we tokenize it
6434 } tokenizerTestData
[] =
6436 { _T(""), _T(" "), 0 },
6437 { _T("Hello, world"), _T(" "), 2 },
6438 { _T("Hello, world "), _T(" "), 2 },
6439 { _T("Hello, world"), _T(","), 2 },
6440 { _T("Hello, world!"), _T(",!"), 2 },
6441 { _T("Hello,, world!"), _T(",!"), 3 },
6442 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
6443 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6444 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
6445 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
6446 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
6447 { _T("01/02/99"), _T("/-"), 3 },
6448 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
6451 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
6453 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
6454 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
6456 size_t count
= tkz
.CountTokens();
6457 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6458 MakePrintable(tt
.str
).c_str(),
6460 MakePrintable(tt
.delims
).c_str(),
6461 modeNames
[tkz
.GetMode()]);
6462 if ( count
== tt
.count
)
6468 wxPrintf(_T("(ERROR: should be %u)\n"), tt
.count
);
6473 // if we emulate strtok(), check that we do it correctly
6474 wxChar
*buf
, *s
= NULL
, *last
;
6476 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
6478 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
6479 wxStrcpy(buf
, tt
.str
);
6481 s
= wxStrtok(buf
, tt
.delims
, &last
);
6488 // now show the tokens themselves
6490 while ( tkz
.HasMoreTokens() )
6492 wxString token
= tkz
.GetNextToken();
6494 wxPrintf(_T("\ttoken %u: '%s'"),
6496 MakePrintable(token
).c_str());
6502 wxPuts(_T(" (ok)"));
6506 wxPrintf(_T(" (ERROR: should be %s)\n"), s
);
6509 s
= wxStrtok(NULL
, tt
.delims
, &last
);
6513 // nothing to compare with
6514 wxPuts(wxEmptyString
);
6518 if ( count2
!= count
)
6520 wxPuts(_T("\tERROR: token count mismatch"));
6526 wxPuts(wxEmptyString
);
6529 static void TestStringReplace()
6531 wxPuts(_T("*** Testing wxString::replace ***"));
6533 static const struct StringReplaceTestData
6535 const wxChar
*original
; // original test string
6536 size_t start
, len
; // the part to replace
6537 const wxChar
*replacement
; // the replacement string
6538 const wxChar
*result
; // and the expected result
6539 } stringReplaceTestData
[] =
6541 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6542 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6543 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6544 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6545 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6548 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
6550 const StringReplaceTestData data
= stringReplaceTestData
[n
];
6552 wxString original
= data
.original
;
6553 original
.replace(data
.start
, data
.len
, data
.replacement
);
6555 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6556 data
.original
, data
.start
, data
.len
, data
.replacement
,
6559 if ( original
== data
.result
)
6565 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
6569 wxPuts(wxEmptyString
);
6573 static void TestStringMatch()
6575 wxPuts(_T("*** Testing wxString::Matches() ***"));
6577 static const struct StringMatchTestData
6580 const wxChar
*wildcard
;
6582 } stringMatchTestData
[] =
6584 { _T("foobar"), _T("foo*"), 1 },
6585 { _T("foobar"), _T("*oo*"), 1 },
6586 { _T("foobar"), _T("*bar"), 1 },
6587 { _T("foobar"), _T("??????"), 1 },
6588 { _T("foobar"), _T("f??b*"), 1 },
6589 { _T("foobar"), _T("f?b*"), 0 },
6590 { _T("foobar"), _T("*goo*"), 0 },
6591 { _T("foobar"), _T("*foo"), 0 },
6592 { _T("foobarfoo"), _T("*foo"), 1 },
6593 { _T(""), _T("*"), 1 },
6594 { _T(""), _T("?"), 0 },
6597 for ( size_t n
= 0; n
< WXSIZEOF(stringMatchTestData
); n
++ )
6599 const StringMatchTestData
& data
= stringMatchTestData
[n
];
6600 bool matches
= wxString(data
.text
).Matches(data
.wildcard
);
6601 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6603 matches
? _T("matches") : _T("doesn't match"),
6605 matches
== data
.matches
? _T("ok") : _T("ERROR"));
6608 wxPuts(wxEmptyString
);
6612 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6613 void ok(int line
, bool ok
, const wxString
& msg
= wxEmptyString
)
6616 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line
) +
6620 void is(int line
, const wxString
& got
, const wxString
& expected
,
6621 const wxString
& msg
= wxEmptyString
)
6623 bool isOk
= got
== expected
;
6624 ok(line
, isOk
, msg
);
6627 wxPuts(_T("Got: ") + got
);
6628 wxPuts(_T("Expected: ") + expected
);
6633 void is(int line
, const wxChar
* got
, const wxChar
* expected
,
6634 const wxString
& msg
= wxEmptyString
)
6636 bool isOk
= wxStrcmp( got
, expected
) == 0;
6637 ok(line
, isOk
, msg
);
6640 wxPuts(_T("Got: ") + wxString(got
));
6641 wxPuts(_T("Expected: ") + wxString(expected
));
6646 void is(int line
, const wxChar
& got
, const wxChar
& expected
,
6647 const wxString
& msg
= wxEmptyString
)
6649 bool isOk
= got
== expected
;
6650 ok(line
, isOk
, msg
);
6653 wxPuts(_T("Got: ") + got
);
6654 wxPuts(_T("Expected: ") + expected
);
6658 void is(int line
, size_t got
, size_t expected
,
6659 const wxString
& msg
= wxEmptyString
)
6661 bool isOk
= got
== expected
;
6662 ok(line
, isOk
, msg
);
6665 wxPuts(wxString::Format(_T("Got: %d"), got
));
6666 wxPuts(wxString::Format(_T("Expected: %d"), expected
));
6670 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6671 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6673 void TestStdString()
6675 wxPuts(_T("*** Testing std::string operations ***\n"));
6678 wxString
s1(_T("abcdefgh")),
6679 s2(_T("abcdefghijklm"), 8),
6680 s3(_T("abcdefghijklm")),
6684 s7(s3
.begin(), s3
.begin() + 8);
6685 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
6687 is( __LINE__
, s1
, _T("abcdefgh") );
6688 is( __LINE__
, s2
, s1
);
6689 is( __LINE__
, s4
, _T("aaaaaaaa") );
6690 is( __LINE__
, s5
, _T("abcdefgh") );
6691 is( __LINE__
, s6
, s1
);
6692 is( __LINE__
, s7
, s1
);
6693 is( __LINE__
, s8
, _T("efgh") );
6696 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6697 s1
.append(_T("def"));
6698 s2
.append(_T("defgh"), 3);
6699 s3
.append(wxString(_T("abcdef")), 3, 6);
6701 s5
.append(3, _T('a'));
6702 s6
.append(s1
.begin() + 3, s1
.end());
6704 is( __LINE__
, s1
, _T("abcdef") );
6705 is( __LINE__
, s2
, _T("abcdef") );
6706 is( __LINE__
, s3
, _T("abcdef") );
6707 is( __LINE__
, s4
, _T("abcabcdef") );
6708 is( __LINE__
, s5
, _T("abcaaa") );
6709 is( __LINE__
, s6
, _T("abcdef") );
6712 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6713 s1
.assign(_T("def"));
6714 s2
.assign(_T("defgh"), 3);
6715 s3
.assign(wxString(_T("abcdef")), 3, 6);
6717 s5
.assign(3, _T('a'));
6718 s6
.assign(s1
.begin() + 1, s1
.end());
6720 is( __LINE__
, s1
, _T("def") );
6721 is( __LINE__
, s2
, _T("def") );
6722 is( __LINE__
, s3
, _T("def") );
6723 is( __LINE__
, s4
, _T("def") );
6724 is( __LINE__
, s5
, _T("aaa") );
6725 is( __LINE__
, s6
, _T("ef") );
6728 s1
= _T("abcdefgh");
6729 s2
= _T("abcdefgh");
6731 s4
= _T("abcdefghi");
6734 s7
= _T("zabcdefg");
6736 ok( __LINE__
, s1
.compare(s2
) == 0 );
6737 ok( __LINE__
, s1
.compare(s3
) > 0 );
6738 ok( __LINE__
, s1
.compare(s4
) < 0 );
6739 ok( __LINE__
, s1
.compare(s5
) > 0 );
6740 ok( __LINE__
, s1
.compare(s6
) < 0 );
6741 ok( __LINE__
, s1
.compare(1, 12, s1
) > 0);
6742 ok( __LINE__
, s1
.compare(_T("abcdefgh")) == 0);
6743 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh")) == 0);
6744 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
6749 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
6750 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
6751 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
6753 is( __LINE__
, s1
, _T("acdefgh") );
6754 is( __LINE__
, s2
, _T("abcd") );
6755 is( __LINE__
, s3
, _T("ac") );
6756 is( __LINE__
, s4
, _T("abcdghi") );
6757 is( __LINE__
, s7
, _T("zabc") );
6758 is( __LINE__
, *it
, _T('c') );
6759 is( __LINE__
, *it2
, _T('g') );
6760 ok( __LINE__
, it3
== s7
.end() );
6764 // 01234567890123456789012345
6765 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6768 is_nom( s1
.find(_T('A')), 7u );
6769 is_nom( s1
.find(_T('A'), 7), 7u );
6770 is_nom( s1
.find(_T('Z')), wxString::npos
);
6771 is_nom( s1
.find(_T('C'), 22), 25u );
6773 is_nom( s1
.find(_T("gAB")), 6u );
6774 is_nom( s1
.find(_T("gAB"), 7), wxString::npos
);
6775 is_nom( s1
.find(_T("gAB"), 6), 6u );
6777 is_nom( s1
.find(_T("gABZZZ"), 2, 3), 6u );
6778 is_nom( s1
.find(_T("gABZZZ"), 7, 3), wxString::npos
);
6780 is_nom( s1
.find(s2
), 6u );
6781 is_nom( s1
.find(s2
, 7), wxString::npos
);
6782 is_nom( s1
.find(s2
, 6), 6u );
6784 // find_first_not_of
6786 // 01234567890123456789012345678901234
6787 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6790 is_nom( s1
.find_first_not_of(_T('a')), 6u );
6791 is_nom( s1
.find_first_not_of(_T('a'), 7), 7u );
6792 is_nom( s2
.find_first_not_of(_T('a')), wxString::npos
);
6794 is_nom( s1
.find_first_not_of(_T("abde"), 4), 7u );
6795 is_nom( s1
.find_first_not_of(_T("abde"), 7), 7u );
6796 is_nom( s1
.find_first_not_of(_T("abcdefghijkl")), wxString::npos
);
6798 is_nom( s1
.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
6801 is_nom( s1
.find_first_of(_T('c')), 7u );
6802 is_nom( s1
.find_first_of(_T('v')), wxString::npos
);
6803 is_nom( s1
.find_first_of(_T('c'), 10), 24u );
6805 is_nom( s1
.find_first_of(_T("ijkl")), 13u );
6806 is_nom( s1
.find_first_of(_T("ddcfg"), 17), 24u );
6807 is_nom( s1
.find_first_of(_T("ddcfga"), 17, 5), 24u );
6811 // 01234567890123456789012345678901234
6812 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6815 is_nom( s2
.find_last_not_of(_T('a')), wxString::npos
);
6816 is_nom( s1
.find_last_not_of(_T('d')), 33u );
6817 is_nom( s1
.find_last_not_of(_T('d'), 25), 24u );
6819 is_nom( s1
.find_last_not_of(_T("bcd")), 22u );
6820 is_nom( s1
.find_last_not_of(_T("abc"), 24), 16u );
6822 is_nom( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
6825 is_nom( s2
.find_last_of(_T('c')), wxString::npos
);
6826 is_nom( s1
.find_last_of(_T('a')), 22u );
6827 is_nom( s1
.find_last_of(_T('b'), 24), 23u );
6829 is_nom( s1
.find_last_of(_T("ijklm")), 16u );
6830 is_nom( s1
.find_last_of(_T("ijklma"), 33, 4), 16u );
6831 is_nom( s1
.find_last_of(_T("a"), 17), 17u );
6834 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
6835 s9
= s10
= _T("cdefg");
6837 s1
.insert(1, _T("cc") );
6838 s2
.insert(2, _T("cdef"), 3);
6840 s4
.insert(2, s10
, 3, 7);
6841 s5
.insert(1, 2, _T('c'));
6842 it
= s6
.insert(s6
.begin() + 3, _T('X'));
6843 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
6844 s8
.insert(s8
.begin(), 2, _T('c'));
6846 is( __LINE__
, s1
, _T("accaaa") );
6847 is( __LINE__
, s2
, _T("aacdeaa") );
6848 is( __LINE__
, s3
, _T("aacdefgaa") );
6849 is( __LINE__
, s4
, _T("aafgaa") );
6850 is( __LINE__
, s5
, _T("accaaa") );
6851 is( __LINE__
, s6
, _T("aaaXa") );
6852 is( __LINE__
, s7
, _T("cdefaaaa") );
6853 is( __LINE__
, s8
, _T("ccaaaa") );
6855 s1
= s2
= s3
= _T("aaaa");
6856 s1
.insert(0, _T("ccc"), 2);
6857 s2
.insert(4, _T("ccc"), 2);
6859 is( __LINE__
, s1
, _T("ccaaaa") );
6860 is( __LINE__
, s2
, _T("aaaacc") );
6863 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
6864 s9
= s10
= _T("werty");
6866 s1
.replace(3, 4, _T("rtyu"));
6867 s1
.replace(8, 7, _T("opopop"));
6868 s2
.replace(10, 12, _T("WWWW"));
6869 s3
.replace(1, 5, s9
);
6870 s4
.replace(1, 4, s9
, 0, 4);
6871 s5
.replace(1, 2, s9
, 1, 12);
6872 s6
.replace(0, 123, s9
, 0, 123);
6873 s7
.replace(2, 7, s9
);
6875 is( __LINE__
, s1
, _T("QWErtyuIopopop") );
6876 is( __LINE__
, s2
, _T("QWERTYUIOPWWWW") );
6877 is( __LINE__
, s3
, _T("QwertyUIOP") );
6878 is( __LINE__
, s4
, _T("QwertYUIOP") );
6879 is( __LINE__
, s5
, _T("QertyRTYUIOP") );
6880 is( __LINE__
, s6
, s9
);
6881 is( __LINE__
, s7
, _T("QWwertyP") );
6885 // 01234567890123456789012345
6886 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6890 is_nom( s1
.rfind(_T('A')), 23u );
6891 is_nom( s1
.rfind(_T('A'), 7), 7u );
6892 is_nom( s1
.rfind(_T('Z')), wxString::npos
);
6893 is_nom( s1
.rfind(_T('C'), 22), 19u );
6895 is_nom( s1
.rfind(_T("cAB")), 22u );
6896 is_nom( s1
.rfind(_T("cAB"), 15), wxString::npos
);
6897 is_nom( s1
.rfind(_T("cAB"), 21), 16u );
6899 is_nom( s1
.rfind(_T("gABZZZ"), 7, 3), 6u );
6900 is_nom( s1
.rfind(_T("gABZZZ"), 5, 3), wxString::npos
);
6902 is_nom( s1
.rfind(s2
), 6u );
6903 is_nom( s1
.rfind(s2
, 5), wxString::npos
);
6904 is_nom( s1
.rfind(s2
, 6), 6u );
6905 is_nom( s1
.rfind(s3
, 1), 0u );
6908 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
6912 s3
.resize( 14, _T(' ') );
6913 s4
.resize( 14, _T('W') );
6915 is_nom( s1
, _T("abcABCdefDEF") );
6916 is_nom( s2
, _T("abcABCdefD") );
6917 is_nom( s3
, _T("abcABCdefDEF ") );
6918 is_nom( s4
, _T("abcABCdefDEFWW") );
6921 s1
= _T("abcdefgABCDEFG");
6923 is_nom( s1
.substr( 0, 14 ), s1
);
6924 is_nom( s1
.substr( 1, 13 ), _T("bcdefgABCDEFG") );
6925 is_nom( s1
.substr( 1, 20 ), _T("bcdefgABCDEFG") );
6926 is_nom( s1
.substr( 14, 30 ), _T("") );
6928 wxPuts(_T("*** Testing std::string operations finished ***\n"));
6931 #endif // TEST_STRINGS
6933 // ----------------------------------------------------------------------------
6935 // ----------------------------------------------------------------------------
6937 #ifdef TEST_SNGLINST
6938 #include "wx/snglinst.h"
6939 #endif // TEST_SNGLINST
6941 int main(int argc
, char **argv
)
6943 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
6945 wxInitializer initializer
;
6948 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
6953 #ifdef TEST_SNGLINST
6954 wxSingleInstanceChecker checker
;
6955 if ( checker
.Create(_T(".wxconsole.lock")) )
6957 if ( checker
.IsAnotherRunning() )
6959 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
6964 // wait some time to give time to launch another instance
6965 wxPrintf(_T("Press \"Enter\" to continue..."));
6968 else // failed to create
6970 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
6972 #endif // TEST_SNGLINST
6976 #endif // TEST_CHARSET
6979 TestCmdLineConvert();
6981 #if wxUSE_CMDLINE_PARSER
6982 static const wxCmdLineEntryDesc cmdLineDesc
[] =
6984 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
6985 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
6986 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
6987 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
6989 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
6990 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
6991 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
6992 wxCMD_LINE_VAL_NUMBER
},
6993 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
6994 wxCMD_LINE_VAL_DATE
},
6996 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
6997 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
7003 wxChar
**wargv
= new wxChar
*[argc
+ 1];
7008 for (n
= 0; n
< argc
; n
++ )
7010 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
7011 wargv
[n
] = wxStrdup(warg
);
7018 #endif // wxUSE_UNICODE
7020 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
7024 for ( int n
= 0; n
< argc
; n
++ )
7029 #endif // wxUSE_UNICODE
7031 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
7032 wxCMD_LINE_VAL_STRING
,
7033 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
7035 switch ( parser
.Parse() )
7038 wxLogMessage(_T("Help was given, terminating."));
7042 ShowCmdLine(parser
);
7046 wxLogMessage(_T("Syntax error detected, aborting."));
7049 #endif // wxUSE_CMDLINE_PARSER
7051 #endif // TEST_CMDLINE
7058 TestStringConstruction();
7061 TestStringTokenizer();
7062 TestStringReplace();
7068 #endif // TEST_STRINGS
7078 #ifdef TEST_DLLLOADER
7080 #endif // TEST_DLLLOADER
7084 #endif // TEST_ENVIRON
7088 #endif // TEST_EXECUTE
7090 #ifdef TEST_FILECONF
7092 #endif // TEST_FILECONF
7101 #endif // TEST_LOCALE
7104 wxPuts(_T("*** Testing wxLog ***"));
7107 for ( size_t n
= 0; n
< 8000; n
++ )
7109 s
<< (wxChar
)(_T('A') + (n
% 26));
7112 wxLogWarning(_T("The length of the string is %lu"),
7113 (unsigned long)s
.length());
7116 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
7118 // this one shouldn't be truncated
7121 // but this one will because log functions use fixed size buffer
7122 // (note that it doesn't need '\n' at the end neither - will be added
7124 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
7133 #ifdef TEST_FILENAME
7134 TestFileNameConstruction();
7135 TestFileNameMakeRelative();
7136 TestFileNameMakeAbsolute();
7137 TestFileNameSplit();
7140 TestFileNameDirManip();
7141 TestFileNameComparison();
7142 TestFileNameOperations();
7143 #endif // TEST_FILENAME
7145 #ifdef TEST_FILETIME
7150 #endif // TEST_FILETIME
7153 wxLog::AddTraceMask(FTP_TRACE_MASK
);
7154 if ( TestFtpConnect() )
7164 #if TEST_INTERACTIVE
7165 TestFtpInteractive();
7168 //else: connecting to the FTP server failed
7175 #ifdef TEST_LONGLONG
7176 // seed pseudo random generator
7177 srand((unsigned)time(NULL
));
7184 TestMultiplication();
7187 TestLongLongConversion();
7188 TestBitOperations();
7189 TestLongLongComparison();
7190 TestLongLongToString();
7191 TestLongLongPrintf();
7193 #endif // TEST_LONGLONG
7201 #endif // TEST_HASHMAP
7205 #endif // TEST_HASHSET
7208 wxLog::AddTraceMask(_T("mime"));
7212 TestMimeAssociate();
7217 #ifdef TEST_INFO_FUNCTIONS
7222 #if TEST_INTERACTIVE
7226 #endif // TEST_INFO_FUNCTIONS
7228 #ifdef TEST_PATHLIST
7230 #endif // TEST_PATHLIST
7238 #endif // TEST_PRINTF
7245 #endif // TEST_REGCONF
7248 // TODO: write a real test using src/regex/tests file
7252 TestRegExSubmatch();
7253 TestRegExReplacement();
7255 #if TEST_INTERACTIVE
7256 TestRegExInteractive();
7259 #endif // TEST_REGEX
7261 #ifdef TEST_REGISTRY
7263 TestRegistryAssociation();
7264 #endif // TEST_REGISTRY
7269 #endif // TEST_SOCKETS
7276 #endif // TEST_STREAMS
7278 #ifdef TEST_TEXTSTREAM
7279 TestTextInputStream();
7280 #endif // TEST_TEXTSTREAM
7283 int nCPUs
= wxThread::GetCPUCount();
7284 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
7286 wxThread::SetConcurrency(nCPUs
);
7288 TestJoinableThreads();
7291 TestJoinableThreads();
7292 TestDetachedThreads();
7293 TestThreadSuspend();
7295 TestThreadConditions();
7299 #endif // TEST_THREADS
7303 #endif // TEST_TIMER
7305 #ifdef TEST_DATETIME
7317 TestTimeArithmetics();
7320 TestTimeSpanFormat();
7326 #if TEST_INTERACTIVE
7327 TestDateTimeInteractive();
7329 #endif // TEST_DATETIME
7331 #ifdef TEST_SCOPEGUARD
7336 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
7338 #endif // TEST_USLEEP
7343 #endif // TEST_VCARD
7347 #endif // TEST_VOLUME
7350 TestUnicodeTextFileRead();
7352 TestUnicodeToFromAscii();
7354 #endif // TEST_UNICODE
7358 TestEncodingConverter();
7359 #endif // TEST_WCHAR
7362 TestZipStreamRead();
7363 TestZipFileSystem();
7367 TestZlibStreamWrite();
7368 TestZlibStreamRead();