]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
wxDateTime starting to work, more tests for it and for threads in console sample
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31
32 //#define TEST_ARRAYS
33 //#define TEST_LOG
34 //#define TEST_STRINGS
35 //#define TEST_THREADS
36 #define TEST_TIME
37 //#define TEST_LONGLONG
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // long long
45 // ----------------------------------------------------------------------------
46
47 #ifdef TEST_LONGLONG
48
49 #include <wx/longlong.h>
50 #include <wx/timer.h>
51
52 static void TestSpeed()
53 {
54 static const long max = 100000000;
55 long n;
56
57 {
58 wxStopWatch sw;
59
60 long l = 0;
61 for ( n = 0; n < max; n++ )
62 {
63 l += n;
64 }
65
66 printf("Summing longs took %ld milliseconds.\n", sw.Time());
67 }
68
69 {
70 wxStopWatch sw;
71
72 __int64 l = 0;
73 for ( n = 0; n < max; n++ )
74 {
75 l += n;
76 }
77
78 printf("Summing __int64s took %ld milliseconds.\n", sw.Time());
79 }
80
81 {
82 wxStopWatch sw;
83
84 wxLongLong l;
85 for ( n = 0; n < max; n++ )
86 {
87 l += n;
88 }
89
90 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
91 }
92 }
93
94 static void TestDivision()
95 {
96 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
97
98 // seed pseudo random generator
99 //srand((unsigned)time(NULL));
100
101 size_t nTested = 0;
102 for ( size_t n = 0; n < 10000; n++ )
103 {
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());
107
108 wxASSERT( (ll * 1000l)/1000l == ll );
109
110 nTested++;
111 }
112
113 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested);
114
115 #undef MAKE_LL
116 }
117
118 #endif // TEST_LONGLONG
119
120 // ----------------------------------------------------------------------------
121 // date time
122 // ----------------------------------------------------------------------------
123
124 #ifdef TEST_TIME
125
126 #include <wx/datetime.h>
127
128 // this test miscellaneous static wxDateTime functions
129 static void TestTimeStatic()
130 {
131 puts("\n*** wxDateTime static methods test ***");
132
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",
136 year,
137 wxDateTime::IsLeapYear(year) ? "" : "not ",
138 wxDateTime::GetNumberOfDays(year));
139
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));
145
146 // leap year logic
147 static const nYears = 5;
148 static const int years[2][nYears] =
149 {
150 // first line: the years to test
151 { 1990, 1976, 2000, 2030, 1984, },
152
153 // second line: TRUE if leap, FALSE otherwise
154 { FALSE, TRUE, TRUE, FALSE, TRUE }
155 };
156
157 for ( size_t n = 0; n < nYears; n++ )
158 {
159 int year = years[0][n];
160 bool should = years[1][n] != 0;
161
162 printf("Year %d is %sa leap year (should be: %s)\n",
163 year,
164 wxDateTime::IsLeapYear(year) ? "" : "not ",
165 should ? "yes" : "no");
166
167 wxASSERT( should == wxDateTime::IsLeapYear(year) );
168 }
169 }
170
171 // test constructing wxDateTime objects
172 static void TestTimeSet()
173 {
174 puts("\n*** wxDateTime construction test ***");
175
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());
180 }
181
182 #endif // TEST_TIME
183
184 // ----------------------------------------------------------------------------
185 // threads
186 // ----------------------------------------------------------------------------
187
188 #ifdef TEST_THREADS
189
190 #include <wx/thread.h>
191
192 static size_t gs_counter = (size_t)-1;
193 static wxCriticalSection gs_critsect;
194 static wxCondition gs_cond;
195
196 class MyJoinableThread : public wxThread
197 {
198 public:
199 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
200 { m_n = n; Create(); }
201
202 // thread execution starts here
203 virtual ExitCode Entry();
204
205 private:
206 size_t m_n;
207 };
208
209 wxThread::ExitCode MyJoinableThread::Entry()
210 {
211 unsigned long res = 1;
212 for ( size_t n = 1; n < m_n; n++ )
213 {
214 res *= n;
215
216 // it's a loooong calculation :-)
217 Sleep(100);
218 }
219
220 return (ExitCode)res;
221 }
222
223 class MyDetachedThread : public wxThread
224 {
225 public:
226 MyDetachedThread(size_t n, char ch) { m_n = n; m_ch = ch; Create(); }
227
228 // thread execution starts here
229 virtual ExitCode Entry();
230
231 // and stops here
232 virtual void OnExit();
233
234 private:
235 size_t m_n; // number of characters to write
236 char m_ch; // character to write
237 };
238
239 wxThread::ExitCode MyDetachedThread::Entry()
240 {
241 {
242 wxCriticalSectionLocker lock(gs_critsect);
243 if ( gs_counter == (size_t)-1 )
244 gs_counter = 1;
245 else
246 gs_counter++;
247 }
248
249 for ( size_t n = 0; n < m_n; n++ )
250 {
251 if ( TestDestroy() )
252 break;
253
254 putchar(m_ch);
255 fflush(stdout);
256
257 wxThread::Sleep(100);
258 }
259
260 return 0;
261 }
262
263 void MyDetachedThread::OnExit()
264 {
265 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
266
267 wxCriticalSectionLocker lock(gs_critsect);
268 if ( !--gs_counter )
269 gs_cond.Signal();
270 }
271
272 void TestDetachedThreads()
273 {
274 puts("\n*** Testing detached threads ***");
275
276 static const size_t nThreads = 3;
277 MyDetachedThread *threads[nThreads];
278 size_t n;
279 for ( n = 0; n < nThreads; n++ )
280 {
281 threads[n] = new MyDetachedThread(10, 'A' + n);
282 }
283
284 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
285 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
286
287 for ( n = 0; n < nThreads; n++ )
288 {
289 threads[n]->Run();
290 }
291
292 // wait until all threads terminate
293 gs_cond.Wait();
294
295 puts("");
296 }
297
298 void TestJoinableThreads()
299 {
300 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
301
302 // calc 10! in the background
303 MyJoinableThread thread(10);
304 thread.Run();
305
306 printf("\nThread terminated with exit code %lu.\n",
307 (unsigned long)thread.Wait());
308 }
309
310 void TestThreadSuspend()
311 {
312 puts("\n*** Testing thread suspend/resume functions ***");
313
314 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
315
316 thread->Run();
317
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
323 // in an error)
324 wxThread::Sleep(300);
325
326 for ( size_t n = 0; n < 3; n++ )
327 {
328 thread->Pause();
329
330 puts("\nThread suspended");
331 if ( n > 0 )
332 {
333 // don't sleep but resume immediately the first time
334 wxThread::Sleep(300);
335 }
336 puts("Going to resume the thread");
337
338 thread->Resume();
339 }
340
341 // wait until the thread terminates
342 gs_cond.Wait();
343
344 puts("");
345 }
346
347 void TestThreadDelete()
348 {
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!
353
354 puts("\n*** Testing thread delete function ***");
355
356 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
357
358 thread1->Run();
359
360 wxThread::Sleep(300);
361
362 thread1->Delete();
363
364 puts("\nDeleted a running thread.");
365
366 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
367
368 thread2->Run();
369
370 wxThread::Sleep(300);
371
372 thread2->Pause();
373
374 thread2->Delete();
375
376 puts("\nDeleted a sleeping thread.");
377
378 MyJoinableThread *thread3 = new MyJoinableThread(20);
379 thread3->Run();
380
381 thread3->Delete();
382
383 puts("\nDeleted a joinable thread.");
384
385 MyJoinableThread *thread4 = new MyJoinableThread(2);
386 thread4->Run();
387
388 wxThread::Sleep(300);
389
390 thread4->Delete();
391
392 puts("\nDeleted a joinable thread which already terminated.");
393
394 puts("");
395 }
396
397 #endif // TEST_THREADS
398
399 // ----------------------------------------------------------------------------
400 // arrays
401 // ----------------------------------------------------------------------------
402
403 #ifdef TEST_ARRAYS
404
405 void PrintArray(const char* name, const wxArrayString& array)
406 {
407 printf("Dump of the array '%s'\n", name);
408
409 size_t nCount = array.GetCount();
410 for ( size_t n = 0; n < nCount; n++ )
411 {
412 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
413 }
414 }
415
416 #endif // TEST_ARRAYS
417
418 // ----------------------------------------------------------------------------
419 // strings
420 // ----------------------------------------------------------------------------
421
422 #ifdef TEST_STRINGS
423
424 #include "wx/timer.h"
425
426 void TestString()
427 {
428 wxStopWatch sw;
429
430 wxString a, b, c;
431
432 a.reserve (128);
433 b.reserve (128);
434 c.reserve (128);
435
436 for (int i = 0; i < 1000000; ++i)
437 {
438 a = "Hello";
439 b = " world";
440 c = "! How'ya doin'?";
441 a += b;
442 a += c;
443 c = "Hello world! What's up?";
444 if (c != a)
445 c = "Doh!";
446 }
447
448 printf ("TestString elapsed time: %ld\n", sw.Time());
449 }
450
451 void TestPChar()
452 {
453 wxStopWatch sw;
454
455 char a [128];
456 char b [128];
457 char c [128];
458
459 for (int i = 0; i < 1000000; ++i)
460 {
461 strcpy (a, "Hello");
462 strcpy (b, " world");
463 strcpy (c, "! How'ya doin'?");
464 strcat (a, b);
465 strcat (a, c);
466 strcpy (c, "Hello world! What's up?");
467 if (strcmp (c, a) == 0)
468 strcpy (c, "Doh!");
469 }
470
471 printf ("TestPChar elapsed time: %ld\n", sw.Time());
472 }
473
474 #endif // TEST_STRINGS
475
476 // ----------------------------------------------------------------------------
477 // entry point
478 // ----------------------------------------------------------------------------
479
480 int main(int argc, char **argv)
481 {
482 if ( !wxInitialize() )
483 {
484 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
485 }
486
487 #ifdef TEST_STRINGS
488 TestPChar();
489 TestString();
490 #endif // TEST_STRINGS
491
492 #ifdef TEST_ARRAYS
493 wxArrayString a1;
494 a1.Add("tiger");
495 a1.Add("cat");
496 a1.Add("lion");
497 a1.Add("dog");
498 a1.Add("human");
499 a1.Add("ape");
500
501 puts("*** Initially:");
502
503 PrintArray("a1", a1);
504
505 wxArrayString a2(a1);
506 PrintArray("a2", a2);
507
508 wxSortedArrayString a3(a1);
509 PrintArray("a3", a3);
510
511 puts("*** After deleting a string from a1");
512 a1.Remove(2);
513
514 PrintArray("a1", a1);
515 PrintArray("a2", a2);
516 PrintArray("a3", a3);
517
518 puts("*** After reassigning a1 to a2 and a3");
519 a3 = a2 = a1;
520 PrintArray("a2", a2);
521 PrintArray("a3", a3);
522 #endif // TEST_ARRAYS
523
524 #ifdef TEST_LOG
525 wxString s;
526 for ( size_t n = 0; n < 8000; n++ )
527 {
528 s << (char)('A' + (n % 26));
529 }
530
531 wxString msg;
532 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
533
534 // this one shouldn't be truncated
535 printf(msg);
536
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
539 // by wxLog anyhow)
540 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
541 #endif // TEST_LOG
542
543 #ifdef TEST_THREADS
544 if ( argc > 1 && argv[1][0] == 't' )
545 wxLog::AddTraceMask("thread");
546
547 if ( 0 )
548 TestDetachedThreads();
549 if ( 0 )
550 TestJoinableThreads();
551 if ( 0 )
552 TestThreadSuspend();
553 if ( 1 )
554 TestThreadDelete();
555
556 #endif // TEST_THREADS
557
558 #ifdef TEST_LONGLONG
559 if ( 0 )
560 TestSpeed();
561 if ( 1 )
562 TestDivision();
563 #endif // TEST_LONGLONG
564
565 #ifdef TEST_TIME
566 TestTimeStatic();
567 TestTimeSet();
568 #endif // TEST_TIME
569
570 wxUninitialize();
571
572 return 0;
573 }