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