]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
test for timegm() added
[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 size_t nYears = 5;
148 static const size_t 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 // test time zones stuff
183 static void TestTimeZones()
184 {
185 puts("\n*** wxDateTime timezone test ***");
186
187 wxDateTime now = wxDateTime::Now();
188
189 printf("Current GMT time:\t%s\n", now.ToGMT().Format().c_str());
190 //TODO 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());
194 }
195
196 // test some minimal support for the dates outside the standard range
197 static void TestTimeRange()
198 {
199 puts("\n*** wxDateTime out-of-standard-range dates test ***");
200
201 printf("JDN 0: \t%s\n",
202 wxDateTime(0.0).Format().c_str());
203 printf("Jan 1, 1AD:\t%s\n",
204 wxDateTime(1, wxDateTime::Jan, 1).Format().c_str());
205 printf("May 29, 2099:\t%s\n",
206 wxDateTime(29, wxDateTime::May, 2099).Format().c_str());
207 }
208
209 // test conversions to JDN &c
210 static void TestTimeJulian()
211 {
212 puts("\n*** wxDateTime to JDN test ***");
213
214 printf("JDN of current time:\t%f\n", wxDateTime::Now().GetJulianDayNumber());
215 printf("JDN of Jan 1, 1900: \t%f\n",
216 wxDateTime(1, wxDateTime::Jan, 1900).GetJulianDayNumber());
217 printf("JDN of Jan 1, 1BC: \t%f\n",
218 wxDateTime(1, wxDateTime::Jan, 0).GetJulianDayNumber());
219 printf("JDN 0: \t%f\n",
220 wxDateTime(24, wxDateTime::Nov, -4713, 12, 0, 0).GetJulianDayNumber());
221 }
222
223 #endif // TEST_TIME
224
225 // ----------------------------------------------------------------------------
226 // threads
227 // ----------------------------------------------------------------------------
228
229 #ifdef TEST_THREADS
230
231 #include <wx/thread.h>
232
233 static size_t gs_counter = (size_t)-1;
234 static wxCriticalSection gs_critsect;
235 static wxCondition gs_cond;
236
237 class MyJoinableThread : public wxThread
238 {
239 public:
240 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
241 { m_n = n; Create(); }
242
243 // thread execution starts here
244 virtual ExitCode Entry();
245
246 private:
247 size_t m_n;
248 };
249
250 wxThread::ExitCode MyJoinableThread::Entry()
251 {
252 unsigned long res = 1;
253 for ( size_t n = 1; n < m_n; n++ )
254 {
255 res *= n;
256
257 // it's a loooong calculation :-)
258 Sleep(100);
259 }
260
261 return (ExitCode)res;
262 }
263
264 class MyDetachedThread : public wxThread
265 {
266 public:
267 MyDetachedThread(size_t n, char ch)
268 {
269 m_n = n;
270 m_ch = ch;
271 m_cancelled = FALSE;
272
273 Create();
274 }
275
276 // thread execution starts here
277 virtual ExitCode Entry();
278
279 // and stops here
280 virtual void OnExit();
281
282 private:
283 size_t m_n; // number of characters to write
284 char m_ch; // character to write
285
286 bool m_cancelled; // FALSE if we exit normally
287 };
288
289 wxThread::ExitCode MyDetachedThread::Entry()
290 {
291 {
292 wxCriticalSectionLocker lock(gs_critsect);
293 if ( gs_counter == (size_t)-1 )
294 gs_counter = 1;
295 else
296 gs_counter++;
297 }
298
299 for ( size_t n = 0; n < m_n; n++ )
300 {
301 if ( TestDestroy() )
302 {
303 m_cancelled = TRUE;
304
305 break;
306 }
307
308 putchar(m_ch);
309 fflush(stdout);
310
311 wxThread::Sleep(100);
312 }
313
314 return 0;
315 }
316
317 void MyDetachedThread::OnExit()
318 {
319 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
320
321 wxCriticalSectionLocker lock(gs_critsect);
322 if ( !--gs_counter && !m_cancelled )
323 gs_cond.Signal();
324 }
325
326 void TestDetachedThreads()
327 {
328 puts("\n*** Testing detached threads ***");
329
330 static const size_t nThreads = 3;
331 MyDetachedThread *threads[nThreads];
332 size_t n;
333 for ( n = 0; n < nThreads; n++ )
334 {
335 threads[n] = new MyDetachedThread(10, 'A' + n);
336 }
337
338 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
339 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
340
341 for ( n = 0; n < nThreads; n++ )
342 {
343 threads[n]->Run();
344 }
345
346 // wait until all threads terminate
347 gs_cond.Wait();
348
349 puts("");
350 }
351
352 void TestJoinableThreads()
353 {
354 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
355
356 // calc 10! in the background
357 MyJoinableThread thread(10);
358 thread.Run();
359
360 printf("\nThread terminated with exit code %lu.\n",
361 (unsigned long)thread.Wait());
362 }
363
364 void TestThreadSuspend()
365 {
366 puts("\n*** Testing thread suspend/resume functions ***");
367
368 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
369
370 thread->Run();
371
372 // this is for this demo only, in a real life program we'd use another
373 // condition variable which would be signaled from wxThread::Entry() to
374 // tell us that the thread really started running - but here just wait a
375 // bit and hope that it will be enough (the problem is, of course, that
376 // the thread might still not run when we call Pause() which will result
377 // in an error)
378 wxThread::Sleep(300);
379
380 for ( size_t n = 0; n < 3; n++ )
381 {
382 thread->Pause();
383
384 puts("\nThread suspended");
385 if ( n > 0 )
386 {
387 // don't sleep but resume immediately the first time
388 wxThread::Sleep(300);
389 }
390 puts("Going to resume the thread");
391
392 thread->Resume();
393 }
394
395 puts("Waiting until it terminates now");
396
397 // wait until the thread terminates
398 gs_cond.Wait();
399
400 puts("");
401 }
402
403 void TestThreadDelete()
404 {
405 // As above, using Sleep() is only for testing here - we must use some
406 // synchronisation object instead to ensure that the thread is still
407 // running when we delete it - deleting a detached thread which already
408 // terminated will lead to a crash!
409
410 puts("\n*** Testing thread delete function ***");
411
412 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
413
414 thread0->Delete();
415
416 puts("\nDeleted a thread which didn't start to run yet.");
417
418 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
419
420 thread1->Run();
421
422 wxThread::Sleep(300);
423
424 thread1->Delete();
425
426 puts("\nDeleted a running thread.");
427
428 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
429
430 thread2->Run();
431
432 wxThread::Sleep(300);
433
434 thread2->Pause();
435
436 thread2->Delete();
437
438 puts("\nDeleted a sleeping thread.");
439
440 MyJoinableThread thread3(20);
441 thread3.Run();
442
443 thread3.Delete();
444
445 puts("\nDeleted a joinable thread.");
446
447 MyJoinableThread thread4(2);
448 thread4.Run();
449
450 wxThread::Sleep(300);
451
452 thread4.Delete();
453
454 puts("\nDeleted a joinable thread which already terminated.");
455
456 puts("");
457 }
458
459 #endif // TEST_THREADS
460
461 // ----------------------------------------------------------------------------
462 // arrays
463 // ----------------------------------------------------------------------------
464
465 #ifdef TEST_ARRAYS
466
467 void PrintArray(const char* name, const wxArrayString& array)
468 {
469 printf("Dump of the array '%s'\n", name);
470
471 size_t nCount = array.GetCount();
472 for ( size_t n = 0; n < nCount; n++ )
473 {
474 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
475 }
476 }
477
478 #endif // TEST_ARRAYS
479
480 // ----------------------------------------------------------------------------
481 // strings
482 // ----------------------------------------------------------------------------
483
484 #ifdef TEST_STRINGS
485
486 #include "wx/timer.h"
487
488 void TestString()
489 {
490 wxStopWatch sw;
491
492 wxString a, b, c;
493
494 a.reserve (128);
495 b.reserve (128);
496 c.reserve (128);
497
498 for (int i = 0; i < 1000000; ++i)
499 {
500 a = "Hello";
501 b = " world";
502 c = "! How'ya doin'?";
503 a += b;
504 a += c;
505 c = "Hello world! What's up?";
506 if (c != a)
507 c = "Doh!";
508 }
509
510 printf ("TestString elapsed time: %ld\n", sw.Time());
511 }
512
513 void TestPChar()
514 {
515 wxStopWatch sw;
516
517 char a [128];
518 char b [128];
519 char c [128];
520
521 for (int i = 0; i < 1000000; ++i)
522 {
523 strcpy (a, "Hello");
524 strcpy (b, " world");
525 strcpy (c, "! How'ya doin'?");
526 strcat (a, b);
527 strcat (a, c);
528 strcpy (c, "Hello world! What's up?");
529 if (strcmp (c, a) == 0)
530 strcpy (c, "Doh!");
531 }
532
533 printf ("TestPChar elapsed time: %ld\n", sw.Time());
534 }
535
536 #endif // TEST_STRINGS
537
538 // ----------------------------------------------------------------------------
539 // entry point
540 // ----------------------------------------------------------------------------
541
542 int main(int argc, char **argv)
543 {
544 if ( !wxInitialize() )
545 {
546 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
547 }
548
549 #ifdef TEST_STRINGS
550 TestPChar();
551 TestString();
552 #endif // TEST_STRINGS
553
554 #ifdef TEST_ARRAYS
555 wxArrayString a1;
556 a1.Add("tiger");
557 a1.Add("cat");
558 a1.Add("lion");
559 a1.Add("dog");
560 a1.Add("human");
561 a1.Add("ape");
562
563 puts("*** Initially:");
564
565 PrintArray("a1", a1);
566
567 wxArrayString a2(a1);
568 PrintArray("a2", a2);
569
570 wxSortedArrayString a3(a1);
571 PrintArray("a3", a3);
572
573 puts("*** After deleting a string from a1");
574 a1.Remove(2);
575
576 PrintArray("a1", a1);
577 PrintArray("a2", a2);
578 PrintArray("a3", a3);
579
580 puts("*** After reassigning a1 to a2 and a3");
581 a3 = a2 = a1;
582 PrintArray("a2", a2);
583 PrintArray("a3", a3);
584 #endif // TEST_ARRAYS
585
586 #ifdef TEST_LOG
587 wxString s;
588 for ( size_t n = 0; n < 8000; n++ )
589 {
590 s << (char)('A' + (n % 26));
591 }
592
593 wxString msg;
594 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
595
596 // this one shouldn't be truncated
597 printf(msg);
598
599 // but this one will because log functions use fixed size buffer
600 // (note that it doesn't need '\n' at the end neither - will be added
601 // by wxLog anyhow)
602 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
603 #endif // TEST_LOG
604
605 #ifdef TEST_THREADS
606 if ( argc > 1 && argv[1][0] == 't' )
607 wxLog::AddTraceMask("thread");
608
609 if ( 1 )
610 TestDetachedThreads();
611 if ( 1 )
612 TestJoinableThreads();
613 if ( 1 )
614 TestThreadSuspend();
615 if ( 1 )
616 TestThreadDelete();
617
618 #endif // TEST_THREADS
619
620 #ifdef TEST_LONGLONG
621 if ( 0 )
622 TestSpeed();
623 if ( 1 )
624 TestDivision();
625 #endif // TEST_LONGLONG
626
627 #ifdef TEST_TIME
628 TestTimeStatic();
629 TestTimeSet();
630 TestTimeZones();
631 TestTimeRange();
632 TestTimeJulian();
633 #endif // TEST_TIME
634
635 wxUninitialize();
636
637 return 0;
638 }