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