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