1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/atomic.cpp
3 // Purpose: wxAtomic??? unit test
4 // Author: Armel Asselin
7 // Copyright: (c) 2006 Armel Asselin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/atomic.h"
24 #include "wx/thread.h"
25 #include "wx/dynarray.h"
28 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // number of times to run the loops: the code takes too long to run if we use
35 // the bigger value with generic atomic operations implementation
36 #ifdef wxHAS_ATOMIC_OPS
37 static const wxInt32 ITERATIONS_NUM
= 10000000;
39 static const wxInt32 ITERATIONS_NUM
= 1000;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class AtomicTestCase
: public CppUnit::TestCase
59 class MyThread
: public wxThread
62 MyThread(wxAtomicInt
&operateOn
, ETestType testType
) : wxThread(wxTHREAD_JOINABLE
),
63 m_operateOn(operateOn
), m_testType(testType
) {}
65 // thread execution starts here
66 virtual void *Entry();
69 wxAtomicInt
&m_operateOn
;
73 CPPUNIT_TEST_SUITE( AtomicTestCase
);
74 CPPUNIT_TEST( TestNoThread
);
75 CPPUNIT_TEST( TestDecReturn
);
76 CPPUNIT_TEST( TestTwoThreadsMix
);
77 CPPUNIT_TEST( TestTenThreadsMix
);
78 CPPUNIT_TEST( TestTwoThreadsSeparate
);
79 CPPUNIT_TEST( TestTenThreadsSeparate
);
80 CPPUNIT_TEST_SUITE_END();
84 void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed
); }
85 void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed
); }
86 void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly
); }
87 void TestTwoThreadsSeparate() { TestWithThreads(2, IncOnly
); }
88 void TestWithThreads(int count
, ETestType testtype
);
90 DECLARE_NO_COPY_CLASS(AtomicTestCase
)
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase
);
96 // also include in its own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase
, "AtomicTestCase" );
99 void AtomicTestCase::TestNoThread()
101 wxAtomicInt int1
= 0,
104 for ( wxInt32 i
= 0; i
< ITERATIONS_NUM
; ++i
)
110 CPPUNIT_ASSERT( int1
== ITERATIONS_NUM
);
111 CPPUNIT_ASSERT( int2
== -ITERATIONS_NUM
);
114 void AtomicTestCase::TestDecReturn()
119 CPPUNIT_ASSERT( i
== 2 );
121 CPPUNIT_ASSERT( wxAtomicDec(i
) > 0 );
122 CPPUNIT_ASSERT( wxAtomicDec(i
) == 0 );
125 void AtomicTestCase::TestWithThreads(int count
, ETestType testType
)
129 wxArrayThread threads
;
132 for ( i
= 0; i
< count
; ++i
)
134 ETestType actualThreadType
;
138 actualThreadType
= testType
;
141 actualThreadType
= (i
&1)==0 ? IncOnly
: DecOnly
;
145 MyThread
*thread
= new MyThread(int1
, actualThreadType
);
147 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
149 wxLogError(wxT("Can't create thread!"));
155 for ( i
= 0; i
< count
; ++i
)
161 for ( i
= 0; i
< count
; ++i
)
163 // each thread should return 0, else it detected some problem
164 CPPUNIT_ASSERT (threads
[i
]->Wait() == (wxThread::ExitCode
)0);
167 CPPUNIT_ASSERT( int1
== 0 );
170 // ----------------------------------------------------------------------------
172 void *AtomicTestCase::MyThread::Entry()
174 wxInt32 negativeValuesSeen
= 0;
176 for ( wxInt32 i
= 0; i
< ITERATIONS_NUM
; ++i
)
178 switch ( m_testType
)
180 case AtomicTestCase::IncAndDecMixed
:
181 wxAtomicInc(m_operateOn
);
182 wxAtomicDec(m_operateOn
);
185 ++negativeValuesSeen
;
188 case AtomicTestCase::IncOnly
:
189 wxAtomicInc(m_operateOn
);
192 case AtomicTestCase::DecOnly
:
193 wxAtomicDec(m_operateOn
);
198 return wxUIntToPtr(negativeValuesSeen
);