]> git.saurik.com Git - wxWidgets.git/blob - tests/events/stopwatch.cpp
Return valid pointers from wxDateTime::ParseXXX() with non-wxString strings.
[wxWidgets.git] / tests / events / stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/stopwatch.cpp
3 // Purpose: Test wxStopWatch class
4 // Author: Francesco Montorsi (extracted from console sample)
5 // Created: 2010-05-16
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10
11 // ----------------------------------------------------------------------------
12 // headers
13 // ----------------------------------------------------------------------------
14
15 #include "testprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #endif // WX_PRECOMP
23
24 #include <time.h>
25
26 #include "wx/stopwatch.h"
27 #include "wx/utils.h"
28
29 namespace
30 {
31
32 const long tolerance = 50; // in ms
33 const int sleepTime = 500;
34
35 } // anonymous namespace
36
37 // --------------------------------------------------------------------------
38 // test class
39 // --------------------------------------------------------------------------
40
41 class StopWatchTestCase : public CppUnit::TestCase
42 {
43 public:
44 StopWatchTestCase() {}
45
46 private:
47 CPPUNIT_TEST_SUITE( StopWatchTestCase );
48 CPPUNIT_TEST( Misc );
49 CPPUNIT_TEST( BackwardsClockBug );
50 CPPUNIT_TEST( RestartBug );
51 CPPUNIT_TEST_SUITE_END();
52
53 void Misc();
54 void BackwardsClockBug();
55 void RestartBug();
56
57 DECLARE_NO_COPY_CLASS(StopWatchTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase );
62
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" );
65
66 void StopWatchTestCase::Misc()
67 {
68 wxStopWatch sw;
69 long t;
70 wxLongLong usec;
71
72 sw.Pause(); // pause it immediately
73
74 // verify that almost no time elapsed
75 usec = sw.TimeInMicro();
76 WX_ASSERT_MESSAGE
77 (
78 ("Elapsed time was %" wxLongLongFmtSpec "dus", usec),
79 usec < tolerance*1000
80 );
81
82 wxSleep(1);
83 t = sw.Time();
84
85 // check that the stop watch doesn't advance while paused
86 WX_ASSERT_MESSAGE
87 (
88 ("Actual time value is %ld", t),
89 t >= 0 && t < tolerance
90 );
91
92 sw.Resume();
93 wxMilliSleep(sleepTime);
94 t = sw.Time();
95 // check that it did advance now by ~1.5s
96 WX_ASSERT_MESSAGE
97 (
98 ("Actual time value is %ld", t),
99 t > sleepTime - tolerance && t < sleepTime + tolerance
100 );
101
102 sw.Pause();
103
104 // check that this sleep won't be taken into account below
105 wxMilliSleep(sleepTime);
106 sw.Resume();
107
108 wxMilliSleep(sleepTime);
109 t = sw.Time();
110
111 // and it should advance again
112 WX_ASSERT_MESSAGE
113 (
114 ("Actual time value is %ld", t),
115 t > 2*sleepTime - tolerance && t < 2*sleepTime + tolerance
116 );
117 }
118
119 void StopWatchTestCase::BackwardsClockBug()
120 {
121 wxStopWatch sw;
122 wxStopWatch sw2;
123
124 for ( size_t n = 0; n < 10; n++ )
125 {
126 sw2.Start();
127
128 for ( size_t m = 0; m < 10000; m++ )
129 {
130 CPPUNIT_ASSERT ( sw.Time() >= 0 && sw2.Time() >= 0 );
131 }
132 }
133 }
134
135 void StopWatchTestCase::RestartBug()
136 {
137 wxStopWatch sw;
138 sw.Pause();
139
140 // Calling Start() should resume the stopwatch if it was paused.
141 static const int offset = 5000;
142 sw.Start(offset);
143 wxMilliSleep(sleepTime);
144
145 long t = sw.Time();
146 WX_ASSERT_MESSAGE
147 (
148 ("Actual time value is %ld", t),
149 t > offset + sleepTime - tolerance &&
150 t < offset + sleepTime + tolerance
151 );
152 }