1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
22 #include <wx/string.h>
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
35 //#define TEST_STRINGS
36 //#define TEST_THREADS
38 //#define TEST_LONGLONG
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
50 #include <wx/mimetype.h>
52 static void TestMimeEnum()
54 wxMimeTypesManager mimeTM
;
55 wxArrayString mimetypes
;
57 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
59 printf("*** All %u known filetypes: ***\n", count
);
64 for ( size_t n
= 0; n
< count
; n
++ )
66 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
69 printf("nothing known about the filetype '%s'!\n",
70 mimetypes
[n
].c_str());
74 filetype
->GetDescription(&desc
);
75 filetype
->GetExtensions(exts
);
78 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
85 printf("\t%s: %s (%s)\n", mimetypes
[n
], desc
, extsAll
);
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
97 #include <wx/longlong.h>
100 static void TestSpeed()
102 static const long max
= 100000000;
109 for ( n
= 0; n
< max
; n
++ )
114 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
121 for ( n
= 0; n
< max
; n
++ )
126 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
133 for ( n
= 0; n
< max
; n
++ )
138 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
142 static void TestDivision()
144 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
146 // seed pseudo random generator
147 //srand((unsigned)time(NULL));
150 for ( size_t n
= 0; n
< 10000; n
++ )
152 // get a random wxLongLong (shifting by 12 the MSB ensures that the
153 // multiplication will not overflow)
154 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
156 wxASSERT( (ll
* 1000l)/1000l == ll
);
161 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
166 #endif // TEST_LONGLONG
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
174 #include <wx/datetime.h>
176 // this test miscellaneous static wxDateTime functions
177 static void TestTimeStatic()
179 puts("\n*** wxDateTime static methods test ***");
181 // some info about the current date
182 int year
= wxDateTime::GetCurrentYear();
183 printf("Current year %d is %sa leap one and has %d days.\n",
185 wxDateTime::IsLeapYear(year
) ? "" : "not ",
186 wxDateTime::GetNumberOfDays(year
));
188 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
189 printf("Current month is '%s' ('%s') and it has %d days\n",
190 wxDateTime::GetMonthName(month
, TRUE
).c_str(),
191 wxDateTime::GetMonthName(month
).c_str(),
192 wxDateTime::GetNumberOfDays(month
));
195 static const size_t nYears
= 5;
196 static const size_t years
[2][nYears
] =
198 // first line: the years to test
199 { 1990, 1976, 2000, 2030, 1984, },
201 // second line: TRUE if leap, FALSE otherwise
202 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
205 for ( size_t n
= 0; n
< nYears
; n
++ )
207 int year
= years
[0][n
];
208 bool should
= years
[1][n
] != 0;
210 printf("Year %d is %sa leap year (should be: %s)\n",
212 wxDateTime::IsLeapYear(year
) ? "" : "not ",
213 should
? "yes" : "no");
215 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
219 // test constructing wxDateTime objects
220 static void TestTimeSet()
222 puts("\n*** wxDateTime construction test ***");
224 printf("Current time:\t%s\n", wxDateTime::Now().Format().c_str());
225 printf("Unix epoch:\t%s\n", wxDateTime((time_t)0).Format().c_str());
226 printf("Today noon:\t%s\n", wxDateTime(12, 0).Format().c_str());
227 printf("May 29, 1976:\t%s\n", wxDateTime(29, wxDateTime::May
, 1976).Format().c_str());
230 // test time zones stuff
231 static void TestTimeZones()
233 puts("\n*** wxDateTime timezone test ***");
235 wxDateTime now
= wxDateTime::Now();
237 printf("Current GMT time:\t%s\n", now
.ToGMT().Format().c_str());
238 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
239 printf("Current time in Paris:\t%s\n", now
.ToTimezone(wxDateTime::CET
).Format().c_str());
240 printf(" Moscow:\t%s\n", now
.ToTimezone(wxDateTime::MSK
).Format().c_str());
241 printf(" New York:\t%s\n", now
.ToTimezone(wxDateTime::EST
).Format().c_str());
244 // test some minimal support for the dates outside the standard range
245 static void TestTimeRange()
247 puts("\n*** wxDateTime out-of-standard-range dates test ***");
249 printf("Unix epoch:\t%s\n",
250 wxDateTime(2440587.5).Format().c_str());
251 printf("Feb 29, 0: \t%s\n",
252 wxDateTime(29, wxDateTime::Feb
, 0).Format().c_str());
253 printf("JDN 0: \t%s\n",
254 wxDateTime(0.0).Format().c_str());
255 printf("Jan 1, 1AD:\t%s\n",
256 wxDateTime(1, wxDateTime::Jan
, 1).Format().c_str());
257 printf("May 29, 2099:\t%s\n",
258 wxDateTime(29, wxDateTime::May
, 2099).Format().c_str());
261 // test conversions to JDN &c
262 static void TestTimeJDN()
264 puts("\n*** wxDateTime to JDN test ***");
268 wxDateTime::wxDateTime_t day
;
269 wxDateTime::Month month
;
274 static const Date testDates
[] =
276 { 21, wxDateTime::Jan
, 2222, 2532648.5 },
277 { 29, wxDateTime::May
, 1976, 2442927.5 },
278 { 1, wxDateTime::Jan
, 1970, 2440587.5 },
279 { 1, wxDateTime::Jan
, 1900, 2415020.5 },
280 { 15, wxDateTime::Oct
, 1582, 2299160.5 },
281 { 4, wxDateTime::Oct
, 1582, 2299149.5 },
282 { 1, wxDateTime::Mar
, 1, 1721484.5 },
283 { 1, wxDateTime::Jan
, 1, 1721425.5 },
284 { 31, wxDateTime::Dec
, 0, 1721424.5 },
285 { 1, wxDateTime::Jan
, 0, 1721059.5 },
286 { 12, wxDateTime::Aug
, -1234, 1270573.5 },
287 { 12, wxDateTime::Aug
, -4000, 260313.5 },
288 { 24, wxDateTime::Nov
, -4713, -0.5 },
291 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
293 const Date
& d
= testDates
[n
];
294 wxDateTime
dt(d
.day
, d
.month
, d
.year
);
295 double jdn
= dt
.GetJulianDayNumber();
297 printf("JDN of %s %02d, %4d%s is:\t%f",
298 wxDateTime::GetMonthName(d
.month
).c_str(),
300 wxDateTime::ConvertYearToBC(d
.year
),
301 d
.year
> 0 ? "AD" : "BC",
309 printf(" (ERROR: should be %f, delta = %f)\n",
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
323 #include <wx/thread.h>
325 static size_t gs_counter
= (size_t)-1;
326 static wxCriticalSection gs_critsect
;
327 static wxCondition gs_cond
;
329 class MyJoinableThread
: public wxThread
332 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
333 { m_n
= n
; Create(); }
335 // thread execution starts here
336 virtual ExitCode
Entry();
342 wxThread::ExitCode
MyJoinableThread::Entry()
344 unsigned long res
= 1;
345 for ( size_t n
= 1; n
< m_n
; n
++ )
349 // it's a loooong calculation :-)
353 return (ExitCode
)res
;
356 class MyDetachedThread
: public wxThread
359 MyDetachedThread(size_t n
, char ch
)
368 // thread execution starts here
369 virtual ExitCode
Entry();
372 virtual void OnExit();
375 size_t m_n
; // number of characters to write
376 char m_ch
; // character to write
378 bool m_cancelled
; // FALSE if we exit normally
381 wxThread::ExitCode
MyDetachedThread::Entry()
384 wxCriticalSectionLocker
lock(gs_critsect
);
385 if ( gs_counter
== (size_t)-1 )
391 for ( size_t n
= 0; n
< m_n
; n
++ )
403 wxThread::Sleep(100);
409 void MyDetachedThread::OnExit()
411 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
413 wxCriticalSectionLocker
lock(gs_critsect
);
414 if ( !--gs_counter
&& !m_cancelled
)
418 void TestDetachedThreads()
420 puts("\n*** Testing detached threads ***");
422 static const size_t nThreads
= 3;
423 MyDetachedThread
*threads
[nThreads
];
425 for ( n
= 0; n
< nThreads
; n
++ )
427 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
430 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
431 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
433 for ( n
= 0; n
< nThreads
; n
++ )
438 // wait until all threads terminate
444 void TestJoinableThreads()
446 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
448 // calc 10! in the background
449 MyJoinableThread
thread(10);
452 printf("\nThread terminated with exit code %lu.\n",
453 (unsigned long)thread
.Wait());
456 void TestThreadSuspend()
458 puts("\n*** Testing thread suspend/resume functions ***");
460 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
464 // this is for this demo only, in a real life program we'd use another
465 // condition variable which would be signaled from wxThread::Entry() to
466 // tell us that the thread really started running - but here just wait a
467 // bit and hope that it will be enough (the problem is, of course, that
468 // the thread might still not run when we call Pause() which will result
470 wxThread::Sleep(300);
472 for ( size_t n
= 0; n
< 3; n
++ )
476 puts("\nThread suspended");
479 // don't sleep but resume immediately the first time
480 wxThread::Sleep(300);
482 puts("Going to resume the thread");
487 puts("Waiting until it terminates now");
489 // wait until the thread terminates
495 void TestThreadDelete()
497 // As above, using Sleep() is only for testing here - we must use some
498 // synchronisation object instead to ensure that the thread is still
499 // running when we delete it - deleting a detached thread which already
500 // terminated will lead to a crash!
502 puts("\n*** Testing thread delete function ***");
504 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
508 puts("\nDeleted a thread which didn't start to run yet.");
510 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
514 wxThread::Sleep(300);
518 puts("\nDeleted a running thread.");
520 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
524 wxThread::Sleep(300);
530 puts("\nDeleted a sleeping thread.");
532 MyJoinableThread
thread3(20);
537 puts("\nDeleted a joinable thread.");
539 MyJoinableThread
thread4(2);
542 wxThread::Sleep(300);
546 puts("\nDeleted a joinable thread which already terminated.");
551 #endif // TEST_THREADS
553 // ----------------------------------------------------------------------------
555 // ----------------------------------------------------------------------------
559 void PrintArray(const char* name
, const wxArrayString
& array
)
561 printf("Dump of the array '%s'\n", name
);
563 size_t nCount
= array
.GetCount();
564 for ( size_t n
= 0; n
< nCount
; n
++ )
566 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
570 #endif // TEST_ARRAYS
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
578 #include "wx/timer.h"
590 for (int i
= 0; i
< 1000000; ++i
)
594 c
= "! How'ya doin'?";
597 c
= "Hello world! What's up?";
602 printf ("TestString elapsed time: %ld\n", sw
.Time());
613 for (int i
= 0; i
< 1000000; ++i
)
616 strcpy (b
, " world");
617 strcpy (c
, "! How'ya doin'?");
620 strcpy (c
, "Hello world! What's up?");
621 if (strcmp (c
, a
) == 0)
625 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
628 #endif // TEST_STRINGS
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 int main(int argc
, char **argv
)
636 if ( !wxInitialize() )
638 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
644 #endif // TEST_STRINGS
655 puts("*** Initially:");
657 PrintArray("a1", a1
);
659 wxArrayString
a2(a1
);
660 PrintArray("a2", a2
);
662 wxSortedArrayString
a3(a1
);
663 PrintArray("a3", a3
);
665 puts("*** After deleting a string from a1");
668 PrintArray("a1", a1
);
669 PrintArray("a2", a2
);
670 PrintArray("a3", a3
);
672 puts("*** After reassigning a1 to a2 and a3");
674 PrintArray("a2", a2
);
675 PrintArray("a3", a3
);
676 #endif // TEST_ARRAYS
680 for ( size_t n
= 0; n
< 8000; n
++ )
682 s
<< (char)('A' + (n
% 26));
686 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
688 // this one shouldn't be truncated
691 // but this one will because log functions use fixed size buffer
692 // (note that it doesn't need '\n' at the end neither - will be added
694 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
698 int nCPUs
= wxThread::GetCPUCount();
699 printf("This system has %d CPUs\n", nCPUs
);
701 wxThread::SetConcurrency(nCPUs
);
703 if ( argc
> 1 && argv
[1][0] == 't' )
704 wxLog::AddTraceMask("thread");
707 TestDetachedThreads();
709 TestJoinableThreads();
715 #endif // TEST_THREADS
722 #endif // TEST_LONGLONG