]> git.saurik.com Git - wxWidgets.git/blame - samples/console/console.cpp
OpenVMS changes
[wxWidgets.git] / samples / console / console.cpp
CommitLineData
37667812
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/console/console.cpp
f6c9f2ed 3// Purpose: A sample console (as opposed to GUI) program using wxWidgets
37667812
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
e87271f3
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
e84010cf 20#include "wx/defs.h"
b11a23f3 21
37667812
VZ
22#include <stdio.h>
23
e84010cf
GD
24#include "wx/string.h"
25#include "wx/file.h"
26#include "wx/app.h"
e046e5f1 27#include "wx/log.h"
8bb6b2c0
VZ
28#include "wx/apptrait.h"
29#include "wx/platinfo.h"
e87271f3 30
d31b7b68
VZ
31// without this pragma, the stupid compiler precompiles #defines below so that
32// changing them doesn't "take place" later!
33#ifdef __VISUALC__
34 #pragma hdrstop
35#endif
36
e87271f3
VZ
37// ----------------------------------------------------------------------------
38// conditional compilation
39// ----------------------------------------------------------------------------
40
daa2c7d9
VZ
41/*
42 A note about all these conditional compilation macros: this file is used
be5a51fb 43 both as a test suite for various non-GUI wxWidgets classes and as a
daa2c7d9
VZ
44 scratchpad for quick tests. So there are two compilation modes: if you
45 define TEST_ALL all tests are run, otherwise you may enable the individual
46 tests individually in the "#else" branch below.
47 */
48
e9d2bb6f
DS
49// what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
50// test, define it to 1 to do all tests.
51#define TEST_ALL 0
52
53
54#if TEST_ALL
31f6de22
VZ
55 #define TEST_CMDLINE
56 #define TEST_DATETIME
57 #define TEST_DIR
93ed8ff7 58 #define TEST_DYNLIB
31f6de22
VZ
59 #define TEST_ENVIRON
60 #define TEST_EXECUTE
61 #define TEST_FILE
62 #define TEST_FILECONF
63 #define TEST_FILENAME
64 #define TEST_FILETIME
8d4dc98f 65 // #define TEST_FTP --FIXME! (RN)
31f6de22 66 #define TEST_INFO_FUNCTIONS
31f6de22
VZ
67 #define TEST_LOCALE
68 #define TEST_LOG
31f6de22 69 #define TEST_MIME
af266e5b 70 #define TEST_MODULE
31f6de22 71 #define TEST_PATHLIST
8d5eff60 72 #define TEST_ODBC
7aeebdcd 73 #define TEST_PRINTF
31f6de22
VZ
74 #define TEST_REGCONF
75 #define TEST_REGEX
76 #define TEST_REGISTRY
24c8053b 77 #define TEST_SCOPEGUARD
31f6de22 78 #define TEST_SNGLINST
8d4dc98f 79// #define TEST_SOCKETS --FIXME! (RN)
eaff0f0d 80 #define TEST_STACKWALKER
af33b199 81 #define TEST_STDPATHS
31f6de22 82 #define TEST_STREAMS
39937656 83 #define TEST_TEXTSTREAM
31f6de22
VZ
84 #define TEST_THREADS
85 #define TEST_TIMER
86 // #define TEST_VCARD -- don't enable this (VZ)
8d4dc98f 87// #define TEST_VOLUME --FIXME! (RN)
31f6de22
VZ
88 #define TEST_WCHAR
89 #define TEST_ZIP
e9d2bb6f 90#else // #if TEST_ALL
549b95b3 91 #define TEST_TIMER
31f6de22 92#endif
f6bcfd97 93
daa2c7d9
VZ
94// some tests are interactive, define this to run them
95#ifdef TEST_INTERACTIVE
96 #undef TEST_INTERACTIVE
97
e9d2bb6f 98 #define TEST_INTERACTIVE 1
daa2c7d9 99#else
e9d2bb6f 100 #define TEST_INTERACTIVE 0
daa2c7d9 101#endif
58b24a56 102
e87271f3
VZ
103// ============================================================================
104// implementation
105// ============================================================================
106
8e907a13
VZ
107// ----------------------------------------------------------------------------
108// helper functions
109// ----------------------------------------------------------------------------
110
1cd53e88 111#if defined(TEST_SOCKETS)
8e907a13
VZ
112
113// replace TABs with \t and CRs with \n
114static wxString MakePrintable(const wxChar *s)
115{
116 wxString str(s);
117 (void)str.Replace(_T("\t"), _T("\\t"));
118 (void)str.Replace(_T("\n"), _T("\\n"));
119 (void)str.Replace(_T("\r"), _T("\\r"));
120
121 return str;
122}
123
124#endif // MakePrintable() is used
125
d34bce84
VZ
126// ----------------------------------------------------------------------------
127// wxCmdLineParser
128// ----------------------------------------------------------------------------
129
d31b7b68
VZ
130#ifdef TEST_CMDLINE
131
e84010cf
GD
132#include "wx/cmdline.h"
133#include "wx/datetime.h"
d34bce84 134
31f6de22
VZ
135#if wxUSE_CMDLINE_PARSER
136
d34bce84
VZ
137static void ShowCmdLine(const wxCmdLineParser& parser)
138{
5df663af 139 wxString s = _T("Command line parsed successfully:\nInput files: ");
d34bce84
VZ
140
141 size_t count = parser.GetParamCount();
142 for ( size_t param = 0; param < count; param++ )
143 {
144 s << parser.GetParam(param) << ' ';
145 }
146
147 s << '\n'
456ae26d
VZ
148 << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
149 << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
d34bce84
VZ
150
151 wxString strVal;
152 long lVal;
153 wxDateTime dt;
456ae26d
VZ
154 if ( parser.Found(_T("o"), &strVal) )
155 s << _T("Output file:\t") << strVal << '\n';
156 if ( parser.Found(_T("i"), &strVal) )
157 s << _T("Input dir:\t") << strVal << '\n';
158 if ( parser.Found(_T("s"), &lVal) )
159 s << _T("Size:\t") << lVal << '\n';
160 if ( parser.Found(_T("d"), &dt) )
161 s << _T("Date:\t") << dt.FormatISODate() << '\n';
162 if ( parser.Found(_T("project_name"), &strVal) )
163 s << _T("Project:\t") << strVal << '\n';
d34bce84
VZ
164
165 wxLogMessage(s);
166}
167
31f6de22
VZ
168#endif // wxUSE_CMDLINE_PARSER
169
170static void TestCmdLineConvert()
171{
456ae26d 172 static const wxChar *cmdlines[] =
31f6de22 173 {
456ae26d
VZ
174 _T("arg1 arg2"),
175 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
176 _T("literal \\\" and \"\""),
31f6de22
VZ
177 };
178
179 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
180 {
456ae26d
VZ
181 const wxChar *cmdline = cmdlines[n];
182 wxPrintf(_T("Parsing: %s\n"), cmdline);
31f6de22
VZ
183 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
184
185 size_t count = args.GetCount();
456ae26d 186 wxPrintf(_T("\targc = %u\n"), count);
31f6de22
VZ
187 for ( size_t arg = 0; arg < count; arg++ )
188 {
456ae26d 189 wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str());
31f6de22
VZ
190 }
191 }
192}
193
d34bce84
VZ
194#endif // TEST_CMDLINE
195
1944c6bd
VZ
196// ----------------------------------------------------------------------------
197// wxDir
198// ----------------------------------------------------------------------------
199
200#ifdef TEST_DIR
201
e84010cf 202#include "wx/dir.h"
1944c6bd 203
35332784
VZ
204#ifdef __UNIX__
205 static const wxChar *ROOTDIR = _T("/");
2f0c19d0 206 static const wxChar *TESTDIR = _T("/usr/local/share");
65e324b4 207#elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
35332784
VZ
208 static const wxChar *ROOTDIR = _T("c:\\");
209 static const wxChar *TESTDIR = _T("d:\\");
210#else
211 #error "don't know where the root directory is"
212#endif
213
1944c6bd
VZ
214static void TestDirEnumHelper(wxDir& dir,
215 int flags = wxDIR_DEFAULT,
216 const wxString& filespec = wxEmptyString)
217{
218 wxString filename;
219
220 if ( !dir.IsOpened() )
221 return;
222
223 bool cont = dir.GetFirst(&filename, filespec, flags);
224 while ( cont )
225 {
456ae26d 226 wxPrintf(_T("\t%s\n"), filename.c_str());
1944c6bd
VZ
227
228 cont = dir.GetNext(&filename);
229 }
230
e9d2bb6f 231 wxPuts(wxEmptyString);
1944c6bd
VZ
232}
233
dab6fbae
WS
234#if TEST_ALL
235
1944c6bd
VZ
236static void TestDirEnum()
237{
456ae26d 238 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
35332784 239
149147e1 240 wxString cwd = wxGetCwd();
9475670f 241 if ( !wxDir::Exists(cwd) )
149147e1 242 {
456ae26d 243 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
149147e1
VZ
244 return;
245 }
246
ee3ef281 247 wxDir dir(cwd);
149147e1
VZ
248 if ( !dir.IsOpened() )
249 {
456ae26d 250 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
149147e1
VZ
251 return;
252 }
1944c6bd 253
456ae26d 254 wxPuts(_T("Enumerating everything in current directory:"));
1944c6bd
VZ
255 TestDirEnumHelper(dir);
256
456ae26d 257 wxPuts(_T("Enumerating really everything in current directory:"));
1944c6bd
VZ
258 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
259
456ae26d 260 wxPuts(_T("Enumerating object files in current directory:"));
f2cb8a17 261 TestDirEnumHelper(dir, wxDIR_DEFAULT, _T("*.o*"));
1944c6bd 262
456ae26d 263 wxPuts(_T("Enumerating directories in current directory:"));
1944c6bd
VZ
264 TestDirEnumHelper(dir, wxDIR_DIRS);
265
456ae26d 266 wxPuts(_T("Enumerating files in current directory:"));
1944c6bd
VZ
267 TestDirEnumHelper(dir, wxDIR_FILES);
268
456ae26d 269 wxPuts(_T("Enumerating files including hidden in current directory:"));
1944c6bd
VZ
270 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
271
35332784 272 dir.Open(ROOTDIR);
1944c6bd 273
456ae26d 274 wxPuts(_T("Enumerating everything in root directory:"));
1944c6bd
VZ
275 TestDirEnumHelper(dir, wxDIR_DEFAULT);
276
456ae26d 277 wxPuts(_T("Enumerating directories in root directory:"));
1944c6bd
VZ
278 TestDirEnumHelper(dir, wxDIR_DIRS);
279
456ae26d 280 wxPuts(_T("Enumerating files in root directory:"));
1944c6bd
VZ
281 TestDirEnumHelper(dir, wxDIR_FILES);
282
456ae26d 283 wxPuts(_T("Enumerating files including hidden in root directory:"));
1944c6bd
VZ
284 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
285
456ae26d 286 wxPuts(_T("Enumerating files in non existing directory:"));
f2cb8a17 287 wxDir dirNo(_T("nosuchdir"));
1944c6bd
VZ
288 TestDirEnumHelper(dirNo);
289}
290
dab6fbae
WS
291#endif // TEST_ALL
292
35332784
VZ
293class DirPrintTraverser : public wxDirTraverser
294{
295public:
e9d2bb6f 296 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
35332784
VZ
297 {
298 return wxDIR_CONTINUE;
299 }
300
301 virtual wxDirTraverseResult OnDir(const wxString& dirname)
302 {
303 wxString path, name, ext;
304 wxSplitPath(dirname, &path, &name, &ext);
305
306 if ( !ext.empty() )
307 name << _T('.') << ext;
308
309 wxString indent;
310 for ( const wxChar *p = path.c_str(); *p; p++ )
311 {
312 if ( wxIsPathSeparator(*p) )
313 indent += _T(" ");
314 }
315
456ae26d 316 wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str());
35332784
VZ
317
318 return wxDIR_CONTINUE;
319 }
320};
321
322static void TestDirTraverse()
323{
456ae26d 324 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
35332784
VZ
325
326 // enum all files
327 wxArrayString files;
328 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
456ae26d 329 wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR);
35332784
VZ
330 if ( n > 1 )
331 {
456ae26d
VZ
332 wxPrintf(_T("First one is '%s'\n"), files[0u].c_str());
333 wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str());
35332784
VZ
334 }
335
336 // enum again with custom traverser
2f0c19d0 337 wxPuts(_T("Now enumerating directories:"));
35332784
VZ
338 wxDir dir(TESTDIR);
339 DirPrintTraverser traverser;
e9d2bb6f 340 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
35332784
VZ
341}
342
dab6fbae
WS
343#if TEST_ALL
344
149147e1
VZ
345static void TestDirExists()
346{
347 wxPuts(_T("*** Testing wxDir::Exists() ***"));
348
456ae26d 349 static const wxChar *dirnames[] =
149147e1
VZ
350 {
351 _T("."),
352#if defined(__WXMSW__)
353 _T("c:"),
354 _T("c:\\"),
355 _T("\\\\share\\file"),
356 _T("c:\\dos"),
357 _T("c:\\dos\\"),
358 _T("c:\\dos\\\\"),
359 _T("c:\\autoexec.bat"),
360#elif defined(__UNIX__)
361 _T("/"),
362 _T("//"),
363 _T("/usr/bin"),
364 _T("/usr//bin"),
365 _T("/usr///bin"),
366#endif
367 };
368
369 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
370 {
456ae26d
VZ
371 wxPrintf(_T("%-40s: %s\n"),
372 dirnames[n],
373 wxDir::Exists(dirnames[n]) ? _T("exists")
374 : _T("doesn't exist"));
149147e1
VZ
375 }
376}
377
dab6fbae
WS
378#endif // TEST_ALL
379
1944c6bd
VZ
380#endif // TEST_DIR
381
f6bcfd97
BP
382// ----------------------------------------------------------------------------
383// wxDllLoader
384// ----------------------------------------------------------------------------
385
93ed8ff7 386#ifdef TEST_DYNLIB
f6bcfd97 387
e84010cf 388#include "wx/dynlib.h"
f6bcfd97
BP
389
390static void TestDllLoad()
391{
392#if defined(__WXMSW__)
393 static const wxChar *LIB_NAME = _T("kernel32.dll");
394 static const wxChar *FUNC_NAME = _T("lstrlenA");
395#elif defined(__UNIX__)
396 // weird: using just libc.so does *not* work!
e259f83e 397 static const wxChar *LIB_NAME = _T("/lib/libc.so.6");
f6bcfd97
BP
398 static const wxChar *FUNC_NAME = _T("strlen");
399#else
400 #error "don't know how to test wxDllLoader on this platform"
401#endif
402
297ebe6b 403 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
f6bcfd97 404
456ae26d
VZ
405 wxDynamicLibrary lib(LIB_NAME);
406 if ( !lib.IsLoaded() )
f6bcfd97
BP
407 {
408 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
409 }
410 else
411 {
93ed8ff7 412 typedef int (wxSTDCALL *wxStrlenType)(const char *);
456ae26d 413 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
f6bcfd97
BP
414 if ( !pfnStrlen )
415 {
416 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
417 FUNC_NAME, LIB_NAME);
418 }
419 else
420 {
e259f83e
VZ
421 wxPrintf(_T("Calling %s dynamically loaded from %s "),
422 FUNC_NAME, LIB_NAME);
423
f6bcfd97
BP
424 if ( pfnStrlen("foo") != 3 )
425 {
456ae26d 426 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
f6bcfd97
BP
427 }
428 else
429 {
456ae26d 430 wxPuts(_T("... ok"));
f6bcfd97
BP
431 }
432 }
93ed8ff7
VZ
433
434#ifdef __WXMSW__
435 static const wxChar *FUNC_NAME_AW = _T("lstrlen");
436
437 typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
438 wxStrlenTypeAorW
439 pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
440 if ( !pfnStrlenAorW )
441 {
442 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
443 FUNC_NAME_AW, LIB_NAME);
444 }
445 else
446 {
447 if ( pfnStrlenAorW(_T("foobar")) != 6 )
448 {
449 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
450 }
451 }
452#endif // __WXMSW__
f6bcfd97
BP
453 }
454}
455
297ebe6b
VZ
456#if defined(__WXMSW__) || defined(__UNIX__)
457
458static void TestDllListLoaded()
459{
460 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
461
462 puts("\nLoaded modules:");
463 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
464 const size_t count = dlls.GetCount();
465 for ( size_t n = 0; n < count; ++n )
466 {
467 const wxDynamicLibraryDetails& details = dlls[n];
eaff0f0d 468 printf("%-45s", details.GetPath().mb_str());
297ebe6b
VZ
469
470 void *addr;
471 size_t len;
472 if ( details.GetAddress(&addr, &len) )
473 {
474 printf(" %08lx:%08lx",
475 (unsigned long)addr, (unsigned long)((char *)addr + len));
476 }
477
eaff0f0d 478 printf(" %s\n", details.GetVersion().mb_str());
297ebe6b
VZ
479 }
480}
481
482#endif
483
93ed8ff7 484#endif // TEST_DYNLIB
f6bcfd97 485
8fd0d89b
VZ
486// ----------------------------------------------------------------------------
487// wxGet/SetEnv
488// ----------------------------------------------------------------------------
489
490#ifdef TEST_ENVIRON
491
e84010cf 492#include "wx/utils.h"
8fd0d89b 493
308978f6
VZ
494static wxString MyGetEnv(const wxString& var)
495{
496 wxString val;
497 if ( !wxGetEnv(var, &val) )
498 val = _T("<empty>");
499 else
500 val = wxString(_T('\'')) + val + _T('\'');
501
502 return val;
503}
504
8fd0d89b
VZ
505static void TestEnvironment()
506{
507 const wxChar *var = _T("wxTestVar");
508
456ae26d 509 wxPuts(_T("*** testing environment access functions ***"));
8fd0d89b 510
456ae26d 511 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 512 wxSetEnv(var, _T("value for wxTestVar"));
456ae26d 513 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 514 wxSetEnv(var, _T("another value"));
456ae26d 515 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 516 wxUnsetEnv(var);
456ae26d
VZ
517 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
518 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
8fd0d89b
VZ
519}
520
521#endif // TEST_ENVIRON
522
d93c719a
VZ
523// ----------------------------------------------------------------------------
524// wxExecute
525// ----------------------------------------------------------------------------
526
527#ifdef TEST_EXECUTE
528
e84010cf 529#include "wx/utils.h"
d93c719a
VZ
530
531static void TestExecute()
532{
456ae26d 533 wxPuts(_T("*** testing wxExecute ***"));
d93c719a
VZ
534
535#ifdef __UNIX__
a1f79c1e 536 #define COMMAND "cat -n ../../Makefile" // "echo hi"
2c8e4738 537 #define SHELL_COMMAND "echo hi from shell"
a1f79c1e 538 #define REDIRECT_COMMAND COMMAND // "date"
d93c719a 539#elif defined(__WXMSW__)
50ded68d 540 #define COMMAND "command.com /c echo hi"
2c8e4738
VZ
541 #define SHELL_COMMAND "echo hi"
542 #define REDIRECT_COMMAND COMMAND
d93c719a
VZ
543#else
544 #error "no command to exec"
545#endif // OS
546
456ae26d 547 wxPrintf(_T("Testing wxShell: "));
2c8e4738 548 fflush(stdout);
f2cb8a17 549 if ( wxShell(_T(SHELL_COMMAND)) )
456ae26d 550 wxPuts(_T("Ok."));
d93c719a 551 else
456ae26d 552 wxPuts(_T("ERROR."));
2c8e4738 553
456ae26d 554 wxPrintf(_T("Testing wxExecute: "));
2c8e4738 555 fflush(stdout);
f2cb8a17 556 if ( wxExecute(_T(COMMAND), true /* sync */) == 0 )
456ae26d 557 wxPuts(_T("Ok."));
2c8e4738 558 else
456ae26d 559 wxPuts(_T("ERROR."));
2c8e4738
VZ
560
561#if 0 // no, it doesn't work (yet?)
456ae26d 562 wxPrintf(_T("Testing async wxExecute: "));
2c8e4738
VZ
563 fflush(stdout);
564 if ( wxExecute(COMMAND) != 0 )
456ae26d 565 wxPuts(_T("Ok (command launched)."));
2c8e4738 566 else
456ae26d 567 wxPuts(_T("ERROR."));
2c8e4738
VZ
568#endif // 0
569
456ae26d 570 wxPrintf(_T("Testing wxExecute with redirection:\n"));
2c8e4738 571 wxArrayString output;
f2cb8a17 572 if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 )
2c8e4738 573 {
456ae26d 574 wxPuts(_T("ERROR."));
2c8e4738
VZ
575 }
576 else
577 {
578 size_t count = output.GetCount();
579 for ( size_t n = 0; n < count; n++ )
580 {
456ae26d 581 wxPrintf(_T("\t%s\n"), output[n].c_str());
2c8e4738
VZ
582 }
583
456ae26d 584 wxPuts(_T("Ok."));
2c8e4738 585 }
d93c719a
VZ
586}
587
588#endif // TEST_EXECUTE
589
f6bcfd97
BP
590// ----------------------------------------------------------------------------
591// file
592// ----------------------------------------------------------------------------
593
594#ifdef TEST_FILE
595
e84010cf
GD
596#include "wx/file.h"
597#include "wx/ffile.h"
598#include "wx/textfile.h"
f6bcfd97
BP
599
600static void TestFileRead()
601{
456ae26d 602 wxPuts(_T("*** wxFile read test ***"));
f6bcfd97
BP
603
604 wxFile file(_T("testdata.fc"));
605 if ( file.IsOpened() )
606 {
456ae26d 607 wxPrintf(_T("File length: %lu\n"), file.Length());
f6bcfd97 608
456ae26d 609 wxPuts(_T("File dump:\n----------"));
f6bcfd97 610
30984dea 611 static const size_t len = 1024;
456ae26d 612 wxChar buf[len];
f6bcfd97
BP
613 for ( ;; )
614 {
30984dea
VZ
615 size_t nRead = file.Read(buf, len);
616 if ( nRead == (size_t)wxInvalidOffset )
f6bcfd97 617 {
456ae26d 618 wxPrintf(_T("Failed to read the file."));
f6bcfd97
BP
619 break;
620 }
621
622 fwrite(buf, nRead, 1, stdout);
623
624 if ( nRead < len )
625 break;
626 }
627
456ae26d 628 wxPuts(_T("----------"));
f6bcfd97
BP
629 }
630 else
631 {
456ae26d 632 wxPrintf(_T("ERROR: can't open test file.\n"));
f6bcfd97
BP
633 }
634
e9d2bb6f 635 wxPuts(wxEmptyString);
f6bcfd97
BP
636}
637
638static void TestTextFileRead()
639{
456ae26d 640 wxPuts(_T("*** wxTextFile read test ***"));
f6bcfd97
BP
641
642 wxTextFile file(_T("testdata.fc"));
643 if ( file.Open() )
644 {
456ae26d
VZ
645 wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount());
646 wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str());
3ca6a5f0
BP
647
648 wxString s;
649
456ae26d 650 wxPuts(_T("\nDumping the entire file:"));
3ca6a5f0
BP
651 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
652 {
456ae26d 653 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 654 }
456ae26d 655 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 656
456ae26d 657 wxPuts(_T("\nAnd now backwards:"));
3ca6a5f0
BP
658 for ( s = file.GetLastLine();
659 file.GetCurrentLine() != 0;
660 s = file.GetPrevLine() )
661 {
456ae26d 662 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 663 }
456ae26d 664 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
f6bcfd97
BP
665 }
666 else
667 {
456ae26d 668 wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName());
f6bcfd97
BP
669 }
670
e9d2bb6f 671 wxPuts(wxEmptyString);
f6bcfd97
BP
672}
673
a339970a
VZ
674static void TestFileCopy()
675{
456ae26d 676 wxPuts(_T("*** Testing wxCopyFile ***"));
a339970a
VZ
677
678 static const wxChar *filename1 = _T("testdata.fc");
679 static const wxChar *filename2 = _T("test2");
680 if ( !wxCopyFile(filename1, filename2) )
681 {
456ae26d 682 wxPuts(_T("ERROR: failed to copy file"));
a339970a
VZ
683 }
684 else
685 {
f2cb8a17
JS
686 wxFFile f1(filename1, _T("rb")),
687 f2(filename2, _T("rb"));
a339970a
VZ
688
689 if ( !f1.IsOpened() || !f2.IsOpened() )
690 {
456ae26d 691 wxPuts(_T("ERROR: failed to open file(s)"));
a339970a
VZ
692 }
693 else
694 {
695 wxString s1, s2;
696 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
697 {
456ae26d 698 wxPuts(_T("ERROR: failed to read file(s)"));
a339970a
VZ
699 }
700 else
701 {
702 if ( (s1.length() != s2.length()) ||
703 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
704 {
456ae26d 705 wxPuts(_T("ERROR: copy error!"));
a339970a
VZ
706 }
707 else
708 {
456ae26d 709 wxPuts(_T("File was copied ok."));
a339970a
VZ
710 }
711 }
712 }
713 }
714
715 if ( !wxRemoveFile(filename2) )
716 {
456ae26d 717 wxPuts(_T("ERROR: failed to remove the file"));
a339970a
VZ
718 }
719
e9d2bb6f 720 wxPuts(wxEmptyString);
a339970a
VZ
721}
722
59062ec1
VZ
723static void TestTempFile()
724{
725 wxPuts(_T("*** wxTempFile test ***"));
726
727 wxTempFile tmpFile;
728 if ( tmpFile.Open(_T("test2")) && tmpFile.Write(_T("the answer is 42")) )
729 {
730 if ( tmpFile.Commit() )
731 wxPuts(_T("File committed."));
732 else
733 wxPuts(_T("ERROR: could't commit temp file."));
734
735 wxRemoveFile(_T("test2"));
736 }
737
738 wxPuts(wxEmptyString);
739}
740
f6bcfd97
BP
741#endif // TEST_FILE
742
ee6e1b1d
VZ
743// ----------------------------------------------------------------------------
744// wxFileConfig
745// ----------------------------------------------------------------------------
746
747#ifdef TEST_FILECONF
748
e84010cf
GD
749#include "wx/confbase.h"
750#include "wx/fileconf.h"
ee6e1b1d
VZ
751
752static const struct FileConfTestData
753{
754 const wxChar *name; // value name
755 const wxChar *value; // the value from the file
756} fcTestData[] =
757{
758 { _T("value1"), _T("one") },
759 { _T("value2"), _T("two") },
760 { _T("novalue"), _T("default") },
761};
762
763static void TestFileConfRead()
764{
456ae26d 765 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
ee6e1b1d
VZ
766
767 wxFileConfig fileconf(_T("test"), wxEmptyString,
768 _T("testdata.fc"), wxEmptyString,
769 wxCONFIG_USE_RELATIVE_PATH);
770
771 // test simple reading
456ae26d 772 wxPuts(_T("\nReading config file:"));
ee6e1b1d
VZ
773 wxString defValue(_T("default")), value;
774 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
775 {
776 const FileConfTestData& data = fcTestData[n];
777 value = fileconf.Read(data.name, defValue);
456ae26d 778 wxPrintf(_T("\t%s = %s "), data.name, value.c_str());
ee6e1b1d
VZ
779 if ( value == data.value )
780 {
456ae26d 781 wxPuts(_T("(ok)"));
ee6e1b1d
VZ
782 }
783 else
784 {
456ae26d 785 wxPrintf(_T("(ERROR: should be %s)\n"), data.value);
ee6e1b1d
VZ
786 }
787 }
788
789 // test enumerating the entries
456ae26d 790 wxPuts(_T("\nEnumerating all root entries:"));
ee6e1b1d
VZ
791 long dummy;
792 wxString name;
793 bool cont = fileconf.GetFirstEntry(name, dummy);
794 while ( cont )
795 {
456ae26d 796 wxPrintf(_T("\t%s = %s\n"),
ee6e1b1d
VZ
797 name.c_str(),
798 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
799
800 cont = fileconf.GetNextEntry(name, dummy);
801 }
7e0777da
VZ
802
803 static const wxChar *testEntry = _T("TestEntry");
804 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
805 fileconf.Write(testEntry, _T("A value"));
806 fileconf.DeleteEntry(testEntry);
807 wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n"));
ee6e1b1d
VZ
808}
809
810#endif // TEST_FILECONF
811
844f90fb
VZ
812// ----------------------------------------------------------------------------
813// wxFileName
814// ----------------------------------------------------------------------------
815
816#ifdef TEST_FILENAME
817
e84010cf 818#include "wx/filename.h"
844f90fb 819
e9d2bb6f 820#if 0
5bc1deeb 821static void DumpFileName(const wxChar *desc, const wxFileName& fn)
81f25632 822{
5bc1deeb
VZ
823 wxPuts(desc);
824
81f25632
VZ
825 wxString full = fn.GetFullPath();
826
827 wxString vol, path, name, ext;
828 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
829
a5b7374f 830 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
81f25632 831 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
a5b7374f
VZ
832
833 wxFileName::SplitPath(full, &path, &name, &ext);
834 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
835 path.c_str(), name.c_str(), ext.c_str());
836
837 wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str());
838 wxPrintf(_T("with volume: \t'%s'\n"),
839 fn.GetPath(wxPATH_GET_VOLUME).c_str());
840 wxPrintf(_T("with separator:\t'%s'\n"),
841 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
842 wxPrintf(_T("with both: \t'%s'\n"),
843 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
9cb47ea2
VZ
844
845 wxPuts(_T("The directories in the path are:"));
846 wxArrayString dirs = fn.GetDirs();
847 size_t count = dirs.GetCount();
848 for ( size_t n = 0; n < count; n++ )
849 {
850 wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str());
851 }
81f25632 852}
e9d2bb6f 853#endif
81f25632 854
ade35f11
VZ
855static void TestFileNameTemp()
856{
456ae26d 857 wxPuts(_T("*** testing wxFileName temp file creation ***"));
ade35f11 858
456ae26d 859 static const wxChar *tmpprefixes[] =
ade35f11 860 {
456ae26d
VZ
861 _T(""),
862 _T("foo"),
863 _T(".."),
864 _T("../bar"),
a2fa5040 865#ifdef __UNIX__
456ae26d
VZ
866 _T("/tmp/foo"),
867 _T("/tmp/foo/bar"), // this one must be an error
a2fa5040 868#endif // __UNIX__
ade35f11
VZ
869 };
870
871 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
872 {
873 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
2db991f4
VZ
874 if ( path.empty() )
875 {
876 // "error" is not in upper case because it may be ok
456ae26d 877 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
2db991f4
VZ
878 }
879 else
ade35f11 880 {
456ae26d 881 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
ade35f11
VZ
882 tmpprefixes[n], path.c_str());
883
884 if ( !wxRemoveFile(path) )
885 {
456ae26d
VZ
886 wxLogWarning(_T("Failed to remove temp file '%s'"),
887 path.c_str());
ade35f11
VZ
888 }
889 }
890 }
891}
892
0aa29b6b
VZ
893static void TestFileNameDirManip()
894{
895 // TODO: test AppendDir(), RemoveDir(), ...
896}
897
844f90fb
VZ
898static void TestFileNameComparison()
899{
900 // TODO!
901}
902
903static void TestFileNameOperations()
904{
905 // TODO!
906}
907
908static void TestFileNameCwd()
909{
910 // TODO!
911}
912
913#endif // TEST_FILENAME
914
d56e2b97
VZ
915// ----------------------------------------------------------------------------
916// wxFileName time functions
917// ----------------------------------------------------------------------------
918
919#ifdef TEST_FILETIME
920
b20edf8b
WS
921#include "wx/filename.h"
922#include "wx/datetime.h"
d56e2b97
VZ
923
924static void TestFileGetTimes()
925{
926 wxFileName fn(_T("testdata.fc"));
927
6dbb903b
VZ
928 wxDateTime dtAccess, dtMod, dtCreate;
929 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
d56e2b97
VZ
930 {
931 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
932 }
933 else
934 {
935 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
936
937 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
6dbb903b
VZ
938 wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
939 wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
940 wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
d56e2b97
VZ
941 }
942}
943
e9d2bb6f 944#if 0
d56e2b97
VZ
945static void TestFileSetTimes()
946{
947 wxFileName fn(_T("testdata.fc"));
948
d56e2b97
VZ
949 if ( !fn.Touch() )
950 {
951 wxPrintf(_T("ERROR: Touch() failed.\n"));
952 }
953}
e9d2bb6f 954#endif
d56e2b97
VZ
955
956#endif // TEST_FILETIME
957
ec37df57
VZ
958// ----------------------------------------------------------------------------
959// wxLocale
960// ----------------------------------------------------------------------------
961
962#ifdef TEST_LOCALE
963
964#include "wx/intl.h"
965#include "wx/utils.h" // for wxSetEnv
966
967static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH);
968
969// find the name of the language from its value
456ae26d
VZ
970static const wxChar *GetLangName(int lang)
971{
972 static const wxChar *languageNames[] =
973 {
974 _T("DEFAULT"),
975 _T("UNKNOWN"),
976 _T("ABKHAZIAN"),
977 _T("AFAR"),
978 _T("AFRIKAANS"),
979 _T("ALBANIAN"),
980 _T("AMHARIC"),
981 _T("ARABIC"),
982 _T("ARABIC_ALGERIA"),
983 _T("ARABIC_BAHRAIN"),
984 _T("ARABIC_EGYPT"),
985 _T("ARABIC_IRAQ"),
986 _T("ARABIC_JORDAN"),
987 _T("ARABIC_KUWAIT"),
988 _T("ARABIC_LEBANON"),
989 _T("ARABIC_LIBYA"),
990 _T("ARABIC_MOROCCO"),
991 _T("ARABIC_OMAN"),
992 _T("ARABIC_QATAR"),
993 _T("ARABIC_SAUDI_ARABIA"),
994 _T("ARABIC_SUDAN"),
995 _T("ARABIC_SYRIA"),
996 _T("ARABIC_TUNISIA"),
997 _T("ARABIC_UAE"),
998 _T("ARABIC_YEMEN"),
999 _T("ARMENIAN"),
1000 _T("ASSAMESE"),
1001 _T("AYMARA"),
1002 _T("AZERI"),
1003 _T("AZERI_CYRILLIC"),
1004 _T("AZERI_LATIN"),
1005 _T("BASHKIR"),
1006 _T("BASQUE"),
1007 _T("BELARUSIAN"),
1008 _T("BENGALI"),
1009 _T("BHUTANI"),
1010 _T("BIHARI"),
1011 _T("BISLAMA"),
1012 _T("BRETON"),
1013 _T("BULGARIAN"),
1014 _T("BURMESE"),
1015 _T("CAMBODIAN"),
1016 _T("CATALAN"),
1017 _T("CHINESE"),
1018 _T("CHINESE_SIMPLIFIED"),
1019 _T("CHINESE_TRADITIONAL"),
1020 _T("CHINESE_HONGKONG"),
1021 _T("CHINESE_MACAU"),
1022 _T("CHINESE_SINGAPORE"),
1023 _T("CHINESE_TAIWAN"),
1024 _T("CORSICAN"),
1025 _T("CROATIAN"),
1026 _T("CZECH"),
1027 _T("DANISH"),
1028 _T("DUTCH"),
1029 _T("DUTCH_BELGIAN"),
1030 _T("ENGLISH"),
1031 _T("ENGLISH_UK"),
1032 _T("ENGLISH_US"),
1033 _T("ENGLISH_AUSTRALIA"),
1034 _T("ENGLISH_BELIZE"),
1035 _T("ENGLISH_BOTSWANA"),
1036 _T("ENGLISH_CANADA"),
1037 _T("ENGLISH_CARIBBEAN"),
1038 _T("ENGLISH_DENMARK"),
1039 _T("ENGLISH_EIRE"),
1040 _T("ENGLISH_JAMAICA"),
1041 _T("ENGLISH_NEW_ZEALAND"),
1042 _T("ENGLISH_PHILIPPINES"),
1043 _T("ENGLISH_SOUTH_AFRICA"),
1044 _T("ENGLISH_TRINIDAD"),
1045 _T("ENGLISH_ZIMBABWE"),
1046 _T("ESPERANTO"),
1047 _T("ESTONIAN"),
1048 _T("FAEROESE"),
1049 _T("FARSI"),
1050 _T("FIJI"),
1051 _T("FINNISH"),
1052 _T("FRENCH"),
1053 _T("FRENCH_BELGIAN"),
1054 _T("FRENCH_CANADIAN"),
1055 _T("FRENCH_LUXEMBOURG"),
1056 _T("FRENCH_MONACO"),
1057 _T("FRENCH_SWISS"),
1058 _T("FRISIAN"),
1059 _T("GALICIAN"),
1060 _T("GEORGIAN"),
1061 _T("GERMAN"),
1062 _T("GERMAN_AUSTRIAN"),
1063 _T("GERMAN_BELGIUM"),
1064 _T("GERMAN_LIECHTENSTEIN"),
1065 _T("GERMAN_LUXEMBOURG"),
1066 _T("GERMAN_SWISS"),
1067 _T("GREEK"),
1068 _T("GREENLANDIC"),
1069 _T("GUARANI"),
1070 _T("GUJARATI"),
1071 _T("HAUSA"),
1072 _T("HEBREW"),
1073 _T("HINDI"),
1074 _T("HUNGARIAN"),
1075 _T("ICELANDIC"),
1076 _T("INDONESIAN"),
1077 _T("INTERLINGUA"),
1078 _T("INTERLINGUE"),
1079 _T("INUKTITUT"),
1080 _T("INUPIAK"),
1081 _T("IRISH"),
1082 _T("ITALIAN"),
1083 _T("ITALIAN_SWISS"),
1084 _T("JAPANESE"),
1085 _T("JAVANESE"),
1086 _T("KANNADA"),
1087 _T("KASHMIRI"),
1088 _T("KASHMIRI_INDIA"),
1089 _T("KAZAKH"),
1090 _T("KERNEWEK"),
1091 _T("KINYARWANDA"),
1092 _T("KIRGHIZ"),
1093 _T("KIRUNDI"),
1094 _T("KONKANI"),
1095 _T("KOREAN"),
1096 _T("KURDISH"),
1097 _T("LAOTHIAN"),
1098 _T("LATIN"),
1099 _T("LATVIAN"),
1100 _T("LINGALA"),
1101 _T("LITHUANIAN"),
1102 _T("MACEDONIAN"),
1103 _T("MALAGASY"),
1104 _T("MALAY"),
1105 _T("MALAYALAM"),
1106 _T("MALAY_BRUNEI_DARUSSALAM"),
1107 _T("MALAY_MALAYSIA"),
1108 _T("MALTESE"),
1109 _T("MANIPURI"),
1110 _T("MAORI"),
1111 _T("MARATHI"),
1112 _T("MOLDAVIAN"),
1113 _T("MONGOLIAN"),
1114 _T("NAURU"),
1115 _T("NEPALI"),
1116 _T("NEPALI_INDIA"),
1117 _T("NORWEGIAN_BOKMAL"),
1118 _T("NORWEGIAN_NYNORSK"),
1119 _T("OCCITAN"),
1120 _T("ORIYA"),
1121 _T("OROMO"),
1122 _T("PASHTO"),
1123 _T("POLISH"),
1124 _T("PORTUGUESE"),
1125 _T("PORTUGUESE_BRAZILIAN"),
1126 _T("PUNJABI"),
1127 _T("QUECHUA"),
1128 _T("RHAETO_ROMANCE"),
1129 _T("ROMANIAN"),
1130 _T("RUSSIAN"),
1131 _T("RUSSIAN_UKRAINE"),
1132 _T("SAMOAN"),
1133 _T("SANGHO"),
1134 _T("SANSKRIT"),
1135 _T("SCOTS_GAELIC"),
1136 _T("SERBIAN"),
1137 _T("SERBIAN_CYRILLIC"),
1138 _T("SERBIAN_LATIN"),
1139 _T("SERBO_CROATIAN"),
1140 _T("SESOTHO"),
1141 _T("SETSWANA"),
1142 _T("SHONA"),
1143 _T("SINDHI"),
1144 _T("SINHALESE"),
1145 _T("SISWATI"),
1146 _T("SLOVAK"),
1147 _T("SLOVENIAN"),
1148 _T("SOMALI"),
1149 _T("SPANISH"),
1150 _T("SPANISH_ARGENTINA"),
1151 _T("SPANISH_BOLIVIA"),
1152 _T("SPANISH_CHILE"),
1153 _T("SPANISH_COLOMBIA"),
1154 _T("SPANISH_COSTA_RICA"),
1155 _T("SPANISH_DOMINICAN_REPUBLIC"),
1156 _T("SPANISH_ECUADOR"),
1157 _T("SPANISH_EL_SALVADOR"),
1158 _T("SPANISH_GUATEMALA"),
1159 _T("SPANISH_HONDURAS"),
1160 _T("SPANISH_MEXICAN"),
1161 _T("SPANISH_MODERN"),
1162 _T("SPANISH_NICARAGUA"),
1163 _T("SPANISH_PANAMA"),
1164 _T("SPANISH_PARAGUAY"),
1165 _T("SPANISH_PERU"),
1166 _T("SPANISH_PUERTO_RICO"),
1167 _T("SPANISH_URUGUAY"),
1168 _T("SPANISH_US"),
1169 _T("SPANISH_VENEZUELA"),
1170 _T("SUNDANESE"),
1171 _T("SWAHILI"),
1172 _T("SWEDISH"),
1173 _T("SWEDISH_FINLAND"),
1174 _T("TAGALOG"),
1175 _T("TAJIK"),
1176 _T("TAMIL"),
1177 _T("TATAR"),
1178 _T("TELUGU"),
1179 _T("THAI"),
1180 _T("TIBETAN"),
1181 _T("TIGRINYA"),
1182 _T("TONGA"),
1183 _T("TSONGA"),
1184 _T("TURKISH"),
1185 _T("TURKMEN"),
1186 _T("TWI"),
1187 _T("UIGHUR"),
1188 _T("UKRAINIAN"),
1189 _T("URDU"),
1190 _T("URDU_INDIA"),
1191 _T("URDU_PAKISTAN"),
1192 _T("UZBEK"),
1193 _T("UZBEK_CYRILLIC"),
1194 _T("UZBEK_LATIN"),
1195 _T("VIETNAMESE"),
1196 _T("VOLAPUK"),
1197 _T("WELSH"),
1198 _T("WOLOF"),
1199 _T("XHOSA"),
1200 _T("YIDDISH"),
1201 _T("YORUBA"),
1202 _T("ZHUANG"),
1203 _T("ZULU"),
ec37df57
VZ
1204 };
1205
1206 if ( (size_t)lang < WXSIZEOF(languageNames) )
1207 return languageNames[lang];
1208 else
456ae26d 1209 return _T("INVALID");
ec37df57
VZ
1210}
1211
1212static void TestDefaultLang()
1213{
456ae26d 1214 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
ec37df57
VZ
1215
1216 static const wxChar *langStrings[] =
1217 {
1218 NULL, // system default
1219 _T("C"),
1220 _T("fr"),
1221 _T("fr_FR"),
1222 _T("en"),
1223 _T("en_GB"),
1224 _T("en_US"),
1225 _T("de_DE.iso88591"),
1226 _T("german"),
1227 _T("?"), // invalid lang spec
1228 _T("klingonese"), // I bet on some systems it does exist...
1229 };
1230
dccce9ea
VZ
1231 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1232 wxLocale::GetSystemEncodingName().c_str(),
1233 wxLocale::GetSystemEncoding());
1234
ec37df57
VZ
1235 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1236 {
456ae26d 1237 const wxChar *langStr = langStrings[n];
ec37df57 1238 if ( langStr )
dccce9ea
VZ
1239 {
1240 // FIXME: this doesn't do anything at all under Windows, we need
1241 // to create a new wxLocale!
ec37df57 1242 wxSetEnv(_T("LC_ALL"), langStr);
dccce9ea 1243 }
ec37df57
VZ
1244
1245 int lang = gs_localeDefault.GetSystemLanguage();
456ae26d
VZ
1246 wxPrintf(_T("Locale for '%s' is %s.\n"),
1247 langStr ? langStr : _T("system default"), GetLangName(lang));
ec37df57
VZ
1248 }
1249}
1250
1251#endif // TEST_LOCALE
1252
696e1ea0
VZ
1253// ----------------------------------------------------------------------------
1254// MIME types
1255// ----------------------------------------------------------------------------
1256
1257#ifdef TEST_MIME
1258
e84010cf 1259#include "wx/mimetype.h"
696e1ea0
VZ
1260
1261static void TestMimeEnum()
1262{
a6c65e88
VZ
1263 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1264
696e1ea0
VZ
1265 wxArrayString mimetypes;
1266
39189b9d 1267 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
696e1ea0 1268
456ae26d 1269 wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
696e1ea0
VZ
1270
1271 wxArrayString exts;
1272 wxString desc;
1273
1274 for ( size_t n = 0; n < count; n++ )
1275 {
39189b9d
VZ
1276 wxFileType *filetype =
1277 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
696e1ea0 1278 if ( !filetype )
c61f4f6d 1279 {
456ae26d 1280 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
97e0ceea 1281 mimetypes[n].c_str());
696e1ea0 1282 continue;
c61f4f6d
VZ
1283 }
1284
696e1ea0
VZ
1285 filetype->GetDescription(&desc);
1286 filetype->GetExtensions(exts);
1287
299fcbfe
VZ
1288 filetype->GetIcon(NULL);
1289
696e1ea0
VZ
1290 wxString extsAll;
1291 for ( size_t e = 0; e < exts.GetCount(); e++ )
1292 {
1293 if ( e > 0 )
1294 extsAll << _T(", ");
1295 extsAll += exts[e];
1296 }
1297
456ae26d 1298 wxPrintf(_T("\t%s: %s (%s)\n"),
54acce90 1299 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
696e1ea0 1300 }
39189b9d 1301
e9d2bb6f 1302 wxPuts(wxEmptyString);
696e1ea0
VZ
1303}
1304
f6bcfd97
BP
1305static void TestMimeOverride()
1306{
1307 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1308
39189b9d
VZ
1309 static const wxChar *mailcap = _T("/tmp/mailcap");
1310 static const wxChar *mimetypes = _T("/tmp/mime.types");
1311
1312 if ( wxFile::Exists(mailcap) )
1313 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1314 mailcap,
1315 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
1316 else
1317 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1318 mailcap);
f6bcfd97 1319
39189b9d
VZ
1320 if ( wxFile::Exists(mimetypes) )
1321 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1322 mimetypes,
1323 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
1324 else
1325 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1326 mimetypes);
1327
e9d2bb6f 1328 wxPuts(wxEmptyString);
f6bcfd97
BP
1329}
1330
1331static void TestMimeFilename()
1332{
1333 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1334
1335 static const wxChar *filenames[] =
1336 {
1337 _T("readme.txt"),
1338 _T("document.pdf"),
1339 _T("image.gif"),
f06ef5f4 1340 _T("picture.jpeg"),
f6bcfd97
BP
1341 };
1342
1343 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1344 {
1345 const wxString fname = filenames[n];
1346 wxString ext = fname.AfterLast(_T('.'));
39189b9d 1347 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
f6bcfd97
BP
1348 if ( !ft )
1349 {
1350 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1351 }
1352 else
1353 {
1354 wxString desc;
1355 if ( !ft->GetDescription(&desc) )
1356 desc = _T("<no description>");
1357
1358 wxString cmd;
1359 if ( !ft->GetOpenCommand(&cmd,
e9d2bb6f 1360 wxFileType::MessageParameters(fname, wxEmptyString)) )
f6bcfd97 1361 cmd = _T("<no command available>");
7aeebdcd 1362 else
2b5f62a0 1363 cmd = wxString(_T('"')) + cmd + _T('"');
f6bcfd97 1364
7aeebdcd 1365 wxPrintf(_T("To open %s (%s) do %s.\n"),
f6bcfd97
BP
1366 fname.c_str(), desc.c_str(), cmd.c_str());
1367
1368 delete ft;
1369 }
1370 }
39189b9d 1371
e9d2bb6f 1372 wxPuts(wxEmptyString);
f6bcfd97
BP
1373}
1374
c7ce8392
VZ
1375static void TestMimeAssociate()
1376{
1377 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1378
a6c65e88
VZ
1379 wxFileTypeInfo ftInfo(
1380 _T("application/x-xyz"),
1381 _T("xyzview '%s'"), // open cmd
1382 _T(""), // print cmd
df0dc216
VZ
1383 _T("XYZ File"), // description
1384 _T(".xyz"), // extensions
1385 NULL // end of extensions
a6c65e88
VZ
1386 );
1387 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
1388
39189b9d 1389 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
c7ce8392
VZ
1390 if ( !ft )
1391 {
1392 wxPuts(_T("ERROR: failed to create association!"));
1393 }
1394 else
1395 {
a6c65e88 1396 // TODO: read it back
c7ce8392
VZ
1397 delete ft;
1398 }
39189b9d 1399
e9d2bb6f 1400 wxPuts(wxEmptyString);
c7ce8392
VZ
1401}
1402
696e1ea0
VZ
1403#endif // TEST_MIME
1404
af266e5b
VZ
1405// ----------------------------------------------------------------------------
1406// module dependencies feature
1407// ----------------------------------------------------------------------------
1408
1409#ifdef TEST_MODULE
1410
1411#include "wx/module.h"
1412
1413class wxTestModule : public wxModule
1414{
1415protected:
1416 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1417 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1418};
1419
1420class wxTestModuleA : public wxTestModule
1421{
1422public:
1423 wxTestModuleA();
1424private:
1425 DECLARE_DYNAMIC_CLASS(wxTestModuleA)
1426};
1427
1428class wxTestModuleB : public wxTestModule
1429{
1430public:
1431 wxTestModuleB();
1432private:
1433 DECLARE_DYNAMIC_CLASS(wxTestModuleB)
1434};
1435
1436class wxTestModuleC : public wxTestModule
1437{
1438public:
1439 wxTestModuleC();
1440private:
1441 DECLARE_DYNAMIC_CLASS(wxTestModuleC)
1442};
1443
1444class wxTestModuleD : public wxTestModule
1445{
1446public:
1447 wxTestModuleD();
1448private:
1449 DECLARE_DYNAMIC_CLASS(wxTestModuleD)
1450};
1451
1452IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC, wxModule)
1453wxTestModuleC::wxTestModuleC()
1454{
1455 AddDependency(CLASSINFO(wxTestModuleD));
1456}
1457
1458IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA, wxModule)
1459wxTestModuleA::wxTestModuleA()
1460{
1461 AddDependency(CLASSINFO(wxTestModuleB));
1462 AddDependency(CLASSINFO(wxTestModuleD));
1463}
1464
1465IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD, wxModule)
1466wxTestModuleD::wxTestModuleD()
1467{
1468}
1469
1470IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB, wxModule)
1471wxTestModuleB::wxTestModuleB()
1472{
1473 AddDependency(CLASSINFO(wxTestModuleD));
1474 AddDependency(CLASSINFO(wxTestModuleC));
1475}
1476
1477#endif // TEST_MODULE
1478
89e60357
VZ
1479// ----------------------------------------------------------------------------
1480// misc information functions
1481// ----------------------------------------------------------------------------
1482
1483#ifdef TEST_INFO_FUNCTIONS
1484
e84010cf 1485#include "wx/utils.h"
89e60357 1486
0219fd2f 1487#if TEST_INTERACTIVE
3a994742
VZ
1488static void TestDiskInfo()
1489{
456ae26d 1490 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
3a994742
VZ
1491
1492 for ( ;; )
1493 {
456ae26d
VZ
1494 wxChar pathname[128];
1495 wxPrintf(_T("\nEnter a directory name: "));
1496 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
3a994742
VZ
1497 break;
1498
1499 // kill the last '\n'
456ae26d 1500 pathname[wxStrlen(pathname) - 1] = 0;
3a994742
VZ
1501
1502 wxLongLong total, free;
1503 if ( !wxGetDiskSpace(pathname, &total, &free) )
1504 {
1505 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1506 }
1507 else
1508 {
eadd7bd2
VZ
1509 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1510 (total / 1024).ToString().c_str(),
1511 (free / 1024).ToString().c_str(),
3a994742
VZ
1512 pathname);
1513 }
1514 }
1515}
0219fd2f 1516#endif // TEST_INTERACTIVE
3a994742 1517
89e60357
VZ
1518static void TestOsInfo()
1519{
456ae26d 1520 wxPuts(_T("*** Testing OS info functions ***\n"));
89e60357
VZ
1521
1522 int major, minor;
1523 wxGetOsVersion(&major, &minor);
456ae26d 1524 wxPrintf(_T("Running under: %s, version %d.%d\n"),
89e60357
VZ
1525 wxGetOsDescription().c_str(), major, minor);
1526
10d878a9 1527 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
89e60357 1528
456ae26d 1529 wxPrintf(_T("Host name is %s (%s).\n"),
89e60357 1530 wxGetHostName().c_str(), wxGetFullHostName().c_str());
bd3277fe 1531
e9d2bb6f 1532 wxPuts(wxEmptyString);
89e60357
VZ
1533}
1534
8bb6b2c0
VZ
1535static void TestPlatformInfo()
1536{
1537 wxPuts(_T("*** Testing wxPlatformInfo functions ***\n"));
1538
1539 // get this platform
1540 wxPlatformInfo plat;
1541
1542 wxPrintf(_T("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str());
1543 wxPrintf(_T("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str());
1544 wxPrintf(_T("Port ID name is: %s\n"), plat.GetPortIdName().c_str());
1545 wxPrintf(_T("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str());
1546 wxPrintf(_T("Architecture is: %s\n"), plat.GetArchName().c_str());
1547 wxPrintf(_T("Endianness is: %s\n"), plat.GetEndiannessName().c_str());
1548
1549 wxPuts(wxEmptyString);
1550}
1551
89e60357
VZ
1552static void TestUserInfo()
1553{
456ae26d 1554 wxPuts(_T("*** Testing user info functions ***\n"));
89e60357 1555
456ae26d
VZ
1556 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1557 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1558 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1559 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
bd3277fe 1560
e9d2bb6f 1561 wxPuts(wxEmptyString);
89e60357
VZ
1562}
1563
1564#endif // TEST_INFO_FUNCTIONS
1565
39189b9d
VZ
1566// ----------------------------------------------------------------------------
1567// path list
1568// ----------------------------------------------------------------------------
1569
1570#ifdef TEST_PATHLIST
1571
ee3ef281
VZ
1572#ifdef __UNIX__
1573 #define CMD_IN_PATH _T("ls")
1574#else
1575 #define CMD_IN_PATH _T("command.com")
1576#endif
1577
39189b9d
VZ
1578static void TestPathList()
1579{
456ae26d 1580 wxPuts(_T("*** Testing wxPathList ***\n"));
39189b9d
VZ
1581
1582 wxPathList pathlist;
ee3ef281
VZ
1583 pathlist.AddEnvList(_T("PATH"));
1584 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
39189b9d
VZ
1585 if ( path.empty() )
1586 {
456ae26d 1587 wxPrintf(_T("ERROR: command not found in the path.\n"));
39189b9d
VZ
1588 }
1589 else
1590 {
456ae26d 1591 wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
39189b9d
VZ
1592 }
1593}
1594
1595#endif // TEST_PATHLIST
1596
07a56e45
VZ
1597// ----------------------------------------------------------------------------
1598// regular expressions
1599// ----------------------------------------------------------------------------
1600
1601#ifdef TEST_REGEX
1602
e84010cf 1603#include "wx/regex.h"
07a56e45 1604
07a56e45
VZ
1605static void TestRegExInteractive()
1606{
1607 wxPuts(_T("*** Testing RE interactively ***"));
1608
1609 for ( ;; )
1610 {
456ae26d
VZ
1611 wxChar pattern[128];
1612 wxPrintf(_T("\nEnter a pattern: "));
1613 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
07a56e45
VZ
1614 break;
1615
1616 // kill the last '\n'
456ae26d 1617 pattern[wxStrlen(pattern) - 1] = 0;
07a56e45
VZ
1618
1619 wxRegEx re;
1620 if ( !re.Compile(pattern) )
1621 {
1622 continue;
1623 }
1624
456ae26d 1625 wxChar text[128];
07a56e45
VZ
1626 for ( ;; )
1627 {
456ae26d
VZ
1628 wxPrintf(_T("Enter text to match: "));
1629 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
07a56e45
VZ
1630 break;
1631
1632 // kill the last '\n'
456ae26d 1633 text[wxStrlen(text) - 1] = 0;
07a56e45
VZ
1634
1635 if ( !re.Matches(text) )
1636 {
456ae26d 1637 wxPrintf(_T("No match.\n"));
07a56e45
VZ
1638 }
1639 else
1640 {
456ae26d 1641 wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
07a56e45
VZ
1642
1643 size_t start, len;
1644 for ( size_t n = 1; ; n++ )
1645 {
1646 if ( !re.GetMatch(&start, &len, n) )
1647 {
1648 break;
1649 }
1650
456ae26d
VZ
1651 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1652 n, wxString(text + start, len).c_str());
07a56e45
VZ
1653 }
1654 }
1655 }
1656 }
1657}
1658
1659#endif // TEST_REGEX
1660
8d5eff60
VZ
1661// ----------------------------------------------------------------------------
1662// database
1663// ----------------------------------------------------------------------------
1664
ba6ea19e
VZ
1665#if !wxUSE_ODBC
1666 #undef TEST_ODBC
1667#endif
1668
8d5eff60
VZ
1669#ifdef TEST_ODBC
1670
b20edf8b 1671#include "wx/db.h"
8d5eff60
VZ
1672
1673static void TestDbOpen()
1674{
1675 HENV henv;
1676 wxDb db(henv);
1677}
1678
1679#endif // TEST_ODBC
1680
7aeebdcd
VZ
1681// ----------------------------------------------------------------------------
1682// printf() tests
1683// ----------------------------------------------------------------------------
1684
1685/*
1686 NB: this stuff was taken from the glibc test suite and modified to build
be5a51fb 1687 in wxWidgets: if I read the copyright below properly, this shouldn't
7aeebdcd
VZ
1688 be a problem
1689 */
1690
1691#ifdef TEST_PRINTF
1692
1693#ifdef wxTEST_PRINTF
1694 // use our functions from wxchar.cpp
1695 #undef wxPrintf
1696 #undef wxSprintf
1697
1698 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1699 // in the tests below
1700 int wxPrintf( const wxChar *format, ... );
1701 int wxSprintf( wxChar *str, const wxChar *format, ... );
1702#endif
1703
f1389d46
VZ
1704#include "wx/longlong.h"
1705
7aeebdcd
VZ
1706#include <float.h>
1707
1708static void rfg1 (void);
1709static void rfg2 (void);
1710
1711
1712static void
1713fmtchk (const wxChar *fmt)
1714{
1715 (void) wxPrintf(_T("%s:\t`"), fmt);
1716 (void) wxPrintf(fmt, 0x12);
1717 (void) wxPrintf(_T("'\n"));
1718}
1719
1720static void
1721fmtst1chk (const wxChar *fmt)
1722{
1723 (void) wxPrintf(_T("%s:\t`"), fmt);
1724 (void) wxPrintf(fmt, 4, 0x12);
1725 (void) wxPrintf(_T("'\n"));
1726}
1727
1728static void
1729fmtst2chk (const wxChar *fmt)
1730{
1731 (void) wxPrintf(_T("%s:\t`"), fmt);
1732 (void) wxPrintf(fmt, 4, 4, 0x12);
1733 (void) wxPrintf(_T("'\n"));
1734}
1735
1736/* This page is covered by the following copyright: */
1737
1738/* (C) Copyright C E Chew
1739 *
1740 * Feel free to copy, use and distribute this software provided:
1741 *
1742 * 1. you do not pretend that you wrote it
1743 * 2. you leave this copyright notice intact.
1744 */
1745
1746/*
1747 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1748 */
1749
1750#define DEC -123
1751#define INT 255
1752#define UNS (~0)
1753
1754/* Formatted Output Test
1755 *
1756 * This exercises the output formatting code.
1757 */
1758
e9d2bb6f
DS
1759wxChar *PointerNull = NULL;
1760
7aeebdcd
VZ
1761static void
1762fp_test (void)
1763{
1764 int i, j, k, l;
1765 wxChar buf[7];
1766 wxChar *prefix = buf;
1767 wxChar tp[20];
1768
1769 wxPuts(_T("\nFormatted output test"));
1770 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1771 wxStrcpy(prefix, _T("%"));
1772 for (i = 0; i < 2; i++) {
1773 for (j = 0; j < 2; j++) {
1774 for (k = 0; k < 2; k++) {
1775 for (l = 0; l < 2; l++) {
1776 wxStrcpy(prefix, _T("%"));
1777 if (i == 0) wxStrcat(prefix, _T("-"));
1778 if (j == 0) wxStrcat(prefix, _T("+"));
1779 if (k == 0) wxStrcat(prefix, _T("#"));
1780 if (l == 0) wxStrcat(prefix, _T("0"));
1781 wxPrintf(_T("%5s |"), prefix);
1782 wxStrcpy(tp, prefix);
1783 wxStrcat(tp, _T("6d |"));
1784 wxPrintf(tp, DEC);
1785 wxStrcpy(tp, prefix);
1786 wxStrcat(tp, _T("6o |"));
1787 wxPrintf(tp, INT);
1788 wxStrcpy(tp, prefix);
1789 wxStrcat(tp, _T("6x |"));
1790 wxPrintf(tp, INT);
1791 wxStrcpy(tp, prefix);
1792 wxStrcat(tp, _T("6X |"));
1793 wxPrintf(tp, INT);
1794 wxStrcpy(tp, prefix);
1795 wxStrcat(tp, _T("6u |"));
1796 wxPrintf(tp, UNS);
1797 wxPrintf(_T("\n"));
1798 }
1799 }
1800 }
1801 }
e9d2bb6f
DS
1802 wxPrintf(_T("%10s\n"), PointerNull);
1803 wxPrintf(_T("%-10s\n"), PointerNull);
7aeebdcd
VZ
1804}
1805
1806static void TestPrintf()
1807{
1808 static wxChar shortstr[] = _T("Hi, Z.");
f1389d46
VZ
1809 static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
1810I am ready for my first lesson today.");
7aeebdcd 1811 int result = 0;
e9d2bb6f 1812 wxString test_format;
7aeebdcd
VZ
1813
1814 fmtchk(_T("%.4x"));
1815 fmtchk(_T("%04x"));
1816 fmtchk(_T("%4.4x"));
1817 fmtchk(_T("%04.4x"));
1818 fmtchk(_T("%4.3x"));
1819 fmtchk(_T("%04.3x"));
1820
1821 fmtst1chk(_T("%.*x"));
1822 fmtst1chk(_T("%0*x"));
1823 fmtst2chk(_T("%*.*x"));
1824 fmtst2chk(_T("%0*.*x"));
1825
e9d2bb6f
DS
1826 wxString bad_format = _T("bad format:\t\"%b\"\n");
1827 wxPrintf(bad_format.c_str());
7aeebdcd
VZ
1828 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
1829
1830 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1831 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1832 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1833 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1834 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1835 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1836 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
e9d2bb6f
DS
1837 test_format = _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1838 wxPrintf(test_format.c_str(), -123456);
7aeebdcd
VZ
1839 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1840 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1841
e9d2bb6f
DS
1842 test_format = _T("zero-padded string:\t\"%010s\"\n");
1843 wxPrintf(test_format.c_str(), shortstr);
1844 test_format = _T("left-adjusted Z string:\t\"%-010s\"\n");
1845 wxPrintf(test_format.c_str(), shortstr);
7aeebdcd
VZ
1846 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
1847 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
e9d2bb6f 1848 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull);
7aeebdcd
VZ
1849 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
1850
1851 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1852 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1853 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1854 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
1855 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1856 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1857 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1858 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1859 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1860 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1861 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1862 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
1863
1864 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1865 wxPrintf (_T(" %6.5f\n"), .1);
1866 wxPrintf (_T("x%5.4fx\n"), .5);
1867
1868 wxPrintf (_T("%#03x\n"), 1);
1869
1870 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1871
1872 {
1873 double d = FLT_MIN;
1874 int niter = 17;
1875
1876 while (niter-- != 0)
1877 wxPrintf (_T("%.17e\n"), d / 2);
1878 fflush (stdout);
1879 }
1880
e9d2bb6f
DS
1881#ifndef __WATCOMC__
1882 // Open Watcom cause compiler error here
1883 // Error! E173: col(24) floating-point constant too small to represent
7aeebdcd 1884 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
e9d2bb6f 1885#endif
7aeebdcd
VZ
1886
1887#define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1888 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
1889 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
1890 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
1891 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
1892 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
1893 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
1894 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
1895 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
1896 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
1897#undef FORMAT
1898
1899 {
1900 wxChar buf[20];
1901 int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
1902
1903 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1904 rc, WXSIZEOF(buf), buf);
1905#if 0
1906 wxChar buf2[512];
1907 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1908 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
1909#endif
1910 }
1911
1912 fp_test ();
1913
1914 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1915 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1916 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1917 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1918 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1919 wxPrintf (_T("%g should be 10\n"), 10.0);
1920 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1921
1922 {
1923 double x=1.0;
1924 wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
1925 }
1926
1927 {
1928 wxChar buf[200];
1929
1930 wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1931
1932 result |= wxStrcmp (buf,
1933 _T("onetwo three "));
1934
1935 wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
1936 }
1937
f1389d46 1938#ifdef wxLongLong_t
7aeebdcd 1939 {
f1389d46 1940 wxChar buf[200];
7aeebdcd 1941
2b5f62a0 1942 wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
f2cb8a17
JS
1943 #if 0
1944 // for some reason below line fails under Borland
f1389d46 1945 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
f2cb8a17 1946 #endif
7aeebdcd 1947
f1389d46 1948 if (wxStrcmp (buf, _T("40000000000")) != 0)
7aeebdcd 1949 {
f1389d46
VZ
1950 result = 1;
1951 wxPuts (_T("\tFAILED"));
7aeebdcd 1952 }
e9d2bb6f
DS
1953 wxUnusedVar(result);
1954 wxPuts (wxEmptyString);
7aeebdcd 1955 }
f1389d46 1956#endif // wxLongLong_t
7aeebdcd
VZ
1957
1958 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
1959 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
1960
1961 wxPuts (_T("--- Should be no further output. ---"));
1962 rfg1 ();
1963 rfg2 ();
1964
1965#if 0
1966 {
1967 wxChar bytes[7];
1968 wxChar buf[20];
1969
1970 memset (bytes, '\xff', sizeof bytes);
1971 wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
1972 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
1973 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
1974 {
1975 wxPuts (_T("%hhn overwrite more bytes"));
1976 result = 1;
1977 }
1978 if (bytes[3] != 3)
1979 {
1980 wxPuts (_T("%hhn wrote incorrect value"));
1981 result = 1;
1982 }
1983 }
1984#endif
1985}
1986
1987static void
1988rfg1 (void)
1989{
1990 wxChar buf[100];
1991
1992 wxSprintf (buf, _T("%5.s"), _T("xyz"));
1993 if (wxStrcmp (buf, _T(" ")) != 0)
1994 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
1995 wxSprintf (buf, _T("%5.f"), 33.3);
1996 if (wxStrcmp (buf, _T(" 33")) != 0)
1997 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
1998 wxSprintf (buf, _T("%8.e"), 33.3e7);
1999 if (wxStrcmp (buf, _T(" 3e+08")) != 0)
2000 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
2001 wxSprintf (buf, _T("%8.E"), 33.3e7);
2002 if (wxStrcmp (buf, _T(" 3E+08")) != 0)
2003 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
2004 wxSprintf (buf, _T("%.g"), 33.3);
2005 if (wxStrcmp (buf, _T("3e+01")) != 0)
2006 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
2007 wxSprintf (buf, _T("%.G"), 33.3);
2008 if (wxStrcmp (buf, _T("3E+01")) != 0)
2009 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
2010}
2011
2012static void
2013rfg2 (void)
2014{
2015 int prec;
2016 wxChar buf[100];
e9d2bb6f 2017 wxString test_format;
7aeebdcd
VZ
2018
2019 prec = 0;
2020 wxSprintf (buf, _T("%.*g"), prec, 3.3);
2021 if (wxStrcmp (buf, _T("3")) != 0)
2022 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2023 prec = 0;
2024 wxSprintf (buf, _T("%.*G"), prec, 3.3);
2025 if (wxStrcmp (buf, _T("3")) != 0)
2026 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2027 prec = 0;
2028 wxSprintf (buf, _T("%7.*G"), prec, 3.33);
2029 if (wxStrcmp (buf, _T(" 3")) != 0)
2030 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
2031 prec = 3;
e9d2bb6f
DS
2032 test_format = _T("%04.*o");
2033 wxSprintf (buf, test_format.c_str(), prec, 33);
7aeebdcd
VZ
2034 if (wxStrcmp (buf, _T(" 041")) != 0)
2035 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
2036 prec = 7;
e9d2bb6f
DS
2037 test_format = _T("%09.*u");
2038 wxSprintf (buf, test_format.c_str(), prec, 33);
7aeebdcd
VZ
2039 if (wxStrcmp (buf, _T(" 0000033")) != 0)
2040 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
2041 prec = 3;
e9d2bb6f
DS
2042 test_format = _T("%04.*x");
2043 wxSprintf (buf, test_format.c_str(), prec, 33);
7aeebdcd
VZ
2044 if (wxStrcmp (buf, _T(" 021")) != 0)
2045 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2046 prec = 3;
e9d2bb6f
DS
2047 test_format = _T("%04.*X");
2048 wxSprintf (buf, test_format.c_str(), prec, 33);
7aeebdcd
VZ
2049 if (wxStrcmp (buf, _T(" 021")) != 0)
2050 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2051}
2052
2053#endif // TEST_PRINTF
2054
6dfec4b8 2055// ----------------------------------------------------------------------------
7ba4fbeb 2056// registry and related stuff
6dfec4b8
VZ
2057// ----------------------------------------------------------------------------
2058
2059// this is for MSW only
2060#ifndef __WXMSW__
7ba4fbeb 2061 #undef TEST_REGCONF
6dfec4b8
VZ
2062 #undef TEST_REGISTRY
2063#endif
2064
7ba4fbeb
VZ
2065#ifdef TEST_REGCONF
2066
e84010cf
GD
2067#include "wx/confbase.h"
2068#include "wx/msw/regconf.h"
7ba4fbeb 2069
e9d2bb6f 2070#if 0
7ba4fbeb
VZ
2071static void TestRegConfWrite()
2072{
e9d2bb6f
DS
2073 wxConfig *config = new wxConfig(_T("myapp"));
2074 config->SetPath(_T("/group1"));
2075 config->Write(_T("entry1"), _T("foo"));
2076 config->SetPath(_T("/group2"));
2077 config->Write(_T("entry1"), _T("bar"));
0aa29b6b 2078}
e9d2bb6f 2079#endif
0aa29b6b
VZ
2080
2081static void TestRegConfRead()
2082{
e9d2bb6f 2083 wxConfig *config = new wxConfig(_T("myapp"));
0aa29b6b
VZ
2084
2085 wxString str;
2086 long dummy;
e9d2bb6f
DS
2087 config->SetPath(_T("/"));
2088 wxPuts(_T("Enumerating / subgroups:"));
0aa29b6b
VZ
2089 bool bCont = config->GetFirstGroup(str, dummy);
2090 while(bCont)
2091 {
e9d2bb6f 2092 wxPuts(str);
0aa29b6b
VZ
2093 bCont = config->GetNextGroup(str, dummy);
2094 }
7ba4fbeb
VZ
2095}
2096
2097#endif // TEST_REGCONF
2098
6dfec4b8
VZ
2099#ifdef TEST_REGISTRY
2100
e84010cf 2101#include "wx/msw/registry.h"
6dfec4b8
VZ
2102
2103// I chose this one because I liked its name, but it probably only exists under
2104// NT
2105static const wxChar *TESTKEY =
2106 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2107
2108static void TestRegistryRead()
2109{
456ae26d 2110 wxPuts(_T("*** testing registry reading ***"));
6dfec4b8
VZ
2111
2112 wxRegKey key(TESTKEY);
456ae26d 2113 wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
6dfec4b8
VZ
2114 if ( !key.Open() )
2115 {
456ae26d 2116 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
6dfec4b8
VZ
2117
2118 return;
2119 }
2120
2121 size_t nSubKeys, nValues;
2122 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2123 {
456ae26d 2124 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
6dfec4b8
VZ
2125 }
2126
456ae26d 2127 wxPrintf(_T("Enumerating values:\n"));
6dfec4b8
VZ
2128
2129 long dummy;
2130 wxString value;
2131 bool cont = key.GetFirstValue(value, dummy);
2132 while ( cont )
2133 {
456ae26d 2134 wxPrintf(_T("Value '%s': type "), value.c_str());
6dfec4b8
VZ
2135 switch ( key.GetValueType(value) )
2136 {
456ae26d
VZ
2137 case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
2138 case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
2139 case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
2140 case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
2141 case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
2142 case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
2143 default: wxPrintf(_T("other (unknown)")); break;
6dfec4b8
VZ
2144 }
2145
456ae26d 2146 wxPrintf(_T(", value = "));
6dfec4b8
VZ
2147 if ( key.IsNumericValue(value) )
2148 {
2149 long val;
2150 key.QueryValue(value, &val);
456ae26d 2151 wxPrintf(_T("%ld"), val);
6dfec4b8
VZ
2152 }
2153 else // string
2154 {
2155 wxString val;
2156 key.QueryValue(value, val);
456ae26d 2157 wxPrintf(_T("'%s'"), val.c_str());
6dfec4b8
VZ
2158
2159 key.QueryRawValue(value, val);
456ae26d 2160 wxPrintf(_T(" (raw value '%s')"), val.c_str());
6dfec4b8
VZ
2161 }
2162
e9d2bb6f 2163 wxPutchar('\n');
6dfec4b8
VZ
2164
2165 cont = key.GetNextValue(value, dummy);
2166 }
2167}
2168
6ba63600
VZ
2169static void TestRegistryAssociation()
2170{
2171 /*
2172 The second call to deleteself genertaes an error message, with a
2173 messagebox saying .flo is crucial to system operation, while the .ddf
2174 call also fails, but with no error message
2175 */
2176
2177 wxRegKey key;
2178
f2cb8a17 2179 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
6ba63600 2180 key.Create();
f2cb8a17
JS
2181 key = _T("ddxf_auto_file") ;
2182 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
6ba63600 2183 key.Create();
f2cb8a17
JS
2184 key = _T("ddxf_auto_file") ;
2185 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
6ba63600 2186 key.Create();
f2cb8a17
JS
2187 key = _T("program,0") ;
2188 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
6ba63600 2189 key.Create();
f2cb8a17 2190 key = _T("program \"%1\"") ;
6ba63600 2191
f2cb8a17 2192 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
6ba63600 2193 key.DeleteSelf();
f2cb8a17 2194 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
6ba63600 2195 key.DeleteSelf();
f2cb8a17 2196 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
6ba63600 2197 key.DeleteSelf();
f2cb8a17 2198 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
6ba63600
VZ
2199 key.DeleteSelf();
2200}
2201
6dfec4b8
VZ
2202#endif // TEST_REGISTRY
2203
c66cca2a
VZ
2204// ----------------------------------------------------------------------------
2205// scope guard
2206// ----------------------------------------------------------------------------
2207
df5168c4
MB
2208#ifdef TEST_SCOPEGUARD
2209
c66cca2a
VZ
2210#include "wx/scopeguard.h"
2211
2212static void function0() { puts("function0()"); }
2213static void function1(int n) { printf("function1(%d)\n", n); }
2214static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
2215
2216struct Object
2217{
2218 void method0() { printf("method0()\n"); }
2219 void method1(int n) { printf("method1(%d)\n", n); }
2220 void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
2221};
2222
2223static void TestScopeGuard()
2224{
24c8053b
RN
2225 wxON_BLOCK_EXIT0(function0);
2226 wxON_BLOCK_EXIT1(function1, 17);
2227 wxON_BLOCK_EXIT2(function2, 3.14, 'p');
c66cca2a
VZ
2228
2229 Object obj;
eb42e596
VZ
2230 wxON_BLOCK_EXIT_OBJ0(obj, Object::method0);
2231 wxON_BLOCK_EXIT_OBJ1(obj, Object::method1, 7);
2232 wxON_BLOCK_EXIT_OBJ2(obj, Object::method2, 2.71, 'e');
c66cca2a
VZ
2233
2234 wxScopeGuard dismissed = wxMakeGuard(function0);
2235 dismissed.Dismiss();
2236}
2237
df5168c4
MB
2238#endif
2239
2c8e4738
VZ
2240// ----------------------------------------------------------------------------
2241// sockets
2242// ----------------------------------------------------------------------------
2243
2244#ifdef TEST_SOCKETS
2245
e84010cf
GD
2246#include "wx/socket.h"
2247#include "wx/protocol/protocol.h"
2248#include "wx/protocol/http.h"
8e907a13
VZ
2249
2250static void TestSocketServer()
2251{
456ae26d 2252 wxPuts(_T("*** Testing wxSocketServer ***\n"));
8e907a13 2253
ccdb23df
VZ
2254 static const int PORT = 3000;
2255
8e907a13 2256 wxIPV4address addr;
ccdb23df 2257 addr.Service(PORT);
8e907a13
VZ
2258
2259 wxSocketServer *server = new wxSocketServer(addr);
2260 if ( !server->Ok() )
2261 {
456ae26d 2262 wxPuts(_T("ERROR: failed to bind"));
ccdb23df
VZ
2263
2264 return;
8e907a13 2265 }
8dfea369 2266
cab8f76e
VZ
2267 bool quit = false;
2268 while ( !quit )
8dfea369 2269 {
456ae26d 2270 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
8dfea369
VZ
2271
2272 wxSocketBase *socket = server->Accept();
2273 if ( !socket )
2274 {
456ae26d 2275 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
8dfea369
VZ
2276 break;
2277 }
2278
456ae26d 2279 wxPuts(_T("Server: got a client."));
8dfea369 2280
ccdb23df
VZ
2281 server->SetTimeout(60); // 1 min
2282
cab8f76e
VZ
2283 bool close = false;
2284 while ( !close && socket->IsConnected() )
8dfea369 2285 {
ccdb23df 2286 wxString s;
456ae26d 2287 wxChar ch = _T('\0');
ccdb23df 2288 for ( ;; )
8dfea369 2289 {
ccdb23df
VZ
2290 if ( socket->Read(&ch, sizeof(ch)).Error() )
2291 {
2292 // don't log error if the client just close the connection
2293 if ( socket->IsConnected() )
2294 {
456ae26d 2295 wxPuts(_T("ERROR: in wxSocket::Read."));
ccdb23df 2296 }
8dfea369 2297
ccdb23df
VZ
2298 break;
2299 }
8dfea369 2300
ccdb23df
VZ
2301 if ( ch == '\r' )
2302 continue;
8dfea369 2303
ccdb23df
VZ
2304 if ( ch == '\n' )
2305 break;
8dfea369 2306
ccdb23df
VZ
2307 s += ch;
2308 }
8dfea369 2309
ccdb23df
VZ
2310 if ( ch != '\n' )
2311 {
2312 break;
2313 }
8dfea369 2314
456ae26d 2315 wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
cab8f76e 2316 if ( s == _T("close") )
ccdb23df 2317 {
cab8f76e 2318 wxPuts(_T("Closing connection"));
8dfea369 2319
cab8f76e 2320 close = true;
ccdb23df 2321 }
cab8f76e
VZ
2322 else if ( s == _T("quit") )
2323 {
2324 close =
2325 quit = true;
ccdb23df 2326
cab8f76e
VZ
2327 wxPuts(_T("Shutting down the server"));
2328 }
2329 else // not a special command
2330 {
2331 socket->Write(s.MakeUpper().c_str(), s.length());
2332 socket->Write("\r\n", 2);
2333 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
2334 }
8dfea369
VZ
2335 }
2336
cab8f76e
VZ
2337 if ( !close )
2338 {
2339 wxPuts(_T("Server: lost a client unexpectedly."));
2340 }
8dfea369 2341
ccdb23df 2342 socket->Destroy();
8dfea369 2343 }
9fc3cba7 2344
ccdb23df
VZ
2345 // same as "delete server" but is consistent with GUI programs
2346 server->Destroy();
8e907a13 2347}
2c8e4738
VZ
2348
2349static void TestSocketClient()
2350{
456ae26d 2351 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2c8e4738 2352
be5a51fb 2353 static const wxChar *hostname = _T("www.wxwidgets.org");
8e907a13
VZ
2354
2355 wxIPV4address addr;
2356 addr.Hostname(hostname);
2357 addr.Service(80);
2358
456ae26d 2359 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
2c8e4738
VZ
2360
2361 wxSocketClient client;
8e907a13 2362 if ( !client.Connect(addr) )
2c8e4738 2363 {
456ae26d 2364 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2c8e4738
VZ
2365 }
2366 else
2367 {
456ae26d 2368 wxPrintf(_T("--- Connected to %s:%u...\n"),
8e907a13
VZ
2369 addr.Hostname().c_str(), addr.Service());
2370
456ae26d 2371 wxChar buf[8192];
2c8e4738 2372
8e907a13
VZ
2373 // could use simply "GET" here I suppose
2374 wxString cmdGet =
456ae26d 2375 wxString::Format(_T("GET http://%s/\r\n"), hostname);
8e907a13 2376 client.Write(cmdGet, cmdGet.length());
456ae26d 2377 wxPrintf(_T("--- Sent command '%s' to the server\n"),
8e907a13 2378 MakePrintable(cmdGet).c_str());
2c8e4738 2379 client.Read(buf, WXSIZEOF(buf));
456ae26d 2380 wxPrintf(_T("--- Server replied:\n%s"), buf);
8e907a13
VZ
2381 }
2382}
2383
2e907fab
VZ
2384#endif // TEST_SOCKETS
2385
b92fd37c
VZ
2386// ----------------------------------------------------------------------------
2387// FTP
2388// ----------------------------------------------------------------------------
2389
2e907fab
VZ
2390#ifdef TEST_FTP
2391
e84010cf 2392#include "wx/protocol/ftp.h"
2e907fab 2393
b92fd37c
VZ
2394static wxFTP ftp;
2395
2396#define FTP_ANONYMOUS
2397
2398#ifdef FTP_ANONYMOUS
456ae26d
VZ
2399 static const wxChar *directory = _T("/pub");
2400 static const wxChar *filename = _T("welcome.msg");
b92fd37c 2401#else
456ae26d
VZ
2402 static const wxChar *directory = _T("/etc");
2403 static const wxChar *filename = _T("issue");
b92fd37c
VZ
2404#endif
2405
2406static bool TestFtpConnect()
8e907a13 2407{
456ae26d 2408 wxPuts(_T("*** Testing FTP connect ***"));
8e907a13 2409
b92fd37c 2410#ifdef FTP_ANONYMOUS
be5a51fb 2411 static const wxChar *hostname = _T("ftp.wxwidgets.org");
b92fd37c 2412
456ae26d 2413 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
b92fd37c 2414#else // !FTP_ANONYMOUS
456ae26d 2415 static const wxChar *hostname = "localhost";
b92fd37c 2416
456ae26d
VZ
2417 wxChar user[256];
2418 wxFgets(user, WXSIZEOF(user), stdin);
2419 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
b92fd37c
VZ
2420 ftp.SetUser(user);
2421
456ae26d
VZ
2422 wxChar password[256];
2423 wxPrintf(_T("Password for %s: "), password);
2424 wxFgets(password, WXSIZEOF(password), stdin);
2425 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
b92fd37c
VZ
2426 ftp.SetPassword(password);
2427
456ae26d 2428 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
b92fd37c
VZ
2429#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2430
2431 if ( !ftp.Connect(hostname) )
2432 {
456ae26d 2433 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
b92fd37c 2434
cab8f76e 2435 return false;
b92fd37c
VZ
2436 }
2437 else
2438 {
456ae26d 2439 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
af33b199
VZ
2440 hostname, ftp.Pwd().c_str());
2441 ftp.Close();
b92fd37c
VZ
2442 }
2443
cab8f76e 2444 return true;
b92fd37c 2445}
b1229561 2446
b92fd37c
VZ
2447// test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2448static void TestFtpWuFtpd()
2449{
2450 wxFTP ftp;
456ae26d 2451 static const wxChar *hostname = _T("ftp.eudora.com");
b1229561
VZ
2452 if ( !ftp.Connect(hostname) )
2453 {
456ae26d 2454 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
b1229561
VZ
2455 }
2456 else
2457 {
456ae26d 2458 static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
b1229561
VZ
2459 wxInputStream *in = ftp.GetInputStream(filename);
2460 if ( !in )
2461 {
456ae26d 2462 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
b1229561
VZ
2463 }
2464 else
2465 {
4c51b688 2466 size_t size = in->GetSize();
456ae26d 2467 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
b1229561 2468
456ae26d 2469 wxChar *data = new wxChar[size];
b1229561
VZ
2470 if ( !in->Read(data, size) )
2471 {
456ae26d 2472 wxPuts(_T("ERROR: read error"));
b1229561
VZ
2473 }
2474 else
2475 {
456ae26d 2476 wxPrintf(_T("Successfully retrieved the file.\n"));
b1229561
VZ
2477 }
2478
2479 delete [] data;
2480 delete in;
2481 }
2482 }
b92fd37c 2483}
b1229561 2484
b92fd37c
VZ
2485static void TestFtpList()
2486{
456ae26d 2487 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
8e907a13 2488
b92fd37c
VZ
2489 // test CWD
2490 if ( !ftp.ChDir(directory) )
2491 {
456ae26d 2492 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
b92fd37c 2493 }
2e907fab 2494
456ae26d 2495 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2e907fab 2496
b92fd37c
VZ
2497 // test NLIST and LIST
2498 wxArrayString files;
2499 if ( !ftp.GetFilesList(files) )
8e907a13 2500 {
456ae26d 2501 wxPuts(_T("ERROR: failed to get NLIST of files"));
8e907a13
VZ
2502 }
2503 else
2504 {
456ae26d 2505 wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
b92fd37c
VZ
2506 size_t count = files.GetCount();
2507 for ( size_t n = 0; n < count; n++ )
8e907a13 2508 {
456ae26d 2509 wxPrintf(_T("\t%s\n"), files[n].c_str());
8e907a13 2510 }
456ae26d 2511 wxPuts(_T("End of the file list"));
b92fd37c 2512 }
8e907a13 2513
b92fd37c
VZ
2514 if ( !ftp.GetDirList(files) )
2515 {
456ae26d 2516 wxPuts(_T("ERROR: failed to get LIST of files"));
b92fd37c
VZ
2517 }
2518 else
2519 {
456ae26d 2520 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
b92fd37c
VZ
2521 size_t count = files.GetCount();
2522 for ( size_t n = 0; n < count; n++ )
8e907a13 2523 {
456ae26d 2524 wxPrintf(_T("\t%s\n"), files[n].c_str());
2e907fab 2525 }
456ae26d 2526 wxPuts(_T("End of the file list"));
b92fd37c
VZ
2527 }
2528
2529 if ( !ftp.ChDir(_T("..")) )
2530 {
456ae26d 2531 wxPuts(_T("ERROR: failed to cd to .."));
b92fd37c 2532 }
2e907fab 2533
456ae26d 2534 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
b92fd37c
VZ
2535}
2536
2537static void TestFtpDownload()
2538{
456ae26d 2539 wxPuts(_T("*** Testing wxFTP download ***\n"));
b92fd37c
VZ
2540
2541 // test RETR
2542 wxInputStream *in = ftp.GetInputStream(filename);
2543 if ( !in )
2544 {
456ae26d 2545 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
b92fd37c
VZ
2546 }
2547 else
2548 {
4c51b688 2549 size_t size = in->GetSize();
456ae26d 2550 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
b92fd37c
VZ
2551 fflush(stdout);
2552
456ae26d 2553 wxChar *data = new wxChar[size];
b92fd37c 2554 if ( !in->Read(data, size) )
2e907fab 2555 {
456ae26d 2556 wxPuts(_T("ERROR: read error"));
2e907fab
VZ
2557 }
2558 else
2559 {
456ae26d 2560 wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
8e907a13
VZ
2561 }
2562
b92fd37c
VZ
2563 delete [] data;
2564 delete in;
2565 }
2566}
8e907a13 2567
b92fd37c
VZ
2568static void TestFtpFileSize()
2569{
456ae26d 2570 wxPuts(_T("*** Testing FTP SIZE command ***"));
b92fd37c
VZ
2571
2572 if ( !ftp.ChDir(directory) )
2573 {
456ae26d 2574 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
b92fd37c
VZ
2575 }
2576
456ae26d 2577 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
b92fd37c
VZ
2578
2579 if ( ftp.FileExists(filename) )
2580 {
2581 int size = ftp.GetFileSize(filename);
2582 if ( size == -1 )
456ae26d 2583 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
8e907a13 2584 else
456ae26d 2585 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
b92fd37c
VZ
2586 }
2587 else
2588 {
456ae26d 2589 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
b92fd37c
VZ
2590 }
2591}
2592
2593static void TestFtpMisc()
2594{
456ae26d 2595 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
b92fd37c 2596
f2cb8a17 2597 if ( ftp.SendCommand(_T("STAT")) != '2' )
b92fd37c 2598 {
456ae26d 2599 wxPuts(_T("ERROR: STAT failed"));
b92fd37c
VZ
2600 }
2601 else
2602 {
456ae26d 2603 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
b92fd37c
VZ
2604 }
2605
f2cb8a17 2606 if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
b92fd37c 2607 {
456ae26d 2608 wxPuts(_T("ERROR: HELP SITE failed"));
b92fd37c
VZ
2609 }
2610 else
2611 {
456ae26d 2612 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
b92fd37c
VZ
2613 ftp.GetLastResult().c_str());
2614 }
2615}
2616
2617static void TestFtpInteractive()
2618{
456ae26d 2619 wxPuts(_T("\n*** Interactive wxFTP test ***"));
b92fd37c 2620
456ae26d 2621 wxChar buf[128];
b92fd37c
VZ
2622
2623 for ( ;; )
2624 {
456ae26d
VZ
2625 wxPrintf(_T("Enter FTP command: "));
2626 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
b92fd37c
VZ
2627 break;
2628
2629 // kill the last '\n'
456ae26d 2630 buf[wxStrlen(buf) - 1] = 0;
b92fd37c
VZ
2631
2632 // special handling of LIST and NLST as they require data connection
2633 wxString start(buf, 4);
2634 start.MakeUpper();
f2cb8a17 2635 if ( start == _T("LIST") || start == _T("NLST") )
8e907a13 2636 {
b92fd37c 2637 wxString wildcard;
456ae26d 2638 if ( wxStrlen(buf) > 4 )
b92fd37c 2639 wildcard = buf + 5;
8e907a13 2640
b92fd37c 2641 wxArrayString files;
f2cb8a17 2642 if ( !ftp.GetList(files, wildcard, start == _T("LIST")) )
8e907a13 2643 {
456ae26d 2644 wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
8e907a13
VZ
2645 }
2646 else
2647 {
456ae26d 2648 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
b92fd37c
VZ
2649 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
2650 size_t count = files.GetCount();
2651 for ( size_t n = 0; n < count; n++ )
2652 {
456ae26d 2653 wxPrintf(_T("\t%s\n"), files[n].c_str());
b92fd37c 2654 }
456ae26d 2655 wxPuts(_T("--- End of the file list"));
8e907a13 2656 }
2e907fab 2657 }
b92fd37c 2658 else // !list
2e907fab 2659 {
456ae26d
VZ
2660 wxChar ch = ftp.SendCommand(buf);
2661 wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
b92fd37c
VZ
2662 if ( ch )
2663 {
456ae26d 2664 wxPrintf(_T(" (return code %c)"), ch);
b92fd37c 2665 }
2e907fab 2666
456ae26d 2667 wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
2e907fab 2668 }
2c8e4738 2669 }
b92fd37c 2670
456ae26d 2671 wxPuts(_T("\n*** done ***"));
2c8e4738
VZ
2672}
2673
b92fd37c 2674static void TestFtpUpload()
f6bcfd97 2675{
456ae26d 2676 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
f6bcfd97 2677
b92fd37c 2678 // upload a file
456ae26d
VZ
2679 static const wxChar *file1 = _T("test1");
2680 static const wxChar *file2 = _T("test2");
b92fd37c
VZ
2681 wxOutputStream *out = ftp.GetOutputStream(file1);
2682 if ( out )
2683 {
456ae26d 2684 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
b92fd37c
VZ
2685 out->Write("First hello", 11);
2686 delete out;
2687 }
f6bcfd97 2688
b92fd37c 2689 // send a command to check the remote file
f2cb8a17 2690 if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' )
f6bcfd97 2691 {
456ae26d 2692 wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
f6bcfd97
BP
2693 }
2694 else
2695 {
456ae26d 2696 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
b92fd37c
VZ
2697 file1, ftp.GetLastResult().c_str());
2698 }
2e907fab 2699
b92fd37c
VZ
2700 out = ftp.GetOutputStream(file2);
2701 if ( out )
2702 {
456ae26d 2703 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
b92fd37c
VZ
2704 out->Write("Second hello", 12);
2705 delete out;
f6bcfd97
BP
2706 }
2707}
2708
2e907fab 2709#endif // TEST_FTP
2c8e4738 2710
eaff0f0d
VZ
2711// ----------------------------------------------------------------------------
2712// stack backtrace
2713// ----------------------------------------------------------------------------
2714
2715#ifdef TEST_STACKWALKER
2716
74e2116a
MB
2717#if wxUSE_STACKWALKER
2718
eaff0f0d
VZ
2719#include "wx/stackwalk.h"
2720
2721class StackDump : public wxStackWalker
2722{
2723public:
2724 StackDump(const char *argv0)
2725 : wxStackWalker(argv0)
2726 {
2727 }
2728
5ba95b79 2729 virtual void Walk(size_t skip = 1)
eaff0f0d
VZ
2730 {
2731 wxPuts(_T("Stack dump:"));
2732
5ba95b79 2733 wxStackWalker::Walk(skip);
eaff0f0d
VZ
2734 }
2735
2736protected:
2737 virtual void OnStackFrame(const wxStackFrame& frame)
2738 {
2739 printf("[%2d] ", frame.GetLevel());
2740
2741 wxString name = frame.GetName();
2742 if ( !name.empty() )
2743 {
2744 printf("%-20.40s", name.mb_str());
2745 }
2746 else
2747 {
2748 printf("0x%08lx", (unsigned long)frame.GetAddress());
2749 }
2750
2751 if ( frame.HasSourceLocation() )
2752 {
2753 printf("\t%s:%d",
2754 frame.GetFileName().mb_str(),
2755 frame.GetLine());
2756 }
2757
2758 puts("");
2759
2760 wxString type, val;
2761 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
2762 {
2763 printf("\t%s %s = %s\n", type.mb_str(), name.mb_str(), val.mb_str());
2764 }
2765 }
2766};
2767
2768static void TestStackWalk(const char *argv0)
2769{
2770 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2771
2772 StackDump dump(argv0);
2773 dump.Walk();
2774}
2775
74e2116a
MB
2776#endif // wxUSE_STACKWALKER
2777
eaff0f0d
VZ
2778#endif // TEST_STACKWALKER
2779
af33b199
VZ
2780// ----------------------------------------------------------------------------
2781// standard paths
2782// ----------------------------------------------------------------------------
2783
2784#ifdef TEST_STDPATHS
2785
2786#include "wx/stdpaths.h"
6f207e66 2787#include "wx/wxchar.h" // wxPrintf
af33b199
VZ
2788
2789static void TestStandardPaths()
2790{
2791 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2792
2793 wxTheApp->SetAppName(_T("console"));
2794
5ba95b79 2795 wxStandardPathsBase& stdp = wxStandardPaths::Get();
af33b199
VZ
2796 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
2797 wxPrintf(_T("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
2798 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
2799 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
2800 wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
2801 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
17af82fb 2802 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
ac7ad70d 2803 wxPrintf(_T("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
af33b199 2804 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
3af9f2de
VZ
2805 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
2806 wxPrintf(_T("Localized res. dir:\t%s\n"),
2807 stdp.GetLocalizedResourcesDir(_T("fr")).c_str());
2808 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2809 stdp.GetLocalizedResourcesDir
2810 (
2811 _T("fr"),
2812 wxStandardPaths::ResourceCat_Messages
2813 ).c_str());
af33b199
VZ
2814}
2815
2816#endif // TEST_STDPATHS
2817
83141d3a
VZ
2818// ----------------------------------------------------------------------------
2819// streams
2820// ----------------------------------------------------------------------------
2821
2822#ifdef TEST_STREAMS
2823
e84010cf
GD
2824#include "wx/wfstream.h"
2825#include "wx/mstream.h"
83141d3a 2826
24f25c8a
VZ
2827static void TestFileStream()
2828{
456ae26d 2829 wxPuts(_T("*** Testing wxFileInputStream ***"));
24f25c8a 2830
e9d2bb6f 2831 static const wxString filename = _T("testdata.fs");
24f25c8a
VZ
2832 {
2833 wxFileOutputStream fsOut(filename);
2834 fsOut.Write("foo", 3);
2835 }
2836
24f25c8a 2837 {
5df663af
VZ
2838 wxFileInputStream fsIn(filename);
2839 wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
2840 while ( !fsIn.Eof() )
2841 {
2842 wxPutchar(fsIn.GetC());
2843 }
24f25c8a
VZ
2844 }
2845
2846 if ( !wxRemoveFile(filename) )
2847 {
e9d2bb6f 2848 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename.c_str());
24f25c8a
VZ
2849 }
2850
456ae26d 2851 wxPuts(_T("\n*** wxFileInputStream test done ***"));
24f25c8a
VZ
2852}
2853
83141d3a
VZ
2854static void TestMemoryStream()
2855{
99a5af7f
VZ
2856 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2857
2858 wxMemoryOutputStream memOutStream;
2859 wxPrintf(_T("Initially out stream offset: %lu\n"),
2860 (unsigned long)memOutStream.TellO());
2861
2862 for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
2863 {
2864 memOutStream.PutC(*p);
2865 }
2866
2867 wxPrintf(_T("Final out stream offset: %lu\n"),
2868 (unsigned long)memOutStream.TellO());
2869
2870 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
83141d3a
VZ
2871
2872 wxChar buf[1024];
99a5af7f 2873 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
83141d3a 2874
99a5af7f
VZ
2875 wxMemoryInputStream memInpStream(buf, len);
2876 wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
83141d3a
VZ
2877 while ( !memInpStream.Eof() )
2878 {
e9d2bb6f 2879 wxPutchar(memInpStream.GetC());
83141d3a
VZ
2880 }
2881
456ae26d 2882 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
83141d3a
VZ
2883}
2884
2885#endif // TEST_STREAMS
2886
d31b7b68
VZ
2887// ----------------------------------------------------------------------------
2888// timers
2889// ----------------------------------------------------------------------------
2890
2891#ifdef TEST_TIMER
2892
5ba95b79 2893#include "wx/stopwatch.h"
e84010cf 2894#include "wx/utils.h"
d31b7b68
VZ
2895
2896static void TestStopWatch()
2897{
456ae26d 2898 wxPuts(_T("*** Testing wxStopWatch ***\n"));
d31b7b68
VZ
2899
2900 wxStopWatch sw;
677eff07 2901 sw.Pause();
456ae26d 2902 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
677eff07
VZ
2903 fflush(stdout);
2904 wxSleep(2);
456ae26d 2905 wxPrintf(_T("\t%ldms\n"), sw.Time());
677eff07 2906
456ae26d 2907 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
677eff07
VZ
2908 fflush(stdout);
2909 sw.Resume();
d31b7b68 2910 wxSleep(3);
456ae26d 2911 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
d31b7b68
VZ
2912
2913 sw.Pause();
456ae26d 2914 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
677eff07 2915 fflush(stdout);
d31b7b68 2916 wxSleep(2);
456ae26d 2917 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
d31b7b68
VZ
2918
2919 sw.Resume();
456ae26d 2920 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
677eff07
VZ
2921 fflush(stdout);
2922 wxSleep(2);
456ae26d 2923 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
87798c00
VZ
2924
2925 wxStopWatch sw2;
456ae26d 2926 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
87798c00
VZ
2927 for ( size_t n = 0; n < 70; n++ )
2928 {
2929 sw2.Start();
89e6463c
GRG
2930
2931 for ( size_t m = 0; m < 100000; m++ )
87798c00 2932 {
89e6463c
GRG
2933 if ( sw.Time() < 0 || sw2.Time() < 0 )
2934 {
456ae26d 2935 wxPuts(_T("\ntime is negative - ERROR!"));
89e6463c 2936 }
87798c00
VZ
2937 }
2938
e9d2bb6f 2939 wxPutchar('.');
677eff07 2940 fflush(stdout);
87798c00
VZ
2941 }
2942
456ae26d 2943 wxPuts(_T(", ok."));
d31b7b68
VZ
2944}
2945
549b95b3
VZ
2946#include "wx/timer.h"
2947#include "wx/evtloop.h"
2948
2949void TestTimer()
2950{
2951 wxPuts(_T("*** Testing wxTimer ***\n"));
2952
2953 class MyTimer : public wxTimer
2954 {
2955 public:
2956 MyTimer() : wxTimer() { m_num = 0; }
2957
2958 virtual void Notify()
2959 {
2960 wxPrintf(_T("%d"), m_num++);
2961
2962 if ( m_num == 10 )
2963 {
2964 wxPrintf(_T("... exiting the event loop"));
2965 Stop();
2966
2967 wxEventLoop::GetActive()->Exit(0);
2968 wxPuts(_T(", ok."));
2969 }
2970
2971 fflush(stdout);
2972 }
2973
2974 private:
2975 int m_num;
2976 };
2977
2978 wxEventLoop loop;
2979
2980 MyTimer timer;
2981 timer.Start(500);
2982
2983 loop.Run();
2984}
2985
d31b7b68
VZ
2986#endif // TEST_TIMER
2987
f6bcfd97
BP
2988// ----------------------------------------------------------------------------
2989// vCard support
2990// ----------------------------------------------------------------------------
2991
2992#ifdef TEST_VCARD
2993
e84010cf 2994#include "wx/vcard.h"
f6bcfd97
BP
2995
2996static void DumpVObject(size_t level, const wxVCardObject& vcard)
2997{
2998 void *cookie;
2999 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
3000 while ( vcObj )
3001 {
456ae26d 3002 wxPrintf(_T("%s%s"),
f6bcfd97
BP
3003 wxString(_T('\t'), level).c_str(),
3004 vcObj->GetName().c_str());
3005
3006 wxString value;
3007 switch ( vcObj->GetType() )
3008 {
3009 case wxVCardObject::String:
3010 case wxVCardObject::UString:
3011 {
3012 wxString val;
3013 vcObj->GetValue(&val);
3014 value << _T('"') << val << _T('"');
3015 }
3016 break;
3017
3018 case wxVCardObject::Int:
3019 {
3020 unsigned int i;
3021 vcObj->GetValue(&i);
3022 value.Printf(_T("%u"), i);
3023 }
3024 break;
3025
3026 case wxVCardObject::Long:
3027 {
3028 unsigned long l;
3029 vcObj->GetValue(&l);
3030 value.Printf(_T("%lu"), l);
3031 }
3032 break;
3033
3034 case wxVCardObject::None:
3035 break;
3036
3037 case wxVCardObject::Object:
3038 value = _T("<node>");
3039 break;
3040
3041 default:
3042 value = _T("<unknown value type>");
3043 }
3044
3045 if ( !!value )
456ae26d 3046 wxPrintf(_T(" = %s"), value.c_str());
e9d2bb6f 3047 wxPutchar('\n');
f6bcfd97
BP
3048
3049 DumpVObject(level + 1, *vcObj);
3050
3051 delete vcObj;
3052 vcObj = vcard.GetNextProp(&cookie);
3053 }
3054}
3055
3056static void DumpVCardAddresses(const wxVCard& vcard)
3057{
456ae26d 3058 wxPuts(_T("\nShowing all addresses from vCard:\n"));
f6bcfd97
BP
3059
3060 size_t nAdr = 0;
3061 void *cookie;
3062 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
3063 while ( addr )
3064 {
3065 wxString flagsStr;
3066 int flags = addr->GetFlags();
3067 if ( flags & wxVCardAddress::Domestic )
3068 {
3069 flagsStr << _T("domestic ");
3070 }
3071 if ( flags & wxVCardAddress::Intl )
3072 {
3073 flagsStr << _T("international ");
3074 }
3075 if ( flags & wxVCardAddress::Postal )
3076 {
3077 flagsStr << _T("postal ");
3078 }
3079 if ( flags & wxVCardAddress::Parcel )
3080 {
3081 flagsStr << _T("parcel ");
3082 }
3083 if ( flags & wxVCardAddress::Home )
3084 {
3085 flagsStr << _T("home ");
3086 }
3087 if ( flags & wxVCardAddress::Work )
3088 {
3089 flagsStr << _T("work ");
3090 }
3091
456ae26d 3092 wxPrintf(_T("Address %u:\n")
f6bcfd97
BP
3093 "\tflags = %s\n"
3094 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3095 ++nAdr,
3096 flagsStr.c_str(),
3097 addr->GetPostOffice().c_str(),
3098 addr->GetExtAddress().c_str(),
3099 addr->GetStreet().c_str(),
3100 addr->GetLocality().c_str(),
3101 addr->GetRegion().c_str(),
3102 addr->GetPostalCode().c_str(),
3103 addr->GetCountry().c_str()
3104 );
3105
3106 delete addr;
3107 addr = vcard.GetNextAddress(&cookie);
3108 }
3109}
3110
3111static void DumpVCardPhoneNumbers(const wxVCard& vcard)
3112{
456ae26d 3113 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
f6bcfd97
BP
3114
3115 size_t nPhone = 0;
3116 void *cookie;
3117 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
3118 while ( phone )
3119 {
3120 wxString flagsStr;
3121 int flags = phone->GetFlags();
3122 if ( flags & wxVCardPhoneNumber::Voice )
3123 {
3124 flagsStr << _T("voice ");
3125 }
3126 if ( flags & wxVCardPhoneNumber::Fax )
3127 {
3128 flagsStr << _T("fax ");
3129 }
3130 if ( flags & wxVCardPhoneNumber::Cellular )
3131 {
3132 flagsStr << _T("cellular ");
3133 }
3134 if ( flags & wxVCardPhoneNumber::Modem )
3135 {
3136 flagsStr << _T("modem ");
3137 }
3138 if ( flags & wxVCardPhoneNumber::Home )
3139 {
3140 flagsStr << _T("home ");
3141 }
3142 if ( flags & wxVCardPhoneNumber::Work )
3143 {
3144 flagsStr << _T("work ");
3145 }
3146
456ae26d 3147 wxPrintf(_T("Phone number %u:\n")
f6bcfd97
BP
3148 "\tflags = %s\n"
3149 "\tvalue = %s\n",
3150 ++nPhone,
3151 flagsStr.c_str(),
3152 phone->GetNumber().c_str()
3153 );
3154
3155 delete phone;
3156 phone = vcard.GetNextPhoneNumber(&cookie);
3157 }
3158}
3159
3160static void TestVCardRead()
3161{
456ae26d 3162 wxPuts(_T("*** Testing wxVCard reading ***\n"));
f6bcfd97
BP
3163
3164 wxVCard vcard(_T("vcard.vcf"));
3165 if ( !vcard.IsOk() )
3166 {
456ae26d 3167 wxPuts(_T("ERROR: couldn't load vCard."));
f6bcfd97
BP
3168 }
3169 else
3170 {
3171 // read individual vCard properties
3172 wxVCardObject *vcObj = vcard.GetProperty("FN");
3173 wxString value;
3174 if ( vcObj )
3175 {
3176 vcObj->GetValue(&value);
3177 delete vcObj;
3178 }
3179 else
3180 {
3181 value = _T("<none>");
3182 }
3183
456ae26d 3184 wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str());
f6bcfd97
BP
3185
3186
3187 if ( !vcard.GetFullName(&value) )
3188 {
3189 value = _T("<none>");
3190 }
3191
456ae26d 3192 wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str());
f6bcfd97 3193
3103e8a9 3194 // now show how to deal with multiply occurring properties
f6bcfd97
BP
3195 DumpVCardAddresses(vcard);
3196 DumpVCardPhoneNumbers(vcard);
3197
3198 // and finally show all
456ae26d 3199 wxPuts(_T("\nNow dumping the entire vCard:\n")
f6bcfd97
BP
3200 "-----------------------------\n");
3201
3202 DumpVObject(0, vcard);
3203 }
3204}
3205
3206static void TestVCardWrite()
3207{
456ae26d 3208 wxPuts(_T("*** Testing wxVCard writing ***\n"));
f6bcfd97
BP
3209
3210 wxVCard vcard;
3211 if ( !vcard.IsOk() )
3212 {
456ae26d 3213 wxPuts(_T("ERROR: couldn't create vCard."));
f6bcfd97
BP
3214 }
3215 else
3216 {
3217 // set some fields
3218 vcard.SetName("Zeitlin", "Vadim");
3219 vcard.SetFullName("Vadim Zeitlin");
be5a51fb 3220 vcard.SetOrganization("wxWidgets", "R&D");
f6bcfd97
BP
3221
3222 // just dump the vCard back
456ae26d
VZ
3223 wxPuts(_T("Entire vCard follows:\n"));
3224 wxPuts(vcard.Write());
f6bcfd97
BP
3225 }
3226}
3227
3228#endif // TEST_VCARD
3229
0e2c5534
VZ
3230// ----------------------------------------------------------------------------
3231// wxVolume tests
3232// ----------------------------------------------------------------------------
3233
ba6ea19e 3234#if !defined(__WIN32__) || !wxUSE_FSVOLUME
0e2c5534
VZ
3235 #undef TEST_VOLUME
3236#endif
3237
3238#ifdef TEST_VOLUME
3239
3240#include "wx/volume.h"
3241
3242static const wxChar *volumeKinds[] =
3243{
3244 _T("floppy"),
3245 _T("hard disk"),
3246 _T("CD-ROM"),
3247 _T("DVD-ROM"),
3248 _T("network volume"),
3249 _T("other volume"),
3250};
3251
3252static void TestFSVolume()
3253{
3254 wxPuts(_T("*** Testing wxFSVolume class ***"));
3255
3256 wxArrayString volumes = wxFSVolume::GetVolumes();
3257 size_t count = volumes.GetCount();
3258
3259 if ( !count )
3260 {
3261 wxPuts(_T("ERROR: no mounted volumes?"));
3262 return;
3263 }
3264
3265 wxPrintf(_T("%u mounted volumes found:\n"), count);
3266
3267 for ( size_t n = 0; n < count; n++ )
3268 {
3269 wxFSVolume vol(volumes[n]);
3270 if ( !vol.IsOk() )
3271 {
3272 wxPuts(_T("ERROR: couldn't create volume"));
3273 continue;
3274 }
3275
3276 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3277 n + 1,
3278 vol.GetDisplayName().c_str(),
3279 vol.GetName().c_str(),
3280 volumeKinds[vol.GetKind()],
3281 vol.IsWritable() ? _T("rw") : _T("ro"),
3282 vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable")
3283 : _T("fixed"));
3284 }
3285}
3286
3287#endif // TEST_VOLUME
3288
f6bcfd97 3289// ----------------------------------------------------------------------------
e7d41190 3290// wide char and Unicode support
f6bcfd97
BP
3291// ----------------------------------------------------------------------------
3292
3293#ifdef TEST_WCHAR
3294
e84010cf
GD
3295#include "wx/strconv.h"
3296#include "wx/fontenc.h"
3297#include "wx/encconv.h"
3298#include "wx/buffer.h"
f6bcfd97 3299
2b5f62a0 3300static const unsigned char utf8koi8r[] =
ac511156
VZ
3301{
3302 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3303 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3304 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3305 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3306 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3307 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3308 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3309};
3310
2b5f62a0
VZ
3311static const unsigned char utf8iso8859_1[] =
3312{
3313 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3314 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3315 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3316 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3317 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3318};
3319
3320static const unsigned char utf8Invalid[] =
3321{
3322 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3323 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3324 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3325 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3326 0x6c, 0x61, 0x79, 0
3327};
3328
3329static const struct Utf8Data
3330{
3331 const unsigned char *text;
3332 size_t len;
3333 const wxChar *charset;
3334 wxFontEncoding encoding;
3335} utf8data[] =
3336{
3337 { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3338 { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 },
3339 { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3340};
456ae26d 3341
f6bcfd97
BP
3342static void TestUtf8()
3343{
456ae26d 3344 wxPuts(_T("*** Testing UTF8 support ***\n"));
f6bcfd97 3345
24f25c8a
VZ
3346 char buf[1024];
3347 wchar_t wbuf[1024];
2b5f62a0
VZ
3348
3349 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
24f25c8a 3350 {
2b5f62a0
VZ
3351 const Utf8Data& u8d = utf8data[n];
3352 if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text,
3353 WXSIZEOF(wbuf)) == (size_t)-1 )
24f25c8a 3354 {
2b5f62a0 3355 wxPuts(_T("ERROR: UTF-8 decoding failed."));
24f25c8a
VZ
3356 }
3357 else
ac511156 3358 {
2b5f62a0
VZ
3359 wxCSConv conv(u8d.charset);
3360 if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 )
3361 {
3362 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset);
3363 }
3364 else
3365 {
3366 wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf);
3367 }
ac511156 3368 }
ac511156 3369
5bc1deeb 3370 wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text));
2b5f62a0
VZ
3371 if ( s.empty() )
3372 s = _T("<< conversion failed >>");
3373 wxPrintf(_T("String in current cset: %s\n"), s.c_str());
3374
ac511156
VZ
3375 }
3376
e9d2bb6f 3377 wxPuts(wxEmptyString);
ac511156 3378}
f6bcfd97 3379
ac511156
VZ
3380static void TestEncodingConverter()
3381{
3382 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3383
3384 // using wxEncodingConverter should give the same result as above
3385 char buf[1024];
3386 wchar_t wbuf[1024];
2b5f62a0
VZ
3387 if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r,
3388 WXSIZEOF(utf8koi8r)) == (size_t)-1 )
ac511156 3389 {
456ae26d 3390 wxPuts(_T("ERROR: UTF-8 decoding failed."));
ac511156
VZ
3391 }
3392 else
3393 {
3394 wxEncodingConverter ec;
3395 ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
3396 ec.Convert(wbuf, buf);
2b5f62a0 3397 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf);
24f25c8a 3398 }
ac511156 3399
e9d2bb6f 3400 wxPuts(wxEmptyString);
f6bcfd97
BP
3401}
3402
3403#endif // TEST_WCHAR
3404
3405// ----------------------------------------------------------------------------
3406// ZIP stream
3407// ----------------------------------------------------------------------------
3408
3409#ifdef TEST_ZIP
3410
2ca8b884
VZ
3411#include "wx/filesys.h"
3412#include "wx/fs_zip.h"
f6bcfd97
BP
3413#include "wx/zipstrm.h"
3414
2ca8b884
VZ
3415static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
3416
f6bcfd97
BP
3417static void TestZipStreamRead()
3418{
456ae26d 3419 wxPuts(_T("*** Testing ZIP reading ***\n"));
f6bcfd97 3420
e9d2bb6f 3421 static const wxString filename = _T("foo");
2ca8b884 3422 wxZipInputStream istr(TESTFILE_ZIP, filename);
456ae26d 3423 wxPrintf(_T("Archive size: %u\n"), istr.GetSize());
f6bcfd97 3424
e9d2bb6f 3425 wxPrintf(_T("Dumping the file '%s':\n"), filename.c_str());
f6bcfd97
BP
3426 while ( !istr.Eof() )
3427 {
e9d2bb6f 3428 wxPutchar(istr.GetC());
f6bcfd97
BP
3429 fflush(stdout);
3430 }
3431
456ae26d 3432 wxPuts(_T("\n----- done ------"));
f6bcfd97
BP
3433}
3434
2ca8b884
VZ
3435static void DumpZipDirectory(wxFileSystem& fs,
3436 const wxString& dir,
3437 const wxString& indent)
3438{
3439 wxString prefix = wxString::Format(_T("%s#zip:%s"),
3440 TESTFILE_ZIP, dir.c_str());
3441 wxString wildcard = prefix + _T("/*");
3442
3443 wxString dirname = fs.FindFirst(wildcard, wxDIR);
3444 while ( !dirname.empty() )
3445 {
3446 if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
3447 {
3448 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3449
3450 break;
3451 }
3452
3453 wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
3454
3455 DumpZipDirectory(fs, dirname,
3456 indent + wxString(_T(' '), 4));
3457
3458 dirname = fs.FindNext();
3459 }
3460
3461 wxString filename = fs.FindFirst(wildcard, wxFILE);
3462 while ( !filename.empty() )
3463 {
3464 if ( !filename.StartsWith(prefix, &filename) )
3465 {
3466 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3467
3468 break;
3469 }
3470
3471 wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
3472
3473 filename = fs.FindNext();
3474 }
3475}
3476
3477static void TestZipFileSystem()
3478{
456ae26d 3479 wxPuts(_T("*** Testing ZIP file system ***\n"));
2ca8b884
VZ
3480
3481 wxFileSystem::AddHandler(new wxZipFSHandler);
3482 wxFileSystem fs;
3483 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
3484
3485 DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
3486}
3487
f6bcfd97
BP
3488#endif // TEST_ZIP
3489
b76b015e
VZ
3490// ----------------------------------------------------------------------------
3491// date time
3492// ----------------------------------------------------------------------------
3493
d31b7b68 3494#ifdef TEST_DATETIME
b76b015e 3495
b713f891 3496#include "wx/math.h"
e84010cf 3497#include "wx/datetime.h"
b76b015e 3498
2f02cb89 3499// this test miscellaneous static wxDateTime functions
4b586201
WS
3500
3501#if TEST_ALL
3502
2f02cb89
VZ
3503static void TestTimeStatic()
3504{
456ae26d 3505 wxPuts(_T("\n*** wxDateTime static methods test ***"));
2f02cb89
VZ
3506
3507 // some info about the current date
3508 int year = wxDateTime::GetCurrentYear();
456ae26d 3509 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
2f02cb89
VZ
3510 year,
3511 wxDateTime::IsLeapYear(year) ? "" : "not ",
3512 wxDateTime::GetNumberOfDays(year));
3513
3514 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
456ae26d 3515 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
f0f951fa 3516 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
2f02cb89
VZ
3517 wxDateTime::GetMonthName(month).c_str(),
3518 wxDateTime::GetNumberOfDays(month));
2f02cb89
VZ
3519}
3520
fcc3d7cb
VZ
3521// test time zones stuff
3522static void TestTimeZones()
3523{
456ae26d 3524 wxPuts(_T("\n*** wxDateTime timezone test ***"));
fcc3d7cb
VZ
3525
3526 wxDateTime now = wxDateTime::Now();
3527
456ae26d
VZ
3528 wxPrintf(_T("Current GMT time:\t%s\n"), now.Format(_T("%c"), wxDateTime::GMT0).c_str());
3529 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0).c_str());
3530 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST).c_str());
3531 wxPrintf(_T("Current time in Paris:\t%s\n"), now.Format(_T("%c"), wxDateTime::CET).c_str());
3532 wxPrintf(_T(" Moscow:\t%s\n"), now.Format(_T("%c"), wxDateTime::MSK).c_str());
3533 wxPrintf(_T(" New York:\t%s\n"), now.Format(_T("%c"), wxDateTime::EST).c_str());
9d9b7755 3534
ccd6aacd
VZ
3535 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3536
9d9b7755
VZ
3537 wxDateTime::Tm tm = now.GetTm();
3538 if ( wxDateTime(tm) != now )
3539 {
456ae26d
VZ
3540 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3541 wxDateTime(tm).Format().c_str(), now.Format().c_str());
9d9b7755 3542 }
fcc3d7cb
VZ
3543}
3544
e6ec579c
VZ
3545// test some minimal support for the dates outside the standard range
3546static void TestTimeRange()
3547{
456ae26d 3548 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
e6ec579c 3549
456ae26d 3550 static const wxChar *fmt = _T("%d-%b-%Y %H:%M:%S");
211c2250 3551
456ae26d
VZ
3552 wxPrintf(_T("Unix epoch:\t%s\n"),
3553 wxDateTime(2440587.5).Format(fmt).c_str());
3554 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3555 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
3556 wxPrintf(_T("JDN 0: \t%s\n"),
3557 wxDateTime(0.0).Format(fmt).c_str());
3558 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3559 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
3560 wxPrintf(_T("May 29, 2099:\t%s\n"),
3561 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
e6ec579c
VZ
3562}
3563
239446b4
VZ
3564// test DST calculations
3565static void TestTimeDST()
3566{
456ae26d 3567 wxPuts(_T("\n*** wxDateTime DST test ***"));
239446b4 3568
456ae26d 3569 wxPrintf(_T("DST is%s in effect now.\n\n"),
e9d2bb6f 3570 wxDateTime::Now().IsDST() ? wxEmptyString : _T(" not"));
239446b4 3571
ccd6aacd 3572 for ( int year = 1990; year < 2005; year++ )
239446b4 3573 {
456ae26d
VZ
3574 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3575 year,
3576 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
3577 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
239446b4
VZ
3578 }
3579}
3580
4b586201
WS
3581#endif // TEST_ALL
3582
3583#if TEST_INTERACTIVE
3584
b92fd37c 3585static void TestDateTimeInteractive()
9d9b7755 3586{
456ae26d 3587 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
9d9b7755 3588
456ae26d 3589 wxChar buf[128];
9d9b7755
VZ
3590
3591 for ( ;; )
3592 {
456ae26d
VZ
3593 wxPrintf(_T("Enter a date: "));
3594 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
9d9b7755
VZ
3595 break;
3596
f6bcfd97 3597 // kill the last '\n'
456ae26d 3598 buf[wxStrlen(buf) - 1] = 0;
f6bcfd97 3599
9d9b7755 3600 wxDateTime dt;
456ae26d 3601 const wxChar *p = dt.ParseDate(buf);
f6bcfd97 3602 if ( !p )
9d9b7755 3603 {
456ae26d 3604 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf);
9d9b7755
VZ
3605
3606 continue;
3607 }
f6bcfd97
BP
3608 else if ( *p )
3609 {
456ae26d 3610 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf);
f6bcfd97 3611 }
9d9b7755 3612
456ae26d
VZ
3613 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3614 dt.Format(_T("%b %d, %Y")).c_str(),
3615 dt.GetDayOfYear(),
3616 dt.GetWeekOfMonth(wxDateTime::Monday_First),
3617 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
3618 dt.GetWeekOfYear(wxDateTime::Monday_First));
9d9b7755
VZ
3619 }
3620
456ae26d 3621 wxPuts(_T("\n*** done ***"));
9d9b7755
VZ
3622}
3623
4b586201
WS
3624#endif // TEST_INTERACTIVE
3625
3626#if TEST_ALL
3627
f6bcfd97
BP
3628static void TestTimeMS()
3629{
456ae26d 3630 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
f6bcfd97
BP
3631
3632 wxDateTime dt1 = wxDateTime::Now(),
3633 dt2 = wxDateTime::UNow();
3634
456ae26d
VZ
3635 wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str());
3636 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3637 wxPrintf(_T("Dummy loop: "));
3ca6a5f0
BP
3638 for ( int i = 0; i < 6000; i++ )
3639 {
3640 //for ( int j = 0; j < 10; j++ )
3641 {
3642 wxString s;
ccd6aacd 3643 s.Printf(_T("%g"), sqrt((float)i));
3ca6a5f0
BP
3644 }
3645
3646 if ( !(i % 100) )
e9d2bb6f 3647 wxPutchar('.');
3ca6a5f0 3648 }
456ae26d 3649 wxPuts(_T(", done"));
3ca6a5f0
BP
3650
3651 dt1 = dt2;
3652 dt2 = wxDateTime::UNow();
456ae26d 3653 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3ca6a5f0 3654
456ae26d 3655 wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str());
f6bcfd97 3656
456ae26d 3657 wxPuts(_T("\n*** done ***"));
f6bcfd97
BP
3658}
3659
0de868d9
VZ
3660static void TestTimeHolidays()
3661{
456ae26d 3662 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
0de868d9
VZ
3663
3664 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
3665 wxDateTime dtStart(1, tm.mon, tm.year),
3666 dtEnd = dtStart.GetLastMonthDay();
3667
3668 wxDateTimeArray hol;
3669 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
3670
456ae26d 3671 const wxChar *format = _T("%d-%b-%Y (%a)");
0de868d9 3672
456ae26d 3673 wxPrintf(_T("All holidays between %s and %s:\n"),
0de868d9
VZ
3674 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
3675
3676 size_t count = hol.GetCount();
3677 for ( size_t n = 0; n < count; n++ )
3678 {
456ae26d 3679 wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str());
0de868d9
VZ
3680 }
3681
e9d2bb6f 3682 wxPuts(wxEmptyString);
0de868d9
VZ
3683}
3684
f6bcfd97
BP
3685static void TestTimeZoneBug()
3686{
456ae26d 3687 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
f6bcfd97
BP
3688
3689 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
3690 for ( int i = 0; i < 31; i++ )
3691 {
456ae26d 3692 wxPrintf(_T("Date %s: week day %s.\n"),
f6bcfd97
BP
3693 date.Format(_T("%d-%m-%Y")).c_str(),
3694 date.GetWeekDayName(date.GetWeekDay()).c_str());
3695
3696 date += wxDateSpan::Day();
3697 }
3698
e9d2bb6f 3699 wxPuts(wxEmptyString);
f6bcfd97
BP
3700}
3701
df05cdc5
VZ
3702static void TestTimeSpanFormat()
3703{
456ae26d 3704 wxPuts(_T("\n*** wxTimeSpan tests ***"));
df05cdc5 3705
456ae26d 3706 static const wxChar *formats[] =
df05cdc5
VZ
3707 {
3708 _T("(default) %H:%M:%S"),
3709 _T("%E weeks and %D days"),
3710 _T("%l milliseconds"),
3711 _T("(with ms) %H:%M:%S:%l"),
3712 _T("100%% of minutes is %M"), // test "%%"
3713 _T("%D days and %H hours"),
a8625337 3714 _T("or also %S seconds"),
df05cdc5
VZ
3715 };
3716
3717 wxTimeSpan ts1(1, 2, 3, 4),
3718 ts2(111, 222, 333);
3719 for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
3720 {
456ae26d 3721 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
df05cdc5
VZ
3722 ts1.Format(formats[n]).c_str(),
3723 ts2.Format(formats[n]).c_str());
3724 }
3725
e9d2bb6f 3726 wxPuts(wxEmptyString);
df05cdc5
VZ
3727}
3728
4b586201
WS
3729#endif // TEST_ALL
3730
d31b7b68 3731#endif // TEST_DATETIME
b76b015e 3732
39937656
VZ
3733// ----------------------------------------------------------------------------
3734// wxTextInput/OutputStream
3735// ----------------------------------------------------------------------------
3736
3737#ifdef TEST_TEXTSTREAM
3738
3739#include "wx/txtstrm.h"
3740#include "wx/wfstream.h"
3741
3742static void TestTextInputStream()
3743{
3744 wxPuts(_T("\n*** wxTextInputStream test ***"));
3745
e9d2bb6f
DS
3746 wxString filename = _T("testdata.fc");
3747 wxFileInputStream fsIn(filename);
39937656
VZ
3748 if ( !fsIn.Ok() )
3749 {
3750 wxPuts(_T("ERROR: couldn't open file."));
3751 }
3752 else
3753 {
3754 wxTextInputStream tis(fsIn);
3755
3756 size_t line = 1;
3757 for ( ;; )
3758 {
3759 const wxString s = tis.ReadLine();
3760
3761 // line could be non empty if the last line of the file isn't
3762 // terminated with EOL
3763 if ( fsIn.Eof() && s.empty() )
3764 break;
3765
3766 wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
3767 }
3768 }
3769}
3770
3771#endif // TEST_TEXTSTREAM
3772
e87271f3
VZ
3773// ----------------------------------------------------------------------------
3774// threads
3775// ----------------------------------------------------------------------------
3776
3777#ifdef TEST_THREADS
3778
e84010cf 3779#include "wx/thread.h"
37667812 3780
bbfa0322
VZ
3781static size_t gs_counter = (size_t)-1;
3782static wxCriticalSection gs_critsect;
c112e100 3783static wxSemaphore gs_cond;
bbfa0322 3784
b568d04f 3785class MyJoinableThread : public wxThread
bbfa0322
VZ
3786{
3787public:
b568d04f
VZ
3788 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
3789 { m_n = n; Create(); }
bbfa0322
VZ
3790
3791 // thread execution starts here
b568d04f 3792 virtual ExitCode Entry();
bbfa0322 3793
b568d04f
VZ
3794private:
3795 size_t m_n;
bbfa0322
VZ
3796};
3797
b568d04f 3798wxThread::ExitCode MyJoinableThread::Entry()
bbfa0322 3799{
b568d04f
VZ
3800 unsigned long res = 1;
3801 for ( size_t n = 1; n < m_n; n++ )
3802 {
3803 res *= n;
3804
3805 // it's a loooong calculation :-)
3806 Sleep(100);
3807 }
bbfa0322 3808
b568d04f 3809 return (ExitCode)res;
bbfa0322
VZ
3810}
3811
b568d04f
VZ
3812class MyDetachedThread : public wxThread
3813{
3814public:
456ae26d 3815 MyDetachedThread(size_t n, wxChar ch)
fcc3d7cb
VZ
3816 {
3817 m_n = n;
3818 m_ch = ch;
cab8f76e 3819 m_cancelled = false;
fcc3d7cb
VZ
3820
3821 Create();
3822 }
b568d04f
VZ
3823
3824 // thread execution starts here
3825 virtual ExitCode Entry();
3826
3827 // and stops here
3828 virtual void OnExit();
3829
3830private:
9fc3ad34 3831 size_t m_n; // number of characters to write
456ae26d 3832 wxChar m_ch; // character to write
fcc3d7cb 3833
cab8f76e 3834 bool m_cancelled; // false if we exit normally
b568d04f
VZ
3835};
3836
3837wxThread::ExitCode MyDetachedThread::Entry()
bbfa0322
VZ
3838{
3839 {
3840 wxCriticalSectionLocker lock(gs_critsect);
3841 if ( gs_counter == (size_t)-1 )
3842 gs_counter = 1;
3843 else
3844 gs_counter++;
3845 }
3846
9fc3ad34 3847 for ( size_t n = 0; n < m_n; n++ )
bbfa0322
VZ
3848 {
3849 if ( TestDestroy() )
fcc3d7cb 3850 {
cab8f76e 3851 m_cancelled = true;
fcc3d7cb 3852
bbfa0322 3853 break;
fcc3d7cb 3854 }
bbfa0322 3855
e9d2bb6f 3856 wxPutchar(m_ch);
bbfa0322
VZ
3857 fflush(stdout);
3858
3859 wxThread::Sleep(100);
3860 }
3861
b568d04f 3862 return 0;
bbfa0322
VZ
3863}
3864
b568d04f 3865void MyDetachedThread::OnExit()
bbfa0322 3866{
456ae26d 3867 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
9fc3ad34 3868
bbfa0322 3869 wxCriticalSectionLocker lock(gs_critsect);
fcc3d7cb 3870 if ( !--gs_counter && !m_cancelled )
c112e100 3871 gs_cond.Post();
bbfa0322
VZ
3872}
3873
2f2f3e2a 3874static void TestDetachedThreads()
9fc3ad34 3875{
456ae26d 3876 wxPuts(_T("\n*** Testing detached threads ***"));
9fc3ad34
VZ
3877
3878 static const size_t nThreads = 3;
3879 MyDetachedThread *threads[nThreads];
3880 size_t n;
3881 for ( n = 0; n < nThreads; n++ )
3882 {
3883 threads[n] = new MyDetachedThread(10, 'A' + n);
3884 }
3885
3886 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
3887 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
3888
3889 for ( n = 0; n < nThreads; n++ )
3890 {
3891 threads[n]->Run();
3892 }
3893
3894 // wait until all threads terminate
3895 gs_cond.Wait();
3896
e9d2bb6f 3897 wxPuts(wxEmptyString);
9fc3ad34
VZ
3898}
3899
2f2f3e2a 3900static void TestJoinableThreads()
9fc3ad34 3901{
456ae26d 3902 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
9fc3ad34
VZ
3903
3904 // calc 10! in the background
3905 MyJoinableThread thread(10);
3906 thread.Run();
3907
456ae26d 3908 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
5bc1deeb 3909 (unsigned long)thread.Wait());
9fc3ad34
VZ
3910}
3911
2f2f3e2a 3912static void TestThreadSuspend()
9fc3ad34 3913{
456ae26d 3914 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
2f02cb89
VZ
3915
3916 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
9fc3ad34
VZ
3917
3918 thread->Run();
3919
3920 // this is for this demo only, in a real life program we'd use another
3921 // condition variable which would be signaled from wxThread::Entry() to
3922 // tell us that the thread really started running - but here just wait a
3923 // bit and hope that it will be enough (the problem is, of course, that
3924 // the thread might still not run when we call Pause() which will result
3925 // in an error)
3926 wxThread::Sleep(300);
3927
3928 for ( size_t n = 0; n < 3; n++ )
3929 {
3930 thread->Pause();
3931
456ae26d 3932 wxPuts(_T("\nThread suspended"));
9fc3ad34
VZ
3933 if ( n > 0 )
3934 {
3935 // don't sleep but resume immediately the first time
3936 wxThread::Sleep(300);
3937 }
456ae26d 3938 wxPuts(_T("Going to resume the thread"));
9fc3ad34
VZ
3939
3940 thread->Resume();
3941 }
3942
456ae26d 3943 wxPuts(_T("Waiting until it terminates now"));
4c460b34 3944
9fc3ad34
VZ
3945 // wait until the thread terminates
3946 gs_cond.Wait();
3947
e9d2bb6f 3948 wxPuts(wxEmptyString);
9fc3ad34
VZ
3949}
3950
2f2f3e2a 3951static void TestThreadDelete()
2f02cb89
VZ
3952{
3953 // As above, using Sleep() is only for testing here - we must use some
3954 // synchronisation object instead to ensure that the thread is still
3955 // running when we delete it - deleting a detached thread which already
3956 // terminated will lead to a crash!
3957
456ae26d 3958 wxPuts(_T("\n*** Testing thread delete function ***"));
2f02cb89 3959
4c460b34
VZ
3960 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
3961
3962 thread0->Delete();
3963
456ae26d 3964 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
4c460b34 3965
2f02cb89
VZ
3966 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
3967
3968 thread1->Run();
3969
3970 wxThread::Sleep(300);
3971
3972 thread1->Delete();
3973
456ae26d 3974 wxPuts(_T("\nDeleted a running thread."));
2f02cb89
VZ
3975
3976 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
3977
3978 thread2->Run();
3979
3980 wxThread::Sleep(300);
3981
3982 thread2->Pause();
3983
3984 thread2->Delete();
3985
456ae26d 3986 wxPuts(_T("\nDeleted a sleeping thread."));
2f02cb89 3987
4c460b34
VZ
3988 MyJoinableThread thread3(20);
3989 thread3.Run();
2f02cb89 3990
4c460b34 3991 thread3.Delete();
2f02cb89 3992
456ae26d 3993 wxPuts(_T("\nDeleted a joinable thread."));
2f02cb89 3994
4c460b34
VZ
3995 MyJoinableThread thread4(2);
3996 thread4.Run();
2f02cb89
VZ
3997
3998 wxThread::Sleep(300);
3999
4c460b34 4000 thread4.Delete();
2f02cb89 4001
456ae26d 4002 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
2f02cb89 4003
e9d2bb6f 4004 wxPuts(wxEmptyString);
2f02cb89
VZ
4005}
4006
2f2f3e2a
VZ
4007class MyWaitingThread : public wxThread
4008{
4009public:
c112e100 4010 MyWaitingThread( wxMutex *mutex, wxCondition *condition )
2f2f3e2a 4011 {
c112e100 4012 m_mutex = mutex;
2f2f3e2a
VZ
4013 m_condition = condition;
4014
4015 Create();
4016 }
4017
4018 virtual ExitCode Entry()
4019 {
456ae26d 4020 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
2f2f3e2a
VZ
4021 fflush(stdout);
4022
c112e100 4023 gs_cond.Post();
2f2f3e2a 4024
456ae26d 4025 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
2f2f3e2a
VZ
4026 fflush(stdout);
4027
c112e100 4028 m_mutex->Lock();
2f2f3e2a 4029 m_condition->Wait();
c112e100 4030 m_mutex->Unlock();
2f2f3e2a 4031
456ae26d 4032 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
2f2f3e2a
VZ
4033 fflush(stdout);
4034
4035 return 0;
4036 }
4037
4038private:
c112e100 4039 wxMutex *m_mutex;
2f2f3e2a
VZ
4040 wxCondition *m_condition;
4041};
4042
4043static void TestThreadConditions()
4044{
c112e100
VZ
4045 wxMutex mutex;
4046 wxCondition condition(mutex);
2f2f3e2a 4047
8d5eff60
VZ
4048 // otherwise its difficult to understand which log messages pertain to
4049 // which condition
456ae26d 4050 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
c112e100 4051 // condition.GetId(), gs_cond.GetId());
8d5eff60 4052
2f2f3e2a 4053 // create and launch threads
60ce696e 4054 MyWaitingThread *threads[10];
2f2f3e2a
VZ
4055
4056 size_t n;
4057 for ( n = 0; n < WXSIZEOF(threads); n++ )
4058 {
c112e100 4059 threads[n] = new MyWaitingThread( &mutex, &condition );
2f2f3e2a
VZ
4060 }
4061
4062 for ( n = 0; n < WXSIZEOF(threads); n++ )
4063 {
4064 threads[n]->Run();
4065 }
4066
4067 // wait until all threads run
456ae26d 4068 wxPuts(_T("Main thread is waiting for the other threads to start"));
2f2f3e2a
VZ
4069 fflush(stdout);
4070
4071 size_t nRunning = 0;
4072 while ( nRunning < WXSIZEOF(threads) )
4073 {
4074 gs_cond.Wait();
4075
2f2f3e2a 4076 nRunning++;
8d5eff60 4077
456ae26d 4078 wxPrintf(_T("Main thread: %u already running\n"), nRunning);
8d5eff60 4079 fflush(stdout);
2f2f3e2a
VZ
4080 }
4081
456ae26d 4082 wxPuts(_T("Main thread: all threads started up."));
2f2f3e2a
VZ
4083 fflush(stdout);
4084
8d5eff60
VZ
4085 wxThread::Sleep(500);
4086
60ce696e 4087#if 1
8d5eff60 4088 // now wake one of them up
456ae26d 4089 wxPrintf(_T("Main thread: about to signal the condition.\n"));
2f2f3e2a
VZ
4090 fflush(stdout);
4091 condition.Signal();
8d5eff60 4092#endif
2f2f3e2a 4093
60ce696e
VZ
4094 wxThread::Sleep(200);
4095
8d5eff60 4096 // wake all the (remaining) threads up, so that they can exit
456ae26d 4097 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
2f2f3e2a
VZ
4098 fflush(stdout);
4099 condition.Broadcast();
4100
8d5eff60
VZ
4101 // give them time to terminate (dirty!)
4102 wxThread::Sleep(500);
2f2f3e2a
VZ
4103}
4104
c112e100
VZ
4105#include "wx/utils.h"
4106
4107class MyExecThread : public wxThread
4108{
4109public:
4110 MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
4111 m_command(command)
4112 {
4113 Create();
4114 }
4115
4116 virtual ExitCode Entry()
4117 {
4118 return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
4119 }
4120
4121private:
4122 wxString m_command;
4123};
4124
4125static void TestThreadExec()
4126{
4127 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4128
4129 MyExecThread thread(_T("true"));
4130 thread.Run();
4131
4132 wxPrintf(_T("Main program exit code: %ld.\n"),
4133 wxExecute(_T("false"), wxEXEC_SYNC));
4134
4135 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
4136}
4137
4138// semaphore tests
4139#include "wx/datetime.h"
4140
4141class MySemaphoreThread : public wxThread
4142{
4143public:
4144 MySemaphoreThread(int i, wxSemaphore *sem)
4145 : wxThread(wxTHREAD_JOINABLE),
4146 m_sem(sem),
4147 m_i(i)
4148 {
4149 Create();
4150 }
4151
4152 virtual ExitCode Entry()
4153 {
f06ef5f4
VZ
4154 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4155 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
c112e100
VZ
4156
4157 m_sem->Wait();
4158
f06ef5f4
VZ
4159 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4160 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
c112e100
VZ
4161
4162 Sleep(1000);
4163
f06ef5f4
VZ
4164 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4165 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
c112e100
VZ
4166
4167 m_sem->Post();
4168
4169 return 0;
4170 }
4171
4172private:
4173 wxSemaphore *m_sem;
4174 int m_i;
4175};
4176
e9d2bb6f 4177WX_DEFINE_ARRAY_PTR(wxThread *, ArrayThreads);
c112e100
VZ
4178
4179static void TestSemaphore()
4180{
4181 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4182
4183 static const int SEM_LIMIT = 3;
4184
4185 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
4186 ArrayThreads threads;
4187
4188 for ( int i = 0; i < 3*SEM_LIMIT; i++ )
4189 {
4190 threads.Add(new MySemaphoreThread(i, &sem));
4191 threads.Last()->Run();
4192 }
4193
4194 for ( size_t n = 0; n < threads.GetCount(); n++ )
4195 {
4196 threads[n]->Wait();
4197 delete threads[n];
4198 }
4199}
4200
e87271f3
VZ
4201#endif // TEST_THREADS
4202
e87271f3
VZ
4203// ----------------------------------------------------------------------------
4204// entry point
4205// ----------------------------------------------------------------------------
4206
daa2c7d9
VZ
4207#ifdef TEST_SNGLINST
4208 #include "wx/snglinst.h"
4209#endif // TEST_SNGLINST
4210
bbfa0322 4211int main(int argc, char **argv)
37667812 4212{
5ba95b79
WS
4213#if wxUSE_UNICODE
4214 wxChar **wxArgv = new wxChar *[argc + 1];
4215
4216 {
4217 int n;
4218
4219 for (n = 0; n < argc; n++ )
4220 {
4221 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
4222 wxArgv[n] = wxStrdup(warg);
4223 }
4224
4225 wxArgv[n] = NULL;
4226 }
4227#else // !wxUSE_UNICODE
4228 #define wxArgv argv
4229#endif // wxUSE_UNICODE/!wxUSE_UNICODE
4230
6f35ed60 4231 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
9cb47ea2 4232
58b24a56
VZ
4233 wxInitializer initializer;
4234 if ( !initializer )
37667812 4235 {
be5a51fb 4236 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
58b24a56
VZ
4237
4238 return -1;
4239 }
4240
4241#ifdef TEST_SNGLINST
b5299791
VZ
4242 wxSingleInstanceChecker checker;
4243 if ( checker.Create(_T(".wxconsole.lock")) )
58b24a56 4244 {
b5299791
VZ
4245 if ( checker.IsAnotherRunning() )
4246 {
4247 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
58b24a56 4248
b5299791
VZ
4249 return 1;
4250 }
37667812 4251
b5299791
VZ
4252 // wait some time to give time to launch another instance
4253 wxPrintf(_T("Press \"Enter\" to continue..."));
4254 wxFgetc(stdin);
4255 }
4256 else // failed to create
4257 {
4258 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4259 }
58b24a56
VZ
4260#endif // TEST_SNGLINST
4261
d34bce84 4262#ifdef TEST_CMDLINE
31f6de22
VZ
4263 TestCmdLineConvert();
4264
4265#if wxUSE_CMDLINE_PARSER
d34bce84
VZ
4266 static const wxCmdLineEntryDesc cmdLineDesc[] =
4267 {
456ae26d 4268 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"),
31a06b07 4269 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
456ae26d
VZ
4270 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") },
4271 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
d34bce84 4272
456ae26d
VZ
4273 { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file") },
4274 { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("input dir") },
4275 { wxCMD_LINE_OPTION, _T("s"), _T("size"), _T("output block size"),
31a06b07 4276 wxCMD_LINE_VAL_NUMBER },
456ae26d 4277 { wxCMD_LINE_OPTION, _T("d"), _T("date"), _T("output file date"),
31a06b07 4278 wxCMD_LINE_VAL_DATE },
d34bce84 4279
456ae26d 4280 { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"),
d34bce84
VZ
4281 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
4282
4283 { wxCMD_LINE_NONE }
4284 };
4285
5ba95b79 4286 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
456ae26d
VZ
4287
4288 parser.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
f6bcfd97
BP
4289 wxCMD_LINE_VAL_STRING,
4290 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
4291
d34bce84
VZ
4292 switch ( parser.Parse() )
4293 {
4294 case -1:
456ae26d 4295 wxLogMessage(_T("Help was given, terminating."));
d34bce84
VZ
4296 break;
4297
4298 case 0:
4299 ShowCmdLine(parser);
4300 break;
4301
4302 default:
456ae26d 4303 wxLogMessage(_T("Syntax error detected, aborting."));
d34bce84
VZ
4304 break;
4305 }
31f6de22
VZ
4306#endif // wxUSE_CMDLINE_PARSER
4307
d34bce84
VZ
4308#endif // TEST_CMDLINE
4309
1944c6bd 4310#ifdef TEST_DIR
e9d2bb6f 4311 #if TEST_ALL
99a5af7f 4312 TestDirExists();
2f0c19d0 4313 TestDirEnum();
e9d2bb6f 4314 #endif
2f0c19d0 4315 TestDirTraverse();
1944c6bd
VZ
4316#endif // TEST_DIR
4317
93ed8ff7 4318#ifdef TEST_DYNLIB
f6bcfd97 4319 TestDllLoad();
297ebe6b 4320 TestDllListLoaded();
93ed8ff7 4321#endif // TEST_DYNLIB
f6bcfd97 4322
8fd0d89b
VZ
4323#ifdef TEST_ENVIRON
4324 TestEnvironment();
4325#endif // TEST_ENVIRON
4326
d93c719a
VZ
4327#ifdef TEST_EXECUTE
4328 TestExecute();
4329#endif // TEST_EXECUTE
4330
ee6e1b1d
VZ
4331#ifdef TEST_FILECONF
4332 TestFileConfRead();
4333#endif // TEST_FILECONF
4334
ec37df57
VZ
4335#ifdef TEST_LOCALE
4336 TestDefaultLang();
4337#endif // TEST_LOCALE
4338
378b05f7 4339#ifdef TEST_LOG
cab8f76e
VZ
4340 wxPuts(_T("*** Testing wxLog ***"));
4341
378b05f7
VZ
4342 wxString s;
4343 for ( size_t n = 0; n < 8000; n++ )
4344 {
456ae26d 4345 s << (wxChar)(_T('A') + (n % 26));
378b05f7
VZ
4346 }
4347
cab8f76e
VZ
4348 wxLogWarning(_T("The length of the string is %lu"),
4349 (unsigned long)s.length());
4350
378b05f7 4351 wxString msg;
456ae26d 4352 msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str());
378b05f7
VZ
4353
4354 // this one shouldn't be truncated
456ae26d 4355 wxPrintf(msg);
378b05f7
VZ
4356
4357 // but this one will because log functions use fixed size buffer
b568d04f
VZ
4358 // (note that it doesn't need '\n' at the end neither - will be added
4359 // by wxLog anyhow)
456ae26d 4360 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s.c_str());
378b05f7
VZ
4361#endif // TEST_LOG
4362
f6bcfd97 4363#ifdef TEST_FILE
e9d2bb6f
DS
4364 TestFileRead();
4365 TestTextFileRead();
4366 TestFileCopy();
59062ec1 4367 TestTempFile();
f6bcfd97
BP
4368#endif // TEST_FILE
4369
844f90fb 4370#ifdef TEST_FILENAME
e9d2bb6f
DS
4371 TestFileNameTemp();
4372 TestFileNameCwd();
4373 TestFileNameDirManip();
4374 TestFileNameComparison();
4375 TestFileNameOperations();
844f90fb
VZ
4376#endif // TEST_FILENAME
4377
d56e2b97
VZ
4378#ifdef TEST_FILETIME
4379 TestFileGetTimes();
e9d2bb6f 4380 #if 0
d56e2b97 4381 TestFileSetTimes();
e9d2bb6f 4382 #endif
d56e2b97
VZ
4383#endif // TEST_FILETIME
4384
07a56e45
VZ
4385#ifdef TEST_FTP
4386 wxLog::AddTraceMask(FTP_TRACE_MASK);
4387 if ( TestFtpConnect() )
4388 {
e9d2bb6f 4389 #if TEST_ALL
07a56e45
VZ
4390 TestFtpList();
4391 TestFtpDownload();
4392 TestFtpMisc();
daa2c7d9 4393 TestFtpFileSize();
07a56e45 4394 TestFtpUpload();
af33b199 4395 #endif // TEST_ALL
daa2c7d9 4396
e9d2bb6f 4397 #if TEST_INTERACTIVE
daa2c7d9 4398 TestFtpInteractive();
e9d2bb6f 4399 #endif
07a56e45
VZ
4400 }
4401 //else: connecting to the FTP server failed
4402
e9d2bb6f 4403 #if 0
07a56e45 4404 TestFtpWuFtpd();
e9d2bb6f 4405 #endif
07a56e45
VZ
4406#endif // TEST_FTP
4407
696e1ea0 4408#ifdef TEST_MIME
f6bcfd97 4409 wxLog::AddTraceMask(_T("mime"));
e9d2bb6f 4410 #if TEST_ALL
f6bcfd97 4411 TestMimeEnum();
ac7ad70d 4412 #endif
c7ce8392 4413 TestMimeOverride();
f06ef5f4 4414 TestMimeAssociate();
f06ef5f4 4415 TestMimeFilename();
696e1ea0
VZ
4416#endif // TEST_MIME
4417
89e60357 4418#ifdef TEST_INFO_FUNCTIONS
8bb6b2c0
VZ
4419 TestOsInfo();
4420 TestPlatformInfo();
4421 TestUserInfo();
19f45995 4422
8bb6b2c0
VZ
4423 #if TEST_INTERACTIVE
4424 TestDiskInfo();
e9d2bb6f 4425 #endif
89e60357
VZ
4426#endif // TEST_INFO_FUNCTIONS
4427
39189b9d
VZ
4428#ifdef TEST_PATHLIST
4429 TestPathList();
4430#endif // TEST_PATHLIST
4431
8d5eff60
VZ
4432#ifdef TEST_ODBC
4433 TestDbOpen();
4434#endif // TEST_ODBC
4435
7aeebdcd
VZ
4436#ifdef TEST_PRINTF
4437 TestPrintf();
4438#endif // TEST_PRINTF
4439
7ba4fbeb 4440#ifdef TEST_REGCONF
e9d2bb6f
DS
4441 #if 0
4442 TestRegConfWrite();
4443 #endif
0aa29b6b 4444 TestRegConfRead();
7ba4fbeb
VZ
4445#endif // TEST_REGCONF
4446
bc10103e
VS
4447#if defined TEST_REGEX && TEST_INTERACTIVE
4448 TestRegExInteractive();
4449#endif // defined TEST_REGEX && TEST_INTERACTIVE
07a56e45 4450
6dfec4b8 4451#ifdef TEST_REGISTRY
daa2c7d9 4452 TestRegistryRead();
6ba63600 4453 TestRegistryAssociation();
6dfec4b8
VZ
4454#endif // TEST_REGISTRY
4455
2c8e4738 4456#ifdef TEST_SOCKETS
daa2c7d9
VZ
4457 TestSocketServer();
4458 TestSocketClient();
2c8e4738
VZ
4459#endif // TEST_SOCKETS
4460
83141d3a 4461#ifdef TEST_STREAMS
e9d2bb6f 4462 #if TEST_ALL
99a5af7f 4463 TestFileStream();
e9d2bb6f 4464 #endif
99a5af7f 4465 TestMemoryStream();
83141d3a
VZ
4466#endif // TEST_STREAMS
4467
39937656
VZ
4468#ifdef TEST_TEXTSTREAM
4469 TestTextInputStream();
4470#endif // TEST_TEXTSTREAM
4471
8d5eff60
VZ
4472#ifdef TEST_THREADS
4473 int nCPUs = wxThread::GetCPUCount();
456ae26d 4474 wxPrintf(_T("This system has %d CPUs\n"), nCPUs);
8d5eff60
VZ
4475 if ( nCPUs != -1 )
4476 wxThread::SetConcurrency(nCPUs);
4477
5bc1deeb
VZ
4478 TestJoinableThreads();
4479
e9d2bb6f 4480 #if TEST_ALL
8d5eff60 4481 TestJoinableThreads();
5bc1deeb 4482 TestDetachedThreads();
8d5eff60
VZ
4483 TestThreadSuspend();
4484 TestThreadDelete();
c112e100
VZ
4485 TestThreadConditions();
4486 TestThreadExec();
7aeebdcd 4487 TestSemaphore();
e9d2bb6f 4488 #endif
8d5eff60
VZ
4489#endif // TEST_THREADS
4490
d31b7b68
VZ
4491#ifdef TEST_TIMER
4492 TestStopWatch();
549b95b3 4493 TestTimer();
d31b7b68
VZ
4494#endif // TEST_TIMER
4495
4496#ifdef TEST_DATETIME
e9d2bb6f 4497 #if TEST_ALL
9d9b7755
VZ
4498 TestTimeStatic();
4499 TestTimeRange();
4500 TestTimeZones();
9d9b7755 4501 TestTimeDST();
f6bcfd97 4502 TestTimeHolidays();
daa2c7d9 4503 TestTimeSpanFormat();
3ca6a5f0 4504 TestTimeMS();
f6bcfd97
BP
4505
4506 TestTimeZoneBug();
e9d2bb6f 4507 #endif
2b5f62a0 4508
e9d2bb6f 4509 #if TEST_INTERACTIVE
b92fd37c 4510 TestDateTimeInteractive();
e9d2bb6f 4511 #endif
d31b7b68 4512#endif // TEST_DATETIME
b76b015e 4513
df5168c4
MB
4514#ifdef TEST_SCOPEGUARD
4515 TestScopeGuard();
4516#endif
4517
eaff0f0d 4518#ifdef TEST_STACKWALKER
60c474a0 4519#if wxUSE_STACKWALKER
eaff0f0d 4520 TestStackWalk(argv[0]);
60c474a0 4521#endif
eaff0f0d
VZ
4522#endif // TEST_STACKWALKER
4523
af33b199
VZ
4524#ifdef TEST_STDPATHS
4525 TestStandardPaths();
4526#endif
4527
551fe3a6 4528#ifdef TEST_USLEEP
456ae26d 4529 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
551fe3a6
VZ
4530 wxUsleep(3000);
4531#endif // TEST_USLEEP
4532
f6bcfd97 4533#ifdef TEST_VCARD
f6bcfd97
BP
4534 TestVCardRead();
4535 TestVCardWrite();
4536#endif // TEST_VCARD
4537
0e2c5534
VZ
4538#ifdef TEST_VOLUME
4539 TestFSVolume();
4540#endif // TEST_VOLUME
4541
f6bcfd97
BP
4542#ifdef TEST_WCHAR
4543 TestUtf8();
ac511156 4544 TestEncodingConverter();
f6bcfd97
BP
4545#endif // TEST_WCHAR
4546
4547#ifdef TEST_ZIP
daa2c7d9 4548 TestZipStreamRead();
2ca8b884 4549 TestZipFileSystem();
f6bcfd97
BP
4550#endif // TEST_ZIP
4551
5ba95b79
WS
4552#if wxUSE_UNICODE
4553 {
4554 for ( int n = 0; n < argc; n++ )
4555 free(wxArgv[n]);
4556
4557 delete [] wxArgv;
4558 }
4559#endif // wxUSE_UNICODE
4560
e9d2bb6f
DS
4561 wxUnusedVar(argc);
4562 wxUnusedVar(argv);
37667812
VZ
4563 return 0;
4564}