]>
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)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/stopwatch.h"
31 const long tolerance
= 50; // in ms
32 const int sleepTime
= 500;
34 } // anonymous namespace
36 // --------------------------------------------------------------------------
38 // --------------------------------------------------------------------------
40 class StopWatchTestCase
: public CppUnit::TestCase
43 StopWatchTestCase() {}
46 CPPUNIT_TEST_SUITE( StopWatchTestCase
);
48 CPPUNIT_TEST( BackwardsClockBug
);
49 CPPUNIT_TEST( RestartBug
);
50 CPPUNIT_TEST_SUITE_END();
53 void BackwardsClockBug();
56 DECLARE_NO_COPY_CLASS(StopWatchTestCase
)
59 // register in the unnamed registry so that these tests are run by default
60 CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase
);
62 // also include in its own registry so that these tests can be run alone
63 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase
, "StopWatchTestCase" );
65 void StopWatchTestCase::Misc()
71 sw
.Pause(); // pause it immediately
73 // verify that almost no time elapsed
74 usec
= sw
.TimeInMicro();
77 ("Elapsed time was %" wxLongLongFmtSpec
"dus", usec
),
84 // check that the stop watch doesn't advance while paused
87 ("Actual time value is %ld", t
),
88 t
>= 0 && t
< tolerance
92 wxMilliSleep(sleepTime
);
94 // check that it did advance now by ~1.5s
97 ("Actual time value is %ld", t
),
98 t
> sleepTime
- tolerance
&& t
< sleepTime
+ tolerance
103 // check that this sleep won't be taken into account below
104 wxMilliSleep(sleepTime
);
107 wxMilliSleep(sleepTime
);
110 // and it should advance again
113 ("Actual time value is %ld", t
),
114 t
> 2*sleepTime
- tolerance
&& t
< 2*sleepTime
+ tolerance
118 void StopWatchTestCase::BackwardsClockBug()
123 for ( size_t n
= 0; n
< 10; n
++ )
127 for ( size_t m
= 0; m
< 10000; m
++ )
129 CPPUNIT_ASSERT ( sw
.Time() >= 0 && sw2
.Time() >= 0 );
134 void StopWatchTestCase::RestartBug()
139 // Calling Start() should resume the stopwatch if it was paused.
140 static const int offset
= 5000;
142 wxMilliSleep(sleepTime
);
147 ("Actual time value is %ld", t
),
148 t
> offset
+ sleepTime
- tolerance
&&
149 t
< offset
+ sleepTime
+ tolerance