stopwatch.cpp 3.5 KB

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