]> git.saurik.com Git - wxWidgets.git/blame - samples/console/console.cpp
Fixed XIM compilation, but doesn't work yet.
[wxWidgets.git] / samples / console / console.cpp
CommitLineData
37667812
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/console/console.cpp
3// Purpose: a sample console (as opposed to GUI) progam using wxWindows
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
e87271f3
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
37667812
VZ
20#include <stdio.h>
21
22#include <wx/string.h>
bbfa0322 23#include <wx/file.h>
37667812 24#include <wx/app.h>
e87271f3
VZ
25
26// ----------------------------------------------------------------------------
27// conditional compilation
28// ----------------------------------------------------------------------------
29
30// what to test?
378b05f7
VZ
31
32//#define TEST_ARRAYS
b568d04f 33//#define TEST_LOG
696e1ea0 34#define TEST_MIME
9fc3ad34 35//#define TEST_STRINGS
696e1ea0 36//#define TEST_THREADS
ef8d96c2 37//#define TEST_TIME
9fc3ad34 38//#define TEST_LONGLONG
e87271f3
VZ
39
40// ============================================================================
41// implementation
42// ============================================================================
43
696e1ea0
VZ
44// ----------------------------------------------------------------------------
45// MIME types
46// ----------------------------------------------------------------------------
47
48#ifdef TEST_MIME
49
50#include <wx/mimetype.h>
51
52static 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 )
c61f4f6d
VZ
68 {
69 printf("nothing known about the filetype '%s'!\n",
70 mimetypes[n].c_str());
696e1ea0 71 continue;
c61f4f6d
VZ
72 }
73
696e1ea0
VZ
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
54acce90
VZ
85 printf("\t%s: %s (%s)\n",
86 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
696e1ea0
VZ
87 }
88}
89
90#endif // TEST_MIME
91
b76b015e
VZ
92// ----------------------------------------------------------------------------
93// long long
94// ----------------------------------------------------------------------------
95
96#ifdef TEST_LONGLONG
97
98#include <wx/longlong.h>
99#include <wx/timer.h>
100
101static void TestSpeed()
102{
103 static const long max = 100000000;
104 long n;
9fc3ad34 105
b76b015e
VZ
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
143static void TestDivision()
144{
2f02cb89 145 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
b76b015e 146
2f02cb89
VZ
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
b76b015e
VZ
165}
166
167#endif // TEST_LONGLONG
168
169// ----------------------------------------------------------------------------
170// date time
171// ----------------------------------------------------------------------------
172
173#ifdef TEST_TIME
174
175#include <wx/datetime.h>
176
2f02cb89
VZ
177// this test miscellaneous static wxDateTime functions
178static 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
fcc3d7cb
VZ
196 static const size_t nYears = 5;
197 static const size_t years[2][nYears] =
2f02cb89
VZ
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
221static 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
fcc3d7cb
VZ
231// test time zones stuff
232static 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());
1ef54dcf 239 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
fcc3d7cb
VZ
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
e6ec579c
VZ
245// test some minimal support for the dates outside the standard range
246static void TestTimeRange()
247{
248 puts("\n*** wxDateTime out-of-standard-range dates test ***");
249
1ef54dcf
VZ
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());
e6ec579c
VZ
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
1ef54dcf 263static void TestTimeJDN()
e6ec579c
VZ
264{
265 puts("\n*** wxDateTime to JDN test ***");
266
1ef54dcf
VZ
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 }
e6ec579c
VZ
314}
315
b76b015e
VZ
316#endif // TEST_TIME
317
e87271f3
VZ
318// ----------------------------------------------------------------------------
319// threads
320// ----------------------------------------------------------------------------
321
322#ifdef TEST_THREADS
323
bbfa0322 324#include <wx/thread.h>
37667812 325
bbfa0322
VZ
326static size_t gs_counter = (size_t)-1;
327static wxCriticalSection gs_critsect;
b568d04f 328static wxCondition gs_cond;
bbfa0322 329
b568d04f 330class MyJoinableThread : public wxThread
bbfa0322
VZ
331{
332public:
b568d04f
VZ
333 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
334 { m_n = n; Create(); }
bbfa0322
VZ
335
336 // thread execution starts here
b568d04f 337 virtual ExitCode Entry();
bbfa0322 338
b568d04f
VZ
339private:
340 size_t m_n;
bbfa0322
VZ
341};
342
b568d04f 343wxThread::ExitCode MyJoinableThread::Entry()
bbfa0322 344{
b568d04f
VZ
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 }
bbfa0322 353
b568d04f 354 return (ExitCode)res;
bbfa0322
VZ
355}
356
b568d04f
VZ
357class MyDetachedThread : public wxThread
358{
359public:
fcc3d7cb
VZ
360 MyDetachedThread(size_t n, char ch)
361 {
362 m_n = n;
363 m_ch = ch;
364 m_cancelled = FALSE;
365
366 Create();
367 }
b568d04f
VZ
368
369 // thread execution starts here
370 virtual ExitCode Entry();
371
372 // and stops here
373 virtual void OnExit();
374
375private:
9fc3ad34
VZ
376 size_t m_n; // number of characters to write
377 char m_ch; // character to write
fcc3d7cb
VZ
378
379 bool m_cancelled; // FALSE if we exit normally
b568d04f
VZ
380};
381
382wxThread::ExitCode MyDetachedThread::Entry()
bbfa0322
VZ
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
9fc3ad34 392 for ( size_t n = 0; n < m_n; n++ )
bbfa0322
VZ
393 {
394 if ( TestDestroy() )
fcc3d7cb
VZ
395 {
396 m_cancelled = TRUE;
397
bbfa0322 398 break;
fcc3d7cb 399 }
bbfa0322
VZ
400
401 putchar(m_ch);
402 fflush(stdout);
403
404 wxThread::Sleep(100);
405 }
406
b568d04f 407 return 0;
bbfa0322
VZ
408}
409
b568d04f 410void MyDetachedThread::OnExit()
bbfa0322 411{
9fc3ad34
VZ
412 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
413
bbfa0322 414 wxCriticalSectionLocker lock(gs_critsect);
fcc3d7cb 415 if ( !--gs_counter && !m_cancelled )
b568d04f 416 gs_cond.Signal();
bbfa0322
VZ
417}
418
9fc3ad34
VZ
419void TestDetachedThreads()
420{
2f02cb89 421 puts("\n*** Testing detached threads ***");
9fc3ad34
VZ
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
445void TestJoinableThreads()
446{
2f02cb89 447 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
9fc3ad34
VZ
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
457void TestThreadSuspend()
458{
2f02cb89
VZ
459 puts("\n*** Testing thread suspend/resume functions ***");
460
461 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
9fc3ad34
VZ
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
4c460b34
VZ
488 puts("Waiting until it terminates now");
489
9fc3ad34
VZ
490 // wait until the thread terminates
491 gs_cond.Wait();
492
493 puts("");
494}
495
2f02cb89
VZ
496void 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
4c460b34
VZ
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
2f02cb89
VZ
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
4c460b34
VZ
533 MyJoinableThread thread3(20);
534 thread3.Run();
2f02cb89 535
4c460b34 536 thread3.Delete();
2f02cb89
VZ
537
538 puts("\nDeleted a joinable thread.");
539
4c460b34
VZ
540 MyJoinableThread thread4(2);
541 thread4.Run();
2f02cb89
VZ
542
543 wxThread::Sleep(300);
544
4c460b34 545 thread4.Delete();
2f02cb89
VZ
546
547 puts("\nDeleted a joinable thread which already terminated.");
548
549 puts("");
550}
551
e87271f3
VZ
552#endif // TEST_THREADS
553
554// ----------------------------------------------------------------------------
555// arrays
556// ----------------------------------------------------------------------------
557
558#ifdef TEST_ARRAYS
559
560void 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
9fc3ad34
VZ
573// ----------------------------------------------------------------------------
574// strings
575// ----------------------------------------------------------------------------
576
577#ifdef TEST_STRINGS
578
579#include "wx/timer.h"
580
581void 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
606void 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
e87271f3
VZ
631// ----------------------------------------------------------------------------
632// entry point
633// ----------------------------------------------------------------------------
634
bbfa0322 635int main(int argc, char **argv)
37667812
VZ
636{
637 if ( !wxInitialize() )
638 {
639 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
640 }
641
9fc3ad34
VZ
642#ifdef TEST_STRINGS
643 TestPChar();
644 TestString();
645#endif // TEST_STRINGS
646
e87271f3
VZ
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
378b05f7
VZ
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
b568d04f
VZ
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());
378b05f7
VZ
696#endif // TEST_LOG
697
e87271f3 698#ifdef TEST_THREADS
696e1ea0
VZ
699 int nCPUs = wxThread::GetCPUCount();
700 printf("This system has %d CPUs\n", nCPUs);
701 if ( nCPUs != -1 )
702 wxThread::SetConcurrency(nCPUs);
ef8d96c2 703
9fc3ad34
VZ
704 if ( argc > 1 && argv[1][0] == 't' )
705 wxLog::AddTraceMask("thread");
b568d04f 706
4c460b34 707 if ( 1 )
2f02cb89 708 TestDetachedThreads();
4c460b34 709 if ( 1 )
2f02cb89 710 TestJoinableThreads();
4c460b34 711 if ( 1 )
2f02cb89
VZ
712 TestThreadSuspend();
713 if ( 1 )
714 TestThreadDelete();
715
e87271f3 716#endif // TEST_THREADS
37667812 717
b76b015e
VZ
718#ifdef TEST_LONGLONG
719 if ( 0 )
720 TestSpeed();
721 if ( 1 )
722 TestDivision();
723#endif // TEST_LONGLONG
724
696e1ea0
VZ
725#ifdef TEST_MIME
726 TestMimeEnum();
727#endif // TEST_MIME
728
b76b015e 729#ifdef TEST_TIME
2f02cb89
VZ
730 TestTimeStatic();
731 TestTimeSet();
fcc3d7cb 732 TestTimeZones();
e6ec579c 733 TestTimeRange();
1ef54dcf 734 TestTimeJDN();
b76b015e
VZ
735#endif // TEST_TIME
736
37667812
VZ
737 wxUninitialize();
738
739 return 0;
740}