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 // ----------------------------------------------------------------------------
23 #error "This sample can't be compiled in GUI mode."
28 #include "wx/string.h"
33 // without this pragma, the stupid compiler precompiles #defines below so that
34 // changing them doesn't "take place" later!
39 // ----------------------------------------------------------------------------
40 // conditional compilation
41 // ----------------------------------------------------------------------------
44 A note about all these conditional compilation macros: this file is used
45 both as a test suite for various non-GUI wxWindows classes and as a
46 scratchpad for quick tests. So there are two compilation modes: if you
47 define TEST_ALL all tests are run, otherwise you may enable the individual
48 tests individually in the "#else" branch below.
51 // what to test (in alphabetic order)? uncomment the line below to do all tests
59 #define TEST_DLLLOADER
70 #define TEST_INFO_FUNCTIONS
82 #define TEST_SCOPEGUARD
87 #define TEST_TEXTSTREAM
91 // #define TEST_VCARD -- don't enable this (VZ)
98 static const bool TEST_ALL
= true;
102 static const bool TEST_ALL
= false;
105 // some tests are interactive, define this to run them
106 #ifdef TEST_INTERACTIVE
107 #undef TEST_INTERACTIVE
109 static const bool TEST_INTERACTIVE
= true;
111 static const bool TEST_INTERACTIVE
= false;
114 // ----------------------------------------------------------------------------
115 // test class for container objects
116 // ----------------------------------------------------------------------------
118 #if defined(TEST_ARRAYS) || defined(TEST_LIST)
120 class Bar
// Foo is already taken in the hash test
123 Bar(const wxString
& name
) : m_name(name
) { ms_bars
++; }
124 Bar(const Bar
& bar
) : m_name(bar
.m_name
) { ms_bars
++; }
125 ~Bar() { ms_bars
--; }
127 static size_t GetNumber() { return ms_bars
; }
129 const wxChar
*GetName() const { return m_name
; }
134 static size_t ms_bars
;
137 size_t Bar::ms_bars
= 0;
139 #endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
141 // ============================================================================
143 // ============================================================================
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
151 // replace TABs with \t and CRs with \n
152 static wxString
MakePrintable(const wxChar
*s
)
155 (void)str
.Replace(_T("\t"), _T("\\t"));
156 (void)str
.Replace(_T("\n"), _T("\\n"));
157 (void)str
.Replace(_T("\r"), _T("\\r"));
162 #endif // MakePrintable() is used
164 // ----------------------------------------------------------------------------
165 // wxFontMapper::CharsetToEncoding
166 // ----------------------------------------------------------------------------
170 #include "wx/fontmap.h"
172 static void TestCharset()
174 static const wxChar
*charsets
[] =
176 // some vali charsets
185 // and now some bogus ones
192 for ( size_t n
= 0; n
< WXSIZEOF(charsets
); n
++ )
194 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charsets
[n
]);
195 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
197 wxFontMapper::Get()->GetEncodingName(enc
).c_str(),
198 wxFontMapper::Get()->GetEncodingDescription(enc
).c_str());
202 #endif // TEST_CHARSET
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
210 #include "wx/cmdline.h"
211 #include "wx/datetime.h"
213 #if wxUSE_CMDLINE_PARSER
215 static void ShowCmdLine(const wxCmdLineParser
& parser
)
217 wxString s
= _T("Input files: ");
219 size_t count
= parser
.GetParamCount();
220 for ( size_t param
= 0; param
< count
; param
++ )
222 s
<< parser
.GetParam(param
) << ' ';
226 << _T("Verbose:\t") << (parser
.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
227 << _T("Quiet:\t") << (parser
.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
232 if ( parser
.Found(_T("o"), &strVal
) )
233 s
<< _T("Output file:\t") << strVal
<< '\n';
234 if ( parser
.Found(_T("i"), &strVal
) )
235 s
<< _T("Input dir:\t") << strVal
<< '\n';
236 if ( parser
.Found(_T("s"), &lVal
) )
237 s
<< _T("Size:\t") << lVal
<< '\n';
238 if ( parser
.Found(_T("d"), &dt
) )
239 s
<< _T("Date:\t") << dt
.FormatISODate() << '\n';
240 if ( parser
.Found(_T("project_name"), &strVal
) )
241 s
<< _T("Project:\t") << strVal
<< '\n';
246 #endif // wxUSE_CMDLINE_PARSER
248 static void TestCmdLineConvert()
250 static const wxChar
*cmdlines
[] =
253 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
254 _T("literal \\\" and \"\""),
257 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
259 const wxChar
*cmdline
= cmdlines
[n
];
260 wxPrintf(_T("Parsing: %s\n"), cmdline
);
261 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
263 size_t count
= args
.GetCount();
264 wxPrintf(_T("\targc = %u\n"), count
);
265 for ( size_t arg
= 0; arg
< count
; arg
++ )
267 wxPrintf(_T("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
272 #endif // TEST_CMDLINE
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
283 static const wxChar
*ROOTDIR
= _T("/");
284 static const wxChar
*TESTDIR
= _T("/usr/local/share");
285 #elif defined(__WXMSW__)
286 static const wxChar
*ROOTDIR
= _T("c:\\");
287 static const wxChar
*TESTDIR
= _T("d:\\");
289 #error "don't know where the root directory is"
292 static void TestDirEnumHelper(wxDir
& dir
,
293 int flags
= wxDIR_DEFAULT
,
294 const wxString
& filespec
= wxEmptyString
)
298 if ( !dir
.IsOpened() )
301 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
304 wxPrintf(_T("\t%s\n"), filename
.c_str());
306 cont
= dir
.GetNext(&filename
);
312 static void TestDirEnum()
314 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
316 wxString cwd
= wxGetCwd();
317 if ( !wxDir::Exists(cwd
) )
319 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
324 if ( !dir
.IsOpened() )
326 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
330 wxPuts(_T("Enumerating everything in current directory:"));
331 TestDirEnumHelper(dir
);
333 wxPuts(_T("Enumerating really everything in current directory:"));
334 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
336 wxPuts(_T("Enumerating object files in current directory:"));
337 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, _T("*.o*"));
339 wxPuts(_T("Enumerating directories in current directory:"));
340 TestDirEnumHelper(dir
, wxDIR_DIRS
);
342 wxPuts(_T("Enumerating files in current directory:"));
343 TestDirEnumHelper(dir
, wxDIR_FILES
);
345 wxPuts(_T("Enumerating files including hidden in current directory:"));
346 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
350 wxPuts(_T("Enumerating everything in root directory:"));
351 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
353 wxPuts(_T("Enumerating directories in root directory:"));
354 TestDirEnumHelper(dir
, wxDIR_DIRS
);
356 wxPuts(_T("Enumerating files in root directory:"));
357 TestDirEnumHelper(dir
, wxDIR_FILES
);
359 wxPuts(_T("Enumerating files including hidden in root directory:"));
360 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
362 wxPuts(_T("Enumerating files in non existing directory:"));
363 wxDir
dirNo(_T("nosuchdir"));
364 TestDirEnumHelper(dirNo
);
367 class DirPrintTraverser
: public wxDirTraverser
370 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
372 return wxDIR_CONTINUE
;
375 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
377 wxString path
, name
, ext
;
378 wxSplitPath(dirname
, &path
, &name
, &ext
);
381 name
<< _T('.') << ext
;
384 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
386 if ( wxIsPathSeparator(*p
) )
390 wxPrintf(_T("%s%s\n"), indent
.c_str(), name
.c_str());
392 return wxDIR_CONTINUE
;
396 static void TestDirTraverse()
398 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
402 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
403 wxPrintf(_T("There are %u files under '%s'\n"), n
, TESTDIR
);
406 wxPrintf(_T("First one is '%s'\n"), files
[0u].c_str());
407 wxPrintf(_T(" last one is '%s'\n"), files
[n
- 1].c_str());
410 // enum again with custom traverser
411 wxPuts(_T("Now enumerating directories:"));
413 DirPrintTraverser traverser
;
414 dir
.Traverse(traverser
, _T(""), wxDIR_DIRS
| wxDIR_HIDDEN
);
417 static void TestDirExists()
419 wxPuts(_T("*** Testing wxDir::Exists() ***"));
421 static const wxChar
*dirnames
[] =
424 #if defined(__WXMSW__)
427 _T("\\\\share\\file"),
431 _T("c:\\autoexec.bat"),
432 #elif defined(__UNIX__)
441 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
443 wxPrintf(_T("%-40s: %s\n"),
445 wxDir::Exists(dirnames
[n
]) ? _T("exists")
446 : _T("doesn't exist"));
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 #ifdef TEST_DLLLOADER
458 #include "wx/dynlib.h"
460 static void TestDllLoad()
462 #if defined(__WXMSW__)
463 static const wxChar
*LIB_NAME
= _T("kernel32.dll");
464 static const wxChar
*FUNC_NAME
= _T("lstrlenA");
465 #elif defined(__UNIX__)
466 // weird: using just libc.so does *not* work!
467 static const wxChar
*LIB_NAME
= _T("/lib/libc-2.0.7.so");
468 static const wxChar
*FUNC_NAME
= _T("strlen");
470 #error "don't know how to test wxDllLoader on this platform"
473 wxPuts(_T("*** testing wxDllLoader ***\n"));
475 wxDynamicLibrary
lib(LIB_NAME
);
476 if ( !lib
.IsLoaded() )
478 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME
);
482 typedef int (*wxStrlenType
)(const char *);
483 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
486 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
487 FUNC_NAME
, LIB_NAME
);
491 if ( pfnStrlen("foo") != 3 )
493 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
497 wxPuts(_T("... ok"));
503 #endif // TEST_DLLLOADER
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
511 #include "wx/utils.h"
513 static wxString
MyGetEnv(const wxString
& var
)
516 if ( !wxGetEnv(var
, &val
) )
519 val
= wxString(_T('\'')) + val
+ _T('\'');
524 static void TestEnvironment()
526 const wxChar
*var
= _T("wxTestVar");
528 wxPuts(_T("*** testing environment access functions ***"));
530 wxPrintf(_T("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
531 wxSetEnv(var
, _T("value for wxTestVar"));
532 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
533 wxSetEnv(var
, _T("another value"));
534 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
536 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
537 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
540 #endif // TEST_ENVIRON
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
548 #include "wx/utils.h"
550 static void TestExecute()
552 wxPuts(_T("*** testing wxExecute ***"));
555 #define COMMAND "cat -n ../../Makefile" // "echo hi"
556 #define SHELL_COMMAND "echo hi from shell"
557 #define REDIRECT_COMMAND COMMAND // "date"
558 #elif defined(__WXMSW__)
559 #define COMMAND "command.com /c echo hi"
560 #define SHELL_COMMAND "echo hi"
561 #define REDIRECT_COMMAND COMMAND
563 #error "no command to exec"
566 wxPrintf(_T("Testing wxShell: "));
568 if ( wxShell(_T(SHELL_COMMAND
)) )
571 wxPuts(_T("ERROR."));
573 wxPrintf(_T("Testing wxExecute: "));
575 if ( wxExecute(_T(COMMAND
), true /* sync */) == 0 )
578 wxPuts(_T("ERROR."));
580 #if 0 // no, it doesn't work (yet?)
581 wxPrintf(_T("Testing async wxExecute: "));
583 if ( wxExecute(COMMAND
) != 0 )
584 wxPuts(_T("Ok (command launched)."));
586 wxPuts(_T("ERROR."));
589 wxPrintf(_T("Testing wxExecute with redirection:\n"));
590 wxArrayString output
;
591 if ( wxExecute(_T(REDIRECT_COMMAND
), output
) != 0 )
593 wxPuts(_T("ERROR."));
597 size_t count
= output
.GetCount();
598 for ( size_t n
= 0; n
< count
; n
++ )
600 wxPrintf(_T("\t%s\n"), output
[n
].c_str());
607 #endif // TEST_EXECUTE
609 // ----------------------------------------------------------------------------
611 // ----------------------------------------------------------------------------
616 #include "wx/ffile.h"
617 #include "wx/textfile.h"
619 static void TestFileRead()
621 wxPuts(_T("*** wxFile read test ***"));
623 wxFile
file(_T("testdata.fc"));
624 if ( file
.IsOpened() )
626 wxPrintf(_T("File length: %lu\n"), file
.Length());
628 wxPuts(_T("File dump:\n----------"));
630 static const off_t len
= 1024;
634 off_t nRead
= file
.Read(buf
, len
);
635 if ( nRead
== wxInvalidOffset
)
637 wxPrintf(_T("Failed to read the file."));
641 fwrite(buf
, nRead
, 1, stdout
);
647 wxPuts(_T("----------"));
651 wxPrintf(_T("ERROR: can't open test file.\n"));
657 static void TestTextFileRead()
659 wxPuts(_T("*** wxTextFile read test ***"));
661 wxTextFile
file(_T("testdata.fc"));
664 wxPrintf(_T("Number of lines: %u\n"), file
.GetLineCount());
665 wxPrintf(_T("Last line: '%s'\n"), file
.GetLastLine().c_str());
669 wxPuts(_T("\nDumping the entire file:"));
670 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
672 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
674 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
676 wxPuts(_T("\nAnd now backwards:"));
677 for ( s
= file
.GetLastLine();
678 file
.GetCurrentLine() != 0;
679 s
= file
.GetPrevLine() )
681 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
683 wxPrintf(_T("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
687 wxPrintf(_T("ERROR: can't open '%s'\n"), file
.GetName());
693 static void TestFileCopy()
695 wxPuts(_T("*** Testing wxCopyFile ***"));
697 static const wxChar
*filename1
= _T("testdata.fc");
698 static const wxChar
*filename2
= _T("test2");
699 if ( !wxCopyFile(filename1
, filename2
) )
701 wxPuts(_T("ERROR: failed to copy file"));
705 wxFFile
f1(filename1
, _T("rb")),
706 f2(filename2
, _T("rb"));
708 if ( !f1
.IsOpened() || !f2
.IsOpened() )
710 wxPuts(_T("ERROR: failed to open file(s)"));
715 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
717 wxPuts(_T("ERROR: failed to read file(s)"));
721 if ( (s1
.length() != s2
.length()) ||
722 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
724 wxPuts(_T("ERROR: copy error!"));
728 wxPuts(_T("File was copied ok."));
734 if ( !wxRemoveFile(filename2
) )
736 wxPuts(_T("ERROR: failed to remove the file"));
744 // ----------------------------------------------------------------------------
746 // ----------------------------------------------------------------------------
750 #include "wx/confbase.h"
751 #include "wx/fileconf.h"
753 static const struct FileConfTestData
755 const wxChar
*name
; // value name
756 const wxChar
*value
; // the value from the file
759 { _T("value1"), _T("one") },
760 { _T("value2"), _T("two") },
761 { _T("novalue"), _T("default") },
764 static void TestFileConfRead()
766 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
768 wxFileConfig
fileconf(_T("test"), wxEmptyString
,
769 _T("testdata.fc"), wxEmptyString
,
770 wxCONFIG_USE_RELATIVE_PATH
);
772 // test simple reading
773 wxPuts(_T("\nReading config file:"));
774 wxString
defValue(_T("default")), value
;
775 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
777 const FileConfTestData
& data
= fcTestData
[n
];
778 value
= fileconf
.Read(data
.name
, defValue
);
779 wxPrintf(_T("\t%s = %s "), data
.name
, value
.c_str());
780 if ( value
== data
.value
)
786 wxPrintf(_T("(ERROR: should be %s)\n"), data
.value
);
790 // test enumerating the entries
791 wxPuts(_T("\nEnumerating all root entries:"));
794 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
797 wxPrintf(_T("\t%s = %s\n"),
799 fileconf
.Read(name
.c_str(), _T("ERROR")).c_str());
801 cont
= fileconf
.GetNextEntry(name
, dummy
);
804 static const wxChar
*testEntry
= _T("TestEntry");
805 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
806 fileconf
.Write(testEntry
, _T("A value"));
807 fileconf
.DeleteEntry(testEntry
);
808 wxPrintf(fileconf
.HasEntry(testEntry
) ? _T("ERROR\n") : _T("ok\n"));
811 #endif // TEST_FILECONF
813 // ----------------------------------------------------------------------------
815 // ----------------------------------------------------------------------------
819 #include "wx/filename.h"
821 static void DumpFileName(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 static void TestHash()
1151 wxPuts(_T("*** Testing wxHashTable ***\n"));
1154 wxHashTable
hash(wxKEY_INTEGER
, 10), hash2(wxKEY_STRING
);
1158 for ( i
= 0; i
< 100; ++i
)
1159 hash
.Put(i
, &o
+ i
);
1162 wxHashTable::compatibility_iterator it
= hash
.Next();
1172 wxPuts(_T("Error in wxHashTable::compatibility_iterator\n"));
1174 for ( i
= 99; i
>= 0; --i
)
1175 if( hash
.Get(i
) != &o
+ i
)
1176 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1178 for ( i
= 0; i
< 100; ++i
)
1179 hash
.Put(i
, &o
+ i
+ 20);
1181 for ( i
= 99; i
>= 0; --i
)
1182 if( hash
.Get(i
) != &o
+ i
)
1183 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1185 for ( i
= 0; i
< 50; ++i
)
1186 if( hash
.Delete(i
) != &o
+ i
)
1187 wxPuts(_T("Error in wxHashTable::Delete\n"));
1189 for ( i
= 50; i
< 100; ++i
)
1190 if( hash
.Get(i
) != &o
+ i
)
1191 wxPuts(_T("Error (3) in wxHashTable::Get/Put\n"));
1193 for ( i
= 0; i
< 50; ++i
)
1194 if( hash
.Get(i
) != &o
+ i
+ 20)
1195 wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n"));
1197 for ( i
= 0; i
< 50; ++i
)
1198 if( hash
.Delete(i
) != &o
+ i
+ 20)
1199 wxPuts(_T("Error (2) in wxHashTable::Delete\n"));
1201 for ( i
= 0; i
< 50; ++i
)
1202 if( hash
.Get(i
) != NULL
)
1203 wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n"));
1205 hash2
.Put(_T("foo"), &o
+ 1);
1206 hash2
.Put(_T("bar"), &o
+ 2);
1207 hash2
.Put(_T("baz"), &o
+ 3);
1209 if (hash2
.Get(_T("moo")) != NULL
)
1210 wxPuts(_T("Error in wxHashTable::Get\n"));
1212 if (hash2
.Get(_T("bar")) != &o
+ 2)
1213 wxPuts(_T("Error in wxHashTable::Get/Put\n"));
1215 hash2
.Put(_T("bar"), &o
+ 0);
1217 if (hash2
.Get(_T("bar")) != &o
+ 2)
1218 wxPuts(_T("Error (2) in wxHashTable::Get/Put\n"));
1223 hash
.DeleteContents(true);
1225 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
1226 hash
.GetCount(), Foo::count
);
1228 static const int hashTestData
[] =
1230 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1234 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1236 hash
.Put(hashTestData
[n
], n
, new Foo(n
));
1239 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
1240 hash
.GetCount(), Foo::count
);
1242 wxPuts(_T("Hash access test:"));
1243 for ( n
= 0; n
< WXSIZEOF(hashTestData
); n
++ )
1245 wxPrintf(_T("\tGetting element with key %d, value %d: "),
1246 hashTestData
[n
], n
);
1247 Foo
*foo
= hash
.Get(hashTestData
[n
], n
);
1250 wxPrintf(_T("ERROR, not found.\n"));
1254 wxPrintf(_T("%d (%s)\n"), foo
->n
,
1255 (size_t)foo
->n
== n
? "ok" : "ERROR");
1259 wxPrintf(_T("\nTrying to get an element not in hash: "));
1261 if ( hash
.Get(1234) || hash
.Get(1, 0) )
1263 wxPuts(_T("ERROR: found!"));
1267 wxPuts(_T("ok (not found)"));
1272 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count
);
1273 wxPuts(_T("*** Testing wxHashTable finished ***\n"));
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1284 #include "wx/hashmap.h"
1286 // test compilation of basic map types
1287 WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash
, wxPointerEqual
, myPtrHashMap
);
1288 WX_DECLARE_HASH_MAP( long, long, wxIntegerHash
, wxIntegerEqual
, myLongHashMap
);
1289 WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash
, wxIntegerEqual
,
1290 myUnsignedHashMap
);
1291 WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1293 WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash
, wxIntegerEqual
,
1295 WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1297 WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash
, wxIntegerEqual
,
1301 // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1302 // myStringHashMap );
1303 WX_DECLARE_STRING_HASH_MAP(wxString
, myStringHashMap
);
1305 typedef myStringHashMap::iterator Itor
;
1307 static void TestHashMap()
1309 wxPuts(_T("*** Testing wxHashMap ***\n"));
1310 myStringHashMap
sh(0); // as small as possible
1313 const size_t count
= 10000;
1315 // init with some data
1316 for( i
= 0; i
< count
; ++i
)
1318 buf
.Printf(wxT("%d"), i
);
1319 sh
[buf
] = wxT("A") + buf
+ wxT("C");
1322 // test that insertion worked
1323 if( sh
.size() != count
)
1325 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh
.size(), count
);
1328 for( i
= 0; i
< count
; ++i
)
1330 buf
.Printf(wxT("%d"), i
);
1331 if( sh
[buf
] != wxT("A") + buf
+ wxT("C") )
1333 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
1338 // check that iterators work
1340 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1344 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
1348 if( it
->second
!= sh
[it
->first
] )
1350 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
1355 if( sh
.size() != i
)
1357 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i
, count
);
1360 // test copy ctor, assignment operator
1361 myStringHashMap
h1( sh
), h2( 0 );
1364 for( i
= 0, it
= sh
.begin(); it
!= sh
.end(); ++it
, ++i
)
1366 if( h1
[it
->first
] != it
->second
)
1368 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it
->first
.c_str());
1371 if( h2
[it
->first
] != it
->second
)
1373 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it
->first
.c_str());
1378 for( i
= 0; i
< count
; ++i
)
1380 buf
.Printf(wxT("%d"), i
);
1381 size_t sz
= sh
.size();
1383 // test find() and erase(it)
1386 it
= sh
.find( buf
);
1387 if( it
!= sh
.end() )
1391 if( sh
.find( buf
) != sh
.end() )
1393 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1397 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i
);
1402 size_t c
= sh
.erase( buf
);
1404 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
1406 if( sh
.find( buf
) != sh
.end() )
1408 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i
);
1412 // count should decrease
1413 if( sh
.size() != sz
- 1 )
1415 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
1419 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
1422 #endif // TEST_HASHMAP
1424 // ----------------------------------------------------------------------------
1426 // ----------------------------------------------------------------------------
1430 #include "wx/hashset.h"
1432 // test compilation of basic map types
1433 WX_DECLARE_HASH_SET( int*, wxPointerHash
, wxPointerEqual
, myPtrHashSet
);
1434 WX_DECLARE_HASH_SET( long, wxIntegerHash
, wxIntegerEqual
, myLongHashSet
);
1435 WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash
, wxIntegerEqual
,
1436 myUnsignedHashSet
);
1437 WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash
, wxIntegerEqual
,
1439 WX_DECLARE_HASH_SET( int, wxIntegerHash
, wxIntegerEqual
,
1441 WX_DECLARE_HASH_SET( short, wxIntegerHash
, wxIntegerEqual
,
1443 WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash
, wxIntegerEqual
,
1445 WX_DECLARE_HASH_SET( wxString
, wxStringHash
, wxStringEqual
,
1457 unsigned long operator()(const MyStruct
& s
) const
1458 { return m_dummy(s
.ptr
); }
1459 MyHash
& operator=(const MyHash
&) { return *this; }
1461 wxPointerHash m_dummy
;
1467 bool operator()(const MyStruct
& s1
, const MyStruct
& s2
) const
1468 { return s1
.ptr
== s2
.ptr
; }
1469 MyEqual
& operator=(const MyEqual
&) { return *this; }
1472 WX_DECLARE_HASH_SET( MyStruct
, MyHash
, MyEqual
, mySet
);
1474 typedef myTestHashSet5 wxStringHashSet
;
1476 static void TestHashSet()
1478 wxPrintf(_T("*** Testing wxHashSet ***\n"));
1480 wxStringHashSet set1
;
1482 set1
.insert( _T("abc") );
1483 set1
.insert( _T("bbc") );
1484 set1
.insert( _T("cbc") );
1485 set1
.insert( _T("abc") );
1487 if( set1
.size() != 3 )
1488 wxPrintf(_T("*** ERROR IN INSERT ***\n"));
1494 tmp
.ptr
= &dummy
; tmp
.str
= _T("ABC");
1496 tmp
.ptr
= &dummy
+ 1;
1498 tmp
.ptr
= &dummy
; tmp
.str
= _T("CDE");
1501 if( set2
.size() != 2 )
1502 wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n"));
1504 mySet::iterator it
= set2
.find( tmp
);
1506 if( it
== set2
.end() )
1507 wxPrintf(_T("*** ERROR IN FIND - 1 ***\n"));
1508 if( it
->ptr
!= &dummy
)
1509 wxPrintf(_T("*** ERROR IN FIND - 2 ***\n"));
1510 if( it
->str
!= _T("ABC") )
1511 wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n"));
1513 wxPrintf(_T("*** Finished testing wxHashSet ***\n"));
1516 #endif // TEST_HASHSET
1518 // ----------------------------------------------------------------------------
1520 // ----------------------------------------------------------------------------
1524 #include "wx/list.h"
1526 WX_DECLARE_LIST(Bar
, wxListBars
);
1527 #include "wx/listimpl.cpp"
1528 WX_DEFINE_LIST(wxListBars
);
1530 WX_DECLARE_LIST(int, wxListInt
);
1531 WX_DEFINE_LIST(wxListInt
);
1533 static void TestList()
1535 wxPuts(_T("*** Testing wxList operations ***\n"));
1541 for ( i
= 0; i
< 5; ++i
)
1542 list1
.Append(dummy
+ i
);
1544 if ( list1
.GetCount() != 5 )
1545 wxPuts(_T("Wrong number of items in list\n"));
1547 if ( list1
.Item(3)->GetData() != dummy
+ 3 )
1548 wxPuts(_T("Error in Item()\n"));
1550 if ( !list1
.Find(dummy
+ 4) )
1551 wxPuts(_T("Error in Find()\n"));
1553 wxListInt::compatibility_iterator node
= list1
.GetFirst();
1558 if ( node
->GetData() != dummy
+ i
)
1559 wxPuts(_T("Error in compatibility_iterator\n"));
1560 node
= node
->GetNext();
1564 if ( size_t(i
) != list1
.GetCount() )
1565 wxPuts(_T("Error in compatibility_iterator\n"));
1567 list1
.Insert(dummy
+ 0);
1568 list1
.Insert(1, dummy
+ 1);
1569 list1
.Insert(list1
.GetFirst()->GetNext()->GetNext(), dummy
+ 2);
1571 node
= list1
.GetFirst();
1576 int* t
= node
->GetData();
1577 if ( t
!= dummy
+ i
)
1578 wxPuts(_T("Error in Insert\n"));
1579 node
= node
->GetNext();
1584 wxPuts(_T("*** Testing wxList operations finished ***\n"));
1586 wxPuts(_T("*** Testing std::list operations ***\n"));
1590 wxListInt::iterator it
, en
;
1591 wxListInt::reverse_iterator rit
, ren
;
1593 for ( i
= 0; i
< 5; ++i
)
1594 list1
.push_back(i
+ &i
);
1596 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
1597 it
!= en
; ++it
, ++i
)
1598 if ( *it
!= i
+ &i
)
1599 wxPuts(_T("Error in iterator\n"));
1601 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
1602 rit
!= ren
; ++rit
, --i
)
1603 if ( *rit
!= i
+ &i
)
1604 wxPuts(_T("Error in reverse_iterator\n"));
1606 if ( *list1
.rbegin() != *--list1
.end() ||
1607 *list1
.begin() != *--list1
.rend() )
1608 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1609 if ( *list1
.begin() != *--++list1
.begin() ||
1610 *list1
.rbegin() != *--++list1
.rbegin() )
1611 wxPuts(_T("Error in iterator/reverse_iterator\n"));
1613 if ( list1
.front() != &i
|| list1
.back() != &i
+ 4 )
1614 wxPuts(_T("Error in front()/back()\n"));
1616 list1
.erase(list1
.begin());
1617 list1
.erase(--list1
.end());
1619 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
1620 it
!= en
; ++it
, ++i
)
1621 if ( *it
!= i
+ &i
)
1622 wxPuts(_T("Error in erase()\n"));
1625 wxPuts(_T("*** Testing std::list operations finished ***\n"));
1628 static void TestListCtor()
1630 wxPuts(_T("*** Testing wxList construction ***\n"));
1634 list1
.Append(new Bar(_T("first")));
1635 list1
.Append(new Bar(_T("second")));
1637 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
1638 list1
.GetCount(), Bar::GetNumber());
1643 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
1644 list1
.GetCount(), list2
.GetCount(), Bar::GetNumber());
1647 list1
.DeleteContents(true);
1649 WX_CLEAR_LIST(wxListBars
, list1
);
1653 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
1658 // ----------------------------------------------------------------------------
1660 // ----------------------------------------------------------------------------
1664 #include "wx/intl.h"
1665 #include "wx/utils.h" // for wxSetEnv
1667 static wxLocale
gs_localeDefault(wxLANGUAGE_ENGLISH
);
1669 // find the name of the language from its value
1670 static const wxChar
*GetLangName(int lang
)
1672 static const wxChar
*languageNames
[] =
1682 _T("ARABIC_ALGERIA"),
1683 _T("ARABIC_BAHRAIN"),
1686 _T("ARABIC_JORDAN"),
1687 _T("ARABIC_KUWAIT"),
1688 _T("ARABIC_LEBANON"),
1690 _T("ARABIC_MOROCCO"),
1693 _T("ARABIC_SAUDI_ARABIA"),
1696 _T("ARABIC_TUNISIA"),
1703 _T("AZERI_CYRILLIC"),
1718 _T("CHINESE_SIMPLIFIED"),
1719 _T("CHINESE_TRADITIONAL"),
1720 _T("CHINESE_HONGKONG"),
1721 _T("CHINESE_MACAU"),
1722 _T("CHINESE_SINGAPORE"),
1723 _T("CHINESE_TAIWAN"),
1729 _T("DUTCH_BELGIAN"),
1733 _T("ENGLISH_AUSTRALIA"),
1734 _T("ENGLISH_BELIZE"),
1735 _T("ENGLISH_BOTSWANA"),
1736 _T("ENGLISH_CANADA"),
1737 _T("ENGLISH_CARIBBEAN"),
1738 _T("ENGLISH_DENMARK"),
1740 _T("ENGLISH_JAMAICA"),
1741 _T("ENGLISH_NEW_ZEALAND"),
1742 _T("ENGLISH_PHILIPPINES"),
1743 _T("ENGLISH_SOUTH_AFRICA"),
1744 _T("ENGLISH_TRINIDAD"),
1745 _T("ENGLISH_ZIMBABWE"),
1753 _T("FRENCH_BELGIAN"),
1754 _T("FRENCH_CANADIAN"),
1755 _T("FRENCH_LUXEMBOURG"),
1756 _T("FRENCH_MONACO"),
1762 _T("GERMAN_AUSTRIAN"),
1763 _T("GERMAN_BELGIUM"),
1764 _T("GERMAN_LIECHTENSTEIN"),
1765 _T("GERMAN_LUXEMBOURG"),
1783 _T("ITALIAN_SWISS"),
1788 _T("KASHMIRI_INDIA"),
1806 _T("MALAY_BRUNEI_DARUSSALAM"),
1807 _T("MALAY_MALAYSIA"),
1817 _T("NORWEGIAN_BOKMAL"),
1818 _T("NORWEGIAN_NYNORSK"),
1825 _T("PORTUGUESE_BRAZILIAN"),
1828 _T("RHAETO_ROMANCE"),
1831 _T("RUSSIAN_UKRAINE"),
1837 _T("SERBIAN_CYRILLIC"),
1838 _T("SERBIAN_LATIN"),
1839 _T("SERBO_CROATIAN"),
1850 _T("SPANISH_ARGENTINA"),
1851 _T("SPANISH_BOLIVIA"),
1852 _T("SPANISH_CHILE"),
1853 _T("SPANISH_COLOMBIA"),
1854 _T("SPANISH_COSTA_RICA"),
1855 _T("SPANISH_DOMINICAN_REPUBLIC"),
1856 _T("SPANISH_ECUADOR"),
1857 _T("SPANISH_EL_SALVADOR"),
1858 _T("SPANISH_GUATEMALA"),
1859 _T("SPANISH_HONDURAS"),
1860 _T("SPANISH_MEXICAN"),
1861 _T("SPANISH_MODERN"),
1862 _T("SPANISH_NICARAGUA"),
1863 _T("SPANISH_PANAMA"),
1864 _T("SPANISH_PARAGUAY"),
1866 _T("SPANISH_PUERTO_RICO"),
1867 _T("SPANISH_URUGUAY"),
1869 _T("SPANISH_VENEZUELA"),
1873 _T("SWEDISH_FINLAND"),
1891 _T("URDU_PAKISTAN"),
1893 _T("UZBEK_CYRILLIC"),
1906 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1907 return languageNames
[lang
];
1909 return _T("INVALID");
1912 static void TestDefaultLang()
1914 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1916 static const wxChar
*langStrings
[] =
1918 NULL
, // system default
1925 _T("de_DE.iso88591"),
1927 _T("?"), // invalid lang spec
1928 _T("klingonese"), // I bet on some systems it does exist...
1931 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1932 wxLocale::GetSystemEncodingName().c_str(),
1933 wxLocale::GetSystemEncoding());
1935 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1937 const wxChar
*langStr
= langStrings
[n
];
1940 // FIXME: this doesn't do anything at all under Windows, we need
1941 // to create a new wxLocale!
1942 wxSetEnv(_T("LC_ALL"), langStr
);
1945 int lang
= gs_localeDefault
.GetSystemLanguage();
1946 wxPrintf(_T("Locale for '%s' is %s.\n"),
1947 langStr
? langStr
: _T("system default"), GetLangName(lang
));
1951 #endif // TEST_LOCALE
1953 // ----------------------------------------------------------------------------
1955 // ----------------------------------------------------------------------------
1959 #include "wx/mimetype.h"
1961 static void TestMimeEnum()
1963 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1965 wxArrayString mimetypes
;
1967 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1969 wxPrintf(_T("*** All %u known filetypes: ***\n"), count
);
1974 for ( size_t n
= 0; n
< count
; n
++ )
1976 wxFileType
*filetype
=
1977 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1980 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1981 mimetypes
[n
].c_str());
1985 filetype
->GetDescription(&desc
);
1986 filetype
->GetExtensions(exts
);
1988 filetype
->GetIcon(NULL
);
1991 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1994 extsAll
<< _T(", ");
1998 wxPrintf(_T("\t%s: %s (%s)\n"),
1999 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
2005 static void TestMimeOverride()
2007 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
2009 static const wxChar
*mailcap
= _T("/tmp/mailcap");
2010 static const wxChar
*mimetypes
= _T("/tmp/mime.types");
2012 if ( wxFile::Exists(mailcap
) )
2013 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
2015 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? _T("ok") : _T("ERROR"));
2017 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
2020 if ( wxFile::Exists(mimetypes
) )
2021 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
2023 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? _T("ok") : _T("ERROR"));
2025 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
2031 static void TestMimeFilename()
2033 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
2035 static const wxChar
*filenames
[] =
2043 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
2045 const wxString fname
= filenames
[n
];
2046 wxString ext
= fname
.AfterLast(_T('.'));
2047 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
2050 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
2055 if ( !ft
->GetDescription(&desc
) )
2056 desc
= _T("<no description>");
2059 if ( !ft
->GetOpenCommand(&cmd
,
2060 wxFileType::MessageParameters(fname
, _T(""))) )
2061 cmd
= _T("<no command available>");
2063 cmd
= wxString(_T('"')) + cmd
+ _T('"');
2065 wxPrintf(_T("To open %s (%s) do %s.\n"),
2066 fname
.c_str(), desc
.c_str(), cmd
.c_str());
2075 static void TestMimeAssociate()
2077 wxPuts(_T("*** Testing creation of filetype association ***\n"));
2079 wxFileTypeInfo
ftInfo(
2080 _T("application/x-xyz"),
2081 _T("xyzview '%s'"), // open cmd
2082 _T(""), // print cmd
2083 _T("XYZ File"), // description
2084 _T(".xyz"), // extensions
2085 NULL
// end of extensions
2087 ftInfo
.SetShortDesc(_T("XYZFile")); // used under Win32 only
2089 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
2092 wxPuts(_T("ERROR: failed to create association!"));
2096 // TODO: read it back
2105 // ----------------------------------------------------------------------------
2106 // misc information functions
2107 // ----------------------------------------------------------------------------
2109 #ifdef TEST_INFO_FUNCTIONS
2111 #include "wx/utils.h"
2113 static void TestDiskInfo()
2115 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
2119 wxChar pathname
[128];
2120 wxPrintf(_T("\nEnter a directory name: "));
2121 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
2124 // kill the last '\n'
2125 pathname
[wxStrlen(pathname
) - 1] = 0;
2127 wxLongLong total
, free
;
2128 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
2130 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
2134 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
2135 (total
/ 1024).ToString().c_str(),
2136 (free
/ 1024).ToString().c_str(),
2142 static void TestOsInfo()
2144 wxPuts(_T("*** Testing OS info functions ***\n"));
2147 wxGetOsVersion(&major
, &minor
);
2148 wxPrintf(_T("Running under: %s, version %d.%d\n"),
2149 wxGetOsDescription().c_str(), major
, minor
);
2151 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
2153 wxPrintf(_T("Host name is %s (%s).\n"),
2154 wxGetHostName().c_str(), wxGetFullHostName().c_str());
2159 static void TestUserInfo()
2161 wxPuts(_T("*** Testing user info functions ***\n"));
2163 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
2164 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
2165 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
2166 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
2171 #endif // TEST_INFO_FUNCTIONS
2173 // ----------------------------------------------------------------------------
2175 // ----------------------------------------------------------------------------
2177 #ifdef TEST_LONGLONG
2179 #include "wx/longlong.h"
2180 #include "wx/timer.h"
2182 // make a 64 bit number from 4 16 bit ones
2183 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
2185 // get a random 64 bit number
2186 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
2188 static const long testLongs
[] =
2199 #if wxUSE_LONGLONG_WX
2200 inline bool operator==(const wxLongLongWx
& a
, const wxLongLongNative
& b
)
2201 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2202 inline bool operator==(const wxLongLongNative
& a
, const wxLongLongWx
& b
)
2203 { return a
.GetHi() == b
.GetHi() && a
.GetLo() == b
.GetLo(); }
2204 #endif // wxUSE_LONGLONG_WX
2206 static void TestSpeed()
2208 static const long max
= 100000000;
2215 for ( n
= 0; n
< max
; n
++ )
2220 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw
.Time());
2223 #if wxUSE_LONGLONG_NATIVE
2228 for ( n
= 0; n
< max
; n
++ )
2233 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw
.Time());
2235 #endif // wxUSE_LONGLONG_NATIVE
2241 for ( n
= 0; n
< max
; n
++ )
2246 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw
.Time());
2250 static void TestLongLongConversion()
2252 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2256 for ( size_t n
= 0; n
< 100000; n
++ )
2260 #if wxUSE_LONGLONG_NATIVE
2261 wxLongLongNative
b(a
.GetHi(), a
.GetLo());
2263 wxASSERT_MSG( a
== b
, _T("conversions failure") );
2265 wxPuts(_T("Can't do it without native long long type, test skipped."));
2268 #endif // wxUSE_LONGLONG_NATIVE
2270 if ( !(nTested
% 1000) )
2279 wxPuts(_T(" done!"));
2282 static void TestMultiplication()
2284 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2288 for ( size_t n
= 0; n
< 100000; n
++ )
2293 #if wxUSE_LONGLONG_NATIVE
2294 wxLongLongNative
aa(a
.GetHi(), a
.GetLo());
2295 wxLongLongNative
bb(b
.GetHi(), b
.GetLo());
2297 wxASSERT_MSG( a
*b
== aa
*bb
, _T("multiplication failure") );
2298 #else // !wxUSE_LONGLONG_NATIVE
2299 wxPuts(_T("Can't do it without native long long type, test skipped."));
2302 #endif // wxUSE_LONGLONG_NATIVE
2304 if ( !(nTested
% 1000) )
2313 wxPuts(_T(" done!"));
2316 static void TestDivision()
2318 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2322 for ( size_t n
= 0; n
< 100000; n
++ )
2324 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2325 // multiplication will not overflow)
2326 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
2328 // get a random (but non null) long (not wxLongLong for now) to divide
2340 #if wxUSE_LONGLONG_NATIVE
2341 wxLongLongNative
m(ll
.GetHi(), ll
.GetLo());
2343 wxLongLongNative p
= m
/ l
, s
= m
% l
;
2344 wxASSERT_MSG( q
== p
&& r
== s
, _T("division failure") );
2345 #else // !wxUSE_LONGLONG_NATIVE
2346 // verify the result
2347 wxASSERT_MSG( ll
== q
*l
+ r
, "division failure" );
2348 #endif // wxUSE_LONGLONG_NATIVE
2350 if ( !(nTested
% 1000) )
2359 wxPuts(_T(" done!"));
2362 static void TestAddition()
2364 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2368 for ( size_t n
= 0; n
< 100000; n
++ )
2374 #if wxUSE_LONGLONG_NATIVE
2375 wxASSERT_MSG( c
== wxLongLongNative(a
.GetHi(), a
.GetLo()) +
2376 wxLongLongNative(b
.GetHi(), b
.GetLo()),
2377 _T("addition failure") );
2378 #else // !wxUSE_LONGLONG_NATIVE
2379 wxASSERT_MSG( c
- b
== a
, "addition failure" );
2380 #endif // wxUSE_LONGLONG_NATIVE
2382 if ( !(nTested
% 1000) )
2391 wxPuts(_T(" done!"));
2394 static void TestBitOperations()
2396 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2400 for ( size_t n
= 0; n
< 100000; n
++ )
2404 #if wxUSE_LONGLONG_NATIVE
2405 for ( size_t n
= 0; n
< 33; n
++ )
2408 #else // !wxUSE_LONGLONG_NATIVE
2409 wxPuts(_T("Can't do it without native long long type, test skipped."));
2412 #endif // wxUSE_LONGLONG_NATIVE
2414 if ( !(nTested
% 1000) )
2423 wxPuts(_T(" done!"));
2426 static void TestLongLongComparison()
2428 #if wxUSE_LONGLONG_WX
2429 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
2431 static const long ls
[2] =
2437 wxLongLongWx lls
[2];
2441 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2445 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
2447 res
= lls
[m
] > testLongs
[n
];
2448 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
2449 ls
[m
], testLongs
[n
], res
? "true" : "false",
2450 res
== (ls
[m
] > testLongs
[n
]) ? "ok" : "ERROR");
2452 res
= lls
[m
] < testLongs
[n
];
2453 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
2454 ls
[m
], testLongs
[n
], res
? "true" : "false",
2455 res
== (ls
[m
] < testLongs
[n
]) ? "ok" : "ERROR");
2457 res
= lls
[m
] == testLongs
[n
];
2458 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
2459 ls
[m
], testLongs
[n
], res
? "true" : "false",
2460 res
== (ls
[m
] == testLongs
[n
]) ? "ok" : "ERROR");
2463 #endif // wxUSE_LONGLONG_WX
2466 static void TestLongLongToString()
2468 wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
2470 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
2472 wxLongLong ll
= testLongs
[n
];
2473 wxPrintf(_T("%ld == %s\n"), testLongs
[n
], ll
.ToString().c_str());
2476 wxLongLong
ll(0x12345678, 0x87654321);
2477 wxPrintf(_T("0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2480 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll
.ToString().c_str());
2483 static void TestLongLongPrintf()
2485 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2487 #ifdef wxLongLongFmtSpec
2488 wxLongLong ll
= wxLL(0x1234567890abcdef);
2489 wxString s
= wxString::Format(_T("%") wxLongLongFmtSpec
_T("x"), ll
);
2490 wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
2491 s
.c_str(), s
== _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
2492 #else // !wxLongLongFmtSpec
2493 #error "wxLongLongFmtSpec not defined for this compiler/platform"
2500 #endif // TEST_LONGLONG
2502 // ----------------------------------------------------------------------------
2504 // ----------------------------------------------------------------------------
2506 #ifdef TEST_PATHLIST
2509 #define CMD_IN_PATH _T("ls")
2511 #define CMD_IN_PATH _T("command.com")
2514 static void TestPathList()
2516 wxPuts(_T("*** Testing wxPathList ***\n"));
2518 wxPathList pathlist
;
2519 pathlist
.AddEnvList(_T("PATH"));
2520 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
2523 wxPrintf(_T("ERROR: command not found in the path.\n"));
2527 wxPrintf(_T("Command found in the path as '%s'.\n"), path
.c_str());
2531 #endif // TEST_PATHLIST
2533 // ----------------------------------------------------------------------------
2534 // regular expressions
2535 // ----------------------------------------------------------------------------
2539 #include "wx/regex.h"
2541 static void TestRegExCompile()
2543 wxPuts(_T("*** Testing RE compilation ***\n"));
2545 static struct RegExCompTestData
2547 const wxChar
*pattern
;
2549 } regExCompTestData
[] =
2551 { _T("foo"), true },
2552 { _T("foo("), false },
2553 { _T("foo(bar"), false },
2554 { _T("foo(bar)"), true },
2555 { _T("foo["), false },
2556 { _T("foo[bar"), false },
2557 { _T("foo[bar]"), true },
2558 { _T("foo{"), true },
2559 { _T("foo{1"), false },
2560 { _T("foo{bar"), true },
2561 { _T("foo{1}"), true },
2562 { _T("foo{1,2}"), true },
2563 { _T("foo{bar}"), true },
2564 { _T("foo*"), true },
2565 { _T("foo**"), false },
2566 { _T("foo+"), true },
2567 { _T("foo++"), false },
2568 { _T("foo?"), true },
2569 { _T("foo??"), false },
2570 { _T("foo?+"), false },
2574 for ( size_t n
= 0; n
< WXSIZEOF(regExCompTestData
); n
++ )
2576 const RegExCompTestData
& data
= regExCompTestData
[n
];
2577 bool ok
= re
.Compile(data
.pattern
);
2579 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2581 ok
? _T("") : _T("not "),
2582 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2586 static void TestRegExMatch()
2588 wxPuts(_T("*** Testing RE matching ***\n"));
2590 static struct RegExMatchTestData
2592 const wxChar
*pattern
;
2595 } regExMatchTestData
[] =
2597 { _T("foo"), _T("bar"), false },
2598 { _T("foo"), _T("foobar"), true },
2599 { _T("^foo"), _T("foobar"), true },
2600 { _T("^foo"), _T("barfoo"), false },
2601 { _T("bar$"), _T("barbar"), true },
2602 { _T("bar$"), _T("barbar "), false },
2605 for ( size_t n
= 0; n
< WXSIZEOF(regExMatchTestData
); n
++ )
2607 const RegExMatchTestData
& data
= regExMatchTestData
[n
];
2609 wxRegEx
re(data
.pattern
);
2610 bool ok
= re
.Matches(data
.text
);
2612 wxPrintf(_T("'%s' %s %s (%s)\n"),
2614 ok
? _T("matches") : _T("doesn't match"),
2616 ok
== data
.correct
? _T("ok") : _T("ERROR"));
2620 static void TestRegExSubmatch()
2622 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2624 wxRegEx
re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2625 if ( !re
.IsValid() )
2627 wxPuts(_T("ERROR: compilation failed."));
2631 wxString text
= _T("Fri Jul 13 18:37:52 CEST 2001");
2633 if ( !re
.Matches(text
) )
2635 wxPuts(_T("ERROR: match expected."));
2639 wxPrintf(_T("Entire match: %s\n"), re
.GetMatch(text
).c_str());
2641 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2642 re
.GetMatch(text
, 3).c_str(),
2643 re
.GetMatch(text
, 2).c_str(),
2644 re
.GetMatch(text
, 4).c_str(),
2645 re
.GetMatch(text
, 1).c_str());
2649 static void TestRegExReplacement()
2651 wxPuts(_T("*** Testing RE replacement ***"));
2653 static struct RegExReplTestData
2657 const wxChar
*result
;
2659 } regExReplTestData
[] =
2661 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2662 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2663 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2664 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2665 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2666 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2667 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2670 const wxChar
*pattern
= _T("([a-z]+)[^0-9]*([0-9]+)");
2671 wxRegEx
re(pattern
);
2673 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern
);
2675 for ( size_t n
= 0; n
< WXSIZEOF(regExReplTestData
); n
++ )
2677 const RegExReplTestData
& data
= regExReplTestData
[n
];
2679 wxString text
= data
.text
;
2680 size_t nRepl
= re
.Replace(&text
, data
.repl
);
2682 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2683 data
.text
, data
.repl
,
2684 nRepl
, nRepl
== 1 ? _T("") : _T("es"),
2686 if ( text
== data
.result
&& nRepl
== data
.count
)
2692 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2693 data
.count
, data
.result
);
2698 static void TestRegExInteractive()
2700 wxPuts(_T("*** Testing RE interactively ***"));
2704 wxChar pattern
[128];
2705 wxPrintf(_T("\nEnter a pattern: "));
2706 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
2709 // kill the last '\n'
2710 pattern
[wxStrlen(pattern
) - 1] = 0;
2713 if ( !re
.Compile(pattern
) )
2721 wxPrintf(_T("Enter text to match: "));
2722 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
2725 // kill the last '\n'
2726 text
[wxStrlen(text
) - 1] = 0;
2728 if ( !re
.Matches(text
) )
2730 wxPrintf(_T("No match.\n"));
2734 wxPrintf(_T("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
2737 for ( size_t n
= 1; ; n
++ )
2739 if ( !re
.GetMatch(&start
, &len
, n
) )
2744 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2745 n
, wxString(text
+ start
, len
).c_str());
2752 #endif // TEST_REGEX
2754 // ----------------------------------------------------------------------------
2756 // ----------------------------------------------------------------------------
2766 static void TestDbOpen()
2774 // ----------------------------------------------------------------------------
2776 // ----------------------------------------------------------------------------
2779 NB: this stuff was taken from the glibc test suite and modified to build
2780 in wxWindows: if I read the copyright below properly, this shouldn't
2786 #ifdef wxTEST_PRINTF
2787 // use our functions from wxchar.cpp
2791 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2792 // in the tests below
2793 int wxPrintf( const wxChar
*format
, ... );
2794 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
2797 #include "wx/longlong.h"
2801 static void rfg1 (void);
2802 static void rfg2 (void);
2806 fmtchk (const wxChar
*fmt
)
2808 (void) wxPrintf(_T("%s:\t`"), fmt
);
2809 (void) wxPrintf(fmt
, 0x12);
2810 (void) wxPrintf(_T("'\n"));
2814 fmtst1chk (const wxChar
*fmt
)
2816 (void) wxPrintf(_T("%s:\t`"), fmt
);
2817 (void) wxPrintf(fmt
, 4, 0x12);
2818 (void) wxPrintf(_T("'\n"));
2822 fmtst2chk (const wxChar
*fmt
)
2824 (void) wxPrintf(_T("%s:\t`"), fmt
);
2825 (void) wxPrintf(fmt
, 4, 4, 0x12);
2826 (void) wxPrintf(_T("'\n"));
2829 /* This page is covered by the following copyright: */
2831 /* (C) Copyright C E Chew
2833 * Feel free to copy, use and distribute this software provided:
2835 * 1. you do not pretend that you wrote it
2836 * 2. you leave this copyright notice intact.
2840 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2847 /* Formatted Output Test
2849 * This exercises the output formatting code.
2857 wxChar
*prefix
= buf
;
2860 wxPuts(_T("\nFormatted output test"));
2861 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2862 wxStrcpy(prefix
, _T("%"));
2863 for (i
= 0; i
< 2; i
++) {
2864 for (j
= 0; j
< 2; j
++) {
2865 for (k
= 0; k
< 2; k
++) {
2866 for (l
= 0; l
< 2; l
++) {
2867 wxStrcpy(prefix
, _T("%"));
2868 if (i
== 0) wxStrcat(prefix
, _T("-"));
2869 if (j
== 0) wxStrcat(prefix
, _T("+"));
2870 if (k
== 0) wxStrcat(prefix
, _T("#"));
2871 if (l
== 0) wxStrcat(prefix
, _T("0"));
2872 wxPrintf(_T("%5s |"), prefix
);
2873 wxStrcpy(tp
, prefix
);
2874 wxStrcat(tp
, _T("6d |"));
2876 wxStrcpy(tp
, prefix
);
2877 wxStrcat(tp
, _T("6o |"));
2879 wxStrcpy(tp
, prefix
);
2880 wxStrcat(tp
, _T("6x |"));
2882 wxStrcpy(tp
, prefix
);
2883 wxStrcat(tp
, _T("6X |"));
2885 wxStrcpy(tp
, prefix
);
2886 wxStrcat(tp
, _T("6u |"));
2893 wxPrintf(_T("%10s\n"), (wxChar
*) NULL
);
2894 wxPrintf(_T("%-10s\n"), (wxChar
*) NULL
);
2897 static void TestPrintf()
2899 static wxChar shortstr
[] = _T("Hi, Z.");
2900 static wxChar longstr
[] = _T("Good morning, Doctor Chandra. This is Hal. \
2901 I am ready for my first lesson today.");
2906 fmtchk(_T("%4.4x"));
2907 fmtchk(_T("%04.4x"));
2908 fmtchk(_T("%4.3x"));
2909 fmtchk(_T("%04.3x"));
2911 fmtst1chk(_T("%.*x"));
2912 fmtst1chk(_T("%0*x"));
2913 fmtst2chk(_T("%*.*x"));
2914 fmtst2chk(_T("%0*.*x"));
2916 wxPrintf(_T("bad format:\t\"%b\"\n"));
2917 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
2919 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
2920 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
2921 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
2922 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
2923 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
2924 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
2925 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
2926 wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
2927 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
2928 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
2930 wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr
);
2931 wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr
);
2932 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr
);
2933 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
2934 wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar
*)NULL
);
2935 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr
);
2937 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
2938 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
2939 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
2940 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20
);
2941 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
2942 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
2943 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
2944 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
2945 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
2946 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
2947 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
2948 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20
);
2950 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
2951 wxPrintf (_T(" %6.5f\n"), .1);
2952 wxPrintf (_T("x%5.4fx\n"), .5);
2954 wxPrintf (_T("%#03x\n"), 1);
2956 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
2962 while (niter
-- != 0)
2963 wxPrintf (_T("%.17e\n"), d
/ 2);
2967 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
2969 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
2970 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
2971 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
2972 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
2973 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
2974 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
2975 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
2976 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
2977 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
2978 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
2983 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), _T("%30s"), _T("foo"));
2985 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
2986 rc
, WXSIZEOF(buf
), buf
);
2989 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
2990 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
2996 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
2997 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
2998 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
2999 wxPrintf (_T("%g should be 123.456\n"), 123.456);
3000 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
3001 wxPrintf (_T("%g should be 10\n"), 10.0);
3002 wxPrintf (_T("%g should be 0.02\n"), 0.02);
3006 wxPrintf(_T("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
3012 wxSprintf(buf
,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
3014 result
|= wxStrcmp (buf
,
3015 _T("onetwo three "));
3017 wxPuts (result
!= 0 ? _T("Test failed!") : _T("Test ok."));
3024 wxSprintf(buf
, _T("%07") wxLongLongFmtSpec
_T("o"), wxLL(040000000000));
3026 // for some reason below line fails under Borland
3027 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
3030 if (wxStrcmp (buf
, _T("40000000000")) != 0)
3033 wxPuts (_T("\tFAILED"));
3037 #endif // wxLongLong_t
3039 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
3040 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
3042 wxPuts (_T("--- Should be no further output. ---"));
3051 memset (bytes
, '\xff', sizeof bytes
);
3052 wxSprintf (buf
, _T("foo%hhn\n"), &bytes
[3]);
3053 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
3054 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
3056 wxPuts (_T("%hhn overwrite more bytes"));
3061 wxPuts (_T("%hhn wrote incorrect value"));
3073 wxSprintf (buf
, _T("%5.s"), _T("xyz"));
3074 if (wxStrcmp (buf
, _T(" ")) != 0)
3075 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" "));
3076 wxSprintf (buf
, _T("%5.f"), 33.3);
3077 if (wxStrcmp (buf
, _T(" 33")) != 0)
3078 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 33"));
3079 wxSprintf (buf
, _T("%8.e"), 33.3e7
);
3080 if (wxStrcmp (buf
, _T(" 3e+08")) != 0)
3081 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3e+08"));
3082 wxSprintf (buf
, _T("%8.E"), 33.3e7
);
3083 if (wxStrcmp (buf
, _T(" 3E+08")) != 0)
3084 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3E+08"));
3085 wxSprintf (buf
, _T("%.g"), 33.3);
3086 if (wxStrcmp (buf
, _T("3e+01")) != 0)
3087 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3e+01"));
3088 wxSprintf (buf
, _T("%.G"), 33.3);
3089 if (wxStrcmp (buf
, _T("3E+01")) != 0)
3090 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3E+01"));
3100 wxSprintf (buf
, _T("%.*g"), prec
, 3.3);
3101 if (wxStrcmp (buf
, _T("3")) != 0)
3102 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3104 wxSprintf (buf
, _T("%.*G"), prec
, 3.3);
3105 if (wxStrcmp (buf
, _T("3")) != 0)
3106 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T("3"));
3108 wxSprintf (buf
, _T("%7.*G"), prec
, 3.33);
3109 if (wxStrcmp (buf
, _T(" 3")) != 0)
3110 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 3"));
3112 wxSprintf (buf
, _T("%04.*o"), prec
, 33);
3113 if (wxStrcmp (buf
, _T(" 041")) != 0)
3114 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 041"));
3116 wxSprintf (buf
, _T("%09.*u"), prec
, 33);
3117 if (wxStrcmp (buf
, _T(" 0000033")) != 0)
3118 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 0000033"));
3120 wxSprintf (buf
, _T("%04.*x"), prec
, 33);
3121 if (wxStrcmp (buf
, _T(" 021")) != 0)
3122 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3124 wxSprintf (buf
, _T("%04.*X"), prec
, 33);
3125 if (wxStrcmp (buf
, _T(" 021")) != 0)
3126 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf
, _T(" 021"));
3129 #endif // TEST_PRINTF
3131 // ----------------------------------------------------------------------------
3132 // registry and related stuff
3133 // ----------------------------------------------------------------------------
3135 // this is for MSW only
3138 #undef TEST_REGISTRY
3143 #include "wx/confbase.h"
3144 #include "wx/msw/regconf.h"
3146 static void TestRegConfWrite()
3148 wxRegConfig
regconf(_T("console"), _T("wxwindows"));
3149 regconf
.Write(_T("Hello"), wxString(_T("world")));
3152 #endif // TEST_REGCONF
3154 #ifdef TEST_REGISTRY
3156 #include "wx/msw/registry.h"
3158 // I chose this one because I liked its name, but it probably only exists under
3160 static const wxChar
*TESTKEY
=
3161 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
3163 static void TestRegistryRead()
3165 wxPuts(_T("*** testing registry reading ***"));
3167 wxRegKey
key(TESTKEY
);
3168 wxPrintf(_T("The test key name is '%s'.\n"), key
.GetName().c_str());
3171 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
3176 size_t nSubKeys
, nValues
;
3177 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
3179 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
3182 wxPrintf(_T("Enumerating values:\n"));
3186 bool cont
= key
.GetFirstValue(value
, dummy
);
3189 wxPrintf(_T("Value '%s': type "), value
.c_str());
3190 switch ( key
.GetValueType(value
) )
3192 case wxRegKey::Type_None
: wxPrintf(_T("ERROR (none)")); break;
3193 case wxRegKey::Type_String
: wxPrintf(_T("SZ")); break;
3194 case wxRegKey::Type_Expand_String
: wxPrintf(_T("EXPAND_SZ")); break;
3195 case wxRegKey::Type_Binary
: wxPrintf(_T("BINARY")); break;
3196 case wxRegKey::Type_Dword
: wxPrintf(_T("DWORD")); break;
3197 case wxRegKey::Type_Multi_String
: wxPrintf(_T("MULTI_SZ")); break;
3198 default: wxPrintf(_T("other (unknown)")); break;
3201 wxPrintf(_T(", value = "));
3202 if ( key
.IsNumericValue(value
) )
3205 key
.QueryValue(value
, &val
);
3206 wxPrintf(_T("%ld"), val
);
3211 key
.QueryValue(value
, val
);
3212 wxPrintf(_T("'%s'"), val
.c_str());
3214 key
.QueryRawValue(value
, val
);
3215 wxPrintf(_T(" (raw value '%s')"), val
.c_str());
3220 cont
= key
.GetNextValue(value
, dummy
);
3224 static void TestRegistryAssociation()
3227 The second call to deleteself genertaes an error message, with a
3228 messagebox saying .flo is crucial to system operation, while the .ddf
3229 call also fails, but with no error message
3234 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3236 key
= _T("ddxf_auto_file") ;
3237 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3239 key
= _T("ddxf_auto_file") ;
3240 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3242 key
= _T("program,0") ;
3243 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3245 key
= _T("program \"%1\"") ;
3247 key
.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
3249 key
.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
3251 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
3253 key
.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
3257 #endif // TEST_REGISTRY
3259 // ----------------------------------------------------------------------------
3261 // ----------------------------------------------------------------------------
3263 #ifdef TEST_SCOPEGUARD
3265 #include "wx/scopeguard.h"
3267 static void function0() { puts("function0()"); }
3268 static void function1(int n
) { printf("function1(%d)\n", n
); }
3269 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
3273 void method0() { printf("method0()\n"); }
3274 void method1(int n
) { printf("method1(%d)\n", n
); }
3275 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
3278 static void TestScopeGuard()
3280 ON_BLOCK_EXIT0(function0
);
3281 ON_BLOCK_EXIT1(function1
, 17);
3282 ON_BLOCK_EXIT2(function2
, 3.14, 'p');
3285 ON_BLOCK_EXIT_OBJ0(obj
, &Object::method0
);
3286 ON_BLOCK_EXIT_OBJ1(obj
, &Object::method1
, 7);
3287 ON_BLOCK_EXIT_OBJ2(obj
, &Object::method2
, 2.71, 'e');
3289 wxScopeGuard dismissed
= wxMakeGuard(function0
);
3290 dismissed
.Dismiss();
3295 // ----------------------------------------------------------------------------
3297 // ----------------------------------------------------------------------------
3301 #include "wx/socket.h"
3302 #include "wx/protocol/protocol.h"
3303 #include "wx/protocol/http.h"
3305 static void TestSocketServer()
3307 wxPuts(_T("*** Testing wxSocketServer ***\n"));
3309 static const int PORT
= 3000;
3314 wxSocketServer
*server
= new wxSocketServer(addr
);
3315 if ( !server
->Ok() )
3317 wxPuts(_T("ERROR: failed to bind"));
3325 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT
);
3327 wxSocketBase
*socket
= server
->Accept();
3330 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
3334 wxPuts(_T("Server: got a client."));
3336 server
->SetTimeout(60); // 1 min
3339 while ( !close
&& socket
->IsConnected() )
3342 wxChar ch
= _T('\0');
3345 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
3347 // don't log error if the client just close the connection
3348 if ( socket
->IsConnected() )
3350 wxPuts(_T("ERROR: in wxSocket::Read."));
3370 wxPrintf(_T("Server: got '%s'.\n"), s
.c_str());
3371 if ( s
== _T("close") )
3373 wxPuts(_T("Closing connection"));
3377 else if ( s
== _T("quit") )
3382 wxPuts(_T("Shutting down the server"));
3384 else // not a special command
3386 socket
->Write(s
.MakeUpper().c_str(), s
.length());
3387 socket
->Write("\r\n", 2);
3388 wxPrintf(_T("Server: wrote '%s'.\n"), s
.c_str());
3394 wxPuts(_T("Server: lost a client unexpectedly."));
3400 // same as "delete server" but is consistent with GUI programs
3404 static void TestSocketClient()
3406 wxPuts(_T("*** Testing wxSocketClient ***\n"));
3408 static const wxChar
*hostname
= _T("www.wxwindows.org");
3411 addr
.Hostname(hostname
);
3414 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname
);
3416 wxSocketClient client
;
3417 if ( !client
.Connect(addr
) )
3419 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3423 wxPrintf(_T("--- Connected to %s:%u...\n"),
3424 addr
.Hostname().c_str(), addr
.Service());
3428 // could use simply "GET" here I suppose
3430 wxString::Format(_T("GET http://%s/\r\n"), hostname
);
3431 client
.Write(cmdGet
, cmdGet
.length());
3432 wxPrintf(_T("--- Sent command '%s' to the server\n"),
3433 MakePrintable(cmdGet
).c_str());
3434 client
.Read(buf
, WXSIZEOF(buf
));
3435 wxPrintf(_T("--- Server replied:\n%s"), buf
);
3439 #endif // TEST_SOCKETS
3441 // ----------------------------------------------------------------------------
3443 // ----------------------------------------------------------------------------
3447 #include "wx/protocol/ftp.h"
3451 #define FTP_ANONYMOUS
3453 #ifdef FTP_ANONYMOUS
3454 static const wxChar
*directory
= _T("/pub");
3455 static const wxChar
*filename
= _T("welcome.msg");
3457 static const wxChar
*directory
= _T("/etc");
3458 static const wxChar
*filename
= _T("issue");
3461 static bool TestFtpConnect()
3463 wxPuts(_T("*** Testing FTP connect ***"));
3465 #ifdef FTP_ANONYMOUS
3466 static const wxChar
*hostname
= _T("ftp.wxwindows.org");
3468 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
3469 #else // !FTP_ANONYMOUS
3470 static const wxChar
*hostname
= "localhost";
3473 wxFgets(user
, WXSIZEOF(user
), stdin
);
3474 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
3477 wxChar password
[256];
3478 wxPrintf(_T("Password for %s: "), password
);
3479 wxFgets(password
, WXSIZEOF(password
), stdin
);
3480 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
3481 ftp
.SetPassword(password
);
3483 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
3484 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3486 if ( !ftp
.Connect(hostname
) )
3488 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3494 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
3495 hostname
, ftp
.Pwd().c_str());
3501 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3502 static void TestFtpWuFtpd()
3505 static const wxChar
*hostname
= _T("ftp.eudora.com");
3506 if ( !ftp
.Connect(hostname
) )
3508 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname
);
3512 static const wxChar
*filename
= _T("eudora/pubs/draft-gellens-submit-09.txt");
3513 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3516 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3520 size_t size
= in
->GetSize();
3521 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3523 wxChar
*data
= new wxChar
[size
];
3524 if ( !in
->Read(data
, size
) )
3526 wxPuts(_T("ERROR: read error"));
3530 wxPrintf(_T("Successfully retrieved the file.\n"));
3539 static void TestFtpList()
3541 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
3544 if ( !ftp
.ChDir(directory
) )
3546 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3549 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3551 // test NLIST and LIST
3552 wxArrayString files
;
3553 if ( !ftp
.GetFilesList(files
) )
3555 wxPuts(_T("ERROR: failed to get NLIST of files"));
3559 wxPrintf(_T("Brief list of files under '%s':\n"), ftp
.Pwd().c_str());
3560 size_t count
= files
.GetCount();
3561 for ( size_t n
= 0; n
< count
; n
++ )
3563 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3565 wxPuts(_T("End of the file list"));
3568 if ( !ftp
.GetDirList(files
) )
3570 wxPuts(_T("ERROR: failed to get LIST of files"));
3574 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp
.Pwd().c_str());
3575 size_t count
= files
.GetCount();
3576 for ( size_t n
= 0; n
< count
; n
++ )
3578 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3580 wxPuts(_T("End of the file list"));
3583 if ( !ftp
.ChDir(_T("..")) )
3585 wxPuts(_T("ERROR: failed to cd to .."));
3588 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3591 static void TestFtpDownload()
3593 wxPuts(_T("*** Testing wxFTP download ***\n"));
3596 wxInputStream
*in
= ftp
.GetInputStream(filename
);
3599 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename
);
3603 size_t size
= in
->GetSize();
3604 wxPrintf(_T("Reading file %s (%u bytes)..."), filename
, size
);
3607 wxChar
*data
= new wxChar
[size
];
3608 if ( !in
->Read(data
, size
) )
3610 wxPuts(_T("ERROR: read error"));
3614 wxPrintf(_T("\nContents of %s:\n%s\n"), filename
, data
);
3622 static void TestFtpFileSize()
3624 wxPuts(_T("*** Testing FTP SIZE command ***"));
3626 if ( !ftp
.ChDir(directory
) )
3628 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory
);
3631 wxPrintf(_T("Current directory is '%s'\n"), ftp
.Pwd().c_str());
3633 if ( ftp
.FileExists(filename
) )
3635 int size
= ftp
.GetFileSize(filename
);
3637 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename
);
3639 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename
, size
);
3643 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename
);
3647 static void TestFtpMisc()
3649 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
3651 if ( ftp
.SendCommand(_T("STAT")) != '2' )
3653 wxPuts(_T("ERROR: STAT failed"));
3657 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp
.GetLastResult().c_str());
3660 if ( ftp
.SendCommand(_T("HELP SITE")) != '2' )
3662 wxPuts(_T("ERROR: HELP SITE failed"));
3666 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
3667 ftp
.GetLastResult().c_str());
3671 static void TestFtpInteractive()
3673 wxPuts(_T("\n*** Interactive wxFTP test ***"));
3679 wxPrintf(_T("Enter FTP command: "));
3680 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3683 // kill the last '\n'
3684 buf
[wxStrlen(buf
) - 1] = 0;
3686 // special handling of LIST and NLST as they require data connection
3687 wxString
start(buf
, 4);
3689 if ( start
== _T("LIST") || start
== _T("NLST") )
3692 if ( wxStrlen(buf
) > 4 )
3695 wxArrayString files
;
3696 if ( !ftp
.GetList(files
, wildcard
, start
== _T("LIST")) )
3698 wxPrintf(_T("ERROR: failed to get %s of files\n"), start
.c_str());
3702 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
3703 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
3704 size_t count
= files
.GetCount();
3705 for ( size_t n
= 0; n
< count
; n
++ )
3707 wxPrintf(_T("\t%s\n"), files
[n
].c_str());
3709 wxPuts(_T("--- End of the file list"));
3714 wxChar ch
= ftp
.SendCommand(buf
);
3715 wxPrintf(_T("Command %s"), ch
? _T("succeeded") : _T("failed"));
3718 wxPrintf(_T(" (return code %c)"), ch
);
3721 wxPrintf(_T(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
3725 wxPuts(_T("\n*** done ***"));
3728 static void TestFtpUpload()
3730 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
3733 static const wxChar
*file1
= _T("test1");
3734 static const wxChar
*file2
= _T("test2");
3735 wxOutputStream
*out
= ftp
.GetOutputStream(file1
);
3738 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3739 out
->Write("First hello", 11);
3743 // send a command to check the remote file
3744 if ( ftp
.SendCommand(wxString(_T("STAT ")) + file1
) != '2' )
3746 wxPrintf(_T("ERROR: STAT %s failed\n"), file1
);
3750 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
3751 file1
, ftp
.GetLastResult().c_str());
3754 out
= ftp
.GetOutputStream(file2
);
3757 wxPrintf(_T("--- Uploading to %s ---\n"), file1
);
3758 out
->Write("Second hello", 12);
3765 // ----------------------------------------------------------------------------
3767 // ----------------------------------------------------------------------------
3771 #include "wx/wfstream.h"
3772 #include "wx/mstream.h"
3774 static void TestFileStream()
3776 wxPuts(_T("*** Testing wxFileInputStream ***"));
3778 static const wxChar
*filename
= _T("testdata.fs");
3780 wxFileOutputStream
fsOut(filename
);
3781 fsOut
.Write("foo", 3);
3784 wxFileInputStream
fsIn(filename
);
3785 wxPrintf(_T("File stream size: %u\n"), fsIn
.GetSize());
3786 while ( !fsIn
.Eof() )
3788 putchar(fsIn
.GetC());
3791 if ( !wxRemoveFile(filename
) )
3793 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename
);
3796 wxPuts(_T("\n*** wxFileInputStream test done ***"));
3799 static void TestMemoryStream()
3801 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3803 wxMemoryOutputStream memOutStream
;
3804 wxPrintf(_T("Initially out stream offset: %lu\n"),
3805 (unsigned long)memOutStream
.TellO());
3807 for ( const wxChar
*p
= _T("Hello, stream!"); *p
; p
++ )
3809 memOutStream
.PutC(*p
);
3812 wxPrintf(_T("Final out stream offset: %lu\n"),
3813 (unsigned long)memOutStream
.TellO());
3815 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
3818 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
3820 wxMemoryInputStream
memInpStream(buf
, len
);
3821 wxPrintf(_T("Memory stream size: %u\n"), memInpStream
.GetSize());
3822 while ( !memInpStream
.Eof() )
3824 putchar(memInpStream
.GetC());
3827 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
3830 #endif // TEST_STREAMS
3832 // ----------------------------------------------------------------------------
3834 // ----------------------------------------------------------------------------
3838 #include "wx/timer.h"
3839 #include "wx/utils.h"
3841 static void TestStopWatch()
3843 wxPuts(_T("*** Testing wxStopWatch ***\n"));
3847 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
3850 wxPrintf(_T("\t%ldms\n"), sw
.Time());
3852 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
3856 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3859 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
3862 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3865 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
3868 wxPrintf(_T("\telapsed time: %ldms\n"), sw
.Time());
3871 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
3872 for ( size_t n
= 0; n
< 70; n
++ )
3876 for ( size_t m
= 0; m
< 100000; m
++ )
3878 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
3880 wxPuts(_T("\ntime is negative - ERROR!"));
3888 wxPuts(_T(", ok."));
3891 #endif // TEST_TIMER
3893 // ----------------------------------------------------------------------------
3895 // ----------------------------------------------------------------------------
3899 #include "wx/vcard.h"
3901 static void DumpVObject(size_t level
, const wxVCardObject
& vcard
)
3904 wxVCardObject
*vcObj
= vcard
.GetFirstProp(&cookie
);
3907 wxPrintf(_T("%s%s"),
3908 wxString(_T('\t'), level
).c_str(),
3909 vcObj
->GetName().c_str());
3912 switch ( vcObj
->GetType() )
3914 case wxVCardObject::String
:
3915 case wxVCardObject::UString
:
3918 vcObj
->GetValue(&val
);
3919 value
<< _T('"') << val
<< _T('"');
3923 case wxVCardObject::Int
:
3926 vcObj
->GetValue(&i
);
3927 value
.Printf(_T("%u"), i
);
3931 case wxVCardObject::Long
:
3934 vcObj
->GetValue(&l
);
3935 value
.Printf(_T("%lu"), l
);
3939 case wxVCardObject::None
:
3942 case wxVCardObject::Object
:
3943 value
= _T("<node>");
3947 value
= _T("<unknown value type>");
3951 wxPrintf(_T(" = %s"), value
.c_str());
3954 DumpVObject(level
+ 1, *vcObj
);
3957 vcObj
= vcard
.GetNextProp(&cookie
);
3961 static void DumpVCardAddresses(const wxVCard
& vcard
)
3963 wxPuts(_T("\nShowing all addresses from vCard:\n"));
3967 wxVCardAddress
*addr
= vcard
.GetFirstAddress(&cookie
);
3971 int flags
= addr
->GetFlags();
3972 if ( flags
& wxVCardAddress::Domestic
)
3974 flagsStr
<< _T("domestic ");
3976 if ( flags
& wxVCardAddress::Intl
)
3978 flagsStr
<< _T("international ");
3980 if ( flags
& wxVCardAddress::Postal
)
3982 flagsStr
<< _T("postal ");
3984 if ( flags
& wxVCardAddress::Parcel
)
3986 flagsStr
<< _T("parcel ");
3988 if ( flags
& wxVCardAddress::Home
)
3990 flagsStr
<< _T("home ");
3992 if ( flags
& wxVCardAddress::Work
)
3994 flagsStr
<< _T("work ");
3997 wxPrintf(_T("Address %u:\n")
3999 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
4002 addr
->GetPostOffice().c_str(),
4003 addr
->GetExtAddress().c_str(),
4004 addr
->GetStreet().c_str(),
4005 addr
->GetLocality().c_str(),
4006 addr
->GetRegion().c_str(),
4007 addr
->GetPostalCode().c_str(),
4008 addr
->GetCountry().c_str()
4012 addr
= vcard
.GetNextAddress(&cookie
);
4016 static void DumpVCardPhoneNumbers(const wxVCard
& vcard
)
4018 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
4022 wxVCardPhoneNumber
*phone
= vcard
.GetFirstPhoneNumber(&cookie
);
4026 int flags
= phone
->GetFlags();
4027 if ( flags
& wxVCardPhoneNumber::Voice
)
4029 flagsStr
<< _T("voice ");
4031 if ( flags
& wxVCardPhoneNumber::Fax
)
4033 flagsStr
<< _T("fax ");
4035 if ( flags
& wxVCardPhoneNumber::Cellular
)
4037 flagsStr
<< _T("cellular ");
4039 if ( flags
& wxVCardPhoneNumber::Modem
)
4041 flagsStr
<< _T("modem ");
4043 if ( flags
& wxVCardPhoneNumber::Home
)
4045 flagsStr
<< _T("home ");
4047 if ( flags
& wxVCardPhoneNumber::Work
)
4049 flagsStr
<< _T("work ");
4052 wxPrintf(_T("Phone number %u:\n")
4057 phone
->GetNumber().c_str()
4061 phone
= vcard
.GetNextPhoneNumber(&cookie
);
4065 static void TestVCardRead()
4067 wxPuts(_T("*** Testing wxVCard reading ***\n"));
4069 wxVCard
vcard(_T("vcard.vcf"));
4070 if ( !vcard
.IsOk() )
4072 wxPuts(_T("ERROR: couldn't load vCard."));
4076 // read individual vCard properties
4077 wxVCardObject
*vcObj
= vcard
.GetProperty("FN");
4081 vcObj
->GetValue(&value
);
4086 value
= _T("<none>");
4089 wxPrintf(_T("Full name retrieved directly: %s\n"), value
.c_str());
4092 if ( !vcard
.GetFullName(&value
) )
4094 value
= _T("<none>");
4097 wxPrintf(_T("Full name from wxVCard API: %s\n"), value
.c_str());
4099 // now show how to deal with multiply occuring properties
4100 DumpVCardAddresses(vcard
);
4101 DumpVCardPhoneNumbers(vcard
);
4103 // and finally show all
4104 wxPuts(_T("\nNow dumping the entire vCard:\n")
4105 "-----------------------------\n");
4107 DumpVObject(0, vcard
);
4111 static void TestVCardWrite()
4113 wxPuts(_T("*** Testing wxVCard writing ***\n"));
4116 if ( !vcard
.IsOk() )
4118 wxPuts(_T("ERROR: couldn't create vCard."));
4123 vcard
.SetName("Zeitlin", "Vadim");
4124 vcard
.SetFullName("Vadim Zeitlin");
4125 vcard
.SetOrganization("wxWindows", "R&D");
4127 // just dump the vCard back
4128 wxPuts(_T("Entire vCard follows:\n"));
4129 wxPuts(vcard
.Write());
4133 #endif // TEST_VCARD
4135 // ----------------------------------------------------------------------------
4137 // ----------------------------------------------------------------------------
4139 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
4145 #include "wx/volume.h"
4147 static const wxChar
*volumeKinds
[] =
4153 _T("network volume"),
4157 static void TestFSVolume()
4159 wxPuts(_T("*** Testing wxFSVolume class ***"));
4161 wxArrayString volumes
= wxFSVolume::GetVolumes();
4162 size_t count
= volumes
.GetCount();
4166 wxPuts(_T("ERROR: no mounted volumes?"));
4170 wxPrintf(_T("%u mounted volumes found:\n"), count
);
4172 for ( size_t n
= 0; n
< count
; n
++ )
4174 wxFSVolume
vol(volumes
[n
]);
4177 wxPuts(_T("ERROR: couldn't create volume"));
4181 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
4183 vol
.GetDisplayName().c_str(),
4184 vol
.GetName().c_str(),
4185 volumeKinds
[vol
.GetKind()],
4186 vol
.IsWritable() ? _T("rw") : _T("ro"),
4187 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? _T("removable")
4192 #endif // TEST_VOLUME
4194 // ----------------------------------------------------------------------------
4195 // wide char and Unicode support
4196 // ----------------------------------------------------------------------------
4200 static void TestUnicodeToFromAscii()
4202 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
4204 static const char *msg
= "Hello, world!";
4205 wxString s
= wxString::FromAscii(msg
);
4207 wxPrintf(_T("Message in Unicode: %s\n"), s
.c_str());
4208 printf("Message in ASCII: %s\n", (const char *)s
.ToAscii());
4210 wxPutchar(_T('\n'));
4213 #endif // TEST_UNICODE
4217 #include "wx/strconv.h"
4218 #include "wx/fontenc.h"
4219 #include "wx/encconv.h"
4220 #include "wx/buffer.h"
4222 static const unsigned char utf8koi8r
[] =
4224 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
4225 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
4226 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
4227 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
4228 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
4229 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
4230 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
4233 static const unsigned char utf8iso8859_1
[] =
4235 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
4236 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
4237 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
4238 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
4239 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
4242 static const unsigned char utf8Invalid
[] =
4244 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
4245 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
4246 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
4247 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
4251 static const struct Utf8Data
4253 const unsigned char *text
;
4255 const wxChar
*charset
;
4256 wxFontEncoding encoding
;
4259 { utf8Invalid
, WXSIZEOF(utf8Invalid
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4260 { utf8koi8r
, WXSIZEOF(utf8koi8r
), _T("koi8-r"), wxFONTENCODING_KOI8
},
4261 { utf8iso8859_1
, WXSIZEOF(utf8iso8859_1
), _T("iso8859-1"), wxFONTENCODING_ISO8859_1
},
4264 static void TestUtf8()
4266 wxPuts(_T("*** Testing UTF8 support ***\n"));
4271 for ( size_t n
= 0; n
< WXSIZEOF(utf8data
); n
++ )
4273 const Utf8Data
& u8d
= utf8data
[n
];
4274 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)u8d
.text
,
4275 WXSIZEOF(wbuf
)) == (size_t)-1 )
4277 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4281 wxCSConv
conv(u8d
.charset
);
4282 if ( conv
.WC2MB(buf
, wbuf
, WXSIZEOF(buf
)) == (size_t)-1 )
4284 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d
.charset
);
4288 wxPrintf(_T("String in %s: %s\n"), u8d
.charset
, buf
);
4292 wxString
s(wxConvUTF8
.cMB2WC((const char *)u8d
.text
), *wxConvCurrent
);
4294 s
= _T("<< conversion failed >>");
4295 wxPrintf(_T("String in current cset: %s\n"), s
.c_str());
4302 static void TestEncodingConverter()
4304 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
4306 // using wxEncodingConverter should give the same result as above
4309 if ( wxConvUTF8
.MB2WC(wbuf
, (const char *)utf8koi8r
,
4310 WXSIZEOF(utf8koi8r
)) == (size_t)-1 )
4312 wxPuts(_T("ERROR: UTF-8 decoding failed."));
4316 wxEncodingConverter ec
;
4317 ec
.Init(wxFONTENCODING_UNICODE
, wxFONTENCODING_KOI8
);
4318 ec
.Convert(wbuf
, buf
);
4319 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf
);
4325 #endif // TEST_WCHAR
4327 // ----------------------------------------------------------------------------
4329 // ----------------------------------------------------------------------------
4333 #include "wx/filesys.h"
4334 #include "wx/fs_zip.h"
4335 #include "wx/zipstrm.h"
4337 static const wxChar
*TESTFILE_ZIP
= _T("testdata.zip");
4339 static void TestZipStreamRead()
4341 wxPuts(_T("*** Testing ZIP reading ***\n"));
4343 static const wxChar
*filename
= _T("foo");
4344 wxZipInputStream
istr(TESTFILE_ZIP
, filename
);
4345 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4347 wxPrintf(_T("Dumping the file '%s':\n"), filename
);
4348 while ( !istr
.Eof() )
4350 putchar(istr
.GetC());
4354 wxPuts(_T("\n----- done ------"));
4357 static void DumpZipDirectory(wxFileSystem
& fs
,
4358 const wxString
& dir
,
4359 const wxString
& indent
)
4361 wxString prefix
= wxString::Format(_T("%s#zip:%s"),
4362 TESTFILE_ZIP
, dir
.c_str());
4363 wxString wildcard
= prefix
+ _T("/*");
4365 wxString dirname
= fs
.FindFirst(wildcard
, wxDIR
);
4366 while ( !dirname
.empty() )
4368 if ( !dirname
.StartsWith(prefix
+ _T('/'), &dirname
) )
4370 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4375 wxPrintf(_T("%s%s\n"), indent
.c_str(), dirname
.c_str());
4377 DumpZipDirectory(fs
, dirname
,
4378 indent
+ wxString(_T(' '), 4));
4380 dirname
= fs
.FindNext();
4383 wxString filename
= fs
.FindFirst(wildcard
, wxFILE
);
4384 while ( !filename
.empty() )
4386 if ( !filename
.StartsWith(prefix
, &filename
) )
4388 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
4393 wxPrintf(_T("%s%s\n"), indent
.c_str(), filename
.c_str());
4395 filename
= fs
.FindNext();
4399 static void TestZipFileSystem()
4401 wxPuts(_T("*** Testing ZIP file system ***\n"));
4403 wxFileSystem::AddHandler(new wxZipFSHandler
);
4405 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP
);
4407 DumpZipDirectory(fs
, _T(""), wxString(_T(' '), 4));
4412 // ----------------------------------------------------------------------------
4414 // ----------------------------------------------------------------------------
4418 #include "wx/zstream.h"
4419 #include "wx/wfstream.h"
4421 static const wxChar
*FILENAME_GZ
= _T("test.gz");
4422 static const wxChar
*TEST_DATA
= _T("hello and hello and hello and hello and hello");
4424 static void TestZlibStreamWrite()
4426 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4428 wxFileOutputStream
fileOutStream(FILENAME_GZ
);
4429 wxZlibOutputStream
ostr(fileOutStream
);
4430 wxPrintf(_T("Compressing the test string... "));
4431 ostr
.Write(TEST_DATA
, wxStrlen(TEST_DATA
) + 1);
4434 wxPuts(_T("(ERROR: failed)"));
4441 wxPuts(_T("\n----- done ------"));
4444 static void TestZlibStreamRead()
4446 wxPuts(_T("*** Testing Zlib stream reading ***\n"));
4448 wxFileInputStream
fileInStream(FILENAME_GZ
);
4449 wxZlibInputStream
istr(fileInStream
);
4450 wxPrintf(_T("Archive size: %u\n"), istr
.GetSize());
4452 wxPuts(_T("Dumping the file:"));
4453 while ( !istr
.Eof() )
4455 putchar(istr
.GetC());
4459 wxPuts(_T("\n----- done ------"));
4464 // ----------------------------------------------------------------------------
4466 // ----------------------------------------------------------------------------
4468 #ifdef TEST_DATETIME
4472 #include "wx/datetime.h"
4477 wxDateTime::wxDateTime_t day
;
4478 wxDateTime::Month month
;
4480 wxDateTime::wxDateTime_t hour
, min
, sec
;
4482 wxDateTime::WeekDay wday
;
4483 time_t gmticks
, ticks
;
4485 void Init(const wxDateTime::Tm
& tm
)
4494 gmticks
= ticks
= -1;
4497 wxDateTime
DT() const
4498 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
4500 bool SameDay(const wxDateTime::Tm
& tm
) const
4502 return day
== tm
.mday
&& month
== tm
.mon
&& year
== tm
.year
;
4505 wxString
Format() const
4508 s
.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"),
4510 wxDateTime::GetMonthName(month
).c_str(),
4512 abs(wxDateTime::ConvertYearToBC(year
)),
4513 year
> 0 ? _T("AD") : _T("BC"));
4517 wxString
FormatDate() const
4520 s
.Printf(_T("%02d-%s-%4d%s"),
4522 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4523 abs(wxDateTime::ConvertYearToBC(year
)),
4524 year
> 0 ? _T("AD") : _T("BC"));
4529 static const Date testDates
[] =
4531 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
4532 { 7, wxDateTime::Feb
, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu
, -1, -1 },
4533 { 8, wxDateTime::Feb
, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri
, -1, -1 },
4534 { 1, wxDateTime::Jan
, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu
, -1, -1 },
4535 { 1, wxDateTime::Jan
, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri
, -1, -1 },
4536 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
4537 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
4538 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
4539 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
4540 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
4541 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
4542 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
4543 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
4544 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
4545 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun
, -1, -1 },
4546 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat
, -1, -1 },
4547 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri
, -1, -1 },
4548 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat
, -1, -1 },
4549 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
4552 // this test miscellaneous static wxDateTime functions
4553 static void TestTimeStatic()
4555 wxPuts(_T("\n*** wxDateTime static methods test ***"));
4557 // some info about the current date
4558 int year
= wxDateTime::GetCurrentYear();
4559 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
4561 wxDateTime::IsLeapYear(year
) ? "" : "not ",
4562 wxDateTime::GetNumberOfDays(year
));
4564 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
4565 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
4566 wxDateTime::GetMonthName(month
, wxDateTime::Name_Abbr
).c_str(),
4567 wxDateTime::GetMonthName(month
).c_str(),
4568 wxDateTime::GetNumberOfDays(month
));
4571 static const size_t nYears
= 5;
4572 static const size_t years
[2][nYears
] =
4574 // first line: the years to test
4575 { 1990, 1976, 2000, 2030, 1984, },
4577 // second line: true if leap, false otherwise
4578 { false, true, true, false, true }
4581 for ( size_t n
= 0; n
< nYears
; n
++ )
4583 int year
= years
[0][n
];
4584 bool should
= years
[1][n
] != 0,
4585 is
= wxDateTime::IsLeapYear(year
);
4587 wxPrintf(_T("Year %d is %sa leap year (%s)\n"),
4590 should
== is
? "ok" : "ERROR");
4592 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
4596 // test constructing wxDateTime objects
4597 static void TestTimeSet()
4599 wxPuts(_T("\n*** wxDateTime construction test ***"));
4601 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4603 const Date
& d1
= testDates
[n
];
4604 wxDateTime dt
= d1
.DT();
4607 d2
.Init(dt
.GetTm());
4609 wxString s1
= d1
.Format(),
4612 wxPrintf(_T("Date: %s == %s (%s)\n"),
4613 s1
.c_str(), s2
.c_str(),
4614 s1
== s2
? _T("ok") : _T("ERROR"));
4618 // test time zones stuff
4619 static void TestTimeZones()
4621 wxPuts(_T("\n*** wxDateTime timezone test ***"));
4623 wxDateTime now
= wxDateTime::Now();
4625 wxPrintf(_T("Current GMT time:\t%s\n"), now
.Format(_T("%c"), wxDateTime::GMT0
).c_str());
4626 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0
).c_str());
4627 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST
).c_str());
4628 wxPrintf(_T("Current time in Paris:\t%s\n"), now
.Format(_T("%c"), wxDateTime::CET
).c_str());
4629 wxPrintf(_T(" Moscow:\t%s\n"), now
.Format(_T("%c"), wxDateTime::MSK
).c_str());
4630 wxPrintf(_T(" New York:\t%s\n"), now
.Format(_T("%c"), wxDateTime::EST
).c_str());
4632 wxDateTime::Tm tm
= now
.GetTm();
4633 if ( wxDateTime(tm
) != now
)
4635 wxPrintf(_T("ERROR: got %s instead of %s\n"),
4636 wxDateTime(tm
).Format().c_str(), now
.Format().c_str());
4640 // test some minimal support for the dates outside the standard range
4641 static void TestTimeRange()
4643 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
4645 static const wxChar
*fmt
= _T("%d-%b-%Y %H:%M:%S");
4647 wxPrintf(_T("Unix epoch:\t%s\n"),
4648 wxDateTime(2440587.5).Format(fmt
).c_str());
4649 wxPrintf(_T("Feb 29, 0: \t%s\n"),
4650 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
4651 wxPrintf(_T("JDN 0: \t%s\n"),
4652 wxDateTime(0.0).Format(fmt
).c_str());
4653 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
4654 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
4655 wxPrintf(_T("May 29, 2099:\t%s\n"),
4656 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
4659 static void TestTimeTicks()
4661 wxPuts(_T("\n*** wxDateTime ticks test ***"));
4663 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4665 const Date
& d
= testDates
[n
];
4666 if ( d
.ticks
== -1 )
4669 wxDateTime dt
= d
.DT();
4670 long ticks
= (dt
.GetValue() / 1000).ToLong();
4671 wxPrintf(_T("Ticks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4672 if ( ticks
== d
.ticks
)
4674 wxPuts(_T(" (ok)"));
4678 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4679 (long)d
.ticks
, (long)(ticks
- d
.ticks
));
4682 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
4683 ticks
= (dt
.GetValue() / 1000).ToLong();
4684 wxPrintf(_T("GMtks of %s:\t% 10ld"), d
.Format().c_str(), ticks
);
4685 if ( ticks
== d
.gmticks
)
4687 wxPuts(_T(" (ok)"));
4691 wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"),
4692 (long)d
.gmticks
, (long)(ticks
- d
.gmticks
));
4699 // test conversions to JDN &c
4700 static void TestTimeJDN()
4702 wxPuts(_T("\n*** wxDateTime to JDN test ***"));
4704 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4706 const Date
& d
= testDates
[n
];
4707 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4708 double jdn
= dt
.GetJulianDayNumber();
4710 wxPrintf(_T("JDN of %s is:\t% 15.6f"), d
.Format().c_str(), jdn
);
4713 wxPuts(_T(" (ok)"));
4717 wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"),
4718 d
.jdn
, jdn
- d
.jdn
);
4723 // test week days computation
4724 static void TestTimeWDays()
4726 wxPuts(_T("\n*** wxDateTime weekday test ***"));
4728 // test GetWeekDay()
4730 for ( n
= 0; n
< WXSIZEOF(testDates
); n
++ )
4732 const Date
& d
= testDates
[n
];
4733 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
4735 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
4736 wxPrintf(_T("%s is: %s"),
4738 wxDateTime::GetWeekDayName(wday
).c_str());
4739 if ( wday
== d
.wday
)
4741 wxPuts(_T(" (ok)"));
4745 wxPrintf(_T(" (ERROR: should be %s)\n"),
4746 wxDateTime::GetWeekDayName(d
.wday
).c_str());
4752 // test SetToWeekDay()
4753 struct WeekDateTestData
4755 Date date
; // the real date (precomputed)
4756 int nWeek
; // its week index in the month
4757 wxDateTime::WeekDay wday
; // the weekday
4758 wxDateTime::Month month
; // the month
4759 int year
; // and the year
4761 wxString
Format() const
4764 switch ( nWeek
< -1 ? -nWeek
: nWeek
)
4766 case 1: which
= _T("first"); break;
4767 case 2: which
= _T("second"); break;
4768 case 3: which
= _T("third"); break;
4769 case 4: which
= _T("fourth"); break;
4770 case 5: which
= _T("fifth"); break;
4772 case -1: which
= _T("last"); break;
4777 which
+= _T(" from end");
4780 s
.Printf(_T("The %s %s of %s in %d"),
4782 wxDateTime::GetWeekDayName(wday
).c_str(),
4783 wxDateTime::GetMonthName(month
).c_str(),
4790 // the array data was generated by the following python program
4792 from DateTime import *
4793 from whrandom import *
4794 from string import *
4796 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4797 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4799 week = DateTimeDelta(7)
4802 year = randint(1900, 2100)
4803 month = randint(1, 12)
4804 day = randint(1, 28)
4805 dt = DateTime(year, month, day)
4806 wday = dt.day_of_week
4808 countFromEnd = choice([-1, 1])
4811 while dt.month is month:
4812 dt = dt - countFromEnd * week
4813 weekNum = weekNum + countFromEnd
4815 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
4817 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
4818 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
4821 static const WeekDateTestData weekDatesTestData
[] =
4823 { { 20, wxDateTime::Mar
, 2045 }, 3, wxDateTime::Mon
, wxDateTime::Mar
, 2045 },
4824 { { 5, wxDateTime::Jun
, 1985 }, -4, wxDateTime::Wed
, wxDateTime::Jun
, 1985 },
4825 { { 12, wxDateTime::Nov
, 1961 }, -3, wxDateTime::Sun
, wxDateTime::Nov
, 1961 },
4826 { { 27, wxDateTime::Feb
, 2093 }, -1, wxDateTime::Fri
, wxDateTime::Feb
, 2093 },
4827 { { 4, wxDateTime::Jul
, 2070 }, -4, wxDateTime::Fri
, wxDateTime::Jul
, 2070 },
4828 { { 2, wxDateTime::Apr
, 1906 }, -5, wxDateTime::Mon
, wxDateTime::Apr
, 1906 },
4829 { { 19, wxDateTime::Jul
, 2023 }, -2, wxDateTime::Wed
, wxDateTime::Jul
, 2023 },
4830 { { 5, wxDateTime::May
, 1958 }, -4, wxDateTime::Mon
, wxDateTime::May
, 1958 },
4831 { { 11, wxDateTime::Aug
, 1900 }, 2, wxDateTime::Sat
, wxDateTime::Aug
, 1900 },
4832 { { 14, wxDateTime::Feb
, 1945 }, 2, wxDateTime::Wed
, wxDateTime::Feb
, 1945 },
4833 { { 25, wxDateTime::Jul
, 1967 }, -1, wxDateTime::Tue
, wxDateTime::Jul
, 1967 },
4834 { { 9, wxDateTime::May
, 1916 }, -4, wxDateTime::Tue
, wxDateTime::May
, 1916 },
4835 { { 20, wxDateTime::Jun
, 1927 }, 3, wxDateTime::Mon
, wxDateTime::Jun
, 1927 },
4836 { { 2, wxDateTime::Aug
, 2000 }, 1, wxDateTime::Wed
, wxDateTime::Aug
, 2000 },
4837 { { 20, wxDateTime::Apr
, 2044 }, 3, wxDateTime::Wed
, wxDateTime::Apr
, 2044 },
4838 { { 20, wxDateTime::Feb
, 1932 }, -2, wxDateTime::Sat
, wxDateTime::Feb
, 1932 },
4839 { { 25, wxDateTime::Jul
, 2069 }, 4, wxDateTime::Thu
, wxDateTime::Jul
, 2069 },
4840 { { 3, wxDateTime::Apr
, 1925 }, 1, wxDateTime::Fri
, wxDateTime::Apr
, 1925 },
4841 { { 21, wxDateTime::Mar
, 2093 }, 3, wxDateTime::Sat
, wxDateTime::Mar
, 2093 },
4842 { { 3, wxDateTime::Dec
, 2074 }, -5, wxDateTime::Mon
, wxDateTime::Dec
, 2074 },
4845 static const wxChar
*fmt
= _T("%d-%b-%Y");
4848 for ( n
= 0; n
< WXSIZEOF(weekDatesTestData
); n
++ )
4850 const WeekDateTestData
& wd
= weekDatesTestData
[n
];
4852 dt
.SetToWeekDay(wd
.wday
, wd
.nWeek
, wd
.month
, wd
.year
);
4854 wxPrintf(_T("%s is %s"), wd
.Format().c_str(), dt
.Format(fmt
).c_str());
4856 const Date
& d
= wd
.date
;
4857 if ( d
.SameDay(dt
.GetTm()) )
4859 wxPuts(_T(" (ok)"));
4863 dt
.Set(d
.day
, d
.month
, d
.year
);
4865 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.Format(fmt
).c_str());
4870 // test the computation of (ISO) week numbers
4871 static void TestTimeWNumber()
4873 wxPuts(_T("\n*** wxDateTime week number test ***"));
4875 struct WeekNumberTestData
4877 Date date
; // the date
4878 wxDateTime::wxDateTime_t week
; // the week number in the year
4879 wxDateTime::wxDateTime_t wmon
; // the week number in the month
4880 wxDateTime::wxDateTime_t wmon2
; // same but week starts with Sun
4881 wxDateTime::wxDateTime_t dnum
; // day number in the year
4884 // data generated with the following python script:
4886 from DateTime import *
4887 from whrandom import *
4888 from string import *
4890 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
4891 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
4893 def GetMonthWeek(dt):
4894 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
4895 if weekNumMonth < 0:
4896 weekNumMonth = weekNumMonth + 53
4899 def GetLastSundayBefore(dt):
4900 if dt.iso_week[2] == 7:
4903 return dt - DateTimeDelta(dt.iso_week[2])
4906 year = randint(1900, 2100)
4907 month = randint(1, 12)
4908 day = randint(1, 28)
4909 dt = DateTime(year, month, day)
4910 dayNum = dt.day_of_year
4911 weekNum = dt.iso_week[1]
4912 weekNumMonth = GetMonthWeek(dt)
4915 dtSunday = GetLastSundayBefore(dt)
4917 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
4918 weekNumMonth2 = weekNumMonth2 + 1
4919 dtSunday = dtSunday - DateTimeDelta(7)
4921 data = { 'day': rjust(`day`, 2), \
4922 'month': monthNames[month - 1], \
4924 'weekNum': rjust(`weekNum`, 2), \
4925 'weekNumMonth': weekNumMonth, \
4926 'weekNumMonth2': weekNumMonth2, \
4927 'dayNum': rjust(`dayNum`, 3) }
4929 print " { { %(day)s, "\
4930 "wxDateTime::%(month)s, "\
4933 "%(weekNumMonth)s, "\
4934 "%(weekNumMonth2)s, "\
4935 "%(dayNum)s }," % data
4938 static const WeekNumberTestData weekNumberTestDates
[] =
4940 { { 27, wxDateTime::Dec
, 1966 }, 52, 5, 5, 361 },
4941 { { 22, wxDateTime::Jul
, 1926 }, 29, 4, 4, 203 },
4942 { { 22, wxDateTime::Oct
, 2076 }, 43, 4, 4, 296 },
4943 { { 1, wxDateTime::Jul
, 1967 }, 26, 1, 1, 182 },
4944 { { 8, wxDateTime::Nov
, 2004 }, 46, 2, 2, 313 },
4945 { { 21, wxDateTime::Mar
, 1920 }, 12, 3, 4, 81 },
4946 { { 7, wxDateTime::Jan
, 1965 }, 1, 2, 2, 7 },
4947 { { 19, wxDateTime::Oct
, 1999 }, 42, 4, 4, 292 },
4948 { { 13, wxDateTime::Aug
, 1955 }, 32, 2, 2, 225 },
4949 { { 18, wxDateTime::Jul
, 2087 }, 29, 3, 3, 199 },
4950 { { 2, wxDateTime::Sep
, 2028 }, 35, 1, 1, 246 },
4951 { { 28, wxDateTime::Jul
, 1945 }, 30, 5, 4, 209 },
4952 { { 15, wxDateTime::Jun
, 1901 }, 24, 3, 3, 166 },
4953 { { 10, wxDateTime::Oct
, 1939 }, 41, 3, 2, 283 },
4954 { { 3, wxDateTime::Dec
, 1965 }, 48, 1, 1, 337 },
4955 { { 23, wxDateTime::Feb
, 1940 }, 8, 4, 4, 54 },
4956 { { 2, wxDateTime::Jan
, 1987 }, 1, 1, 1, 2 },
4957 { { 11, wxDateTime::Aug
, 2079 }, 32, 2, 2, 223 },
4958 { { 2, wxDateTime::Feb
, 2063 }, 5, 1, 1, 33 },
4959 { { 16, wxDateTime::Oct
, 1942 }, 42, 3, 3, 289 },
4962 for ( size_t n
= 0; n
< WXSIZEOF(weekNumberTestDates
); n
++ )
4964 const WeekNumberTestData
& wn
= weekNumberTestDates
[n
];
4965 const Date
& d
= wn
.date
;
4967 wxDateTime dt
= d
.DT();
4969 wxDateTime::wxDateTime_t
4970 week
= dt
.GetWeekOfYear(wxDateTime::Monday_First
),
4971 wmon
= dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
4972 wmon2
= dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
4973 dnum
= dt
.GetDayOfYear();
4975 wxPrintf(_T("%s: the day number is %d"), d
.FormatDate().c_str(), dnum
);
4976 if ( dnum
== wn
.dnum
)
4978 wxPrintf(_T(" (ok)"));
4982 wxPrintf(_T(" (ERROR: should be %d)"), wn
.dnum
);
4985 wxPrintf(_T(", week in month = %d"), wmon
);
4986 if ( wmon
!= wn
.wmon
)
4988 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon
);
4991 wxPrintf(_T(" or %d"), wmon2
);
4992 if ( wmon2
== wn
.wmon2
)
4994 wxPrintf(_T(" (ok)"));
4998 wxPrintf(_T(" (ERROR: should be %d)"), wn
.wmon2
);
5001 wxPrintf(_T(", week in year = %d"), week
);
5002 if ( week
!= wn
.week
)
5004 wxPrintf(_T(" (ERROR: should be %d)"), wn
.week
);
5007 wxPutchar(_T('\n'));
5009 wxDateTime
dt2(1, wxDateTime::Jan
, d
.year
);
5010 dt2
.SetToTheWeek(wn
.week
, dt
.GetWeekDay());
5014 d2
.Init(dt2
.GetTm());
5015 wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
5016 d2
.FormatDate().c_str());
5021 // test DST calculations
5022 static void TestTimeDST()
5024 wxPuts(_T("\n*** wxDateTime DST test ***"));
5026 wxPrintf(_T("DST is%s in effect now.\n\n"),
5027 wxDateTime::Now().IsDST() ? _T("") : _T(" not"));
5029 // taken from http://www.energy.ca.gov/daylightsaving.html
5030 static const Date datesDST
[2][2004 - 1900 + 1] =
5033 { 1, wxDateTime::Apr
, 1990 },
5034 { 7, wxDateTime::Apr
, 1991 },
5035 { 5, wxDateTime::Apr
, 1992 },
5036 { 4, wxDateTime::Apr
, 1993 },
5037 { 3, wxDateTime::Apr
, 1994 },
5038 { 2, wxDateTime::Apr
, 1995 },
5039 { 7, wxDateTime::Apr
, 1996 },
5040 { 6, wxDateTime::Apr
, 1997 },
5041 { 5, wxDateTime::Apr
, 1998 },
5042 { 4, wxDateTime::Apr
, 1999 },
5043 { 2, wxDateTime::Apr
, 2000 },
5044 { 1, wxDateTime::Apr
, 2001 },
5045 { 7, wxDateTime::Apr
, 2002 },
5046 { 6, wxDateTime::Apr
, 2003 },
5047 { 4, wxDateTime::Apr
, 2004 },
5050 { 28, wxDateTime::Oct
, 1990 },
5051 { 27, wxDateTime::Oct
, 1991 },
5052 { 25, wxDateTime::Oct
, 1992 },
5053 { 31, wxDateTime::Oct
, 1993 },
5054 { 30, wxDateTime::Oct
, 1994 },
5055 { 29, wxDateTime::Oct
, 1995 },
5056 { 27, wxDateTime::Oct
, 1996 },
5057 { 26, wxDateTime::Oct
, 1997 },
5058 { 25, wxDateTime::Oct
, 1998 },
5059 { 31, wxDateTime::Oct
, 1999 },
5060 { 29, wxDateTime::Oct
, 2000 },
5061 { 28, wxDateTime::Oct
, 2001 },
5062 { 27, wxDateTime::Oct
, 2002 },
5063 { 26, wxDateTime::Oct
, 2003 },
5064 { 31, wxDateTime::Oct
, 2004 },
5069 for ( year
= 1990; year
< 2005; year
++ )
5071 wxDateTime dtBegin
= wxDateTime::GetBeginDST(year
, wxDateTime::USA
),
5072 dtEnd
= wxDateTime::GetEndDST(year
, wxDateTime::USA
);
5074 wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
5075 year
, dtBegin
.Format().c_str(), dtEnd
.Format().c_str());
5077 size_t n
= year
- 1990;
5078 const Date
& dBegin
= datesDST
[0][n
];
5079 const Date
& dEnd
= datesDST
[1][n
];
5081 if ( dBegin
.SameDay(dtBegin
.GetTm()) && dEnd
.SameDay(dtEnd
.GetTm()) )
5083 wxPuts(_T(" (ok)"));
5087 wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
5088 wxDateTime::GetMonthName(dBegin
.month
).c_str(), dBegin
.day
,
5089 wxDateTime::GetMonthName(dEnd
.month
).c_str(), dEnd
.day
);
5095 for ( year
= 1990; year
< 2005; year
++ )
5097 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
5099 wxDateTime::GetBeginDST(year
, wxDateTime::Country_EEC
).Format().c_str(),
5100 wxDateTime::GetEndDST(year
, wxDateTime::Country_EEC
).Format().c_str());
5104 // test wxDateTime -> text conversion
5105 static void TestTimeFormat()
5107 wxPuts(_T("\n*** wxDateTime formatting test ***"));
5109 // some information may be lost during conversion, so store what kind
5110 // of info should we recover after a round trip
5113 CompareNone
, // don't try comparing
5114 CompareBoth
, // dates and times should be identical
5115 CompareDate
, // dates only
5116 CompareTime
// time only
5121 CompareKind compareKind
;
5122 const wxChar
*format
;
5123 } formatTestFormats
[] =
5125 { CompareBoth
, _T("---> %c") },
5126 { CompareDate
, _T("Date is %A, %d of %B, in year %Y") },
5127 { CompareBoth
, _T("Date is %x, time is %X") },
5128 { CompareTime
, _T("Time is %H:%M:%S or %I:%M:%S %p") },
5129 { CompareNone
, _T("The day of year: %j, the week of year: %W") },
5130 { CompareDate
, _T("ISO date without separators: %Y%m%d") },
5133 static const Date formatTestDates
[] =
5135 { 29, wxDateTime::May
, 1976, 18, 30, 00 },
5136 { 31, wxDateTime::Dec
, 1999, 23, 30, 00 },
5138 // this test can't work for other centuries because it uses two digit
5139 // years in formats, so don't even try it
5140 { 29, wxDateTime::May
, 2076, 18, 30, 00 },
5141 { 29, wxDateTime::Feb
, 2400, 02, 15, 25 },
5142 { 01, wxDateTime::Jan
, -52, 03, 16, 47 },
5146 // an extra test (as it doesn't depend on date, don't do it in the loop)
5147 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
5149 for ( size_t d
= 0; d
< WXSIZEOF(formatTestDates
) + 1; d
++ )
5153 wxDateTime dt
= d
== 0 ? wxDateTime::Now() : formatTestDates
[d
- 1].DT();
5154 for ( size_t n
= 0; n
< WXSIZEOF(formatTestFormats
); n
++ )
5156 wxString s
= dt
.Format(formatTestFormats
[n
].format
);
5157 wxPrintf(_T("%s"), s
.c_str());
5159 // what can we recover?
5160 int kind
= formatTestFormats
[n
].compareKind
;
5164 const wxChar
*result
= dt2
.ParseFormat(s
, formatTestFormats
[n
].format
);
5167 // converion failed - should it have?
5168 if ( kind
== CompareNone
)
5169 wxPuts(_T(" (ok)"));
5171 wxPuts(_T(" (ERROR: conversion back failed)"));
5175 // should have parsed the entire string
5176 wxPuts(_T(" (ERROR: conversion back stopped too soon)"));
5180 bool equal
= false; // suppress compilaer warning
5188 equal
= dt
.IsSameDate(dt2
);
5192 equal
= dt
.IsSameTime(dt2
);
5198 wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
5199 dt2
.Format().c_str(), dt
.Format().c_str());
5203 wxPuts(_T(" (ok)"));
5210 // test text -> wxDateTime conversion
5211 static void TestTimeParse()
5213 wxPuts(_T("\n*** wxDateTime parse test ***"));
5215 struct ParseTestData
5217 const wxChar
*format
;
5222 static const ParseTestData parseTestDates
[] =
5224 { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec
, 1999, 00, 46, 40 }, true },
5225 { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec
, 1999, 03, 17, 20 }, true },
5228 for ( size_t n
= 0; n
< WXSIZEOF(parseTestDates
); n
++ )
5230 const wxChar
*format
= parseTestDates
[n
].format
;
5232 wxPrintf(_T("%s => "), format
);
5235 if ( dt
.ParseRfc822Date(format
) )
5237 wxPrintf(_T("%s "), dt
.Format().c_str());
5239 if ( parseTestDates
[n
].good
)
5241 wxDateTime dtReal
= parseTestDates
[n
].date
.DT();
5248 wxPrintf(_T("(ERROR: should be %s)\n"), dtReal
.Format().c_str());
5253 wxPuts(_T("(ERROR: bad format)"));
5258 wxPrintf(_T("bad format (%s)\n"),
5259 parseTestDates
[n
].good
? "ERROR" : "ok");
5264 static void TestDateTimeInteractive()
5266 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
5272 wxPrintf(_T("Enter a date: "));
5273 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
5276 // kill the last '\n'
5277 buf
[wxStrlen(buf
) - 1] = 0;
5280 const wxChar
*p
= dt
.ParseDate(buf
);
5283 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf
);
5289 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p
- buf
);
5292 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
5293 dt
.Format(_T("%b %d, %Y")).c_str(),
5295 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
5296 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
5297 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
5300 wxPuts(_T("\n*** done ***"));
5303 static void TestTimeMS()
5305 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
5307 wxDateTime dt1
= wxDateTime::Now(),
5308 dt2
= wxDateTime::UNow();
5310 wxPrintf(_T("Now = %s\n"), dt1
.Format(_T("%H:%M:%S:%l")).c_str());
5311 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5312 wxPrintf(_T("Dummy loop: "));
5313 for ( int i
= 0; i
< 6000; i
++ )
5315 //for ( int j = 0; j < 10; j++ )
5318 s
.Printf(_T("%g"), sqrt(i
));
5324 wxPuts(_T(", done"));
5327 dt2
= wxDateTime::UNow();
5328 wxPrintf(_T("UNow = %s\n"), dt2
.Format(_T("%H:%M:%S:%l")).c_str());
5330 wxPrintf(_T("Loop executed in %s ms\n"), (dt2
- dt1
).Format(_T("%l")).c_str());
5332 wxPuts(_T("\n*** done ***"));
5335 static void TestTimeArithmetics()
5337 wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
5339 static const struct ArithmData
5341 ArithmData(const wxDateSpan
& sp
, const wxChar
*nam
)
5342 : span(sp
), name(nam
) { }
5346 } testArithmData
[] =
5348 ArithmData(wxDateSpan::Day(), _T("day")),
5349 ArithmData(wxDateSpan::Week(), _T("week")),
5350 ArithmData(wxDateSpan::Month(), _T("month")),
5351 ArithmData(wxDateSpan::Year(), _T("year")),
5352 ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")),
5355 wxDateTime
dt(29, wxDateTime::Dec
, 1999), dt1
, dt2
;
5357 for ( size_t n
= 0; n
< WXSIZEOF(testArithmData
); n
++ )
5359 wxDateSpan span
= testArithmData
[n
].span
;
5363 const wxChar
*name
= testArithmData
[n
].name
;
5364 wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"),
5365 dt
.FormatISODate().c_str(), name
, dt1
.FormatISODate().c_str(),
5366 dt
.FormatISODate().c_str(), name
, dt2
.FormatISODate().c_str());
5368 wxPrintf(_T("Going back: %s"), (dt1
- span
).FormatISODate().c_str());
5369 if ( dt1
- span
== dt
)
5371 wxPuts(_T(" (ok)"));
5375 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5378 wxPrintf(_T("Going forward: %s"), (dt2
+ span
).FormatISODate().c_str());
5379 if ( dt2
+ span
== dt
)
5381 wxPuts(_T(" (ok)"));
5385 wxPrintf(_T(" (ERROR: should be %s)\n"), dt
.FormatISODate().c_str());
5388 wxPrintf(_T("Double increment: %s"), (dt2
+ 2*span
).FormatISODate().c_str());
5389 if ( dt2
+ 2*span
== dt1
)
5391 wxPuts(_T(" (ok)"));
5395 wxPrintf(_T(" (ERROR: should be %s)\n"), dt2
.FormatISODate().c_str());
5402 static void TestTimeHolidays()
5404 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
5406 wxDateTime::Tm tm
= wxDateTime(29, wxDateTime::May
, 2000).GetTm();
5407 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
5408 dtEnd
= dtStart
.GetLastMonthDay();
5410 wxDateTimeArray hol
;
5411 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
5413 const wxChar
*format
= _T("%d-%b-%Y (%a)");
5415 wxPrintf(_T("All holidays between %s and %s:\n"),
5416 dtStart
.Format(format
).c_str(), dtEnd
.Format(format
).c_str());
5418 size_t count
= hol
.GetCount();
5419 for ( size_t n
= 0; n
< count
; n
++ )
5421 wxPrintf(_T("\t%s\n"), hol
[n
].Format(format
).c_str());
5427 static void TestTimeZoneBug()
5429 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
5431 wxDateTime date
= wxDateTime(1, wxDateTime::Mar
, 2000);
5432 for ( int i
= 0; i
< 31; i
++ )
5434 wxPrintf(_T("Date %s: week day %s.\n"),
5435 date
.Format(_T("%d-%m-%Y")).c_str(),
5436 date
.GetWeekDayName(date
.GetWeekDay()).c_str());
5438 date
+= wxDateSpan::Day();
5444 static void TestTimeSpanFormat()
5446 wxPuts(_T("\n*** wxTimeSpan tests ***"));
5448 static const wxChar
*formats
[] =
5450 _T("(default) %H:%M:%S"),
5451 _T("%E weeks and %D days"),
5452 _T("%l milliseconds"),
5453 _T("(with ms) %H:%M:%S:%l"),
5454 _T("100%% of minutes is %M"), // test "%%"
5455 _T("%D days and %H hours"),
5456 _T("or also %S seconds"),
5459 wxTimeSpan
ts1(1, 2, 3, 4),
5461 for ( size_t n
= 0; n
< WXSIZEOF(formats
); n
++ )
5463 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
5464 ts1
.Format(formats
[n
]).c_str(),
5465 ts2
.Format(formats
[n
]).c_str());
5471 #endif // TEST_DATETIME
5473 // ----------------------------------------------------------------------------
5474 // wxTextInput/OutputStream
5475 // ----------------------------------------------------------------------------
5477 #ifdef TEST_TEXTSTREAM
5479 #include "wx/txtstrm.h"
5480 #include "wx/wfstream.h"
5482 static void TestTextInputStream()
5484 wxPuts(_T("\n*** wxTextInputStream test ***"));
5486 wxFileInputStream
fsIn(_T("testdata.fc"));
5489 wxPuts(_T("ERROR: couldn't open file."));
5493 wxTextInputStream
tis(fsIn
);
5498 const wxString s
= tis
.ReadLine();
5500 // line could be non empty if the last line of the file isn't
5501 // terminated with EOL
5502 if ( fsIn
.Eof() && s
.empty() )
5505 wxPrintf(_T("Line %d: %s\n"), line
++, s
.c_str());
5510 #endif // TEST_TEXTSTREAM
5512 // ----------------------------------------------------------------------------
5514 // ----------------------------------------------------------------------------
5518 #include "wx/thread.h"
5520 static size_t gs_counter
= (size_t)-1;
5521 static wxCriticalSection gs_critsect
;
5522 static wxSemaphore gs_cond
;
5524 class MyJoinableThread
: public wxThread
5527 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
5528 { m_n
= n
; Create(); }
5530 // thread execution starts here
5531 virtual ExitCode
Entry();
5537 wxThread::ExitCode
MyJoinableThread::Entry()
5539 unsigned long res
= 1;
5540 for ( size_t n
= 1; n
< m_n
; n
++ )
5544 // it's a loooong calculation :-)
5548 return (ExitCode
)res
;
5551 class MyDetachedThread
: public wxThread
5554 MyDetachedThread(size_t n
, wxChar ch
)
5558 m_cancelled
= false;
5563 // thread execution starts here
5564 virtual ExitCode
Entry();
5567 virtual void OnExit();
5570 size_t m_n
; // number of characters to write
5571 wxChar m_ch
; // character to write
5573 bool m_cancelled
; // false if we exit normally
5576 wxThread::ExitCode
MyDetachedThread::Entry()
5579 wxCriticalSectionLocker
lock(gs_critsect
);
5580 if ( gs_counter
== (size_t)-1 )
5586 for ( size_t n
= 0; n
< m_n
; n
++ )
5588 if ( TestDestroy() )
5598 wxThread::Sleep(100);
5604 void MyDetachedThread::OnExit()
5606 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
5608 wxCriticalSectionLocker
lock(gs_critsect
);
5609 if ( !--gs_counter
&& !m_cancelled
)
5613 static void TestDetachedThreads()
5615 wxPuts(_T("\n*** Testing detached threads ***"));
5617 static const size_t nThreads
= 3;
5618 MyDetachedThread
*threads
[nThreads
];
5620 for ( n
= 0; n
< nThreads
; n
++ )
5622 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
5625 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
5626 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
5628 for ( n
= 0; n
< nThreads
; n
++ )
5633 // wait until all threads terminate
5639 static void TestJoinableThreads()
5641 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
5643 // calc 10! in the background
5644 MyJoinableThread
thread(10);
5647 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5648 (unsigned long)thread
.Wait());
5651 static void TestThreadSuspend()
5653 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
5655 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
5659 // this is for this demo only, in a real life program we'd use another
5660 // condition variable which would be signaled from wxThread::Entry() to
5661 // tell us that the thread really started running - but here just wait a
5662 // bit and hope that it will be enough (the problem is, of course, that
5663 // the thread might still not run when we call Pause() which will result
5665 wxThread::Sleep(300);
5667 for ( size_t n
= 0; n
< 3; n
++ )
5671 wxPuts(_T("\nThread suspended"));
5674 // don't sleep but resume immediately the first time
5675 wxThread::Sleep(300);
5677 wxPuts(_T("Going to resume the thread"));
5682 wxPuts(_T("Waiting until it terminates now"));
5684 // wait until the thread terminates
5690 static void TestThreadDelete()
5692 // As above, using Sleep() is only for testing here - we must use some
5693 // synchronisation object instead to ensure that the thread is still
5694 // running when we delete it - deleting a detached thread which already
5695 // terminated will lead to a crash!
5697 wxPuts(_T("\n*** Testing thread delete function ***"));
5699 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
5703 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
5705 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
5709 wxThread::Sleep(300);
5713 wxPuts(_T("\nDeleted a running thread."));
5715 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
5719 wxThread::Sleep(300);
5725 wxPuts(_T("\nDeleted a sleeping thread."));
5727 MyJoinableThread
thread3(20);
5732 wxPuts(_T("\nDeleted a joinable thread."));
5734 MyJoinableThread
thread4(2);
5737 wxThread::Sleep(300);
5741 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
5746 class MyWaitingThread
: public wxThread
5749 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
5752 m_condition
= condition
;
5757 virtual ExitCode
Entry()
5759 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
5764 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
5768 m_condition
->Wait();
5771 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
5779 wxCondition
*m_condition
;
5782 static void TestThreadConditions()
5785 wxCondition
condition(mutex
);
5787 // otherwise its difficult to understand which log messages pertain to
5789 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
5790 // condition.GetId(), gs_cond.GetId());
5792 // create and launch threads
5793 MyWaitingThread
*threads
[10];
5796 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5798 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
5801 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
5806 // wait until all threads run
5807 wxPuts(_T("Main thread is waiting for the other threads to start"));
5810 size_t nRunning
= 0;
5811 while ( nRunning
< WXSIZEOF(threads
) )
5817 wxPrintf(_T("Main thread: %u already running\n"), nRunning
);
5821 wxPuts(_T("Main thread: all threads started up."));
5824 wxThread::Sleep(500);
5827 // now wake one of them up
5828 wxPrintf(_T("Main thread: about to signal the condition.\n"));
5833 wxThread::Sleep(200);
5835 // wake all the (remaining) threads up, so that they can exit
5836 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
5838 condition
.Broadcast();
5840 // give them time to terminate (dirty!)
5841 wxThread::Sleep(500);
5844 #include "wx/utils.h"
5846 class MyExecThread
: public wxThread
5849 MyExecThread(const wxString
& command
) : wxThread(wxTHREAD_JOINABLE
),
5855 virtual ExitCode
Entry()
5857 return (ExitCode
)wxExecute(m_command
, wxEXEC_SYNC
);
5864 static void TestThreadExec()
5866 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
5868 MyExecThread
thread(_T("true"));
5871 wxPrintf(_T("Main program exit code: %ld.\n"),
5872 wxExecute(_T("false"), wxEXEC_SYNC
));
5874 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread
.Wait());
5878 #include "wx/datetime.h"
5880 class MySemaphoreThread
: public wxThread
5883 MySemaphoreThread(int i
, wxSemaphore
*sem
)
5884 : wxThread(wxTHREAD_JOINABLE
),
5891 virtual ExitCode
Entry()
5893 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
5894 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5898 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
5899 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5903 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
5904 wxDateTime::Now().FormatTime().c_str(), m_i
, (long)GetId());
5916 WX_DEFINE_ARRAY(wxThread
*, ArrayThreads
);
5918 static void TestSemaphore()
5920 wxPuts(_T("*** Testing wxSemaphore class. ***"));
5922 static const int SEM_LIMIT
= 3;
5924 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
5925 ArrayThreads threads
;
5927 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
5929 threads
.Add(new MySemaphoreThread(i
, &sem
));
5930 threads
.Last()->Run();
5933 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
5940 #endif // TEST_THREADS
5942 // ----------------------------------------------------------------------------
5944 // ----------------------------------------------------------------------------
5948 #include "wx/dynarray.h"
5950 typedef unsigned short ushort
;
5952 #define DefineCompare(name, T) \
5954 int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
5956 return first - second; \
5959 int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
5961 return *first - *second; \
5964 int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
5966 return *second - *first; \
5969 DefineCompare(UShort, ushort);
5970 DefineCompare(Int
, int);
5972 // test compilation of all macros
5973 WX_DEFINE_ARRAY_SHORT(ushort
, wxArrayUShort
);
5974 WX_DEFINE_SORTED_ARRAY_SHORT(ushort
, wxSortedArrayUShortNoCmp
);
5975 WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort
, UShortCompareValues
, wxSortedArrayUShort
);
5976 WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues
, wxSortedArrayInt
);
5978 WX_DECLARE_OBJARRAY(Bar
, ArrayBars
);
5979 #include "wx/arrimpl.cpp"
5980 WX_DEFINE_OBJARRAY(ArrayBars
);
5982 static void PrintArray(const wxChar
* name
, const wxArrayString
& array
)
5984 wxPrintf(_T("Dump of the array '%s'\n"), name
);
5986 size_t nCount
= array
.GetCount();
5987 for ( size_t n
= 0; n
< nCount
; n
++ )
5989 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
5993 static void PrintArray(const wxChar
* name
, const wxSortedArrayString
& array
)
5995 wxPrintf(_T("Dump of the array '%s'\n"), name
);
5997 size_t nCount
= array
.GetCount();
5998 for ( size_t n
= 0; n
< nCount
; n
++ )
6000 wxPrintf(_T("\t%s[%u] = '%s'\n"), name
, n
, array
[n
].c_str());
6004 int wxCMPFUNC_CONV
StringLenCompare(const wxString
& first
,
6005 const wxString
& second
)
6007 return first
.length() - second
.length();
6010 #define TestArrayOf(name) \
6012 static void PrintArray(const wxChar* name, const wxSortedArray##name & array) \
6014 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6016 size_t nCount = array.GetCount(); \
6017 for ( size_t n = 0; n < nCount; n++ ) \
6019 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6023 static void PrintArray(const wxChar* name, const wxArray##name & array) \
6025 wxPrintf(_T("Dump of the array '%s'\n"), name); \
6027 size_t nCount = array.GetCount(); \
6028 for ( size_t n = 0; n < nCount; n++ ) \
6030 wxPrintf(_T("\t%s[%u] = %d\n"), name, n, array[n]); \
6034 static void TestArrayOf ## name ## s() \
6036 wxPrintf(_T("*** Testing wxArray%s ***\n"), #name); \
6044 wxPuts(_T("Initially:")); \
6045 PrintArray(_T("a"), a); \
6047 wxPuts(_T("After sort:")); \
6048 a.Sort(name ## Compare); \
6049 PrintArray(_T("a"), a); \
6051 wxPuts(_T("After reverse sort:")); \
6052 a.Sort(name ## RevCompare); \
6053 PrintArray(_T("a"), a); \
6055 wxSortedArray##name b; \
6061 wxPuts(_T("Sorted array initially:")); \
6062 PrintArray(_T("b"), b); \
6065 TestArrayOf(UShort
);
6068 static void TestStlArray()
6070 wxPuts(_T("*** Testing std::vector operations ***\n"));
6074 wxArrayInt::iterator it
, en
;
6075 wxArrayInt::reverse_iterator rit
, ren
;
6077 for ( i
= 0; i
< 5; ++i
)
6080 for ( it
= list1
.begin(), en
= list1
.end(), i
= 0;
6081 it
!= en
; ++it
, ++i
)
6083 wxPuts(_T("Error in iterator\n"));
6085 for ( rit
= list1
.rbegin(), ren
= list1
.rend(), i
= 4;
6086 rit
!= ren
; ++rit
, --i
)
6088 wxPuts(_T("Error in reverse_iterator\n"));
6090 if ( *list1
.rbegin() != *(list1
.end()-1) ||
6091 *list1
.begin() != *(list1
.rend()-1) )
6092 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6094 it
= list1
.begin()+1;
6095 rit
= list1
.rbegin()+1;
6096 if ( *list1
.begin() != *(it
-1) ||
6097 *list1
.rbegin() != *(rit
-1) )
6098 wxPuts(_T("Error in iterator/reverse_iterator\n"));
6100 if ( list1
.front() != 0 || list1
.back() != 4 )
6101 wxPuts(_T("Error in front()/back()\n"));
6103 list1
.erase(list1
.begin());
6104 list1
.erase(list1
.end()-1);
6106 for ( it
= list1
.begin(), en
= list1
.end(), i
= 1;
6107 it
!= en
; ++it
, ++i
)
6109 wxPuts(_T("Error in erase()\n"));
6112 wxPuts(_T("*** Testing std::vector operations finished ***\n"));
6115 static void TestArrayOfObjects()
6117 wxPuts(_T("*** Testing wxObjArray ***\n"));
6121 Bar
bar(_T("second bar (two copies!)"));
6123 wxPrintf(_T("Initially: %u objects in the array, %u objects total.\n"),
6124 bars
.GetCount(), Bar::GetNumber());
6126 bars
.Add(new Bar(_T("first bar")));
6129 wxPrintf(_T("Now: %u objects in the array, %u objects total.\n"),
6130 bars
.GetCount(), Bar::GetNumber());
6132 bars
.RemoveAt(1, bars
.GetCount() - 1);
6134 wxPrintf(_T("After removing all but first element: %u objects in the ")
6135 _T("array, %u objects total.\n"),
6136 bars
.GetCount(), Bar::GetNumber());
6140 wxPrintf(_T("After Empty(): %u objects in the array, %u objects total.\n"),
6141 bars
.GetCount(), Bar::GetNumber());
6144 wxPrintf(_T("Finally: no more objects in the array, %u objects total.\n"),
6148 #endif // TEST_ARRAYS
6150 // ----------------------------------------------------------------------------
6152 // ----------------------------------------------------------------------------
6156 #include "wx/timer.h"
6157 #include "wx/tokenzr.h"
6159 static void TestStringConstruction()
6161 wxPuts(_T("*** Testing wxString constructores ***"));
6163 #define TEST_CTOR(args, res) \
6166 wxPrintf(_T("wxString%s = %s "), #args, s.c_str()); \
6169 wxPuts(_T("(ok)")); \
6173 wxPrintf(_T("(ERROR: should be %s)\n"), res); \
6177 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
6178 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
6179 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
6180 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
6182 static const wxChar
*s
= _T("?really!");
6183 const wxChar
*start
= wxStrchr(s
, _T('r'));
6184 const wxChar
*end
= wxStrchr(s
, _T('!'));
6185 TEST_CTOR((start
, end
), _T("really"));
6190 static void TestString()
6200 for (int i
= 0; i
< 1000000; ++i
)
6204 c
= _T("! How'ya doin'?");
6207 c
= _T("Hello world! What's up?");
6212 wxPrintf(_T("TestString elapsed time: %ld\n"), sw
.Time());
6215 static void TestPChar()
6223 for (int i
= 0; i
< 1000000; ++i
)
6225 wxStrcpy (a
, _T("Hello"));
6226 wxStrcpy (b
, _T(" world"));
6227 wxStrcpy (c
, _T("! How'ya doin'?"));
6230 wxStrcpy (c
, _T("Hello world! What's up?"));
6231 if (wxStrcmp (c
, a
) == 0)
6232 wxStrcpy (c
, _T("Doh!"));
6235 wxPrintf(_T("TestPChar elapsed time: %ld\n"), sw
.Time());
6238 static void TestStringSub()
6240 wxString
s(_T("Hello, world!"));
6242 wxPuts(_T("*** Testing wxString substring extraction ***"));
6244 wxPrintf(_T("String = '%s'\n"), s
.c_str());
6245 wxPrintf(_T("Left(5) = '%s'\n"), s
.Left(5).c_str());
6246 wxPrintf(_T("Right(6) = '%s'\n"), s
.Right(6).c_str());
6247 wxPrintf(_T("Mid(3, 5) = '%s'\n"), s(3, 5).c_str());
6248 wxPrintf(_T("Mid(3) = '%s'\n"), s
.Mid(3).c_str());
6249 wxPrintf(_T("substr(3, 5) = '%s'\n"), s
.substr(3, 5).c_str());
6250 wxPrintf(_T("substr(3) = '%s'\n"), s
.substr(3).c_str());
6252 static const wxChar
*prefixes
[] =
6256 _T("Hello, world!"),
6257 _T("Hello, world!!!"),
6263 for ( size_t n
= 0; n
< WXSIZEOF(prefixes
); n
++ )
6265 wxString prefix
= prefixes
[n
], rest
;
6266 bool rc
= s
.StartsWith(prefix
, &rest
);
6267 wxPrintf(_T("StartsWith('%s') = %s"), prefix
.c_str(), rc
? _T("true") : _T("false"));
6270 wxPrintf(_T(" (the rest is '%s')\n"), rest
.c_str());
6281 static void TestStringFormat()
6283 wxPuts(_T("*** Testing wxString formatting ***"));
6286 s
.Printf(_T("%03d"), 18);
6288 wxPrintf(_T("Number 18: %s\n"), wxString::Format(_T("%03d"), 18).c_str());
6289 wxPrintf(_T("Number 18: %s\n"), s
.c_str());
6294 // returns "not found" for npos, value for all others
6295 static wxString
PosToString(size_t res
)
6297 wxString s
= res
== wxString::npos
? wxString(_T("not found"))
6298 : wxString::Format(_T("%u"), res
);
6302 static void TestStringFind()
6304 wxPuts(_T("*** Testing wxString find() functions ***"));
6306 static const wxChar
*strToFind
= _T("ell");
6307 static const struct StringFindTest
6311 result
; // of searching "ell" in str
6314 { _T("Well, hello world"), 0, 1 },
6315 { _T("Well, hello world"), 6, 7 },
6316 { _T("Well, hello world"), 9, wxString::npos
},
6319 for ( size_t n
= 0; n
< WXSIZEOF(findTestData
); n
++ )
6321 const StringFindTest
& ft
= findTestData
[n
];
6322 size_t res
= wxString(ft
.str
).find(strToFind
, ft
.start
);
6324 wxPrintf(_T("Index of '%s' in '%s' starting from %u is %s "),
6325 strToFind
, ft
.str
, ft
.start
, PosToString(res
).c_str());
6327 size_t resTrue
= ft
.result
;
6328 if ( res
== resTrue
)
6334 wxPrintf(_T("(ERROR: should be %s)\n"),
6335 PosToString(resTrue
).c_str());
6342 static void TestStringTokenizer()
6344 wxPuts(_T("*** Testing wxStringTokenizer ***"));
6346 static const wxChar
*modeNames
[] =
6350 _T("return all empty"),
6355 static const struct StringTokenizerTest
6357 const wxChar
*str
; // string to tokenize
6358 const wxChar
*delims
; // delimiters to use
6359 size_t count
; // count of token
6360 wxStringTokenizerMode mode
; // how should we tokenize it
6361 } tokenizerTestData
[] =
6363 { _T(""), _T(" "), 0 },
6364 { _T("Hello, world"), _T(" "), 2 },
6365 { _T("Hello, world "), _T(" "), 2 },
6366 { _T("Hello, world"), _T(","), 2 },
6367 { _T("Hello, world!"), _T(",!"), 2 },
6368 { _T("Hello,, world!"), _T(",!"), 3 },
6369 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL
},
6370 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
6371 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 4 },
6372 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 6, wxTOKEN_RET_EMPTY
},
6373 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS
, 9, wxTOKEN_RET_EMPTY_ALL
},
6374 { _T("01/02/99"), _T("/-"), 3 },
6375 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS
},
6378 for ( size_t n
= 0; n
< WXSIZEOF(tokenizerTestData
); n
++ )
6380 const StringTokenizerTest
& tt
= tokenizerTestData
[n
];
6381 wxStringTokenizer
tkz(tt
.str
, tt
.delims
, tt
.mode
);
6383 size_t count
= tkz
.CountTokens();
6384 wxPrintf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
6385 MakePrintable(tt
.str
).c_str(),
6387 MakePrintable(tt
.delims
).c_str(),
6388 modeNames
[tkz
.GetMode()]);
6389 if ( count
== tt
.count
)
6395 wxPrintf(_T("(ERROR: should be %u)\n"), tt
.count
);
6400 // if we emulate strtok(), check that we do it correctly
6401 wxChar
*buf
, *s
= NULL
, *last
;
6403 if ( tkz
.GetMode() == wxTOKEN_STRTOK
)
6405 buf
= new wxChar
[wxStrlen(tt
.str
) + 1];
6406 wxStrcpy(buf
, tt
.str
);
6408 s
= wxStrtok(buf
, tt
.delims
, &last
);
6415 // now show the tokens themselves
6417 while ( tkz
.HasMoreTokens() )
6419 wxString token
= tkz
.GetNextToken();
6421 wxPrintf(_T("\ttoken %u: '%s'"),
6423 MakePrintable(token
).c_str());
6429 wxPuts(_T(" (ok)"));
6433 wxPrintf(_T(" (ERROR: should be %s)\n"), s
);
6436 s
= wxStrtok(NULL
, tt
.delims
, &last
);
6440 // nothing to compare with
6445 if ( count2
!= count
)
6447 wxPuts(_T("\tERROR: token count mismatch"));
6456 static void TestStringReplace()
6458 wxPuts(_T("*** Testing wxString::replace ***"));
6460 static const struct StringReplaceTestData
6462 const wxChar
*original
; // original test string
6463 size_t start
, len
; // the part to replace
6464 const wxChar
*replacement
; // the replacement string
6465 const wxChar
*result
; // and the expected result
6466 } stringReplaceTestData
[] =
6468 { _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
6469 { _T("increase"), 0, 2, _T("de"), _T("decrease") },
6470 { _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
6471 { _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
6472 { _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
6475 for ( size_t n
= 0; n
< WXSIZEOF(stringReplaceTestData
); n
++ )
6477 const StringReplaceTestData data
= stringReplaceTestData
[n
];
6479 wxString original
= data
.original
;
6480 original
.replace(data
.start
, data
.len
, data
.replacement
);
6482 wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
6483 data
.original
, data
.start
, data
.len
, data
.replacement
,
6486 if ( original
== data
.result
)
6492 wxPrintf(_T("(ERROR: should be '%s')\n"), data
.result
);
6499 static void TestStringMatch()
6501 wxPuts(_T("*** Testing wxString::Matches() ***"));
6503 static const struct StringMatchTestData
6506 const wxChar
*wildcard
;
6508 } stringMatchTestData
[] =
6510 { _T("foobar"), _T("foo*"), 1 },
6511 { _T("foobar"), _T("*oo*"), 1 },
6512 { _T("foobar"), _T("*bar"), 1 },
6513 { _T("foobar"), _T("??????"), 1 },
6514 { _T("foobar"), _T("f??b*"), 1 },
6515 { _T("foobar"), _T("f?b*"), 0 },
6516 { _T("foobar"), _T("*goo*"), 0 },
6517 { _T("foobar"), _T("*foo"), 0 },
6518 { _T("foobarfoo"), _T("*foo"), 1 },
6519 { _T(""), _T("*"), 1 },
6520 { _T(""), _T("?"), 0 },
6523 for ( size_t n
= 0; n
< WXSIZEOF(stringMatchTestData
); n
++ )
6525 const StringMatchTestData
& data
= stringMatchTestData
[n
];
6526 bool matches
= wxString(data
.text
).Matches(data
.wildcard
);
6527 wxPrintf(_T("'%s' %s '%s' (%s)\n"),
6529 matches
? _T("matches") : _T("doesn't match"),
6531 matches
== data
.matches
? _T("ok") : _T("ERROR"));
6537 // Sigh, I want Test::Simple, Test::More and Test::Harness...
6538 void ok(int line
, bool ok
, const wxString
& msg
= wxEmptyString
)
6541 wxPuts(_T("NOT OK: (") + wxString::Format(_T("%d"), line
) +
6545 void is(int line
, const wxString
& got
, const wxString
& expected
,
6546 const wxString
& msg
= wxEmptyString
)
6548 bool isOk
= got
== expected
;
6549 ok(line
, isOk
, msg
);
6552 wxPuts(_T("Got: ") + got
);
6553 wxPuts(_T("Expected: ") + expected
);
6558 void is(int line
, const wxChar
* got
, const wxChar
* expected
,
6559 const wxString
& msg
= wxEmptyString
)
6561 bool isOk
= wxStrcmp( got
, expected
) == 0;
6562 ok(line
, isOk
, msg
);
6565 wxPuts(_T("Got: ") + wxString(got
));
6566 wxPuts(_T("Expected: ") + wxString(expected
));
6571 void is(int line
, const wxChar
& got
, const wxChar
& expected
,
6572 const wxString
& msg
= wxEmptyString
)
6574 bool isOk
= got
== expected
;
6575 ok(line
, isOk
, msg
);
6578 wxPuts(_T("Got: ") + got
);
6579 wxPuts(_T("Expected: ") + expected
);
6583 void is(int line
, size_t got
, size_t expected
,
6584 const wxString
& msg
= wxEmptyString
)
6586 bool isOk
= got
== expected
;
6587 ok(line
, isOk
, msg
);
6590 wxPuts(wxString::Format(_T("Got: %ld"), got
));
6591 wxPuts(wxString::Format(_T("Expected: %ld"), expected
));
6595 #define is_m( got, expected, message ) is( __LINE__, (got), (expected), (message) )
6596 #define is_nom( got, expected ) is( __LINE__, (got), (expected), wxEmptyString )
6598 void TestStdString()
6600 wxPuts(_T("*** Testing std::string operations ***\n"));
6603 wxString
s1(_T("abcdefgh")),
6604 s2(_T("abcdefghijklm"), 8),
6605 s3(_T("abcdefghijklm")),
6609 s7(s3
.begin(), s3
.begin() + 8);
6610 wxString
s8(s1
, 4, 8), s9
, s10
, s11
;
6612 is( __LINE__
, s1
, _T("abcdefgh") );
6613 is( __LINE__
, s2
, s1
);
6614 is( __LINE__
, s4
, _T("aaaaaaaa") );
6615 is( __LINE__
, s5
, _T("abcdefgh") );
6616 is( __LINE__
, s6
, s1
);
6617 is( __LINE__
, s7
, s1
);
6618 is( __LINE__
, s8
, _T("efgh") );
6621 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6622 s1
.append(_T("def"));
6623 s2
.append(_T("defgh"), 3);
6624 s3
.append(wxString(_T("abcdef")), 3, 6);
6626 s5
.append(3, _T('a'));
6627 s6
.append(s1
.begin() + 3, s1
.end());
6629 is( __LINE__
, s1
, _T("abcdef") );
6630 is( __LINE__
, s2
, _T("abcdef") );
6631 is( __LINE__
, s3
, _T("abcdef") );
6632 is( __LINE__
, s4
, _T("abcabcdef") );
6633 is( __LINE__
, s5
, _T("abcaaa") );
6634 is( __LINE__
, s6
, _T("abcdef") );
6637 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("abc");
6638 s1
.assign(_T("def"));
6639 s2
.assign(_T("defgh"), 3);
6640 s3
.assign(wxString(_T("abcdef")), 3, 6);
6642 s5
.assign(3, _T('a'));
6643 s6
.assign(s1
.begin() + 1, s1
.end());
6645 is( __LINE__
, s1
, _T("def") );
6646 is( __LINE__
, s2
, _T("def") );
6647 is( __LINE__
, s3
, _T("def") );
6648 is( __LINE__
, s4
, _T("def") );
6649 is( __LINE__
, s5
, _T("aaa") );
6650 is( __LINE__
, s6
, _T("ef") );
6653 s1
= _T("abcdefgh");
6654 s2
= _T("abcdefgh");
6656 s4
= _T("abcdefghi");
6659 s7
= _T("zabcdefg");
6661 ok( __LINE__
, s1
.compare(s2
) == 0 );
6662 ok( __LINE__
, s1
.compare(s3
) > 0 );
6663 ok( __LINE__
, s1
.compare(s4
) < 0 );
6664 ok( __LINE__
, s1
.compare(s5
) > 0 );
6665 ok( __LINE__
, s1
.compare(s6
) < 0 );
6666 ok( __LINE__
, s1
.compare(1, 12, s1
) > 0);
6667 ok( __LINE__
, s1
.compare(_T("abcdefgh")) == 0);
6668 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh")) == 0);
6669 ok( __LINE__
, s1
.compare(1, 7, _T("bcdefgh"), 7) == 0);
6674 wxString::iterator it
= s3
.erase(s3
.begin() + 1);
6675 wxString::iterator it2
= s4
.erase(s4
.begin() + 4, s4
.begin() + 6);
6676 wxString::iterator it3
= s7
.erase(s7
.begin() + 4, s7
.begin() + 8);
6678 is( __LINE__
, s1
, _T("acdefgh") );
6679 is( __LINE__
, s2
, _T("abcd") );
6680 is( __LINE__
, s3
, _T("ac") );
6681 is( __LINE__
, s4
, _T("abcdghi") );
6682 is( __LINE__
, s7
, _T("zabc") );
6683 is( __LINE__
, *it
, _T('c') );
6684 is( __LINE__
, *it2
, _T('g') );
6685 ok( __LINE__
, it3
== s7
.end() );
6689 // 01234567890123456789012345
6690 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6693 is_nom( s1
.find(_T('A')), 7u );
6694 is_nom( s1
.find(_T('A'), 7), 7u );
6695 is_nom( s1
.find(_T('Z')), wxString::npos
);
6696 is_nom( s1
.find(_T('C'), 22), 25u );
6698 is_nom( s1
.find(_T("gAB")), 6u );
6699 is_nom( s1
.find(_T("gAB"), 7), wxString::npos
);
6700 is_nom( s1
.find(_T("gAB"), 6), 6u );
6702 is_nom( s1
.find(_T("gABZZZ"), 2, 3), 6u );
6703 is_nom( s1
.find(_T("gABZZZ"), 7, 3), wxString::npos
);
6705 is_nom( s1
.find(s2
), 6u );
6706 is_nom( s1
.find(s2
, 7), wxString::npos
);
6707 is_nom( s1
.find(s2
, 6), 6u );
6709 // find_first_not_of
6711 // 01234567890123456789012345678901234
6712 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6715 is_nom( s1
.find_first_not_of(_T('a')), 6u );
6716 is_nom( s1
.find_first_not_of(_T('a'), 7), 7u );
6717 is_nom( s2
.find_first_not_of(_T('a')), wxString::npos
);
6719 is_nom( s1
.find_first_not_of(_T("abde"), 4), 7u );
6720 is_nom( s1
.find_first_not_of(_T("abde"), 7), 7u );
6721 is_nom( s1
.find_first_not_of(_T("abcdefghijkl")), wxString::npos
);
6723 is_nom( s1
.find_first_not_of(_T("abcdefghi"), 0, 4), 9u );
6726 is_nom( s1
.find_first_of(_T('c')), 7u );
6727 is_nom( s1
.find_first_of(_T('v')), wxString::npos
);
6728 is_nom( s1
.find_first_of(_T('c'), 10), 24u );
6730 is_nom( s1
.find_first_of(_T("ijkl")), 13u );
6731 is_nom( s1
.find_first_of(_T("ddcfg"), 17), 24u );
6732 is_nom( s1
.find_first_of(_T("ddcfga"), 17, 5), 24u );
6736 // 01234567890123456789012345678901234
6737 s1
= _T("aaaaaabcdefghlkjiaaaaaabcdbcdbcdbcd");
6740 is_nom( s2
.find_last_not_of(_T('a')), wxString::npos
);
6741 is_nom( s1
.find_last_not_of(_T('d')), 33u );
6742 is_nom( s1
.find_last_not_of(_T('d'), 25), 24u );
6744 is_nom( s1
.find_last_not_of(_T("bcd")), 22u );
6745 is_nom( s1
.find_last_not_of(_T("abc"), 24), 16u );
6747 is_nom( s1
.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 24, 3), 16u );
6750 is_nom( s2
.find_last_of(_T('c')), wxString::npos
);
6751 is_nom( s1
.find_last_of(_T('a')), 22u );
6752 is_nom( s1
.find_last_of(_T('b'), 24), 23u );
6754 is_nom( s1
.find_last_of(_T("ijklm")), 16u );
6755 is_nom( s1
.find_last_of(_T("ijklma"), 33, 4), 16u );
6756 is_nom( s1
.find_last_of(_T("a"), 17), 17u );
6759 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("aaaa");
6760 s9
= s10
= _T("cdefg");
6762 s1
.insert(1, _T("cc") );
6763 s2
.insert(2, _T("cdef"), 3);
6765 s4
.insert(2, s10
, 3, 7);
6766 s5
.insert(1, 2, _T('c'));
6767 it
= s6
.insert(s6
.begin() + 3, _T('X'));
6768 s7
.insert(s7
.begin(), s9
.begin(), s9
.end() - 1);
6769 s8
.insert(s8
.begin(), 2, _T('c'));
6771 is( __LINE__
, s1
, _T("accaaa") );
6772 is( __LINE__
, s2
, _T("aacdeaa") );
6773 is( __LINE__
, s3
, _T("aacdefgaa") );
6774 is( __LINE__
, s4
, _T("aafgaa") );
6775 is( __LINE__
, s5
, _T("accaaa") );
6776 is( __LINE__
, s6
, _T("aaaXa") );
6777 is( __LINE__
, s7
, _T("cdefaaaa") );
6778 is( __LINE__
, s8
, _T("ccaaaa") );
6780 s1
= s2
= s3
= _T("aaaa");
6781 s1
.insert(0, _T("ccc"), 2);
6782 s2
.insert(4, _T("ccc"), 2);
6784 is( __LINE__
, s1
, _T("ccaaaa") );
6785 is( __LINE__
, s2
, _T("aaaacc") );
6788 s1
= s2
= s3
= s4
= s5
= s6
= s7
= s8
= _T("QWERTYUIOP");
6789 s9
= s10
= _T("werty");
6791 s1
.replace(3, 4, _T("rtyu"));
6792 s1
.replace(8, 7, _T("opopop"));
6793 s2
.replace(10, 12, _T("WWWW"));
6794 s3
.replace(1, 5, s9
);
6795 s4
.replace(1, 4, s9
, 0, 4);
6796 s5
.replace(1, 2, s9
, 1, 12);
6797 s6
.replace(0, 123, s9
, 0, 123);
6798 s7
.replace(2, 7, s9
);
6800 is( __LINE__
, s1
, _T("QWErtyuIopopop") );
6801 is( __LINE__
, s2
, _T("QWERTYUIOPWWWW") );
6802 is( __LINE__
, s3
, _T("QwertyUIOP") );
6803 is( __LINE__
, s4
, _T("QwertYUIOP") );
6804 is( __LINE__
, s5
, _T("QertyRTYUIOP") );
6805 is( __LINE__
, s6
, s9
);
6806 is( __LINE__
, s7
, _T("QWwertyP") );
6810 // 01234567890123456789012345
6811 s1
= _T("abcdefgABCDEFGabcABCabcABC");
6815 is_nom( s1
.rfind(_T('A')), 23u );
6816 is_nom( s1
.rfind(_T('A'), 7), 7u );
6817 is_nom( s1
.rfind(_T('Z')), wxString::npos
);
6818 is_nom( s1
.rfind(_T('C'), 22), 19u );
6820 is_nom( s1
.rfind(_T("cAB")), 22u );
6821 is_nom( s1
.rfind(_T("cAB"), 15), wxString::npos
);
6822 is_nom( s1
.rfind(_T("cAB"), 21), 16u );
6824 is_nom( s1
.rfind(_T("gABZZZ"), 7, 3), 6u );
6825 is_nom( s1
.rfind(_T("gABZZZ"), 5, 3), wxString::npos
);
6827 is_nom( s1
.rfind(s2
), 6u );
6828 is_nom( s1
.rfind(s2
, 5), wxString::npos
);
6829 is_nom( s1
.rfind(s2
, 6), 6u );
6830 is_nom( s1
.rfind(s3
, 1), 0u );
6833 s1
= s2
= s3
= s4
= _T("abcABCdefDEF");
6837 s3
.resize( 14, _T(' ') );
6838 s4
.resize( 14, _T('W') );
6840 is_nom( s1
, _T("abcABCdefDEF") );
6841 is_nom( s2
, _T("abcABCdefD") );
6842 is_nom( s3
, _T("abcABCdefDEF ") );
6843 is_nom( s4
, _T("abcABCdefDEFWW") );
6846 s1
= _T("abcdefgABCDEFG");
6848 is_nom( s1
.substr( 0, 14 ), s1
);
6849 is_nom( s1
.substr( 1, 13 ), _T("bcdefgABCDEFG") );
6850 is_nom( s1
.substr( 1, 20 ), _T("bcdefgABCDEFG") );
6851 is_nom( s1
.substr( 14, 30 ), _T("") );
6853 wxPuts(_T("*** Testing std::string operations finished ***\n"));
6856 #endif // TEST_STRINGS
6858 // ----------------------------------------------------------------------------
6860 // ----------------------------------------------------------------------------
6862 #ifdef TEST_SNGLINST
6863 #include "wx/snglinst.h"
6864 #endif // TEST_SNGLINST
6866 int main(int argc
, char **argv
)
6868 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
6870 wxInitializer initializer
;
6873 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
6878 #ifdef TEST_SNGLINST
6879 wxSingleInstanceChecker checker
;
6880 if ( checker
.Create(_T(".wxconsole.lock")) )
6882 if ( checker
.IsAnotherRunning() )
6884 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
6889 // wait some time to give time to launch another instance
6890 wxPrintf(_T("Press \"Enter\" to continue..."));
6893 else // failed to create
6895 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
6897 #endif // TEST_SNGLINST
6901 #endif // TEST_CHARSET
6904 TestCmdLineConvert();
6906 #if wxUSE_CMDLINE_PARSER
6907 static const wxCmdLineEntryDesc cmdLineDesc
[] =
6909 { wxCMD_LINE_SWITCH
, _T("h"), _T("help"), _T("show this help message"),
6910 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
6911 { wxCMD_LINE_SWITCH
, _T("v"), _T("verbose"), _T("be verbose") },
6912 { wxCMD_LINE_SWITCH
, _T("q"), _T("quiet"), _T("be quiet") },
6914 { wxCMD_LINE_OPTION
, _T("o"), _T("output"), _T("output file") },
6915 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("input dir") },
6916 { wxCMD_LINE_OPTION
, _T("s"), _T("size"), _T("output block size"),
6917 wxCMD_LINE_VAL_NUMBER
},
6918 { wxCMD_LINE_OPTION
, _T("d"), _T("date"), _T("output file date"),
6919 wxCMD_LINE_VAL_DATE
},
6921 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("input file"),
6922 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
6928 wxChar
**wargv
= new wxChar
*[argc
+ 1];
6933 for (n
= 0; n
< argc
; n
++ )
6935 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
6936 wargv
[n
] = wxStrdup(warg
);
6943 #endif // wxUSE_UNICODE
6945 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
6949 for ( int n
= 0; n
< argc
; n
++ )
6954 #endif // wxUSE_UNICODE
6956 parser
.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
6957 wxCMD_LINE_VAL_STRING
,
6958 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
6960 switch ( parser
.Parse() )
6963 wxLogMessage(_T("Help was given, terminating."));
6967 ShowCmdLine(parser
);
6971 wxLogMessage(_T("Syntax error detected, aborting."));
6974 #endif // wxUSE_CMDLINE_PARSER
6976 #endif // TEST_CMDLINE
6984 TestStringConstruction();
6987 TestStringTokenizer();
6988 TestStringReplace();
6996 #endif // TEST_STRINGS
6999 if ( 1 || TEST_ALL
)
7002 a1
.Add(_T("tiger"));
7004 a1
.Add(_T("lion"), 3);
7006 a1
.Add(_T("human"));
7009 wxPuts(_T("*** Initially:"));
7011 PrintArray(_T("a1"), a1
);
7013 wxArrayString
a2(a1
);
7014 PrintArray(_T("a2"), a2
);
7017 wxSortedArrayString
a3(a1
);
7019 wxSortedArrayString a3
;
7020 for (wxArrayString::iterator it
= a1
.begin(), en
= a1
.end();
7024 PrintArray(_T("a3"), a3
);
7026 wxPuts(_T("*** After deleting three strings from a1"));
7029 PrintArray(_T("a1"), a1
);
7030 PrintArray(_T("a2"), a2
);
7031 PrintArray(_T("a3"), a3
);
7034 wxPuts(_T("*** After reassigning a1 to a2 and a3"));
7036 PrintArray(_T("a2"), a2
);
7037 PrintArray(_T("a3"), a3
);
7040 wxPuts(_T("*** After sorting a1"));
7042 PrintArray(_T("a1"), a1
);
7044 wxPuts(_T("*** After sorting a1 in reverse order"));
7046 PrintArray(_T("a1"), a1
);
7049 wxPuts(_T("*** After sorting a1 by the string length"));
7050 a1
.Sort(&StringLenCompare
);
7051 PrintArray(_T("a1"), a1
);
7054 TestArrayOfObjects();
7055 TestArrayOfUShorts();
7060 #endif // TEST_ARRAYS
7071 #ifdef TEST_DLLLOADER
7073 #endif // TEST_DLLLOADER
7077 #endif // TEST_ENVIRON
7081 #endif // TEST_EXECUTE
7083 #ifdef TEST_FILECONF
7085 #endif // TEST_FILECONF
7094 #endif // TEST_LOCALE
7097 wxPuts(_T("*** Testing wxLog ***"));
7100 for ( size_t n
= 0; n
< 8000; n
++ )
7102 s
<< (wxChar
)(_T('A') + (n
% 26));
7105 wxLogWarning(_T("The length of the string is %lu"),
7106 (unsigned long)s
.length());
7109 msg
.Printf(_T("A very very long message: '%s', the end!\n"), s
.c_str());
7111 // this one shouldn't be truncated
7114 // but this one will because log functions use fixed size buffer
7115 // (note that it doesn't need '\n' at the end neither - will be added
7117 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s
.c_str());
7129 #ifdef TEST_FILENAME
7133 fn
.Assign(_T("c:\\foo"), _T("bar.baz"));
7134 fn
.Assign(_T("/u/os9-port/Viewer/tvision/WEI2HZ-3B3-14_05-04-00MSC1.asc"));
7139 TestFileNameConstruction();
7142 TestFileNameConstruction();
7143 TestFileNameMakeRelative();
7144 TestFileNameMakeAbsolute();
7145 TestFileNameSplit();
7148 TestFileNameComparison();
7149 TestFileNameOperations();
7151 #endif // TEST_FILENAME
7153 #ifdef TEST_FILETIME
7157 #endif // TEST_FILETIME
7160 wxLog::AddTraceMask(FTP_TRACE_MASK
);
7161 if ( TestFtpConnect() )
7172 if ( TEST_INTERACTIVE
)
7173 TestFtpInteractive();
7175 //else: connecting to the FTP server failed
7181 #ifdef TEST_LONGLONG
7182 // seed pseudo random generator
7183 srand((unsigned)time(NULL
));
7192 TestMultiplication();
7195 TestLongLongConversion();
7196 TestBitOperations();
7197 TestLongLongComparison();
7198 TestLongLongToString();
7199 TestLongLongPrintf();
7201 #endif // TEST_LONGLONG
7209 #endif // TEST_HASHMAP
7213 #endif // TEST_HASHSET
7216 wxLog::AddTraceMask(_T("mime"));
7221 TestMimeAssociate();
7226 #ifdef TEST_INFO_FUNCTIONS
7232 if ( TEST_INTERACTIVE
)
7235 #endif // TEST_INFO_FUNCTIONS
7237 #ifdef TEST_PATHLIST
7239 #endif // TEST_PATHLIST
7247 #endif // TEST_PRINTF
7251 #endif // TEST_REGCONF
7254 // TODO: write a real test using src/regex/tests file
7259 TestRegExSubmatch();
7260 TestRegExReplacement();
7262 if ( TEST_INTERACTIVE
)
7263 TestRegExInteractive();
7265 #endif // TEST_REGEX
7267 #ifdef TEST_REGISTRY
7269 TestRegistryAssociation();
7270 #endif // TEST_REGISTRY
7275 #endif // TEST_SOCKETS
7283 #endif // TEST_STREAMS
7285 #ifdef TEST_TEXTSTREAM
7286 TestTextInputStream();
7287 #endif // TEST_TEXTSTREAM
7290 int nCPUs
= wxThread::GetCPUCount();
7291 wxPrintf(_T("This system has %d CPUs\n"), nCPUs
);
7293 wxThread::SetConcurrency(nCPUs
);
7295 TestDetachedThreads();
7298 TestJoinableThreads();
7299 TestThreadSuspend();
7301 TestThreadConditions();
7305 #endif // TEST_THREADS
7309 #endif // TEST_TIMER
7311 #ifdef TEST_DATETIME
7324 TestTimeArithmetics();
7327 TestTimeSpanFormat();
7335 if ( TEST_INTERACTIVE
)
7336 TestDateTimeInteractive();
7337 #endif // TEST_DATETIME
7339 #ifdef TEST_SCOPEGUARD
7344 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
7346 #endif // TEST_USLEEP
7351 #endif // TEST_VCARD
7355 #endif // TEST_VOLUME
7358 TestUnicodeToFromAscii();
7359 #endif // TEST_UNICODE
7363 TestEncodingConverter();
7364 #endif // TEST_WCHAR
7367 TestZipStreamRead();
7368 TestZipFileSystem();
7372 TestZlibStreamWrite();
7373 TestZlibStreamRead();