• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestThread.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Debug.h>
00018 #include <spl/threading/Thread.h>
00019 #include <spl/Log.h>
00020 
00021 #ifdef DEBUG
00022 
00023 static volatile bool flag;
00024 static volatile int count;
00025 
00026 class _TestThread : public Thread
00027 {
00028 protected:
00029         int m_op;
00030         Mutex m_startedMutex;
00031 
00032 public:
00033         _TestThread( int op )
00034         {
00035                 m_op = op;
00036                 m_startedMutex.Lock();
00037         }
00038 
00039         void Run()
00040         {
00041                 m_startedMutex.Unlock();
00042 
00043                 switch ( m_op )
00044                 {
00045                 case 1:
00046                         Op1();
00047                         break;
00048                 case 2:
00049                         Op2();
00050                         break;
00051                 case 3:
00052                         Op3();
00053                         break;
00054                 }
00055         }
00056 
00057         void WaitStarted()
00058         {
00059                 m_startedMutex.Lock();
00060                 m_startedMutex.Unlock();
00061         }
00062 
00063 private:
00064         void Op1()
00065         {
00066                 flag = true;
00067         }
00068 
00069         void Op2()
00070         {
00071                 for ( int x = 0; x < 1000; x++ )
00072                 {
00073                         count++;
00074                 }
00075         }
00076 
00077         void Op3()
00078         {
00079                 for ( int x = 0; x < 100000; x++ )
00080                 {
00081                         Thread::YYield();
00082                         count++;
00083                 }
00084         }
00085 };
00086 
00087 static bool singleTest1()
00088 {
00089         int mcnt = Log::SMessageCount();
00090         flag = false;
00091         count = 0;
00092         _TestThread th(1);
00093         th.Start();
00094         th.Join();
00095         UNIT_ASSERT( "singleTest1", flag == true );
00096         return Log::SMessageCount() == mcnt;
00097 }
00098 
00099 static bool dualTest1()
00100 {
00101         int mcnt = Log::SMessageCount();
00102         count = 0;
00103         flag = false;
00104         _TestThread th1(1);
00105         _TestThread th2(2);
00106         th2.Start();
00107         th1.Start();
00108         th2.Join();
00109         th1.Join();
00110 
00111         UNIT_ASSERT( "dualTest1", flag == true );
00112         UNIT_ASSERT( "dualTest1", count == 1000 );
00113 
00114         return Log::SMessageCount() == mcnt;
00115 }
00116 
00117 static bool killTest()
00118 {
00119         int mcnt = Log::SMessageCount();
00120         flag = false;
00121         count = 0;
00122         _TestThread th(3);
00123         th.Start();
00124         th.WaitStarted();
00125         th.Kill();
00126         th.Join();
00127         UNIT_ASSERT( "killTest", count < 10000 );
00128         return Log::SMessageCount() == mcnt;
00129 }
00130 
00131 bool threadTestHarness()
00132 {
00133         bool ret = singleTest1();
00134         ret |= dualTest1();
00135         ret |= killTest();
00136         return ret;
00137 }
00138 
00139 #endif
00140