]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
fix include file
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
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
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/defs.h"
21
22 #include <stdio.h>
23
24 #include "wx/string.h"
25 #include "wx/file.h"
26 #include "wx/filename.h"
27 #include "wx/app.h"
28 #include "wx/log.h"
29 #include "wx/apptrait.h"
30 #include "wx/platinfo.h"
31 #include "wx/wxchar.h"
32
33 // without this pragma, the stupid compiler precompiles #defines below so that
34 // changing them doesn't "take place" later!
35 #ifdef __VISUALC__
36 #pragma hdrstop
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // conditional compilation
41 // ----------------------------------------------------------------------------
42
43 /*
44 A note about all these conditional compilation macros: this file is used
45 both as a test suite for various non-GUI wxWidgets classes and as a
46 scratchpad for quick tests. So there are two compilation modes: if you
47 define TEST_ALL all tests are run, otherwise you may enable the individual
48 tests individually in the "#else" branch below.
49 */
50
51 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
52 // test, define it to 1 to do all tests.
53 #define TEST_ALL 1
54
55
56 #if TEST_ALL
57 #define TEST_CMDLINE
58 #define TEST_DATETIME
59 #define TEST_DIR
60 #define TEST_DYNLIB
61 #define TEST_ENVIRON
62 #define TEST_EXECUTE
63 #define TEST_FILE
64 #define TEST_FILECONF
65 #define TEST_FILENAME
66 #define TEST_FILETIME
67 // #define TEST_FTP --FIXME! (RN)
68 #define TEST_INFO_FUNCTIONS
69 #define TEST_LOCALE
70 #define TEST_LOG
71 #define TEST_MIME
72 #define TEST_MODULE
73 #define TEST_PATHLIST
74 #define TEST_PRINTF
75 #define TEST_REGCONF
76 #define TEST_REGEX
77 #define TEST_REGISTRY
78 #define TEST_SCOPEGUARD
79 #define TEST_SNGLINST
80 // #define TEST_SOCKETS --FIXME! (RN)
81 #define TEST_STACKWALKER
82 #define TEST_STDPATHS
83 #define TEST_STREAMS
84 #define TEST_TEXTSTREAM
85 #define TEST_THREADS
86 #define TEST_TIMER
87 // #define TEST_VCARD -- don't enable this (VZ)
88 // #define TEST_VOLUME --FIXME! (RN)
89 #define TEST_WCHAR
90 #define TEST_ZIP
91 #else // #if TEST_ALL
92 #define TEST_EXECUTE
93 #endif
94
95 // some tests are interactive, define this to run them
96 #ifdef TEST_INTERACTIVE
97 #undef TEST_INTERACTIVE
98
99 #define TEST_INTERACTIVE 1
100 #else
101 #define TEST_INTERACTIVE 0
102 #endif
103
104 // ============================================================================
105 // implementation
106 // ============================================================================
107
108 // ----------------------------------------------------------------------------
109 // helper functions
110 // ----------------------------------------------------------------------------
111
112 #if defined(TEST_SOCKETS)
113
114 // replace TABs with \t and CRs with \n
115 static wxString MakePrintable(const wxChar *s)
116 {
117 wxString str(s);
118 (void)str.Replace(_T("\t"), _T("\\t"));
119 (void)str.Replace(_T("\n"), _T("\\n"));
120 (void)str.Replace(_T("\r"), _T("\\r"));
121
122 return str;
123 }
124
125 #endif // MakePrintable() is used
126
127 // ----------------------------------------------------------------------------
128 // wxCmdLineParser
129 // ----------------------------------------------------------------------------
130
131 #ifdef TEST_CMDLINE
132
133 #include "wx/cmdline.h"
134 #include "wx/datetime.h"
135
136 #if wxUSE_CMDLINE_PARSER
137
138 static void ShowCmdLine(const wxCmdLineParser& parser)
139 {
140 wxString s = _T("Command line parsed successfully:\nInput files: ");
141
142 size_t count = parser.GetParamCount();
143 for ( size_t param = 0; param < count; param++ )
144 {
145 s << parser.GetParam(param) << ' ';
146 }
147
148 s << '\n'
149 << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n'
150 << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n';
151
152 wxString strVal;
153 long lVal;
154 double dVal;
155 wxDateTime dt;
156 if ( parser.Found(_T("o"), &strVal) )
157 s << _T("Output file:\t") << strVal << '\n';
158 if ( parser.Found(_T("i"), &strVal) )
159 s << _T("Input dir:\t") << strVal << '\n';
160 if ( parser.Found(_T("s"), &lVal) )
161 s << _T("Size:\t") << lVal << '\n';
162 if ( parser.Found(_T("f"), &dVal) )
163 s << _T("Double:\t") << dVal << '\n';
164 if ( parser.Found(_T("d"), &dt) )
165 s << _T("Date:\t") << dt.FormatISODate() << '\n';
166 if ( parser.Found(_T("project_name"), &strVal) )
167 s << _T("Project:\t") << strVal << '\n';
168
169 wxLogMessage(s);
170 }
171
172 #endif // wxUSE_CMDLINE_PARSER
173
174 static void TestCmdLineConvert()
175 {
176 static const wxChar *cmdlines[] =
177 {
178 _T("arg1 arg2"),
179 _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
180 _T("literal \\\" and \"\""),
181 };
182
183 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
184 {
185 const wxChar *cmdline = cmdlines[n];
186 wxPrintf(_T("Parsing: %s\n"), cmdline);
187 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
188
189 size_t count = args.GetCount();
190 wxPrintf(_T("\targc = %u\n"), count);
191 for ( size_t arg = 0; arg < count; arg++ )
192 {
193 wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str());
194 }
195 }
196 }
197
198 #endif // TEST_CMDLINE
199
200 // ----------------------------------------------------------------------------
201 // wxDir
202 // ----------------------------------------------------------------------------
203
204 #ifdef TEST_DIR
205
206 #include "wx/dir.h"
207
208 #ifdef __UNIX__
209 static const wxChar *ROOTDIR = _T("/");
210 static const wxChar *TESTDIR = _T("/usr/local/share");
211 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
212 static const wxChar *ROOTDIR = _T("c:\\");
213 static const wxChar *TESTDIR = _T("d:\\");
214 #else
215 #error "don't know where the root directory is"
216 #endif
217
218 static void TestDirEnumHelper(wxDir& dir,
219 int flags = wxDIR_DEFAULT,
220 const wxString& filespec = wxEmptyString)
221 {
222 wxString filename;
223
224 if ( !dir.IsOpened() )
225 return;
226
227 bool cont = dir.GetFirst(&filename, filespec, flags);
228 while ( cont )
229 {
230 wxPrintf(_T("\t%s\n"), filename.c_str());
231
232 cont = dir.GetNext(&filename);
233 }
234
235 wxPuts(wxEmptyString);
236 }
237
238 #if TEST_ALL
239
240 static void TestDirEnum()
241 {
242 wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***"));
243
244 wxString cwd = wxGetCwd();
245 if ( !wxDir::Exists(cwd) )
246 {
247 wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
248 return;
249 }
250
251 wxDir dir(cwd);
252 if ( !dir.IsOpened() )
253 {
254 wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
255 return;
256 }
257
258 wxPuts(_T("Enumerating everything in current directory:"));
259 TestDirEnumHelper(dir);
260
261 wxPuts(_T("Enumerating really everything in current directory:"));
262 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
263
264 wxPuts(_T("Enumerating object files in current directory:"));
265 TestDirEnumHelper(dir, wxDIR_DEFAULT, _T("*.o*"));
266
267 wxPuts(_T("Enumerating directories in current directory:"));
268 TestDirEnumHelper(dir, wxDIR_DIRS);
269
270 wxPuts(_T("Enumerating files in current directory:"));
271 TestDirEnumHelper(dir, wxDIR_FILES);
272
273 wxPuts(_T("Enumerating files including hidden in current directory:"));
274 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
275
276 dir.Open(ROOTDIR);
277
278 wxPuts(_T("Enumerating everything in root directory:"));
279 TestDirEnumHelper(dir, wxDIR_DEFAULT);
280
281 wxPuts(_T("Enumerating directories in root directory:"));
282 TestDirEnumHelper(dir, wxDIR_DIRS);
283
284 wxPuts(_T("Enumerating files in root directory:"));
285 TestDirEnumHelper(dir, wxDIR_FILES);
286
287 wxPuts(_T("Enumerating files including hidden in root directory:"));
288 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
289
290 wxPuts(_T("Enumerating files in non existing directory:"));
291 wxDir dirNo(_T("nosuchdir"));
292 TestDirEnumHelper(dirNo);
293 }
294
295 #endif // TEST_ALL
296
297 class DirPrintTraverser : public wxDirTraverser
298 {
299 public:
300 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
301 {
302 return wxDIR_CONTINUE;
303 }
304
305 virtual wxDirTraverseResult OnDir(const wxString& dirname)
306 {
307 wxString path, name, ext;
308 wxFileName::SplitPath(dirname, &path, &name, &ext);
309
310 if ( !ext.empty() )
311 name << _T('.') << ext;
312
313 wxString indent;
314 for ( const wxChar *p = path.c_str(); *p; p++ )
315 {
316 if ( wxIsPathSeparator(*p) )
317 indent += _T(" ");
318 }
319
320 wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str());
321
322 return wxDIR_CONTINUE;
323 }
324 };
325
326 static void TestDirTraverse()
327 {
328 wxPuts(_T("*** Testing wxDir::Traverse() ***"));
329
330 // enum all files
331 wxArrayString files;
332 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
333 wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR);
334 if ( n > 1 )
335 {
336 wxPrintf(_T("First one is '%s'\n"), files[0u].c_str());
337 wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str());
338 }
339
340 // enum again with custom traverser
341 wxPuts(_T("Now enumerating directories:"));
342 wxDir dir(TESTDIR);
343 DirPrintTraverser traverser;
344 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
345 }
346
347 #if TEST_ALL
348
349 static void TestDirExists()
350 {
351 wxPuts(_T("*** Testing wxDir::Exists() ***"));
352
353 static const wxChar *dirnames[] =
354 {
355 _T("."),
356 #if defined(__WXMSW__)
357 _T("c:"),
358 _T("c:\\"),
359 _T("\\\\share\\file"),
360 _T("c:\\dos"),
361 _T("c:\\dos\\"),
362 _T("c:\\dos\\\\"),
363 _T("c:\\autoexec.bat"),
364 #elif defined(__UNIX__)
365 _T("/"),
366 _T("//"),
367 _T("/usr/bin"),
368 _T("/usr//bin"),
369 _T("/usr///bin"),
370 #endif
371 };
372
373 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
374 {
375 wxPrintf(_T("%-40s: %s\n"),
376 dirnames[n],
377 wxDir::Exists(dirnames[n]) ? _T("exists")
378 : _T("doesn't exist"));
379 }
380 }
381
382 #endif // TEST_ALL
383
384 #endif // TEST_DIR
385
386 // ----------------------------------------------------------------------------
387 // wxDllLoader
388 // ----------------------------------------------------------------------------
389
390 #ifdef TEST_DYNLIB
391
392 #include "wx/dynlib.h"
393
394 static void TestDllLoad()
395 {
396 #if defined(__WXMSW__)
397 static const wxChar *LIB_NAME = _T("kernel32.dll");
398 static const wxChar *FUNC_NAME = _T("lstrlenA");
399 #elif defined(__UNIX__)
400 // weird: using just libc.so does *not* work!
401 static const wxChar *LIB_NAME = _T("/lib/libc.so.6");
402 static const wxChar *FUNC_NAME = _T("strlen");
403 #else
404 #error "don't know how to test wxDllLoader on this platform"
405 #endif
406
407 wxPuts(_T("*** testing basic wxDynamicLibrary functions ***\n"));
408
409 wxDynamicLibrary lib(LIB_NAME);
410 if ( !lib.IsLoaded() )
411 {
412 wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME);
413 }
414 else
415 {
416 typedef int (wxSTDCALL *wxStrlenType)(const char *);
417 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
418 if ( !pfnStrlen )
419 {
420 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
421 FUNC_NAME, LIB_NAME);
422 }
423 else
424 {
425 wxPrintf(_T("Calling %s dynamically loaded from %s "),
426 FUNC_NAME, LIB_NAME);
427
428 if ( pfnStrlen("foo") != 3 )
429 {
430 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
431 }
432 else
433 {
434 wxPuts(_T("... ok"));
435 }
436 }
437
438 #ifdef __WXMSW__
439 static const wxChar *FUNC_NAME_AW = _T("lstrlen");
440
441 typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
442 wxStrlenTypeAorW
443 pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
444 if ( !pfnStrlenAorW )
445 {
446 wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"),
447 FUNC_NAME_AW, LIB_NAME);
448 }
449 else
450 {
451 if ( pfnStrlenAorW(_T("foobar")) != 6 )
452 {
453 wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n"));
454 }
455 }
456 #endif // __WXMSW__
457 }
458 }
459
460 #if defined(__WXMSW__) || defined(__UNIX__)
461
462 static void TestDllListLoaded()
463 {
464 wxPuts(_T("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
465
466 puts("\nLoaded modules:");
467 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
468 const size_t count = dlls.GetCount();
469 for ( size_t n = 0; n < count; ++n )
470 {
471 const wxDynamicLibraryDetails& details = dlls[n];
472 printf("%-45s", (const char *)details.GetPath().mb_str());
473
474 void *addr;
475 size_t len;
476 if ( details.GetAddress(&addr, &len) )
477 {
478 printf(" %08lx:%08lx",
479 (unsigned long)addr, (unsigned long)((char *)addr + len));
480 }
481
482 printf(" %s\n", (const char *)details.GetVersion().mb_str());
483 }
484 }
485
486 #endif
487
488 #endif // TEST_DYNLIB
489
490 // ----------------------------------------------------------------------------
491 // wxGet/SetEnv
492 // ----------------------------------------------------------------------------
493
494 #ifdef TEST_ENVIRON
495
496 #include "wx/utils.h"
497
498 static wxString MyGetEnv(const wxString& var)
499 {
500 wxString val;
501 if ( !wxGetEnv(var, &val) )
502 val = _T("<empty>");
503 else
504 val = wxString(_T('\'')) + val + _T('\'');
505
506 return val;
507 }
508
509 static void TestEnvironment()
510 {
511 const wxChar *var = _T("wxTestVar");
512
513 wxPuts(_T("*** testing environment access functions ***"));
514
515 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
516 wxSetEnv(var, _T("value for wxTestVar"));
517 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
518 wxSetEnv(var, _T("another value"));
519 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
520 wxUnsetEnv(var);
521 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
522 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str());
523 }
524
525 #endif // TEST_ENVIRON
526
527 // ----------------------------------------------------------------------------
528 // wxExecute
529 // ----------------------------------------------------------------------------
530
531 #ifdef TEST_EXECUTE
532
533 #include "wx/utils.h"
534
535 static void TestExecute()
536 {
537 wxPuts(_T("*** testing wxExecute ***"));
538
539 #ifdef __UNIX__
540 #define COMMAND "echo hi"
541 #define ASYNC_COMMAND "xclock"
542 #define SHELL_COMMAND "echo hi from shell"
543 #define REDIRECT_COMMAND "cat -n Makefile"
544 #elif defined(__WXMSW__)
545 #define COMMAND "command.com /c echo hi"
546 #define ASYNC_COMMAND "notepad"
547 #define SHELL_COMMAND "echo hi"
548 #define REDIRECT_COMMAND COMMAND
549 #else
550 #error "no command to exec"
551 #endif // OS
552
553 wxPrintf(_T("Testing wxShell: "));
554 fflush(stdout);
555 if ( wxShell(_T(SHELL_COMMAND)) )
556 wxPuts(_T("Ok."));
557 else
558 wxPuts(_T("ERROR."));
559
560 wxPrintf(_T("Testing wxExecute: "));
561 fflush(stdout);
562 if ( wxExecute(_T(COMMAND), wxEXEC_SYNC) == 0 )
563 wxPuts(_T("Ok."));
564 else
565 wxPuts(_T("ERROR."));
566
567 wxPrintf(_T("Testing async wxExecute: "));
568 fflush(stdout);
569 int pid = wxExecute(ASYNC_COMMAND);
570 if ( pid != 0 )
571 {
572 wxPuts(_T("Ok (command launched)."));
573 if ( wxKill(pid) == -1 )
574 wxPuts("ERROR: failed to kill child process.");
575 }
576 else
577 wxPuts(_T("ERROR."));
578
579 wxPrintf(_T("Testing wxExecute with redirection:\n"));
580 wxArrayString output;
581 if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 )
582 {
583 wxPuts(_T("ERROR."));
584 }
585 else
586 {
587 // don't show too much output, MAX_LINES is enough
588 static const unsigned MAX_LINES = 20;
589
590 const unsigned count = output.size();
591 for ( unsigned n = 0;
592 n < (count > MAX_LINES ? MAX_LINES/2 : count);
593 n++ )
594 {
595 wxPrintf("%04u:\t%s\n", n + 1, output[n]);
596 }
597
598 if ( count > MAX_LINES )
599 {
600 wxPrintf("... skipping %u lines...\n", count - MAX_LINES);
601
602 for ( unsigned n = count - MAX_LINES/2; n < count; n++ )
603 {
604 wxPrintf("%04u:\t%s\n", n + 1, output[n]);
605 }
606 }
607
608 wxPuts(_T("Ok."));
609 }
610 }
611
612 #endif // TEST_EXECUTE
613
614 // ----------------------------------------------------------------------------
615 // file
616 // ----------------------------------------------------------------------------
617
618 #ifdef TEST_FILE
619
620 #include "wx/file.h"
621 #include "wx/ffile.h"
622 #include "wx/textfile.h"
623
624 static void TestFileRead()
625 {
626 wxPuts(_T("*** wxFile read test ***"));
627
628 wxFile file(_T("testdata.fc"));
629 if ( file.IsOpened() )
630 {
631 wxPrintf(_T("File length: %lu\n"), file.Length());
632
633 wxPuts(_T("File dump:\n----------"));
634
635 static const size_t len = 1024;
636 wxChar buf[len];
637 for ( ;; )
638 {
639 size_t nRead = file.Read(buf, len);
640 if ( nRead == (size_t)wxInvalidOffset )
641 {
642 wxPrintf(_T("Failed to read the file."));
643 break;
644 }
645
646 fwrite(buf, nRead, 1, stdout);
647
648 if ( nRead < len )
649 break;
650 }
651
652 wxPuts(_T("----------"));
653 }
654 else
655 {
656 wxPrintf(_T("ERROR: can't open test file.\n"));
657 }
658
659 wxPuts(wxEmptyString);
660 }
661
662 static void TestTextFileRead()
663 {
664 wxPuts(_T("*** wxTextFile read test ***"));
665
666 wxTextFile file(_T("testdata.fc"));
667 if ( file.Open() )
668 {
669 wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount());
670 wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str());
671
672 wxString s;
673
674 wxPuts(_T("\nDumping the entire file:"));
675 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
676 {
677 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
678 }
679 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
680
681 wxPuts(_T("\nAnd now backwards:"));
682 for ( s = file.GetLastLine();
683 file.GetCurrentLine() != 0;
684 s = file.GetPrevLine() )
685 {
686 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
687 }
688 wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
689 }
690 else
691 {
692 wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName());
693 }
694
695 wxPuts(wxEmptyString);
696 }
697
698 static void TestFileCopy()
699 {
700 wxPuts(_T("*** Testing wxCopyFile ***"));
701
702 static const wxChar *filename1 = _T("testdata.fc");
703 static const wxChar *filename2 = _T("test2");
704 if ( !wxCopyFile(filename1, filename2) )
705 {
706 wxPuts(_T("ERROR: failed to copy file"));
707 }
708 else
709 {
710 wxFFile f1(filename1, _T("rb")),
711 f2(filename2, _T("rb"));
712
713 if ( !f1.IsOpened() || !f2.IsOpened() )
714 {
715 wxPuts(_T("ERROR: failed to open file(s)"));
716 }
717 else
718 {
719 wxString s1, s2;
720 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
721 {
722 wxPuts(_T("ERROR: failed to read file(s)"));
723 }
724 else
725 {
726 if ( (s1.length() != s2.length()) ||
727 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
728 {
729 wxPuts(_T("ERROR: copy error!"));
730 }
731 else
732 {
733 wxPuts(_T("File was copied ok."));
734 }
735 }
736 }
737 }
738
739 if ( !wxRemoveFile(filename2) )
740 {
741 wxPuts(_T("ERROR: failed to remove the file"));
742 }
743
744 wxPuts(wxEmptyString);
745 }
746
747 static void TestTempFile()
748 {
749 wxPuts(_T("*** wxTempFile test ***"));
750
751 wxTempFile tmpFile;
752 if ( tmpFile.Open(_T("test2")) && tmpFile.Write(_T("the answer is 42")) )
753 {
754 if ( tmpFile.Commit() )
755 wxPuts(_T("File committed."));
756 else
757 wxPuts(_T("ERROR: could't commit temp file."));
758
759 wxRemoveFile(_T("test2"));
760 }
761
762 wxPuts(wxEmptyString);
763 }
764
765 #endif // TEST_FILE
766
767 // ----------------------------------------------------------------------------
768 // wxFileConfig
769 // ----------------------------------------------------------------------------
770
771 #ifdef TEST_FILECONF
772
773 #include "wx/confbase.h"
774 #include "wx/fileconf.h"
775
776 static const struct FileConfTestData
777 {
778 const wxChar *name; // value name
779 const wxChar *value; // the value from the file
780 } fcTestData[] =
781 {
782 { _T("value1"), _T("one") },
783 { _T("value2"), _T("two") },
784 { _T("novalue"), _T("default") },
785 };
786
787 static void TestFileConfRead()
788 {
789 wxPuts(_T("*** testing wxFileConfig loading/reading ***"));
790
791 wxFileConfig fileconf(_T("test"), wxEmptyString,
792 _T("testdata.fc"), wxEmptyString,
793 wxCONFIG_USE_RELATIVE_PATH);
794
795 // test simple reading
796 wxPuts(_T("\nReading config file:"));
797 wxString defValue(_T("default")), value;
798 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
799 {
800 const FileConfTestData& data = fcTestData[n];
801 value = fileconf.Read(data.name, defValue);
802 wxPrintf(_T("\t%s = %s "), data.name, value.c_str());
803 if ( value == data.value )
804 {
805 wxPuts(_T("(ok)"));
806 }
807 else
808 {
809 wxPrintf(_T("(ERROR: should be %s)\n"), data.value);
810 }
811 }
812
813 // test enumerating the entries
814 wxPuts(_T("\nEnumerating all root entries:"));
815 long dummy;
816 wxString name;
817 bool cont = fileconf.GetFirstEntry(name, dummy);
818 while ( cont )
819 {
820 wxPrintf(_T("\t%s = %s\n"),
821 name.c_str(),
822 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
823
824 cont = fileconf.GetNextEntry(name, dummy);
825 }
826
827 static const wxChar *testEntry = _T("TestEntry");
828 wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
829 fileconf.Write(testEntry, _T("A value"));
830 fileconf.DeleteEntry(testEntry);
831 wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n"));
832 }
833
834 #endif // TEST_FILECONF
835
836 // ----------------------------------------------------------------------------
837 // wxFileName
838 // ----------------------------------------------------------------------------
839
840 #ifdef TEST_FILENAME
841
842 #include "wx/filename.h"
843
844 #if 0
845 static void DumpFileName(const wxChar *desc, const wxFileName& fn)
846 {
847 wxPuts(desc);
848
849 wxString full = fn.GetFullPath();
850
851 wxString vol, path, name, ext;
852 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
853
854 wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
855 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
856
857 wxFileName::SplitPath(full, &path, &name, &ext);
858 wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"),
859 path.c_str(), name.c_str(), ext.c_str());
860
861 wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str());
862 wxPrintf(_T("with volume: \t'%s'\n"),
863 fn.GetPath(wxPATH_GET_VOLUME).c_str());
864 wxPrintf(_T("with separator:\t'%s'\n"),
865 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
866 wxPrintf(_T("with both: \t'%s'\n"),
867 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
868
869 wxPuts(_T("The directories in the path are:"));
870 wxArrayString dirs = fn.GetDirs();
871 size_t count = dirs.GetCount();
872 for ( size_t n = 0; n < count; n++ )
873 {
874 wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str());
875 }
876 }
877 #endif
878
879 static void TestFileNameTemp()
880 {
881 wxPuts(_T("*** testing wxFileName temp file creation ***"));
882
883 static const wxChar *tmpprefixes[] =
884 {
885 _T(""),
886 _T("foo"),
887 _T(".."),
888 _T("../bar"),
889 #ifdef __UNIX__
890 _T("/tmp/foo"),
891 _T("/tmp/foo/bar"), // this one must be an error
892 #endif // __UNIX__
893 };
894
895 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
896 {
897 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
898 if ( path.empty() )
899 {
900 // "error" is not in upper case because it may be ok
901 wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
902 }
903 else
904 {
905 wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"),
906 tmpprefixes[n], path.c_str());
907
908 if ( !wxRemoveFile(path) )
909 {
910 wxLogWarning(_T("Failed to remove temp file '%s'"),
911 path.c_str());
912 }
913 }
914 }
915 }
916
917 static void TestFileNameDirManip()
918 {
919 // TODO: test AppendDir(), RemoveDir(), ...
920 }
921
922 static void TestFileNameComparison()
923 {
924 // TODO!
925 }
926
927 static void TestFileNameOperations()
928 {
929 // TODO!
930 }
931
932 static void TestFileNameCwd()
933 {
934 // TODO!
935 }
936
937 #endif // TEST_FILENAME
938
939 // ----------------------------------------------------------------------------
940 // wxFileName time functions
941 // ----------------------------------------------------------------------------
942
943 #ifdef TEST_FILETIME
944
945 #include "wx/filename.h"
946 #include "wx/datetime.h"
947
948 static void TestFileGetTimes()
949 {
950 wxFileName fn(_T("testdata.fc"));
951
952 wxDateTime dtAccess, dtMod, dtCreate;
953 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
954 {
955 wxPrintf(_T("ERROR: GetTimes() failed.\n"));
956 }
957 else
958 {
959 static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
960
961 wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
962 wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
963 wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
964 wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
965 }
966 }
967
968 #if 0
969 static void TestFileSetTimes()
970 {
971 wxFileName fn(_T("testdata.fc"));
972
973 if ( !fn.Touch() )
974 {
975 wxPrintf(_T("ERROR: Touch() failed.\n"));
976 }
977 }
978 #endif
979
980 #endif // TEST_FILETIME
981
982 // ----------------------------------------------------------------------------
983 // wxLocale
984 // ----------------------------------------------------------------------------
985
986 #ifdef TEST_LOCALE
987
988 #include "wx/intl.h"
989 #include "wx/utils.h" // for wxSetEnv
990
991 static wxLocale gs_localeDefault;
992 // NOTE: don't init it here as it needs a wxAppTraits object
993 // and thus must be init-ed after creation of the wxInitializer
994 // class in the main()
995
996 // find the name of the language from its value
997 static const wxChar *GetLangName(int lang)
998 {
999 static const wxChar *languageNames[] =
1000 {
1001 _T("DEFAULT"),
1002 _T("UNKNOWN"),
1003 _T("ABKHAZIAN"),
1004 _T("AFAR"),
1005 _T("AFRIKAANS"),
1006 _T("ALBANIAN"),
1007 _T("AMHARIC"),
1008 _T("ARABIC"),
1009 _T("ARABIC_ALGERIA"),
1010 _T("ARABIC_BAHRAIN"),
1011 _T("ARABIC_EGYPT"),
1012 _T("ARABIC_IRAQ"),
1013 _T("ARABIC_JORDAN"),
1014 _T("ARABIC_KUWAIT"),
1015 _T("ARABIC_LEBANON"),
1016 _T("ARABIC_LIBYA"),
1017 _T("ARABIC_MOROCCO"),
1018 _T("ARABIC_OMAN"),
1019 _T("ARABIC_QATAR"),
1020 _T("ARABIC_SAUDI_ARABIA"),
1021 _T("ARABIC_SUDAN"),
1022 _T("ARABIC_SYRIA"),
1023 _T("ARABIC_TUNISIA"),
1024 _T("ARABIC_UAE"),
1025 _T("ARABIC_YEMEN"),
1026 _T("ARMENIAN"),
1027 _T("ASSAMESE"),
1028 _T("AYMARA"),
1029 _T("AZERI"),
1030 _T("AZERI_CYRILLIC"),
1031 _T("AZERI_LATIN"),
1032 _T("BASHKIR"),
1033 _T("BASQUE"),
1034 _T("BELARUSIAN"),
1035 _T("BENGALI"),
1036 _T("BHUTANI"),
1037 _T("BIHARI"),
1038 _T("BISLAMA"),
1039 _T("BRETON"),
1040 _T("BULGARIAN"),
1041 _T("BURMESE"),
1042 _T("CAMBODIAN"),
1043 _T("CATALAN"),
1044 _T("CHINESE"),
1045 _T("CHINESE_SIMPLIFIED"),
1046 _T("CHINESE_TRADITIONAL"),
1047 _T("CHINESE_HONGKONG"),
1048 _T("CHINESE_MACAU"),
1049 _T("CHINESE_SINGAPORE"),
1050 _T("CHINESE_TAIWAN"),
1051 _T("CORSICAN"),
1052 _T("CROATIAN"),
1053 _T("CZECH"),
1054 _T("DANISH"),
1055 _T("DUTCH"),
1056 _T("DUTCH_BELGIAN"),
1057 _T("ENGLISH"),
1058 _T("ENGLISH_UK"),
1059 _T("ENGLISH_US"),
1060 _T("ENGLISH_AUSTRALIA"),
1061 _T("ENGLISH_BELIZE"),
1062 _T("ENGLISH_BOTSWANA"),
1063 _T("ENGLISH_CANADA"),
1064 _T("ENGLISH_CARIBBEAN"),
1065 _T("ENGLISH_DENMARK"),
1066 _T("ENGLISH_EIRE"),
1067 _T("ENGLISH_JAMAICA"),
1068 _T("ENGLISH_NEW_ZEALAND"),
1069 _T("ENGLISH_PHILIPPINES"),
1070 _T("ENGLISH_SOUTH_AFRICA"),
1071 _T("ENGLISH_TRINIDAD"),
1072 _T("ENGLISH_ZIMBABWE"),
1073 _T("ESPERANTO"),
1074 _T("ESTONIAN"),
1075 _T("FAEROESE"),
1076 _T("FARSI"),
1077 _T("FIJI"),
1078 _T("FINNISH"),
1079 _T("FRENCH"),
1080 _T("FRENCH_BELGIAN"),
1081 _T("FRENCH_CANADIAN"),
1082 _T("FRENCH_LUXEMBOURG"),
1083 _T("FRENCH_MONACO"),
1084 _T("FRENCH_SWISS"),
1085 _T("FRISIAN"),
1086 _T("GALICIAN"),
1087 _T("GEORGIAN"),
1088 _T("GERMAN"),
1089 _T("GERMAN_AUSTRIAN"),
1090 _T("GERMAN_BELGIUM"),
1091 _T("GERMAN_LIECHTENSTEIN"),
1092 _T("GERMAN_LUXEMBOURG"),
1093 _T("GERMAN_SWISS"),
1094 _T("GREEK"),
1095 _T("GREENLANDIC"),
1096 _T("GUARANI"),
1097 _T("GUJARATI"),
1098 _T("HAUSA"),
1099 _T("HEBREW"),
1100 _T("HINDI"),
1101 _T("HUNGARIAN"),
1102 _T("ICELANDIC"),
1103 _T("INDONESIAN"),
1104 _T("INTERLINGUA"),
1105 _T("INTERLINGUE"),
1106 _T("INUKTITUT"),
1107 _T("INUPIAK"),
1108 _T("IRISH"),
1109 _T("ITALIAN"),
1110 _T("ITALIAN_SWISS"),
1111 _T("JAPANESE"),
1112 _T("JAVANESE"),
1113 _T("KANNADA"),
1114 _T("KASHMIRI"),
1115 _T("KASHMIRI_INDIA"),
1116 _T("KAZAKH"),
1117 _T("KERNEWEK"),
1118 _T("KINYARWANDA"),
1119 _T("KIRGHIZ"),
1120 _T("KIRUNDI"),
1121 _T("KONKANI"),
1122 _T("KOREAN"),
1123 _T("KURDISH"),
1124 _T("LAOTHIAN"),
1125 _T("LATIN"),
1126 _T("LATVIAN"),
1127 _T("LINGALA"),
1128 _T("LITHUANIAN"),
1129 _T("MACEDONIAN"),
1130 _T("MALAGASY"),
1131 _T("MALAY"),
1132 _T("MALAYALAM"),
1133 _T("MALAY_BRUNEI_DARUSSALAM"),
1134 _T("MALAY_MALAYSIA"),
1135 _T("MALTESE"),
1136 _T("MANIPURI"),
1137 _T("MAORI"),
1138 _T("MARATHI"),
1139 _T("MOLDAVIAN"),
1140 _T("MONGOLIAN"),
1141 _T("NAURU"),
1142 _T("NEPALI"),
1143 _T("NEPALI_INDIA"),
1144 _T("NORWEGIAN_BOKMAL"),
1145 _T("NORWEGIAN_NYNORSK"),
1146 _T("OCCITAN"),
1147 _T("ORIYA"),
1148 _T("OROMO"),
1149 _T("PASHTO"),
1150 _T("POLISH"),
1151 _T("PORTUGUESE"),
1152 _T("PORTUGUESE_BRAZILIAN"),
1153 _T("PUNJABI"),
1154 _T("QUECHUA"),
1155 _T("RHAETO_ROMANCE"),
1156 _T("ROMANIAN"),
1157 _T("RUSSIAN"),
1158 _T("RUSSIAN_UKRAINE"),
1159 _T("SAMOAN"),
1160 _T("SANGHO"),
1161 _T("SANSKRIT"),
1162 _T("SCOTS_GAELIC"),
1163 _T("SERBIAN"),
1164 _T("SERBIAN_CYRILLIC"),
1165 _T("SERBIAN_LATIN"),
1166 _T("SERBO_CROATIAN"),
1167 _T("SESOTHO"),
1168 _T("SETSWANA"),
1169 _T("SHONA"),
1170 _T("SINDHI"),
1171 _T("SINHALESE"),
1172 _T("SISWATI"),
1173 _T("SLOVAK"),
1174 _T("SLOVENIAN"),
1175 _T("SOMALI"),
1176 _T("SPANISH"),
1177 _T("SPANISH_ARGENTINA"),
1178 _T("SPANISH_BOLIVIA"),
1179 _T("SPANISH_CHILE"),
1180 _T("SPANISH_COLOMBIA"),
1181 _T("SPANISH_COSTA_RICA"),
1182 _T("SPANISH_DOMINICAN_REPUBLIC"),
1183 _T("SPANISH_ECUADOR"),
1184 _T("SPANISH_EL_SALVADOR"),
1185 _T("SPANISH_GUATEMALA"),
1186 _T("SPANISH_HONDURAS"),
1187 _T("SPANISH_MEXICAN"),
1188 _T("SPANISH_MODERN"),
1189 _T("SPANISH_NICARAGUA"),
1190 _T("SPANISH_PANAMA"),
1191 _T("SPANISH_PARAGUAY"),
1192 _T("SPANISH_PERU"),
1193 _T("SPANISH_PUERTO_RICO"),
1194 _T("SPANISH_URUGUAY"),
1195 _T("SPANISH_US"),
1196 _T("SPANISH_VENEZUELA"),
1197 _T("SUNDANESE"),
1198 _T("SWAHILI"),
1199 _T("SWEDISH"),
1200 _T("SWEDISH_FINLAND"),
1201 _T("TAGALOG"),
1202 _T("TAJIK"),
1203 _T("TAMIL"),
1204 _T("TATAR"),
1205 _T("TELUGU"),
1206 _T("THAI"),
1207 _T("TIBETAN"),
1208 _T("TIGRINYA"),
1209 _T("TONGA"),
1210 _T("TSONGA"),
1211 _T("TURKISH"),
1212 _T("TURKMEN"),
1213 _T("TWI"),
1214 _T("UIGHUR"),
1215 _T("UKRAINIAN"),
1216 _T("URDU"),
1217 _T("URDU_INDIA"),
1218 _T("URDU_PAKISTAN"),
1219 _T("UZBEK"),
1220 _T("UZBEK_CYRILLIC"),
1221 _T("UZBEK_LATIN"),
1222 _T("VIETNAMESE"),
1223 _T("VOLAPUK"),
1224 _T("WELSH"),
1225 _T("WOLOF"),
1226 _T("XHOSA"),
1227 _T("YIDDISH"),
1228 _T("YORUBA"),
1229 _T("ZHUANG"),
1230 _T("ZULU"),
1231 };
1232
1233 if ( (size_t)lang < WXSIZEOF(languageNames) )
1234 return languageNames[lang];
1235 else
1236 return _T("INVALID");
1237 }
1238
1239 static void TestDefaultLang()
1240 {
1241 wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***"));
1242
1243 gs_localeDefault.Init(wxLANGUAGE_ENGLISH);
1244
1245 static const wxChar *langStrings[] =
1246 {
1247 NULL, // system default
1248 _T("C"),
1249 _T("fr"),
1250 _T("fr_FR"),
1251 _T("en"),
1252 _T("en_GB"),
1253 _T("en_US"),
1254 _T("de_DE.iso88591"),
1255 _T("german"),
1256 _T("?"), // invalid lang spec
1257 _T("klingonese"), // I bet on some systems it does exist...
1258 };
1259
1260 wxPrintf(_T("The default system encoding is %s (%d)\n"),
1261 wxLocale::GetSystemEncodingName().c_str(),
1262 wxLocale::GetSystemEncoding());
1263
1264 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1265 {
1266 const wxChar *langStr = langStrings[n];
1267 if ( langStr )
1268 {
1269 // FIXME: this doesn't do anything at all under Windows, we need
1270 // to create a new wxLocale!
1271 wxSetEnv(_T("LC_ALL"), langStr);
1272 }
1273
1274 int lang = gs_localeDefault.GetSystemLanguage();
1275 wxPrintf(_T("Locale for '%s' is %s.\n"),
1276 langStr ? langStr : _T("system default"), GetLangName(lang));
1277 }
1278 }
1279
1280 #endif // TEST_LOCALE
1281
1282 // ----------------------------------------------------------------------------
1283 // MIME types
1284 // ----------------------------------------------------------------------------
1285
1286 #ifdef TEST_MIME
1287
1288 #include "wx/mimetype.h"
1289
1290 static void TestMimeEnum()
1291 {
1292 wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1293
1294 wxArrayString mimetypes;
1295
1296 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
1297
1298 wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
1299
1300 wxArrayString exts;
1301 wxString desc;
1302
1303 for ( size_t n = 0; n < count; n++ )
1304 {
1305 wxFileType *filetype =
1306 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
1307 if ( !filetype )
1308 {
1309 wxPrintf(_T("nothing known about the filetype '%s'!\n"),
1310 mimetypes[n].c_str());
1311 continue;
1312 }
1313
1314 filetype->GetDescription(&desc);
1315 filetype->GetExtensions(exts);
1316
1317 filetype->GetIcon(NULL);
1318
1319 wxString extsAll;
1320 for ( size_t e = 0; e < exts.GetCount(); e++ )
1321 {
1322 if ( e > 0 )
1323 extsAll << _T(", ");
1324 extsAll += exts[e];
1325 }
1326
1327 wxPrintf(_T("\t%s: %s (%s)\n"),
1328 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
1329 }
1330
1331 wxPuts(wxEmptyString);
1332 }
1333
1334 static void TestMimeFilename()
1335 {
1336 wxPuts(_T("*** Testing MIME type from filename query ***\n"));
1337
1338 static const wxChar *filenames[] =
1339 {
1340 _T("readme.txt"),
1341 _T("document.pdf"),
1342 _T("image.gif"),
1343 _T("picture.jpeg"),
1344 };
1345
1346 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1347 {
1348 const wxString fname = filenames[n];
1349 wxString ext = fname.AfterLast(_T('.'));
1350 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
1351 if ( !ft )
1352 {
1353 wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1354 }
1355 else
1356 {
1357 wxString desc;
1358 if ( !ft->GetDescription(&desc) )
1359 desc = _T("<no description>");
1360
1361 wxString cmd;
1362 if ( !ft->GetOpenCommand(&cmd,
1363 wxFileType::MessageParameters(fname, wxEmptyString)) )
1364 cmd = _T("<no command available>");
1365 else
1366 cmd = wxString(_T('"')) + cmd + _T('"');
1367
1368 wxPrintf(_T("To open %s (%s) do %s.\n"),
1369 fname.c_str(), desc.c_str(), cmd.c_str());
1370
1371 delete ft;
1372 }
1373 }
1374
1375 wxPuts(wxEmptyString);
1376 }
1377
1378 // these tests were broken by wxMimeTypesManager changes, temporarily disabling
1379 #if 0
1380
1381 static void TestMimeOverride()
1382 {
1383 wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
1384
1385 static const wxChar *mailcap = _T("/tmp/mailcap");
1386 static const wxChar *mimetypes = _T("/tmp/mime.types");
1387
1388 if ( wxFile::Exists(mailcap) )
1389 wxPrintf(_T("Loading mailcap from '%s': %s\n"),
1390 mailcap,
1391 wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
1392 else
1393 wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1394 mailcap);
1395
1396 if ( wxFile::Exists(mimetypes) )
1397 wxPrintf(_T("Loading mime.types from '%s': %s\n"),
1398 mimetypes,
1399 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
1400 else
1401 wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1402 mimetypes);
1403
1404 wxPuts(wxEmptyString);
1405 }
1406
1407 static void TestMimeAssociate()
1408 {
1409 wxPuts(_T("*** Testing creation of filetype association ***\n"));
1410
1411 wxFileTypeInfo ftInfo(
1412 _T("application/x-xyz"),
1413 _T("xyzview '%s'"), // open cmd
1414 _T(""), // print cmd
1415 _T("XYZ File"), // description
1416 _T(".xyz"), // extensions
1417 wxNullPtr // end of extensions
1418 );
1419 ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
1420
1421 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
1422 if ( !ft )
1423 {
1424 wxPuts(_T("ERROR: failed to create association!"));
1425 }
1426 else
1427 {
1428 // TODO: read it back
1429 delete ft;
1430 }
1431
1432 wxPuts(wxEmptyString);
1433 }
1434
1435 #endif // 0
1436
1437 #endif // TEST_MIME
1438
1439 // ----------------------------------------------------------------------------
1440 // module dependencies feature
1441 // ----------------------------------------------------------------------------
1442
1443 #ifdef TEST_MODULE
1444
1445 #include "wx/module.h"
1446
1447 class wxTestModule : public wxModule
1448 {
1449 protected:
1450 virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1451 virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1452 };
1453
1454 class wxTestModuleA : public wxTestModule
1455 {
1456 public:
1457 wxTestModuleA();
1458 private:
1459 DECLARE_DYNAMIC_CLASS(wxTestModuleA)
1460 };
1461
1462 class wxTestModuleB : public wxTestModule
1463 {
1464 public:
1465 wxTestModuleB();
1466 private:
1467 DECLARE_DYNAMIC_CLASS(wxTestModuleB)
1468 };
1469
1470 class wxTestModuleC : public wxTestModule
1471 {
1472 public:
1473 wxTestModuleC();
1474 private:
1475 DECLARE_DYNAMIC_CLASS(wxTestModuleC)
1476 };
1477
1478 class wxTestModuleD : public wxTestModule
1479 {
1480 public:
1481 wxTestModuleD();
1482 private:
1483 DECLARE_DYNAMIC_CLASS(wxTestModuleD)
1484 };
1485
1486 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC, wxModule)
1487 wxTestModuleC::wxTestModuleC()
1488 {
1489 AddDependency(CLASSINFO(wxTestModuleD));
1490 }
1491
1492 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA, wxModule)
1493 wxTestModuleA::wxTestModuleA()
1494 {
1495 AddDependency(CLASSINFO(wxTestModuleB));
1496 AddDependency(CLASSINFO(wxTestModuleD));
1497 }
1498
1499 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD, wxModule)
1500 wxTestModuleD::wxTestModuleD()
1501 {
1502 }
1503
1504 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB, wxModule)
1505 wxTestModuleB::wxTestModuleB()
1506 {
1507 AddDependency(CLASSINFO(wxTestModuleD));
1508 AddDependency(CLASSINFO(wxTestModuleC));
1509 }
1510
1511 #endif // TEST_MODULE
1512
1513 // ----------------------------------------------------------------------------
1514 // misc information functions
1515 // ----------------------------------------------------------------------------
1516
1517 #ifdef TEST_INFO_FUNCTIONS
1518
1519 #include "wx/utils.h"
1520
1521 #if TEST_INTERACTIVE
1522 static void TestDiskInfo()
1523 {
1524 wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
1525
1526 for ( ;; )
1527 {
1528 wxChar pathname[128];
1529 wxPrintf(_T("\nEnter a directory name: "));
1530 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
1531 break;
1532
1533 // kill the last '\n'
1534 pathname[wxStrlen(pathname) - 1] = 0;
1535
1536 wxLongLong total, free;
1537 if ( !wxGetDiskSpace(pathname, &total, &free) )
1538 {
1539 wxPuts(_T("ERROR: wxGetDiskSpace failed."));
1540 }
1541 else
1542 {
1543 wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
1544 (total / 1024).ToString().c_str(),
1545 (free / 1024).ToString().c_str(),
1546 pathname);
1547 }
1548 }
1549 }
1550 #endif // TEST_INTERACTIVE
1551
1552 static void TestOsInfo()
1553 {
1554 wxPuts(_T("*** Testing OS info functions ***\n"));
1555
1556 int major, minor;
1557 wxGetOsVersion(&major, &minor);
1558 wxPrintf(_T("Running under: %s, version %d.%d\n"),
1559 wxGetOsDescription().c_str(), major, minor);
1560
1561 wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1562
1563 wxPrintf(_T("Host name is %s (%s).\n"),
1564 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1565
1566 wxPuts(wxEmptyString);
1567 }
1568
1569 static void TestPlatformInfo()
1570 {
1571 wxPuts(_T("*** Testing wxPlatformInfo functions ***\n"));
1572
1573 // get this platform
1574 wxPlatformInfo plat;
1575
1576 wxPrintf(_T("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str());
1577 wxPrintf(_T("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str());
1578 wxPrintf(_T("Port ID name is: %s\n"), plat.GetPortIdName().c_str());
1579 wxPrintf(_T("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str());
1580 wxPrintf(_T("Architecture is: %s\n"), plat.GetArchName().c_str());
1581 wxPrintf(_T("Endianness is: %s\n"), plat.GetEndiannessName().c_str());
1582
1583 wxPuts(wxEmptyString);
1584 }
1585
1586 static void TestUserInfo()
1587 {
1588 wxPuts(_T("*** Testing user info functions ***\n"));
1589
1590 wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
1591 wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
1592 wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1593 wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1594
1595 wxPuts(wxEmptyString);
1596 }
1597
1598 #endif // TEST_INFO_FUNCTIONS
1599
1600 // ----------------------------------------------------------------------------
1601 // path list
1602 // ----------------------------------------------------------------------------
1603
1604 #ifdef TEST_PATHLIST
1605
1606 #ifdef __UNIX__
1607 #define CMD_IN_PATH _T("ls")
1608 #else
1609 #define CMD_IN_PATH _T("command.com")
1610 #endif
1611
1612 static void TestPathList()
1613 {
1614 wxPuts(_T("*** Testing wxPathList ***\n"));
1615
1616 wxPathList pathlist;
1617 pathlist.AddEnvList(_T("PATH"));
1618 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
1619 if ( path.empty() )
1620 {
1621 wxPrintf(_T("ERROR: command not found in the path.\n"));
1622 }
1623 else
1624 {
1625 wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
1626 }
1627 }
1628
1629 #endif // TEST_PATHLIST
1630
1631 // ----------------------------------------------------------------------------
1632 // regular expressions
1633 // ----------------------------------------------------------------------------
1634
1635 #ifdef TEST_REGEX
1636
1637 #include "wx/regex.h"
1638
1639 static void TestRegExInteractive()
1640 {
1641 wxPuts(_T("*** Testing RE interactively ***"));
1642
1643 for ( ;; )
1644 {
1645 wxChar pattern[128];
1646 wxPrintf(_T("\nEnter a pattern: "));
1647 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
1648 break;
1649
1650 // kill the last '\n'
1651 pattern[wxStrlen(pattern) - 1] = 0;
1652
1653 wxRegEx re;
1654 if ( !re.Compile(pattern) )
1655 {
1656 continue;
1657 }
1658
1659 wxChar text[128];
1660 for ( ;; )
1661 {
1662 wxPrintf(_T("Enter text to match: "));
1663 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
1664 break;
1665
1666 // kill the last '\n'
1667 text[wxStrlen(text) - 1] = 0;
1668
1669 if ( !re.Matches(text) )
1670 {
1671 wxPrintf(_T("No match.\n"));
1672 }
1673 else
1674 {
1675 wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
1676
1677 size_t start, len;
1678 for ( size_t n = 1; ; n++ )
1679 {
1680 if ( !re.GetMatch(&start, &len, n) )
1681 {
1682 break;
1683 }
1684
1685 wxPrintf(_T("Subexpr %u matched '%s'\n"),
1686 n, wxString(text + start, len).c_str());
1687 }
1688 }
1689 }
1690 }
1691 }
1692
1693 #endif // TEST_REGEX
1694
1695 // ----------------------------------------------------------------------------
1696 // printf() tests
1697 // ----------------------------------------------------------------------------
1698
1699 /*
1700 NB: this stuff was taken from the glibc test suite and modified to build
1701 in wxWidgets: if I read the copyright below properly, this shouldn't
1702 be a problem
1703 */
1704
1705 #ifdef TEST_PRINTF
1706
1707 #ifdef wxTEST_PRINTF
1708 // use our functions from wxchar.cpp
1709 #undef wxPrintf
1710 #undef wxSprintf
1711
1712 // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
1713 // in the tests below
1714 int wxPrintf( const wxChar *format, ... );
1715 int wxSprintf( wxChar *str, const wxChar *format, ... );
1716 #endif
1717
1718 #include "wx/longlong.h"
1719
1720 #include <float.h>
1721
1722 static void rfg1 (void);
1723 static void rfg2 (void);
1724
1725
1726 static void
1727 fmtchk (const wxChar *fmt)
1728 {
1729 (void) wxPrintf(_T("%s:\t`"), fmt);
1730 (void) wxPrintf(fmt, 0x12);
1731 (void) wxPrintf(_T("'\n"));
1732 }
1733
1734 static void
1735 fmtst1chk (const wxChar *fmt)
1736 {
1737 (void) wxPrintf(_T("%s:\t`"), fmt);
1738 (void) wxPrintf(fmt, 4, 0x12);
1739 (void) wxPrintf(_T("'\n"));
1740 }
1741
1742 static void
1743 fmtst2chk (const wxChar *fmt)
1744 {
1745 (void) wxPrintf(_T("%s:\t`"), fmt);
1746 (void) wxPrintf(fmt, 4, 4, 0x12);
1747 (void) wxPrintf(_T("'\n"));
1748 }
1749
1750 /* This page is covered by the following copyright: */
1751
1752 /* (C) Copyright C E Chew
1753 *
1754 * Feel free to copy, use and distribute this software provided:
1755 *
1756 * 1. you do not pretend that you wrote it
1757 * 2. you leave this copyright notice intact.
1758 */
1759
1760 /*
1761 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1762 */
1763
1764 #define DEC -123
1765 #define INT 255
1766 #define UNS (~0)
1767
1768 /* Formatted Output Test
1769 *
1770 * This exercises the output formatting code.
1771 */
1772
1773 wxChar *PointerNull = NULL;
1774
1775 static void
1776 fp_test (void)
1777 {
1778 int i, j, k, l;
1779 wxChar buf[7];
1780 wxChar *prefix = buf;
1781 wxChar tp[20];
1782
1783 wxPuts(_T("\nFormatted output test"));
1784 wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
1785 wxStrcpy(prefix, _T("%"));
1786 for (i = 0; i < 2; i++) {
1787 for (j = 0; j < 2; j++) {
1788 for (k = 0; k < 2; k++) {
1789 for (l = 0; l < 2; l++) {
1790 wxStrcpy(prefix, _T("%"));
1791 if (i == 0) wxStrcat(prefix, _T("-"));
1792 if (j == 0) wxStrcat(prefix, _T("+"));
1793 if (k == 0) wxStrcat(prefix, _T("#"));
1794 if (l == 0) wxStrcat(prefix, _T("0"));
1795 wxPrintf(_T("%5s |"), prefix);
1796 wxStrcpy(tp, prefix);
1797 wxStrcat(tp, _T("6d |"));
1798 wxPrintf(tp, DEC);
1799 wxStrcpy(tp, prefix);
1800 wxStrcat(tp, _T("6o |"));
1801 wxPrintf(tp, INT);
1802 wxStrcpy(tp, prefix);
1803 wxStrcat(tp, _T("6x |"));
1804 wxPrintf(tp, INT);
1805 wxStrcpy(tp, prefix);
1806 wxStrcat(tp, _T("6X |"));
1807 wxPrintf(tp, INT);
1808 wxStrcpy(tp, prefix);
1809 wxStrcat(tp, _T("6u |"));
1810 wxPrintf(tp, UNS);
1811 wxPrintf(_T("\n"));
1812 }
1813 }
1814 }
1815 }
1816 wxPrintf(_T("%10s\n"), PointerNull);
1817 wxPrintf(_T("%-10s\n"), PointerNull);
1818 }
1819
1820 static void TestPrintf()
1821 {
1822 static wxChar shortstr[] = _T("Hi, Z.");
1823 static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
1824 I am ready for my first lesson today.");
1825 int result = 0;
1826 wxString test_format;
1827
1828 fmtchk(_T("%.4x"));
1829 fmtchk(_T("%04x"));
1830 fmtchk(_T("%4.4x"));
1831 fmtchk(_T("%04.4x"));
1832 fmtchk(_T("%4.3x"));
1833 fmtchk(_T("%04.3x"));
1834
1835 fmtst1chk(_T("%.*x"));
1836 fmtst1chk(_T("%0*x"));
1837 fmtst2chk(_T("%*.*x"));
1838 fmtst2chk(_T("%0*.*x"));
1839
1840 wxString bad_format = _T("bad format:\t\"%b\"\n");
1841 wxPrintf(bad_format.c_str());
1842 wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
1843
1844 wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
1845 wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
1846 wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
1847 wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
1848 wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
1849 wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1850 wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1851 test_format = _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
1852 wxPrintf(test_format.c_str(), -123456);
1853 wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1854 wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1855
1856 test_format = _T("zero-padded string:\t\"%010s\"\n");
1857 wxPrintf(test_format.c_str(), shortstr);
1858 test_format = _T("left-adjusted Z string:\t\"%-010s\"\n");
1859 wxPrintf(test_format.c_str(), shortstr);
1860 wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
1861 wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
1862 wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull);
1863 wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
1864
1865 wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
1866 wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
1867 wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
1868 wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
1869 wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
1870 wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
1871 wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
1872 wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
1873 wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
1874 wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
1875 wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
1876 wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
1877
1878 wxPrintf (_T(" %6.5f\n"), .099999999860301614);
1879 wxPrintf (_T(" %6.5f\n"), .1);
1880 wxPrintf (_T("x%5.4fx\n"), .5);
1881
1882 wxPrintf (_T("%#03x\n"), 1);
1883
1884 //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
1885
1886 {
1887 double d = FLT_MIN;
1888 int niter = 17;
1889
1890 while (niter-- != 0)
1891 wxPrintf (_T("%.17e\n"), d / 2);
1892 fflush (stdout);
1893 }
1894
1895 #ifndef __WATCOMC__
1896 // Open Watcom cause compiler error here
1897 // Error! E173: col(24) floating-point constant too small to represent
1898 wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
1899 #endif
1900
1901 #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
1902 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
1903 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
1904 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
1905 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
1906 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
1907 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
1908 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
1909 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
1910 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
1911 #undef FORMAT
1912
1913 {
1914 wxChar buf[20];
1915 int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
1916
1917 wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1918 rc, WXSIZEOF(buf), buf);
1919 #if 0
1920 wxChar buf2[512];
1921 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1922 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
1923 #endif
1924 }
1925
1926 fp_test ();
1927
1928 wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
1929 wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
1930 wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
1931 wxPrintf (_T("%g should be 123.456\n"), 123.456);
1932 wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
1933 wxPrintf (_T("%g should be 10\n"), 10.0);
1934 wxPrintf (_T("%g should be 0.02\n"), 0.02);
1935
1936 {
1937 double x=1.0;
1938 wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
1939 }
1940
1941 {
1942 wxChar buf[200];
1943
1944 wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
1945
1946 result |= wxStrcmp (buf,
1947 _T("onetwo three "));
1948
1949 wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
1950 }
1951
1952 #ifdef wxLongLong_t
1953 {
1954 wxChar buf[200];
1955
1956 wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
1957 #if 0
1958 // for some reason below line fails under Borland
1959 wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
1960 #endif
1961
1962 if (wxStrcmp (buf, _T("40000000000")) != 0)
1963 {
1964 result = 1;
1965 wxPuts (_T("\tFAILED"));
1966 }
1967 wxUnusedVar(result);
1968 wxPuts (wxEmptyString);
1969 }
1970 #endif // wxLongLong_t
1971
1972 wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
1973 wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
1974
1975 wxPuts (_T("--- Should be no further output. ---"));
1976 rfg1 ();
1977 rfg2 ();
1978
1979 #if 0
1980 {
1981 wxChar bytes[7];
1982 wxChar buf[20];
1983
1984 memset (bytes, '\xff', sizeof bytes);
1985 wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
1986 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
1987 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
1988 {
1989 wxPuts (_T("%hhn overwrite more bytes"));
1990 result = 1;
1991 }
1992 if (bytes[3] != 3)
1993 {
1994 wxPuts (_T("%hhn wrote incorrect value"));
1995 result = 1;
1996 }
1997 }
1998 #endif
1999 }
2000
2001 static void
2002 rfg1 (void)
2003 {
2004 wxChar buf[100];
2005
2006 wxSprintf (buf, _T("%5.s"), _T("xyz"));
2007 if (wxStrcmp (buf, _T(" ")) != 0)
2008 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
2009 wxSprintf (buf, _T("%5.f"), 33.3);
2010 if (wxStrcmp (buf, _T(" 33")) != 0)
2011 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
2012 wxSprintf (buf, _T("%8.e"), 33.3e7);
2013 if (wxStrcmp (buf, _T(" 3e+08")) != 0)
2014 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
2015 wxSprintf (buf, _T("%8.E"), 33.3e7);
2016 if (wxStrcmp (buf, _T(" 3E+08")) != 0)
2017 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
2018 wxSprintf (buf, _T("%.g"), 33.3);
2019 if (wxStrcmp (buf, _T("3e+01")) != 0)
2020 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
2021 wxSprintf (buf, _T("%.G"), 33.3);
2022 if (wxStrcmp (buf, _T("3E+01")) != 0)
2023 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
2024 }
2025
2026 static void
2027 rfg2 (void)
2028 {
2029 int prec;
2030 wxChar buf[100];
2031 wxString test_format;
2032
2033 prec = 0;
2034 wxSprintf (buf, _T("%.*g"), prec, 3.3);
2035 if (wxStrcmp (buf, _T("3")) != 0)
2036 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2037 prec = 0;
2038 wxSprintf (buf, _T("%.*G"), prec, 3.3);
2039 if (wxStrcmp (buf, _T("3")) != 0)
2040 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
2041 prec = 0;
2042 wxSprintf (buf, _T("%7.*G"), prec, 3.33);
2043 if (wxStrcmp (buf, _T(" 3")) != 0)
2044 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
2045 prec = 3;
2046 test_format = _T("%04.*o");
2047 wxSprintf (buf, test_format.c_str(), prec, 33);
2048 if (wxStrcmp (buf, _T(" 041")) != 0)
2049 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
2050 prec = 7;
2051 test_format = _T("%09.*u");
2052 wxSprintf (buf, test_format.c_str(), prec, 33);
2053 if (wxStrcmp (buf, _T(" 0000033")) != 0)
2054 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
2055 prec = 3;
2056 test_format = _T("%04.*x");
2057 wxSprintf (buf, test_format.c_str(), prec, 33);
2058 if (wxStrcmp (buf, _T(" 021")) != 0)
2059 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2060 prec = 3;
2061 test_format = _T("%04.*X");
2062 wxSprintf (buf, test_format.c_str(), prec, 33);
2063 if (wxStrcmp (buf, _T(" 021")) != 0)
2064 wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
2065 }
2066
2067 #endif // TEST_PRINTF
2068
2069 // ----------------------------------------------------------------------------
2070 // registry and related stuff
2071 // ----------------------------------------------------------------------------
2072
2073 // this is for MSW only
2074 #ifndef __WXMSW__
2075 #undef TEST_REGCONF
2076 #undef TEST_REGISTRY
2077 #endif
2078
2079 #ifdef TEST_REGCONF
2080
2081 #include "wx/confbase.h"
2082 #include "wx/msw/regconf.h"
2083
2084 #if 0
2085 static void TestRegConfWrite()
2086 {
2087 wxConfig *config = new wxConfig(_T("myapp"));
2088 config->SetPath(_T("/group1"));
2089 config->Write(_T("entry1"), _T("foo"));
2090 config->SetPath(_T("/group2"));
2091 config->Write(_T("entry1"), _T("bar"));
2092 }
2093 #endif
2094
2095 static void TestRegConfRead()
2096 {
2097 wxRegConfig *config = new wxRegConfig(_T("myapp"));
2098
2099 wxString str;
2100 long dummy;
2101 config->SetPath(_T("/"));
2102 wxPuts(_T("Enumerating / subgroups:"));
2103 bool bCont = config->GetFirstGroup(str, dummy);
2104 while(bCont)
2105 {
2106 wxPuts(str);
2107 bCont = config->GetNextGroup(str, dummy);
2108 }
2109 }
2110
2111 #endif // TEST_REGCONF
2112
2113 #ifdef TEST_REGISTRY
2114
2115 #include "wx/msw/registry.h"
2116
2117 // I chose this one because I liked its name, but it probably only exists under
2118 // NT
2119 static const wxChar *TESTKEY =
2120 _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2121
2122 static void TestRegistryRead()
2123 {
2124 wxPuts(_T("*** testing registry reading ***"));
2125
2126 wxRegKey key(TESTKEY);
2127 wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
2128 if ( !key.Open() )
2129 {
2130 wxPuts(_T("ERROR: test key can't be opened, aborting test."));
2131
2132 return;
2133 }
2134
2135 size_t nSubKeys, nValues;
2136 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2137 {
2138 wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
2139 }
2140
2141 wxPrintf(_T("Enumerating values:\n"));
2142
2143 long dummy;
2144 wxString value;
2145 bool cont = key.GetFirstValue(value, dummy);
2146 while ( cont )
2147 {
2148 wxPrintf(_T("Value '%s': type "), value.c_str());
2149 switch ( key.GetValueType(value) )
2150 {
2151 case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
2152 case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
2153 case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
2154 case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
2155 case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
2156 case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
2157 default: wxPrintf(_T("other (unknown)")); break;
2158 }
2159
2160 wxPrintf(_T(", value = "));
2161 if ( key.IsNumericValue(value) )
2162 {
2163 long val;
2164 key.QueryValue(value, &val);
2165 wxPrintf(_T("%ld"), val);
2166 }
2167 else // string
2168 {
2169 wxString val;
2170 key.QueryValue(value, val);
2171 wxPrintf(_T("'%s'"), val.c_str());
2172
2173 key.QueryRawValue(value, val);
2174 wxPrintf(_T(" (raw value '%s')"), val.c_str());
2175 }
2176
2177 wxPutchar('\n');
2178
2179 cont = key.GetNextValue(value, dummy);
2180 }
2181 }
2182
2183 static void TestRegistryAssociation()
2184 {
2185 /*
2186 The second call to deleteself genertaes an error message, with a
2187 messagebox saying .flo is crucial to system operation, while the .ddf
2188 call also fails, but with no error message
2189 */
2190
2191 wxRegKey key;
2192
2193 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2194 key.Create();
2195 key = _T("ddxf_auto_file") ;
2196 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2197 key.Create();
2198 key = _T("ddxf_auto_file") ;
2199 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2200 key.Create();
2201 key = _T("program,0") ;
2202 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2203 key.Create();
2204 key = _T("program \"%1\"") ;
2205
2206 key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
2207 key.DeleteSelf();
2208 key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
2209 key.DeleteSelf();
2210 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2211 key.DeleteSelf();
2212 key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2213 key.DeleteSelf();
2214 }
2215
2216 #endif // TEST_REGISTRY
2217
2218 // ----------------------------------------------------------------------------
2219 // scope guard
2220 // ----------------------------------------------------------------------------
2221
2222 #ifdef TEST_SCOPEGUARD
2223
2224 #include "wx/scopeguard.h"
2225
2226 static void function0() { puts("function0()"); }
2227 static void function1(int n) { printf("function1(%d)\n", n); }
2228 static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
2229
2230 struct Object
2231 {
2232 void method0() { printf("method0()\n"); }
2233 void method1(int n) { printf("method1(%d)\n", n); }
2234 void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
2235 };
2236
2237 static void TestScopeGuard()
2238 {
2239 wxON_BLOCK_EXIT0(function0);
2240 wxON_BLOCK_EXIT1(function1, 17);
2241 wxON_BLOCK_EXIT2(function2, 3.14, 'p');
2242
2243 Object obj;
2244 wxON_BLOCK_EXIT_OBJ0(obj, Object::method0);
2245 wxON_BLOCK_EXIT_OBJ1(obj, Object::method1, 7);
2246 wxON_BLOCK_EXIT_OBJ2(obj, Object::method2, 2.71, 'e');
2247
2248 wxScopeGuard dismissed = wxMakeGuard(function0);
2249 dismissed.Dismiss();
2250 }
2251
2252 #endif
2253
2254 // ----------------------------------------------------------------------------
2255 // sockets
2256 // ----------------------------------------------------------------------------
2257
2258 #ifdef TEST_SOCKETS
2259
2260 #include "wx/socket.h"
2261 #include "wx/protocol/protocol.h"
2262 #include "wx/protocol/http.h"
2263
2264 static void TestSocketServer()
2265 {
2266 wxPuts(_T("*** Testing wxSocketServer ***\n"));
2267
2268 static const int PORT = 3000;
2269
2270 wxIPV4address addr;
2271 addr.Service(PORT);
2272
2273 wxSocketServer *server = new wxSocketServer(addr);
2274 if ( !server->Ok() )
2275 {
2276 wxPuts(_T("ERROR: failed to bind"));
2277
2278 return;
2279 }
2280
2281 bool quit = false;
2282 while ( !quit )
2283 {
2284 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
2285
2286 wxSocketBase *socket = server->Accept();
2287 if ( !socket )
2288 {
2289 wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
2290 break;
2291 }
2292
2293 wxPuts(_T("Server: got a client."));
2294
2295 server->SetTimeout(60); // 1 min
2296
2297 bool close = false;
2298 while ( !close && socket->IsConnected() )
2299 {
2300 wxString s;
2301 wxChar ch = _T('\0');
2302 for ( ;; )
2303 {
2304 if ( socket->Read(&ch, sizeof(ch)).Error() )
2305 {
2306 // don't log error if the client just close the connection
2307 if ( socket->IsConnected() )
2308 {
2309 wxPuts(_T("ERROR: in wxSocket::Read."));
2310 }
2311
2312 break;
2313 }
2314
2315 if ( ch == '\r' )
2316 continue;
2317
2318 if ( ch == '\n' )
2319 break;
2320
2321 s += ch;
2322 }
2323
2324 if ( ch != '\n' )
2325 {
2326 break;
2327 }
2328
2329 wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
2330 if ( s == _T("close") )
2331 {
2332 wxPuts(_T("Closing connection"));
2333
2334 close = true;
2335 }
2336 else if ( s == _T("quit") )
2337 {
2338 close =
2339 quit = true;
2340
2341 wxPuts(_T("Shutting down the server"));
2342 }
2343 else // not a special command
2344 {
2345 socket->Write(s.MakeUpper().c_str(), s.length());
2346 socket->Write("\r\n", 2);
2347 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
2348 }
2349 }
2350
2351 if ( !close )
2352 {
2353 wxPuts(_T("Server: lost a client unexpectedly."));
2354 }
2355
2356 socket->Destroy();
2357 }
2358
2359 // same as "delete server" but is consistent with GUI programs
2360 server->Destroy();
2361 }
2362
2363 static void TestSocketClient()
2364 {
2365 wxPuts(_T("*** Testing wxSocketClient ***\n"));
2366
2367 static const wxChar *hostname = _T("www.wxwidgets.org");
2368
2369 wxIPV4address addr;
2370 addr.Hostname(hostname);
2371 addr.Service(80);
2372
2373 wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
2374
2375 wxSocketClient client;
2376 if ( !client.Connect(addr) )
2377 {
2378 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2379 }
2380 else
2381 {
2382 wxPrintf(_T("--- Connected to %s:%u...\n"),
2383 addr.Hostname().c_str(), addr.Service());
2384
2385 wxChar buf[8192];
2386
2387 // could use simply "GET" here I suppose
2388 wxString cmdGet =
2389 wxString::Format(_T("GET http://%s/\r\n"), hostname);
2390 client.Write(cmdGet, cmdGet.length());
2391 wxPrintf(_T("--- Sent command '%s' to the server\n"),
2392 MakePrintable(cmdGet).c_str());
2393 client.Read(buf, WXSIZEOF(buf));
2394 wxPrintf(_T("--- Server replied:\n%s"), buf);
2395 }
2396 }
2397
2398 #endif // TEST_SOCKETS
2399
2400 // ----------------------------------------------------------------------------
2401 // FTP
2402 // ----------------------------------------------------------------------------
2403
2404 #ifdef TEST_FTP
2405
2406 #include "wx/protocol/ftp.h"
2407
2408 static wxFTP ftp;
2409
2410 #define FTP_ANONYMOUS
2411
2412 #ifdef FTP_ANONYMOUS
2413 static const wxChar *directory = _T("/pub");
2414 static const wxChar *filename = _T("welcome.msg");
2415 #else
2416 static const wxChar *directory = _T("/etc");
2417 static const wxChar *filename = _T("issue");
2418 #endif
2419
2420 static bool TestFtpConnect()
2421 {
2422 wxPuts(_T("*** Testing FTP connect ***"));
2423
2424 #ifdef FTP_ANONYMOUS
2425 static const wxChar *hostname = _T("ftp.wxwidgets.org");
2426
2427 wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
2428 #else // !FTP_ANONYMOUS
2429 static const wxChar *hostname = "localhost";
2430
2431 wxChar user[256];
2432 wxFgets(user, WXSIZEOF(user), stdin);
2433 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
2434 ftp.SetUser(user);
2435
2436 wxChar password[256];
2437 wxPrintf(_T("Password for %s: "), password);
2438 wxFgets(password, WXSIZEOF(password), stdin);
2439 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
2440 ftp.SetPassword(password);
2441
2442 wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
2443 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2444
2445 if ( !ftp.Connect(hostname) )
2446 {
2447 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2448
2449 return false;
2450 }
2451 else
2452 {
2453 wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
2454 hostname, ftp.Pwd().c_str());
2455 ftp.Close();
2456 }
2457
2458 return true;
2459 }
2460
2461 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
2462 static void TestFtpWuFtpd()
2463 {
2464 wxFTP ftp;
2465 static const wxChar *hostname = _T("ftp.eudora.com");
2466 if ( !ftp.Connect(hostname) )
2467 {
2468 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
2469 }
2470 else
2471 {
2472 static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
2473 wxInputStream *in = ftp.GetInputStream(filename);
2474 if ( !in )
2475 {
2476 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
2477 }
2478 else
2479 {
2480 size_t size = in->GetSize();
2481 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
2482
2483 wxChar *data = new wxChar[size];
2484 if ( !in->Read(data, size) )
2485 {
2486 wxPuts(_T("ERROR: read error"));
2487 }
2488 else
2489 {
2490 wxPrintf(_T("Successfully retrieved the file.\n"));
2491 }
2492
2493 delete [] data;
2494 delete in;
2495 }
2496 }
2497 }
2498
2499 static void TestFtpList()
2500 {
2501 wxPuts(_T("*** Testing wxFTP file listing ***\n"));
2502
2503 // test CWD
2504 if ( !ftp.ChDir(directory) )
2505 {
2506 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
2507 }
2508
2509 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2510
2511 // test NLIST and LIST
2512 wxArrayString files;
2513 if ( !ftp.GetFilesList(files) )
2514 {
2515 wxPuts(_T("ERROR: failed to get NLIST of files"));
2516 }
2517 else
2518 {
2519 wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
2520 size_t count = files.GetCount();
2521 for ( size_t n = 0; n < count; n++ )
2522 {
2523 wxPrintf(_T("\t%s\n"), files[n].c_str());
2524 }
2525 wxPuts(_T("End of the file list"));
2526 }
2527
2528 if ( !ftp.GetDirList(files) )
2529 {
2530 wxPuts(_T("ERROR: failed to get LIST of files"));
2531 }
2532 else
2533 {
2534 wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
2535 size_t count = files.GetCount();
2536 for ( size_t n = 0; n < count; n++ )
2537 {
2538 wxPrintf(_T("\t%s\n"), files[n].c_str());
2539 }
2540 wxPuts(_T("End of the file list"));
2541 }
2542
2543 if ( !ftp.ChDir(_T("..")) )
2544 {
2545 wxPuts(_T("ERROR: failed to cd to .."));
2546 }
2547
2548 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2549 }
2550
2551 static void TestFtpDownload()
2552 {
2553 wxPuts(_T("*** Testing wxFTP download ***\n"));
2554
2555 // test RETR
2556 wxInputStream *in = ftp.GetInputStream(filename);
2557 if ( !in )
2558 {
2559 wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
2560 }
2561 else
2562 {
2563 size_t size = in->GetSize();
2564 wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
2565 fflush(stdout);
2566
2567 wxChar *data = new wxChar[size];
2568 if ( !in->Read(data, size) )
2569 {
2570 wxPuts(_T("ERROR: read error"));
2571 }
2572 else
2573 {
2574 wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
2575 }
2576
2577 delete [] data;
2578 delete in;
2579 }
2580 }
2581
2582 static void TestFtpFileSize()
2583 {
2584 wxPuts(_T("*** Testing FTP SIZE command ***"));
2585
2586 if ( !ftp.ChDir(directory) )
2587 {
2588 wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
2589 }
2590
2591 wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
2592
2593 if ( ftp.FileExists(filename) )
2594 {
2595 int size = ftp.GetFileSize(filename);
2596 if ( size == -1 )
2597 wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
2598 else
2599 wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
2600 }
2601 else
2602 {
2603 wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
2604 }
2605 }
2606
2607 static void TestFtpMisc()
2608 {
2609 wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
2610
2611 if ( ftp.SendCommand(_T("STAT")) != '2' )
2612 {
2613 wxPuts(_T("ERROR: STAT failed"));
2614 }
2615 else
2616 {
2617 wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
2618 }
2619
2620 if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
2621 {
2622 wxPuts(_T("ERROR: HELP SITE failed"));
2623 }
2624 else
2625 {
2626 wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
2627 ftp.GetLastResult().c_str());
2628 }
2629 }
2630
2631 static void TestFtpInteractive()
2632 {
2633 wxPuts(_T("\n*** Interactive wxFTP test ***"));
2634
2635 wxChar buf[128];
2636
2637 for ( ;; )
2638 {
2639 wxPrintf(_T("Enter FTP command: "));
2640 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
2641 break;
2642
2643 // kill the last '\n'
2644 buf[wxStrlen(buf) - 1] = 0;
2645
2646 // special handling of LIST and NLST as they require data connection
2647 wxString start(buf, 4);
2648 start.MakeUpper();
2649 if ( start == _T("LIST") || start == _T("NLST") )
2650 {
2651 wxString wildcard;
2652 if ( wxStrlen(buf) > 4 )
2653 wildcard = buf + 5;
2654
2655 wxArrayString files;
2656 if ( !ftp.GetList(files, wildcard, start == _T("LIST")) )
2657 {
2658 wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
2659 }
2660 else
2661 {
2662 wxPrintf(_T("--- %s of '%s' under '%s':\n"),
2663 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
2664 size_t count = files.GetCount();
2665 for ( size_t n = 0; n < count; n++ )
2666 {
2667 wxPrintf(_T("\t%s\n"), files[n].c_str());
2668 }
2669 wxPuts(_T("--- End of the file list"));
2670 }
2671 }
2672 else // !list
2673 {
2674 wxChar ch = ftp.SendCommand(buf);
2675 wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
2676 if ( ch )
2677 {
2678 wxPrintf(_T(" (return code %c)"), ch);
2679 }
2680
2681 wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
2682 }
2683 }
2684
2685 wxPuts(_T("\n*** done ***"));
2686 }
2687
2688 static void TestFtpUpload()
2689 {
2690 wxPuts(_T("*** Testing wxFTP uploading ***\n"));
2691
2692 // upload a file
2693 static const wxChar *file1 = _T("test1");
2694 static const wxChar *file2 = _T("test2");
2695 wxOutputStream *out = ftp.GetOutputStream(file1);
2696 if ( out )
2697 {
2698 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
2699 out->Write("First hello", 11);
2700 delete out;
2701 }
2702
2703 // send a command to check the remote file
2704 if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' )
2705 {
2706 wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
2707 }
2708 else
2709 {
2710 wxPrintf(_T("STAT %s returned:\n\n%s\n"),
2711 file1, ftp.GetLastResult().c_str());
2712 }
2713
2714 out = ftp.GetOutputStream(file2);
2715 if ( out )
2716 {
2717 wxPrintf(_T("--- Uploading to %s ---\n"), file1);
2718 out->Write("Second hello", 12);
2719 delete out;
2720 }
2721 }
2722
2723 #endif // TEST_FTP
2724
2725 // ----------------------------------------------------------------------------
2726 // stack backtrace
2727 // ----------------------------------------------------------------------------
2728
2729 #ifdef TEST_STACKWALKER
2730
2731 #if wxUSE_STACKWALKER
2732
2733 #include "wx/stackwalk.h"
2734
2735 class StackDump : public wxStackWalker
2736 {
2737 public:
2738 StackDump(const char *argv0)
2739 : wxStackWalker(argv0)
2740 {
2741 }
2742
2743 virtual void Walk(size_t skip = 1)
2744 {
2745 wxPuts(_T("Stack dump:"));
2746
2747 wxStackWalker::Walk(skip);
2748 }
2749
2750 protected:
2751 virtual void OnStackFrame(const wxStackFrame& frame)
2752 {
2753 printf("[%2d] ", (int) frame.GetLevel());
2754
2755 wxString name = frame.GetName();
2756 if ( !name.empty() )
2757 {
2758 printf("%-20.40s", (const char*)name.mb_str());
2759 }
2760 else
2761 {
2762 printf("0x%08lx", (unsigned long)frame.GetAddress());
2763 }
2764
2765 if ( frame.HasSourceLocation() )
2766 {
2767 printf("\t%s:%d",
2768 (const char*)frame.GetFileName().mb_str(),
2769 (int)frame.GetLine());
2770 }
2771
2772 puts("");
2773
2774 wxString type, val;
2775 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
2776 {
2777 printf("\t%s %s = %s\n", (const char*)type.mb_str(),
2778 (const char*)name.mb_str(),
2779 (const char*)val.mb_str());
2780 }
2781 }
2782 };
2783
2784 static void TestStackWalk(const char *argv0)
2785 {
2786 wxPuts(_T("*** Testing wxStackWalker ***\n"));
2787
2788 StackDump dump(argv0);
2789 dump.Walk();
2790 }
2791
2792 #endif // wxUSE_STACKWALKER
2793
2794 #endif // TEST_STACKWALKER
2795
2796 // ----------------------------------------------------------------------------
2797 // standard paths
2798 // ----------------------------------------------------------------------------
2799
2800 #ifdef TEST_STDPATHS
2801
2802 #include "wx/stdpaths.h"
2803 #include "wx/wxchar.h" // wxPrintf
2804
2805 static void TestStandardPaths()
2806 {
2807 wxPuts(_T("*** Testing wxStandardPaths ***\n"));
2808
2809 wxTheApp->SetAppName(_T("console"));
2810
2811 wxStandardPathsBase& stdp = wxStandardPaths::Get();
2812 wxPrintf(_T("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
2813 wxPrintf(_T("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
2814 wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
2815 wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
2816 wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
2817 wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
2818 wxPrintf(_T("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
2819 wxPrintf(_T("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
2820 wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
2821 wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
2822 wxPrintf(_T("Localized res. dir:\t%s\n"),
2823 stdp.GetLocalizedResourcesDir(_T("fr")).c_str());
2824 wxPrintf(_T("Message catalogs dir:\t%s\n"),
2825 stdp.GetLocalizedResourcesDir
2826 (
2827 _T("fr"),
2828 wxStandardPaths::ResourceCat_Messages
2829 ).c_str());
2830 }
2831
2832 #endif // TEST_STDPATHS
2833
2834 // ----------------------------------------------------------------------------
2835 // streams
2836 // ----------------------------------------------------------------------------
2837
2838 #ifdef TEST_STREAMS
2839
2840 #include "wx/wfstream.h"
2841 #include "wx/mstream.h"
2842
2843 static void TestFileStream()
2844 {
2845 wxPuts(_T("*** Testing wxFileInputStream ***"));
2846
2847 static const wxString filename = _T("testdata.fs");
2848 {
2849 wxFileOutputStream fsOut(filename);
2850 fsOut.Write("foo", 3);
2851 }
2852
2853 {
2854 wxFileInputStream fsIn(filename);
2855 wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
2856 while ( !fsIn.Eof() )
2857 {
2858 wxPutchar(fsIn.GetC());
2859 }
2860 }
2861
2862 if ( !wxRemoveFile(filename) )
2863 {
2864 wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename.c_str());
2865 }
2866
2867 wxPuts(_T("\n*** wxFileInputStream test done ***"));
2868 }
2869
2870 static void TestMemoryStream()
2871 {
2872 wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
2873
2874 wxMemoryOutputStream memOutStream;
2875 wxPrintf(_T("Initially out stream offset: %lu\n"),
2876 (unsigned long)memOutStream.TellO());
2877
2878 for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
2879 {
2880 memOutStream.PutC(*p);
2881 }
2882
2883 wxPrintf(_T("Final out stream offset: %lu\n"),
2884 (unsigned long)memOutStream.TellO());
2885
2886 wxPuts(_T("*** Testing wxMemoryInputStream ***"));
2887
2888 wxChar buf[1024];
2889 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
2890
2891 wxMemoryInputStream memInpStream(buf, len);
2892 wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
2893 while ( !memInpStream.Eof() )
2894 {
2895 wxPutchar(memInpStream.GetC());
2896 }
2897
2898 wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
2899 }
2900
2901 #endif // TEST_STREAMS
2902
2903 // ----------------------------------------------------------------------------
2904 // timers
2905 // ----------------------------------------------------------------------------
2906
2907 #ifdef TEST_TIMER
2908
2909 #include "wx/stopwatch.h"
2910 #include "wx/utils.h"
2911
2912 static void TestStopWatch()
2913 {
2914 wxPuts(_T("*** Testing wxStopWatch ***\n"));
2915
2916 wxStopWatch sw;
2917 sw.Pause();
2918 wxPrintf(_T("Initially paused, after 2 seconds time is..."));
2919 fflush(stdout);
2920 wxSleep(2);
2921 wxPrintf(_T("\t%ldms\n"), sw.Time());
2922
2923 wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
2924 fflush(stdout);
2925 sw.Resume();
2926 wxSleep(3);
2927 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2928
2929 sw.Pause();
2930 wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
2931 fflush(stdout);
2932 wxSleep(2);
2933 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2934
2935 sw.Resume();
2936 wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
2937 fflush(stdout);
2938 wxSleep(2);
2939 wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
2940
2941 wxStopWatch sw2;
2942 wxPuts(_T("\nChecking for 'backwards clock' bug..."));
2943 for ( size_t n = 0; n < 70; n++ )
2944 {
2945 sw2.Start();
2946
2947 for ( size_t m = 0; m < 100000; m++ )
2948 {
2949 if ( sw.Time() < 0 || sw2.Time() < 0 )
2950 {
2951 wxPuts(_T("\ntime is negative - ERROR!"));
2952 }
2953 }
2954
2955 wxPutchar('.');
2956 fflush(stdout);
2957 }
2958
2959 wxPuts(_T(", ok."));
2960 }
2961
2962 #include "wx/timer.h"
2963 #include "wx/evtloop.h"
2964
2965 void TestTimer()
2966 {
2967 wxPuts(_T("*** Testing wxTimer ***\n"));
2968
2969 class MyTimer : public wxTimer
2970 {
2971 public:
2972 MyTimer() : wxTimer() { m_num = 0; }
2973
2974 virtual void Notify()
2975 {
2976 wxPrintf(_T("%d"), m_num++);
2977 fflush(stdout);
2978
2979 if ( m_num == 10 )
2980 {
2981 wxPrintf(_T("... exiting the event loop"));
2982 Stop();
2983
2984 wxEventLoop::GetActive()->Exit(0);
2985 wxPuts(_T(", ok."));
2986 }
2987
2988 fflush(stdout);
2989 }
2990
2991 private:
2992 int m_num;
2993 };
2994
2995 wxEventLoop loop;
2996
2997 wxTimer timer1;
2998 timer1.Start(100, true /* one shot */);
2999 timer1.Stop();
3000 timer1.Start(100, true /* one shot */);
3001
3002 MyTimer timer;
3003 timer.Start(500);
3004
3005 loop.Run();
3006 }
3007
3008 #endif // TEST_TIMER
3009
3010 // ----------------------------------------------------------------------------
3011 // vCard support
3012 // ----------------------------------------------------------------------------
3013
3014 #ifdef TEST_VCARD
3015
3016 #include "wx/vcard.h"
3017
3018 static void DumpVObject(size_t level, const wxVCardObject& vcard)
3019 {
3020 void *cookie;
3021 wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
3022 while ( vcObj )
3023 {
3024 wxPrintf(_T("%s%s"),
3025 wxString(_T('\t'), level).c_str(),
3026 vcObj->GetName().c_str());
3027
3028 wxString value;
3029 switch ( vcObj->GetType() )
3030 {
3031 case wxVCardObject::String:
3032 case wxVCardObject::UString:
3033 {
3034 wxString val;
3035 vcObj->GetValue(&val);
3036 value << _T('"') << val << _T('"');
3037 }
3038 break;
3039
3040 case wxVCardObject::Int:
3041 {
3042 unsigned int i;
3043 vcObj->GetValue(&i);
3044 value.Printf(_T("%u"), i);
3045 }
3046 break;
3047
3048 case wxVCardObject::Long:
3049 {
3050 unsigned long l;
3051 vcObj->GetValue(&l);
3052 value.Printf(_T("%lu"), l);
3053 }
3054 break;
3055
3056 case wxVCardObject::None:
3057 break;
3058
3059 case wxVCardObject::Object:
3060 value = _T("<node>");
3061 break;
3062
3063 default:
3064 value = _T("<unknown value type>");
3065 }
3066
3067 if ( !!value )
3068 wxPrintf(_T(" = %s"), value.c_str());
3069 wxPutchar('\n');
3070
3071 DumpVObject(level + 1, *vcObj);
3072
3073 delete vcObj;
3074 vcObj = vcard.GetNextProp(&cookie);
3075 }
3076 }
3077
3078 static void DumpVCardAddresses(const wxVCard& vcard)
3079 {
3080 wxPuts(_T("\nShowing all addresses from vCard:\n"));
3081
3082 size_t nAdr = 0;
3083 void *cookie;
3084 wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
3085 while ( addr )
3086 {
3087 wxString flagsStr;
3088 int flags = addr->GetFlags();
3089 if ( flags & wxVCardAddress::Domestic )
3090 {
3091 flagsStr << _T("domestic ");
3092 }
3093 if ( flags & wxVCardAddress::Intl )
3094 {
3095 flagsStr << _T("international ");
3096 }
3097 if ( flags & wxVCardAddress::Postal )
3098 {
3099 flagsStr << _T("postal ");
3100 }
3101 if ( flags & wxVCardAddress::Parcel )
3102 {
3103 flagsStr << _T("parcel ");
3104 }
3105 if ( flags & wxVCardAddress::Home )
3106 {
3107 flagsStr << _T("home ");
3108 }
3109 if ( flags & wxVCardAddress::Work )
3110 {
3111 flagsStr << _T("work ");
3112 }
3113
3114 wxPrintf(_T("Address %u:\n")
3115 "\tflags = %s\n"
3116 "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
3117 ++nAdr,
3118 flagsStr.c_str(),
3119 addr->GetPostOffice().c_str(),
3120 addr->GetExtAddress().c_str(),
3121 addr->GetStreet().c_str(),
3122 addr->GetLocality().c_str(),
3123 addr->GetRegion().c_str(),
3124 addr->GetPostalCode().c_str(),
3125 addr->GetCountry().c_str()
3126 );
3127
3128 delete addr;
3129 addr = vcard.GetNextAddress(&cookie);
3130 }
3131 }
3132
3133 static void DumpVCardPhoneNumbers(const wxVCard& vcard)
3134 {
3135 wxPuts(_T("\nShowing all phone numbers from vCard:\n"));
3136
3137 size_t nPhone = 0;
3138 void *cookie;
3139 wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
3140 while ( phone )
3141 {
3142 wxString flagsStr;
3143 int flags = phone->GetFlags();
3144 if ( flags & wxVCardPhoneNumber::Voice )
3145 {
3146 flagsStr << _T("voice ");
3147 }
3148 if ( flags & wxVCardPhoneNumber::Fax )
3149 {
3150 flagsStr << _T("fax ");
3151 }
3152 if ( flags & wxVCardPhoneNumber::Cellular )
3153 {
3154 flagsStr << _T("cellular ");
3155 }
3156 if ( flags & wxVCardPhoneNumber::Modem )
3157 {
3158 flagsStr << _T("modem ");
3159 }
3160 if ( flags & wxVCardPhoneNumber::Home )
3161 {
3162 flagsStr << _T("home ");
3163 }
3164 if ( flags & wxVCardPhoneNumber::Work )
3165 {
3166 flagsStr << _T("work ");
3167 }
3168
3169 wxPrintf(_T("Phone number %u:\n")
3170 "\tflags = %s\n"
3171 "\tvalue = %s\n",
3172 ++nPhone,
3173 flagsStr.c_str(),
3174 phone->GetNumber().c_str()
3175 );
3176
3177 delete phone;
3178 phone = vcard.GetNextPhoneNumber(&cookie);
3179 }
3180 }
3181
3182 static void TestVCardRead()
3183 {
3184 wxPuts(_T("*** Testing wxVCard reading ***\n"));
3185
3186 wxVCard vcard(_T("vcard.vcf"));
3187 if ( !vcard.IsOk() )
3188 {
3189 wxPuts(_T("ERROR: couldn't load vCard."));
3190 }
3191 else
3192 {
3193 // read individual vCard properties
3194 wxVCardObject *vcObj = vcard.GetProperty("FN");
3195 wxString value;
3196 if ( vcObj )
3197 {
3198 vcObj->GetValue(&value);
3199 delete vcObj;
3200 }
3201 else
3202 {
3203 value = _T("<none>");
3204 }
3205
3206 wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str());
3207
3208
3209 if ( !vcard.GetFullName(&value) )
3210 {
3211 value = _T("<none>");
3212 }
3213
3214 wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str());
3215
3216 // now show how to deal with multiply occurring properties
3217 DumpVCardAddresses(vcard);
3218 DumpVCardPhoneNumbers(vcard);
3219
3220 // and finally show all
3221 wxPuts(_T("\nNow dumping the entire vCard:\n")
3222 "-----------------------------\n");
3223
3224 DumpVObject(0, vcard);
3225 }
3226 }
3227
3228 static void TestVCardWrite()
3229 {
3230 wxPuts(_T("*** Testing wxVCard writing ***\n"));
3231
3232 wxVCard vcard;
3233 if ( !vcard.IsOk() )
3234 {
3235 wxPuts(_T("ERROR: couldn't create vCard."));
3236 }
3237 else
3238 {
3239 // set some fields
3240 vcard.SetName("Zeitlin", "Vadim");
3241 vcard.SetFullName("Vadim Zeitlin");
3242 vcard.SetOrganization("wxWidgets", "R&D");
3243
3244 // just dump the vCard back
3245 wxPuts(_T("Entire vCard follows:\n"));
3246 wxPuts(vcard.Write());
3247 }
3248 }
3249
3250 #endif // TEST_VCARD
3251
3252 // ----------------------------------------------------------------------------
3253 // wxVolume tests
3254 // ----------------------------------------------------------------------------
3255
3256 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
3257 #undef TEST_VOLUME
3258 #endif
3259
3260 #ifdef TEST_VOLUME
3261
3262 #include "wx/volume.h"
3263
3264 static const wxChar *volumeKinds[] =
3265 {
3266 _T("floppy"),
3267 _T("hard disk"),
3268 _T("CD-ROM"),
3269 _T("DVD-ROM"),
3270 _T("network volume"),
3271 _T("other volume"),
3272 };
3273
3274 static void TestFSVolume()
3275 {
3276 wxPuts(_T("*** Testing wxFSVolume class ***"));
3277
3278 wxArrayString volumes = wxFSVolume::GetVolumes();
3279 size_t count = volumes.GetCount();
3280
3281 if ( !count )
3282 {
3283 wxPuts(_T("ERROR: no mounted volumes?"));
3284 return;
3285 }
3286
3287 wxPrintf(_T("%u mounted volumes found:\n"), count);
3288
3289 for ( size_t n = 0; n < count; n++ )
3290 {
3291 wxFSVolume vol(volumes[n]);
3292 if ( !vol.IsOk() )
3293 {
3294 wxPuts(_T("ERROR: couldn't create volume"));
3295 continue;
3296 }
3297
3298 wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"),
3299 n + 1,
3300 vol.GetDisplayName().c_str(),
3301 vol.GetName().c_str(),
3302 volumeKinds[vol.GetKind()],
3303 vol.IsWritable() ? _T("rw") : _T("ro"),
3304 vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable")
3305 : _T("fixed"));
3306 }
3307 }
3308
3309 #endif // TEST_VOLUME
3310
3311 // ----------------------------------------------------------------------------
3312 // wide char and Unicode support
3313 // ----------------------------------------------------------------------------
3314
3315 #ifdef TEST_WCHAR
3316
3317 #include "wx/strconv.h"
3318 #include "wx/fontenc.h"
3319 #include "wx/encconv.h"
3320 #include "wx/buffer.h"
3321
3322 static const unsigned char utf8koi8r[] =
3323 {
3324 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
3325 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
3326 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
3327 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
3328 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
3329 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
3330 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
3331 };
3332
3333 static const unsigned char utf8iso8859_1[] =
3334 {
3335 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
3336 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
3337 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
3338 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
3339 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
3340 };
3341
3342 static const unsigned char utf8Invalid[] =
3343 {
3344 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
3345 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
3346 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
3347 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
3348 0x6c, 0x61, 0x79, 0
3349 };
3350
3351 static const struct Utf8Data
3352 {
3353 const unsigned char *text;
3354 size_t len;
3355 const wxChar *charset;
3356 wxFontEncoding encoding;
3357 } utf8data[] =
3358 {
3359 { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3360 { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 },
3361 { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
3362 };
3363
3364 static void TestUtf8()
3365 {
3366 wxPuts(_T("*** Testing UTF8 support ***\n"));
3367
3368 char buf[1024];
3369 wchar_t wbuf[1024];
3370
3371 for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
3372 {
3373 const Utf8Data& u8d = utf8data[n];
3374 if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text,
3375 WXSIZEOF(wbuf)) == (size_t)-1 )
3376 {
3377 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3378 }
3379 else
3380 {
3381 wxCSConv conv(u8d.charset);
3382 if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 )
3383 {
3384 wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset);
3385 }
3386 else
3387 {
3388 wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf);
3389 }
3390 }
3391
3392 wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text));
3393 if ( s.empty() )
3394 s = _T("<< conversion failed >>");
3395 wxPrintf(_T("String in current cset: %s\n"), s.c_str());
3396
3397 }
3398
3399 wxPuts(wxEmptyString);
3400 }
3401
3402 static void TestEncodingConverter()
3403 {
3404 wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
3405
3406 // using wxEncodingConverter should give the same result as above
3407 char buf[1024];
3408 wchar_t wbuf[1024];
3409 if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r,
3410 WXSIZEOF(utf8koi8r)) == (size_t)-1 )
3411 {
3412 wxPuts(_T("ERROR: UTF-8 decoding failed."));
3413 }
3414 else
3415 {
3416 wxEncodingConverter ec;
3417 ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
3418 ec.Convert(wbuf, buf);
3419 wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf);
3420 }
3421
3422 wxPuts(wxEmptyString);
3423 }
3424
3425 #endif // TEST_WCHAR
3426
3427 // ----------------------------------------------------------------------------
3428 // ZIP stream
3429 // ----------------------------------------------------------------------------
3430
3431 #ifdef TEST_ZIP
3432
3433 #include "wx/filesys.h"
3434 #include "wx/fs_zip.h"
3435 #include "wx/zipstrm.h"
3436
3437 static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
3438
3439 static void TestZipStreamRead()
3440 {
3441 wxPuts(_T("*** Testing ZIP reading ***\n"));
3442
3443 static const wxString filename = _T("foo");
3444 wxFFileInputStream in(TESTFILE_ZIP);
3445 wxZipInputStream istr(in);
3446 wxZipEntry entry(filename);
3447 istr.OpenEntry(entry);
3448
3449 wxPrintf(_T("Archive size: %u\n"), istr.GetSize());
3450
3451 wxPrintf(_T("Dumping the file '%s':\n"), filename.c_str());
3452 while ( !istr.Eof() )
3453 {
3454 wxPutchar(istr.GetC());
3455 fflush(stdout);
3456 }
3457
3458 wxPuts(_T("\n----- done ------"));
3459 }
3460
3461 static void DumpZipDirectory(wxFileSystem& fs,
3462 const wxString& dir,
3463 const wxString& indent)
3464 {
3465 wxString prefix = wxString::Format(_T("%s#zip:%s"),
3466 TESTFILE_ZIP, dir.c_str());
3467 wxString wildcard = prefix + _T("/*");
3468
3469 wxString dirname = fs.FindFirst(wildcard, wxDIR);
3470 while ( !dirname.empty() )
3471 {
3472 if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
3473 {
3474 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3475
3476 break;
3477 }
3478
3479 wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
3480
3481 DumpZipDirectory(fs, dirname,
3482 indent + wxString(_T(' '), 4));
3483
3484 dirname = fs.FindNext();
3485 }
3486
3487 wxString filename = fs.FindFirst(wildcard, wxFILE);
3488 while ( !filename.empty() )
3489 {
3490 if ( !filename.StartsWith(prefix, &filename) )
3491 {
3492 wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
3493
3494 break;
3495 }
3496
3497 wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
3498
3499 filename = fs.FindNext();
3500 }
3501 }
3502
3503 static void TestZipFileSystem()
3504 {
3505 wxPuts(_T("*** Testing ZIP file system ***\n"));
3506
3507 wxFileSystem::AddHandler(new wxZipFSHandler);
3508 wxFileSystem fs;
3509 wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
3510
3511 DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
3512 }
3513
3514 #endif // TEST_ZIP
3515
3516 // ----------------------------------------------------------------------------
3517 // date time
3518 // ----------------------------------------------------------------------------
3519
3520 #ifdef TEST_DATETIME
3521
3522 #include "wx/math.h"
3523 #include "wx/datetime.h"
3524
3525 // this test miscellaneous static wxDateTime functions
3526
3527 #if TEST_ALL
3528
3529 static void TestTimeStatic()
3530 {
3531 wxPuts(_T("\n*** wxDateTime static methods test ***"));
3532
3533 // some info about the current date
3534 int year = wxDateTime::GetCurrentYear();
3535 wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"),
3536 year,
3537 wxDateTime::IsLeapYear(year) ? "" : "not ",
3538 wxDateTime::GetNumberOfDays(year));
3539
3540 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
3541 wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"),
3542 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
3543 wxDateTime::GetMonthName(month).c_str(),
3544 wxDateTime::GetNumberOfDays(month));
3545 }
3546
3547 // test time zones stuff
3548 static void TestTimeZones()
3549 {
3550 wxPuts(_T("\n*** wxDateTime timezone test ***"));
3551
3552 wxDateTime now = wxDateTime::Now();
3553
3554 wxPrintf(_T("Current GMT time:\t%s\n"), now.Format(_T("%c"), wxDateTime::GMT0).c_str());
3555 wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0).c_str());
3556 wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST).c_str());
3557 wxPrintf(_T("Current time in Paris:\t%s\n"), now.Format(_T("%c"), wxDateTime::CET).c_str());
3558 wxPrintf(_T(" Moscow:\t%s\n"), now.Format(_T("%c"), wxDateTime::MSK).c_str());
3559 wxPrintf(_T(" New York:\t%s\n"), now.Format(_T("%c"), wxDateTime::EST).c_str());
3560
3561 wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
3562
3563 wxDateTime::Tm tm = now.GetTm();
3564 if ( wxDateTime(tm) != now )
3565 {
3566 wxPrintf(_T("ERROR: got %s instead of %s\n"),
3567 wxDateTime(tm).Format().c_str(), now.Format().c_str());
3568 }
3569 }
3570
3571 // test some minimal support for the dates outside the standard range
3572 static void TestTimeRange()
3573 {
3574 wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***"));
3575
3576 static const wxChar *fmt = _T("%d-%b-%Y %H:%M:%S");
3577
3578 wxPrintf(_T("Unix epoch:\t%s\n"),
3579 wxDateTime(2440587.5).Format(fmt).c_str());
3580 wxPrintf(_T("Feb 29, 0: \t%s\n"),
3581 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
3582 wxPrintf(_T("JDN 0: \t%s\n"),
3583 wxDateTime(0.0).Format(fmt).c_str());
3584 wxPrintf(_T("Jan 1, 1AD:\t%s\n"),
3585 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
3586 wxPrintf(_T("May 29, 2099:\t%s\n"),
3587 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
3588 }
3589
3590 // test DST calculations
3591 static void TestTimeDST()
3592 {
3593 wxPuts(_T("\n*** wxDateTime DST test ***"));
3594
3595 wxPrintf(_T("DST is%s in effect now.\n\n"),
3596 wxDateTime::Now().IsDST() ? wxEmptyString : _T(" not"));
3597
3598 for ( int year = 1990; year < 2005; year++ )
3599 {
3600 wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
3601 year,
3602 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
3603 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
3604 }
3605 }
3606
3607 #endif // TEST_ALL
3608
3609 #if TEST_INTERACTIVE
3610
3611 static void TestDateTimeInteractive()
3612 {
3613 wxPuts(_T("\n*** interactive wxDateTime tests ***"));
3614
3615 wxChar buf[128];
3616
3617 for ( ;; )
3618 {
3619 wxPrintf(_T("Enter a date: "));
3620 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
3621 break;
3622
3623 // kill the last '\n'
3624 buf[wxStrlen(buf) - 1] = 0;
3625
3626 wxDateTime dt;
3627 const wxChar *p = dt.ParseDate(buf);
3628 if ( !p )
3629 {
3630 wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf);
3631
3632 continue;
3633 }
3634 else if ( *p )
3635 {
3636 wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf);
3637 }
3638
3639 wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
3640 dt.Format(_T("%b %d, %Y")).c_str(),
3641 dt.GetDayOfYear(),
3642 dt.GetWeekOfMonth(wxDateTime::Monday_First),
3643 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
3644 dt.GetWeekOfYear(wxDateTime::Monday_First));
3645 }
3646
3647 wxPuts(_T("\n*** done ***"));
3648 }
3649
3650 #endif // TEST_INTERACTIVE
3651
3652 #if TEST_ALL
3653
3654 static void TestTimeMS()
3655 {
3656 wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
3657
3658 wxDateTime dt1 = wxDateTime::Now(),
3659 dt2 = wxDateTime::UNow();
3660
3661 wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str());
3662 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3663 wxPrintf(_T("Dummy loop: "));
3664 for ( int i = 0; i < 6000; i++ )
3665 {
3666 //for ( int j = 0; j < 10; j++ )
3667 {
3668 wxString s;
3669 s.Printf(_T("%g"), sqrt((float)i));
3670 }
3671
3672 if ( !(i % 100) )
3673 wxPutchar('.');
3674 }
3675 wxPuts(_T(", done"));
3676
3677 dt1 = dt2;
3678 dt2 = wxDateTime::UNow();
3679 wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
3680
3681 wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str());
3682
3683 wxPuts(_T("\n*** done ***"));
3684 }
3685
3686 static void TestTimeHolidays()
3687 {
3688 wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n"));
3689
3690 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
3691 wxDateTime dtStart(1, tm.mon, tm.year),
3692 dtEnd = dtStart.GetLastMonthDay();
3693
3694 wxDateTimeArray hol;
3695 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
3696
3697 const wxChar *format = _T("%d-%b-%Y (%a)");
3698
3699 wxPrintf(_T("All holidays between %s and %s:\n"),
3700 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
3701
3702 size_t count = hol.GetCount();
3703 for ( size_t n = 0; n < count; n++ )
3704 {
3705 wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str());
3706 }
3707
3708 wxPuts(wxEmptyString);
3709 }
3710
3711 static void TestTimeZoneBug()
3712 {
3713 wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
3714
3715 wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
3716 for ( int i = 0; i < 31; i++ )
3717 {
3718 wxPrintf(_T("Date %s: week day %s.\n"),
3719 date.Format(_T("%d-%m-%Y")).c_str(),
3720 date.GetWeekDayName(date.GetWeekDay()).c_str());
3721
3722 date += wxDateSpan::Day();
3723 }
3724
3725 wxPuts(wxEmptyString);
3726 }
3727
3728 static void TestTimeSpanFormat()
3729 {
3730 wxPuts(_T("\n*** wxTimeSpan tests ***"));
3731
3732 static const wxChar *formats[] =
3733 {
3734 _T("(default) %H:%M:%S"),
3735 _T("%E weeks and %D days"),
3736 _T("%l milliseconds"),
3737 _T("(with ms) %H:%M:%S:%l"),
3738 _T("100%% of minutes is %M"), // test "%%"
3739 _T("%D days and %H hours"),
3740 _T("or also %S seconds"),
3741 };
3742
3743 wxTimeSpan ts1(1, 2, 3, 4),
3744 ts2(111, 222, 333);
3745 for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
3746 {
3747 wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
3748 ts1.Format(formats[n]).c_str(),
3749 ts2.Format(formats[n]).c_str());
3750 }
3751
3752 wxPuts(wxEmptyString);
3753 }
3754
3755 #endif // TEST_ALL
3756
3757 #endif // TEST_DATETIME
3758
3759 // ----------------------------------------------------------------------------
3760 // wxTextInput/OutputStream
3761 // ----------------------------------------------------------------------------
3762
3763 #ifdef TEST_TEXTSTREAM
3764
3765 #include "wx/txtstrm.h"
3766 #include "wx/wfstream.h"
3767
3768 static void TestTextInputStream()
3769 {
3770 wxPuts(_T("\n*** wxTextInputStream test ***"));
3771
3772 wxString filename = _T("testdata.fc");
3773 wxFileInputStream fsIn(filename);
3774 if ( !fsIn.Ok() )
3775 {
3776 wxPuts(_T("ERROR: couldn't open file."));
3777 }
3778 else
3779 {
3780 wxTextInputStream tis(fsIn);
3781
3782 size_t line = 1;
3783 for ( ;; )
3784 {
3785 const wxString s = tis.ReadLine();
3786
3787 // line could be non empty if the last line of the file isn't
3788 // terminated with EOL
3789 if ( fsIn.Eof() && s.empty() )
3790 break;
3791
3792 wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
3793 }
3794 }
3795 }
3796
3797 #endif // TEST_TEXTSTREAM
3798
3799 // ----------------------------------------------------------------------------
3800 // threads
3801 // ----------------------------------------------------------------------------
3802
3803 #ifdef TEST_THREADS
3804
3805 #include "wx/thread.h"
3806
3807 static size_t gs_counter = (size_t)-1;
3808 static wxCriticalSection gs_critsect;
3809 static wxSemaphore gs_cond;
3810
3811 class MyJoinableThread : public wxThread
3812 {
3813 public:
3814 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
3815 { m_n = n; Create(); }
3816
3817 // thread execution starts here
3818 virtual ExitCode Entry();
3819
3820 private:
3821 size_t m_n;
3822 };
3823
3824 wxThread::ExitCode MyJoinableThread::Entry()
3825 {
3826 unsigned long res = 1;
3827 for ( size_t n = 1; n < m_n; n++ )
3828 {
3829 res *= n;
3830
3831 // it's a loooong calculation :-)
3832 Sleep(100);
3833 }
3834
3835 return (ExitCode)res;
3836 }
3837
3838 class MyDetachedThread : public wxThread
3839 {
3840 public:
3841 MyDetachedThread(size_t n, wxChar ch)
3842 {
3843 m_n = n;
3844 m_ch = ch;
3845 m_cancelled = false;
3846
3847 Create();
3848 }
3849
3850 // thread execution starts here
3851 virtual ExitCode Entry();
3852
3853 // and stops here
3854 virtual void OnExit();
3855
3856 private:
3857 size_t m_n; // number of characters to write
3858 wxChar m_ch; // character to write
3859
3860 bool m_cancelled; // false if we exit normally
3861 };
3862
3863 wxThread::ExitCode MyDetachedThread::Entry()
3864 {
3865 {
3866 wxCriticalSectionLocker lock(gs_critsect);
3867 if ( gs_counter == (size_t)-1 )
3868 gs_counter = 1;
3869 else
3870 gs_counter++;
3871 }
3872
3873 for ( size_t n = 0; n < m_n; n++ )
3874 {
3875 if ( TestDestroy() )
3876 {
3877 m_cancelled = true;
3878
3879 break;
3880 }
3881
3882 wxPutchar(m_ch);
3883 fflush(stdout);
3884
3885 wxThread::Sleep(100);
3886 }
3887
3888 return 0;
3889 }
3890
3891 void MyDetachedThread::OnExit()
3892 {
3893 wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
3894
3895 wxCriticalSectionLocker lock(gs_critsect);
3896 if ( !--gs_counter && !m_cancelled )
3897 gs_cond.Post();
3898 }
3899
3900 static void TestDetachedThreads()
3901 {
3902 wxPuts(_T("\n*** Testing detached threads ***"));
3903
3904 static const size_t nThreads = 3;
3905 MyDetachedThread *threads[nThreads];
3906 size_t n;
3907 for ( n = 0; n < nThreads; n++ )
3908 {
3909 threads[n] = new MyDetachedThread(10, 'A' + n);
3910 }
3911
3912 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
3913 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
3914
3915 for ( n = 0; n < nThreads; n++ )
3916 {
3917 threads[n]->Run();
3918 }
3919
3920 // wait until all threads terminate
3921 gs_cond.Wait();
3922
3923 wxPuts(wxEmptyString);
3924 }
3925
3926 static void TestJoinableThreads()
3927 {
3928 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
3929
3930 // calc 10! in the background
3931 MyJoinableThread thread(10);
3932 thread.Run();
3933
3934 wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
3935 (unsigned long)thread.Wait());
3936 }
3937
3938 static void TestThreadSuspend()
3939 {
3940 wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
3941
3942 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
3943
3944 thread->Run();
3945
3946 // this is for this demo only, in a real life program we'd use another
3947 // condition variable which would be signaled from wxThread::Entry() to
3948 // tell us that the thread really started running - but here just wait a
3949 // bit and hope that it will be enough (the problem is, of course, that
3950 // the thread might still not run when we call Pause() which will result
3951 // in an error)
3952 wxThread::Sleep(300);
3953
3954 for ( size_t n = 0; n < 3; n++ )
3955 {
3956 thread->Pause();
3957
3958 wxPuts(_T("\nThread suspended"));
3959 if ( n > 0 )
3960 {
3961 // don't sleep but resume immediately the first time
3962 wxThread::Sleep(300);
3963 }
3964 wxPuts(_T("Going to resume the thread"));
3965
3966 thread->Resume();
3967 }
3968
3969 wxPuts(_T("Waiting until it terminates now"));
3970
3971 // wait until the thread terminates
3972 gs_cond.Wait();
3973
3974 wxPuts(wxEmptyString);
3975 }
3976
3977 static void TestThreadDelete()
3978 {
3979 // As above, using Sleep() is only for testing here - we must use some
3980 // synchronisation object instead to ensure that the thread is still
3981 // running when we delete it - deleting a detached thread which already
3982 // terminated will lead to a crash!
3983
3984 wxPuts(_T("\n*** Testing thread delete function ***"));
3985
3986 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
3987
3988 thread0->Delete();
3989
3990 wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
3991
3992 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
3993
3994 thread1->Run();
3995
3996 wxThread::Sleep(300);
3997
3998 thread1->Delete();
3999
4000 wxPuts(_T("\nDeleted a running thread."));
4001
4002 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
4003
4004 thread2->Run();
4005
4006 wxThread::Sleep(300);
4007
4008 thread2->Pause();
4009
4010 thread2->Delete();
4011
4012 wxPuts(_T("\nDeleted a sleeping thread."));
4013
4014 MyJoinableThread thread3(20);
4015 thread3.Run();
4016
4017 thread3.Delete();
4018
4019 wxPuts(_T("\nDeleted a joinable thread."));
4020
4021 MyJoinableThread thread4(2);
4022 thread4.Run();
4023
4024 wxThread::Sleep(300);
4025
4026 thread4.Delete();
4027
4028 wxPuts(_T("\nDeleted a joinable thread which already terminated."));
4029
4030 wxPuts(wxEmptyString);
4031 }
4032
4033 class MyWaitingThread : public wxThread
4034 {
4035 public:
4036 MyWaitingThread( wxMutex *mutex, wxCondition *condition )
4037 {
4038 m_mutex = mutex;
4039 m_condition = condition;
4040
4041 Create();
4042 }
4043
4044 virtual ExitCode Entry()
4045 {
4046 wxPrintf(_T("Thread %lu has started running.\n"), GetId());
4047 fflush(stdout);
4048
4049 gs_cond.Post();
4050
4051 wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
4052 fflush(stdout);
4053
4054 m_mutex->Lock();
4055 m_condition->Wait();
4056 m_mutex->Unlock();
4057
4058 wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
4059 fflush(stdout);
4060
4061 return 0;
4062 }
4063
4064 private:
4065 wxMutex *m_mutex;
4066 wxCondition *m_condition;
4067 };
4068
4069 static void TestThreadConditions()
4070 {
4071 wxMutex mutex;
4072 wxCondition condition(mutex);
4073
4074 // otherwise its difficult to understand which log messages pertain to
4075 // which condition
4076 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
4077 // condition.GetId(), gs_cond.GetId());
4078
4079 // create and launch threads
4080 MyWaitingThread *threads[10];
4081
4082 size_t n;
4083 for ( n = 0; n < WXSIZEOF(threads); n++ )
4084 {
4085 threads[n] = new MyWaitingThread( &mutex, &condition );
4086 }
4087
4088 for ( n = 0; n < WXSIZEOF(threads); n++ )
4089 {
4090 threads[n]->Run();
4091 }
4092
4093 // wait until all threads run
4094 wxPuts(_T("Main thread is waiting for the other threads to start"));
4095 fflush(stdout);
4096
4097 size_t nRunning = 0;
4098 while ( nRunning < WXSIZEOF(threads) )
4099 {
4100 gs_cond.Wait();
4101
4102 nRunning++;
4103
4104 wxPrintf(_T("Main thread: %u already running\n"), nRunning);
4105 fflush(stdout);
4106 }
4107
4108 wxPuts(_T("Main thread: all threads started up."));
4109 fflush(stdout);
4110
4111 wxThread::Sleep(500);
4112
4113 #if 1
4114 // now wake one of them up
4115 wxPrintf(_T("Main thread: about to signal the condition.\n"));
4116 fflush(stdout);
4117 condition.Signal();
4118 #endif
4119
4120 wxThread::Sleep(200);
4121
4122 // wake all the (remaining) threads up, so that they can exit
4123 wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
4124 fflush(stdout);
4125 condition.Broadcast();
4126
4127 // give them time to terminate (dirty!)
4128 wxThread::Sleep(500);
4129 }
4130
4131 #include "wx/utils.h"
4132
4133 class MyExecThread : public wxThread
4134 {
4135 public:
4136 MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
4137 m_command(command)
4138 {
4139 Create();
4140 }
4141
4142 virtual ExitCode Entry()
4143 {
4144 return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
4145 }
4146
4147 private:
4148 wxString m_command;
4149 };
4150
4151 static void TestThreadExec()
4152 {
4153 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
4154
4155 MyExecThread thread(_T("true"));
4156 thread.Run();
4157
4158 wxPrintf(_T("Main program exit code: %ld.\n"),
4159 wxExecute(_T("false"), wxEXEC_SYNC));
4160
4161 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
4162 }
4163
4164 // semaphore tests
4165 #include "wx/datetime.h"
4166
4167 class MySemaphoreThread : public wxThread
4168 {
4169 public:
4170 MySemaphoreThread(int i, wxSemaphore *sem)
4171 : wxThread(wxTHREAD_JOINABLE),
4172 m_sem(sem),
4173 m_i(i)
4174 {
4175 Create();
4176 }
4177
4178 virtual ExitCode Entry()
4179 {
4180 wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
4181 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4182
4183 m_sem->Wait();
4184
4185 wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
4186 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4187
4188 Sleep(1000);
4189
4190 wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
4191 wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
4192
4193 m_sem->Post();
4194
4195 return 0;
4196 }
4197
4198 private:
4199 wxSemaphore *m_sem;
4200 int m_i;
4201 };
4202
4203 WX_DEFINE_ARRAY_PTR(wxThread *, ArrayThreads);
4204
4205 static void TestSemaphore()
4206 {
4207 wxPuts(_T("*** Testing wxSemaphore class. ***"));
4208
4209 static const int SEM_LIMIT = 3;
4210
4211 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
4212 ArrayThreads threads;
4213
4214 for ( int i = 0; i < 3*SEM_LIMIT; i++ )
4215 {
4216 threads.Add(new MySemaphoreThread(i, &sem));
4217 threads.Last()->Run();
4218 }
4219
4220 for ( size_t n = 0; n < threads.GetCount(); n++ )
4221 {
4222 threads[n]->Wait();
4223 delete threads[n];
4224 }
4225 }
4226
4227 #endif // TEST_THREADS
4228
4229 // ----------------------------------------------------------------------------
4230 // entry point
4231 // ----------------------------------------------------------------------------
4232
4233 #ifdef TEST_SNGLINST
4234 #include "wx/snglinst.h"
4235 #endif // TEST_SNGLINST
4236
4237 int main(int argc, char **argv)
4238 {
4239 #if wxUSE_UNICODE
4240 wxChar **wxArgv = new wxChar *[argc + 1];
4241
4242 {
4243 int n;
4244
4245 for (n = 0; n < argc; n++ )
4246 {
4247 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
4248 wxArgv[n] = wxStrdup(warg);
4249 }
4250
4251 wxArgv[n] = NULL;
4252 }
4253 #else // !wxUSE_UNICODE
4254 #define wxArgv argv
4255 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
4256
4257 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
4258
4259 wxInitializer initializer;
4260 if ( !initializer )
4261 {
4262 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
4263
4264 return -1;
4265 }
4266
4267 #ifdef TEST_SNGLINST
4268 wxSingleInstanceChecker checker;
4269 if ( checker.Create(_T(".wxconsole.lock")) )
4270 {
4271 if ( checker.IsAnotherRunning() )
4272 {
4273 wxPrintf(_T("Another instance of the program is running, exiting.\n"));
4274
4275 return 1;
4276 }
4277
4278 // wait some time to give time to launch another instance
4279 wxPrintf(_T("Press \"Enter\" to continue..."));
4280 wxFgetc(stdin);
4281 }
4282 else // failed to create
4283 {
4284 wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n"));
4285 }
4286 #endif // TEST_SNGLINST
4287
4288 #ifdef TEST_CMDLINE
4289 TestCmdLineConvert();
4290
4291 #if wxUSE_CMDLINE_PARSER
4292 static const wxCmdLineEntryDesc cmdLineDesc[] =
4293 {
4294 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
4295 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
4296 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
4297 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
4298
4299 { wxCMD_LINE_OPTION, "o", "output", "output file" },
4300 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
4301 { wxCMD_LINE_OPTION, "s", "size", "output block size",
4302 wxCMD_LINE_VAL_NUMBER },
4303 { wxCMD_LINE_OPTION, "d", "date", "output file date",
4304 wxCMD_LINE_VAL_DATE },
4305 { wxCMD_LINE_OPTION, "f", "double", "output double",
4306 wxCMD_LINE_VAL_DOUBLE },
4307
4308 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
4309 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
4310
4311 { wxCMD_LINE_NONE }
4312 };
4313
4314 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
4315
4316 parser.AddOption(_T("project_name"), _T(""), _T("full path to project file"),
4317 wxCMD_LINE_VAL_STRING,
4318 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
4319
4320 switch ( parser.Parse() )
4321 {
4322 case -1:
4323 wxLogMessage(_T("Help was given, terminating."));
4324 break;
4325
4326 case 0:
4327 ShowCmdLine(parser);
4328 break;
4329
4330 default:
4331 wxLogMessage(_T("Syntax error detected, aborting."));
4332 break;
4333 }
4334 #endif // wxUSE_CMDLINE_PARSER
4335
4336 #endif // TEST_CMDLINE
4337
4338 #ifdef TEST_DIR
4339 #if TEST_ALL
4340 TestDirExists();
4341 TestDirEnum();
4342 #endif
4343 TestDirTraverse();
4344 #endif // TEST_DIR
4345
4346 #ifdef TEST_DYNLIB
4347 TestDllLoad();
4348 TestDllListLoaded();
4349 #endif // TEST_DYNLIB
4350
4351 #ifdef TEST_ENVIRON
4352 TestEnvironment();
4353 #endif // TEST_ENVIRON
4354
4355 #ifdef TEST_EXECUTE
4356 TestExecute();
4357 #endif // TEST_EXECUTE
4358
4359 #ifdef TEST_FILECONF
4360 TestFileConfRead();
4361 #endif // TEST_FILECONF
4362
4363 #ifdef TEST_LOCALE
4364 TestDefaultLang();
4365 #endif // TEST_LOCALE
4366
4367 #ifdef TEST_LOG
4368 wxPuts(_T("*** Testing wxLog ***"));
4369
4370 wxString s;
4371 for ( size_t n = 0; n < 8000; n++ )
4372 {
4373 s << (wxChar)(_T('A') + (n % 26));
4374 }
4375
4376 wxLogWarning(_T("The length of the string is %lu"),
4377 (unsigned long)s.length());
4378
4379 wxString msg;
4380 msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str());
4381
4382 // this one shouldn't be truncated
4383 wxPrintf(msg);
4384
4385 // but this one will because log functions use fixed size buffer
4386 // (note that it doesn't need '\n' at the end neither - will be added
4387 // by wxLog anyhow)
4388 wxLogMessage(_T("A very very long message 2: '%s', the end!"), s.c_str());
4389 #endif // TEST_LOG
4390
4391 #ifdef TEST_FILE
4392 TestFileRead();
4393 TestTextFileRead();
4394 TestFileCopy();
4395 TestTempFile();
4396 #endif // TEST_FILE
4397
4398 #ifdef TEST_FILENAME
4399 TestFileNameTemp();
4400 TestFileNameCwd();
4401 TestFileNameDirManip();
4402 TestFileNameComparison();
4403 TestFileNameOperations();
4404 #endif // TEST_FILENAME
4405
4406 #ifdef TEST_FILETIME
4407 TestFileGetTimes();
4408 #if 0
4409 TestFileSetTimes();
4410 #endif
4411 #endif // TEST_FILETIME
4412
4413 #ifdef TEST_FTP
4414 wxLog::AddTraceMask(FTP_TRACE_MASK);
4415 if ( TestFtpConnect() )
4416 {
4417 #if TEST_ALL
4418 TestFtpList();
4419 TestFtpDownload();
4420 TestFtpMisc();
4421 TestFtpFileSize();
4422 TestFtpUpload();
4423 #endif // TEST_ALL
4424
4425 #if TEST_INTERACTIVE
4426 TestFtpInteractive();
4427 #endif
4428 }
4429 //else: connecting to the FTP server failed
4430
4431 #if 0
4432 TestFtpWuFtpd();
4433 #endif
4434 #endif // TEST_FTP
4435
4436 #ifdef TEST_MIME
4437 //wxLog::AddTraceMask(_T("mime"));
4438 TestMimeEnum();
4439 #if 0
4440 TestMimeOverride();
4441 TestMimeAssociate();
4442 #endif
4443 TestMimeFilename();
4444 #endif // TEST_MIME
4445
4446 #ifdef TEST_INFO_FUNCTIONS
4447 TestOsInfo();
4448 TestPlatformInfo();
4449 TestUserInfo();
4450
4451 #if TEST_INTERACTIVE
4452 TestDiskInfo();
4453 #endif
4454 #endif // TEST_INFO_FUNCTIONS
4455
4456 #ifdef TEST_PATHLIST
4457 TestPathList();
4458 #endif // TEST_PATHLIST
4459
4460 #ifdef TEST_PRINTF
4461 TestPrintf();
4462 #endif // TEST_PRINTF
4463
4464 #ifdef TEST_REGCONF
4465 #if 0
4466 TestRegConfWrite();
4467 #endif
4468 TestRegConfRead();
4469 #endif // TEST_REGCONF
4470
4471 #if defined TEST_REGEX && TEST_INTERACTIVE
4472 TestRegExInteractive();
4473 #endif // defined TEST_REGEX && TEST_INTERACTIVE
4474
4475 #ifdef TEST_REGISTRY
4476 TestRegistryRead();
4477 TestRegistryAssociation();
4478 #endif // TEST_REGISTRY
4479
4480 #ifdef TEST_SOCKETS
4481 TestSocketServer();
4482 TestSocketClient();
4483 #endif // TEST_SOCKETS
4484
4485 #ifdef TEST_STREAMS
4486 #if TEST_ALL
4487 TestFileStream();
4488 #endif
4489 TestMemoryStream();
4490 #endif // TEST_STREAMS
4491
4492 #ifdef TEST_TEXTSTREAM
4493 TestTextInputStream();
4494 #endif // TEST_TEXTSTREAM
4495
4496 #ifdef TEST_THREADS
4497 int nCPUs = wxThread::GetCPUCount();
4498 wxPrintf(_T("This system has %d CPUs\n"), nCPUs);
4499 if ( nCPUs != -1 )
4500 wxThread::SetConcurrency(nCPUs);
4501
4502 TestJoinableThreads();
4503
4504 #if TEST_ALL
4505 TestJoinableThreads();
4506 TestDetachedThreads();
4507 TestThreadSuspend();
4508 TestThreadDelete();
4509 TestThreadConditions();
4510 TestThreadExec();
4511 TestSemaphore();
4512 #endif
4513 #endif // TEST_THREADS
4514
4515 #ifdef TEST_TIMER
4516 TestStopWatch();
4517 TestTimer();
4518 #endif // TEST_TIMER
4519
4520 #ifdef TEST_DATETIME
4521 #if TEST_ALL
4522 TestTimeStatic();
4523 TestTimeRange();
4524 TestTimeZones();
4525 TestTimeDST();
4526 TestTimeHolidays();
4527 TestTimeSpanFormat();
4528 TestTimeMS();
4529
4530 TestTimeZoneBug();
4531 #endif
4532
4533 #if TEST_INTERACTIVE
4534 TestDateTimeInteractive();
4535 #endif
4536 #endif // TEST_DATETIME
4537
4538 #ifdef TEST_SCOPEGUARD
4539 TestScopeGuard();
4540 #endif
4541
4542 #ifdef TEST_STACKWALKER
4543 #if wxUSE_STACKWALKER
4544 TestStackWalk(argv[0]);
4545 #endif
4546 #endif // TEST_STACKWALKER
4547
4548 #ifdef TEST_STDPATHS
4549 TestStandardPaths();
4550 #endif
4551
4552 #ifdef TEST_USLEEP
4553 wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z..."));
4554 wxUsleep(3000);
4555 #endif // TEST_USLEEP
4556
4557 #ifdef TEST_VCARD
4558 TestVCardRead();
4559 TestVCardWrite();
4560 #endif // TEST_VCARD
4561
4562 #ifdef TEST_VOLUME
4563 TestFSVolume();
4564 #endif // TEST_VOLUME
4565
4566 #ifdef TEST_WCHAR
4567 TestUtf8();
4568 TestEncodingConverter();
4569 #endif // TEST_WCHAR
4570
4571 #ifdef TEST_ZIP
4572 TestZipStreamRead();
4573 TestZipFileSystem();
4574 #endif // TEST_ZIP
4575
4576 #if wxUSE_UNICODE
4577 {
4578 for ( int n = 0; n < argc; n++ )
4579 free(wxArgv[n]);
4580
4581 delete [] wxArgv;
4582 }
4583 #endif // wxUSE_UNICODE
4584
4585 wxUnusedVar(argc);
4586 wxUnusedVar(argv);
4587 return 0;
4588 }