1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // IMPORTANT NOTE FOR WXWIDGETS USERS:
13 // If you're a wxWidgets user and you're looking at this file to learn how to
14 // structure a wxWidgets console application, then you don't have much to learn.
15 // This application is used more for testing rather than as sample but
16 // basically the following simple block is enough for you to start your
17 // own console application:
20 int main(int argc, char **argv)
22 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
24 wxInitializer initializer;
27 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
31 static const wxCmdLineEntryDesc cmdLineDesc[] =
33 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
34 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
35 // ... your other command line options here...
40 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
41 switch ( parser.Parse() )
44 wxLogMessage(wxT("Help was given, terminating."));
48 // everything is ok; proceed
52 wxLogMessage(wxT("Syntax error detected, aborting."));
56 // do something useful here
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
75 #include "wx/string.h"
77 #include "wx/filename.h"
80 #include "wx/apptrait.h"
81 #include "wx/platinfo.h"
82 #include "wx/wxchar.h"
84 // without this pragma, the stupid compiler precompiles #defines below so that
85 // changing them doesn't "take place" later!
90 // ----------------------------------------------------------------------------
91 // conditional compilation
92 // ----------------------------------------------------------------------------
95 A note about all these conditional compilation macros: this file is used
96 both as a test suite for various non-GUI wxWidgets classes and as a
97 scratchpad for quick tests. So there are two compilation modes: if you
98 define TEST_ALL all tests are run, otherwise you may enable the individual
99 tests individually in the "#else" branch below.
102 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
103 // test, define it to 1 to do all tests.
109 #define TEST_DATETIME
114 #define TEST_FILECONF
115 #define TEST_FILENAME
116 #define TEST_FILETIME
118 #define TEST_INFO_FUNCTIONS
123 #define TEST_PATHLIST
127 #define TEST_REGISTRY
128 #define TEST_SCOPEGUARD
129 #define TEST_SNGLINST
130 // #define TEST_SOCKETS --FIXME! (RN)
131 #define TEST_STACKWALKER
132 #define TEST_STDPATHS
135 // #define TEST_VOLUME --FIXME! (RN)
136 #else // #if TEST_ALL
137 #define TEST_DATETIME
140 // some tests are interactive, define this to run them
141 #ifdef TEST_INTERACTIVE
142 #undef TEST_INTERACTIVE
144 #define TEST_INTERACTIVE 1
146 #define TEST_INTERACTIVE 1
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 #if defined(TEST_SOCKETS)
159 // replace TABs with \t and CRs with \n
160 static wxString
MakePrintable(const wxChar
*s
)
163 (void)str
.Replace(wxT("\t"), wxT("\\t"));
164 (void)str
.Replace(wxT("\n"), wxT("\\n"));
165 (void)str
.Replace(wxT("\r"), wxT("\\r"));
170 #endif // MakePrintable() is used
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
178 #include "wx/cmdline.h"
179 #include "wx/datetime.h"
181 #if wxUSE_CMDLINE_PARSER
183 static void ShowCmdLine(const wxCmdLineParser
& parser
)
185 wxString s
= wxT("Command line parsed successfully:\nInput files: ");
187 size_t count
= parser
.GetParamCount();
188 for ( size_t param
= 0; param
< count
; param
++ )
190 s
<< parser
.GetParam(param
) << ' ';
194 << wxT("Verbose:\t") << (parser
.Found(wxT("v")) ? wxT("yes") : wxT("no")) << '\n'
195 << wxT("Quiet:\t") << (parser
.Found(wxT("q")) ? wxT("yes") : wxT("no")) << '\n';
201 if ( parser
.Found(wxT("o"), &strVal
) )
202 s
<< wxT("Output file:\t") << strVal
<< '\n';
203 if ( parser
.Found(wxT("i"), &strVal
) )
204 s
<< wxT("Input dir:\t") << strVal
<< '\n';
205 if ( parser
.Found(wxT("s"), &lVal
) )
206 s
<< wxT("Size:\t") << lVal
<< '\n';
207 if ( parser
.Found(wxT("f"), &dVal
) )
208 s
<< wxT("Double:\t") << dVal
<< '\n';
209 if ( parser
.Found(wxT("d"), &dt
) )
210 s
<< wxT("Date:\t") << dt
.FormatISODate() << '\n';
211 if ( parser
.Found(wxT("project_name"), &strVal
) )
212 s
<< wxT("Project:\t") << strVal
<< '\n';
217 #endif // wxUSE_CMDLINE_PARSER
219 static void TestCmdLineConvert()
221 static const wxChar
*cmdlines
[] =
224 wxT("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
225 wxT("literal \\\" and \"\""),
228 for ( size_t n
= 0; n
< WXSIZEOF(cmdlines
); n
++ )
230 const wxChar
*cmdline
= cmdlines
[n
];
231 wxPrintf(wxT("Parsing: %s\n"), cmdline
);
232 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdline
);
234 size_t count
= args
.GetCount();
235 wxPrintf(wxT("\targc = %u\n"), count
);
236 for ( size_t arg
= 0; arg
< count
; arg
++ )
238 wxPrintf(wxT("\targv[%u] = %s\n"), arg
, args
[arg
].c_str());
243 #endif // TEST_CMDLINE
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
254 static const wxChar
*ROOTDIR
= wxT("/");
255 static const wxChar
*TESTDIR
= wxT("/usr/local/share");
256 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
257 static const wxChar
*ROOTDIR
= wxT("c:\\");
258 static const wxChar
*TESTDIR
= wxT("d:\\");
260 #error "don't know where the root directory is"
263 static void TestDirEnumHelper(wxDir
& dir
,
264 int flags
= wxDIR_DEFAULT
,
265 const wxString
& filespec
= wxEmptyString
)
269 if ( !dir
.IsOpened() )
272 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
275 wxPrintf(wxT("\t%s\n"), filename
.c_str());
277 cont
= dir
.GetNext(&filename
);
280 wxPuts(wxEmptyString
);
285 static void TestDirEnum()
287 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
289 wxString cwd
= wxGetCwd();
290 if ( !wxDir::Exists(cwd
) )
292 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
297 if ( !dir
.IsOpened() )
299 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
303 wxPuts(wxT("Enumerating everything in current directory:"));
304 TestDirEnumHelper(dir
);
306 wxPuts(wxT("Enumerating really everything in current directory:"));
307 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
309 wxPuts(wxT("Enumerating object files in current directory:"));
310 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, wxT("*.o*"));
312 wxPuts(wxT("Enumerating directories in current directory:"));
313 TestDirEnumHelper(dir
, wxDIR_DIRS
);
315 wxPuts(wxT("Enumerating files in current directory:"));
316 TestDirEnumHelper(dir
, wxDIR_FILES
);
318 wxPuts(wxT("Enumerating files including hidden in current directory:"));
319 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
323 wxPuts(wxT("Enumerating everything in root directory:"));
324 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
326 wxPuts(wxT("Enumerating directories in root directory:"));
327 TestDirEnumHelper(dir
, wxDIR_DIRS
);
329 wxPuts(wxT("Enumerating files in root directory:"));
330 TestDirEnumHelper(dir
, wxDIR_FILES
);
332 wxPuts(wxT("Enumerating files including hidden in root directory:"));
333 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
335 wxPuts(wxT("Enumerating files in non existing directory:"));
336 wxDir
dirNo(wxT("nosuchdir"));
337 TestDirEnumHelper(dirNo
);
342 class DirPrintTraverser
: public wxDirTraverser
345 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
347 return wxDIR_CONTINUE
;
350 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
352 wxString path
, name
, ext
;
353 wxFileName::SplitPath(dirname
, &path
, &name
, &ext
);
356 name
<< wxT('.') << ext
;
359 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
361 if ( wxIsPathSeparator(*p
) )
365 wxPrintf(wxT("%s%s\n"), indent
.c_str(), name
.c_str());
367 return wxDIR_CONTINUE
;
371 static void TestDirTraverse()
373 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
377 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
378 wxPrintf(wxT("There are %u files under '%s'\n"), n
, TESTDIR
);
381 wxPrintf(wxT("First one is '%s'\n"), files
[0u].c_str());
382 wxPrintf(wxT(" last one is '%s'\n"), files
[n
- 1].c_str());
385 // enum again with custom traverser
386 wxPuts(wxT("Now enumerating directories:"));
388 DirPrintTraverser traverser
;
389 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
394 static void TestDirExists()
396 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
398 static const wxChar
*dirnames
[] =
401 #if defined(__WXMSW__)
404 wxT("\\\\share\\file"),
408 wxT("c:\\autoexec.bat"),
409 #elif defined(__UNIX__)
418 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
420 wxPrintf(wxT("%-40s: %s\n"),
422 wxDir::Exists(dirnames
[n
]) ? wxT("exists")
423 : wxT("doesn't exist"));
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
437 #include "wx/dynlib.h"
439 static void TestDllLoad()
441 #if defined(__WXMSW__)
442 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
443 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
444 #elif defined(__UNIX__)
445 // weird: using just libc.so does *not* work!
446 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
447 static const wxChar
*FUNC_NAME
= wxT("strlen");
449 #error "don't know how to test wxDllLoader on this platform"
452 wxPuts(wxT("*** testing basic wxDynamicLibrary functions ***\n"));
454 wxDynamicLibrary
lib(LIB_NAME
);
455 if ( !lib
.IsLoaded() )
457 wxPrintf(wxT("ERROR: failed to load '%s'.\n"), LIB_NAME
);
461 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
462 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
465 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
466 FUNC_NAME
, LIB_NAME
);
470 wxPrintf(wxT("Calling %s dynamically loaded from %s "),
471 FUNC_NAME
, LIB_NAME
);
473 if ( pfnStrlen("foo") != 3 )
475 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
479 wxPuts(wxT("... ok"));
484 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
486 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
488 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
489 if ( !pfnStrlenAorW
)
491 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
492 FUNC_NAME_AW
, LIB_NAME
);
496 if ( pfnStrlenAorW(wxT("foobar")) != 6 )
498 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
505 #if defined(__WXMSW__) || defined(__UNIX__)
507 static void TestDllListLoaded()
509 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
511 puts("\nLoaded modules:");
512 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
513 const size_t count
= dlls
.GetCount();
514 for ( size_t n
= 0; n
< count
; ++n
)
516 const wxDynamicLibraryDetails
& details
= dlls
[n
];
517 printf("%-45s", (const char *)details
.GetPath().mb_str());
519 void *addr
wxDUMMY_INITIALIZE(NULL
);
520 size_t len
wxDUMMY_INITIALIZE(0);
521 if ( details
.GetAddress(&addr
, &len
) )
523 printf(" %08lx:%08lx",
524 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
527 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
533 #endif // TEST_DYNLIB
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
541 #include "wx/utils.h"
543 static wxString
MyGetEnv(const wxString
& var
)
546 if ( !wxGetEnv(var
, &val
) )
547 val
= wxT("<empty>");
549 val
= wxString(wxT('\'')) + val
+ wxT('\'');
554 static void TestEnvironment()
556 const wxChar
*var
= wxT("wxTestVar");
558 wxPuts(wxT("*** testing environment access functions ***"));
560 wxPrintf(wxT("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
561 wxSetEnv(var
, wxT("value for wxTestVar"));
562 wxPrintf(wxT("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
563 wxSetEnv(var
, wxT("another value"));
564 wxPrintf(wxT("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
566 wxPrintf(wxT("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
567 wxPrintf(wxT("PATH = %s\n"), MyGetEnv(wxT("PATH")).c_str());
570 #endif // TEST_ENVIRON
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
579 #include "wx/ffile.h"
580 #include "wx/textfile.h"
582 static void TestFileRead()
584 wxPuts(wxT("*** wxFile read test ***"));
586 wxFile
file(wxT("testdata.fc"));
587 if ( file
.IsOpened() )
589 wxPrintf(wxT("File length: %lu\n"), file
.Length());
591 wxPuts(wxT("File dump:\n----------"));
593 static const size_t len
= 1024;
597 size_t nRead
= file
.Read(buf
, len
);
598 if ( nRead
== (size_t)wxInvalidOffset
)
600 wxPrintf(wxT("Failed to read the file."));
604 fwrite(buf
, nRead
, 1, stdout
);
610 wxPuts(wxT("----------"));
614 wxPrintf(wxT("ERROR: can't open test file.\n"));
617 wxPuts(wxEmptyString
);
620 static void TestTextFileRead()
622 wxPuts(wxT("*** wxTextFile read test ***"));
624 wxTextFile
file(wxT("testdata.fc"));
627 wxPrintf(wxT("Number of lines: %u\n"), file
.GetLineCount());
628 wxPrintf(wxT("Last line: '%s'\n"), file
.GetLastLine().c_str());
632 wxPuts(wxT("\nDumping the entire file:"));
633 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
635 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
637 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
639 wxPuts(wxT("\nAnd now backwards:"));
640 for ( s
= file
.GetLastLine();
641 file
.GetCurrentLine() != 0;
642 s
= file
.GetPrevLine() )
644 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
646 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
650 wxPrintf(wxT("ERROR: can't open '%s'\n"), file
.GetName());
653 wxPuts(wxEmptyString
);
656 static void TestFileCopy()
658 wxPuts(wxT("*** Testing wxCopyFile ***"));
660 static const wxChar
*filename1
= wxT("testdata.fc");
661 static const wxChar
*filename2
= wxT("test2");
662 if ( !wxCopyFile(filename1
, filename2
) )
664 wxPuts(wxT("ERROR: failed to copy file"));
668 wxFFile
f1(filename1
, wxT("rb")),
669 f2(filename2
, wxT("rb"));
671 if ( !f1
.IsOpened() || !f2
.IsOpened() )
673 wxPuts(wxT("ERROR: failed to open file(s)"));
678 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
680 wxPuts(wxT("ERROR: failed to read file(s)"));
684 if ( (s1
.length() != s2
.length()) ||
685 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
687 wxPuts(wxT("ERROR: copy error!"));
691 wxPuts(wxT("File was copied ok."));
697 if ( !wxRemoveFile(filename2
) )
699 wxPuts(wxT("ERROR: failed to remove the file"));
702 wxPuts(wxEmptyString
);
705 static void TestTempFile()
707 wxPuts(wxT("*** wxTempFile test ***"));
710 if ( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) )
712 if ( tmpFile
.Commit() )
713 wxPuts(wxT("File committed."));
715 wxPuts(wxT("ERROR: could't commit temp file."));
717 wxRemoveFile(wxT("test2"));
720 wxPuts(wxEmptyString
);
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
731 #include "wx/confbase.h"
732 #include "wx/fileconf.h"
734 static const struct FileConfTestData
736 const wxChar
*name
; // value name
737 const wxChar
*value
; // the value from the file
740 { wxT("value1"), wxT("one") },
741 { wxT("value2"), wxT("two") },
742 { wxT("novalue"), wxT("default") },
745 static void TestFileConfRead()
747 wxPuts(wxT("*** testing wxFileConfig loading/reading ***"));
749 wxFileConfig
fileconf(wxT("test"), wxEmptyString
,
750 wxT("testdata.fc"), wxEmptyString
,
751 wxCONFIG_USE_RELATIVE_PATH
);
753 // test simple reading
754 wxPuts(wxT("\nReading config file:"));
755 wxString
defValue(wxT("default")), value
;
756 for ( size_t n
= 0; n
< WXSIZEOF(fcTestData
); n
++ )
758 const FileConfTestData
& data
= fcTestData
[n
];
759 value
= fileconf
.Read(data
.name
, defValue
);
760 wxPrintf(wxT("\t%s = %s "), data
.name
, value
.c_str());
761 if ( value
== data
.value
)
767 wxPrintf(wxT("(ERROR: should be %s)\n"), data
.value
);
771 // test enumerating the entries
772 wxPuts(wxT("\nEnumerating all root entries:"));
775 bool cont
= fileconf
.GetFirstEntry(name
, dummy
);
778 wxPrintf(wxT("\t%s = %s\n"),
780 fileconf
.Read(name
.c_str(), wxT("ERROR")).c_str());
782 cont
= fileconf
.GetNextEntry(name
, dummy
);
785 static const wxChar
*testEntry
= wxT("TestEntry");
786 wxPrintf(wxT("\nTesting deletion of newly created \"Test\" entry: "));
787 fileconf
.Write(testEntry
, wxT("A value"));
788 fileconf
.DeleteEntry(testEntry
);
789 wxPrintf(fileconf
.HasEntry(testEntry
) ? wxT("ERROR\n") : wxT("ok\n"));
792 #endif // TEST_FILECONF
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
800 #include "wx/filename.h"
803 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
807 wxString full
= fn
.GetFullPath();
809 wxString vol
, path
, name
, ext
;
810 wxFileName::SplitPath(full
, &vol
, &path
, &name
, &ext
);
812 wxPrintf(wxT("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
813 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
815 wxFileName::SplitPath(full
, &path
, &name
, &ext
);
816 wxPrintf(wxT("or\t\t-> path '%s', name '%s', ext '%s'\n"),
817 path
.c_str(), name
.c_str(), ext
.c_str());
819 wxPrintf(wxT("path is also:\t'%s'\n"), fn
.GetPath().c_str());
820 wxPrintf(wxT("with volume: \t'%s'\n"),
821 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
822 wxPrintf(wxT("with separator:\t'%s'\n"),
823 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
824 wxPrintf(wxT("with both: \t'%s'\n"),
825 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
827 wxPuts(wxT("The directories in the path are:"));
828 wxArrayString dirs
= fn
.GetDirs();
829 size_t count
= dirs
.GetCount();
830 for ( size_t n
= 0; n
< count
; n
++ )
832 wxPrintf(wxT("\t%u: %s\n"), n
, dirs
[n
].c_str());
837 static void TestFileNameTemp()
839 wxPuts(wxT("*** testing wxFileName temp file creation ***"));
841 static const wxChar
*tmpprefixes
[] =
849 wxT("/tmp/foo/bar"), // this one must be an error
853 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
855 wxString path
= wxFileName::CreateTempFileName(tmpprefixes
[n
]);
858 // "error" is not in upper case because it may be ok
859 wxPrintf(wxT("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
863 wxPrintf(wxT("Prefix '%s'\t-> temp file '%s'\n"),
864 tmpprefixes
[n
], path
.c_str());
866 if ( !wxRemoveFile(path
) )
868 wxLogWarning(wxT("Failed to remove temp file '%s'"),
875 static void TestFileNameDirManip()
877 // TODO: test AppendDir(), RemoveDir(), ...
880 static void TestFileNameComparison()
885 static void TestFileNameOperations()
890 static void TestFileNameCwd()
895 #endif // TEST_FILENAME
897 // ----------------------------------------------------------------------------
898 // wxFileName time functions
899 // ----------------------------------------------------------------------------
903 #include "wx/filename.h"
904 #include "wx/datetime.h"
906 static void TestFileGetTimes()
908 wxFileName
fn(wxT("testdata.fc"));
910 wxDateTime dtAccess
, dtMod
, dtCreate
;
911 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
913 wxPrintf(wxT("ERROR: GetTimes() failed.\n"));
917 static const wxChar
*fmt
= wxT("%Y-%b-%d %H:%M:%S");
919 wxPrintf(wxT("File times for '%s':\n"), fn
.GetFullPath().c_str());
920 wxPrintf(wxT("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
921 wxPrintf(wxT("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
922 wxPrintf(wxT("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
927 static void TestFileSetTimes()
929 wxFileName
fn(wxT("testdata.fc"));
933 wxPrintf(wxT("ERROR: Touch() failed.\n"));
938 #endif // TEST_FILETIME
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
947 #include "wx/utils.h" // for wxSetEnv
949 static wxLocale gs_localeDefault
;
950 // NOTE: don't init it here as it needs a wxAppTraits object
951 // and thus must be init-ed after creation of the wxInitializer
952 // class in the main()
954 // find the name of the language from its value
955 static const wxChar
*GetLangName(int lang
)
957 static const wxChar
*languageNames
[] =
967 wxT("ARABIC_ALGERIA"),
968 wxT("ARABIC_BAHRAIN"),
971 wxT("ARABIC_JORDAN"),
972 wxT("ARABIC_KUWAIT"),
973 wxT("ARABIC_LEBANON"),
975 wxT("ARABIC_MOROCCO"),
978 wxT("ARABIC_SAUDI_ARABIA"),
981 wxT("ARABIC_TUNISIA"),
988 wxT("AZERI_CYRILLIC"),
1003 wxT("CHINESE_SIMPLIFIED"),
1004 wxT("CHINESE_TRADITIONAL"),
1005 wxT("CHINESE_HONGKONG"),
1006 wxT("CHINESE_MACAU"),
1007 wxT("CHINESE_SINGAPORE"),
1008 wxT("CHINESE_TAIWAN"),
1014 wxT("DUTCH_BELGIAN"),
1018 wxT("ENGLISH_AUSTRALIA"),
1019 wxT("ENGLISH_BELIZE"),
1020 wxT("ENGLISH_BOTSWANA"),
1021 wxT("ENGLISH_CANADA"),
1022 wxT("ENGLISH_CARIBBEAN"),
1023 wxT("ENGLISH_DENMARK"),
1024 wxT("ENGLISH_EIRE"),
1025 wxT("ENGLISH_JAMAICA"),
1026 wxT("ENGLISH_NEW_ZEALAND"),
1027 wxT("ENGLISH_PHILIPPINES"),
1028 wxT("ENGLISH_SOUTH_AFRICA"),
1029 wxT("ENGLISH_TRINIDAD"),
1030 wxT("ENGLISH_ZIMBABWE"),
1038 wxT("FRENCH_BELGIAN"),
1039 wxT("FRENCH_CANADIAN"),
1040 wxT("FRENCH_LUXEMBOURG"),
1041 wxT("FRENCH_MONACO"),
1042 wxT("FRENCH_SWISS"),
1047 wxT("GERMAN_AUSTRIAN"),
1048 wxT("GERMAN_BELGIUM"),
1049 wxT("GERMAN_LIECHTENSTEIN"),
1050 wxT("GERMAN_LUXEMBOURG"),
1051 wxT("GERMAN_SWISS"),
1068 wxT("ITALIAN_SWISS"),
1073 wxT("KASHMIRI_INDIA"),
1091 wxT("MALAY_BRUNEI_DARUSSALAM"),
1092 wxT("MALAY_MALAYSIA"),
1101 wxT("NEPALI_INDIA"),
1102 wxT("NORWEGIAN_BOKMAL"),
1103 wxT("NORWEGIAN_NYNORSK"),
1110 wxT("PORTUGUESE_BRAZILIAN"),
1113 wxT("RHAETO_ROMANCE"),
1116 wxT("RUSSIAN_UKRAINE"),
1120 wxT("SCOTS_GAELIC"),
1122 wxT("SERBIAN_CYRILLIC"),
1123 wxT("SERBIAN_LATIN"),
1124 wxT("SERBO_CROATIAN"),
1135 wxT("SPANISH_ARGENTINA"),
1136 wxT("SPANISH_BOLIVIA"),
1137 wxT("SPANISH_CHILE"),
1138 wxT("SPANISH_COLOMBIA"),
1139 wxT("SPANISH_COSTA_RICA"),
1140 wxT("SPANISH_DOMINICAN_REPUBLIC"),
1141 wxT("SPANISH_ECUADOR"),
1142 wxT("SPANISH_EL_SALVADOR"),
1143 wxT("SPANISH_GUATEMALA"),
1144 wxT("SPANISH_HONDURAS"),
1145 wxT("SPANISH_MEXICAN"),
1146 wxT("SPANISH_MODERN"),
1147 wxT("SPANISH_NICARAGUA"),
1148 wxT("SPANISH_PANAMA"),
1149 wxT("SPANISH_PARAGUAY"),
1150 wxT("SPANISH_PERU"),
1151 wxT("SPANISH_PUERTO_RICO"),
1152 wxT("SPANISH_URUGUAY"),
1154 wxT("SPANISH_VENEZUELA"),
1158 wxT("SWEDISH_FINLAND"),
1176 wxT("URDU_PAKISTAN"),
1178 wxT("UZBEK_CYRILLIC"),
1191 if ( (size_t)lang
< WXSIZEOF(languageNames
) )
1192 return languageNames
[lang
];
1194 return wxT("INVALID");
1197 static void TestDefaultLang()
1199 wxPuts(wxT("*** Testing wxLocale::GetSystemLanguage ***"));
1201 gs_localeDefault
.Init(wxLANGUAGE_ENGLISH
);
1203 static const wxChar
*langStrings
[] =
1205 NULL
, // system default
1212 wxT("de_DE.iso88591"),
1214 wxT("?"), // invalid lang spec
1215 wxT("klingonese"), // I bet on some systems it does exist...
1218 wxPrintf(wxT("The default system encoding is %s (%d)\n"),
1219 wxLocale::GetSystemEncodingName().c_str(),
1220 wxLocale::GetSystemEncoding());
1222 for ( size_t n
= 0; n
< WXSIZEOF(langStrings
); n
++ )
1224 const wxChar
*langStr
= langStrings
[n
];
1227 // FIXME: this doesn't do anything at all under Windows, we need
1228 // to create a new wxLocale!
1229 wxSetEnv(wxT("LC_ALL"), langStr
);
1232 int lang
= gs_localeDefault
.GetSystemLanguage();
1233 wxPrintf(wxT("Locale for '%s' is %s.\n"),
1234 langStr
? langStr
: wxT("system default"), GetLangName(lang
));
1238 #endif // TEST_LOCALE
1240 // ----------------------------------------------------------------------------
1242 // ----------------------------------------------------------------------------
1246 #include "wx/mimetype.h"
1248 static void TestMimeEnum()
1250 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1252 wxArrayString mimetypes
;
1254 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
1256 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count
);
1261 for ( size_t n
= 0; n
< count
; n
++ )
1263 wxFileType
*filetype
=
1264 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
1267 wxPrintf(wxT("nothing known about the filetype '%s'!\n"),
1268 mimetypes
[n
].c_str());
1272 filetype
->GetDescription(&desc
);
1273 filetype
->GetExtensions(exts
);
1275 filetype
->GetIcon(NULL
);
1278 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
1281 extsAll
<< wxT(", ");
1285 wxPrintf(wxT("\t%s: %s (%s)\n"),
1286 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
1289 wxPuts(wxEmptyString
);
1292 static void TestMimeFilename()
1294 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
1296 static const wxChar
*filenames
[] =
1299 wxT("document.pdf"),
1301 wxT("picture.jpeg"),
1304 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
1306 const wxString fname
= filenames
[n
];
1307 wxString ext
= fname
.AfterLast(wxT('.'));
1308 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
1311 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
1316 if ( !ft
->GetDescription(&desc
) )
1317 desc
= wxT("<no description>");
1320 if ( !ft
->GetOpenCommand(&cmd
,
1321 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
1322 cmd
= wxT("<no command available>");
1324 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
1326 wxPrintf(wxT("To open %s (%s) do %s.\n"),
1327 fname
.c_str(), desc
.c_str(), cmd
.c_str());
1333 wxPuts(wxEmptyString
);
1336 // these tests were broken by wxMimeTypesManager changes, temporarily disabling
1339 static void TestMimeOverride()
1341 wxPuts(wxT("*** Testing wxMimeTypesManager additional files loading ***\n"));
1343 static const wxChar
*mailcap
= wxT("/tmp/mailcap");
1344 static const wxChar
*mimetypes
= wxT("/tmp/mime.types");
1346 if ( wxFile::Exists(mailcap
) )
1347 wxPrintf(wxT("Loading mailcap from '%s': %s\n"),
1349 wxTheMimeTypesManager
->ReadMailcap(mailcap
) ? wxT("ok") : wxT("ERROR"));
1351 wxPrintf(wxT("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1354 if ( wxFile::Exists(mimetypes
) )
1355 wxPrintf(wxT("Loading mime.types from '%s': %s\n"),
1357 wxTheMimeTypesManager
->ReadMimeTypes(mimetypes
) ? wxT("ok") : wxT("ERROR"));
1359 wxPrintf(wxT("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1362 wxPuts(wxEmptyString
);
1365 static void TestMimeAssociate()
1367 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
1369 wxFileTypeInfo
ftInfo(
1370 wxT("application/x-xyz"),
1371 wxT("xyzview '%s'"), // open cmd
1372 wxT(""), // print cmd
1373 wxT("XYZ File"), // description
1374 wxT(".xyz"), // extensions
1375 wxNullPtr
// end of extensions
1377 ftInfo
.SetShortDesc(wxT("XYZFile")); // used under Win32 only
1379 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
1382 wxPuts(wxT("ERROR: failed to create association!"));
1386 // TODO: read it back
1390 wxPuts(wxEmptyString
);
1397 // ----------------------------------------------------------------------------
1398 // module dependencies feature
1399 // ----------------------------------------------------------------------------
1403 #include "wx/module.h"
1405 class wxTestModule
: public wxModule
1408 virtual bool OnInit() { wxPrintf(wxT("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1409 virtual void OnExit() { wxPrintf(wxT("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1412 class wxTestModuleA
: public wxTestModule
1417 DECLARE_DYNAMIC_CLASS(wxTestModuleA
)
1420 class wxTestModuleB
: public wxTestModule
1425 DECLARE_DYNAMIC_CLASS(wxTestModuleB
)
1428 class wxTestModuleC
: public wxTestModule
1433 DECLARE_DYNAMIC_CLASS(wxTestModuleC
)
1436 class wxTestModuleD
: public wxTestModule
1441 DECLARE_DYNAMIC_CLASS(wxTestModuleD
)
1444 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC
, wxModule
)
1445 wxTestModuleC::wxTestModuleC()
1447 AddDependency(CLASSINFO(wxTestModuleD
));
1450 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA
, wxModule
)
1451 wxTestModuleA::wxTestModuleA()
1453 AddDependency(CLASSINFO(wxTestModuleB
));
1454 AddDependency(CLASSINFO(wxTestModuleD
));
1457 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD
, wxModule
)
1458 wxTestModuleD::wxTestModuleD()
1462 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB
, wxModule
)
1463 wxTestModuleB::wxTestModuleB()
1465 AddDependency(CLASSINFO(wxTestModuleD
));
1466 AddDependency(CLASSINFO(wxTestModuleC
));
1469 #endif // TEST_MODULE
1471 // ----------------------------------------------------------------------------
1472 // misc information functions
1473 // ----------------------------------------------------------------------------
1475 #ifdef TEST_INFO_FUNCTIONS
1477 #include "wx/utils.h"
1479 #if TEST_INTERACTIVE
1480 static void TestDiskInfo()
1482 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
1486 wxChar pathname
[128];
1487 wxPrintf(wxT("\nEnter a directory name: "));
1488 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
1491 // kill the last '\n'
1492 pathname
[wxStrlen(pathname
) - 1] = 0;
1494 wxLongLong total
, free
;
1495 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
1497 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
1501 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
1502 (total
/ 1024).ToString().c_str(),
1503 (free
/ 1024).ToString().c_str(),
1508 #endif // TEST_INTERACTIVE
1510 static void TestOsInfo()
1512 wxPuts(wxT("*** Testing OS info functions ***\n"));
1515 wxGetOsVersion(&major
, &minor
);
1516 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
1517 wxGetOsDescription().c_str(), major
, minor
);
1519 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1521 wxPrintf(wxT("Host name is %s (%s).\n"),
1522 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1524 wxPuts(wxEmptyString
);
1527 static void TestPlatformInfo()
1529 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
1531 // get this platform
1532 wxPlatformInfo plat
;
1534 wxPrintf(wxT("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
1535 wxPrintf(wxT("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
1536 wxPrintf(wxT("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
1537 wxPrintf(wxT("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
1538 wxPrintf(wxT("Architecture is: %s\n"), plat
.GetArchName().c_str());
1539 wxPrintf(wxT("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
1541 wxPuts(wxEmptyString
);
1544 static void TestUserInfo()
1546 wxPuts(wxT("*** Testing user info functions ***\n"));
1548 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
1549 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
1550 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1551 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1553 wxPuts(wxEmptyString
);
1556 #endif // TEST_INFO_FUNCTIONS
1558 // ----------------------------------------------------------------------------
1560 // ----------------------------------------------------------------------------
1562 #ifdef TEST_PATHLIST
1565 #define CMD_IN_PATH wxT("ls")
1567 #define CMD_IN_PATH wxT("command.com")
1570 static void TestPathList()
1572 wxPuts(wxT("*** Testing wxPathList ***\n"));
1574 wxPathList pathlist
;
1575 pathlist
.AddEnvList(wxT("PATH"));
1576 wxString path
= pathlist
.FindValidPath(CMD_IN_PATH
);
1579 wxPrintf(wxT("ERROR: command not found in the path.\n"));
1583 wxPrintf(wxT("Command found in the path as '%s'.\n"), path
.c_str());
1587 #endif // TEST_PATHLIST
1589 // ----------------------------------------------------------------------------
1590 // regular expressions
1591 // ----------------------------------------------------------------------------
1593 #if defined TEST_REGEX && TEST_INTERACTIVE
1595 #include "wx/regex.h"
1597 static void TestRegExInteractive()
1599 wxPuts(wxT("*** Testing RE interactively ***"));
1603 wxChar pattern
[128];
1604 wxPrintf(wxT("\nEnter a pattern: "));
1605 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1608 // kill the last '\n'
1609 pattern
[wxStrlen(pattern
) - 1] = 0;
1612 if ( !re
.Compile(pattern
) )
1620 wxPrintf(wxT("Enter text to match: "));
1621 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1624 // kill the last '\n'
1625 text
[wxStrlen(text
) - 1] = 0;
1627 if ( !re
.Matches(text
) )
1629 wxPrintf(wxT("No match.\n"));
1633 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1636 for ( size_t n
= 1; ; n
++ )
1638 if ( !re
.GetMatch(&start
, &len
, n
) )
1643 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
1644 n
, wxString(text
+ start
, len
).c_str());
1651 #endif // TEST_REGEX
1653 // ----------------------------------------------------------------------------
1655 // ----------------------------------------------------------------------------
1658 NB: this stuff was taken from the glibc test suite and modified to build
1659 in wxWidgets: if I read the copyright below properly, this shouldn't
1665 #ifdef wxTEST_PRINTF
1666 // use our functions from wxchar.cpp
1670 // NB: do _not_ use WX_ATTRIBUTE_PRINTF here, we have some invalid formats
1671 // in the tests below
1672 int wxPrintf( const wxChar
*format
, ... );
1673 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... );
1676 #include "wx/longlong.h"
1680 static void rfg1 (void);
1681 static void rfg2 (void);
1685 fmtchk (const wxChar
*fmt
)
1687 (void) wxPrintf(wxT("%s:\t`"), fmt
);
1688 (void) wxPrintf(fmt
, 0x12);
1689 (void) wxPrintf(wxT("'\n"));
1693 fmtst1chk (const wxChar
*fmt
)
1695 (void) wxPrintf(wxT("%s:\t`"), fmt
);
1696 (void) wxPrintf(fmt
, 4, 0x12);
1697 (void) wxPrintf(wxT("'\n"));
1701 fmtst2chk (const wxChar
*fmt
)
1703 (void) wxPrintf(wxT("%s:\t`"), fmt
);
1704 (void) wxPrintf(fmt
, 4, 4, 0x12);
1705 (void) wxPrintf(wxT("'\n"));
1708 /* This page is covered by the following copyright: */
1710 /* (C) Copyright C E Chew
1712 * Feel free to copy, use and distribute this software provided:
1714 * 1. you do not pretend that you wrote it
1715 * 2. you leave this copyright notice intact.
1719 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1726 /* Formatted Output Test
1728 * This exercises the output formatting code.
1731 wxChar
*PointerNull
= NULL
;
1738 wxChar
*prefix
= buf
;
1741 wxPuts(wxT("\nFormatted output test"));
1742 wxPrintf(wxT("prefix 6d 6o 6x 6X 6u\n"));
1743 wxStrcpy(prefix
, wxT("%"));
1744 for (i
= 0; i
< 2; i
++) {
1745 for (j
= 0; j
< 2; j
++) {
1746 for (k
= 0; k
< 2; k
++) {
1747 for (l
= 0; l
< 2; l
++) {
1748 wxStrcpy(prefix
, wxT("%"));
1749 if (i
== 0) wxStrcat(prefix
, wxT("-"));
1750 if (j
== 0) wxStrcat(prefix
, wxT("+"));
1751 if (k
== 0) wxStrcat(prefix
, wxT("#"));
1752 if (l
== 0) wxStrcat(prefix
, wxT("0"));
1753 wxPrintf(wxT("%5s |"), prefix
);
1754 wxStrcpy(tp
, prefix
);
1755 wxStrcat(tp
, wxT("6d |"));
1757 wxStrcpy(tp
, prefix
);
1758 wxStrcat(tp
, wxT("6o |"));
1760 wxStrcpy(tp
, prefix
);
1761 wxStrcat(tp
, wxT("6x |"));
1763 wxStrcpy(tp
, prefix
);
1764 wxStrcat(tp
, wxT("6X |"));
1766 wxStrcpy(tp
, prefix
);
1767 wxStrcat(tp
, wxT("6u |"));
1769 wxPrintf(wxT("\n"));
1774 wxPrintf(wxT("%10s\n"), PointerNull
);
1775 wxPrintf(wxT("%-10s\n"), PointerNull
);
1778 static void TestPrintf()
1780 static wxChar shortstr
[] = wxT("Hi, Z.");
1781 static wxChar longstr
[] = wxT("Good morning, Doctor Chandra. This is Hal. \
1782 I am ready for my first lesson today.");
1784 wxString test_format
;
1786 fmtchk(wxT("%.4x"));
1787 fmtchk(wxT("%04x"));
1788 fmtchk(wxT("%4.4x"));
1789 fmtchk(wxT("%04.4x"));
1790 fmtchk(wxT("%4.3x"));
1791 fmtchk(wxT("%04.3x"));
1793 fmtst1chk(wxT("%.*x"));
1794 fmtst1chk(wxT("%0*x"));
1795 fmtst2chk(wxT("%*.*x"));
1796 fmtst2chk(wxT("%0*.*x"));
1798 wxString bad_format
= wxT("bad format:\t\"%b\"\n");
1799 wxPrintf(bad_format
.c_str());
1800 wxPrintf(wxT("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL
);
1802 wxPrintf(wxT("decimal negative:\t\"%d\"\n"), -2345);
1803 wxPrintf(wxT("octal negative:\t\"%o\"\n"), -2345);
1804 wxPrintf(wxT("hex negative:\t\"%x\"\n"), -2345);
1805 wxPrintf(wxT("long decimal number:\t\"%ld\"\n"), -123456L);
1806 wxPrintf(wxT("long octal negative:\t\"%lo\"\n"), -2345L);
1807 wxPrintf(wxT("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1808 wxPrintf(wxT("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1809 test_format
= wxT("left-adjusted ZLDN:\t\"%-010ld\"\n");
1810 wxPrintf(test_format
.c_str(), -123456);
1811 wxPrintf(wxT("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1812 wxPrintf(wxT("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1814 test_format
= wxT("zero-padded string:\t\"%010s\"\n");
1815 wxPrintf(test_format
.c_str(), shortstr
);
1816 test_format
= wxT("left-adjusted Z string:\t\"%-010s\"\n");
1817 wxPrintf(test_format
.c_str(), shortstr
);
1818 wxPrintf(wxT("space-padded string:\t\"%10s\"\n"), shortstr
);
1819 wxPrintf(wxT("left-adjusted S string:\t\"%-10s\"\n"), shortstr
);
1820 wxPrintf(wxT("null string:\t\"%s\"\n"), PointerNull
);
1821 wxPrintf(wxT("limited string:\t\"%.22s\"\n"), longstr
);
1823 wxPrintf(wxT("e-style >= 1:\t\"%e\"\n"), 12.34);
1824 wxPrintf(wxT("e-style >= .1:\t\"%e\"\n"), 0.1234);
1825 wxPrintf(wxT("e-style < .1:\t\"%e\"\n"), 0.001234);
1826 wxPrintf(wxT("e-style big:\t\"%.60e\"\n"), 1e20
);
1827 wxPrintf(wxT("e-style == .1:\t\"%e\"\n"), 0.1);
1828 wxPrintf(wxT("f-style >= 1:\t\"%f\"\n"), 12.34);
1829 wxPrintf(wxT("f-style >= .1:\t\"%f\"\n"), 0.1234);
1830 wxPrintf(wxT("f-style < .1:\t\"%f\"\n"), 0.001234);
1831 wxPrintf(wxT("g-style >= 1:\t\"%g\"\n"), 12.34);
1832 wxPrintf(wxT("g-style >= .1:\t\"%g\"\n"), 0.1234);
1833 wxPrintf(wxT("g-style < .1:\t\"%g\"\n"), 0.001234);
1834 wxPrintf(wxT("g-style big:\t\"%.60g\"\n"), 1e20
);
1836 wxPrintf (wxT(" %6.5f\n"), .099999999860301614);
1837 wxPrintf (wxT(" %6.5f\n"), .1);
1838 wxPrintf (wxT("x%5.4fx\n"), .5);
1840 wxPrintf (wxT("%#03x\n"), 1);
1842 //wxPrintf (wxT("something really insane: %.10000f\n"), 1.0);
1848 while (niter
-- != 0)
1849 wxPrintf (wxT("%.17e\n"), d
/ 2);
1854 // Open Watcom cause compiler error here
1855 // Error! E173: col(24) floating-point constant too small to represent
1856 wxPrintf (wxT("%15.5e\n"), 4.9406564584124654e-324);
1859 #define FORMAT wxT("|%12.4f|%12.4e|%12.4g|\n")
1860 wxPrintf (FORMAT
, 0.0, 0.0, 0.0);
1861 wxPrintf (FORMAT
, 1.0, 1.0, 1.0);
1862 wxPrintf (FORMAT
, -1.0, -1.0, -1.0);
1863 wxPrintf (FORMAT
, 100.0, 100.0, 100.0);
1864 wxPrintf (FORMAT
, 1000.0, 1000.0, 1000.0);
1865 wxPrintf (FORMAT
, 10000.0, 10000.0, 10000.0);
1866 wxPrintf (FORMAT
, 12345.0, 12345.0, 12345.0);
1867 wxPrintf (FORMAT
, 100000.0, 100000.0, 100000.0);
1868 wxPrintf (FORMAT
, 123456.0, 123456.0, 123456.0);
1873 int rc
= wxSnprintf (buf
, WXSIZEOF(buf
), wxT("%30s"), wxT("foo"));
1875 wxPrintf(wxT("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1876 rc
, WXSIZEOF(buf
), buf
);
1879 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1880 wxSnprintf(buf2
, WXSIZEOFbuf2
), "%.999999u", 10));
1886 wxPrintf (wxT("%e should be 1.234568e+06\n"), 1234567.8);
1887 wxPrintf (wxT("%f should be 1234567.800000\n"), 1234567.8);
1888 wxPrintf (wxT("%g should be 1.23457e+06\n"), 1234567.8);
1889 wxPrintf (wxT("%g should be 123.456\n"), 123.456);
1890 wxPrintf (wxT("%g should be 1e+06\n"), 1000000.0);
1891 wxPrintf (wxT("%g should be 10\n"), 10.0);
1892 wxPrintf (wxT("%g should be 0.02\n"), 0.02);
1896 wxPrintf(wxT("%.17f\n"),(1.0/x
/10.0+1.0)*x
-x
);
1902 wxSprintf(buf
,wxT("%*s%*s%*s"),-1,wxT("one"),-20,wxT("two"),-30,wxT("three"));
1904 result
|= wxStrcmp (buf
,
1905 wxT("onetwo three "));
1907 wxPuts (result
!= 0 ? wxT("Test failed!") : wxT("Test ok."));
1914 wxSprintf(buf
, "%07" wxLongLongFmtSpec
"o", wxLL(040000000000));
1916 // for some reason below line fails under Borland
1917 wxPrintf (wxT("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf
);
1920 if (wxStrcmp (buf
, wxT("40000000000")) != 0)
1923 wxPuts (wxT("\tFAILED"));
1925 wxUnusedVar(result
);
1926 wxPuts (wxEmptyString
);
1928 #endif // wxLongLong_t
1930 wxPrintf (wxT("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX
+ 2, UCHAR_MAX
+ 2);
1931 wxPrintf (wxT("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX
+ 2, USHRT_MAX
+ 2);
1933 wxPuts (wxT("--- Should be no further output. ---"));
1942 memset (bytes
, '\xff', sizeof bytes
);
1943 wxSprintf (buf
, wxT("foo%hhn\n"), &bytes
[3]);
1944 if (bytes
[0] != '\xff' || bytes
[1] != '\xff' || bytes
[2] != '\xff'
1945 || bytes
[4] != '\xff' || bytes
[5] != '\xff' || bytes
[6] != '\xff')
1947 wxPuts (wxT("%hhn overwrite more bytes"));
1952 wxPuts (wxT("%hhn wrote incorrect value"));
1964 wxSprintf (buf
, wxT("%5.s"), wxT("xyz"));
1965 if (wxStrcmp (buf
, wxT(" ")) != 0)
1966 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" "));
1967 wxSprintf (buf
, wxT("%5.f"), 33.3);
1968 if (wxStrcmp (buf
, wxT(" 33")) != 0)
1969 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 33"));
1970 wxSprintf (buf
, wxT("%8.e"), 33.3e7
);
1971 if (wxStrcmp (buf
, wxT(" 3e+08")) != 0)
1972 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3e+08"));
1973 wxSprintf (buf
, wxT("%8.E"), 33.3e7
);
1974 if (wxStrcmp (buf
, wxT(" 3E+08")) != 0)
1975 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3E+08"));
1976 wxSprintf (buf
, wxT("%.g"), 33.3);
1977 if (wxStrcmp (buf
, wxT("3e+01")) != 0)
1978 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3e+01"));
1979 wxSprintf (buf
, wxT("%.G"), 33.3);
1980 if (wxStrcmp (buf
, wxT("3E+01")) != 0)
1981 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3E+01"));
1989 wxString test_format
;
1992 wxSprintf (buf
, wxT("%.*g"), prec
, 3.3);
1993 if (wxStrcmp (buf
, wxT("3")) != 0)
1994 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3"));
1996 wxSprintf (buf
, wxT("%.*G"), prec
, 3.3);
1997 if (wxStrcmp (buf
, wxT("3")) != 0)
1998 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT("3"));
2000 wxSprintf (buf
, wxT("%7.*G"), prec
, 3.33);
2001 if (wxStrcmp (buf
, wxT(" 3")) != 0)
2002 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 3"));
2004 test_format
= wxT("%04.*o");
2005 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2006 if (wxStrcmp (buf
, wxT(" 041")) != 0)
2007 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 041"));
2009 test_format
= wxT("%09.*u");
2010 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2011 if (wxStrcmp (buf
, wxT(" 0000033")) != 0)
2012 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 0000033"));
2014 test_format
= wxT("%04.*x");
2015 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2016 if (wxStrcmp (buf
, wxT(" 021")) != 0)
2017 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 021"));
2019 test_format
= wxT("%04.*X");
2020 wxSprintf (buf
, test_format
.c_str(), prec
, 33);
2021 if (wxStrcmp (buf
, wxT(" 021")) != 0)
2022 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf
, wxT(" 021"));
2025 #endif // TEST_PRINTF
2027 // ----------------------------------------------------------------------------
2028 // registry and related stuff
2029 // ----------------------------------------------------------------------------
2031 // this is for MSW only
2034 #undef TEST_REGISTRY
2039 #include "wx/confbase.h"
2040 #include "wx/msw/regconf.h"
2043 static void TestRegConfWrite()
2045 wxConfig
*config
= new wxConfig(wxT("myapp"));
2046 config
->SetPath(wxT("/group1"));
2047 config
->Write(wxT("entry1"), wxT("foo"));
2048 config
->SetPath(wxT("/group2"));
2049 config
->Write(wxT("entry1"), wxT("bar"));
2053 static void TestRegConfRead()
2055 wxRegConfig
*config
= new wxRegConfig(wxT("myapp"));
2059 config
->SetPath(wxT("/"));
2060 wxPuts(wxT("Enumerating / subgroups:"));
2061 bool bCont
= config
->GetFirstGroup(str
, dummy
);
2065 bCont
= config
->GetNextGroup(str
, dummy
);
2069 #endif // TEST_REGCONF
2071 #ifdef TEST_REGISTRY
2073 #include "wx/msw/registry.h"
2075 // I chose this one because I liked its name, but it probably only exists under
2077 static const wxChar
*TESTKEY
=
2078 wxT("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2080 static void TestRegistryRead()
2082 wxPuts(wxT("*** testing registry reading ***"));
2084 wxRegKey
key(TESTKEY
);
2085 wxPrintf(wxT("The test key name is '%s'.\n"), key
.GetName().c_str());
2088 wxPuts(wxT("ERROR: test key can't be opened, aborting test."));
2093 size_t nSubKeys
, nValues
;
2094 if ( key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
2096 wxPrintf(wxT("It has %u subkeys and %u values.\n"), nSubKeys
, nValues
);
2099 wxPrintf(wxT("Enumerating values:\n"));
2103 bool cont
= key
.GetFirstValue(value
, dummy
);
2106 wxPrintf(wxT("Value '%s': type "), value
.c_str());
2107 switch ( key
.GetValueType(value
) )
2109 case wxRegKey::Type_None
: wxPrintf(wxT("ERROR (none)")); break;
2110 case wxRegKey::Type_String
: wxPrintf(wxT("SZ")); break;
2111 case wxRegKey::Type_Expand_String
: wxPrintf(wxT("EXPAND_SZ")); break;
2112 case wxRegKey::Type_Binary
: wxPrintf(wxT("BINARY")); break;
2113 case wxRegKey::Type_Dword
: wxPrintf(wxT("DWORD")); break;
2114 case wxRegKey::Type_Multi_String
: wxPrintf(wxT("MULTI_SZ")); break;
2115 default: wxPrintf(wxT("other (unknown)")); break;
2118 wxPrintf(wxT(", value = "));
2119 if ( key
.IsNumericValue(value
) )
2122 key
.QueryValue(value
, &val
);
2123 wxPrintf(wxT("%ld"), val
);
2128 key
.QueryValue(value
, val
);
2129 wxPrintf(wxT("'%s'"), val
.c_str());
2131 key
.QueryRawValue(value
, val
);
2132 wxPrintf(wxT(" (raw value '%s')"), val
.c_str());
2137 cont
= key
.GetNextValue(value
, dummy
);
2141 static void TestRegistryAssociation()
2144 The second call to deleteself genertaes an error message, with a
2145 messagebox saying .flo is crucial to system operation, while the .ddf
2146 call also fails, but with no error message
2151 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2153 key
= wxT("ddxf_auto_file") ;
2154 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2156 key
= wxT("ddxf_auto_file") ;
2157 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2159 key
= wxT("program,0") ;
2160 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2162 key
= wxT("program \"%1\"") ;
2164 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2166 key
.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2168 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2170 key
.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2174 #endif // TEST_REGISTRY
2176 // ----------------------------------------------------------------------------
2178 // ----------------------------------------------------------------------------
2180 #ifdef TEST_SCOPEGUARD
2182 #include "wx/scopeguard.h"
2184 static void function0() { puts("function0()"); }
2185 static void function1(int n
) { printf("function1(%d)\n", n
); }
2186 static void function2(double x
, char c
) { printf("function2(%g, %c)\n", x
, c
); }
2190 void method0() { printf("method0()\n"); }
2191 void method1(int n
) { printf("method1(%d)\n", n
); }
2192 void method2(double x
, char c
) { printf("method2(%g, %c)\n", x
, c
); }
2195 static void TestScopeGuard()
2197 wxON_BLOCK_EXIT0(function0
);
2198 wxON_BLOCK_EXIT1(function1
, 17);
2199 wxON_BLOCK_EXIT2(function2
, 3.14, 'p');
2202 wxON_BLOCK_EXIT_OBJ0(obj
, Object::method0
);
2203 wxON_BLOCK_EXIT_OBJ1(obj
, Object::method1
, 7);
2204 wxON_BLOCK_EXIT_OBJ2(obj
, Object::method2
, 2.71, 'e');
2206 wxScopeGuard dismissed
= wxMakeGuard(function0
);
2207 dismissed
.Dismiss();
2212 // ----------------------------------------------------------------------------
2214 // ----------------------------------------------------------------------------
2218 #include "wx/socket.h"
2219 #include "wx/protocol/protocol.h"
2220 #include "wx/protocol/http.h"
2222 static void TestSocketServer()
2224 wxPuts(wxT("*** Testing wxSocketServer ***\n"));
2226 static const int PORT
= 3000;
2231 wxSocketServer
*server
= new wxSocketServer(addr
);
2232 if ( !server
->Ok() )
2234 wxPuts(wxT("ERROR: failed to bind"));
2242 wxPrintf(wxT("Server: waiting for connection on port %d...\n"), PORT
);
2244 wxSocketBase
*socket
= server
->Accept();
2247 wxPuts(wxT("ERROR: wxSocketServer::Accept() failed."));
2251 wxPuts(wxT("Server: got a client."));
2253 server
->SetTimeout(60); // 1 min
2256 while ( !close
&& socket
->IsConnected() )
2259 wxChar ch
= wxT('\0');
2262 if ( socket
->Read(&ch
, sizeof(ch
)).Error() )
2264 // don't log error if the client just close the connection
2265 if ( socket
->IsConnected() )
2267 wxPuts(wxT("ERROR: in wxSocket::Read."));
2287 wxPrintf(wxT("Server: got '%s'.\n"), s
.c_str());
2288 if ( s
== wxT("close") )
2290 wxPuts(wxT("Closing connection"));
2294 else if ( s
== wxT("quit") )
2299 wxPuts(wxT("Shutting down the server"));
2301 else // not a special command
2303 socket
->Write(s
.MakeUpper().c_str(), s
.length());
2304 socket
->Write("\r\n", 2);
2305 wxPrintf(wxT("Server: wrote '%s'.\n"), s
.c_str());
2311 wxPuts(wxT("Server: lost a client unexpectedly."));
2317 // same as "delete server" but is consistent with GUI programs
2321 static void TestSocketClient()
2323 wxPuts(wxT("*** Testing wxSocketClient ***\n"));
2325 static const wxChar
*hostname
= wxT("www.wxwidgets.org");
2328 addr
.Hostname(hostname
);
2331 wxPrintf(wxT("--- Attempting to connect to %s:80...\n"), hostname
);
2333 wxSocketClient client
;
2334 if ( !client
.Connect(addr
) )
2336 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
2340 wxPrintf(wxT("--- Connected to %s:%u...\n"),
2341 addr
.Hostname().c_str(), addr
.Service());
2345 // could use simply "GET" here I suppose
2347 wxString::Format(wxT("GET http://%s/\r\n"), hostname
);
2348 client
.Write(cmdGet
, cmdGet
.length());
2349 wxPrintf(wxT("--- Sent command '%s' to the server\n"),
2350 MakePrintable(cmdGet
).c_str());
2351 client
.Read(buf
, WXSIZEOF(buf
));
2352 wxPrintf(wxT("--- Server replied:\n%s"), buf
);
2356 #endif // TEST_SOCKETS
2358 // ----------------------------------------------------------------------------
2360 // ----------------------------------------------------------------------------
2364 #include "wx/protocol/ftp.h"
2365 #include "wx/protocol/log.h"
2367 #define FTP_ANONYMOUS
2371 #ifdef FTP_ANONYMOUS
2372 static const wxChar
*directory
= wxT("/pub");
2373 static const wxChar
*filename
= wxT("welcome.msg");
2375 static const wxChar
*directory
= wxT("/etc");
2376 static const wxChar
*filename
= wxT("issue");
2379 static bool TestFtpConnect()
2381 wxPuts(wxT("*** Testing FTP connect ***"));
2383 #ifdef FTP_ANONYMOUS
2384 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
2386 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
2387 #else // !FTP_ANONYMOUS
2388 static const wxChar
*hostname
= "localhost";
2391 wxFgets(user
, WXSIZEOF(user
), stdin
);
2392 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
2395 wxChar password
[256];
2396 wxPrintf(wxT("Password for %s: "), password
);
2397 wxFgets(password
, WXSIZEOF(password
), stdin
);
2398 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
2399 ftp
->SetPassword(password
);
2401 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
2402 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2404 if ( !ftp
->Connect(hostname
) )
2406 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
2412 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
2413 hostname
, ftp
->Pwd().c_str());
2420 static void TestFtpList()
2422 wxPuts(wxT("*** Testing wxFTP file listing ***\n"));
2425 if ( !ftp
->ChDir(directory
) )
2427 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory
);
2430 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2432 // test NLIST and LIST
2433 wxArrayString files
;
2434 if ( !ftp
->GetFilesList(files
) )
2436 wxPuts(wxT("ERROR: failed to get NLIST of files"));
2440 wxPrintf(wxT("Brief list of files under '%s':\n"), ftp
->Pwd().c_str());
2441 size_t count
= files
.GetCount();
2442 for ( size_t n
= 0; n
< count
; n
++ )
2444 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
2446 wxPuts(wxT("End of the file list"));
2449 if ( !ftp
->GetDirList(files
) )
2451 wxPuts(wxT("ERROR: failed to get LIST of files"));
2455 wxPrintf(wxT("Detailed list of files under '%s':\n"), ftp
->Pwd().c_str());
2456 size_t count
= files
.GetCount();
2457 for ( size_t n
= 0; n
< count
; n
++ )
2459 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
2461 wxPuts(wxT("End of the file list"));
2464 if ( !ftp
->ChDir(wxT("..")) )
2466 wxPuts(wxT("ERROR: failed to cd to .."));
2469 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2472 static void TestFtpDownload()
2474 wxPuts(wxT("*** Testing wxFTP download ***\n"));
2477 wxInputStream
*in
= ftp
->GetInputStream(filename
);
2480 wxPrintf(wxT("ERROR: couldn't get input stream for %s\n"), filename
);
2484 size_t size
= in
->GetSize();
2485 wxPrintf(wxT("Reading file %s (%u bytes)..."), filename
, size
);
2488 wxChar
*data
= new wxChar
[size
];
2489 if ( !in
->Read(data
, size
) )
2491 wxPuts(wxT("ERROR: read error"));
2495 wxPrintf(wxT("\nContents of %s:\n%s\n"), filename
, data
);
2503 static void TestFtpFileSize()
2505 wxPuts(wxT("*** Testing FTP SIZE command ***"));
2507 if ( !ftp
->ChDir(directory
) )
2509 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory
);
2512 wxPrintf(wxT("Current directory is '%s'\n"), ftp
->Pwd().c_str());
2514 if ( ftp
->FileExists(filename
) )
2516 int size
= ftp
->GetFileSize(filename
);
2518 wxPrintf(wxT("ERROR: couldn't get size of '%s'\n"), filename
);
2520 wxPrintf(wxT("Size of '%s' is %d bytes.\n"), filename
, size
);
2524 wxPrintf(wxT("ERROR: '%s' doesn't exist\n"), filename
);
2528 static void TestFtpMisc()
2530 wxPuts(wxT("*** Testing miscellaneous wxFTP functions ***"));
2532 if ( ftp
->SendCommand(wxT("STAT")) != '2' )
2534 wxPuts(wxT("ERROR: STAT failed"));
2538 wxPrintf(wxT("STAT returned:\n\n%s\n"), ftp
->GetLastResult().c_str());
2541 if ( ftp
->SendCommand(wxT("HELP SITE")) != '2' )
2543 wxPuts(wxT("ERROR: HELP SITE failed"));
2547 wxPrintf(wxT("The list of site-specific commands:\n\n%s\n"),
2548 ftp
->GetLastResult().c_str());
2552 #if TEST_INTERACTIVE
2554 static void TestFtpInteractive()
2556 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
2562 wxPrintf(wxT("Enter FTP command: "));
2563 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
2566 // kill the last '\n'
2567 buf
[wxStrlen(buf
) - 1] = 0;
2569 // special handling of LIST and NLST as they require data connection
2570 wxString
start(buf
, 4);
2572 if ( start
== wxT("LIST") || start
== wxT("NLST") )
2575 if ( wxStrlen(buf
) > 4 )
2578 wxArrayString files
;
2579 if ( !ftp
->GetList(files
, wildcard
, start
== wxT("LIST")) )
2581 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
2585 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
2586 start
.c_str(), wildcard
.c_str(), ftp
->Pwd().c_str());
2587 size_t count
= files
.GetCount();
2588 for ( size_t n
= 0; n
< count
; n
++ )
2590 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
2592 wxPuts(wxT("--- End of the file list"));
2597 wxChar ch
= ftp
->SendCommand(buf
);
2598 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
2601 wxPrintf(wxT(" (return code %c)"), ch
);
2604 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
->GetLastResult().c_str());
2608 wxPuts(wxT("\n*** done ***"));
2611 #endif // TEST_INTERACTIVE
2613 static void TestFtpUpload()
2615 wxPuts(wxT("*** Testing wxFTP uploading ***\n"));
2618 static const wxChar
*file1
= wxT("test1");
2619 static const wxChar
*file2
= wxT("test2");
2620 wxOutputStream
*out
= ftp
->GetOutputStream(file1
);
2623 wxPrintf(wxT("--- Uploading to %s ---\n"), file1
);
2624 out
->Write("First hello", 11);
2628 // send a command to check the remote file
2629 if ( ftp
->SendCommand(wxString(wxT("STAT ")) + file1
) != '2' )
2631 wxPrintf(wxT("ERROR: STAT %s failed\n"), file1
);
2635 wxPrintf(wxT("STAT %s returned:\n\n%s\n"),
2636 file1
, ftp
->GetLastResult().c_str());
2639 out
= ftp
->GetOutputStream(file2
);
2642 wxPrintf(wxT("--- Uploading to %s ---\n"), file1
);
2643 out
->Write("Second hello", 12);
2650 // ----------------------------------------------------------------------------
2652 // ----------------------------------------------------------------------------
2654 #ifdef TEST_STACKWALKER
2656 #if wxUSE_STACKWALKER
2658 #include "wx/stackwalk.h"
2660 class StackDump
: public wxStackWalker
2663 StackDump(const char *argv0
)
2664 : wxStackWalker(argv0
)
2668 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
2670 wxPuts(wxT("Stack dump:"));
2672 wxStackWalker::Walk(skip
, maxdepth
);
2676 virtual void OnStackFrame(const wxStackFrame
& frame
)
2678 printf("[%2d] ", (int) frame
.GetLevel());
2680 wxString name
= frame
.GetName();
2681 if ( !name
.empty() )
2683 printf("%-20.40s", (const char*)name
.mb_str());
2687 printf("0x%08lx", (unsigned long)frame
.GetAddress());
2690 if ( frame
.HasSourceLocation() )
2693 (const char*)frame
.GetFileName().mb_str(),
2694 (int)frame
.GetLine());
2700 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
2702 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
2703 (const char*)name
.mb_str(),
2704 (const char*)val
.mb_str());
2709 static void TestStackWalk(const char *argv0
)
2711 wxPuts(wxT("*** Testing wxStackWalker ***\n"));
2713 StackDump
dump(argv0
);
2717 #endif // wxUSE_STACKWALKER
2719 #endif // TEST_STACKWALKER
2721 // ----------------------------------------------------------------------------
2723 // ----------------------------------------------------------------------------
2725 #ifdef TEST_STDPATHS
2727 #include "wx/stdpaths.h"
2728 #include "wx/wxchar.h" // wxPrintf
2730 static void TestStandardPaths()
2732 wxPuts(wxT("*** Testing wxStandardPaths ***\n"));
2734 wxTheApp
->SetAppName(wxT("console"));
2736 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
2737 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
2738 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
2739 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
2740 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
2741 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
2742 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
2743 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
2744 wxPrintf(wxT("Executable path:\t%s\n"), stdp
.GetExecutablePath().c_str());
2745 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
2746 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
2747 wxPrintf(wxT("Localized res. dir:\t%s\n"),
2748 stdp
.GetLocalizedResourcesDir(wxT("fr")).c_str());
2749 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
2750 stdp
.GetLocalizedResourcesDir
2753 wxStandardPaths::ResourceCat_Messages
2757 #endif // TEST_STDPATHS
2759 // ----------------------------------------------------------------------------
2761 // ----------------------------------------------------------------------------
2765 #include "wx/wfstream.h"
2766 #include "wx/mstream.h"
2768 static void TestFileStream()
2770 wxPuts(wxT("*** Testing wxFileInputStream ***"));
2772 static const wxString filename
= wxT("testdata.fs");
2774 wxFileOutputStream
fsOut(filename
);
2775 fsOut
.Write("foo", 3);
2779 wxFileInputStream
fsIn(filename
);
2780 wxPrintf(wxT("File stream size: %u\n"), fsIn
.GetSize());
2782 while ( (c
=fsIn
.GetC()) != wxEOF
)
2788 if ( !wxRemoveFile(filename
) )
2790 wxPrintf(wxT("ERROR: failed to remove the file '%s'.\n"), filename
.c_str());
2793 wxPuts(wxT("\n*** wxFileInputStream test done ***"));
2796 static void TestMemoryStream()
2798 wxPuts(wxT("*** Testing wxMemoryOutputStream ***"));
2800 wxMemoryOutputStream memOutStream
;
2801 wxPrintf(wxT("Initially out stream offset: %lu\n"),
2802 (unsigned long)memOutStream
.TellO());
2804 for ( const wxChar
*p
= wxT("Hello, stream!"); *p
; p
++ )
2806 memOutStream
.PutC(*p
);
2809 wxPrintf(wxT("Final out stream offset: %lu\n"),
2810 (unsigned long)memOutStream
.TellO());
2812 wxPuts(wxT("*** Testing wxMemoryInputStream ***"));
2815 size_t len
= memOutStream
.CopyTo(buf
, WXSIZEOF(buf
));
2817 wxMemoryInputStream
memInpStream(buf
, len
);
2818 wxPrintf(wxT("Memory stream size: %u\n"), memInpStream
.GetSize());
2820 while ( (c
=memInpStream
.GetC()) != wxEOF
)
2825 wxPuts(wxT("\n*** wxMemoryInputStream test done ***"));
2828 #endif // TEST_STREAMS
2830 // ----------------------------------------------------------------------------
2832 // ----------------------------------------------------------------------------
2836 #include "wx/stopwatch.h"
2837 #include "wx/utils.h"
2839 static void TestStopWatch()
2841 wxPuts(wxT("*** Testing wxStopWatch ***\n"));
2845 wxPrintf(wxT("Initially paused, after 2 seconds time is..."));
2848 wxPrintf(wxT("\t%ldms\n"), sw
.Time());
2850 wxPrintf(wxT("Resuming stopwatch and sleeping 3 seconds..."));
2854 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2857 wxPrintf(wxT("Pausing agan and sleeping 2 more seconds..."));
2860 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2863 wxPrintf(wxT("Finally resuming and sleeping 2 more seconds..."));
2866 wxPrintf(wxT("\telapsed time: %ldms\n"), sw
.Time());
2869 wxPuts(wxT("\nChecking for 'backwards clock' bug..."));
2870 for ( size_t n
= 0; n
< 70; n
++ )
2874 for ( size_t m
= 0; m
< 100000; m
++ )
2876 if ( sw
.Time() < 0 || sw2
.Time() < 0 )
2878 wxPuts(wxT("\ntime is negative - ERROR!"));
2886 wxPuts(wxT(", ok."));
2889 #include "wx/timer.h"
2890 #include "wx/evtloop.h"
2894 wxPuts(wxT("*** Testing wxTimer ***\n"));
2896 class MyTimer
: public wxTimer
2899 MyTimer() : wxTimer() { m_num
= 0; }
2901 virtual void Notify()
2903 wxPrintf(wxT("%d"), m_num
++);
2908 wxPrintf(wxT("... exiting the event loop"));
2911 wxEventLoop::GetActive()->Exit(0);
2912 wxPuts(wxT(", ok."));
2925 timer1
.Start(100, true /* one shot */);
2927 timer1
.Start(100, true /* one shot */);
2935 #endif // TEST_TIMER
2937 // ----------------------------------------------------------------------------
2939 // ----------------------------------------------------------------------------
2941 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
2947 #include "wx/volume.h"
2949 static const wxChar
*volumeKinds
[] =
2955 wxT("network volume"),
2956 wxT("other volume"),
2959 static void TestFSVolume()
2961 wxPuts(wxT("*** Testing wxFSVolume class ***"));
2963 wxArrayString volumes
= wxFSVolume::GetVolumes();
2964 size_t count
= volumes
.GetCount();
2968 wxPuts(wxT("ERROR: no mounted volumes?"));
2972 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
2974 for ( size_t n
= 0; n
< count
; n
++ )
2976 wxFSVolume
vol(volumes
[n
]);
2979 wxPuts(wxT("ERROR: couldn't create volume"));
2983 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
2985 vol
.GetDisplayName().c_str(),
2986 vol
.GetName().c_str(),
2987 volumeKinds
[vol
.GetKind()],
2988 vol
.IsWritable() ? wxT("rw") : wxT("ro"),
2989 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? wxT("removable")
2994 #endif // TEST_VOLUME
2996 // ----------------------------------------------------------------------------
2998 // ----------------------------------------------------------------------------
3000 #ifdef TEST_DATETIME
3002 #include "wx/math.h"
3003 #include "wx/datetime.h"
3005 #if TEST_INTERACTIVE
3007 static void TestDateTimeInteractive()
3009 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
3015 wxPrintf(wxT("Enter a date: "));
3016 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
3019 // kill the last '\n'
3020 buf
[wxStrlen(buf
) - 1] = 0;
3023 const wxChar
*p
= dt
.ParseDate(buf
);
3026 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
3032 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
3035 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
3036 dt
.Format(wxT("%b %d, %Y")).c_str(),
3038 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
3039 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
3040 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
3043 wxPuts(wxT("\n*** done ***"));
3046 #endif // TEST_INTERACTIVE
3047 #endif // TEST_DATETIME
3049 // ----------------------------------------------------------------------------
3051 // ----------------------------------------------------------------------------
3053 #ifdef TEST_SNGLINST
3054 #include "wx/snglinst.h"
3055 #endif // TEST_SNGLINST
3057 int main(int argc
, char **argv
)
3060 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
3065 for (n
= 0; n
< argc
; n
++ )
3067 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
3068 wxArgv
[n
] = wxStrdup(warg
);
3073 #else // !wxUSE_UNICODE
3075 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
3077 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
3079 wxInitializer initializer
;
3082 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
3087 #ifdef TEST_SNGLINST
3088 wxSingleInstanceChecker checker
;
3089 if ( checker
.Create(wxT(".wxconsole.lock")) )
3091 if ( checker
.IsAnotherRunning() )
3093 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
3098 // wait some time to give time to launch another instance
3099 wxPrintf(wxT("Press \"Enter\" to continue..."));
3102 else // failed to create
3104 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
3106 #endif // TEST_SNGLINST
3109 TestCmdLineConvert();
3111 #if wxUSE_CMDLINE_PARSER
3112 static const wxCmdLineEntryDesc cmdLineDesc
[] =
3114 { wxCMD_LINE_SWITCH
, "h", "help", "show this help message",
3115 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
3116 { wxCMD_LINE_SWITCH
, "v", "verbose", "be verbose" },
3117 { wxCMD_LINE_SWITCH
, "q", "quiet", "be quiet" },
3119 { wxCMD_LINE_OPTION
, "o", "output", "output file" },
3120 { wxCMD_LINE_OPTION
, "i", "input", "input dir" },
3121 { wxCMD_LINE_OPTION
, "s", "size", "output block size",
3122 wxCMD_LINE_VAL_NUMBER
},
3123 { wxCMD_LINE_OPTION
, "d", "date", "output file date",
3124 wxCMD_LINE_VAL_DATE
},
3125 { wxCMD_LINE_OPTION
, "f", "double", "output double",
3126 wxCMD_LINE_VAL_DOUBLE
},
3128 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file",
3129 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
3134 wxCmdLineParser
parser(cmdLineDesc
, argc
, wxArgv
);
3136 parser
.AddOption(wxT("project_name"), wxT(""), wxT("full path to project file"),
3137 wxCMD_LINE_VAL_STRING
,
3138 wxCMD_LINE_OPTION_MANDATORY
| wxCMD_LINE_NEEDS_SEPARATOR
);
3140 switch ( parser
.Parse() )
3143 wxLogMessage(wxT("Help was given, terminating."));
3147 ShowCmdLine(parser
);
3151 wxLogMessage(wxT("Syntax error detected, aborting."));
3154 #endif // wxUSE_CMDLINE_PARSER
3156 #endif // TEST_CMDLINE
3168 TestDllListLoaded();
3169 #endif // TEST_DYNLIB
3173 #endif // TEST_ENVIRON
3175 #ifdef TEST_FILECONF
3177 #endif // TEST_FILECONF
3181 #endif // TEST_LOCALE
3184 wxPuts(wxT("*** Testing wxLog ***"));
3187 for ( size_t n
= 0; n
< 8000; n
++ )
3189 s
<< (wxChar
)(wxT('A') + (n
% 26));
3192 wxLogWarning(wxT("The length of the string is %lu"),
3193 (unsigned long)s
.length());
3196 msg
.Printf(wxT("A very very long message: '%s', the end!\n"), s
.c_str());
3198 // this one shouldn't be truncated
3201 // but this one will because log functions use fixed size buffer
3202 // (note that it doesn't need '\n' at the end neither - will be added
3204 wxLogMessage(wxT("A very very long message 2: '%s', the end!"), s
.c_str());
3214 #ifdef TEST_FILENAME
3217 TestFileNameDirManip();
3218 TestFileNameComparison();
3219 TestFileNameOperations();
3220 #endif // TEST_FILENAME
3222 #ifdef TEST_FILETIME
3227 #endif // TEST_FILETIME
3230 wxLog::AddTraceMask(FTP_TRACE_MASK
);
3232 // wxFTP cannot be a static variable as its ctor needs to access
3233 // wxWidgets internals after it has been initialized
3235 ftp
->SetLog(new wxProtocolLog(FTP_TRACE_MASK
));
3237 if ( TestFtpConnect() )
3247 #if TEST_INTERACTIVE
3248 //TestFtpInteractive();
3251 //else: connecting to the FTP server failed
3257 //wxLog::AddTraceMask(wxT("mime"));
3261 TestMimeAssociate();
3266 #ifdef TEST_INFO_FUNCTIONS
3271 #if TEST_INTERACTIVE
3274 #endif // TEST_INFO_FUNCTIONS
3276 #ifdef TEST_PATHLIST
3278 #endif // TEST_PATHLIST
3282 #endif // TEST_PRINTF
3289 #endif // TEST_REGCONF
3291 #if defined TEST_REGEX && TEST_INTERACTIVE
3292 TestRegExInteractive();
3293 #endif // defined TEST_REGEX && TEST_INTERACTIVE
3295 #ifdef TEST_REGISTRY
3297 TestRegistryAssociation();
3298 #endif // TEST_REGISTRY
3303 #endif // TEST_SOCKETS
3310 #endif // TEST_STREAMS
3315 #endif // TEST_TIMER
3317 #ifdef TEST_DATETIME
3318 #if TEST_INTERACTIVE
3319 TestDateTimeInteractive();
3321 #endif // TEST_DATETIME
3323 #ifdef TEST_SCOPEGUARD
3327 #ifdef TEST_STACKWALKER
3328 #if wxUSE_STACKWALKER
3329 TestStackWalk(argv
[0]);
3331 #endif // TEST_STACKWALKER
3333 #ifdef TEST_STDPATHS
3334 TestStandardPaths();
3338 wxPuts(wxT("Sleeping for 3 seconds... z-z-z-z-z..."));
3340 #endif // TEST_USLEEP
3344 #endif // TEST_VOLUME
3348 for ( int n
= 0; n
< argc
; n
++ )
3353 #endif // wxUSE_UNICODE