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