]>
Commit | Line | Data |
---|---|---|
37667812 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/console/console.cpp | |
3 | // Purpose: a sample console (as opposed to GUI) progam using wxWindows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 04.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
e87271f3 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
e84010cf | 20 | #include "wx/defs.h" |
b11a23f3 VZ |
21 | |
22 | #if wxUSE_GUI | |
23 | #error "This sample can't be compiled in GUI mode." | |
24 | #endif // wxUSE_GUI | |
25 | ||
37667812 VZ |
26 | #include <stdio.h> |
27 | ||
e84010cf GD |
28 | #include "wx/string.h" |
29 | #include "wx/file.h" | |
30 | #include "wx/app.h" | |
e87271f3 | 31 | |
d31b7b68 VZ |
32 | // without this pragma, the stupid compiler precompiles #defines below so that |
33 | // changing them doesn't "take place" later! | |
34 | #ifdef __VISUALC__ | |
35 | #pragma hdrstop | |
36 | #endif | |
37 | ||
e87271f3 VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // conditional compilation | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
daa2c7d9 VZ |
42 | /* |
43 | A note about all these conditional compilation macros: this file is used | |
44 | both as a test suite for various non-GUI wxWindows classes and as a | |
45 | scratchpad for quick tests. So there are two compilation modes: if you | |
46 | define TEST_ALL all tests are run, otherwise you may enable the individual | |
47 | tests individually in the "#else" branch below. | |
48 | */ | |
49 | ||
31f6de22 | 50 | // what to test (in alphabetic order)? uncomment the line below to do all tests |
353f41cb | 51 | // #define TEST_ALL |
31f6de22 VZ |
52 | #ifdef TEST_ALL |
53 | #define TEST_ARRAYS | |
54 | #define TEST_CHARSET | |
55 | #define TEST_CMDLINE | |
56 | #define TEST_DATETIME | |
57 | #define TEST_DIR | |
58 | #define TEST_DLLLOADER | |
59 | #define TEST_ENVIRON | |
60 | #define TEST_EXECUTE | |
61 | #define TEST_FILE | |
62 | #define TEST_FILECONF | |
63 | #define TEST_FILENAME | |
64 | #define TEST_FILETIME | |
65 | #define TEST_FTP | |
66 | #define TEST_HASH | |
0508ba2a | 67 | #define TEST_HASHMAP |
31f6de22 VZ |
68 | #define TEST_INFO_FUNCTIONS |
69 | #define TEST_LIST | |
70 | #define TEST_LOCALE | |
71 | #define TEST_LOG | |
72 | #define TEST_LONGLONG | |
73 | #define TEST_MIME | |
74 | #define TEST_PATHLIST | |
8d5eff60 | 75 | #define TEST_ODBC |
31f6de22 VZ |
76 | #define TEST_REGCONF |
77 | #define TEST_REGEX | |
78 | #define TEST_REGISTRY | |
79 | #define TEST_SNGLINST | |
80 | #define TEST_SOCKETS | |
81 | #define TEST_STREAMS | |
82 | #define TEST_STRINGS | |
83 | #define TEST_THREADS | |
84 | #define TEST_TIMER | |
85 | // #define TEST_VCARD -- don't enable this (VZ) | |
0e2c5534 | 86 | #define TEST_VOLUME |
31f6de22 VZ |
87 | #define TEST_WCHAR |
88 | #define TEST_ZIP | |
89 | #define TEST_ZLIB | |
daa2c7d9 VZ |
90 | |
91 | #undef TEST_ALL | |
92 | static const bool TEST_ALL = TRUE; | |
31f6de22 | 93 | #else |
99a5af7f | 94 | #define TEST_STREAMS |
daa2c7d9 VZ |
95 | |
96 | static const bool TEST_ALL = FALSE; | |
31f6de22 | 97 | #endif |
f6bcfd97 | 98 | |
daa2c7d9 VZ |
99 | // some tests are interactive, define this to run them |
100 | #ifdef TEST_INTERACTIVE | |
101 | #undef TEST_INTERACTIVE | |
102 | ||
19f45995 | 103 | static const bool TEST_INTERACTIVE = TRUE; |
daa2c7d9 VZ |
104 | #else |
105 | static const bool TEST_INTERACTIVE = FALSE; | |
106 | #endif | |
58b24a56 | 107 | |
f6bcfd97 BP |
108 | // ---------------------------------------------------------------------------- |
109 | // test class for container objects | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
112 | #if defined(TEST_ARRAYS) || defined(TEST_LIST) | |
113 | ||
114 | class Bar // Foo is already taken in the hash test | |
115 | { | |
116 | public: | |
117 | Bar(const wxString& name) : m_name(name) { ms_bars++; } | |
3dc01741 | 118 | Bar(const Bar& bar) : m_name(bar.m_name) { ms_bars++; } |
f6bcfd97 BP |
119 | ~Bar() { ms_bars--; } |
120 | ||
121 | static size_t GetNumber() { return ms_bars; } | |
122 | ||
123 | const char *GetName() const { return m_name; } | |
124 | ||
125 | private: | |
126 | wxString m_name; | |
127 | ||
128 | static size_t ms_bars; | |
129 | }; | |
130 | ||
131 | size_t Bar::ms_bars = 0; | |
132 | ||
133 | #endif // defined(TEST_ARRAYS) || defined(TEST_LIST) | |
e87271f3 VZ |
134 | |
135 | // ============================================================================ | |
136 | // implementation | |
137 | // ============================================================================ | |
138 | ||
8e907a13 VZ |
139 | // ---------------------------------------------------------------------------- |
140 | // helper functions | |
141 | // ---------------------------------------------------------------------------- | |
142 | ||
143 | #if defined(TEST_STRINGS) || defined(TEST_SOCKETS) | |
144 | ||
145 | // replace TABs with \t and CRs with \n | |
146 | static wxString MakePrintable(const wxChar *s) | |
147 | { | |
148 | wxString str(s); | |
149 | (void)str.Replace(_T("\t"), _T("\\t")); | |
150 | (void)str.Replace(_T("\n"), _T("\\n")); | |
151 | (void)str.Replace(_T("\r"), _T("\\r")); | |
152 | ||
153 | return str; | |
154 | } | |
155 | ||
156 | #endif // MakePrintable() is used | |
157 | ||
551fe3a6 VZ |
158 | // ---------------------------------------------------------------------------- |
159 | // wxFontMapper::CharsetToEncoding | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | #ifdef TEST_CHARSET | |
163 | ||
e84010cf | 164 | #include "wx/fontmap.h" |
551fe3a6 VZ |
165 | |
166 | static void TestCharset() | |
167 | { | |
168 | static const wxChar *charsets[] = | |
169 | { | |
170 | // some vali charsets | |
171 | _T("us-ascii "), | |
172 | _T("iso8859-1 "), | |
173 | _T("iso-8859-12 "), | |
174 | _T("koi8-r "), | |
175 | _T("utf-7 "), | |
176 | _T("cp1250 "), | |
177 | _T("windows-1252"), | |
178 | ||
179 | // and now some bogus ones | |
180 | _T(" "), | |
181 | _T("cp1249 "), | |
182 | _T("iso--8859-1 "), | |
183 | _T("iso-8859-19 "), | |
184 | }; | |
185 | ||
186 | for ( size_t n = 0; n < WXSIZEOF(charsets); n++ ) | |
187 | { | |
142b3bc2 | 188 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charsets[n]); |
551fe3a6 VZ |
189 | wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"), |
190 | charsets[n], | |
142b3bc2 VS |
191 | wxFontMapper::Get()->GetEncodingName(enc).c_str(), |
192 | wxFontMapper::Get()->GetEncodingDescription(enc).c_str()); | |
551fe3a6 VZ |
193 | } |
194 | } | |
195 | ||
196 | #endif // TEST_CHARSET | |
197 | ||
d34bce84 VZ |
198 | // ---------------------------------------------------------------------------- |
199 | // wxCmdLineParser | |
200 | // ---------------------------------------------------------------------------- | |
201 | ||
d31b7b68 VZ |
202 | #ifdef TEST_CMDLINE |
203 | ||
e84010cf GD |
204 | #include "wx/cmdline.h" |
205 | #include "wx/datetime.h" | |
d34bce84 | 206 | |
31f6de22 VZ |
207 | #if wxUSE_CMDLINE_PARSER |
208 | ||
d34bce84 VZ |
209 | static void ShowCmdLine(const wxCmdLineParser& parser) |
210 | { | |
211 | wxString s = "Input files: "; | |
212 | ||
213 | size_t count = parser.GetParamCount(); | |
214 | for ( size_t param = 0; param < count; param++ ) | |
215 | { | |
216 | s << parser.GetParam(param) << ' '; | |
217 | } | |
218 | ||
219 | s << '\n' | |
220 | << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n' | |
221 | << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n'; | |
222 | ||
223 | wxString strVal; | |
224 | long lVal; | |
225 | wxDateTime dt; | |
226 | if ( parser.Found("o", &strVal) ) | |
227 | s << "Output file:\t" << strVal << '\n'; | |
228 | if ( parser.Found("i", &strVal) ) | |
229 | s << "Input dir:\t" << strVal << '\n'; | |
230 | if ( parser.Found("s", &lVal) ) | |
231 | s << "Size:\t" << lVal << '\n'; | |
232 | if ( parser.Found("d", &dt) ) | |
233 | s << "Date:\t" << dt.FormatISODate() << '\n'; | |
f6bcfd97 BP |
234 | if ( parser.Found("project_name", &strVal) ) |
235 | s << "Project:\t" << strVal << '\n'; | |
d34bce84 VZ |
236 | |
237 | wxLogMessage(s); | |
238 | } | |
239 | ||
31f6de22 VZ |
240 | #endif // wxUSE_CMDLINE_PARSER |
241 | ||
242 | static void TestCmdLineConvert() | |
243 | { | |
244 | static const char *cmdlines[] = | |
245 | { | |
246 | "arg1 arg2", | |
247 | "-a \"-bstring 1\" -c\"string 2\" \"string 3\"", | |
248 | "literal \\\" and \"\"", | |
249 | }; | |
250 | ||
251 | for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ ) | |
252 | { | |
253 | const char *cmdline = cmdlines[n]; | |
254 | printf("Parsing: %s\n", cmdline); | |
255 | wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline); | |
256 | ||
257 | size_t count = args.GetCount(); | |
258 | printf("\targc = %u\n", count); | |
259 | for ( size_t arg = 0; arg < count; arg++ ) | |
260 | { | |
daa2c7d9 | 261 | printf("\targv[%u] = %s\n", arg, args[arg].c_str()); |
31f6de22 VZ |
262 | } |
263 | } | |
264 | } | |
265 | ||
d34bce84 VZ |
266 | #endif // TEST_CMDLINE |
267 | ||
1944c6bd VZ |
268 | // ---------------------------------------------------------------------------- |
269 | // wxDir | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | #ifdef TEST_DIR | |
273 | ||
e84010cf | 274 | #include "wx/dir.h" |
1944c6bd | 275 | |
35332784 VZ |
276 | #ifdef __UNIX__ |
277 | static const wxChar *ROOTDIR = _T("/"); | |
278 | static const wxChar *TESTDIR = _T("/usr"); | |
279 | #elif defined(__WXMSW__) | |
280 | static const wxChar *ROOTDIR = _T("c:\\"); | |
281 | static const wxChar *TESTDIR = _T("d:\\"); | |
282 | #else | |
283 | #error "don't know where the root directory is" | |
284 | #endif | |
285 | ||
1944c6bd VZ |
286 | static void TestDirEnumHelper(wxDir& dir, |
287 | int flags = wxDIR_DEFAULT, | |
288 | const wxString& filespec = wxEmptyString) | |
289 | { | |
290 | wxString filename; | |
291 | ||
292 | if ( !dir.IsOpened() ) | |
293 | return; | |
294 | ||
295 | bool cont = dir.GetFirst(&filename, filespec, flags); | |
296 | while ( cont ) | |
297 | { | |
298 | printf("\t%s\n", filename.c_str()); | |
299 | ||
300 | cont = dir.GetNext(&filename); | |
301 | } | |
302 | ||
303 | puts(""); | |
304 | } | |
305 | ||
306 | static void TestDirEnum() | |
307 | { | |
35332784 VZ |
308 | puts("*** Testing wxDir::GetFirst/GetNext ***"); |
309 | ||
149147e1 | 310 | wxString cwd = wxGetCwd(); |
9475670f | 311 | if ( !wxDir::Exists(cwd) ) |
149147e1 VZ |
312 | { |
313 | printf("ERROR: current directory '%s' doesn't exist?\n", cwd.c_str()); | |
314 | return; | |
315 | } | |
316 | ||
99a5af7f | 317 | wxDir dir("s:/tmp/foo"); |
149147e1 VZ |
318 | if ( !dir.IsOpened() ) |
319 | { | |
320 | printf("ERROR: failed to open current directory '%s'.\n", cwd.c_str()); | |
321 | return; | |
322 | } | |
1944c6bd VZ |
323 | |
324 | puts("Enumerating everything in current directory:"); | |
325 | TestDirEnumHelper(dir); | |
326 | ||
327 | puts("Enumerating really everything in current directory:"); | |
328 | TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT); | |
329 | ||
330 | puts("Enumerating object files in current directory:"); | |
331 | TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o"); | |
332 | ||
333 | puts("Enumerating directories in current directory:"); | |
334 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
335 | ||
336 | puts("Enumerating files in current directory:"); | |
337 | TestDirEnumHelper(dir, wxDIR_FILES); | |
338 | ||
339 | puts("Enumerating files including hidden in current directory:"); | |
340 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
341 | ||
35332784 | 342 | dir.Open(ROOTDIR); |
1944c6bd VZ |
343 | |
344 | puts("Enumerating everything in root directory:"); | |
345 | TestDirEnumHelper(dir, wxDIR_DEFAULT); | |
346 | ||
347 | puts("Enumerating directories in root directory:"); | |
348 | TestDirEnumHelper(dir, wxDIR_DIRS); | |
349 | ||
350 | puts("Enumerating files in root directory:"); | |
351 | TestDirEnumHelper(dir, wxDIR_FILES); | |
352 | ||
353 | puts("Enumerating files including hidden in root directory:"); | |
354 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); | |
355 | ||
356 | puts("Enumerating files in non existing directory:"); | |
357 | wxDir dirNo("nosuchdir"); | |
358 | TestDirEnumHelper(dirNo); | |
359 | } | |
360 | ||
35332784 VZ |
361 | class DirPrintTraverser : public wxDirTraverser |
362 | { | |
363 | public: | |
364 | virtual wxDirTraverseResult OnFile(const wxString& filename) | |
365 | { | |
366 | return wxDIR_CONTINUE; | |
367 | } | |
368 | ||
369 | virtual wxDirTraverseResult OnDir(const wxString& dirname) | |
370 | { | |
371 | wxString path, name, ext; | |
372 | wxSplitPath(dirname, &path, &name, &ext); | |
373 | ||
374 | if ( !ext.empty() ) | |
375 | name << _T('.') << ext; | |
376 | ||
377 | wxString indent; | |
378 | for ( const wxChar *p = path.c_str(); *p; p++ ) | |
379 | { | |
380 | if ( wxIsPathSeparator(*p) ) | |
381 | indent += _T(" "); | |
382 | } | |
383 | ||
384 | printf("%s%s\n", indent.c_str(), name.c_str()); | |
385 | ||
386 | return wxDIR_CONTINUE; | |
387 | } | |
388 | }; | |
389 | ||
390 | static void TestDirTraverse() | |
391 | { | |
392 | puts("*** Testing wxDir::Traverse() ***"); | |
393 | ||
394 | // enum all files | |
395 | wxArrayString files; | |
396 | size_t n = wxDir::GetAllFiles(TESTDIR, &files); | |
397 | printf("There are %u files under '%s'\n", n, TESTDIR); | |
398 | if ( n > 1 ) | |
399 | { | |
2d3112ad VZ |
400 | printf("First one is '%s'\n", files[0u].c_str()); |
401 | printf(" last one is '%s'\n", files[n - 1].c_str()); | |
35332784 VZ |
402 | } |
403 | ||
404 | // enum again with custom traverser | |
405 | wxDir dir(TESTDIR); | |
406 | DirPrintTraverser traverser; | |
407 | dir.Traverse(traverser, _T(""), wxDIR_DIRS | wxDIR_HIDDEN); | |
408 | } | |
409 | ||
149147e1 VZ |
410 | static void TestDirExists() |
411 | { | |
412 | wxPuts(_T("*** Testing wxDir::Exists() ***")); | |
413 | ||
414 | static const char *dirnames[] = | |
415 | { | |
416 | _T("."), | |
417 | #if defined(__WXMSW__) | |
418 | _T("c:"), | |
419 | _T("c:\\"), | |
420 | _T("\\\\share\\file"), | |
421 | _T("c:\\dos"), | |
422 | _T("c:\\dos\\"), | |
423 | _T("c:\\dos\\\\"), | |
424 | _T("c:\\autoexec.bat"), | |
425 | #elif defined(__UNIX__) | |
426 | _T("/"), | |
427 | _T("//"), | |
428 | _T("/usr/bin"), | |
429 | _T("/usr//bin"), | |
430 | _T("/usr///bin"), | |
431 | #endif | |
432 | }; | |
433 | ||
434 | for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ ) | |
435 | { | |
436 | printf(_T("%-40s: %s\n"), | |
437 | dirnames[n], | |
438 | wxDir::Exists(dirnames[n]) ? _T("exists") : _T("doesn't exist")); | |
439 | } | |
440 | } | |
441 | ||
1944c6bd VZ |
442 | #endif // TEST_DIR |
443 | ||
f6bcfd97 BP |
444 | // ---------------------------------------------------------------------------- |
445 | // wxDllLoader | |
446 | // ---------------------------------------------------------------------------- | |
447 | ||
448 | #ifdef TEST_DLLLOADER | |
449 | ||
e84010cf | 450 | #include "wx/dynlib.h" |
f6bcfd97 BP |
451 | |
452 | static void TestDllLoad() | |
453 | { | |
454 | #if defined(__WXMSW__) | |
455 | static const wxChar *LIB_NAME = _T("kernel32.dll"); | |
456 | static const wxChar *FUNC_NAME = _T("lstrlenA"); | |
457 | #elif defined(__UNIX__) | |
458 | // weird: using just libc.so does *not* work! | |
459 | static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so"); | |
460 | static const wxChar *FUNC_NAME = _T("strlen"); | |
461 | #else | |
462 | #error "don't know how to test wxDllLoader on this platform" | |
463 | #endif | |
464 | ||
465 | puts("*** testing wxDllLoader ***\n"); | |
466 | ||
467 | wxDllType dllHandle = wxDllLoader::LoadLibrary(LIB_NAME); | |
468 | if ( !dllHandle ) | |
469 | { | |
470 | wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME); | |
471 | } | |
472 | else | |
473 | { | |
e84010cf | 474 | typedef int (*strlenType)(const char *); |
f6bcfd97 BP |
475 | strlenType pfnStrlen = (strlenType)wxDllLoader::GetSymbol(dllHandle, FUNC_NAME); |
476 | if ( !pfnStrlen ) | |
477 | { | |
478 | wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"), | |
479 | FUNC_NAME, LIB_NAME); | |
480 | } | |
481 | else | |
482 | { | |
483 | if ( pfnStrlen("foo") != 3 ) | |
484 | { | |
485 | wxPrintf(_T("ERROR: loaded function is not strlen()!\n")); | |
486 | } | |
487 | else | |
488 | { | |
489 | puts("... ok"); | |
490 | } | |
491 | } | |
492 | ||
493 | wxDllLoader::UnloadLibrary(dllHandle); | |
494 | } | |
495 | } | |
496 | ||
497 | #endif // TEST_DLLLOADER | |
498 | ||
8fd0d89b VZ |
499 | // ---------------------------------------------------------------------------- |
500 | // wxGet/SetEnv | |
501 | // ---------------------------------------------------------------------------- | |
502 | ||
503 | #ifdef TEST_ENVIRON | |
504 | ||
e84010cf | 505 | #include "wx/utils.h" |
8fd0d89b | 506 | |
308978f6 VZ |
507 | static wxString MyGetEnv(const wxString& var) |
508 | { | |
509 | wxString val; | |
510 | if ( !wxGetEnv(var, &val) ) | |
511 | val = _T("<empty>"); | |
512 | else | |
513 | val = wxString(_T('\'')) + val + _T('\''); | |
514 | ||
515 | return val; | |
516 | } | |
517 | ||
8fd0d89b VZ |
518 | static void TestEnvironment() |
519 | { | |
520 | const wxChar *var = _T("wxTestVar"); | |
521 | ||
522 | puts("*** testing environment access functions ***"); | |
523 | ||
308978f6 | 524 | printf("Initially getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); |
8fd0d89b | 525 | wxSetEnv(var, _T("value for wxTestVar")); |
308978f6 | 526 | printf("After wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); |
8fd0d89b | 527 | wxSetEnv(var, _T("another value")); |
308978f6 | 528 | printf("After 2nd wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); |
8fd0d89b | 529 | wxUnsetEnv(var); |
308978f6 | 530 | printf("After wxUnsetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str()); |
2d3112ad | 531 | printf("PATH = %s\n", MyGetEnv(_T("PATH")).c_str()); |
8fd0d89b VZ |
532 | } |
533 | ||
534 | #endif // TEST_ENVIRON | |
535 | ||
d93c719a VZ |
536 | // ---------------------------------------------------------------------------- |
537 | // wxExecute | |
538 | // ---------------------------------------------------------------------------- | |
539 | ||
540 | #ifdef TEST_EXECUTE | |
541 | ||
e84010cf | 542 | #include "wx/utils.h" |
d93c719a VZ |
543 | |
544 | static void TestExecute() | |
545 | { | |
546 | puts("*** testing wxExecute ***"); | |
547 | ||
548 | #ifdef __UNIX__ | |
a1f79c1e | 549 | #define COMMAND "cat -n ../../Makefile" // "echo hi" |
2c8e4738 | 550 | #define SHELL_COMMAND "echo hi from shell" |
a1f79c1e | 551 | #define REDIRECT_COMMAND COMMAND // "date" |
d93c719a VZ |
552 | #elif defined(__WXMSW__) |
553 | #define COMMAND "command.com -c 'echo hi'" | |
2c8e4738 VZ |
554 | #define SHELL_COMMAND "echo hi" |
555 | #define REDIRECT_COMMAND COMMAND | |
d93c719a VZ |
556 | #else |
557 | #error "no command to exec" | |
558 | #endif // OS | |
559 | ||
2c8e4738 VZ |
560 | printf("Testing wxShell: "); |
561 | fflush(stdout); | |
562 | if ( wxShell(SHELL_COMMAND) ) | |
563 | puts("Ok."); | |
d93c719a | 564 | else |
2c8e4738 VZ |
565 | puts("ERROR."); |
566 | ||
567 | printf("Testing wxExecute: "); | |
568 | fflush(stdout); | |
569 | if ( wxExecute(COMMAND, TRUE /* sync */) == 0 ) | |
570 | puts("Ok."); | |
571 | else | |
572 | puts("ERROR."); | |
573 | ||
574 | #if 0 // no, it doesn't work (yet?) | |
575 | printf("Testing async wxExecute: "); | |
576 | fflush(stdout); | |
577 | if ( wxExecute(COMMAND) != 0 ) | |
578 | puts("Ok (command launched)."); | |
579 | else | |
580 | puts("ERROR."); | |
581 | #endif // 0 | |
582 | ||
583 | printf("Testing wxExecute with redirection:\n"); | |
584 | wxArrayString output; | |
585 | if ( wxExecute(REDIRECT_COMMAND, output) != 0 ) | |
586 | { | |
587 | puts("ERROR."); | |
588 | } | |
589 | else | |
590 | { | |
591 | size_t count = output.GetCount(); | |
592 | for ( size_t n = 0; n < count; n++ ) | |
593 | { | |
594 | printf("\t%s\n", output[n].c_str()); | |
595 | } | |
596 | ||
597 | puts("Ok."); | |
598 | } | |
d93c719a VZ |
599 | } |
600 | ||
601 | #endif // TEST_EXECUTE | |
602 | ||
f6bcfd97 BP |
603 | // ---------------------------------------------------------------------------- |
604 | // file | |
605 | // ---------------------------------------------------------------------------- | |
606 | ||
607 | #ifdef TEST_FILE | |
608 | ||
e84010cf GD |
609 | #include "wx/file.h" |
610 | #include "wx/ffile.h" | |
611 | #include "wx/textfile.h" | |
f6bcfd97 BP |
612 | |
613 | static void TestFileRead() | |
614 | { | |
615 | puts("*** wxFile read test ***"); | |
616 | ||
617 | wxFile file(_T("testdata.fc")); | |
618 | if ( file.IsOpened() ) | |
619 | { | |
620 | printf("File length: %lu\n", file.Length()); | |
621 | ||
622 | puts("File dump:\n----------"); | |
623 | ||
3ca6a5f0 | 624 | static const off_t len = 1024; |
f6bcfd97 BP |
625 | char buf[len]; |
626 | for ( ;; ) | |
627 | { | |
628 | off_t nRead = file.Read(buf, len); | |
629 | if ( nRead == wxInvalidOffset ) | |
630 | { | |
631 | printf("Failed to read the file."); | |
632 | break; | |
633 | } | |
634 | ||
635 | fwrite(buf, nRead, 1, stdout); | |
636 | ||
637 | if ( nRead < len ) | |
638 | break; | |
639 | } | |
640 | ||
641 | puts("----------"); | |
642 | } | |
643 | else | |
644 | { | |
645 | printf("ERROR: can't open test file.\n"); | |
646 | } | |
647 | ||
648 | puts(""); | |
649 | } | |
650 | ||
651 | static void TestTextFileRead() | |
652 | { | |
653 | puts("*** wxTextFile read test ***"); | |
654 | ||
655 | wxTextFile file(_T("testdata.fc")); | |
656 | if ( file.Open() ) | |
657 | { | |
658 | printf("Number of lines: %u\n", file.GetLineCount()); | |
659 | printf("Last line: '%s'\n", file.GetLastLine().c_str()); | |
3ca6a5f0 BP |
660 | |
661 | wxString s; | |
662 | ||
663 | puts("\nDumping the entire file:"); | |
664 | for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() ) | |
665 | { | |
666 | printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str()); | |
667 | } | |
668 | printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str()); | |
669 | ||
670 | puts("\nAnd now backwards:"); | |
671 | for ( s = file.GetLastLine(); | |
672 | file.GetCurrentLine() != 0; | |
673 | s = file.GetPrevLine() ) | |
674 | { | |
675 | printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str()); | |
676 | } | |
677 | printf("%6u: %s\n", file.GetCurrentLine() + 1, s.c_str()); | |
f6bcfd97 BP |
678 | } |
679 | else | |
680 | { | |
681 | printf("ERROR: can't open '%s'\n", file.GetName()); | |
682 | } | |
683 | ||
684 | puts(""); | |
685 | } | |
686 | ||
a339970a VZ |
687 | static void TestFileCopy() |
688 | { | |
689 | puts("*** Testing wxCopyFile ***"); | |
690 | ||
691 | static const wxChar *filename1 = _T("testdata.fc"); | |
692 | static const wxChar *filename2 = _T("test2"); | |
693 | if ( !wxCopyFile(filename1, filename2) ) | |
694 | { | |
695 | puts("ERROR: failed to copy file"); | |
696 | } | |
697 | else | |
698 | { | |
699 | wxFFile f1(filename1, "rb"), | |
700 | f2(filename2, "rb"); | |
701 | ||
702 | if ( !f1.IsOpened() || !f2.IsOpened() ) | |
703 | { | |
704 | puts("ERROR: failed to open file(s)"); | |
705 | } | |
706 | else | |
707 | { | |
708 | wxString s1, s2; | |
709 | if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) ) | |
710 | { | |
711 | puts("ERROR: failed to read file(s)"); | |
712 | } | |
713 | else | |
714 | { | |
715 | if ( (s1.length() != s2.length()) || | |
716 | (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) ) | |
717 | { | |
718 | puts("ERROR: copy error!"); | |
719 | } | |
720 | else | |
721 | { | |
722 | puts("File was copied ok."); | |
723 | } | |
724 | } | |
725 | } | |
726 | } | |
727 | ||
728 | if ( !wxRemoveFile(filename2) ) | |
729 | { | |
730 | puts("ERROR: failed to remove the file"); | |
731 | } | |
732 | ||
733 | puts(""); | |
734 | } | |
735 | ||
f6bcfd97 BP |
736 | #endif // TEST_FILE |
737 | ||
ee6e1b1d VZ |
738 | // ---------------------------------------------------------------------------- |
739 | // wxFileConfig | |
740 | // ---------------------------------------------------------------------------- | |
741 | ||
742 | #ifdef TEST_FILECONF | |
743 | ||
e84010cf GD |
744 | #include "wx/confbase.h" |
745 | #include "wx/fileconf.h" | |
ee6e1b1d VZ |
746 | |
747 | static const struct FileConfTestData | |
748 | { | |
749 | const wxChar *name; // value name | |
750 | const wxChar *value; // the value from the file | |
751 | } fcTestData[] = | |
752 | { | |
753 | { _T("value1"), _T("one") }, | |
754 | { _T("value2"), _T("two") }, | |
755 | { _T("novalue"), _T("default") }, | |
756 | }; | |
757 | ||
758 | static void TestFileConfRead() | |
759 | { | |
760 | puts("*** testing wxFileConfig loading/reading ***"); | |
761 | ||
762 | wxFileConfig fileconf(_T("test"), wxEmptyString, | |
763 | _T("testdata.fc"), wxEmptyString, | |
764 | wxCONFIG_USE_RELATIVE_PATH); | |
765 | ||
766 | // test simple reading | |
767 | puts("\nReading config file:"); | |
768 | wxString defValue(_T("default")), value; | |
769 | for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ ) | |
770 | { | |
771 | const FileConfTestData& data = fcTestData[n]; | |
772 | value = fileconf.Read(data.name, defValue); | |
773 | printf("\t%s = %s ", data.name, value.c_str()); | |
774 | if ( value == data.value ) | |
775 | { | |
776 | puts("(ok)"); | |
777 | } | |
778 | else | |
779 | { | |
780 | printf("(ERROR: should be %s)\n", data.value); | |
781 | } | |
782 | } | |
783 | ||
784 | // test enumerating the entries | |
785 | puts("\nEnumerating all root entries:"); | |
786 | long dummy; | |
787 | wxString name; | |
788 | bool cont = fileconf.GetFirstEntry(name, dummy); | |
789 | while ( cont ) | |
790 | { | |
791 | printf("\t%s = %s\n", | |
792 | name.c_str(), | |
793 | fileconf.Read(name.c_str(), _T("ERROR")).c_str()); | |
794 | ||
795 | cont = fileconf.GetNextEntry(name, dummy); | |
796 | } | |
797 | } | |
798 | ||
799 | #endif // TEST_FILECONF | |
800 | ||
844f90fb VZ |
801 | // ---------------------------------------------------------------------------- |
802 | // wxFileName | |
803 | // ---------------------------------------------------------------------------- | |
804 | ||
805 | #ifdef TEST_FILENAME | |
806 | ||
e84010cf | 807 | #include "wx/filename.h" |
844f90fb | 808 | |
81f25632 VZ |
809 | static void DumpFileName(const wxFileName& fn) |
810 | { | |
811 | wxString full = fn.GetFullPath(); | |
812 | ||
813 | wxString vol, path, name, ext; | |
814 | wxFileName::SplitPath(full, &vol, &path, &name, &ext); | |
815 | ||
a5b7374f | 816 | wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"), |
81f25632 | 817 | full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str()); |
a5b7374f VZ |
818 | |
819 | wxFileName::SplitPath(full, &path, &name, &ext); | |
820 | wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"), | |
821 | path.c_str(), name.c_str(), ext.c_str()); | |
822 | ||
823 | wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str()); | |
824 | wxPrintf(_T("with volume: \t'%s'\n"), | |
825 | fn.GetPath(wxPATH_GET_VOLUME).c_str()); | |
826 | wxPrintf(_T("with separator:\t'%s'\n"), | |
827 | fn.GetPath(wxPATH_GET_SEPARATOR).c_str()); | |
828 | wxPrintf(_T("with both: \t'%s'\n"), | |
829 | fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str()); | |
9cb47ea2 VZ |
830 | |
831 | wxPuts(_T("The directories in the path are:")); | |
832 | wxArrayString dirs = fn.GetDirs(); | |
833 | size_t count = dirs.GetCount(); | |
834 | for ( size_t n = 0; n < count; n++ ) | |
835 | { | |
836 | wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str()); | |
837 | } | |
81f25632 VZ |
838 | } |
839 | ||
8e7dda21 | 840 | static struct FileNameInfo |
42b1f941 | 841 | { |
8e7dda21 | 842 | const wxChar *fullname; |
a874db92 | 843 | const wxChar *volume; |
8e7dda21 VZ |
844 | const wxChar *path; |
845 | const wxChar *name; | |
846 | const wxChar *ext; | |
a874db92 VZ |
847 | bool isAbsolute; |
848 | wxPathFormat format; | |
8e7dda21 VZ |
849 | } filenames[] = |
850 | { | |
a874db92 VZ |
851 | // Unix file names |
852 | { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), TRUE, wxPATH_UNIX }, | |
853 | { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), TRUE, wxPATH_UNIX }, | |
854 | { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), TRUE, wxPATH_UNIX }, | |
855 | { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), FALSE, wxPATH_UNIX }, | |
856 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX }, | |
857 | { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), TRUE, wxPATH_UNIX }, | |
858 | { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), TRUE, wxPATH_UNIX }, | |
859 | { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX }, | |
860 | { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), TRUE, wxPATH_UNIX }, | |
861 | ||
862 | // Windows file names | |
863 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS }, | |
864 | { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), FALSE, wxPATH_DOS }, | |
865 | { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS }, | |
866 | { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS }, | |
867 | { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), TRUE, wxPATH_DOS }, | |
868 | { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS }, | |
6307da56 | 869 | { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS }, |
a874db92 | 870 | |
2db991f4 | 871 | // wxFileName support for Mac file names is broken currently |
a2fa5040 | 872 | #if 0 |
a874db92 VZ |
873 | // Mac file names |
874 | { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), TRUE, wxPATH_MAC }, | |
daa2c7d9 VZ |
875 | { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), TRUE, wxPATH_MAC }, |
876 | { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), TRUE, wxPATH_MAC }, | |
a874db92 | 877 | { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), FALSE, wxPATH_MAC }, |
daa2c7d9 VZ |
878 | { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC }, |
879 | { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC }, | |
a2fa5040 | 880 | #endif // 0 |
a874db92 VZ |
881 | |
882 | // VMS file names | |
883 | { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), TRUE, wxPATH_VMS }, | |
884 | { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), FALSE, wxPATH_VMS }, | |
42b1f941 VZ |
885 | }; |
886 | ||
844f90fb VZ |
887 | static void TestFileNameConstruction() |
888 | { | |
889 | puts("*** testing wxFileName construction ***"); | |
890 | ||
844f90fb VZ |
891 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) |
892 | { | |
a874db92 VZ |
893 | const FileNameInfo& fni = filenames[n]; |
894 | ||
895 | wxFileName fn(fni.fullname, fni.format); | |
896 | ||
897 | wxString fullname = fn.GetFullPath(fni.format); | |
898 | if ( fullname != fni.fullname ) | |
899 | { | |
900 | printf("ERROR: fullname should be '%s'\n", fni.fullname); | |
901 | } | |
844f90fb | 902 | |
a2fa5040 | 903 | bool isAbsolute = fn.IsAbsolute(fni.format); |
a874db92 VZ |
904 | printf("'%s' is %s (%s)\n\t", |
905 | fullname.c_str(), | |
906 | isAbsolute ? "absolute" : "relative", | |
907 | isAbsolute == fni.isAbsolute ? "ok" : "ERROR"); | |
908 | ||
909 | if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) ) | |
844f90fb VZ |
910 | { |
911 | puts("ERROR (couldn't be normalized)"); | |
912 | } | |
913 | else | |
914 | { | |
a874db92 | 915 | printf("normalized: '%s'\n", fn.GetFullPath(fni.format).c_str()); |
844f90fb VZ |
916 | } |
917 | } | |
918 | ||
919 | puts(""); | |
920 | } | |
921 | ||
42b1f941 VZ |
922 | static void TestFileNameSplit() |
923 | { | |
924 | puts("*** testing wxFileName splitting ***"); | |
925 | ||
926 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
927 | { | |
a874db92 VZ |
928 | const FileNameInfo& fni = filenames[n]; |
929 | wxString volume, path, name, ext; | |
930 | wxFileName::SplitPath(fni.fullname, | |
931 | &volume, &path, &name, &ext, fni.format); | |
932 | ||
933 | printf("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'", | |
934 | fni.fullname, | |
935 | volume.c_str(), path.c_str(), name.c_str(), ext.c_str()); | |
8e7dda21 | 936 | |
a874db92 VZ |
937 | if ( volume != fni.volume ) |
938 | printf(" (ERROR: volume = '%s')", fni.volume); | |
8e7dda21 VZ |
939 | if ( path != fni.path ) |
940 | printf(" (ERROR: path = '%s')", fni.path); | |
941 | if ( name != fni.name ) | |
942 | printf(" (ERROR: name = '%s')", fni.name); | |
943 | if ( ext != fni.ext ) | |
944 | printf(" (ERROR: ext = '%s')", fni.ext); | |
a874db92 | 945 | |
8e7dda21 | 946 | puts(""); |
42b1f941 | 947 | } |
42b1f941 VZ |
948 | } |
949 | ||
ade35f11 VZ |
950 | static void TestFileNameTemp() |
951 | { | |
952 | puts("*** testing wxFileName temp file creation ***"); | |
953 | ||
954 | static const char *tmpprefixes[] = | |
955 | { | |
a2fa5040 | 956 | "", |
ade35f11 | 957 | "foo", |
ade35f11 VZ |
958 | "..", |
959 | "../bar", | |
a2fa5040 VZ |
960 | #ifdef __UNIX__ |
961 | "/tmp/foo", | |
ade35f11 | 962 | "/tmp/foo/bar", // this one must be an error |
a2fa5040 | 963 | #endif // __UNIX__ |
ade35f11 VZ |
964 | }; |
965 | ||
966 | for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ ) | |
967 | { | |
968 | wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]); | |
2db991f4 VZ |
969 | if ( path.empty() ) |
970 | { | |
971 | // "error" is not in upper case because it may be ok | |
972 | printf("Prefix '%s'\t-> error\n", tmpprefixes[n]); | |
973 | } | |
974 | else | |
ade35f11 VZ |
975 | { |
976 | printf("Prefix '%s'\t-> temp file '%s'\n", | |
977 | tmpprefixes[n], path.c_str()); | |
978 | ||
979 | if ( !wxRemoveFile(path) ) | |
980 | { | |
981 | wxLogWarning("Failed to remove temp file '%s'", path.c_str()); | |
982 | } | |
983 | } | |
984 | } | |
985 | } | |
986 | ||
f7d886af VZ |
987 | static void TestFileNameMakeRelative() |
988 | { | |
989 | puts("*** testing wxFileName::MakeRelativeTo() ***"); | |
990 | ||
991 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
992 | { | |
993 | const FileNameInfo& fni = filenames[n]; | |
994 | ||
995 | wxFileName fn(fni.fullname, fni.format); | |
996 | ||
997 | // choose the base dir of the same format | |
998 | wxString base; | |
999 | switch ( fni.format ) | |
1000 | { | |
1001 | case wxPATH_UNIX: | |
1002 | base = "/usr/bin/"; | |
1003 | break; | |
1004 | ||
1005 | case wxPATH_DOS: | |
1006 | base = "c:\\"; | |
1007 | break; | |
1008 | ||
1009 | case wxPATH_MAC: | |
1010 | case wxPATH_VMS: | |
1011 | // TODO: I don't know how this is supposed to work there | |
1012 | continue; | |
daa2c7d9 VZ |
1013 | |
1014 | case wxPATH_NATIVE: // make gcc happy | |
1015 | default: | |
1016 | wxFAIL_MSG( "unexpected path format" ); | |
f7d886af VZ |
1017 | } |
1018 | ||
1019 | printf("'%s' relative to '%s': ", | |
1020 | fn.GetFullPath(fni.format).c_str(), base.c_str()); | |
1021 | ||
1022 | if ( !fn.MakeRelativeTo(base, fni.format) ) | |
1023 | { | |
1024 | puts("unchanged"); | |
1025 | } | |
1026 | else | |
1027 | { | |
1028 | printf("'%s'\n", fn.GetFullPath(fni.format).c_str()); | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | ||
844f90fb VZ |
1033 | static void TestFileNameComparison() |
1034 | { | |
1035 | // TODO! | |
1036 | } | |
1037 | ||
1038 | static void TestFileNameOperations() | |
1039 | { | |
1040 | // TODO! | |
1041 | } | |
1042 | ||
1043 | static void TestFileNameCwd() | |
1044 | { | |
1045 | // TODO! | |
1046 | } | |
1047 | ||
1048 | #endif // TEST_FILENAME | |
1049 | ||
d56e2b97 VZ |
1050 | // ---------------------------------------------------------------------------- |
1051 | // wxFileName time functions | |
1052 | // ---------------------------------------------------------------------------- | |
1053 | ||
1054 | #ifdef TEST_FILETIME | |
1055 | ||
1056 | #include <wx/filename.h> | |
1057 | #include <wx/datetime.h> | |
1058 | ||
1059 | static void TestFileGetTimes() | |
1060 | { | |
1061 | wxFileName fn(_T("testdata.fc")); | |
1062 | ||
6dbb903b VZ |
1063 | wxDateTime dtAccess, dtMod, dtCreate; |
1064 | if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) ) | |
d56e2b97 VZ |
1065 | { |
1066 | wxPrintf(_T("ERROR: GetTimes() failed.\n")); | |
1067 | } | |
1068 | else | |
1069 | { | |
1070 | static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S"); | |
1071 | ||
1072 | wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str()); | |
6dbb903b VZ |
1073 | wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str()); |
1074 | wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str()); | |
1075 | wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str()); | |
d56e2b97 VZ |
1076 | } |
1077 | } | |
1078 | ||
1079 | static void TestFileSetTimes() | |
1080 | { | |
1081 | wxFileName fn(_T("testdata.fc")); | |
1082 | ||
d56e2b97 VZ |
1083 | if ( !fn.Touch() ) |
1084 | { | |
1085 | wxPrintf(_T("ERROR: Touch() failed.\n")); | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | #endif // TEST_FILETIME | |
1090 | ||
2c8e4738 VZ |
1091 | // ---------------------------------------------------------------------------- |
1092 | // wxHashTable | |
1093 | // ---------------------------------------------------------------------------- | |
1094 | ||
1095 | #ifdef TEST_HASH | |
1096 | ||
e84010cf | 1097 | #include "wx/hash.h" |
2c8e4738 VZ |
1098 | |
1099 | struct Foo | |
1100 | { | |
1101 | Foo(int n_) { n = n_; count++; } | |
1102 | ~Foo() { count--; } | |
1103 | ||
1104 | int n; | |
1105 | ||
1106 | static size_t count; | |
1107 | }; | |
1108 | ||
1109 | size_t Foo::count = 0; | |
1110 | ||
1111 | WX_DECLARE_LIST(Foo, wxListFoos); | |
1112 | WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos); | |
1113 | ||
e84010cf | 1114 | #include "wx/listimpl.cpp" |
2c8e4738 VZ |
1115 | |
1116 | WX_DEFINE_LIST(wxListFoos); | |
1117 | ||
1118 | static void TestHash() | |
1119 | { | |
1120 | puts("*** Testing wxHashTable ***\n"); | |
1121 | ||
1122 | { | |
1123 | wxHashFoos hash; | |
1124 | hash.DeleteContents(TRUE); | |
1125 | ||
1126 | printf("Hash created: %u foos in hash, %u foos totally\n", | |
1127 | hash.GetCount(), Foo::count); | |
1128 | ||
1129 | static const int hashTestData[] = | |
1130 | { | |
1131 | 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1, | |
1132 | }; | |
1133 | ||
1134 | size_t n; | |
1135 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) | |
1136 | { | |
1137 | hash.Put(hashTestData[n], n, new Foo(n)); | |
1138 | } | |
1139 | ||
1140 | printf("Hash filled: %u foos in hash, %u foos totally\n", | |
1141 | hash.GetCount(), Foo::count); | |
1142 | ||
1143 | puts("Hash access test:"); | |
1144 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) | |
1145 | { | |
1146 | printf("\tGetting element with key %d, value %d: ", | |
1147 | hashTestData[n], n); | |
1148 | Foo *foo = hash.Get(hashTestData[n], n); | |
1149 | if ( !foo ) | |
1150 | { | |
1151 | printf("ERROR, not found.\n"); | |
1152 | } | |
1153 | else | |
1154 | { | |
1155 | printf("%d (%s)\n", foo->n, | |
1156 | (size_t)foo->n == n ? "ok" : "ERROR"); | |
1157 | } | |
1158 | } | |
1159 | ||
1160 | printf("\nTrying to get an element not in hash: "); | |
1161 | ||
1162 | if ( hash.Get(1234) || hash.Get(1, 0) ) | |
1163 | { | |
1164 | puts("ERROR: found!"); | |
1165 | } | |
1166 | else | |
1167 | { | |
1168 | puts("ok (not found)"); | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | printf("Hash destroyed: %u foos left\n", Foo::count); | |
1173 | } | |
1174 | ||
1175 | #endif // TEST_HASH | |
1176 | ||
0508ba2a MB |
1177 | // ---------------------------------------------------------------------------- |
1178 | // wxHashMap | |
1179 | // ---------------------------------------------------------------------------- | |
1180 | ||
1181 | #ifdef TEST_HASHMAP | |
1182 | ||
1183 | #include "wx/hashmap.h" | |
1184 | ||
1185 | // test compilation of basic map types | |
1186 | WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash, wxPointerEqual, myPtrHashMap ); | |
1187 | WX_DECLARE_HASH_MAP( long, long, wxIntegerHash, wxIntegerEqual, myLongHashMap ); | |
1188 | WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash, wxIntegerEqual, | |
1189 | myUnsignedHashMap ); | |
1190 | WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash, wxIntegerEqual, | |
1191 | myTestHashMap1 ); | |
1192 | WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash, wxIntegerEqual, | |
1193 | myTestHashMap2 ); | |
1194 | WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash, wxIntegerEqual, | |
1195 | myTestHashMap3 ); | |
1196 | WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash, wxIntegerEqual, | |
1197 | myTestHashMap4 ); | |
60ce696e VZ |
1198 | |
1199 | // same as: | |
1200 | // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual, | |
1201 | // myStringHashMap ); | |
1202 | WX_DECLARE_STRING_HASH_MAP(wxString, myStringHashMap); | |
0508ba2a MB |
1203 | |
1204 | typedef myStringHashMap::iterator Itor; | |
1205 | ||
1206 | static void TestHashMap() | |
1207 | { | |
1208 | puts("*** Testing wxHashMap ***\n"); | |
1209 | myStringHashMap sh(0); // as small as possible | |
1210 | wxString buf; | |
1211 | size_t i; | |
1212 | const size_t count = 10000; | |
1213 | ||
1214 | // init with some data | |
1215 | for( i = 0; i < count; ++i ) | |
1216 | { | |
1217 | buf.Printf(wxT("%d"), i ); | |
1218 | sh[buf] = wxT("A") + buf + wxT("C"); | |
1219 | } | |
1220 | ||
1221 | // test that insertion worked | |
1222 | if( sh.size() != count ) | |
1223 | { | |
1224 | printf("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n", sh.size(), count); | |
1225 | } | |
1226 | ||
1227 | for( i = 0; i < count; ++i ) | |
1228 | { | |
1229 | buf.Printf(wxT("%d"), i ); | |
1230 | if( sh[buf] != wxT("A") + buf + wxT("C") ) | |
1231 | { | |
1232 | printf("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n"); | |
1233 | return; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | // check that iterators work | |
1238 | Itor it; | |
1239 | for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i ) | |
1240 | { | |
1241 | if( i == count ) | |
1242 | { | |
1243 | printf("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n"); | |
1244 | return; | |
1245 | } | |
1246 | ||
1247 | if( it->second != sh[it->first] ) | |
1248 | { | |
1249 | printf("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n"); | |
1250 | return; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | if( sh.size() != i ) | |
1255 | { | |
1256 | printf("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n", i, count); | |
1257 | } | |
1258 | ||
1259 | // test copy ctor, assignment operator | |
1260 | myStringHashMap h1( sh ), h2( 0 ); | |
1261 | h2 = sh; | |
1262 | ||
1263 | for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i ) | |
1264 | { | |
1265 | if( h1[it->first] != it->second ) | |
1266 | { | |
1267 | printf("*** ERROR: COPY CTOR BROKEN %s ***\n", it->first.c_str()); | |
1268 | } | |
1269 | ||
1270 | if( h2[it->first] != it->second ) | |
1271 | { | |
1272 | printf("*** ERROR: OPERATOR= BROKEN %s ***\n", it->first.c_str()); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | // other tests | |
1277 | for( i = 0; i < count; ++i ) | |
1278 | { | |
1279 | buf.Printf(wxT("%d"), i ); | |
1280 | size_t sz = sh.size(); | |
1281 | ||
1282 | // test find() and erase(it) | |
1283 | if( i < 100 ) | |
1284 | { | |
1285 | it = sh.find( buf ); | |
1286 | if( it != sh.end() ) | |
1287 | { | |
1288 | sh.erase( it ); | |
1289 | ||
1290 | if( sh.find( buf ) != sh.end() ) | |
1291 | { | |
1292 | printf("*** ERROR: FOUND DELETED ELEMENT %u ***\n", i); | |
1293 | } | |
1294 | } | |
1295 | else | |
1296 | printf("*** ERROR: CANT FIND ELEMENT %u ***\n", i); | |
1297 | } | |
1298 | else | |
1299 | // test erase(key) | |
1300 | { | |
1301 | size_t c = sh.erase( buf ); | |
1302 | if( c != 1 ) | |
1303 | printf("*** ERROR: SHOULD RETURN 1 ***\n"); | |
1304 | ||
1305 | if( sh.find( buf ) != sh.end() ) | |
1306 | { | |
1307 | printf("*** ERROR: FOUND DELETED ELEMENT %u ***\n", i); | |
1308 | } | |
1309 | } | |
1310 | ||
1311 | // count should decrease | |
1312 | if( sh.size() != sz - 1 ) | |
1313 | { | |
1314 | printf("*** ERROR: COUNT DID NOT DECREASE ***\n"); | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | printf("*** Finished testing wxHashMap ***\n"); | |
1319 | } | |
1320 | ||
60ce696e | 1321 | #endif // TEST_HASHMAP |
0508ba2a | 1322 | |
f6bcfd97 BP |
1323 | // ---------------------------------------------------------------------------- |
1324 | // wxList | |
1325 | // ---------------------------------------------------------------------------- | |
1326 | ||
1327 | #ifdef TEST_LIST | |
1328 | ||
e84010cf | 1329 | #include "wx/list.h" |
f6bcfd97 BP |
1330 | |
1331 | WX_DECLARE_LIST(Bar, wxListBars); | |
e84010cf | 1332 | #include "wx/listimpl.cpp" |
f6bcfd97 BP |
1333 | WX_DEFINE_LIST(wxListBars); |
1334 | ||
1335 | static void TestListCtor() | |
1336 | { | |
1337 | puts("*** Testing wxList construction ***\n"); | |
1338 | ||
1339 | { | |
1340 | wxListBars list1; | |
1341 | list1.Append(new Bar(_T("first"))); | |
1342 | list1.Append(new Bar(_T("second"))); | |
1343 | ||
1344 | printf("After 1st list creation: %u objects in the list, %u objects total.\n", | |
1345 | list1.GetCount(), Bar::GetNumber()); | |
1346 | ||
1347 | wxListBars list2; | |
1348 | list2 = list1; | |
1349 | ||
1350 | printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n", | |
1351 | list1.GetCount(), list2.GetCount(), Bar::GetNumber()); | |
1352 | ||
1353 | list1.DeleteContents(TRUE); | |
1354 | } | |
1355 | ||
1356 | printf("After list destruction: %u objects left.\n", Bar::GetNumber()); | |
1357 | } | |
1358 | ||
1359 | #endif // TEST_LIST | |
1360 | ||
ec37df57 VZ |
1361 | // ---------------------------------------------------------------------------- |
1362 | // wxLocale | |
1363 | // ---------------------------------------------------------------------------- | |
1364 | ||
1365 | #ifdef TEST_LOCALE | |
1366 | ||
1367 | #include "wx/intl.h" | |
1368 | #include "wx/utils.h" // for wxSetEnv | |
1369 | ||
1370 | static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH); | |
1371 | ||
1372 | // find the name of the language from its value | |
1373 | static const char *GetLangName(int lang) | |
1374 | { | |
1375 | static const char *languageNames[] = | |
1376 | { | |
daa2c7d9 VZ |
1377 | "DEFAULT", |
1378 | "UNKNOWN", | |
ec37df57 VZ |
1379 | "ABKHAZIAN", |
1380 | "AFAR", | |
1381 | "AFRIKAANS", | |
1382 | "ALBANIAN", | |
1383 | "AMHARIC", | |
1384 | "ARABIC", | |
1385 | "ARABIC_ALGERIA", | |
1386 | "ARABIC_BAHRAIN", | |
1387 | "ARABIC_EGYPT", | |
1388 | "ARABIC_IRAQ", | |
1389 | "ARABIC_JORDAN", | |
1390 | "ARABIC_KUWAIT", | |
1391 | "ARABIC_LEBANON", | |
1392 | "ARABIC_LIBYA", | |
1393 | "ARABIC_MOROCCO", | |
1394 | "ARABIC_OMAN", | |
1395 | "ARABIC_QATAR", | |
1396 | "ARABIC_SAUDI_ARABIA", | |
1397 | "ARABIC_SUDAN", | |
1398 | "ARABIC_SYRIA", | |
1399 | "ARABIC_TUNISIA", | |
1400 | "ARABIC_UAE", | |
1401 | "ARABIC_YEMEN", | |
1402 | "ARMENIAN", | |
1403 | "ASSAMESE", | |
1404 | "AYMARA", | |
1405 | "AZERI", | |
1406 | "AZERI_CYRILLIC", | |
1407 | "AZERI_LATIN", | |
1408 | "BASHKIR", | |
1409 | "BASQUE", | |
1410 | "BELARUSIAN", | |
1411 | "BENGALI", | |
1412 | "BHUTANI", | |
1413 | "BIHARI", | |
1414 | "BISLAMA", | |
1415 | "BRETON", | |
1416 | "BULGARIAN", | |
1417 | "BURMESE", | |
1418 | "CAMBODIAN", | |
1419 | "CATALAN", | |
1420 | "CHINESE", | |
1421 | "CHINESE_SIMPLIFIED", | |
1422 | "CHINESE_TRADITIONAL", | |
1423 | "CHINESE_HONGKONG", | |
1424 | "CHINESE_MACAU", | |
1425 | "CHINESE_SINGAPORE", | |
1426 | "CHINESE_TAIWAN", | |
1427 | "CORSICAN", | |
1428 | "CROATIAN", | |
1429 | "CZECH", | |
1430 | "DANISH", | |
1431 | "DUTCH", | |
1432 | "DUTCH_BELGIAN", | |
1433 | "ENGLISH", | |
1434 | "ENGLISH_UK", | |
1435 | "ENGLISH_US", | |
1436 | "ENGLISH_AUSTRALIA", | |
1437 | "ENGLISH_BELIZE", | |
1438 | "ENGLISH_BOTSWANA", | |
1439 | "ENGLISH_CANADA", | |
1440 | "ENGLISH_CARIBBEAN", | |
1441 | "ENGLISH_DENMARK", | |
1442 | "ENGLISH_EIRE", | |
1443 | "ENGLISH_JAMAICA", | |
1444 | "ENGLISH_NEW_ZEALAND", | |
1445 | "ENGLISH_PHILIPPINES", | |
1446 | "ENGLISH_SOUTH_AFRICA", | |
1447 | "ENGLISH_TRINIDAD", | |
1448 | "ENGLISH_ZIMBABWE", | |
1449 | "ESPERANTO", | |
1450 | "ESTONIAN", | |
1451 | "FAEROESE", | |
1452 | "FARSI", | |
1453 | "FIJI", | |
1454 | "FINNISH", | |
1455 | "FRENCH", | |
1456 | "FRENCH_BELGIAN", | |
1457 | "FRENCH_CANADIAN", | |
1458 | "FRENCH_LUXEMBOURG", | |
1459 | "FRENCH_MONACO", | |
1460 | "FRENCH_SWISS", | |
1461 | "FRISIAN", | |
1462 | "GALICIAN", | |
1463 | "GEORGIAN", | |
1464 | "GERMAN", | |
1465 | "GERMAN_AUSTRIAN", | |
1466 | "GERMAN_BELGIUM", | |
1467 | "GERMAN_LIECHTENSTEIN", | |
1468 | "GERMAN_LUXEMBOURG", | |
1469 | "GERMAN_SWISS", | |
1470 | "GREEK", | |
1471 | "GREENLANDIC", | |
1472 | "GUARANI", | |
1473 | "GUJARATI", | |
1474 | "HAUSA", | |
1475 | "HEBREW", | |
1476 | "HINDI", | |
1477 | "HUNGARIAN", | |
1478 | "ICELANDIC", | |
1479 | "INDONESIAN", | |
1480 | "INTERLINGUA", | |
1481 | "INTERLINGUE", | |
1482 | "INUKTITUT", | |
1483 | "INUPIAK", | |
1484 | "IRISH", | |
1485 | "ITALIAN", | |
1486 | "ITALIAN_SWISS", | |
1487 | "JAPANESE", | |
1488 | "JAVANESE", | |
1489 | "KANNADA", | |
1490 | "KASHMIRI", | |
1491 | "KASHMIRI_INDIA", | |
1492 | "KAZAKH", | |
1493 | "KERNEWEK", | |
1494 | "KINYARWANDA", | |
1495 | "KIRGHIZ", | |
1496 | "KIRUNDI", | |
1497 | "KONKANI", | |
1498 | "KOREAN", | |
1499 | "KURDISH", | |
1500 | "LAOTHIAN", | |
1501 | "LATIN", | |
1502 | "LATVIAN", | |
1503 | "LINGALA", | |
1504 | "LITHUANIAN", | |
1505 | "MACEDONIAN", | |
1506 | "MALAGASY", | |
1507 | "MALAY", | |
1508 | "MALAYALAM", | |
1509 | "MALAY_BRUNEI_DARUSSALAM", | |
1510 | "MALAY_MALAYSIA", | |
1511 | "MALTESE", | |
1512 | "MANIPURI", | |
1513 | "MAORI", | |
1514 | "MARATHI", | |
1515 | "MOLDAVIAN", | |
1516 | "MONGOLIAN", | |
1517 | "NAURU", | |
1518 | "NEPALI", | |
1519 | "NEPALI_INDIA", | |
1520 | "NORWEGIAN_BOKMAL", | |
1521 | "NORWEGIAN_NYNORSK", | |
1522 | "OCCITAN", | |
1523 | "ORIYA", | |
1524 | "OROMO", | |
1525 | "PASHTO", | |
1526 | "POLISH", | |
1527 | "PORTUGUESE", | |
1528 | "PORTUGUESE_BRAZILIAN", | |
1529 | "PUNJABI", | |
1530 | "QUECHUA", | |
1531 | "RHAETO_ROMANCE", | |
1532 | "ROMANIAN", | |
1533 | "RUSSIAN", | |
1534 | "RUSSIAN_UKRAINE", | |
1535 | "SAMOAN", | |
1536 | "SANGHO", | |
1537 | "SANSKRIT", | |
1538 | "SCOTS_GAELIC", | |
1539 | "SERBIAN", | |
1540 | "SERBIAN_CYRILLIC", | |
1541 | "SERBIAN_LATIN", | |
1542 | "SERBO_CROATIAN", | |
1543 | "SESOTHO", | |
1544 | "SETSWANA", | |
1545 | "SHONA", | |
1546 | "SINDHI", | |
1547 | "SINHALESE", | |
1548 | "SISWATI", | |
1549 | "SLOVAK", | |
1550 | "SLOVENIAN", | |
1551 | "SOMALI", | |
1552 | "SPANISH", | |
1553 | "SPANISH_ARGENTINA", | |
1554 | "SPANISH_BOLIVIA", | |
1555 | "SPANISH_CHILE", | |
1556 | "SPANISH_COLOMBIA", | |
1557 | "SPANISH_COSTA_RICA", | |
1558 | "SPANISH_DOMINICAN_REPUBLIC", | |
1559 | "SPANISH_ECUADOR", | |
1560 | "SPANISH_EL_SALVADOR", | |
1561 | "SPANISH_GUATEMALA", | |
1562 | "SPANISH_HONDURAS", | |
1563 | "SPANISH_MEXICAN", | |
1564 | "SPANISH_MODERN", | |
1565 | "SPANISH_NICARAGUA", | |
1566 | "SPANISH_PANAMA", | |
1567 | "SPANISH_PARAGUAY", | |
1568 | "SPANISH_PERU", | |
1569 | "SPANISH_PUERTO_RICO", | |
1570 | "SPANISH_URUGUAY", | |
1571 | "SPANISH_US", | |
1572 | "SPANISH_VENEZUELA", | |
1573 | "SUNDANESE", | |
1574 | "SWAHILI", | |
1575 | "SWEDISH", | |
1576 | "SWEDISH_FINLAND", | |
1577 | "TAGALOG", | |
1578 | "TAJIK", | |
1579 | "TAMIL", | |
1580 | "TATAR", | |
1581 | "TELUGU", | |
1582 | "THAI", | |
1583 | "TIBETAN", | |
1584 | "TIGRINYA", | |
1585 | "TONGA", | |
1586 | "TSONGA", | |
1587 | "TURKISH", | |
1588 | "TURKMEN", | |
1589 | "TWI", | |
1590 | "UIGHUR", | |
1591 | "UKRAINIAN", | |
1592 | "URDU", | |
1593 | "URDU_INDIA", | |
1594 | "URDU_PAKISTAN", | |
1595 | "UZBEK", | |
1596 | "UZBEK_CYRILLIC", | |
1597 | "UZBEK_LATIN", | |
1598 | "VIETNAMESE", | |
1599 | "VOLAPUK", | |
1600 | "WELSH", | |
1601 | "WOLOF", | |
1602 | "XHOSA", | |
1603 | "YIDDISH", | |
1604 | "YORUBA", | |
1605 | "ZHUANG", | |
1606 | "ZULU", | |
1607 | }; | |
1608 | ||
1609 | if ( (size_t)lang < WXSIZEOF(languageNames) ) | |
1610 | return languageNames[lang]; | |
1611 | else | |
1612 | return "INVALID"; | |
1613 | } | |
1614 | ||
1615 | static void TestDefaultLang() | |
1616 | { | |
1617 | puts("*** Testing wxLocale::GetSystemLanguage ***"); | |
1618 | ||
1619 | static const wxChar *langStrings[] = | |
1620 | { | |
1621 | NULL, // system default | |
1622 | _T("C"), | |
1623 | _T("fr"), | |
1624 | _T("fr_FR"), | |
1625 | _T("en"), | |
1626 | _T("en_GB"), | |
1627 | _T("en_US"), | |
1628 | _T("de_DE.iso88591"), | |
1629 | _T("german"), | |
1630 | _T("?"), // invalid lang spec | |
1631 | _T("klingonese"), // I bet on some systems it does exist... | |
1632 | }; | |
1633 | ||
dccce9ea VZ |
1634 | wxPrintf(_T("The default system encoding is %s (%d)\n"), |
1635 | wxLocale::GetSystemEncodingName().c_str(), | |
1636 | wxLocale::GetSystemEncoding()); | |
1637 | ||
ec37df57 VZ |
1638 | for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ ) |
1639 | { | |
1640 | const char *langStr = langStrings[n]; | |
1641 | if ( langStr ) | |
dccce9ea VZ |
1642 | { |
1643 | // FIXME: this doesn't do anything at all under Windows, we need | |
1644 | // to create a new wxLocale! | |
ec37df57 | 1645 | wxSetEnv(_T("LC_ALL"), langStr); |
dccce9ea | 1646 | } |
ec37df57 VZ |
1647 | |
1648 | int lang = gs_localeDefault.GetSystemLanguage(); | |
1649 | printf("Locale for '%s' is %s.\n", | |
1650 | langStr ? langStr : "system default", GetLangName(lang)); | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | #endif // TEST_LOCALE | |
1655 | ||
696e1ea0 VZ |
1656 | // ---------------------------------------------------------------------------- |
1657 | // MIME types | |
1658 | // ---------------------------------------------------------------------------- | |
1659 | ||
1660 | #ifdef TEST_MIME | |
1661 | ||
e84010cf | 1662 | #include "wx/mimetype.h" |
696e1ea0 VZ |
1663 | |
1664 | static void TestMimeEnum() | |
1665 | { | |
a6c65e88 VZ |
1666 | wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n")); |
1667 | ||
696e1ea0 VZ |
1668 | wxArrayString mimetypes; |
1669 | ||
39189b9d | 1670 | size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes); |
696e1ea0 VZ |
1671 | |
1672 | printf("*** All %u known filetypes: ***\n", count); | |
1673 | ||
1674 | wxArrayString exts; | |
1675 | wxString desc; | |
1676 | ||
1677 | for ( size_t n = 0; n < count; n++ ) | |
1678 | { | |
39189b9d VZ |
1679 | wxFileType *filetype = |
1680 | wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]); | |
696e1ea0 | 1681 | if ( !filetype ) |
c61f4f6d | 1682 | { |
97e0ceea VZ |
1683 | printf("nothing known about the filetype '%s'!\n", |
1684 | mimetypes[n].c_str()); | |
696e1ea0 | 1685 | continue; |
c61f4f6d VZ |
1686 | } |
1687 | ||
696e1ea0 VZ |
1688 | filetype->GetDescription(&desc); |
1689 | filetype->GetExtensions(exts); | |
1690 | ||
299fcbfe VZ |
1691 | filetype->GetIcon(NULL); |
1692 | ||
696e1ea0 VZ |
1693 | wxString extsAll; |
1694 | for ( size_t e = 0; e < exts.GetCount(); e++ ) | |
1695 | { | |
1696 | if ( e > 0 ) | |
1697 | extsAll << _T(", "); | |
1698 | extsAll += exts[e]; | |
1699 | } | |
1700 | ||
54acce90 VZ |
1701 | printf("\t%s: %s (%s)\n", |
1702 | mimetypes[n].c_str(), desc.c_str(), extsAll.c_str()); | |
696e1ea0 | 1703 | } |
39189b9d VZ |
1704 | |
1705 | puts(""); | |
696e1ea0 VZ |
1706 | } |
1707 | ||
f6bcfd97 BP |
1708 | static void TestMimeOverride() |
1709 | { | |
1710 | wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n")); | |
1711 | ||
39189b9d VZ |
1712 | static const wxChar *mailcap = _T("/tmp/mailcap"); |
1713 | static const wxChar *mimetypes = _T("/tmp/mime.types"); | |
1714 | ||
1715 | if ( wxFile::Exists(mailcap) ) | |
1716 | wxPrintf(_T("Loading mailcap from '%s': %s\n"), | |
1717 | mailcap, | |
1718 | wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR")); | |
1719 | else | |
1720 | wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"), | |
1721 | mailcap); | |
f6bcfd97 | 1722 | |
39189b9d VZ |
1723 | if ( wxFile::Exists(mimetypes) ) |
1724 | wxPrintf(_T("Loading mime.types from '%s': %s\n"), | |
1725 | mimetypes, | |
1726 | wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR")); | |
1727 | else | |
1728 | wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"), | |
1729 | mimetypes); | |
1730 | ||
1731 | puts(""); | |
f6bcfd97 BP |
1732 | } |
1733 | ||
1734 | static void TestMimeFilename() | |
1735 | { | |
1736 | wxPuts(_T("*** Testing MIME type from filename query ***\n")); | |
1737 | ||
1738 | static const wxChar *filenames[] = | |
1739 | { | |
1740 | _T("readme.txt"), | |
1741 | _T("document.pdf"), | |
1742 | _T("image.gif"), | |
f06ef5f4 | 1743 | _T("picture.jpeg"), |
f6bcfd97 BP |
1744 | }; |
1745 | ||
1746 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
1747 | { | |
1748 | const wxString fname = filenames[n]; | |
1749 | wxString ext = fname.AfterLast(_T('.')); | |
39189b9d | 1750 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); |
f6bcfd97 BP |
1751 | if ( !ft ) |
1752 | { | |
1753 | wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str()); | |
1754 | } | |
1755 | else | |
1756 | { | |
1757 | wxString desc; | |
1758 | if ( !ft->GetDescription(&desc) ) | |
1759 | desc = _T("<no description>"); | |
1760 | ||
1761 | wxString cmd; | |
1762 | if ( !ft->GetOpenCommand(&cmd, | |
1763 | wxFileType::MessageParameters(fname, _T(""))) ) | |
1764 | cmd = _T("<no command available>"); | |
1765 | ||
1766 | wxPrintf(_T("To open %s (%s) do '%s'.\n"), | |
1767 | fname.c_str(), desc.c_str(), cmd.c_str()); | |
1768 | ||
1769 | delete ft; | |
1770 | } | |
1771 | } | |
39189b9d VZ |
1772 | |
1773 | puts(""); | |
f6bcfd97 BP |
1774 | } |
1775 | ||
c7ce8392 VZ |
1776 | static void TestMimeAssociate() |
1777 | { | |
1778 | wxPuts(_T("*** Testing creation of filetype association ***\n")); | |
1779 | ||
a6c65e88 VZ |
1780 | wxFileTypeInfo ftInfo( |
1781 | _T("application/x-xyz"), | |
1782 | _T("xyzview '%s'"), // open cmd | |
1783 | _T(""), // print cmd | |
df0dc216 VZ |
1784 | _T("XYZ File"), // description |
1785 | _T(".xyz"), // extensions | |
1786 | NULL // end of extensions | |
a6c65e88 VZ |
1787 | ); |
1788 | ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only | |
1789 | ||
39189b9d | 1790 | wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo); |
c7ce8392 VZ |
1791 | if ( !ft ) |
1792 | { | |
1793 | wxPuts(_T("ERROR: failed to create association!")); | |
1794 | } | |
1795 | else | |
1796 | { | |
a6c65e88 | 1797 | // TODO: read it back |
c7ce8392 VZ |
1798 | delete ft; |
1799 | } | |
39189b9d VZ |
1800 | |
1801 | puts(""); | |
c7ce8392 VZ |
1802 | } |
1803 | ||
696e1ea0 VZ |
1804 | #endif // TEST_MIME |
1805 | ||
89e60357 VZ |
1806 | // ---------------------------------------------------------------------------- |
1807 | // misc information functions | |
1808 | // ---------------------------------------------------------------------------- | |
1809 | ||
1810 | #ifdef TEST_INFO_FUNCTIONS | |
1811 | ||
e84010cf | 1812 | #include "wx/utils.h" |
89e60357 | 1813 | |
3a994742 VZ |
1814 | static void TestDiskInfo() |
1815 | { | |
eadd7bd2 | 1816 | puts("*** Testing wxGetDiskSpace() ***"); |
3a994742 VZ |
1817 | |
1818 | for ( ;; ) | |
1819 | { | |
1820 | char pathname[128]; | |
1821 | printf("\nEnter a directory name: "); | |
1822 | if ( !fgets(pathname, WXSIZEOF(pathname), stdin) ) | |
1823 | break; | |
1824 | ||
1825 | // kill the last '\n' | |
1826 | pathname[strlen(pathname) - 1] = 0; | |
1827 | ||
1828 | wxLongLong total, free; | |
1829 | if ( !wxGetDiskSpace(pathname, &total, &free) ) | |
1830 | { | |
1831 | wxPuts(_T("ERROR: wxGetDiskSpace failed.")); | |
1832 | } | |
1833 | else | |
1834 | { | |
eadd7bd2 VZ |
1835 | wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"), |
1836 | (total / 1024).ToString().c_str(), | |
1837 | (free / 1024).ToString().c_str(), | |
3a994742 VZ |
1838 | pathname); |
1839 | } | |
1840 | } | |
1841 | } | |
1842 | ||
89e60357 VZ |
1843 | static void TestOsInfo() |
1844 | { | |
1845 | puts("*** Testing OS info functions ***\n"); | |
1846 | ||
1847 | int major, minor; | |
1848 | wxGetOsVersion(&major, &minor); | |
1849 | printf("Running under: %s, version %d.%d\n", | |
1850 | wxGetOsDescription().c_str(), major, minor); | |
1851 | ||
bd3277fe | 1852 | printf("%ld free bytes of memory left.\n", wxGetFreeMemory()); |
89e60357 VZ |
1853 | |
1854 | printf("Host name is %s (%s).\n", | |
1855 | wxGetHostName().c_str(), wxGetFullHostName().c_str()); | |
bd3277fe VZ |
1856 | |
1857 | puts(""); | |
89e60357 VZ |
1858 | } |
1859 | ||
1860 | static void TestUserInfo() | |
1861 | { | |
1862 | puts("*** Testing user info functions ***\n"); | |
1863 | ||
1864 | printf("User id is:\t%s\n", wxGetUserId().c_str()); | |
1865 | printf("User name is:\t%s\n", wxGetUserName().c_str()); | |
1866 | printf("Home dir is:\t%s\n", wxGetHomeDir().c_str()); | |
1867 | printf("Email address:\t%s\n", wxGetEmailAddress().c_str()); | |
bd3277fe VZ |
1868 | |
1869 | puts(""); | |
89e60357 VZ |
1870 | } |
1871 | ||
1872 | #endif // TEST_INFO_FUNCTIONS | |
1873 | ||
b76b015e VZ |
1874 | // ---------------------------------------------------------------------------- |
1875 | // long long | |
1876 | // ---------------------------------------------------------------------------- | |
1877 | ||
1878 | #ifdef TEST_LONGLONG | |
1879 | ||
e84010cf GD |
1880 | #include "wx/longlong.h" |
1881 | #include "wx/timer.h" | |
b76b015e | 1882 | |
2a310492 VZ |
1883 | // make a 64 bit number from 4 16 bit ones |
1884 | #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3) | |
1885 | ||
1886 | // get a random 64 bit number | |
1887 | #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand()) | |
1888 | ||
3a994742 VZ |
1889 | static const long testLongs[] = |
1890 | { | |
1891 | 0, | |
1892 | 1, | |
1893 | -1, | |
1894 | LONG_MAX, | |
1895 | LONG_MIN, | |
1896 | 0x1234, | |
1897 | -0x1234 | |
1898 | }; | |
1899 | ||
7d0bb74d | 1900 | #if wxUSE_LONGLONG_WX |
2a310492 VZ |
1901 | inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b) |
1902 | { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); } | |
1903 | inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b) | |
1904 | { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); } | |
7d0bb74d | 1905 | #endif // wxUSE_LONGLONG_WX |
2a310492 | 1906 | |
b76b015e VZ |
1907 | static void TestSpeed() |
1908 | { | |
1909 | static const long max = 100000000; | |
1910 | long n; | |
9fc3ad34 | 1911 | |
b76b015e VZ |
1912 | { |
1913 | wxStopWatch sw; | |
1914 | ||
1915 | long l = 0; | |
1916 | for ( n = 0; n < max; n++ ) | |
1917 | { | |
1918 | l += n; | |
1919 | } | |
1920 | ||
1921 | printf("Summing longs took %ld milliseconds.\n", sw.Time()); | |
1922 | } | |
1923 | ||
2ea24d9f | 1924 | #if wxUSE_LONGLONG_NATIVE |
b76b015e VZ |
1925 | { |
1926 | wxStopWatch sw; | |
1927 | ||
2ea24d9f | 1928 | wxLongLong_t l = 0; |
b76b015e VZ |
1929 | for ( n = 0; n < max; n++ ) |
1930 | { | |
1931 | l += n; | |
1932 | } | |
1933 | ||
2ea24d9f | 1934 | printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time()); |
b76b015e | 1935 | } |
2ea24d9f | 1936 | #endif // wxUSE_LONGLONG_NATIVE |
b76b015e VZ |
1937 | |
1938 | { | |
1939 | wxStopWatch sw; | |
1940 | ||
1941 | wxLongLong l; | |
1942 | for ( n = 0; n < max; n++ ) | |
1943 | { | |
1944 | l += n; | |
1945 | } | |
1946 | ||
1947 | printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time()); | |
1948 | } | |
1949 | } | |
1950 | ||
2a310492 | 1951 | static void TestLongLongConversion() |
b76b015e | 1952 | { |
2a310492 VZ |
1953 | puts("*** Testing wxLongLong conversions ***\n"); |
1954 | ||
1955 | wxLongLong a; | |
1956 | size_t nTested = 0; | |
1957 | for ( size_t n = 0; n < 100000; n++ ) | |
1958 | { | |
1959 | a = RAND_LL(); | |
1960 | ||
1961 | #if wxUSE_LONGLONG_NATIVE | |
1962 | wxLongLongNative b(a.GetHi(), a.GetLo()); | |
5e6a0e83 | 1963 | |
2a310492 VZ |
1964 | wxASSERT_MSG( a == b, "conversions failure" ); |
1965 | #else | |
1966 | puts("Can't do it without native long long type, test skipped."); | |
b76b015e | 1967 | |
2a310492 VZ |
1968 | return; |
1969 | #endif // wxUSE_LONGLONG_NATIVE | |
1970 | ||
1971 | if ( !(nTested % 1000) ) | |
1972 | { | |
1973 | putchar('.'); | |
1974 | fflush(stdout); | |
1975 | } | |
1976 | ||
1977 | nTested++; | |
1978 | } | |
1979 | ||
1980 | puts(" done!"); | |
1981 | } | |
1982 | ||
1983 | static void TestMultiplication() | |
1984 | { | |
1985 | puts("*** Testing wxLongLong multiplication ***\n"); | |
1986 | ||
1987 | wxLongLong a, b; | |
1988 | size_t nTested = 0; | |
1989 | for ( size_t n = 0; n < 100000; n++ ) | |
1990 | { | |
1991 | a = RAND_LL(); | |
1992 | b = RAND_LL(); | |
1993 | ||
1994 | #if wxUSE_LONGLONG_NATIVE | |
1995 | wxLongLongNative aa(a.GetHi(), a.GetLo()); | |
1996 | wxLongLongNative bb(b.GetHi(), b.GetLo()); | |
1997 | ||
1998 | wxASSERT_MSG( a*b == aa*bb, "multiplication failure" ); | |
1999 | #else // !wxUSE_LONGLONG_NATIVE | |
2000 | puts("Can't do it without native long long type, test skipped."); | |
2001 | ||
2002 | return; | |
2003 | #endif // wxUSE_LONGLONG_NATIVE | |
2004 | ||
2005 | if ( !(nTested % 1000) ) | |
2006 | { | |
2007 | putchar('.'); | |
2008 | fflush(stdout); | |
2009 | } | |
2010 | ||
2011 | nTested++; | |
2012 | } | |
2013 | ||
2014 | puts(" done!"); | |
2015 | } | |
2016 | ||
2017 | static void TestDivision() | |
2018 | { | |
2019 | puts("*** Testing wxLongLong division ***\n"); | |
2f02cb89 | 2020 | |
2ea24d9f | 2021 | wxLongLong q, r; |
2f02cb89 | 2022 | size_t nTested = 0; |
5e6a0e83 | 2023 | for ( size_t n = 0; n < 100000; n++ ) |
2f02cb89 VZ |
2024 | { |
2025 | // get a random wxLongLong (shifting by 12 the MSB ensures that the | |
2026 | // multiplication will not overflow) | |
2027 | wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand()); | |
2028 | ||
19f45995 VZ |
2029 | // get a random (but non null) long (not wxLongLong for now) to divide |
2030 | // it with | |
2031 | long l; | |
2032 | do | |
2033 | { | |
2034 | l = rand(); | |
2035 | } | |
2036 | while ( !l ); | |
2037 | ||
2ea24d9f VZ |
2038 | q = ll / l; |
2039 | r = ll % l; | |
2040 | ||
2a310492 VZ |
2041 | #if wxUSE_LONGLONG_NATIVE |
2042 | wxLongLongNative m(ll.GetHi(), ll.GetLo()); | |
2043 | ||
2044 | wxLongLongNative p = m / l, s = m % l; | |
2045 | wxASSERT_MSG( q == p && r == s, "division failure" ); | |
2046 | #else // !wxUSE_LONGLONG_NATIVE | |
5e6a0e83 | 2047 | // verify the result |
2ea24d9f | 2048 | wxASSERT_MSG( ll == q*l + r, "division failure" ); |
2a310492 | 2049 | #endif // wxUSE_LONGLONG_NATIVE |
2f02cb89 | 2050 | |
5e6a0e83 VZ |
2051 | if ( !(nTested % 1000) ) |
2052 | { | |
2053 | putchar('.'); | |
2054 | fflush(stdout); | |
2055 | } | |
2056 | ||
2f02cb89 VZ |
2057 | nTested++; |
2058 | } | |
2059 | ||
5e6a0e83 | 2060 | puts(" done!"); |
2a310492 | 2061 | } |
2f02cb89 | 2062 | |
2a310492 VZ |
2063 | static void TestAddition() |
2064 | { | |
2065 | puts("*** Testing wxLongLong addition ***\n"); | |
2066 | ||
2067 | wxLongLong a, b, c; | |
2068 | size_t nTested = 0; | |
2069 | for ( size_t n = 0; n < 100000; n++ ) | |
2070 | { | |
2071 | a = RAND_LL(); | |
2072 | b = RAND_LL(); | |
2073 | c = a + b; | |
2074 | ||
2075 | #if wxUSE_LONGLONG_NATIVE | |
2076 | wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) + | |
2077 | wxLongLongNative(b.GetHi(), b.GetLo()), | |
7c968cee | 2078 | "addition failure" ); |
2a310492 VZ |
2079 | #else // !wxUSE_LONGLONG_NATIVE |
2080 | wxASSERT_MSG( c - b == a, "addition failure" ); | |
2081 | #endif // wxUSE_LONGLONG_NATIVE | |
2082 | ||
2083 | if ( !(nTested % 1000) ) | |
2084 | { | |
2085 | putchar('.'); | |
2086 | fflush(stdout); | |
2087 | } | |
2088 | ||
2089 | nTested++; | |
2090 | } | |
2091 | ||
2092 | puts(" done!"); | |
b76b015e VZ |
2093 | } |
2094 | ||
2a310492 VZ |
2095 | static void TestBitOperations() |
2096 | { | |
2097 | puts("*** Testing wxLongLong bit operation ***\n"); | |
2098 | ||
f6bcfd97 | 2099 | wxLongLong ll; |
2a310492 VZ |
2100 | size_t nTested = 0; |
2101 | for ( size_t n = 0; n < 100000; n++ ) | |
2102 | { | |
f6bcfd97 | 2103 | ll = RAND_LL(); |
2a310492 VZ |
2104 | |
2105 | #if wxUSE_LONGLONG_NATIVE | |
2106 | for ( size_t n = 0; n < 33; n++ ) | |
2107 | { | |
2a310492 | 2108 | } |
2a310492 VZ |
2109 | #else // !wxUSE_LONGLONG_NATIVE |
2110 | puts("Can't do it without native long long type, test skipped."); | |
2111 | ||
2112 | return; | |
2113 | #endif // wxUSE_LONGLONG_NATIVE | |
2114 | ||
2115 | if ( !(nTested % 1000) ) | |
2116 | { | |
2117 | putchar('.'); | |
2118 | fflush(stdout); | |
2119 | } | |
2120 | ||
2121 | nTested++; | |
2122 | } | |
2123 | ||
2124 | puts(" done!"); | |
2125 | } | |
2126 | ||
f6bcfd97 BP |
2127 | static void TestLongLongComparison() |
2128 | { | |
2d3112ad | 2129 | #if wxUSE_LONGLONG_WX |
f6bcfd97 BP |
2130 | puts("*** Testing wxLongLong comparison ***\n"); |
2131 | ||
f6bcfd97 BP |
2132 | static const long ls[2] = |
2133 | { | |
2134 | 0x1234, | |
2135 | -0x1234, | |
2136 | }; | |
2137 | ||
2138 | wxLongLongWx lls[2]; | |
2139 | lls[0] = ls[0]; | |
3a994742 | 2140 | lls[1] = ls[1]; |
f6bcfd97 BP |
2141 | |
2142 | for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ ) | |
2143 | { | |
2144 | bool res; | |
2145 | ||
2146 | for ( size_t m = 0; m < WXSIZEOF(lls); m++ ) | |
2147 | { | |
2148 | res = lls[m] > testLongs[n]; | |
2149 | printf("0x%lx > 0x%lx is %s (%s)\n", | |
2150 | ls[m], testLongs[n], res ? "true" : "false", | |
2151 | res == (ls[m] > testLongs[n]) ? "ok" : "ERROR"); | |
2152 | ||
2153 | res = lls[m] < testLongs[n]; | |
2154 | printf("0x%lx < 0x%lx is %s (%s)\n", | |
2155 | ls[m], testLongs[n], res ? "true" : "false", | |
2156 | res == (ls[m] < testLongs[n]) ? "ok" : "ERROR"); | |
2157 | ||
2158 | res = lls[m] == testLongs[n]; | |
2159 | printf("0x%lx == 0x%lx is %s (%s)\n", | |
2160 | ls[m], testLongs[n], res ? "true" : "false", | |
2161 | res == (ls[m] == testLongs[n]) ? "ok" : "ERROR"); | |
2162 | } | |
2163 | } | |
2d3112ad | 2164 | #endif // wxUSE_LONGLONG_WX |
f6bcfd97 BP |
2165 | } |
2166 | ||
3a994742 VZ |
2167 | static void TestLongLongPrint() |
2168 | { | |
2169 | wxPuts(_T("*** Testing wxLongLong printing ***\n")); | |
2170 | ||
2171 | for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ ) | |
2172 | { | |
2173 | wxLongLong ll = testLongs[n]; | |
2174 | wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str()); | |
2175 | } | |
2176 | ||
2177 | wxLongLong ll(0x12345678, 0x87654321); | |
2178 | wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str()); | |
2179 | ||
2180 | ll.Negate(); | |
2181 | wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str()); | |
2182 | } | |
2183 | ||
2a310492 VZ |
2184 | #undef MAKE_LL |
2185 | #undef RAND_LL | |
2186 | ||
b76b015e VZ |
2187 | #endif // TEST_LONGLONG |
2188 | ||
39189b9d VZ |
2189 | // ---------------------------------------------------------------------------- |
2190 | // path list | |
2191 | // ---------------------------------------------------------------------------- | |
2192 | ||
2193 | #ifdef TEST_PATHLIST | |
2194 | ||
2195 | static void TestPathList() | |
2196 | { | |
2197 | puts("*** Testing wxPathList ***\n"); | |
2198 | ||
2199 | wxPathList pathlist; | |
2200 | pathlist.AddEnvList("PATH"); | |
2201 | wxString path = pathlist.FindValidPath("ls"); | |
2202 | if ( path.empty() ) | |
2203 | { | |
2204 | printf("ERROR: command not found in the path.\n"); | |
2205 | } | |
2206 | else | |
2207 | { | |
2208 | printf("Command found in the path as '%s'.\n", path.c_str()); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | #endif // TEST_PATHLIST | |
2213 | ||
07a56e45 VZ |
2214 | // ---------------------------------------------------------------------------- |
2215 | // regular expressions | |
2216 | // ---------------------------------------------------------------------------- | |
2217 | ||
2218 | #ifdef TEST_REGEX | |
2219 | ||
e84010cf | 2220 | #include "wx/regex.h" |
07a56e45 VZ |
2221 | |
2222 | static void TestRegExCompile() | |
2223 | { | |
2224 | wxPuts(_T("*** Testing RE compilation ***\n")); | |
2225 | ||
2226 | static struct RegExCompTestData | |
2227 | { | |
2228 | const wxChar *pattern; | |
2229 | bool correct; | |
2230 | } regExCompTestData[] = | |
2231 | { | |
2232 | { _T("foo"), TRUE }, | |
2233 | { _T("foo("), FALSE }, | |
2234 | { _T("foo(bar"), FALSE }, | |
2235 | { _T("foo(bar)"), TRUE }, | |
2236 | { _T("foo["), FALSE }, | |
2237 | { _T("foo[bar"), FALSE }, | |
2238 | { _T("foo[bar]"), TRUE }, | |
2239 | { _T("foo{"), TRUE }, | |
2240 | { _T("foo{1"), FALSE }, | |
2241 | { _T("foo{bar"), TRUE }, | |
2242 | { _T("foo{1}"), TRUE }, | |
2243 | { _T("foo{1,2}"), TRUE }, | |
2244 | { _T("foo{bar}"), TRUE }, | |
2245 | { _T("foo*"), TRUE }, | |
2246 | { _T("foo**"), FALSE }, | |
2247 | { _T("foo+"), TRUE }, | |
2248 | { _T("foo++"), FALSE }, | |
2249 | { _T("foo?"), TRUE }, | |
2250 | { _T("foo??"), FALSE }, | |
2251 | { _T("foo?+"), FALSE }, | |
2252 | }; | |
2253 | ||
2254 | wxRegEx re; | |
2255 | for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ ) | |
2256 | { | |
2257 | const RegExCompTestData& data = regExCompTestData[n]; | |
2258 | bool ok = re.Compile(data.pattern); | |
2259 | ||
2260 | wxPrintf(_T("'%s' is %sa valid RE (%s)\n"), | |
2261 | data.pattern, | |
2262 | ok ? _T("") : _T("not "), | |
2263 | ok == data.correct ? _T("ok") : _T("ERROR")); | |
2264 | } | |
2265 | } | |
2266 | ||
2267 | static void TestRegExMatch() | |
2268 | { | |
2269 | wxPuts(_T("*** Testing RE matching ***\n")); | |
2270 | ||
2271 | static struct RegExMatchTestData | |
2272 | { | |
2273 | const wxChar *pattern; | |
2274 | const wxChar *text; | |
2275 | bool correct; | |
2276 | } regExMatchTestData[] = | |
2277 | { | |
2278 | { _T("foo"), _T("bar"), FALSE }, | |
2279 | { _T("foo"), _T("foobar"), TRUE }, | |
2280 | { _T("^foo"), _T("foobar"), TRUE }, | |
2281 | { _T("^foo"), _T("barfoo"), FALSE }, | |
2282 | { _T("bar$"), _T("barbar"), TRUE }, | |
2283 | { _T("bar$"), _T("barbar "), FALSE }, | |
2284 | }; | |
2285 | ||
2286 | for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ ) | |
2287 | { | |
2288 | const RegExMatchTestData& data = regExMatchTestData[n]; | |
2289 | ||
2290 | wxRegEx re(data.pattern); | |
2291 | bool ok = re.Matches(data.text); | |
2292 | ||
2293 | wxPrintf(_T("'%s' %s %s (%s)\n"), | |
2294 | data.pattern, | |
2295 | ok ? _T("matches") : _T("doesn't match"), | |
2296 | data.text, | |
2297 | ok == data.correct ? _T("ok") : _T("ERROR")); | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | static void TestRegExSubmatch() | |
2302 | { | |
2303 | wxPuts(_T("*** Testing RE subexpressions ***\n")); | |
2304 | ||
2305 | wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$")); | |
2306 | if ( !re.IsValid() ) | |
2307 | { | |
2308 | wxPuts(_T("ERROR: compilation failed.")); | |
2309 | return; | |
2310 | } | |
2311 | ||
2312 | wxString text = _T("Fri Jul 13 18:37:52 CEST 2001"); | |
2313 | ||
2314 | if ( !re.Matches(text) ) | |
2315 | { | |
2316 | wxPuts(_T("ERROR: match expected.")); | |
2317 | } | |
2318 | else | |
2319 | { | |
2320 | wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str()); | |
2321 | ||
2322 | wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"), | |
2323 | re.GetMatch(text, 3).c_str(), | |
2324 | re.GetMatch(text, 2).c_str(), | |
2325 | re.GetMatch(text, 4).c_str(), | |
2326 | re.GetMatch(text, 1).c_str()); | |
2327 | } | |
2328 | } | |
2329 | ||
765624f7 VZ |
2330 | static void TestRegExReplacement() |
2331 | { | |
2332 | wxPuts(_T("*** Testing RE replacement ***")); | |
2333 | ||
2334 | static struct RegExReplTestData | |
2335 | { | |
2336 | const wxChar *text; | |
2337 | const wxChar *repl; | |
2338 | const wxChar *result; | |
2339 | size_t count; | |
2340 | } regExReplTestData[] = | |
2341 | { | |
2342 | { _T("foo123"), _T("bar"), _T("bar"), 1 }, | |
2343 | { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 }, | |
2344 | { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 }, | |
2345 | { _T("123foo"), _T("bar"), _T("123foo"), 0 }, | |
2346 | { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 }, | |
2347 | { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 }, | |
2348 | { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 }, | |
2349 | }; | |
2350 | ||
2351 | const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)"); | |
daa2c7d9 | 2352 | wxRegEx re(pattern); |
765624f7 VZ |
2353 | |
2354 | wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern); | |
2355 | ||
2356 | for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ ) | |
2357 | { | |
2358 | const RegExReplTestData& data = regExReplTestData[n]; | |
2359 | ||
2360 | wxString text = data.text; | |
2361 | size_t nRepl = re.Replace(&text, data.repl); | |
2362 | ||
2363 | wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("), | |
2364 | data.text, data.repl, | |
2365 | nRepl, nRepl == 1 ? _T("") : _T("es"), | |
2366 | text.c_str()); | |
2367 | if ( text == data.result && nRepl == data.count ) | |
2368 | { | |
2369 | wxPuts(_T("ok)")); | |
2370 | } | |
2371 | else | |
2372 | { | |
2373 | wxPrintf(_T("ERROR: should be %u and '%s')\n"), | |
2374 | data.count, data.result); | |
2375 | } | |
2376 | } | |
2377 | } | |
2378 | ||
07a56e45 VZ |
2379 | static void TestRegExInteractive() |
2380 | { | |
2381 | wxPuts(_T("*** Testing RE interactively ***")); | |
2382 | ||
2383 | for ( ;; ) | |
2384 | { | |
2385 | char pattern[128]; | |
2386 | printf("\nEnter a pattern: "); | |
2387 | if ( !fgets(pattern, WXSIZEOF(pattern), stdin) ) | |
2388 | break; | |
2389 | ||
2390 | // kill the last '\n' | |
2391 | pattern[strlen(pattern) - 1] = 0; | |
2392 | ||
2393 | wxRegEx re; | |
2394 | if ( !re.Compile(pattern) ) | |
2395 | { | |
2396 | continue; | |
2397 | } | |
2398 | ||
2399 | char text[128]; | |
2400 | for ( ;; ) | |
2401 | { | |
2402 | printf("Enter text to match: "); | |
2403 | if ( !fgets(text, WXSIZEOF(text), stdin) ) | |
2404 | break; | |
2405 | ||
2406 | // kill the last '\n' | |
2407 | text[strlen(text) - 1] = 0; | |
2408 | ||
2409 | if ( !re.Matches(text) ) | |
2410 | { | |
2411 | printf("No match.\n"); | |
2412 | } | |
2413 | else | |
2414 | { | |
2415 | printf("Pattern matches at '%s'\n", re.GetMatch(text).c_str()); | |
2416 | ||
2417 | size_t start, len; | |
2418 | for ( size_t n = 1; ; n++ ) | |
2419 | { | |
2420 | if ( !re.GetMatch(&start, &len, n) ) | |
2421 | { | |
2422 | break; | |
2423 | } | |
2424 | ||
2425 | printf("Subexpr %u matched '%s'\n", | |
2426 | n, wxString(text + start, len).c_str()); | |
2427 | } | |
2428 | } | |
2429 | } | |
2430 | } | |
2431 | } | |
2432 | ||
2433 | #endif // TEST_REGEX | |
2434 | ||
8d5eff60 VZ |
2435 | // ---------------------------------------------------------------------------- |
2436 | // database | |
2437 | // ---------------------------------------------------------------------------- | |
2438 | ||
ba6ea19e VZ |
2439 | #if !wxUSE_ODBC |
2440 | #undef TEST_ODBC | |
2441 | #endif | |
2442 | ||
8d5eff60 VZ |
2443 | #ifdef TEST_ODBC |
2444 | ||
2445 | #include <wx/db.h> | |
2446 | ||
2447 | static void TestDbOpen() | |
2448 | { | |
2449 | HENV henv; | |
2450 | wxDb db(henv); | |
2451 | } | |
2452 | ||
2453 | #endif // TEST_ODBC | |
2454 | ||
6dfec4b8 | 2455 | // ---------------------------------------------------------------------------- |
7ba4fbeb | 2456 | // registry and related stuff |
6dfec4b8 VZ |
2457 | // ---------------------------------------------------------------------------- |
2458 | ||
2459 | // this is for MSW only | |
2460 | #ifndef __WXMSW__ | |
7ba4fbeb | 2461 | #undef TEST_REGCONF |
6dfec4b8 VZ |
2462 | #undef TEST_REGISTRY |
2463 | #endif | |
2464 | ||
7ba4fbeb VZ |
2465 | #ifdef TEST_REGCONF |
2466 | ||
e84010cf GD |
2467 | #include "wx/confbase.h" |
2468 | #include "wx/msw/regconf.h" | |
7ba4fbeb VZ |
2469 | |
2470 | static void TestRegConfWrite() | |
2471 | { | |
2472 | wxRegConfig regconf(_T("console"), _T("wxwindows")); | |
2473 | regconf.Write(_T("Hello"), wxString(_T("world"))); | |
2474 | } | |
2475 | ||
2476 | #endif // TEST_REGCONF | |
2477 | ||
6dfec4b8 VZ |
2478 | #ifdef TEST_REGISTRY |
2479 | ||
e84010cf | 2480 | #include "wx/msw/registry.h" |
6dfec4b8 VZ |
2481 | |
2482 | // I chose this one because I liked its name, but it probably only exists under | |
2483 | // NT | |
2484 | static const wxChar *TESTKEY = | |
2485 | _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl"); | |
2486 | ||
2487 | static void TestRegistryRead() | |
2488 | { | |
2489 | puts("*** testing registry reading ***"); | |
2490 | ||
2491 | wxRegKey key(TESTKEY); | |
2492 | printf("The test key name is '%s'.\n", key.GetName().c_str()); | |
2493 | if ( !key.Open() ) | |
2494 | { | |
2495 | puts("ERROR: test key can't be opened, aborting test."); | |
2496 | ||
2497 | return; | |
2498 | } | |
2499 | ||
2500 | size_t nSubKeys, nValues; | |
2501 | if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) ) | |
2502 | { | |
2503 | printf("It has %u subkeys and %u values.\n", nSubKeys, nValues); | |
2504 | } | |
2505 | ||
2506 | printf("Enumerating values:\n"); | |
2507 | ||
2508 | long dummy; | |
2509 | wxString value; | |
2510 | bool cont = key.GetFirstValue(value, dummy); | |
2511 | while ( cont ) | |
2512 | { | |
2513 | printf("Value '%s': type ", value.c_str()); | |
2514 | switch ( key.GetValueType(value) ) | |
2515 | { | |
2516 | case wxRegKey::Type_None: printf("ERROR (none)"); break; | |
2517 | case wxRegKey::Type_String: printf("SZ"); break; | |
2518 | case wxRegKey::Type_Expand_String: printf("EXPAND_SZ"); break; | |
2519 | case wxRegKey::Type_Binary: printf("BINARY"); break; | |
2520 | case wxRegKey::Type_Dword: printf("DWORD"); break; | |
2521 | case wxRegKey::Type_Multi_String: printf("MULTI_SZ"); break; | |
2522 | default: printf("other (unknown)"); break; | |
2523 | } | |
2524 | ||
2525 | printf(", value = "); | |
2526 | if ( key.IsNumericValue(value) ) | |
2527 | { | |
2528 | long val; | |
2529 | key.QueryValue(value, &val); | |
2530 | printf("%ld", val); | |
2531 | } | |
2532 | else // string | |
2533 | { | |
2534 | wxString val; | |
2535 | key.QueryValue(value, val); | |
2536 | printf("'%s'", val.c_str()); | |
2537 | ||
2538 | key.QueryRawValue(value, val); | |
2539 | printf(" (raw value '%s')", val.c_str()); | |
2540 | } | |
2541 | ||
2542 | putchar('\n'); | |
2543 | ||
2544 | cont = key.GetNextValue(value, dummy); | |
2545 | } | |
2546 | } | |
2547 | ||
6ba63600 VZ |
2548 | static void TestRegistryAssociation() |
2549 | { | |
2550 | /* | |
2551 | The second call to deleteself genertaes an error message, with a | |
2552 | messagebox saying .flo is crucial to system operation, while the .ddf | |
2553 | call also fails, but with no error message | |
2554 | */ | |
2555 | ||
2556 | wxRegKey key; | |
2557 | ||
2558 | key.SetName("HKEY_CLASSES_ROOT\\.ddf" ); | |
2559 | key.Create(); | |
2560 | key = "ddxf_auto_file" ; | |
2561 | key.SetName("HKEY_CLASSES_ROOT\\.flo" ); | |
2562 | key.Create(); | |
2563 | key = "ddxf_auto_file" ; | |
2564 | key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"); | |
2565 | key.Create(); | |
2566 | key = "program,0" ; | |
2567 | key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"); | |
2568 | key.Create(); | |
2569 | key = "program \"%1\"" ; | |
2570 | ||
2571 | key.SetName("HKEY_CLASSES_ROOT\\.ddf" ); | |
2572 | key.DeleteSelf(); | |
2573 | key.SetName("HKEY_CLASSES_ROOT\\.flo" ); | |
2574 | key.DeleteSelf(); | |
2575 | key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"); | |
2576 | key.DeleteSelf(); | |
2577 | key.SetName("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"); | |
2578 | key.DeleteSelf(); | |
2579 | } | |
2580 | ||
6dfec4b8 VZ |
2581 | #endif // TEST_REGISTRY |
2582 | ||
2c8e4738 VZ |
2583 | // ---------------------------------------------------------------------------- |
2584 | // sockets | |
2585 | // ---------------------------------------------------------------------------- | |
2586 | ||
2587 | #ifdef TEST_SOCKETS | |
2588 | ||
e84010cf GD |
2589 | #include "wx/socket.h" |
2590 | #include "wx/protocol/protocol.h" | |
2591 | #include "wx/protocol/http.h" | |
8e907a13 VZ |
2592 | |
2593 | static void TestSocketServer() | |
2594 | { | |
2595 | puts("*** Testing wxSocketServer ***\n"); | |
2596 | ||
ccdb23df VZ |
2597 | static const int PORT = 3000; |
2598 | ||
8e907a13 | 2599 | wxIPV4address addr; |
ccdb23df | 2600 | addr.Service(PORT); |
8e907a13 VZ |
2601 | |
2602 | wxSocketServer *server = new wxSocketServer(addr); | |
2603 | if ( !server->Ok() ) | |
2604 | { | |
2605 | puts("ERROR: failed to bind"); | |
ccdb23df VZ |
2606 | |
2607 | return; | |
8e907a13 | 2608 | } |
8dfea369 VZ |
2609 | |
2610 | for ( ;; ) | |
2611 | { | |
ccdb23df | 2612 | printf("Server: waiting for connection on port %d...\n", PORT); |
8dfea369 VZ |
2613 | |
2614 | wxSocketBase *socket = server->Accept(); | |
2615 | if ( !socket ) | |
2616 | { | |
2617 | puts("ERROR: wxSocketServer::Accept() failed."); | |
2618 | break; | |
2619 | } | |
2620 | ||
2621 | puts("Server: got a client."); | |
2622 | ||
ccdb23df VZ |
2623 | server->SetTimeout(60); // 1 min |
2624 | ||
2625 | while ( socket->IsConnected() ) | |
8dfea369 | 2626 | { |
ccdb23df VZ |
2627 | wxString s; |
2628 | char ch = '\0'; | |
2629 | for ( ;; ) | |
8dfea369 | 2630 | { |
ccdb23df VZ |
2631 | if ( socket->Read(&ch, sizeof(ch)).Error() ) |
2632 | { | |
2633 | // don't log error if the client just close the connection | |
2634 | if ( socket->IsConnected() ) | |
2635 | { | |
2636 | puts("ERROR: in wxSocket::Read."); | |
2637 | } | |
8dfea369 | 2638 | |
ccdb23df VZ |
2639 | break; |
2640 | } | |
8dfea369 | 2641 | |
ccdb23df VZ |
2642 | if ( ch == '\r' ) |
2643 | continue; | |
8dfea369 | 2644 | |
ccdb23df VZ |
2645 | if ( ch == '\n' ) |
2646 | break; | |
8dfea369 | 2647 | |
ccdb23df VZ |
2648 | s += ch; |
2649 | } | |
8dfea369 | 2650 | |
ccdb23df VZ |
2651 | if ( ch != '\n' ) |
2652 | { | |
2653 | break; | |
2654 | } | |
8dfea369 | 2655 | |
ccdb23df VZ |
2656 | printf("Server: got '%s'.\n", s.c_str()); |
2657 | if ( s == _T("bye") ) | |
2658 | { | |
2659 | delete socket; | |
8dfea369 | 2660 | |
ccdb23df VZ |
2661 | break; |
2662 | } | |
2663 | ||
2664 | socket->Write(s.MakeUpper().c_str(), s.length()); | |
2665 | socket->Write("\r\n", 2); | |
2666 | printf("Server: wrote '%s'.\n", s.c_str()); | |
8dfea369 VZ |
2667 | } |
2668 | ||
ccdb23df | 2669 | puts("Server: lost a client."); |
8dfea369 | 2670 | |
ccdb23df | 2671 | socket->Destroy(); |
8dfea369 | 2672 | } |
9fc3cba7 | 2673 | |
ccdb23df VZ |
2674 | // same as "delete server" but is consistent with GUI programs |
2675 | server->Destroy(); | |
8e907a13 | 2676 | } |
2c8e4738 VZ |
2677 | |
2678 | static void TestSocketClient() | |
2679 | { | |
2680 | puts("*** Testing wxSocketClient ***\n"); | |
2681 | ||
8e907a13 VZ |
2682 | static const char *hostname = "www.wxwindows.org"; |
2683 | ||
2684 | wxIPV4address addr; | |
2685 | addr.Hostname(hostname); | |
2686 | addr.Service(80); | |
2687 | ||
2688 | printf("--- Attempting to connect to %s:80...\n", hostname); | |
2c8e4738 VZ |
2689 | |
2690 | wxSocketClient client; | |
8e907a13 | 2691 | if ( !client.Connect(addr) ) |
2c8e4738 | 2692 | { |
8e907a13 | 2693 | printf("ERROR: failed to connect to %s\n", hostname); |
2c8e4738 VZ |
2694 | } |
2695 | else | |
2696 | { | |
8e907a13 VZ |
2697 | printf("--- Connected to %s:%u...\n", |
2698 | addr.Hostname().c_str(), addr.Service()); | |
2699 | ||
2c8e4738 VZ |
2700 | char buf[8192]; |
2701 | ||
8e907a13 VZ |
2702 | // could use simply "GET" here I suppose |
2703 | wxString cmdGet = | |
2704 | wxString::Format("GET http://%s/\r\n", hostname); | |
2705 | client.Write(cmdGet, cmdGet.length()); | |
2706 | printf("--- Sent command '%s' to the server\n", | |
2707 | MakePrintable(cmdGet).c_str()); | |
2c8e4738 | 2708 | client.Read(buf, WXSIZEOF(buf)); |
8e907a13 VZ |
2709 | printf("--- Server replied:\n%s", buf); |
2710 | } | |
2711 | } | |
2712 | ||
2e907fab VZ |
2713 | #endif // TEST_SOCKETS |
2714 | ||
b92fd37c VZ |
2715 | // ---------------------------------------------------------------------------- |
2716 | // FTP | |
2717 | // ---------------------------------------------------------------------------- | |
2718 | ||
2e907fab VZ |
2719 | #ifdef TEST_FTP |
2720 | ||
e84010cf | 2721 | #include "wx/protocol/ftp.h" |
2e907fab | 2722 | |
b92fd37c VZ |
2723 | static wxFTP ftp; |
2724 | ||
2725 | #define FTP_ANONYMOUS | |
2726 | ||
2727 | #ifdef FTP_ANONYMOUS | |
2728 | static const char *directory = "/pub"; | |
2729 | static const char *filename = "welcome.msg"; | |
2730 | #else | |
2731 | static const char *directory = "/etc"; | |
2732 | static const char *filename = "issue"; | |
2733 | #endif | |
2734 | ||
2735 | static bool TestFtpConnect() | |
8e907a13 | 2736 | { |
b92fd37c | 2737 | puts("*** Testing FTP connect ***"); |
8e907a13 | 2738 | |
b92fd37c VZ |
2739 | #ifdef FTP_ANONYMOUS |
2740 | static const char *hostname = "ftp.wxwindows.org"; | |
2741 | ||
2742 | printf("--- Attempting to connect to %s:21 anonymously...\n", hostname); | |
2743 | #else // !FTP_ANONYMOUS | |
2744 | static const char *hostname = "localhost"; | |
2745 | ||
2746 | char user[256]; | |
2747 | fgets(user, WXSIZEOF(user), stdin); | |
2748 | user[strlen(user) - 1] = '\0'; // chop off '\n' | |
2749 | ftp.SetUser(user); | |
2750 | ||
2751 | char password[256]; | |
2752 | printf("Password for %s: ", password); | |
2753 | fgets(password, WXSIZEOF(password), stdin); | |
2754 | password[strlen(password) - 1] = '\0'; // chop off '\n' | |
2755 | ftp.SetPassword(password); | |
2756 | ||
2757 | printf("--- Attempting to connect to %s:21 as %s...\n", hostname, user); | |
2758 | #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS | |
2759 | ||
2760 | if ( !ftp.Connect(hostname) ) | |
2761 | { | |
2762 | printf("ERROR: failed to connect to %s\n", hostname); | |
2763 | ||
2764 | return FALSE; | |
2765 | } | |
2766 | else | |
2767 | { | |
2768 | printf("--- Connected to %s, current directory is '%s'\n", | |
2769 | hostname, ftp.Pwd().c_str()); | |
2770 | } | |
2771 | ||
2772 | return TRUE; | |
2773 | } | |
b1229561 | 2774 | |
b92fd37c VZ |
2775 | // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0? |
2776 | static void TestFtpWuFtpd() | |
2777 | { | |
2778 | wxFTP ftp; | |
b1229561 VZ |
2779 | static const char *hostname = "ftp.eudora.com"; |
2780 | if ( !ftp.Connect(hostname) ) | |
2781 | { | |
2782 | printf("ERROR: failed to connect to %s\n", hostname); | |
2783 | } | |
2784 | else | |
2785 | { | |
2786 | static const char *filename = "eudora/pubs/draft-gellens-submit-09.txt"; | |
2787 | wxInputStream *in = ftp.GetInputStream(filename); | |
2788 | if ( !in ) | |
2789 | { | |
2790 | printf("ERROR: couldn't get input stream for %s\n", filename); | |
2791 | } | |
2792 | else | |
2793 | { | |
2794 | size_t size = in->StreamSize(); | |
2795 | printf("Reading file %s (%u bytes)...", filename, size); | |
2796 | ||
2797 | char *data = new char[size]; | |
2798 | if ( !in->Read(data, size) ) | |
2799 | { | |
2800 | puts("ERROR: read error"); | |
2801 | } | |
2802 | else | |
2803 | { | |
2804 | printf("Successfully retrieved the file.\n"); | |
2805 | } | |
2806 | ||
2807 | delete [] data; | |
2808 | delete in; | |
2809 | } | |
2810 | } | |
b92fd37c | 2811 | } |
b1229561 | 2812 | |
b92fd37c VZ |
2813 | static void TestFtpList() |
2814 | { | |
2815 | puts("*** Testing wxFTP file listing ***\n"); | |
8e907a13 | 2816 | |
b92fd37c VZ |
2817 | // test CWD |
2818 | if ( !ftp.ChDir(directory) ) | |
2819 | { | |
2820 | printf("ERROR: failed to cd to %s\n", directory); | |
2821 | } | |
2e907fab | 2822 | |
b92fd37c | 2823 | printf("Current directory is '%s'\n", ftp.Pwd().c_str()); |
2e907fab | 2824 | |
b92fd37c VZ |
2825 | // test NLIST and LIST |
2826 | wxArrayString files; | |
2827 | if ( !ftp.GetFilesList(files) ) | |
8e907a13 | 2828 | { |
b92fd37c | 2829 | puts("ERROR: failed to get NLIST of files"); |
8e907a13 VZ |
2830 | } |
2831 | else | |
2832 | { | |
b92fd37c VZ |
2833 | printf("Brief list of files under '%s':\n", ftp.Pwd().c_str()); |
2834 | size_t count = files.GetCount(); | |
2835 | for ( size_t n = 0; n < count; n++ ) | |
8e907a13 | 2836 | { |
b92fd37c | 2837 | printf("\t%s\n", files[n].c_str()); |
8e907a13 | 2838 | } |
b92fd37c VZ |
2839 | puts("End of the file list"); |
2840 | } | |
8e907a13 | 2841 | |
b92fd37c VZ |
2842 | if ( !ftp.GetDirList(files) ) |
2843 | { | |
2844 | puts("ERROR: failed to get LIST of files"); | |
2845 | } | |
2846 | else | |
2847 | { | |
2848 | printf("Detailed list of files under '%s':\n", ftp.Pwd().c_str()); | |
2849 | size_t count = files.GetCount(); | |
2850 | for ( size_t n = 0; n < count; n++ ) | |
8e907a13 | 2851 | { |
b92fd37c | 2852 | printf("\t%s\n", files[n].c_str()); |
2e907fab | 2853 | } |
b92fd37c VZ |
2854 | puts("End of the file list"); |
2855 | } | |
2856 | ||
2857 | if ( !ftp.ChDir(_T("..")) ) | |
2858 | { | |
2859 | puts("ERROR: failed to cd to .."); | |
2860 | } | |
2e907fab | 2861 | |
b92fd37c VZ |
2862 | printf("Current directory is '%s'\n", ftp.Pwd().c_str()); |
2863 | } | |
2864 | ||
2865 | static void TestFtpDownload() | |
2866 | { | |
2867 | puts("*** Testing wxFTP download ***\n"); | |
2868 | ||
2869 | // test RETR | |
2870 | wxInputStream *in = ftp.GetInputStream(filename); | |
2871 | if ( !in ) | |
2872 | { | |
2873 | printf("ERROR: couldn't get input stream for %s\n", filename); | |
2874 | } | |
2875 | else | |
2876 | { | |
2877 | size_t size = in->StreamSize(); | |
2878 | printf("Reading file %s (%u bytes)...", filename, size); | |
2879 | fflush(stdout); | |
2880 | ||
2881 | char *data = new char[size]; | |
2882 | if ( !in->Read(data, size) ) | |
2e907fab | 2883 | { |
b92fd37c | 2884 | puts("ERROR: read error"); |
2e907fab VZ |
2885 | } |
2886 | else | |
2887 | { | |
b92fd37c | 2888 | printf("\nContents of %s:\n%s\n", filename, data); |
8e907a13 VZ |
2889 | } |
2890 | ||
b92fd37c VZ |
2891 | delete [] data; |
2892 | delete in; | |
2893 | } | |
2894 | } | |
8e907a13 | 2895 | |
b92fd37c VZ |
2896 | static void TestFtpFileSize() |
2897 | { | |
2898 | puts("*** Testing FTP SIZE command ***"); | |
2899 | ||
2900 | if ( !ftp.ChDir(directory) ) | |
2901 | { | |
2902 | printf("ERROR: failed to cd to %s\n", directory); | |
2903 | } | |
2904 | ||
2905 | printf("Current directory is '%s'\n", ftp.Pwd().c_str()); | |
2906 | ||
2907 | if ( ftp.FileExists(filename) ) | |
2908 | { | |
2909 | int size = ftp.GetFileSize(filename); | |
2910 | if ( size == -1 ) | |
2911 | printf("ERROR: couldn't get size of '%s'\n", filename); | |
8e907a13 | 2912 | else |
b92fd37c VZ |
2913 | printf("Size of '%s' is %d bytes.\n", filename, size); |
2914 | } | |
2915 | else | |
2916 | { | |
2917 | printf("ERROR: '%s' doesn't exist\n", filename); | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | static void TestFtpMisc() | |
2922 | { | |
2923 | puts("*** Testing miscellaneous wxFTP functions ***"); | |
2924 | ||
2925 | if ( ftp.SendCommand("STAT") != '2' ) | |
2926 | { | |
2927 | puts("ERROR: STAT failed"); | |
2928 | } | |
2929 | else | |
2930 | { | |
2931 | printf("STAT returned:\n\n%s\n", ftp.GetLastResult().c_str()); | |
2932 | } | |
2933 | ||
2934 | if ( ftp.SendCommand("HELP SITE") != '2' ) | |
2935 | { | |
2936 | puts("ERROR: HELP SITE failed"); | |
2937 | } | |
2938 | else | |
2939 | { | |
2940 | printf("The list of site-specific commands:\n\n%s\n", | |
2941 | ftp.GetLastResult().c_str()); | |
2942 | } | |
2943 | } | |
2944 | ||
2945 | static void TestFtpInteractive() | |
2946 | { | |
2947 | puts("\n*** Interactive wxFTP test ***"); | |
2948 | ||
2949 | char buf[128]; | |
2950 | ||
2951 | for ( ;; ) | |
2952 | { | |
2953 | printf("Enter FTP command: "); | |
2954 | if ( !fgets(buf, WXSIZEOF(buf), stdin) ) | |
2955 | break; | |
2956 | ||
2957 | // kill the last '\n' | |
2958 | buf[strlen(buf) - 1] = 0; | |
2959 | ||
2960 | // special handling of LIST and NLST as they require data connection | |
2961 | wxString start(buf, 4); | |
2962 | start.MakeUpper(); | |
2963 | if ( start == "LIST" || start == "NLST" ) | |
8e907a13 | 2964 | { |
b92fd37c VZ |
2965 | wxString wildcard; |
2966 | if ( strlen(buf) > 4 ) | |
2967 | wildcard = buf + 5; | |
8e907a13 | 2968 | |
b92fd37c VZ |
2969 | wxArrayString files; |
2970 | if ( !ftp.GetList(files, wildcard, start == "LIST") ) | |
8e907a13 | 2971 | { |
b92fd37c | 2972 | printf("ERROR: failed to get %s of files\n", start.c_str()); |
8e907a13 VZ |
2973 | } |
2974 | else | |
2975 | { | |
b92fd37c VZ |
2976 | printf("--- %s of '%s' under '%s':\n", |
2977 | start.c_str(), wildcard.c_str(), ftp.Pwd().c_str()); | |
2978 | size_t count = files.GetCount(); | |
2979 | for ( size_t n = 0; n < count; n++ ) | |
2980 | { | |
2981 | printf("\t%s\n", files[n].c_str()); | |
2982 | } | |
2983 | puts("--- End of the file list"); | |
8e907a13 | 2984 | } |
2e907fab | 2985 | } |
b92fd37c | 2986 | else // !list |
2e907fab | 2987 | { |
b92fd37c VZ |
2988 | char ch = ftp.SendCommand(buf); |
2989 | printf("Command %s", ch ? "succeeded" : "failed"); | |
2990 | if ( ch ) | |
2991 | { | |
2992 | printf(" (return code %c)", ch); | |
2993 | } | |
2e907fab | 2994 | |
b92fd37c | 2995 | printf(", server reply:\n%s\n\n", ftp.GetLastResult().c_str()); |
2e907fab | 2996 | } |
2c8e4738 | 2997 | } |
b92fd37c VZ |
2998 | |
2999 | puts("\n*** done ***"); | |
2c8e4738 VZ |
3000 | } |
3001 | ||
b92fd37c | 3002 | static void TestFtpUpload() |
f6bcfd97 BP |
3003 | { |
3004 | puts("*** Testing wxFTP uploading ***\n"); | |
3005 | ||
b92fd37c VZ |
3006 | // upload a file |
3007 | static const char *file1 = "test1"; | |
3008 | static const char *file2 = "test2"; | |
3009 | wxOutputStream *out = ftp.GetOutputStream(file1); | |
3010 | if ( out ) | |
3011 | { | |
3012 | printf("--- Uploading to %s ---\n", file1); | |
3013 | out->Write("First hello", 11); | |
3014 | delete out; | |
3015 | } | |
f6bcfd97 | 3016 | |
b92fd37c VZ |
3017 | // send a command to check the remote file |
3018 | if ( ftp.SendCommand(wxString("STAT ") + file1) != '2' ) | |
f6bcfd97 | 3019 | { |
b92fd37c | 3020 | printf("ERROR: STAT %s failed\n", file1); |
f6bcfd97 BP |
3021 | } |
3022 | else | |
3023 | { | |
b92fd37c VZ |
3024 | printf("STAT %s returned:\n\n%s\n", |
3025 | file1, ftp.GetLastResult().c_str()); | |
3026 | } | |
2e907fab | 3027 | |
b92fd37c VZ |
3028 | out = ftp.GetOutputStream(file2); |
3029 | if ( out ) | |
3030 | { | |
3031 | printf("--- Uploading to %s ---\n", file1); | |
3032 | out->Write("Second hello", 12); | |
3033 | delete out; | |
f6bcfd97 BP |
3034 | } |
3035 | } | |
3036 | ||
2e907fab | 3037 | #endif // TEST_FTP |
2c8e4738 | 3038 | |
83141d3a VZ |
3039 | // ---------------------------------------------------------------------------- |
3040 | // streams | |
3041 | // ---------------------------------------------------------------------------- | |
3042 | ||
3043 | #ifdef TEST_STREAMS | |
3044 | ||
e84010cf GD |
3045 | #include "wx/wfstream.h" |
3046 | #include "wx/mstream.h" | |
83141d3a | 3047 | |
24f25c8a VZ |
3048 | static void TestFileStream() |
3049 | { | |
3050 | puts("*** Testing wxFileInputStream ***"); | |
3051 | ||
3052 | static const wxChar *filename = _T("testdata.fs"); | |
3053 | { | |
3054 | wxFileOutputStream fsOut(filename); | |
3055 | fsOut.Write("foo", 3); | |
3056 | } | |
3057 | ||
3058 | wxFileInputStream fsIn(filename); | |
3059 | printf("File stream size: %u\n", fsIn.GetSize()); | |
3060 | while ( !fsIn.Eof() ) | |
3061 | { | |
3062 | putchar(fsIn.GetC()); | |
3063 | } | |
3064 | ||
3065 | if ( !wxRemoveFile(filename) ) | |
3066 | { | |
3067 | printf("ERROR: failed to remove the file '%s'.\n", filename); | |
3068 | } | |
3069 | ||
3070 | puts("\n*** wxFileInputStream test done ***"); | |
3071 | } | |
3072 | ||
83141d3a VZ |
3073 | static void TestMemoryStream() |
3074 | { | |
99a5af7f VZ |
3075 | wxPuts(_T("*** Testing wxMemoryOutputStream ***")); |
3076 | ||
3077 | wxMemoryOutputStream memOutStream; | |
3078 | wxPrintf(_T("Initially out stream offset: %lu\n"), | |
3079 | (unsigned long)memOutStream.TellO()); | |
3080 | ||
3081 | for ( const wxChar *p = _T("Hello, stream!"); *p; p++ ) | |
3082 | { | |
3083 | memOutStream.PutC(*p); | |
3084 | } | |
3085 | ||
3086 | wxPrintf(_T("Final out stream offset: %lu\n"), | |
3087 | (unsigned long)memOutStream.TellO()); | |
3088 | ||
3089 | wxPuts(_T("*** Testing wxMemoryInputStream ***")); | |
83141d3a VZ |
3090 | |
3091 | wxChar buf[1024]; | |
99a5af7f | 3092 | size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf)); |
83141d3a | 3093 | |
99a5af7f VZ |
3094 | wxMemoryInputStream memInpStream(buf, len); |
3095 | wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize()); | |
83141d3a VZ |
3096 | while ( !memInpStream.Eof() ) |
3097 | { | |
3098 | putchar(memInpStream.GetC()); | |
3099 | } | |
3100 | ||
3101 | puts("\n*** wxMemoryInputStream test done ***"); | |
3102 | } | |
3103 | ||
3104 | #endif // TEST_STREAMS | |
3105 | ||
d31b7b68 VZ |
3106 | // ---------------------------------------------------------------------------- |
3107 | // timers | |
3108 | // ---------------------------------------------------------------------------- | |
3109 | ||
3110 | #ifdef TEST_TIMER | |
3111 | ||
e84010cf GD |
3112 | #include "wx/timer.h" |
3113 | #include "wx/utils.h" | |
d31b7b68 VZ |
3114 | |
3115 | static void TestStopWatch() | |
3116 | { | |
3117 | puts("*** Testing wxStopWatch ***\n"); | |
3118 | ||
3119 | wxStopWatch sw; | |
677eff07 VZ |
3120 | sw.Pause(); |
3121 | printf("Initially paused, after 2 seconds time is..."); | |
3122 | fflush(stdout); | |
3123 | wxSleep(2); | |
3124 | printf("\t%ldms\n", sw.Time()); | |
3125 | ||
3126 | printf("Resuming stopwatch and sleeping 3 seconds..."); | |
3127 | fflush(stdout); | |
3128 | sw.Resume(); | |
d31b7b68 | 3129 | wxSleep(3); |
87798c00 | 3130 | printf("\telapsed time: %ldms\n", sw.Time()); |
d31b7b68 VZ |
3131 | |
3132 | sw.Pause(); | |
677eff07 VZ |
3133 | printf("Pausing agan and sleeping 2 more seconds..."); |
3134 | fflush(stdout); | |
d31b7b68 | 3135 | wxSleep(2); |
87798c00 | 3136 | printf("\telapsed time: %ldms\n", sw.Time()); |
d31b7b68 VZ |
3137 | |
3138 | sw.Resume(); | |
677eff07 VZ |
3139 | printf("Finally resuming and sleeping 2 more seconds..."); |
3140 | fflush(stdout); | |
3141 | wxSleep(2); | |
87798c00 VZ |
3142 | printf("\telapsed time: %ldms\n", sw.Time()); |
3143 | ||
3144 | wxStopWatch sw2; | |
3145 | puts("\nChecking for 'backwards clock' bug..."); | |
3146 | for ( size_t n = 0; n < 70; n++ ) | |
3147 | { | |
3148 | sw2.Start(); | |
89e6463c GRG |
3149 | |
3150 | for ( size_t m = 0; m < 100000; m++ ) | |
87798c00 | 3151 | { |
89e6463c GRG |
3152 | if ( sw.Time() < 0 || sw2.Time() < 0 ) |
3153 | { | |
3154 | puts("\ntime is negative - ERROR!"); | |
3155 | } | |
87798c00 VZ |
3156 | } |
3157 | ||
3158 | putchar('.'); | |
677eff07 | 3159 | fflush(stdout); |
87798c00 VZ |
3160 | } |
3161 | ||
3162 | puts(", ok."); | |
d31b7b68 VZ |
3163 | } |
3164 | ||
3165 | #endif // TEST_TIMER | |
3166 | ||
f6bcfd97 BP |
3167 | // ---------------------------------------------------------------------------- |
3168 | // vCard support | |
3169 | // ---------------------------------------------------------------------------- | |
3170 | ||
3171 | #ifdef TEST_VCARD | |
3172 | ||
e84010cf | 3173 | #include "wx/vcard.h" |
f6bcfd97 BP |
3174 | |
3175 | static void DumpVObject(size_t level, const wxVCardObject& vcard) | |
3176 | { | |
3177 | void *cookie; | |
3178 | wxVCardObject *vcObj = vcard.GetFirstProp(&cookie); | |
3179 | while ( vcObj ) | |
3180 | { | |
3181 | printf("%s%s", | |
3182 | wxString(_T('\t'), level).c_str(), | |
3183 | vcObj->GetName().c_str()); | |
3184 | ||
3185 | wxString value; | |
3186 | switch ( vcObj->GetType() ) | |
3187 | { | |
3188 | case wxVCardObject::String: | |
3189 | case wxVCardObject::UString: | |
3190 | { | |
3191 | wxString val; | |
3192 | vcObj->GetValue(&val); | |
3193 | value << _T('"') << val << _T('"'); | |
3194 | } | |
3195 | break; | |
3196 | ||
3197 | case wxVCardObject::Int: | |
3198 | { | |
3199 | unsigned int i; | |
3200 | vcObj->GetValue(&i); | |
3201 | value.Printf(_T("%u"), i); | |
3202 | } | |
3203 | break; | |
3204 | ||
3205 | case wxVCardObject::Long: | |
3206 | { | |
3207 | unsigned long l; | |
3208 | vcObj->GetValue(&l); | |
3209 | value.Printf(_T("%lu"), l); | |
3210 | } | |
3211 | break; | |
3212 | ||
3213 | case wxVCardObject::None: | |
3214 | break; | |
3215 | ||
3216 | case wxVCardObject::Object: | |
3217 | value = _T("<node>"); | |
3218 | break; | |
3219 | ||
3220 | default: | |
3221 | value = _T("<unknown value type>"); | |
3222 | } | |
3223 | ||
3224 | if ( !!value ) | |
3225 | printf(" = %s", value.c_str()); | |
3226 | putchar('\n'); | |
3227 | ||
3228 | DumpVObject(level + 1, *vcObj); | |
3229 | ||
3230 | delete vcObj; | |
3231 | vcObj = vcard.GetNextProp(&cookie); | |
3232 | } | |
3233 | } | |
3234 | ||
3235 | static void DumpVCardAddresses(const wxVCard& vcard) | |
3236 | { | |
3237 | puts("\nShowing all addresses from vCard:\n"); | |
3238 | ||
3239 | size_t nAdr = 0; | |
3240 | void *cookie; | |
3241 | wxVCardAddress *addr = vcard.GetFirstAddress(&cookie); | |
3242 | while ( addr ) | |
3243 | { | |
3244 | wxString flagsStr; | |
3245 | int flags = addr->GetFlags(); | |
3246 | if ( flags & wxVCardAddress::Domestic ) | |
3247 | { | |
3248 | flagsStr << _T("domestic "); | |
3249 | } | |
3250 | if ( flags & wxVCardAddress::Intl ) | |
3251 | { | |
3252 | flagsStr << _T("international "); | |
3253 | } | |
3254 | if ( flags & wxVCardAddress::Postal ) | |
3255 | { | |
3256 | flagsStr << _T("postal "); | |
3257 | } | |
3258 | if ( flags & wxVCardAddress::Parcel ) | |
3259 | { | |
3260 | flagsStr << _T("parcel "); | |
3261 | } | |
3262 | if ( flags & wxVCardAddress::Home ) | |
3263 | { | |
3264 | flagsStr << _T("home "); | |
3265 | } | |
3266 | if ( flags & wxVCardAddress::Work ) | |
3267 | { | |
3268 | flagsStr << _T("work "); | |
3269 | } | |
3270 | ||
3271 | printf("Address %u:\n" | |
3272 | "\tflags = %s\n" | |
3273 | "\tvalue = %s;%s;%s;%s;%s;%s;%s\n", | |
3274 | ++nAdr, | |
3275 | flagsStr.c_str(), | |
3276 | addr->GetPostOffice().c_str(), | |
3277 | addr->GetExtAddress().c_str(), | |
3278 | addr->GetStreet().c_str(), | |
3279 | addr->GetLocality().c_str(), | |
3280 | addr->GetRegion().c_str(), | |
3281 | addr->GetPostalCode().c_str(), | |
3282 | addr->GetCountry().c_str() | |
3283 | ); | |
3284 | ||
3285 | delete addr; | |
3286 | addr = vcard.GetNextAddress(&cookie); | |
3287 | } | |
3288 | } | |
3289 | ||
3290 | static void DumpVCardPhoneNumbers(const wxVCard& vcard) | |
3291 | { | |
3292 | puts("\nShowing all phone numbers from vCard:\n"); | |
3293 | ||
3294 | size_t nPhone = 0; | |
3295 | void *cookie; | |
3296 | wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie); | |
3297 | while ( phone ) | |
3298 | { | |
3299 | wxString flagsStr; | |
3300 | int flags = phone->GetFlags(); | |
3301 | if ( flags & wxVCardPhoneNumber::Voice ) | |
3302 | { | |
3303 | flagsStr << _T("voice "); | |
3304 | } | |
3305 | if ( flags & wxVCardPhoneNumber::Fax ) | |
3306 | { | |
3307 | flagsStr << _T("fax "); | |
3308 | } | |
3309 | if ( flags & wxVCardPhoneNumber::Cellular ) | |
3310 | { | |
3311 | flagsStr << _T("cellular "); | |
3312 | } | |
3313 | if ( flags & wxVCardPhoneNumber::Modem ) | |
3314 | { | |
3315 | flagsStr << _T("modem "); | |
3316 | } | |
3317 | if ( flags & wxVCardPhoneNumber::Home ) | |
3318 | { | |
3319 | flagsStr << _T("home "); | |
3320 | } | |
3321 | if ( flags & wxVCardPhoneNumber::Work ) | |
3322 | { | |
3323 | flagsStr << _T("work "); | |
3324 | } | |
3325 | ||
3326 | printf("Phone number %u:\n" | |
3327 | "\tflags = %s\n" | |
3328 | "\tvalue = %s\n", | |
3329 | ++nPhone, | |
3330 | flagsStr.c_str(), | |
3331 | phone->GetNumber().c_str() | |
3332 | ); | |
3333 | ||
3334 | delete phone; | |
3335 | phone = vcard.GetNextPhoneNumber(&cookie); | |
3336 | } | |
3337 | } | |
3338 | ||
3339 | static void TestVCardRead() | |
3340 | { | |
3341 | puts("*** Testing wxVCard reading ***\n"); | |
3342 | ||
3343 | wxVCard vcard(_T("vcard.vcf")); | |
3344 | if ( !vcard.IsOk() ) | |
3345 | { | |
3346 | puts("ERROR: couldn't load vCard."); | |
3347 | } | |
3348 | else | |
3349 | { | |
3350 | // read individual vCard properties | |
3351 | wxVCardObject *vcObj = vcard.GetProperty("FN"); | |
3352 | wxString value; | |
3353 | if ( vcObj ) | |
3354 | { | |
3355 | vcObj->GetValue(&value); | |
3356 | delete vcObj; | |
3357 | } | |
3358 | else | |
3359 | { | |
3360 | value = _T("<none>"); | |
3361 | } | |
3362 | ||
3363 | printf("Full name retrieved directly: %s\n", value.c_str()); | |
3364 | ||
3365 | ||
3366 | if ( !vcard.GetFullName(&value) ) | |
3367 | { | |
3368 | value = _T("<none>"); | |
3369 | } | |
3370 | ||
3371 | printf("Full name from wxVCard API: %s\n", value.c_str()); | |
3372 | ||
3373 | // now show how to deal with multiply occuring properties | |
3374 | DumpVCardAddresses(vcard); | |
3375 | DumpVCardPhoneNumbers(vcard); | |
3376 | ||
3377 | // and finally show all | |
3378 | puts("\nNow dumping the entire vCard:\n" | |
3379 | "-----------------------------\n"); | |
3380 | ||
3381 | DumpVObject(0, vcard); | |
3382 | } | |
3383 | } | |
3384 | ||
3385 | static void TestVCardWrite() | |
3386 | { | |
3387 | puts("*** Testing wxVCard writing ***\n"); | |
3388 | ||
3389 | wxVCard vcard; | |
3390 | if ( !vcard.IsOk() ) | |
3391 | { | |
3392 | puts("ERROR: couldn't create vCard."); | |
3393 | } | |
3394 | else | |
3395 | { | |
3396 | // set some fields | |
3397 | vcard.SetName("Zeitlin", "Vadim"); | |
3398 | vcard.SetFullName("Vadim Zeitlin"); | |
3399 | vcard.SetOrganization("wxWindows", "R&D"); | |
3400 | ||
3401 | // just dump the vCard back | |
3402 | puts("Entire vCard follows:\n"); | |
3403 | puts(vcard.Write()); | |
3404 | } | |
3405 | } | |
3406 | ||
3407 | #endif // TEST_VCARD | |
3408 | ||
0e2c5534 VZ |
3409 | // ---------------------------------------------------------------------------- |
3410 | // wxVolume tests | |
3411 | // ---------------------------------------------------------------------------- | |
3412 | ||
ba6ea19e | 3413 | #if !defined(__WIN32__) || !wxUSE_FSVOLUME |
0e2c5534 VZ |
3414 | #undef TEST_VOLUME |
3415 | #endif | |
3416 | ||
3417 | #ifdef TEST_VOLUME | |
3418 | ||
3419 | #include "wx/volume.h" | |
3420 | ||
3421 | static const wxChar *volumeKinds[] = | |
3422 | { | |
3423 | _T("floppy"), | |
3424 | _T("hard disk"), | |
3425 | _T("CD-ROM"), | |
3426 | _T("DVD-ROM"), | |
3427 | _T("network volume"), | |
3428 | _T("other volume"), | |
3429 | }; | |
3430 | ||
3431 | static void TestFSVolume() | |
3432 | { | |
3433 | wxPuts(_T("*** Testing wxFSVolume class ***")); | |
3434 | ||
3435 | wxArrayString volumes = wxFSVolume::GetVolumes(); | |
3436 | size_t count = volumes.GetCount(); | |
3437 | ||
3438 | if ( !count ) | |
3439 | { | |
3440 | wxPuts(_T("ERROR: no mounted volumes?")); | |
3441 | return; | |
3442 | } | |
3443 | ||
3444 | wxPrintf(_T("%u mounted volumes found:\n"), count); | |
3445 | ||
3446 | for ( size_t n = 0; n < count; n++ ) | |
3447 | { | |
3448 | wxFSVolume vol(volumes[n]); | |
3449 | if ( !vol.IsOk() ) | |
3450 | { | |
3451 | wxPuts(_T("ERROR: couldn't create volume")); | |
3452 | continue; | |
3453 | } | |
3454 | ||
3455 | wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"), | |
3456 | n + 1, | |
3457 | vol.GetDisplayName().c_str(), | |
3458 | vol.GetName().c_str(), | |
3459 | volumeKinds[vol.GetKind()], | |
3460 | vol.IsWritable() ? _T("rw") : _T("ro"), | |
3461 | vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable") | |
3462 | : _T("fixed")); | |
3463 | } | |
3464 | } | |
3465 | ||
3466 | #endif // TEST_VOLUME | |
3467 | ||
f6bcfd97 BP |
3468 | // ---------------------------------------------------------------------------- |
3469 | // wide char (Unicode) support | |
3470 | // ---------------------------------------------------------------------------- | |
3471 | ||
3472 | #ifdef TEST_WCHAR | |
3473 | ||
e84010cf GD |
3474 | #include "wx/strconv.h" |
3475 | #include "wx/fontenc.h" | |
3476 | #include "wx/encconv.h" | |
3477 | #include "wx/buffer.h" | |
f6bcfd97 | 3478 | |
ac511156 VZ |
3479 | static const char textInUtf8[] = |
3480 | { | |
3481 | 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176, | |
3482 | 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208, | |
3483 | 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188, | |
3484 | 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208, | |
3485 | 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181, | |
3486 | 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208, | |
3487 | 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0 | |
3488 | }; | |
3489 | ||
f6bcfd97 BP |
3490 | static void TestUtf8() |
3491 | { | |
3492 | puts("*** Testing UTF8 support ***\n"); | |
3493 | ||
24f25c8a VZ |
3494 | char buf[1024]; |
3495 | wchar_t wbuf[1024]; | |
3496 | if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 ) | |
f6bcfd97 | 3497 | { |
24f25c8a | 3498 | puts("ERROR: UTF-8 decoding failed."); |
f6bcfd97 | 3499 | } |
24f25c8a VZ |
3500 | else |
3501 | { | |
24f25c8a VZ |
3502 | wxCSConv conv(_T("koi8-r")); |
3503 | if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 ) | |
3504 | { | |
3505 | puts("ERROR: conversion to KOI8-R failed."); | |
3506 | } | |
3507 | else | |
ac511156 VZ |
3508 | { |
3509 | printf("The resulting string (in KOI8-R): %s\n", buf); | |
3510 | } | |
3511 | } | |
3512 | ||
3513 |