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
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());
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
204 #include <wx/thread.h>
206 static size_t gs_counter
= (size_t)-1;
207 static wxCriticalSection gs_critsect
;
208 static wxCondition gs_cond
;
210 class MyJoinableThread
: public wxThread
213 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
214 { m_n
= n
; Create(); }
216 // thread execution starts here
217 virtual ExitCode
Entry();
223 wxThread::ExitCode
MyJoinableThread::Entry()
225 unsigned long res
= 1;
226 for ( size_t n
= 1; n
< m_n
; n
++ )
230 // it's a loooong calculation :-)
234 return (ExitCode
)res
;
237 class MyDetachedThread
: public wxThread
240 MyDetachedThread(size_t n
, char ch
)
249 // thread execution starts here
250 virtual ExitCode
Entry();
253 virtual void OnExit();
256 size_t m_n
; // number of characters to write
257 char m_ch
; // character to write
259 bool m_cancelled
; // FALSE if we exit normally
262 wxThread::ExitCode
MyDetachedThread::Entry()
265 wxCriticalSectionLocker
lock(gs_critsect
);
266 if ( gs_counter
== (size_t)-1 )
272 for ( size_t n
= 0; n
< m_n
; n
++ )
284 wxThread::Sleep(100);
290 void MyDetachedThread::OnExit()
292 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
294 wxCriticalSectionLocker
lock(gs_critsect
);
295 if ( !--gs_counter
&& !m_cancelled
)
299 void TestDetachedThreads()
301 puts("\n*** Testing detached threads ***");
303 static const size_t nThreads
= 3;
304 MyDetachedThread
*threads
[nThreads
];
306 for ( n
= 0; n
< nThreads
; n
++ )
308 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
311 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
312 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
314 for ( n
= 0; n
< nThreads
; n
++ )
319 // wait until all threads terminate
325 void TestJoinableThreads()
327 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
329 // calc 10! in the background
330 MyJoinableThread
thread(10);
333 printf("\nThread terminated with exit code %lu.\n",
334 (unsigned long)thread
.Wait());
337 void TestThreadSuspend()
339 puts("\n*** Testing thread suspend/resume functions ***");
341 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
345 // this is for this demo only, in a real life program we'd use another
346 // condition variable which would be signaled from wxThread::Entry() to
347 // tell us that the thread really started running - but here just wait a
348 // bit and hope that it will be enough (the problem is, of course, that
349 // the thread might still not run when we call Pause() which will result
351 wxThread::Sleep(300);
353 for ( size_t n
= 0; n
< 3; n
++ )
357 puts("\nThread suspended");
360 // don't sleep but resume immediately the first time
361 wxThread::Sleep(300);
363 puts("Going to resume the thread");
368 // wait until the thread terminates
374 void TestThreadDelete()
376 // As above, using Sleep() is only for testing here - we must use some
377 // synchronisation object instead to ensure that the thread is still
378 // running when we delete it - deleting a detached thread which already
379 // terminated will lead to a crash!
381 puts("\n*** Testing thread delete function ***");
383 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
387 wxThread::Sleep(300);
391 puts("\nDeleted a running thread.");
393 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
397 wxThread::Sleep(300);
403 puts("\nDeleted a sleeping thread.");
405 MyJoinableThread
*thread3
= new MyJoinableThread(20);
410 puts("\nDeleted a joinable thread.");
412 MyJoinableThread
*thread4
= new MyJoinableThread(2);
415 wxThread::Sleep(300);
419 puts("\nDeleted a joinable thread which already terminated.");
424 #endif // TEST_THREADS
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
432 void PrintArray(const char* name
, const wxArrayString
& array
)
434 printf("Dump of the array '%s'\n", name
);
436 size_t nCount
= array
.GetCount();
437 for ( size_t n
= 0; n
< nCount
; n
++ )
439 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
443 #endif // TEST_ARRAYS
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
451 #include "wx/timer.h"
463 for (int i
= 0; i
< 1000000; ++i
)
467 c
= "! How'ya doin'?";
470 c
= "Hello world! What's up?";
475 printf ("TestString elapsed time: %ld\n", sw
.Time());
486 for (int i
= 0; i
< 1000000; ++i
)
489 strcpy (b
, " world");
490 strcpy (c
, "! How'ya doin'?");
493 strcpy (c
, "Hello world! What's up?");
494 if (strcmp (c
, a
) == 0)
498 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
501 #endif // TEST_STRINGS
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
507 int main(int argc
, char **argv
)
509 if ( !wxInitialize() )
511 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
517 #endif // TEST_STRINGS
528 puts("*** Initially:");
530 PrintArray("a1", a1
);
532 wxArrayString
a2(a1
);
533 PrintArray("a2", a2
);
535 wxSortedArrayString
a3(a1
);
536 PrintArray("a3", a3
);
538 puts("*** After deleting a string from a1");
541 PrintArray("a1", a1
);
542 PrintArray("a2", a2
);
543 PrintArray("a3", a3
);
545 puts("*** After reassigning a1 to a2 and a3");
547 PrintArray("a2", a2
);
548 PrintArray("a3", a3
);
549 #endif // TEST_ARRAYS
553 for ( size_t n
= 0; n
< 8000; n
++ )
555 s
<< (char)('A' + (n
% 26));
559 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
561 // this one shouldn't be truncated
564 // but this one will because log functions use fixed size buffer
565 // (note that it doesn't need '\n' at the end neither - will be added
567 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
571 if ( argc
> 1 && argv
[1][0] == 't' )
572 wxLog::AddTraceMask("thread");
575 TestDetachedThreads();
577 TestJoinableThreads();
583 #endif // TEST_THREADS
590 #endif // TEST_LONGLONG