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