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