Get rid of CppUnit boilerplate in atomic unit test
Also use a single test function with different sections instead of using a helper function with 4 wrappers calling it for simplicity.
This commit is contained in:
@@ -30,14 +30,11 @@ WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
|
||||
static const wxInt32 ITERATIONS_NUM = 10000;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// test class
|
||||
// test helper thread
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class AtomicTestCase : public CppUnit::TestCase
|
||||
namespace
|
||||
{
|
||||
public:
|
||||
AtomicTestCase() { }
|
||||
|
||||
enum ETestType
|
||||
{
|
||||
IncAndDecMixed,
|
||||
@@ -45,7 +42,6 @@ public:
|
||||
DecOnly
|
||||
};
|
||||
|
||||
private:
|
||||
class MyThread : public wxThread
|
||||
{
|
||||
public:
|
||||
@@ -59,34 +55,13 @@ private:
|
||||
wxAtomicInt &m_operateOn;
|
||||
ETestType m_testType;
|
||||
};
|
||||
} // anonymous namespace
|
||||
|
||||
CPPUNIT_TEST_SUITE( AtomicTestCase );
|
||||
CPPUNIT_TEST( TestNoThread );
|
||||
CPPUNIT_TEST( TestDecReturn );
|
||||
CPPUNIT_TEST( TestTwoThreadsMix );
|
||||
CPPUNIT_TEST( TestTenThreadsMix );
|
||||
CPPUNIT_TEST( TestTwoThreadsSeparate );
|
||||
CPPUNIT_TEST( TestTenThreadsSeparate );
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
// ----------------------------------------------------------------------------
|
||||
// the tests themselves
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void TestNoThread();
|
||||
void TestDecReturn();
|
||||
void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed); }
|
||||
void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed); }
|
||||
void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly); }
|
||||
void TestTwoThreadsSeparate() { TestWithThreads(2, IncOnly); }
|
||||
void TestWithThreads(int count, ETestType testtype);
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(AtomicTestCase);
|
||||
};
|
||||
|
||||
// register in the unnamed registry so that these tests are run by default
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase );
|
||||
|
||||
// also include in its own registry so that these tests can be run alone
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase, "AtomicTestCase" );
|
||||
|
||||
void AtomicTestCase::TestNoThread()
|
||||
TEST_CASE("Atomic::NoThread", "[atomic]")
|
||||
{
|
||||
wxAtomicInt int1 = 0,
|
||||
int2 = 0;
|
||||
@@ -97,23 +72,31 @@ void AtomicTestCase::TestNoThread()
|
||||
wxAtomicDec(int2);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT( int1 == ITERATIONS_NUM );
|
||||
CPPUNIT_ASSERT( int2 == -ITERATIONS_NUM );
|
||||
CHECK( int1 == ITERATIONS_NUM );
|
||||
CHECK( int2 == -ITERATIONS_NUM );
|
||||
}
|
||||
|
||||
void AtomicTestCase::TestDecReturn()
|
||||
TEST_CASE("Atomic::DecReturn", "[atomic]")
|
||||
{
|
||||
wxAtomicInt i(0);
|
||||
wxAtomicInc(i);
|
||||
wxAtomicInc(i);
|
||||
CPPUNIT_ASSERT( i == 2 );
|
||||
CHECK( i == 2 );
|
||||
|
||||
CPPUNIT_ASSERT( wxAtomicDec(i) > 0 );
|
||||
CPPUNIT_ASSERT( wxAtomicDec(i) == 0 );
|
||||
CHECK( wxAtomicDec(i) > 0 );
|
||||
CHECK( wxAtomicDec(i) == 0 );
|
||||
}
|
||||
|
||||
void AtomicTestCase::TestWithThreads(int count, ETestType testType)
|
||||
TEST_CASE("Atomic::WithThreads", "[atomic]")
|
||||
{
|
||||
int count;
|
||||
ETestType testType;
|
||||
|
||||
SECTION( "2 threads using inc and dec") { count = 2; testType = IncAndDecMixed; }
|
||||
SECTION("10 threads using inc and dec") { count = 10; testType = IncAndDecMixed; }
|
||||
SECTION( "2 threads using inc or dec" ) { count = 2; testType = IncOnly; }
|
||||
SECTION("10 threads using inc or dec" ) { count = 10; testType = IncOnly; }
|
||||
|
||||
wxAtomicInt int1=0;
|
||||
|
||||
wxArrayThread threads;
|
||||
@@ -152,16 +135,16 @@ void AtomicTestCase::TestWithThreads(int count, ETestType testType)
|
||||
for ( i = 0; i < count; ++i )
|
||||
{
|
||||
// each thread should return 0, else it detected some problem
|
||||
CPPUNIT_ASSERT (threads[i]->Wait() == (wxThread::ExitCode)0);
|
||||
CHECK (threads[i]->Wait() == (wxThread::ExitCode)0);
|
||||
delete threads[i];
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT( int1 == 0 );
|
||||
CHECK( int1 == 0 );
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void *AtomicTestCase::MyThread::Entry()
|
||||
void *MyThread::Entry()
|
||||
{
|
||||
wxInt32 negativeValuesSeen = 0;
|
||||
|
||||
@@ -169,17 +152,17 @@ void *AtomicTestCase::MyThread::Entry()
|
||||
{
|
||||
switch ( m_testType )
|
||||
{
|
||||
case AtomicTestCase::IncAndDecMixed:
|
||||
case IncAndDecMixed:
|
||||
wxAtomicInc(m_operateOn);
|
||||
if ( wxAtomicDec(m_operateOn) < 0 )
|
||||
++negativeValuesSeen;
|
||||
break;
|
||||
|
||||
case AtomicTestCase::IncOnly:
|
||||
case IncOnly:
|
||||
wxAtomicInc(m_operateOn);
|
||||
break;
|
||||
|
||||
case AtomicTestCase::DecOnly:
|
||||
case DecOnly:
|
||||
wxAtomicDec(m_operateOn);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user