]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/stopwatch.cpp
3 // Purpose: Test wxStopWatch class
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
26 #include "wx/stopwatch.h"
32 const long tolerance
= 50; // in ms
33 const int sleepTime
= 500;
35 } // anonymous namespace
37 // --------------------------------------------------------------------------
39 // --------------------------------------------------------------------------
41 class StopWatchTestCase
: public CppUnit::TestCase
44 StopWatchTestCase() {}
47 CPPUNIT_TEST_SUITE( StopWatchTestCase
);
49 CPPUNIT_TEST( BackwardsClockBug
);
50 CPPUNIT_TEST( RestartBug
);
51 CPPUNIT_TEST_SUITE_END();
54 void BackwardsClockBug();
57 DECLARE_NO_COPY_CLASS(StopWatchTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase
, "StopWatchTestCase" );
66 void StopWatchTestCase::Misc()
72 sw
.Pause(); // pause it immediately
74 // verify that almost no time elapsed
75 usec
= sw
.TimeInMicro();
78 ("Elapsed time was %" wxLongLongFmtSpec
"dus", usec
),
85 // check that the stop watch doesn't advance while paused
88 ("Actual time value is %ld", t
),
89 t
>= 0 && t
< tolerance
93 wxMilliSleep(sleepTime
);
95 // check that it did advance now by ~1.5s
98 ("Actual time value is %ld", t
),
99 t
> sleepTime
- tolerance
&& t
< sleepTime
+ tolerance
104 // check that this sleep won't be taken into account below
105 wxMilliSleep(sleepTime
);
108 wxMilliSleep(sleepTime
);
111 // and it should advance again
114 ("Actual time value is %ld", t
),
115 t
> 2*sleepTime
- tolerance
&& t
< 2*sleepTime
+ tolerance
119 void StopWatchTestCase::BackwardsClockBug()
124 for ( size_t n
= 0; n
< 10; n
++ )
128 for ( size_t m
= 0; m
< 10000; m
++ )
130 CPPUNIT_ASSERT ( sw
.Time() >= 0 && sw2
.Time() >= 0 );
135 void StopWatchTestCase::RestartBug()
140 // Calling Start() should resume the stopwatch if it was paused.
141 static const int offset
= 5000;
143 wxMilliSleep(sleepTime
);
148 ("Actual time value is %ld", t
),
149 t
> offset
+ sleepTime
- tolerance
&&
150 t
< offset
+ sleepTime
+ tolerance