]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
1b29d30af23b167fe6bc6731895895ba7dcb853b
[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 time_t gmticks, ticks;
266
267 void Init(const wxDateTime::Tm& tm)
268 {
269 day = tm.mday;
270 month = tm.mon;
271 year = tm.year;
272 hour = tm.hour;
273 min = tm.min;
274 sec = tm.sec;
275 jdn = 0.0;
276 gmticks = ticks = -1;
277 }
278
279 wxDateTime DT() const
280 { return wxDateTime(day, month, year, hour, min, sec); }
281
282 wxString Format() const
283 {
284 wxString s;
285 s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
286 hour, min, sec,
287 wxDateTime::GetMonthName(month).c_str(),
288 day,
289 abs(wxDateTime::ConvertYearToBC(year)),
290 year > 0 ? "AD" : "BC");
291 return s;
292 }
293 };
294
295 static const Date testDates[] =
296 {
297 { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, 0, -3600 },
298 { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, -1, -1 },
299 { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, 202219200, 202212000 },
300 { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, 194400000, 194396400 },
301 { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, -1, -1 },
302 { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, -1, -1 },
303 { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, -1, -1 },
304 { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, -1, -1 },
305 { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, -1, -1 },
306 { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, -1, -1 },
307 { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, -1, -1 },
308 { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, -1, -1 },
309 { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, -1, -1 },
310 { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, -1, -1 },
311 { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, -1, -1 },
312 };
313
314 // this test miscellaneous static wxDateTime functions
315 static void TestTimeStatic()
316 {
317 puts("\n*** wxDateTime static methods test ***");
318
319 // some info about the current date
320 int year = wxDateTime::GetCurrentYear();
321 printf("Current year %d is %sa leap one and has %d days.\n",
322 year,
323 wxDateTime::IsLeapYear(year) ? "" : "not ",
324 wxDateTime::GetNumberOfDays(year));
325
326 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
327 printf("Current month is '%s' ('%s') and it has %d days\n",
328 wxDateTime::GetMonthName(month, TRUE).c_str(),
329 wxDateTime::GetMonthName(month).c_str(),
330 wxDateTime::GetNumberOfDays(month));
331
332 // leap year logic
333 static const size_t nYears = 5;
334 static const size_t years[2][nYears] =
335 {
336 // first line: the years to test
337 { 1990, 1976, 2000, 2030, 1984, },
338
339 // second line: TRUE if leap, FALSE otherwise
340 { FALSE, TRUE, TRUE, FALSE, TRUE }
341 };
342
343 for ( size_t n = 0; n < nYears; n++ )
344 {
345 int year = years[0][n];
346 bool should = years[1][n] != 0;
347
348 printf("Year %d is %sa leap year (should be: %s)\n",
349 year,
350 wxDateTime::IsLeapYear(year) ? "" : "not ",
351 should ? "yes" : "no");
352
353 wxASSERT( should == wxDateTime::IsLeapYear(year) );
354 }
355 }
356
357 // test constructing wxDateTime objects
358 static void TestTimeSet()
359 {
360 puts("\n*** wxDateTime construction test ***");
361
362 #if 0
363 printf("Current time:\t%s\n", wxDateTime::Now().Format().c_str());
364 printf("Unix epoch:\t%s\n", wxDateTime((time_t)0).Format().c_str());
365 printf("Today noon:\t%s\n", wxDateTime(12, 0).Format().c_str());
366 printf("May 29, 1976:\t%s\n", wxDateTime(29, wxDateTime::May, 1976).Format().c_str());
367 printf("Jan 1, 1900:\t%s\n", wxDateTime(1, wxDateTime::Jan, 1900).Format().c_str());
368 #else
369 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
370 {
371 const Date& d1 = testDates[n];
372 wxDateTime dt = d1.DT();
373
374 Date d2;
375 d2.Init(dt.GetTm());
376
377 wxString s1 = d1.Format(),
378 s2 = d2.Format();
379
380 printf("Date: %s == %s (%s)\n",
381 s1.c_str(), s2.c_str(),
382 s1 == s2 ? "ok" : "ERROR");
383 }
384 #endif
385 }
386
387 // test time zones stuff
388 static void TestTimeZones()
389 {
390 puts("\n*** wxDateTime timezone test ***");
391
392 wxDateTime now = wxDateTime::Now();
393
394 printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
395 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
396 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
397 printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
398 printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
399 printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
400 }
401
402 // test some minimal support for the dates outside the standard range
403 static void TestTimeRange()
404 {
405 puts("\n*** wxDateTime out-of-standard-range dates test ***");
406
407 printf("Unix epoch:\t%s\n",
408 wxDateTime(2440587.5).Format().c_str());
409 printf("Feb 29, 0: \t%s\n",
410 wxDateTime(29, wxDateTime::Feb, 0).Format().c_str());
411 printf("JDN 0: \t%s\n",
412 wxDateTime(0.0).Format().c_str());
413 printf("Jan 1, 1AD:\t%s\n",
414 wxDateTime(1, wxDateTime::Jan, 1).Format().c_str());
415 printf("May 29, 2099:\t%s\n",
416 wxDateTime(29, wxDateTime::May, 2099).Format().c_str());
417 }
418
419 static void TestTimeTicks()
420 {
421 puts("\n*** wxDateTime ticks test ***");
422
423 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
424 {
425 const Date& d = testDates[n];
426 if ( d.ticks == -1 )
427 continue;
428
429 wxDateTime dt = d.DT();
430 long ticks = (dt.GetValue() / 1000).ToLong();
431 printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
432 if ( ticks == d.ticks )
433 {
434 puts(" (ok)");
435 }
436 else
437 {
438 printf(" (ERROR: should be %ld, delta = %ld)\n",
439 d.ticks, ticks - d.ticks);
440 }
441
442 dt = d.DT().ToTimezone(wxDateTime::GMT0);
443 ticks = (dt.GetValue() / 1000).ToLong();
444 printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
445 if ( ticks == d.gmticks )
446 {
447 puts(" (ok)");
448 }
449 else
450 {
451 printf(" (ERROR: should be %ld, delta = %ld)\n",
452 d.gmticks, ticks - d.gmticks);
453 }
454 }
455
456 puts("");
457 }
458
459 // test conversions to JDN &c
460 static void TestTimeJDN()
461 {
462 puts("\n*** wxDateTime to JDN test ***");
463
464 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
465 {
466 const Date& d = testDates[n];
467 wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
468 double jdn = dt.GetJulianDayNumber();
469
470 printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
471 if ( jdn == d.jdn )
472 {
473 puts(" (ok)");
474 }
475 else
476 {
477 printf(" (ERROR: should be %f, delta = %f)\n",
478 d.jdn, jdn - d.jdn);
479 }
480 }
481 }
482
483 #endif // TEST_TIME
484
485 // ----------------------------------------------------------------------------
486 // threads
487 // ----------------------------------------------------------------------------
488
489 #ifdef TEST_THREADS
490
491 #include <wx/thread.h>
492
493 static size_t gs_counter = (size_t)-1;
494 static wxCriticalSection gs_critsect;
495 static wxCondition gs_cond;
496
497 class MyJoinableThread : public wxThread
498 {
499 public:
500 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
501 { m_n = n; Create(); }
502
503 // thread execution starts here
504 virtual ExitCode Entry();
505
506 private:
507 size_t m_n;
508 };
509
510 wxThread::ExitCode MyJoinableThread::Entry()
511 {
512 unsigned long res = 1;
513 for ( size_t n = 1; n < m_n; n++ )
514 {
515 res *= n;
516
517 // it's a loooong calculation :-)
518 Sleep(100);
519 }
520
521 return (ExitCode)res;
522 }
523
524 class MyDetachedThread : public wxThread
525 {
526 public:
527 MyDetachedThread(size_t n, char ch)
528 {
529 m_n = n;
530 m_ch = ch;
531 m_cancelled = FALSE;
532
533 Create();
534 }
535
536 // thread execution starts here
537 virtual ExitCode Entry();
538
539 // and stops here
540 virtual void OnExit();
541
542 private:
543 size_t m_n; // number of characters to write
544 char m_ch; // character to write
545
546 bool m_cancelled; // FALSE if we exit normally
547 };
548
549 wxThread::ExitCode MyDetachedThread::Entry()
550 {
551 {
552 wxCriticalSectionLocker lock(gs_critsect);
553 if ( gs_counter == (size_t)-1 )
554 gs_counter = 1;
555 else
556 gs_counter++;
557 }
558
559 for ( size_t n = 0; n < m_n; n++ )
560 {
561 if ( TestDestroy() )
562 {
563 m_cancelled = TRUE;
564
565 break;
566 }
567
568 putchar(m_ch);
569 fflush(stdout);
570
571 wxThread::Sleep(100);
572 }
573
574 return 0;
575 }
576
577 void MyDetachedThread::OnExit()
578 {
579 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
580
581 wxCriticalSectionLocker lock(gs_critsect);
582 if ( !--gs_counter && !m_cancelled )
583 gs_cond.Signal();
584 }
585
586 void TestDetachedThreads()
587 {
588 puts("\n*** Testing detached threads ***");
589
590 static const size_t nThreads = 3;
591 MyDetachedThread *threads[nThreads];
592 size_t n;
593 for ( n = 0; n < nThreads; n++ )
594 {
595 threads[n] = new MyDetachedThread(10, 'A' + n);
596 }
597
598 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
599 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
600
601 for ( n = 0; n < nThreads; n++ )
602 {
603 threads[n]->Run();
604 }
605
606 // wait until all threads terminate
607 gs_cond.Wait();
608
609 puts("");
610 }
611
612 void TestJoinableThreads()
613 {
614 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
615
616 // calc 10! in the background
617 MyJoinableThread thread(10);
618 thread.Run();
619
620 printf("\nThread terminated with exit code %lu.\n",
621 (unsigned long)thread.Wait());
622 }
623
624 void TestThreadSuspend()
625 {
626 puts("\n*** Testing thread suspend/resume functions ***");
627
628 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
629
630 thread->Run();
631
632 // this is for this demo only, in a real life program we'd use another
633 // condition variable which would be signaled from wxThread::Entry() to
634 // tell us that the thread really started running - but here just wait a
635 // bit and hope that it will be enough (the problem is, of course, that
636 // the thread might still not run when we call Pause() which will result
637 // in an error)
638 wxThread::Sleep(300);
639
640 for ( size_t n = 0; n < 3; n++ )
641 {
642 thread->Pause();
643
644 puts("\nThread suspended");
645 if ( n > 0 )
646 {
647 // don't sleep but resume immediately the first time
648 wxThread::Sleep(300);
649 }
650 puts("Going to resume the thread");
651
652 thread->Resume();
653 }
654
655 puts("Waiting until it terminates now");
656
657 // wait until the thread terminates
658 gs_cond.Wait();
659
660 puts("");
661 }
662
663 void TestThreadDelete()
664 {
665 // As above, using Sleep() is only for testing here - we must use some
666 // synchronisation object instead to ensure that the thread is still
667 // running when we delete it - deleting a detached thread which already
668 // terminated will lead to a crash!
669
670 puts("\n*** Testing thread delete function ***");
671
672 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
673
674 thread0->Delete();
675
676 puts("\nDeleted a thread which didn't start to run yet.");
677
678 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
679
680 thread1->Run();
681
682 wxThread::Sleep(300);
683
684 thread1->Delete();
685
686 puts("\nDeleted a running thread.");
687
688 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
689
690 thread2->Run();
691
692 wxThread::Sleep(300);
693
694 thread2->Pause();
695
696 thread2->Delete();
697
698 puts("\nDeleted a sleeping thread.");
699
700 MyJoinableThread thread3(20);
701 thread3.Run();
702
703 thread3.Delete();
704
705 puts("\nDeleted a joinable thread.");
706
707 MyJoinableThread thread4(2);
708 thread4.Run();
709
710 wxThread::Sleep(300);
711
712 thread4.Delete();
713
714 puts("\nDeleted a joinable thread which already terminated.");
715
716 puts("");
717 }
718
719 #endif // TEST_THREADS
720
721 // ----------------------------------------------------------------------------
722 // arrays
723 // ----------------------------------------------------------------------------
724
725 #ifdef TEST_ARRAYS
726
727 void PrintArray(const char* name, const wxArrayString& array)
728 {
729 printf("Dump of the array '%s'\n", name);
730
731 size_t nCount = array.GetCount();
732 for ( size_t n = 0; n < nCount; n++ )
733 {
734 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
735 }
736 }
737
738 #endif // TEST_ARRAYS
739
740 // ----------------------------------------------------------------------------
741 // strings
742 // ----------------------------------------------------------------------------
743
744 #ifdef TEST_STRINGS
745
746 #include "wx/timer.h"
747
748 static void TestString()
749 {
750 wxStopWatch sw;
751
752 wxString a, b, c;
753
754 a.reserve (128);
755 b.reserve (128);
756 c.reserve (128);
757
758 for (int i = 0; i < 1000000; ++i)
759 {
760 a = "Hello";
761 b = " world";
762 c = "! How'ya doin'?";
763 a += b;
764 a += c;
765 c = "Hello world! What's up?";
766 if (c != a)
767 c = "Doh!";
768 }
769
770 printf ("TestString elapsed time: %ld\n", sw.Time());
771 }
772
773 static void TestPChar()
774 {
775 wxStopWatch sw;
776
777 char a [128];
778 char b [128];
779 char c [128];
780
781 for (int i = 0; i < 1000000; ++i)
782 {
783 strcpy (a, "Hello");
784 strcpy (b, " world");
785 strcpy (c, "! How'ya doin'?");
786 strcat (a, b);
787 strcat (a, c);
788 strcpy (c, "Hello world! What's up?");
789 if (strcmp (c, a) == 0)
790 strcpy (c, "Doh!");
791 }
792
793 printf ("TestPChar elapsed time: %ld\n", sw.Time());
794 }
795
796 static void TestStringSub()
797 {
798 wxString s("Hello, world!");
799
800 puts("*** Testing wxString substring extraction ***");
801
802 printf("String = '%s'\n", s.c_str());
803 printf("Left(5) = '%s'\n", s.Left(5).c_str());
804 printf("Right(6) = '%s'\n", s.Right(6).c_str());
805 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
806 printf("Mid(3) = '%s'\n", s.Mid(3).c_str());
807 printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str());
808 printf("substr(3) = '%s'\n", s.substr(3).c_str());
809
810 puts("");
811 }
812
813 #endif // TEST_STRINGS
814
815 // ----------------------------------------------------------------------------
816 // entry point
817 // ----------------------------------------------------------------------------
818
819 int main(int argc, char **argv)
820 {
821 if ( !wxInitialize() )
822 {
823 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
824 }
825
826 #ifdef TEST_STRINGS
827 if ( 0 )
828 {
829 TestPChar();
830 TestString();
831 }
832 TestStringSub();
833 #endif // TEST_STRINGS
834
835 #ifdef TEST_ARRAYS
836 wxArrayString a1;
837 a1.Add("tiger");
838 a1.Add("cat");
839 a1.Add("lion");
840 a1.Add("dog");
841 a1.Add("human");
842 a1.Add("ape");
843
844 puts("*** Initially:");
845
846 PrintArray("a1", a1);
847
848 wxArrayString a2(a1);
849 PrintArray("a2", a2);
850
851 wxSortedArrayString a3(a1);
852 PrintArray("a3", a3);
853
854 puts("*** After deleting a string from a1");
855 a1.Remove(2);
856
857 PrintArray("a1", a1);
858 PrintArray("a2", a2);
859 PrintArray("a3", a3);
860
861 puts("*** After reassigning a1 to a2 and a3");
862 a3 = a2 = a1;
863 PrintArray("a2", a2);
864 PrintArray("a3", a3);
865 #endif // TEST_ARRAYS
866
867 #ifdef TEST_DIR
868 TestDirEnum();
869 #endif // TEST_DIR
870
871 #ifdef TEST_LOG
872 wxString s;
873 for ( size_t n = 0; n < 8000; n++ )
874 {
875 s << (char)('A' + (n % 26));
876 }
877
878 wxString msg;
879 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
880
881 // this one shouldn't be truncated
882 printf(msg);
883
884 // but this one will because log functions use fixed size buffer
885 // (note that it doesn't need '\n' at the end neither - will be added
886 // by wxLog anyhow)
887 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
888 #endif // TEST_LOG
889
890 #ifdef TEST_THREADS
891 int nCPUs = wxThread::GetCPUCount();
892 printf("This system has %d CPUs\n", nCPUs);
893 if ( nCPUs != -1 )
894 wxThread::SetConcurrency(nCPUs);
895
896 if ( argc > 1 && argv[1][0] == 't' )
897 wxLog::AddTraceMask("thread");
898
899 if ( 1 )
900 TestDetachedThreads();
901 if ( 1 )
902 TestJoinableThreads();
903 if ( 1 )
904 TestThreadSuspend();
905 if ( 1 )
906 TestThreadDelete();
907
908 #endif // TEST_THREADS
909
910 #ifdef TEST_LONGLONG
911 if ( 0 )
912 TestSpeed();
913 if ( 1 )
914 TestDivision();
915 #endif // TEST_LONGLONG
916
917 #ifdef TEST_MIME
918 TestMimeEnum();
919 #endif // TEST_MIME
920
921 #ifdef TEST_TIME
922 TestTimeSet();
923 if ( 0 )
924 {
925 TestTimeStatic();
926 TestTimeZones();
927 TestTimeRange();
928 TestTimeTicks();
929 TestTimeJDN();
930 }
931 #endif // TEST_TIME
932
933 wxUninitialize();
934
935 return 0;
936 }