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