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