]>
Commit | Line | Data |
---|---|---|
37667812 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/console/console.cpp | |
3 | // Purpose: a sample console (as opposed to GUI) progam using wxWindows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 04.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
e87271f3 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
e84010cf | 20 | #include "wx/defs.h" |
b11a23f3 | 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 | |
41 | both as a test suite for various non-GUI wxWindows classes and as a | |
42 | scratchpad for quick tests. So there are two compilation modes: if you | |
43 | define TEST_ALL all tests are run, otherwise you may enable the individual | |
44 | tests individually in the "#else" branch below. | |
45 | */ | |
46 | ||
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 | |
53 | ||
31f6de22 VZ |
54 | #define TEST_CHARSET |
55 | #define TEST_CMDLINE | |
56 | #define TEST_DATETIME | |
57 | #define TEST_DIR | |
58 | #define TEST_DLLLOADER | |
59 | #define TEST_ENVIRON | |
60 | #define TEST_EXECUTE | |
61 | #define TEST_FILE | |
62 | #define TEST_FILECONF | |
63 | #define TEST_FILENAME | |
64 | #define TEST_FILETIME | |
8d4dc98f | 65 | // #define TEST_FTP --FIXME! (RN) |
31f6de22 | 66 | #define TEST_HASH |
0508ba2a | 67 | #define TEST_HASHMAP |
8142d704 | 68 | #define TEST_HASHSET |
31f6de22 VZ |
69 | #define TEST_INFO_FUNCTIONS |
70 | #define TEST_LIST | |
71 | #define TEST_LOCALE | |
72 | #define TEST_LOG | |
31f6de22 VZ |
73 | #define TEST_MIME |
74 | #define TEST_PATHLIST | |
8d5eff60 | 75 | #define TEST_ODBC |
7aeebdcd | 76 | #define TEST_PRINTF |
31f6de22 VZ |
77 | #define TEST_REGCONF |
78 | #define TEST_REGEX | |
79 | #define TEST_REGISTRY | |
24c8053b | 80 | #define TEST_SCOPEGUARD |
31f6de22 | 81 | #define TEST_SNGLINST |
8d4dc98f | 82 | // #define TEST_SOCKETS --FIXME! (RN) |
31f6de22 | 83 | #define TEST_STREAMS |
39937656 | 84 | #define TEST_TEXTSTREAM |
31f6de22 VZ |
85 | #define TEST_THREADS |
86 | #define TEST_TIMER | |
87 | // #define TEST_VCARD -- don't enable this (VZ) | |
8d4dc98f | 88 | // #define TEST_VOLUME --FIXME! (RN) |
31f6de22 VZ |
89 | #define TEST_WCHAR |
90 | #define TEST_ZIP | |
daa2c7d9 | 91 | |
e9d2bb6f DS |
92 | #else // #if TEST_ALL |
93 | ||
0aa29b6b | 94 | #define TEST_FILENAME |
daa2c7d9 | 95 | |
31f6de22 | 96 | #endif |
f6bcfd97 | 97 | |
daa2c7d9 VZ |
98 | // some tests are interactive, define this to run them |
99 | #ifdef TEST_INTERACTIVE | |
100 | #undef TEST_INTERACTIVE | |
101 | ||
e9d2bb6f | 102 | #define TEST_INTERACTIVE 1 |
daa2c7d9 | 103 | #else |
e9d2bb6f | 104 | #define TEST_INTERACTIVE 0 |
daa2c7d9 | 105 | #endif |
58b24a56 | 106 | |
f6bcfd97 BP |
107 | // ---------------------------------------------------------------------------- |
108 | // test class for container objects | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
cf1014a2 | 111 | #if defined(TEST_LIST) |
f6bcfd97 BP |
112 | |
113 | class Bar // Foo is already taken in the hash test | |
114 | { | |
115 | public: | |
116 | Bar(const wxString& name) : m_name(name) { ms_bars++; } | |
3dc01741 | 117 | Bar(const Bar& bar) : m_name(bar.m_name) { ms_bars++; } |
f6bcfd97 BP |
118 | ~Bar() { ms_bars--; } |
119 | ||
120 | static size_t GetNumber() { return ms_bars; } | |
121 | ||
456ae26d | 122 | const wxChar *GetName() const { return m_name; } |
f6bcfd97 BP |
123 | |
124 | private: | |
125 | wxString m_name; | |
126 | ||
127 | static size_t ms_bars; | |
128 | }; | |
129 | ||
130 | size_t Bar::ms_bars = 0; | |
131 | ||
cf1014a2 | 132 | #endif // defined(TEST_LIST) |
e87271f3 VZ |
133 | |
134 | // ============================================================================ | |
135 | // implementation | |
136 | // ============================================================================ | |
137 | ||
8e907a13 VZ |
138 | // ---------------------------------------------------------------------------- |
139 | // helper functions | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
1cd53e88 | 142 | #if defined(TEST_SOCKETS) |
8e907a13 VZ |
143 | |
144 | // replace TABs with \t and CRs with \n | |
145 | static wxString MakePrintable(const wxChar *s) | |
146 | { | |
147 | wxString str(s); | |
148 | (void)str.Replace(_T("\t"), _T("\\t")); | |
149 | (void)str.Replace(_T("\n"), _T("\\n")); | |
150 | (void)str.Replace(_T("\r"), _T("\\r")); | |
151 | ||
152 | return str; | |
153 | } | |
154 | ||
155 | #endif // MakePrintable() is used | |
156 | ||
551fe3a6 VZ |
157 | // ---------------------------------------------------------------------------- |
158 | // wxFontMapper::CharsetToEncoding | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | #ifdef TEST_CHARSET | |
162 | ||
e84010cf | 163 | #include "wx/fontmap.h" |
551fe3a6 VZ |
164 | |
165 | static void TestCharset() | |
166 | { | |
167 | static const wxChar *charsets[] = | |
168 | { | |
169 | // some vali charsets | |
170 | _T("us-ascii "), | |
171 | _T("iso8859-1 "), | |
172 | _T("iso-8859-12 "), | |
173 | _T("koi8-r "), | |
174 | _T("utf-7 "), | |
175 | _T("cp1250 "), | |
176 | _T("windows-1252"), | |
177 | ||
178 | // and now some bogus ones | |
179 | _T(" "), | |
180 | _T("cp1249 "), | |
181 | _T("iso--8859-1 "), | |
182 | _T("iso-8859-19 "), | |
183 | }; | |
184 | ||
185 | for ( size_t n = 0; n < WXSIZEOF(charsets); n++ ) | |
186 | { | |
142b3bc2 | 187 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charsets[n]); |
551fe3a6 VZ |
188 | wxPrintf(_T("Charset: %s\tEncoding: %s (%s)\n"), |
189 | charsets[n], | |
142b3bc2 VS |
190 | wxFontMapper::Get()->GetEncodingName(enc).c_str(), |
191 | wxFontMapper::Get()->GetEncodingDescription(enc).c_str()); | |
551fe3a6 VZ |
192 | } |
193 | } | |
194 | ||
195 | #endif // TEST_CHARSET | |
196 | ||
d34bce84 VZ |
197 | // ---------------------------------------------------------------------------- |
198 | // wxCmdLineParser | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
d31b7b68 VZ |
201 | #ifdef TEST_CMDLINE |
202 | ||
e84010cf GD |
203 | #include "wx/cmdline.h" |
204 | #include "wx/datetime.h" | |
d34bce84 | 205 | |
31f6de22 VZ |
206 | #if wxUSE_CMDLINE_PARSER |
207 | ||
d34bce84 VZ |
208 | static void ShowCmdLine(const wxCmdLineParser& parser) |
209 | { | |
456ae26d | 210 | wxString s = _T("Input files: "); |
d34bce84 VZ |
211 | |
212 | size_t count = parser.GetParamCount(); | |
213 | for ( size_t param = 0; param < count; param++ ) | |
214 | { | |
215 | s << parser.GetParam(param) << ' '; | |
216 | } | |
217 | ||
218 | s << '\n' | |
456ae26d VZ |
219 | << _T("Verbose:\t") << (parser.Found(_T("v")) ? _T("yes") : _T("no")) << '\n' |
220 | << _T("Quiet:\t") << (parser.Found(_T("q")) ? _T("yes") : _T("no")) << '\n'; | |
d34bce84 VZ |
221 | |
222 | wxString strVal; | |
223 | long lVal; | |
224 | wxDateTime dt; | |
456ae26d VZ |
225 | if ( parser.Found(_T("o"), &strVal) ) |
226 | s << _T("Output file:\t") << strVal << '\n'; | |
227 | if ( parser.Found(_T("i"), &strVal) ) | |
228 | s << _T("Input dir:\t") << strVal << '\n'; | |
229 | if ( parser.Found(_T("s"), &lVal) ) | |
230 | s << _T("Size:\t") << lVal << '\n'; | |
231 | if ( parser.Found(_T("d"), &dt) ) | |
232 | s << _T("Date:\t") << dt.FormatISODate() << '\n'; | |
233 | if ( parser.Found(_T("project_name"), &strVal) ) | |
234 | s << _T("Project:\t") << strVal << '\n'; | |
d34bce84 VZ |
235 | |
236 | wxLogMessage(s); | |
237 | } | |
238 | ||
31f6de22 VZ |
239 | #endif // wxUSE_CMDLINE_PARSER |
240 | ||
241 | static void TestCmdLineConvert() | |
242 | { | |
456ae26d | 243 | static const wxChar *cmdlines[] = |
31f6de22 | 244 | { |
456ae26d VZ |
245 | _T("arg1 arg2"), |
246 | _T("-a \"-bstring 1\" -c\"string 2\" \"string 3\""), | |
247 | _T("literal \\\" and \"\""), | |
31f6de22 VZ |
248 | }; |
249 | ||
250 | for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ ) | |
251 | { | |
456ae26d VZ |
252 | const wxChar *cmdline = cmdlines[n]; |
253 | wxPrintf(_T("Parsing: %s\n"), cmdline); | |
31f6de22 VZ |
254 | wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline); |
255 | ||
256 | size_t count = args.GetCount(); | |
456ae26d | 257 | wxPrintf(_T("\targc = %u\n"), count); |
31f6de22 VZ |
258 | for ( size_t arg = 0; arg < count; arg++ ) |
259 | { | |
456ae26d | 260 | wxPrintf(_T("\targv[%u] = %s\n"), arg, args[arg].c_str()); |
31f6de22 VZ |
261 | } |
262 | } | |
263 | } | |
264 | ||
d34bce84 VZ |
265 | #endif // TEST_CMDLINE |
266 | ||
1944c6bd VZ |
267 | // ---------------------------------------------------------------------------- |
268 | // wxDir | |
269 | // ---------------------------------------------------------------------------- | |
270 | ||
271 | #ifdef TEST_DIR | |
272 | ||
e84010cf | 273 | #include "wx/dir.h" |
1944c6bd | 274 | |
35332784 VZ |
275 | #ifdef __UNIX__ |
276 | static const wxChar *ROOTDIR = _T("/"); | |
2f0c19d0 | 277 | static const wxChar *TESTDIR = _T("/usr/local/share"); |
35332784 VZ |
278 | #elif defined(__WXMSW__) |
279 | static const wxChar *ROOTDIR = _T("c:\\"); | |
280 | static const wxChar *TESTDIR = _T("d:\\"); | |
281 | #else | |
282 | #error "don't know where the root directory is" | |
283 | #endif | |
284 | ||
1944c6bd VZ |
285 | static void TestDirEnumHelper(wxDir& dir, |
286 | int flags = wxDIR_DEFAULT, | |
287 | const wxString& filespec = wxEmptyString) | |
288 | { | |
289 | wxString filename; | |
290 | ||
291 | if ( !dir.IsOpened() ) | |
292 | return; | |
293 | ||
294 | bool cont = dir.GetFirst(&filename, filespec, flags); | |
295 | while ( cont ) | |
296 | { | |
456ae26d | 297 | wxPrintf(_T("\t%s\n"), filename.c_str()); |
1944c6bd VZ |
298 | |
299 | cont = dir.GetNext(&filename); | |
300 | } | |
301 | ||
e9d2bb6f | 302 | wxPuts(wxEmptyString); |
1944c6bd VZ |
303 | } |
304 | ||
305 | static void TestDirEnum() | |
306 | { | |
456ae26d | 307 | wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***")); |
35332784 | 308 | |
149147e1 | 309 | wxString cwd = wxGetCwd(); |
9475670f | 310 | if ( !wxDir::Exists(cwd) ) |
149147e1 | 311 | { |
456ae26d | 312 | wxPrintf(_T("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str()); |
149147e1 VZ |
313 | return; |
314 | } | |
315 | ||
ee3ef281 | 316 | wxDir dir(cwd); |
149147e1 VZ |
317 | if ( !dir.IsOpened() ) |
318 | { | |
456ae26d | 319 | wxPrintf(_T("ERROR: failed to open current directory '%s'.\n"), cwd.c_str()); |
149147e1 VZ |
320 | return; |
321 | } | |
1944c6bd | 322 | |
456ae26d | 323 | wxPuts(_T("Enumerating everything in current directory:")); |
1944c6bd VZ |
324 | TestDirEnumHelper(dir); |
325 | ||
456ae26d | 326 | wxPuts(_T("Enumerating really everything in current directory:")); |
1944c6bd VZ |
327 | TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT); |
328 | ||
456ae26d | 329 | wxPuts(_T("Enumerating object files in current directory:")); |
f2cb8a17 | 330 | TestDirEnumHelper(dir, wxDIR_DEFAULT, _T("*.o*")); |
1944c6bd | 331 | |
456ae26d | 332 | wxPuts(_T("Enumerating directories in current directory:")); |
1944c6bd VZ |
333 | TestDirEnumHelper(dir, wxDIR_DIRS); |
334 | ||
456ae26d | 335 | wxPuts(_T("Enumerating files in current directory:")); |
1944c6bd VZ |
336 | TestDirEnumHelper(dir, wxDIR_FILES); |
337 | ||
456ae26d | 338 | wxPuts(_T("Enumerating files including hidden in current directory:")); |
1944c6bd VZ |
339 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); |
340 | ||
35332784 | 341 | dir.Open(ROOTDIR); |
1944c6bd | 342 | |
456ae26d | 343 | wxPuts(_T("Enumerating everything in root directory:")); |
1944c6bd VZ |
344 | TestDirEnumHelper(dir, wxDIR_DEFAULT); |
345 | ||
456ae26d | 346 | wxPuts(_T("Enumerating directories in root directory:")); |
1944c6bd VZ |
347 | TestDirEnumHelper(dir, wxDIR_DIRS); |
348 | ||
456ae26d | 349 | wxPuts(_T("Enumerating files in root directory:")); |
1944c6bd VZ |
350 | TestDirEnumHelper(dir, wxDIR_FILES); |
351 | ||
456ae26d | 352 | wxPuts(_T("Enumerating files including hidden in root directory:")); |
1944c6bd VZ |
353 | TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN); |
354 | ||
456ae26d | 355 | wxPuts(_T("Enumerating files in non existing directory:")); |
f2cb8a17 | 356 | wxDir dirNo(_T("nosuchdir")); |
1944c6bd VZ |
357 | TestDirEnumHelper(dirNo); |
358 | } | |
359 | ||
35332784 VZ |
360 | class DirPrintTraverser : public wxDirTraverser |
361 | { | |
362 | public: | |
e9d2bb6f | 363 | virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename)) |
35332784 VZ |
364 | { |
365 | return wxDIR_CONTINUE; | |
366 | } | |
367 | ||
368 | virtual wxDirTraverseResult OnDir(const wxString& dirname) | |
369 | { | |
370 | wxString path, name, ext; | |
371 | wxSplitPath(dirname, &path, &name, &ext); | |
372 | ||
373 | if ( !ext.empty() ) | |
374 | name << _T('.') << ext; | |
375 | ||
376 | wxString indent; | |
377 | for ( const wxChar *p = path.c_str(); *p; p++ ) | |
378 | { | |
379 | if ( wxIsPathSeparator(*p) ) | |
380 | indent += _T(" "); | |
381 | } | |
382 | ||
456ae26d | 383 | wxPrintf(_T("%s%s\n"), indent.c_str(), name.c_str()); |
35332784 VZ |
384 | |
385 | return wxDIR_CONTINUE; | |
386 | } | |
387 | }; | |
388 | ||
389 | static void TestDirTraverse() | |
390 | { | |
456ae26d | 391 | wxPuts(_T("*** Testing wxDir::Traverse() ***")); |
35332784 VZ |
392 | |
393 | // enum all files | |
394 | wxArrayString files; | |
395 | size_t n = wxDir::GetAllFiles(TESTDIR, &files); | |
456ae26d | 396 | wxPrintf(_T("There are %u files under '%s'\n"), n, TESTDIR); |
35332784 VZ |
397 | if ( n > 1 ) |
398 | { | |
456ae26d VZ |
399 | wxPrintf(_T("First one is '%s'\n"), files[0u].c_str()); |
400 | wxPrintf(_T(" last one is '%s'\n"), files[n - 1].c_str()); | |
35332784 VZ |
401 | } |
402 | ||
403 | // enum again with custom traverser | |
2f0c19d0 | 404 | wxPuts(_T("Now enumerating directories:")); |
35332784 VZ |
405 | wxDir dir(TESTDIR); |
406 | DirPrintTraverser traverser; | |
e9d2bb6f | 407 | dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN); |
35332784 VZ |
408 | } |
409 | ||
149147e1 VZ |
410 | static void TestDirExists() |
411 | { | |
412 | wxPuts(_T("*** Testing wxDir::Exists() ***")); | |
413 | ||
456ae26d | 414 | static const wxChar *dirnames[] = |
149147e1 VZ |
415 | { |
416 | _T("."), | |
417 | #if defined(__WXMSW__) | |
418 | _T("c:"), | |
419 | _T("c:\\"), | |
420 | _T("\\\\share\\file"), | |
421 | _T("c:\\dos"), | |
422 | _T("c:\\dos\\"), | |
423 | _T("c:\\dos\\\\"), | |
424 | _T("c:\\autoexec.bat"), | |
425 | #elif defined(__UNIX__) | |
426 | _T("/"), | |
427 | _T("//"), | |
428 | _T("/usr/bin"), | |
429 | _T("/usr//bin"), | |
430 | _T("/usr///bin"), | |
431 | #endif | |
432 | }; | |
433 | ||
434 | for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ ) | |
435 | { | |
456ae26d VZ |
436 | wxPrintf(_T("%-40s: %s\n"), |
437 | dirnames[n], | |
438 | wxDir::Exists(dirnames[n]) ? _T("exists") | |
439 | : _T("doesn't exist")); | |
149147e1 VZ |
440 | } |
441 | } | |
442 | ||
1944c6bd VZ |
443 | #endif // TEST_DIR |
444 | ||
f6bcfd97 BP |
445 | // ---------------------------------------------------------------------------- |
446 | // wxDllLoader | |
447 | // ---------------------------------------------------------------------------- | |
448 | ||
449 | #ifdef TEST_DLLLOADER | |
450 | ||
e84010cf | 451 | #include "wx/dynlib.h" |
f6bcfd97 BP |
452 | |
453 | static void TestDllLoad() | |
454 | { | |
455 | #if defined(__WXMSW__) | |
456 | static const wxChar *LIB_NAME = _T("kernel32.dll"); | |
457 | static const wxChar *FUNC_NAME = _T("lstrlenA"); | |
458 | #elif defined(__UNIX__) | |
459 | // weird: using just libc.so does *not* work! | |
460 | static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so"); | |
461 | static const wxChar *FUNC_NAME = _T("strlen"); | |
462 | #else | |
463 | #error "don't know how to test wxDllLoader on this platform" | |
464 | #endif | |
465 | ||
456ae26d | 466 | wxPuts(_T("*** testing wxDllLoader ***\n")); |
f6bcfd97 | 467 | |
456ae26d VZ |
468 | wxDynamicLibrary lib(LIB_NAME); |
469 | if ( !lib.IsLoaded() ) | |
f6bcfd97 BP |
470 | { |
471 | wxPrintf(_T("ERROR: failed to load '%s'.\n"), LIB_NAME); | |
472 | } | |
473 | else | |
474 | { | |
456ae26d VZ |
475 | typedef int (*wxStrlenType)(const char *); |
476 | wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME); | |
f6bcfd97 BP |
477 | if ( !pfnStrlen ) |
478 | { | |
479 | wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"), | |
480 | FUNC_NAME, LIB_NAME); | |
481 | } | |
482 | else | |
483 | { | |
484 | if ( pfnStrlen("foo") != 3 ) | |
485 | { | |
456ae26d | 486 | wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n")); |
f6bcfd97 BP |
487 | } |
488 | else | |
489 | { | |
456ae26d | 490 | wxPuts(_T("... ok")); |
f6bcfd97 BP |
491 | } |
492 | } | |
f6bcfd97 BP |
493 | } |
494 | } | |
495 | ||
496 | #endif // TEST_DLLLOADER | |
497 | ||
8fd0d89b VZ |
498 | // ---------------------------------------------------------------------------- |
499 | // wxGet/SetEnv | |
500 | // ---------------------------------------------------------------------------- | |
501 | ||
502 | #ifdef TEST_ENVIRON | |
503 | ||
e84010cf | 504 | #include "wx/utils.h" |
8fd0d89b | 505 | |
308978f6 VZ |
506 | static wxString MyGetEnv(const wxString& var) |
507 | { | |
508 | wxString val; | |
509 | if ( !wxGetEnv(var, &val) ) | |
510 | val = _T("<empty>"); | |
511 | else | |
512 | val = wxString(_T('\'')) + val + _T('\''); | |
513 | ||
514 | return val; | |
515 | } | |
516 | ||
8fd0d89b VZ |
517 | static void TestEnvironment() |
518 | { | |
519 | const wxChar *var = _T("wxTestVar"); | |
520 | ||
456ae26d | 521 | wxPuts(_T("*** testing environment access functions ***")); |
8fd0d89b | 522 | |
456ae26d | 523 | wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); |
8fd0d89b | 524 | wxSetEnv(var, _T("value for wxTestVar")); |
456ae26d | 525 | wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); |
8fd0d89b | 526 | wxSetEnv(var, _T("another value")); |
456ae26d | 527 | wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); |
8fd0d89b | 528 | wxUnsetEnv(var); |
456ae26d VZ |
529 | wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); |
530 | wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str()); | |
8fd0d89b VZ |
531 | } |
532 | ||
533 | #endif // TEST_ENVIRON | |
534 | ||
d93c719a VZ |
535 | // ---------------------------------------------------------------------------- |
536 | // wxExecute | |
537 | // ---------------------------------------------------------------------------- | |
538 | ||
539 | #ifdef TEST_EXECUTE | |
540 | ||
e84010cf | 541 | #include "wx/utils.h" |
d93c719a VZ |
542 | |
543 | static void TestExecute() | |
544 | { | |
456ae26d | 545 | wxPuts(_T("*** testing wxExecute ***")); |
d93c719a VZ |
546 | |
547 | #ifdef __UNIX__ | |
a1f79c1e | 548 | #define COMMAND "cat -n ../../Makefile" // "echo hi" |
2c8e4738 | 549 | #define SHELL_COMMAND "echo hi from shell" |
a1f79c1e | 550 | #define REDIRECT_COMMAND COMMAND // "date" |
d93c719a | 551 | #elif defined(__WXMSW__) |
50ded68d | 552 | #define COMMAND "command.com /c echo hi" |
2c8e4738 VZ |
553 | #define SHELL_COMMAND "echo hi" |
554 | #define REDIRECT_COMMAND COMMAND | |
d93c719a VZ |
555 | #else |
556 | #error "no command to exec" | |
557 | #endif // OS | |
558 | ||
456ae26d | 559 | wxPrintf(_T("Testing wxShell: ")); |
2c8e4738 | 560 | fflush(stdout); |
f2cb8a17 | 561 | if ( wxShell(_T(SHELL_COMMAND)) ) |
456ae26d | 562 | wxPuts(_T("Ok.")); |
d93c719a | 563 | else |
456ae26d | 564 | wxPuts(_T("ERROR.")); |
2c8e4738 | 565 | |
456ae26d | 566 | wxPrintf(_T("Testing wxExecute: ")); |
2c8e4738 | 567 | fflush(stdout); |
f2cb8a17 | 568 | if ( wxExecute(_T(COMMAND), true /* sync */) == 0 ) |
456ae26d | 569 | wxPuts(_T("Ok.")); |
2c8e4738 | 570 | else |
456ae26d | 571 | wxPuts(_T("ERROR.")); |
2c8e4738 VZ |
572 | |
573 | #if 0 // no, it doesn't work (yet?) | |
456ae26d | 574 | wxPrintf(_T("Testing async wxExecute: ")); |
2c8e4738 VZ |
575 | fflush(stdout); |
576 | if ( wxExecute(COMMAND) != 0 ) | |
456ae26d | 577 | wxPuts(_T("Ok (command launched).")); |
2c8e4738 | 578 | else |
456ae26d | 579 | wxPuts(_T("ERROR.")); |
2c8e4738 VZ |
580 | #endif // 0 |
581 | ||
456ae26d | 582 | wxPrintf(_T("Testing wxExecute with redirection:\n")); |
2c8e4738 | 583 | wxArrayString output; |
f2cb8a17 | 584 | if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 ) |
2c8e4738 | 585 | { |
456ae26d | 586 | wxPuts(_T("ERROR.")); |
2c8e4738 VZ |
587 | } |
588 | else | |
589 | { | |
590 | size_t count = output.GetCount(); | |
591 | for ( size_t n = 0; n < count; n++ ) | |
592 | { | |
456ae26d | 593 | wxPrintf(_T("\t%s\n"), output[n].c_str()); |
2c8e4738 VZ |
594 | } |
595 | ||
456ae26d | 596 | wxPuts(_T("Ok.")); |
2c8e4738 | 597 | } |
d93c719a VZ |
598 | } |
599 | ||
600 | #endif // TEST_EXECUTE | |
601 | ||
f6bcfd97 BP |
602 | // ---------------------------------------------------------------------------- |
603 | // file | |
604 | // ---------------------------------------------------------------------------- | |
605 | ||
606 | #ifdef TEST_FILE | |
607 | ||
e84010cf GD |
608 | #include "wx/file.h" |
609 | #include "wx/ffile.h" | |
610 | #include "wx/textfile.h" | |
f6bcfd97 BP |
611 | |
612 | static void TestFileRead() | |
613 | { | |
456ae26d | 614 | wxPuts(_T("*** wxFile read test ***")); |
f6bcfd97 BP |
615 | |
616 | wxFile file(_T("testdata.fc")); | |
617 | if ( file.IsOpened() ) | |
618 | { | |
456ae26d | 619 | wxPrintf(_T("File length: %lu\n"), file.Length()); |
f6bcfd97 | 620 | |
456ae26d | 621 | wxPuts(_T("File dump:\n----------")); |
f6bcfd97 | 622 | |
3ca6a5f0 | 623 | static const off_t len = 1024; |
456ae26d | 624 | wxChar buf[len]; |
f6bcfd97 BP |
625 | for ( ;; ) |
626 | { | |
627 | off_t nRead = file.Read(buf, len); | |
628 | if ( nRead == wxInvalidOffset ) | |
629 | { | |
456ae26d | 630 | wxPrintf(_T("Failed to read the file.")); |
f6bcfd97 BP |
631 | break; |
632 | } | |
633 | ||
634 | fwrite(buf, nRead, 1, stdout); | |
635 | ||
636 | if ( nRead < len ) | |
637 | break; | |
638 | } | |
639 | ||
456ae26d | 640 | wxPuts(_T("----------")); |
f6bcfd97 BP |
641 | } |
642 | else | |
643 | { | |
456ae26d | 644 | wxPrintf(_T("ERROR: can't open test file.\n")); |
f6bcfd97 BP |
645 | } |
646 | ||
e9d2bb6f | 647 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
648 | } |
649 | ||
650 | static void TestTextFileRead() | |
651 | { | |
456ae26d | 652 | wxPuts(_T("*** wxTextFile read test ***")); |
f6bcfd97 BP |
653 | |
654 | wxTextFile file(_T("testdata.fc")); | |
655 | if ( file.Open() ) | |
656 | { | |
456ae26d VZ |
657 | wxPrintf(_T("Number of lines: %u\n"), file.GetLineCount()); |
658 | wxPrintf(_T("Last line: '%s'\n"), file.GetLastLine().c_str()); | |
3ca6a5f0 BP |
659 | |
660 | wxString s; | |
661 | ||
456ae26d | 662 | wxPuts(_T("\nDumping the entire file:")); |
3ca6a5f0 BP |
663 | for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() ) |
664 | { | |
456ae26d | 665 | wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str()); |
3ca6a5f0 | 666 | } |
456ae26d | 667 | wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str()); |
3ca6a5f0 | 668 | |
456ae26d | 669 | wxPuts(_T("\nAnd now backwards:")); |
3ca6a5f0 BP |
670 | for ( s = file.GetLastLine(); |
671 | file.GetCurrentLine() != 0; | |
672 | s = file.GetPrevLine() ) | |
673 | { | |
456ae26d | 674 | wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str()); |
3ca6a5f0 | 675 | } |
456ae26d | 676 | wxPrintf(_T("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str()); |
f6bcfd97 BP |
677 | } |
678 | else | |
679 | { | |
456ae26d | 680 | wxPrintf(_T("ERROR: can't open '%s'\n"), file.GetName()); |
f6bcfd97 BP |
681 | } |
682 | ||
e9d2bb6f | 683 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
684 | } |
685 | ||
a339970a VZ |
686 | static void TestFileCopy() |
687 | { | |
456ae26d | 688 | wxPuts(_T("*** Testing wxCopyFile ***")); |
a339970a VZ |
689 | |
690 | static const wxChar *filename1 = _T("testdata.fc"); | |
691 | static const wxChar *filename2 = _T("test2"); | |
692 | if ( !wxCopyFile(filename1, filename2) ) | |
693 | { | |
456ae26d | 694 | wxPuts(_T("ERROR: failed to copy file")); |
a339970a VZ |
695 | } |
696 | else | |
697 | { | |
f2cb8a17 JS |
698 | wxFFile f1(filename1, _T("rb")), |
699 | f2(filename2, _T("rb")); | |
a339970a VZ |
700 | |
701 | if ( !f1.IsOpened() || !f2.IsOpened() ) | |
702 | { | |
456ae26d | 703 | wxPuts(_T("ERROR: failed to open file(s)")); |
a339970a VZ |
704 | } |
705 | else | |
706 | { | |
707 | wxString s1, s2; | |
708 | if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) ) | |
709 | { | |
456ae26d | 710 | wxPuts(_T("ERROR: failed to read file(s)")); |
a339970a VZ |
711 | } |
712 | else | |
713 | { | |
714 | if ( (s1.length() != s2.length()) || | |
715 | (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) ) | |
716 | { | |
456ae26d | 717 | wxPuts(_T("ERROR: copy error!")); |
a339970a VZ |
718 | } |
719 | else | |
720 | { | |
456ae26d | 721 | wxPuts(_T("File was copied ok.")); |
a339970a VZ |
722 | } |
723 | } | |
724 | } | |
725 | } | |
726 | ||
727 | if ( !wxRemoveFile(filename2) ) | |
728 | { | |
456ae26d | 729 | wxPuts(_T("ERROR: failed to remove the file")); |
a339970a VZ |
730 | } |
731 | ||
e9d2bb6f | 732 | wxPuts(wxEmptyString); |
a339970a VZ |
733 | } |
734 | ||
f6bcfd97 BP |
735 | #endif // TEST_FILE |
736 | ||
ee6e1b1d VZ |
737 | // ---------------------------------------------------------------------------- |
738 | // wxFileConfig | |
739 | // ---------------------------------------------------------------------------- | |
740 | ||
741 | #ifdef TEST_FILECONF | |
742 | ||
e84010cf GD |
743 | #include "wx/confbase.h" |
744 | #include "wx/fileconf.h" | |
ee6e1b1d VZ |
745 | |
746 | static const struct FileConfTestData | |
747 | { | |
748 | const wxChar *name; // value name | |
749 | const wxChar *value; // the value from the file | |
750 | } fcTestData[] = | |
751 | { | |
752 | { _T("value1"), _T("one") }, | |
753 | { _T("value2"), _T("two") }, | |
754 | { _T("novalue"), _T("default") }, | |
755 | }; | |
756 | ||
757 | static void TestFileConfRead() | |
758 | { | |
456ae26d | 759 | wxPuts(_T("*** testing wxFileConfig loading/reading ***")); |
ee6e1b1d VZ |
760 | |
761 | wxFileConfig fileconf(_T("test"), wxEmptyString, | |
762 | _T("testdata.fc"), wxEmptyString, | |
763 | wxCONFIG_USE_RELATIVE_PATH); | |
764 | ||
765 | // test simple reading | |
456ae26d | 766 | wxPuts(_T("\nReading config file:")); |
ee6e1b1d VZ |
767 | wxString defValue(_T("default")), value; |
768 | for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ ) | |
769 | { | |
770 | const FileConfTestData& data = fcTestData[n]; | |
771 | value = fileconf.Read(data.name, defValue); | |
456ae26d | 772 | wxPrintf(_T("\t%s = %s "), data.name, value.c_str()); |
ee6e1b1d VZ |
773 | if ( value == data.value ) |
774 | { | |
456ae26d | 775 | wxPuts(_T("(ok)")); |
ee6e1b1d VZ |
776 | } |
777 | else | |
778 | { | |
456ae26d | 779 | wxPrintf(_T("(ERROR: should be %s)\n"), data.value); |
ee6e1b1d VZ |
780 | } |
781 | } | |
782 | ||
783 | // test enumerating the entries | |
456ae26d | 784 | wxPuts(_T("\nEnumerating all root entries:")); |
ee6e1b1d VZ |
785 | long dummy; |
786 | wxString name; | |
787 | bool cont = fileconf.GetFirstEntry(name, dummy); | |
788 | while ( cont ) | |
789 | { | |
456ae26d | 790 | wxPrintf(_T("\t%s = %s\n"), |
ee6e1b1d VZ |
791 | name.c_str(), |
792 | fileconf.Read(name.c_str(), _T("ERROR")).c_str()); | |
793 | ||
794 | cont = fileconf.GetNextEntry(name, dummy); | |
795 | } | |
7e0777da VZ |
796 | |
797 | static const wxChar *testEntry = _T("TestEntry"); | |
798 | wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: ")); | |
799 | fileconf.Write(testEntry, _T("A value")); | |
800 | fileconf.DeleteEntry(testEntry); | |
801 | wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n")); | |
ee6e1b1d VZ |
802 | } |
803 | ||
804 | #endif // TEST_FILECONF | |
805 | ||
844f90fb VZ |
806 | // ---------------------------------------------------------------------------- |
807 | // wxFileName | |
808 | // ---------------------------------------------------------------------------- | |
809 | ||
810 | #ifdef TEST_FILENAME | |
811 | ||
e84010cf | 812 | #include "wx/filename.h" |
844f90fb | 813 | |
e9d2bb6f | 814 | #if 0 |
5bc1deeb | 815 | static void DumpFileName(const wxChar *desc, const wxFileName& fn) |
81f25632 | 816 | { |
5bc1deeb VZ |
817 | wxPuts(desc); |
818 | ||
81f25632 VZ |
819 | wxString full = fn.GetFullPath(); |
820 | ||
821 | wxString vol, path, name, ext; | |
822 | wxFileName::SplitPath(full, &vol, &path, &name, &ext); | |
823 | ||
a5b7374f | 824 | wxPrintf(_T("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"), |
81f25632 | 825 | full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str()); |
a5b7374f VZ |
826 | |
827 | wxFileName::SplitPath(full, &path, &name, &ext); | |
828 | wxPrintf(_T("or\t\t-> path '%s', name '%s', ext '%s'\n"), | |
829 | path.c_str(), name.c_str(), ext.c_str()); | |
830 | ||
831 | wxPrintf(_T("path is also:\t'%s'\n"), fn.GetPath().c_str()); | |
832 | wxPrintf(_T("with volume: \t'%s'\n"), | |
833 | fn.GetPath(wxPATH_GET_VOLUME).c_str()); | |
834 | wxPrintf(_T("with separator:\t'%s'\n"), | |
835 | fn.GetPath(wxPATH_GET_SEPARATOR).c_str()); | |
836 | wxPrintf(_T("with both: \t'%s'\n"), | |
837 | fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str()); | |
9cb47ea2 VZ |
838 | |
839 | wxPuts(_T("The directories in the path are:")); | |
840 | wxArrayString dirs = fn.GetDirs(); | |
841 | size_t count = dirs.GetCount(); | |
842 | for ( size_t n = 0; n < count; n++ ) | |
843 | { | |
844 | wxPrintf(_T("\t%u: %s\n"), n, dirs[n].c_str()); | |
845 | } | |
81f25632 | 846 | } |
e9d2bb6f | 847 | #endif |
81f25632 | 848 | |
8e7dda21 | 849 | static struct FileNameInfo |
42b1f941 | 850 | { |
8e7dda21 | 851 | const wxChar *fullname; |
a874db92 | 852 | const wxChar *volume; |
8e7dda21 VZ |
853 | const wxChar *path; |
854 | const wxChar *name; | |
855 | const wxChar *ext; | |
a874db92 VZ |
856 | bool isAbsolute; |
857 | wxPathFormat format; | |
8e7dda21 VZ |
858 | } filenames[] = |
859 | { | |
a874db92 | 860 | // Unix file names |
cab8f76e VZ |
861 | { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX }, |
862 | { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX }, | |
863 | { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX }, | |
864 | { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX }, | |
865 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX }, | |
866 | { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX }, | |
867 | { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX }, | |
868 | { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX }, | |
869 | { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX }, | |
a874db92 VZ |
870 | |
871 | // Windows file names | |
cab8f76e VZ |
872 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS }, |
873 | { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS }, | |
874 | { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS }, | |
875 | { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
876 | { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS }, | |
877 | { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
878 | { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
a874db92 | 879 | |
2db991f4 | 880 | // wxFileName support for Mac file names is broken currently |
a2fa5040 | 881 | #if 0 |
a874db92 | 882 | // Mac file names |
cab8f76e VZ |
883 | { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC }, |
884 | { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC }, | |
885 | { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC }, | |
886 | { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC }, | |
887 | { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
888 | { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
a2fa5040 | 889 | #endif // 0 |
a874db92 VZ |
890 | |
891 | // VMS file names | |
cab8f76e VZ |
892 | { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS }, |
893 | { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS }, | |
42b1f941 VZ |
894 | }; |
895 | ||
844f90fb VZ |
896 | static void TestFileNameConstruction() |
897 | { | |
456ae26d | 898 | wxPuts(_T("*** testing wxFileName construction ***")); |
844f90fb | 899 | |
844f90fb VZ |
900 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) |
901 | { | |
a874db92 VZ |
902 | const FileNameInfo& fni = filenames[n]; |
903 | ||
904 | wxFileName fn(fni.fullname, fni.format); | |
905 | ||
906 | wxString fullname = fn.GetFullPath(fni.format); | |
907 | if ( fullname != fni.fullname ) | |
908 | { | |
456ae26d | 909 | wxPrintf(_T("ERROR: fullname should be '%s'\n"), fni.fullname); |
a874db92 | 910 | } |
844f90fb | 911 | |
a2fa5040 | 912 | bool isAbsolute = fn.IsAbsolute(fni.format); |
456ae26d | 913 | wxPrintf(_T("'%s' is %s (%s)\n\t"), |
a874db92 VZ |
914 | fullname.c_str(), |
915 | isAbsolute ? "absolute" : "relative", | |
916 | isAbsolute == fni.isAbsolute ? "ok" : "ERROR"); | |
917 | ||
e9d2bb6f | 918 | if ( !fn.Normalize(wxPATH_NORM_ALL, wxEmptyString, fni.format) ) |
844f90fb | 919 | { |
456ae26d | 920 | wxPuts(_T("ERROR (couldn't be normalized)")); |
844f90fb VZ |
921 | } |
922 | else | |
923 | { | |
456ae26d | 924 | wxPrintf(_T("normalized: '%s'\n"), fn.GetFullPath(fni.format).c_str()); |
844f90fb VZ |
925 | } |
926 | } | |
927 | ||
e9d2bb6f | 928 | wxPuts(wxEmptyString); |
844f90fb VZ |
929 | } |
930 | ||
42b1f941 VZ |
931 | static void TestFileNameSplit() |
932 | { | |
456ae26d | 933 | wxPuts(_T("*** testing wxFileName splitting ***")); |
42b1f941 VZ |
934 | |
935 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
936 | { | |
a874db92 VZ |
937 | const FileNameInfo& fni = filenames[n]; |
938 | wxString volume, path, name, ext; | |
939 | wxFileName::SplitPath(fni.fullname, | |
940 | &volume, &path, &name, &ext, fni.format); | |
941 | ||
456ae26d | 942 | wxPrintf(_T("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'"), |
a874db92 VZ |
943 | fni.fullname, |
944 | volume.c_str(), path.c_str(), name.c_str(), ext.c_str()); | |
8e7dda21 | 945 | |
a874db92 | 946 | if ( volume != fni.volume ) |
456ae26d | 947 | wxPrintf(_T(" (ERROR: volume = '%s')"), fni.volume); |
8e7dda21 | 948 | if ( path != fni.path ) |
456ae26d | 949 | wxPrintf(_T(" (ERROR: path = '%s')"), fni.path); |
8e7dda21 | 950 | if ( name != fni.name ) |
456ae26d | 951 | wxPrintf(_T(" (ERROR: name = '%s')"), fni.name); |
8e7dda21 | 952 | if ( ext != fni.ext ) |
456ae26d | 953 | wxPrintf(_T(" (ERROR: ext = '%s')"), fni.ext); |
a874db92 | 954 | |
e9d2bb6f | 955 | wxPuts(wxEmptyString); |
42b1f941 | 956 | } |
42b1f941 VZ |
957 | } |
958 | ||
ade35f11 VZ |
959 | static void TestFileNameTemp() |
960 | { | |
456ae26d | 961 | wxPuts(_T("*** testing wxFileName temp file creation ***")); |
ade35f11 | 962 | |
456ae26d | 963 | static const wxChar *tmpprefixes[] = |
ade35f11 | 964 | { |
456ae26d VZ |
965 | _T(""), |
966 | _T("foo"), | |
967 | _T(".."), | |
968 | _T("../bar"), | |
a2fa5040 | 969 | #ifdef __UNIX__ |
456ae26d VZ |
970 | _T("/tmp/foo"), |
971 | _T("/tmp/foo/bar"), // this one must be an error | |
a2fa5040 | 972 | #endif // __UNIX__ |
ade35f11 VZ |
973 | }; |
974 | ||
975 | for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ ) | |
976 | { | |
977 | wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]); | |
2db991f4 VZ |
978 | if ( path.empty() ) |
979 | { | |
980 | // "error" is not in upper case because it may be ok | |
456ae26d | 981 | wxPrintf(_T("Prefix '%s'\t-> error\n"), tmpprefixes[n]); |
2db991f4 VZ |
982 | } |
983 | else | |
ade35f11 | 984 | { |
456ae26d | 985 | wxPrintf(_T("Prefix '%s'\t-> temp file '%s'\n"), |
ade35f11 VZ |
986 | tmpprefixes[n], path.c_str()); |
987 | ||
988 | if ( !wxRemoveFile(path) ) | |
989 | { | |
456ae26d VZ |
990 | wxLogWarning(_T("Failed to remove temp file '%s'"), |
991 | path.c_str()); | |
ade35f11 VZ |
992 | } |
993 | } | |
994 | } | |
995 | } | |
996 | ||
f7d886af VZ |
997 | static void TestFileNameMakeRelative() |
998 | { | |
456ae26d | 999 | wxPuts(_T("*** testing wxFileName::MakeRelativeTo() ***")); |
f7d886af VZ |
1000 | |
1001 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
1002 | { | |
1003 | const FileNameInfo& fni = filenames[n]; | |
1004 | ||
1005 | wxFileName fn(fni.fullname, fni.format); | |
1006 | ||
1007 | // choose the base dir of the same format | |
1008 | wxString base; | |
1009 | switch ( fni.format ) | |
1010 | { | |
1011 | case wxPATH_UNIX: | |
f2cb8a17 | 1012 | base = _T("/usr/bin/"); |
f7d886af VZ |
1013 | break; |
1014 | ||
1015 | case wxPATH_DOS: | |
f2cb8a17 | 1016 | base = _T("c:\\"); |
f7d886af VZ |
1017 | break; |
1018 | ||
1019 | case wxPATH_MAC: | |
1020 | case wxPATH_VMS: | |
1021 | // TODO: I don't know how this is supposed to work there | |
1022 | continue; | |
daa2c7d9 VZ |
1023 | |
1024 | case wxPATH_NATIVE: // make gcc happy | |
1025 | default: | |
f2cb8a17 | 1026 | wxFAIL_MSG( _T("unexpected path format") ); |
f7d886af VZ |
1027 | } |
1028 | ||
456ae26d | 1029 | wxPrintf(_T("'%s' relative to '%s': "), |
f7d886af VZ |
1030 | fn.GetFullPath(fni.format).c_str(), base.c_str()); |
1031 | ||
1032 | if ( !fn.MakeRelativeTo(base, fni.format) ) | |
1033 | { | |
456ae26d | 1034 | wxPuts(_T("unchanged")); |
f7d886af VZ |
1035 | } |
1036 | else | |
1037 | { | |
456ae26d | 1038 | wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str()); |
f7d886af VZ |
1039 | } |
1040 | } | |
1041 | } | |
1042 | ||
e7266247 VS |
1043 | static void TestFileNameMakeAbsolute() |
1044 | { | |
1045 | wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***")); | |
1046 | ||
1047 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
1048 | { | |
1049 | const FileNameInfo& fni = filenames[n]; | |
1050 | wxFileName fn(fni.fullname, fni.format); | |
2ab25aca | 1051 | |
e7266247 VS |
1052 | wxPrintf(_T("'%s' absolutized: "), |
1053 | fn.GetFullPath(fni.format).c_str()); | |
1054 | fn.MakeAbsolute(); | |
1055 | wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str()); | |
1056 | } | |
1057 | ||
e9d2bb6f | 1058 | wxPuts(wxEmptyString); |
e7266247 VS |
1059 | } |
1060 | ||
0aa29b6b VZ |
1061 | static void TestFileNameDirManip() |
1062 | { | |
1063 | // TODO: test AppendDir(), RemoveDir(), ... | |
1064 | } | |
1065 | ||
844f90fb VZ |
1066 | static void TestFileNameComparison() |
1067 | { | |
1068 | // TODO! | |
1069 | } | |
1070 | ||
1071 | static void TestFileNameOperations() | |
1072 | { | |
1073 | // TODO! | |
1074 | } | |
1075 | ||
1076 | static void TestFileNameCwd() | |
1077 | { | |
1078 | // TODO! | |
1079 | } | |
1080 | ||
1081 | #endif // TEST_FILENAME | |
1082 | ||
d56e2b97 VZ |
1083 | // ---------------------------------------------------------------------------- |
1084 | // wxFileName time functions | |
1085 | // ---------------------------------------------------------------------------- | |
1086 | ||
1087 | #ifdef TEST_FILETIME | |
1088 | ||
1089 | #include <wx/filename.h> | |
1090 | #include <wx/datetime.h> | |
1091 | ||
1092 | static void TestFileGetTimes() | |
1093 | { | |
1094 | wxFileName fn(_T("testdata.fc")); | |
1095 | ||
6dbb903b VZ |
1096 | wxDateTime dtAccess, dtMod, dtCreate; |
1097 | if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) ) | |
d56e2b97 VZ |
1098 | { |
1099 | wxPrintf(_T("ERROR: GetTimes() failed.\n")); | |
1100 | } | |
1101 | else | |
1102 | { | |
1103 | static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S"); | |
1104 | ||
1105 | wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str()); | |
6dbb903b VZ |
1106 | wxPrintf(_T("Creation: \t%s\n"), dtCreate.Format(fmt).c_str()); |
1107 | wxPrintf(_T("Last read: \t%s\n"), dtAccess.Format(fmt).c_str()); | |
1108 | wxPrintf(_T("Last write: \t%s\n"), dtMod.Format(fmt).c_str()); | |
d56e2b97 VZ |
1109 | } |
1110 | } | |
1111 | ||
e9d2bb6f | 1112 | #if 0 |
d56e2b97 VZ |
1113 | static void TestFileSetTimes() |
1114 | { | |
1115 | wxFileName fn(_T("testdata.fc")); | |
1116 | ||
d56e2b97 VZ |
1117 | if ( !fn.Touch() ) |
1118 | { | |
1119 | wxPrintf(_T("ERROR: Touch() failed.\n")); | |
1120 | } | |
1121 | } | |
e9d2bb6f | 1122 | #endif |
d56e2b97 VZ |
1123 | |
1124 | #endif // TEST_FILETIME | |
1125 | ||
2c8e4738 VZ |
1126 | // ---------------------------------------------------------------------------- |
1127 | // wxHashTable | |
1128 | // ---------------------------------------------------------------------------- | |
1129 | ||
1130 | #ifdef TEST_HASH | |
1131 | ||
e84010cf | 1132 | #include "wx/hash.h" |
2c8e4738 VZ |
1133 | |
1134 | struct Foo | |
1135 | { | |
1136 | Foo(int n_) { n = n_; count++; } | |
1137 | ~Foo() { count--; } | |
1138 | ||
1139 | int n; | |
1140 | ||
1141 | static size_t count; | |
1142 | }; | |
1143 | ||
1144 | size_t Foo::count = 0; | |
1145 | ||
1146 | WX_DECLARE_LIST(Foo, wxListFoos); | |
1147 | WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos); | |
1148 | ||
e84010cf | 1149 | #include "wx/listimpl.cpp" |
2c8e4738 VZ |
1150 | |
1151 | WX_DEFINE_LIST(wxListFoos); | |
1152 | ||
1a6d9c76 MB |
1153 | #include "wx/timer.h" |
1154 | ||
2c8e4738 VZ |
1155 | static void TestHash() |
1156 | { | |
456ae26d | 1157 | wxPuts(_T("*** Testing wxHashTable ***\n")); |
1a6d9c76 MB |
1158 | const int COUNT = 100; |
1159 | ||
1160 | wxStopWatch sw; | |
1161 | ||
1162 | sw.Start(); | |
2c8e4738 | 1163 | |
df5168c4 | 1164 | { |
8ae94823 | 1165 | wxHashTable hash(wxKEY_INTEGER, 10), hash2(wxKEY_STRING); |
760d3c7c | 1166 | wxObject o; |
df5168c4 MB |
1167 | int i; |
1168 | ||
1a6d9c76 | 1169 | for ( i = 0; i < COUNT; ++i ) |
760d3c7c | 1170 | hash.Put(i, &o + i); |
df5168c4 MB |
1171 | |
1172 | hash.BeginFind(); | |
1173 | wxHashTable::compatibility_iterator it = hash.Next(); | |
1174 | i = 0; | |
1175 | ||
1176 | while (it) | |
1177 | { | |
1178 | ++i; | |
1179 | it = hash.Next(); | |
1180 | } | |
1181 | ||
1a6d9c76 | 1182 | if (i != COUNT) |
df5168c4 MB |
1183 | wxPuts(_T("Error in wxHashTable::compatibility_iterator\n")); |
1184 | ||
1185 | for ( i = 99; i >= 0; --i ) | |
760d3c7c | 1186 | if( hash.Get(i) != &o + i ) |
df5168c4 MB |
1187 | wxPuts(_T("Error in wxHashTable::Get/Put\n")); |
1188 | ||
1a6d9c76 | 1189 | for ( i = 0; i < COUNT; ++i ) |
760d3c7c MB |
1190 | hash.Put(i, &o + i + 20); |
1191 | ||
1192 | for ( i = 99; i >= 0; --i ) | |
1193 | if( hash.Get(i) != &o + i) | |
1194 | wxPuts(_T("Error (2) in wxHashTable::Get/Put\n")); | |
1195 | ||
1a6d9c76 | 1196 | for ( i = 0; i < COUNT/2; ++i ) |
760d3c7c MB |
1197 | if( hash.Delete(i) != &o + i) |
1198 | wxPuts(_T("Error in wxHashTable::Delete\n")); | |
1199 | ||
1a6d9c76 | 1200 | for ( i = COUNT/2; i < COUNT; ++i ) |
760d3c7c MB |
1201 | if( hash.Get(i) != &o + i) |
1202 | wxPuts(_T("Error (3) in wxHashTable::Get/Put\n")); | |
1203 | ||
1a6d9c76 | 1204 | for ( i = 0; i < COUNT/2; ++i ) |
760d3c7c MB |
1205 | if( hash.Get(i) != &o + i + 20) |
1206 | wxPuts(_T("Error (4) in wxHashTable::Put/Delete\n")); | |
df5168c4 | 1207 | |
1a6d9c76 | 1208 | for ( i = 0; i < COUNT/2; ++i ) |
760d3c7c MB |
1209 | if( hash.Delete(i) != &o + i + 20) |
1210 | wxPuts(_T("Error (2) in wxHashTable::Delete\n")); | |
1211 | ||
1a6d9c76 | 1212 | for ( i = 0; i < COUNT/2; ++i ) |
760d3c7c MB |
1213 | if( hash.Get(i) != NULL) |
1214 | wxPuts(_T("Error (5) in wxHashTable::Put/Delete\n")); | |
1215 | ||
1216 | hash2.Put(_T("foo"), &o + 1); | |
1217 | hash2.Put(_T("bar"), &o + 2); | |
1218 | hash2.Put(_T("baz"), &o + 3); | |
1219 | ||
1220 | if (hash2.Get(_T("moo")) != NULL) | |
df5168c4 MB |
1221 | wxPuts(_T("Error in wxHashTable::Get\n")); |
1222 | ||
760d3c7c | 1223 | if (hash2.Get(_T("bar")) != &o + 2) |
df5168c4 | 1224 | wxPuts(_T("Error in wxHashTable::Get/Put\n")); |
760d3c7c MB |
1225 | |
1226 | hash2.Put(_T("bar"), &o + 0); | |
1227 | ||
1228 | if (hash2.Get(_T("bar")) != &o + 2) | |
1229 | wxPuts(_T("Error (2) in wxHashTable::Get/Put\n")); | |
df5168c4 | 1230 | } |
1a6d9c76 MB |
1231 | |
1232 | // and now some corner-case testing; 3 and 13 hash to the same bucket | |
1233 | { | |
1234 | wxHashTable hash(wxKEY_INTEGER, 10); | |
1235 | wxObject dummy; | |
1236 | ||
1237 | hash.Put(3, &dummy); | |
1238 | hash.Delete(3); | |
1239 | ||
1240 | if (hash.Get(3) != NULL) | |
1241 | wxPuts(_T("Corner case 1 failure\n")); | |
1242 | ||
1243 | hash.Put(3, &dummy); | |
1244 | hash.Put(13, &dummy); | |
1245 | hash.Delete(3); | |
1246 | ||
1247 | if (hash.Get(3) != NULL) | |
1248 | wxPuts(_T("Corner case 2 failure\n")); | |
1249 | ||
1250 | hash.Delete(13); | |
1251 | ||
1252 | if (hash.Get(13) != NULL) | |
1253 | wxPuts(_T("Corner case 3 failure\n")); | |
1254 | ||
1255 | hash.Put(3, &dummy); | |
1256 | hash.Put(13, &dummy); | |
1257 | hash.Delete(13); | |
1258 | ||
1259 | if (hash.Get(13) != NULL) | |
1260 | wxPuts(_T("Corner case 4 failure\n")); | |
1261 | ||
1262 | hash.Delete(3); | |
1263 | ||
1264 | if (hash.Get(3) != NULL) | |
1265 | wxPuts(_T("Corner case 5 failure\n")); | |
1266 | } | |
1267 | ||
1268 | { | |
1269 | wxHashTable hash(wxKEY_INTEGER, 10); | |
1270 | wxObject dummy; | |
1271 | ||
1272 | hash.Put(3, 7, &dummy + 7); | |
1273 | hash.Put(4, 8, &dummy + 8); | |
1274 | ||
1275 | if (hash.Get(7) != NULL) wxPuts(_T("Key/Hash 1 failure\n")); | |
1276 | if (hash.Get(3, 7) != &dummy + 7) wxPuts(_T("Key/Hash 2 failure\n")); | |
1277 | if (hash.Get(4) != NULL) wxPuts(_T("Key/Hash 3 failure\n")); | |
1278 | if (hash.Get(3) != NULL) wxPuts(_T("Key/Hash 4 failure\n")); | |
1279 | if (hash.Get(8) != NULL) wxPuts(_T("Key/Hash 5 failure\n")); | |
1280 | if (hash.Get(8, 4) != NULL) wxPuts(_T("Key/Hash 6 failure\n")); | |
1281 | ||
1282 | if (hash.Delete(7) != NULL) wxPuts(_T("Key/Hash 7 failure\n")); | |
1283 | if (hash.Delete(3) != NULL) wxPuts(_T("Key/Hash 8 failure\n")); | |
1284 | if (hash.Delete(3, 7) != &dummy + 7) wxPuts(_T("Key/Hash 8 failure\n")); | |
1285 | } | |
1286 | ||
2c8e4738 VZ |
1287 | { |
1288 | wxHashFoos hash; | |
cab8f76e | 1289 | hash.DeleteContents(true); |
2c8e4738 | 1290 | |
456ae26d | 1291 | wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"), |
2c8e4738 VZ |
1292 | hash.GetCount(), Foo::count); |
1293 | ||
1294 | static const int hashTestData[] = | |
1295 | { | |
1296 | 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1, | |
1297 | }; | |
1298 | ||
1299 | size_t n; | |
1300 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) | |
1301 | { | |
1302 | hash.Put(hashTestData[n], n, new Foo(n)); | |
1303 | } | |
1304 | ||
456ae26d | 1305 | wxPrintf(_T("Hash filled: %u foos in hash, %u foos totally\n"), |
2c8e4738 VZ |
1306 | hash.GetCount(), Foo::count); |
1307 | ||
456ae26d | 1308 | wxPuts(_T("Hash access test:")); |
2c8e4738 VZ |
1309 | for ( n = 0; n < WXSIZEOF(hashTestData); n++ ) |
1310 | { | |
456ae26d | 1311 | wxPrintf(_T("\tGetting element with key %d, value %d: "), |
2c8e4738 VZ |
1312 | hashTestData[n], n); |
1313 | Foo *foo = hash.Get(hashTestData[n], n); | |
1314 | if ( !foo ) | |
1315 | { | |
456ae26d | 1316 | wxPrintf(_T("ERROR, not found.\n")); |
2c8e4738 VZ |
1317 | } |
1318 | else | |
1319 | { | |
456ae26d | 1320 | wxPrintf(_T("%d (%s)\n"), foo->n, |
2c8e4738 VZ |
1321 | (size_t)foo->n == n ? "ok" : "ERROR"); |
1322 | } | |
1323 | } | |
1324 | ||
456ae26d | 1325 | wxPrintf(_T("\nTrying to get an element not in hash: ")); |
2c8e4738 VZ |
1326 | |
1327 | if ( hash.Get(1234) || hash.Get(1, 0) ) | |
1328 | { | |
456ae26d | 1329 | wxPuts(_T("ERROR: found!")); |
2c8e4738 VZ |
1330 | } |
1331 | else | |
1332 | { | |
456ae26d | 1333 | wxPuts(_T("ok (not found)")); |
2c8e4738 | 1334 | } |
1a6d9c76 MB |
1335 | |
1336 | Foo* foo = hash.Delete(0); | |
1337 | ||
1338 | wxPrintf(_T("Removed 1 foo: %u foos still there\n"), Foo::count); | |
1339 | ||
1340 | delete foo; | |
1341 | ||
1342 | wxPrintf(_T("Foo deleted: %u foos left\n"), Foo::count); | |
2c8e4738 VZ |
1343 | } |
1344 | ||
456ae26d | 1345 | wxPrintf(_T("Hash destroyed: %u foos left\n"), Foo::count); |
df5168c4 | 1346 | wxPuts(_T("*** Testing wxHashTable finished ***\n")); |
1a6d9c76 MB |
1347 | |
1348 | wxPrintf(_T("Time: %ld\n"), sw.Time()); | |
2c8e4738 VZ |
1349 | } |
1350 | ||
1351 | #endif // TEST_HASH | |
1352 | ||
0508ba2a MB |
1353 | // ---------------------------------------------------------------------------- |
1354 | // wxHashMap | |
1355 | // ---------------------------------------------------------------------------- | |
1356 | ||
1357 | #ifdef TEST_HASHMAP | |
1358 | ||
1359 | #include "wx/hashmap.h" | |
1360 | ||
1361 | // test compilation of basic map types | |
1362 | WX_DECLARE_HASH_MAP( int*, int*, wxPointerHash, wxPointerEqual, myPtrHashMap ); | |
1363 | WX_DECLARE_HASH_MAP( long, long, wxIntegerHash, wxIntegerEqual, myLongHashMap ); | |
1364 | WX_DECLARE_HASH_MAP( unsigned long, unsigned, wxIntegerHash, wxIntegerEqual, | |
1365 | myUnsignedHashMap ); | |
1366 | WX_DECLARE_HASH_MAP( unsigned int, unsigned, wxIntegerHash, wxIntegerEqual, | |
1367 | myTestHashMap1 ); | |
1368 | WX_DECLARE_HASH_MAP( int, unsigned, wxIntegerHash, wxIntegerEqual, | |
1369 | myTestHashMap2 ); | |
1370 | WX_DECLARE_HASH_MAP( short, unsigned, wxIntegerHash, wxIntegerEqual, | |
1371 | myTestHashMap3 ); | |
1372 | WX_DECLARE_HASH_MAP( unsigned short, unsigned, wxIntegerHash, wxIntegerEqual, | |
1373 | myTestHashMap4 ); | |
60ce696e VZ |
1374 | |
1375 | // same as: | |
1376 | // WX_DECLARE_HASH_MAP( wxString, wxString, wxStringHash, wxStringEqual, | |
1377 | // myStringHashMap ); | |
1378 | WX_DECLARE_STRING_HASH_MAP(wxString, myStringHashMap); | |
0508ba2a MB |
1379 | |
1380 | typedef myStringHashMap::iterator Itor; | |
1381 | ||
1382 | static void TestHashMap() | |
1383 | { | |
456ae26d | 1384 | wxPuts(_T("*** Testing wxHashMap ***\n")); |
0508ba2a MB |
1385 | myStringHashMap sh(0); // as small as possible |
1386 | wxString buf; | |
1387 | size_t i; | |
1388 | const size_t count = 10000; | |
1389 | ||
1390 | // init with some data | |
1391 | for( i = 0; i < count; ++i ) | |
1392 | { | |
1393 | buf.Printf(wxT("%d"), i ); | |
1394 | sh[buf] = wxT("A") + buf + wxT("C"); | |
1395 | } | |
1396 | ||
1397 | // test that insertion worked | |
1398 | if( sh.size() != count ) | |
1399 | { | |
456ae26d | 1400 | wxPrintf(_T("*** ERROR: %u ELEMENTS, SHOULD BE %u ***\n"), sh.size(), count); |
0508ba2a MB |
1401 | } |
1402 | ||
1403 | for( i = 0; i < count; ++i ) | |
1404 | { | |
1405 | buf.Printf(wxT("%d"), i ); | |
1406 | if( sh[buf] != wxT("A") + buf + wxT("C") ) | |
1407 | { | |
456ae26d | 1408 | wxPrintf(_T("*** ERROR INSERTION BROKEN! STOPPING NOW! ***\n")); |
0508ba2a MB |
1409 | return; |
1410 | } | |
1411 | } | |
1412 | ||
1413 | // check that iterators work | |
1414 | Itor it; | |
1415 | for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i ) | |
1416 | { | |
1417 | if( i == count ) | |
1418 | { | |
456ae26d | 1419 | wxPrintf(_T("*** ERROR ITERATORS DO NOT TERMINATE! STOPPING NOW! ***\n")); |
0508ba2a MB |
1420 | return; |
1421 | } | |
1422 | ||
1423 | if( it->second != sh[it->first] ) | |
1424 | { | |
456ae26d | 1425 | wxPrintf(_T("*** ERROR ITERATORS BROKEN! STOPPING NOW! ***\n")); |
0508ba2a MB |
1426 | return; |
1427 | } | |
1428 | } | |
1429 | ||
1430 | if( sh.size() != i ) | |
1431 | { | |
456ae26d | 1432 | wxPrintf(_T("*** ERROR: %u ELEMENTS ITERATED, SHOULD BE %u ***\n"), i, count); |
0508ba2a MB |
1433 | } |
1434 | ||
1435 | // test copy ctor, assignment operator | |
1436 | myStringHashMap h1( sh ), h2( 0 ); | |
1437 | h2 = sh; | |
1438 | ||
1439 | for( i = 0, it = sh.begin(); it != sh.end(); ++it, ++i ) | |
1440 | { | |
1441 | if( h1[it->first] != it->second ) | |
1442 | { | |
456ae26d | 1443 | wxPrintf(_T("*** ERROR: COPY CTOR BROKEN %s ***\n"), it->first.c_str()); |
0508ba2a MB |
1444 | } |
1445 | ||
1446 | if( h2[it->first] != it->second ) | |
1447 | { | |
456ae26d | 1448 | wxPrintf(_T("*** ERROR: OPERATOR= BROKEN %s ***\n"), it->first.c_str()); |
0508ba2a MB |
1449 | } |
1450 | } | |
1451 | ||
1452 | // other tests | |
1453 | for( i = 0; i < count; ++i ) | |
1454 | { | |
1455 | buf.Printf(wxT("%d"), i ); | |
1456 | size_t sz = sh.size(); | |
1457 | ||
1458 | // test find() and erase(it) | |
1459 | if( i < 100 ) | |
1460 | { | |
1461 | it = sh.find( buf ); | |
1462 | if( it != sh.end() ) | |
1463 | { | |
1464 | sh.erase( it ); | |
1465 | ||
1466 | if( sh.find( buf ) != sh.end() ) | |
1467 | { | |
456ae26d | 1468 | wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i); |
0508ba2a MB |
1469 | } |
1470 | } | |
1471 | else | |
456ae26d | 1472 | wxPrintf(_T("*** ERROR: CANT FIND ELEMENT %u ***\n"), i); |
0508ba2a MB |
1473 | } |
1474 | else | |
1475 | // test erase(key) | |
1476 | { | |
1477 | size_t c = sh.erase( buf ); | |
1478 | if( c != 1 ) | |
456ae26d | 1479 | wxPrintf(_T("*** ERROR: SHOULD RETURN 1 ***\n")); |
0508ba2a MB |
1480 | |
1481 | if( sh.find( buf ) != sh.end() ) | |
1482 | { | |
456ae26d | 1483 | wxPrintf(_T("*** ERROR: FOUND DELETED ELEMENT %u ***\n"), i); |
0508ba2a MB |
1484 | } |
1485 | } | |
1486 | ||
1487 | // count should decrease | |
1488 | if( sh.size() != sz - 1 ) | |
1489 | { | |
456ae26d | 1490 | wxPrintf(_T("*** ERROR: COUNT DID NOT DECREASE ***\n")); |
0508ba2a MB |
1491 | } |
1492 | } | |
1493 | ||
456ae26d | 1494 | wxPrintf(_T("*** Finished testing wxHashMap ***\n")); |
0508ba2a MB |
1495 | } |
1496 | ||
60ce696e | 1497 | #endif // TEST_HASHMAP |
0508ba2a | 1498 | |
8142d704 MB |
1499 | // ---------------------------------------------------------------------------- |
1500 | // wxHashSet | |
1501 | // ---------------------------------------------------------------------------- | |
1502 | ||
1503 | #ifdef TEST_HASHSET | |
1504 | ||
1505 | #include "wx/hashset.h" | |
1506 | ||
1507 | // test compilation of basic map types | |
1508 | WX_DECLARE_HASH_SET( int*, wxPointerHash, wxPointerEqual, myPtrHashSet ); | |
1509 | WX_DECLARE_HASH_SET( long, wxIntegerHash, wxIntegerEqual, myLongHashSet ); | |
1510 | WX_DECLARE_HASH_SET( unsigned long, wxIntegerHash, wxIntegerEqual, | |
1511 | myUnsignedHashSet ); | |
1512 | WX_DECLARE_HASH_SET( unsigned int, wxIntegerHash, wxIntegerEqual, | |
1513 | myTestHashSet1 ); | |
1514 | WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual, | |
1515 | myTestHashSet2 ); | |
1516 | WX_DECLARE_HASH_SET( short, wxIntegerHash, wxIntegerEqual, | |
1517 | myTestHashSet3 ); | |
1518 | WX_DECLARE_HASH_SET( unsigned short, wxIntegerHash, wxIntegerEqual, | |
1519 | myTestHashSet4 ); | |
1520 | WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, | |
1521 | myTestHashSet5 ); | |
1522 | ||
1523 | struct MyStruct | |
1524 | { | |
1525 | int* ptr; | |
1526 | wxString str; | |
1527 | }; | |
1528 | ||
1529 | class MyHash | |
1530 | { | |
1531 | public: | |
1532 | unsigned long operator()(const MyStruct& s) const | |
1533 | { return m_dummy(s.ptr); } | |
1534 | MyHash& operator=(const MyHash&) { return *this; } | |
1535 | private: | |
1536 | wxPointerHash m_dummy; | |
1537 | }; | |
1538 | ||
1539 | class MyEqual | |
1540 | { | |
1541 | public: | |
1542 | bool operator()(const MyStruct& s1, const MyStruct& s2) const | |
1543 | { return s1.ptr == s2.ptr; } | |
1544 | MyEqual& operator=(const MyEqual&) { return *this; } | |
1545 | }; | |
1546 | ||
1547 | WX_DECLARE_HASH_SET( MyStruct, MyHash, MyEqual, mySet ); | |
1548 | ||
1549 | typedef myTestHashSet5 wxStringHashSet; | |
1550 | ||
1551 | static void TestHashSet() | |
1552 | { | |
1553 | wxPrintf(_T("*** Testing wxHashSet ***\n")); | |
1554 | ||
1555 | wxStringHashSet set1; | |
1556 | ||
1557 | set1.insert( _T("abc") ); | |
1558 | set1.insert( _T("bbc") ); | |
1559 | set1.insert( _T("cbc") ); | |
1560 | set1.insert( _T("abc") ); | |
1561 | ||
1562 | if( set1.size() != 3 ) | |
1563 | wxPrintf(_T("*** ERROR IN INSERT ***\n")); | |
1564 | ||
1565 | mySet set2; | |
1566 | int dummy; | |
1567 | MyStruct tmp; | |
1568 | ||
1569 | tmp.ptr = &dummy; tmp.str = _T("ABC"); | |
1570 | set2.insert( tmp ); | |
1571 | tmp.ptr = &dummy + 1; | |
1572 | set2.insert( tmp ); | |
1573 | tmp.ptr = &dummy; tmp.str = _T("CDE"); | |
1574 | set2.insert( tmp ); | |
1575 | ||
1576 | if( set2.size() != 2 ) | |
1577 | wxPrintf(_T("*** ERROR IN INSERT - 2 ***\n")); | |
1578 | ||
1579 | mySet::iterator it = set2.find( tmp ); | |
1580 | ||
1581 | if( it == set2.end() ) | |
1582 | wxPrintf(_T("*** ERROR IN FIND - 1 ***\n")); | |
1583 | if( it->ptr != &dummy ) | |
1584 | wxPrintf(_T("*** ERROR IN FIND - 2 ***\n")); | |
1585 | if( it->str != _T("ABC") ) | |
1586 | wxPrintf(_T("*** ERROR IN INSERT - 3 ***\n")); | |
1587 | ||
1588 | wxPrintf(_T("*** Finished testing wxHashSet ***\n")); | |
1589 | } | |
1590 | ||
1591 | #endif // TEST_HASHSET | |
1592 | ||
f6bcfd97 BP |
1593 | // ---------------------------------------------------------------------------- |
1594 | // wxList | |
1595 | // ---------------------------------------------------------------------------- | |
1596 | ||
1597 | #ifdef TEST_LIST | |
1598 | ||
e84010cf | 1599 | #include "wx/list.h" |
f6bcfd97 BP |
1600 | |
1601 | WX_DECLARE_LIST(Bar, wxListBars); | |
e84010cf | 1602 | #include "wx/listimpl.cpp" |
f6bcfd97 BP |
1603 | WX_DEFINE_LIST(wxListBars); |
1604 | ||
df5168c4 MB |
1605 | WX_DECLARE_LIST(int, wxListInt); |
1606 | WX_DEFINE_LIST(wxListInt); | |
1607 | ||
1608 | static void TestList() | |
1609 | { | |
1610 | wxPuts(_T("*** Testing wxList operations ***\n")); | |
1611 | { | |
1612 | wxListInt list1; | |
1613 | int dummy[5]; | |
1614 | int i; | |
1615 | ||
1616 | for ( i = 0; i < 5; ++i ) | |
1617 | list1.Append(dummy + i); | |
1618 | ||
1619 | if ( list1.GetCount() != 5 ) | |
1620 | wxPuts(_T("Wrong number of items in list\n")); | |
1621 | ||
1622 | if ( list1.Item(3)->GetData() != dummy + 3 ) | |
1623 | wxPuts(_T("Error in Item()\n")); | |
1624 | ||
1625 | if ( !list1.Find(dummy + 4) ) | |
1626 | wxPuts(_T("Error in Find()\n")); | |
1627 | ||
1628 | wxListInt::compatibility_iterator node = list1.GetFirst(); | |
1629 | i = 0; | |
1630 | ||
1631 | while (node) | |
1632 | { | |
1633 | if ( node->GetData() != dummy + i ) | |
1634 | wxPuts(_T("Error in compatibility_iterator\n")); | |
1635 | node = node->GetNext(); | |
1636 | ++i; | |
1637 | } | |
1638 | ||
1639 | if ( size_t(i) != list1.GetCount() ) | |
1640 | wxPuts(_T("Error in compatibility_iterator\n")); | |
1641 | ||
1642 | list1.Insert(dummy + 0); | |
1643 | list1.Insert(1, dummy + 1); | |
1644 | list1.Insert(list1.GetFirst()->GetNext()->GetNext(), dummy + 2); | |
1645 | ||
1646 | node = list1.GetFirst(); | |
1647 | i = 0; | |
1648 | ||
1649 | while (i < 3) | |
1650 | { | |
1651 | int* t = node->GetData(); | |
1652 | if ( t != dummy + i ) | |
1653 | wxPuts(_T("Error in Insert\n")); | |
1654 | node = node->GetNext(); | |
1655 | ++i; | |
1656 | } | |
1657 | } | |
1658 | ||
1659 | wxPuts(_T("*** Testing wxList operations finished ***\n")); | |
1660 | ||
1661 | wxPuts(_T("*** Testing std::list operations ***\n")); | |
1662 | ||
1663 | { | |
1664 | wxListInt list1; | |
1665 | wxListInt::iterator it, en; | |
1666 | wxListInt::reverse_iterator rit, ren; | |
1667 | int i; | |
1668 | for ( i = 0; i < 5; ++i ) | |
1669 | list1.push_back(i + &i); | |
1670 | ||
1671 | for ( it = list1.begin(), en = list1.end(), i = 0; | |
1672 | it != en; ++it, ++i ) | |
1673 | if ( *it != i + &i ) | |
1674 | wxPuts(_T("Error in iterator\n")); | |
1675 | ||
1676 | for ( rit = list1.rbegin(), ren = list1.rend(), i = 4; | |
1677 | rit != ren; ++rit, --i ) | |
1678 | if ( *rit != i + &i ) | |
1679 | wxPuts(_T("Error in reverse_iterator\n")); | |
1680 | ||
1681 | if ( *list1.rbegin() != *--list1.end() || | |
1682 | *list1.begin() != *--list1.rend() ) | |
1683 | wxPuts(_T("Error in iterator/reverse_iterator\n")); | |
1684 | if ( *list1.begin() != *--++list1.begin() || | |
1685 | *list1.rbegin() != *--++list1.rbegin() ) | |
1686 | wxPuts(_T("Error in iterator/reverse_iterator\n")); | |
1687 | ||
1688 | if ( list1.front() != &i || list1.back() != &i + 4 ) | |
1689 | wxPuts(_T("Error in front()/back()\n")); | |
1690 | ||
1691 | list1.erase(list1.begin()); | |
1692 | list1.erase(--list1.end()); | |
1693 | ||
1694 | for ( it = list1.begin(), en = list1.end(), i = 1; | |
1695 | it != en; ++it, ++i ) | |
1696 | if ( *it != i + &i ) | |
1697 | wxPuts(_T("Error in erase()\n")); | |
1698 | } | |
e9d2bb6f | 1699 | |
df5168c4 MB |
1700 | wxPuts(_T("*** Testing std::list operations finished ***\n")); |
1701 | } | |
1702 | ||
f6bcfd97 BP |
1703 | static void TestListCtor() |
1704 | { | |
456ae26d | 1705 | wxPuts(_T("*** Testing wxList construction ***\n")); |
f6bcfd97 BP |
1706 | |
1707 | { | |
1708 | wxListBars list1; | |
1709 | list1.Append(new Bar(_T("first"))); | |
1710 | list1.Append(new Bar(_T("second"))); | |
1711 | ||
456ae26d | 1712 | wxPrintf(_T("After 1st list creation: %u objects in the list, %u objects total.\n"), |
f6bcfd97 BP |
1713 | list1.GetCount(), Bar::GetNumber()); |
1714 | ||
1715 | wxListBars list2; | |
1716 | list2 = list1; | |
1717 | ||
456ae26d | 1718 | wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"), |
f6bcfd97 BP |
1719 | list1.GetCount(), list2.GetCount(), Bar::GetNumber()); |
1720 | ||
df5168c4 | 1721 | #if !wxUSE_STL |
cab8f76e | 1722 | list1.DeleteContents(true); |
df5168c4 MB |
1723 | #else |
1724 | WX_CLEAR_LIST(wxListBars, list1); | |
1725 | #endif | |
f6bcfd97 BP |
1726 | } |
1727 | ||
456ae26d | 1728 | wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber()); |
f6bcfd97 BP |
1729 | } |
1730 | ||
1731 | #endif // TEST_LIST | |
1732 | ||
ec37df57 VZ |
1733 | // ---------------------------------------------------------------------------- |
1734 | // wxLocale | |
1735 | // ---------------------------------------------------------------------------- | |
1736 | ||
1737 | #ifdef TEST_LOCALE | |
1738 | ||
1739 | #include "wx/intl.h" | |
1740 | #include "wx/utils.h" // for wxSetEnv | |
1741 | ||
1742 | static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH); | |
1743 | ||
1744 | // find the name of the language from its value | |
456ae26d VZ |
1745 | static const wxChar *GetLangName(int lang) |
1746 | { | |
1747 | static const wxChar *languageNames[] = | |
1748 | { | |
1749 | _T("DEFAULT"), | |
1750 | _T("UNKNOWN"), | |
1751 | _T("ABKHAZIAN"), | |
1752 | _T("AFAR"), | |
1753 | _T("AFRIKAANS"), | |
1754 | _T("ALBANIAN"), | |
1755 | _T("AMHARIC"), | |
1756 | _T("ARABIC"), | |
1757 | _T("ARABIC_ALGERIA"), | |
1758 | _T("ARABIC_BAHRAIN"), | |
1759 | _T("ARABIC_EGYPT"), | |
1760 | _T("ARABIC_IRAQ"), | |
1761 | _T("ARABIC_JORDAN"), | |
1762 | _T("ARABIC_KUWAIT"), | |
1763 | _T("ARABIC_LEBANON"), | |
1764 | _T("ARABIC_LIBYA"), | |
1765 | _T("ARABIC_MOROCCO"), | |
1766 | _T("ARABIC_OMAN"), | |
1767 | _T("ARABIC_QATAR"), | |
1768 | _T("ARABIC_SAUDI_ARABIA"), | |
1769 | _T("ARABIC_SUDAN"), | |
1770 | _T("ARABIC_SYRIA"), | |
1771 | _T("ARABIC_TUNISIA"), | |
1772 | _T("ARABIC_UAE"), | |
1773 | _T("ARABIC_YEMEN"), | |
1774 | _T("ARMENIAN"), | |
1775 | _T("ASSAMESE"), | |
1776 | _T("AYMARA"), | |
1777 | _T("AZERI"), | |
1778 | _T("AZERI_CYRILLIC"), | |
1779 | _T("AZERI_LATIN"), | |
1780 | _T("BASHKIR"), | |
1781 | _T("BASQUE"), | |
1782 | _T("BELARUSIAN"), | |
1783 | _T("BENGALI"), | |
1784 | _T("BHUTANI"), | |
1785 | _T("BIHARI"), | |
1786 | _T("BISLAMA"), | |
1787 | _T("BRETON"), | |
1788 | _T("BULGARIAN"), | |
1789 | _T("BURMESE"), | |
1790 | _T("CAMBODIAN"), | |
1791 | _T("CATALAN"), | |
1792 | _T("CHINESE"), | |
1793 | _T("CHINESE_SIMPLIFIED"), | |
1794 | _T("CHINESE_TRADITIONAL"), | |
1795 | _T("CHINESE_HONGKONG"), | |
1796 | _T("CHINESE_MACAU"), | |
1797 | _T("CHINESE_SINGAPORE"), | |
1798 | _T("CHINESE_TAIWAN"), | |
1799 | _T("CORSICAN"), | |
1800 | _T("CROATIAN"), | |
1801 | _T("CZECH"), | |
1802 | _T("DANISH"), | |
1803 | _T("DUTCH"), | |
1804 | _T("DUTCH_BELGIAN"), | |
1805 | _T("ENGLISH"), | |
1806 | _T("ENGLISH_UK"), | |
1807 | _T("ENGLISH_US"), | |
1808 | _T("ENGLISH_AUSTRALIA"), | |
1809 | _T("ENGLISH_BELIZE"), | |
1810 | _T("ENGLISH_BOTSWANA"), | |
1811 | _T("ENGLISH_CANADA"), | |
1812 | _T("ENGLISH_CARIBBEAN"), | |
1813 | _T("ENGLISH_DENMARK"), | |
1814 | _T("ENGLISH_EIRE"), | |
1815 | _T("ENGLISH_JAMAICA"), | |
1816 | _T("ENGLISH_NEW_ZEALAND"), | |
1817 | _T("ENGLISH_PHILIPPINES"), | |
1818 | _T("ENGLISH_SOUTH_AFRICA"), | |
1819 | _T("ENGLISH_TRINIDAD"), | |
1820 | _T("ENGLISH_ZIMBABWE"), | |
1821 | _T("ESPERANTO"), | |
1822 | _T("ESTONIAN"), | |
1823 | _T("FAEROESE"), | |
1824 | _T("FARSI"), | |
1825 | _T("FIJI"), | |
1826 | _T("FINNISH"), | |
1827 | _T("FRENCH"), | |
1828 | _T("FRENCH_BELGIAN"), | |
1829 | _T("FRENCH_CANADIAN"), | |
1830 | _T("FRENCH_LUXEMBOURG"), | |
1831 | _T("FRENCH_MONACO"), | |
1832 | _T("FRENCH_SWISS"), | |
1833 | _T("FRISIAN"), | |
1834 | _T("GALICIAN"), | |
1835 | _T("GEORGIAN"), | |
1836 | _T("GERMAN"), | |
1837 | _T("GERMAN_AUSTRIAN"), | |
1838 | _T("GERMAN_BELGIUM"), | |
1839 | _T("GERMAN_LIECHTENSTEIN"), | |
1840 | _T("GERMAN_LUXEMBOURG"), | |
1841 | _T("GERMAN_SWISS"), | |
1842 | _T("GREEK"), | |
1843 | _T("GREENLANDIC"), | |
1844 | _T("GUARANI"), | |
1845 | _T("GUJARATI"), | |
1846 | _T("HAUSA"), | |
1847 | _T("HEBREW"), | |
1848 | _T("HINDI"), | |
1849 | _T("HUNGARIAN"), | |
1850 | _T("ICELANDIC"), | |
1851 | _T("INDONESIAN"), | |
1852 | _T("INTERLINGUA"), | |
1853 | _T("INTERLINGUE"), | |
1854 | _T("INUKTITUT"), | |
1855 | _T("INUPIAK"), | |
1856 | _T("IRISH"), | |
1857 | _T("ITALIAN"), | |
1858 | _T("ITALIAN_SWISS"), | |
1859 | _T("JAPANESE"), | |
1860 | _T("JAVANESE"), | |
1861 | _T("KANNADA"), | |
1862 | _T("KASHMIRI"), | |
1863 | _T("KASHMIRI_INDIA"), | |
1864 | _T("KAZAKH"), | |
1865 | _T("KERNEWEK"), | |
1866 | _T("KINYARWANDA"), | |
1867 | _T("KIRGHIZ"), | |
1868 | _T("KIRUNDI"), | |
1869 | _T("KONKANI"), | |
1870 | _T("KOREAN"), | |
1871 | _T("KURDISH"), | |
1872 | _T("LAOTHIAN"), | |
1873 | _T("LATIN"), | |
1874 | _T("LATVIAN"), | |
1875 | _T("LINGALA"), | |
1876 | _T("LITHUANIAN"), | |
1877 | _T("MACEDONIAN"), | |
1878 | _T("MALAGASY"), | |
1879 | _T("MALAY"), | |
1880 | _T("MALAYALAM"), | |
1881 | _T("MALAY_BRUNEI_DARUSSALAM"), | |
1882 | _T("MALAY_MALAYSIA"), | |
1883 | _T("MALTESE"), | |
1884 | _T("MANIPURI"), | |
1885 | _T("MAORI"), | |
1886 | _T("MARATHI"), | |
1887 | _T("MOLDAVIAN"), | |
1888 | _T("MONGOLIAN"), | |
1889 | _T("NAURU"), | |
1890 | _T("NEPALI"), | |
1891 | _T("NEPALI_INDIA"), | |
1892 | _T("NORWEGIAN_BOKMAL"), | |
1893 | _T("NORWEGIAN_NYNORSK"), | |
1894 | _T("OCCITAN"), | |
1895 | _T("ORIYA"), | |
1896 | _T("OROMO"), | |
1897 | _T("PASHTO"), | |
1898 | _T("POLISH"), | |
1899 | _T("PORTUGUESE"), | |
1900 | _T("PORTUGUESE_BRAZILIAN"), | |
1901 | _T("PUNJABI"), | |
1902 | _T("QUECHUA"), | |
1903 | _T("RHAETO_ROMANCE"), | |
1904 | _T("ROMANIAN"), | |
1905 | _T("RUSSIAN"), | |
1906 | _T("RUSSIAN_UKRAINE"), | |
1907 | _T("SAMOAN"), | |
1908 | _T("SANGHO"), | |
1909 | _T("SANSKRIT"), | |
1910 | _T("SCOTS_GAELIC"), | |
1911 | _T("SERBIAN"), | |
1912 | _T("SERBIAN_CYRILLIC"), | |
1913 | _T("SERBIAN_LATIN"), | |
1914 | _T("SERBO_CROATIAN"), | |
1915 | _T("SESOTHO"), | |
1916 | _T("SETSWANA"), | |
1917 | _T("SHONA"), | |
1918 | _T("SINDHI"), | |
1919 | _T("SINHALESE"), | |
1920 | _T("SISWATI"), | |
1921 | _T("SLOVAK"), | |
1922 | _T("SLOVENIAN"), | |
1923 | _T("SOMALI"), | |
1924 | _T("SPANISH"), | |
1925 | _T("SPANISH_ARGENTINA"), | |
1926 | _T("SPANISH_BOLIVIA"), | |
1927 | _T("SPANISH_CHILE"), | |
1928 | _T("SPANISH_COLOMBIA"), | |
1929 | _T("SPANISH_COSTA_RICA"), | |
1930 | _T("SPANISH_DOMINICAN_REPUBLIC"), | |
1931 | _T("SPANISH_ECUADOR"), | |
1932 | _T("SPANISH_EL_SALVADOR"), | |
1933 | _T("SPANISH_GUATEMALA"), | |
1934 | _T("SPANISH_HONDURAS"), | |
1935 | _T("SPANISH_MEXICAN"), | |
1936 | _T("SPANISH_MODERN"), | |
1937 | _T("SPANISH_NICARAGUA"), | |
1938 | _T("SPANISH_PANAMA"), | |
1939 | _T("SPANISH_PARAGUAY"), | |
1940 | _T("SPANISH_PERU"), | |
1941 | _T("SPANISH_PUERTO_RICO"), | |
1942 | _T("SPANISH_URUGUAY"), | |
1943 | _T("SPANISH_US"), | |
1944 | _T("SPANISH_VENEZUELA"), | |
1945 | _T("SUNDANESE"), | |
1946 | _T("SWAHILI"), | |
1947 | _T("SWEDISH"), | |
1948 | _T("SWEDISH_FINLAND"), | |
1949 | _T("TAGALOG"), | |
1950 | _T("TAJIK"), | |
1951 | _T("TAMIL"), | |
1952 | _T("TATAR"), | |
1953 | _T("TELUGU"), | |
1954 | _T("THAI"), | |
1955 | _T("TIBETAN"), | |
1956 | _T("TIGRINYA"), | |
1957 | _T("TONGA"), | |
1958 | _T("TSONGA"), | |
1959 | _T("TURKISH"), | |
1960 | _T("TURKMEN"), | |
1961 | _T("TWI"), | |
1962 | _T("UIGHUR"), | |
1963 | _T("UKRAINIAN"), | |
1964 | _T("URDU"), | |
1965 | _T("URDU_INDIA"), | |
1966 | _T("URDU_PAKISTAN"), | |
1967 | _T("UZBEK"), | |
1968 | _T("UZBEK_CYRILLIC"), | |
1969 | _T("UZBEK_LATIN"), | |
1970 | _T("VIETNAMESE"), | |
1971 | _T("VOLAPUK"), | |
1972 | _T("WELSH"), | |
1973 | _T("WOLOF"), | |
1974 | _T("XHOSA"), | |
1975 | _T("YIDDISH"), | |
1976 | _T("YORUBA"), | |
1977 | _T("ZHUANG"), | |
1978 | _T("ZULU"), | |
ec37df57 VZ |
1979 | }; |
1980 | ||
1981 | if ( (size_t)lang < WXSIZEOF(languageNames) ) | |
1982 | return languageNames[lang]; | |
1983 | else | |
456ae26d | 1984 | return _T("INVALID"); |
ec37df57 VZ |
1985 | } |
1986 | ||
1987 | static void TestDefaultLang() | |
1988 | { | |
456ae26d | 1989 | wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***")); |
ec37df57 VZ |
1990 | |
1991 | static const wxChar *langStrings[] = | |
1992 | { | |
1993 | NULL, // system default | |
1994 | _T("C"), | |
1995 | _T("fr"), | |
1996 | _T("fr_FR"), | |
1997 | _T("en"), | |
1998 | _T("en_GB"), | |
1999 | _T("en_US"), | |
2000 | _T("de_DE.iso88591"), | |
2001 | _T("german"), | |
2002 | _T("?"), // invalid lang spec | |
2003 | _T("klingonese"), // I bet on some systems it does exist... | |
2004 | }; | |
2005 | ||
dccce9ea VZ |
2006 | wxPrintf(_T("The default system encoding is %s (%d)\n"), |
2007 | wxLocale::GetSystemEncodingName().c_str(), | |
2008 | wxLocale::GetSystemEncoding()); | |
2009 | ||
ec37df57 VZ |
2010 | for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ ) |
2011 | { | |
456ae26d | 2012 | const wxChar *langStr = langStrings[n]; |
ec37df57 | 2013 | if ( langStr ) |
dccce9ea VZ |
2014 | { |
2015 | // FIXME: this doesn't do anything at all under Windows, we need | |
2016 | // to create a new wxLocale! | |
ec37df57 | 2017 | wxSetEnv(_T("LC_ALL"), langStr); |
dccce9ea | 2018 | } |
ec37df57 VZ |
2019 | |
2020 | int lang = gs_localeDefault.GetSystemLanguage(); | |
456ae26d VZ |
2021 | wxPrintf(_T("Locale for '%s' is %s.\n"), |
2022 | langStr ? langStr : _T("system default"), GetLangName(lang)); | |
ec37df57 VZ |
2023 | } |
2024 | } | |
2025 | ||
2026 | #endif // TEST_LOCALE | |
2027 | ||
696e1ea0 VZ |
2028 | // ---------------------------------------------------------------------------- |
2029 | // MIME types | |
2030 | // ---------------------------------------------------------------------------- | |
2031 | ||
2032 | #ifdef TEST_MIME | |
2033 | ||
e84010cf | 2034 | #include "wx/mimetype.h" |
696e1ea0 VZ |
2035 | |
2036 | static void TestMimeEnum() | |
2037 | { | |
a6c65e88 VZ |
2038 | wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n")); |
2039 | ||
696e1ea0 VZ |
2040 | wxArrayString mimetypes; |
2041 | ||
39189b9d | 2042 | size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes); |
696e1ea0 | 2043 | |
456ae26d | 2044 | wxPrintf(_T("*** All %u known filetypes: ***\n"), count); |
696e1ea0 VZ |
2045 | |
2046 | wxArrayString exts; | |
2047 | wxString desc; | |
2048 | ||
2049 | for ( size_t n = 0; n < count; n++ ) | |
2050 | { | |
39189b9d VZ |
2051 | wxFileType *filetype = |
2052 | wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]); | |
696e1ea0 | 2053 | if ( !filetype ) |
c61f4f6d | 2054 | { |
456ae26d | 2055 | wxPrintf(_T("nothing known about the filetype '%s'!\n"), |
97e0ceea | 2056 | mimetypes[n].c_str()); |
696e1ea0 | 2057 | continue; |
c61f4f6d VZ |
2058 | } |
2059 | ||
696e1ea0 VZ |
2060 | filetype->GetDescription(&desc); |
2061 | filetype->GetExtensions(exts); | |
2062 | ||
299fcbfe VZ |
2063 | filetype->GetIcon(NULL); |
2064 | ||
696e1ea0 VZ |
2065 | wxString extsAll; |
2066 | for ( size_t e = 0; e < exts.GetCount(); e++ ) | |
2067 | { | |
2068 | if ( e > 0 ) | |
2069 | extsAll << _T(", "); | |
2070 | extsAll += exts[e]; | |
2071 | } | |
2072 | ||
456ae26d | 2073 | wxPrintf(_T("\t%s: %s (%s)\n"), |
54acce90 | 2074 | mimetypes[n].c_str(), desc.c_str(), extsAll.c_str()); |
696e1ea0 | 2075 | } |
39189b9d | 2076 | |
e9d2bb6f | 2077 | wxPuts(wxEmptyString); |
696e1ea0 VZ |
2078 | } |
2079 | ||
f6bcfd97 BP |
2080 | static void TestMimeOverride() |
2081 | { | |
2082 | wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n")); | |
2083 | ||
39189b9d VZ |
2084 | static const wxChar *mailcap = _T("/tmp/mailcap"); |
2085 | static const wxChar *mimetypes = _T("/tmp/mime.types"); | |
2086 | ||
2087 | if ( wxFile::Exists(mailcap) ) | |
2088 | wxPrintf(_T("Loading mailcap from '%s': %s\n"), | |
2089 | mailcap, | |
2090 | wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR")); | |
2091 | else | |
2092 | wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"), | |
2093 | mailcap); | |
f6bcfd97 | 2094 | |
39189b9d VZ |
2095 | if ( wxFile::Exists(mimetypes) ) |
2096 | wxPrintf(_T("Loading mime.types from '%s': %s\n"), | |
2097 | mimetypes, | |
2098 | wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR")); | |
2099 | else | |
2100 | wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"), | |
2101 | mimetypes); | |
2102 | ||
e9d2bb6f | 2103 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
2104 | } |
2105 | ||
2106 | static void TestMimeFilename() | |
2107 | { | |
2108 | wxPuts(_T("*** Testing MIME type from filename query ***\n")); | |
2109 | ||
2110 | static const wxChar *filenames[] = | |
2111 | { | |
2112 | _T("readme.txt"), | |
2113 | _T("document.pdf"), | |
2114 | _T("image.gif"), | |
f06ef5f4 | 2115 | _T("picture.jpeg"), |
f6bcfd97 BP |
2116 | }; |
2117 | ||
2118 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
2119 | { | |
2120 | const wxString fname = filenames[n]; | |
2121 | wxString ext = fname.AfterLast(_T('.')); | |
39189b9d | 2122 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); |
f6bcfd97 BP |
2123 | if ( !ft ) |
2124 | { | |
2125 | wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str()); | |
2126 | } | |
2127 | else | |
2128 | { | |
2129 | wxString desc; | |
2130 | if ( !ft->GetDescription(&desc) ) | |
2131 | desc = _T("<no description>"); | |
2132 | ||
2133 | wxString cmd; | |
2134 | if ( !ft->GetOpenCommand(&cmd, | |
e9d2bb6f | 2135 | wxFileType::MessageParameters(fname, wxEmptyString)) ) |
f6bcfd97 | 2136 | cmd = _T("<no command available>"); |
7aeebdcd | 2137 | else |
2b5f62a0 | 2138 | cmd = wxString(_T('"')) + cmd + _T('"'); |
f6bcfd97 | 2139 | |
7aeebdcd | 2140 | wxPrintf(_T("To open %s (%s) do %s.\n"), |
f6bcfd97 BP |
2141 | fname.c_str(), desc.c_str(), cmd.c_str()); |
2142 | ||
2143 | delete ft; | |
2144 | } | |
2145 | } | |
39189b9d | 2146 | |
e9d2bb6f | 2147 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
2148 | } |
2149 | ||
c7ce8392 VZ |
2150 | static void TestMimeAssociate() |
2151 | { | |
2152 | wxPuts(_T("*** Testing creation of filetype association ***\n")); | |
2153 | ||
a6c65e88 VZ |
2154 | wxFileTypeInfo ftInfo( |
2155 | _T("application/x-xyz"), | |
2156 | _T("xyzview '%s'"), // open cmd | |
2157 | _T(""), // print cmd | |
df0dc216 VZ |
2158 | _T("XYZ File"), // description |
2159 | _T(".xyz"), // extensions | |
2160 | NULL // end of extensions | |
a6c65e88 VZ |
2161 | ); |
2162 | ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only | |
2163 | ||
39189b9d | 2164 | wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo); |
c7ce8392 VZ |
2165 | if ( !ft ) |
2166 | { | |
2167 | wxPuts(_T("ERROR: failed to create association!")); | |
2168 | } | |
2169 | else | |
2170 | { | |
a6c65e88 | 2171 | // TODO: read it back |
c7ce8392 VZ |
2172 | delete ft; |
2173 | } | |
39189b9d | 2174 | |
e9d2bb6f | 2175 | wxPuts(wxEmptyString); |
c7ce8392 VZ |
2176 | } |
2177 | ||
696e1ea0 VZ |
2178 | #endif // TEST_MIME |
2179 | ||
89e60357 VZ |
2180 | // ---------------------------------------------------------------------------- |
2181 | // misc information functions | |
2182 | // ---------------------------------------------------------------------------- | |
2183 | ||
2184 | #ifdef TEST_INFO_FUNCTIONS | |
2185 | ||
e84010cf | 2186 | #include "wx/utils.h" |
89e60357 | 2187 | |
3a994742 VZ |
2188 | static void TestDiskInfo() |
2189 | { | |
456ae26d | 2190 | wxPuts(_T("*** Testing wxGetDiskSpace() ***")); |
3a994742 VZ |
2191 | |
2192 | for ( ;; ) | |
2193 | { | |
456ae26d VZ |
2194 | wxChar pathname[128]; |
2195 | wxPrintf(_T("\nEnter a directory name: ")); | |
2196 | if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) ) | |
3a994742 VZ |
2197 | break; |
2198 | ||
2199 | // kill the last '\n' | |
456ae26d | 2200 | pathname[wxStrlen(pathname) - 1] = 0; |
3a994742 VZ |
2201 | |
2202 | wxLongLong total, free; | |
2203 | if ( !wxGetDiskSpace(pathname, &total, &free) ) | |
2204 | { | |
2205 | wxPuts(_T("ERROR: wxGetDiskSpace failed.")); | |
2206 | } | |
2207 | else | |
2208 | { | |
eadd7bd2 VZ |
2209 | wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"), |
2210 | (total / 1024).ToString().c_str(), | |
2211 | (free / 1024).ToString().c_str(), | |
3a994742 VZ |
2212 | pathname); |
2213 | } | |
2214 | } | |
2215 | } | |
2216 | ||
89e60357 VZ |
2217 | static void TestOsInfo() |
2218 | { | |
456ae26d | 2219 | wxPuts(_T("*** Testing OS info functions ***\n")); |
89e60357 VZ |
2220 | |
2221 | int major, minor; | |
2222 | wxGetOsVersion(&major, &minor); | |
456ae26d | 2223 | wxPrintf(_T("Running under: %s, version %d.%d\n"), |
89e60357 VZ |
2224 | wxGetOsDescription().c_str(), major, minor); |
2225 | ||
456ae26d | 2226 | wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory()); |
89e60357 | 2227 | |
456ae26d | 2228 | wxPrintf(_T("Host name is %s (%s).\n"), |
89e60357 | 2229 | wxGetHostName().c_str(), wxGetFullHostName().c_str()); |
bd3277fe | 2230 | |
e9d2bb6f | 2231 | wxPuts(wxEmptyString); |
89e60357 VZ |
2232 | } |
2233 | ||
2234 | static void TestUserInfo() | |
2235 | { | |
456ae26d | 2236 | wxPuts(_T("*** Testing user info functions ***\n")); |
89e60357 | 2237 | |
456ae26d VZ |
2238 | wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str()); |
2239 | wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str()); | |
2240 | wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str()); | |
2241 | wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str()); | |
bd3277fe | 2242 | |
e9d2bb6f | 2243 | wxPuts(wxEmptyString); |
89e60357 VZ |
2244 | } |
2245 | ||
2246 | #endif // TEST_INFO_FUNCTIONS | |
2247 | ||
39189b9d VZ |
2248 | // ---------------------------------------------------------------------------- |
2249 | // path list | |
2250 | // ---------------------------------------------------------------------------- | |
2251 | ||
2252 | #ifdef TEST_PATHLIST | |
2253 | ||
ee3ef281 VZ |
2254 | #ifdef __UNIX__ |
2255 | #define CMD_IN_PATH _T("ls") | |
2256 | #else | |
2257 | #define CMD_IN_PATH _T("command.com") | |
2258 | #endif | |
2259 | ||
39189b9d VZ |
2260 | static void TestPathList() |
2261 | { | |
456ae26d | 2262 | wxPuts(_T("*** Testing wxPathList ***\n")); |
39189b9d VZ |
2263 | |
2264 | wxPathList pathlist; | |
ee3ef281 VZ |
2265 | pathlist.AddEnvList(_T("PATH")); |
2266 | wxString path = pathlist.FindValidPath(CMD_IN_PATH); | |
39189b9d VZ |
2267 | if ( path.empty() ) |
2268 | { | |
456ae26d | 2269 | wxPrintf(_T("ERROR: command not found in the path.\n")); |
39189b9d VZ |
2270 | } |
2271 | else | |
2272 | { | |
456ae26d | 2273 | wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str()); |
39189b9d VZ |
2274 | } |
2275 | } | |
2276 | ||
2277 | #endif // TEST_PATHLIST | |
2278 | ||
07a56e45 VZ |
2279 | // ---------------------------------------------------------------------------- |
2280 | // regular expressions | |
2281 | // ---------------------------------------------------------------------------- | |
2282 | ||
2283 | #ifdef TEST_REGEX | |
2284 | ||
e84010cf | 2285 | #include "wx/regex.h" |
07a56e45 | 2286 | |
07a56e45 VZ |
2287 | static void TestRegExInteractive() |
2288 | { | |
2289 | wxPuts(_T("*** Testing RE interactively ***")); | |
2290 | ||
2291 | for ( ;; ) | |
2292 | { | |
456ae26d VZ |
2293 | wxChar pattern[128]; |
2294 | wxPrintf(_T("\nEnter a pattern: ")); | |
2295 | if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) ) | |
07a56e45 VZ |
2296 | break; |
2297 | ||
2298 | // kill the last '\n' | |
456ae26d | 2299 | pattern[wxStrlen(pattern) - 1] = 0; |
07a56e45 VZ |
2300 | |
2301 | wxRegEx re; | |
2302 | if ( !re.Compile(pattern) ) | |
2303 | { | |
2304 | continue; | |
2305 | } | |
2306 | ||
456ae26d | 2307 | wxChar text[128]; |
07a56e45 VZ |
2308 | for ( ;; ) |
2309 | { | |
456ae26d VZ |
2310 | wxPrintf(_T("Enter text to match: ")); |
2311 | if ( !wxFgets(text, WXSIZEOF(text), stdin) ) | |
07a56e45 VZ |
2312 | break; |
2313 | ||
2314 | // kill the last '\n' | |
456ae26d | 2315 | text[wxStrlen(text) - 1] = 0; |
07a56e45 VZ |
2316 | |
2317 | if ( !re.Matches(text) ) | |
2318 | { | |
456ae26d | 2319 | wxPrintf(_T("No match.\n")); |
07a56e45 VZ |
2320 | } |
2321 | else | |
2322 | { | |
456ae26d | 2323 | wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str()); |
07a56e45 VZ |
2324 | |
2325 | size_t start, len; | |
2326 | for ( size_t n = 1; ; n++ ) | |
2327 | { | |
2328 | if ( !re.GetMatch(&start, &len, n) ) | |
2329 | { | |
2330 | break; | |
2331 | } | |
2332 | ||
456ae26d VZ |
2333 | wxPrintf(_T("Subexpr %u matched '%s'\n"), |
2334 | n, wxString(text + start, len).c_str()); | |
07a56e45 VZ |
2335 | } |
2336 | } | |
2337 | } | |
2338 | } | |
2339 | } | |
2340 | ||
2341 | #endif // TEST_REGEX | |
2342 | ||
8d5eff60 VZ |
2343 | // ---------------------------------------------------------------------------- |
2344 | // database | |
2345 | // ---------------------------------------------------------------------------- | |
2346 | ||
ba6ea19e VZ |
2347 | #if !wxUSE_ODBC |
2348 | #undef TEST_ODBC | |
2349 | #endif | |
2350 | ||
8d5eff60 VZ |
2351 | #ifdef TEST_ODBC |
2352 | ||
2353 | #include <wx/db.h> | |
2354 | ||
2355 | static void TestDbOpen() | |
2356 | { | |
2357 | HENV henv; | |
2358 | wxDb db(henv); | |
2359 | } | |
2360 | ||
2361 | #endif // TEST_ODBC | |
2362 | ||
7aeebdcd VZ |
2363 | // ---------------------------------------------------------------------------- |
2364 | // printf() tests | |
2365 | // ---------------------------------------------------------------------------- | |
2366 | ||
2367 | /* | |
2368 | NB: this stuff was taken from the glibc test suite and modified to build | |
2369 | in wxWindows: if I read the copyright below properly, this shouldn't | |
2370 | be a problem | |
2371 | */ | |
2372 | ||
2373 | #ifdef TEST_PRINTF | |
2374 | ||
2375 | #ifdef wxTEST_PRINTF | |
2376 | // use our functions from wxchar.cpp | |
2377 | #undef wxPrintf | |
2378 | #undef wxSprintf | |
2379 | ||
2380 | // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats | |
2381 | // in the tests below | |
2382 | int wxPrintf( const wxChar *format, ... ); | |
2383 | int wxSprintf( wxChar *str, const wxChar *format, ... ); | |
2384 | #endif | |
2385 | ||
f1389d46 VZ |
2386 | #include "wx/longlong.h" |
2387 | ||
7aeebdcd VZ |
2388 | #include <float.h> |
2389 | ||
2390 | static void rfg1 (void); | |
2391 | static void rfg2 (void); | |
2392 | ||
2393 | ||
2394 | static void | |
2395 | fmtchk (const wxChar *fmt) | |
2396 | { | |
2397 | (void) wxPrintf(_T("%s:\t`"), fmt); | |
2398 | (void) wxPrintf(fmt, 0x12); | |
2399 | (void) wxPrintf(_T("'\n")); | |
2400 | } | |
2401 | ||
2402 | static void | |
2403 | fmtst1chk (const wxChar *fmt) | |
2404 | { | |
2405 | (void) wxPrintf(_T("%s:\t`"), fmt); | |
2406 | (void) wxPrintf(fmt, 4, 0x12); | |
2407 | (void) wxPrintf(_T("'\n")); | |
2408 | } | |
2409 | ||
2410 | static void | |
2411 | fmtst2chk (const wxChar *fmt) | |
2412 | { | |
2413 | (void) wxPrintf(_T("%s:\t`"), fmt); | |
2414 | (void) wxPrintf(fmt, 4, 4, 0x12); | |
2415 | (void) wxPrintf(_T("'\n")); | |
2416 | } | |
2417 | ||
2418 | /* This page is covered by the following copyright: */ | |
2419 | ||
2420 | /* (C) Copyright C E Chew | |
2421 | * | |
2422 | * Feel free to copy, use and distribute this software provided: | |
2423 | * | |
2424 | * 1. you do not pretend that you wrote it | |
2425 | * 2. you leave this copyright notice intact. | |
2426 | */ | |
2427 | ||
2428 | /* | |
2429 | * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans. | |
2430 | */ | |
2431 | ||
2432 | #define DEC -123 | |
2433 | #define INT 255 | |
2434 | #define UNS (~0) | |
2435 | ||
2436 | /* Formatted Output Test | |
2437 | * | |
2438 | * This exercises the output formatting code. | |
2439 | */ | |
2440 | ||
e9d2bb6f DS |
2441 | wxChar *PointerNull = NULL; |
2442 | ||
7aeebdcd VZ |
2443 | static void |
2444 | fp_test (void) | |
2445 | { | |
2446 | int i, j, k, l; | |
2447 | wxChar buf[7]; | |
2448 | wxChar *prefix = buf; | |
2449 | wxChar tp[20]; | |
2450 | ||
2451 | wxPuts(_T("\nFormatted output test")); | |
2452 | wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n")); | |
2453 | wxStrcpy(prefix, _T("%")); | |
2454 | for (i = 0; i < 2; i++) { | |
2455 | for (j = 0; j < 2; j++) { | |
2456 | for (k = 0; k < 2; k++) { | |
2457 | for (l = 0; l < 2; l++) { | |
2458 | wxStrcpy(prefix, _T("%")); | |
2459 | if (i == 0) wxStrcat(prefix, _T("-")); | |
2460 | if (j == 0) wxStrcat(prefix, _T("+")); | |
2461 | if (k == 0) wxStrcat(prefix, _T("#")); | |
2462 | if (l == 0) wxStrcat(prefix, _T("0")); | |
2463 | wxPrintf(_T("%5s |"), prefix); | |
2464 | wxStrcpy(tp, prefix); | |
2465 | wxStrcat(tp, _T("6d |")); | |
2466 | wxPrintf(tp, DEC); | |
2467 | wxStrcpy(tp, prefix); | |
2468 | wxStrcat(tp, _T("6o |")); | |
2469 | wxPrintf(tp, INT); | |
2470 | wxStrcpy(tp, prefix); | |
2471 | wxStrcat(tp, _T("6x |")); | |
2472 | wxPrintf(tp, INT); | |
2473 | wxStrcpy(tp, prefix); | |
2474 | wxStrcat(tp, _T("6X |")); | |
2475 | wxPrintf(tp, INT); | |
2476 | wxStrcpy(tp, prefix); | |
2477 | wxStrcat(tp, _T("6u |")); | |
2478 | wxPrintf(tp, UNS); | |
2479 | wxPrintf(_T("\n")); | |
2480 | } | |
2481 | } | |
2482 | } | |
2483 | } | |
e9d2bb6f DS |
2484 | wxPrintf(_T("%10s\n"), PointerNull); |
2485 | wxPrintf(_T("%-10s\n"), PointerNull); | |
7aeebdcd VZ |
2486 | } |
2487 | ||
2488 | static void TestPrintf() | |
2489 | { | |
2490 | static wxChar shortstr[] = _T("Hi, Z."); | |
f1389d46 VZ |
2491 | static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \ |
2492 | I am ready for my first lesson today."); | |
7aeebdcd | 2493 | int result = 0; |
e9d2bb6f | 2494 | wxString test_format; |
7aeebdcd VZ |
2495 | |
2496 | fmtchk(_T("%.4x")); | |
2497 | fmtchk(_T("%04x")); | |
2498 | fmtchk(_T("%4.4x")); | |
2499 | fmtchk(_T("%04.4x")); | |
2500 | fmtchk(_T("%4.3x")); | |
2501 | fmtchk(_T("%04.3x")); | |
2502 | ||
2503 | fmtst1chk(_T("%.*x")); | |
2504 | fmtst1chk(_T("%0*x")); | |
2505 | fmtst2chk(_T("%*.*x")); | |
2506 | fmtst2chk(_T("%0*.*x")); | |
2507 | ||
e9d2bb6f DS |
2508 | wxString bad_format = _T("bad format:\t\"%b\"\n"); |
2509 | wxPrintf(bad_format.c_str()); | |
7aeebdcd VZ |
2510 | wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL); |
2511 | ||
2512 | wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345); | |
2513 | wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345); | |
2514 | wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345); | |
2515 | wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L); | |
2516 | wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L); | |
2517 | wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L); | |
2518 | wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L); | |
e9d2bb6f DS |
2519 | test_format = _T("left-adjusted ZLDN:\t\"%-010ld\"\n"); |
2520 | wxPrintf(test_format.c_str(), -123456); | |
7aeebdcd VZ |
2521 | wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L); |
2522 | wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L); | |
2523 | ||
e9d2bb6f DS |
2524 | test_format = _T("zero-padded string:\t\"%010s\"\n"); |
2525 | wxPrintf(test_format.c_str(), shortstr); | |
2526 | test_format = _T("left-adjusted Z string:\t\"%-010s\"\n"); | |
2527 | wxPrintf(test_format.c_str(), shortstr); | |
7aeebdcd VZ |
2528 | wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr); |
2529 | wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr); | |
e9d2bb6f | 2530 | wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull); |
7aeebdcd VZ |
2531 | wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr); |
2532 | ||
2533 | wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34); | |
2534 | wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234); | |
2535 | wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234); | |
2536 | wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20); | |
2537 | wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1); | |
2538 | wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34); | |
2539 | wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234); | |
2540 | wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234); | |
2541 | wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34); | |
2542 | wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234); | |
2543 | wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234); | |
2544 | wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20); | |
2545 | ||
2546 | wxPrintf (_T(" %6.5f\n"), .099999999860301614); | |
2547 | wxPrintf (_T(" %6.5f\n"), .1); | |
2548 | wxPrintf (_T("x%5.4fx\n"), .5); | |
2549 | ||
2550 | wxPrintf (_T("%#03x\n"), 1); | |
2551 | ||
2552 | //wxPrintf (_T("something really insane: %.10000f\n"), 1.0); | |
2553 | ||
2554 | { | |
2555 | double d = FLT_MIN; | |
2556 | int niter = 17; | |
2557 | ||
2558 | while (niter-- != 0) | |
2559 | wxPrintf (_T("%.17e\n"), d / 2); | |
2560 | fflush (stdout); | |
2561 | } | |
2562 | ||
e9d2bb6f DS |
2563 | #ifndef __WATCOMC__ |
2564 | // Open Watcom cause compiler error here | |
2565 | // Error! E173: col(24) floating-point constant too small to represent | |
7aeebdcd | 2566 | wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324); |
e9d2bb6f | 2567 | #endif |
7aeebdcd VZ |
2568 | |
2569 | #define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n") | |
2570 | wxPrintf (FORMAT, 0.0, 0.0, 0.0); | |
2571 | wxPrintf (FORMAT, 1.0, 1.0, 1.0); | |
2572 | wxPrintf (FORMAT, -1.0, -1.0, -1.0); | |
2573 | wxPrintf (FORMAT, 100.0, 100.0, 100.0); | |
2574 | wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0); | |
2575 | wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0); | |
2576 | wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0); | |
2577 | wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0); | |
2578 | wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0); | |
2579 | #undef FORMAT | |
2580 | ||
2581 | { | |
2582 | wxChar buf[20]; | |
2583 | int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo")); | |
2584 | ||
2585 | wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"), | |
2586 | rc, WXSIZEOF(buf), buf); | |
2587 | #if 0 | |
2588 | wxChar buf2[512]; | |
2589 | wxPrintf ("snprintf (\"%%.999999u\", 10)\n", | |
2590 | wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10)); | |
2591 | #endif | |
2592 | } | |
2593 | ||
2594 | fp_test (); | |
2595 | ||
2596 | wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8); | |
2597 | wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8); | |
2598 | wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8); | |
2599 | wxPrintf (_T("%g should be 123.456\n"), 123.456); | |
2600 | wxPrintf (_T("%g should be 1e+06\n"), 1000000.0); | |
2601 | wxPrintf (_T("%g should be 10\n"), 10.0); | |
2602 | wxPrintf (_T("%g should be 0.02\n"), 0.02); | |
2603 | ||
2604 | { | |
2605 | double x=1.0; | |
2606 | wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x); | |
2607 | } | |
2608 | ||
2609 | { | |
2610 | wxChar buf[200]; | |
2611 | ||
2612 | wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three")); | |
2613 | ||
2614 | result |= wxStrcmp (buf, | |
2615 | _T("onetwo three ")); | |
2616 | ||
2617 | wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok.")); | |
2618 | } | |
2619 | ||
f1389d46 | 2620 | #ifdef wxLongLong_t |
7aeebdcd | 2621 | { |
f1389d46 | 2622 | wxChar buf[200]; |
7aeebdcd | 2623 | |
2b5f62a0 | 2624 | wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000)); |
f2cb8a17 JS |
2625 | #if 0 |
2626 | // for some reason below line fails under Borland | |
f1389d46 | 2627 | wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf); |
f2cb8a17 | 2628 | #endif |
7aeebdcd | 2629 | |
f1389d46 | 2630 | if (wxStrcmp (buf, _T("40000000000")) != 0) |
7aeebdcd | 2631 | { |
f1389d46 VZ |
2632 | result = 1; |
2633 | wxPuts (_T("\tFAILED")); | |
7aeebdcd | 2634 | } |
e9d2bb6f DS |
2635 | wxUnusedVar(result); |
2636 | wxPuts (wxEmptyString); | |
7aeebdcd | 2637 | } |
f1389d46 | 2638 | #endif // wxLongLong_t |
7aeebdcd VZ |
2639 | |
2640 | wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2); | |
2641 | wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2); | |
2642 | ||
2643 | wxPuts (_T("--- Should be no further output. ---")); | |
2644 | rfg1 (); | |
2645 | rfg2 (); | |
2646 | ||
2647 | #if 0 | |
2648 | { | |
2649 | wxChar bytes[7]; | |
2650 | wxChar buf[20]; | |
2651 | ||
2652 | memset (bytes, '\xff', sizeof bytes); | |
2653 | wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]); | |
2654 | if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff' | |
2655 | || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff') | |
2656 | { | |
2657 | wxPuts (_T("%hhn overwrite more bytes")); | |
2658 | result = 1; | |
2659 | } | |
2660 | if (bytes[3] != 3) | |
2661 | { | |
2662 | wxPuts (_T("%hhn wrote incorrect value")); | |
2663 | result = 1; | |
2664 | } | |
2665 | } | |
2666 | #endif | |
2667 | } | |
2668 | ||
2669 | static void | |
2670 | rfg1 (void) | |
2671 | { | |
2672 | wxChar buf[100]; | |
2673 | ||
2674 | wxSprintf (buf, _T("%5.s"), _T("xyz")); | |
2675 | if (wxStrcmp (buf, _T(" ")) != 0) | |
2676 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" ")); | |
2677 | wxSprintf (buf, _T("%5.f"), 33.3); | |
2678 | if (wxStrcmp (buf, _T(" 33")) != 0) | |
2679 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33")); | |
2680 | wxSprintf (buf, _T("%8.e"), 33.3e7); | |
2681 | if (wxStrcmp (buf, _T(" 3e+08")) != 0) | |
2682 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08")); | |
2683 | wxSprintf (buf, _T("%8.E"), 33.3e7); | |
2684 | if (wxStrcmp (buf, _T(" 3E+08")) != 0) | |
2685 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08")); | |
2686 | wxSprintf (buf, _T("%.g"), 33.3); | |
2687 | if (wxStrcmp (buf, _T("3e+01")) != 0) | |
2688 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01")); | |
2689 | wxSprintf (buf, _T("%.G"), 33.3); | |
2690 | if (wxStrcmp (buf, _T("3E+01")) != 0) | |
2691 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01")); | |
2692 | } | |
2693 | ||
2694 | static void | |
2695 | rfg2 (void) | |
2696 | { | |
2697 | int prec; | |
2698 | wxChar buf[100]; | |
e9d2bb6f | 2699 | wxString test_format; |
7aeebdcd VZ |
2700 | |
2701 | prec = 0; | |
2702 | wxSprintf (buf, _T("%.*g"), prec, 3.3); | |
2703 | if (wxStrcmp (buf, _T("3")) != 0) | |
2704 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3")); | |
2705 | prec = 0; | |
2706 | wxSprintf (buf, _T("%.*G"), prec, 3.3); | |
2707 | if (wxStrcmp (buf, _T("3")) != 0) | |
2708 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3")); | |
2709 | prec = 0; | |
2710 | wxSprintf (buf, _T("%7.*G"), prec, 3.33); | |
2711 | if (wxStrcmp (buf, _T(" 3")) != 0) | |
2712 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3")); | |
2713 | prec = 3; | |
e9d2bb6f DS |
2714 | test_format = _T("%04.*o"); |
2715 | wxSprintf (buf, test_format.c_str(), prec, 33); | |
7aeebdcd VZ |
2716 | if (wxStrcmp (buf, _T(" 041")) != 0) |
2717 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041")); | |
2718 | prec = 7; | |
e9d2bb6f DS |
2719 | test_format = _T("%09.*u"); |
2720 | wxSprintf (buf, test_format.c_str(), prec, 33); | |
7aeebdcd VZ |
2721 | if (wxStrcmp (buf, _T(" 0000033")) != 0) |
2722 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033")); | |
2723 | prec = 3; | |
e9d2bb6f DS |
2724 | test_format = _T("%04.*x"); |
2725 | wxSprintf (buf, test_format.c_str(), prec, 33); | |
7aeebdcd VZ |
2726 | if (wxStrcmp (buf, _T(" 021")) != 0) |
2727 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021")); | |
2728 | prec = 3; | |
e9d2bb6f DS |
2729 | test_format = _T("%04.*X"); |
2730 | wxSprintf (buf, test_format.c_str(), prec, 33); | |
7aeebdcd VZ |
2731 | if (wxStrcmp (buf, _T(" 021")) != 0) |
2732 | wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021")); | |
2733 | } | |
2734 | ||
2735 | #endif // TEST_PRINTF | |
2736 | ||
6dfec4b8 | 2737 | // ---------------------------------------------------------------------------- |
7ba4fbeb | 2738 | // registry and related stuff |
6dfec4b8 VZ |
2739 | // ---------------------------------------------------------------------------- |
2740 | ||
2741 | // this is for MSW only | |
2742 | #ifndef __WXMSW__ | |
7ba4fbeb | 2743 | #undef TEST_REGCONF |
6dfec4b8 VZ |
2744 | #undef TEST_REGISTRY |
2745 | #endif | |
2746 | ||
7ba4fbeb VZ |
2747 | #ifdef TEST_REGCONF |
2748 | ||
e84010cf GD |
2749 | #include "wx/confbase.h" |
2750 | #include "wx/msw/regconf.h" | |
7ba4fbeb | 2751 | |
e9d2bb6f | 2752 | #if 0 |
7ba4fbeb VZ |
2753 | static void TestRegConfWrite() |
2754 | { | |
e9d2bb6f DS |
2755 | wxConfig *config = new wxConfig(_T("myapp")); |
2756 | config->SetPath(_T("/group1")); | |
2757 | config->Write(_T("entry1"), _T("foo")); | |
2758 | config->SetPath(_T("/group2")); | |
2759 | config->Write(_T("entry1"), _T("bar")); | |
0aa29b6b | 2760 | } |
e9d2bb6f | 2761 | #endif |
0aa29b6b VZ |
2762 | |
2763 | static void TestRegConfRead() | |
2764 | { | |
e9d2bb6f | 2765 | wxConfig *config = new wxConfig(_T("myapp")); |
0aa29b6b VZ |
2766 | |
2767 | wxString str; | |
2768 | long dummy; | |
e9d2bb6f DS |
2769 | config->SetPath(_T("/")); |
2770 | wxPuts(_T("Enumerating / subgroups:")); | |
0aa29b6b VZ |
2771 | bool bCont = config->GetFirstGroup(str, dummy); |
2772 | while(bCont) | |
2773 | { | |
e9d2bb6f | 2774 | wxPuts(str); |
0aa29b6b VZ |
2775 | bCont = config->GetNextGroup(str, dummy); |
2776 | } | |
7ba4fbeb VZ |
2777 | } |
2778 | ||
2779 | #endif // TEST_REGCONF | |
2780 | ||
6dfec4b8 VZ |
2781 | #ifdef TEST_REGISTRY |
2782 | ||
e84010cf | 2783 | #include "wx/msw/registry.h" |
6dfec4b8 VZ |
2784 | |
2785 | // I chose this one because I liked its name, but it probably only exists under | |
2786 | // NT | |
2787 | static const wxChar *TESTKEY = | |
2788 | _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl"); | |
2789 | ||
2790 | static void TestRegistryRead() | |
2791 | { | |
456ae26d | 2792 | wxPuts(_T("*** testing registry reading ***")); |
6dfec4b8 VZ |
2793 | |
2794 | wxRegKey key(TESTKEY); | |
456ae26d | 2795 | wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str()); |
6dfec4b8 VZ |
2796 | if ( !key.Open() ) |
2797 | { | |
456ae26d | 2798 | wxPuts(_T("ERROR: test key can't be opened, aborting test.")); |
6dfec4b8 VZ |
2799 | |
2800 | return; | |
2801 | } | |
2802 | ||
2803 | size_t nSubKeys, nValues; | |
2804 | if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) ) | |
2805 | { | |
456ae26d | 2806 | wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues); |
6dfec4b8 VZ |
2807 | } |
2808 | ||
456ae26d | 2809 | wxPrintf(_T("Enumerating values:\n")); |
6dfec4b8 VZ |
2810 | |
2811 | long dummy; | |
2812 | wxString value; | |
2813 | bool cont = key.GetFirstValue(value, dummy); | |
2814 | while ( cont ) | |
2815 | { | |
456ae26d | 2816 | wxPrintf(_T("Value '%s': type "), value.c_str()); |
6dfec4b8 VZ |
2817 | switch ( key.GetValueType(value) ) |
2818 | { | |
456ae26d VZ |
2819 | case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break; |
2820 | case wxRegKey::Type_String: wxPrintf(_T("SZ")); break; | |
2821 | case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break; | |
2822 | case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break; | |
2823 | case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break; | |
2824 | case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break; | |
2825 | default: wxPrintf(_T("other (unknown)")); break; | |
6dfec4b8 VZ |
2826 | } |
2827 | ||
456ae26d | 2828 | wxPrintf(_T(", value = ")); |
6dfec4b8 VZ |
2829 | if ( key.IsNumericValue(value) ) |
2830 | { | |
2831 | long val; | |
2832 | key.QueryValue(value, &val); | |
456ae26d | 2833 | wxPrintf(_T("%ld"), val); |
6dfec4b8 VZ |
2834 | } |
2835 | else // string | |
2836 | { | |
2837 | wxString val; | |
2838 | key.QueryValue(value, val); | |
456ae26d | 2839 | wxPrintf(_T("'%s'"), val.c_str()); |
6dfec4b8 VZ |
2840 | |
2841 | key.QueryRawValue(value, val); | |
456ae26d | 2842 | wxPrintf(_T(" (raw value '%s')"), val.c_str()); |
6dfec4b8 VZ |
2843 | } |
2844 | ||
e9d2bb6f | 2845 | wxPutchar('\n'); |
6dfec4b8 VZ |
2846 | |
2847 | cont = key.GetNextValue(value, dummy); | |
2848 | } | |
2849 | } | |
2850 | ||
6ba63600 VZ |
2851 | static void TestRegistryAssociation() |
2852 | { | |
2853 | /* | |
2854 | The second call to deleteself genertaes an error message, with a | |
2855 | messagebox saying .flo is crucial to system operation, while the .ddf | |
2856 | call also fails, but with no error message | |
2857 | */ | |
2858 | ||
2859 | wxRegKey key; | |
2860 | ||
f2cb8a17 | 2861 | key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") ); |
6ba63600 | 2862 | key.Create(); |
f2cb8a17 JS |
2863 | key = _T("ddxf_auto_file") ; |
2864 | key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") ); | |
6ba63600 | 2865 | key.Create(); |
f2cb8a17 JS |
2866 | key = _T("ddxf_auto_file") ; |
2867 | key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon")); | |
6ba63600 | 2868 | key.Create(); |
f2cb8a17 JS |
2869 | key = _T("program,0") ; |
2870 | key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command")); | |
6ba63600 | 2871 | key.Create(); |
f2cb8a17 | 2872 | key = _T("program \"%1\"") ; |
6ba63600 | 2873 | |
f2cb8a17 | 2874 | key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") ); |
6ba63600 | 2875 | key.DeleteSelf(); |
f2cb8a17 | 2876 | key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") ); |
6ba63600 | 2877 | key.DeleteSelf(); |
f2cb8a17 | 2878 | key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon")); |
6ba63600 | 2879 | key.DeleteSelf(); |
f2cb8a17 | 2880 | key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command")); |
6ba63600 VZ |
2881 | key.DeleteSelf(); |
2882 | } | |
2883 | ||
6dfec4b8 VZ |
2884 | #endif // TEST_REGISTRY |
2885 | ||
c66cca2a VZ |
2886 | // ---------------------------------------------------------------------------- |
2887 | // scope guard | |
2888 | // ---------------------------------------------------------------------------- | |
2889 | ||
df5168c4 MB |
2890 | #ifdef TEST_SCOPEGUARD |
2891 | ||
c66cca2a VZ |
2892 | #include "wx/scopeguard.h" |
2893 | ||
2894 | static void function0() { puts("function0()"); } | |
2895 | static void function1(int n) { printf("function1(%d)\n", n); } | |
2896 | static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); } | |
2897 | ||
2898 | struct Object | |
2899 | { | |
2900 | void method0() { printf("method0()\n"); } | |
2901 | void method1(int n) { printf("method1(%d)\n", n); } | |
2902 | void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); } | |
2903 | }; | |
2904 | ||
2905 | static void TestScopeGuard() | |
2906 | { | |
24c8053b RN |
2907 | wxON_BLOCK_EXIT0(function0); |
2908 | wxON_BLOCK_EXIT1(function1, 17); | |
2909 | wxON_BLOCK_EXIT2(function2, 3.14, 'p'); | |
c66cca2a VZ |
2910 | |
2911 | Object obj; | |
24c8053b RN |
2912 | wxON_BLOCK_EXIT_OBJ0(obj, &Object::method0); |
2913 | wxON_BLOCK_EXIT_OBJ1(obj, &Object::method1, 7); | |
2914 | wxON_BLOCK_EXIT_OBJ2(obj, &Object::method2, 2.71, 'e'); | |
c66cca2a VZ |
2915 | |
2916 | wxScopeGuard dismissed = wxMakeGuard(function0); | |
2917 | dismissed.Dismiss(); | |
2918 | } | |
2919 | ||
df5168c4 MB |
2920 | #endif |
2921 | ||
2c8e4738 VZ |
2922 | // ---------------------------------------------------------------------------- |
2923 | // sockets | |
2924 | // ---------------------------------------------------------------------------- | |
2925 | ||
2926 | #ifdef TEST_SOCKETS | |
2927 | ||
e84010cf GD |
2928 | #include "wx/socket.h" |
2929 | #include "wx/protocol/protocol.h" | |
2930 | #include "wx/protocol/http.h" | |
8e907a13 VZ |
2931 | |
2932 | static void TestSocketServer() | |
2933 | { | |
456ae26d | 2934 | wxPuts(_T("*** Testing wxSocketServer ***\n")); |
8e907a13 | 2935 | |
ccdb23df VZ |
2936 | static const int PORT = 3000; |
2937 | ||
8e907a13 | 2938 | wxIPV4address addr; |
ccdb23df | 2939 | addr.Service(PORT); |
8e907a13 VZ |
2940 | |
2941 | wxSocketServer *server = new wxSocketServer(addr); | |
2942 | if ( !server->Ok() ) | |
2943 | { | |
456ae26d | 2944 | wxPuts(_T("ERROR: failed to bind")); |
ccdb23df VZ |
2945 | |
2946 | return; | |
8e907a13 | 2947 | } |
8dfea369 | 2948 | |
cab8f76e VZ |
2949 | bool quit = false; |
2950 | while ( !quit ) | |
8dfea369 | 2951 | { |
456ae26d | 2952 | wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT); |
8dfea369 VZ |
2953 | |
2954 | wxSocketBase *socket = server->Accept(); | |
2955 | if ( !socket ) | |
2956 | { | |
456ae26d | 2957 | wxPuts(_T("ERROR: wxSocketServer::Accept() failed.")); |
8dfea369 VZ |
2958 | break; |
2959 | } | |
2960 | ||
456ae26d | 2961 | wxPuts(_T("Server: got a client.")); |
8dfea369 | 2962 | |
ccdb23df VZ |
2963 | server->SetTimeout(60); // 1 min |
2964 | ||
cab8f76e VZ |
2965 | bool close = false; |
2966 | while ( !close && socket->IsConnected() ) | |
8dfea369 | 2967 | { |
ccdb23df | 2968 | wxString s; |
456ae26d | 2969 | wxChar ch = _T('\0'); |
ccdb23df | 2970 | for ( ;; ) |
8dfea369 | 2971 | { |
ccdb23df VZ |
2972 | if ( socket->Read(&ch, sizeof(ch)).Error() ) |
2973 | { | |
2974 | // don't log error if the client just close the connection | |
2975 | if ( socket->IsConnected() ) | |
2976 | { | |
456ae26d | 2977 | wxPuts(_T("ERROR: in wxSocket::Read.")); |
ccdb23df | 2978 | } |
8dfea369 | 2979 | |
ccdb23df VZ |
2980 | break; |
2981 | } | |
8dfea369 | 2982 | |
ccdb23df VZ |
2983 | if ( ch == '\r' ) |
2984 | continue; | |
8dfea369 | 2985 | |
ccdb23df VZ |
2986 | if ( ch == '\n' ) |
2987 | break; | |
8dfea369 | 2988 | |
ccdb23df VZ |
2989 | s += ch; |
2990 | } | |
8dfea369 | 2991 | |
ccdb23df VZ |
2992 | if ( ch != '\n' ) |
2993 | { | |
2994 | break; | |
2995 | } | |
8dfea369 | 2996 | |
456ae26d | 2997 | wxPrintf(_T("Server: got '%s'.\n"), s.c_str()); |
cab8f76e | 2998 | if ( s == _T("close") ) |
ccdb23df | 2999 | { |
cab8f76e | 3000 | wxPuts(_T("Closing connection")); |
8dfea369 | 3001 | |
cab8f76e | 3002 | close = true; |
ccdb23df | 3003 | } |
cab8f76e VZ |
3004 | else if ( s == _T("quit") ) |
3005 | { | |
3006 | close = | |
3007 | quit = true; | |
ccdb23df | 3008 | |
cab8f76e VZ |
3009 | wxPuts(_T("Shutting down the server")); |
3010 | } | |
3011 | else // not a special command | |
3012 | { | |
3013 | socket->Write(s.MakeUpper().c_str(), s.length()); | |
3014 | socket->Write("\r\n", 2); | |
3015 | wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str()); | |
3016 | } | |
8dfea369 VZ |
3017 | } |
3018 | ||
cab8f76e VZ |
3019 | if ( !close ) |
3020 | { | |
3021 | wxPuts(_T("Server: lost a client unexpectedly.")); | |
3022 | } | |
8dfea369 | 3023 | |
ccdb23df | 3024 | socket->Destroy(); |
8dfea369 | 3025 | } |
9fc3cba7 | 3026 | |
ccdb23df VZ |
3027 | // same as "delete server" but is consistent with GUI programs |
3028 | server->Destroy(); | |
8e907a13 | 3029 | } |
2c8e4738 VZ |
3030 | |
3031 | static void TestSocketClient() | |
3032 | { | |
456ae26d | 3033 | wxPuts(_T("*** Testing wxSocketClient ***\n")); |
2c8e4738 | 3034 | |
456ae26d | 3035 | static const wxChar *hostname = _T("www.wxwindows.org"); |
8e907a13 VZ |
3036 | |
3037 | wxIPV4address addr; | |
3038 | addr.Hostname(hostname); | |
3039 | addr.Service(80); | |
3040 | ||
456ae26d | 3041 | wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname); |
2c8e4738 VZ |
3042 | |
3043 | wxSocketClient client; | |
8e907a13 | 3044 | if ( !client.Connect(addr) ) |
2c8e4738 | 3045 | { |
456ae26d | 3046 | wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); |
2c8e4738 VZ |
3047 | } |
3048 | else | |
3049 | { | |
456ae26d | 3050 | wxPrintf(_T("--- Connected to %s:%u...\n"), |
8e907a13 VZ |
3051 | addr.Hostname().c_str(), addr.Service()); |
3052 | ||
456ae26d | 3053 | wxChar buf[8192]; |
2c8e4738 | 3054 | |
8e907a13 VZ |
3055 | // could use simply "GET" here I suppose |
3056 | wxString cmdGet = | |
456ae26d | 3057 | wxString::Format(_T("GET http://%s/\r\n"), hostname); |
8e907a13 | 3058 | client.Write(cmdGet, cmdGet.length()); |
456ae26d | 3059 | wxPrintf(_T("--- Sent command '%s' to the server\n"), |
8e907a13 | 3060 | MakePrintable(cmdGet).c_str()); |
2c8e4738 | 3061 | client.Read(buf, WXSIZEOF(buf)); |
456ae26d | 3062 | wxPrintf(_T("--- Server replied:\n%s"), buf); |
8e907a13 VZ |
3063 | } |
3064 | } | |
3065 | ||
2e907fab VZ |
3066 | #endif // TEST_SOCKETS |
3067 | ||
b92fd37c VZ |
3068 | // ---------------------------------------------------------------------------- |
3069 | // FTP | |
3070 | // ---------------------------------------------------------------------------- | |
3071 | ||
2e907fab VZ |
3072 | #ifdef TEST_FTP |
3073 | ||
e84010cf | 3074 | #include "wx/protocol/ftp.h" |
2e907fab | 3075 | |
b92fd37c VZ |
3076 | static wxFTP ftp; |
3077 | ||
3078 | #define FTP_ANONYMOUS | |
3079 | ||
3080 | #ifdef FTP_ANONYMOUS | |
456ae26d VZ |
3081 | static const wxChar *directory = _T("/pub"); |
3082 | static const wxChar *filename = _T("welcome.msg"); | |
b92fd37c | 3083 | #else |
456ae26d VZ |
3084 | static const wxChar *directory = _T("/etc"); |
3085 | static const wxChar *filename = _T("issue"); | |
b92fd37c VZ |
3086 | #endif |
3087 | ||
3088 | static bool TestFtpConnect() | |
8e907a13 | 3089 | { |
456ae26d | 3090 | wxPuts(_T("*** Testing FTP connect ***")); |
8e907a13 | 3091 | |
b92fd37c | 3092 | #ifdef FTP_ANONYMOUS |
456ae26d | 3093 | static const wxChar *hostname = _T("ftp.wxwindows.org"); |
b92fd37c | 3094 | |
456ae26d | 3095 | wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname); |
b92fd37c | 3096 | #else // !FTP_ANONYMOUS |
456ae26d | 3097 | static const wxChar *hostname = "localhost"; |
b92fd37c | 3098 | |
456ae26d VZ |
3099 | wxChar user[256]; |
3100 | wxFgets(user, WXSIZEOF(user), stdin); | |
3101 | user[wxStrlen(user) - 1] = '\0'; // chop off '\n' | |
b92fd37c VZ |
3102 | ftp.SetUser(user); |
3103 | ||
456ae26d VZ |
3104 | wxChar password[256]; |
3105 | wxPrintf(_T("Password for %s: "), password); | |
3106 | wxFgets(password, WXSIZEOF(password), stdin); | |
3107 | password[wxStrlen(password) - 1] = '\0'; // chop off '\n' | |
b92fd37c VZ |
3108 | ftp.SetPassword(password); |
3109 | ||
456ae26d | 3110 | wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user); |
b92fd37c VZ |
3111 | #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS |
3112 | ||
3113 | if ( !ftp.Connect(hostname) ) | |
3114 | { | |
456ae26d | 3115 | wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); |
b92fd37c | 3116 | |
cab8f76e | 3117 | return false; |
b92fd37c VZ |
3118 | } |
3119 | else | |
3120 | { | |
456ae26d | 3121 | wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"), |
b92fd37c VZ |
3122 | hostname, ftp.Pwd().c_str()); |
3123 | } | |
3124 | ||
cab8f76e | 3125 | return true; |
b92fd37c | 3126 | } |
b1229561 | 3127 | |
b92fd37c VZ |
3128 | // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0? |
3129 | static void TestFtpWuFtpd() | |
3130 | { | |
3131 | wxFTP ftp; | |
456ae26d | 3132 | static const wxChar *hostname = _T("ftp.eudora.com"); |
b1229561 VZ |
3133 | if ( !ftp.Connect(hostname) ) |
3134 | { | |
456ae26d | 3135 | wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); |
b1229561 VZ |
3136 | } |
3137 | else | |
3138 | { | |
456ae26d | 3139 | static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt"); |
b1229561 VZ |
3140 | wxInputStream *in = ftp.GetInputStream(filename); |
3141 | if ( !in ) | |
3142 | { | |
456ae26d | 3143 | wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename); |
b1229561 VZ |
3144 | } |
3145 | else | |
3146 | { | |
4c51b688 | 3147 | size_t size = in->GetSize(); |
456ae26d | 3148 | wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size); |
b1229561 | 3149 | |
456ae26d | 3150 | wxChar *data = new wxChar[size]; |
b1229561 VZ |
3151 | if ( !in->Read(data, size) ) |
3152 | { | |
456ae26d | 3153 | wxPuts(_T("ERROR: read error")); |
b1229561 VZ |
3154 | } |
3155 | else | |
3156 | { | |
456ae26d | 3157 | wxPrintf(_T("Successfully retrieved the file.\n")); |
b1229561 VZ |
3158 | } |
3159 | ||
3160 | delete [] data; | |
3161 | delete in; | |
3162 | } | |
3163 | } | |
b92fd37c | 3164 | } |
b1229561 | 3165 | |
b92fd37c VZ |
3166 | static void TestFtpList() |
3167 | { | |
456ae26d | 3168 | wxPuts(_T("*** Testing wxFTP file listing ***\n")); |
8e907a13 | 3169 | |
b92fd37c VZ |
3170 | // test CWD |
3171 | if ( !ftp.ChDir(directory) ) | |
3172 | { | |
456ae26d | 3173 | wxPrintf(_T("ERROR: failed to cd to %s\n"), directory); |
b92fd37c | 3174 | } |
2e907fab | 3175 | |
456ae26d | 3176 | wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); |
2e907fab | 3177 | |
b92fd37c VZ |
3178 | // test NLIST and LIST |
3179 | wxArrayString files; | |
3180 | if ( !ftp.GetFilesList(files) ) | |
8e907a13 | 3181 | { |
456ae26d | 3182 | wxPuts(_T("ERROR: failed to get NLIST of files")); |
8e907a13 VZ |
3183 | } |
3184 | else | |
3185 | { | |
456ae26d | 3186 | wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str()); |
b92fd37c VZ |
3187 | size_t count = files.GetCount(); |
3188 | for ( size_t n = 0; n < count; n++ ) | |
8e907a13 | 3189 | { |
456ae26d | 3190 | wxPrintf(_T("\t%s\n"), files[n].c_str()); |
8e907a13 | 3191 | } |
456ae26d | 3192 | wxPuts(_T("End of the file list")); |
b92fd37c | 3193 | } |
8e907a13 | 3194 | |
b92fd37c VZ |
3195 | if ( !ftp.GetDirList(files) ) |
3196 | { | |
456ae26d | 3197 | wxPuts(_T("ERROR: failed to get LIST of files")); |
b92fd37c VZ |
3198 | } |
3199 | else | |
3200 | { | |
456ae26d | 3201 | wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str()); |
b92fd37c VZ |
3202 | size_t count = files.GetCount(); |
3203 | for ( size_t n = 0; n < count; n++ ) | |
8e907a13 | 3204 | { |
456ae26d | 3205 | wxPrintf(_T("\t%s\n"), files[n].c_str()); |
2e907fab | 3206 | } |
456ae26d | 3207 | wxPuts(_T("End of the file list")); |
b92fd37c VZ |
3208 | } |
3209 | ||
3210 | if ( !ftp.ChDir(_T("..")) ) | |
3211 | { | |
456ae26d | 3212 | wxPuts(_T("ERROR: failed to cd to ..")); |
b92fd37c | 3213 | } |
2e907fab | 3214 | |
456ae26d | 3215 | wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); |
b92fd37c VZ |
3216 | } |
3217 | ||
3218 | static void TestFtpDownload() | |
3219 | { | |
456ae26d | 3220 | wxPuts(_T("*** Testing wxFTP download ***\n")); |
b92fd37c VZ |
3221 | |
3222 | // test RETR | |
3223 | wxInputStream *in = ftp.GetInputStream(filename); | |
3224 | if ( !in ) | |
3225 | { | |
456ae26d | 3226 | wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename); |
b92fd37c VZ |
3227 | } |
3228 | else | |
3229 | { | |
4c51b688 | 3230 | size_t size = in->GetSize(); |
456ae26d | 3231 | wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size); |
b92fd37c VZ |
3232 | fflush(stdout); |
3233 | ||
456ae26d | 3234 | wxChar *data = new wxChar[size]; |
b92fd37c | 3235 | if ( !in->Read(data, size) ) |
2e907fab | 3236 | { |
456ae26d | 3237 | wxPuts(_T("ERROR: read error")); |
2e907fab VZ |
3238 | } |
3239 | else | |
3240 | { | |
456ae26d | 3241 | wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data); |
8e907a13 VZ |
3242 | } |
3243 | ||
b92fd37c VZ |
3244 | delete [] data; |
3245 | delete in; | |
3246 | } | |
3247 | } | |
8e907a13 | 3248 | |
b92fd37c VZ |
3249 | static void TestFtpFileSize() |
3250 | { | |
456ae26d | 3251 | wxPuts(_T("*** Testing FTP SIZE command ***")); |
b92fd37c VZ |
3252 | |
3253 | if ( !ftp.ChDir(directory) ) | |
3254 | { | |
456ae26d | 3255 | wxPrintf(_T("ERROR: failed to cd to %s\n"), directory); |
b92fd37c VZ |
3256 | } |
3257 | ||
456ae26d | 3258 | wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); |
b92fd37c VZ |
3259 | |
3260 | if ( ftp.FileExists(filename) ) | |
3261 | { | |
3262 | int size = ftp.GetFileSize(filename); | |
3263 | if ( size == -1 ) | |
456ae26d | 3264 | wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename); |
8e907a13 | 3265 | else |
456ae26d | 3266 | wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size); |
b92fd37c VZ |
3267 | } |
3268 | else | |
3269 | { | |
456ae26d | 3270 | wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename); |
b92fd37c VZ |
3271 | } |
3272 | } | |
3273 | ||
3274 | static void TestFtpMisc() | |
3275 | { | |
456ae26d | 3276 | wxPuts(_T("*** Testing miscellaneous wxFTP functions ***")); |
b92fd37c | 3277 | |
f2cb8a17 | 3278 | if ( ftp.SendCommand(_T("STAT")) != '2' ) |
b92fd37c | 3279 | { |
456ae26d | 3280 | wxPuts(_T("ERROR: STAT failed")); |
b92fd37c VZ |
3281 | } |
3282 | else | |
3283 | { | |
456ae26d | 3284 | wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str()); |
b92fd37c VZ |
3285 | } |
3286 | ||
f2cb8a17 | 3287 | if ( ftp.SendCommand(_T("HELP SITE")) != '2' ) |
b92fd37c | 3288 | { |
456ae26d | 3289 | wxPuts(_T("ERROR: HELP SITE failed")); |
b92fd37c VZ |
3290 | } |
3291 | else | |
3292 | { | |
456ae26d | 3293 | wxPrintf(_T("The list of site-specific commands:\n\n%s\n"), |
b92fd37c VZ |
3294 | ftp.GetLastResult().c_str()); |
3295 | } | |
3296 | } | |
3297 | ||
3298 | static void TestFtpInteractive() | |
3299 | { | |
456ae26d | 3300 | wxPuts(_T("\n*** Interactive wxFTP test ***")); |
b92fd37c | 3301 | |
456ae26d | 3302 | wxChar buf[128]; |
b92fd37c VZ |
3303 | |
3304 | for ( ;; ) | |
3305 | { | |
456ae26d VZ |
3306 | wxPrintf(_T("Enter FTP command: ")); |
3307 | if ( !wxFgets(buf, WXSIZEOF(buf), stdin) ) | |
b92fd37c VZ |
3308 | break; |
3309 | ||
3310 | // kill the last '\n' | |
456ae26d | 3311 | buf[wxStrlen(buf) - 1] = 0; |
b92fd37c VZ |
3312 | |
3313 | // special handling of LIST and NLST as they require data connection | |
3314 | wxString start(buf, 4); | |
3315 | start.MakeUpper(); | |
f2cb8a17 | 3316 | if ( start == _T("LIST") || start == _T("NLST") ) |
8e907a13 | 3317 | { |
b92fd37c | 3318 | wxString wildcard; |
456ae26d | 3319 | if ( wxStrlen(buf) > 4 ) |
b92fd37c | 3320 | wildcard = buf + 5; |
8e907a13 | 3321 | |
b92fd37c | 3322 | wxArrayString files; |
f2cb8a17 | 3323 | if ( !ftp.GetList(files, wildcard, start == _T("LIST")) ) |
8e907a13 | 3324 | { |
456ae26d | 3325 | wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str()); |
8e907a13 VZ |
3326 | } |
3327 | else | |
3328 | { | |
456ae26d | 3329 | wxPrintf(_T("--- %s of '%s' under '%s':\n"), |
b92fd37c VZ |
3330 | start.c_str(), wildcard.c_str(), ftp.Pwd().c_str()); |
3331 | size_t count = files.GetCount(); | |
3332 | for ( size_t n = 0; n < count; n++ ) | |
3333 | { | |
456ae26d | 3334 | wxPrintf(_T("\t%s\n"), files[n].c_str()); |
b92fd37c | 3335 | } |
456ae26d | 3336 | wxPuts(_T("--- End of the file list")); |
8e907a13 | 3337 | } |
2e907fab | 3338 | } |
b92fd37c | 3339 | else // !list |
2e907fab | 3340 | { |
456ae26d VZ |
3341 | wxChar ch = ftp.SendCommand(buf); |
3342 | wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed")); | |
b92fd37c VZ |
3343 | if ( ch ) |
3344 | { | |
456ae26d | 3345 | wxPrintf(_T(" (return code %c)"), ch); |
b92fd37c | 3346 | } |
2e907fab | 3347 | |
456ae26d | 3348 | wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str()); |
2e907fab | 3349 | } |
2c8e4738 | 3350 | } |
b92fd37c | 3351 | |
456ae26d | 3352 | wxPuts(_T("\n*** done ***")); |
2c8e4738 VZ |
3353 | } |
3354 | ||
b92fd37c | 3355 | static void TestFtpUpload() |
f6bcfd97 | 3356 | { |
456ae26d | 3357 | wxPuts(_T("*** Testing wxFTP uploading ***\n")); |
f6bcfd97 | 3358 | |
b92fd37c | 3359 | // upload a file |
456ae26d VZ |
3360 | static const wxChar *file1 = _T("test1"); |
3361 | static const wxChar *file2 = _T("test2"); | |
b92fd37c VZ |
3362 | wxOutputStream *out = ftp.GetOutputStream(file1); |
3363 | if ( out ) | |
3364 | { | |
456ae26d | 3365 | wxPrintf(_T("--- Uploading to %s ---\n"), file1); |
b92fd37c VZ |
3366 | out->Write("First hello", 11); |
3367 | delete out; | |
3368 | } | |
f6bcfd97 | 3369 | |
b92fd37c | 3370 | // send a command to check the remote file |
f2cb8a17 | 3371 | if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' ) |
f6bcfd97 | 3372 | { |
456ae26d | 3373 | wxPrintf(_T("ERROR: STAT %s failed\n"), file1); |
f6bcfd97 BP |
3374 | } |
3375 | else | |
3376 | { | |
456ae26d | 3377 | wxPrintf(_T("STAT %s returned:\n\n%s\n"), |
b92fd37c VZ |
3378 | file1, ftp.GetLastResult().c_str()); |
3379 | } | |
2e907fab | 3380 | |
b92fd37c VZ |
3381 | out = ftp.GetOutputStream(file2); |
3382 | if ( out ) | |
3383 | { | |
456ae26d | 3384 | wxPrintf(_T("--- Uploading to %s ---\n"), file1); |
b92fd37c VZ |
3385 | out->Write("Second hello", 12); |
3386 | delete out; | |
f6bcfd97 BP |
3387 | } |
3388 | } | |
3389 | ||
2e907fab | 3390 | #endif // TEST_FTP |
2c8e4738 | 3391 | |
83141d3a VZ |
3392 | // ---------------------------------------------------------------------------- |
3393 | // streams | |
3394 | // ---------------------------------------------------------------------------- | |
3395 | ||
3396 | #ifdef TEST_STREAMS | |
3397 | ||
e84010cf GD |
3398 | #include "wx/wfstream.h" |
3399 | #include "wx/mstream.h" | |
83141d3a | 3400 | |
24f25c8a VZ |
3401 | static void TestFileStream() |
3402 | { | |
456ae26d | 3403 | wxPuts(_T("*** Testing wxFileInputStream ***")); |
24f25c8a | 3404 | |
e9d2bb6f | 3405 | static const wxString filename = _T("testdata.fs"); |
24f25c8a VZ |
3406 | { |
3407 | wxFileOutputStream fsOut(filename); | |
3408 | fsOut.Write("foo", 3); | |
3409 | } | |
3410 | ||
3411 | wxFileInputStream fsIn(filename); | |
456ae26d | 3412 | wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize()); |
24f25c8a VZ |
3413 | while ( !fsIn.Eof() ) |
3414 | { | |
e9d2bb6f | 3415 | wxPutchar(fsIn.GetC()); |
24f25c8a VZ |
3416 | } |
3417 | ||
3418 | if ( !wxRemoveFile(filename) ) | |
3419 | { | |
e9d2bb6f | 3420 | wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename.c_str()); |
24f25c8a VZ |
3421 | } |
3422 | ||
456ae26d | 3423 | wxPuts(_T("\n*** wxFileInputStream test done ***")); |
24f25c8a VZ |
3424 | } |
3425 | ||
83141d3a VZ |
3426 | static void TestMemoryStream() |
3427 | { | |
99a5af7f VZ |
3428 | wxPuts(_T("*** Testing wxMemoryOutputStream ***")); |
3429 | ||
3430 | wxMemoryOutputStream memOutStream; | |
3431 | wxPrintf(_T("Initially out stream offset: %lu\n"), | |
3432 | (unsigned long)memOutStream.TellO()); | |
3433 | ||
3434 | for ( const wxChar *p = _T("Hello, stream!"); *p; p++ ) | |
3435 | { | |
3436 | memOutStream.PutC(*p); | |
3437 | } | |
3438 | ||
3439 | wxPrintf(_T("Final out stream offset: %lu\n"), | |
3440 | (unsigned long)memOutStream.TellO()); | |
3441 | ||
3442 | wxPuts(_T("*** Testing wxMemoryInputStream ***")); | |
83141d3a VZ |
3443 | |
3444 | wxChar buf[1024]; | |
99a5af7f | 3445 | size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf)); |
83141d3a | 3446 | |
99a5af7f VZ |
3447 | wxMemoryInputStream memInpStream(buf, len); |
3448 | wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize()); | |
83141d3a VZ |
3449 | while ( !memInpStream.Eof() ) |
3450 | { | |
e9d2bb6f | 3451 | wxPutchar(memInpStream.GetC()); |
83141d3a VZ |
3452 | } |
3453 | ||
456ae26d | 3454 | wxPuts(_T("\n*** wxMemoryInputStream test done ***")); |
83141d3a VZ |
3455 | } |
3456 | ||
3457 | #endif // TEST_STREAMS | |
3458 | ||
d31b7b68 VZ |
3459 | // ---------------------------------------------------------------------------- |
3460 | // timers | |
3461 | // ---------------------------------------------------------------------------- | |
3462 | ||
3463 | #ifdef TEST_TIMER | |
3464 | ||
e84010cf GD |
3465 | #include "wx/timer.h" |
3466 | #include "wx/utils.h" | |
d31b7b68 VZ |
3467 | |
3468 | static void TestStopWatch() | |
3469 | { | |
456ae26d | 3470 | wxPuts(_T("*** Testing wxStopWatch ***\n")); |
d31b7b68 VZ |
3471 | |
3472 | wxStopWatch sw; | |
677eff07 | 3473 | sw.Pause(); |
456ae26d | 3474 | wxPrintf(_T("Initially paused, after 2 seconds time is...")); |
677eff07 VZ |
3475 | fflush(stdout); |
3476 | wxSleep(2); | |
456ae26d | 3477 | wxPrintf(_T("\t%ldms\n"), sw.Time()); |
677eff07 | 3478 | |
456ae26d | 3479 | wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds...")); |
677eff07 VZ |
3480 | fflush(stdout); |
3481 | sw.Resume(); | |
d31b7b68 | 3482 | wxSleep(3); |
456ae26d | 3483 | wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time()); |
d31b7b68 VZ |
3484 | |
3485 | sw.Pause(); | |
456ae26d | 3486 | wxPrintf(_T("Pausing agan and sleeping 2 more seconds...")); |
677eff07 | 3487 | fflush(stdout); |
d31b7b68 | 3488 | wxSleep(2); |
456ae26d | 3489 | wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time()); |
d31b7b68 VZ |
3490 | |
3491 | sw.Resume(); | |
456ae26d | 3492 | wxPrintf(_T("Finally resuming and sleeping 2 more seconds...")); |
677eff07 VZ |
3493 | fflush(stdout); |
3494 | wxSleep(2); | |
456ae26d | 3495 | wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time()); |
87798c00 VZ |
3496 | |
3497 | wxStopWatch sw2; | |
456ae26d | 3498 | wxPuts(_T("\nChecking for 'backwards clock' bug...")); |
87798c00 VZ |
3499 | for ( size_t n = 0; n < 70; n++ ) |
3500 | { | |
3501 | sw2.Start(); | |
89e6463c GRG |
3502 | |
3503 | for ( size_t m = 0; m < 100000; m++ ) | |
87798c00 | 3504 | { |
89e6463c GRG |
3505 | if ( sw.Time() < 0 || sw2.Time() < 0 ) |
3506 | { | |
456ae26d | 3507 | wxPuts(_T("\ntime is negative - ERROR!")); |
89e6463c | 3508 | } |
87798c00 VZ |
3509 | } |
3510 | ||
e9d2bb6f | 3511 | wxPutchar('.'); |
677eff07 | 3512 | fflush(stdout); |
87798c00 VZ |
3513 | } |
3514 | ||
456ae26d | 3515 | wxPuts(_T(", ok.")); |
d31b7b68 VZ |
3516 | } |
3517 | ||
3518 | #endif // TEST_TIMER | |
3519 | ||
f6bcfd97 BP |
3520 | // ---------------------------------------------------------------------------- |
3521 | // vCard support | |
3522 | // ---------------------------------------------------------------------------- | |
3523 | ||
3524 | #ifdef TEST_VCARD | |
3525 | ||
e84010cf | 3526 | #include "wx/vcard.h" |
f6bcfd97 BP |
3527 | |
3528 | static void DumpVObject(size_t level, const wxVCardObject& vcard) | |
3529 | { | |
3530 | void *cookie; | |
3531 | wxVCardObject *vcObj = vcard.GetFirstProp(&cookie); | |
3532 | while ( vcObj ) | |
3533 | { | |
456ae26d | 3534 | wxPrintf(_T("%s%s"), |
f6bcfd97 BP |
3535 | wxString(_T('\t'), level).c_str(), |
3536 | vcObj->GetName().c_str()); | |
3537 | ||
3538 | wxString value; | |
3539 | switch ( vcObj->GetType() ) | |
3540 | { | |
3541 | case wxVCardObject::String: | |
3542 | case wxVCardObject::UString: | |
3543 | { | |
3544 | wxString val; | |
3545 | vcObj->GetValue(&val); | |
3546 | value << _T('"') << val << _T('"'); | |
3547 | } | |
3548 | break; | |
3549 | ||
3550 | case wxVCardObject::Int: | |
3551 | { | |
3552 | unsigned int i; | |
3553 | vcObj->GetValue(&i); | |
3554 | value.Printf(_T("%u"), i); | |
3555 | } | |
3556 | break; | |
3557 | ||
3558 | case wxVCardObject::Long: | |
3559 | { | |
3560 | unsigned long l; | |
3561 | vcObj->GetValue(&l); | |
3562 | value.Printf(_T("%lu"), l); | |
3563 | } | |
3564 | break; | |
3565 | ||
3566 | case wxVCardObject::None: | |
3567 | break; | |
3568 | ||
3569 | case wxVCardObject::Object: | |
3570 | value = _T("<node>"); | |
3571 | break; | |
3572 | ||
3573 | default: | |
3574 | value = _T("<unknown value type>"); | |
3575 | } | |
3576 | ||
3577 | if ( !!value ) | |
456ae26d | 3578 | wxPrintf(_T(" = %s"), value.c_str()); |
e9d2bb6f | 3579 | wxPutchar('\n'); |
f6bcfd97 BP |
3580 | |
3581 | DumpVObject(level + 1, *vcObj); | |
3582 | ||
3583 | delete vcObj; | |
3584 | vcObj = vcard.GetNextProp(&cookie); | |
3585 | } | |
3586 | } | |
3587 | ||
3588 | static void DumpVCardAddresses(const wxVCard& vcard) | |
3589 | { | |
456ae26d | 3590 | wxPuts(_T("\nShowing all addresses from vCard:\n")); |
f6bcfd97 BP |
3591 | |
3592 | size_t nAdr = 0; | |
3593 | void *cookie; | |
3594 | wxVCardAddress *addr = vcard.GetFirstAddress(&cookie); | |
3595 | while ( addr ) | |
3596 | { | |
3597 | wxString flagsStr; | |
3598 | int flags = addr->GetFlags(); | |
3599 | if ( flags & wxVCardAddress::Domestic ) | |
3600 | { | |
3601 | flagsStr << _T("domestic "); | |
3602 | } | |
3603 | if ( flags & wxVCardAddress::Intl ) | |
3604 | { | |
3605 | flagsStr << _T("international "); | |
3606 | } | |
3607 | if ( flags & wxVCardAddress::Postal ) | |
3608 | { | |
3609 | flagsStr << _T("postal "); | |
3610 | } | |
3611 | if ( flags & wxVCardAddress::Parcel ) | |
3612 | { | |
3613 | flagsStr << _T("parcel "); | |
3614 | } | |
3615 | if ( flags & wxVCardAddress::Home ) | |
3616 | { | |
3617 | flagsStr << _T("home "); | |
3618 | } | |
3619 | if ( flags & wxVCardAddress::Work ) | |
3620 | { | |
3621 | flagsStr << _T("work "); | |
3622 | } | |
3623 | ||
456ae26d | 3624 | wxPrintf(_T("Address %u:\n") |
f6bcfd97 BP |
3625 | "\tflags = %s\n" |
3626 | "\tvalue = %s;%s;%s;%s;%s;%s;%s\n", | |
3627 | ++nAdr, | |
3628 | flagsStr.c_str(), | |
3629 | addr->GetPostOffice().c_str(), | |
3630 | addr->GetExtAddress().c_str(), | |
3631 | addr->GetStreet().c_str(), | |
3632 | addr->GetLocality().c_str(), | |
3633 | addr->GetRegion().c_str(), | |
3634 | addr->GetPostalCode().c_str(), | |
3635 | addr->GetCountry().c_str() | |
3636 | ); | |
3637 | ||
3638 | delete addr; | |
3639 | addr = vcard.GetNextAddress(&cookie); | |
3640 | } | |
3641 | } | |
3642 | ||
3643 | static void DumpVCardPhoneNumbers(const wxVCard& vcard) | |
3644 | { | |
456ae26d | 3645 | wxPuts(_T("\nShowing all phone numbers from vCard:\n")); |
f6bcfd97 BP |
3646 | |
3647 | size_t nPhone = 0; | |
3648 | void *cookie; | |
3649 | wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie); | |
3650 | while ( phone ) | |
3651 | { | |
3652 | wxString flagsStr; | |
3653 | int flags = phone->GetFlags(); | |
3654 | if ( flags & wxVCardPhoneNumber::Voice ) | |
3655 | { | |
3656 | flagsStr << _T("voice "); | |
3657 | } | |
3658 | if ( flags & wxVCardPhoneNumber::Fax ) | |
3659 | { | |
3660 | flagsStr << _T("fax "); | |
3661 | } | |
3662 | if ( flags & wxVCardPhoneNumber::Cellular ) | |
3663 | { | |
3664 | flagsStr << _T("cellular "); | |
3665 | } | |
3666 | if ( flags & wxVCardPhoneNumber::Modem ) | |
3667 | { | |
3668 | flagsStr << _T("modem "); | |
3669 | } | |
3670 | if ( flags & wxVCardPhoneNumber::Home ) | |
3671 | { | |
3672 | flagsStr << _T("home "); | |
3673 | } | |
3674 | if ( flags & wxVCardPhoneNumber::Work ) | |
3675 | { | |
3676 | flagsStr << _T("work "); | |
3677 | } | |
3678 | ||
456ae26d | 3679 | wxPrintf(_T("Phone number %u:\n") |
f6bcfd97 BP |
3680 | "\tflags = %s\n" |
3681 | "\tvalue = %s\n", | |
3682 | ++nPhone, | |
3683 | flagsStr.c_str(), | |
3684 | phone->GetNumber().c_str() | |
3685 | ); | |
3686 | ||
3687 | delete phone; | |
3688 | phone = vcard.GetNextPhoneNumber(&cookie); | |
3689 | } | |
3690 | } | |
3691 | ||
3692 | static void TestVCardRead() | |
3693 | { | |
456ae26d | 3694 | wxPuts(_T("*** Testing wxVCard reading ***\n")); |
f6bcfd97 BP |
3695 | |
3696 | wxVCard vcard(_T("vcard.vcf")); | |
3697 | if ( !vcard.IsOk() ) | |
3698 | { | |
456ae26d | 3699 | wxPuts(_T("ERROR: couldn't load vCard.")); |
f6bcfd97 BP |
3700 | } |
3701 | else | |
3702 | { | |
3703 | // read individual vCard properties | |
3704 | wxVCardObject *vcObj = vcard.GetProperty("FN"); | |
3705 | wxString value; | |
3706 | if ( vcObj ) | |
3707 | { | |
3708 | vcObj->GetValue(&value); | |
3709 | delete vcObj; | |
3710 | } | |
3711 | else | |
3712 | { | |
3713 | value = _T("<none>"); | |
3714 | } | |
3715 | ||
456ae26d | 3716 | wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str()); |
f6bcfd97 BP |
3717 | |
3718 | ||
3719 | if ( !vcard.GetFullName(&value) ) | |
3720 | { | |
3721 | value = _T("<none>"); | |
3722 | } | |
3723 | ||
456ae26d | 3724 | wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str()); |
f6bcfd97 BP |
3725 | |
3726 | // now show how to deal with multiply occuring properties | |
3727 | DumpVCardAddresses(vcard); | |
3728 | DumpVCardPhoneNumbers(vcard); | |
3729 | ||
3730 | // and finally show all | |
456ae26d | 3731 | wxPuts(_T("\nNow dumping the entire vCard:\n") |
f6bcfd97 BP |
3732 | "-----------------------------\n"); |
3733 | ||
3734 | DumpVObject(0, vcard); | |
3735 | } | |
3736 | } | |
3737 | ||
3738 | static void TestVCardWrite() | |
3739 | { | |
456ae26d | 3740 | wxPuts(_T("*** Testing wxVCard writing ***\n")); |
f6bcfd97 BP |
3741 | |
3742 | wxVCard vcard; | |
3743 | if ( !vcard.IsOk() ) | |
3744 | { | |
456ae26d | 3745 | wxPuts(_T("ERROR: couldn't create vCard.")); |
f6bcfd97 BP |
3746 | } |
3747 | else | |
3748 | { | |
3749 | // set some fields | |
3750 | vcard.SetName("Zeitlin", "Vadim"); | |
3751 | vcard.SetFullName("Vadim Zeitlin"); | |
3752 | vcard.SetOrganization("wxWindows", "R&D"); | |
3753 | ||
3754 | // just dump the vCard back | |
456ae26d VZ |
3755 | wxPuts(_T("Entire vCard follows:\n")); |
3756 | wxPuts(vcard.Write()); | |
f6bcfd97 BP |
3757 | } |
3758 | } | |
3759 | ||
3760 | #endif // TEST_VCARD | |
3761 | ||
0e2c5534 VZ |
3762 | // ---------------------------------------------------------------------------- |
3763 | // wxVolume tests | |
3764 | // ---------------------------------------------------------------------------- | |
3765 | ||
ba6ea19e | 3766 | #if !defined(__WIN32__) || !wxUSE_FSVOLUME |
0e2c5534 VZ |
3767 | #undef TEST_VOLUME |
3768 | #endif | |
3769 | ||
3770 | #ifdef TEST_VOLUME | |
3771 | ||
3772 | #include "wx/volume.h" | |
3773 | ||
3774 | static const wxChar *volumeKinds[] = | |
3775 | { | |
3776 | _T("floppy"), | |
3777 | _T("hard disk"), | |
3778 | _T("CD-ROM"), | |
3779 | _T("DVD-ROM"), | |
3780 | _T("network volume"), | |
3781 | _T("other volume"), | |
3782 | }; | |
3783 | ||
3784 | static void TestFSVolume() | |
3785 | { | |
3786 | wxPuts(_T("*** Testing wxFSVolume class ***")); | |
3787 | ||
3788 | wxArrayString volumes = wxFSVolume::GetVolumes(); | |
3789 | size_t count = volumes.GetCount(); | |
3790 | ||
3791 | if ( !count ) | |
3792 | { | |
3793 | wxPuts(_T("ERROR: no mounted volumes?")); | |
3794 | return; | |
3795 | } | |
3796 | ||
3797 | wxPrintf(_T("%u mounted volumes found:\n"), count); | |
3798 | ||
3799 | for ( size_t n = 0; n < count; n++ ) | |
3800 | { | |
3801 | wxFSVolume vol(volumes[n]); | |
3802 | if ( !vol.IsOk() ) | |
3803 | { | |
3804 | wxPuts(_T("ERROR: couldn't create volume")); | |
3805 | continue; | |
3806 | } | |
3807 | ||
3808 | wxPrintf(_T("%u: %s (%s), %s, %s, %s\n"), | |
3809 | n + 1, | |
3810 | vol.GetDisplayName().c_str(), | |
3811 | vol.GetName().c_str(), | |
3812 | volumeKinds[vol.GetKind()], | |
3813 | vol.IsWritable() ? _T("rw") : _T("ro"), | |
3814 | vol.GetFlags() & wxFS_VOL_REMOVABLE ? _T("removable") | |
3815 | : _T("fixed")); | |
3816 | } | |
3817 | } | |
3818 | ||
3819 | #endif // TEST_VOLUME | |
3820 | ||
f6bcfd97 | 3821 | // ---------------------------------------------------------------------------- |
e7d41190 | 3822 | // wide char and Unicode support |
f6bcfd97 BP |
3823 | // ---------------------------------------------------------------------------- |
3824 | ||
3825 | #ifdef TEST_WCHAR | |
3826 | ||
e84010cf GD |
3827 | #include "wx/strconv.h" |
3828 | #include "wx/fontenc.h" | |
3829 | #include "wx/encconv.h" | |
3830 | #include "wx/buffer.h" | |
f6bcfd97 | 3831 | |
2b5f62a0 | 3832 | static const unsigned char utf8koi8r[] = |
ac511156 VZ |
3833 | { |
3834 | 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176, | |
3835 | 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208, | |
3836 | 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188, | |
3837 | 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208, | |
3838 | 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181, | |
3839 | 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208, | |
3840 | 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0 | |
3841 | }; | |
3842 | ||
2b5f62a0 VZ |
3843 | static const unsigned char utf8iso8859_1[] = |
3844 | { | |
3845 | 0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e, | |
3846 | 0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65, | |
3847 | 0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65, | |
3848 | 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65, | |
3849 | 0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0 | |
3850 | }; | |
3851 | ||
3852 | static const unsigned char utf8Invalid[] = | |
3853 | { | |
3854 | 0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30, | |
3855 | 0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6, | |
3856 | 0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88, | |
3857 | 0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70, | |
3858 | 0x6c, 0x61, 0x79, 0 | |
3859 | }; | |
3860 | ||
3861 | static const struct Utf8Data | |
3862 | { | |
3863 | const unsigned char *text; | |
3864 | size_t len; | |
3865 | const wxChar *charset; | |
3866 | wxFontEncoding encoding; | |
3867 | } utf8data[] = | |
3868 | { | |
3869 | { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 }, | |
3870 | { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 }, | |
3871 | { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 }, | |
3872 | }; | |
456ae26d | 3873 | |
f6bcfd97 BP |
3874 | static void TestUtf8() |
3875 | { | |
456ae26d | 3876 | wxPuts(_T("*** Testing UTF8 support ***\n")); |
f6bcfd97 | 3877 | |
24f25c8a VZ |
3878 | char buf[1024]; |
3879 | wchar_t wbuf[1024]; | |
2b5f62a0 VZ |
3880 | |
3881 | for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ ) | |
24f25c8a | 3882 | { |
2b5f62a0 VZ |
3883 | const Utf8Data& u8d = utf8data[n]; |
3884 | if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text, | |
3885 | WXSIZEOF(wbuf)) == (size_t)-1 ) | |
24f25c8a | 3886 | { |
2b5f62a0 | 3887 | wxPuts(_T("ERROR: UTF-8 decoding failed.")); |
24f25c8a VZ |
3888 | } |
3889 | else | |
ac511156 | 3890 | { |
2b5f62a0 VZ |
3891 | wxCSConv conv(u8d.charset); |
3892 | if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 ) | |
3893 | { | |
3894 | wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset); | |
3895 | } | |
3896 | else | |
3897 | { | |
3898 | wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf); | |
3899 | } | |
ac511156 | 3900 | } |
ac511156 | 3901 | |
5bc1deeb | 3902 | wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text)); |
2b5f62a0 VZ |
3903 | if ( s.empty() ) |
3904 | s = _T("<< conversion failed >>"); | |
3905 | wxPrintf(_T("String in current cset: %s\n"), s.c_str()); | |
3906 | ||
ac511156 VZ |
3907 | } |
3908 | ||
e9d2bb6f | 3909 | wxPuts(wxEmptyString); |
ac511156 | 3910 | } |
f6bcfd97 | 3911 | |
ac511156 VZ |
3912 | static void TestEncodingConverter() |
3913 | { | |
3914 | wxPuts(_T("*** Testing wxEncodingConverter ***\n")); | |
3915 | ||
3916 | // using wxEncodingConverter should give the same result as above | |
3917 | char buf[1024]; | |
3918 | wchar_t wbuf[1024]; | |
2b5f62a0 VZ |
3919 | if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r, |
3920 | WXSIZEOF(utf8koi8r)) == (size_t)-1 ) | |
ac511156 | 3921 | { |
456ae26d | 3922 | wxPuts(_T("ERROR: UTF-8 decoding failed.")); |
ac511156 VZ |
3923 | } |
3924 | else | |
3925 | { | |
3926 | wxEncodingConverter ec; | |
3927 | ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8); | |
3928 | ec.Convert(wbuf, buf); | |
2b5f62a0 | 3929 | wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf); |
24f25c8a | 3930 | } |
ac511156 | 3931 | |
e9d2bb6f | 3932 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
3933 | } |
3934 | ||
3935 | #endif // TEST_WCHAR | |
3936 | ||
3937 | // ---------------------------------------------------------------------------- | |
3938 | // ZIP stream | |
3939 | // ---------------------------------------------------------------------------- | |
3940 | ||
3941 | #ifdef TEST_ZIP | |
3942 | ||
2ca8b884 VZ |
3943 | #include "wx/filesys.h" |
3944 | #include "wx/fs_zip.h" | |
f6bcfd97 BP |
3945 | #include "wx/zipstrm.h" |
3946 | ||
2ca8b884 VZ |
3947 | static const wxChar *TESTFILE_ZIP = _T("testdata.zip"); |
3948 | ||
f6bcfd97 BP |
3949 | static void TestZipStreamRead() |
3950 | { | |
456ae26d | 3951 | wxPuts(_T("*** Testing ZIP reading ***\n")); |
f6bcfd97 | 3952 | |
e9d2bb6f | 3953 | static const wxString filename = _T("foo"); |
2ca8b884 | 3954 | wxZipInputStream istr(TESTFILE_ZIP, filename); |
456ae26d | 3955 | wxPrintf(_T("Archive size: %u\n"), istr.GetSize()); |
f6bcfd97 | 3956 | |
e9d2bb6f | 3957 | wxPrintf(_T("Dumping the file '%s':\n"), filename.c_str()); |
f6bcfd97 BP |
3958 | while ( !istr.Eof() ) |
3959 | { | |
e9d2bb6f | 3960 | wxPutchar(istr.GetC()); |
f6bcfd97 BP |
3961 | fflush(stdout); |
3962 | } | |
3963 | ||
456ae26d | 3964 | wxPuts(_T("\n----- done ------")); |
f6bcfd97 BP |
3965 | } |
3966 | ||
2ca8b884 VZ |
3967 | static void DumpZipDirectory(wxFileSystem& fs, |
3968 | const wxString& dir, | |
3969 | const wxString& indent) | |
3970 | { | |
3971 | wxString prefix = wxString::Format(_T("%s#zip:%s"), | |
3972 | TESTFILE_ZIP, dir.c_str()); | |
3973 | wxString wildcard = prefix + _T("/*"); | |
3974 | ||
3975 | wxString dirname = fs.FindFirst(wildcard, wxDIR); | |
3976 | while ( !dirname.empty() ) | |
3977 | { | |
3978 | if ( !dirname.StartsWith(prefix + _T('/'), &dirname) ) | |
3979 | { | |
3980 | wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n")); | |
3981 | ||
3982 | break; | |
3983 | } | |
3984 | ||
3985 | wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str()); | |
3986 | ||
3987 | DumpZipDirectory(fs, dirname, | |
3988 | indent + wxString(_T(' '), 4)); | |
3989 | ||
3990 | dirname = fs.FindNext(); | |
3991 | } | |
3992 | ||
3993 | wxString filename = fs.FindFirst(wildcard, wxFILE); | |
3994 | while ( !filename.empty() ) | |
3995 | { | |
3996 | if ( !filename.StartsWith(prefix, &filename) ) | |
3997 | { | |
3998 | wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n")); | |
3999 | ||
4000 | break; | |
4001 | } | |
4002 | ||
4003 | wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str()); | |
4004 | ||
4005 | filename = fs.FindNext(); | |
4006 | } | |
4007 | } | |
4008 | ||
4009 | static void TestZipFileSystem() | |
4010 | { | |
456ae26d | 4011 | wxPuts(_T("*** Testing ZIP file system ***\n")); |
2ca8b884 VZ |
4012 | |
4013 | wxFileSystem::AddHandler(new wxZipFSHandler); | |
4014 | wxFileSystem fs; | |
4015 | wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP); | |
4016 | ||
4017 | DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4)); | |
4018 | } | |
4019 | ||
f6bcfd97 BP |
4020 | #endif // TEST_ZIP |
4021 | ||
b76b015e VZ |
4022 | // ---------------------------------------------------------------------------- |
4023 | // date time | |
4024 | // ---------------------------------------------------------------------------- | |
4025 | ||
d31b7b68 | 4026 | #ifdef TEST_DATETIME |
b76b015e | 4027 | |
551fe3a6 VZ |
4028 | #include <math.h> |
4029 | ||
e84010cf | 4030 | #include "wx/datetime.h" |
b76b015e | 4031 | |
299fcbfe VZ |
4032 | // the test data |
4033 | struct Date | |
4034 | { | |
4035 | wxDateTime::wxDateTime_t day; | |
4036 | wxDateTime::Month month; | |
4037 | int year; | |
4038 | wxDateTime::wxDateTime_t hour, min, sec; | |
4039 | double jdn; | |
211c2250 | 4040 | wxDateTime::WeekDay wday; |
299fcbfe VZ |
4041 | time_t gmticks, ticks; |
4042 | ||
4043 | void Init(const wxDateTime::Tm& tm) | |
4044 | { | |
4045 | day = tm.mday; | |
4046 | month = tm.mon; | |
4047 | year = tm.year; | |
4048 | hour = tm.hour; | |
4049 | min = tm.min; | |
4050 | sec = tm.sec; | |
4051 | jdn = 0.0; | |
4052 | gmticks = ticks = -1; | |
4053 | } | |
4054 | ||
4055 | wxDateTime DT() const | |
4056 | { return wxDateTime(day, month, year, hour, min, sec); } | |
4057 | ||
239446b4 VZ |
4058 | bool SameDay(const wxDateTime::Tm& tm) const |
4059 | { | |
4060 | return day == tm.mday && month == tm.mon && year == tm.year; | |
4061 | } | |
4062 | ||
299fcbfe VZ |
4063 | wxString Format() const |
4064 | { | |
4065 | wxString s; | |
456ae26d | 4066 | s.Printf(_T("%02d:%02d:%02d %10s %02d, %4d%s"), |
299fcbfe VZ |
4067 | hour, min, sec, |
4068 | wxDateTime::GetMonthName(month).c_str(), | |
4069 | day, | |
4070 | abs(wxDateTime::ConvertYearToBC(year)), | |
456ae26d | 4071 | year > 0 ? _T("AD") : _T("BC")); |
299fcbfe VZ |
4072 | return s; |
4073 | } | |
239446b4 VZ |
4074 | |
4075 | wxString FormatDate() const | |
4076 | { | |
4077 | wxString s; | |
456ae26d | 4078 | s.Printf(_T("%02d-%s-%4d%s"), |
239446b4 | 4079 | day, |
f0f951fa | 4080 | wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), |
239446b4 | 4081 | abs(wxDateTime::ConvertYearToBC(year)), |
456ae26d | 4082 | year > 0 ? _T("AD") : _T("BC")); |
239446b4 VZ |
4083 | return s; |
4084 | } | |
299fcbfe VZ |
4085 | }; |
4086 | ||
4087 | static const Date testDates[] = | |
4088 | { | |
211c2250 | 4089 | { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 }, |
e7d41190 VZ |
4090 | { 7, wxDateTime::Feb, 2036, 00, 00, 00, 2464730.5, wxDateTime::Thu, -1, -1 }, |
4091 | { 8, wxDateTime::Feb, 2036, 00, 00, 00, 2464731.5, wxDateTime::Fri, -1, -1 }, | |
4092 | { 1, wxDateTime::Jan, 2037, 00, 00, 00, 2465059.5, wxDateTime::Thu, -1, -1 }, | |
4093 | { 1, wxDateTime::Jan, 2038, 00, 00, 00, 2465424.5, wxDateTime::Fri, -1, -1 }, | |
211c2250 VZ |
4094 | { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 }, |
4095 | { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 }, | |
4096 | { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 }, | |
4097 | { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 }, | |
4098 | { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 }, | |
4099 | { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 }, | |
4100 | { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 }, | |
4101 | { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 }, | |
4102 | { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 }, | |
239446b4 VZ |
4103 | { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 }, |
4104 | { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 }, | |
4105 | { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 }, | |
4106 | { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 }, | |
211c2250 | 4107 | { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 }, |
299fcbfe VZ |
4108 | }; |
4109 | ||
2f02cb89 VZ |
4110 | // this test miscellaneous static wxDateTime functions |
4111 | static void TestTimeStatic() | |
4112 | { | |
456ae26d | 4113 | wxPuts(_T("\n*** wxDateTime static methods test ***")); |
2f02cb89 VZ |
4114 | |
4115 | // some info about the current date | |
4116 | int year = wxDateTime::GetCurrentYear(); | |
456ae26d | 4117 | wxPrintf(_T("Current year %d is %sa leap one and has %d days.\n"), |
2f02cb89 VZ |
4118 | year, |
4119 | wxDateTime::IsLeapYear(year) ? "" : "not ", | |
4120 | wxDateTime::GetNumberOfDays(year)); | |
4121 | ||
4122 | wxDateTime::Month month = wxDateTime::GetCurrentMonth(); | |
456ae26d | 4123 | wxPrintf(_T("Current month is '%s' ('%s') and it has %d days\n"), |
f0f951fa | 4124 | wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), |
2f02cb89 VZ |
4125 | wxDateTime::GetMonthName(month).c_str(), |
4126 | wxDateTime::GetNumberOfDays(month)); | |
4127 | ||
4128 | // leap year logic | |
fcc3d7cb VZ |
4129 | static const size_t nYears = 5; |
4130 | static const size_t years[2][nYears] = | |
2f02cb89 VZ |
4131 | { |
4132 | // first line: the years to test | |
4133 | { 1990, 1976, 2000, 2030, 1984, }, | |
4134 | ||
cab8f76e VZ |
4135 | // second line: true if leap, false otherwise |
4136 | { false, true, true, false, true } | |
2f02cb89 VZ |
4137 | }; |
4138 | ||
4139 | for ( size_t n = 0; n < nYears; n++ ) | |
4140 | { | |
4141 | int year = years[0][n]; | |
239446b4 VZ |
4142 | bool should = years[1][n] != 0, |
4143 | is = wxDateTime::IsLeapYear(year); | |
2f02cb89 | 4144 | |
456ae26d | 4145 | wxPrintf(_T("Year %d is %sa leap year (%s)\n"), |
2f02cb89 | 4146 | year, |
239446b4 VZ |
4147 | is ? "" : "not ", |
4148 | should == is ? "ok" : "ERROR"); | |
2f02cb89 VZ |
4149 | |
4150 | wxASSERT( should == wxDateTime::IsLeapYear(year) ); | |
4151 | } | |
4152 | } | |
4153 | ||
4154 | // test constructing wxDateTime objects | |
4155 | static void TestTimeSet() | |
4156 | { | |
456ae26d | 4157 | wxPuts(_T("\n*** wxDateTime construction test ***")); |
2f02cb89 | 4158 | |
299fcbfe VZ |
4159 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
4160 | { | |
4161 | const Date& d1 = testDates[n]; | |
4162 | wxDateTime dt = d1.DT(); | |
4163 | ||
4164 | Date d2; | |
4165 | d2.Init(dt.GetTm()); | |
4166 | ||
4167 | wxString s1 = d1.Format(), | |
4168 | s2 = d2.Format(); | |
4169 | ||
456ae26d VZ |
4170 | wxPrintf(_T("Date: %s == %s (%s)\n"), |
4171 | s1.c_str(), s2.c_str(), | |
4172 | s1 == s2 ? _T("ok") : _T("ERROR")); | |
299fcbfe | 4173 | } |
2f02cb89 VZ |
4174 | } |
4175 | ||
fcc3d7cb VZ |
4176 | // test time zones stuff |
4177 | static void TestTimeZones() | |
4178 | { | |
456ae26d | 4179 | wxPuts(_T("\n*** wxDateTime timezone test ***")); |
fcc3d7cb VZ |
4180 | |
4181 | wxDateTime now = wxDateTime::Now(); | |
4182 | ||
456ae26d VZ |
4183 | wxPrintf(_T("Current GMT time:\t%s\n"), now.Format(_T("%c"), wxDateTime::GMT0).c_str()); |
4184 | wxPrintf(_T("Unix epoch (GMT):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::GMT0).c_str()); | |
4185 | wxPrintf(_T("Unix epoch (EST):\t%s\n"), wxDateTime((time_t)0).Format(_T("%c"), wxDateTime::EST).c_str()); | |
4186 | wxPrintf(_T("Current time in Paris:\t%s\n"), now.Format(_T("%c"), wxDateTime::CET).c_str()); | |
4187 | wxPrintf(_T(" Moscow:\t%s\n"), now.Format(_T("%c"), wxDateTime::MSK).c_str()); | |
4188 | wxPrintf(_T(" New York:\t%s\n"), now.Format(_T("%c"), wxDateTime::EST).c_str()); | |
9d9b7755 VZ |
4189 | |
4190 | wxDateTime::Tm tm = now.GetTm(); | |
4191 | if ( wxDateTime(tm) != now ) | |
4192 | { | |
456ae26d VZ |
4193 | wxPrintf(_T("ERROR: got %s instead of %s\n"), |
4194 | wxDateTime(tm).Format().c_str(), now.Format().c_str()); | |
9d9b7755 | 4195 | } |
fcc3d7cb VZ |
4196 | } |
4197 | ||
e6ec579c VZ |
4198 | // test some minimal support for the dates outside the standard range |
4199 | static void TestTimeRange() | |
4200 | { | |
456ae26d | 4201 | wxPuts(_T("\n*** wxDateTime out-of-standard-range dates test ***")); |
e6ec579c | 4202 | |
456ae26d | 4203 | static const wxChar *fmt = _T("%d-%b-%Y %H:%M:%S"); |
211c2250 | 4204 | |
456ae26d VZ |
4205 | wxPrintf(_T("Unix epoch:\t%s\n"), |
4206 | wxDateTime(2440587.5).Format(fmt).c_str()); | |
4207 | wxPrintf(_T("Feb 29, 0: \t%s\n"), | |
4208 | wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str()); | |
4209 | wxPrintf(_T("JDN 0: \t%s\n"), | |
4210 | wxDateTime(0.0).Format(fmt).c_str()); | |
4211 | wxPrintf(_T("Jan 1, 1AD:\t%s\n"), | |
4212 | wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str()); | |
4213 | wxPrintf(_T("May 29, 2099:\t%s\n"), | |
4214 | wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str()); | |
e6ec579c VZ |
4215 | } |
4216 | ||
299fcbfe | 4217 | static void TestTimeTicks() |
e6ec579c | 4218 | { |
456ae26d | 4219 | wxPuts(_T("\n*** wxDateTime ticks test ***")); |
e6ec579c | 4220 | |
299fcbfe | 4221 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) |
1ef54dcf | 4222 | { |
299fcbfe VZ |
4223 | const Date& d = testDates[n]; |
4224 | if ( d.ticks == -1 ) | |
4225 | continue; | |
1ef54dcf | 4226 | |
299fcbfe VZ |
4227 | wxDateTime dt = d.DT(); |
4228 | long ticks = (dt.GetValue() / 1000).ToLong(); | |
456ae26d | 4229 | wxPrintf(_T("Ticks of %s:\t% 10ld"), d.Format().c_str(), ticks); |
299fcbfe VZ |
4230 | if ( ticks == d.ticks ) |
4231 | { | |
456ae26d | 4232 | wxPuts(_T(" (ok)")); |
299fcbfe VZ |
4233 | } |
4234 | else | |
4235 | { | |
456ae26d VZ |
4236 | wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"), |
4237 | (long)d.ticks, (long)(ticks - d.ticks)); | |
299fcbfe VZ |
4238 | } |
4239 | ||
4240 | dt = d.DT().ToTimezone(wxDateTime::GMT0); | |
4241 | ticks = (dt.GetValue() / 1000).ToLong(); | |
456ae26d | 4242 | wxPrintf(_T("GMtks of %s:\t% 10ld"), d.Format().c_str(), ticks); |
299fcbfe VZ |
4243 | if ( ticks == d.gmticks ) |
4244 | { | |
456ae26d | 4245 | wxPuts(_T(" (ok)")); |
299fcbfe VZ |
4246 | } |
4247 | else | |
4248 | { | |
456ae26d VZ |
4249 | wxPrintf(_T(" (ERROR: should be %ld, delta = %ld)\n"), |
4250 | (long)d.gmticks, (long)(ticks - d.gmticks)); | |
299fcbfe VZ |
4251 | } |
4252 | } | |
4253 | ||
e9d2bb6f | 4254 | wxPuts(wxEmptyString); |
299fcbfe VZ |
4255 | } |
4256 | ||
4257 | // test conversions to JDN &c | |
4258 | static void TestTimeJDN() | |
4259 | { | |
456ae26d | 4260 | wxPuts(_T("\n*** wxDateTime to JDN test ***")); |
1ef54dcf VZ |
4261 | |
4262 | for ( size_t n = 0; n < WXSIZEOF(testDates); n++ ) | |
4263 | { | |
4264 | const Date& d = testDates[n]; | |
299fcbfe | 4265 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); |
1ef54dcf VZ |
4266 | double jdn = dt.GetJulianDayNumber(); |
4267 | ||
456ae26d | 4268 | wxPrintf(_T("JDN of %s is:\t% 15.6f"), d.Format().c_str(), jdn); |
1ef54dcf VZ |
4269 | if ( jdn == d.jdn ) |
4270 | { | |
456ae26d | 4271 | wxPuts(_T(" (ok)")); |
1ef54dcf VZ |
4272 | } |
4273 | else | |
4274 | { | |
456ae26d VZ |
4275 | wxPrintf(_T(" (ERROR: should be %f, delta = %f)\n"), |
4276 | d.jdn, jdn - d.jdn); | |
1ef54dcf VZ |
4277 | } |
4278 | } | |
e6ec579c VZ |
4279 | } |
4280 | ||
211c2250 VZ |
4281 | // test week days computation |
4282 | static void TestTimeWDays() | |
4283 | { | |
456ae26d | 4284 | wxPuts(_T("\n*** wxDateTime weekday test ***")); |
211c2250 | 4285 | |
239446b4 VZ |
4286 | // test GetWeekDay() |
4287 | size_t n; | |
4288 | for ( n = 0; n < WXSIZEOF(testDates); n++ ) | |
211c2250 VZ |
4289 | { |
4290 | const Date& d = testDates[n]; | |
4291 | wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec); | |
4292 | ||
4293 | wxDateTime::WeekDay wday = dt.GetWeekDay(); | |
456ae26d VZ |
4294 | wxPrintf(_T("%s is: %s"), |
4295 | d.Format().c_str(), | |
4296 | wxDateTime::GetWeekDayName(wday).c_str()); | |
211c2250 VZ |
4297 | if ( wday == d.wday ) |
4298 | { | |
456ae26d | 4299 | wxPuts(_T(" (ok)")); |
211c2250 VZ |
4300 | } |
4301 | else | |
4302 | { | |
456ae26d VZ |
4303 | wxPrintf(_T(" (ERROR: should be %s)\n"), |
4304 | wxDateTime::GetWeekDayName(d.wday).c_str()); | |
239446b4 VZ |
4305 | } |
4306 | } | |
4307 | ||
e9d2bb6f | 4308 | wxPuts(wxEmptyString); |
239446b4 VZ |
4309 | |
4310 | // test SetToWeekDay() | |
4311 | struct WeekDateTestData | |
4312 | { | |
4313 | Date date; // the real date (precomputed) | |
4314 | int nWeek; // its week index in the month | |
4315 | wxDateTime::WeekDay wday; // the weekday | |
4316 | wxDateTime::Month month; // the month | |
4317 | int year; // and the year | |
4318 | ||
4319 | wxString Format() const | |
4320 | { | |
4321 | wxString s, which; | |
4322 | switch ( nWeek < -1 ? -nWeek : nWeek ) | |
4323 | { | |
456ae26d VZ |
4324 | case 1: which = _T("first"); break; |
4325 | case 2: which = _T("second"); break; | |
4326 | case 3: which = _T("third"); break; | |
4327 | case 4: which = _T("fourth"); break; | |
4328 | case 5: which = _T("fifth"); break; | |
239446b4 | 4329 | |
456ae26d | 4330 | case -1: which = _T("last"); break; |
239446b4 VZ |
4331 | } |
4332 | ||
4333 | if ( nWeek < -1 ) | |
4334 | { | |
456ae26d | 4335 | which += _T(" from end"); |
239446b4 VZ |
4336 | } |
4337 | ||
456ae26d | 4338 | s.Printf(_T("The %s %s of %s in %d"), |
239446b4 VZ |
4339 | which.c_str(), |
4340 | wxDateTime::GetWeekDayName(wday).c_str(), | |
4341 | wxDateTime::GetMonthName(month).c_str(), | |
4342 | year); | |
4343 | ||
4344 | return s; | |
4345 | } | |
4346 | }; | |
4347 | ||
4348 | // the array data was generated by the following python program | |
4349 | /* | |
4350 | from DateTime import * | |
4351 | from whrandom import * | |
4352 | from string import * | |
4353 | ||
4354 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
4355 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
4356 | ||
4357 | week = DateTimeDelta(7) | |
4358 | ||
4359 | for n in range(20): | |
4360 | year = randint(1900, 2100) | |
4361 | month = randint(1, 12) | |
4362 | day = randint(1, 28) | |
4363 | dt = DateTime(year, month, day) | |
4364 | wday = dt.day_of_week | |
4365 | ||
4366 | countFromEnd = choice([-1, 1]) | |
4367 | weekNum = 0; | |
4368 | ||
4369 | while dt.month is month: | |
4370 | dt = dt - countFromEnd * week | |
4371 | weekNum = weekNum + countFromEnd | |
4372 | ||
4373 | data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] } | |
97e0ceea | 4374 | |
239446b4 VZ |
4375 | print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\ |
4376 | "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data | |
97e0ceea | 4377 | */ |
239446b4 VZ |
4378 | |
4379 | static const WeekDateTestData weekDatesTestData[] = | |
4380 | { | |
4381 | { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 }, | |
4382 | { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 }, | |
4383 | { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 }, | |
4384 | { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 }, | |
4385 | { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 }, | |
4386 | { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 }, | |
4387 | { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 }, | |
4388 | { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 }, | |
4389 | { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 }, | |
4390 | { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 }, | |
4391 | { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 }, | |
4392 | { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 }, | |
4393 | { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 }, | |
4394 | { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 }, | |
4395 | { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 }, | |
4396 | { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 }, | |
4397 | { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 }, | |
4398 | { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 }, | |
4399 | { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 }, | |
4400 | { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 }, | |
4401 | }; | |
4402 | ||
456ae26d | 4403 | static const wxChar *fmt = _T("%d-%b-%Y"); |
239446b4 VZ |
4404 | |
4405 | wxDateTime dt; | |
4406 | for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ ) | |
4407 | { | |
4408 | const WeekDateTestData& wd = weekDatesTestData[n]; | |
4409 | ||
4410 | dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year); | |
4411 | ||
456ae26d | 4412 | wxPrintf(_T("%s is %s"), wd.Format().c_str(), dt.Format(fmt).c_str()); |
239446b4 VZ |
4413 | |
4414 | const Date& d = wd.date; | |
4415 | if ( d.SameDay(dt.GetTm()) ) | |
4416 | { | |
456ae26d | 4417 | wxPuts(_T(" (ok)")); |
239446b4 VZ |
4418 | } |
4419 | else | |
4420 | { | |
4421 | dt.Set(d.day, d.month, d.year); | |
4422 | ||
456ae26d | 4423 | wxPrintf(_T(" (ERROR: should be %s)\n"), dt.Format(fmt).c_str()); |
211c2250 VZ |
4424 | } |
4425 | } | |
4426 | } | |
4427 | ||
239446b4 VZ |
4428 | // test the computation of (ISO) week numbers |
4429 | static void TestTimeWNumber() | |
4430 | { | |
456ae26d | 4431 | wxPuts(_T("\n*** wxDateTime week number test ***")); |
239446b4 VZ |
4432 | |
4433 | struct WeekNumberTestData | |
4434 | { | |
4435 | Date date; // the date | |
9d9b7755 VZ |
4436 | wxDateTime::wxDateTime_t week; // the week number in the year |
4437 | wxDateTime::wxDateTime_t wmon; // the week number in the month | |
4438 | wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun | |
239446b4 VZ |
4439 | wxDateTime::wxDateTime_t dnum; // day number in the year |
4440 | }; | |
4441 | ||
4442 | // data generated with the following python script: | |
4443 | /* | |
4444 | from DateTime import * | |
4445 | from whrandom import * | |
4446 | from string import * | |
4447 | ||
4448 | monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] | |
4449 | wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] | |
4450 | ||
9d9b7755 VZ |
4451 | def GetMonthWeek(dt): |
4452 | weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1 | |
4453 | if weekNumMonth < 0: | |
4454 | weekNumMonth = weekNumMonth + 53 | |
4455 | return weekNumMonth | |
7c968cee | 4456 | |
9d9b7755 VZ |
4457 | def GetLastSundayBefore(dt): |
4458 | if dt.iso_week[2] == 7: | |
4459 | return dt | |
4460 | else: | |
4461 | return dt - DateTimeDelta(dt.iso_week[2]) | |
4462 | ||
239446b4 VZ |
4463 | for n in range(20): |
4464 | year = randint(1900, 2100) | |
4465 | month = randint(1, 12) | |
4466 | day = randint(1, 28) | |
4467 | dt = DateTime(year, month, day) | |
4468 | dayNum = dt.day_of_year | |
4469 | weekNum = dt.iso_week[1] | |
9d9b7755 VZ |
4470 | weekNumMonth = GetMonthWeek(dt) |
4471 | ||
4472 | weekNumMonth2 = 0 | |
4473 | dtSunday = GetLastSundayBefore(dt) | |
4474 | ||
4475 | while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)): | |
4476 | weekNumMonth2 = weekNumMonth2 + 1 | |
4477 | dtSunday = dtSunday - DateTimeDelta(7) | |
4478 | ||
4479 | data = { 'day': rjust(`day`, 2), \ | |
4480 | 'month': monthNames[month - 1], \ | |
4481 | 'year': year, \ | |
4482 | 'weekNum': rjust(`weekNum`, 2), \ | |
4483 | 'weekNumMonth': weekNumMonth, \ | |
4484 | 'weekNumMonth2': weekNumMonth2, \ | |
4485 | 'dayNum': rjust(`dayNum`, 3) } | |
4486 | ||
4487 | print " { { %(day)s, "\ | |
4488 | "wxDateTime::%(month)s, "\ | |
4489 | "%(year)d }, "\ | |
4490 | "%(weekNum)s, "\ | |
4491 | "%(weekNumMonth)s, "\ | |
4492 | "%(weekNumMonth2)s, "\ | |
239446b4 | 4493 | "%(dayNum)s }," % data |
9d9b7755 | 4494 | |
239446b4 VZ |
4495 | */ |
4496 | static const WeekNumberTestData weekNumberTestDates[] = | |
4497 | { | |
9d9b7755 VZ |
4498 | { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 }, |
4499 | { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 }, | |
4500 | { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 }, | |
4501 | { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 }, | |
4502 | { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 }, | |
4503 | { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 }, | |
4504 | { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 }, | |
4505 | { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 }, | |
4506 | { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 }, | |
4507 | { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 }, | |
4508 | { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 }, | |
4509 | { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 }, | |
4510 | { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 }, | |
4511 | { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 }, | |
4512 | { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 }, | |
4513 | { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 }, | |
4514 | { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 }, | |
4515 | { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 }, | |
4516 | { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 }, | |
4517 | { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 }, | |
239446b4 VZ |
4518 | }; |
4519 | ||
4520 | for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ ) | |
4521 | { | |
4522 | const WeekNumberTestData& wn = weekNumberTestDates[n]; | |
4523 | const Date& d = wn.date; | |
4524 | ||
4525 | wxDateTime dt = d.DT(); | |
4526 | ||
9d9b7755 VZ |
4527 | wxDateTime::wxDateTime_t |
4528 | week = dt.GetWeekOfYear(wxDateTime::Monday_First), | |
4529 | wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First), | |
4530 | wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First), | |
4531 | dnum = dt.GetDayOfYear(); | |
239446b4 | 4532 | |
456ae26d | 4533 | wxPrintf(_T("%s: the day number is %d"), d.FormatDate().c_str(), dnum); |
239446b4 VZ |
4534 | if ( dnum == wn.dnum ) |
4535 | { | |
456ae26d | 4536 | wxPrintf(_T(" (ok)")); |
239446b4 VZ |
4537 | } |
4538 | else | |
4539 | { | |
456ae26d | 4540 | wxPrintf(_T(" (ERROR: should be %d)"), wn.dnum); |
239446b4 VZ |
4541 | } |
4542 | ||
2b5f62a0 VZ |
4543 | wxPrintf(_T(", week in month = %d"), wmon); |
4544 | if ( wmon != wn.wmon ) | |
9d9b7755 | 4545 | { |
456ae26d | 4546 | wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon); |
9d9b7755 VZ |
4547 | } |
4548 | ||
456ae26d | 4549 | wxPrintf(_T(" or %d"), wmon2); |
9d9b7755 VZ |
4550 | if ( wmon2 == wn.wmon2 ) |
4551 | { | |
456ae26d | 4552 | wxPrintf(_T(" (ok)")); |
9d9b7755 VZ |
4553 | } |
4554 | else | |
4555 | { | |
456ae26d | 4556 | wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon2); |
9d9b7755 VZ |
4557 | } |
4558 | ||
2b5f62a0 VZ |
4559 | wxPrintf(_T(", week in year = %d"), week); |
4560 | if ( week != wn.week ) | |
239446b4 | 4561 | { |
2b5f62a0 | 4562 | wxPrintf(_T(" (ERROR: should be %d)"), wn.week); |
239446b4 | 4563 | } |
2b5f62a0 VZ |
4564 | |
4565 | wxPutchar(_T('\n')); | |
4566 | ||
4567 | wxDateTime dt2(1, wxDateTime::Jan, d.year); | |
4568 | dt2.SetToTheWeek(wn.week, dt.GetWeekDay()); | |
4569 | if ( dt2 != dt ) | |
239446b4 | 4570 | { |
2b5f62a0 VZ |
4571 | Date d2; |
4572 | d2.Init(dt2.GetTm()); | |
4573 | wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"), | |
4574 | d2.FormatDate().c_str()); | |
239446b4 VZ |
4575 | } |
4576 | } | |
4577 | } | |
4578 | ||
4579 | // test DST calculations | |
4580 | static void TestTimeDST() | |
4581 | { | |
456ae26d | 4582 | wxPuts(_T("\n*** wxDateTime DST test ***")); |
239446b4 | 4583 | |
456ae26d | 4584 | wxPrintf(_T("DST is%s in effect now.\n\n"), |
e9d2bb6f | 4585 | wxDateTime::Now().IsDST() ? wxEmptyString : _T(" not")); |
239446b4 VZ |
4586 | |
4587 | // taken from http://www.energy.ca.gov/daylightsaving.html | |
4588 | static const Date datesDST[2][2004 - 1900 + 1] = | |
4589 | { | |
4590 | { | |
4591 | { 1, wxDateTime::Apr, 1990 }, | |
4592 | { 7, wxDateTime::Apr, 1991 }, | |
4593 | { 5, wxDateTime::Apr, 1992 }, | |
4594 | { 4, wxDateTime::Apr, 1993 }, | |
4595 | { 3, wxDateTime::Apr, 1994 }, | |
4596 | { 2, wxDateTime::Apr, 1995 }, | |
4597 | { 7, wxDateTime::Apr, 1996 }, | |
4598 | { 6, wxDateTime::Apr, 1997 }, | |
4599 | { 5, wxDateTime::Apr, 1998 }, | |
4600 | { 4, wxDateTime::Apr, 1999 }, | |
4601 | { 2, wxDateTime::Apr, 2000 }, | |
4602 | { 1, wxDateTime::Apr, 2001 }, | |
4603 | { 7, wxDateTime::Apr, 2002 }, | |
4604 | { 6, wxDateTime::Apr, 2003 }, | |
4605 | { 4, wxDateTime::Apr, 2004 }, | |
4606 | }, | |
4607 | { | |
4608 | { 28, wxDateTime::Oct, 1990 }, | |
4609 | { 27, wxDateTime::Oct, 1991 }, | |
4610 | { 25, wxDateTime::Oct, 1992 }, | |
4611 | { 31, wxDateTime::Oct, 1993 }, | |
4612 | { 30, wxDateTime::Oct, 1994 }, | |
4613 | { 29, wxDateTime::Oct, 1995 }, | |
4614 | { 27, wxDateTime::Oct, 1996 }, | |
4615 | { 26, wxDateTime::Oct, 1997 }, | |
4616 | { 25, wxDateTime::Oct, 1998 }, | |
4617 | { 31, wxDateTime::Oct, 1999 }, | |
4618 | { 29, wxDateTime::Oct, 2000 }, | |
4619 | { 28, wxDateTime::Oct, 2001 }, | |
4620 | { 27, wxDateTime::Oct, 2002 }, | |
4621 | { 26, wxDateTime::Oct, 2003 }, | |
4622 | { 31, wxDateTime::Oct, 2004 }, | |
4623 | } | |
4624 | }; | |
4625 | ||
4626 | int year; | |
4627 | for ( year = 1990; year < 2005; year++ ) | |
4628 | { | |
4629 | wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA), | |
4630 | dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA); | |
4631 | ||
456ae26d VZ |
4632 | wxPrintf(_T("DST period in the US for year %d: from %s to %s"), |
4633 | year, dtBegin.Format().c_str(), dtEnd.Format().c_str()); | |
239446b4 VZ |
4634 | |
4635 | size_t n = year - 1990; | |
4636 | const Date& dBegin = datesDST[0][n]; | |
4637 | const Date& dEnd = datesDST[1][n]; | |
97e0ceea | 4638 | |
239446b4 VZ |
4639 | if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) ) |
4640 | { | |
456ae26d | 4641 | wxPuts(_T(" (ok)")); |
239446b4 VZ |
4642 | } |
4643 | else | |
4644 | { | |
456ae26d VZ |
4645 | wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"), |
4646 | wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day, | |
4647 | wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day); | |
239446b4 VZ |
4648 | } |
4649 | } | |
4650 | ||
e9d2bb6f | 4651 | wxPuts(wxEmptyString); |
239446b4 VZ |
4652 | |
4653 | for ( year = 1990; year < 2005; year++ ) | |
4654 | { | |
456ae26d VZ |
4655 | wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"), |
4656 | year, | |
4657 | wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(), | |
4658 | wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str()); | |
239446b4 VZ |
4659 | } |
4660 | } | |
4661 | ||
68ee7c47 VZ |
4662 | // test wxDateTime -> text conversion |
4663 | static void TestTimeFormat() | |
4664 | { | |
456ae26d | 4665 | wxPuts(_T("\n*** wxDateTime formatting test ***")); |
68ee7c47 | 4666 | |
b38e2f7d VZ |
4667 | // some information may be lost during conversion, so store what kind |
4668 | // of info should we recover after a round trip | |
4669 | enum CompareKind | |
68ee7c47 | 4670 | { |
b38e2f7d VZ |
4671 | CompareNone, // don't try comparing |
4672 | CompareBoth, // dates and times should be identical | |
4673 | CompareDate, // dates only | |
4674 | CompareTime // time only | |
4675 | }; | |
4676 | ||
4677 | static const struct | |
4678 | { | |
4679 | CompareKind compareKind; | |
456ae26d | 4680 | const wxChar *format; |
b38e2f7d VZ |
4681 | } formatTestFormats[] = |
4682 | { | |
456ae26d VZ |
4683 | { CompareBoth, _T("---> %c") }, |
4684 | { CompareDate, _T("Date is %A, %d of %B, in year %Y") }, | |
4685 | { CompareBoth, _T("Date is %x, time is %X") }, | |
4686 | { CompareTime, _T("Time is %H:%M:%S or %I:%M:%S %p") }, | |
4687 | { CompareNone, _T("The day of year: %j, the week of year: %W") }, | |
ee3ef281 | 4688 | { CompareDate, _T("ISO date without separators: %Y%m%d") }, |
68ee7c47 VZ |
4689 | }; |
4690 | ||
4691 | static const Date formatTestDates[] = | |
4692 | { | |
68ee7c47 VZ |
4693 | { 29, wxDateTime::May, 1976, 18, 30, 00 }, |
4694 | { 31, wxDateTime::Dec, 1999, 23, 30, 00 }, | |
b38e2f7d VZ |
4695 | #if 0 |
4696 | // this test can't work for other centuries because it uses two digit | |
4697 | // years in formats, so don't even try it | |
68ee7c47 VZ |
4698 | { 29, wxDateTime::May, 2076, 18, 30, 00 }, |
4699 | { 29, wxDateTime::Feb, 2400, 02, 15, 25 }, | |
4700 | { 01, wxDateTime::Jan, -52, 03, 16, 47 }, | |
b38e2f7d | 4701 | #endif |
68ee7c47 VZ |
4702 | }; |
4703 | ||
4704 | // an extra test (as it doesn't depend on date, don't do it in the loop) | |
456ae26d | 4705 | wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str()); |
68ee7c47 | 4706 | |
b38e2f7d | 4707 | for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ ) |
68ee7c47 | 4708 | { |
e9d2bb6f | 4709 | wxPuts(wxEmptyString); |
68ee7c47 | 4710 | |
b38e2f7d | 4711 | wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT(); |
68ee7c47 VZ |
4712 | for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) |
4713 | { | |
b38e2f7d | 4714 | wxString s = dt.Format(formatTestFormats[n].format); |
456ae26d | 4715 | wxPrintf(_T("%s"), s.c_str()); |
f0f951fa | 4716 | |
b38e2f7d VZ |
4717 | // what can we recover? |
4718 | int kind = formatTestFormats[n].compareKind; | |
4719 | ||
f0f951fa VZ |
4720 | // convert back |
4721 | wxDateTime dt2; | |
b38e2f7d | 4722 | const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format); |
f0f951fa VZ |
4723 | if ( !result ) |
4724 | { | |
b38e2f7d VZ |
4725 | // converion failed - should it have? |
4726 | if ( kind == CompareNone ) | |
456ae26d | 4727 | wxPuts(_T(" (ok)")); |
b38e2f7d | 4728 | else |
456ae26d | 4729 | wxPuts(_T(" (ERROR: conversion back failed)")); |
f0f951fa VZ |
4730 | } |
4731 | else if ( *result ) | |
4732 | { | |
4733 | // should have parsed the entire string | |
456ae26d | 4734 | wxPuts(_T(" (ERROR: conversion back stopped too soon)")); |
f0f951fa | 4735 | } |
f0f951fa VZ |
4736 | else |
4737 | { | |
cab8f76e | 4738 | bool equal = false; // suppress compilaer warning |
b38e2f7d VZ |
4739 | switch ( kind ) |
4740 | { | |
4741 | case CompareBoth: | |
4742 | equal = dt2 == dt; | |
4743 | break; | |
4744 | ||
4745 | case CompareDate: | |
4746 | equal = dt.IsSameDate(dt2); | |
4747 | break; | |
4748 | ||
4749 | case CompareTime: | |
4750 | equal = dt.IsSameTime(dt2); | |
4751 | break; | |
4752 | } | |
4753 | ||
4754 | if ( !equal ) | |
4755 | { | |
456ae26d | 4756 | wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"), |
b38e2f7d VZ |
4757 | dt2.Format().c_str(), dt.Format().c_str()); |
4758 | } | |
4759 | else | |
4760 | { | |
456ae26d | 4761 | wxPuts(_T(" (ok)")); |
b38e2f7d | 4762 | } |
f0f951fa | 4763 | } |
68ee7c47 VZ |
4764 | } |
4765 | } | |
4766 | } | |
4767 | ||
97e0ceea VZ |
4768 | // test text -> wxDateTime conversion |
4769 | static void TestTimeParse() | |
4770 | { | |
456ae26d | 4771 | wxPuts(_T("\n*** wxDateTime parse test ***")); |
97e0ceea VZ |
4772 | |
4773 | struct ParseTestData | |
4774 | { | |
456ae26d | 4775 | const wxChar *format; |
97e0ceea VZ |
4776 | Date date; |
4777 | bool good; | |
4778 | }; | |
4779 | ||
4780 | static const ParseTestData parseTestDates[] = | |
4781 | { | |
cab8f76e VZ |
4782 | { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, true }, |
4783 | { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, true }, | |
97e0ceea VZ |
4784 | }; |
4785 | ||
4786 | for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) | |
4787 | { | |
456ae26d | 4788 | const wxChar *format = parseTestDates[n].format; |
97e0ceea | 4789 | |
456ae26d | 4790 | wxPrintf(_T("%s => "), format); |
97e0ceea VZ |
4791 | |
4792 | wxDateTime dt; | |
4793 | if ( dt.ParseRfc822Date(format) ) | |
4794 | { | |
456ae26d | 4795 | wxPrintf(_T("%s "), dt.Format().c_str()); |
97e0ceea VZ |
4796 | |
4797 | if ( parseTestDates[n].good ) | |
4798 | { | |
4799 | wxDateTime dtReal = parseTestDates[n].date.DT(); | |
4800 | if ( dt == dtReal ) | |
4801 | { | |
456ae26d | 4802 | wxPuts(_T("(ok)")); |
97e0ceea VZ |
4803 | } |
4804 | else | |
4805 | { | |
456ae26d | 4806 | wxPrintf(_T("(ERROR: should be %s)\n"), dtReal.Format().c_str()); |
97e0ceea VZ |
4807 | } |
4808 | } | |
4809 | else | |
4810 | { | |
456ae26d | 4811 | wxPuts(_T("(ERROR: bad format)")); |
97e0ceea VZ |
4812 | } |
4813 | } | |
4814 | else | |
4815 | { | |
456ae26d | 4816 | wxPrintf(_T("bad format (%s)\n"), |
97e0ceea VZ |
4817 | parseTestDates[n].good ? "ERROR" : "ok"); |
4818 | } | |
4819 | } | |
4820 | } | |
4821 | ||
b92fd37c | 4822 | static void TestDateTimeInteractive() |
9d9b7755 | 4823 | { |
456ae26d | 4824 | wxPuts(_T("\n*** interactive wxDateTime tests ***")); |
9d9b7755 | 4825 | |
456ae26d | 4826 | wxChar buf[128]; |
9d9b7755 VZ |
4827 | |
4828 | for ( ;; ) | |
4829 | { | |
456ae26d VZ |
4830 | wxPrintf(_T("Enter a date: ")); |
4831 | if ( !wxFgets(buf, WXSIZEOF(buf), stdin) ) | |
9d9b7755 VZ |
4832 | break; |
4833 | ||
f6bcfd97 | 4834 | // kill the last '\n' |
456ae26d | 4835 | buf[wxStrlen(buf) - 1] = 0; |
f6bcfd97 | 4836 | |
9d9b7755 | 4837 | wxDateTime dt; |
456ae26d | 4838 | const wxChar *p = dt.ParseDate(buf); |
f6bcfd97 | 4839 | if ( !p ) |
9d9b7755 | 4840 | { |
456ae26d | 4841 | wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf); |
9d9b7755 VZ |
4842 | |
4843 | continue; | |
4844 | } | |
f6bcfd97 BP |
4845 | else if ( *p ) |
4846 | { | |
456ae26d | 4847 | wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf); |
f6bcfd97 | 4848 | } |
9d9b7755 | 4849 | |
456ae26d VZ |
4850 | wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"), |
4851 | dt.Format(_T("%b %d, %Y")).c_str(), | |
4852 | dt.GetDayOfYear(), | |
4853 | dt.GetWeekOfMonth(wxDateTime::Monday_First), | |
4854 | dt.GetWeekOfMonth(wxDateTime::Sunday_First), | |
4855 | dt.GetWeekOfYear(wxDateTime::Monday_First)); | |
9d9b7755 VZ |
4856 | } |
4857 | ||
456ae26d | 4858 | wxPuts(_T("\n*** done ***")); |
9d9b7755 VZ |
4859 | } |
4860 | ||
f6bcfd97 BP |
4861 | static void TestTimeMS() |
4862 | { | |
456ae26d | 4863 | wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***")); |
f6bcfd97 BP |
4864 | |
4865 | wxDateTime dt1 = wxDateTime::Now(), | |
4866 | dt2 = wxDateTime::UNow(); | |
4867 | ||
456ae26d VZ |
4868 | wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str()); |
4869 | wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str()); | |
4870 | wxPrintf(_T("Dummy loop: ")); | |
3ca6a5f0 BP |
4871 | for ( int i = 0; i < 6000; i++ ) |
4872 | { | |
4873 | //for ( int j = 0; j < 10; j++ ) | |
4874 | { | |
4875 | wxString s; | |
456ae26d | 4876 | s.Printf(_T("%g"), sqrt(i)); |
3ca6a5f0 BP |
4877 | } |
4878 | ||
4879 | if ( !(i % 100) ) | |
e9d2bb6f | 4880 | wxPutchar('.'); |
3ca6a5f0 | 4881 | } |
456ae26d | 4882 | wxPuts(_T(", done")); |
3ca6a5f0 BP |
4883 | |
4884 | dt1 = dt2; | |
4885 | dt2 = wxDateTime::UNow(); | |
456ae26d | 4886 | wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str()); |
3ca6a5f0 | 4887 | |
456ae26d | 4888 | wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str()); |
f6bcfd97 | 4889 | |
456ae26d | 4890 | wxPuts(_T("\n*** done ***")); |
f6bcfd97 BP |
4891 | } |
4892 | ||
9d9b7755 VZ |
4893 | static void TestTimeArithmetics() |
4894 | { | |
456ae26d | 4895 | wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***")); |
9d9b7755 | 4896 | |
f6bcfd97 | 4897 | static const struct ArithmData |
9d9b7755 | 4898 | { |
456ae26d | 4899 | ArithmData(const wxDateSpan& sp, const wxChar *nam) |
f6bcfd97 BP |
4900 | : span(sp), name(nam) { } |
4901 | ||
9d9b7755 | 4902 | wxDateSpan span; |
456ae26d | 4903 | const wxChar *name; |
7c968cee | 4904 | } testArithmData[] = |
9d9b7755 | 4905 | { |
456ae26d VZ |
4906 | ArithmData(wxDateSpan::Day(), _T("day")), |
4907 | ArithmData(wxDateSpan::Week(), _T("week")), | |
4908 | ArithmData(wxDateSpan::Month(), _T("month")), | |
4909 | ArithmData(wxDateSpan::Year(), _T("year")), | |
4910 | ArithmData(wxDateSpan(1, 2, 3, 4), _T("year, 2 months, 3 weeks, 4 days")), | |
9d9b7755 | 4911 | }; |
7c968cee | 4912 | |
9d9b7755 VZ |
4913 | wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2; |
4914 | ||
4915 | for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ ) | |
4916 | { | |
4917 | wxDateSpan span = testArithmData[n].span; | |
4918 | dt1 = dt + span; | |
4919 | dt2 = dt - span; | |
4920 | ||
456ae26d VZ |
4921 | const wxChar *name = testArithmData[n].name; |
4922 | wxPrintf(_T("%s + %s = %s, %s - %s = %s\n"), | |
9d9b7755 VZ |
4923 | dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(), |
4924 | dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str()); | |
4925 | ||
456ae26d | 4926 | wxPrintf(_T("Going back: %s"), (dt1 - span).FormatISODate().c_str()); |
9d9b7755 VZ |
4927 | if ( dt1 - span == dt ) |
4928 | { | |
456ae26d | 4929 | wxPuts(_T(" (ok)")); |
9d9b7755 VZ |
4930 | } |
4931 | else | |
4932 | { | |
456ae26d | 4933 | wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str()); |
9d9b7755 VZ |
4934 | } |
4935 | ||
456ae26d | 4936 | wxPrintf(_T("Going forward: %s"), (dt2 + span).FormatISODate().c_str()); |
9d9b7755 VZ |
4937 | if ( dt2 + span == dt ) |
4938 | { | |
456ae26d | 4939 | wxPuts(_T(" (ok)")); |
9d9b7755 VZ |
4940 | } |
4941 | else | |
4942 | { | |
456ae26d | 4943 | wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str()); |
9d9b7755 VZ |
4944 | } |
4945 | ||
456ae26d | 4946 | wxPrintf(_T("Double increment: %s"), (dt2 + 2*span).FormatISODate().c_str()); |
9d9b7755 VZ |
4947 | if ( dt2 + 2*span == dt1 ) |
4948 | { | |
456ae26d | 4949 | wxPuts(_T(" (ok)")); |
9d9b7755 VZ |
4950 | } |
4951 | else | |
4952 | { | |
456ae26d | 4953 | wxPrintf(_T(" (ERROR: should be %s)\n"), dt2.FormatISODate().c_str()); |
9d9b7755 VZ |
4954 | } |
4955 | ||
e9d2bb6f | 4956 | wxPuts(wxEmptyString); |
9d9b7755 VZ |
4957 | } |
4958 | } | |
4959 | ||
0de868d9 VZ |
4960 | static void TestTimeHolidays() |
4961 | { | |
456ae26d | 4962 | wxPuts(_T("\n*** testing wxDateTimeHolidayAuthority ***\n")); |
0de868d9 VZ |
4963 | |
4964 | wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm(); | |
4965 | wxDateTime dtStart(1, tm.mon, tm.year), | |
4966 | dtEnd = dtStart.GetLastMonthDay(); | |
4967 | ||
4968 | wxDateTimeArray hol; | |
4969 | wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol); | |
4970 | ||
456ae26d | 4971 | const wxChar *format = _T("%d-%b-%Y (%a)"); |
0de868d9 | 4972 | |
456ae26d | 4973 | wxPrintf(_T("All holidays between %s and %s:\n"), |
0de868d9 VZ |
4974 | dtStart.Format(format).c_str(), dtEnd.Format(format).c_str()); |
4975 | ||
4976 | size_t count = hol.GetCount(); | |
4977 | for ( size_t n = 0; n < count; n++ ) | |
4978 | { | |
456ae26d | 4979 | wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str()); |
0de868d9 VZ |
4980 | } |
4981 | ||
e9d2bb6f | 4982 | wxPuts(wxEmptyString); |
0de868d9 VZ |
4983 | } |
4984 | ||
f6bcfd97 BP |
4985 | static void TestTimeZoneBug() |
4986 | { | |
456ae26d | 4987 | wxPuts(_T("\n*** testing for DST/timezone bug ***\n")); |
f6bcfd97 BP |
4988 | |
4989 | wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000); | |
4990 | for ( int i = 0; i < 31; i++ ) | |
4991 | { | |
456ae26d | 4992 | wxPrintf(_T("Date %s: week day %s.\n"), |
f6bcfd97 BP |
4993 | date.Format(_T("%d-%m-%Y")).c_str(), |
4994 | date.GetWeekDayName(date.GetWeekDay()).c_str()); | |
4995 | ||
4996 | date += wxDateSpan::Day(); | |
4997 | } | |
4998 | ||
e9d2bb6f | 4999 | wxPuts(wxEmptyString); |
f6bcfd97 BP |
5000 | } |
5001 | ||
df05cdc5 VZ |
5002 | static void TestTimeSpanFormat() |
5003 | { | |
456ae26d | 5004 | wxPuts(_T("\n*** wxTimeSpan tests ***")); |
df05cdc5 | 5005 | |
456ae26d | 5006 | static const wxChar *formats[] = |
df05cdc5 VZ |
5007 | { |
5008 | _T("(default) %H:%M:%S"), | |
5009 | _T("%E weeks and %D days"), | |
5010 | _T("%l milliseconds"), | |
5011 | _T("(with ms) %H:%M:%S:%l"), | |
5012 | _T("100%% of minutes is %M"), // test "%%" | |
5013 | _T("%D days and %H hours"), | |
a8625337 | 5014 | _T("or also %S seconds"), |
df05cdc5 VZ |
5015 | }; |
5016 | ||
5017 | wxTimeSpan ts1(1, 2, 3, 4), | |
5018 | ts2(111, 222, 333); | |
5019 | for ( size_t n = 0; n < WXSIZEOF(formats); n++ ) | |
5020 | { | |
456ae26d | 5021 | wxPrintf(_T("ts1 = %s\tts2 = %s\n"), |
df05cdc5 VZ |
5022 | ts1.Format(formats[n]).c_str(), |
5023 | ts2.Format(formats[n]).c_str()); | |
5024 | } | |
5025 | ||
e9d2bb6f | 5026 | wxPuts(wxEmptyString); |
df05cdc5 VZ |
5027 | } |
5028 | ||
d31b7b68 | 5029 | #endif // TEST_DATETIME |
b76b015e | 5030 | |
39937656 VZ |
5031 | // ---------------------------------------------------------------------------- |
5032 | // wxTextInput/OutputStream | |
5033 | // ---------------------------------------------------------------------------- | |
5034 | ||
5035 | #ifdef TEST_TEXTSTREAM | |
5036 | ||
5037 | #include "wx/txtstrm.h" | |
5038 | #include "wx/wfstream.h" | |
5039 | ||
5040 | static void TestTextInputStream() | |
5041 | { | |
5042 | wxPuts(_T("\n*** wxTextInputStream test ***")); | |
5043 | ||
e9d2bb6f DS |
5044 | wxString filename = _T("testdata.fc"); |
5045 | wxFileInputStream fsIn(filename); | |
39937656 VZ |
5046 | if ( !fsIn.Ok() ) |
5047 | { | |
5048 | wxPuts(_T("ERROR: couldn't open file.")); | |
5049 | } | |
5050 | else | |
5051 | { | |
5052 | wxTextInputStream tis(fsIn); | |
5053 | ||
5054 | size_t line = 1; | |
5055 | for ( ;; ) | |
5056 | { | |
5057 | const wxString s = tis.ReadLine(); | |
5058 | ||
5059 | // line could be non empty if the last line of the file isn't | |
5060 | // terminated with EOL | |
5061 | if ( fsIn.Eof() && s.empty() ) | |
5062 | break; | |
5063 | ||
5064 | wxPrintf(_T("Line %d: %s\n"), line++, s.c_str()); | |
5065 | } | |
5066 | } | |
5067 | } | |
5068 | ||
5069 | #endif // TEST_TEXTSTREAM | |
5070 | ||
e87271f3 VZ |
5071 | // ---------------------------------------------------------------------------- |
5072 | // threads | |
5073 | // ---------------------------------------------------------------------------- | |
5074 | ||
5075 | #ifdef TEST_THREADS | |
5076 | ||
e84010cf | 5077 | #include "wx/thread.h" |
37667812 | 5078 | |
bbfa0322 VZ |
5079 | static size_t gs_counter = (size_t)-1; |
5080 | static wxCriticalSection gs_critsect; | |
c112e100 | 5081 | static wxSemaphore gs_cond; |
bbfa0322 | 5082 | |
b568d04f | 5083 | class MyJoinableThread : public wxThread |
bbfa0322 VZ |
5084 | { |
5085 | public: | |
b568d04f VZ |
5086 | MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE) |
5087 | { m_n = n; Create(); } | |
bbfa0322 VZ |
5088 | |
5089 | // thread execution starts here | |
b568d04f | 5090 | virtual ExitCode Entry(); |
bbfa0322 | 5091 | |
b568d04f VZ |
5092 | private: |
5093 | size_t m_n; | |
bbfa0322 VZ |
5094 | }; |
5095 | ||
b568d04f | 5096 | wxThread::ExitCode MyJoinableThread::Entry() |
bbfa0322 | 5097 | { |
b568d04f VZ |
5098 | unsigned long res = 1; |
5099 | for ( size_t n = 1; n < m_n; n++ ) | |
5100 | { | |
5101 | res *= n; | |
5102 | ||
5103 | // it's a loooong calculation :-) | |
5104 | Sleep(100); | |
5105 | } | |
bbfa0322 | 5106 | |
b568d04f | 5107 | return (ExitCode)res; |
bbfa0322 VZ |
5108 | } |
5109 | ||
b568d04f VZ |
5110 | class MyDetachedThread : public wxThread |
5111 | { | |
5112 | public: | |
456ae26d | 5113 | MyDetachedThread(size_t n, wxChar ch) |
fcc3d7cb VZ |
5114 | { |
5115 | m_n = n; | |
5116 | m_ch = ch; | |
cab8f76e | 5117 | m_cancelled = false; |
fcc3d7cb VZ |
5118 | |
5119 | Create(); | |
5120 | } | |
b568d04f VZ |
5121 | |
5122 | // thread execution starts here | |
5123 | virtual ExitCode Entry(); | |
5124 | ||
5125 | // and stops here | |
5126 | virtual void OnExit(); | |
5127 | ||
5128 | private: | |
9fc3ad34 | 5129 | size_t m_n; // number of characters to write |
456ae26d | 5130 | wxChar m_ch; // character to write |
fcc3d7cb | 5131 | |
cab8f76e | 5132 | bool m_cancelled; // false if we exit normally |
b568d04f VZ |
5133 | }; |
5134 | ||
5135 | wxThread::ExitCode MyDetachedThread::Entry() | |
bbfa0322 VZ |
5136 | { |
5137 | { | |
5138 | wxCriticalSectionLocker lock(gs_critsect); | |
5139 | if ( gs_counter == (size_t)-1 ) | |
5140 | gs_counter = 1; | |
5141 | else | |
5142 | gs_counter++; | |
5143 | } | |
5144 | ||
9fc3ad34 | 5145 | for ( size_t n = 0; n < m_n; n++ ) |
bbfa0322 VZ |
5146 | { |
5147 | if ( TestDestroy() ) | |
fcc3d7cb | 5148 | { |
cab8f76e | 5149 | m_cancelled = true; |
fcc3d7cb | 5150 | |
bbfa0322 | 5151 | break; |
fcc3d7cb | 5152 | } |
bbfa0322 | 5153 | |
e9d2bb6f | 5154 | wxPutchar(m_ch); |
bbfa0322 VZ |
5155 | fflush(stdout); |
5156 | ||
5157 | wxThread::Sleep(100); | |
5158 | } | |
5159 | ||
b568d04f | 5160 | return 0; |
bbfa0322 VZ |
5161 | } |
5162 | ||
b568d04f | 5163 | void MyDetachedThread::OnExit() |
bbfa0322 | 5164 | { |
456ae26d | 5165 | wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId()); |
9fc3ad34 | 5166 | |
bbfa0322 | 5167 | wxCriticalSectionLocker lock(gs_critsect); |
fcc3d7cb | 5168 | if ( !--gs_counter && !m_cancelled ) |
c112e100 | 5169 | gs_cond.Post(); |
bbfa0322 VZ |
5170 | } |
5171 | ||
2f2f3e2a | 5172 | static void TestDetachedThreads() |
9fc3ad34 | 5173 | { |
456ae26d | 5174 | wxPuts(_T("\n*** Testing detached threads ***")); |
9fc3ad34 VZ |
5175 | |
5176 | static const size_t nThreads = 3; | |
5177 | MyDetachedThread *threads[nThreads]; | |
5178 | size_t n; | |
5179 | for ( n = 0; n < nThreads; n++ ) | |
5180 | { | |
5181 | threads[n] = new MyDetachedThread(10, 'A' + n); | |
5182 | } | |
5183 | ||
5184 | threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY); | |
5185 | threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY); | |
5186 | ||
5187 | for ( n = 0; n < nThreads; n++ ) | |
5188 | { | |
5189 | threads[n]->Run(); | |
5190 | } | |
5191 | ||
5192 | // wait until all threads terminate | |
5193 | gs_cond.Wait(); | |
5194 | ||
e9d2bb6f | 5195 | wxPuts(wxEmptyString); |
9fc3ad34 VZ |
5196 | } |
5197 | ||
2f2f3e2a | 5198 | static void TestJoinableThreads() |
9fc3ad34 | 5199 | { |
456ae26d | 5200 | wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***")); |
9fc3ad34 VZ |
5201 | |
5202 | // calc 10! in the background | |
5203 | MyJoinableThread thread(10); | |
5204 | thread.Run(); | |
5205 | ||
456ae26d | 5206 | wxPrintf(_T("\nThread terminated with exit code %lu.\n"), |
5bc1deeb | 5207 | (unsigned long)thread.Wait()); |
9fc3ad34 VZ |
5208 | } |
5209 | ||
2f2f3e2a | 5210 | static void TestThreadSuspend() |
9fc3ad34 | 5211 | { |
456ae26d | 5212 | wxPuts(_T("\n*** Testing thread suspend/resume functions ***")); |
2f02cb89 VZ |
5213 | |
5214 | MyDetachedThread *thread = new MyDetachedThread(15, 'X'); | |
9fc3ad34 VZ |
5215 | |
5216 | thread->Run(); | |
5217 | ||
5218 | // this is for this demo only, in a real life program we'd use another | |
5219 | // condition variable which would be signaled from wxThread::Entry() to | |
5220 | // tell us that the thread really started running - but here just wait a | |
5221 | // bit and hope that it will be enough (the problem is, of course, that | |
5222 | // the thread might still not run when we call Pause() which will result | |
5223 | // in an error) | |
5224 | wxThread::Sleep(300); | |
5225 | ||
5226 | for ( size_t n = 0; n < 3; n++ ) | |
5227 | { | |
5228 | thread->Pause(); | |
5229 | ||
456ae26d | 5230 | wxPuts(_T("\nThread suspended")); |
9fc3ad34 VZ |
5231 | if ( n > 0 ) |
5232 | { | |
5233 | // don't sleep but resume immediately the first time | |
5234 | wxThread::Sleep(300); | |
5235 | } | |
456ae26d | 5236 | wxPuts(_T("Going to resume the thread")); |
9fc3ad34 VZ |
5237 | |
5238 | thread->Resume(); | |
5239 | } | |
5240 | ||
456ae26d | 5241 | wxPuts(_T("Waiting until it terminates now")); |
4c460b34 | 5242 | |
9fc3ad34 VZ |
5243 | // wait until the thread terminates |
5244 | gs_cond.Wait(); | |
5245 | ||
e9d2bb6f | 5246 | wxPuts(wxEmptyString); |
9fc3ad34 VZ |
5247 | } |
5248 | ||
2f2f3e2a | 5249 | static void TestThreadDelete() |
2f02cb89 VZ |
5250 | { |
5251 | // As above, using Sleep() is only for testing here - we must use some | |
5252 | // synchronisation object instead to ensure that the thread is still | |
5253 | // running when we delete it - deleting a detached thread which already | |
5254 | // terminated will lead to a crash! | |
5255 | ||
456ae26d | 5256 | wxPuts(_T("\n*** Testing thread delete function ***")); |
2f02cb89 | 5257 | |
4c460b34 VZ |
5258 | MyDetachedThread *thread0 = new MyDetachedThread(30, 'W'); |
5259 | ||
5260 | thread0->Delete(); | |
5261 | ||
456ae26d | 5262 | wxPuts(_T("\nDeleted a thread which didn't start to run yet.")); |
4c460b34 | 5263 | |
2f02cb89 VZ |
5264 | MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y'); |
5265 | ||
5266 | thread1->Run(); | |
5267 | ||
5268 | wxThread::Sleep(300); | |
5269 | ||
5270 | thread1->Delete(); | |
5271 | ||
456ae26d | 5272 | wxPuts(_T("\nDeleted a running thread.")); |
2f02cb89 VZ |
5273 | |
5274 | MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z'); | |
5275 | ||
5276 | thread2->Run(); | |
5277 | ||
5278 | wxThread::Sleep(300); | |
5279 | ||
5280 | thread2->Pause(); | |
5281 | ||
5282 | thread2->Delete(); | |
5283 | ||
456ae26d | 5284 | wxPuts(_T("\nDeleted a sleeping thread.")); |
2f02cb89 | 5285 | |
4c460b34 VZ |
5286 | MyJoinableThread thread3(20); |
5287 | thread3.Run(); | |
2f02cb89 | 5288 | |
4c460b34 | 5289 | thread3.Delete(); |
2f02cb89 | 5290 | |
456ae26d | 5291 | wxPuts(_T("\nDeleted a joinable thread.")); |
2f02cb89 | 5292 | |
4c460b34 VZ |
5293 | MyJoinableThread thread4(2); |
5294 | thread4.Run(); | |
2f02cb89 VZ |
5295 | |
5296 | wxThread::Sleep(300); | |
5297 | ||
4c460b34 | 5298 | thread4.Delete(); |
2f02cb89 | 5299 | |
456ae26d | 5300 | wxPuts(_T("\nDeleted a joinable thread which already terminated.")); |
2f02cb89 | 5301 | |
e9d2bb6f | 5302 | wxPuts(wxEmptyString); |
2f02cb89 VZ |
5303 | } |
5304 | ||
2f2f3e2a VZ |
5305 | class MyWaitingThread : public wxThread |
5306 | { | |
5307 | public: | |
c112e100 | 5308 | MyWaitingThread( wxMutex *mutex, wxCondition *condition ) |
2f2f3e2a | 5309 | { |
c112e100 | 5310 | m_mutex = mutex; |
2f2f3e2a VZ |
5311 | m_condition = condition; |
5312 | ||
5313 | Create(); | |
5314 | } | |
5315 | ||
5316 | virtual ExitCode Entry() | |
5317 | { | |
456ae26d | 5318 | wxPrintf(_T("Thread %lu has started running.\n"), GetId()); |
2f2f3e2a VZ |
5319 | fflush(stdout); |
5320 | ||
c112e100 | 5321 | gs_cond.Post(); |
2f2f3e2a | 5322 | |
456ae26d | 5323 | wxPrintf(_T("Thread %lu starts to wait...\n"), GetId()); |
2f2f3e2a VZ |
5324 | fflush(stdout); |
5325 | ||
c112e100 | 5326 | m_mutex->Lock(); |
2f2f3e2a | 5327 | m_condition->Wait(); |
c112e100 | 5328 | m_mutex->Unlock(); |
2f2f3e2a | 5329 | |
456ae26d | 5330 | wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId()); |
2f2f3e2a VZ |
5331 | fflush(stdout); |
5332 | ||
5333 | return 0; | |
5334 | } | |
5335 | ||
5336 | private: | |
c112e100 | 5337 | wxMutex *m_mutex; |
2f2f3e2a VZ |
5338 | wxCondition *m_condition; |
5339 | }; | |
5340 | ||
5341 | static void TestThreadConditions() | |
5342 | { | |
c112e100 VZ |
5343 | wxMutex mutex; |
5344 | wxCondition condition(mutex); | |
2f2f3e2a | 5345 | |
8d5eff60 VZ |
5346 | // otherwise its difficult to understand which log messages pertain to |
5347 | // which condition | |
456ae26d | 5348 | //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"), |
c112e100 | 5349 | // condition.GetId(), gs_cond.GetId()); |
8d5eff60 | 5350 | |
2f2f3e2a | 5351 | // create and launch threads |
60ce696e | 5352 | MyWaitingThread *threads[10]; |
2f2f3e2a VZ |
5353 | |
5354 | size_t n; | |
5355 | for ( n = 0; n < WXSIZEOF(threads); n++ ) | |
5356 | { | |
c112e100 | 5357 | threads[n] = new MyWaitingThread( &mutex, &condition ); |
2f2f3e2a VZ |
5358 | } |
5359 | ||
5360 | for ( n = 0; n < WXSIZEOF(threads); n++ ) | |
5361 | { | |
5362 | threads[n]->Run(); | |
5363 | } | |
5364 | ||
5365 | // wait until all threads run | |
456ae26d | 5366 | wxPuts(_T("Main thread is waiting for the other threads to start")); |
2f2f3e2a VZ |
5367 | fflush(stdout); |
5368 | ||
5369 | size_t nRunning = 0; | |
5370 | while ( nRunning < WXSIZEOF(threads) ) | |
5371 | { | |
5372 | gs_cond.Wait(); | |
5373 | ||
2f2f3e2a | 5374 | nRunning++; |
8d5eff60 | 5375 | |
456ae26d | 5376 | wxPrintf(_T("Main thread: %u already running\n"), nRunning); |
8d5eff60 | 5377 | fflush(stdout); |
2f2f3e2a VZ |
5378 | } |
5379 | ||
456ae26d | 5380 | wxPuts(_T("Main thread: all threads started up.")); |
2f2f3e2a VZ |
5381 | fflush(stdout); |
5382 | ||
8d5eff60 VZ |
5383 | wxThread::Sleep(500); |
5384 | ||
60ce696e | 5385 | #if 1 |
8d5eff60 | 5386 | // now wake one of them up |
456ae26d | 5387 | wxPrintf(_T("Main thread: about to signal the condition.\n")); |
2f2f3e2a VZ |
5388 | fflush(stdout); |
5389 | condition.Signal(); | |
8d5eff60 | 5390 | #endif |
2f2f3e2a | 5391 | |
60ce696e VZ |
5392 | wxThread::Sleep(200); |
5393 | ||
8d5eff60 | 5394 | // wake all the (remaining) threads up, so that they can exit |
456ae26d | 5395 | wxPrintf(_T("Main thread: about to broadcast the condition.\n")); |
2f2f3e2a VZ |
5396 | fflush(stdout); |
5397 | condition.Broadcast(); | |
5398 | ||
8d5eff60 VZ |
5399 | // give them time to terminate (dirty!) |
5400 | wxThread::Sleep(500); | |
2f2f3e2a VZ |
5401 | } |
5402 | ||
c112e100 VZ |
5403 | #include "wx/utils.h" |
5404 | ||
5405 | class MyExecThread : public wxThread | |
5406 | { | |
5407 | public: | |
5408 | MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE), | |
5409 | m_command(command) | |
5410 | { | |
5411 | Create(); | |
5412 | } | |
5413 | ||
5414 | virtual ExitCode Entry() | |
5415 | { | |
5416 | return (ExitCode)wxExecute(m_command, wxEXEC_SYNC); | |
5417 | } | |
5418 | ||
5419 | private: | |
5420 | wxString m_command; | |
5421 | }; | |
5422 | ||
5423 | static void TestThreadExec() | |
5424 | { | |
5425 | wxPuts(_T("*** Testing wxExecute interaction with threads ***\n")); | |
5426 | ||
5427 | MyExecThread thread(_T("true")); | |
5428 | thread.Run(); | |
5429 | ||
5430 | wxPrintf(_T("Main program exit code: %ld.\n"), | |
5431 | wxExecute(_T("false"), wxEXEC_SYNC)); | |
5432 | ||
5433 | wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait()); | |
5434 | } | |
5435 | ||
5436 | // semaphore tests | |
5437 | #include "wx/datetime.h" | |
5438 | ||
5439 | class MySemaphoreThread : public wxThread | |
5440 | { | |
5441 | public: | |
5442 | MySemaphoreThread(int i, wxSemaphore *sem) | |
5443 | : wxThread(wxTHREAD_JOINABLE), | |
5444 | m_sem(sem), | |
5445 | m_i(i) | |
5446 | { | |
5447 | Create(); | |
5448 | } | |
5449 | ||
5450 | virtual ExitCode Entry() | |
5451 | { | |
f06ef5f4 VZ |
5452 | wxPrintf(_T("%s: Thread #%d (%ld) starting to wait for semaphore...\n"), |
5453 | wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId()); | |
c112e100 VZ |
5454 | |
5455 | m_sem->Wait(); | |
5456 | ||
f06ef5f4 VZ |
5457 | wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"), |
5458 | wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId()); | |
c112e100 VZ |
5459 | |
5460 | Sleep(1000); | |
5461 | ||
f06ef5f4 VZ |
5462 | wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"), |
5463 | wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId()); | |
c112e100 VZ |
5464 | |
5465 | m_sem->Post(); | |
5466 | ||
5467 | return 0; | |
5468 | } | |
5469 | ||
5470 | private: | |
5471 | wxSemaphore *m_sem; | |
5472 | int m_i; | |
5473 | }; | |
5474 | ||
e9d2bb6f | 5475 | WX_DEFINE_ARRAY_PTR(wxThread *, ArrayThreads); |
c112e100 VZ |
5476 | |
5477 | static void TestSemaphore() | |
5478 | { | |
5479 | wxPuts(_T("*** Testing wxSemaphore class. ***")); | |
5480 | ||
5481 | static const int SEM_LIMIT = 3; | |
5482 | ||
5483 | wxSemaphore sem(SEM_LIMIT, SEM_LIMIT); | |
5484 | ArrayThreads threads; | |
5485 | ||
5486 | for ( int i = 0; i < 3*SEM_LIMIT; i++ ) | |
5487 | { | |
5488 | threads.Add(new MySemaphoreThread(i, &sem)); | |
5489 | threads.Last()->Run(); | |
5490 | } | |
5491 | ||
5492 | for ( size_t n = 0; n < threads.GetCount(); n++ ) | |
5493 | { | |
5494 | threads[n]->Wait(); | |
5495 | delete threads[n]; | |
5496 | } | |
5497 | } | |
5498 | ||
e87271f3 VZ |
5499 | #endif // TEST_THREADS |
5500 | ||
e87271f3 VZ |
5501 | // ---------------------------------------------------------------------------- |
5502 | // entry point | |
5503 | // ---------------------------------------------------------------------------- | |
5504 | ||
daa2c7d9 VZ |
5505 | #ifdef TEST_SNGLINST |
5506 | #include "wx/snglinst.h" | |
5507 | #endif // TEST_SNGLINST | |
5508 | ||
bbfa0322 | 5509 | int main(int argc, char **argv) |
37667812 | 5510 | { |
6f35ed60 | 5511 | wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program"); |
9cb47ea2 | 5512 | |
58b24a56 VZ |
5513 | wxInitializer initializer; |
5514 | if ( !initializer ) | |
37667812 VZ |
5515 | { |
5516 | fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); | |
58b24a56 VZ |
5517 | |
5518 | return -1; | |
5519 | } | |
5520 | ||
5521 | #ifdef TEST_SNGLINST | |
b5299791 VZ |
5522 | wxSingleInstanceChecker checker; |
5523 | if ( checker.Create(_T(".wxconsole.lock")) ) | |
58b24a56 | 5524 | { |
b5299791 VZ |
5525 | if ( checker.IsAnotherRunning() ) |
5526 | { | |
5527 | wxPrintf(_T("Another instance of the program is running, exiting.\n")); | |
58b24a56 | 5528 | |
b5299791 VZ |
5529 | return 1; |
5530 | } | |
37667812 | 5531 | |
b5299791 VZ |
5532 | // wait some time to give time to launch another instance |
5533 | wxPrintf(_T("Press \"Enter\" to continue...")); | |
5534 | wxFgetc(stdin); | |
5535 | } | |
5536 | else // failed to create | |
5537 | { | |
5538 | wxPrintf(_T("Failed to init wxSingleInstanceChecker.\n")); | |
5539 | } | |
58b24a56 VZ |
5540 | #endif // TEST_SNGLINST |
5541 | ||
551fe3a6 VZ |
5542 | #ifdef TEST_CHARSET |
5543 | TestCharset(); | |
5544 | #endif // TEST_CHARSET | |
0de868d9 | 5545 | |
d34bce84 | 5546 | #ifdef TEST_CMDLINE |
31f6de22 VZ |
5547 | TestCmdLineConvert(); |
5548 | ||
5549 | #if wxUSE_CMDLINE_PARSER | |
d34bce84 VZ |
5550 | static const wxCmdLineEntryDesc cmdLineDesc[] = |
5551 | { | |
456ae26d | 5552 | { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"), |
31a06b07 | 5553 | wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, |
456ae26d VZ |
5554 | { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") }, |
5555 | { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") }, | |
d34bce84 | 5556 | |
456ae26d VZ |
5557 | { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file") }, |
5558 | { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("input dir") }, | |
5559 | { wxCMD_LINE_OPTION, _T("s"), _T("size"), _T("output block size"), | |
31a06b07 | 5560 | wxCMD_LINE_VAL_NUMBER }, |
456ae26d | 5561 | { wxCMD_LINE_OPTION, _T("d"), _T("date"), _T("output file date"), |
31a06b07 | 5562 | wxCMD_LINE_VAL_DATE }, |
d34bce84 | 5563 | |
456ae26d | 5564 | { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"), |
d34bce84 VZ |
5565 | wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, |
5566 | ||
5567 | { wxCMD_LINE_NONE } | |
5568 | }; | |
5569 | ||
456ae26d VZ |
5570 | #if wxUSE_UNICODE |
5571 | wxChar **wargv = new wxChar *[argc + 1]; | |
5572 | ||
5573 | { | |
f2cb8a17 JS |
5574 | int n; |
5575 | ||
5576 | for (n = 0; n < argc; n++ ) | |
456ae26d VZ |
5577 | { |
5578 | wxMB2WXbuf warg = wxConvertMB2WX(argv[n]); | |
5579 | wargv[n] = wxStrdup(warg); | |
5580 | } | |
5581 | ||
5582 | wargv[n] = NULL; | |
5583 | } | |
5584 | ||
5585 | #define argv wargv | |
5586 | #endif // wxUSE_UNICODE | |
5587 | ||
d34bce84 VZ |
5588 | wxCmdLineParser parser(cmdLineDesc, argc, argv); |
5589 | ||
456ae26d VZ |
5590 | #if wxUSE_UNICODE |
5591 | { | |
5592 | for ( int n = 0; n < argc; n++ ) | |
5593 | free(wargv[n]); | |
5594 | ||
5595 | delete [] wargv; | |
5596 | } | |
5597 | #endif // wxUSE_UNICODE | |
5598 | ||
5599 | parser.AddOption(_T("project_name"), _T(""), _T("full path to project file"), | |
f6bcfd97 BP |
5600 | wxCMD_LINE_VAL_STRING, |
5601 | wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR); | |
5602 | ||
d34bce84 VZ |
5603 | switch ( parser.Parse() ) |
5604 | { | |
5605 | case -1: | |
456ae26d | 5606 | wxLogMessage(_T("Help was given, terminating.")); |
d34bce84 VZ |
5607 | break; |
5608 | ||
5609 | case 0: | |
5610 | ShowCmdLine(parser); | |
5611 | break; | |
5612 | ||
5613 | default: | |
456ae26d | 5614 | wxLogMessage(_T("Syntax error detected, aborting.")); |
d34bce84 VZ |
5615 | break; |
5616 | } | |
31f6de22 VZ |
5617 | #endif // wxUSE_CMDLINE_PARSER |
5618 | ||
d34bce84 VZ |
5619 | #endif // TEST_CMDLINE |
5620 | ||
1944c6bd | 5621 | #ifdef TEST_DIR |
e9d2bb6f | 5622 | #if TEST_ALL |
99a5af7f | 5623 | TestDirExists(); |
2f0c19d0 | 5624 | TestDirEnum(); |
e9d2bb6f | 5625 | #endif |
2f0c19d0 | 5626 | TestDirTraverse(); |
1944c6bd VZ |
5627 | #endif // TEST_DIR |
5628 | ||
f6bcfd97 BP |
5629 | #ifdef TEST_DLLLOADER |
5630 | TestDllLoad(); | |
5631 | #endif // TEST_DLLLOADER | |
5632 | ||
8fd0d89b VZ |
5633 | #ifdef TEST_ENVIRON |
5634 | TestEnvironment(); | |
5635 | #endif // TEST_ENVIRON | |
5636 | ||
d93c719a VZ |
5637 | #ifdef TEST_EXECUTE |
5638 | TestExecute(); | |
5639 | #endif // TEST_EXECUTE | |
5640 | ||
ee6e1b1d VZ |
5641 | #ifdef TEST_FILECONF |
5642 | TestFileConfRead(); | |
5643 | #endif // TEST_FILECONF | |
5644 | ||
f6bcfd97 BP |
5645 | #ifdef TEST_LIST |
5646 | TestListCtor(); | |
df5168c4 | 5647 | TestList(); |
f6bcfd97 BP |
5648 | #endif // TEST_LIST |
5649 | ||
ec37df57 VZ |
5650 | #ifdef TEST_LOCALE |
5651 | TestDefaultLang(); | |
5652 | #endif // TEST_LOCALE | |
5653 | ||
378b05f7 | 5654 | #ifdef TEST_LOG |
cab8f76e VZ |
5655 | wxPuts(_T("*** Testing wxLog ***")); |
5656 | ||
378b05f7 VZ |
5657 | wxString s; |
5658 | for ( size_t n = 0; n < 8000; n++ ) | |
5659 | { | |
456ae26d | 5660 | s << (wxChar)(_T('A') + (n % 26)); |
378b05f7 VZ |
5661 | } |
5662 | ||
cab8f76e VZ |
5663 | wxLogWarning(_T("The length of the string is %lu"), |
5664 | (unsigned long)s.length()); | |
5665 | ||
378b05f7 | 5666 | wxString msg; |
456ae26d | 5667 | msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str()); |
378b05f7 VZ |
5668 | |
5669 | // this one shouldn't be truncated | |
456ae26d | 5670 | wxPrintf(msg); |
378b05f7 VZ |
5671 | |
5672 | // but this one will because log functions use fixed size buffer | |
b568d04f VZ |
5673 | // (note that it doesn't need '\n' at the end neither - will be added |
5674 | // by wxLog anyhow) | |
456ae26d | 5675 | wxLogMessage(_T("A very very long message 2: '%s', the end!"), s.c_str()); |
378b05f7 VZ |
5676 | #endif // TEST_LOG |
5677 | ||
f6bcfd97 | 5678 | #ifdef TEST_FILE |
e9d2bb6f DS |
5679 | TestFileRead(); |
5680 | TestTextFileRead(); | |
5681 | TestFileCopy(); | |
f6bcfd97 BP |
5682 | #endif // TEST_FILE |
5683 | ||
844f90fb | 5684 | #ifdef TEST_FILENAME |
e9d2bb6f DS |
5685 | TestFileNameConstruction(); |
5686 | TestFileNameMakeRelative(); | |
5687 | TestFileNameMakeAbsolute(); | |
5688 | TestFileNameSplit(); | |
5689 | TestFileNameTemp(); | |
5690 | TestFileNameCwd(); | |
5691 | TestFileNameDirManip(); | |
5692 | TestFileNameComparison(); | |
5693 | TestFileNameOperations(); | |
844f90fb VZ |
5694 | #endif // TEST_FILENAME |
5695 | ||
d56e2b97 VZ |
5696 | #ifdef TEST_FILETIME |
5697 | TestFileGetTimes(); | |
e9d2bb6f | 5698 | #if 0 |
d56e2b97 | 5699 | TestFileSetTimes(); |
e9d2bb6f | 5700 | #endif |
d56e2b97 VZ |
5701 | #endif // TEST_FILETIME |
5702 | ||
07a56e45 VZ |
5703 | #ifdef TEST_FTP |
5704 | wxLog::AddTraceMask(FTP_TRACE_MASK); | |
5705 | if ( TestFtpConnect() ) | |
5706 | { | |
e9d2bb6f | 5707 | #if TEST_ALL |
07a56e45 VZ |
5708 | TestFtpList(); |
5709 | TestFtpDownload(); | |
5710 | TestFtpMisc(); | |
daa2c7d9 | 5711 | TestFtpFileSize(); |
07a56e45 | 5712 | TestFtpUpload(); |
e9d2bb6f | 5713 | #endif |
daa2c7d9 | 5714 | |
e9d2bb6f | 5715 | #if TEST_INTERACTIVE |
daa2c7d9 | 5716 | TestFtpInteractive(); |
e9d2bb6f | 5717 | #endif |
07a56e45 VZ |
5718 | } |
5719 | //else: connecting to the FTP server failed | |
5720 | ||
e9d2bb6f | 5721 | #if 0 |
07a56e45 | 5722 | TestFtpWuFtpd(); |
e9d2bb6f | 5723 | #endif |
07a56e45 VZ |
5724 | #endif // TEST_FTP |
5725 | ||
2c8e4738 VZ |
5726 | #ifdef TEST_HASH |
5727 | TestHash(); | |
5728 | #endif // TEST_HASH | |
5729 | ||
0508ba2a MB |
5730 | #ifdef TEST_HASHMAP |
5731 | TestHashMap(); | |
5732 | #endif // TEST_HASHMAP | |
5733 | ||
8142d704 MB |
5734 | #ifdef TEST_HASHSET |
5735 | TestHashSet(); | |
5736 | #endif // TEST_HASHSET | |
5737 | ||
696e1ea0 | 5738 | #ifdef TEST_MIME |
f6bcfd97 | 5739 | wxLog::AddTraceMask(_T("mime")); |
e9d2bb6f | 5740 | #if TEST_ALL |
f6bcfd97 | 5741 | TestMimeEnum(); |
c7ce8392 | 5742 | TestMimeOverride(); |
f06ef5f4 | 5743 | TestMimeAssociate(); |
e9d2bb6f | 5744 | #endif |
f06ef5f4 | 5745 | TestMimeFilename(); |
696e1ea0 VZ |
5746 | #endif // TEST_MIME |
5747 | ||
89e60357 | 5748 | #ifdef TEST_INFO_FUNCTIONS |
e9d2bb6f | 5749 | #if TEST_ALL |
3a994742 VZ |
5750 | TestOsInfo(); |
5751 | TestUserInfo(); | |
19f45995 | 5752 | |
e9d2bb6f | 5753 | #if TEST_INTERACTIVE |
19f45995 | 5754 | TestDiskInfo(); |
e9d2bb6f DS |
5755 | #endif |
5756 | #endif | |
89e60357 VZ |
5757 | #endif // TEST_INFO_FUNCTIONS |
5758 | ||
39189b9d VZ |
5759 | #ifdef TEST_PATHLIST |
5760 | TestPathList(); | |
5761 | #endif // TEST_PATHLIST | |
5762 | ||
8d5eff60 VZ |
5763 | #ifdef TEST_ODBC |
5764 | TestDbOpen(); | |
5765 | #endif // TEST_ODBC | |
5766 | ||
7aeebdcd VZ |
5767 | #ifdef TEST_PRINTF |
5768 | TestPrintf(); | |
5769 | #endif // TEST_PRINTF | |
5770 | ||
7ba4fbeb | 5771 | #ifdef TEST_REGCONF |
e9d2bb6f DS |
5772 | #if 0 |
5773 | TestRegConfWrite(); | |
5774 | #endif | |
0aa29b6b | 5775 | TestRegConfRead(); |
7ba4fbeb VZ |
5776 | #endif // TEST_REGCONF |
5777 | ||
bc10103e VS |
5778 | #if defined TEST_REGEX && TEST_INTERACTIVE |
5779 | TestRegExInteractive(); | |
5780 | #endif // defined TEST_REGEX && TEST_INTERACTIVE | |
07a56e45 | 5781 | |
6dfec4b8 | 5782 | #ifdef TEST_REGISTRY |
daa2c7d9 | 5783 | TestRegistryRead(); |
6ba63600 | 5784 | TestRegistryAssociation(); |
6dfec4b8 VZ |
5785 | #endif // TEST_REGISTRY |
5786 | ||
2c8e4738 | 5787 | #ifdef TEST_SOCKETS |
daa2c7d9 VZ |
5788 | TestSocketServer(); |
5789 | TestSocketClient(); | |
2c8e4738 VZ |
5790 | #endif // TEST_SOCKETS |
5791 | ||
83141d3a | 5792 | #ifdef TEST_STREAMS |
e9d2bb6f | 5793 | #if TEST_ALL |
99a5af7f | 5794 | TestFileStream(); |
e9d2bb6f | 5795 | #endif |
99a5af7f | 5796 | TestMemoryStream(); |
83141d3a VZ |
5797 | #endif // TEST_STREAMS |
5798 | ||
39937656 VZ |
5799 | #ifdef TEST_TEXTSTREAM |
5800 | TestTextInputStream(); | |
5801 | #endif // TEST_TEXTSTREAM | |
5802 | ||
8d5eff60 VZ |
5803 | #ifdef TEST_THREADS |
5804 | int nCPUs = wxThread::GetCPUCount(); | |
456ae26d | 5805 | wxPrintf(_T("This system has %d CPUs\n"), nCPUs); |
8d5eff60 VZ |
5806 | if ( nCPUs != -1 ) |
5807 | wxThread::SetConcurrency(nCPUs); | |
5808 | ||
5bc1deeb VZ |
5809 | TestJoinableThreads(); |
5810 | ||
e9d2bb6f | 5811 | #if TEST_ALL |
8d5eff60 | 5812 | TestJoinableThreads(); |
5bc1deeb | 5813 | TestDetachedThreads(); |
8d5eff60 VZ |
5814 | TestThreadSuspend(); |
5815 | TestThreadDelete(); | |
c112e100 VZ |
5816 | TestThreadConditions(); |
5817 | TestThreadExec(); | |
7aeebdcd | 5818 | TestSemaphore(); |
e9d2bb6f | 5819 | #endif |
8d5eff60 VZ |
5820 | #endif // TEST_THREADS |
5821 | ||
d31b7b68 VZ |
5822 | #ifdef TEST_TIMER |
5823 | TestStopWatch(); | |
5824 | #endif // TEST_TIMER | |
5825 | ||
5826 | #ifdef TEST_DATETIME | |
e9d2bb6f | 5827 | #if TEST_ALL |
9d9b7755 VZ |
5828 | TestTimeSet(); |
5829 | TestTimeStatic(); | |
5830 | TestTimeRange(); | |
5831 | TestTimeZones(); | |
5832 | TestTimeTicks(); | |
5833 | TestTimeJDN(); | |
5834 | TestTimeDST(); | |
5835 | TestTimeWDays(); | |
5836 | TestTimeWNumber(); | |
5837 | TestTimeParse(); | |
9d9b7755 | 5838 | TestTimeArithmetics(); |
f6bcfd97 BP |
5839 | TestTimeHolidays(); |
5840 | TestTimeFormat(); | |
daa2c7d9 | 5841 | TestTimeSpanFormat(); |
3ca6a5f0 | 5842 | TestTimeMS(); |
f6bcfd97 BP |
5843 | |
5844 | TestTimeZoneBug(); | |
e9d2bb6f | 5845 | #endif |
2b5f62a0 | 5846 | |
e9d2bb6f | 5847 | #if TEST_INTERACTIVE |
b92fd37c | 5848 | TestDateTimeInteractive(); |
e9d2bb6f | 5849 | #endif |
d31b7b68 | 5850 | #endif // TEST_DATETIME |
b76b015e | 5851 | |
df5168c4 MB |
5852 | #ifdef TEST_SCOPEGUARD |
5853 | TestScopeGuard(); | |
5854 | #endif | |
5855 | ||
551fe3a6 | 5856 | #ifdef TEST_USLEEP |
456ae26d | 5857 | wxPuts(_T("Sleeping for 3 seconds... z-z-z-z-z...")); |
551fe3a6 VZ |
5858 | wxUsleep(3000); |
5859 | #endif // TEST_USLEEP | |
5860 | ||
f6bcfd97 | 5861 | #ifdef TEST_VCARD |
f6bcfd97 BP |
5862 | TestVCardRead(); |
5863 | TestVCardWrite(); | |
5864 | #endif // TEST_VCARD | |
5865 | ||
0e2c5534 VZ |
5866 | #ifdef TEST_VOLUME |
5867 | TestFSVolume(); | |
5868 | #endif // TEST_VOLUME | |
5869 | ||
f6bcfd97 BP |
5870 | #ifdef TEST_WCHAR |
5871 | TestUtf8(); | |
ac511156 | 5872 | TestEncodingConverter(); |
f6bcfd97 BP |
5873 | #endif // TEST_WCHAR |
5874 | ||
5875 | #ifdef TEST_ZIP | |
daa2c7d9 | 5876 | TestZipStreamRead(); |
2ca8b884 | 5877 | TestZipFileSystem(); |
f6bcfd97 BP |
5878 | #endif // TEST_ZIP |
5879 | ||
e9d2bb6f DS |
5880 | wxUnusedVar(argc); |
5881 | wxUnusedVar(argv); | |
37667812 VZ |
5882 | return 0; |
5883 | } | |
f6bcfd97 | 5884 |