]>
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 | // 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 | // -------------------------------------------------------------------------- | |
30 | // test class | |
31 | // -------------------------------------------------------------------------- | |
32 | ||
33 | class StopWatchTestCase : public CppUnit::TestCase | |
34 | { | |
35 | public: | |
36 | StopWatchTestCase() {} | |
37 | ||
38 | private: | |
39 | CPPUNIT_TEST_SUITE( StopWatchTestCase ); | |
40 | CPPUNIT_TEST( Misc ); | |
41 | CPPUNIT_TEST( BackwardsClockBug ); | |
42 | CPPUNIT_TEST_SUITE_END(); | |
43 | ||
44 | void Misc(); | |
45 | void BackwardsClockBug(); | |
46 | ||
47 | DECLARE_NO_COPY_CLASS(StopWatchTestCase) | |
48 | }; | |
49 | ||
50 | // register in the unnamed registry so that these tests are run by default | |
51 | CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase ); | |
52 | ||
53 | // also include in it's own registry so that these tests can be run alone | |
54 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" ); | |
55 | ||
56 | void StopWatchTestCase::Misc() | |
57 | { | |
58 | wxStopWatch sw; | |
59 | long tmp; | |
60 | ||
61 | sw.Pause(); // pause it immediately | |
62 | ||
63 | wxSleep(2); | |
64 | tmp = sw.Time(); | |
65 | CPPUNIT_ASSERT(tmp >= 0 && tmp < 100); | |
66 | // should not have counted while paused! | |
67 | ||
68 | sw.Resume(); | |
69 | wxSleep(3); | |
70 | tmp = sw.Time(); | |
71 | CPPUNIT_ASSERT(tmp >= 3000 && tmp < 4000); | |
72 | ||
73 | sw.Pause(); | |
74 | sw.Resume(); | |
75 | ||
76 | wxSleep(2); | |
77 | tmp = sw.Time(); | |
78 | CPPUNIT_ASSERT(tmp >= 5000 && tmp < 6000); | |
79 | } | |
80 | ||
81 | void StopWatchTestCase::BackwardsClockBug() | |
82 | { | |
83 | wxStopWatch sw; | |
84 | wxStopWatch sw2; | |
85 | ||
86 | for ( size_t n = 0; n < 10; n++ ) | |
87 | { | |
88 | sw2.Start(); | |
89 | ||
90 | for ( size_t m = 0; m < 10000; m++ ) | |
91 | { | |
92 | CPPUNIT_ASSERT ( sw.Time() >= 0 && sw2.Time() >= 0 ); | |
93 | } | |
94 | } | |
95 | } |