]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/thread/atomic.cpp
Don't use GetThreadId() in wxMSW code.
[wxWidgets.git] / tests / thread / atomic.cpp
index 976266331bbfb3b327e6292efd6e95545052102c..913aabf300cd2475724a0b327eff9dacb0e077e9 100644 (file)
 
 WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
 
 
 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
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // test class
 // ----------------------------------------------------------------------------
@@ -36,8 +48,6 @@ class AtomicTestCase : public CppUnit::TestCase
 public:
     AtomicTestCase() { }
 
 public:
     AtomicTestCase() { }
 
-private:
-
     enum ETestType
     {
         IncAndDecMixed,
     enum ETestType
     {
         IncAndDecMixed,
@@ -45,6 +55,7 @@ private:
         DecOnly
     };
 
         DecOnly
     };
 
+private:
     class MyThread : public wxThread
     {
     public:
     class MyThread : public wxThread
     {
     public:
@@ -61,6 +72,7 @@ private:
 
     CPPUNIT_TEST_SUITE( AtomicTestCase );
         CPPUNIT_TEST( TestNoThread );
 
     CPPUNIT_TEST_SUITE( AtomicTestCase );
         CPPUNIT_TEST( TestNoThread );
+        CPPUNIT_TEST( TestDecReturn );
         CPPUNIT_TEST( TestTwoThreadsMix );
         CPPUNIT_TEST( TestTenThreadsMix );
         CPPUNIT_TEST( TestTwoThreadsSeparate );
         CPPUNIT_TEST( TestTwoThreadsMix );
         CPPUNIT_TEST( TestTenThreadsMix );
         CPPUNIT_TEST( TestTwoThreadsSeparate );
@@ -68,6 +80,7 @@ private:
     CPPUNIT_TEST_SUITE_END();
 
     void TestNoThread();
     CPPUNIT_TEST_SUITE_END();
 
     void TestNoThread();
+    void TestDecReturn();
     void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed); }
     void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed); }
     void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly); }
     void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed); }
     void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed); }
     void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly); }
@@ -80,21 +93,33 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase );
 
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase );
 
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase, "AtomicTestCase" );
 
 void AtomicTestCase::TestNoThread()
 {
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase, "AtomicTestCase" );
 
 void AtomicTestCase::TestNoThread()
 {
-    wxAtomicInt    int1=0, int2=0;
+    wxAtomicInt int1 = 0,
+                int2 = 0;
 
 
-    for (wxInt32 i=0; i<10000000; ++i)
+    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
     {
         wxAtomicInc(int1);
         wxAtomicDec(int2);
     }
 
     {
         wxAtomicInc(int1);
         wxAtomicDec(int2);
     }
 
-    CPPUNIT_ASSERT( int1 == 10000000 );
-    CPPUNIT_ASSERT( int2 == -10000000 );
+    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)
 }
 
 void AtomicTestCase::TestWithThreads(int count, ETestType testType)
@@ -148,27 +173,27 @@ void *AtomicTestCase::MyThread::Entry()
 {
     wxInt32 negativeValuesSeen = 0;
 
 {
     wxInt32 negativeValuesSeen = 0;
 
-    for (wxInt32 i=0; i<10000000; ++i)
+    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
     {
     {
-        switch (m_testType)
+        switch ( m_testType )
         {
         {
-        case AtomicTestCase::IncAndDecMixed:
-            wxAtomicInc(m_operateOn);
-            wxAtomicDec(m_operateOn);
+            case AtomicTestCase::IncAndDecMixed:
+                wxAtomicInc(m_operateOn);
+                wxAtomicDec(m_operateOn);
 
 
-            if (m_operateOn < 0)
-                ++negativeValuesSeen;
-            break;
+                if (m_operateOn < 0)
+                    ++negativeValuesSeen;
+                break;
 
 
-        case AtomicTestCase::IncOnly:
-            wxAtomicInc(m_operateOn);
-            break;
+            case AtomicTestCase::IncOnly:
+                wxAtomicInc(m_operateOn);
+                break;
 
 
-        case AtomicTestCase::DecOnly:
-            wxAtomicDec(m_operateOn);
-            break;
+            case AtomicTestCase::DecOnly:
+                wxAtomicDec(m_operateOn);
+                break;
         }
     }
 
         }
     }
 
-    return (wxThread::ExitCode)negativeValuesSeen;
+    return wxUIntToPtr(negativeValuesSeen);
 }
 }