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