]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
wxMimeTypeManager::EnumAll() seems to work, couple of minor corrections to the MIME...
[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_LOG
34 #define TEST_MIME
35 //#define TEST_STRINGS
36 //#define TEST_THREADS
37 //#define TEST_TIME
38 //#define TEST_LONGLONG
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // MIME types
46 // ----------------------------------------------------------------------------
47
48 #ifdef TEST_MIME
49
50 #include <wx/mimetype.h>
51
52 static void TestMimeEnum()
53 {
54 wxMimeTypesManager mimeTM;
55 wxArrayString mimetypes;
56
57 size_t count = mimeTM.EnumAllFileTypes(mimetypes);
58
59 printf("*** All %u known filetypes: ***\n", count);
60
61 wxArrayString exts;
62 wxString desc;
63
64 for ( size_t n = 0; n < count; n++ )
65 {
66 wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]);
67 if ( !filetype )
68 {
69 printf("nothing known about the filetype '%s'!\n",
70 mimetypes[n].c_str());
71 continue;
72 }
73
74 filetype->GetDescription(&desc);
75 filetype->GetExtensions(exts);
76
77 wxString extsAll;
78 for ( size_t e = 0; e < exts.GetCount(); e++ )
79 {
80 if ( e > 0 )
81 extsAll << _T(", ");
82 extsAll += exts[e];
83 }
84
85 printf("\t%s: %s (%s)\n",
86 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
87 }
88 }
89
90 #endif // TEST_MIME
91
92 // ----------------------------------------------------------------------------
93 // long long
94 // ----------------------------------------------------------------------------
95
96 #ifdef TEST_LONGLONG
97
98 #include <wx/longlong.h>
99 #include <wx/timer.h>
100
101 static void TestSpeed()
102 {
103 static const long max = 100000000;
104 long n;
105
106 {
107 wxStopWatch sw;
108
109 long l = 0;
110 for ( n = 0; n < max; n++ )
111 {
112 l += n;
113 }
114
115 printf("Summing longs took %ld milliseconds.\n", sw.Time());
116 }
117
118 {
119 wxStopWatch sw;
120
121 __int64 l = 0;
122 for ( n = 0; n < max; n++ )
123 {
124 l += n;
125 }
126
127 printf("Summing __int64s took %ld milliseconds.\n", sw.Time());
128 }
129
130 {
131 wxStopWatch sw;
132
133 wxLongLong l;
134 for ( n = 0; n < max; n++ )
135 {
136 l += n;
137 }
138
139 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
140 }
141 }
142
143 static void TestDivision()
144 {
145 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
146
147 // seed pseudo random generator
148 //srand((unsigned)time(NULL));
149
150 size_t nTested = 0;
151 for ( size_t n = 0; n < 10000; n++ )
152 {
153 // get a random wxLongLong (shifting by 12 the MSB ensures that the
154 // multiplication will not overflow)
155 wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
156
157 wxASSERT( (ll * 1000l)/1000l == ll );
158
159 nTested++;
160 }
161
162 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested);
163
164 #undef MAKE_LL
165 }
166
167 #endif // TEST_LONGLONG
168
169 // ----------------------------------------------------------------------------
170 // date time
171 // ----------------------------------------------------------------------------
172
173 #ifdef TEST_TIME
174
175 #include <wx/datetime.h>
176
177 // this test miscellaneous static wxDateTime functions
178 static void TestTimeStatic()
179 {
180 puts("\n*** wxDateTime static methods test ***");
181
182 // some info about the current date
183 int year = wxDateTime::GetCurrentYear();
184 printf("Current year %d is %sa leap one and has %d days.\n",
185 year,
186 wxDateTime::IsLeapYear(year) ? "" : "not ",
187 wxDateTime::GetNumberOfDays(year));
188
189 wxDateTime::Month month = wxDateTime::GetCurrentMonth();
190 printf("Current month is '%s' ('%s') and it has %d days\n",
191 wxDateTime::GetMonthName(month, TRUE).c_str(),
192 wxDateTime::GetMonthName(month).c_str(),
193 wxDateTime::GetNumberOfDays(month));
194
195 // leap year logic
196 static const size_t nYears = 5;
197 static const size_t years[2][nYears] =
198 {
199 // first line: the years to test
200 { 1990, 1976, 2000, 2030, 1984, },
201
202 // second line: TRUE if leap, FALSE otherwise
203 { FALSE, TRUE, TRUE, FALSE, TRUE }
204 };
205
206 for ( size_t n = 0; n < nYears; n++ )
207 {
208 int year = years[0][n];
209 bool should = years[1][n] != 0;
210
211 printf("Year %d is %sa leap year (should be: %s)\n",
212 year,
213 wxDateTime::IsLeapYear(year) ? "" : "not ",
214 should ? "yes" : "no");
215
216 wxASSERT( should == wxDateTime::IsLeapYear(year) );
217 }
218 }
219
220 // test constructing wxDateTime objects
221 static void TestTimeSet()
222 {
223 puts("\n*** wxDateTime construction test ***");
224
225 printf("Current time:\t%s\n", wxDateTime::Now().Format().c_str());
226 printf("Unix epoch:\t%s\n", wxDateTime((time_t)0).Format().c_str());
227 printf("Today noon:\t%s\n", wxDateTime(12, 0).Format().c_str());
228 printf("May 29, 1976:\t%s\n", wxDateTime(29, wxDateTime::May, 1976).Format().c_str());
229 }
230
231 // test time zones stuff
232 static void TestTimeZones()
233 {
234 puts("\n*** wxDateTime timezone test ***");
235
236 wxDateTime now = wxDateTime::Now();
237
238 printf("Current GMT time:\t%s\n", now.ToGMT().Format().c_str());
239 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
240 printf("Current time in Paris:\t%s\n", now.ToTimezone(wxDateTime::CET).Format().c_str());
241 printf(" Moscow:\t%s\n", now.ToTimezone(wxDateTime::MSK).Format().c_str());
242 printf(" New York:\t%s\n", now.ToTimezone(wxDateTime::EST).Format().c_str());
243 }
244
245 // test some minimal support for the dates outside the standard range
246 static void TestTimeRange()
247 {
248 puts("\n*** wxDateTime out-of-standard-range dates test ***");
249
250 printf("Unix epoch:\t%s\n",
251 wxDateTime(2440587.5).Format().c_str());
252 printf("Feb 29, 0: \t%s\n",
253 wxDateTime(29, wxDateTime::Feb, 0).Format().c_str());
254 printf("JDN 0: \t%s\n",
255 wxDateTime(0.0).Format().c_str());
256 printf("Jan 1, 1AD:\t%s\n",
257 wxDateTime(1, wxDateTime::Jan, 1).Format().c_str());
258 printf("May 29, 2099:\t%s\n",
259 wxDateTime(29, wxDateTime::May, 2099).Format().c_str());
260 }
261
262 // test conversions to JDN &c
263 static void TestTimeJDN()
264 {
265 puts("\n*** wxDateTime to JDN test ***");
266
267 struct Date
268 {
269 wxDateTime::wxDateTime_t day;
270 wxDateTime::Month month;
271 int year;
272 double jdn;
273 };
274
275 static const Date testDates[] =
276 {
277 { 21, wxDateTime::Jan, 2222, 2532648.5 },
278 { 29, wxDateTime::May, 1976, 2442927.5 },
279 { 1, wxDateTime::Jan, 1970, 2440587.5 },
280 { 1, wxDateTime::Jan, 1900, 2415020.5 },
281 { 15, wxDateTime::Oct, 1582, 2299160.5 },
282 { 4, wxDateTime::Oct, 1582, 2299149.5 },
283 { 1, wxDateTime::Mar, 1, 1721484.5 },
284 { 1, wxDateTime::Jan, 1, 1721425.5 },
285 { 31, wxDateTime::Dec, 0, 1721424.5 },
286 { 1, wxDateTime::Jan, 0, 1721059.5 },
287 { 12, wxDateTime::Aug, -1234, 1270573.5 },
288 { 12, wxDateTime::Aug, -4000, 260313.5 },
289 { 24, wxDateTime::Nov, -4713, -0.5 },
290 };
291
292 for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
293 {
294 const Date& d = testDates[n];
295 wxDateTime dt(d.day, d.month, d.year);
296 double jdn = dt.GetJulianDayNumber();
297
298 printf("JDN of %s %02d, %4d%s is:\t%f",
299 wxDateTime::GetMonthName(d.month).c_str(),
300 d.day,
301 wxDateTime::ConvertYearToBC(d.year),
302 d.year > 0 ? "AD" : "BC",
303 jdn);
304 if ( jdn == d.jdn )
305 {
306 puts(" (ok)");
307 }
308 else
309 {
310 printf(" (ERROR: should be %f, delta = %f)\n",
311 d.jdn, jdn - d.jdn);
312 }
313 }
314 }
315
316 #endif // TEST_TIME
317
318 // ----------------------------------------------------------------------------
319 // threads
320 // ----------------------------------------------------------------------------
321
322 #ifdef TEST_THREADS
323
324 #include <wx/thread.h>
325
326 static size_t gs_counter = (size_t)-1;
327 static wxCriticalSection gs_critsect;
328 static wxCondition gs_cond;
329
330 class MyJoinableThread : public wxThread
331 {
332 public:
333 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
334 { m_n = n; Create(); }
335
336 // thread execution starts here
337 virtual ExitCode Entry();
338
339 private:
340 size_t m_n;
341 };
342
343 wxThread::ExitCode MyJoinableThread::Entry()
344 {
345 unsigned long res = 1;
346 for ( size_t n = 1; n < m_n; n++ )
347 {
348 res *= n;
349
350 // it's a loooong calculation :-)
351 Sleep(100);
352 }
353
354 return (ExitCode)res;
355 }
356
357 class MyDetachedThread : public wxThread
358 {
359 public:
360 MyDetachedThread(size_t n, char ch)
361 {
362 m_n = n;
363 m_ch = ch;
364 m_cancelled = FALSE;
365
366 Create();
367 }
368
369 // thread execution starts here
370 virtual ExitCode Entry();
371
372 // and stops here
373 virtual void OnExit();
374
375 private:
376 size_t m_n; // number of characters to write
377 char m_ch; // character to write
378
379 bool m_cancelled; // FALSE if we exit normally
380 };
381
382 wxThread::ExitCode MyDetachedThread::Entry()
383 {
384 {
385 wxCriticalSectionLocker lock(gs_critsect);
386 if ( gs_counter == (size_t)-1 )
387 gs_counter = 1;
388 else
389 gs_counter++;
390 }
391
392 for ( size_t n = 0; n < m_n; n++ )
393 {
394 if ( TestDestroy() )
395 {
396 m_cancelled = TRUE;
397
398 break;
399 }
400
401 putchar(m_ch);
402 fflush(stdout);
403
404 wxThread::Sleep(100);
405 }
406
407 return 0;
408 }
409
410 void MyDetachedThread::OnExit()
411 {
412 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
413
414 wxCriticalSectionLocker lock(gs_critsect);
415 if ( !--gs_counter && !m_cancelled )
416 gs_cond.Signal();
417 }
418
419 void TestDetachedThreads()
420 {
421 puts("\n*** Testing detached threads ***");
422
423 static const size_t nThreads = 3;
424 MyDetachedThread *threads[nThreads];
425 size_t n;
426 for ( n = 0; n < nThreads; n++ )
427 {
428 threads[n] = new MyDetachedThread(10, 'A' + n);
429 }
430
431 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
432 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
433
434 for ( n = 0; n < nThreads; n++ )
435 {
436 threads[n]->Run();
437 }
438
439 // wait until all threads terminate
440 gs_cond.Wait();
441
442 puts("");
443 }
444
445 void TestJoinableThreads()
446 {
447 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
448
449 // calc 10! in the background
450 MyJoinableThread thread(10);
451 thread.Run();
452
453 printf("\nThread terminated with exit code %lu.\n",
454 (unsigned long)thread.Wait());
455 }
456
457 void TestThreadSuspend()
458 {
459 puts("\n*** Testing thread suspend/resume functions ***");
460
461 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
462
463 thread->Run();
464
465 // this is for this demo only, in a real life program we'd use another
466 // condition variable which would be signaled from wxThread::Entry() to
467 // tell us that the thread really started running - but here just wait a
468 // bit and hope that it will be enough (the problem is, of course, that
469 // the thread might still not run when we call Pause() which will result
470 // in an error)
471 wxThread::Sleep(300);
472
473 for ( size_t n = 0; n < 3; n++ )
474 {
475 thread->Pause();
476
477 puts("\nThread suspended");
478 if ( n > 0 )
479 {
480 // don't sleep but resume immediately the first time
481 wxThread::Sleep(300);
482 }
483 puts("Going to resume the thread");
484
485 thread->Resume();
486 }
487
488 puts("Waiting until it terminates now");
489
490 // wait until the thread terminates
491 gs_cond.Wait();
492
493 puts("");
494 }
495
496 void TestThreadDelete()
497 {
498 // As above, using Sleep() is only for testing here - we must use some
499 // synchronisation object instead to ensure that the thread is still
500 // running when we delete it - deleting a detached thread which already
501 // terminated will lead to a crash!
502
503 puts("\n*** Testing thread delete function ***");
504
505 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
506
507 thread0->Delete();
508
509 puts("\nDeleted a thread which didn't start to run yet.");
510
511 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
512
513 thread1->Run();
514
515 wxThread::Sleep(300);
516
517 thread1->Delete();
518
519 puts("\nDeleted a running thread.");
520
521 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
522
523 thread2->Run();
524
525 wxThread::Sleep(300);
526
527 thread2->Pause();
528
529 thread2->Delete();
530
531 puts("\nDeleted a sleeping thread.");
532
533 MyJoinableThread thread3(20);
534 thread3.Run();
535
536 thread3.Delete();
537
538 puts("\nDeleted a joinable thread.");
539
540 MyJoinableThread thread4(2);
541 thread4.Run();
542
543 wxThread::Sleep(300);
544
545 thread4.Delete();
546
547 puts("\nDeleted a joinable thread which already terminated.");
548
549 puts("");
550 }
551
552 #endif // TEST_THREADS
553
554 // ----------------------------------------------------------------------------
555 // arrays
556 // ----------------------------------------------------------------------------
557
558 #ifdef TEST_ARRAYS
559
560 void PrintArray(const char* name, const wxArrayString& array)
561 {
562 printf("Dump of the array '%s'\n", name);
563
564 size_t nCount = array.GetCount();
565 for ( size_t n = 0; n < nCount; n++ )
566 {
567 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
568 }
569 }
570
571 #endif // TEST_ARRAYS
572
573 // ----------------------------------------------------------------------------
574 // strings
575 // ----------------------------------------------------------------------------
576
577 #ifdef TEST_STRINGS
578
579 #include "wx/timer.h"
580
581 void TestString()
582 {
583 wxStopWatch sw;
584
585 wxString a, b, c;
586
587 a.reserve (128);
588 b.reserve (128);
589 c.reserve (128);
590
591 for (int i = 0; i < 1000000; ++i)
592 {
593 a = "Hello";
594 b = " world";
595 c = "! How'ya doin'?";
596 a += b;
597 a += c;
598 c = "Hello world! What's up?";
599 if (c != a)
600 c = "Doh!";
601 }
602
603 printf ("TestString elapsed time: %ld\n", sw.Time());
604 }
605
606 void TestPChar()
607 {
608 wxStopWatch sw;
609
610 char a [128];
611 char b [128];
612 char c [128];
613
614 for (int i = 0; i < 1000000; ++i)
615 {
616 strcpy (a, "Hello");
617 strcpy (b, " world");
618 strcpy (c, "! How'ya doin'?");
619 strcat (a, b);
620 strcat (a, c);
621 strcpy (c, "Hello world! What's up?");
622 if (strcmp (c, a) == 0)
623 strcpy (c, "Doh!");
624 }
625
626 printf ("TestPChar elapsed time: %ld\n", sw.Time());
627 }
628
629 #endif // TEST_STRINGS
630
631 // ----------------------------------------------------------------------------
632 // entry point
633 // ----------------------------------------------------------------------------
634
635 int main(int argc, char **argv)
636 {
637 if ( !wxInitialize() )
638 {
639 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
640 }
641
642 #ifdef TEST_STRINGS
643 TestPChar();
644 TestString();
645 #endif // TEST_STRINGS
646
647 #ifdef TEST_ARRAYS
648 wxArrayString a1;
649 a1.Add("tiger");
650 a1.Add("cat");
651 a1.Add("lion");
652 a1.Add("dog");
653 a1.Add("human");
654 a1.Add("ape");
655
656 puts("*** Initially:");
657
658 PrintArray("a1", a1);
659
660 wxArrayString a2(a1);
661 PrintArray("a2", a2);
662
663 wxSortedArrayString a3(a1);
664 PrintArray("a3", a3);
665
666 puts("*** After deleting a string from a1");
667 a1.Remove(2);
668
669 PrintArray("a1", a1);
670 PrintArray("a2", a2);
671 PrintArray("a3", a3);
672
673 puts("*** After reassigning a1 to a2 and a3");
674 a3 = a2 = a1;
675 PrintArray("a2", a2);
676 PrintArray("a3", a3);
677 #endif // TEST_ARRAYS
678
679 #ifdef TEST_LOG
680 wxString s;
681 for ( size_t n = 0; n < 8000; n++ )
682 {
683 s << (char)('A' + (n % 26));
684 }
685
686 wxString msg;
687 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
688
689 // this one shouldn't be truncated
690 printf(msg);
691
692 // but this one will because log functions use fixed size buffer
693 // (note that it doesn't need '\n' at the end neither - will be added
694 // by wxLog anyhow)
695 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
696 #endif // TEST_LOG
697
698 #ifdef TEST_THREADS
699 int nCPUs = wxThread::GetCPUCount();
700 printf("This system has %d CPUs\n", nCPUs);
701 if ( nCPUs != -1 )
702 wxThread::SetConcurrency(nCPUs);
703
704 if ( argc > 1 && argv[1][0] == 't' )
705 wxLog::AddTraceMask("thread");
706
707 if ( 1 )
708 TestDetachedThreads();
709 if ( 1 )
710 TestJoinableThreads();
711 if ( 1 )
712 TestThreadSuspend();
713 if ( 1 )
714 TestThreadDelete();
715
716 #endif // TEST_THREADS
717
718 #ifdef TEST_LONGLONG
719 if ( 0 )
720 TestSpeed();
721 if ( 1 )
722 TestDivision();
723 #endif // TEST_LONGLONG
724
725 #ifdef TEST_MIME
726 TestMimeEnum();
727 #endif // TEST_MIME
728
729 #ifdef TEST_TIME
730 TestTimeStatic();
731 TestTimeSet();
732 TestTimeZones();
733 TestTimeRange();
734 TestTimeJDN();
735 #endif // TEST_TIME
736
737 wxUninitialize();
738
739 return 0;
740 }