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