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 nYears
= 5;
148 static const int 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());
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
190 #include <wx/thread.h>
192 static size_t gs_counter
= (size_t)-1;
193 static wxCriticalSection gs_critsect
;
194 static wxCondition gs_cond
;
196 class MyJoinableThread
: public wxThread
199 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
200 { m_n
= n
; Create(); }
202 // thread execution starts here
203 virtual ExitCode
Entry();
209 wxThread::ExitCode
MyJoinableThread::Entry()
211 unsigned long res
= 1;
212 for ( size_t n
= 1; n
< m_n
; n
++ )
216 // it's a loooong calculation :-)
220 return (ExitCode
)res
;
223 class MyDetachedThread
: public wxThread
226 MyDetachedThread(size_t n
, char ch
) { m_n
= n
; m_ch
= ch
; Create(); }
228 // thread execution starts here
229 virtual ExitCode
Entry();
232 virtual void OnExit();
235 size_t m_n
; // number of characters to write
236 char m_ch
; // character to write
239 wxThread::ExitCode
MyDetachedThread::Entry()
242 wxCriticalSectionLocker
lock(gs_critsect
);
243 if ( gs_counter
== (size_t)-1 )
249 for ( size_t n
= 0; n
< m_n
; n
++ )
257 wxThread::Sleep(100);
263 void MyDetachedThread::OnExit()
265 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
267 wxCriticalSectionLocker
lock(gs_critsect
);
272 void TestDetachedThreads()
274 puts("\n*** Testing detached threads ***");
276 static const size_t nThreads
= 3;
277 MyDetachedThread
*threads
[nThreads
];
279 for ( n
= 0; n
< nThreads
; n
++ )
281 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
284 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
285 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
287 for ( n
= 0; n
< nThreads
; n
++ )
292 // wait until all threads terminate
298 void TestJoinableThreads()
300 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
302 // calc 10! in the background
303 MyJoinableThread
thread(10);
306 printf("\nThread terminated with exit code %lu.\n",
307 (unsigned long)thread
.Wait());
310 void TestThreadSuspend()
312 puts("\n*** Testing thread suspend/resume functions ***");
314 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
318 // this is for this demo only, in a real life program we'd use another
319 // condition variable which would be signaled from wxThread::Entry() to
320 // tell us that the thread really started running - but here just wait a
321 // bit and hope that it will be enough (the problem is, of course, that
322 // the thread might still not run when we call Pause() which will result
324 wxThread::Sleep(300);
326 for ( size_t n
= 0; n
< 3; n
++ )
330 puts("\nThread suspended");
333 // don't sleep but resume immediately the first time
334 wxThread::Sleep(300);
336 puts("Going to resume the thread");
341 // wait until the thread terminates
347 void TestThreadDelete()
349 // As above, using Sleep() is only for testing here - we must use some
350 // synchronisation object instead to ensure that the thread is still
351 // running when we delete it - deleting a detached thread which already
352 // terminated will lead to a crash!
354 puts("\n*** Testing thread delete function ***");
356 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
360 wxThread::Sleep(300);
364 puts("\nDeleted a running thread.");
366 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
370 wxThread::Sleep(300);
376 puts("\nDeleted a sleeping thread.");
378 MyJoinableThread
*thread3
= new MyJoinableThread(20);
383 puts("\nDeleted a joinable thread.");
385 MyJoinableThread
*thread4
= new MyJoinableThread(2);
388 wxThread::Sleep(300);
392 puts("\nDeleted a joinable thread which already terminated.");
397 #endif // TEST_THREADS
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
405 void PrintArray(const char* name
, const wxArrayString
& array
)
407 printf("Dump of the array '%s'\n", name
);
409 size_t nCount
= array
.GetCount();
410 for ( size_t n
= 0; n
< nCount
; n
++ )
412 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
416 #endif // TEST_ARRAYS
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
424 #include "wx/timer.h"
436 for (int i
= 0; i
< 1000000; ++i
)
440 c
= "! How'ya doin'?";
443 c
= "Hello world! What's up?";
448 printf ("TestString elapsed time: %ld\n", sw
.Time());
459 for (int i
= 0; i
< 1000000; ++i
)
462 strcpy (b
, " world");
463 strcpy (c
, "! How'ya doin'?");
466 strcpy (c
, "Hello world! What's up?");
467 if (strcmp (c
, a
) == 0)
471 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
474 #endif // TEST_STRINGS
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 int main(int argc
, char **argv
)
482 if ( !wxInitialize() )
484 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
490 #endif // TEST_STRINGS
501 puts("*** Initially:");
503 PrintArray("a1", a1
);
505 wxArrayString
a2(a1
);
506 PrintArray("a2", a2
);
508 wxSortedArrayString
a3(a1
);
509 PrintArray("a3", a3
);
511 puts("*** After deleting a string from a1");
514 PrintArray("a1", a1
);
515 PrintArray("a2", a2
);
516 PrintArray("a3", a3
);
518 puts("*** After reassigning a1 to a2 and a3");
520 PrintArray("a2", a2
);
521 PrintArray("a3", a3
);
522 #endif // TEST_ARRAYS
526 for ( size_t n
= 0; n
< 8000; n
++ )
528 s
<< (char)('A' + (n
% 26));
532 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
534 // this one shouldn't be truncated
537 // but this one will because log functions use fixed size buffer
538 // (note that it doesn't need '\n' at the end neither - will be added
540 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
544 if ( argc
> 1 && argv
[1][0] == 't' )
545 wxLog::AddTraceMask("thread");
548 TestDetachedThreads();
550 TestJoinableThreads();
556 #endif // TEST_THREADS
563 #endif // TEST_LONGLONG