]> git.saurik.com Git - wxWidgets.git/blame - samples/console/console.cpp
added Turkish translation
[wxWidgets.git] / samples / console / console.cpp
CommitLineData
37667812
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/console/console.cpp
3// Purpose: a sample console (as opposed to GUI) progam using wxWindows
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
VZ
21
22#if wxUSE_GUI
23 #error "This sample can't be compiled in GUI mode."
24#endif // wxUSE_GUI
25
37667812
VZ
26#include <stdio.h>
27
e84010cf
GD
28#include "wx/string.h"
29#include "wx/file.h"
30#include "wx/app.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
44 both as a test suite for various non-GUI wxWindows classes and as a
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
31f6de22 50// what to test (in alphabetic order)? uncomment the line below to do all tests
456ae26d 51#define TEST_ALL
31f6de22
VZ
52#ifdef TEST_ALL
53 #define TEST_ARRAYS
54 #define TEST_CHARSET
55 #define TEST_CMDLINE
56 #define TEST_DATETIME
57 #define TEST_DIR
58 #define TEST_DLLLOADER
59 #define TEST_ENVIRON
60 #define TEST_EXECUTE
61 #define TEST_FILE
62 #define TEST_FILECONF
63 #define TEST_FILENAME
64 #define TEST_FILETIME
65 #define TEST_FTP
66 #define TEST_HASH
0508ba2a 67 #define TEST_HASHMAP
31f6de22
VZ
68 #define TEST_INFO_FUNCTIONS
69 #define TEST_LIST
70 #define TEST_LOCALE
71 #define TEST_LOG
72 #define TEST_LONGLONG
73 #define TEST_MIME
74 #define TEST_PATHLIST
8d5eff60 75 #define TEST_ODBC
7aeebdcd 76 #define TEST_PRINTF
31f6de22
VZ
77 #define TEST_REGCONF
78 #define TEST_REGEX
79 #define TEST_REGISTRY
80 #define TEST_SNGLINST
81 #define TEST_SOCKETS
82 #define TEST_STREAMS
83 #define TEST_STRINGS
84 #define TEST_THREADS
85 #define TEST_TIMER
e7d41190 86 #define TEST_UNICODE
31f6de22 87 // #define TEST_VCARD -- don't enable this (VZ)
0e2c5534 88 #define TEST_VOLUME
31f6de22
VZ
89 #define TEST_WCHAR
90 #define TEST_ZIP
91 #define TEST_ZLIB
daa2c7d9
VZ
92
93 #undef TEST_ALL
94 static const bool TEST_ALL = TRUE;
31f6de22 95#else
7aeebdcd 96 #define TEST_PRINTF
daa2c7d9
VZ
97
98 static const bool TEST_ALL = FALSE;
31f6de22 99#endif
f6bcfd97 100
daa2c7d9
VZ
101// some tests are interactive, define this to run them
102#ifdef TEST_INTERACTIVE
103 #undef TEST_INTERACTIVE
104
19f45995 105 static const bool TEST_INTERACTIVE = TRUE;
daa2c7d9
VZ
106#else
107 static const bool TEST_INTERACTIVE = FALSE;
108#endif
58b24a56 109
f6bcfd97
BP
110// ----------------------------------------------------------------------------
111// test class for container objects
112// ----------------------------------------------------------------------------
113
114#if defined(TEST_ARRAYS) || defined(TEST_LIST)
115
116class Bar // Foo is already taken in the hash test
117{
118public:
119 Bar(const wxString& name) : m_name(name) { ms_bars++; }
3dc01741 120 Bar(const Bar& bar) : m_name(bar.m_name) { ms_bars++; }
f6bcfd97
BP
121 ~Bar() { ms_bars--; }
122
123 static size_t GetNumber() { return ms_bars; }
124
456ae26d 125 const wxChar *GetName() const { return m_name; }
f6bcfd97
BP
126
127private:
128 wxString m_name;
129
130 static size_t ms_bars;
131};
132
133size_t Bar::ms_bars = 0;
134
135#endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
e87271f3
VZ
136
137// ============================================================================
138// implementation
139// ============================================================================
140
8e907a13
VZ
141// ----------------------------------------------------------------------------
142// helper functions
143// ----------------------------------------------------------------------------
144
145#if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
146
147// replace TABs with \t and CRs with \n
148static wxString MakePrintable(const wxChar *s)
149{
150 wxString str(s);
151 (void)str.Replace(_T("\t"), _T("\\t"));
152 (void)str.Replace(_T("\n"), _T("\\n"));
153 (void)str.Replace(_T("\r"), _T("\\r"));
154
155 return str;
156}
157
158#endif // MakePrintable() is used
159
551fe3a6
VZ
160// ----------------------------------------------------------------------------
161// wxFontMapper::CharsetToEncoding
162// ----------------------------------------------------------------------------
163
164#ifdef TEST_CHARSET
165
e84010cf 166#include "wx/fontmap.h"
551fe3a6
VZ
167
168static void TestCharset()
169{
170 static const wxChar *charsets[] =
171 {
172 // some vali charsets
173 _T("us-ascii "),
174 _T("iso8859-1 "),
175 _T("iso-8859-12 "),
176 _T("koi8-r "),
177 _T("utf-7 "),
178 _T("cp1250 "),
179 _T("windows-1252"),
180
181 // and now some bogus ones
182 _T(" "),
183 _T("cp1249 "),
184 _T("iso--8859-1 "),
185 _T("iso-8859-19 "),
186 };
187
188 for ( size_t n = 0; n < WXSIZEOF(charsets); n++ )
189 {
142b3bc2 190 wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charsets[n]);
551fe3a6
VZ
191 wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"),
192 charsets[n],
142b3bc2
VS
193 wxFontMapper::Get()->GetEncodingName(enc).c_str(),
194 wxFontMapper::Get()->GetEncodingDescription(enc).c_str());
551fe3a6
VZ
195 }
196}
197
198#endif // TEST_CHARSET
199
d34bce84
VZ
200// ----------------------------------------------------------------------------
201// wxCmdLineParser
202// ----------------------------------------------------------------------------
203
d31b7b68
VZ
204#ifdef TEST_CMDLINE
205
e84010cf
GD
206#include "wx/cmdline.h"
207#include "wx/datetime.h"
456ae26d 208#include "wx/log.h"
d34bce84 209
31f6de22
VZ
210#if wxUSE_CMDLINE_PARSER
211
d34bce84
VZ
212static void ShowCmdLine(const wxCmdLineParser& parser)
213{
456ae26d 214 wxString s = _T("Input files: ");
d34bce84
VZ
215
216 size_t count = parser.GetParamCount();
217 for ( size_t param = 0; param < count; param++ )
218 {
219 s << parser.GetParam(param) << ' ';
220 }
221
222 s << '\n'
456ae26d
VZ
223 << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
224 << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
d34bce84
VZ
225
226 wxString strVal;
227 long lVal;
228 wxDateTime dt;
456ae26d
VZ
229 if ( parser.Found(_T("o"), &strVal) )
230 s << _T("Output file:\t") << strVal << '\n';
231 if ( parser.Found(_T("i"), &strVal) )
232 s << _T("Input dir:\t") << strVal << '\n';
233 if ( parser.Found(_T("s"), &lVal) )
234 s << _T("Size:\t") << lVal << '\n';
235 if ( parser.Found(_T("d"), &dt) )
236 s << _T("Date:\t") << dt.FormatISODate() << '\n';
237 if ( parser.Found(_T("project_name"), &strVal) )
238 s << _T("Project:\t") << strVal << '\n';
d34bce84
VZ
239
240 wxLogMessage(s);
241}
242
31f6de22
VZ
243#endif // wxUSE_CMDLINE_PARSER
244
245static void TestCmdLineConvert()
246{
456ae26d 247 static const wxChar *cmdlines[] =
31f6de22 248 {
456ae26d
VZ
249 _T("arg1 arg2"),
250 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
251 _T("literal \\\" and \"\""),
31f6de22
VZ
252 };
253
254 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
255 {
456ae26d
VZ
256 const wxChar *cmdline = cmdlines[n];
257 wxPrintf(_T("Parsing: %s\n"), cmdline);
31f6de22
VZ
258 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
259
260 size_t count = args.GetCount();
456ae26d 261 wxPrintf(_T("\targc = %u\n"), count);
31f6de22
VZ
262 for ( size_t arg = 0; arg < count; arg++ )
263 {
456ae26d 264 wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str());
31f6de22
VZ
265 }
266 }
267}
268
d34bce84
VZ
269#endif // TEST_CMDLINE
270
1944c6bd
VZ
271// ----------------------------------------------------------------------------
272// wxDir
273// ----------------------------------------------------------------------------
274
275#ifdef TEST_DIR
276
e84010cf 277#include "wx/dir.h"
1944c6bd 278
35332784
VZ
279#ifdef __UNIX__
280 static const wxChar *ROOTDIR = _T("/");
281 static const wxChar *TESTDIR = _T("/usr");
282#elif defined(__WXMSW__)
283 static const wxChar *ROOTDIR = _T("c:\\");
284 static const wxChar *TESTDIR = _T("d:\\");
285#else
286 #error "don't know where the root directory is"
287#endif
288
1944c6bd
VZ
289static void TestDirEnumHelper(wxDir& dir,
290 int flags = wxDIR_DEFAULT,
291 const wxString& filespec = wxEmptyString)
292{
293 wxString filename;
294
295 if ( !dir.IsOpened() )
296 return;
297
298 bool cont = dir.GetFirst(&filename, filespec, flags);
299 while ( cont )
300 {
456ae26d 301 wxPrintf(_T("\t%s\n"), filename.c_str());
1944c6bd
VZ
302
303 cont = dir.GetNext(&filename);
304 }
305
456ae26d 306 wxPuts(_T(""));
1944c6bd
VZ
307}
308
309static void TestDirEnum()
310{
456ae26d 311 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
35332784 312
149147e1 313 wxString cwd = wxGetCwd();
9475670f 314 if ( !wxDir::Exists(cwd) )
149147e1 315 {
456ae26d 316 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
149147e1
VZ
317 return;
318 }
319
ee3ef281 320 wxDir dir(cwd);
149147e1
VZ
321 if ( !dir.IsOpened() )
322 {
456ae26d 323 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
149147e1
VZ
324 return;
325 }
1944c6bd 326
456ae26d 327 wxPuts(_T("Enumerating everything in current directory:"));
1944c6bd
VZ
328 TestDirEnumHelper(dir);
329
456ae26d 330 wxPuts(_T("Enumerating really everything in current directory:"));
1944c6bd
VZ
331 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
332
456ae26d 333 wxPuts(_T("Enumerating object files in current directory:"));
ee3ef281 334 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o*");
1944c6bd 335
456ae26d 336 wxPuts(_T("Enumerating directories in current directory:"));
1944c6bd
VZ
337 TestDirEnumHelper(dir, wxDIR_DIRS);
338
456ae26d 339 wxPuts(_T("Enumerating files in current directory:"));
1944c6bd
VZ
340 TestDirEnumHelper(dir, wxDIR_FILES);
341
456ae26d 342 wxPuts(_T("Enumerating files including hidden in current directory:"));
1944c6bd
VZ
343 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
344
35332784 345 dir.Open(ROOTDIR);
1944c6bd 346
456ae26d 347 wxPuts(_T("Enumerating everything in root directory:"));
1944c6bd
VZ
348 TestDirEnumHelper(dir, wxDIR_DEFAULT);
349
456ae26d 350 wxPuts(_T("Enumerating directories in root directory:"));
1944c6bd
VZ
351 TestDirEnumHelper(dir, wxDIR_DIRS);
352
456ae26d 353 wxPuts(_T("Enumerating files in root directory:"));
1944c6bd
VZ
354 TestDirEnumHelper(dir, wxDIR_FILES);
355
456ae26d 356 wxPuts(_T("Enumerating files including hidden in root directory:"));
1944c6bd
VZ
357 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
358
456ae26d 359 wxPuts(_T("Enumerating files in non existing directory:"));
1944c6bd
VZ
360 wxDir dirNo("nosuchdir");
361 TestDirEnumHelper(dirNo);
362}
363
35332784
VZ
364class DirPrintTraverser : public wxDirTraverser
365{
366public:
367 virtual wxDirTraverseResult OnFile(const wxString& filename)
368 {
369 return wxDIR_CONTINUE;
370 }
371
372 virtual wxDirTraverseResult OnDir(const wxString& dirname)
373 {
374 wxString path, name, ext;
375 wxSplitPath(dirname, &path, &name, &ext);
376
377 if ( !ext.empty() )
378 name << _T('.') << ext;
379
380 wxString indent;
381 for ( const wxChar *p = path.c_str(); *p; p++ )
382 {
383 if ( wxIsPathSeparator(*p) )
384 indent += _T(" ");
385 }
386
456ae26d 387 wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str());
35332784
VZ
388
389 return wxDIR_CONTINUE;
390 }
391};
392
393static void TestDirTraverse()
394{
456ae26d 395 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
35332784
VZ
396
397 // enum all files
398 wxArrayString files;
399 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
456ae26d 400 wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR);
35332784
VZ
401 if ( n > 1 )
402 {
456ae26d
VZ
403 wxPrintf(_T("First one is '%s'\n"), files[0u].c_str());
404 wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str());
35332784
VZ
405 }
406
407 // enum again with custom traverser
408 wxDir dir(TESTDIR);
409 DirPrintTraverser traverser;
410 dir.Traverse(traverser, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
411}
412
149147e1
VZ
413static void TestDirExists()
414{
415 wxPuts(_T("*** Testing wxDir::Exists() ***"));
416
456ae26d 417 static const wxChar *dirnames[] =
149147e1
VZ
418 {
419 _T("."),
420#if defined(__WXMSW__)
421 _T("c:"),
422 _T("c:\\"),
423 _T("\\\\share\\file"),
424 _T("c:\\dos"),
425 _T("c:\\dos\\"),
426 _T("c:\\dos\\\\"),
427 _T("c:\\autoexec.bat"),
428#elif defined(__UNIX__)
429 _T("/"),
430 _T("//"),
431 _T("/usr/bin"),
432 _T("/usr//bin"),
433 _T("/usr///bin"),
434#endif
435 };
436
437 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
438 {
456ae26d
VZ
439 wxPrintf(_T("%-40s: %s\n"),
440 dirnames[n],
441 wxDir::Exists(dirnames[n]) ? _T("exists")
442 : _T("doesn't exist"));
149147e1
VZ
443 }
444}
445
1944c6bd
VZ
446#endif // TEST_DIR
447
f6bcfd97
BP
448// ----------------------------------------------------------------------------
449// wxDllLoader
450// ----------------------------------------------------------------------------
451
452#ifdef TEST_DLLLOADER
453
e84010cf 454#include "wx/dynlib.h"
f6bcfd97
BP
455
456static void TestDllLoad()
457{
458#if defined(__WXMSW__)
459 static const wxChar *LIB_NAME = _T("kernel32.dll");
460 static const wxChar *FUNC_NAME = _T("lstrlenA");
461#elif defined(__UNIX__)
462 // weird: using just libc.so does *not* work!
463 static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so");
464 static const wxChar *FUNC_NAME = _T("strlen");
465#else
466 #error "don't know how to test wxDllLoader on this platform"
467#endif
468
456ae26d 469 wxPuts(_T("*** testing wxDllLoader ***\n"));
f6bcfd97 470
456ae26d
VZ
471 wxDynamicLibrary lib(LIB_NAME);
472 if ( !lib.IsLoaded() )
f6bcfd97
BP
473 {
474 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
475 }
476 else
477 {
456ae26d
VZ
478 typedef int (*wxStrlenType)(const char *);
479 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
f6bcfd97
BP
480 if ( !pfnStrlen )
481 {
482 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
483 FUNC_NAME, LIB_NAME);
484 }
485 else
486 {
487 if ( pfnStrlen("foo") != 3 )
488 {
456ae26d 489 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
f6bcfd97
BP
490 }
491 else
492 {
456ae26d 493 wxPuts(_T("... ok"));
f6bcfd97
BP
494 }
495 }
f6bcfd97
BP
496 }
497}
498
499#endif // TEST_DLLLOADER
500
8fd0d89b
VZ
501// ----------------------------------------------------------------------------
502// wxGet/SetEnv
503// ----------------------------------------------------------------------------
504
505#ifdef TEST_ENVIRON
506
e84010cf 507#include "wx/utils.h"
8fd0d89b 508
308978f6
VZ
509static wxString MyGetEnv(const wxString& var)
510{
511 wxString val;
512 if ( !wxGetEnv(var, &val) )
513 val = _T("<empty>");
514 else
515 val = wxString(_T('\'')) + val + _T('\'');
516
517 return val;
518}
519
8fd0d89b
VZ
520static void TestEnvironment()
521{
522 const wxChar *var = _T("wxTestVar");
523
456ae26d 524 wxPuts(_T("*** testing environment access functions ***"));
8fd0d89b 525
456ae26d 526 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 527 wxSetEnv(var, _T("value for wxTestVar"));
456ae26d 528 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 529 wxSetEnv(var, _T("another value"));
456ae26d 530 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
8fd0d89b 531 wxUnsetEnv(var);
456ae26d
VZ
532 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
533 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
8fd0d89b
VZ
534}
535
536#endif // TEST_ENVIRON
537
d93c719a
VZ
538// ----------------------------------------------------------------------------
539// wxExecute
540// ----------------------------------------------------------------------------
541
542#ifdef TEST_EXECUTE
543
e84010cf 544#include "wx/utils.h"
d93c719a
VZ
545
546static void TestExecute()
547{
456ae26d 548 wxPuts(_T("*** testing wxExecute ***"));
d93c719a
VZ
549
550#ifdef __UNIX__
a1f79c1e 551 #define COMMAND "cat -n ../../Makefile" // "echo hi"
2c8e4738 552 #define SHELL_COMMAND "echo hi from shell"
a1f79c1e 553 #define REDIRECT_COMMAND COMMAND // "date"
d93c719a 554#elif defined(__WXMSW__)
50ded68d 555 #define COMMAND "command.com /c echo hi"
2c8e4738
VZ
556 #define SHELL_COMMAND "echo hi"
557 #define REDIRECT_COMMAND COMMAND
d93c719a
VZ
558#else
559 #error "no command to exec"
560#endif // OS
561
456ae26d 562 wxPrintf(_T("Testing wxShell: "));
2c8e4738
VZ
563 fflush(stdout);
564 if ( wxShell(SHELL_COMMAND) )
456ae26d 565 wxPuts(_T("Ok."));
d93c719a 566 else
456ae26d 567 wxPuts(_T("ERROR."));
2c8e4738 568
456ae26d 569 wxPrintf(_T("Testing wxExecute: "));
2c8e4738
VZ
570 fflush(stdout);
571 if ( wxExecute(COMMAND, TRUE /* sync */) == 0 )
456ae26d 572 wxPuts(_T("Ok."));
2c8e4738 573 else
456ae26d 574 wxPuts(_T("ERROR."));
2c8e4738
VZ
575
576#if 0 // no, it doesn't work (yet?)
456ae26d 577 wxPrintf(_T("Testing async wxExecute: "));
2c8e4738
VZ
578 fflush(stdout);
579 if ( wxExecute(COMMAND) != 0 )
456ae26d 580 wxPuts(_T("Ok (command launched)."));
2c8e4738 581 else
456ae26d 582 wxPuts(_T("ERROR."));
2c8e4738
VZ
583#endif // 0
584
456ae26d 585 wxPrintf(_T("Testing wxExecute with redirection:\n"));
2c8e4738
VZ
586 wxArrayString output;
587 if ( wxExecute(REDIRECT_COMMAND, output) != 0 )
588 {
456ae26d 589 wxPuts(_T("ERROR."));
2c8e4738
VZ
590 }
591 else
592 {
593 size_t count = output.GetCount();
594 for ( size_t n = 0; n < count; n++ )
595 {
456ae26d 596 wxPrintf(_T("\t%s\n"), output[n].c_str());
2c8e4738
VZ
597 }
598
456ae26d 599 wxPuts(_T("Ok."));
2c8e4738 600 }
d93c719a
VZ
601}
602
603#endif // TEST_EXECUTE
604
f6bcfd97
BP
605// ----------------------------------------------------------------------------
606// file
607// ----------------------------------------------------------------------------
608
609#ifdef TEST_FILE
610
e84010cf
GD
611#include "wx/file.h"
612#include "wx/ffile.h"
613#include "wx/textfile.h"
f6bcfd97
BP
614
615static void TestFileRead()
616{
456ae26d 617 wxPuts(_T("*** wxFile read test ***"));
f6bcfd97
BP
618
619 wxFile file(_T("testdata.fc"));
620 if ( file.IsOpened() )
621 {
456ae26d 622 wxPrintf(_T("File length: %lu\n"), file.Length());
f6bcfd97 623
456ae26d 624 wxPuts(_T("File dump:\n----------"));
f6bcfd97 625
3ca6a5f0 626 static const off_t len = 1024;
456ae26d 627 wxChar buf[len];
f6bcfd97
BP
628 for ( ;; )
629 {
630 off_t nRead = file.Read(buf, len);
631 if ( nRead == wxInvalidOffset )
632 {
456ae26d 633 wxPrintf(_T("Failed to read the file."));
f6bcfd97
BP
634 break;
635 }
636
637 fwrite(buf, nRead, 1, stdout);
638
639 if ( nRead < len )
640 break;
641 }
642
456ae26d 643 wxPuts(_T("----------"));
f6bcfd97
BP
644 }
645 else
646 {
456ae26d 647 wxPrintf(_T("ERROR: can't open test file.\n"));
f6bcfd97
BP
648 }
649
456ae26d 650 wxPuts(_T(""));
f6bcfd97
BP
651}
652
653static void TestTextFileRead()
654{
456ae26d 655 wxPuts(_T("*** wxTextFile read test ***"));
f6bcfd97
BP
656
657 wxTextFile file(_T("testdata.fc"));
658 if ( file.Open() )
659 {
456ae26d
VZ
660 wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount());
661 wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str());
3ca6a5f0
BP
662
663 wxString s;
664
456ae26d 665 wxPuts(_T("\nDumping the entire file:"));
3ca6a5f0
BP
666 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
667 {
456ae26d 668 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 669 }
456ae26d 670 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 671
456ae26d 672 wxPuts(_T("\nAnd now backwards:"));
3ca6a5f0
BP
673 for ( s = file.GetLastLine();
674 file.GetCurrentLine() != 0;
675 s = file.GetPrevLine() )
676 {
456ae26d 677 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
3ca6a5f0 678 }
456ae26d 679 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
f6bcfd97
BP
680 }
681 else
682 {
456ae26d 683 wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName());
f6bcfd97
BP
684 }
685
456ae26d 686 wxPuts(_T(""));
f6bcfd97
BP
687}
688
a339970a
VZ
689static void TestFileCopy()
690{
456ae26d 691 wxPuts(_T("*** Testing wxCopyFile ***"));
a339970a
VZ
692
693 static const wxChar *filename1 = _T("testdata.fc");
694 static const wxChar *filename2 = _T("test2");
695 if ( !wxCopyFile(filename1, filename2) )
696 {
456ae26d 697 wxPuts(_T("ERROR: failed to copy file"));
a339970a
VZ
698 }
699 else
700 {
701 wxFFile f1(filename1, "rb"),
702 f2(filename2, "rb");
703
704 if ( !f1.IsOpened() || !f2.IsOpened() )
705 {
456ae26d 706 wxPuts(_T("ERROR: failed to open file(s)"));
a339970a
VZ
707 }
708 else
709 {
710 wxString s1, s2;
711 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
712 {
456ae26d 713 wxPuts(_T("ERROR: failed to read file(s)"));
a339970a
VZ
714 }
715 else
716 {
717 if ( (s1.length() != s2.length()) ||
718 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
719 {
456ae26d 720 wxPuts(_T("ERROR: copy error!"));
a339970a
VZ
721 }
722 else
723 {
456ae26d 724 wxPuts(_T("File was copied ok."));
a339970a
VZ
725 }
726 }
727 }
728 }
729
730 if ( !wxRemoveFile(filename2) )
731 {
456ae26d 732 wxPuts(_T("ERROR: failed to remove the file"));
a339970a
VZ
733 }
734
456ae26d 735 wxPuts(_T(""));
a339970a
VZ
736}
737
f6bcfd97
BP
738#endif // TEST_FILE
739
ee6e1b1d
VZ
740// ----------------------------------------------------------------------------
741// wxFileConfig
742// ----------------------------------------------------------------------------
743
744#ifdef TEST_FILECONF
745
e84010cf
GD
746#include "wx/confbase.h"
747#include "wx/fileconf.h"
ee6e1b1d
VZ
748
749static const struct FileConfTestData
750{
751 const wxChar *name; // value name
752 const wxChar *value; // the value from the file
753} fcTestData[] =
754{
755 { _T("value1"), _T("one") },
756 { _T("value2"), _T("two") },
757 { _T("novalue"), _T("default") },
758};
759
760static void TestFileConfRead()
761{
456ae26d 762 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
ee6e1b1d
VZ
763
764 wxFileConfig fileconf(_T("test"), wxEmptyString,
765 _T("testdata.fc"), wxEmptyString,
766 wxCONFIG_USE_RELATIVE_PATH);
767
768 // test simple reading
456ae26d 769 wxPuts(_T("\nReading config file:"));
ee6e1b1d
VZ
770 wxString defValue(_T("default")), value;
771 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
772 {
773 const FileConfTestData& data = fcTestData[n];
774 value = fileconf.Read(data.name, defValue);
456ae26d 775 wxPrintf(_T("\t%s = %s "), data.name, value.c_str());
ee6e1b1d
VZ
776 if ( value == data.value )
777 {
456ae26d 778 wxPuts(_T("(ok)"));
ee6e1b1d
VZ
779 }
780 else
781 {
456ae26d 782 wxPrintf(_T("(ERROR: should be %s)\n"), data.value);
ee6e1b1d
VZ
783 }
784 }
785
786 // test enumerating the entries
456ae26d 787 wxPuts(_T("\nEnumerating all root entries:"));
ee6e1b1d
VZ
788 long dummy;
789 wxString name;
790 bool cont = fileconf.GetFirstEntry(name, dummy);
791 while ( cont )
792 {
456ae26d 793 wxPrintf(_T("\t%s = %s\n"),
ee6e1b1d
VZ
794 name.c_str(),
795 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
796
797 cont = fileconf.GetNextEntry(name, dummy);
798 }
799}
800
801#endif // TEST_FILECONF
802
844f90fb
VZ
803// ----------------------------------------------------------------------------
804// wxFileName
805// ----------------------------------------------------------------------------
806
807#ifdef TEST_FILENAME
808
e84010cf 809#include "wx/filename.h"
844f90fb 810
81f25632
VZ
811static void DumpFileName(const wxFileName& fn)
812{
813 wxString full = fn.GetFullPath();
814
815 wxString vol, path, name, ext;
816 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
817
a5b7374f 818 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
81f25632 819 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
a5b7374f
VZ
820
821 wxFileName::SplitPath(full, &path, &name, &ext);
822 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
823 path.c_str(), name.c_str(), ext.c_str());
824
825 wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str());
826 wxPrintf(_T("with volume: \t'%s'\n"),
827 fn.GetPath(wxPATH_GET_VOLUME).c_str());
828 wxPrintf(_T("with separator:\t'%s'\n"),
829 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
830 wxPrintf(_T("with both: \t'%s'\n"),
831 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
9cb47ea2
VZ
832
833 wxPuts(_T("The directories in the path are:"));
834 wxArrayString dirs = fn.GetDirs();
835 size_t count = dirs.GetCount();
836 for ( size_t n = 0; n < count; n++ )
837 {
838 wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str());
839 }
81f25632
VZ
840}
841
8e7dda21 842static struct FileNameInfo
42b1f941 843{
8e7dda21 844 const wxChar *fullname;
a874db92 845 const wxChar *volume;
8e7dda21
VZ
846 const wxChar *path;
847 const wxChar *name;
848 const wxChar *ext;
a874db92
VZ
849 bool isAbsolute;
850 wxPathFormat format;
8e7dda21
VZ
851} filenames[] =
852{
a874db92
VZ
853 // Unix file names
854 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), TRUE, wxPATH_UNIX },
855 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), TRUE, wxPATH_UNIX },
856 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), TRUE, wxPATH_UNIX },
857 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), FALSE, wxPATH_UNIX },
858 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
859 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), TRUE, wxPATH_UNIX },
860 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), TRUE, wxPATH_UNIX },
861 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
862 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), TRUE, wxPATH_UNIX },
863
864 // Windows file names
865 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
866 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
867 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
868 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
869 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), TRUE, wxPATH_DOS },
870 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
6307da56 871 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
a874db92 872
2db991f4 873 // wxFileName support for Mac file names is broken currently
a2fa5040 874#if 0
a874db92
VZ
875 // Mac file names
876 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
daa2c7d9
VZ
877 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
878 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), TRUE, wxPATH_MAC },
a874db92 879 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), FALSE, wxPATH_MAC },
daa2c7d9
VZ
880 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
881 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
a2fa5040 882#endif // 0
a874db92
VZ
883
884 // VMS file names
885 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), TRUE, wxPATH_VMS },
886 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), FALSE, wxPATH_VMS },
42b1f941
VZ
887};
888
844f90fb
VZ
889static void TestFileNameConstruction()
890{
456ae26d 891 wxPuts(_T("*** testing wxFileName construction ***"));
844f90fb 892
844f90fb
VZ
893 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
894 {
a874db92
VZ
895 const FileNameInfo& fni = filenames[n];
896
897 wxFileName fn(fni.fullname, fni.format);
898
899 wxString fullname = fn.GetFullPath(fni.format);
900 if ( fullname != fni.fullname )
901 {
456ae26d 902 wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni.fullname);
a874db92 903 }
844f90fb 904
a2fa5040 905 bool isAbsolute = fn.IsAbsolute(fni.format);
456ae26d 906 wxPrintf(_T("'%s' is %s (%s)\n\t"),
a874db92
VZ
907 fullname.c_str(),
908 isAbsolute ? "absolute" : "relative",
909 isAbsolute == fni.isAbsolute ? "ok" : "ERROR");
910
911 if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) )
844f90fb 912 {
456ae26d 913 wxPuts(_T("ERROR (couldn't be normalized)"));
844f90fb
VZ
914 }
915 else
916 {
456ae26d 917 wxPrintf(_T("normalized: '%s'\n"), fn.GetFullPath(fni.format).c_str());
844f90fb
VZ
918 }
919 }
920
456ae26d 921 wxPuts(_T(""));
844f90fb
VZ
922}
923
42b1f941
VZ
924static void TestFileNameSplit()
925{
456ae26d 926 wxPuts(_T("*** testing wxFileName splitting ***"));
42b1f941
VZ
927
928 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
929 {
a874db92
VZ
930 const FileNameInfo& fni = filenames[n];
931 wxString volume, path, name, ext;
932 wxFileName::SplitPath(fni.fullname,
933 &volume, &path, &name, &ext, fni.format);
934
456ae26d 935 wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"),
a874db92
VZ
936 fni.fullname,
937 volume.c_str(), path.c_str(), name.c_str(), ext.c_str());
8e7dda21 938
a874db92 939 if ( volume != fni.volume )
456ae26d 940 wxPrintf(_T(" (ERROR: volume = '%s')"), fni.volume);
8e7dda21 941 if ( path != fni.path )
456ae26d 942 wxPrintf(_T(" (ERROR: path = '%s')"), fni.path);
8e7dda21 943 if ( name != fni.name )
456ae26d 944 wxPrintf(_T(" (ERROR: name = '%s')"), fni.name);
8e7dda21 945 if ( ext != fni.ext )
456ae26d 946 wxPrintf(_T(" (ERROR: ext = '%s')"), fni.ext);
a874db92 947
456ae26d 948 wxPuts(_T(""));
42b1f941 949 }
42b1f941
VZ
950}
951
ade35f11
VZ
952static void TestFileNameTemp()
953{
456ae26d 954 wxPuts(_T("*** testing wxFileName temp file creation ***"));
ade35f11 955
456ae26d 956 static const wxChar *tmpprefixes[] =
ade35f11 957 {
456ae26d
VZ
958 _T(""),
959 _T("foo"),
960 _T(".."),
961 _T("../bar"),
a2fa5040 962#ifdef __UNIX__
456ae26d
VZ
963 _T("/tmp/foo"),
964 _T("/tmp/foo/bar"), // this one must be an error
a2fa5040 965#endif // __UNIX__
ade35f11
VZ
966 };
967
968 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
969 {
970 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
2db991f4
VZ
971 if ( path.empty() )
972 {
973 // "error" is not in upper case because it may be ok
456ae26d 974 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
2db991f4
VZ
975 }
976 else
ade35f11 977 {
456ae26d 978 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
ade35f11
VZ
979 tmpprefixes[n], path.c_str());
980
981 if ( !wxRemoveFile(path) )
982 {
456ae26d
VZ
983 wxLogWarning(_T("Failed to remove temp file '%s'"),
984 path.c_str());
ade35f11
VZ
985 }
986 }
987 }
988}
989
f7d886af
VZ
990static void TestFileNameMakeRelative()
991{
456ae26d 992 wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***"));
f7d886af
VZ
993
994 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
995 {
996 const FileNameInfo& fni = filenames[n];
997
998 wxFileName fn(fni.fullname, fni.format);
999
1000 // choose the base dir of the same format
1001 wxString base;
1002 switch ( fni.format )
1003 {
1004 case wxPATH_UNIX:
1005 base = "/usr/bin/";
1006 break;
1007
1008 case wxPATH_DOS:
1009 base = "c:\\";
1010 break;
1011
1012 case wxPATH_MAC:
1013 case wxPATH_VMS:
1014 // TODO: I don't know how this is supposed to work there
1015 continue;
daa2c7d9
VZ
1016
1017 case wxPATH_NATIVE: // make gcc happy
1018 default:
1019 wxFAIL_MSG( "unexpected path format" );
f7d886af
VZ
1020 }
1021
456ae26d 1022 wxPrintf(_T("'%s' relative to '%s': "),
f7d886af
VZ
1023 fn.GetFullPath(fni.format).c_str(), base.c_str());
1024
1025 if ( !fn.MakeRelativeTo(base, fni.format) )
1026 {
456ae26d 1027 wxPuts(_T("unchanged"));
f7d886af
VZ
1028 }
1029 else
1030 {
456ae26d 1031 wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str());
f7d886af
VZ
1032 }
1033 }
1034}
1035
844f90fb
VZ
1036static void TestFileNameComparison()
1037{
1038 // TODO!
1039}
1040
1041static void TestFileNameOperations()
1042{
1043 // TODO!
1044}
1045
1046static void TestFileNameCwd()
1047{
1048 // TODO!
1049}
1050
1051#endif // TEST_FILENAME
1052
d56e2b97
VZ
1053// ----------------------------------------------------------------------------
1054// wxFileName time functions
1055// ----------------------------------------------------------------------------
1056
1057#ifdef TEST_FILETIME
1058
1059#include <wx/filename.h>
1060#include <wx/datetime.h>
1061
1062static void TestFileGetTimes()
1063{
1064 wxFileName fn(_T("testdata.fc"));
1065
6dbb903b
VZ
1066 wxDateTime dtAccess, dtMod, dtCreate;
1067 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
d56e2b97
VZ
1068 {
1069 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
1070 }
1071 else
1072 {
1073 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
1074
1075 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
6dbb903b
VZ
1076 wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
1077 wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
1078 wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
d56e2b97
VZ
1079 }
1080}
1081
1082static void TestFileSetTimes()
1083{
1084 wxFileName fn(_T("testdata.fc"));
1085
d56e2b97
VZ
1086 if ( !fn.Touch() )
1087 {
1088 wxPrintf(_T("ERROR: Touch() failed.\n"));
1089 }
1090}
1091
1092#endif // TEST_FILETIME
1093
2c8e4738
VZ
1094// ----------------------------------------------------------------------------
1095// wxHashTable
1096// ----------------------------------------------------------------------------
1097
1098#ifdef TEST_HASH
1099
e84010cf 1100#include "wx/hash.h"
2c8e4738
VZ
1101
1102struct Foo
1103{
1104 Foo(int n_) { n = n_; count++; }
1105 ~Foo() { count--; }
1106
1107 int n;
1108
1109 static size_t count;
1110};
1111
1112size_t Foo::count = 0;
1113
1114WX_DECLARE_LIST(Foo, wxListFoos);
1115WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos);
1116
e84010cf 1117#include "wx/listimpl.cpp"
2c8e4738
VZ
1118
1119WX_DEFINE_LIST(wxListFoos);
1120
1121static void TestHash()
1122{
456ae26d 1123 wxPuts(_T("*** Testing wxHashTable ***\n"));
2c8e4738
VZ
1124
1125 {
1126 wxHashFoos hash;
1127 hash.DeleteContents(TRUE);
1128
456ae26d 1129 wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
2c8e4738
VZ
1130 hash.GetCount(), Foo::count);
1131
1132 static const int hashTestData[] =
1133 {
1134 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
1135 };
1136
1137 size_t n;
1138 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
1139 {
1140 hash.Put(hashTestData[n], n, new Foo(n));
1141 }
1142
456ae26d 1143 wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"),
2c8e4738
VZ
1144 hash.GetCount(), Foo::count);
1145
456ae26d 1146 wxPuts(_T("Hash access test:"));
2c8e4738
VZ
1147 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
1148 {
456ae26d 1149 wxPrintf(_T("\tGetting element with key %d, value %d: "),
2c8e4738
VZ
1150 hashTestData[n], n);
1151 Foo *foo = hash.Get(hashTestData[n], n);
1152 if ( !foo )
1153 {
456ae26d 1154 wxPrintf(_T("ERROR, not found.\n"));
2c8e4738
VZ
1155 }
1156 else
1157 {
456ae26d 1158 wxPrintf(_T("%d (%s)\n"), foo->n,
2c8e4738
VZ
1159 (size_t)foo->n == n ? "ok" : "ERROR");
1160 }
1161 }
1162
456ae26d 1163 wxPrintf(_T("\nTrying to get an element not in hash: "));
2c8e4738
VZ
1164
1165 if ( hash.Get(1234) || hash.Get(1, 0) )
1166 {
456ae26d 1167 wxPuts(_T("ERROR: found!"));
2c8e4738
VZ
1168 }
1169 else
1170 {
456ae26d 1171 wxPuts(_T("ok (not found)"));
2c8e4738
VZ
1172 }
1173 }
1174
456ae26d 1175 wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count);
2c8e4738
VZ
1176}
1177
1178#endif // TEST_HASH
1179
0508ba2a
MB
1180// ----------------------------------------------------------------------------
1181// wxHashMap
1182// ----------------------------------------------------------------------------
1183
1184#ifdef TEST_HASHMAP
1185
1186#include "wx/hashmap.h"
1187
1188// test compilation of basic map types
1189WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash, wxPointerEqual, myPtrHashMap );
1190WX_DECLARE_HASH_MAP( long, long, wxIntegerHash, wxIntegerEqual, myLongHashMap );
1191WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash, wxIntegerEqual,
1192 myUnsignedHashMap );
1193WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash, wxIntegerEqual,
1194 myTestHashMap1 );
1195WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash, wxIntegerEqual,
1196 myTestHashMap2 );
1197WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash, wxIntegerEqual,
1198 myTestHashMap3 );
1199WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash, wxIntegerEqual,
1200 myTestHashMap4 );
60ce696e
VZ
1201
1202// same as:
1203// WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual,
1204// myStringHashMap );
1205WX_DECLARE_STRING_HASH_MAP(wxString, myStringHashMap);
0508ba2a
MB
1206
1207typedef myStringHashMap::iterator Itor;
1208
1209static void TestHashMap()
1210{
456ae26d 1211 wxPuts(_T("*** Testing wxHashMap ***\n"));
0508ba2a
MB
1212 myStringHashMap sh(0); // as small as possible
1213 wxString buf;
1214 size_t i;
1215 const size_t count = 10000;
1216
1217 // init with some data
1218 for( i = 0; i < count; ++i )
1219 {
1220 buf.Printf(wxT("%d"), i );
1221 sh[buf] = wxT("A") + buf + wxT("C");
1222 }
1223
1224 // test that insertion worked
1225 if( sh.size() != count )
1226 {
456ae26d 1227 wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh.size(), count);
0508ba2a
MB
1228 }
1229
1230 for( i = 0; i < count; ++i )
1231 {
1232 buf.Printf(wxT("%d"), i );
1233 if( sh[buf] != wxT("A") + buf + wxT("C") )
1234 {
456ae26d 1235 wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"));
0508ba2a
MB
1236 return;
1237 }
1238 }
1239
1240 // check that iterators work
1241 Itor it;
1242 for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i )
1243 {
1244 if( i == count )
1245 {
456ae26d 1246 wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"));
0508ba2a
MB
1247 return;
1248 }
1249
1250 if( it->second != sh[it->first] )
1251 {
456ae26d 1252 wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"));
0508ba2a
MB
1253 return;
1254 }
1255 }
1256
1257 if( sh.size() != i )
1258 {
456ae26d 1259 wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i, count);
0508ba2a
MB
1260 }
1261
1262 // test copy ctor, assignment operator
1263 myStringHashMap h1( sh ), h2( 0 );
1264 h2 = sh;
1265
1266 for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i )
1267 {
1268 if( h1[it->first] != it->second )
1269 {
456ae26d 1270 wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it->first.c_str());
0508ba2a
MB
1271 }
1272
1273 if( h2[it->first] != it->second )
1274 {
456ae26d 1275 wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it->first.c_str());
0508ba2a
MB
1276 }
1277 }
1278
1279 // other tests
1280 for( i = 0; i < count; ++i )
1281 {
1282 buf.Printf(wxT("%d"), i );
1283 size_t sz = sh.size();
1284
1285 // test find() and erase(it)
1286 if( i < 100 )
1287 {
1288 it = sh.find( buf );
1289 if( it != sh.end() )
1290 {
1291 sh.erase( it );
1292
1293 if( sh.find( buf ) != sh.end() )
1294 {
456ae26d 1295 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i);
0508ba2a
MB
1296 }
1297 }
1298 else
456ae26d 1299 wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i);
0508ba2a
MB
1300 }
1301 else
1302 // test erase(key)
1303 {
1304 size_t c = sh.erase( buf );
1305 if( c != 1 )
456ae26d 1306 wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n"));
0508ba2a
MB
1307
1308 if( sh.find( buf ) != sh.end() )
1309 {
456ae26d 1310 wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i);
0508ba2a
MB
1311 }
1312 }
1313
1314 // count should decrease
1315 if( sh.size() != sz - 1 )
1316 {
456ae26d 1317 wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n"));
0508ba2a
MB
1318 }
1319 }
1320
456ae26d 1321 wxPrintf(_T("*** Finished testing wxHashMap ***\n"));
0508ba2a
MB
1322}
1323
60ce696e 1324#endif // TEST_HASHMAP
0508ba2a 1325
f6bcfd97
BP
1326// ----------------------------------------------------------------------------
1327// wxList
1328// ----------------------------------------------------------------------------
1329
1330#ifdef TEST_LIST
1331
e84010cf 1332#include "wx/list.h"
f6bcfd97
BP
1333
1334WX_DECLARE_LIST(Bar, wxListBars);
e84010cf 1335#include "wx/listimpl.cpp"
f6bcfd97
BP
1336WX_DEFINE_LIST(wxListBars);
1337
1338static void TestListCtor()
1339{
456ae26d 1340 wxPuts(_T("*** Testing wxList construction ***\n"));
f6bcfd97
BP
1341
1342 {
1343 wxListBars list1;
1344 list1.Append(new Bar(_T("first")));
1345 list1.Append(new Bar(_T("second")));
1346
456ae26d 1347 wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"),
f6bcfd97
BP
1348 list1.GetCount(), Bar::GetNumber());
1349
1350 wxListBars list2;
1351 list2 = list1;
1352
456ae26d 1353 wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
f6bcfd97
BP
1354 list1.GetCount(), list2.GetCount(), Bar::GetNumber());
1355
1356 list1.DeleteContents(TRUE);
1357 }
1358
456ae26d 1359 wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
f6bcfd97
BP
1360}
1361
1362#endif // TEST_LIST
1363
ec37df57
VZ
1364// ----------------------------------------------------------------------------
1365// wxLocale
1366// ----------------------------------------------------------------------------
1367
1368#ifdef TEST_LOCALE
1369
1370#include "wx/intl.h"
1371#include "wx/utils.h" // for wxSetEnv
1372
1373static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH);
1374
1375// find the name of the language from its value
456ae26d
VZ
1376static const wxChar *GetLangName(int lang)
1377{
1378 static const wxChar *languageNames[] =
1379 {
1380 _T("DEFAULT"),
1381 _T("UNKNOWN"),
1382 _T("ABKHAZIAN"),
1383 _T("AFAR"),
1384 _T("AFRIKAANS"),
1385 _T("ALBANIAN"),
1386 _T("AMHARIC"),
1387 _T("ARABIC"),
1388 _T("ARABIC_ALGERIA"),
1389 _T("ARABIC_BAHRAIN"),
1390 _T("ARABIC_EGYPT"),
1391 _T("ARABIC_IRAQ"),
1392 _T("ARABIC_JORDAN"),
1393 _T("ARABIC_KUWAIT"),
1394 _T("ARABIC_LEBANON"),
1395 _T("ARABIC_LIBYA"),
1396 _T("ARABIC_MOROCCO"),
1397 _T("ARABIC_OMAN"),
1398 _T("ARABIC_QATAR"),
1399 _T("ARABIC_SAUDI_ARABIA"),
1400 _T("ARABIC_SUDAN"),
1401 _T("ARABIC_SYRIA"),
1402 _T("ARABIC_TUNISIA"),
1403 _T("ARABIC_UAE"),
1404 _T("ARABIC_YEMEN"),
1405 _T("ARMENIAN"),
1406 _T("ASSAMESE"),
1407 _T("AYMARA"),
1408 _T("AZERI"),
1409 _T("AZERI_CYRILLIC"),
1410 _T("AZERI_LATIN"),
1411 _T("BASHKIR"),
1412 _T("BASQUE"),
1413 _T("BELARUSIAN"),
1414 _T("BENGALI"),
1415 _T("BHUTANI"),
1416 _T("BIHARI"),
1417 _T("BISLAMA"),
1418 _T("BRETON"),
1419 _T("BULGARIAN"),
1420 _T("BURMESE"),
1421 _T("CAMBODIAN"),
1422 _T("CATALAN"),
1423 _T("CHINESE"),
1424 _T("CHINESE_SIMPLIFIED"),
1425 _T("CHINESE_TRADITIONAL"),
1426 _T("CHINESE_HONGKONG"),
1427 _T("CHINESE_MACAU"),
1428 _T("CHINESE_SINGAPORE"),
1429 _T("CHINESE_TAIWAN"),
1430 _T("CORSICAN"),
1431 _T("CROATIAN"),
1432 _T("CZECH"),
1433 _T("DANISH"),
1434 _T("DUTCH"),
1435 _T("DUTCH_BELGIAN"),
1436 _T("ENGLISH"),
1437 _T("ENGLISH_UK"),
1438 _T("ENGLISH_US"),
1439 _T("ENGLISH_AUSTRALIA"),
1440 _T("ENGLISH_BELIZE"),
1441 _T("ENGLISH_BOTSWANA"),
1442 _T("ENGLISH_CANADA"),
1443 _T("ENGLISH_CARIBBEAN"),
1444 _T("ENGLISH_DENMARK"),
1445 _T("ENGLISH_EIRE"),
1446 _T("ENGLISH_JAMAICA"),
1447 _T("ENGLISH_NEW_ZEALAND"),
1448 _T("ENGLISH_PHILIPPINES"),
1449 _T("ENGLISH_SOUTH_AFRICA"),
1450 _T("ENGLISH_TRINIDAD"),
1451 _T("ENGLISH_ZIMBABWE"),
1452 _T("ESPERANTO"),
1453 _T("ESTONIAN"),
1454 _T("FAEROESE"),
1455 _T("FARSI"),
1456 _T("FIJI"),
1457 _T("FINNISH"),
1458 _T("FRENCH"),
1459 _T("FRENCH_BELGIAN"),
1460 _T("FRENCH_CANADIAN"),
1461 _T("FRENCH_LUXEMBOURG"),
1462 _T("FRENCH_MONACO"),
1463 _T("FRENCH_SWISS"),
1464 _T("FRISIAN"),
1465 _T("GALICIAN"),
1466 _T("GEORGIAN"),
1467 _T("GERMAN"),
1468 _T("GERMAN_AUSTRIAN"),
1469 _T("GERMAN_BELGIUM"),
1470 _T("GERMAN_LIECHTENSTEIN"),
1471 _T("GERMAN_LUXEMBOURG"),
1472 _T("GERMAN_SWISS"),
1473 _T("GREEK"),
1474 _T("GREENLANDIC"),
1475 _T("GUARANI"),
1476 _T("GUJARATI"),
1477 _T("HAUSA"),
1478 _T("HEBREW"),
1479 _T("HINDI"),
1480 _T("HUNGARIAN"),
1481 _T("ICELANDIC"),
1482 _T("INDONESIAN"),
1483 _T("INTERLINGUA"),
1484 _T("INTERLINGUE"),
1485 _T("INUKTITUT"),
1486 _T("INUPIAK"),
1487 _T("IRISH"),
1488 _T("ITALIAN"),
1489 _T("ITALIAN_SWISS"),
1490 _T("JAPANESE"),
1491 _T("JAVANESE"),
1492 _T("KANNADA"),
1493 _T("KASHMIRI"),
1494 _T("KASHMIRI_INDIA"),
1495 _T("KAZAKH"),
1496 _T("KERNEWEK"),
1497 _T("KINYARWANDA"),
1498 _T("KIRGHIZ"),
1499 _T("KIRUNDI"),
1500 _T("KONKANI"),
1501 _T("KOREAN"),
1502 _T("KURDISH"),
1503 _T("LAOTHIAN"),
1504 _T("LATIN"),
1505 _T("LATVIAN"),
1506 _T("LINGALA"),
1507 _T("LITHUANIAN"),
1508 _T("MACEDONIAN"),
1509 _T("MALAGASY"),
1510 _T("MALAY"),
1511 _T("MALAYALAM"),
1512 _T("MALAY_BRUNEI_DARUSSALAM"),
1513 _T("MALAY_MALAYSIA"),
1514 _T("MALTESE"),
1515 _T("MANIPURI"),
1516 _T("MAORI"),
1517 _T("MARATHI"),
1518 _T("MOLDAVIAN"),
1519 _T("MONGOLIAN"),
1520 _T("NAURU"),
1521 _T("NEPALI"),
1522 _T("NEPALI_INDIA"),
1523 _T("NORWEGIAN_BOKMAL"),
1524 _T("NORWEGIAN_NYNORSK"),
1525 _T("OCCITAN"),
1526 _T("ORIYA"),
1527 _T("OROMO"),
1528 _T("PASHTO"),
1529 _T("POLISH"),
1530 _T("PORTUGUESE"),
1531 _T("PORTUGUESE_BRAZILIAN"),
1532 _T("PUNJABI"),
1533 _T("QUECHUA"),
1534 _T("RHAETO_ROMANCE"),
1535 _T("ROMANIAN"),
1536 _T("RUSSIAN"),
1537 _T("RUSSIAN_UKRAINE"),
1538 _T("SAMOAN"),
1539 _T("SANGHO"),
1540 _T("SANSKRIT"),
1541 _T("SCOTS_GAELIC"),
1542 _T("SERBIAN"),
1543 _T("SERBIAN_CYRILLIC"),
1544 _T("SERBIAN_LATIN"),
1545 _T("SERBO_CROATIAN"),
1546 _T("SESOTHO"),
1547 _T("SETSWANA"),
1548 _T("SHONA"),
1549 _T("SINDHI"),
1550 _T("SINHALESE"),
1551 _T("SISWATI"),
1552 _T("SLOVAK"),
1553 _T("SLOVENIAN"),
1554 _T("SOMALI"),
1555 _T("SPANISH"),
1556 _T("SPANISH_ARGENTINA"),
1557 _T("SPANISH_BOLIVIA"),
1558 _T("SPANISH_CHILE"),
1559 _T("SPANISH_COLOMBIA"),
1560 _T("SPANISH_COSTA_RICA"),
1561 _T("SPANISH_DOMINICAN_REPUBLIC"),
1562 _T("SPANISH_ECUADOR"),
1563 _T("SPANISH_EL_SALVADOR"),
1564 _T("SPANISH_GUATEMALA"),
1565 _T("SPANISH_HONDURAS"),
1566 _T("SPANISH_MEXICAN"),
1567 _T("SPANISH_MODERN"),
1568 _T("SPANISH_NICARAGUA"),
1569 _T("SPANISH_PANAMA"),
1570 _T("SPANISH_PARAGUAY"),
1571 _T("SPANISH_PERU"),
1572 _T("SPANISH_PUERTO_RICO"),
1573 _T("SPANISH_URUGUAY"),
1574 _T("SPANISH_US"),
1575 _T("SPANISH_VENEZUELA"),
1576 _T("SUNDANESE"),
1577 _T("SWAHILI"),
1578 _T("SWEDISH"),
1579 _T("SWEDISH_FINLAND"),
1580 _T("TAGALOG"),
1581 _T("TAJIK"),
1582 _T("TAMIL"),
1583 _T("TATAR"),
1584 _T("TELUGU"),
1585 _T("THAI"),
1586 _T("TIBETAN"),
1587 _T("TIGRINYA"),
1588 _T("TONGA"),
1589 _T("TSONGA"),
1590 _T("TURKISH"),
1591 _T("TURKMEN"),
1592 _T("TWI"),
1593 _T("UIGHUR"),
1594 _T("UKRAINIAN"),
1595 _T("URDU"),
1596 _T("URDU_INDIA"),
1597 _T("URDU_PAKISTAN"),
1598 _T("UZBEK"),
1599 _T("UZBEK_CYRILLIC"),
1600 _T("UZBEK_LATIN"),
1601 _T("VIETNAMESE"),
1602 _T("VOLAPUK"),
1603 _T("WELSH"),
1604 _T("WOLOF"),
1605 _T("XHOSA"),
1606 _T("YIDDISH"),
1607 _T("YORUBA"),
1608 _T("ZHUANG"),
1609 _T("ZULU"),
ec37df57
VZ
1610 };
1611
1612 if ( (size_t)lang < WXSIZEOF(languageNames) )
1613 return languageNames[lang];
1614 else
456ae26d 1615 return _T("INVALID");
ec37df57
VZ
1616}
1617
1618static void TestDefaultLang()
1619{
456ae26d 1620 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
ec37df57
VZ
1621
1622 static const wxChar *langStrings[] =
1623 {
1624 NULL, // system default
1625 _T("C"),
1626 _T("fr"),
1627 _T("fr_FR"),
1628 _T("en"),
1629 _T("en_GB"),
1630 _T("en_US"),
1631 _T("de_DE.iso88591"),
1632 _T("german"),
1633 _T("?"), // invalid lang spec
1634 _T("klingonese"), // I bet on some systems it does exist...
1635 };
1636
dccce9ea
VZ
1637 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1638 wxLocale::GetSystemEncodingName().c_str(),
1639 wxLocale::GetSystemEncoding());
1640
ec37df57
VZ
1641 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1642 {
456ae26d 1643 const wxChar *langStr = langStrings[n];
ec37df57 1644 if ( langStr )
dccce9ea
VZ
1645 {
1646 // FIXME: this doesn't do anything at all under Windows, we need
1647 // to create a new wxLocale!
ec37df57 1648 wxSetEnv(_T("LC_ALL"), langStr);
dccce9ea 1649 }
ec37df57
VZ
1650
1651 int lang = gs_localeDefault.GetSystemLanguage();
456ae26d
VZ
1652 wxPrintf(_T("Locale for '%s' is %s.\n"),
1653 langStr ? langStr : _T("system default"), GetLangName(lang));
ec37df57
VZ
1654 }
1655}
1656
1657#endif // TEST_LOCALE
1658
696e1ea0
VZ
1659// ----------------------------------------------------------------------------
1660// MIME types
1661// ----------------------------------------------------------------------------
1662
1663#ifdef TEST_MIME
1664
e84010cf 1665#include "wx/mimetype.h"
696e1ea0
VZ
1666
1667static void TestMimeEnum()
1668{
a6c65e88
VZ
1669 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1670
696e1ea0
VZ
1671 wxArrayString mimetypes;
1672
39189b9d 1673 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
696e1ea0 1674
456ae26d 1675 wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
696e1ea0
VZ
1676
1677 wxArrayString exts;
1678 wxString desc;
1679
1680 for ( size_t n = 0; n < count; n++ )
1681 {
39189b9d
VZ
1682 wxFileType *filetype =
1683 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
696e1ea0 1684 if ( !filetype )
c61f4f6d 1685 {
456ae26d 1686 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
97e0ceea 1687 mimetypes[n].c_str());
696e1ea0 1688 continue;
c61f4f6d
VZ
1689 }
1690
696e1ea0
VZ
1691 filetype->GetDescription(&desc);
1692 filetype->GetExtensions(exts);
1693
299fcbfe
VZ
1694 filetype->GetIcon(NULL);
1695
696e1ea0
VZ
1696 wxString extsAll;
1697 for ( size_t e = 0; e < exts.GetCount(); e++ )
1698 {
1699 if ( e > 0 )
1700 extsAll << _T(", ");
1701 extsAll += exts[e];
1702 }
1703
456ae26d 1704 wxPrintf(_T("\t%s: %s (%s)\n"),
54acce90 1705 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
696e1ea0 1706 }
39189b9d 1707
456ae26d 1708 wxPuts(_T(""));
696e1ea0
VZ
1709}
1710
f6bcfd97
BP
1711static void TestMimeOverride()
1712{
1713 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1714
39189b9d
VZ
1715 static const wxChar *mailcap = _T("/tmp/mailcap");
1716 static const wxChar *mimetypes = _T("/tmp/mime.types");
1717
1718 if ( wxFile::Exists(mailcap) )
1719 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1720 mailcap,
1721 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
1722 else
1723 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1724 mailcap);
f6bcfd97 1725
39189b9d
VZ
1726 if ( wxFile::Exists(mimetypes) )
1727 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1728 mimetypes,
1729 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
1730 else
1731 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1732 mimetypes);
1733
456ae26d 1734 wxPuts(_T(""));
f6bcfd97
BP
1735}
1736
1737static void TestMimeFilename()
1738{
1739 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1740
1741 static const wxChar *filenames[] =
1742 {
1743 _T("readme.txt"),
1744 _T("document.pdf"),
1745 _T("image.gif"),
f06ef5f4 1746 _T("picture.jpeg"),
f6bcfd97
BP
1747 };
1748
1749 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1750 {
1751 const wxString fname = filenames[n];
1752 wxString ext = fname.AfterLast(_T('.'));
39189b9d 1753 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
f6bcfd97
BP
1754 if ( !ft )
1755 {
1756 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1757 }
1758 else
1759 {
1760 wxString desc;
1761 if ( !ft->GetDescription(&desc) )
1762 desc = _T("<no description>");
1763
1764 wxString cmd;
1765 if ( !ft->GetOpenCommand(&cmd,
1766 wxFileType::MessageParameters(fname, _T(""))) )
1767 cmd = _T("<no command available>");
7aeebdcd
VZ
1768 else
1769 cmd = wxString('"') + cmd + '"';
f6bcfd97 1770
7aeebdcd 1771 wxPrintf(_T("To open %s (%s) do %s.\n"),
f6bcfd97
BP
1772 fname.c_str(), desc.c_str(), cmd.c_str());
1773
1774 delete ft;
1775 }
1776 }
39189b9d 1777
456ae26d 1778 wxPuts(_T(""));
f6bcfd97
BP
1779}
1780
c7ce8392
VZ
1781static void TestMimeAssociate()
1782{
1783 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1784
a6c65e88
VZ
1785 wxFileTypeInfo ftInfo(
1786 _T("application/x-xyz"),
1787 _T("xyzview '%s'"), // open cmd
1788 _T(""), // print cmd
df0dc216
VZ
1789 _T("XYZ File"), // description
1790 _T(".xyz"), // extensions
1791 NULL // end of extensions
a6c65e88
VZ
1792 );
1793 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
1794
39189b9d 1795 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
c7ce8392
VZ
1796 if ( !ft )
1797 {
1798 wxPuts(_T("ERROR: failed to create association!"));
1799 }
1800 else
1801 {
a6c65e88 1802 // TODO: read it back
c7ce8392
VZ
1803 delete ft;
1804 }
39189b9d 1805
456ae26d 1806 wxPuts(_T(""));
c7ce8392
VZ
1807}
1808
696e1ea0
VZ
1809#endif // TEST_MIME
1810
89e60357
VZ
1811// ----------------------------------------------------------------------------
1812// misc information functions
1813// ----------------------------------------------------------------------------
1814
1815#ifdef TEST_INFO_FUNCTIONS
1816
e84010cf 1817#include "wx/utils.h"
89e60357 1818
3a994742
VZ
1819static void TestDiskInfo()
1820{
456ae26d 1821 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
3a994742
VZ
1822
1823 for ( ;; )
1824 {
456ae26d
VZ
1825 wxChar pathname[128];
1826 wxPrintf(_T("\nEnter a directory name: "));
1827 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
3a994742
VZ
1828 break;
1829
1830 // kill the last '\n'
456ae26d 1831 pathname[wxStrlen(pathname) - 1] = 0;
3a994742
VZ
1832
1833 wxLongLong total, free;
1834 if ( !wxGetDiskSpace(pathname, &total, &free) )
1835 {
1836 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1837 }
1838 else
1839 {
eadd7bd2
VZ
1840 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1841 (total / 1024).ToString().c_str(),
1842 (free / 1024).ToString().c_str(),
3a994742
VZ
1843 pathname);
1844 }
1845 }
1846}
1847
89e60357
VZ
1848static void TestOsInfo()
1849{
456ae26d 1850 wxPuts(_T("*** Testing OS info functions ***\n"));
89e60357
VZ
1851
1852 int major, minor;
1853 wxGetOsVersion(&major, &minor);
456ae26d 1854 wxPrintf(_T("Running under: %s, version %d.%d\n"),
89e60357
VZ
1855 wxGetOsDescription().c_str(), major, minor);
1856
456ae26d 1857 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
89e60357 1858
456ae26d 1859 wxPrintf(_T("Host name is %s (%s).\n"),
89e60357 1860 wxGetHostName().c_str(), wxGetFullHostName().c_str());
bd3277fe 1861
456ae26d 1862 wxPuts(_T(""));
89e60357
VZ
1863}
1864
1865static void TestUserInfo()
1866{
456ae26d 1867 wxPuts(_T("*** Testing user info functions ***\n"));
89e60357 1868
456ae26d
VZ
1869 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1870 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1871 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1872 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
bd3277fe 1873
456ae26d 1874 wxPuts(_T(""));
89e60357
VZ
1875}
1876
1877#endif // TEST_INFO_FUNCTIONS
1878
b76b015e
VZ
1879// ----------------------------------------------------------------------------
1880// long long
1881// ----------------------------------------------------------------------------
1882
1883#ifdef TEST_LONGLONG
1884
e84010cf
GD
1885#include "wx/longlong.h"
1886#include "wx/timer.h"
b76b015e 1887
2a310492
VZ
1888// make a 64 bit number from 4 16 bit ones
1889#define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
1890
1891// get a random 64 bit number
1892#define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
1893
3a994742
VZ
1894static const long testLongs[] =
1895{
1896 0,
1897 1,
1898 -1,
1899 LONG_MAX,
1900 LONG_MIN,
1901 0x1234,
1902 -0x1234
1903};
1904
7d0bb74d 1905#if wxUSE_LONGLONG_WX
2a310492
VZ
1906inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
1907 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
1908inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
1909 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
7d0bb74d 1910#endif // wxUSE_LONGLONG_WX
2a310492 1911
b76b015e
VZ
1912static void TestSpeed()
1913{
1914 static const long max = 100000000;
1915 long n;
9fc3ad34 1916
b76b015e
VZ
1917 {
1918 wxStopWatch sw;
1919
1920 long l = 0;
1921 for ( n = 0; n < max; n++ )
1922 {
1923 l += n;
1924 }
1925
456ae26d 1926 wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw.Time());
b76b015e
VZ
1927 }
1928
2ea24d9f 1929#if wxUSE_LONGLONG_NATIVE
b76b015e
VZ
1930 {
1931 wxStopWatch sw;
1932
2ea24d9f 1933 wxLongLong_t l = 0;
b76b015e
VZ
1934 for ( n = 0; n < max; n++ )
1935 {
1936 l += n;
1937 }
1938
456ae26d 1939 wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw.Time());
b76b015e 1940 }
2ea24d9f 1941#endif // wxUSE_LONGLONG_NATIVE
b76b015e
VZ
1942
1943 {
1944 wxStopWatch sw;
1945
1946 wxLongLong l;
1947 for ( n = 0; n < max; n++ )
1948 {
1949 l += n;
1950 }
1951
456ae26d 1952 wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw.Time());
b76b015e
VZ
1953 }
1954}
1955
2a310492 1956static void TestLongLongConversion()
b76b015e 1957{
456ae26d 1958 wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
2a310492
VZ
1959
1960 wxLongLong a;
1961 size_t nTested = 0;
1962 for ( size_t n = 0; n < 100000; n++ )
1963 {
1964 a = RAND_LL();
1965
1966#if wxUSE_LONGLONG_NATIVE
1967 wxLongLongNative b(a.GetHi(), a.GetLo());
5e6a0e83 1968
2a310492
VZ
1969 wxASSERT_MSG( a == b, "conversions failure" );
1970#else
456ae26d 1971 wxPuts(_T("Can't do it without native long long type, test skipped."));
b76b015e 1972
2a310492
VZ
1973 return;
1974#endif // wxUSE_LONGLONG_NATIVE
1975
1976 if ( !(nTested % 1000) )
1977 {
1978 putchar('.');
1979 fflush(stdout);
1980 }
1981
1982 nTested++;
1983 }
1984
456ae26d 1985 wxPuts(_T(" done!"));
2a310492
VZ
1986}
1987
1988static void TestMultiplication()
1989{
456ae26d 1990 wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
2a310492
VZ
1991
1992 wxLongLong a, b;
1993 size_t nTested = 0;
1994 for ( size_t n = 0; n < 100000; n++ )
1995 {
1996 a = RAND_LL();
1997 b = RAND_LL();
1998
1999#if wxUSE_LONGLONG_NATIVE
2000 wxLongLongNative aa(a.GetHi(), a.GetLo());
2001 wxLongLongNative bb(b.GetHi(), b.GetLo());
2002
2003 wxASSERT_MSG( a*b == aa*bb, "multiplication failure" );
2004#else // !wxUSE_LONGLONG_NATIVE
456ae26d 2005 wxPuts(_T("Can't do it without native long long type, test skipped."));
2a310492
VZ
2006
2007 return;
2008#endif // wxUSE_LONGLONG_NATIVE
2009
2010 if ( !(nTested % 1000) )
2011 {
2012 putchar('.');
2013 fflush(stdout);
2014 }
2015
2016 nTested++;
2017 }
2018
456ae26d 2019 wxPuts(_T(" done!"));
2a310492
VZ
2020}
2021
2022static void TestDivision()
2023{
456ae26d 2024 wxPuts(_T("*** Testing wxLongLong division ***\n"));
2f02cb89 2025
2ea24d9f 2026 wxLongLong q, r;
2f02cb89 2027 size_t nTested = 0;
5e6a0e83 2028 for ( size_t n = 0; n < 100000; n++ )
2f02cb89
VZ
2029 {
2030 // get a random wxLongLong (shifting by 12 the MSB ensures that the
2031 // multiplication will not overflow)
2032 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
2033
19f45995
VZ
2034 // get a random (but non null) long (not wxLongLong for now) to divide
2035 // it with
2036 long l;
2037 do
2038 {
2039 l = rand();
2040 }
2041 while ( !l );
2042
2ea24d9f
VZ
2043 q = ll / l;
2044 r = ll % l;
2045
2a310492
VZ
2046#if wxUSE_LONGLONG_NATIVE
2047 wxLongLongNative m(ll.GetHi(), ll.GetLo());
2048
2049 wxLongLongNative p = m / l, s = m % l;
2050 wxASSERT_MSG( q == p && r == s, "division failure" );
2051#else // !wxUSE_LONGLONG_NATIVE
5e6a0e83 2052 // verify the result
2ea24d9f 2053 wxASSERT_MSG( ll == q*l + r, "division failure" );
2a310492 2054#endif // wxUSE_LONGLONG_NATIVE
2f02cb89 2055
5e6a0e83
VZ
2056 if ( !(nTested % 1000) )
2057 {
2058 putchar('.');
2059 fflush(stdout);
2060 }
2061
2f02cb89
VZ
2062 nTested++;
2063 }
2064
456ae26d 2065 wxPuts(_T(" done!"));
2a310492 2066}
2f02cb89 2067
2a310492
VZ
2068static void TestAddition()
2069{
456ae26d 2070 wxPuts(_T("*** Testing wxLongLong addition ***\n"));
2a310492
VZ
2071
2072 wxLongLong a, b, c;
2073 size_t nTested = 0;
2074 for ( size_t n = 0; n < 100000; n++ )
2075 {
2076 a = RAND_LL();
2077 b = RAND_LL();
2078 c = a + b;
2079
2080#if wxUSE_LONGLONG_NATIVE
2081 wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
2082 wxLongLongNative(b.GetHi(), b.GetLo()),
7c968cee 2083 "addition failure" );
2a310492
VZ
2084#else // !wxUSE_LONGLONG_NATIVE
2085 wxASSERT_MSG( c - b == a, "addition failure" );
2086#endif // wxUSE_LONGLONG_NATIVE
2087
2088 if ( !(nTested % 1000) )
2089 {
2090 putchar('.');
2091 fflush(stdout);
2092 }
2093
2094 nTested++;
2095 }
2096
456ae26d 2097 wxPuts(_T(" done!"));
b76b015e
VZ
2098}
2099
2a310492
VZ
2100static void TestBitOperations()
2101{
456ae26d 2102 wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
2a310492 2103
f6bcfd97 2104 wxLongLong ll;
2a310492
VZ
2105 size_t nTested = 0;
2106 for ( size_t n = 0; n < 100000; n++ )
2107 {
f6bcfd97 2108 ll = RAND_LL();
2a310492
VZ
2109
2110#if wxUSE_LONGLONG_NATIVE
2111 for ( size_t n = 0; n < 33; n++ )
2112 {
2a310492 2113 }
2a310492 2114#else // !wxUSE_LONGLONG_NATIVE
456ae26d 2115 wxPuts(_T("Can't do it without native long long type, test skipped."));
2a310492
VZ
2116
2117 return;
2118#endif // wxUSE_LONGLONG_NATIVE
2119
2120 if ( !(nTested % 1000) )
2121 {
2122 putchar('.');
2123 fflush(stdout);
2124 }
2125
2126 nTested++;
2127 }
2128
456ae26d 2129 wxPuts(_T(" done!"));
2a310492
VZ
2130}
2131
f6bcfd97
BP
2132static void TestLongLongComparison()
2133{
2d3112ad 2134#if wxUSE_LONGLONG_WX
456ae26d 2135 wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
f6bcfd97 2136
f6bcfd97
BP
2137 static const long ls[2] =
2138 {
2139 0x1234,
2140 -0x1234,
2141 };
2142
2143 wxLongLongWx lls[2];
2144 lls[0] = ls[0];
3a994742 2145 lls[1] = ls[1];
f6bcfd97
BP
2146
2147 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
2148 {
2149 bool res;
2150
2151 for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
2152 {
2153 res = lls[m] > testLongs[n];
456ae26d 2154 wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
f6bcfd97
BP
2155 ls[m], testLongs[n], res ? "true" : "false",
2156 res == (ls[m] > testLongs[n]) ? "ok" : "ERROR");
2157
2158 res = lls[m] < testLongs[n];
456ae26d 2159 wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
f6bcfd97
BP
2160 ls[m], testLongs[n], res ? "true" : "false",
2161 res == (ls[m] < testLongs[n]) ? "ok" : "ERROR");
2162
2163 res = lls[m] == testLongs[n];
456ae26d 2164 wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
f6bcfd97
BP
2165 ls[m], testLongs[n], res ? "true" : "false",
2166 res == (ls[m] == testLongs[n]) ? "ok" : "ERROR");
2167 }
2168 }
2d3112ad 2169#endif // wxUSE_LONGLONG_WX
f6bcfd97
BP
2170}
2171
3a994742
VZ
2172static void TestLongLongPrint()
2173{
2174 wxPuts(_T("*** Testing wxLongLong printing ***\n"));
2175
2176 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
2177 {
2178 wxLongLong ll = testLongs[n];
2179 wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str());
2180 }
2181
2182 wxLongLong ll(0x12345678, 0x87654321);
2183 wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str());
2184
2185 ll.Negate();
2186 wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str());
2187}
2188
2a310492
VZ
2189#undef MAKE_LL
2190#undef RAND_LL
2191
b76b015e
VZ
2192#endif // TEST_LONGLONG
2193
39189b9d
VZ
2194// ----------------------------------------------------------------------------
2195// path list
2196// ----------------------------------------------------------------------------
2197
2198#ifdef TEST_PATHLIST
2199
ee3ef281
VZ
2200#ifdef __UNIX__
2201 #define CMD_IN_PATH _T("ls")
2202#else
2203 #define CMD_IN_PATH _T("command.com")
2204#endif
2205
39189b9d
VZ
2206static void TestPathList()
2207{
456ae26d 2208 wxPuts(_T("*** Testing wxPathList ***\n"));
39189b9d
VZ
2209
2210 wxPathList pathlist;
ee3ef281
VZ
2211 pathlist.AddEnvList(_T("PATH"));
2212 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
39189b9d
VZ
2213 if ( path.empty() )
2214 {
456ae26d 2215 wxPrintf(_T("ERROR: command not found in the path.\n"));
39189b9d
VZ
2216 }
2217 else
2218 {
456ae26d 2219 wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
39189b9d
VZ
2220 }
2221}
2222
2223#endif // TEST_PATHLIST
2224
07a56e45
VZ
2225// ----------------------------------------------------------------------------
2226// regular expressions
2227// ----------------------------------------------------------------------------
2228
2229#ifdef TEST_REGEX
2230
e84010cf 2231#include "wx/regex.h"
07a56e45
VZ
2232
2233static void TestRegExCompile()
2234{
2235 wxPuts(_T("*** Testing RE compilation ***\n"));
2236
2237 static struct RegExCompTestData
2238 {
2239 const wxChar *pattern;
2240 bool correct;
2241 } regExCompTestData[] =
2242 {
2243 { _T("foo"), TRUE },
2244 { _T("foo("), FALSE },
2245 { _T("foo(bar"), FALSE },
2246 { _T("foo(bar)"), TRUE },
2247 { _T("foo["), FALSE },
2248 { _T("foo[bar"), FALSE },
2249 { _T("foo[bar]"), TRUE },
2250 { _T("foo{"), TRUE },
2251 { _T("foo{1"), FALSE },
2252 { _T("foo{bar"), TRUE },
2253 { _T("foo{1}"), TRUE },
2254 { _T("foo{1,2}"), TRUE },
2255 { _T("foo{bar}"), TRUE },
2256 { _T("foo*"), TRUE },
2257 { _T("foo**"), FALSE },
2258 { _T("foo+"), TRUE },
2259 { _T("foo++"), FALSE },
2260 { _T("foo?"), TRUE },
2261 { _T("foo??"), FALSE },
2262 { _T("foo?+"), FALSE },
2263 };
2264
2265 wxRegEx re;
2266 for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ )
2267 {
2268 const RegExCompTestData& data = regExCompTestData[n];
2269 bool ok = re.Compile(data.pattern);
2270
2271 wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
2272 data.pattern,
2273 ok ? _T("") : _T("not "),
2274 ok == data.correct ? _T("ok") : _T("ERROR"));
2275 }
2276}
2277
2278static void TestRegExMatch()
2279{
2280 wxPuts(_T("*** Testing RE matching ***\n"));
2281
2282 static struct RegExMatchTestData
2283 {
2284 const wxChar *pattern;
2285 const wxChar *text;
2286 bool correct;
2287 } regExMatchTestData[] =
2288 {
2289 { _T("foo"), _T("bar"), FALSE },
2290 { _T("foo"), _T("foobar"), TRUE },
2291 { _T("^foo"), _T("foobar"), TRUE },
2292 { _T("^foo"), _T("barfoo"), FALSE },
2293 { _T("bar$"), _T("barbar"), TRUE },
2294 { _T("bar$"), _T("barbar "), FALSE },
2295 };
2296
2297 for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
2298 {
2299 const RegExMatchTestData& data = regExMatchTestData[n];
2300
2301 wxRegEx re(data.pattern);
2302 bool ok = re.Matches(data.text);
2303
2304 wxPrintf(_T("'%s' %s %s (%s)\n"),
2305 data.pattern,
2306 ok ? _T("matches") : _T("doesn't match"),
2307 data.text,
2308 ok == data.correct ? _T("ok") : _T("ERROR"));
2309 }
2310}
2311
2312static void TestRegExSubmatch()
2313{
2314 wxPuts(_T("*** Testing RE subexpressions ***\n"));
2315
2316 wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
2317 if ( !re.IsValid() )
2318 {
2319 wxPuts(_T("ERROR: compilation failed."));
2320 return;
2321 }
2322
2323 wxString text = _T("Fri Jul 13 18:37:52 CEST 2001");
2324
2325 if ( !re.Matches(text) )
2326 {
2327 wxPuts(_T("ERROR: match expected."));
2328 }
2329 else
2330 {
2331 wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str());
2332
2333 wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
2334 re.GetMatch(text, 3).c_str(),
2335 re.GetMatch(text, 2).c_str(),
2336 re.GetMatch(text, 4).c_str(),
2337 re.GetMatch(text, 1).c_str());
2338 }
2339}
2340
765624f7
VZ
2341static void TestRegExReplacement()
2342{
2343 wxPuts(_T("*** Testing RE replacement ***"));
2344
2345 static struct RegExReplTestData
2346 {
2347 const wxChar *text;
2348 const wxChar *repl;
2349 const wxChar *result;
2350 size_t count;
2351 } regExReplTestData[] =
2352 {
2353 { _T("foo123"), _T("bar"), _T("bar"), 1 },
2354 { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
2355 { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
2356 { _T("123foo"), _T("bar"), _T("123foo"), 0 },
2357 { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
2358 { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
2359 { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
2360 };
2361
2362 const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)");
daa2c7d9 2363 wxRegEx re(pattern);
765624f7
VZ
2364
2365 wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern);
2366
2367 for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ )
2368 {
2369 const RegExReplTestData& data = regExReplTestData[n];
2370
2371 wxString text = data.text;
2372 size_t nRepl = re.Replace(&text, data.repl);
2373
2374 wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
2375 data.text, data.repl,
2376 nRepl, nRepl == 1 ? _T("") : _T("es"),
2377 text.c_str());
2378 if ( text == data.result && nRepl == data.count )
2379 {
2380 wxPuts(_T("ok)"));
2381 }
2382 else
2383 {
2384 wxPrintf(_T("ERROR: should be %u and '%s')\n"),
2385 data.count, data.result);
2386 }
2387 }
2388}
2389
07a56e45
VZ
2390static void TestRegExInteractive()
2391{
2392 wxPuts(_T("*** Testing RE interactively ***"));
2393
2394 for ( ;; )
2395 {
456ae26d
VZ
2396 wxChar pattern[128];
2397 wxPrintf(_T("\nEnter a pattern: "));
2398 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
07a56e45
VZ
2399 break;
2400
2401 // kill the last '\n'
456ae26d 2402 pattern[wxStrlen(pattern) - 1] = 0;
07a56e45
VZ
2403
2404 wxRegEx re;
2405 if ( !re.Compile(pattern) )
2406 {
2407 continue;
2408 }
2409
456ae26d 2410 wxChar text[128];
07a56e45
VZ
2411 for ( ;; )
2412 {
456ae26d
VZ
2413 wxPrintf(_T("Enter text to match: "));
2414 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
07a56e45
VZ
2415 break;
2416
2417 // kill the last '\n'
456ae26d 2418 text[wxStrlen(text) - 1] = 0;
07a56e45
VZ
2419
2420 if ( !re.Matches(text) )
2421 {
456ae26d 2422 wxPrintf(_T("No match.\n"));
07a56e45
VZ
2423 }
2424 else
2425 {
456ae26d 2426 wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
07a56e45
VZ
2427
2428 size_t start, len;
2429 for ( size_t n = 1; ; n++ )
2430 {
2431 if ( !re.GetMatch(&start, &len, n) )
2432 {
2433 break;
2434 }
2435
456ae26d
VZ
2436 wxPrintf(_T("Subexpr %u matched '%s'\n"),
2437 n, wxString(text + start, len).c_str());
07a56e45
VZ
2438 }
2439 }
2440 }
2441 }
2442}
2443
2444#endif // TEST_REGEX
2445
8d5eff60
VZ
2446// ----------------------------------------------------------------------------
2447// database
2448// ----------------------------------------------------------------------------
2449
ba6ea19e
VZ
2450#if !wxUSE_ODBC
2451 #undef TEST_ODBC
2452#endif
2453
8d5eff60
VZ
2454#ifdef TEST_ODBC
2455
2456#include <wx/db.h>
2457
2458static void TestDbOpen()
2459{
2460 HENV henv;
2461 wxDb db(henv);
2462}
2463
2464#endif // TEST_ODBC
2465
7aeebdcd
VZ
2466// ----------------------------------------------------------------------------
2467// printf() tests
2468// ----------------------------------------------------------------------------
2469
2470/*
2471 NB: this stuff was taken from the glibc test suite and modified to build
2472 in wxWindows: if I read the copyright below properly, this shouldn't
2473 be a problem
2474 */
2475
2476#ifdef TEST_PRINTF
2477
2478#ifdef wxTEST_PRINTF
2479 // use our functions from wxchar.cpp
2480 #undef wxPrintf
2481 #undef wxSprintf
2482
2483 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
2484 // in the tests below
2485 int wxPrintf( const wxChar *format, ... );
2486 int wxSprintf( wxChar *str, const wxChar *format, ... );
2487#endif
2488
2489#include <float.h>
2490
2491static void rfg1 (void);
2492static void rfg2 (void);
2493
2494
2495static void
2496fmtchk (const wxChar *fmt)
2497{
2498 (void) wxPrintf(_T("%s:\t`"), fmt);
2499 (void) wxPrintf(fmt, 0x12);
2500 (void) wxPrintf(_T("'\n"));
2501}
2502
2503static void
2504fmtst1chk (const wxChar *fmt)
2505{
2506 (void) wxPrintf(_T("%s:\t`"), fmt);
2507 (void) wxPrintf(fmt, 4, 0x12);
2508 (void) wxPrintf(_T("'\n"));
2509}
2510
2511static void
2512fmtst2chk (const wxChar *fmt)
2513{
2514 (void) wxPrintf(_T("%s:\t`"), fmt);
2515 (void) wxPrintf(fmt, 4, 4, 0x12);
2516 (void) wxPrintf(_T("'\n"));
2517}
2518
2519/* This page is covered by the following copyright: */
2520
2521/* (C) Copyright C E Chew
2522 *
2523 * Feel free to copy, use and distribute this software provided:
2524 *
2525 * 1. you do not pretend that you wrote it
2526 * 2. you leave this copyright notice intact.
2527 */
2528
2529/*
2530 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
2531 */
2532
2533#define DEC -123
2534#define INT 255
2535#define UNS (~0)
2536
2537/* Formatted Output Test
2538 *
2539 * This exercises the output formatting code.
2540 */
2541
2542static void
2543fp_test (void)
2544{
2545 int i, j, k, l;
2546 wxChar buf[7];
2547 wxChar *prefix = buf;
2548 wxChar tp[20];
2549
2550 wxPuts(_T("\nFormatted output test"));
2551 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
2552 wxStrcpy(prefix, _T("%"));
2553 for (i = 0; i < 2; i++) {
2554 for (j = 0; j < 2; j++) {
2555 for (k = 0; k < 2; k++) {
2556 for (l = 0; l < 2; l++) {
2557 wxStrcpy(prefix, _T("%"));
2558 if (i == 0) wxStrcat(prefix, _T("-"));
2559 if (j == 0) wxStrcat(prefix, _T("+"));
2560 if (k == 0) wxStrcat(prefix, _T("#"));
2561 if (l == 0) wxStrcat(prefix, _T("0"));
2562 wxPrintf(_T("%5s |"), prefix);
2563 wxStrcpy(tp, prefix);
2564 wxStrcat(tp, _T("6d |"));
2565 wxPrintf(tp, DEC);
2566 wxStrcpy(tp, prefix);
2567 wxStrcat(tp, _T("6o |"));
2568 wxPrintf(tp, INT);
2569 wxStrcpy(tp, prefix);
2570 wxStrcat(tp, _T("6x |"));
2571 wxPrintf(tp, INT);
2572 wxStrcpy(tp, prefix);
2573 wxStrcat(tp, _T("6X |"));
2574 wxPrintf(tp, INT);
2575 wxStrcpy(tp, prefix);
2576 wxStrcat(tp, _T("6u |"));
2577 wxPrintf(tp, UNS);
2578 wxPrintf(_T("\n"));
2579 }
2580 }
2581 }
2582 }
2583 wxPrintf(_T("%10s\n"), (wxChar *) NULL);
2584 wxPrintf(_T("%-10s\n"), (wxChar *) NULL);
2585}
2586
2587static void TestPrintf()
2588{
2589 static wxChar shortstr[] = _T("Hi, Z.");
2590 static wxChar longstr[] = "Good morning, Doctor Chandra. This is Hal. \
2591I am ready for my first lesson today.";
2592 int result = 0;
2593
2594 fmtchk(_T("%.4x"));
2595 fmtchk(_T("%04x"));
2596 fmtchk(_T("%4.4x"));
2597 fmtchk(_T("%04.4x"));
2598 fmtchk(_T("%4.3x"));
2599 fmtchk(_T("%04.3x"));
2600
2601 fmtst1chk(_T("%.*x"));
2602 fmtst1chk(_T("%0*x"));
2603 fmtst2chk(_T("%*.*x"));
2604 fmtst2chk(_T("%0*.*x"));
2605
2606 wxPrintf(_T("bad format:\t\"%b\"\n"));
2607 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
2608
2609 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
2610 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
2611 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
2612 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
2613 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
2614 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
2615 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
2616 wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
2617 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
2618 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
2619
2620 wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr);
2621 wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr);
2622 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
2623 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
2624 wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar *)NULL);
2625 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
2626
2627 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
2628 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
2629 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
2630 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
2631 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
2632 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
2633 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
2634 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
2635 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
2636 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
2637 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
2638 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
2639
2640 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
2641 wxPrintf (_T(" %6.5f\n"), .1);
2642 wxPrintf (_T("x%5.4fx\n"), .5);
2643
2644 wxPrintf (_T("%#03x\n"), 1);
2645
2646 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
2647
2648 {
2649 double d = FLT_MIN;
2650 int niter = 17;
2651
2652 while (niter-- != 0)
2653 wxPrintf (_T("%.17e\n"), d / 2);
2654 fflush (stdout);
2655 }
2656
2657 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
2658
2659#define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
2660 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
2661 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
2662 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
2663 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
2664 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
2665 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
2666 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
2667 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
2668 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
2669#undef FORMAT
2670
2671 {
2672 wxChar buf[20];
2673 int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
2674
2675 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
2676 rc, WXSIZEOF(buf), buf);
2677#if 0
2678 wxChar buf2[512];
2679 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
2680 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
2681#endif
2682 }
2683
2684 fp_test ();
2685
2686 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
2687 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
2688 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
2689 wxPrintf (_T("%g should be 123.456\n"), 123.456);
2690 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
2691 wxPrintf (_T("%g should be 10\n"), 10.0);
2692 wxPrintf (_T("%g should be 0.02\n"), 0.02);
2693
2694 {
2695 double x=1.0;
2696 wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
2697 }
2698
2699 {
2700 wxChar buf[200];
2701
2702 wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
2703
2704 result |= wxStrcmp (buf,
2705 _T("onetwo three "));
2706
2707 wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
2708 }
2709
2710 {
2711 wxChar buf[200];
2712
2713 wxSprintf (buf, _T("%07Lo"), 040000000000ll);
2714 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
2715
2716 if (wxStrcmp (buf, _T("40000000000")) != 0)
2717 {
2718 result = 1;
2719 wxPuts (_T("\tFAILED"));
2720 }
2721 wxPuts ("");
2722 }
2723
2724 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
2725 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
2726
2727 wxPuts (_T("--- Should be no further output. ---"));
2728 rfg1 ();
2729 rfg2 ();
2730
2731#if 0
2732 {
2733 wxChar bytes[7];
2734 wxChar buf[20];
2735
2736 memset (bytes, '\xff', sizeof bytes);
2737 wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
2738 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
2739 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
2740 {
2741 wxPuts (_T("%hhn overwrite more bytes"));
2742 result = 1;
2743 }
2744 if (bytes[3] != 3)
2745 {
2746 wxPuts (_T("%hhn wrote incorrect value"));
2747 result = 1;
2748 }
2749 }
2750#endif
2751}
2752
2753static void
2754rfg1 (void)
2755{
2756 wxChar buf[100];
2757
2758 wxSprintf (buf, _T("%5.s"), _T("xyz"));
2759 if (wxStrcmp (buf, _T(" ")) != 0)
2760 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
2761 wxSprintf (buf, _T("%5.f"), 33.3);
2762 if (wxStrcmp (buf, _T(" 33")) != 0)
2763 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
2764 wxSprintf (buf, _T("%8.e"), 33.3e7);
2765 if (wxStrcmp (buf, _T(" 3e+08")) != 0)
2766 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
2767 wxSprintf (buf, _T("%8.E"), 33.3e7);
2768 if (wxStrcmp (buf, _T(" 3E+08")) != 0)
2769 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
2770 wxSprintf (buf, _T("%.g"), 33.3);
2771 if (wxStrcmp (buf, _T("3e+01")) != 0)
2772 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
2773 wxSprintf (buf, _T("%.G"), 33.3);
2774 if (wxStrcmp (buf, _T("3E+01")) != 0)
2775 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
2776}
2777
2778static void
2779rfg2 (void)
2780{
2781 int prec;
2782 wxChar buf[100];
2783
2784 prec = 0;
2785 wxSprintf (buf, _T("%.*g"), prec, 3.3);
2786 if (wxStrcmp (buf, _T("3")) != 0)
2787 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2788 prec = 0;
2789 wxSprintf (buf, _T("%.*G"), prec, 3.3);
2790 if (wxStrcmp (buf, _T("3")) != 0)
2791 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2792 prec = 0;
2793 wxSprintf (buf, _T("%7.*G"), prec, 3.33);
2794 if (wxStrcmp (buf, _T(" 3")) != 0)
2795 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
2796 prec = 3;
2797 wxSprintf (buf, _T("%04.*o"), prec, 33);
2798 if (wxStrcmp (buf, _T(" 041")) != 0)
2799 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
2800 prec = 7;
2801 wxSprintf (buf, _T("%09.*u"), prec, 33);
2802 if (wxStrcmp (buf, _T(" 0000033")) != 0)
2803 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
2804 prec = 3;
2805 wxSprintf (buf, _T("%04.*x"), prec, 33);
2806 if (wxStrcmp (buf, _T(" 021")) != 0)
2807 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2808 prec = 3;
2809 wxSprintf (buf, _T("%04.*X"), prec, 33);
2810 if (wxStrcmp (buf, _T(" 021")) != 0)
2811 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2812}
2813
2814#endif // TEST_PRINTF
2815
6dfec4b8 2816// ----------------------------------------------------------------------------
7ba4fbeb 2817// registry and related stuff
6dfec4b8
VZ
2818// ----------------------------------------------------------------------------
2819
2820// this is for MSW only
2821#ifndef __WXMSW__
7ba4fbeb 2822 #undef TEST_REGCONF
6dfec4b8
VZ
2823 #undef TEST_REGISTRY
2824#endif
2825
7ba4fbeb
VZ
2826#ifdef TEST_REGCONF
2827
e84010cf
GD
2828#include "wx/confbase.h"
2829#include "wx/msw/regconf.h"
7ba4fbeb
VZ
2830
2831static void TestRegConfWrite()
2832{
2833 wxRegConfig regconf(_T("console"), _T("wxwindows"));
2834 regconf.Write(_T("Hello"), wxString(_T("world")));
2835}
2836
2837#endif // TEST_REGCONF
2838
6dfec4b8
VZ
2839#ifdef TEST_REGISTRY
2840
e84010cf 2841#include "wx/msw/registry.h"
6dfec4b8
VZ
2842
2843// I chose this one because I liked its name, but it probably only exists under
2844// NT
2845static const wxChar *TESTKEY =
2846 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2847
2848static void TestRegistryRead()
2849{
456ae26d 2850 wxPuts(_T("*** testing registry reading ***"));
6dfec4b8
VZ
2851
2852 wxRegKey key(TESTKEY);
456ae26d 2853 wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
6dfec4b8
VZ
2854 if ( !key.Open() )
2855 {
456ae26d 2856 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
6dfec4b8
VZ
2857
2858 return;
2859 }
2860
2861 size_t nSubKeys, nValues;
2862 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2863 {
456ae26d 2864 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
6dfec4b8
VZ
2865 }
2866
456ae26d 2867 wxPrintf(_T("Enumerating values:\n"));
6dfec4b8
VZ
2868
2869 long dummy;
2870 wxString value;
2871 bool cont = key.GetFirstValue(value, dummy);
2872 while ( cont )
2873 {
456ae26d 2874 wxPrintf(_T("Value '%s': type "), value.c_str());
6dfec4b8
VZ
2875 switch ( key.GetValueType(value) )
2876 {
456ae26d
VZ
2877 case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
2878 case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
2879 case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
2880 case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
2881 case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
2882 case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
2883 default: wxPrintf(_T("other (unknown)")); break;
6dfec4b8
VZ
2884 }
2885
456ae26d 2886 wxPrintf(_T(", value = "));
6dfec4b8
VZ
2887 if ( key.IsNumericValue(value) )
2888 {
2889 long val;
2890 key.QueryValue(value, &val);
456ae26d 2891 wxPrintf(_T("%ld"), val);
6dfec4b8
VZ
2892 }
2893 else // string
2894 {
2895 wxString val;
2896 key.QueryValue(value, val);
456ae26d 2897 wxPrintf(_T("'%s'"), val.c_str());
6dfec4b8
VZ
2898
2899 key.QueryRawValue(value, val);
456ae26d 2900 wxPrintf(_T(" (raw value '%s')"), val.c_str());
6dfec4b8
VZ
2901 }
2902
2903 putchar('\n');
2904
2905 cont = key.GetNextValue(value, dummy);
2906 }
2907}
2908
6ba63600
VZ
2909static void TestRegistryAssociation()
2910{
2911 /*
2912 The second call to deleteself genertaes an error message, with a
2913 messagebox saying .flo is crucial to system operation, while the .ddf
2914 call also fails, but with no error message
2915 */
2916
2917 wxRegKey key;
2918
2919 key.SetName("HKEY_CLASSES_ROOT\\.ddf" );
2920 key.Create();
2921 key = "ddxf_auto_file" ;
2922 key.SetName("HKEY_CLASSES_ROOT\\.flo" );
2923 key.Create();
2924 key = "ddxf_auto_file" ;
2925 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon");
2926 key.Create();
2927 key = "program,0" ;
2928 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command");
2929 key.Create();
2930 key = "program \"%1\"" ;
2931
2932 key.SetName("HKEY_CLASSES_ROOT\\.ddf" );
2933 key.DeleteSelf();
2934 key.SetName("HKEY_CLASSES_ROOT\\.flo" );
2935 key.DeleteSelf();
2936 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon");
2937 key.DeleteSelf();
2938 key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command");
2939 key.DeleteSelf();
2940}
2941
6dfec4b8
VZ
2942#endif // TEST_REGISTRY
2943
2c8e4738
VZ
2944// ----------------------------------------------------------------------------
2945// sockets
2946// ----------------------------------------------------------------------------
2947
2948#ifdef TEST_SOCKETS
2949
e84010cf
GD
2950#include "wx/socket.h"
2951#include "wx/protocol/protocol.h"
2952#include "wx/protocol/http.h"
8e907a13
VZ
2953
2954static void TestSocketServer()
2955{
456ae26d 2956 wxPuts(_T("*** Testing wxSocketServer ***\n"));
8e907a13 2957
ccdb23df
VZ
2958 static const int PORT = 3000;
2959
8e907a13 2960 wxIPV4address addr;
ccdb23df 2961 addr.Service(PORT);
8e907a13
VZ
2962
2963 wxSocketServer *server = new wxSocketServer(addr);
2964 if ( !server->Ok() )
2965 {
456ae26d 2966 wxPuts(_T("ERROR: failed to bind"));
ccdb23df
VZ
2967
2968 return;
8e907a13 2969 }
8dfea369
VZ
2970
2971 for ( ;; )
2972 {
456ae26d 2973 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
8dfea369
VZ
2974
2975 wxSocketBase *socket = server->Accept();
2976 if ( !socket )
2977 {
456ae26d 2978 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
8dfea369
VZ
2979 break;
2980 }
2981
456ae26d 2982 wxPuts(_T("Server: got a client."));
8dfea369 2983
ccdb23df
VZ
2984 server->SetTimeout(60); // 1 min
2985
2986 while ( socket->IsConnected() )
8dfea369 2987 {
ccdb23df 2988 wxString s;
456ae26d 2989 wxChar ch = _T('\0');
ccdb23df 2990 for ( ;; )
8dfea369 2991 {
ccdb23df
VZ
2992 if ( socket->Read(&ch, sizeof(ch)).Error() )
2993 {
2994 // don't log error if the client just close the connection
2995 if ( socket->IsConnected() )
2996 {
456ae26d 2997 wxPuts(_T("ERROR: in wxSocket::Read."));
ccdb23df 2998 }
8dfea369 2999
ccdb23df
VZ
3000 break;
3001 }
8dfea369 3002
ccdb23df
VZ
3003 if ( ch == '\r' )
3004 continue;
8dfea369 3005
ccdb23df
VZ
3006 if ( ch == '\n' )
3007 break;
8dfea369 3008
ccdb23df
VZ
3009 s += ch;
3010 }
8dfea369 3011
ccdb23df
VZ
3012 if ( ch != '\n' )
3013 {
3014 break;
3015 }
8dfea369 3016
456ae26d 3017 wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
ccdb23df
VZ
3018 if ( s == _T("bye") )
3019 {
3020 delete socket;
8dfea369 3021
ccdb23df
VZ
3022 break;
3023 }
3024
3025 socket->Write(s.MakeUpper().c_str(), s.length());
3026 socket->Write("\r\n", 2);
456ae26d 3027 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
8dfea369
VZ
3028 }
3029
456ae26d 3030 wxPuts(_T("Server: lost a client."));
8dfea369 3031
ccdb23df 3032 socket->Destroy();
8dfea369 3033 }
9fc3cba7 3034
ccdb23df
VZ
3035 // same as "delete server" but is consistent with GUI programs
3036 server->Destroy();
8e907a13 3037}
2c8e4738
VZ
3038
3039static void TestSocketClient()
3040{
456ae26d 3041 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2c8e4738 3042
456ae26d 3043 static const wxChar *hostname = _T("www.wxwindows.org");
8e907a13
VZ
3044
3045 wxIPV4address addr;
3046 addr.Hostname(hostname);
3047 addr.Service(80);
3048
456ae26d 3049 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
2c8e4738
VZ
3050
3051 wxSocketClient client;
8e907a13 3052 if ( !client.Connect(addr) )
2c8e4738 3053 {
456ae26d 3054 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2c8e4738
VZ
3055 }
3056 else
3057 {
456ae26d 3058 wxPrintf(_T("--- Connected to %s:%u...\n"),
8e907a13
VZ
3059 addr.Hostname().c_str(), addr.Service());
3060
456ae26d 3061 wxChar buf[8192];
2c8e4738 3062
8e907a13
VZ
3063 // could use simply "GET" here I suppose
3064 wxString cmdGet =
456ae26d 3065 wxString::Format(_T("GET http://%s/\r\n"), hostname);
8e907a13 3066 client.Write(cmdGet, cmdGet.length());
456ae26d 3067 wxPrintf(_T("--- Sent command '%s' to the server\n"),
8e907a13 3068 MakePrintable(cmdGet).c_str());
2c8e4738 3069 client.Read(buf, WXSIZEOF(buf));
456ae26d 3070 wxPrintf(_T("--- Server replied:\n%s"), buf);
8e907a13
VZ
3071 }
3072}
3073
2e907fab
VZ
3074#endif // TEST_SOCKETS
3075
b92fd37c
VZ
3076// ----------------------------------------------------------------------------
3077// FTP
3078// ----------------------------------------------------------------------------
3079
2e907fab
VZ
3080#ifdef TEST_FTP
3081
e84010cf 3082#include "wx/protocol/ftp.h"
2e907fab 3083
b92fd37c
VZ
3084static wxFTP ftp;
3085
3086#define FTP_ANONYMOUS
3087
3088#ifdef FTP_ANONYMOUS
456ae26d
VZ
3089 static const wxChar *directory = _T("/pub");
3090 static const wxChar *filename = _T("welcome.msg");
b92fd37c 3091#else
456ae26d
VZ
3092 static const wxChar *directory = _T("/etc");
3093 static const wxChar *filename = _T("issue");
b92fd37c
VZ
3094#endif
3095
3096static bool TestFtpConnect()
8e907a13 3097{
456ae26d 3098 wxPuts(_T("*** Testing FTP connect ***"));
8e907a13 3099
b92fd37c 3100#ifdef FTP_ANONYMOUS
456ae26d 3101 static const wxChar *hostname = _T("ftp.wxwindows.org");
b92fd37c 3102
456ae26d 3103 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
b92fd37c 3104#else // !FTP_ANONYMOUS
456ae26d 3105 static const wxChar *hostname = "localhost";
b92fd37c 3106
456ae26d
VZ
3107 wxChar user[256];
3108 wxFgets(user, WXSIZEOF(user), stdin);
3109 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
b92fd37c
VZ
3110 ftp.SetUser(user);
3111
456ae26d
VZ
3112 wxChar password[256];
3113 wxPrintf(_T("Password for %s: "), password);
3114 wxFgets(password, WXSIZEOF(password), stdin);
3115 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
b92fd37c
VZ
3116 ftp.SetPassword(password);
3117
456ae26d 3118 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
b92fd37c
VZ
3119#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
3120
3121 if ( !ftp.Connect(hostname) )
3122 {
456ae26d 3123 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
b92fd37c
VZ
3124
3125 return FALSE;
3126 }
3127 else
3128 {
456ae26d 3129 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
b92fd37c
VZ
3130 hostname, ftp.Pwd().c_str());
3131 }
3132
3133 return TRUE;
3134}
b1229561 3135
b92fd37c
VZ
3136// test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
3137static void TestFtpWuFtpd()
3138{
3139 wxFTP ftp;
456ae26d 3140 static const wxChar *hostname = _T("ftp.eudora.com");
b1229561
VZ
3141 if ( !ftp.Connect(hostname) )
3142 {
456ae26d 3143 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
b1229561
VZ
3144 }
3145 else
3146 {
456ae26d 3147 static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
b1229561
VZ
3148 wxInputStream *in = ftp.GetInputStream(filename);
3149 if ( !in )
3150 {
456ae26d 3151 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
b1229561
VZ
3152 }
3153 else
3154 {
3155 size_t size = in->StreamSize();
456ae26d 3156 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
b1229561 3157
456ae26d 3158 wxChar *data = new wxChar[size];
b1229561
VZ
3159 if ( !in->Read(data, size) )
3160 {
456ae26d 3161 wxPuts(_T("ERROR: read error"));
b1229561
VZ
3162 }
3163 else
3164 {
456ae26d 3165 wxPrintf(_T("Successfully retrieved the file.\n"));
b1229561
VZ
3166 }
3167
3168 delete [] data;
3169 delete in;
3170 }
3171 }
b92fd37c 3172}
b1229561 3173
b92fd37c
VZ
3174static void TestFtpList()
3175{
456ae26d 3176 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
8e907a13 3177
b92fd37c
VZ
3178 // test CWD
3179 if ( !ftp.ChDir(directory) )
3180 {
456ae26d 3181 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
b92fd37c 3182 }
2e907fab 3183
456ae26d 3184 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2e907fab 3185
b92fd37c
VZ
3186 // test NLIST and LIST
3187 wxArrayString files;
3188 if ( !ftp.GetFilesList(files) )
8e907a13 3189 {
456ae26d 3190 wxPuts(_T("ERROR: failed to get NLIST of files"));
8e907a13
VZ
3191 }
3192 else
3193 {
456ae26d 3194 wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
b92fd37c
VZ
3195 size_t count = files.GetCount();
3196 for ( size_t n = 0; n < count; n++ )
8e907a13 3197 {
456ae26d 3198 wxPrintf(_T("\t%s\n"), files[n].c_str());
8e907a13 3199 }
456ae26d 3200 wxPuts(_T("End of the file list"));
b92fd37c 3201 }
8e907a13 3202
b92fd37c
VZ
3203 if ( !ftp.GetDirList(files) )
3204 {
456ae26d 3205 wxPuts(_T("ERROR: failed to get LIST of files"));
b92fd37c
VZ
3206 }
3207 else
3208 {
456ae26d 3209 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
b92fd37c
VZ
3210 size_t count = files.GetCount();
3211 for ( size_t n = 0; n < count; n++ )
8e907a13 3212 {
456ae26d 3213 wxPrintf(_T("\t%s\n"), files[n].c_str());
2e907fab 3214 }
456ae26d 3215 wxPuts(_T("End of the file list"));
b92fd37c
VZ
3216 }
3217
3218 if ( !ftp.ChDir(_T("..")) )
3219 {
456ae26d 3220 wxPuts(_T("ERROR: failed to cd to .."));
b92fd37c 3221 }
2e907fab 3222
456ae26d 3223 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
b92fd37c
VZ
3224}
3225
3226static void TestFtpDownload()
3227{
456ae26d 3228 wxPuts(_T("*** Testing wxFTP download ***\n"));
b92fd37c
VZ
3229
3230 // test RETR
3231 wxInputStream *in = ftp.GetInputStream(filename);
3232 if ( !in )
3233 {
456ae26d 3234 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
b92fd37c
VZ
3235 }
3236 else
3237 {
3238 size_t size = in->StreamSize();
456ae26d 3239 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
b92fd37c
VZ
3240 fflush(stdout);
3241
456ae26d 3242 wxChar *data = new wxChar[size];
b92fd37c 3243 if ( !in->Read(data, size) )
2e907fab 3244 {
456ae26d 3245 wxPuts(_T("ERROR: read error"));
2e907fab
VZ
3246 }
3247 else
3248 {
456ae26d 3249 wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
8e907a13
VZ
3250 }
3251
b92fd37c
VZ
3252 delete [] data;
3253 delete in;
3254 }
3255}
8e907a13 3256
b92fd37c
VZ
3257static void TestFtpFileSize()
3258{
456ae26d 3259 wxPuts(_T("*** Testing FTP SIZE command ***"));
b92fd37c
VZ
3260
3261 if ( !ftp.ChDir(directory) )
3262 {
456ae26d 3263 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
b92fd37c
VZ
3264 }
3265
456ae26d 3266 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
b92fd37c
VZ
3267
3268 if ( ftp.FileExists(filename) )
3269 {
3270 int size = ftp.GetFileSize(filename);
3271 if ( size == -1 )
456ae26d 3272 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
8e907a13 3273 else
456ae26d 3274 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
b92fd37c
VZ
3275 }
3276 else
3277 {
456ae26d 3278 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
b92fd37c
VZ
3279 }
3280}
3281
3282static void TestFtpMisc()
3283{
456ae26d 3284 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
b92fd37c
VZ
3285
3286 if ( ftp.SendCommand("STAT") != '2' )
3287 {
456ae26d 3288 wxPuts(_T("ERROR: STAT failed"));
b92fd37c
VZ
3289 }
3290 else
3291 {
456ae26d 3292 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
b92fd37c
VZ
3293 }
3294
3295 if ( ftp.SendCommand("HELP SITE") != '2' )
3296 {
456ae26d 3297 wxPuts(_T("ERROR: HELP SITE failed"));
b92fd37c
VZ
3298 }
3299 else
3300 {
456ae26d 3301 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
b92fd37c
VZ
3302 ftp.GetLastResult().c_str());
3303 }
3304}
3305
3306static void TestFtpInteractive()
3307{
456ae26d 3308 wxPuts(_T("\n*** Interactive wxFTP test ***"));
b92fd37c 3309
456ae26d 3310 wxChar buf[128];
b92fd37c
VZ
3311
3312 for ( ;; )
3313 {
456ae26d
VZ
3314 wxPrintf(_T("Enter FTP command: "));
3315 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
b92fd37c
VZ
3316 break;
3317
3318 // kill the last '\n'
456ae26d 3319 buf[wxStrlen(buf) - 1] = 0;
b92fd37c
VZ
3320
3321 // special handling of LIST and NLST as they require data connection
3322 wxString start(buf, 4);
3323 start.MakeUpper();
3324 if ( start == "LIST" || start == "NLST" )
8e907a13 3325 {
b92fd37c 3326 wxString wildcard;
456ae26d 3327 if ( wxStrlen(buf) > 4 )
b92fd37c 3328 wildcard = buf + 5;
8e907a13 3329
b92fd37c
VZ
3330 wxArrayString files;
3331 if ( !ftp.GetList(files, wildcard, start == "LIST") )
8e907a13 3332 {
456ae26d 3333 wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
8e907a13
VZ
3334 }
3335 else
3336 {
456ae26d 3337 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
b92fd37c
VZ
3338 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
3339 size_t count = files.GetCount();
3340 for ( size_t n = 0; n < count; n++ )
3341 {
456ae26d 3342 wxPrintf(_T("\t%s\n"), files[n].c_str());
b92fd37c 3343 }
456ae26d 3344 wxPuts(_T("--- End of the file list"));
8e907a13 3345 }
2e907fab 3346 }
b92fd37c 3347 else // !list
2e907fab 3348 {
456ae26d
VZ
3349 wxChar ch = ftp.SendCommand(buf);
3350 wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
b92fd37c
VZ
3351 if ( ch )
3352 {
456ae26d 3353 wxPrintf(_T(" (return code %c)"), ch);
b92fd37c 3354 }
2e907fab 3355
456ae26d 3356 wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
2e907fab 3357 }
2c8e4738 3358 }
b92fd37c 3359
456ae26d 3360 wxPuts(_T("\n*** done ***"));
2c8e4738
VZ
3361}
3362
b92fd37c 3363static void TestFtpUpload()
f6bcfd97 3364{
456ae26d 3365 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
f6bcfd97 3366
b92fd37c 3367 // upload a file
456ae26d
VZ
3368 static const wxChar *file1 = _T("test1");
3369 static const wxChar *file2 = _T("test2");
b92fd37c
VZ
3370 wxOutputStream *out = ftp.GetOutputStream(file1);
3371 if ( out )
3372 {
456ae26d 3373 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
b92fd37c
VZ
3374 out->Write("First hello", 11);
3375 delete out;
3376 }
f6bcfd97 3377
b92fd37c
VZ
3378 // send a command to check the remote file
3379 if ( ftp.SendCommand(wxString("STAT ") + file1) != '2' )
f6bcfd97 3380 {
456ae26d 3381 wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
f6bcfd97
BP
3382 }
3383 else
3384 {
456ae26d 3385 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
b92fd37c
VZ
3386 file1, ftp.GetLastResult().c_str());
3387 }
2e907fab 3388
b92fd37c
VZ
3389 out = ftp.GetOutputStream(file2);
3390 if ( out )
3391 {
456ae26d 3392 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
b92fd37c
VZ
3393 out->Write("Second hello", 12);
3394 delete out;
f6bcfd97
BP
3395 }
3396}
3397
2e907fab 3398#endif // TEST_FTP
2c8e4738 3399
83141d3a
VZ
3400// ----------------------------------------------------------------------------
3401// streams
3402// ----------------------------------------------------------------------------
3403
3404#ifdef TEST_STREAMS
3405
e84010cf
GD
3406#include "wx/wfstream.h"
3407#include "wx/mstream.h"
83141d3a 3408
24f25c8a
VZ
3409static void TestFileStream()
3410{
456ae26d 3411 wxPuts(_T("*** Testing wxFileInputStream ***"));
24f25c8a
VZ
3412
3413 static const wxChar *filename = _T("testdata.fs");
3414 {
3415 wxFileOutputStream fsOut(filename);
3416 fsOut.Write("foo", 3);
3417 }
3418
3419 wxFileInputStream fsIn(filename);
456ae26d 3420 wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
24f25c8a
VZ
3421 while ( !fsIn.Eof() )
3422 {
3423 putchar(fsIn.GetC());
3424 }
3425
3426 if ( !wxRemoveFile(filename) )
3427 {
456ae26d 3428 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename);
24f25c8a
VZ
3429 }
3430
456ae26d 3431 wxPuts(_T("\n*** wxFileInputStream test done ***"));
24f25c8a
VZ
3432}
3433
83141d3a
VZ
3434static void TestMemoryStream()
3435{
99a5af7f
VZ
3436 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
3437
3438 wxMemoryOutputStream memOutStream;
3439 wxPrintf(_T("Initially out stream offset: %lu\n"),
3440 (unsigned long)memOutStream.TellO());
3441
3442 for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
3443 {
3444 memOutStream.PutC(*p);
3445 }
3446
3447 wxPrintf(_T("Final out stream offset: %lu\n"),
3448 (unsigned long)memOutStream.TellO());
3449
3450 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
83141d3a
VZ
3451
3452 wxChar buf[1024];
99a5af7f 3453 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
83141d3a 3454
99a5af7f
VZ
3455 wxMemoryInputStream memInpStream(buf, len);
3456 wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
83141d3a
VZ
3457 while ( !memInpStream.Eof() )
3458 {
3459 putchar(memInpStream.GetC());
3460 }
3461
456ae26d 3462 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
83141d3a
VZ
3463}
3464
3465#endif // TEST_STREAMS
3466
d31b7b68
VZ
3467// ----------------------------------------------------------------------------
3468// timers
3469// ----------------------------------------------------------------------------
3470
3471#ifdef TEST_TIMER
3472
e84010cf
GD
3473#include "wx/timer.h"
3474#include "wx/utils.h"
d31b7b68
VZ
3475
3476static void TestStopWatch()
3477{
456ae26d 3478 wxPuts(_T("*** Testing wxStopWatch ***\n"));
d31b7b68
VZ
3479
3480 wxStopWatch sw;
677eff07 3481 sw.Pause();
456ae26d 3482 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
677eff07
VZ
3483 fflush(stdout);
3484 wxSleep(2);
456ae26d 3485 wxPrintf(_T("\t%ldms\n"), sw.Time());
677eff07 3486
456ae26d 3487 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
677eff07
VZ
3488 fflush(stdout);
3489 sw.Resume();
d31b7b68 3490 wxSleep(3);
456ae26d 3491 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
d31b7b68
VZ
3492
3493 sw.Pause();
456ae26d 3494 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
677eff07 3495 fflush(stdout);
d31b7b68 3496 wxSleep(2);
456ae26d 3497 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
d31b7b68
VZ
3498
3499 sw.Resume();
456ae26d 3500 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
677eff07
VZ
3501 fflush(stdout);
3502 wxSleep(2);
456ae26d 3503 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
87798c00
VZ
3504
3505 wxStopWatch sw2;
456ae26d 3506 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
87798c00
VZ
3507 for ( size_t n = 0; n < 70; n++ )
3508 {
3509 sw2.Start();
89e6463c
GRG
3510
3511 for ( size_t m = 0; m < 100000; m++ )
87798c00 3512 {
89e6463c
GRG
3513 if ( sw.Time() < 0 || sw2.Time() < 0 )
3514 {
456ae26d 3515 wxPuts(_T("\ntime is negative - ERROR!"));
89e6463c 3516 }
87798c00
VZ
3517 }
3518
3519 putchar('.');
677eff07 3520 fflush(stdout);
87798c00
VZ
3521 }
3522
456ae26d 3523 wxPuts(_T(", ok."));
d31b7b68
VZ
3524}
3525
3526#endif // TEST_TIMER
3527
f6bcfd97
BP
3528// ----------------------------------------------------------------------------
3529// vCard support
3530// ----------------------------------------------------------------------------
3531
3532#ifdef TEST_VCARD
3533
e84010cf 3534#include "wx/vcard.h"
f6bcfd97
BP
3535
3536static void DumpVObject(size_t level, const wxVCardObject& vcard)
3537{
3538 void *cookie;
3539 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
3540 while ( vcObj )
3541 {
456ae26d 3542 wxPrintf(_T("%s%s"),
f6bcfd97
BP
3543 wxString(_T('\t'), level).c_str(),
3544 vcObj->GetName().c_str());
3545
3546 wxString value;
3547 switch ( vcObj->GetType() )
3548 {
3549 case wxVCardObject::String:
3550 case wxVCardObject::UString:
3551 {
3552 wxString val;
3553 vcObj->GetValue(&val);
3554 value << _T('"') << val << _T('"');
3555 }
3556 break;
3557
3558 case wxVCardObject::Int:
3559 {
3560 unsigned int i;
3561 vcObj->GetValue(&i);
3562 value.Printf(_T("%u"), i);
3563 }
3564 break;
3565
3566 case wxVCardObject::Long:
3567 {
3568 unsigned long l;
3569 vcObj->GetValue(&l);
3570 value.Printf(_T("%lu"), l);
3571 }
3572 break;
3573
3574 case wxVCardObject::None:
3575 break;
3576
3577 case wxVCardObject::Object:
3578 value = _T("<node>");
3579 break;
3580
3581 default:
3582 value = _T("<unknown value type>");
3583 }
3584
3585 if ( !!value )
456ae26d 3586 wxPrintf(_T(" = %s"), value.c_str());
f6bcfd97
BP
3587 putchar('\n');
3588
3589 DumpVObject(level + 1, *vcObj);
3590
3591 delete vcObj;
3592 vcObj = vcard.GetNextProp(&cookie);
3593 }
3594}
3595
3596static void DumpVCardAddresses(const wxVCard& vcard)
3597{
456ae26d 3598 wxPuts(_T("\nShowing all addresses from vCard:\n"));
f6bcfd97
BP
3599
3600 size_t nAdr = 0;
3601 void *cookie;
3602 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
3603 while ( addr )
3604 {
3605 wxString flagsStr;
3606 int flags = addr->GetFlags();
3607 if ( flags & wxVCardAddress::Domestic )
3608 {
3609 flagsStr << _T("domestic ");
3610 }
3611 if ( flags & wxVCardAddress::Intl )
3612 {
3613 flagsStr << _T("international ");
3614 }
3615 if ( flags & wxVCardAddress::Postal )
3616 {
3617 flagsStr << _T("postal ");
3618 }
3619 if ( flags & wxVCardAddress::Parcel )
3620 {
3621 flagsStr << _T("parcel ");
3622 }
3623 if ( flags & wxVCardAddress::Home )
3624 {
3625 flagsStr << _T("home ");
3626 }
3627 if ( flags & wxVCardAddress::Work )
3628 {
3629 flagsStr << _T("work ");
3630 }
3631
456ae26d 3632 wxPrintf(_T("Address %u:\n")
f6bcfd97
BP
3633 "\tflags = %s\n"
3634 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3635 ++nAdr,
3636 flagsStr.c_str(),
3637 addr->GetPostOffice().c_str(),
3638 addr->GetExtAddress().c_str(),
3639 addr->GetStreet().c_str(),
3640 addr->GetLocality().c_str(),
3641 addr->GetRegion().c_str(),
3642 addr->GetPostalCode().c_str(),
3643 addr->GetCountry().c_str()
3644 );
3645
3646 delete addr;
3647 addr = vcard.GetNextAddress(&cookie);
3648 }
3649}
3650
3651static void DumpVCardPhoneNumbers(const wxVCard& vcard)
3652{
456ae26d 3653 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
f6bcfd97
BP
3654
3655 size_t nPhone = 0;
3656 void *cookie;
3657 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
3658 while ( phone )
3659 {
3660 wxString flagsStr;
3661 int flags = phone->GetFlags();
3662 if ( flags & wxVCardPhoneNumber::Voice )
3663 {
3664 flagsStr << _T("voice ");
3665 }
3666 if ( flags & wxVCardPhoneNumber::Fax )
3667 {
3668 flagsStr << _T("fax ");
3669 }
3670 if ( flags & wxVCardPhoneNumber::Cellular )
3671 {
3672 flagsStr << _T("cellular ");
3673 }
3674 if ( flags & wxVCardPhoneNumber::Modem )
3675 {
3676 flagsStr << _T("modem ");
3677 }
3678 if ( flags & wxVCardPhoneNumber::Home )
3679 {
3680 flagsStr << _T("home ");
3681 }
3682 if ( flags & wxVCardPhoneNumber::Work )
3683 {
3684 flagsStr << _T("work ");
3685 }
3686
456ae26d 3687 wxPrintf(_T("Phone number %u:\n")
f6bcfd97
BP
3688 "\tflags = %s\n"
3689 "\tvalue = %s\n",
3690 ++nPhone,
3691 flagsStr.c_str(),
3692 phone->GetNumber().c_str()
3693 );
3694
3695 delete phone;
3696 phone = vcard.GetNextPhoneNumber(&cookie);
3697 }
3698}
3699
3700static void TestVCardRead()
3701{
456ae26d 3702 wxPuts(_T("*** Testing wxVCard reading ***\n"));
f6bcfd97
BP
3703
3704 wxVCard vcard(_T("vcard.vcf"));
3705 if ( !vcard.IsOk() )
3706 {
456ae26d 3707 wxPuts(_T("ERROR: couldn't load vCard."));
f6bcfd97
BP
3708 }
3709 else
3710 {
3711 // read individual vCard properties
3712 wxVCardObject *vcObj = vcard.GetProperty("FN");
3713 wxString value;
3714 if ( vcObj )
3715 {
3716 vcObj->GetValue(&value);
3717 delete vcObj;
3718 }
3719 else
3720 {
3721 value = _T("<none>");
3722 }
3723
456ae26d 3724 wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str());
f6bcfd97
BP
3725
3726
3727 if ( !vcard.GetFullName(&value) )
3728 {
3729 value = _T("<none>");
3730 }
3731
456ae26d 3732 wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str());
f6bcfd97
BP
3733
3734 // now show how to deal with multiply occuring properties
3735 DumpVCardAddresses(vcard);
3736 DumpVCardPhoneNumbers(vcard);
3737
3738 // and finally show all
456ae26d 3739 wxPuts(_T("\nNow dumping the entire vCard:\n")
f6bcfd97
BP
3740 "-----------------------------\n");
3741
3742 DumpVObject(0, vcard);
3743 }
3744}
3745
3746static void TestVCardWrite()
3747{
456ae26d 3748 wxPuts(_T("*** Testing wxVCard writing ***\n"));
f6bcfd97
BP
3749
3750 wxVCard vcard;
3751 if ( !vcard.IsOk() )
3752 {
456ae26d 3753 wxPuts(_T("ERROR: couldn't create vCard."));
f6bcfd97
BP
3754 }
3755 else
3756 {
3757 // set some fields
3758 vcard.SetName("Zeitlin", "Vadim");
3759 vcard.SetFullName("Vadim Zeitlin");
3760 vcard.SetOrganization("wxWindows", "R&D");
3761
3762 // just dump the vCard back
456ae26d
VZ
3763 wxPuts(_T("Entire vCard follows:\n"));
3764 wxPuts(vcard.Write());
f6bcfd97
BP
3765 }
3766}
3767
3768#endif // TEST_VCARD
3769
0e2c5534
VZ
3770// ----------------------------------------------------------------------------
3771// wxVolume tests
3772// ----------------------------------------------------------------------------
3773
ba6ea19e 3774#if !defined(__WIN32__) || !wxUSE_FSVOLUME
0e2c5534
VZ
3775 #undef TEST_VOLUME
3776#endif
3777
3778#ifdef TEST_VOLUME
3779
3780#include "wx/volume.h"
3781
3782static const wxChar *volumeKinds[] =
3783{
3784 _T("floppy"),
3785 _T("hard disk"),
3786 _T("CD-ROM"),
3787 _T("DVD-ROM"),
3788 _T("network volume"),
3789 _T("other volume"),
3790};
3791
3792static void TestFSVolume()
3793{
3794 wxPuts(_T("*** Testing wxFSVolume class ***"));
3795
3796 wxArrayString volumes = wxFSVolume::GetVolumes();
3797 size_t count = volumes.GetCount();
3798
3799 if ( !count )
3800 {
3801 wxPuts(_T("ERROR: no mounted volumes?"));
3802 return;
3803 }
3804
3805 wxPrintf(_T("%u mounted volumes found:\n"), count);
3806
3807 for ( size_t n = 0; n < count; n++ )
3808 {
3809 wxFSVolume vol(volumes[n]);
3810 if ( !vol.IsOk() )
3811 {
3812 wxPuts(_T("ERROR: couldn't create volume"));
3813 continue;
3814 }
3815
3816 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3817 n + 1,
3818 vol.GetDisplayName().c_str(),
3819 vol.GetName().c_str(),
3820 volumeKinds[vol.GetKind()],
3821 vol.IsWritable() ? _T("rw") : _T("ro"),
3822 vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable")
3823 : _T("fixed"));
3824 }
3825}
3826
3827#endif // TEST_VOLUME
3828
f6bcfd97 3829// ----------------------------------------------------------------------------
e7d41190 3830// wide char and Unicode support
f6bcfd97
BP
3831// ----------------------------------------------------------------------------
3832
e7d41190
VZ
3833#ifdef TEST_UNICODE
3834
3835static void TestUnicodeToFromAscii()
3836{
3837 wxPuts(_T("Testing wxString::To/FromAscii()\n"));
3838
3839 static const char *msg = "Hello, world!";
3840 wxString s = wxString::FromAscii(msg);
3841
3842 wxPrintf(_T("Message in Unicode: %s\n"), s.c_str());
7aeebdcd 3843 printf("Message in ASCII: %s\n", (const char *)s.ToAscii());
e7d41190
VZ
3844
3845 wxPutchar(_T('\n'));
3846}
3847
3848#endif // TEST_UNICODE
3849
f6bcfd97
BP
3850#ifdef TEST_WCHAR
3851
e84010cf
GD
3852#include "wx/strconv.h"
3853#include "wx/fontenc.h"
3854#include "wx/encconv.h"
3855#include "wx/buffer.h"
f6bcfd97 3856
456ae26d 3857static const unsigned char textInUtf8_[] =
ac511156
VZ
3858{
3859 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3860 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3861 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3862 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3863 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3864 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3865 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3866};
3867
456ae26d
VZ
3868#define textInUtf8 ((const char *)textInUtf8_)
3869
f6bcfd97
BP
3870static void TestUtf8()
3871{
456ae26d 3872 wxPuts(_T("*** Testing UTF8 support ***\n"));
f6bcfd97 3873
24f25c8a
VZ
3874 char buf[1024];
3875 wchar_t wbuf[1024];
3876 if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
f6bcfd97 3877 {
456ae26d 3878 wxPuts(_T("ERROR: UTF-8 decoding failed."));
f6bcfd97 3879 }
24f25c8a
VZ
3880 else
3881 {
24f25c8a
VZ
3882 wxCSConv conv(_T("koi8-r"));
3883 if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 )
3884 {
456ae26d 3885 wxPuts(_T("ERROR: conversion to KOI8-R failed."));
24f25c8a
VZ
3886 }
3887 else
ac511156 3888 {
456ae26d 3889 wxPrintf(_T("The resulting string (in KOI8-R): %s\n"), buf);
ac511156
VZ
3890 }
3891 }
3892
3893