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