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 class AtomicTestCase
: public CppUnit::TestCase
48 class MyThread
: public wxThread
51 MyThread(wxAtomicInt
&operateOn
, ETestType testType
) : wxThread(wxTHREAD_JOINABLE
),
52 m_operateOn(operateOn
), m_testType(testType
) {}
54 // thread execution starts here
55 virtual void *Entry();
58 wxAtomicInt
&m_operateOn
;
62 CPPUNIT_TEST_SUITE( AtomicTestCase
);
63 CPPUNIT_TEST( TestNoThread
);
64 CPPUNIT_TEST( TestDecReturn
);
65 CPPUNIT_TEST( TestTwoThreadsMix
);
66 CPPUNIT_TEST( TestTenThreadsMix
);
67 CPPUNIT_TEST( TestTwoThreadsSeparate
);
68 CPPUNIT_TEST( TestTenThreadsSeparate
);
69 CPPUNIT_TEST_SUITE_END();
73 void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed
); }
74 void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed
); }
75 void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly
); }
76 void TestTwoThreadsSeparate() { TestWithThreads(2, IncOnly
); }
77 void TestWithThreads(int count
, ETestType testtype
);
79 DECLARE_NO_COPY_CLASS(AtomicTestCase
)
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase
);
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase
, "AtomicTestCase" );
88 void AtomicTestCase::TestNoThread()
90 wxAtomicInt int1
=0, int2
=0;
92 for (wxInt32 i
=0; i
<10000000; ++i
)
98 CPPUNIT_ASSERT( int1
== 10000000 );
99 CPPUNIT_ASSERT( int2
== -10000000 );
102 void AtomicTestCase::TestDecReturn()
107 CPPUNIT_ASSERT( i
== 2 );
109 CPPUNIT_ASSERT( wxAtomicDec(i
) > 0 );
110 CPPUNIT_ASSERT( wxAtomicDec(i
) == 0 );
113 void AtomicTestCase::TestWithThreads(int count
, ETestType testType
)
117 wxArrayThread threads
;
120 for ( i
= 0; i
< count
; ++i
)
122 ETestType actualThreadType
;
126 actualThreadType
= testType
;
129 actualThreadType
= (i
&1)==0 ? IncOnly
: DecOnly
;
133 MyThread
*thread
= new MyThread(int1
, actualThreadType
);
135 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
137 wxLogError(wxT("Can't create thread!"));
143 for ( i
= 0; i
< count
; ++i
)
149 for ( i
= 0; i
< count
; ++i
)
151 // each thread should return 0, else it detected some problem
152 CPPUNIT_ASSERT (threads
[i
]->Wait() == (wxThread::ExitCode
)0);
155 CPPUNIT_ASSERT( int1
== 0 );
158 // ----------------------------------------------------------------------------
160 void *AtomicTestCase::MyThread::Entry()
162 wxInt32 negativeValuesSeen
= 0;
164 for (wxInt32 i
=0; i
<10000000; ++i
)
168 case AtomicTestCase::IncAndDecMixed
:
169 wxAtomicInc(m_operateOn
);
170 wxAtomicDec(m_operateOn
);
173 ++negativeValuesSeen
;
176 case AtomicTestCase::IncOnly
:
177 wxAtomicInc(m_operateOn
);
180 case AtomicTestCase::DecOnly
:
181 wxAtomicDec(m_operateOn
);
186 return (wxThread::ExitCode
)negativeValuesSeen
;