1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/thread/atomic.cpp
// Purpose: wxAtomic??? unit test
// Author: Armel Asselin
// Created: 2006-12-14
// Copyright: (c) 2006 Armel Asselin
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#endif // WX_PRECOMP
#include "wx/atomic.h"
#include "wx/thread.h"
#include "wx/dynarray.h"
#include "wx/log.h"
WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// number of times to run the loops: the code takes too long to run if we use
// the bigger value with generic atomic operations implementation
#ifdef wxHAS_ATOMIC_OPS
static const wxInt32 ITERATIONS_NUM = 10000000;
#else
static const wxInt32 ITERATIONS_NUM = 1000;
#endif
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class AtomicTestCase : public CppUnit::TestCase
{
public:
AtomicTestCase() { }
enum ETestType
{
IncAndDecMixed,
IncOnly,
DecOnly
};
private:
class MyThread : public wxThread
{
public:
MyThread(wxAtomicInt &operateOn, ETestType testType) : wxThread(wxTHREAD_JOINABLE),
m_operateOn(operateOn), m_testType(testType) {}
// thread execution starts here
virtual void *Entry();
public:
wxAtomicInt &m_operateOn;
ETestType m_testType;
};
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();
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);
DECLARE_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()
{
wxAtomicInt int1 = 0,
int2 = 0;
for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
{
wxAtomicInc(int1);
wxAtomicDec(int2);
}
CPPUNIT_ASSERT( int1 == ITERATIONS_NUM );
CPPUNIT_ASSERT( int2 == -ITERATIONS_NUM );
}
void AtomicTestCase::TestDecReturn()
{
wxAtomicInt i(0);
wxAtomicInc(i);
wxAtomicInc(i);
CPPUNIT_ASSERT( i == 2 );
CPPUNIT_ASSERT( wxAtomicDec(i) > 0 );
CPPUNIT_ASSERT( wxAtomicDec(i) == 0 );
}
void AtomicTestCase::TestWithThreads(int count, ETestType testType)
{
wxAtomicInt int1=0;
wxArrayThread threads;
int i;
for ( i = 0; i < count; ++i )
{
ETestType actualThreadType;
switch(testType)
{
default:
actualThreadType = testType;
break;
case IncOnly:
actualThreadType = (i&1)==0 ? IncOnly : DecOnly;
break;
}
MyThread *thread = new MyThread(int1, actualThreadType);
if ( thread->Create() != wxTHREAD_NO_ERROR )
{
wxLogError(wxT("Can't create thread!"));
}
else
threads.Add(thread);
}
for ( i = 0; i < count; ++i )
{
threads[i]->Run();
}
for ( i = 0; i < count; ++i )
{
// each thread should return 0, else it detected some problem
CPPUNIT_ASSERT (threads[i]->Wait() == (wxThread::ExitCode)0);
}
CPPUNIT_ASSERT( int1 == 0 );
}
// ----------------------------------------------------------------------------
void *AtomicTestCase::MyThread::Entry()
{
wxInt32 negativeValuesSeen = 0;
for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
{
switch ( m_testType )
{
case AtomicTestCase::IncAndDecMixed:
wxAtomicInc(m_operateOn);
wxAtomicDec(m_operateOn);
if (m_operateOn < 0)
++negativeValuesSeen;
break;
case AtomicTestCase::IncOnly:
wxAtomicInc(m_operateOn);
break;
case AtomicTestCase::DecOnly:
wxAtomicDec(m_operateOn);
break;
}
}
return wxUIntToPtr(negativeValuesSeen);
}
|