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",
86 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
98 #include <wx/longlong.h>
101 static void TestSpeed()
103 static const long max
= 100000000;
110 for ( n
= 0; n
< max
; n
++ )
115 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
122 for ( n
= 0; n
< max
; n
++ )
127 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
134 for ( n
= 0; n
< max
; n
++ )
139 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
143 static void TestDivision()
145 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
147 // seed pseudo random generator
148 //srand((unsigned)time(NULL));
151 for ( size_t n
= 0; n
< 10000; n
++ )
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());
157 wxASSERT( (ll
* 1000l)/1000l == ll
);
162 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
167 #endif // TEST_LONGLONG
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
175 #include <wx/datetime.h>
177 // this test miscellaneous static wxDateTime functions
178 static void TestTimeStatic()
180 puts("\n*** wxDateTime static methods test ***");
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",
186 wxDateTime::IsLeapYear(year
) ? "" : "not ",
187 wxDateTime::GetNumberOfDays(year
));
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
));
196 static const size_t nYears
= 5;
197 static const size_t years
[2][nYears
] =
199 // first line: the years to test
200 { 1990, 1976, 2000, 2030, 1984, },
202 // second line: TRUE if leap, FALSE otherwise
203 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
206 for ( size_t n
= 0; n
< nYears
; n
++ )
208 int year
= years
[0][n
];
209 bool should
= years
[1][n
] != 0;
211 printf("Year %d is %sa leap year (should be: %s)\n",
213 wxDateTime::IsLeapYear(year
) ? "" : "not ",
214 should
? "yes" : "no");
216 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
220 // test constructing wxDateTime objects
221 static void TestTimeSet()
223 puts("\n*** wxDateTime construction test ***");
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());
231 // test time zones stuff
232 static void TestTimeZones()
234 puts("\n*** wxDateTime timezone test ***");
236 wxDateTime now
= wxDateTime::Now();
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());
245 // test some minimal support for the dates outside the standard range
246 static void TestTimeRange()
248 puts("\n*** wxDateTime out-of-standard-range dates test ***");
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());
262 // test conversions to JDN &c
263 static void TestTimeJDN()
265 puts("\n*** wxDateTime to JDN test ***");
269 wxDateTime::wxDateTime_t day
;
270 wxDateTime::Month month
;
275 static const Date testDates
[] =
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 },
292 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
294 const Date
& d
= testDates
[n
];
295 wxDateTime
dt(d
.day
, d
.month
, d
.year
);
296 double jdn
= dt
.GetJulianDayNumber();
298 printf("JDN of %s %02d, %4d%s is:\t%f",
299 wxDateTime::GetMonthName(d
.month
).c_str(),
301 wxDateTime::ConvertYearToBC(d
.year
),
302 d
.year
> 0 ? "AD" : "BC",
310 printf(" (ERROR: should be %f, delta = %f)\n",
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
324 #include <wx/thread.h>
326 static size_t gs_counter
= (size_t)-1;
327 static wxCriticalSection gs_critsect
;
328 static wxCondition gs_cond
;
330 class MyJoinableThread
: public wxThread
333 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
334 { m_n
= n
; Create(); }
336 // thread execution starts here
337 virtual ExitCode
Entry();
343 wxThread::ExitCode
MyJoinableThread::Entry()
345 unsigned long res
= 1;
346 for ( size_t n
= 1; n
< m_n
; n
++ )
350 // it's a loooong calculation :-)
354 return (ExitCode
)res
;
357 class MyDetachedThread
: public wxThread
360 MyDetachedThread(size_t n
, char ch
)
369 // thread execution starts here
370 virtual ExitCode
Entry();
373 virtual void OnExit();
376 size_t m_n
; // number of characters to write
377 char m_ch
; // character to write
379 bool m_cancelled
; // FALSE if we exit normally
382 wxThread::ExitCode
MyDetachedThread::Entry()
385 wxCriticalSectionLocker
lock(gs_critsect
);
386 if ( gs_counter
== (size_t)-1 )
392 for ( size_t n
= 0; n
< m_n
; n
++ )
404 wxThread::Sleep(100);
410 void MyDetachedThread::OnExit()
412 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
414 wxCriticalSectionLocker
lock(gs_critsect
);
415 if ( !--gs_counter
&& !m_cancelled
)
419 void TestDetachedThreads()
421 puts("\n*** Testing detached threads ***");
423 static const size_t nThreads
= 3;
424 MyDetachedThread
*threads
[nThreads
];
426 for ( n
= 0; n
< nThreads
; n
++ )
428 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
431 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
432 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
434 for ( n
= 0; n
< nThreads
; n
++ )
439 // wait until all threads terminate
445 void TestJoinableThreads()
447 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
449 // calc 10! in the background
450 MyJoinableThread
thread(10);
453 printf("\nThread terminated with exit code %lu.\n",
454 (unsigned long)thread
.Wait());
457 void TestThreadSuspend()
459 puts("\n*** Testing thread suspend/resume functions ***");
461 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
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
471 wxThread::Sleep(300);
473 for ( size_t n
= 0; n
< 3; n
++ )
477 puts("\nThread suspended");
480 // don't sleep but resume immediately the first time
481 wxThread::Sleep(300);
483 puts("Going to resume the thread");
488 puts("Waiting until it terminates now");
490 // wait until the thread terminates
496 void TestThreadDelete()
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!
503 puts("\n*** Testing thread delete function ***");
505 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
509 puts("\nDeleted a thread which didn't start to run yet.");
511 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
515 wxThread::Sleep(300);
519 puts("\nDeleted a running thread.");
521 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
525 wxThread::Sleep(300);
531 puts("\nDeleted a sleeping thread.");
533 MyJoinableThread
thread3(20);
538 puts("\nDeleted a joinable thread.");
540 MyJoinableThread
thread4(2);
543 wxThread::Sleep(300);
547 puts("\nDeleted a joinable thread which already terminated.");
552 #endif // TEST_THREADS
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
560 void PrintArray(const char* name
, const wxArrayString
& array
)
562 printf("Dump of the array '%s'\n", name
);
564 size_t nCount
= array
.GetCount();
565 for ( size_t n
= 0; n
< nCount
; n
++ )
567 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
571 #endif // TEST_ARRAYS
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
579 #include "wx/timer.h"
591 for (int i
= 0; i
< 1000000; ++i
)
595 c
= "! How'ya doin'?";
598 c
= "Hello world! What's up?";
603 printf ("TestString elapsed time: %ld\n", sw
.Time());
614 for (int i
= 0; i
< 1000000; ++i
)
617 strcpy (b
, " world");
618 strcpy (c
, "! How'ya doin'?");
621 strcpy (c
, "Hello world! What's up?");
622 if (strcmp (c
, a
) == 0)
626 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
629 #endif // TEST_STRINGS
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 int main(int argc
, char **argv
)
637 if ( !wxInitialize() )
639 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
645 #endif // TEST_STRINGS
656 puts("*** Initially:");
658 PrintArray("a1", a1
);
660 wxArrayString
a2(a1
);
661 PrintArray("a2", a2
);
663 wxSortedArrayString
a3(a1
);
664 PrintArray("a3", a3
);
666 puts("*** After deleting a string from a1");
669 PrintArray("a1", a1
);
670 PrintArray("a2", a2
);
671 PrintArray("a3", a3
);
673 puts("*** After reassigning a1 to a2 and a3");
675 PrintArray("a2", a2
);
676 PrintArray("a3", a3
);
677 #endif // TEST_ARRAYS
681 for ( size_t n
= 0; n
< 8000; n
++ )
683 s
<< (char)('A' + (n
% 26));
687 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
689 // this one shouldn't be truncated
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
695 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
699 int nCPUs
= wxThread::GetCPUCount();
700 printf("This system has %d CPUs\n", nCPUs
);
702 wxThread::SetConcurrency(nCPUs
);
704 if ( argc
> 1 && argv
[1][0] == 't' )
705 wxLog::AddTraceMask("thread");
708 TestDetachedThreads();
710 TestJoinableThreads();
716 #endif // TEST_THREADS
723 #endif // TEST_LONGLONG