]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
1. wxFTP works (somehow)
[wxWidgets.git] / samples / console / console.cpp
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
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // without this pragma, the stupid compiler precompiles #defines below so that
27 // changing them doesn't "take place" later!
28 #ifdef __VISUALC__
29 #pragma hdrstop
30 #endif
31
32 // ----------------------------------------------------------------------------
33 // conditional compilation
34 // ----------------------------------------------------------------------------
35
36 // what to test (in alphabetic order)?
37
38 //#define TEST_ARRAYS
39 //#define TEST_CMDLINE
40 //#define TEST_DATETIME
41 //#define TEST_DIR
42 //#define TEST_EXECUTE
43 //#define TEST_FILECONF
44 //#define TEST_HASH
45 //#define TEST_LOG
46 //#define TEST_LONGLONG
47 //#define TEST_MIME
48 #define TEST_SOCKETS
49 //#define TEST_STRINGS
50 //#define TEST_THREADS
51 //#define TEST_TIMER
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // helper functions
59 // ----------------------------------------------------------------------------
60
61 #if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
62
63 // replace TABs with \t and CRs with \n
64 static wxString MakePrintable(const wxChar *s)
65 {
66 wxString str(s);
67 (void)str.Replace(_T("\t"), _T("\\t"));
68 (void)str.Replace(_T("\n"), _T("\\n"));
69 (void)str.Replace(_T("\r"), _T("\\r"));
70
71 return str;
72 }
73
74 #endif // MakePrintable() is used
75
76 // ----------------------------------------------------------------------------
77 // wxCmdLineParser
78 // ----------------------------------------------------------------------------
79
80 #ifdef TEST_CMDLINE
81
82 #include <wx/cmdline.h>
83 #include <wx/datetime.h>
84
85 static void ShowCmdLine(const wxCmdLineParser& parser)
86 {
87 wxString s = "Input files: ";
88
89 size_t count = parser.GetParamCount();
90 for ( size_t param = 0; param < count; param++ )
91 {
92 s << parser.GetParam(param) << ' ';
93 }
94
95 s << '\n'
96 << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n'
97 << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n';
98
99 wxString strVal;
100 long lVal;
101 wxDateTime dt;
102 if ( parser.Found("o", &strVal) )
103 s << "Output file:\t" << strVal << '\n';
104 if ( parser.Found("i", &strVal) )
105 s << "Input dir:\t" << strVal << '\n';
106 if ( parser.Found("s", &lVal) )
107 s << "Size:\t" << lVal << '\n';
108 if ( parser.Found("d", &dt) )
109 s << "Date:\t" << dt.FormatISODate() << '\n';
110
111 wxLogMessage(s);
112 }
113
114 #endif // TEST_CMDLINE
115
116 // ----------------------------------------------------------------------------
117 // wxDir
118 // ----------------------------------------------------------------------------
119
120 #ifdef TEST_DIR
121
122 #include <wx/dir.h>
123
124 static void TestDirEnumHelper(wxDir& dir,
125 int flags = wxDIR_DEFAULT,
126 const wxString& filespec = wxEmptyString)
127 {
128 wxString filename;
129
130 if ( !dir.IsOpened() )
131 return;
132
133 bool cont = dir.GetFirst(&filename, filespec, flags);
134 while ( cont )
135 {
136 printf("\t%s\n", filename.c_str());
137
138 cont = dir.GetNext(&filename);
139 }
140
141 puts("");
142 }
143
144 static void TestDirEnum()
145 {
146 wxDir dir(wxGetCwd());
147
148 puts("Enumerating everything in current directory:");
149 TestDirEnumHelper(dir);
150
151 puts("Enumerating really everything in current directory:");
152 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
153
154 puts("Enumerating object files in current directory:");
155 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o");
156
157 puts("Enumerating directories in current directory:");
158 TestDirEnumHelper(dir, wxDIR_DIRS);
159
160 puts("Enumerating files in current directory:");
161 TestDirEnumHelper(dir, wxDIR_FILES);
162
163 puts("Enumerating files including hidden in current directory:");
164 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
165
166 #ifdef __UNIX__
167 dir.Open("/");
168 #elif defined(__WXMSW__)
169 dir.Open("c:\\");
170 #else
171 #error "don't know where the root directory is"
172 #endif
173
174 puts("Enumerating everything in root directory:");
175 TestDirEnumHelper(dir, wxDIR_DEFAULT);
176
177 puts("Enumerating directories in root directory:");
178 TestDirEnumHelper(dir, wxDIR_DIRS);
179
180 puts("Enumerating files in root directory:");
181 TestDirEnumHelper(dir, wxDIR_FILES);
182
183 puts("Enumerating files including hidden in root directory:");
184 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
185
186 puts("Enumerating files in non existing directory:");
187 wxDir dirNo("nosuchdir");
188 TestDirEnumHelper(dirNo);
189 }
190
191 #endif // TEST_DIR
192
193 // ----------------------------------------------------------------------------
194 // wxExecute
195 // ----------------------------------------------------------------------------
196
197 #ifdef TEST_EXECUTE
198
199 #include <wx/utils.h>
200
201 static void TestExecute()
202 {
203 puts("*** testing wxExecute ***");
204
205 #ifdef __UNIX__
206 #define COMMAND "echo hi"
207 #define SHELL_COMMAND "echo hi from shell"
208 #define REDIRECT_COMMAND "date"
209 #elif defined(__WXMSW__)
210 #define COMMAND "command.com -c 'echo hi'"
211 #define SHELL_COMMAND "echo hi"
212 #define REDIRECT_COMMAND COMMAND
213 #else
214 #error "no command to exec"
215 #endif // OS
216
217 printf("Testing wxShell: ");
218 fflush(stdout);
219 if ( wxShell(SHELL_COMMAND) )
220 puts("Ok.");
221 else
222 puts("ERROR.");
223
224 printf("Testing wxExecute: ");
225 fflush(stdout);
226 if ( wxExecute(COMMAND, TRUE /* sync */) == 0 )
227 puts("Ok.");
228 else
229 puts("ERROR.");
230
231 #if 0 // no, it doesn't work (yet?)
232 printf("Testing async wxExecute: ");
233 fflush(stdout);
234 if ( wxExecute(COMMAND) != 0 )
235 puts("Ok (command launched).");
236 else
237 puts("ERROR.");
238 #endif // 0
239
240 printf("Testing wxExecute with redirection:\n");
241 wxArrayString output;
242 if ( wxExecute(REDIRECT_COMMAND, output) != 0 )
243 {
244 puts("ERROR.");
245 }
246 else
247 {
248 size_t count = output.GetCount();
249 for ( size_t n = 0; n < count; n++ )
250 {
251 printf("\t%s\n", output[n].c_str());
252 }
253
254 puts("Ok.");
255 }
256 }
257
258 #endif // TEST_EXECUTE
259
260 // ----------------------------------------------------------------------------
261 // wxFileConfig
262 // ----------------------------------------------------------------------------
263
264 #ifdef TEST_FILECONF
265
266 #include <wx/confbase.h>
267 #include <wx/fileconf.h>
268
269 static const struct FileConfTestData
270 {
271 const wxChar *name; // value name
272 const wxChar *value; // the value from the file
273 } fcTestData[] =
274 {
275 { _T("value1"), _T("one") },
276 { _T("value2"), _T("two") },
277 { _T("novalue"), _T("default") },
278 };
279
280 static void TestFileConfRead()
281 {
282 puts("*** testing wxFileConfig loading/reading ***");
283
284 wxFileConfig fileconf(_T("test"), wxEmptyString,
285 _T("testdata.fc"), wxEmptyString,
286 wxCONFIG_USE_RELATIVE_PATH);
287
288 // test simple reading
289 puts("\nReading config file:");
290 wxString defValue(_T("default")), value;
291 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
292 {
293 const FileConfTestData& data = fcTestData[n];
294 value = fileconf.Read(data.name, defValue);
295 printf("\t%s = %s ", data.name, value.c_str());
296 if ( value == data.value )
297 {
298 puts("(ok)");
299 }
300 else
301 {
302 printf("(ERROR: should be %s)\n", data.value);
303 }
304 }
305
306 // test enumerating the entries
307 puts("\nEnumerating all root entries:");
308 long dummy;
309 wxString name;
310 bool cont = fileconf.GetFirstEntry(name, dummy);
311 while ( cont )
312 {
313 printf("\t%s = %s\n",
314 name.c_str(),
315 fileconf.Read(name.c_str(), _T("ERROR")).c_str());
316
317 cont = fileconf.GetNextEntry(name, dummy);
318 }
319 }
320
321 #endif // TEST_FILECONF
322
323 // ----------------------------------------------------------------------------
324 // wxHashTable
325 // ----------------------------------------------------------------------------
326
327 #ifdef TEST_HASH
328
329 #include <wx/hash.h>
330
331 struct Foo
332 {
333 Foo(int n_) { n = n_; count++; }
334 ~Foo() { count--; }
335
336 int n;
337
338 static size_t count;
339 };
340
341 size_t Foo::count = 0;
342
343 WX_DECLARE_LIST(Foo, wxListFoos);
344 WX_DECLARE_HASH(Foo, wxListFoos, wxHashFoos);
345
346 #include <wx/listimpl.cpp>
347
348 WX_DEFINE_LIST(wxListFoos);
349
350 static void TestHash()
351 {
352 puts("*** Testing wxHashTable ***\n");
353
354 {
355 wxHashFoos hash;
356 hash.DeleteContents(TRUE);
357
358 printf("Hash created: %u foos in hash, %u foos totally\n",
359 hash.GetCount(), Foo::count);
360
361 static const int hashTestData[] =
362 {
363 0, 1, 17, -2, 2, 4, -4, 345, 3, 3, 2, 1,
364 };
365
366 size_t n;
367 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
368 {
369 hash.Put(hashTestData[n], n, new Foo(n));
370 }
371
372 printf("Hash filled: %u foos in hash, %u foos totally\n",
373 hash.GetCount(), Foo::count);
374
375 puts("Hash access test:");
376 for ( n = 0; n < WXSIZEOF(hashTestData); n++ )
377 {
378 printf("\tGetting element with key %d, value %d: ",
379 hashTestData[n], n);
380 Foo *foo = hash.Get(hashTestData[n], n);
381 if ( !foo )
382 {
383 printf("ERROR, not found.\n");
384 }
385 else
386 {
387 printf("%d (%s)\n", foo->n,
388 (size_t)foo->n == n ? "ok" : "ERROR");
389 }
390 }
391
392 printf("\nTrying to get an element not in hash: ");
393
394 if ( hash.Get(1234) || hash.Get(1, 0) )
395 {
396 puts("ERROR: found!");
397 }
398 else
399 {
400 puts("ok (not found)");
401 }
402 }
403
404 printf("Hash destroyed: %u foos left\n", Foo::count);
405 }
406
407 #endif // TEST_HASH
408
409 // ----------------------------------------------------------------------------
410 // MIME types
411 // ----------------------------------------------------------------------------
412
413 #ifdef TEST_MIME
414
415 #include <wx/mimetype.h>
416
417 static void TestMimeEnum()
418 {
419 wxMimeTypesManager mimeTM;
420 wxArrayString mimetypes;
421
422 size_t count = mimeTM.EnumAllFileTypes(mimetypes);
423
424 printf("*** All %u known filetypes: ***\n", count);
425
426 wxArrayString exts;
427 wxString desc;
428
429 for ( size_t n = 0; n < count; n++ )
430 {
431 wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]);
432 if ( !filetype )
433 {
434 printf("nothing known about the filetype '%s'!\n",
435 mimetypes[n].c_str());
436 continue;
437 }
438
439 filetype->GetDescription(&desc);
440 filetype->GetExtensions(exts);
441
442 filetype->GetIcon(NULL);
443
444 wxString extsAll;
445 for ( size_t e = 0; e < exts.GetCount(); e++ )
446 {
447 if ( e > 0 )
448 extsAll << _T(", ");
449 extsAll += exts[e];
450 }
451
452 printf("\t%s: %s (%s)\n",
453 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
454 }
455 }
456
457 #endif // TEST_MIME
458
459 // ----------------------------------------------------------------------------
460 // long long
461 // ----------------------------------------------------------------------------
462
463 #ifdef TEST_LONGLONG
464
465 #include <wx/longlong.h>
466 #include <wx/timer.h>
467
468 // make a 64 bit number from 4 16 bit ones
469 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
470
471 // get a random 64 bit number
472 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
473
474 #if wxUSE_LONGLONG_WX
475 inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
476 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
477 inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
478 { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
479 #endif // wxUSE_LONGLONG_WX
480
481 static void TestSpeed()
482 {
483 static const long max = 100000000;
484 long n;
485
486 {
487 wxStopWatch sw;
488
489 long l = 0;
490 for ( n = 0; n < max; n++ )
491 {
492 l += n;
493 }
494
495 printf("Summing longs took %ld milliseconds.\n", sw.Time());
496 }
497
498 #if wxUSE_LONGLONG_NATIVE
499 {
500 wxStopWatch sw;
501
502 wxLongLong_t l = 0;
503 for ( n = 0; n < max; n++ )
504 {
505 l += n;
506 }
507
508 printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time());
509 }
510 #endif // wxUSE_LONGLONG_NATIVE
511
512 {
513 wxStopWatch sw;
514
515 wxLongLong l;
516 for ( n = 0; n < max; n++ )
517 {
518 l += n;
519 }
520
521 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
522 }
523 }
524
525 static void TestLongLongConversion()
526 {
527 puts("*** Testing wxLongLong conversions ***\n");
528
529 wxLongLong a;
530 size_t nTested = 0;
531 for ( size_t n = 0; n < 100000; n++ )
532 {
533 a = RAND_LL();
534
535 #if wxUSE_LONGLONG_NATIVE
536 wxLongLongNative b(a.GetHi(), a.GetLo());
537
538 wxASSERT_MSG( a == b, "conversions failure" );
539 #else
540 puts("Can't do it without native long long type, test skipped.");
541
542 return;
543 #endif // wxUSE_LONGLONG_NATIVE
544
545 if ( !(nTested % 1000) )
546 {
547 putchar('.');
548 fflush(stdout);
549 }
550
551 nTested++;
552 }
553
554 puts(" done!");
555 }
556
557 static void TestMultiplication()
558 {
559 puts("*** Testing wxLongLong multiplication ***\n");
560
561 wxLongLong a, b;
562 size_t nTested = 0;
563 for ( size_t n = 0; n < 100000; n++ )
564 {
565 a = RAND_LL();
566 b = RAND_LL();
567
568 #if wxUSE_LONGLONG_NATIVE
569 wxLongLongNative aa(a.GetHi(), a.GetLo());
570 wxLongLongNative bb(b.GetHi(), b.GetLo());
571
572 wxASSERT_MSG( a*b == aa*bb, "multiplication failure" );
573 #else // !wxUSE_LONGLONG_NATIVE
574 puts("Can't do it without native long long type, test skipped.");
575
576 return;
577 #endif // wxUSE_LONGLONG_NATIVE
578
579 if ( !(nTested % 1000) )
580 {
581 putchar('.');
582 fflush(stdout);
583 }
584
585 nTested++;
586 }
587
588 puts(" done!");
589 }
590
591 static void TestDivision()
592 {
593 puts("*** Testing wxLongLong division ***\n");
594
595 wxLongLong q, r;
596 size_t nTested = 0;
597 for ( size_t n = 0; n < 100000; n++ )
598 {
599 // get a random wxLongLong (shifting by 12 the MSB ensures that the
600 // multiplication will not overflow)
601 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
602
603 // get a random long (not wxLongLong for now) to divide it with
604 long l = rand();
605 q = ll / l;
606 r = ll % l;
607
608 #if wxUSE_LONGLONG_NATIVE
609 wxLongLongNative m(ll.GetHi(), ll.GetLo());
610
611 wxLongLongNative p = m / l, s = m % l;
612 wxASSERT_MSG( q == p && r == s, "division failure" );
613 #else // !wxUSE_LONGLONG_NATIVE
614 // verify the result
615 wxASSERT_MSG( ll == q*l + r, "division failure" );
616 #endif // wxUSE_LONGLONG_NATIVE
617
618 if ( !(nTested % 1000) )
619 {
620 putchar('.');
621 fflush(stdout);
622 }
623
624 nTested++;
625 }
626
627 puts(" done!");
628 }
629
630 static void TestAddition()
631 {
632 puts("*** Testing wxLongLong addition ***\n");
633
634 wxLongLong a, b, c;
635 size_t nTested = 0;
636 for ( size_t n = 0; n < 100000; n++ )
637 {
638 a = RAND_LL();
639 b = RAND_LL();
640 c = a + b;
641
642 #if wxUSE_LONGLONG_NATIVE
643 wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
644 wxLongLongNative(b.GetHi(), b.GetLo()),
645 "addition failure" );
646 #else // !wxUSE_LONGLONG_NATIVE
647 wxASSERT_MSG( c - b == a, "addition failure" );
648 #endif // wxUSE_LONGLONG_NATIVE
649
650 if ( !(nTested % 1000) )
651 {
652 putchar('.');
653 fflush(stdout);
654 }
655
656 nTested++;
657 }
658
659 puts(" done!");
660 }
661
662 static void TestBitOperations()
663 {
664 puts("*** Testing wxLongLong bit operation ***\n");
665
666 wxLongLong a, c;
667 size_t nTested = 0;
668 for ( size_t n = 0; n < 100000; n++ )
669 {
670 a = RAND_LL();
671
672 #if wxUSE_LONGLONG_NATIVE
673 for ( size_t n = 0; n < 33; n++ )
674 {
675 wxLongLongNative b(a.GetHi(), a.GetLo());
676
677 b >>= n;
678 c = a >> n;
679
680 wxASSERT_MSG( b == c, "bit shift failure" );
681
682 b = wxLongLongNative(a.GetHi(), a.GetLo()) << n;
683 c = a << n;
684
685 wxASSERT_MSG( b == c, "bit shift failure" );
686 }
687
688 #else // !wxUSE_LONGLONG_NATIVE
689 puts("Can't do it without native long long type, test skipped.");
690
691 return;
692 #endif // wxUSE_LONGLONG_NATIVE
693
694 if ( !(nTested % 1000) )
695 {
696 putchar('.');
697 fflush(stdout);
698 }
699
700 nTested++;
701 }
702
703 puts(" done!");
704 }
705
706 #undef MAKE_LL
707 #undef RAND_LL
708
709 #endif // TEST_LONGLONG
710
711 // ----------------------------------------------------------------------------
712 // sockets
713 // ----------------------------------------------------------------------------
714
715 #ifdef TEST_SOCKETS
716
717 #include <wx/socket.h>
718 #include <wx/protocol/protocol.h>
719 #include <wx/protocol/ftp.h>
720 #include <wx/protocol/http.h>
721
722 static void TestSocketServer()
723 {
724 puts("*** Testing wxSocketServer ***\n");
725
726 // we want to launch a server
727 wxIPV4address addr;
728 addr.Service(3000);
729
730 wxSocketServer *server = new wxSocketServer(addr);
731 if ( !server->Ok() )
732 {
733 puts("ERROR: failed to bind");
734 }
735 }
736
737 static void TestSocketClient()
738 {
739 puts("*** Testing wxSocketClient ***\n");
740
741 static const char *hostname = "www.wxwindows.org";
742
743 wxIPV4address addr;
744 addr.Hostname(hostname);
745 addr.Service(80);
746
747 printf("--- Attempting to connect to %s:80...\n", hostname);
748
749 wxSocketClient client;
750 if ( !client.Connect(addr) )
751 {
752 printf("ERROR: failed to connect to %s\n", hostname);
753 }
754 else
755 {
756 printf("--- Connected to %s:%u...\n",
757 addr.Hostname().c_str(), addr.Service());
758
759 char buf[8192];
760
761 // could use simply "GET" here I suppose
762 wxString cmdGet =
763 wxString::Format("GET http://%s/\r\n", hostname);
764 client.Write(cmdGet, cmdGet.length());
765 printf("--- Sent command '%s' to the server\n",
766 MakePrintable(cmdGet).c_str());
767 client.Read(buf, WXSIZEOF(buf));
768 printf("--- Server replied:\n%s", buf);
769 }
770 }
771
772 static void TestProtocolFtp()
773 {
774 puts("*** Testing wxFTP ***\n");
775
776 wxLog::AddTraceMask(_T("ftp"));
777
778 static const char *hostname = "ftp.wxwindows.org";
779
780 printf("--- Attempting to connect to %s:21...\n", hostname);
781
782 wxFTP ftp;
783 if ( !ftp.Connect(hostname) )
784 {
785 printf("ERROR: failed to connect to %s\n", hostname);
786 }
787 else
788 {
789 printf("--- Connected to %s, current directory is '%s'\n",
790 hostname, ftp.Pwd().c_str());
791 if ( !ftp.ChDir(_T("pub")) )
792 {
793 puts("ERROR: failed to cd to pub");
794 }
795
796 wxArrayString files;
797 if ( !ftp.GetList(files) )
798 {
799 puts("ERROR: failed to get list of files");
800 }
801 else
802 {
803 printf("List of files under '%s':\n", ftp.Pwd().c_str());
804 size_t count = files.GetCount();
805 for ( size_t n = 0; n < count; n++ )
806 {
807 printf("\t%s\n", files[n].c_str());
808 }
809 puts("End of the file list");
810 }
811
812 if ( !ftp.ChDir(_T("..")) )
813 {
814 puts("ERROR: failed to cd to ..");
815 }
816
817 static const char *filename = "welcome.msg";
818 wxInputStream *in = ftp.GetInputStream(filename);
819 if ( !in )
820 {
821 puts("ERROR: couldn't get input stream");
822 }
823 else
824 {
825 size_t size = in->StreamSize();
826 printf("Reading file %s (%u bytes)...", filename, size);
827
828 char *data = new char[size];
829 if ( !in->Read(data, size) )
830 {
831 puts("ERROR: read error");
832 }
833 else
834 {
835 printf("\nContents of %s:\n%s\n", filename, data);
836 }
837
838 delete [] data;
839 delete in;
840 }
841 }
842 }
843
844 #endif // TEST_SOCKETS
845
846 // ----------------------------------------------------------------------------
847 // timers
848 // ----------------------------------------------------------------------------
849
850 #ifdef TEST_TIMER
851
852 #include <wx/timer.h>
853 #include <wx/utils.h>
854
855 static void TestStopWatch()
856 {
857 puts("*** Testing wxStopWatch ***\n");
858
859 wxStopWatch sw;
860 printf("Sleeping 3 seconds...");
861 wxSleep(3);
862 printf("\telapsed time: %ldms\n", sw.Time());
863
864 sw.Pause();
865 printf("Sleeping 2 more seconds...");
866 wxSleep(2);
867 printf("\telapsed time: %ldms\n", sw.Time());
868
869 sw.Resume();
870 printf("And 3 more seconds...");
871 wxSleep(3);
872 printf("\telapsed time: %ldms\n", sw.Time());
873
874 wxStopWatch sw2;
875 puts("\nChecking for 'backwards clock' bug...");
876 for ( size_t n = 0; n < 70; n++ )
877 {
878 sw2.Start();
879
880 for ( size_t m = 0; m < 100000; m++ )
881 {
882 if ( sw.Time() < 0 || sw2.Time() < 0 )
883 {
884 puts("\ntime is negative - ERROR!");
885 }
886 }
887
888 putchar('.');
889 }
890
891 puts(", ok.");
892 }
893
894 #endif // TEST_TIMER
895
896 // ----------------------------------------------------------------------------
897 // date time
898 // ----------------------------------------------------------------------------
899
900 #ifdef TEST_DATETIME
901
902 #include <wx/date.h>
903
904 #include <wx/datetime.h>
905
906 // the test data
907 struct Date
908 {
909 wxDateTime::wxDateTime_t day;
910 wxDateTime::Month month;
911 int year;
912 wxDateTime::wxDateTime_t hour, min, sec;
913 double jdn;
914 wxDateTime::WeekDay wday;
915 time_t gmticks, ticks;
916
917 void Init(const wxDateTime::Tm& tm)
918 {
919 day = tm.mday;
920 month = tm.mon;
921 year = tm.year;
922 hour = tm.hour;
923 min = tm.min;
924 sec = tm.sec;
925 jdn = 0.0;
926 gmticks = ticks = -1;
927 }
928
929 wxDateTime DT() const
930 { return wxDateTime(day, month, year, hour, min, sec); }
931
932 bool SameDay(const wxDateTime::Tm& tm) const
933 {
934 return day == tm.mday && month == tm.mon && year == tm.year;
935 }
936
937 wxString Format() const
938 {
939 wxString s;
940 s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
941 hour, min, sec,
942 wxDateTime::GetMonthName(month).c_str(),
943 day,
944 abs(wxDateTime::ConvertYearToBC(year)),
945 year > 0 ? "AD" : "BC");
946 return s;
947 }
948
949 wxString FormatDate() const
950 {
951 wxString s;
952 s.Printf("%02d-%s-%4d%s",
953 day,
954 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
955 abs(wxDateTime::ConvertYearToBC(year)),
956 year > 0 ? "AD" : "BC");
957 return s;
958 }
959 };
960
961 static const Date testDates[] =
962 {
963 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
964 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
965 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
966 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
967 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
968 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
969 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
970 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
971 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
972 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
973 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
974 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
975 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
976 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
977 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
978 };
979
980 // this test miscellaneous static wxDateTime functions
981 static void TestTimeStatic()
982 {
983 puts("\n*** wxDateTime static methods test ***");
984
985 // some info about the current date
986 int year = wxDateTime::GetCurrentYear();
987 printf("Current year %d is %sa leap one and has %d days.\n",
988 year,
989 wxDateTime::IsLeapYear(year) ? "" : "not ",
990 wxDateTime::GetNumberOfDays(year));
991
992 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
993 printf("Current month is '%s' ('%s') and it has %d days\n",
994 wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
995 wxDateTime::GetMonthName(month).c_str(),
996 wxDateTime::GetNumberOfDays(month));
997
998 // leap year logic
999 static const size_t nYears = 5;
1000 static const size_t years[2][nYears] =
1001 {
1002 // first line: the years to test
1003 { 1990, 1976, 2000, 2030, 1984, },
1004
1005 // second line: TRUE if leap, FALSE otherwise
1006 { FALSE, TRUE, TRUE, FALSE, TRUE }
1007 };
1008
1009 for ( size_t n = 0; n < nYears; n++ )
1010 {
1011 int year = years[0][n];
1012 bool should = years[1][n] != 0,
1013 is = wxDateTime::IsLeapYear(year);
1014
1015 printf("Year %d is %sa leap year (%s)\n",
1016 year,
1017 is ? "" : "not ",
1018 should == is ? "ok" : "ERROR");
1019
1020 wxASSERT( should == wxDateTime::IsLeapYear(year) );
1021 }
1022 }
1023
1024 // test constructing wxDateTime objects
1025 static void TestTimeSet()
1026 {
1027 puts("\n*** wxDateTime construction test ***");
1028
1029 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1030 {
1031 const Date& d1 = testDates[n];
1032 wxDateTime dt = d1.DT();
1033
1034 Date d2;
1035 d2.Init(dt.GetTm());
1036
1037 wxString s1 = d1.Format(),
1038 s2 = d2.Format();
1039
1040 printf("Date: %s == %s (%s)\n",
1041 s1.c_str(), s2.c_str(),
1042 s1 == s2 ? "ok" : "ERROR");
1043 }
1044 }
1045
1046 // test time zones stuff
1047 static void TestTimeZones()
1048 {
1049 puts("\n*** wxDateTime timezone test ***");
1050
1051 wxDateTime now = wxDateTime::Now();
1052
1053 printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
1054 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
1055 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
1056 printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
1057 printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
1058 printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
1059
1060 wxDateTime::Tm tm = now.GetTm();
1061 if ( wxDateTime(tm) != now )
1062 {
1063 printf("ERROR: got %s instead of %s\n",
1064 wxDateTime(tm).Format().c_str(), now.Format().c_str());
1065 }
1066 }
1067
1068 // test some minimal support for the dates outside the standard range
1069 static void TestTimeRange()
1070 {
1071 puts("\n*** wxDateTime out-of-standard-range dates test ***");
1072
1073 static const char *fmt = "%d-%b-%Y %H:%M:%S";
1074
1075 printf("Unix epoch:\t%s\n",
1076 wxDateTime(2440587.5).Format(fmt).c_str());
1077 printf("Feb 29, 0: \t%s\n",
1078 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
1079 printf("JDN 0: \t%s\n",
1080 wxDateTime(0.0).Format(fmt).c_str());
1081 printf("Jan 1, 1AD:\t%s\n",
1082 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
1083 printf("May 29, 2099:\t%s\n",
1084 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
1085 }
1086
1087 static void TestTimeTicks()
1088 {
1089 puts("\n*** wxDateTime ticks test ***");
1090
1091 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1092 {
1093 const Date& d = testDates[n];
1094 if ( d.ticks == -1 )
1095 continue;
1096
1097 wxDateTime dt = d.DT();
1098 long ticks = (dt.GetValue() / 1000).ToLong();
1099 printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
1100 if ( ticks == d.ticks )
1101 {
1102 puts(" (ok)");
1103 }
1104 else
1105 {
1106 printf(" (ERROR: should be %ld, delta = %ld)\n",
1107 d.ticks, ticks - d.ticks);
1108 }
1109
1110 dt = d.DT().ToTimezone(wxDateTime::GMT0);
1111 ticks = (dt.GetValue() / 1000).ToLong();
1112 printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
1113 if ( ticks == d.gmticks )
1114 {
1115 puts(" (ok)");
1116 }
1117 else
1118 {
1119 printf(" (ERROR: should be %ld, delta = %ld)\n",
1120 d.gmticks, ticks - d.gmticks);
1121 }
1122 }
1123
1124 puts("");
1125 }
1126
1127 // test conversions to JDN &c
1128 static void TestTimeJDN()
1129 {
1130 puts("\n*** wxDateTime to JDN test ***");
1131
1132 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
1133 {
1134 const Date& d = testDates[n];
1135 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
1136 double jdn = dt.GetJulianDayNumber();
1137
1138 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
1139 if ( jdn == d.jdn )
1140 {
1141 puts(" (ok)");
1142 }
1143 else
1144 {
1145 printf(" (ERROR: should be %f, delta = %f)\n",
1146 d.jdn, jdn - d.jdn);
1147 }
1148 }
1149 }
1150
1151 // test week days computation
1152 static void TestTimeWDays()
1153 {
1154 puts("\n*** wxDateTime weekday test ***");
1155
1156 // test GetWeekDay()
1157 size_t n;
1158 for ( n = 0; n < WXSIZEOF(testDates); n++ )
1159 {
1160 const Date& d = testDates[n];
1161 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
1162
1163 wxDateTime::WeekDay wday = dt.GetWeekDay();
1164 printf("%s is: %s",
1165 d.Format().c_str(),
1166 wxDateTime::GetWeekDayName(wday).c_str());
1167 if ( wday == d.wday )
1168 {
1169 puts(" (ok)");
1170 }
1171 else
1172 {
1173 printf(" (ERROR: should be %s)\n",
1174 wxDateTime::GetWeekDayName(d.wday).c_str());
1175 }
1176 }
1177
1178 puts("");
1179
1180 // test SetToWeekDay()
1181 struct WeekDateTestData
1182 {
1183 Date date; // the real date (precomputed)
1184 int nWeek; // its week index in the month
1185 wxDateTime::WeekDay wday; // the weekday
1186 wxDateTime::Month month; // the month
1187 int year; // and the year
1188
1189 wxString Format() const
1190 {
1191 wxString s, which;
1192 switch ( nWeek < -1 ? -nWeek : nWeek )
1193 {
1194 case 1: which = "first"; break;
1195 case 2: which = "second"; break;
1196 case 3: which = "third"; break;
1197 case 4: which = "fourth"; break;
1198 case 5: which = "fifth"; break;
1199
1200 case -1: which = "last"; break;
1201 }
1202
1203 if ( nWeek < -1 )
1204 {
1205 which += " from end";
1206 }
1207
1208 s.Printf("The %s %s of %s in %d",
1209 which.c_str(),
1210 wxDateTime::GetWeekDayName(wday).c_str(),
1211 wxDateTime::GetMonthName(month).c_str(),
1212 year);
1213
1214 return s;
1215 }
1216 };
1217
1218 // the array data was generated by the following python program
1219 /*
1220 from DateTime import *
1221 from whrandom import *
1222 from string import *
1223
1224 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
1225 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
1226
1227 week = DateTimeDelta(7)
1228
1229 for n in range(20):
1230 year = randint(1900, 2100)
1231 month = randint(1, 12)
1232 day = randint(1, 28)
1233 dt = DateTime(year, month, day)
1234 wday = dt.day_of_week
1235
1236 countFromEnd = choice([-1, 1])
1237 weekNum = 0;
1238
1239 while dt.month is month:
1240 dt = dt - countFromEnd * week
1241 weekNum = weekNum + countFromEnd
1242
1243 data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
1244
1245 print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
1246 "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
1247 */
1248
1249 static const WeekDateTestData weekDatesTestData[] =
1250 {
1251 { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
1252 { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
1253 { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
1254 { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
1255 { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
1256 { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
1257 { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
1258 { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
1259 { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
1260 { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
1261 { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
1262 { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
1263 { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
1264 { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
1265 { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
1266 { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
1267 { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
1268 { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
1269 { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
1270 { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 },
1271 };
1272
1273 static const char *fmt = "%d-%b-%Y";
1274
1275 wxDateTime dt;
1276 for ( n = 0; n < WXSIZEOF(weekDatesTestData); n++ )
1277 {
1278 const WeekDateTestData& wd = weekDatesTestData[n];
1279
1280 dt.SetToWeekDay(wd.wday, wd.nWeek, wd.month, wd.year);
1281
1282 printf("%s is %s", wd.Format().c_str(), dt.Format(fmt).c_str());
1283
1284 const Date& d = wd.date;
1285 if ( d.SameDay(dt.GetTm()) )
1286 {
1287 puts(" (ok)");
1288 }
1289 else
1290 {
1291 dt.Set(d.day, d.month, d.year);
1292
1293 printf(" (ERROR: should be %s)\n", dt.Format(fmt).c_str());
1294 }
1295 }
1296 }
1297
1298 // test the computation of (ISO) week numbers
1299 static void TestTimeWNumber()
1300 {
1301 puts("\n*** wxDateTime week number test ***");
1302
1303 struct WeekNumberTestData
1304 {
1305 Date date; // the date
1306 wxDateTime::wxDateTime_t week; // the week number in the year
1307 wxDateTime::wxDateTime_t wmon; // the week number in the month
1308 wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
1309 wxDateTime::wxDateTime_t dnum; // day number in the year
1310 };
1311
1312 // data generated with the following python script:
1313 /*
1314 from DateTime import *
1315 from whrandom import *
1316 from string import *
1317
1318 monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
1319 wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
1320
1321 def GetMonthWeek(dt):
1322 weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
1323 if weekNumMonth < 0:
1324 weekNumMonth = weekNumMonth + 53
1325 return weekNumMonth
1326
1327 def GetLastSundayBefore(dt):
1328 if dt.iso_week[2] == 7:
1329 return dt
1330 else:
1331 return dt - DateTimeDelta(dt.iso_week[2])
1332
1333 for n in range(20):
1334 year = randint(1900, 2100)
1335 month = randint(1, 12)
1336 day = randint(1, 28)
1337 dt = DateTime(year, month, day)
1338 dayNum = dt.day_of_year
1339 weekNum = dt.iso_week[1]
1340 weekNumMonth = GetMonthWeek(dt)
1341
1342 weekNumMonth2 = 0
1343 dtSunday = GetLastSundayBefore(dt)
1344
1345 while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
1346 weekNumMonth2 = weekNumMonth2 + 1
1347 dtSunday = dtSunday - DateTimeDelta(7)
1348
1349 data = { 'day': rjust(`day`, 2), \
1350 'month': monthNames[month - 1], \
1351 'year': year, \
1352 'weekNum': rjust(`weekNum`, 2), \
1353 'weekNumMonth': weekNumMonth, \
1354 'weekNumMonth2': weekNumMonth2, \
1355 'dayNum': rjust(`dayNum`, 3) }
1356
1357 print " { { %(day)s, "\
1358 "wxDateTime::%(month)s, "\
1359 "%(year)d }, "\
1360 "%(weekNum)s, "\
1361 "%(weekNumMonth)s, "\
1362 "%(weekNumMonth2)s, "\
1363 "%(dayNum)s }," % data
1364
1365 */
1366 static const WeekNumberTestData weekNumberTestDates[] =
1367 {
1368 { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 },
1369 { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 },
1370 { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 },
1371 { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 },
1372 { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 },
1373 { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 },
1374 { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 },
1375 { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 },
1376 { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 },
1377 { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 },
1378 { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 },
1379 { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 },
1380 { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 },
1381 { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 },
1382 { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 },
1383 { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 },
1384 { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 },
1385 { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 },
1386 { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 },
1387 { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 },
1388 };
1389
1390 for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
1391 {
1392 const WeekNumberTestData& wn = weekNumberTestDates[n];
1393 const Date& d = wn.date;
1394
1395 wxDateTime dt = d.DT();
1396
1397 wxDateTime::wxDateTime_t
1398 week = dt.GetWeekOfYear(wxDateTime::Monday_First),
1399 wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
1400 wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
1401 dnum = dt.GetDayOfYear();
1402
1403 printf("%s: the day number is %d",
1404 d.FormatDate().c_str(), dnum);
1405 if ( dnum == wn.dnum )
1406 {
1407 printf(" (ok)");
1408 }
1409 else
1410 {
1411 printf(" (ERROR: should be %d)", wn.dnum);
1412 }
1413
1414 printf(", week in month is %d", wmon);
1415 if ( wmon == wn.wmon )
1416 {
1417 printf(" (ok)");
1418 }
1419 else
1420 {
1421 printf(" (ERROR: should be %d)", wn.wmon);
1422 }
1423
1424 printf(" or %d", wmon2);
1425 if ( wmon2 == wn.wmon2 )
1426 {
1427 printf(" (ok)");
1428 }
1429 else
1430 {
1431 printf(" (ERROR: should be %d)", wn.wmon2);
1432 }
1433
1434 printf(", week in year is %d", week);
1435 if ( week == wn.week )
1436 {
1437 puts(" (ok)");
1438 }
1439 else
1440 {
1441 printf(" (ERROR: should be %d)\n", wn.week);
1442 }
1443 }
1444 }
1445
1446 // test DST calculations
1447 static void TestTimeDST()
1448 {
1449 puts("\n*** wxDateTime DST test ***");
1450
1451 printf("DST is%s in effect now.\n\n",
1452 wxDateTime::Now().IsDST() ? "" : " not");
1453
1454 // taken from http://www.energy.ca.gov/daylightsaving.html
1455 static const Date datesDST[2][2004 - 1900 + 1] =
1456 {
1457 {
1458 { 1, wxDateTime::Apr, 1990 },
1459 { 7, wxDateTime::Apr, 1991 },
1460 { 5, wxDateTime::Apr, 1992 },
1461 { 4, wxDateTime::Apr, 1993 },
1462 { 3, wxDateTime::Apr, 1994 },
1463 { 2, wxDateTime::Apr, 1995 },
1464 { 7, wxDateTime::Apr, 1996 },
1465 { 6, wxDateTime::Apr, 1997 },
1466 { 5, wxDateTime::Apr, 1998 },
1467 { 4, wxDateTime::Apr, 1999 },
1468 { 2, wxDateTime::Apr, 2000 },
1469 { 1, wxDateTime::Apr, 2001 },
1470 { 7, wxDateTime::Apr, 2002 },
1471 { 6, wxDateTime::Apr, 2003 },
1472 { 4, wxDateTime::Apr, 2004 },
1473 },
1474 {
1475 { 28, wxDateTime::Oct, 1990 },
1476 { 27, wxDateTime::Oct, 1991 },
1477 { 25, wxDateTime::Oct, 1992 },
1478 { 31, wxDateTime::Oct, 1993 },
1479 { 30, wxDateTime::Oct, 1994 },
1480 { 29, wxDateTime::Oct, 1995 },
1481 { 27, wxDateTime::Oct, 1996 },
1482 { 26, wxDateTime::Oct, 1997 },
1483 { 25, wxDateTime::Oct, 1998 },
1484 { 31, wxDateTime::Oct, 1999 },
1485 { 29, wxDateTime::Oct, 2000 },
1486 { 28, wxDateTime::Oct, 2001 },
1487 { 27, wxDateTime::Oct, 2002 },
1488 { 26, wxDateTime::Oct, 2003 },
1489 { 31, wxDateTime::Oct, 2004 },
1490 }
1491 };
1492
1493 int year;
1494 for ( year = 1990; year < 2005; year++ )
1495 {
1496 wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
1497 dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
1498
1499 printf("DST period in the US for year %d: from %s to %s",
1500 year, dtBegin.Format().c_str(), dtEnd.Format().c_str());
1501
1502 size_t n = year - 1990;
1503 const Date& dBegin = datesDST[0][n];
1504 const Date& dEnd = datesDST[1][n];
1505
1506 if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) )
1507 {
1508 puts(" (ok)");
1509 }
1510 else
1511 {
1512 printf(" (ERROR: should be %s %d to %s %d)\n",
1513 wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day,
1514 wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day);
1515 }
1516 }
1517
1518 puts("");
1519
1520 for ( year = 1990; year < 2005; year++ )
1521 {
1522 printf("DST period in Europe for year %d: from %s to %s\n",
1523 year,
1524 wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
1525 wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
1526 }
1527 }
1528
1529 // test wxDateTime -> text conversion
1530 static void TestTimeFormat()
1531 {
1532 puts("\n*** wxDateTime formatting test ***");
1533
1534 // some information may be lost during conversion, so store what kind
1535 // of info should we recover after a round trip
1536 enum CompareKind
1537 {
1538 CompareNone, // don't try comparing
1539 CompareBoth, // dates and times should be identical
1540 CompareDate, // dates only
1541 CompareTime // time only
1542 };
1543
1544 static const struct
1545 {
1546 CompareKind compareKind;
1547 const char *format;
1548 } formatTestFormats[] =
1549 {
1550 { CompareBoth, "---> %c" },
1551 { CompareDate, "Date is %A, %d of %B, in year %Y" },
1552 { CompareBoth, "Date is %x, time is %X" },
1553 { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
1554 { CompareNone, "The day of year: %j, the week of year: %W" },
1555 };
1556
1557 static const Date formatTestDates[] =
1558 {
1559 { 29, wxDateTime::May, 1976, 18, 30, 00 },
1560 { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
1561 #if 0
1562 // this test can't work for other centuries because it uses two digit
1563 // years in formats, so don't even try it
1564 { 29, wxDateTime::May, 2076, 18, 30, 00 },
1565 { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
1566 { 01, wxDateTime::Jan, -52, 03, 16, 47 },
1567 #endif
1568 };
1569
1570 // an extra test (as it doesn't depend on date, don't do it in the loop)
1571 printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
1572
1573 for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
1574 {
1575 puts("");
1576
1577 wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
1578 for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
1579 {
1580 wxString s = dt.Format(formatTestFormats[n].format);
1581 printf("%s", s.c_str());
1582
1583 // what can we recover?
1584 int kind = formatTestFormats[n].compareKind;
1585
1586 // convert back
1587 wxDateTime dt2;
1588 const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
1589 if ( !result )
1590 {
1591 // converion failed - should it have?
1592 if ( kind == CompareNone )
1593 puts(" (ok)");
1594 else
1595 puts(" (ERROR: conversion back failed)");
1596 }
1597 else if ( *result )
1598 {
1599 // should have parsed the entire string
1600 puts(" (ERROR: conversion back stopped too soon)");
1601 }
1602 else
1603 {
1604 bool equal = FALSE; // suppress compilaer warning
1605 switch ( kind )
1606 {
1607 case CompareBoth:
1608 equal = dt2 == dt;
1609 break;
1610
1611 case CompareDate:
1612 equal = dt.IsSameDate(dt2);
1613 break;
1614
1615 case CompareTime:
1616 equal = dt.IsSameTime(dt2);
1617 break;
1618 }
1619
1620 if ( !equal )
1621 {
1622 printf(" (ERROR: got back '%s' instead of '%s')\n",
1623 dt2.Format().c_str(), dt.Format().c_str());
1624 }
1625 else
1626 {
1627 puts(" (ok)");
1628 }
1629 }
1630 }
1631 }
1632 }
1633
1634 // test text -> wxDateTime conversion
1635 static void TestTimeParse()
1636 {
1637 puts("\n*** wxDateTime parse test ***");
1638
1639 struct ParseTestData
1640 {
1641 const char *format;
1642 Date date;
1643 bool good;
1644 };
1645
1646 static const ParseTestData parseTestDates[] =
1647 {
1648 { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE },
1649 { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE },
1650 };
1651
1652 for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
1653 {
1654 const char *format = parseTestDates[n].format;
1655
1656 printf("%s => ", format);
1657
1658 wxDateTime dt;
1659 if ( dt.ParseRfc822Date(format) )
1660 {
1661 printf("%s ", dt.Format().c_str());
1662
1663 if ( parseTestDates[n].good )
1664 {
1665 wxDateTime dtReal = parseTestDates[n].date.DT();
1666 if ( dt == dtReal )
1667 {
1668 puts("(ok)");
1669 }
1670 else
1671 {
1672 printf("(ERROR: should be %s)\n", dtReal.Format().c_str());
1673 }
1674 }
1675 else
1676 {
1677 puts("(ERROR: bad format)");
1678 }
1679 }
1680 else
1681 {
1682 printf("bad format (%s)\n",
1683 parseTestDates[n].good ? "ERROR" : "ok");
1684 }
1685 }
1686 }
1687
1688 static void TestInteractive()
1689 {
1690 puts("\n*** interactive wxDateTime tests ***");
1691
1692 char buf[128];
1693
1694 for ( ;; )
1695 {
1696 printf("Enter a date: ");
1697 if ( !fgets(buf, WXSIZEOF(buf), stdin) )
1698 break;
1699
1700 wxDateTime dt;
1701 if ( !dt.ParseDate(buf) )
1702 {
1703 puts("failed to parse the date");
1704
1705 continue;
1706 }
1707
1708 printf("%s: day %u, week of month %u/%u, week of year %u\n",
1709 dt.FormatISODate().c_str(),
1710 dt.GetDayOfYear(),
1711 dt.GetWeekOfMonth(wxDateTime::Monday_First),
1712 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
1713 dt.GetWeekOfYear(wxDateTime::Monday_First));
1714 }
1715
1716 puts("\n*** done ***");
1717 }
1718
1719 static void TestTimeArithmetics()
1720 {
1721 puts("\n*** testing arithmetic operations on wxDateTime ***");
1722
1723 static const struct
1724 {
1725 wxDateSpan span;
1726 const char *name;
1727 } testArithmData[] =
1728 {
1729 { wxDateSpan::Day(), "day" },
1730 { wxDateSpan::Week(), "week" },
1731 { wxDateSpan::Month(), "month" },
1732 { wxDateSpan::Year(), "year" },
1733 { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
1734 };
1735
1736 wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2;
1737
1738 for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
1739 {
1740 wxDateSpan span = testArithmData[n].span;
1741 dt1 = dt + span;
1742 dt2 = dt - span;
1743
1744 const char *name = testArithmData[n].name;
1745 printf("%s + %s = %s, %s - %s = %s\n",
1746 dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(),
1747 dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str());
1748
1749 printf("Going back: %s", (dt1 - span).FormatISODate().c_str());
1750 if ( dt1 - span == dt )
1751 {
1752 puts(" (ok)");
1753 }
1754 else
1755 {
1756 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
1757 }
1758
1759 printf("Going forward: %s", (dt2 + span).FormatISODate().c_str());
1760 if ( dt2 + span == dt )
1761 {
1762 puts(" (ok)");
1763 }
1764 else
1765 {
1766 printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
1767 }
1768
1769 printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str());
1770 if ( dt2 + 2*span == dt1 )
1771 {
1772 puts(" (ok)");
1773 }
1774 else
1775 {
1776 printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str());
1777 }
1778
1779 puts("");
1780 }
1781 }
1782
1783 static void TestTimeHolidays()
1784 {
1785 puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
1786
1787 wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
1788 wxDateTime dtStart(1, tm.mon, tm.year),
1789 dtEnd = dtStart.GetLastMonthDay();
1790
1791 wxDateTimeArray hol;
1792 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
1793
1794 const wxChar *format = "%d-%b-%Y (%a)";
1795
1796 printf("All holidays between %s and %s:\n",
1797 dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
1798
1799 size_t count = hol.GetCount();
1800 for ( size_t n = 0; n < count; n++ )
1801 {
1802 printf("\t%s\n", hol[n].Format(format).c_str());
1803 }
1804
1805 puts("");
1806 }
1807
1808 #if 0
1809
1810 // test compatibility with the old wxDate/wxTime classes
1811 static void TestTimeCompatibility()
1812 {
1813 puts("\n*** wxDateTime compatibility test ***");
1814
1815 printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str());
1816 printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str());
1817
1818 double jdnNow = wxDateTime::Now().GetJDN();
1819 long jdnMidnight = (long)(jdnNow - 0.5);
1820 printf("wxDate for today: %s\n", wxDate(jdnMidnight).FormatDate().c_str());
1821
1822 jdnMidnight = wxDate().Set().GetJulianDate();
1823 printf("wxDateTime for today: %s\n",
1824 wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str());
1825
1826 int flags = wxEUROPEAN;//wxFULL;
1827 wxDate date;
1828 date.Set();
1829 printf("Today is %s\n", date.FormatDate(flags).c_str());
1830 for ( int n = 0; n < 7; n++ )
1831 {
1832 printf("Previous %s is %s\n",
1833 wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n),
1834 date.Previous(n + 1).FormatDate(flags).c_str());
1835 }
1836 }
1837
1838 #endif // 0
1839
1840 #endif // TEST_DATETIME
1841
1842 // ----------------------------------------------------------------------------
1843 // threads
1844 // ----------------------------------------------------------------------------
1845
1846 #ifdef TEST_THREADS
1847
1848 #include <wx/thread.h>
1849
1850 static size_t gs_counter = (size_t)-1;
1851 static wxCriticalSection gs_critsect;
1852 static wxCondition gs_cond;
1853
1854 class MyJoinableThread : public wxThread
1855 {
1856 public:
1857 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
1858 { m_n = n; Create(); }
1859
1860 // thread execution starts here
1861 virtual ExitCode Entry();
1862
1863 private:
1864 size_t m_n;
1865 };
1866
1867 wxThread::ExitCode MyJoinableThread::Entry()
1868 {
1869 unsigned long res = 1;
1870 for ( size_t n = 1; n < m_n; n++ )
1871 {
1872 res *= n;
1873
1874 // it's a loooong calculation :-)
1875 Sleep(100);
1876 }
1877
1878 return (ExitCode)res;
1879 }
1880
1881 class MyDetachedThread : public wxThread
1882 {
1883 public:
1884 MyDetachedThread(size_t n, char ch)
1885 {
1886 m_n = n;
1887 m_ch = ch;
1888 m_cancelled = FALSE;
1889
1890 Create();
1891 }
1892
1893 // thread execution starts here
1894 virtual ExitCode Entry();
1895
1896 // and stops here
1897 virtual void OnExit();
1898
1899 private:
1900 size_t m_n; // number of characters to write
1901 char m_ch; // character to write
1902
1903 bool m_cancelled; // FALSE if we exit normally
1904 };
1905
1906 wxThread::ExitCode MyDetachedThread::Entry()
1907 {
1908 {
1909 wxCriticalSectionLocker lock(gs_critsect);
1910 if ( gs_counter == (size_t)-1 )
1911 gs_counter = 1;
1912 else
1913 gs_counter++;
1914 }
1915
1916 for ( size_t n = 0; n < m_n; n++ )
1917 {
1918 if ( TestDestroy() )
1919 {
1920 m_cancelled = TRUE;
1921
1922 break;
1923 }
1924
1925 putchar(m_ch);
1926 fflush(stdout);
1927
1928 wxThread::Sleep(100);
1929 }
1930
1931 return 0;
1932 }
1933
1934 void MyDetachedThread::OnExit()
1935 {
1936 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
1937
1938 wxCriticalSectionLocker lock(gs_critsect);
1939 if ( !--gs_counter && !m_cancelled )
1940 gs_cond.Signal();
1941 }
1942
1943 void TestDetachedThreads()
1944 {
1945 puts("\n*** Testing detached threads ***");
1946
1947 static const size_t nThreads = 3;
1948 MyDetachedThread *threads[nThreads];
1949 size_t n;
1950 for ( n = 0; n < nThreads; n++ )
1951 {
1952 threads[n] = new MyDetachedThread(10, 'A' + n);
1953 }
1954
1955 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
1956 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
1957
1958 for ( n = 0; n < nThreads; n++ )
1959 {
1960 threads[n]->Run();
1961 }
1962
1963 // wait until all threads terminate
1964 gs_cond.Wait();
1965
1966 puts("");
1967 }
1968
1969 void TestJoinableThreads()
1970 {
1971 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
1972
1973 // calc 10! in the background
1974 MyJoinableThread thread(10);
1975 thread.Run();
1976
1977 printf("\nThread terminated with exit code %lu.\n",
1978 (unsigned long)thread.Wait());
1979 }
1980
1981 void TestThreadSuspend()
1982 {
1983 puts("\n*** Testing thread suspend/resume functions ***");
1984
1985 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
1986
1987 thread->Run();
1988
1989 // this is for this demo only, in a real life program we'd use another
1990 // condition variable which would be signaled from wxThread::Entry() to
1991 // tell us that the thread really started running - but here just wait a
1992 // bit and hope that it will be enough (the problem is, of course, that
1993 // the thread might still not run when we call Pause() which will result
1994 // in an error)
1995 wxThread::Sleep(300);
1996
1997 for ( size_t n = 0; n < 3; n++ )
1998 {
1999 thread->Pause();
2000
2001 puts("\nThread suspended");
2002 if ( n > 0 )
2003 {
2004 // don't sleep but resume immediately the first time
2005 wxThread::Sleep(300);
2006 }
2007 puts("Going to resume the thread");
2008
2009 thread->Resume();
2010 }
2011
2012 puts("Waiting until it terminates now");
2013
2014 // wait until the thread terminates
2015 gs_cond.Wait();
2016
2017 puts("");
2018 }
2019
2020 void TestThreadDelete()
2021 {
2022 // As above, using Sleep() is only for testing here - we must use some
2023 // synchronisation object instead to ensure that the thread is still
2024 // running when we delete it - deleting a detached thread which already
2025 // terminated will lead to a crash!
2026
2027 puts("\n*** Testing thread delete function ***");
2028
2029 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
2030
2031 thread0->Delete();
2032
2033 puts("\nDeleted a thread which didn't start to run yet.");
2034
2035 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
2036
2037 thread1->Run();
2038
2039 wxThread::Sleep(300);
2040
2041 thread1->Delete();
2042
2043 puts("\nDeleted a running thread.");
2044
2045 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
2046
2047 thread2->Run();
2048
2049 wxThread::Sleep(300);
2050
2051 thread2->Pause();
2052
2053 thread2->Delete();
2054
2055 puts("\nDeleted a sleeping thread.");
2056
2057 MyJoinableThread thread3(20);
2058 thread3.Run();
2059
2060 thread3.Delete();
2061
2062 puts("\nDeleted a joinable thread.");
2063
2064 MyJoinableThread thread4(2);
2065 thread4.Run();
2066
2067 wxThread::Sleep(300);
2068
2069 thread4.Delete();
2070
2071 puts("\nDeleted a joinable thread which already terminated.");
2072
2073 puts("");
2074 }
2075
2076 #endif // TEST_THREADS
2077
2078 // ----------------------------------------------------------------------------
2079 // arrays
2080 // ----------------------------------------------------------------------------
2081
2082 #ifdef TEST_ARRAYS
2083
2084 void PrintArray(const char* name, const wxArrayString& array)
2085 {
2086 printf("Dump of the array '%s'\n", name);
2087
2088 size_t nCount = array.GetCount();
2089 for ( size_t n = 0; n < nCount; n++ )
2090 {
2091 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
2092 }
2093 }
2094
2095 #endif // TEST_ARRAYS
2096
2097 // ----------------------------------------------------------------------------
2098 // strings
2099 // ----------------------------------------------------------------------------
2100
2101 #ifdef TEST_STRINGS
2102
2103 #include "wx/timer.h"
2104 #include "wx/tokenzr.h"
2105
2106 static void TestStringConstruction()
2107 {
2108 puts("*** Testing wxString constructores ***");
2109
2110 #define TEST_CTOR(args, res) \
2111 { \
2112 wxString s args ; \
2113 printf("wxString%s = %s ", #args, s.c_str()); \
2114 if ( s == res ) \
2115 { \
2116 puts("(ok)"); \
2117 } \
2118 else \
2119 { \
2120 printf("(ERROR: should be %s)\n", res); \
2121 } \
2122 }
2123
2124 TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
2125 TEST_CTOR((_T("Hello"), 4), _T("Hell"));
2126 TEST_CTOR((_T("Hello"), 5), _T("Hello"));
2127 // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
2128
2129 static const wxChar *s = _T("?really!");
2130 const wxChar *start = wxStrchr(s, _T('r'));
2131 const wxChar *end = wxStrchr(s, _T('!'));
2132 TEST_CTOR((start, end), _T("really"));
2133
2134 puts("");
2135 }
2136
2137 static void TestString()
2138 {
2139 wxStopWatch sw;
2140
2141 wxString a, b, c;
2142
2143 a.reserve (128);
2144 b.reserve (128);
2145 c.reserve (128);
2146
2147 for (int i = 0; i < 1000000; ++i)
2148 {
2149 a = "Hello";
2150 b = " world";
2151 c = "! How'ya doin'?";
2152 a += b;
2153 a += c;
2154 c = "Hello world! What's up?";
2155 if (c != a)
2156 c = "Doh!";
2157 }
2158
2159 printf ("TestString elapsed time: %ld\n", sw.Time());
2160 }
2161
2162 static void TestPChar()
2163 {
2164 wxStopWatch sw;
2165
2166 char a [128];
2167 char b [128];
2168 char c [128];
2169
2170 for (int i = 0; i < 1000000; ++i)
2171 {
2172 strcpy (a, "Hello");
2173 strcpy (b, " world");
2174 strcpy (c, "! How'ya doin'?");
2175 strcat (a, b);
2176 strcat (a, c);
2177 strcpy (c, "Hello world! What's up?");
2178 if (strcmp (c, a) == 0)
2179 strcpy (c, "Doh!");
2180 }
2181
2182 printf ("TestPChar elapsed time: %ld\n", sw.Time());
2183 }
2184
2185 static void TestStringSub()
2186 {
2187 wxString s("Hello, world!");
2188
2189 puts("*** Testing wxString substring extraction ***");
2190
2191 printf("String = '%s'\n", s.c_str());
2192 printf("Left(5) = '%s'\n", s.Left(5).c_str());
2193 printf("Right(6) = '%s'\n", s.Right(6).c_str());
2194 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
2195 printf("Mid(3) = '%s'\n", s.Mid(3).c_str());
2196 printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str());
2197 printf("substr(3) = '%s'\n", s.substr(3).c_str());
2198
2199 puts("");
2200 }
2201
2202 static void TestStringFormat()
2203 {
2204 puts("*** Testing wxString formatting ***");
2205
2206 wxString s;
2207 s.Printf("%03d", 18);
2208
2209 printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
2210 printf("Number 18: %s\n", s.c_str());
2211
2212 puts("");
2213 }
2214
2215 // returns "not found" for npos, value for all others
2216 static wxString PosToString(size_t res)
2217 {
2218 wxString s = res == wxString::npos ? wxString(_T("not found"))
2219 : wxString::Format(_T("%u"), res);
2220 return s;
2221 }
2222
2223 static void TestStringFind()
2224 {
2225 puts("*** Testing wxString find() functions ***");
2226
2227 static const wxChar *strToFind = _T("ell");
2228 static const struct StringFindTest
2229 {
2230 const wxChar *str;
2231 size_t start,
2232 result; // of searching "ell" in str
2233 } findTestData[] =
2234 {
2235 { _T("Well, hello world"), 0, 1 },
2236 { _T("Well, hello world"), 6, 7 },
2237 { _T("Well, hello world"), 9, wxString::npos },
2238 };
2239
2240 for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
2241 {
2242 const StringFindTest& ft = findTestData[n];
2243 size_t res = wxString(ft.str).find(strToFind, ft.start);
2244
2245 printf(_T("Index of '%s' in '%s' starting from %u is %s "),
2246 strToFind, ft.str, ft.start, PosToString(res).c_str());
2247
2248 size_t resTrue = ft.result;
2249 if ( res == resTrue )
2250 {
2251 puts(_T("(ok)"));
2252 }
2253 else
2254 {
2255 printf(_T("(ERROR: should be %s)\n"),
2256 PosToString(resTrue).c_str());
2257 }
2258 }
2259
2260 puts("");
2261 }
2262
2263 static void TestStringTokenizer()
2264 {
2265 puts("*** Testing wxStringTokenizer ***");
2266
2267 static const wxChar *modeNames[] =
2268 {
2269 _T("default"),
2270 _T("return empty"),
2271 _T("return all empty"),
2272 _T("with delims"),
2273 _T("like strtok"),
2274 };
2275
2276 static const struct StringTokenizerTest
2277 {
2278 const wxChar *str; // string to tokenize
2279 const wxChar *delims; // delimiters to use
2280 size_t count; // count of token
2281 wxStringTokenizerMode mode; // how should we tokenize it
2282 } tokenizerTestData[] =
2283 {
2284 { _T(""), _T(" "), 0 },
2285 { _T("Hello, world"), _T(" "), 2 },
2286 { _T("Hello, world "), _T(" "), 2 },
2287 { _T("Hello, world"), _T(","), 2 },
2288 { _T("Hello, world!"), _T(",!"), 2 },
2289 { _T("Hello,, world!"), _T(",!"), 3 },
2290 { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL },
2291 { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
2292 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 },
2293 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY },
2294 { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL },
2295 { _T("01/02/99"), _T("/-"), 3 },
2296 { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS },
2297 };
2298
2299 for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
2300 {
2301 const StringTokenizerTest& tt = tokenizerTestData[n];
2302 wxStringTokenizer tkz(tt.str, tt.delims, tt.mode);
2303
2304 size_t count = tkz.CountTokens();
2305 printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
2306 MakePrintable(tt.str).c_str(),
2307 count,
2308 MakePrintable(tt.delims).c_str(),
2309 modeNames[tkz.GetMode()]);
2310 if ( count == tt.count )
2311 {
2312 puts(_T("(ok)"));
2313 }
2314 else
2315 {
2316 printf(_T("(ERROR: should be %u)\n"), tt.count);
2317
2318 continue;
2319 }
2320
2321 // if we emulate strtok(), check that we do it correctly
2322 wxChar *buf, *s, *last;
2323
2324 if ( tkz.GetMode() == wxTOKEN_STRTOK )
2325 {
2326 buf = new wxChar[wxStrlen(tt.str) + 1];
2327 wxStrcpy(buf, tt.str);
2328
2329 s = wxStrtok(buf, tt.delims, &last);
2330 }
2331 else
2332 {
2333 buf = NULL;
2334 }
2335
2336 // now show the tokens themselves
2337 size_t count2 = 0;
2338 while ( tkz.HasMoreTokens() )
2339 {
2340 wxString token = tkz.GetNextToken();
2341
2342 printf(_T("\ttoken %u: '%s'"),
2343 ++count2,
2344 MakePrintable(token).c_str());
2345
2346 if ( buf )
2347 {
2348 if ( token == s )
2349 {
2350 puts(" (ok)");
2351 }
2352 else
2353 {
2354 printf(" (ERROR: should be %s)\n", s);
2355 }
2356
2357 s = wxStrtok(NULL, tt.delims, &last);
2358 }
2359 else
2360 {
2361 // nothing to compare with
2362 puts("");
2363 }
2364 }
2365
2366 if ( count2 != count )
2367 {
2368 puts(_T("\tERROR: token count mismatch"));
2369 }
2370
2371 delete [] buf;
2372 }
2373
2374 puts("");
2375 }
2376
2377 #endif // TEST_STRINGS
2378
2379 // ----------------------------------------------------------------------------
2380 // entry point
2381 // ----------------------------------------------------------------------------
2382
2383 int main(int argc, char **argv)
2384 {
2385 if ( !wxInitialize() )
2386 {
2387 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
2388 }
2389
2390 #ifdef TEST_USLEEP
2391 puts("Sleeping for 3 seconds... z-z-z-z-z...");
2392 wxUsleep(3000);
2393 #endif // TEST_USLEEP
2394
2395 #ifdef TEST_CMDLINE
2396 static const wxCmdLineEntryDesc cmdLineDesc[] =
2397 {
2398 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
2399 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
2400
2401 { wxCMD_LINE_OPTION, "o", "output", "output file" },
2402 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
2403 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
2404 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
2405
2406 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
2407 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
2408
2409 { wxCMD_LINE_NONE }
2410 };
2411
2412 wxCmdLineParser parser(cmdLineDesc, argc, argv);
2413
2414 switch ( parser.Parse() )
2415 {
2416 case -1:
2417 wxLogMessage("Help was given, terminating.");
2418 break;
2419
2420 case 0:
2421 ShowCmdLine(parser);
2422 break;
2423
2424 default:
2425 wxLogMessage("Syntax error detected, aborting.");
2426 break;
2427 }
2428 #endif // TEST_CMDLINE
2429
2430 #ifdef TEST_STRINGS
2431 if ( 0 )
2432 {
2433 TestPChar();
2434 TestString();
2435 }
2436 if ( 0 )
2437 {
2438 TestStringConstruction();
2439 TestStringSub();
2440 TestStringFormat();
2441 TestStringFind();
2442 TestStringTokenizer();
2443 }
2444 #endif // TEST_STRINGS
2445
2446 #ifdef TEST_ARRAYS
2447 wxArrayString a1;
2448 a1.Add("tiger");
2449 a1.Add("cat");
2450 a1.Add("lion");
2451 a1.Add("dog");
2452 a1.Add("human");
2453 a1.Add("ape");
2454
2455 puts("*** Initially:");
2456
2457 PrintArray("a1", a1);
2458
2459 wxArrayString a2(a1);
2460 PrintArray("a2", a2);
2461
2462 wxSortedArrayString a3(a1);
2463 PrintArray("a3", a3);
2464
2465 puts("*** After deleting a string from a1");
2466 a1.Remove(2);
2467
2468 PrintArray("a1", a1);
2469 PrintArray("a2", a2);
2470 PrintArray("a3", a3);
2471
2472 puts("*** After reassigning a1 to a2 and a3");
2473 a3 = a2 = a1;
2474 PrintArray("a2", a2);
2475 PrintArray("a3", a3);
2476 #endif // TEST_ARRAYS
2477
2478 #ifdef TEST_DIR
2479 TestDirEnum();
2480 #endif // TEST_DIR
2481
2482 #ifdef TEST_EXECUTE
2483 TestExecute();
2484 #endif // TEST_EXECUTE
2485
2486 #ifdef TEST_FILECONF
2487 TestFileConfRead();
2488 #endif // TEST_FILECONF
2489
2490 #ifdef TEST_LOG
2491 wxString s;
2492 for ( size_t n = 0; n < 8000; n++ )
2493 {
2494 s << (char)('A' + (n % 26));
2495 }
2496
2497 wxString msg;
2498 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
2499
2500 // this one shouldn't be truncated
2501 printf(msg);
2502
2503 // but this one will because log functions use fixed size buffer
2504 // (note that it doesn't need '\n' at the end neither - will be added
2505 // by wxLog anyhow)
2506 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
2507 #endif // TEST_LOG
2508
2509 #ifdef TEST_THREADS
2510 int nCPUs = wxThread::GetCPUCount();
2511 printf("This system has %d CPUs\n", nCPUs);
2512 if ( nCPUs != -1 )
2513 wxThread::SetConcurrency(nCPUs);
2514
2515 if ( argc > 1 && argv[1][0] == 't' )
2516 wxLog::AddTraceMask("thread");
2517
2518 if ( 1 )
2519 TestDetachedThreads();
2520 if ( 1 )
2521 TestJoinableThreads();
2522 if ( 1 )
2523 TestThreadSuspend();
2524 if ( 1 )
2525 TestThreadDelete();
2526
2527 #endif // TEST_THREADS
2528
2529 #ifdef TEST_LONGLONG
2530 // seed pseudo random generator
2531 srand((unsigned)time(NULL));
2532
2533 if ( 0 )
2534 {
2535 TestSpeed();
2536 }
2537 TestMultiplication();
2538 if ( 0 )
2539 {
2540 TestDivision();
2541 TestAddition();
2542 TestLongLongConversion();
2543 TestBitOperations();
2544 }
2545 #endif // TEST_LONGLONG
2546
2547 #ifdef TEST_HASH
2548 TestHash();
2549 #endif // TEST_HASH
2550
2551 #ifdef TEST_MIME
2552 TestMimeEnum();
2553 #endif // TEST_MIME
2554
2555 #ifdef TEST_SOCKETS
2556 if ( 0 )
2557 {
2558 TestSocketServer();
2559 TestSocketClient();
2560 }
2561 TestProtocolFtp();
2562 #endif // TEST_SOCKETS
2563
2564 #ifdef TEST_TIMER
2565 TestStopWatch();
2566 #endif // TEST_TIMER
2567
2568 #ifdef TEST_DATETIME
2569 if ( 0 )
2570 {
2571 TestTimeSet();
2572 TestTimeStatic();
2573 TestTimeRange();
2574 TestTimeZones();
2575 TestTimeTicks();
2576 TestTimeJDN();
2577 TestTimeDST();
2578 TestTimeWDays();
2579 TestTimeWNumber();
2580 TestTimeParse();
2581 TestTimeFormat();
2582 TestTimeArithmetics();
2583 }
2584 TestTimeHolidays();
2585 if ( 0 )
2586 TestInteractive();
2587 #endif // TEST_DATETIME
2588
2589 wxUninitialize();
2590
2591 return 0;
2592 }