wxWidgets/tests/events/stopwatch.cpp
Vadim Zeitlin e70fc11ef1 Replace CppUnit with Catch for unit tests
Drop the legacy CppUnit testing framework used for the unit tests.
Replacing it with Catch has the advantage of not requiring CppUnit
libraries to be installed on the system in order to be able to run
tests (Catch is header-only and a copy of it is now included in the
main repository itself) and, in the future, of being able to write
the tests in a much more natural way.

For now, however, avoid changing the existing tests code as much as
[reasonably] possible to avoid introducing bugs in them and provide
the CppUnit compatibility macros in the new wx/catch_cppunit.h header
which allow to preserve the 99% of the existing code unchanged. Some
of the required changes are:

 - Decompose asserts using "a && b" conditions into multiple asserts
   checking "a" and "b" independently. This would have been better
   even with CppUnit (to know which part of condition exactly failed)
   and is required with Catch.

 - Use extra parentheses around such conditions when they can't be
   easily decomposed in the arrays test, due to the use of macros.
   This is not ideal from the point of view of messages given when
   the tests fail but will do for now.

 - Rewrite asserts using "a || b" as a combination of condition
   checks and assert macros. Again, this is better anyhow, and is
   required with Catch. Incidentally, this allowed to fix a bug in
   the "exec" unit test which didn't leave enough time for the new
   process to be launched before trying to kill it.

 - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros,
   our emulation of this macro can be used only once.

 - Provide string conversions using Catch-specific StringMaker for
   a couple of types.

 - Replace custom wxImage comparison with a Catch-specific matcher
   class.

 - Remove most of test running logic from test.cpp, in particular don't
   parse command line ourselves any longer but use Catch built-in
   command line parser. This is a source of a minor regression:
   previously, both "Foo" and "FooTestCase" could be used as the name of
   the test to run, but now only the latter is accepted.
2017-11-02 01:53:16 +01:00

173 lines
4.2 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/events/stopwatch.cpp
// Purpose: Test wxStopWatch class
// Author: Francesco Montorsi (extracted from console sample)
// Created: 2010-05-16
// 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"
namespace
{
const long tolerance = 50; // in ms
const int sleepTime = 500;
} // anonymous namespace
// --------------------------------------------------------------------------
// test class
// --------------------------------------------------------------------------
class StopWatchTestCase : public CppUnit::TestCase
{
public:
StopWatchTestCase() {}
private:
CPPUNIT_TEST_SUITE( StopWatchTestCase );
CPPUNIT_TEST( Misc );
CPPUNIT_TEST( BackwardsClockBug );
CPPUNIT_TEST( RestartBug );
CPPUNIT_TEST_SUITE_END();
void Misc();
void BackwardsClockBug();
void RestartBug();
wxDECLARE_NO_COPY_CLASS(StopWatchTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" );
void StopWatchTestCase::Misc()
{
// Buildbot machines are quite slow and sleep doesn't work reliably there,
// i.e. it can sleep for much longer than requested. This is not really an
// error, so just don't run this test there -- and if you get failures in
// this test when running it interactively, this might also be normal if
// the machine is under heavy load.
if ( IsAutomaticTest() )
return;
wxStopWatch sw;
long t;
wxLongLong usec;
sw.Pause(); // pause it immediately
// verify that almost no time elapsed
usec = sw.TimeInMicro();
WX_ASSERT_MESSAGE
(
("Elapsed time was %" wxLongLongFmtSpec "dus", usec),
usec < tolerance*1000
);
wxSleep(1);
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
);
sw.Resume();
wxMilliSleep(sleepTime);
t = sw.Time();
// check that it did advance now by ~1.5s
WX_ASSERT_MESSAGE
(
("Actual time value is %ld", t),
t > sleepTime - tolerance && t < 2*sleepTime
);
sw.Pause();
// check that this sleep won't be taken into account below
wxMilliSleep(sleepTime);
sw.Resume();
wxMilliSleep(sleepTime);
t = sw.Time();
// and it should advance again
WX_ASSERT_MESSAGE
(
("Actual time value is %ld", t),
t > 2*sleepTime - tolerance && t < 3*sleepTime
);
}
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 );
CPPUNIT_ASSERT( sw2.Time() >= 0 );
}
}
}
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
);
// As above, this is not actually due to the fact of the test being
// automatic but just because buildbot machines are usually pretty slow, so
// this test often fails there simply because of the high load on them.
if ( !IsAutomaticTest() )
{
WX_ASSERT_MESSAGE
(
("Actual time value is %ld", t),
t < offset + sleepTime + tolerance
);
}
}