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 // ----------------------------------------------------------------------------
34 //#define TEST_STRINGS
35 //#define TEST_THREADS
37 //#define TEST_LONGLONG
39 // ============================================================================
41 // ============================================================================
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
49 #include <wx/longlong.h>
52 static void TestSpeed()
54 static const long max
= 100000000;
61 for ( n
= 0; n
< max
; n
++ )
66 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
73 for ( n
= 0; n
< max
; n
++ )
78 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
85 for ( n
= 0; n
< max
; n
++ )
90 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
94 static void TestDivision()
96 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
98 // seed pseudo random generator
99 //srand((unsigned)time(NULL));
102 for ( size_t n
= 0; n
< 10000; n
++ )
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());
108 wxASSERT( (ll
* 1000l)/1000l == ll
);
113 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
118 #endif // TEST_LONGLONG
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
126 #include <wx/datetime.h>
128 // this test miscellaneous static wxDateTime functions
129 static void TestTimeStatic()
131 puts("\n*** wxDateTime static methods test ***");
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",
137 wxDateTime::IsLeapYear(year
) ? "" : "not ",
138 wxDateTime::GetNumberOfDays(year
));
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
));
147 static const size_t nYears
= 5;
148 static const size_t years
[2][nYears
] =
150 // first line: the years to test
151 { 1990, 1976, 2000, 2030, 1984, },
153 // second line: TRUE if leap, FALSE otherwise
154 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
157 for ( size_t n
= 0; n
< nYears
; n
++ )
159 int year
= years
[0][n
];
160 bool should
= years
[1][n
] != 0;
162 printf("Year %d is %sa leap year (should be: %s)\n",
164 wxDateTime::IsLeapYear(year
) ? "" : "not ",
165 should
? "yes" : "no");
167 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
171 // test constructing wxDateTime objects
172 static void TestTimeSet()
174 puts("\n*** wxDateTime construction test ***");
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());
182 // test time zones stuff
183 static void TestTimeZones()
185 puts("\n*** wxDateTime timezone test ***");
187 wxDateTime now
= wxDateTime::Now();
189 printf("Current GMT time:\t%s\n", now
.ToGMT().Format().c_str());
190 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).MakeGMT().Format().c_str());
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());
196 // test some minimal support for the dates outside the standard range
197 static void TestTimeRange()
199 puts("\n*** wxDateTime out-of-standard-range dates test ***");
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());
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());
213 // test conversions to JDN &c
214 static void TestTimeJDN()
216 puts("\n*** wxDateTime to JDN test ***");
220 wxDateTime::wxDateTime_t day
;
221 wxDateTime::Month month
;
226 static const Date testDates
[] =
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 },
243 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
245 const Date
& d
= testDates
[n
];
246 wxDateTime
dt(d
.day
, d
.month
, d
.year
);
247 double jdn
= dt
.GetJulianDayNumber();
249 printf("JDN of %s %02d, %4d%s is:\t%f",
250 wxDateTime::GetMonthName(d
.month
).c_str(),
252 wxDateTime::ConvertYearToBC(d
.year
),
253 d
.year
> 0 ? "AD" : "BC",
261 printf(" (ERROR: should be %f, delta = %f)\n",
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
275 #include <wx/thread.h>
277 static size_t gs_counter
= (size_t)-1;
278 static wxCriticalSection gs_critsect
;
279 static wxCondition gs_cond
;
281 class MyJoinableThread
: public wxThread
284 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
285 { m_n
= n
; Create(); }
287 // thread execution starts here
288 virtual ExitCode
Entry();
294 wxThread::ExitCode
MyJoinableThread::Entry()
296 unsigned long res
= 1;
297 for ( size_t n
= 1; n
< m_n
; n
++ )
301 // it's a loooong calculation :-)
305 return (ExitCode
)res
;
308 class MyDetachedThread
: public wxThread
311 MyDetachedThread(size_t n
, char ch
)
320 // thread execution starts here
321 virtual ExitCode
Entry();
324 virtual void OnExit();
327 size_t m_n
; // number of characters to write
328 char m_ch
; // character to write
330 bool m_cancelled
; // FALSE if we exit normally
333 wxThread::ExitCode
MyDetachedThread::Entry()
336 wxCriticalSectionLocker
lock(gs_critsect
);
337 if ( gs_counter
== (size_t)-1 )
343 for ( size_t n
= 0; n
< m_n
; n
++ )
355 wxThread::Sleep(100);
361 void MyDetachedThread::OnExit()
363 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
365 wxCriticalSectionLocker
lock(gs_critsect
);
366 if ( !--gs_counter
&& !m_cancelled
)
370 void TestDetachedThreads()
372 puts("\n*** Testing detached threads ***");
374 static const size_t nThreads
= 3;
375 MyDetachedThread
*threads
[nThreads
];
377 for ( n
= 0; n
< nThreads
; n
++ )
379 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
382 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
383 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
385 for ( n
= 0; n
< nThreads
; n
++ )
390 // wait until all threads terminate
396 void TestJoinableThreads()
398 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
400 // calc 10! in the background
401 MyJoinableThread
thread(10);
404 printf("\nThread terminated with exit code %lu.\n",
405 (unsigned long)thread
.Wait());
408 void TestThreadSuspend()
410 puts("\n*** Testing thread suspend/resume functions ***");
412 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
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
422 wxThread::Sleep(300);
424 for ( size_t n
= 0; n
< 3; n
++ )
428 puts("\nThread suspended");
431 // don't sleep but resume immediately the first time
432 wxThread::Sleep(300);
434 puts("Going to resume the thread");
439 puts("Waiting until it terminates now");
441 // wait until the thread terminates
447 void TestThreadDelete()
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!
454 puts("\n*** Testing thread delete function ***");
456 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
460 puts("\nDeleted a thread which didn't start to run yet.");
462 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
466 wxThread::Sleep(300);
470 puts("\nDeleted a running thread.");
472 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
476 wxThread::Sleep(300);
482 puts("\nDeleted a sleeping thread.");
484 MyJoinableThread
thread3(20);
489 puts("\nDeleted a joinable thread.");
491 MyJoinableThread
thread4(2);
494 wxThread::Sleep(300);
498 puts("\nDeleted a joinable thread which already terminated.");
503 #endif // TEST_THREADS
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
511 void PrintArray(const char* name
, const wxArrayString
& array
)
513 printf("Dump of the array '%s'\n", name
);
515 size_t nCount
= array
.GetCount();
516 for ( size_t n
= 0; n
< nCount
; n
++ )
518 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
522 #endif // TEST_ARRAYS
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
530 #include "wx/timer.h"
542 for (int i
= 0; i
< 1000000; ++i
)
546 c
= "! How'ya doin'?";
549 c
= "Hello world! What's up?";
554 printf ("TestString elapsed time: %ld\n", sw
.Time());
565 for (int i
= 0; i
< 1000000; ++i
)
568 strcpy (b
, " world");
569 strcpy (c
, "! How'ya doin'?");
572 strcpy (c
, "Hello world! What's up?");
573 if (strcmp (c
, a
) == 0)
577 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
580 #endif // TEST_STRINGS
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 int main(int argc
, char **argv
)
588 if ( !wxInitialize() )
590 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
596 #endif // TEST_STRINGS
607 puts("*** Initially:");
609 PrintArray("a1", a1
);
611 wxArrayString
a2(a1
);
612 PrintArray("a2", a2
);
614 wxSortedArrayString
a3(a1
);
615 PrintArray("a3", a3
);
617 puts("*** After deleting a string from a1");
620 PrintArray("a1", a1
);
621 PrintArray("a2", a2
);
622 PrintArray("a3", a3
);
624 puts("*** After reassigning a1 to a2 and a3");
626 PrintArray("a2", a2
);
627 PrintArray("a3", a3
);
628 #endif // TEST_ARRAYS
632 for ( size_t n
= 0; n
< 8000; n
++ )
634 s
<< (char)('A' + (n
% 26));
638 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
640 // this one shouldn't be truncated
643 // but this one will because log functions use fixed size buffer
644 // (note that it doesn't need '\n' at the end neither - will be added
646 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
650 if ( argc
> 1 && argv
[1][0] == 't' )
651 wxLog::AddTraceMask("thread");
654 TestDetachedThreads();
656 TestJoinableThreads();
662 #endif // TEST_THREADS
669 #endif // TEST_LONGLONG