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