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