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)? uncomment the line below to do all tests
55 #define TEST_DLLLOADER
66 #define TEST_INFO_FUNCTIONS
78 #define TEST_SCOPEGUARD
83 #define TEST_TEXTSTREAM
87 // #define TEST_VCARD -- don't enable this (VZ)
94 static const bool TEST_ALL
= true;
100 static const bool TEST_ALL
= false;
103 // some tests are interactive, define this to run them
104 #ifdef TEST_INTERACTIVE
105 #undef TEST_INTERACTIVE
107 static const bool TEST_INTERACTIVE
= true;
109 static const bool TEST_INTERACTIVE
= false;
112 // ----------------------------------------------------------------------------
113 // test class for container objects
114 // ----------------------------------------------------------------------------
116 #if defined(TEST_ARRAYS) || 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_ARRAYS) || 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
);
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
& 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
, _T(""), 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"));
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());
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"));
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"
819 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
823 wxString full
= fn
.GetFullPath();
825 wxString vol
, path
, name
, ext
;
826 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
828 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
829 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
831 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
832 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
833 path
.c_str(), name
.c_str(), ext
.c_str());
835 wxPrintf(_T("path is also:\t'%s'\n"), fn
.GetPath().c_str());
836 wxPrintf(_T("with volume: \t'%s'\n"),
837 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
838 wxPrintf(_T("with separator:\t'%s'\n"),
839 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
840 wxPrintf(_T("with both: \t'%s'\n"),
841 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
843 wxPuts(_T("The directories in the path are:"));
844 wxArrayString dirs
= fn
.GetDirs();
845 size_t count
= dirs
.GetCount();
846 for ( size_t n
= 0; n
< count
; n
++ )
848 wxPrintf(_T("\t%u: %s\n"), n
, dirs
[n
].c_str());
852 static struct FileNameInfo
854 const wxChar
*fullname
;
855 const wxChar
*volume
;
864 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
865 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
866 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
867 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
868 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
869 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
870 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
871 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
872 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
874 // Windows file names
875 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
876 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
877 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
878 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
879 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
880 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
881 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
883 // wxFileName support for Mac file names is broken currently
886 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
887 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
888 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
889 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
890 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
891 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
895 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
896 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
899 static void TestFileNameConstruction()
901 wxPuts(_T("*** testing wxFileName construction ***"));
903 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
905 const FileNameInfo
& fni
= filenames
[n
];
907 wxFileName
fn(fni
.fullname
, fni
.format
);
909 wxString fullname
= fn
.GetFullPath(fni
.format
);
910 if ( fullname
!= fni
.fullname
)
912 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni
.fullname
);
915 bool isAbsolute
= fn
.IsAbsolute(fni
.format
);
916 wxPrintf(_T("'%s' is %s (%s)\n\t"),
918 isAbsolute
? "absolute" : "relative",
919 isAbsolute
== fni
.isAbsolute
? "ok" : "ERROR");
921 if ( !fn
.Normalize(wxPATH_NORM_ALL
, _T(""), fni
.format
) )
923 wxPuts(_T("ERROR (couldn't be normalized)"));
927 wxPrintf(_T("normalized: '%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
934 static void TestFileNameSplit()
936 wxPuts(_T("*** testing wxFileName splitting ***"));
938 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
940 const FileNameInfo
& fni
= filenames
[n
];
941 wxString volume
, path
, name
, ext
;
942 wxFileName::SplitPath(fni
.fullname
,
943 &volume
, &path
, &name
, &ext
, fni
.format
);
945 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
947 volume
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
949 if ( volume
!= fni
.volume
)
950 wxPrintf(_T(" (ERROR: volume = '%s')"), fni
.volume
);
951 if ( path
!= fni
.path
)
952 wxPrintf(_T(" (ERROR: path = '%s')"), fni
.path
);
953 if ( name
!= fni
.name
)
954 wxPrintf(_T(" (ERROR: name = '%s')"), fni
.name
);
955 if ( ext
!= fni
.ext
)
956 wxPrintf(_T(" (ERROR: ext = '%s')"), fni
.ext
);
962 static void TestFileNameTemp()
964 wxPuts(_T("*** testing wxFileName temp file creation ***"));
966 static const wxChar
*tmpprefixes
[] =
974 _T("/tmp/foo/bar"), // this one must be an error
978 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
980 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
983 // "error" is not in upper case because it may be ok
984 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
988 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
989 tmpprefixes
[n
], path
.c_str());
991 if ( !wxRemoveFile(path
) )
993 wxLogWarning(_T("Failed to remove temp file '%s'"),
1000 static void TestFileNameMakeRelative()
1002 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
1004 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1006 const FileNameInfo
& fni
= filenames
[n
];
1008 wxFileName
fn(fni
.fullname
, fni
.format
);
1010 // choose the base dir of the same format
1012 switch ( fni
.format
)
1015 base
= _T("/usr/bin/");
1024 // TODO: I don't know how this is supposed to work there
1027 case wxPATH_NATIVE
: // make gcc happy
1029 wxFAIL_MSG( _T("unexpected path format") );
1032 wxPrintf(_T("'%s' relative to '%s': "),
1033 fn
.GetFullPath(fni
.format
).c_str(), base
.c_str());
1035 if ( !fn
.MakeRelativeTo(base
, fni
.format
) )
1037 wxPuts(_T("unchanged"));
1041 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1046 static void TestFileNameMakeAbsolute()
1048 wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
1050 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1052 const FileNameInfo
& fni
= filenames
[n
];
1053 wxFileName
fn(fni
.fullname
, fni
.format
);
1055 wxPrintf(_T("'%s' absolutized: "),
1056 fn
.GetFullPath(fni
.format
).c_str());
1058 wxPrintf(_T("'%s'\n"), fn
.GetFullPath(fni
.format
).c_str());
1064 static void TestFileNameComparison()
1069 static void TestFileNameOperations()
1074 static void TestFileNameCwd()
1079 #endif // TEST_FILENAME
1081 // ----------------------------------------------------------------------------
1082 // wxFileName time functions
1083 // ----------------------------------------------------------------------------
1085 #ifdef TEST_FILETIME
1087 #include <wx/filename.h>
1088 #include <wx/datetime.h>
1090 static void TestFileGetTimes()
1092 wxFileName
fn(_T("testdata.fc"));
1094 wxDateTime dtAccess
, dtMod
, dtCreate
;
1095 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
1097 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1101 static const wxChar
*fmt
= _T("%Y-%b-%d %H:%M:%S");
1103 wxPrintf(_T("File times for '%s':\n"), fn
.GetFullPath().c_str());
1104 wxPrintf(_T("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
1105 wxPrintf(_T("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
1106 wxPrintf(_T("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
1110 static void TestFileSetTimes()
1112 wxFileName
fn(_T("testdata.fc"));
1116 wxPrintf(_T("ERROR: Touch() failed.\n"));
1120 #endif // TEST_FILETIME
1122 // ----------------------------------------------------------------------------
1124 // ----------------------------------------------------------------------------
1128 #include "wx/hash.h"
1132 Foo(int n_
) { n
= n_
; count
++; }
1137 static size_t count
;
1140 size_t Foo::count
= 0;
1142 WX_DECLARE_LIST(Foo
, wxListFoos
);
1143 WX_DECLARE_HASH(Foo
, wxListFoos
, wxHashFoos
);
1145 #include "wx/listimpl.cpp"
1147 WX_DEFINE_LIST(wxListFoos
);
1149 #include "wx/timer.h"
1151 static void TestHash()
1153 wxPuts(_T("*** Testing wxHashTable ***\n"));
1154 const int COUNT
= 100;
1161 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
1165 for ( i
= 0; i
< COUNT
; ++i
)
1166 hash
.Put(i
, &o
+ i
);
1169 wxHashTable::compatibility_iterator it
= hash
.Next();
1179 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1181 for ( i
= 99; i
>= 0; --i
)
1182 if( hash
.Get(i
) != &o
+ i
)
1183 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1185 for ( i
= 0; i
< COUNT
; ++i
)
1186 hash
.Put(i
, &o
+ i
+ 20);
1188 for ( i
= 99; i
>= 0; --i
)
1189 if( hash
.Get(i
) != &o
+ i
)
1190 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1192 for ( i
= 0; i
< COUNT
/2; ++i
)
1193 if( hash
.Delete(i
) != &o
+ i
)
1194 wxPuts(_T("Error in wxHashTable::Delete\n"));
1196 for ( i
= COUNT
/2; i
< COUNT
; ++i
)
1197 if( hash
.Get(i
) != &o
+ i
)
1198 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1200 for ( i
= 0; i
< COUNT
/2; ++i
)
1201 if( hash
.Get(i
) != &o
+ i
+ 20)
1202 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1204 for ( i
= 0; i
< COUNT
/2; ++i
)
1205 if( hash
.Delete(i
) != &o
+ i
+ 20)
1206 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1208 for ( i
= 0; i
< COUNT
/2; ++i
)
1209 if( hash
.Get(i
) != NULL
)
1210 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1212 hash2
.Put(_T("foo"), &o
+ 1);
1213 hash2
.Put(_T("bar"), &o
+ 2);
1214 hash2
.Put(_T("baz"), &o
+ 3);
1216 if (hash2
.Get(_T("moo")) != NULL
)
1217 wxPuts(_T("Error in wxHashTable::Get\n"));
1219 if (hash2
.Get(_T("bar")) != &o
+ 2)
1220 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1222 hash2
.Put(_T("bar"), &o
+ 0);
1224 if (hash2
.Get(_T("bar")) != &o
+ 2)
1225 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1228 // and now some corner-case testing; 3 and 13 hash to the same bucket
1230 wxHashTable
hash(wxKEY_INTEGER
, 10);
1233 hash
.Put(3, &dummy
);
1236 if (hash
.Get(3) != NULL
)
1237 wxPuts(_T("Corner case 1 failure\n"));
1239 hash
.Put(3, &dummy
);
1240 hash
.Put(13, &dummy
);
1243 if (hash
.Get(3) != NULL
)
1244 wxPuts(_T("Corner case 2 failure\n"));
1248 if (hash
.Get(13) != NULL
)
1249 wxPuts(_T("Corner case 3 failure\n"));
1251 hash
.Put(3, &dummy
);
1252 hash
.Put(13, &dummy
);
1255 if (hash
.Get(13) != NULL
)
1256 wxPuts(_T("Corner case 4 failure\n"));
1260 if (hash
.Get(3) != NULL
)
1261 wxPuts(_T("Corner case 5 failure\n"));
1265 wxHashTable
hash(wxKEY_INTEGER
, 10);
1268 hash
.Put(3, 7, &dummy
+ 7);
1269 hash
.Put(4, 8, &dummy
+ 8);
1271 if (hash
.Get(7) != NULL
) wxPuts(_T("Key/Hash 1 failure\n"));
1272 if (hash
.Get(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 2 failure\n"));
1273 if (hash
.Get(4) != NULL
) wxPuts(_T("Key/Hash 3 failure\n"));
1274 if (hash
.Get(3) != NULL
) wxPuts(_T("Key/Hash 4 failure\n"));
1275 if (hash
.Get(8) != NULL
) wxPuts(_T("Key/Hash 5 failure\n"));
1276 if (hash
.Get(8, 4) != NULL
) wxPuts(_T("Key/Hash 6 failure\n"));
1278 if (hash
.Delete(7) != NULL
) wxPuts(_T("Key/Hash 7 failure\n"));
1279 if (hash
.Delete(3) != NULL
) wxPuts(_T("Key/Hash 8 failure\n"));
1280 if (hash
.Delete(3, 7) != &dummy
+ 7) wxPuts(_T("Key/Hash 8 failure\n"));
1285 hash
.DeleteContents(true);
1287 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1288 hash
.GetCount(), Foo::count
);
1290 static const int hashTestData
[] =
1292 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1296 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1298 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
1301 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1302 hash
.GetCount(), Foo::count
);
1304 wxPuts(_T("Hash access test:"));
1305 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1307 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1308 hashTestData
[n
], n
);
1309 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
1312 wxPrintf(_T("ERROR, not found.\n"));
1316 wxPrintf(_T("%d (%s)\n"), foo
->n
,
1317 (size_t)foo
->n
== n
? "ok" : "ERROR");
1321 wxPrintf(_T("\nTrying to get an element not in hash: "));
1323 if ( hash
.Get(1234) || hash
.Get(1, 0) )
1325 wxPuts(_T("ERROR: found!"));
1329 wxPuts(_T("ok (not found)"));
1332 Foo
* foo
= hash
.Delete(0);
1334 wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count
);
1338 wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count
);
1341 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count
);
1342 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1344 wxPrintf(_T("Time: %ld\n"), sw
.Time());
1349 // ----------------------------------------------------------------------------
1351 // ----------------------------------------------------------------------------
1355 #include "wx/hashmap.h"
1357 // test compilation of basic map types
1358 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
1359 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
1360 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
1361 myUnsignedHashMap
);
1362 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1364 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1366 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1368 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1372 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1373 // myStringHashMap );
1374 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
1376 typedef myStringHashMap::iterator Itor
;
1378 static void TestHashMap()
1380 wxPuts(_T("*** Testing wxHashMap ***\n"));
1381 myStringHashMap
sh(0); // as small as possible
1384 const size_t count
= 10000;
1386 // init with some data
1387 for( i
= 0; i
< count
; ++i
)
1389 buf
.Printf(wxT("%d"), i
);
1390 sh
[buf
] = wxT("A") + buf
+ wxT("C");
1393 // test that insertion worked
1394 if( sh
.size() != count
)
1396 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh
.size(), count
);
1399 for( i
= 0; i
< count
; ++i
)
1401 buf
.Printf(wxT("%d"), i
);
1402 if( sh
[buf
] != wxT("A") + buf
+ wxT("C") )
1404 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1409 // check that iterators work
1411 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1415 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1419 if( it
->second
!= sh
[it
->first
] )
1421 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1426 if( sh
.size() != i
)
1428 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i
, count
);
1431 // test copy ctor, assignment operator
1432 myStringHashMap
h1( sh
), h2( 0 );
1435 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1437 if( h1
[it
->first
] != it
->second
)
1439 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it
->first
.c_str());
1442 if( h2
[it
->first
] != it
->second
)
1444 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it
->first
.c_str());
1449 for( i
= 0; i
< count
; ++i
)
1451 buf
.Printf(wxT("%d"), i
);
1452 size_t sz
= sh
.size();
1454 // test find() and erase(it)
1457 it
= sh
.find( buf
);
1458 if( it
!= sh
.end() )
1462 if( sh
.find( buf
) != sh
.end() )
1464 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1468 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i
);
1473 size_t c
= sh
.erase( buf
);
1475 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1477 if( sh
.find( buf
) != sh
.end() )
1479 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1483 // count should decrease
1484 if( sh
.size() != sz
- 1 )
1486 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1490 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1493 #endif // TEST_HASHMAP
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1501 #include "wx/hashset.h"
1503 // test compilation of basic map types
1504 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
1505 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
1506 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
1507 myUnsignedHashSet
);
1508 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
1510 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
1512 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
1514 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
1516 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
1528 unsigned long operator()(const MyStruct
& s
) const
1529 { return m_dummy(s
.ptr
); }
1530 MyHash
& operator=(const MyHash
&) { return *this; }
1532 wxPointerHash m_dummy
;
1538 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
1539 { return s1
.ptr
== s2
.ptr
; }
1540 MyEqual
& operator=(const MyEqual
&) { return *this; }
1543 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
1545 typedef myTestHashSet5 wxStringHashSet
;
1547 static void TestHashSet()
1549 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1551 wxStringHashSet set1
;
1553 set1
.insert( _T("abc") );
1554 set1
.insert( _T("bbc") );
1555 set1
.insert( _T("cbc") );
1556 set1
.insert( _T("abc") );
1558 if( set1
.size() != 3 )
1559 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1565 tmp
.ptr
= &dummy
; tmp
.str
= _T("ABC");
1567 tmp
.ptr
= &dummy
+ 1;
1569 tmp
.ptr
= &dummy
; tmp
.str
= _T("CDE");
1572 if( set2
.size() != 2 )
1573 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1575 mySet::iterator it
= set2
.find( tmp
);
1577 if( it
== set2
.end() )
1578 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1579 if( it
->ptr
!= &dummy
)
1580 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1581 if( it
->str
!= _T("ABC") )
1582 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1584 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1587 #endif // TEST_HASHSET
1589 // ----------------------------------------------------------------------------
1591 // ----------------------------------------------------------------------------
1595 #include "wx/list.h"
1597 WX_DECLARE_LIST(Bar
, wxListBars
);
1598 #include "wx/listimpl.cpp"
1599 WX_DEFINE_LIST(wxListBars
);
1601 WX_DECLARE_LIST(int, wxListInt
);
1602 WX_DEFINE_LIST(wxListInt
);
1604 static void TestList()
1606 wxPuts(_T("*** Testing wxList operations ***\n"));
1612 for ( i
= 0; i
< 5; ++i
)
1613 list1
.Append(dummy
+ i
);
1615 if ( list1
.GetCount() != 5 )
1616 wxPuts(_T("Wrong number of items in list\n"));
1618 if ( list1
.Item(3)->GetData() != dummy
+ 3 )
1619 wxPuts(_T("Error in Item()\n"));
1621 if ( !list1
.Find(dummy
+ 4) )
1622 wxPuts(_T("Error in Find()\n"));
1624 wxListInt::compatibility_iterator node
= list1
.GetFirst();
1629 if ( node
->GetData() != dummy
+ i
)
1630 wxPuts(_T("Error in compatibility_iterator\n"));
1631 node
= node
->GetNext();
1635 if ( size_t(i
) != list1
.GetCount() )
1636 wxPuts(_T("Error in compatibility_iterator\n"));
1638 list1
.Insert(dummy
+ 0);
1639 list1
.Insert(1, dummy
+ 1);
1640 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
1642 node
= list1
.GetFirst();
1647 int* t
= node
->GetData();
1648 if ( t
!= dummy
+ i
)
1649 wxPuts(_T("Error in Insert\n"));
1650 node
= node
->GetNext();
1655 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1657 wxPuts(_T("*** Testing std::list operations ***\n"));
1661 wxListInt::iterator it
, en
;
1662 wxListInt::reverse_iterator rit
, ren
;
1664 for ( i
= 0; i
< 5; ++i
)
1665 list1
.push_back(i
+ &i
);
1667 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
1668 it
!= en
; ++it
, ++i
)
1669 if ( *it
!= i
+ &i
)
1670 wxPuts(_T("Error in iterator\n"));
1672 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
1673 rit
!= ren
; ++rit
, --i
)
1674 if ( *rit
!= i
+ &i
)
1675 wxPuts(_T("Error in reverse_iterator\n"));
1677 if ( *list1
.rbegin() != *--list1
.end() ||
1678 *list1
.begin() != *--list1
.rend() )
1679 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1680 if ( *list1
.begin() != *--++list1
.begin() ||
1681 *list1
.rbegin() != *--++list1
.rbegin() )
1682 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1684 if ( list1
.front() != &i
|| list1
.back() != &i
+ 4 )
1685 wxPuts(_T("Error in front()/back()\n"));
1687 list1
.erase(list1
.begin());
1688 list1
.erase(--list1
.end());
1690 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
1691 it
!= en
; ++it
, ++i
)
1692 if ( *it
!= i
+ &i
)
1693 wxPuts(_T("Error in erase()\n"));
1696 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1699 static void TestListCtor()
1701 wxPuts(_T("*** Testing wxList construction ***\n"));
1705 list1
.Append(new Bar(_T("first")));
1706 list1
.Append(new Bar(_T("second")));
1708 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1709 list1
.GetCount(), Bar::GetNumber());
1714 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1715 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
1718 list1
.DeleteContents(true);
1720 WX_CLEAR_LIST(wxListBars
, list1
);
1724 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1729 // ----------------------------------------------------------------------------
1731 // ----------------------------------------------------------------------------
1735 #include "wx/intl.h"
1736 #include "wx/utils.h" // for wxSetEnv
1738 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
1740 // find the name of the language from its value
1741 static const wxChar
*GetLangName(int lang
)
1743 static const wxChar
*languageNames
[] =
1753 _T("ARABIC_ALGERIA"),
1754 _T("ARABIC_BAHRAIN"),
1757 _T("ARABIC_JORDAN"),
1758 _T("ARABIC_KUWAIT"),
1759 _T("ARABIC_LEBANON"),
1761 _T("ARABIC_MOROCCO"),
1764 _T("ARABIC_SAUDI_ARABIA"),
1767 _T("ARABIC_TUNISIA"),
1774 _T("AZERI_CYRILLIC"),
1789 _T("CHINESE_SIMPLIFIED"),
1790 _T("CHINESE_TRADITIONAL"),
1791 _T("CHINESE_HONGKONG"),
1792 _T("CHINESE_MACAU"),
1793 _T("CHINESE_SINGAPORE"),
1794 _T("CHINESE_TAIWAN"),
1800 _T("DUTCH_BELGIAN"),
1804 _T("ENGLISH_AUSTRALIA"),
1805 _T("ENGLISH_BELIZE"),
1806 _T("ENGLISH_BOTSWANA"),
1807 _T("ENGLISH_CANADA"),
1808 _T("ENGLISH_CARIBBEAN"),
1809 _T("ENGLISH_DENMARK"),
1811 _T("ENGLISH_JAMAICA"),
1812 _T("ENGLISH_NEW_ZEALAND"),
1813 _T("ENGLISH_PHILIPPINES"),
1814 _T("ENGLISH_SOUTH_AFRICA"),
1815 _T("ENGLISH_TRINIDAD"),
1816 _T("ENGLISH_ZIMBABWE"),
1824 _T("FRENCH_BELGIAN"),
1825 _T("FRENCH_CANADIAN"),
1826 _T("FRENCH_LUXEMBOURG"),
1827 _T("FRENCH_MONACO"),
1833 _T("GERMAN_AUSTRIAN"),
1834 _T("GERMAN_BELGIUM"),
1835 _T("GERMAN_LIECHTENSTEIN"),
1836 _T("GERMAN_LUXEMBOURG"),
1854 _T("ITALIAN_SWISS"),
1859 _T("KASHMIRI_INDIA"),
1877 _T("MALAY_BRUNEI_DARUSSALAM"),
1878 _T("MALAY_MALAYSIA"),
1888 _T("NORWEGIAN_BOKMAL"),
1889 _T("NORWEGIAN_NYNORSK"),
1896 _T("PORTUGUESE_BRAZILIAN"),
1899 _T("RHAETO_ROMANCE"),
1902 _T("RUSSIAN_UKRAINE"),
1908 _T("SERBIAN_CYRILLIC"),
1909 _T("SERBIAN_LATIN"),
1910 _T("SERBO_CROATIAN"),
1921 _T("SPANISH_ARGENTINA"),
1922 _T("SPANISH_BOLIVIA"),
1923 _T("SPANISH_CHILE"),
1924 _T("SPANISH_COLOMBIA"),
1925 _T("SPANISH_COSTA_RICA"),
1926 _T("SPANISH_DOMINICAN_REPUBLIC"),
1927 _T("SPANISH_ECUADOR"),
1928 _T("SPANISH_EL_SALVADOR"),
1929 _T("SPANISH_GUATEMALA"),
1930 _T("SPANISH_HONDURAS"),
1931 _T("SPANISH_MEXICAN"),
1932 _T("SPANISH_MODERN"),
1933 _T("SPANISH_NICARAGUA"),
1934 _T("SPANISH_PANAMA"),
1935 _T("SPANISH_PARAGUAY"),
1937 _T("SPANISH_PUERTO_RICO"),
1938 _T("SPANISH_URUGUAY"),
1940 _T("SPANISH_VENEZUELA"),
1944 _T("SWEDISH_FINLAND"),
1962 _T("URDU_PAKISTAN"),
1964 _T("UZBEK_CYRILLIC"),
1977 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1978 return languageNames
[lang
];
1980 return _T("INVALID");
1983 static void TestDefaultLang()
1985 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1987 static const wxChar
*langStrings
[] =
1989 NULL
, // system default
1996 _T("de_DE.iso88591"),
1998 _T("?"), // invalid lang spec
1999 _T("klingonese"), // I bet on some systems it does exist...
2002 wxPrintf(_T("The default system encoding is %s (%d)\n"),
2003 wxLocale::GetSystemEncodingName().c_str(),
2004 wxLocale::GetSystemEncoding());
2006 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
2008 const wxChar
*langStr
= langStrings
[n
];
2011 // FIXME: this doesn't do anything at all under Windows, we need
2012 // to create a new wxLocale!
2013 wxSetEnv(_T("LC_ALL"), langStr
);
2016 int lang
= gs_localeDefault
.GetSystemLanguage();
2017 wxPrintf(_T("Locale for '%s' is %s.\n"),
2018 langStr
? langStr
: _T("system default"), GetLangName(lang
));
2022 #endif // TEST_LOCALE
2024 // ----------------------------------------------------------------------------
2026 // ----------------------------------------------------------------------------
2030 #include "wx/mimetype.h"
2032 static void TestMimeEnum()
2034 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
2036 wxArrayString mimetypes
;
2038 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
2040 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
2045 for ( size_t n
= 0; n
< count
; n
++ )
2047 wxFileType
*filetype
=
2048 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
2051 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
2052 mimetypes
[n
].c_str());
2056 filetype
->GetDescription(&desc
);
2057 filetype
->GetExtensions(exts
);
2059 filetype
->GetIcon(NULL
);
2062 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
2065 extsAll
<< _T(", ");
2069 wxPrintf(_T("\t%s: %s (%s)\n"),
2070 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
2076 static void TestMimeOverride()
2078 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2080 static const wxChar
*mailcap
= _T("/tmp/mailcap");
2081 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
2083 if ( wxFile::Exists(mailcap
) )
2084 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2086 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
2088 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2091 if ( wxFile::Exists(mimetypes
) )
2092 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2094 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
2096 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2102 static void TestMimeFilename()
2104 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2106 static const wxChar
*filenames
[] =
2114 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
2116 const wxString fname
= filenames
[n
];
2117 wxString ext
= fname
.AfterLast(_T('.'));
2118 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
2121 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
2126 if ( !ft
->GetDescription(&desc
) )
2127 desc
= _T("<no description>");
2130 if ( !ft
->GetOpenCommand(&cmd
,
2131 wxFileType::MessageParameters(fname
, _T(""))) )
2132 cmd
= _T("<no command available>");
2134 cmd
= wxString(_T('"')) + cmd
+ _T('"');
2136 wxPrintf(_T("To open %s (%s) do %s.\n"),
2137 fname
.c_str(), desc
.c_str(), cmd
.c_str());
2146 static void TestMimeAssociate()
2148 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2150 wxFileTypeInfo
ftInfo(
2151 _T("application/x-xyz"),
2152 _T("xyzview '%s'"), // open cmd
2153 _T(""), // print cmd
2154 _T("XYZ File"), // description
2155 _T(".xyz"), // extensions
2156 NULL
// end of extensions
2158 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
2160 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
2163 wxPuts(_T("ERROR: failed to create association!"));
2167 // TODO: read it back
2176 // ----------------------------------------------------------------------------
2177 // misc information functions
2178 // ----------------------------------------------------------------------------
2180 #ifdef TEST_INFO_FUNCTIONS
2182 #include "wx/utils.h"
2184 static void TestDiskInfo()
2186 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2190 wxChar pathname
[128];
2191 wxPrintf(_T("\nEnter a directory name: "));
2192 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
2195 // kill the last '\n'
2196 pathname
[wxStrlen(pathname
) - 1] = 0;
2198 wxLongLong total
, free
;
2199 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
2201 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2205 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2206 (total
/ 1024).ToString().c_str(),
2207 (free
/ 1024).ToString().c_str(),
2213 static void TestOsInfo()
2215 wxPuts(_T("*** Testing OS info functions ***\n"));
2218 wxGetOsVersion(&major
, &minor
);
2219 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2220 wxGetOsDescription().c_str(), major
, minor
);
2222 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2224 wxPrintf(_T("Host name is %s (%s).\n"),
2225 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2230 static void TestUserInfo()
2232 wxPuts(_T("*** Testing user info functions ***\n"));
2234 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2235 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2236 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2237 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2242 #endif // TEST_INFO_FUNCTIONS
2244 // ----------------------------------------------------------------------------
2246 // ----------------------------------------------------------------------------
2248 #ifdef TEST_LONGLONG
2250 #include "wx/longlong.h"
2251 #include "wx/timer.h"
2253 // make a 64 bit number from 4 16 bit ones
2254 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
2256 // get a random 64 bit number
2257 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
2259 static const long testLongs
[] =
2270 #if wxUSE_LONGLONG_WX
2271 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
2272 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2273 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
2274 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2275 #endif // wxUSE_LONGLONG_WX
2277 static void TestSpeed()
2279 static const long max
= 100000000;
2286 for ( n
= 0; n
< max
; n
++ )
2291 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw
.Time());
2294 #if wxUSE_LONGLONG_NATIVE
2299 for ( n
= 0; n
< max
; n
++ )
2304 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw
.Time());
2306 #endif // wxUSE_LONGLONG_NATIVE
2312 for ( n
= 0; n
< max
; n
++ )
2317 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw
.Time());
2321 static void TestLongLongConversion()
2323 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2327 for ( size_t n
= 0; n
< 100000; n
++ )
2331 #if wxUSE_LONGLONG_NATIVE
2332 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
2334 wxASSERT_MSG( a
== b
, _T("conversions failure") );
2336 wxPuts(_T("Can't do it without native long long type, test skipped."));
2339 #endif // wxUSE_LONGLONG_NATIVE
2341 if ( !(nTested
% 1000) )
2350 wxPuts(_T(" done!"));
2353 static void TestMultiplication()
2355 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2359 for ( size_t n
= 0; n
< 100000; n
++ )
2364 #if wxUSE_LONGLONG_NATIVE
2365 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
2366 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
2368 wxASSERT_MSG( a
*b
== aa
*bb
, _T("multiplication failure") );
2369 #else // !wxUSE_LONGLONG_NATIVE
2370 wxPuts(_T("Can't do it without native long long type, test skipped."));
2373 #endif // wxUSE_LONGLONG_NATIVE
2375 if ( !(nTested
% 1000) )
2384 wxPuts(_T(" done!"));
2387 static void TestDivision()
2389 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2393 for ( size_t n
= 0; n
< 100000; n
++ )
2395 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2396 // multiplication will not overflow)
2397 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
2399 // get a random (but non null) long (not wxLongLong for now) to divide
2411 #if wxUSE_LONGLONG_NATIVE
2412 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
2414 wxLongLongNative p
= m
/ l
, s
= m
% l
;
2415 wxASSERT_MSG( q
== p
&& r
== s
, _T("division failure") );
2416 #else // !wxUSE_LONGLONG_NATIVE
2417 // verify the result
2418 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
2419 #endif // wxUSE_LONGLONG_NATIVE
2421 if ( !(nTested
% 1000) )
2430 wxPuts(_T(" done!"));
2433 static void TestAddition()
2435 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2439 for ( size_t n
= 0; n
< 100000; n
++ )
2445 #if wxUSE_LONGLONG_NATIVE
2446 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
2447 wxLongLongNative(b
.GetHi(), b
.GetLo()),
2448 _T("addition failure") );
2449 #else // !wxUSE_LONGLONG_NATIVE
2450 wxASSERT_MSG( c
- b
== a
, "addition failure" );
2451 #endif // wxUSE_LONGLONG_NATIVE
2453 if ( !(nTested
% 1000) )
2462 wxPuts(_T(" done!"));
2465 static void TestBitOperations()
2467 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2471 for ( size_t n
= 0; n
< 100000; n
++ )
2475 #if wxUSE_LONGLONG_NATIVE
2476 for ( size_t n
= 0; n
< 33; n
++ )
2479 #else // !wxUSE_LONGLONG_NATIVE
2480 wxPuts(_T("Can't do it without native long long type, test skipped."));
2483 #endif // wxUSE_LONGLONG_NATIVE
2485 if ( !(nTested
% 1000) )
2494 wxPuts(_T(" done!"));
2497 static void TestLongLongComparison()
2499 #if wxUSE_LONGLONG_WX
2500 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
2502 static const long ls
[2] =
2508 wxLongLongWx lls
[2];
2512 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2516 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
2518 res
= lls
[m
] > testLongs
[n
];
2519 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
2520 ls
[m
], testLongs
[n
], res
? "true" : "false",
2521 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
2523 res
= lls
[m
] < testLongs
[n
];
2524 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
2525 ls
[m
], testLongs
[n
], res
? "true" : "false",
2526 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
2528 res
= lls
[m
] == testLongs
[n
];
2529 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
2530 ls
[m
], testLongs
[n
], res
? "true" : "false",
2531 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
2534 #endif // wxUSE_LONGLONG_WX
2537 static void TestLongLongToString()
2539 wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
2541 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2543 wxLongLong ll
= testLongs
[n
];
2544 wxPrintf(_T("%ld == %s\n"), testLongs
[n
], ll
.ToString().c_str());
2547 wxLongLong
ll(0x12345678, 0x87654321);
2548 wxPrintf(_T("0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2551 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2554 static void TestLongLongPrintf()
2556 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2558 #ifdef wxLongLongFmtSpec
2559 wxLongLong ll
= wxLL(0x1234567890abcdef);
2560 wxString s
= wxString::Format(_T("%") wxLongLongFmtSpec
_T("x"), ll
);
2561 wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
2562 s
.c_str(), s
== _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
2563 #else // !wxLongLongFmtSpec
2564 #error "wxLongLongFmtSpec not defined for this compiler/platform"
2571 #endif // TEST_LONGLONG
2573 // ----------------------------------------------------------------------------
2575 // ----------------------------------------------------------------------------
2577 #ifdef TEST_PATHLIST
2580 #define CMD_IN_PATH _T("ls")
2582 #define CMD_IN_PATH _T("command.com")
2585 static void TestPathList()
2587 wxPuts(_T("*** Testing wxPathList ***\n"));
2589 wxPathList pathlist
;
2590 pathlist
.AddEnvList(_T("PATH"));
2591 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
2594 wxPrintf(_T("ERROR: command not found in the path.\n"));
2598 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
2602 #endif // TEST_PATHLIST
2604 // ----------------------------------------------------------------------------
2605 // regular expressions
2606 // ----------------------------------------------------------------------------
2610 #include "wx/regex.h"
2612 static void TestRegExCompile()
2614 wxPuts(_T("*** Testing RE compilation ***\n"));
2616 static struct RegExCompTestData
2618 const wxChar
*pattern
;
2620 } regExCompTestData
[] =
2622 { _T("foo"), true },
2623 { _T("foo("), false },
2624 { _T("foo(bar"), false },
2625 { _T("foo(bar)"), true },
2626 { _T("foo["), false },
2627 { _T("foo[bar"), false },
2628 { _T("foo[bar]"), true },
2629 { _T("foo{"), true },
2630 { _T("foo{1"), false },
2631 { _T("foo{bar"), true },
2632 { _T("foo{1}"), true },
2633 { _T("foo{1,2}"), true },
2634 { _T("foo{bar}"), true },
2635 { _T("foo*"), true },
2636 { _T("foo**"), false },
2637 { _T("foo+"), true },
2638 { _T("foo++"), false },
2639 { _T("foo?"), true },
2640 { _T("foo??"), false },
2641 { _T("foo?+"), false },
2645 for ( size_t n
= 0; n
< WXSIZEOF(regExCompTestData
); n
++ )
2647 const RegExCompTestData
& data
= regExCompTestData
[n
];
2648 bool ok
= re
.Compile(data
.pattern
);
2650 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2652 ok
? _T("") : _T("not "),
2653 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2657 static void TestRegExMatch()
2659 wxPuts(_T("*** Testing RE matching ***\n"));
2661 static struct RegExMatchTestData
2663 const wxChar
*pattern
;
2666 } regExMatchTestData
[] =
2668 { _T("foo"), _T("bar"), false },
2669 { _T("foo"), _T("foobar"), true },
2670 { _T("^foo"), _T("foobar"), true },
2671 { _T("^foo"), _T("barfoo"), false },
2672 { _T("bar$"), _T("barbar"), true },
2673 { _T("bar$"), _T("barbar "), false },
2676 for ( size_t n
= 0; n
< WXSIZEOF(regExMatchTestData
); n
++ )
2678 const RegExMatchTestData
& data
= regExMatchTestData
[n
];
2680 wxRegEx
re(data
.pattern
);
2681 bool ok
= re
.Matches(data
.text
);
2683 wxPrintf(_T("'%s' %s %s (%s)\n"),
2685 ok
? _T("matches") : _T("doesn't match"),
2687 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2691 static void TestRegExSubmatch()
2693 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2695 wxRegEx
re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2696 if ( !re
.IsValid() )
2698 wxPuts(_T("ERROR: compilation failed."));
2702 wxString text
= _T("Fri Jul 13 18:37:52 CEST 2001");
2704 if ( !re
.Matches(text
) )
2706 wxPuts(_T("ERROR: match expected."));
2710 wxPrintf(_T("Entire match: %s\n"), re
.GetMatch(text
).c_str());
2712 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2713 re
.GetMatch(text
, 3).c_str(),
2714 re
.GetMatch(text
, 2).c_str(),
2715 re
.GetMatch(text
, 4).c_str(),
2716 re
.GetMatch(text
, 1).c_str());
2720 static void TestRegExReplacement()
2722 wxPuts(_T("*** Testing RE replacement ***"));
2724 static struct RegExReplTestData
2728 const wxChar
*result
;
2730 } regExReplTestData
[] =
2732 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2733 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2734 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2735 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2736 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2737 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2738 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2741 const wxChar
*pattern
= _T("([a-z]+)[^0-9]*([0-9]+)");
2742 wxRegEx
re(pattern
);
2744 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern
);
2746 for ( size_t n
= 0; n
< WXSIZEOF(regExReplTestData
); n
++ )
2748 const RegExReplTestData
& data
= regExReplTestData
[n
];
2750 wxString text
= data
.text
;
2751 size_t nRepl
= re
.Replace(&text
, data
.repl
);
2753 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2754 data
.text
, data
.repl
,
2755 nRepl
, nRepl
== 1 ? _T("") : _T("es"),
2757 if ( text
== data
.result
&& nRepl
== data
.count
)
2763 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2764 data
.count
, data
.result
);
2769 static void TestRegExInteractive()
2771 wxPuts(_T("*** Testing RE interactively ***"));
2775 wxChar pattern
[128];
2776 wxPrintf(_T("\nEnter a pattern: "));
2777 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
2780 // kill the last '\n'
2781 pattern
[wxStrlen(pattern
) - 1] = 0;
2784 if ( !re
.Compile(pattern
) )
2792 wxPrintf(_T("Enter text to match: "));
2793 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
2796 // kill the last '\n'
2797 text
[wxStrlen(text
) - 1] = 0;
2799 if ( !re
.Matches(text
) )
2801 wxPrintf(_T("No match.\n"));
2805 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
2808 for ( size_t n
= 1; ; n
++ )
2810 if ( !re
.GetMatch(&start
, &len
, n
) )
2815 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2816 n
, wxString(text
+ start
, len
).c_str());
2823 #endif // TEST_REGEX
2825 // ----------------------------------------------------------------------------
2827 // ----------------------------------------------------------------------------
2837 static void TestDbOpen()
2845 // ----------------------------------------------------------------------------
2847 // ----------------------------------------------------------------------------
2850 NB: this stuff was taken from the glibc test suite and modified to build
2851 in wxWindows: if I read the copyright below properly, this shouldn't
2857 #ifdef wxTEST_PRINTF
2858 // use our functions from wxchar.cpp
2862 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2863 // in the tests below
2864 int wxPrintf( const wxChar
*format
, ... );
2865 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
2868 #include "wx/longlong.h"
2872 static void rfg1 (void);
2873 static void rfg2 (void);
2877 fmtchk (const wxChar
*fmt
)
2879 (void) wxPrintf(_T("%s:\t`"), fmt
);
2880 (void) wxPrintf(fmt
, 0x12);
2881 (void) wxPrintf(_T("'\n"));
2885 fmtst1chk (const wxChar
*fmt
)
2887 (void) wxPrintf(_T("%s:\t`"), fmt
);
2888 (void) wxPrintf(fmt
, 4, 0x12);
2889 (void) wxPrintf(_T("'\n"));
2893 fmtst2chk (const wxChar
*fmt
)
2895 (void) wxPrintf(_T("%s:\t`"), fmt
);
2896 (void) wxPrintf(fmt
, 4, 4, 0x12);
2897 (void) wxPrintf(_T("'\n"));
2900 /* This page is covered by the following copyright: */
2902 /* (C) Copyright C E Chew
2904 * Feel free to copy, use and distribute this software provided:
2906 * 1. you do not pretend that you wrote it
2907 * 2. you leave this copyright notice intact.
2911 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2918 /* Formatted Output Test
2920 * This exercises the output formatting code.
2928 wxChar
*prefix
= buf
;
2931 wxPuts(_T("\nFormatted output test"));
2932 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2933 wxStrcpy(prefix
, _T("%"));
2934 for (i
= 0; i
< 2; i
++) {
2935 for (j
= 0; j
< 2; j
++) {
2936 for (k
= 0; k
< 2; k
++) {
2937 for (l
= 0; l
< 2; l
++) {
2938 wxStrcpy(prefix
, _T("%"));
2939 if (i
== 0) wxStrcat(prefix
, _T("-"));
2940 if (j
== 0) wxStrcat(prefix
, _T("+"));
2941 if (k
== 0) wxStrcat(prefix
, _T("#"));
2942 if (l
== 0) wxStrcat(prefix
, _T("0"));
2943 wxPrintf(_T("%5s |"), prefix
);
2944 wxStrcpy(tp
, prefix
);
2945 wxStrcat(tp
, _T("6d |"));
2947 wxStrcpy(tp
, prefix
);
2948 wxStrcat(tp
, _T("6o |"));
2950 wxStrcpy(tp
, prefix
);
2951 wxStrcat(tp
, _T("6x |"));
2953 wxStrcpy(tp
, prefix
);
2954 wxStrcat(tp
, _T("6X |"));
2956 wxStrcpy(tp
, prefix
);
2957 wxStrcat(tp
, _T("6u |"));
2964 wxPrintf(_T("%10s\n"), (wxChar
*) NULL
);
2965 wxPrintf(_T("%-10s\n"), (wxChar
*) NULL
);
2968 static void TestPrintf()
2970 static wxChar shortstr
[] = _T("Hi, Z.");
2971 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
2972 I am ready for my first lesson today.");
2977 fmtchk(_T("%4.4x"));
2978 fmtchk(_T("%04.4x"));
2979 fmtchk(_T("%4.3x"));
2980 fmtchk(_T("%04.3x"));
2982 fmtst1chk(_T("%.*x"));
2983 fmtst1chk(_T("%0*x"));
2984 fmtst2chk(_T("%*.*x"));
2985 fmtst2chk(_T("%0*.*x"));
2987 wxPrintf(_T("bad format:\t\"%b\"\n"));
2988 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
2990 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
2991 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
2992 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
2993 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
2994 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
2995 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
2996 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
2997 wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
2998 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
2999 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
3001 wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr
);
3002 wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr
);
3003 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
3004 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
3005 wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar
*)NULL
);
3006 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
3008 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
3009 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
3010 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
3011 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
3012 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
3013 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
3014 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
3015 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
3016 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
3017 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
3018 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
3019 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
3021 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
3022 wxPrintf (_T(" %6.5f\n"), .1);
3023 wxPrintf (_T("x%5.4fx\n"), .5);
3025 wxPrintf (_T("%#03x\n"), 1);
3027 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
3033 while (niter
-- != 0)
3034 wxPrintf (_T("%.17e\n"), d
/ 2);
3038 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
3040 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
3041 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
3042 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
3043 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
3044 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
3045 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
3046 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
3047 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
3048 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
3049 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
3054 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
3056 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
3057 rc
, WXSIZEOF(buf
), buf
);
3060 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
3061 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
3067 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
3068 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
3069 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
3070 wxPrintf (_T("%g should be 123.456\n"), 123.456);
3071 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
3072 wxPrintf (_T("%g should be 10\n"), 10.0);
3073 wxPrintf (_T("%g should be 0.02\n"), 0.02);
3077 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
3083 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
3085 result
|= wxStrcmp (buf
,
3086 _T("onetwo three "));
3088 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
3095 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
3097 // for some reason below line fails under Borland
3098 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
3101 if (wxStrcmp (buf
, _T("40000000000")) != 0)
3104 wxPuts (_T("\tFAILED"));
3108 #endif // wxLongLong_t
3110 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
3111 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
3113 wxPuts (_T("--- Should be no further output. ---"));
3122 memset (bytes
, '\xff', sizeof bytes
);
3123 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
3124 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
3125 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
3127 wxPuts (_T("%hhn overwrite more bytes"));
3132 wxPuts (_T("%hhn wrote incorrect value"));
3144 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
3145 if (wxStrcmp (buf
, _T(" ")) != 0)
3146 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
3147 wxSprintf (buf
, _T("%5.f"), 33.3);
3148 if (wxStrcmp (buf
, _T(" 33")) != 0)
3149 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
3150 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
3151 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
3152 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
3153 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
3154 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
3155 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
3156 wxSprintf (buf
, _T("%.g"), 33.3);
3157 if (wxStrcmp (buf
, _T("3e+01")) != 0)
3158 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
3159 wxSprintf (buf
, _T("%.G"), 33.3);
3160 if (wxStrcmp (buf
, _T("3E+01")) != 0)
3161 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
3171 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
3172 if (wxStrcmp (buf
, _T("3")) != 0)
3173 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3175 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
3176 if (wxStrcmp (buf
, _T("3")) != 0)
3177 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3179 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
3180 if (wxStrcmp (buf
, _T(" 3")) != 0)
3181 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
3183 wxSprintf (buf
, _T("%04.*o"), prec
, 33);
3184 if (wxStrcmp (buf
, _T(" 041")) != 0)
3185 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
3187 wxSprintf (buf
, _T("%09.*u"), prec
, 33);
3188 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
3189 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
3191 wxSprintf (buf
, _T("%04.*x"), prec
, 33);
3192 if (wxStrcmp (buf
, _T(" 021")) != 0)
3193 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3195 wxSprintf (buf
, _T("%04.*X"), prec
, 33);
3196 if (wxStrcmp (buf
, _T(" 021")) != 0)
3197 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3200 #endif // TEST_PRINTF
3202 // ----------------------------------------------------------------------------
3203 // registry and related stuff
3204 // ----------------------------------------------------------------------------
3206 // this is for MSW only
3209 #undef TEST_REGISTRY
3214 #include "wx/confbase.h"
3215 #include "wx/msw/regconf.h"
3217 static void TestRegConfWrite()
3219 wxRegConfig
regconf(_T("console"), _T("wxwindows"));
3220 regconf
.Write(_T("Hello"), wxString(_T("world")));
3223 #endif // TEST_REGCONF
3225 #ifdef TEST_REGISTRY
3227 #include "wx/msw/registry.h"
3229 // I chose this one because I liked its name, but it probably only exists under
3231 static const wxChar
*TESTKEY
=
3232 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
3234 static void TestRegistryRead()
3236 wxPuts(_T("*** testing registry reading ***"));
3238 wxRegKey
key(TESTKEY
);
3239 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
3242 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
3247 size_t nSubKeys
, nValues
;
3248 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
3250 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
3253 wxPrintf(_T("Enumerating values:\n"));
3257 bool cont
= key
.GetFirstValue(value
, dummy
);
3260 wxPrintf(_T("Value '%s': type "), value
.c_str());
3261 switch ( key
.GetValueType(value
) )
3263 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
3264 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
3265 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
3266 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
3267 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
3268 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
3269 default: wxPrintf(_T("other (unknown)")); break;
3272 wxPrintf(_T(", value = "));
3273 if ( key
.IsNumericValue(value
) )
3276 key
.QueryValue(value
, &val
);
3277 wxPrintf(_T("%ld"), val
);
3282 key
.QueryValue(value
, val
);
3283 wxPrintf(_T("'%s'"), val
.c_str());
3285 key
.QueryRawValue(value
, val
);
3286 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
3291 cont
= key
.GetNextValue(value
, dummy
);
3295 static void TestRegistryAssociation()
3298 The second call to deleteself genertaes an error message, with a
3299 messagebox saying .flo is crucial to system operation, while the .ddf
3300 call also fails, but with no error message
3305 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3307 key
= _T("ddxf_auto_file") ;
3308 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3310 key
= _T("ddxf_auto_file") ;
3311 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3313 key
= _T("program,0") ;
3314 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3316 key
= _T("program \"%1\"") ;
3318 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3320 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3322 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3324 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3328 #endif // TEST_REGISTRY
3330 // ----------------------------------------------------------------------------
3332 // ----------------------------------------------------------------------------
3334 #ifdef TEST_SCOPEGUARD
3336 #include "wx/scopeguard.h"
3338 static void function0() { puts("function0()"); }
3339 static void function1(int n
) { printf("function1(%d)\n", n
); }
3340 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
3344 void method0() { printf("method0()\n"); }
3345 void method1(int n
) { printf("method1(%d)\n", n
); }
3346 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
3349 static void TestScopeGuard()
3351 ON_BLOCK_EXIT0(function0
);
3352 ON_BLOCK_EXIT1(function1
, 17);
3353 ON_BLOCK_EXIT2(function2
, 3.14, 'p');
3356 ON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
3357 ON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
3358 ON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
3360 wxScopeGuard dismissed
= wxMakeGuard(function0
);
3361 dismissed
.Dismiss();
3366 // ----------------------------------------------------------------------------
3368 // ----------------------------------------------------------------------------
3372 #include "wx/socket.h"
3373 #include "wx/protocol/protocol.h"
3374 #include "wx/protocol/http.h"
3376 static void TestSocketServer()
3378 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3380 static const int PORT
= 3000;
3385 wxSocketServer
*server
= new wxSocketServer(addr
);
3386 if ( !server
->Ok() )
3388 wxPuts(_T("ERROR: failed to bind"));
3396 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
3398 wxSocketBase
*socket
= server
->Accept();
3401 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3405 wxPuts(_T("Server: got a client."));
3407 server
->SetTimeout(60); // 1 min
3410 while ( !close
&& socket
->IsConnected() )
3413 wxChar ch
= _T('\0');
3416 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
3418 // don't log error if the client just close the connection
3419 if ( socket
->IsConnected() )
3421 wxPuts(_T("ERROR: in wxSocket::Read."));
3441 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
3442 if ( s
== _T("close") )
3444 wxPuts(_T("Closing connection"));
3448 else if ( s
== _T("quit") )
3453 wxPuts(_T("Shutting down the server"));
3455 else // not a special command
3457 socket
->Write(s
.MakeUpper().c_str(), s
.length());
3458 socket
->Write("\r\n", 2);
3459 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
3465 wxPuts(_T("Server: lost a client unexpectedly."));
3471 // same as "delete server" but is consistent with GUI programs
3475 static void TestSocketClient()
3477 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3479 static const wxChar
*hostname
= _T("www.wxwindows.org");
3482 addr
.Hostname(hostname
);
3485 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
3487 wxSocketClient client
;
3488 if ( !client
.Connect(addr
) )
3490 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3494 wxPrintf(_T("--- Connected to %s:%u...\n"),
3495 addr
.Hostname().c_str(), addr
.Service());
3499 // could use simply "GET" here I suppose
3501 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
3502 client
.Write(cmdGet
, cmdGet
.length());
3503 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3504 MakePrintable(cmdGet
).c_str());
3505 client
.Read(buf
, WXSIZEOF(buf
));
3506 wxPrintf(_T("--- Server replied:\n%s"), buf
);
3510 #endif // TEST_SOCKETS
3512 // ----------------------------------------------------------------------------
3514 // ----------------------------------------------------------------------------
3518 #include "wx/protocol/ftp.h"
3522 #define FTP_ANONYMOUS
3524 #ifdef FTP_ANONYMOUS
3525 static const wxChar
*directory
= _T("/pub");
3526 static const wxChar
*filename
= _T("welcome.msg");
3528 static const wxChar
*directory
= _T("/etc");
3529 static const wxChar
*filename
= _T("issue");
3532 static bool TestFtpConnect()
3534 wxPuts(_T("*** Testing FTP connect ***"));
3536 #ifdef FTP_ANONYMOUS
3537 static const wxChar
*hostname
= _T("ftp.wxwindows.org");
3539 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
3540 #else // !FTP_ANONYMOUS
3541 static const wxChar
*hostname
= "localhost";
3544 wxFgets(user
, WXSIZEOF(user
), stdin
);
3545 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
3548 wxChar password
[256];
3549 wxPrintf(_T("Password for %s: "), password
);
3550 wxFgets(password
, WXSIZEOF(password
), stdin
);
3551 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
3552 ftp
.SetPassword(password
);
3554 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
3555 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3557 if ( !ftp
.Connect(hostname
) )
3559 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3565 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3566 hostname
, ftp
.Pwd().c_str());
3572 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3573 static void TestFtpWuFtpd()
3576 static const wxChar
*hostname
= _T("ftp.eudora.com");
3577 if ( !ftp
.Connect(hostname
) )
3579 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3583 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
3584 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3587 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3591 size_t size
= in
->GetSize();
3592 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3594 wxChar
*data
= new wxChar
[size
];
3595 if ( !in
->Read(data
, size
) )
3597 wxPuts(_T("ERROR: read error"));
3601 wxPrintf(_T("Successfully retrieved the file.\n"));
3610 static void TestFtpList()
3612 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3615 if ( !ftp
.ChDir(directory
) )
3617 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3620 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3622 // test NLIST and LIST
3623 wxArrayString files
;
3624 if ( !ftp
.GetFilesList(files
) )
3626 wxPuts(_T("ERROR: failed to get NLIST of files"));
3630 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
3631 size_t count
= files
.GetCount();
3632 for ( size_t n
= 0; n
< count
; n
++ )
3634 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3636 wxPuts(_T("End of the file list"));
3639 if ( !ftp
.GetDirList(files
) )
3641 wxPuts(_T("ERROR: failed to get LIST of files"));
3645 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
3646 size_t count
= files
.GetCount();
3647 for ( size_t n
= 0; n
< count
; n
++ )
3649 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3651 wxPuts(_T("End of the file list"));
3654 if ( !ftp
.ChDir(_T("..")) )
3656 wxPuts(_T("ERROR: failed to cd to .."));
3659 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3662 static void TestFtpDownload()
3664 wxPuts(_T("*** Testing wxFTP download ***\n"));
3667 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3670 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3674 size_t size
= in
->GetSize();
3675 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3678 wxChar
*data
= new wxChar
[size
];
3679 if ( !in
->Read(data
, size
) )
3681 wxPuts(_T("ERROR: read error"));
3685 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
3693 static void TestFtpFileSize()
3695 wxPuts(_T("*** Testing FTP SIZE command ***"));
3697 if ( !ftp
.ChDir(directory
) )
3699 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3702 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3704 if ( ftp
.FileExists(filename
) )
3706 int size
= ftp
.GetFileSize(filename
);
3708 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
3710 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
3714 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
3718 static void TestFtpMisc()
3720 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3722 if ( ftp
.SendCommand(_T("STAT")) != '2' )
3724 wxPuts(_T("ERROR: STAT failed"));
3728 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
3731 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
3733 wxPuts(_T("ERROR: HELP SITE failed"));
3737 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3738 ftp
.GetLastResult().c_str());
3742 static void TestFtpInteractive()
3744 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3750 wxPrintf(_T("Enter FTP command: "));
3751 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3754 // kill the last '\n'
3755 buf
[wxStrlen(buf
) - 1] = 0;
3757 // special handling of LIST and NLST as they require data connection
3758 wxString
start(buf
, 4);
3760 if ( start
== _T("LIST") || start
== _T("NLST") )
3763 if ( wxStrlen(buf
) > 4 )
3766 wxArrayString files
;
3767 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
3769 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
3773 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3774 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
3775 size_t count
= files
.GetCount();
3776 for ( size_t n
= 0; n
< count
; n
++ )
3778 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3780 wxPuts(_T("--- End of the file list"));
3785 wxChar ch
= ftp
.SendCommand(buf
);
3786 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
3789 wxPrintf(_T(" (return code %c)"), ch
);
3792 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
3796 wxPuts(_T("\n*** done ***"));
3799 static void TestFtpUpload()
3801 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3804 static const wxChar
*file1
= _T("test1");
3805 static const wxChar
*file2
= _T("test2");
3806 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
3809 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3810 out
->Write("First hello", 11);
3814 // send a command to check the remote file
3815 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
3817 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
3821 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3822 file1
, ftp
.GetLastResult().c_str());
3825 out
= ftp
.GetOutputStream(file2
);
3828 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3829 out
->Write("Second hello", 12);
3836 // ----------------------------------------------------------------------------
3838 // ----------------------------------------------------------------------------
3842 #include "wx/wfstream.h"
3843 #include "wx/mstream.h"
3845 static void TestFileStream()
3847 wxPuts(_T("*** Testing wxFileInputStream ***"));
3849 static const wxChar
*filename
= _T("testdata.fs");
3851 wxFileOutputStream
fsOut(filename
);
3852 fsOut
.Write("foo", 3);
3855 wxFileInputStream
fsIn(filename
);
3856 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
3857 while ( !fsIn
.Eof() )
3859 putchar(fsIn
.GetC());
3862 if ( !wxRemoveFile(filename
) )
3864 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
);
3867 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3870 static void TestMemoryStream()
3872 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3874 wxMemoryOutputStream memOutStream
;
3875 wxPrintf(_T("Initially out stream offset: %lu\n"),
3876 (unsigned long)memOutStream
.TellO());
3878 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
3880 memOutStream
.PutC(*p
);
3883 wxPrintf(_T("Final out stream offset: %lu\n"),
3884 (unsigned long)memOutStream
.TellO());
3886 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3889 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
3891 wxMemoryInputStream
memInpStream(buf
, len
);
3892 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
3893 while ( !memInpStream
.Eof() )
3895 putchar(memInpStream
.GetC());
3898 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3901 #endif // TEST_STREAMS
3903 // ----------------------------------------------------------------------------
3905 // ----------------------------------------------------------------------------
3909 #include "wx/timer.h"
3910 #include "wx/utils.h"
3912 static void TestStopWatch()
3914 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3918 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3921 wxPrintf(_T("\t%ldms\n"), sw
.Time());
3923 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3927 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3930 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3933 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3936 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3939 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3942 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
3943 for ( size_t n
= 0; n
< 70; n
++ )
3947 for ( size_t m
= 0; m
< 100000; m
++ )
3949 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
3951 wxPuts(_T("\ntime is negative - ERROR!"));
3959 wxPuts(_T(", ok."));
3962 #endif // TEST_TIMER
3964 // ----------------------------------------------------------------------------
3966 // ----------------------------------------------------------------------------
3970 #include "wx/vcard.h"
3972 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
3975 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
3978 wxPrintf(_T("%s%s"),
3979 wxString(_T('\t'), level
).c_str(),
3980 vcObj
->GetName().c_str());
3983 switch ( vcObj
->GetType() )
3985 case wxVCardObject::String
:
3986 case wxVCardObject::UString
:
3989 vcObj
->GetValue(&val
);
3990 value
<< _T('"') << val
<< _T('"');
3994 case wxVCardObject::Int
:
3997 vcObj
->GetValue(&i
);
3998 value
.Printf(_T("%u"), i
);
4002 case wxVCardObject::Long
:
4005 vcObj
->GetValue(&l
);
4006 value
.Printf(_T("%lu"), l
);
4010 case wxVCardObject::None
:
4013 case wxVCardObject::Object
:
4014 value
= _T("<node>");
4018 value
= _T("<unknown value type>");
4022 wxPrintf(_T(" = %s"), value
.c_str());
4025 DumpVObject(level
+ 1, *vcObj
);
4028 vcObj
= vcard
.GetNextProp(&cookie
);
4032 static void DumpVCardAddresses(const wxVCard
& vcard
)
4034 wxPuts(_T("\nShowing all addresses from vCard:\n"));
4038 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
4042 int flags
= addr
->GetFlags();
4043 if ( flags
& wxVCardAddress::Domestic
)
4045 flagsStr
<< _T("domestic ");
4047 if ( flags
& wxVCardAddress::Intl
)
4049 flagsStr
<< _T("international ");
4051 if ( flags
& wxVCardAddress::Postal
)
4053 flagsStr
<< _T("postal ");
4055 if ( flags
& wxVCardAddress::Parcel
)
4057 flagsStr
<< _T("parcel ");
4059 if ( flags
& wxVCardAddress::Home
)
4061 flagsStr
<< _T("home ");
4063 if ( flags
& wxVCardAddress::Work
)
4065 flagsStr
<< _T("work ");
4068 wxPrintf(_T("Address %u:\n")
4070 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
4073 addr
->GetPostOffice().c_str(),
4074 addr
->GetExtAddress().c_str(),
4075 addr
->GetStreet().c_str(),
4076 addr
->GetLocality().c_str(),
4077 addr
->GetRegion().c_str(),
4078 addr
->GetPostalCode().c_str(),
4079 addr
->GetCountry().c_str()
4083 addr
= vcard
.GetNextAddress(&cookie
);
4087 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
4089 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
4093 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
4097 int flags
= phone
->GetFlags();
4098 if ( flags
& wxVCardPhoneNumber::Voice
)
4100 flagsStr
<< _T("voice ");
4102 if ( flags
& wxVCardPhoneNumber::Fax
)
4104 flagsStr
<< _T("fax ");
4106 if ( flags
& wxVCardPhoneNumber::Cellular
)
4108 flagsStr
<< _T("cellular ");
4110 if ( flags
& wxVCardPhoneNumber::Modem
)
4112 flagsStr
<< _T("modem ");
4114 if ( flags
& wxVCardPhoneNumber::Home
)
4116 flagsStr
<< _T("home ");
4118 if ( flags
& wxVCardPhoneNumber::Work
)
4120 flagsStr
<< _T("work ");
4123 wxPrintf(_T("Phone number %u:\n")
4128 phone
->GetNumber().c_str()
4132 phone
= vcard
.GetNextPhoneNumber(&cookie
);
4136 static void TestVCardRead()
4138 wxPuts(_T("*** Testing wxVCard reading ***\n"));
4140 wxVCard
vcard(_T("vcard.vcf"));
4141 if ( !vcard
.IsOk() )
4143 wxPuts(_T("ERROR: couldn't load vCard."));
4147 // read individual vCard properties
4148 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
4152 vcObj
->GetValue(&value
);
4157 value
= _T("<none>");
4160 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
4163 if ( !vcard
.GetFullName(&value
) )
4165 value
= _T("<none>");
4168 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
4170 // now show how to deal with multiply occuring properties
4171 DumpVCardAddresses(vcard
);
4172 DumpVCardPhoneNumbers(vcard
);
4174 // and finally show all
4175 wxPuts(_T("\nNow dumping the entire vCard:\n")
4176 "-----------------------------\n");
4178 DumpVObject(0, vcard
);
4182 static void TestVCardWrite()
4184 wxPuts(_T("*** Testing wxVCard writing ***\n"));
4187 if ( !vcard
.IsOk() )
4189 wxPuts(_T("ERROR: couldn't create vCard."));
4194 vcard
.SetName("Zeitlin", "Vadim");
4195 vcard
.SetFullName("Vadim Zeitlin");
4196 vcard
.SetOrganization("wxWindows", "R&D");
4198 // just dump the vCard back
4199 wxPuts(_T("Entire vCard follows:\n"));
4200 wxPuts(vcard
.Write());
4204 #endif // TEST_VCARD
4206 // ----------------------------------------------------------------------------
4208 // ----------------------------------------------------------------------------
4210 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
4216 #include "wx/volume.h"
4218 static const wxChar
*volumeKinds
[] =
4224 _T("network volume"),
4228 static void TestFSVolume()
4230 wxPuts(_T("*** Testing wxFSVolume class ***"));
4232 wxArrayString volumes
= wxFSVolume::GetVolumes();
4233 size_t count
= volumes
.GetCount();
4237 wxPuts(_T("ERROR: no mounted volumes?"));
4241 wxPrintf(_T("%u mounted volumes found:\n"), count
);
4243 for ( size_t n
= 0; n
< count
; n
++ )
4245 wxFSVolume
vol(volumes
[n
]);
4248 wxPuts(_T("ERROR: couldn't create volume"));
4252 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
4254 vol
.GetDisplayName().c_str(),
4255 vol
.GetName().c_str(),
4256 volumeKinds
[vol
.GetKind()],
4257 vol
.IsWritable() ? _T("rw") : _T("ro"),
4258 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
4263 #endif // TEST_VOLUME
4265 // ----------------------------------------------------------------------------
4266 // wide char and Unicode support
4267 // ----------------------------------------------------------------------------
4271 static void TestUnicodeToFromAscii()
4273 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
4275 static const char *msg
= "Hello, world!";
4276 wxString s
= wxString::FromAscii(msg
);
4278 wxPrintf(_T("Message in Unicode: %s\n"), s
.c_str());
4279 printf("Message in ASCII: %s\n", (const char *)s
.ToAscii());
4281 wxPutchar(_T('\n'));
4284 #include "wx/textfile.h"
4286 static void TestUnicodeTextFileRead()
4288 wxPuts(_T("Testing wxTextFile in Unicode build\n"));
4291 if ( file
.Open(_T("testdata.fc"), wxConvLocal
) )
4293 const size_t count
= file
.GetLineCount();
4294 for ( size_t n
= 0; n
< count
; n
++ )
4296 const wxString
& s
= file
[n
];
4298 wxPrintf(_T("Line %u: \"%s\" (len %u, last char = '%c')\n"),
4299 (unsigned)n
, s
.c_str(), (unsigned)s
.length(), s
.Last());
4304 #endif // TEST_UNICODE
4308 #include "wx/strconv.h"
4309 #include "wx/fontenc.h"
4310 #include "wx/encconv.h"
4311 #include "wx/buffer.h"
4313 static const unsigned char utf8koi8r
[] =
4315 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4316 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4317 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4318 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4319 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4320 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4321 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4324 static const unsigned char utf8iso8859_1
[] =
4326 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4327 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4328 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4329 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4330 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4333 static const unsigned char utf8Invalid
[] =
4335 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4336 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4337 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4338 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4342 static const struct Utf8Data
4344 const unsigned char *text
;
4346 const wxChar
*charset
;
4347 wxFontEncoding encoding
;
4350 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4351 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
4352 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4355 static void TestUtf8()
4357 wxPuts(_T("*** Testing UTF8 support ***\n"));
4362 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
4364 const Utf8Data
& u8d
= utf8data
[n
];
4365 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
4366 WXSIZEOF(wbuf
)) == (size_t)-1 )
4368 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4372 wxCSConv
conv(u8d
.charset
);
4373 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
4375 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
4379 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
4383 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
));
4385 s
= _T("<< conversion failed >>");
4386 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
4393 static void TestEncodingConverter()
4395 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4397 // using wxEncodingConverter should give the same result as above
4400 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
4401 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
4403 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4407 wxEncodingConverter ec
;
4408 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
4409 ec
.Convert(wbuf
, buf
);
4410 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
4416 #endif // TEST_WCHAR
4418 // ----------------------------------------------------------------------------
4420 // ----------------------------------------------------------------------------
4424 #include "wx/filesys.h"
4425 #include "wx/fs_zip.h"
4426 #include "wx/zipstrm.h"
4428 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
4430 static void TestZipStreamRead()
4432 wxPuts(_T("*** Testing ZIP reading ***\n"));
4434 static const wxChar
*filename
= _T("foo");
4435 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
4436 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4438 wxPrintf(_T("Dumping the file '%s':\n"), filename
);
4439 while ( !istr
.Eof() )
4441 putchar(istr
.GetC());
4445 wxPuts(_T("\n----- done ------"));
4448 static void DumpZipDirectory(wxFileSystem
& fs
,
4449 const wxString
& dir
,
4450 const wxString
& indent
)
4452 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
4453 TESTFILE_ZIP
, dir
.c_str());
4454 wxString wildcard
= prefix
+ _T("/*");
4456 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
4457 while ( !dirname
.empty() )
4459 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
4461 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4466 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
4468 DumpZipDirectory(fs
, dirname
,
4469 indent
+ wxString(_T(' '), 4));
4471 dirname
= fs
.FindNext();
4474 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
4475 while ( !filename
.empty() )
4477 if ( !filename
.StartsWith(prefix
, &filename
) )
4479 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4484 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
4486 filename
= fs
.FindNext();
4490 static void TestZipFileSystem()
4492 wxPuts(_T("*** Testing ZIP file system ***\n"));
4494 wxFileSystem::AddHandler(new wxZipFSHandler
);
4496 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
4498 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
4503 // ----------------------------------------------------------------------------
4505 // ----------------------------------------------------------------------------
4509 #include "wx/zstream.h"
4510 #include "wx/wfstream.h"
4512 static const wxChar
*FILENAME_GZ
= _T("test.gz");
4513 static const wxChar
*TEST_DATA
= _T("hello and hello and hello and hello and hello");
4515 static void TestZlibStreamWrite()
4517 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4519 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
4520 wxZlibOutputStream
ostr(fileOutStream
);
4521 wxPrintf(_T("Compressing the test string... "));
4522 ostr
.Write(TEST_DATA
, wxStrlen(TEST_DATA
) + 1);
4525 wxPuts(_T("(ERROR: failed)"));
4532 wxPuts(_T("\n----- done ------"));
4535 static void TestZlibStreamRead()
4537 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4539 wxFileInputStream
fileInStream(FILENAME_GZ
);
4540 wxZlibInputStream
istr(fileInStream
);
4541 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4543 wxPuts(_T("Dumping the file:"));
4544 while ( !istr
.Eof() )
4546 putchar(istr
.GetC());
4550 wxPuts(_T("\n----- done ------"));
4555 // ----------------------------------------------------------------------------
4557 // ----------------------------------------------------------------------------
4559 #ifdef TEST_DATETIME
4563 #include "wx/datetime.h"
4568 wxDateTime::wxDateTime_t day
;
4569 wxDateTime::Month month
;
4571 wxDateTime::wxDateTime_t hour
, min
, sec
;
4573 wxDateTime::WeekDay wday
;
4574 time_t gmticks
, ticks
;
4576 void Init(const wxDateTime::Tm
& tm
)
4585 gmticks
= ticks
= -1;
4588 wxDateTime
DT() const
4589 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
4591 bool SameDay(const wxDateTime::Tm
& tm
) const
4593 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
4596 wxString
Format() const
4599 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4601 wxDateTime::GetMonthName(month
).c_str(),
4603 abs(wxDateTime::ConvertYearToBC(year
)),
4604 year
> 0 ? _T("AD") : _T("BC"));
4608 wxString
FormatDate() const
4611 s
.Printf(_T("%02d-%s-%4d%s"),
4613 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4614 abs(wxDateTime::ConvertYearToBC(year
)),
4615 year
> 0 ? _T("AD") : _T("BC"));
4620 static const Date testDates
[] =
4622 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
4623 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
4624 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
4625 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
4626 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
4627 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
4628 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
4629 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
4630 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
4631 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
4632 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
4633 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
4634 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
4635 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
4636 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
4637 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
4638 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
4639 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
4640 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
4643 // this test miscellaneous static wxDateTime functions
4644 static void TestTimeStatic()
4646 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4648 // some info about the current date
4649 int year
= wxDateTime::GetCurrentYear();
4650 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4652 wxDateTime::IsLeapYear(year
) ? "" : "not ",
4653 wxDateTime::GetNumberOfDays(year
));
4655 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
4656 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4657 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4658 wxDateTime::GetMonthName(month
).c_str(),
4659 wxDateTime::GetNumberOfDays(month
));
4662 static const size_t nYears
= 5;
4663 static const size_t years
[2][nYears
] =
4665 // first line: the years to test
4666 { 1990, 1976, 2000, 2030, 1984, },
4668 // second line: true if leap, false otherwise
4669 { false, true, true, false, true }
4672 for ( size_t n
= 0; n
< nYears
; n
++ )
4674 int year
= years
[0][n
];
4675 bool should
= years
[1][n
] != 0,
4676 is
= wxDateTime::IsLeapYear(year
);
4678 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4681 should
== is
? "ok" : "ERROR");
4683 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
4687 // test constructing wxDateTime objects
4688 static void TestTimeSet()
4690 wxPuts(_T("\n*** wxDateTime construction test ***"));
4692 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4694 const Date
& d1
= testDates
[n
];
4695 wxDateTime dt
= d1
.DT();
4698 d2
.Init(dt
.GetTm());
4700 wxString s1
= d1
.Format(),
4703 wxPrintf(_T("Date: %s == %s (%s)\n"),
4704 s1
.c_str(), s2
.c_str(),
4705 s1
== s2
? _T("ok") : _T("ERROR"));
4709 // test time zones stuff
4710 static void TestTimeZones()
4712 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4714 wxDateTime now
= wxDateTime::Now();
4716 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
4717 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
4718 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
4719 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
4720 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
4721 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
4723 wxDateTime::Tm tm
= now
.GetTm();
4724 if ( wxDateTime(tm
) != now
)
4726 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4727 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
4731 // test some minimal support for the dates outside the standard range
4732 static void TestTimeRange()
4734 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4736 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
4738 wxPrintf(_T("Unix epoch:\t%s\n"),
4739 wxDateTime(2440587.5).Format(fmt
).c_str());
4740 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4741 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
4742 wxPrintf(_T("JDN 0: \t%s\n"),
4743 wxDateTime(0.0).Format(fmt
).c_str());
4744 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4745 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
4746 wxPrintf(_T("May 29, 2099:\t%s\n"),
4747 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
4750 static void TestTimeTicks()
4752 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4754 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4756 const Date
& d
= testDates
[n
];
4757 if ( d
.ticks
== -1 )
4760 wxDateTime dt
= d
.DT();
4761 long ticks
= (dt
.GetValue() / 1000).ToLong();
4762 wxPrintf(_T("Ticks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4763 if ( ticks
== d
.ticks
)
4765 wxPuts(_T(" (ok)"));
4769 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4770 (long)d
.ticks
, (long)(ticks
- d
.ticks
));
4773 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
4774 ticks
= (dt
.GetValue() / 1000).ToLong();
4775 wxPrintf(_T("GMtks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4776 if ( ticks
== d
.gmticks
)
4778 wxPuts(_T(" (ok)"));
4782 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4783 (long)d
.gmticks
, (long)(ticks
- d
.gmticks
));
4790 // test conversions to JDN &c
4791 static void TestTimeJDN()
4793 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4795 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4797 const Date
& d
= testDates
[n
];
4798 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4799 double jdn
= dt
.GetJulianDayNumber();
4801 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d
.Format().c_str(), jdn
);
4804 wxPuts(_T(" (ok)"));
4808 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4809 d
.jdn
, jdn
- d
.jdn
);
4814 // test week days computation
4815 static void TestTimeWDays()
4817 wxPuts(_T("\n*** wxDateTime weekday test ***"));
4819 // test GetWeekDay()
4821 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4823 const Date
& d
= testDates
[n
];
4824 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4826 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
4827 wxPrintf(_T("%s is: %s"),
4829 wxDateTime::GetWeekDayName(wday
).c_str());
4830 if ( wday
== d
.wday
)
4832 wxPuts(_T(" (ok)"));
4836 wxPrintf(_T(" (ERROR: should be %s)\n"),
4837 wxDateTime::GetWeekDayName(d
.wday
).c_str());
4843 // test SetToWeekDay()
4844 struct WeekDateTestData
4846 Date date
; // the real date (precomputed)
4847 int nWeek
; // its week index in the month
4848 wxDateTime::WeekDay wday
; // the weekday
4849 wxDateTime::Month month
; // the month
4850 int year
; // and the year
4852 wxString
Format() const
4855 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
4857 case 1: which
= _T("first"); break;
4858 case 2: which
= _T("second"); break;
4859 case 3: which
= _T("third"); break;
4860 case 4: which
= _T("fourth"); break;
4861 case 5: which
= _T("fifth"); break;
4863 case -1: which
= _T("last"); break;
4868 which
+= _T(" from end");
4871 s
.Printf(_T("The %s %s of %s in %d"),
4873 wxDateTime::GetWeekDayName(wday
).c_str(),
4874 wxDateTime::GetMonthName(month
).c_str(),
4881 // the array data was generated by the following python program
4883 from DateTime import *
4884 from whrandom import *
4885 from string import *
4887 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4888 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4890 week = DateTimeDelta(7)
4893 year = randint(1900, 2100)
4894 month = randint(1, 12)
4895 day = randint(1, 28)
4896 dt = DateTime(year, month, day)
4897 wday = dt.day_of_week
4899 countFromEnd = choice([-1, 1])
4902 while dt.month is month:
4903 dt = dt - countFromEnd * week
4904 weekNum = weekNum + countFromEnd
4906 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
4908 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
4909 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
4912 static const WeekDateTestData weekDatesTestData
[] =
4914 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
4915 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
4916 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
4917 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
4918 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
4919 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
4920 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
4921 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
4922 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
4923 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
4924 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
4925 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
4926 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
4927 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
4928 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
4929 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
4930 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
4931 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
4932 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
4933 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
4936 static const wxChar
*fmt
= _T("%d-%b-%Y");
4939 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
4941 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
4943 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
4945 wxPrintf(_T("%s is %s"), wd
.Format().c_str(), dt
.Format(fmt
).c_str());
4947 const Date
& d
= wd
.date
;
4948 if ( d
.SameDay(dt
.GetTm()) )
4950 wxPuts(_T(" (ok)"));
4954 dt
.Set(d
.day
, d
.month
, d
.year
);
4956 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.Format(fmt
).c_str());
4961 // test the computation of (ISO) week numbers
4962 static void TestTimeWNumber()
4964 wxPuts(_T("\n*** wxDateTime week number test ***"));
4966 struct WeekNumberTestData
4968 Date date
; // the date
4969 wxDateTime::wxDateTime_t week
; // the week number in the year
4970 wxDateTime::wxDateTime_t wmon
; // the week number in the month
4971 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
4972 wxDateTime::wxDateTime_t dnum
; // day number in the year
4975 // data generated with the following python script:
4977 from DateTime import *
4978 from whrandom import *
4979 from string import *
4981 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4982 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4984 def GetMonthWeek(dt):
4985 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
4986 if weekNumMonth < 0:
4987 weekNumMonth = weekNumMonth + 53
4990 def GetLastSundayBefore(dt):
4991 if dt.iso_week[2] == 7:
4994 return dt - DateTimeDelta(dt.iso_week[2])
4997 year = randint(1900, 2100)
4998 month = randint(1, 12)
4999 day = randint(1, 28)
5000 dt = DateTime(year, month, day)
5001 dayNum = dt.day_of_year
5002 weekNum = dt.iso_week[1]
5003 weekNumMonth = GetMonthWeek(dt)
5006 dtSunday = GetLastSundayBefore(dt)
5008 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
5009 weekNumMonth2 = weekNumMonth2 + 1
5010 dtSunday = dtSunday - DateTimeDelta(7)
5012 data = { 'day': rjust(`day`, 2), \
5013 'month': monthNames[month - 1], \
5015 'weekNum': rjust(`weekNum`, 2), \
5016 'weekNumMonth': weekNumMonth, \
5017 'weekNumMonth2': weekNumMonth2, \
5018 'dayNum': rjust(`dayNum`, 3) }
5020 print " { { %(day)s, "\
5021 "wxDateTime::%(month)s, "\
5024 "%(weekNumMonth)s, "\
5025 "%(weekNumMonth2)s, "\
5026 "%(dayNum)s }," % data
5029 static const WeekNumberTestData weekNumberTestDates
[] =
5031 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
5032 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
5033 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
5034 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
5035 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
5036 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
5037 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
5038 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
5039 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
5040 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
5041 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
5042 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
5043 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
5044 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
5045 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
5046 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
5047 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
5048 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
5049 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
5050 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
5053 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
5055 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
5056 const Date
& d
= wn
.date
;
5058 wxDateTime dt
= d
.DT();
5060 wxDateTime::wxDateTime_t
5061 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
5062 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5063 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5064 dnum
= dt
.GetDayOfYear();
5066 wxPrintf(_T("%s: the day number is %d"), d
.FormatDate().c_str(), dnum
);
5067 if ( dnum
== wn
.dnum
)
5069 wxPrintf(_T(" (ok)"));
5073 wxPrintf(_T(" (ERROR: should be %d)"), wn
.dnum
);
5076 wxPrintf(_T(", week in month = %d"), wmon
);
5077 if ( wmon
!= wn
.wmon
)
5079 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon
);
5082 wxPrintf(_T(" or %d"), wmon2
);
5083 if ( wmon2
== wn
.wmon2
)
5085 wxPrintf(_T(" (ok)"));
5089 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon2
);
5092 wxPrintf(_T(", week in year = %d"), week
);
5093 if ( week
!= wn
.week
)
5095 wxPrintf(_T(" (ERROR: should be %d)"), wn
.week
);
5098 wxPutchar(_T('\n'));
5100 wxDateTime
dt2(1, wxDateTime::Jan
, d
.year
);
5101 dt2
.SetToTheWeek(wn
.week
, dt
.GetWeekDay());
5105 d2
.Init(dt2
.GetTm());
5106 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
5107 d2
.FormatDate().c_str());
5112 // test DST calculations
5113 static void TestTimeDST()
5115 wxPuts(_T("\n*** wxDateTime DST test ***"));
5117 wxPrintf(_T("DST is%s in effect now.\n\n"),
5118 wxDateTime::Now().IsDST() ? _T("") : _T(" not"));
5120 // taken from http://www.energy.ca.gov/daylightsaving.html
5121 static const Date datesDST
[2][2004 - 1900 + 1] =
5124 { 1, wxDateTime::Apr
, 1990 },
5125 { 7, wxDateTime::Apr
, 1991 },
5126 { 5, wxDateTime::Apr
, 1992 },
5127 { 4, wxDateTime::Apr
, 1993 },
5128 { 3, wxDateTime::Apr
, 1994 },
5129 { 2, wxDateTime::Apr
, 1995 },
5130 { 7, wxDateTime::Apr
, 1996 },
5131 { 6, wxDateTime::Apr
, 1997 },
5132 { 5, wxDateTime::Apr
, 1998 },
5133 { 4, wxDateTime::Apr
, 1999 },
5134 { 2, wxDateTime::Apr
, 2000 },
5135 { 1, wxDateTime::Apr
, 2001 },
5136 { 7, wxDateTime::Apr
, 2002 },
5137 { 6, wxDateTime::Apr
, 2003 },
5138 { 4, wxDateTime::Apr
, 2004 },
5141 { 28, wxDateTime::Oct
, 1990 },
5142 { 27, wxDateTime::Oct
, 1991 },
5143 { 25, wxDateTime::Oct
, 1992 },
5144 { 31, wxDateTime::Oct
, 1993 },
5145 { 30, wxDateTime::Oct
, 1994 },
5146 { 29, wxDateTime::Oct
, 1995 },
5147 { 27, wxDateTime::Oct
, 1996 },
5148 { 26, wxDateTime::Oct
, 1997 },
5149 { 25, wxDateTime::Oct
, 1998 },
5150 { 31, wxDateTime::Oct
, 1999 },
5151 { 29, wxDateTime::Oct
, 2000 },
5152 { 28, wxDateTime::Oct
, 2001 },
5153 { 27, wxDateTime::Oct
, 2002 },
5154 { 26, wxDateTime::Oct
, 2003 },
5155 { 31, wxDateTime::Oct
, 2004 },
5160 for ( year
= 1990; year
< 2005; year
++ )
5162 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
5163 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
5165 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5166 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
5168 size_t n
= year
- 1990;
5169 const Date
& dBegin
= datesDST
[0][n
];
5170 const Date
& dEnd
= datesDST
[1][n
];
5172 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
5174 wxPuts(_T(" (ok)"));
5178 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5179 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
5180 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
5186 for ( year
= 1990; year
< 2005; year
++ )
5188 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5190 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
5191 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
5195 // test wxDateTime -> text conversion
5196 static void TestTimeFormat()
5198 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5200 // some information may be lost during conversion, so store what kind
5201 // of info should we recover after a round trip
5204 CompareNone
, // don't try comparing
5205 CompareBoth
, // dates and times should be identical
5206 CompareDate
, // dates only
5207 CompareTime
// time only
5212 CompareKind compareKind
;
5213 const wxChar
*format
;
5214 } formatTestFormats
[] =
5216 { CompareBoth
, _T("---> %c") },
5217 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
5218 { CompareBoth
, _T("Date is %x, time is %X") },
5219 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5220 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
5221 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
5224 static const Date formatTestDates
[] =
5226 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
5227 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
5229 // this test can't work for other centuries because it uses two digit
5230 // years in formats, so don't even try it
5231 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
5232 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
5233 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
5237 // an extra test (as it doesn't depend on date, don't do it in the loop)
5238 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5240 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
5244 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
5245 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
5247 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
5248 wxPrintf(_T("%s"), s
.c_str());
5250 // what can we recover?
5251 int kind
= formatTestFormats
[n
].compareKind
;
5255 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
5258 // converion failed - should it have?
5259 if ( kind
== CompareNone
)
5260 wxPuts(_T(" (ok)"));
5262 wxPuts(_T(" (ERROR: conversion back failed)"));
5266 // should have parsed the entire string
5267 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5271 bool equal
= false; // suppress compilaer warning
5279 equal
= dt
.IsSameDate(dt2
);
5283 equal
= dt
.IsSameTime(dt2
);
5289 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5290 dt2
.Format().c_str(), dt
.Format().c_str());
5294 wxPuts(_T(" (ok)"));
5301 // test text -> wxDateTime conversion
5302 static void TestTimeParse()
5304 wxPuts(_T("\n*** wxDateTime parse test ***"));
5306 struct ParseTestData
5308 const wxChar
*format
;
5313 static const ParseTestData parseTestDates
[] =
5315 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, true },
5316 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, true },
5319 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
5321 const wxChar
*format
= parseTestDates
[n
].format
;
5323 wxPrintf(_T("%s => "), format
);
5326 if ( dt
.ParseRfc822Date(format
) )
5328 wxPrintf(_T("%s "), dt
.Format().c_str());
5330 if ( parseTestDates
[n
].good
)
5332 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
5339 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal
.Format().c_str());
5344 wxPuts(_T("(ERROR: bad format)"));
5349 wxPrintf(_T("bad format (%s)\n"),
5350 parseTestDates
[n
].good
? "ERROR" : "ok");
5355 static void TestDateTimeInteractive()
5357 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5363 wxPrintf(_T("Enter a date: "));
5364 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
5367 // kill the last '\n'
5368 buf
[wxStrlen(buf
) - 1] = 0;
5371 const wxChar
*p
= dt
.ParseDate(buf
);
5374 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
5380 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
5383 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5384 dt
.Format(_T("%b %d, %Y")).c_str(),
5386 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5387 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5388 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
5391 wxPuts(_T("\n*** done ***"));
5394 static void TestTimeMS()
5396 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5398 wxDateTime dt1
= wxDateTime::Now(),
5399 dt2
= wxDateTime::UNow();
5401 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
5402 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5403 wxPrintf(_T("Dummy loop: "));
5404 for ( int i
= 0; i
< 6000; i
++ )
5406 //for ( int j = 0; j < 10; j++ )
5409 s
.Printf(_T("%g"), sqrt(i
));
5415 wxPuts(_T(", done"));
5418 dt2
= wxDateTime::UNow();
5419 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5421 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
5423 wxPuts(_T("\n*** done ***"));
5426 static void TestTimeArithmetics()
5428 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5430 static const struct ArithmData
5432 ArithmData(const wxDateSpan
& sp
, const wxChar
*nam
)
5433 : span(sp
), name(nam
) { }
5437 } testArithmData
[] =
5439 ArithmData(wxDateSpan::Day(), _T("day")),
5440 ArithmData(wxDateSpan::Week(), _T("week")),
5441 ArithmData(wxDateSpan::Month(), _T("month")),
5442 ArithmData(wxDateSpan::Year(), _T("year")),
5443 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5446 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
5448 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
5450 wxDateSpan span
= testArithmData
[n
].span
;
5454 const wxChar
*name
= testArithmData
[n
].name
;
5455 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5456 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
5457 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
5459 wxPrintf(_T("Going back: %s"), (dt1
- span
).FormatISODate().c_str());
5460 if ( dt1
- span
== dt
)
5462 wxPuts(_T(" (ok)"));
5466 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5469 wxPrintf(_T("Going forward: %s"), (dt2
+ span
).FormatISODate().c_str());
5470 if ( dt2
+ span
== dt
)
5472 wxPuts(_T(" (ok)"));
5476 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5479 wxPrintf(_T("Double increment: %s"), (dt2
+ 2*span
).FormatISODate().c_str());
5480 if ( dt2
+ 2*span
== dt1
)
5482 wxPuts(_T(" (ok)"));
5486 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2
.FormatISODate().c_str());
5493 static void TestTimeHolidays()
5495 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5497 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
5498 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
5499 dtEnd
= dtStart
.GetLastMonthDay();
5501 wxDateTimeArray hol
;
5502 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
5504 const wxChar
*format
= _T("%d-%b-%Y (%a)");
5506 wxPrintf(_T("All holidays between %s and %s:\n"),
5507 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
5509 size_t count
= hol
.GetCount();
5510 for ( size_t n
= 0; n
< count
; n
++ )
5512 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
5518 static void TestTimeZoneBug()
5520 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5522 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
5523 for ( int i
= 0; i
< 31; i
++ )
5525 wxPrintf(_T("Date %s: week day %s.\n"),
5526 date
.Format(_T("%d-%m-%Y")).c_str(),
5527 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
5529 date
+= wxDateSpan::Day();
5535 static void TestTimeSpanFormat()
5537 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5539 static const wxChar
*formats
[] =
5541 _T("(default) %H:%M:%S"),
5542 _T("%E weeks and %D days"),
5543 _T("%l milliseconds"),
5544 _T("(with ms) %H:%M:%S:%l"),
5545 _T("100%% of minutes is %M"), // test "%%"
5546 _T("%D days and %H hours"),
5547 _T("or also %S seconds"),
5550 wxTimeSpan
ts1(1, 2, 3, 4),
5552 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
5554 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5555 ts1
.Format(formats
[n
]).c_str(),
5556 ts2
.Format(formats
[n
]).c_str());
5562 #endif // TEST_DATETIME
5564 // ----------------------------------------------------------------------------
5565 // wxTextInput/OutputStream
5566 // ----------------------------------------------------------------------------
5568 #ifdef TEST_TEXTSTREAM
5570 #include "wx/txtstrm.h"
5571 #include "wx/wfstream.h"
5573 static void TestTextInputStream()
5575 wxPuts(_T("\n*** wxTextInputStream test ***"));
5577 wxFileInputStream
fsIn(_T("testdata.fc"));
5580 wxPuts(_T("ERROR: couldn't open file."));
5584 wxTextInputStream
tis(fsIn
);
5589 const wxString s
= tis
.ReadLine();
5591 // line could be non empty if the last line of the file isn't
5592 // terminated with EOL
5593 if ( fsIn
.Eof() && s
.empty() )
5596 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
5601 #endif // TEST_TEXTSTREAM
5603 // ----------------------------------------------------------------------------
5605 // ----------------------------------------------------------------------------
5609 #include "wx/thread.h"
5611 static size_t gs_counter
= (size_t)-1;
5612 static wxCriticalSection gs_critsect
;
5613 static wxSemaphore gs_cond
;
5615 class MyJoinableThread
: public wxThread
5618 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
5619 { m_n
= n
; Create(); }
5621 // thread execution starts here
5622 virtual ExitCode
Entry();
5628 wxThread::ExitCode
MyJoinableThread::Entry()
5630 unsigned long res
= 1;
5631 for ( size_t n
= 1; n
< m_n
; n
++ )
5635 // it's a loooong calculation :-)
5639 return (ExitCode
)res
;
5642 class MyDetachedThread
: public wxThread
5645 MyDetachedThread(size_t n
, wxChar ch
)
5649 m_cancelled
= false;
5654 // thread execution starts here
5655 virtual ExitCode
Entry();
5658 virtual void OnExit();
5661 size_t m_n
; // number of characters to write
5662 wxChar m_ch
; // character to write
5664 bool m_cancelled
; // false if we exit normally
5667 wxThread::ExitCode
MyDetachedThread::Entry()
5670 wxCriticalSectionLocker
lock(gs_critsect
);
5671 if ( gs_counter
== (size_t)-1 )
5677 for ( size_t n
= 0; n
< m_n
; n
++ )
5679 if ( TestDestroy() )
5689 wxThread::Sleep(100);
5695 void MyDetachedThread::OnExit()
5697 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5699 wxCriticalSectionLocker
lock(gs_critsect
);
5700 if ( !--gs_counter
&& !m_cancelled
)
5704 static void TestDetachedThreads()
5706 wxPuts(_T("\n*** Testing detached threads ***"));
5708 static const size_t nThreads
= 3;
5709 MyDetachedThread
*threads
[nThreads
];
5711 for ( n
= 0; n
< nThreads
; n
++ )
5713 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
5716 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
5717 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
5719 for ( n
= 0; n
< nThreads
; n
++ )
5724 // wait until all threads terminate
5730 static void TestJoinableThreads()
5732 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5734 // calc 10! in the background
5735 MyJoinableThread
thread(10);
5738 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5739 (unsigned long)thread
.Wait());
5742 static void TestThreadSuspend()
5744 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5746 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
5750 // this is for this demo only, in a real life program we'd use another
5751 // condition variable which would be signaled from wxThread::Entry() to
5752 // tell us that the thread really started running - but here just wait a
5753 // bit and hope that it will be enough (the problem is, of course, that
5754 // the thread might still not run when we call Pause() which will result
5756 wxThread::Sleep(300);
5758 for ( size_t n
= 0; n
< 3; n
++ )
5762 wxPuts(_T("\nThread suspended"));
5765 // don't sleep but resume immediately the first time
5766 wxThread::Sleep(300);
5768 wxPuts(_T("Going to resume the thread"));
5773 wxPuts(_T("Waiting until it terminates now"));
5775 // wait until the thread terminates
5781 static void TestThreadDelete()
5783 // As above, using Sleep() is only for testing here - we must use some
5784 // synchronisation object instead to ensure that the thread is still
5785 // running when we delete it - deleting a detached thread which already
5786 // terminated will lead to a crash!
5788 wxPuts(_T("\n*** Testing thread delete function ***"));
5790 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
5794 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5796 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
5800 wxThread::Sleep(300);
5804 wxPuts(_T("\nDeleted a running thread."));
5806 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
5810 wxThread::Sleep(300);
5816 wxPuts(_T("\nDeleted a sleeping thread."));
5818 MyJoinableThread
thread3(20);
5823 wxPuts(_T("\nDeleted a joinable thread."));
5825 MyJoinableThread
thread4(2);
5828 wxThread::Sleep(300);
5832 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
5837 class MyWaitingThread
: public wxThread
5840 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
5843 m_condition
= condition
;
5848 virtual ExitCode
Entry()
5850 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
5855 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
5859 m_condition
->Wait();
5862 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
5870 wxCondition
*m_condition
;
5873 static void TestThreadConditions()
5876 wxCondition
condition(mutex
);
5878 // otherwise its difficult to understand which log messages pertain to
5880 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
5881 // condition.GetId(), gs_cond.GetId());
5883 // create and launch threads
5884 MyWaitingThread
*threads
[10];
5887 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5889 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
5892 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5897 // wait until all threads run
5898 wxPuts(_T("Main thread is waiting for the other threads to start"));
5901 size_t nRunning
= 0;
5902 while ( nRunning
< WXSIZEOF(threads
) )
5908 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
5912 wxPuts(_T("Main thread: all threads started up."));
5915 wxThread::Sleep(500);
5918 // now wake one of them up
5919 wxPrintf(_T("Main thread: about to signal the condition.\n"));
5924 wxThread::Sleep(200);
5926 // wake all the (remaining) threads up, so that they can exit
5927 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
5929 condition
.Broadcast();
5931 // give them time to terminate (dirty!)
5932 wxThread::Sleep(500);
5935 #include "wx/utils.h"
5937 class MyExecThread
: public wxThread
5940 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
5946 virtual ExitCode
Entry()
5948 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
5955 static void TestThreadExec()
5957 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
5959 MyExecThread
thread(_T("true"));
5962 wxPrintf(_T("Main program exit code: %ld.\n"),
5963 wxExecute(_T("false"), wxEXEC_SYNC
));
5965 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
5969 #include "wx/datetime.h"
5971 class MySemaphoreThread
: public wxThread
5974 MySemaphoreThread(int i
, wxSemaphore
*sem
)
5975 : wxThread(wxTHREAD_JOINABLE
),
5982 virtual ExitCode
Entry()
5984 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
5985 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5989 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
5990 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5994 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
5995 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
6007 WX_DEFINE_ARRAY(wxThread
*, ArrayThreads
);
6009 static void TestSemaphore()
6011 wxPuts(_T("*** Testing wxSemaphore class. ***"));
6013 static const int SEM_LIMIT
= 3;
6015 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
6016 ArrayThreads threads
;
6018 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
6020 threads
.Add(new MySemaphoreThread(i
, &sem
));
6021 threads
.Last()->Run();
6024 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
6031 #endif // TEST_THREADS
6033 // ----------------------------------------------------------------------------
6035 // ----------------------------------------------------------------------------
6039 #include "wx/dynarray.h"
6041 typedef unsigned short ushort
;
6043 static int MyStringCompare(wxString
* s1
, wxString
* s2
)
6045 return wxStrcmp(s1
->c_str(), s2
->c_str());
6048 static int MyStringReverseCompare(wxString
* s1
, wxString
* s2
)
6050 return -wxStrcmp(s1
->c_str(), s2
->c_str());
6054 #define DefineCompare(name, T) \
6056 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
6058 return first - second; \
6061 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
6063 return *first - *second; \
6066 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
6068 return *second - *first; \
6071 DefineCompare(UShort, ushort);
6072 DefineCompare(Int
, int);
6074 // test compilation of all macros
6075 WX_DEFINE_ARRAY_SHORT(ushort
, wxArrayUShort
);
6076 WX_DEFINE_SORTED_ARRAY_SHORT(ushort
, wxSortedArrayUShortNoCmp
);
6077 WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort
, UShortCompareValues
, wxSortedArrayUShort
);
6078 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues
, wxSortedArrayInt
);
6080 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
6081 #include "wx/arrimpl.cpp"
6082 WX_DEFINE_OBJARRAY(ArrayBars
);
6084 static void PrintArray(const wxChar
* name
, const wxArrayString
& array
)
6086 wxPrintf(_T("Dump of the array '%s'\n"), name
);
6088 size_t nCount
= array
.GetCount();
6089 for ( size_t n
= 0; n
< nCount
; n
++ )
6091 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
6095 static void PrintArray(const wxChar
* name
, const wxSortedArrayString
& array
)
6097 wxPrintf(_T("Dump of the array '%s'\n"), name
);
6099 size_t nCount
= array
.GetCount();
6100 for ( size_t n
= 0; n
< nCount
; n
++ )
6102 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
6106 int wxCMPFUNC_CONV
StringLenCompare(const wxString
& first
,
6107 const wxString
& second
)
6109 return first
.length() - second
.length();
6112 #define TestArrayOf(name) \
6114 static void PrintArray(const wxChar* name, const wxSortedArray##name & array) \
6116 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6118 size_t nCount = array.GetCount(); \
6119 for ( size_t n = 0; n < nCount; n++ ) \
6121 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6125 static void PrintArray(const wxChar* name, const wxArray##name & array) \
6127 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6129 size_t nCount = array.GetCount(); \
6130 for ( size_t n = 0; n < nCount; n++ ) \
6132 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6136 static void TestArrayOf ## name ## s() \
6138 wxPrintf(_T("*** Testing wxArray%s ***\n"), #name); \
6146 wxPuts(_T("Initially:")); \
6147 PrintArray(_T("a"), a); \
6149 wxPuts(_T("After sort:")); \
6150 a.Sort(name ## Compare); \
6151 PrintArray(_T("a"), a); \
6153 wxPuts(_T("After reverse sort:")); \
6154 a.Sort(name ## RevCompare); \
6155 PrintArray(_T("a"), a); \
6157 wxSortedArray##name b; \
6163 wxPuts(_T("Sorted array initially:")); \
6164 PrintArray(_T("b"), b); \
6167 TestArrayOf(UShort
);
6170 static void TestStlArray()
6172 wxPuts(_T("*** Testing std::vector operations ***\n"));
6176 wxArrayInt::iterator it
, en
;
6177 wxArrayInt::reverse_iterator rit
, ren
;
6179 for ( i
= 0; i
< 5; ++i
)
6182 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
6183 it
!= en
; ++it
, ++i
)
6185 wxPuts(_T("Error in iterator\n"));
6187 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
6188 rit
!= ren
; ++rit
, --i
)
6190 wxPuts(_T("Error in reverse_iterator\n"));
6192 if ( *list1
.rbegin() != *(list1
.end()-1) ||
6193 *list1
.begin() != *(list1
.rend()-1) )
6194 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6196 it
= list1
.begin()+1;
6197 rit
= list1
.rbegin()+1;
6198 if ( *list1
.begin() != *(it
-1) ||
6199 *list1
.rbegin() != *(rit
-1) )
6200 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6202 if ( list1
.front() != 0 || list1
.back() != 4 )
6203 wxPuts(_T("Error in front()/back()\n"));
6205 list1
.erase(list1
.begin());
6206 list1
.erase(list1
.end()-1);
6208 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
6209 it
!= en
; ++it
, ++i
)
6211 wxPuts(_T("Error in erase()\n"));
6214 wxPuts(_T("*** Testing std::vector operations finished ***\n"));
6217 static void TestArrayOfObjects()
6219 wxPuts(_T("*** Testing wxObjArray ***\n"));
6223 Bar
bar(_T("second bar (two copies!)"));
6225 wxPrintf(_T("Initially: %u objects in the array, %u objects total.\n"),
6226 bars
.GetCount(), Bar::GetNumber());
6228 bars
.Add(new Bar(_T("first bar")));
6231 wxPrintf(_T("Now: %u objects in the array, %u objects total.\n"),
6232 bars
.GetCount(), Bar::GetNumber());
6234 bars
.RemoveAt(1, bars
.GetCount() - 1);
6236 wxPrintf(_T("After removing all but first element: %u objects in the ")
6237 _T("array, %u objects total.\n"),
6238 bars
.GetCount(), Bar::GetNumber());
6242 wxPrintf(_T("After Empty(): %u objects in the array, %u objects total.\n"),
6243 bars
.GetCount(), Bar::GetNumber());
6246 wxPrintf(_T("Finally: no more objects in the array, %u objects total.\n"),
6250 #endif // TEST_ARRAYS
6252 // ----------------------------------------------------------------------------
6254 // ----------------------------------------------------------------------------
6258 #include "wx/timer.h"
6259 #include "wx/tokenzr.h"
6261 static void TestStringConstruction()
6263 wxPuts(_T("*** Testing wxString constructores ***"));
6265 #define TEST_CTOR(args, res) \
6268 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
6271 wxPuts(_T("(ok)")); \
6275 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
6279 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
6280 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
6281 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
6282 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
6284 static const wxChar
*s
= _T("?really!");
6285 const wxChar
*start
= wxStrchr(s
, _T('r'));
6286 const wxChar
*end
= wxStrchr(s
, _T('!'));
6287 TEST_CTOR((start
, end
), _T("really"));
6292 static void TestString()
6302 for (int i
= 0; i
< 1000000; ++i
)
6306 c
= _T("! How'ya doin'?");
6309 c
= _T("Hello world! What's up?");
6314 wxPrintf(_T("TestString elapsed time: %ld\n"), sw
.Time());
6317 static void TestPChar()
6325 for (int i
= 0; i
< 1000000; ++i
)
6327 wxStrcpy (a
, _T("Hello"));
6328 wxStrcpy (b
, _T(" world"));
6329 wxStrcpy (c
, _T("! How'ya doin'?"));
6332 wxStrcpy (c
, _T("Hello world! What's up?"));
6333 if (wxStrcmp (c
, a
) == 0)
6334 wxStrcpy (c
, _T("Doh!"));
6337 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw
.Time());
6340 static void TestStringSub()
6342 wxString
s(_T("Hello, world!"));
6344 wxPuts(_T("*** Testing wxString substring extraction ***"));
6346 wxPrintf(_T("String = '%s'\n"), s
.c_str());
6347 wxPrintf(_T("Left(5) = '%s'\n"), s
.Left(5).c_str());
6348 wxPrintf(_T("Right(6) = '%s'\n"), s
.Right(6).c_str());
6349 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
6350 wxPrintf(_T("Mid(3) = '%s'\n"), s
.Mid(3).c_str());
6351 wxPrintf(_T("substr(3, 5) = '%s'\n"), s
.substr(3, 5).c_str());
6352 wxPrintf(_T("substr(3) = '%s'\n"), s
.substr(3).c_str());
6354 static const wxChar
*prefixes
[] =
6358 _T("Hello, world!"),
6359 _T("Hello, world!!!"),
6365 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
6367 wxString prefix
= prefixes
[n
], rest
;
6368 bool rc
= s
.StartsWith(prefix
, &rest
);
6369 wxPrintf(_T("StartsWith('%s') = %s"), prefix
.c_str(), rc
? _T("true") : _T("false"));
6372 wxPrintf(_T(" (the rest is '%s')\n"), rest
.c_str());
6383 static void TestStringFormat()
6385 wxPuts(_T("*** Testing wxString formatting ***"));
6388 s
.Printf(_T("%03d"), 18);
6390 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6391 wxPrintf(_T("Number 18: %s\n"), s
.c_str());
6396 // returns "not found" for npos, value for all others
6397 static wxString
PosToString(size_t res
)
6399 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
6400 : wxString::Format(_T("%u"), res
);
6404 static void TestStringFind()
6406 wxPuts(_T("*** Testing wxString find() functions ***"));
6408 static const wxChar
*strToFind
= _T("ell");
6409 static const struct StringFindTest
6413 result
; // of searching "ell" in str
6416 { _T("Well, hello world"), 0, 1 },
6417 { _T("Well, hello world"), 6, 7 },
6418 { _T("Well, hello world"), 9, wxString::npos
},
6421 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
6423 const StringFindTest
& ft
= findTestData
[n
];
6424 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
6426 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6427 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
6429 size_t resTrue
= ft
.result
;
6430 if ( res
== resTrue
)
6436 wxPrintf(_T("(ERROR: should be %s)\n"),
6437 PosToString(resTrue
).c_str());
6444 static void TestStringTokenizer()
6446 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6448 static const wxChar
*modeNames
[] =
6452 _T("return all empty"),
6457 static const struct StringTokenizerTest
6459 const wxChar
*str
; // string to tokenize
6460 const wxChar
*delims
; // delimiters to use
6461 size_t count
; // count of token
6462 wxStringTokenizerMode mode
; // how should we tokenize it
6463 } tokenizerTestData
[] =
6465 { _T(""), _T(" "), 0 },
6466 { _T("Hello, world"), _T(" "), 2 },
6467 { _T("Hello, world "), _T(" "), 2 },
6468 { _T("Hello, world"), _T(","), 2 },
6469 { _T("Hello, world!"), _T(",!"), 2 },
6470 { _T("Hello,, world!"), _T(",!"), 3 },
6471 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
6472 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6473 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
6474 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
6475 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
6476 { _T("01/02/99"), _T("/-"), 3 },
6477 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
6480 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
6482 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
6483 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
6485 size_t count
= tkz
.CountTokens();
6486 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6487 MakePrintable(tt
.str
).c_str(),
6489 MakePrintable(tt
.delims
).c_str(),
6490 modeNames
[tkz
.GetMode()]);
6491 if ( count
== tt
.count
)
6497 wxPrintf(_T("(ERROR: should be %u)\n"), tt
.count
);
6502 // if we emulate strtok(), check that we do it correctly
6503 wxChar
*buf
, *s
= NULL
, *last
;
6505 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
6507 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
6508 wxStrcpy(buf
, tt
.str
);
6510 s
= wxStrtok(buf
, tt
.delims
, &last
);
6517 // now show the tokens themselves
6519 while ( tkz
.HasMoreTokens() )
6521 wxString token
= tkz
.GetNextToken();
6523 wxPrintf(_T("\ttoken %u: '%s'"),
6525 MakePrintable(token
).c_str());
6531 wxPuts(_T(" (ok)"));
6535 wxPrintf(_T(" (ERROR: should be %s)\n"), s
);
6538 s
= wxStrtok(NULL
, tt
.delims
, &last
);
6542 // nothing to compare with
6547 if ( count2
!= count
)
6549 wxPuts(_T("\tERROR: token count mismatch"));
6558 static void TestStringReplace()
6560 wxPuts(_T("*** Testing wxString::replace ***"));
6562 static const struct StringReplaceTestData
6564 const wxChar
*original
; // original test string
6565 size_t start
, len
; // the part to replace
6566 const wxChar
*replacement
; // the replacement string
6567 const wxChar
*result
; // and the expected result
6568 } stringReplaceTestData
[] =
6570 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6571 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6572 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6573 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6574 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6577 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
6579 const StringReplaceTestData data
= stringReplaceTestData
[n
];
6581 wxString original
= data
.original
;
6582 original
.replace(data
.start
, data
.len
, data
.replacement
);
6584 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6585 data
.original
, data
.start
, data
.len
, data
.replacement
,
6588 if ( original
== data
.result
)
6594 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
6601 static void TestStringMatch()
6603 wxPuts(_T("*** Testing wxString::Matches() ***"));
6605 static const struct StringMatchTestData
6608 const wxChar
*wildcard
;
6610 } stringMatchTestData
[] =
6612 { _T("foobar"), _T("foo*"), 1 },
6613 { _T("foobar"), _T("*oo*"), 1 },
6614 { _T("foobar"), _T("*bar"), 1 },
6615 { _T("foobar"), _T("??????"), 1 },
6616 { _T("foobar"), _T("f??b*"), 1 },
6617 { _T("foobar"), _T("f?b*"), 0 },
6618 { _T("foobar"), _T("*goo*"), 0 },
6619 { _T("foobar"), _T("*foo"), 0 },
6620 { _T("foobarfoo"), _T("*foo"), 1 },
6621 { _T(""), _T("*"), 1 },
6622 { _T(""), _T("?"), 0 },
6625 for ( size_t n
= 0; n
< WXSIZEOF(stringMatchTestData
); n
++ )
6627 const StringMatchTestData
& data
= stringMatchTestData
[n
];
6628 bool matches
= wxString(data
.text
).Matches(data
.wildcard
);
6629 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6631 matches
? _T("matches") : _T("doesn't match"),
6633 matches
== data
.matches
? _T("ok") : _T("ERROR"));
6639 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6640 void ok(int line
, bool ok
, const wxString
& msg
= wxEmptyString
)
6643 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line
) +
6647 void is(int line
, const wxString
& got
, const wxString
& expected
,
6648 const wxString
& msg
= wxEmptyString
)
6650 bool isOk
= got
== expected
;
6651 ok(line
, isOk
, msg
);
6654 wxPuts(_T("Got: ") + got
);
6655 wxPuts(_T("Expected: ") + expected
);
6660 void is(int line
, const wxChar
* got
, const wxChar
* expected
,
6661 const wxString
& msg
= wxEmptyString
)
6663 bool isOk
= wxStrcmp( got
, expected
) == 0;
6664 ok(line
, isOk
, msg
);
6667 wxPuts(_T("Got: ") + wxString(got
));
6668 wxPuts(_T("Expected: ") + wxString(expected
));
6673 void is(int line
, const wxChar
& got
, const wxChar
& expected
,
6674 const wxString
& msg
= wxEmptyString
)
6676 bool isOk
= got
== expected
;
6677 ok(line
, isOk
, msg
);
6680 wxPuts(_T("Got: ") + got
);
6681 wxPuts(_T("Expected: ") + expected
);
6685 void is(int line
, size_t got
, size_t expected
,
6686 const wxString
& msg
= wxEmptyString
)
6688 bool isOk
= got
== expected
;
6689 ok(line
, isOk
, msg
);
6692 wxPuts(wxString::Format(_T("Got: %ld"), got
));
6693 wxPuts(wxString::Format(_T("Expected: %ld"), expected
));
6697 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6698 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6700 void TestStdString()
6702 wxPuts(_T("*** Testing std::string operations ***\n"));
6705 wxString
s1(_T("abcdefgh")),
6706 s2(_T("abcdefghijklm"), 8),
6707 s3(_T("abcdefghijklm")),
6711 s7(s3
.begin(), s3
.begin() + 8);
6712 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
6714 is( __LINE__
, s1
, _T("abcdefgh") );
6715 is( __LINE__
, s2
, s1
);
6716 is( __LINE__
, s4
, _T("aaaaaaaa") );
6717 is( __LINE__
, s5
, _T("abcdefgh") );
6718 is( __LINE__
, s6
, s1
);
6719 is( __LINE__
, s7
, s1
);
6720 is( __LINE__
, s8
, _T("efgh") );
6723 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6724 s1
.append(_T("def"));
6725 s2
.append(_T("defgh"), 3);
6726 s3
.append(wxString(_T("abcdef")), 3, 6);
6728 s5
.append(3, _T('a'));
6729 s6
.append(s1
.begin() + 3, s1
.end());
6731 is( __LINE__
, s1
, _T("abcdef") );
6732 is( __LINE__
, s2
, _T("abcdef") );
6733 is( __LINE__
, s3
, _T("abcdef") );
6734 is( __LINE__
, s4
, _T("abcabcdef") );
6735 is( __LINE__
, s5
, _T("abcaaa") );
6736 is( __LINE__
, s6
, _T("abcdef") );
6739 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6740 s1
.assign(_T("def"));
6741 s2
.assign(_T("defgh"), 3);
6742 s3
.assign(wxString(_T("abcdef")), 3, 6);
6744 s5
.assign(3, _T('a'));
6745 s6
.assign(s1
.begin() + 1, s1
.end());
6747 is( __LINE__
, s1
, _T("def") );
6748 is( __LINE__
, s2
, _T("def") );
6749 is( __LINE__
, s3
, _T("def") );
6750 is( __LINE__
, s4
, _T("def") );
6751 is( __LINE__
, s5
, _T("aaa") );
6752 is( __LINE__
, s6
, _T("ef") );
6755 s1
= _T("abcdefgh");
6756 s2
= _T("abcdefgh");
6758 s4
= _T("abcdefghi");
6761 s7
= _T("zabcdefg");
6763 ok( __LINE__
, s1
.compare(s2
) == 0 );
6764 ok( __LINE__
, s1
.compare(s3
) > 0 );
6765 ok( __LINE__
, s1
.compare(s4
) < 0 );
6766 ok( __LINE__
, s1
.compare(s5
) > 0 );
6767 ok( __LINE__
, s1
.compare(s6
) < 0 );
6768 ok( __LINE__
, s1
.compare(1, 12, s1
) > 0);
6769 ok( __LINE__
, s1
.compare(_T("abcdefgh")) == 0);
6770 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh")) == 0);
6771 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
6776 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
6777 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
6778 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
6780 is( __LINE__
, s1
, _T("acdefgh") );
6781 is( __LINE__
, s2
, _T("abcd") );
6782 is( __LINE__
, s3
, _T("ac") );
6783 is( __LINE__
, s4
, _T("abcdghi") );
6784 is( __LINE__
, s7
, _T("zabc") );
6785 is( __LINE__
, *it
, _T('c') );
6786 is( __LINE__
, *it2
, _T('g') );
6787 ok( __LINE__
, it3
== s7
.end() );
6791 // 01234567890123456789012345
6792 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6795 is_nom( s1
.find(_T('A')), 7u );
6796 is_nom( s1
.find(_T('A'), 7), 7u );
6797 is_nom( s1
.find(_T('Z')), wxString::npos
);
6798 is_nom( s1
.find(_T('C'), 22), 25u );
6800 is_nom( s1
.find(_T("gAB")), 6u );
6801 is_nom( s1
.find(_T("gAB"), 7), wxString::npos
);
6802 is_nom( s1
.find(_T("gAB"), 6), 6u );
6804 is_nom( s1
.find(_T("gABZZZ"), 2, 3), 6u );
6805 is_nom( s1
.find(_T("gABZZZ"), 7, 3), wxString::npos
);
6807 is_nom( s1
.find(s2
), 6u );
6808 is_nom( s1
.find(s2
, 7), wxString::npos
);
6809 is_nom( s1
.find(s2
, 6), 6u );
6811 // find_first_not_of
6813 // 01234567890123456789012345678901234
6814 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6817 is_nom( s1
.find_first_not_of(_T('a')), 6u );
6818 is_nom( s1
.find_first_not_of(_T('a'), 7), 7u );
6819 is_nom( s2
.find_first_not_of(_T('a')), wxString::npos
);
6821 is_nom( s1
.find_first_not_of(_T("abde"), 4), 7u );
6822 is_nom( s1
.find_first_not_of(_T("abde"), 7), 7u );
6823 is_nom( s1
.find_first_not_of(_T("abcdefghijkl")), wxString::npos
);
6825 is_nom( s1
.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
6828 is_nom( s1
.find_first_of(_T('c')), 7u );
6829 is_nom( s1
.find_first_of(_T('v')), wxString::npos
);
6830 is_nom( s1
.find_first_of(_T('c'), 10), 24u );
6832 is_nom( s1
.find_first_of(_T("ijkl")), 13u );
6833 is_nom( s1
.find_first_of(_T("ddcfg"), 17), 24u );
6834 is_nom( s1
.find_first_of(_T("ddcfga"), 17, 5), 24u );
6838 // 01234567890123456789012345678901234
6839 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6842 is_nom( s2
.find_last_not_of(_T('a')), wxString::npos
);
6843 is_nom( s1
.find_last_not_of(_T('d')), 33u );
6844 is_nom( s1
.find_last_not_of(_T('d'), 25), 24u );
6846 is_nom( s1
.find_last_not_of(_T("bcd")), 22u );
6847 is_nom( s1
.find_last_not_of(_T("abc"), 24), 16u );
6849 is_nom( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
6852 is_nom( s2
.find_last_of(_T('c')), wxString::npos
);
6853 is_nom( s1
.find_last_of(_T('a')), 22u );
6854 is_nom( s1
.find_last_of(_T('b'), 24), 23u );
6856 is_nom( s1
.find_last_of(_T("ijklm")), 16u );
6857 is_nom( s1
.find_last_of(_T("ijklma"), 33, 4), 16u );
6858 is_nom( s1
.find_last_of(_T("a"), 17), 17u );
6861 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
6862 s9
= s10
= _T("cdefg");
6864 s1
.insert(1, _T("cc") );
6865 s2
.insert(2, _T("cdef"), 3);
6867 s4
.insert(2, s10
, 3, 7);
6868 s5
.insert(1, 2, _T('c'));
6869 it
= s6
.insert(s6
.begin() + 3, _T('X'));
6870 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
6871 s8
.insert(s8
.begin(), 2, _T('c'));
6873 is( __LINE__
, s1
, _T("accaaa") );
6874 is( __LINE__
, s2
, _T("aacdeaa") );
6875 is( __LINE__
, s3
, _T("aacdefgaa") );
6876 is( __LINE__
, s4
, _T("aafgaa") );
6877 is( __LINE__
, s5
, _T("accaaa") );
6878 is( __LINE__
, s6
, _T("aaaXa") );
6879 is( __LINE__
, s7
, _T("cdefaaaa") );
6880 is( __LINE__
, s8
, _T("ccaaaa") );
6882 s1
= s2
= s3
= _T("aaaa");
6883 s1
.insert(0, _T("ccc"), 2);
6884 s2
.insert(4, _T("ccc"), 2);
6886 is( __LINE__
, s1
, _T("ccaaaa") );
6887 is( __LINE__
, s2
, _T("aaaacc") );
6890 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
6891 s9
= s10
= _T("werty");
6893 s1
.replace(3, 4, _T("rtyu"));
6894 s1
.replace(8, 7, _T("opopop"));
6895 s2
.replace(10, 12, _T("WWWW"));
6896 s3
.replace(1, 5, s9
);
6897 s4
.replace(1, 4, s9
, 0, 4);
6898 s5
.replace(1, 2, s9
, 1, 12);
6899 s6
.replace(0, 123, s9
, 0, 123);
6900 s7
.replace(2, 7, s9
);
6902 is( __LINE__
, s1
, _T("QWErtyuIopopop") );
6903 is( __LINE__
, s2
, _T("QWERTYUIOPWWWW") );
6904 is( __LINE__
, s3
, _T("QwertyUIOP") );
6905 is( __LINE__
, s4
, _T("QwertYUIOP") );
6906 is( __LINE__
, s5
, _T("QertyRTYUIOP") );
6907 is( __LINE__
, s6
, s9
);
6908 is( __LINE__
, s7
, _T("QWwertyP") );
6912 // 01234567890123456789012345
6913 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6917 is_nom( s1
.rfind(_T('A')), 23u );
6918 is_nom( s1
.rfind(_T('A'), 7), 7u );
6919 is_nom( s1
.rfind(_T('Z')), wxString::npos
);
6920 is_nom( s1
.rfind(_T('C'), 22), 19u );
6922 is_nom( s1
.rfind(_T("cAB")), 22u );
6923 is_nom( s1
.rfind(_T("cAB"), 15), wxString::npos
);
6924 is_nom( s1
.rfind(_T("cAB"), 21), 16u );
6926 is_nom( s1
.rfind(_T("gABZZZ"), 7, 3), 6u );
6927 is_nom( s1
.rfind(_T("gABZZZ"), 5, 3), wxString::npos
);
6929 is_nom( s1
.rfind(s2
), 6u );
6930 is_nom( s1
.rfind(s2
, 5), wxString::npos
);
6931 is_nom( s1
.rfind(s2
, 6), 6u );
6932 is_nom( s1
.rfind(s3
, 1), 0u );
6935 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
6939 s3
.resize( 14, _T(' ') );
6940 s4
.resize( 14, _T('W') );
6942 is_nom( s1
, _T("abcABCdefDEF") );
6943 is_nom( s2
, _T("abcABCdefD") );
6944 is_nom( s3
, _T("abcABCdefDEF ") );
6945 is_nom( s4
, _T("abcABCdefDEFWW") );
6948 s1
= _T("abcdefgABCDEFG");
6950 is_nom( s1
.substr( 0, 14 ), s1
);
6951 is_nom( s1
.substr( 1, 13 ), _T("bcdefgABCDEFG") );
6952 is_nom( s1
.substr( 1, 20 ), _T("bcdefgABCDEFG") );
6953 is_nom( s1
.substr( 14, 30 ), _T("") );
6955 wxPuts(_T("*** Testing std::string operations finished ***\n"));
6958 #endif // TEST_STRINGS
6960 // ----------------------------------------------------------------------------
6962 // ----------------------------------------------------------------------------
6964 #ifdef TEST_SNGLINST
6965 #include "wx/snglinst.h"
6966 #endif // TEST_SNGLINST
6968 int main(int argc
, char **argv
)
6970 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
6972 wxInitializer initializer
;
6975 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
6980 #ifdef TEST_SNGLINST
6981 wxSingleInstanceChecker checker
;
6982 if ( checker
.Create(_T(".wxconsole.lock")) )
6984 if ( checker
.IsAnotherRunning() )
6986 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
6991 // wait some time to give time to launch another instance
6992 wxPrintf(_T("Press \"Enter\" to continue..."));
6995 else // failed to create
6997 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
6999 #endif // TEST_SNGLINST
7003 #endif // TEST_CHARSET
7006 TestCmdLineConvert();
7008 #if wxUSE_CMDLINE_PARSER
7009 static const wxCmdLineEntryDesc cmdLineDesc
[] =
7011 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
7012 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
7013 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
7014 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
7016 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
7017 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
7018 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
7019 wxCMD_LINE_VAL_NUMBER
},
7020 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
7021 wxCMD_LINE_VAL_DATE
},
7023 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
7024 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
7030 wxChar
**wargv
= new wxChar
*[argc
+ 1];
7035 for (n
= 0; n
< argc
; n
++ )
7037 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
7038 wargv
[n
] = wxStrdup(warg
);
7045 #endif // wxUSE_UNICODE
7047 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
7051 for ( int n
= 0; n
< argc
; n
++ )
7056 #endif // wxUSE_UNICODE
7058 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
7059 wxCMD_LINE_VAL_STRING
,
7060 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
7062 switch ( parser
.Parse() )
7065 wxLogMessage(_T("Help was given, terminating."));
7069 ShowCmdLine(parser
);
7073 wxLogMessage(_T("Syntax error detected, aborting."));
7076 #endif // wxUSE_CMDLINE_PARSER
7078 #endif // TEST_CMDLINE
7086 TestStringConstruction();
7089 TestStringTokenizer();
7090 TestStringReplace();
7098 #endif // TEST_STRINGS
7101 if ( 1 || TEST_ALL
)
7104 a1
.Add(_T("tiger"));
7106 a1
.Add(_T("lion"), 3);
7108 a1
.Add(_T("human"));
7111 wxPuts(_T("*** Initially:"));
7113 PrintArray(_T("a1"), a1
);
7115 wxArrayString
a2(a1
);
7116 PrintArray(_T("a2"), a2
);
7119 wxSortedArrayString
a3(a1
);
7121 wxSortedArrayString a3
;
7122 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end();
7126 PrintArray(_T("a3"), a3
);
7128 wxPuts(_T("*** After deleting three strings from a1"));
7131 PrintArray(_T("a1"), a1
);
7132 PrintArray(_T("a2"), a2
);
7133 PrintArray(_T("a3"), a3
);
7136 wxPuts(_T("*** After reassigning a1 to a2 and a3"));
7138 PrintArray(_T("a2"), a2
);
7139 PrintArray(_T("a3"), a3
);
7142 wxPuts(_T("*** After sorting a1"));
7144 PrintArray(_T("a1"), a1
);
7146 wxPuts(_T("*** After sorting a1 in reverse order"));
7148 PrintArray(_T("a1"), a1
);
7151 wxPuts(_T("*** After sorting a1 by the string length"));
7152 a1
.Sort(&StringLenCompare
);
7153 PrintArray(_T("a1"), a1
);
7156 TestArrayOfObjects();
7157 TestArrayOfUShorts();
7162 #endif // TEST_ARRAYS
7173 #ifdef TEST_DLLLOADER
7175 #endif // TEST_DLLLOADER
7179 #endif // TEST_ENVIRON
7183 #endif // TEST_EXECUTE
7185 #ifdef TEST_FILECONF
7187 #endif // TEST_FILECONF
7196 #endif // TEST_LOCALE
7199 wxPuts(_T("*** Testing wxLog ***"));
7202 for ( size_t n
= 0; n
< 8000; n
++ )
7204 s
<< (wxChar
)(_T('A') + (n
% 26));
7207 wxLogWarning(_T("The length of the string is %lu"),
7208 (unsigned long)s
.length());
7211 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
7213 // this one shouldn't be truncated
7216 // but this one will because log functions use fixed size buffer
7217 // (note that it doesn't need '\n' at the end neither - will be added
7219 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
7231 #ifdef TEST_FILENAME
7234 wxFileName
fn(_T("c:\\foo"), _T("bar.baz"));
7235 DumpFileName(_T("Before Normalize():"), fn
);
7238 DumpFileName(_T("After Normalize():"), fn
);
7243 TestFileNameConstruction();
7244 TestFileNameMakeRelative();
7245 TestFileNameMakeAbsolute();
7246 TestFileNameSplit();
7249 TestFileNameComparison();
7250 TestFileNameOperations();
7252 #endif // TEST_FILENAME
7254 #ifdef TEST_FILETIME
7258 #endif // TEST_FILETIME
7261 wxLog::AddTraceMask(FTP_TRACE_MASK
);
7262 if ( TestFtpConnect() )
7273 if ( TEST_INTERACTIVE
)
7274 TestFtpInteractive();
7276 //else: connecting to the FTP server failed
7282 #ifdef TEST_LONGLONG
7283 // seed pseudo random generator
7284 srand((unsigned)time(NULL
));
7293 TestMultiplication();
7296 TestLongLongConversion();
7297 TestBitOperations();
7298 TestLongLongComparison();
7299 TestLongLongToString();
7300 TestLongLongPrintf();
7302 #endif // TEST_LONGLONG
7310 #endif // TEST_HASHMAP
7314 #endif // TEST_HASHSET
7317 wxLog::AddTraceMask(_T("mime"));
7322 TestMimeAssociate();
7327 #ifdef TEST_INFO_FUNCTIONS
7333 if ( TEST_INTERACTIVE
)
7336 #endif // TEST_INFO_FUNCTIONS
7338 #ifdef TEST_PATHLIST
7340 #endif // TEST_PATHLIST
7348 #endif // TEST_PRINTF
7352 #endif // TEST_REGCONF
7355 // TODO: write a real test using src/regex/tests file
7360 TestRegExSubmatch();
7361 TestRegExReplacement();
7363 if ( TEST_INTERACTIVE
)
7364 TestRegExInteractive();
7366 #endif // TEST_REGEX
7368 #ifdef TEST_REGISTRY
7370 TestRegistryAssociation();
7371 #endif // TEST_REGISTRY
7376 #endif // TEST_SOCKETS
7384 #endif // TEST_STREAMS
7386 #ifdef TEST_TEXTSTREAM
7387 TestTextInputStream();
7388 #endif // TEST_TEXTSTREAM
7391 int nCPUs
= wxThread::GetCPUCount();
7392 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
7394 wxThread::SetConcurrency(nCPUs
);
7396 TestJoinableThreads();
7400 TestJoinableThreads();
7401 TestDetachedThreads();
7402 TestThreadSuspend();
7404 TestThreadConditions();
7408 #endif // TEST_THREADS
7412 #endif // TEST_TIMER
7414 #ifdef TEST_DATETIME
7427 TestTimeArithmetics();
7430 TestTimeSpanFormat();
7436 if ( TEST_INTERACTIVE
)
7437 TestDateTimeInteractive();
7438 #endif // TEST_DATETIME
7440 #ifdef TEST_SCOPEGUARD
7445 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
7447 #endif // TEST_USLEEP
7452 #endif // TEST_VCARD
7456 #endif // TEST_VOLUME
7459 TestUnicodeTextFileRead();
7462 TestUnicodeToFromAscii();
7464 #endif // TEST_UNICODE
7468 TestEncodingConverter();
7469 #endif // TEST_WCHAR
7472 TestZipStreamRead();
7473 TestZipFileSystem();
7477 TestZlibStreamWrite();
7478 TestZlibStreamRead();