]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/thread/misc.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[wxWidgets.git] / tests / thread / misc.cpp
index d2eb6aacb55d22b334c7b9a0eb9ad213b13c394f..d637748bda158e84ae2ed4c37e25b6ee83feae57 100644 (file)
@@ -3,7 +3,6 @@
 // Purpose:     Miscellaneous wxThread test cases
 // Author:      Francesco Montorsi (extracted from console sample)
 // Created:     2010-05-10
-// RCS-ID:      $Id$
 // Copyright:   (c) 2010 wxWidgets team
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -21,7 +20,7 @@
 #endif // WX_PRECOMP
 
 #include "wx/thread.h"
-#include "wx/tls.h"
+#include "wx/utils.h"
 
 // ----------------------------------------------------------------------------
 // globals
@@ -105,7 +104,7 @@ wxThread::ExitCode MyDetachedThread::Entry()
         //wxPutchar(m_ch);
         //fflush(stdout);
 
-        wxThread::Sleep(100);
+        wxMilliSleep(100);
     }
 
     return 0;
@@ -168,6 +167,8 @@ public:
 
     virtual ExitCode Entry()
     {
+        wxUnusedVar(m_i);
+
         //wxPrintf(wxT("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
         //         wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
 
@@ -208,6 +209,7 @@ private:
         CPPUNIT_TEST( TestDetached );
         CPPUNIT_TEST( TestThreadSuspend );
         CPPUNIT_TEST( TestThreadDelete );
+        CPPUNIT_TEST( TestThreadRun );
         CPPUNIT_TEST( TestThreadConditions );
         CPPUNIT_TEST( TestSemaphore );
     CPPUNIT_TEST_SUITE_END();
@@ -218,6 +220,7 @@ private:
 
     void TestThreadSuspend();
     void TestThreadDelete();
+    void TestThreadRun();
     void TestThreadConditions();
 
     DECLARE_NO_COPY_CLASS(MiscThreadTestCase)
@@ -226,7 +229,7 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( MiscThreadTestCase );
 
-// 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( MiscThreadTestCase, "MiscThreadTestCase" );
 
 MiscThreadTestCase::MiscThreadTestCase()
@@ -255,8 +258,8 @@ void MiscThreadTestCase::TestDetached()
         threads[n] = new MyDetachedThread(10, 'A' + n);
     }
 
-    threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
-    threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
+    threads[0]->SetPriority(wxPRIORITY_MIN);
+    threads[1]->SetPriority(wxPRIORITY_MAX);
 
     for ( n = 0; n < nThreads; n++ )
     {
@@ -299,7 +302,7 @@ void MiscThreadTestCase::TestThreadSuspend()
     // bit and hope that it will be enough (the problem is, of course, that
     // the thread might still not run when we call Pause() which will result
     // in an error)
-    wxThread::Sleep(300);
+    wxMilliSleep(300);
 
     for ( size_t n = 0; n < 3; n++ )
     {
@@ -308,7 +311,7 @@ void MiscThreadTestCase::TestThreadSuspend()
         if ( n > 0 )
         {
             // don't sleep but resume immediately the first time
-            wxThread::Sleep(300);
+            wxMilliSleep(300);
         }
 
         CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread->Resume() );
@@ -320,6 +323,7 @@ void MiscThreadTestCase::TestThreadSuspend()
 
 void MiscThreadTestCase::TestThreadDelete()
 {
+    // FIXME:
     // As above, using Sleep() is only for testing here - we must use some
     // synchronisation object instead to ensure that the thread is still
     // running when we delete it - deleting a detached thread which already
@@ -331,13 +335,13 @@ void MiscThreadTestCase::TestThreadDelete()
 
     MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1->Run() );
-    wxThread::Sleep(300);
+    wxMilliSleep(300);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1->Delete() );      
         // delete a running thread
 
     MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Run() );
-    wxThread::Sleep(300);
+    wxMilliSleep(300);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Pause() );
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Delete() );      
         // delete a sleeping thread
@@ -345,15 +349,25 @@ void MiscThreadTestCase::TestThreadDelete()
     MyJoinableThread thread3(20);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Run() );
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Delete() );       
-        // delete a joinable thread
+        // delete a joinable running thread
 
     MyJoinableThread thread4(2);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Run() );
-    wxThread::Sleep(300);
+    wxMilliSleep(300);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Delete() );       
         // delete a joinable thread which already terminated
 }
 
+void MiscThreadTestCase::TestThreadRun()
+{
+    MyJoinableThread thread1(2);
+    CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1.Run() );
+    thread1.Wait();     // wait until the thread ends
+
+    // verify that running twice the same thread fails
+    WX_ASSERT_FAILS_WITH_ASSERT( thread1.Run() );
+}
+
 void MiscThreadTestCase::TestThreadConditions()
 {
     wxMutex mutex;
@@ -390,18 +404,18 @@ void MiscThreadTestCase::TestThreadConditions()
         // note that main thread is already running
     }
 
-    wxThread::Sleep(500);
+    wxMilliSleep(500);
 
 #if 1
     // now wake one of them up
     CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Signal() );
 #endif
 
-    wxThread::Sleep(200);
+    wxMilliSleep(200);
 
     // wake all the (remaining) threads up, so that they can exit
     CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Broadcast() );
 
     // give them time to terminate (dirty!)
-    wxThread::Sleep(500);
+    wxMilliSleep(500);
 }