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