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"
26 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class AtomicTestCase
: public CppUnit::TestCase
46 class MyThread
: public wxThread
49 MyThread(wxAtomicInt
&operateOn
, ETestType testType
) : wxThread(wxTHREAD_JOINABLE
),
50 m_operateOn(operateOn
), m_testType(testType
) {}
52 // thread execution starts here
53 virtual void *Entry();
56 wxAtomicInt
&m_operateOn
;
60 CPPUNIT_TEST_SUITE( AtomicTestCase
);
61 CPPUNIT_TEST( TestNoThread
);
62 CPPUNIT_TEST( TestTwoThreadsMix
);
63 CPPUNIT_TEST( TestTenThreadsMix
);
64 CPPUNIT_TEST( TestTwoThreadsSeparate
);
65 CPPUNIT_TEST( TestTenThreadsSeparate
);
66 CPPUNIT_TEST_SUITE_END();
69 void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed
); }
70 void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed
); }
71 void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly
); }
72 void TestTwoThreadsSeparate() { TestWithThreads(2, IncOnly
); }
73 void TestWithThreads(int count
, ETestType testtype
);
75 DECLARE_NO_COPY_CLASS(AtomicTestCase
)
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase
);
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase
, "AtomicTestCase" );
84 void AtomicTestCase::TestNoThread()
86 wxAtomicInt int1
=0, int2
=0;
88 for (wxInt32 i
=0; i
<10000000; ++i
)
94 CPPUNIT_ASSERT( int1
== 10000000 );
95 CPPUNIT_ASSERT( int2
== -10000000 );
98 void AtomicTestCase::TestWithThreads(int count
, ETestType testType
)
102 wxArrayThread threads
;
105 for ( i
= 0; i
< count
; ++i
)
107 ETestType actualThreadType
;
111 actualThreadType
= testType
;
114 actualThreadType
= (i
&1)==0 ? IncOnly
: DecOnly
;
118 MyThread
*thread
= new MyThread(int1
, actualThreadType
);
120 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
122 wxLogError(wxT("Can't create thread!"));
128 for ( i
= 0; i
< count
; ++i
)
134 for ( i
= 0; i
< count
; ++i
)
136 // each thread should return 0, else it detected some problem
137 CPPUNIT_ASSERT (threads
[i
]->Wait() == (wxThread::ExitCode
)0);
140 CPPUNIT_ASSERT( int1
== 0 );
143 // ----------------------------------------------------------------------------
145 void *AtomicTestCase::MyThread::Entry()
147 wxInt32 negativeValuesSeen
= 0;
149 for (wxInt32 i
=0; i
<10000000; ++i
)
153 case AtomicTestCase::IncAndDecMixed
:
154 wxAtomicInc(m_operateOn
);
155 wxAtomicDec(m_operateOn
);
158 ++negativeValuesSeen
;
161 case AtomicTestCase::IncOnly
:
162 wxAtomicInc(m_operateOn
);
165 case AtomicTestCase::DecOnly
:
166 wxAtomicDec(m_operateOn
);
171 return (wxThread::ExitCode
)negativeValuesSeen
;