2010-05-16 15:44:17 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/events/stopwatch.cpp
|
|
|
|
// Purpose: Test wxStopWatch class
|
|
|
|
// Author: Francesco Montorsi (extracted from console sample)
|
|
|
|
// Created: 2010-05-16
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2010 wxWidgets team
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "wx/stopwatch.h"
|
|
|
|
#include "wx/utils.h"
|
|
|
|
|
2011-12-03 00:34:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
const long tolerance = 10; // in ms
|
|
|
|
const int sleepTime = 500;
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2010-05-16 15:44:17 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class StopWatchTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StopWatchTestCase() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( StopWatchTestCase );
|
|
|
|
CPPUNIT_TEST( Misc );
|
|
|
|
CPPUNIT_TEST( BackwardsClockBug );
|
2011-12-03 00:34:00 +00:00
|
|
|
CPPUNIT_TEST( RestartBug );
|
2010-05-16 15:44:17 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void Misc();
|
|
|
|
void BackwardsClockBug();
|
2011-12-03 00:34:00 +00:00
|
|
|
void RestartBug();
|
2010-05-16 15:44:17 +00:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(StopWatchTestCase)
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase );
|
|
|
|
|
2011-04-30 10:57:04 +00:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2010-05-16 15:44:17 +00:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" );
|
|
|
|
|
|
|
|
void StopWatchTestCase::Misc()
|
|
|
|
{
|
|
|
|
wxStopWatch sw;
|
2010-08-22 22:16:05 +00:00
|
|
|
long t;
|
2011-11-27 19:50:12 +00:00
|
|
|
wxLongLong usec;
|
2010-05-16 15:44:17 +00:00
|
|
|
|
|
|
|
sw.Pause(); // pause it immediately
|
|
|
|
|
2011-11-27 19:50:12 +00:00
|
|
|
// verify that almost no time elapsed
|
|
|
|
usec = sw.TimeInMicro();
|
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Elapsed time was %" wxLongLongFmtSpec "dus", usec),
|
|
|
|
usec < tolerance*1000
|
|
|
|
);
|
|
|
|
|
2011-11-27 19:50:05 +00:00
|
|
|
wxSleep(1);
|
2010-08-22 22:16:05 +00:00
|
|
|
t = sw.Time();
|
|
|
|
|
|
|
|
// check that the stop watch doesn't advance while paused
|
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Actual time value is %ld", t),
|
|
|
|
t >= 0 && t < tolerance
|
|
|
|
);
|
2010-05-16 15:44:17 +00:00
|
|
|
|
|
|
|
sw.Resume();
|
2011-11-27 19:50:05 +00:00
|
|
|
wxMilliSleep(sleepTime);
|
2010-08-22 22:16:05 +00:00
|
|
|
t = sw.Time();
|
2011-11-27 19:50:05 +00:00
|
|
|
// check that it did advance now by ~1.5s
|
2010-08-22 22:16:05 +00:00
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Actual time value is %ld", t),
|
2011-11-27 19:50:05 +00:00
|
|
|
t > sleepTime - tolerance && t < sleepTime + tolerance
|
2010-08-22 22:16:05 +00:00
|
|
|
);
|
2010-05-16 15:44:17 +00:00
|
|
|
|
|
|
|
sw.Pause();
|
2011-11-27 19:50:05 +00:00
|
|
|
|
|
|
|
// check that this sleep won't be taken into account below
|
|
|
|
wxMilliSleep(sleepTime);
|
2010-05-16 15:44:17 +00:00
|
|
|
sw.Resume();
|
|
|
|
|
2011-11-27 19:50:05 +00:00
|
|
|
wxMilliSleep(sleepTime);
|
2010-08-22 22:16:05 +00:00
|
|
|
t = sw.Time();
|
|
|
|
|
|
|
|
// and it should advance again
|
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Actual time value is %ld", t),
|
2011-11-27 19:50:05 +00:00
|
|
|
t > 2*sleepTime - tolerance && t < 2*sleepTime + tolerance
|
2010-08-22 22:16:05 +00:00
|
|
|
);
|
2010-05-16 15:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StopWatchTestCase::BackwardsClockBug()
|
|
|
|
{
|
|
|
|
wxStopWatch sw;
|
|
|
|
wxStopWatch sw2;
|
|
|
|
|
|
|
|
for ( size_t n = 0; n < 10; n++ )
|
|
|
|
{
|
|
|
|
sw2.Start();
|
|
|
|
|
|
|
|
for ( size_t m = 0; m < 10000; m++ )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT ( sw.Time() >= 0 && sw2.Time() >= 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 00:34:00 +00:00
|
|
|
|
|
|
|
void StopWatchTestCase::RestartBug()
|
|
|
|
{
|
|
|
|
wxStopWatch sw;
|
|
|
|
sw.Pause();
|
|
|
|
|
|
|
|
// Calling Start() should resume the stopwatch if it was paused.
|
|
|
|
static const int offset = 5000;
|
|
|
|
sw.Start(offset);
|
|
|
|
wxMilliSleep(sleepTime);
|
|
|
|
|
|
|
|
long t = sw.Time();
|
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Actual time value is %ld", t),
|
|
|
|
t > offset + sleepTime - tolerance &&
|
|
|
|
t < offset + sleepTime + tolerance
|
|
|
|
);
|
|
|
|
}
|