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