]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
8bb9a2002abdf02286a7ca85bb1b8b92ff05149f
[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 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31
32 //#define TEST_ARRAYS
33 //#define TEST_DIR
34 //#define TEST_LOG
35 //#define TEST_MIME
36 //#define TEST_STRINGS
37 //#define TEST_THREADS
38 #define TEST_TIME
39 //#define TEST_LONGLONG
40
41 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxDir
47 // ----------------------------------------------------------------------------
48
49 #ifdef TEST_DIR
50
51 #include <wx/dir.h>
52
53 static void TestDirEnumHelper(wxDir& dir,
54 int flags = wxDIR_DEFAULT,
55 const wxString& filespec = wxEmptyString)
56 {
57 wxString filename;
58
59 if ( !dir.IsOpened() )
60 return;
61
62 bool cont = dir.GetFirst(&filename, filespec, flags);
63 while ( cont )
64 {
65 printf("\t%s\n", filename.c_str());
66
67 cont = dir.GetNext(&filename);
68 }
69
70 puts("");
71 }
72
73 static void TestDirEnum()
74 {
75 wxDir dir(wxGetCwd());
76
77 puts("Enumerating everything in current directory:");
78 TestDirEnumHelper(dir);
79
80 puts("Enumerating really everything in current directory:");
81 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
82
83 puts("Enumerating object files in current directory:");
84 TestDirEnumHelper(dir, wxDIR_DEFAULT, "*.o");
85
86 puts("Enumerating directories in current directory:");
87 TestDirEnumHelper(dir, wxDIR_DIRS);
88
89 puts("Enumerating files in current directory:");
90 TestDirEnumHelper(dir, wxDIR_FILES);
91
92 puts("Enumerating files including hidden in current directory:");
93 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
94
95 #ifdef __UNIX__
96 dir.Open("/");
97 #elif defined(__WXMSW__)
98 dir.Open("c:\\");
99 #else
100 #error "don't know where the root directory is"
101 #endif
102
103 puts("Enumerating everything in root directory:");
104 TestDirEnumHelper(dir, wxDIR_DEFAULT);
105
106 puts("Enumerating directories in root directory:");
107 TestDirEnumHelper(dir, wxDIR_DIRS);
108
109 puts("Enumerating files in root directory:");
110 TestDirEnumHelper(dir, wxDIR_FILES);
111
112 puts("Enumerating files including hidden in root directory:");
113 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
114
115 puts("Enumerating files in non existing directory:");
116 wxDir dirNo("nosuchdir");
117 TestDirEnumHelper(dirNo);
118 }
119
120 #endif // TEST_DIR
121
122 // ----------------------------------------------------------------------------
123 // MIME types
124 // ----------------------------------------------------------------------------
125
126 #ifdef TEST_MIME
127
128 #include <wx/mimetype.h>
129
130 static void TestMimeEnum()
131 {
132 wxMimeTypesManager mimeTM;
133 wxArrayString mimetypes;
134
135 size_t count = mimeTM.EnumAllFileTypes(mimetypes);
136
137 printf("*** All %u known filetypes: ***\n", count);
138
139 wxArrayString exts;
140 wxString desc;
141
142 for ( size_t n = 0; n < count; n++ )
143 {
144 wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]);
145 if ( !filetype )
146 {
147 printf("nothing known about the filetype '%s'!\n",
148 mimetypes[n].c_str());
149 continue;
150 }
151
152 filetype->GetDescription(&desc);
153 filetype->GetExtensions(exts);
154
155 filetype->GetIcon(NULL);
156
157 wxString extsAll;
158 for ( size_t e = 0; e < exts.GetCount(); e++ )
159 {
160 if ( e > 0 )
161 extsAll << _T(", ");
162 extsAll += exts[e];
163 }
164
165 printf("\t%s: %s (%s)\n",
166 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
167 }
168 }
169
170 #endif // TEST_MIME
171
172 // ----------------------------------------------------------------------------
173 // long long
174 // ----------------------------------------------------------------------------
175
176 #ifdef TEST_LONGLONG
177
178 #include <wx/longlong.h>
179 #include <wx/timer.h>
180
181 static void TestSpeed()
182 {
183 static const long max = 100000000;
184 long n;
185
186 {
187 wxStopWatch sw;
188
189 long l = 0;
190 for ( n = 0; n < max; n++ )
191 {
192 l += n;
193 }
194
195 printf("Summing longs took %ld milliseconds.\n", sw.Time());
196 }
197
198 {
199 wxStopWatch sw;
200
201 __int64 l = 0;
202 for ( n = 0; n < max; n++ )
203 {
204 l += n;
205 }
206
207 printf("Summing __int64s took %ld milliseconds.\n", sw.Time());
208 }
209
210 {
211 wxStopWatch sw;
212
213 wxLongLong l;
214 for ( n = 0; n < max; n++ )
215 {
216 l += n;
217 }
218
219 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
220 }
221 }
222
223 static void TestDivision()
224 {
225 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
226
227 // seed pseudo random generator
228 //srand((unsigned)time(NULL));
229
230 size_t nTested = 0;
231 for ( size_t n = 0; n < 10000; n++ )
232 {
233 // get a random wxLongLong (shifting by 12 the MSB ensures that the
234 // multiplication will not overflow)
235 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
236
237 wxASSERT( (ll * 1000l)/1000l == ll );
238
239 nTested++;
240 }
241
242 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested);
243
244 #undef MAKE_LL
245 }
246
247 #endif // TEST_LONGLONG
248
249 // ----------------------------------------------------------------------------
250 // date time
251 // ----------------------------------------------------------------------------
252
253 #ifdef TEST_TIME
254
255 #include <wx/datetime.h>
256
257 // the test data
258 struct Date
259 {
260 wxDateTime::wxDateTime_t day;
261 wxDateTime::Month month;
262 int year;
263 wxDateTime::wxDateTime_t hour, min, sec;
264 double jdn;
265 wxDateTime::WeekDay wday;
266 time_t gmticks, ticks;
267
268 void Init(const wxDateTime::Tm& tm)
269 {
270 day = tm.mday;
271 month = tm.mon;
272 year = tm.year;
273 hour = tm.hour;
274 min = tm.min;
275 sec = tm.sec;
276 jdn = 0.0;
277 gmticks = ticks = -1;
278 }
279
280 wxDateTime DT() const
281 { return wxDateTime(day, month, year, hour, min, sec); }
282
283 wxString Format() const
284 {
285 wxString s;
286 s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
287 hour, min, sec,
288 wxDateTime::GetMonthName(month).c_str(),
289 day,
290 abs(wxDateTime::ConvertYearToBC(year)),
291 year > 0 ? "AD" : "BC");
292 return s;
293 }
294 };
295
296 static const Date testDates[] =
297 {
298 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
299 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
300 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
301 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
302 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
303 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
304 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
305 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
306 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
307 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
308 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Tue, -1, -1 },
309 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Wed, -1, -1 },
310 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Thu, -1, -1 },
311 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Wed, -1, -1 },
312 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
313 };
314
315 // this test miscellaneous static wxDateTime functions
316 static void TestTimeStatic()
317 {
318 puts("\n*** wxDateTime static methods test ***");
319
320 // some info about the current date
321 int year = wxDateTime::GetCurrentYear();
322 printf("Current year %d is %sa leap one and has %d days.\n",
323 year,
324 wxDateTime::IsLeapYear(year) ? "" : "not ",
325 wxDateTime::GetNumberOfDays(year));
326
327 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
328 printf("Current month is '%s' ('%s') and it has %d days\n",
329 wxDateTime::GetMonthName(month, TRUE).c_str(),
330 wxDateTime::GetMonthName(month).c_str(),
331 wxDateTime::GetNumberOfDays(month));
332
333 // leap year logic
334 static const size_t nYears = 5;
335 static const size_t years[2][nYears] =
336 {
337 // first line: the years to test
338 { 1990, 1976, 2000, 2030, 1984, },
339
340 // second line: TRUE if leap, FALSE otherwise
341 { FALSE, TRUE, TRUE, FALSE, TRUE }
342 };
343
344 for ( size_t n = 0; n < nYears; n++ )
345 {
346 int year = years[0][n];
347 bool should = years[1][n] != 0;
348
349 printf("Year %d is %sa leap year (should be: %s)\n",
350 year,
351 wxDateTime::IsLeapYear(year) ? "" : "not ",
352 should ? "yes" : "no");
353
354 wxASSERT( should == wxDateTime::IsLeapYear(year) );
355 }
356 }
357
358 // test constructing wxDateTime objects
359 static void TestTimeSet()
360 {
361 puts("\n*** wxDateTime construction test ***");
362
363 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
364 {
365 const Date& d1 = testDates[n];
366 wxDateTime dt = d1.DT();
367
368 Date d2;
369 d2.Init(dt.GetTm());
370
371 wxString s1 = d1.Format(),
372 s2 = d2.Format();
373
374 printf("Date: %s == %s (%s)\n",
375 s1.c_str(), s2.c_str(),
376 s1 == s2 ? "ok" : "ERROR");
377 }
378 }
379
380 // test time zones stuff
381 static void TestTimeZones()
382 {
383 puts("\n*** wxDateTime timezone test ***");
384
385 wxDateTime now = wxDateTime::Now();
386
387 printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
388 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
389 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
390 printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
391 printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
392 printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
393 }
394
395 // test some minimal support for the dates outside the standard range
396 static void TestTimeRange()
397 {
398 puts("\n*** wxDateTime out-of-standard-range dates test ***");
399
400 static const char *fmt = "%d-%b-%Y %H:%M:%S";
401
402 printf("Unix epoch:\t%s\n",
403 wxDateTime(2440587.5).Format(fmt).c_str());
404 printf("Feb 29, 0: \t%s\n",
405 wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
406 printf("JDN 0: \t%s\n",
407 wxDateTime(0.0).Format(fmt).c_str());
408 printf("Jan 1, 1AD:\t%s\n",
409 wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
410 printf("May 29, 2099:\t%s\n",
411 wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
412 }
413
414 static void TestTimeTicks()
415 {
416 puts("\n*** wxDateTime ticks test ***");
417
418 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
419 {
420 const Date& d = testDates[n];
421 if ( d.ticks == -1 )
422 continue;
423
424 wxDateTime dt = d.DT();
425 long ticks = (dt.GetValue() / 1000).ToLong();
426 printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
427 if ( ticks == d.ticks )
428 {
429 puts(" (ok)");
430 }
431 else
432 {
433 printf(" (ERROR: should be %ld, delta = %ld)\n",
434 d.ticks, ticks - d.ticks);
435 }
436
437 dt = d.DT().ToTimezone(wxDateTime::GMT0);
438 ticks = (dt.GetValue() / 1000).ToLong();
439 printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
440 if ( ticks == d.gmticks )
441 {
442 puts(" (ok)");
443 }
444 else
445 {
446 printf(" (ERROR: should be %ld, delta = %ld)\n",
447 d.gmticks, ticks - d.gmticks);
448 }
449 }
450
451 puts("");
452 }
453
454 // test conversions to JDN &c
455 static void TestTimeJDN()
456 {
457 puts("\n*** wxDateTime to JDN test ***");
458
459 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
460 {
461 const Date& d = testDates[n];
462 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
463 double jdn = dt.GetJulianDayNumber();
464
465 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
466 if ( jdn == d.jdn )
467 {
468 puts(" (ok)");
469 }
470 else
471 {
472 printf(" (ERROR: should be %f, delta = %f)\n",
473 d.jdn, jdn - d.jdn);
474 }
475 }
476 }
477
478 // test week days computation
479 static void TestTimeWDays()
480 {
481 puts("\n*** wxDateTime weekday test ***");
482
483 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
484 {
485 const Date& d = testDates[n];
486 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
487
488 wxDateTime::WeekDay wday = dt.GetWeekDay();
489 printf("%s is: %s",
490 d.Format().c_str(),
491 wxDateTime::GetWeekDayName(wday));
492 if ( wday == d.wday )
493 {
494 puts(" (ok)");
495 }
496 else
497 {
498 printf(" (ERROR: should be %s)\n",
499 wxDateTime::GetWeekDayName(d.wday));
500 }
501 }
502 }
503
504 #endif // TEST_TIME
505
506 // ----------------------------------------------------------------------------
507 // threads
508 // ----------------------------------------------------------------------------
509
510 #ifdef TEST_THREADS
511
512 #include <wx/thread.h>
513
514 static size_t gs_counter = (size_t)-1;
515 static wxCriticalSection gs_critsect;
516 static wxCondition gs_cond;
517
518 class MyJoinableThread : public wxThread
519 {
520 public:
521 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
522 { m_n = n; Create(); }
523
524 // thread execution starts here
525 virtual ExitCode Entry();
526
527 private:
528 size_t m_n;
529 };
530
531 wxThread::ExitCode MyJoinableThread::Entry()
532 {
533 unsigned long res = 1;
534 for ( size_t n = 1; n < m_n; n++ )
535 {
536 res *= n;
537
538 // it's a loooong calculation :-)
539 Sleep(100);
540 }
541
542 return (ExitCode)res;
543 }
544
545 class MyDetachedThread : public wxThread
546 {
547 public:
548 MyDetachedThread(size_t n, char ch)
549 {
550 m_n = n;
551 m_ch = ch;
552 m_cancelled = FALSE;
553
554 Create();
555 }
556
557 // thread execution starts here
558 virtual ExitCode Entry();
559
560 // and stops here
561 virtual void OnExit();
562
563 private:
564 size_t m_n; // number of characters to write
565 char m_ch; // character to write
566
567 bool m_cancelled; // FALSE if we exit normally
568 };
569
570 wxThread::ExitCode MyDetachedThread::Entry()
571 {
572 {
573 wxCriticalSectionLocker lock(gs_critsect);
574 if ( gs_counter == (size_t)-1 )
575 gs_counter = 1;
576 else
577 gs_counter++;
578 }
579
580 for ( size_t n = 0; n < m_n; n++ )
581 {
582 if ( TestDestroy() )
583 {
584 m_cancelled = TRUE;
585
586 break;
587 }
588
589 putchar(m_ch);
590 fflush(stdout);
591
592 wxThread::Sleep(100);
593 }
594
595 return 0;
596 }
597
598 void MyDetachedThread::OnExit()
599 {
600 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
601
602 wxCriticalSectionLocker lock(gs_critsect);
603 if ( !--gs_counter && !m_cancelled )
604 gs_cond.Signal();
605 }
606
607 void TestDetachedThreads()
608 {
609 puts("\n*** Testing detached threads ***");
610
611 static const size_t nThreads = 3;
612 MyDetachedThread *threads[nThreads];
613 size_t n;
614 for ( n = 0; n < nThreads; n++ )
615 {
616 threads[n] = new MyDetachedThread(10, 'A' + n);
617 }
618
619 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
620 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
621
622 for ( n = 0; n < nThreads; n++ )
623 {
624 threads[n]->Run();
625 }
626
627 // wait until all threads terminate
628 gs_cond.Wait();
629
630 puts("");
631 }
632
633 void TestJoinableThreads()
634 {
635 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
636
637 // calc 10! in the background
638 MyJoinableThread thread(10);
639 thread.Run();
640
641 printf("\nThread terminated with exit code %lu.\n",
642 (unsigned long)thread.Wait());
643 }
644
645 void TestThreadSuspend()
646 {
647 puts("\n*** Testing thread suspend/resume functions ***");
648
649 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
650
651 thread->Run();
652
653 // this is for this demo only, in a real life program we'd use another
654 // condition variable which would be signaled from wxThread::Entry() to
655 // tell us that the thread really started running - but here just wait a
656 // bit and hope that it will be enough (the problem is, of course, that
657 // the thread might still not run when we call Pause() which will result
658 // in an error)
659 wxThread::Sleep(300);
660
661 for ( size_t n = 0; n < 3; n++ )
662 {
663 thread->Pause();
664
665 puts("\nThread suspended");
666 if ( n > 0 )
667 {
668 // don't sleep but resume immediately the first time
669 wxThread::Sleep(300);
670 }
671 puts("Going to resume the thread");
672
673 thread->Resume();
674 }
675
676 puts("Waiting until it terminates now");
677
678 // wait until the thread terminates
679 gs_cond.Wait();
680
681 puts("");
682 }
683
684 void TestThreadDelete()
685 {
686 // As above, using Sleep() is only for testing here - we must use some
687 // synchronisation object instead to ensure that the thread is still
688 // running when we delete it - deleting a detached thread which already
689 // terminated will lead to a crash!
690
691 puts("\n*** Testing thread delete function ***");
692
693 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
694
695 thread0->Delete();
696
697 puts("\nDeleted a thread which didn't start to run yet.");
698
699 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
700
701 thread1->Run();
702
703 wxThread::Sleep(300);
704
705 thread1->Delete();
706
707 puts("\nDeleted a running thread.");
708
709 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
710
711 thread2->Run();
712
713 wxThread::Sleep(300);
714
715 thread2->Pause();
716
717 thread2->Delete();
718
719 puts("\nDeleted a sleeping thread.");
720
721 MyJoinableThread thread3(20);
722 thread3.Run();
723
724 thread3.Delete();
725
726 puts("\nDeleted a joinable thread.");
727
728 MyJoinableThread thread4(2);
729 thread4.Run();
730
731 wxThread::Sleep(300);
732
733 thread4.Delete();
734
735 puts("\nDeleted a joinable thread which already terminated.");
736
737 puts("");
738 }
739
740 #endif // TEST_THREADS
741
742 // ----------------------------------------------------------------------------
743 // arrays
744 // ----------------------------------------------------------------------------
745
746 #ifdef TEST_ARRAYS
747
748 void PrintArray(const char* name, const wxArrayString& array)
749 {
750 printf("Dump of the array '%s'\n", name);
751
752 size_t nCount = array.GetCount();
753 for ( size_t n = 0; n < nCount; n++ )
754 {
755 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
756 }
757 }
758
759 #endif // TEST_ARRAYS
760
761 // ----------------------------------------------------------------------------
762 // strings
763 // ----------------------------------------------------------------------------
764
765 #ifdef TEST_STRINGS
766
767 #include "wx/timer.h"
768
769 static void TestString()
770 {
771 wxStopWatch sw;
772
773 wxString a, b, c;
774
775 a.reserve (128);
776 b.reserve (128);
777 c.reserve (128);
778
779 for (int i = 0; i < 1000000; ++i)
780 {
781 a = "Hello";
782 b = " world";
783 c = "! How'ya doin'?";
784 a += b;
785 a += c;
786 c = "Hello world! What's up?";
787 if (c != a)
788 c = "Doh!";
789 }
790
791 printf ("TestString elapsed time: %ld\n", sw.Time());
792 }
793
794 static void TestPChar()
795 {
796 wxStopWatch sw;
797
798 char a [128];
799 char b [128];
800 char c [128];
801
802 for (int i = 0; i < 1000000; ++i)
803 {
804 strcpy (a, "Hello");
805 strcpy (b, " world");
806 strcpy (c, "! How'ya doin'?");
807 strcat (a, b);
808 strcat (a, c);
809 strcpy (c, "Hello world! What's up?");
810 if (strcmp (c, a) == 0)
811 strcpy (c, "Doh!");
812 }
813
814 printf ("TestPChar elapsed time: %ld\n", sw.Time());
815 }
816
817 static void TestStringSub()
818 {
819 wxString s("Hello, world!");
820
821 puts("*** Testing wxString substring extraction ***");
822
823 printf("String = '%s'\n", s.c_str());
824 printf("Left(5) = '%s'\n", s.Left(5).c_str());
825 printf("Right(6) = '%s'\n", s.Right(6).c_str());
826 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
827 printf("Mid(3) = '%s'\n", s.Mid(3).c_str());
828 printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str());
829 printf("substr(3) = '%s'\n", s.substr(3).c_str());
830
831 puts("");
832 }
833
834 #endif // TEST_STRINGS
835
836 // ----------------------------------------------------------------------------
837 // entry point
838 // ----------------------------------------------------------------------------
839
840 int main(int argc, char **argv)
841 {
842 if ( !wxInitialize() )
843 {
844 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
845 }
846
847 #ifdef TEST_STRINGS
848 if ( 0 )
849 {
850 TestPChar();
851 TestString();
852 }
853 TestStringSub();
854 #endif // TEST_STRINGS
855
856 #ifdef TEST_ARRAYS
857 wxArrayString a1;
858 a1.Add("tiger");
859 a1.Add("cat");
860 a1.Add("lion");
861 a1.Add("dog");
862 a1.Add("human");
863 a1.Add("ape");
864
865 puts("*** Initially:");
866
867 PrintArray("a1", a1);
868
869 wxArrayString a2(a1);
870 PrintArray("a2", a2);
871
872 wxSortedArrayString a3(a1);
873 PrintArray("a3", a3);
874
875 puts("*** After deleting a string from a1");
876 a1.Remove(2);
877
878 PrintArray("a1", a1);
879 PrintArray("a2", a2);
880 PrintArray("a3", a3);
881
882 puts("*** After reassigning a1 to a2 and a3");
883 a3 = a2 = a1;
884 PrintArray("a2", a2);
885 PrintArray("a3", a3);
886 #endif // TEST_ARRAYS
887
888 #ifdef TEST_DIR
889 TestDirEnum();
890 #endif // TEST_DIR
891
892 #ifdef TEST_LOG
893 wxString s;
894 for ( size_t n = 0; n < 8000; n++ )
895 {
896 s << (char)('A' + (n % 26));
897 }
898
899 wxString msg;
900 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
901
902 // this one shouldn't be truncated
903 printf(msg);
904
905 // but this one will because log functions use fixed size buffer
906 // (note that it doesn't need '\n' at the end neither - will be added
907 // by wxLog anyhow)
908 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
909 #endif // TEST_LOG
910
911 #ifdef TEST_THREADS
912 int nCPUs = wxThread::GetCPUCount();
913 printf("This system has %d CPUs\n", nCPUs);
914 if ( nCPUs != -1 )
915 wxThread::SetConcurrency(nCPUs);
916
917 if ( argc > 1 && argv[1][0] == 't' )
918 wxLog::AddTraceMask("thread");
919
920 if ( 1 )
921 TestDetachedThreads();
922 if ( 1 )
923 TestJoinableThreads();
924 if ( 1 )
925 TestThreadSuspend();
926 if ( 1 )
927 TestThreadDelete();
928
929 #endif // TEST_THREADS
930
931 #ifdef TEST_LONGLONG
932 if ( 0 )
933 TestSpeed();
934 if ( 1 )
935 TestDivision();
936 #endif // TEST_LONGLONG
937
938 #ifdef TEST_MIME
939 TestMimeEnum();
940 #endif // TEST_MIME
941
942 #ifdef TEST_TIME
943 if ( 0 )
944 {
945 TestTimeSet();
946 TestTimeStatic();
947 TestTimeZones();
948 TestTimeRange();
949 TestTimeTicks();
950 TestTimeJDN();
951 }
952 TestTimeWDays();
953 #endif // TEST_TIME
954
955 wxUninitialize();
956
957 return 0;
958 }