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 filetype
->GetDescription(&desc
);
70 filetype
->GetExtensions(exts
);
73 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
80 printf("\t%s: %s (%s)\n", mimetypes
[n
], desc
, extsAll
);
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 #include <wx/longlong.h>
95 static void TestSpeed()
97 static const long max
= 100000000;
104 for ( n
= 0; n
< max
; n
++ )
109 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
116 for ( n
= 0; n
< max
; n
++ )
121 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
128 for ( n
= 0; n
< max
; n
++ )
133 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
137 static void TestDivision()
139 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
141 // seed pseudo random generator
142 //srand((unsigned)time(NULL));
145 for ( size_t n
= 0; n
< 10000; n
++ )
147 // get a random wxLongLong (shifting by 12 the MSB ensures that the
148 // multiplication will not overflow)
149 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
151 wxASSERT( (ll
* 1000l)/1000l == ll
);
156 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
161 #endif // TEST_LONGLONG
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 #include <wx/datetime.h>
171 // this test miscellaneous static wxDateTime functions
172 static void TestTimeStatic()
174 puts("\n*** wxDateTime static methods test ***");
176 // some info about the current date
177 int year
= wxDateTime::GetCurrentYear();
178 printf("Current year %d is %sa leap one and has %d days.\n",
180 wxDateTime::IsLeapYear(year
) ? "" : "not ",
181 wxDateTime::GetNumberOfDays(year
));
183 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
184 printf("Current month is '%s' ('%s') and it has %d days\n",
185 wxDateTime::GetMonthName(month
, TRUE
).c_str(),
186 wxDateTime::GetMonthName(month
).c_str(),
187 wxDateTime::GetNumberOfDays(month
));
190 static const size_t nYears
= 5;
191 static const size_t years
[2][nYears
] =
193 // first line: the years to test
194 { 1990, 1976, 2000, 2030, 1984, },
196 // second line: TRUE if leap, FALSE otherwise
197 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
200 for ( size_t n
= 0; n
< nYears
; n
++ )
202 int year
= years
[0][n
];
203 bool should
= years
[1][n
] != 0;
205 printf("Year %d is %sa leap year (should be: %s)\n",
207 wxDateTime::IsLeapYear(year
) ? "" : "not ",
208 should
? "yes" : "no");
210 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
214 // test constructing wxDateTime objects
215 static void TestTimeSet()
217 puts("\n*** wxDateTime construction test ***");
219 printf("Current time:\t%s\n", wxDateTime::Now().Format().c_str());
220 printf("Unix epoch:\t%s\n", wxDateTime((time_t)0).Format().c_str());
221 printf("Today noon:\t%s\n", wxDateTime(12, 0).Format().c_str());
222 printf("May 29, 1976:\t%s\n", wxDateTime(29, wxDateTime::May
, 1976).Format().c_str());
225 // test time zones stuff
226 static void TestTimeZones()
228 puts("\n*** wxDateTime timezone test ***");
230 wxDateTime now
= wxDateTime::Now();
232 printf("Current GMT time:\t%s\n", now
.ToGMT().Format().c_str());
233 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
234 printf("Current time in Paris:\t%s\n", now
.ToTimezone(wxDateTime::CET
).Format().c_str());
235 printf(" Moscow:\t%s\n", now
.ToTimezone(wxDateTime::MSK
).Format().c_str());
236 printf(" New York:\t%s\n", now
.ToTimezone(wxDateTime::EST
).Format().c_str());
239 // test some minimal support for the dates outside the standard range
240 static void TestTimeRange()
242 puts("\n*** wxDateTime out-of-standard-range dates test ***");
244 printf("Unix epoch:\t%s\n",
245 wxDateTime(2440587.5).Format().c_str());
246 printf("Feb 29, 0: \t%s\n",
247 wxDateTime(29, wxDateTime::Feb
, 0).Format().c_str());
248 printf("JDN 0: \t%s\n",
249 wxDateTime(0.0).Format().c_str());
250 printf("Jan 1, 1AD:\t%s\n",
251 wxDateTime(1, wxDateTime::Jan
, 1).Format().c_str());
252 printf("May 29, 2099:\t%s\n",
253 wxDateTime(29, wxDateTime::May
, 2099).Format().c_str());
256 // test conversions to JDN &c
257 static void TestTimeJDN()
259 puts("\n*** wxDateTime to JDN test ***");
263 wxDateTime::wxDateTime_t day
;
264 wxDateTime::Month month
;
269 static const Date testDates
[] =
271 { 21, wxDateTime::Jan
, 2222, 2532648.5 },
272 { 29, wxDateTime::May
, 1976, 2442927.5 },
273 { 1, wxDateTime::Jan
, 1970, 2440587.5 },
274 { 1, wxDateTime::Jan
, 1900, 2415020.5 },
275 { 15, wxDateTime::Oct
, 1582, 2299160.5 },
276 { 4, wxDateTime::Oct
, 1582, 2299149.5 },
277 { 1, wxDateTime::Mar
, 1, 1721484.5 },
278 { 1, wxDateTime::Jan
, 1, 1721425.5 },
279 { 31, wxDateTime::Dec
, 0, 1721424.5 },
280 { 1, wxDateTime::Jan
, 0, 1721059.5 },
281 { 12, wxDateTime::Aug
, -1234, 1270573.5 },
282 { 12, wxDateTime::Aug
, -4000, 260313.5 },
283 { 24, wxDateTime::Nov
, -4713, -0.5 },
286 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
288 const Date
& d
= testDates
[n
];
289 wxDateTime
dt(d
.day
, d
.month
, d
.year
);
290 double jdn
= dt
.GetJulianDayNumber();
292 printf("JDN of %s %02d, %4d%s is:\t%f",
293 wxDateTime::GetMonthName(d
.month
).c_str(),
295 wxDateTime::ConvertYearToBC(d
.year
),
296 d
.year
> 0 ? "AD" : "BC",
304 printf(" (ERROR: should be %f, delta = %f)\n",
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
318 #include <wx/thread.h>
320 static size_t gs_counter
= (size_t)-1;
321 static wxCriticalSection gs_critsect
;
322 static wxCondition gs_cond
;
324 class MyJoinableThread
: public wxThread
327 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
328 { m_n
= n
; Create(); }
330 // thread execution starts here
331 virtual ExitCode
Entry();
337 wxThread::ExitCode
MyJoinableThread::Entry()
339 unsigned long res
= 1;
340 for ( size_t n
= 1; n
< m_n
; n
++ )
344 // it's a loooong calculation :-)
348 return (ExitCode
)res
;
351 class MyDetachedThread
: public wxThread
354 MyDetachedThread(size_t n
, char ch
)
363 // thread execution starts here
364 virtual ExitCode
Entry();
367 virtual void OnExit();
370 size_t m_n
; // number of characters to write
371 char m_ch
; // character to write
373 bool m_cancelled
; // FALSE if we exit normally
376 wxThread::ExitCode
MyDetachedThread::Entry()
379 wxCriticalSectionLocker
lock(gs_critsect
);
380 if ( gs_counter
== (size_t)-1 )
386 for ( size_t n
= 0; n
< m_n
; n
++ )
398 wxThread::Sleep(100);
404 void MyDetachedThread::OnExit()
406 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
408 wxCriticalSectionLocker
lock(gs_critsect
);
409 if ( !--gs_counter
&& !m_cancelled
)
413 void TestDetachedThreads()
415 puts("\n*** Testing detached threads ***");
417 static const size_t nThreads
= 3;
418 MyDetachedThread
*threads
[nThreads
];
420 for ( n
= 0; n
< nThreads
; n
++ )
422 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
425 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
426 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
428 for ( n
= 0; n
< nThreads
; n
++ )
433 // wait until all threads terminate
439 void TestJoinableThreads()
441 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
443 // calc 10! in the background
444 MyJoinableThread
thread(10);
447 printf("\nThread terminated with exit code %lu.\n",
448 (unsigned long)thread
.Wait());
451 void TestThreadSuspend()
453 puts("\n*** Testing thread suspend/resume functions ***");
455 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
459 // this is for this demo only, in a real life program we'd use another
460 // condition variable which would be signaled from wxThread::Entry() to
461 // tell us that the thread really started running - but here just wait a
462 // bit and hope that it will be enough (the problem is, of course, that
463 // the thread might still not run when we call Pause() which will result
465 wxThread::Sleep(300);
467 for ( size_t n
= 0; n
< 3; n
++ )
471 puts("\nThread suspended");
474 // don't sleep but resume immediately the first time
475 wxThread::Sleep(300);
477 puts("Going to resume the thread");
482 puts("Waiting until it terminates now");
484 // wait until the thread terminates
490 void TestThreadDelete()
492 // As above, using Sleep() is only for testing here - we must use some
493 // synchronisation object instead to ensure that the thread is still
494 // running when we delete it - deleting a detached thread which already
495 // terminated will lead to a crash!
497 puts("\n*** Testing thread delete function ***");
499 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
503 puts("\nDeleted a thread which didn't start to run yet.");
505 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
509 wxThread::Sleep(300);
513 puts("\nDeleted a running thread.");
515 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
519 wxThread::Sleep(300);
525 puts("\nDeleted a sleeping thread.");
527 MyJoinableThread
thread3(20);
532 puts("\nDeleted a joinable thread.");
534 MyJoinableThread
thread4(2);
537 wxThread::Sleep(300);
541 puts("\nDeleted a joinable thread which already terminated.");
546 #endif // TEST_THREADS
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
554 void PrintArray(const char* name
, const wxArrayString
& array
)
556 printf("Dump of the array '%s'\n", name
);
558 size_t nCount
= array
.GetCount();
559 for ( size_t n
= 0; n
< nCount
; n
++ )
561 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
565 #endif // TEST_ARRAYS
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
573 #include "wx/timer.h"
585 for (int i
= 0; i
< 1000000; ++i
)
589 c
= "! How'ya doin'?";
592 c
= "Hello world! What's up?";
597 printf ("TestString elapsed time: %ld\n", sw
.Time());
608 for (int i
= 0; i
< 1000000; ++i
)
611 strcpy (b
, " world");
612 strcpy (c
, "! How'ya doin'?");
615 strcpy (c
, "Hello world! What's up?");
616 if (strcmp (c
, a
) == 0)
620 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
623 #endif // TEST_STRINGS
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
629 int main(int argc
, char **argv
)
631 if ( !wxInitialize() )
633 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
639 #endif // TEST_STRINGS
650 puts("*** Initially:");
652 PrintArray("a1", a1
);
654 wxArrayString
a2(a1
);
655 PrintArray("a2", a2
);
657 wxSortedArrayString
a3(a1
);
658 PrintArray("a3", a3
);
660 puts("*** After deleting a string from a1");
663 PrintArray("a1", a1
);
664 PrintArray("a2", a2
);
665 PrintArray("a3", a3
);
667 puts("*** After reassigning a1 to a2 and a3");
669 PrintArray("a2", a2
);
670 PrintArray("a3", a3
);
671 #endif // TEST_ARRAYS
675 for ( size_t n
= 0; n
< 8000; n
++ )
677 s
<< (char)('A' + (n
% 26));
681 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
683 // this one shouldn't be truncated
686 // but this one will because log functions use fixed size buffer
687 // (note that it doesn't need '\n' at the end neither - will be added
689 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
693 int nCPUs
= wxThread::GetCPUCount();
694 printf("This system has %d CPUs\n", nCPUs
);
696 wxThread::SetConcurrency(nCPUs
);
698 if ( argc
> 1 && argv
[1][0] == 't' )
699 wxLog::AddTraceMask("thread");
702 TestDetachedThreads();
704 TestJoinableThreads();
710 #endif // TEST_THREADS
717 #endif // TEST_LONGLONG