TestThread.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00027 
00028 #include "../include/TestThread.h"
00029 #include "../../include/PCMFileSystemManager.h"
00030 
00031 namespace ParCompMarkTest
00032 {
00033 
00034   void TestThread::setUp()
00035   {
00036         std::string currentLogFile = "logs/TestThread.log";
00037 
00038         if(Logger::getInstance() && Logger::getInstance()->getLogFileName() != currentLogFile)
00039          Logger::destroyInstance();
00040 
00041         if(!FileSystemManager::getInstance())
00042         {
00043          FileSystemManager::createInstance();
00044          FileSystemManager::getInstance()->setAppDirectory("./");
00045          FileSystemManager::getInstance()->initialize();
00046         }
00047 
00048         if(!Logger::getInstance())
00049         {
00050          Logger::createInstance();
00051          Logger::getInstance()->setLogFileName(currentLogFile);
00052          Logger::getInstance()->initialize();
00053         }
00054   }
00055 
00056  /*----------------------------------------------------------------------*/
00057 
00058   void TestThread::tearDown()
00059   {
00060   }
00061 
00062  /*----------------------------------------------------------------------*/
00063 
00064   //
00065   // Constructor & destructor tests
00066   //
00067 
00068   void TestThread::test_constructor_cstd__string()
00069   {
00070 
00071         /*
00072          * You have to verify the following:
00073          * 
00074          * Thread constructor.
00075          */
00076 
00077         Thread *t = 0;
00078 
00079         CPPUNIT_ASSERT_NO_THROW(t = new Thread("test_constructor_std__string"));
00080         delete t;
00081   }
00082 
00083  /*----------------------------------------------------------------------*/
00084 
00085   void TestThread::test_destructor()
00086   {
00087         // Tested upper
00088 
00089         Thread *t;
00090 
00091         t = new Thread("test_destructor");
00092         t->initThread(1, 0, true, true);
00093         sleep(2);
00094         CPPUNIT_ASSERT_NO_THROW(delete t);
00095 
00096         t = new Thread("test_destructor");
00097         t->initThread(10, 0, true, true);
00098         CPPUNIT_ASSERT_NO_THROW(delete t);
00099 
00100         t = new Thread("test_destructor");
00101         t->initThread(10, 0, true, false);
00102         CPPUNIT_ASSERT_NO_THROW(delete t);
00103 
00104   }
00105 
00106  /*----------------------------------------------------------------------*/
00107 
00108   //
00109   // Class method tests
00110   //
00111 
00112   void TestThread::test_yield()
00113   {
00114 
00115         /*
00116          * You have to verify the following:
00117          * 
00118          * Yield current thread. 
00119          */
00120 
00121         CPPUNIT_ASSERT_NO_THROW(Thread::yield());
00122   }
00123 
00124  /*----------------------------------------------------------------------*/
00125 
00126   void TestThread::test_getUSTime()
00127   {
00128 
00129         /*
00130          * You have to verify the following:
00131          * 
00132          * Get system time in us.
00133          */
00134 
00135         // No exception and returns positive value
00136         ParCompMark::Real time1;
00137         CPPUNIT_ASSERT_NO_THROW(time1 = Thread::getUSTime());
00138         CPPUNIT_ASSERT(time1 > 0.0);
00139 
00140         usleep(10 * 1000);
00141 
00142         // Monotonic
00143         ParCompMark::Real time2;
00144         CPPUNIT_ASSERT_NO_THROW(time2 = Thread::getUSTime());
00145         CPPUNIT_ASSERT(time2 >= time1);
00146   }
00147 
00148  /*----------------------------------------------------------------------*/
00149 
00150   void TestThread::test_entryPoint_void_p()
00151   {
00152 
00153         /*
00154          * You have to verify the following:
00155          * 
00156          * Static entry point for the thread.
00157          */
00158 
00159         // This method can not be called
00160   }
00161 
00162  /*----------------------------------------------------------------------*/
00163 
00164   //
00165   // Method tests
00166   //
00167 
00168   void TestThread::test_initThread_cu32_cu32_cbool_cbool()
00169   {
00170 
00171         /*
00172          * You have to verify the following:
00173          * 
00174          * Start thread.
00175          */
00176 
00177         Thread *t = new Thread("test_initThread_u32_u32");
00178 
00179         CPPUNIT_ASSERT_NO_THROW(t->initThread());
00180         CPPUNIT_ASSERT_THROW(t->initThread(0, 100, true, true), ParCompMark::Exception);
00181         CPPUNIT_ASSERT_NO_THROW(t->initThread(10, 100, true, true));
00182         CPPUNIT_ASSERT_NO_THROW(t->initThread(10, 100, true, false));
00183         CPPUNIT_ASSERT_NO_THROW(t->initThread(10, 100, false, false));
00184         CPPUNIT_ASSERT_THROW(t->initThread(10, 100, false, true), ParCompMark::Exception);
00185         CPPUNIT_ASSERT(t->mIterationNumber == 10);
00186         CPPUNIT_ASSERT(t->mExpectedFPS == 100);
00187         delete t;
00188   }
00189 
00190  /*----------------------------------------------------------------------*/
00191 
00192   void TestThread::test_threadInitialize()
00193   {
00194 
00195         /*
00196          * You have to verify the following:
00197          * 
00198          * Some init step as thread.
00199          */
00200 
00201         CPPUNIT_FAIL("Implement this test!");
00202   }
00203 
00204  /*----------------------------------------------------------------------*/
00205 
00206   void TestThread::test_threadFinalize()
00207   {
00208 
00209         /*
00210          * You have to verify the following:
00211          * 
00212          * Some finitiate step as thread.
00213          */
00214 
00215         CPPUNIT_FAIL("Implement this test!");
00216   }
00217 
00218  /*----------------------------------------------------------------------*/
00219 
00220   void TestThread::test_startThread()
00221   {
00222 
00223         /*
00224          * You have to verify the following:
00225          * 
00226          * Start thread.
00227          */
00228 
00229         // Killing thread with destructor
00230         Thread *t = new Thread("test_startThread 1");
00231 
00232         t->initThread(100, 0);
00233         CPPUNIT_ASSERT_NO_THROW(t->startThread());
00234         CPPUNIT_ASSERT_NO_THROW(delete t);
00235 
00236         // Let the thread run
00237         t = new Thread("test_startThread 2");
00238         t->initThread(1, 0);
00239         CPPUNIT_ASSERT_NO_THROW(t->startThread());
00240         usleep(500 * 1000);
00241         CPPUNIT_ASSERT_NO_THROW(delete t);
00242 
00243         // wait for thread 
00244         t = new Thread("test_startThread 3");
00245         t->initThread(10, 0, true, true);
00246         t->startThread();
00247         CPPUNIT_ASSERT_NO_THROW(delete t);
00248 
00249         t = new Thread("test_startThread 3");
00250         t->initThread(10, 0, true, false);
00251         t->startThread();
00252         CPPUNIT_ASSERT_NO_THROW(delete t);
00253 
00254   }
00255 
00256  /*----------------------------------------------------------------------*/
00257 
00258   void TestThread::test_joinThread()
00259   {
00260 
00261         /*
00262          * You have to verify the following:
00263          * 
00264          * Join thread. Wait for its ending.
00265          * !!! You can join if the thread will stop (mIterationNumber != 0 or 
00266          * mStopRequested = true).
00267          * 
00268          */
00269         Thread *t;
00270 
00271         t = new Thread("test_joinThread");
00272         t->initThread(10, 0, true, false);
00273         t->startThread();
00274         CPPUNIT_ASSERT_NO_THROW(delete t);
00275 
00276         t = new Thread("test_joinThread");
00277         t->initThread(10, 0, true, false);
00278         t->startThread();
00279         t->joinThread();
00280         CPPUNIT_ASSERT_NO_THROW(delete t);
00281 
00282         //join but thread not run
00283         t = new Thread("test_joinThread");
00284         t->initThread(1, 0, true, false);
00285         t->startThread();
00286         sleep(2);
00287         CPPUNIT_ASSERT_NO_THROW(t->joinThread());
00288         CPPUNIT_ASSERT_NO_THROW(delete t);
00289 
00290         t = new Thread("test_joinThread");
00291         t->initThread(0, 0, true, false);
00292         t->startThread();
00293         t->stopThread();
00294         CPPUNIT_ASSERT_NO_THROW(t->joinThread());
00295         CPPUNIT_ASSERT_NO_THROW(delete t);
00296 
00297         t = new Thread("test_joinThread");
00298         t->initThread(0, 0, true, false);
00299         t->startThread();
00300         t->stopThread();
00301         sleep(1);
00302         CPPUNIT_ASSERT_NO_THROW(t->joinThread());
00303         CPPUNIT_ASSERT_NO_THROW(delete t);
00304 
00305   }
00306 
00307  /*----------------------------------------------------------------------*/
00308 
00309   void TestThread::test_shutDownThread()
00310   {
00311 
00312         /*
00313          * You have to verify the following:
00314          * 
00315          * Immediately stop the thread.
00316          */
00317 
00318         // Tested in test_startThread with destructor
00319   }
00320 
00321  /*----------------------------------------------------------------------*/
00322 
00323   void TestThread::test_stopThread()
00324   {
00325 
00326         /*
00327          * You have to verify the following:
00328          * 
00329          * Request thread shut down.
00330          */
00331 
00332         // Killing thread with destructor
00333         Thread *t = new Thread("test_stopThread");
00334 
00335         t->initThread(1000, 0);
00336         CPPUNIT_ASSERT_NO_THROW(t->startThread());
00337         usleep(10 * 1000);
00338         CPPUNIT_ASSERT_NO_THROW(t->stopThread());
00339         usleep(500 * 1000);
00340         CPPUNIT_ASSERT_NO_THROW(delete t);
00341   }
00342 
00343  /*----------------------------------------------------------------------*/
00344 
00345   void TestThread::test_go()
00346   {
00347 
00348         /*
00349          * You have to verify the following:
00350          * 
00351          * Go forward from wait.
00352          */
00353 
00354         CPPUNIT_FAIL("Implement this test!");
00355   }
00356 
00357  /*----------------------------------------------------------------------*/
00358 
00359   void TestThread::test_thread()
00360   {
00361 
00362         /*
00363          * You have to verify the following:
00364          * 
00365          * This is the thread method containing one or more (or inifinite) iterations.
00366          */
00367 
00368         // Tested above
00369   }
00370 
00371  /*----------------------------------------------------------------------*/
00372 
00373   void TestThread::test_iteration()
00374   {
00375 
00376         /*
00377          * You have to verify the following:
00378          * 
00379          * One iteration step. This method calls the overridable task method.
00380          */
00381 
00382         // Tested above
00383   }
00384 
00385  /*----------------------------------------------------------------------*/
00386 
00387   void TestThread::test_task()
00388   {
00389 
00390         /*
00391          * You have to verify the following:
00392          * 
00393          * It is the task of the thread. It have to be overiden.
00394          * This method should be abstact. It is not, because of unit testing of this class.
00395          * 
00396          */
00397 
00398         // No need to test
00399   }
00400 
00401  /*----------------------------------------------------------------------*/
00402 
00403   void TestThread::test_wait()
00404   {
00405 
00406         /*
00407          * You have to verify the following:
00408          * 
00409          * Wait for something.
00410          */
00411 
00412         CPPUNIT_FAIL("Implement this test!");
00413   }
00414 
00415  /*----------------------------------------------------------------------*/
00416 
00417   // To register the suite we add:
00419   CPPUNIT_TEST_SUITE_REGISTRATION(TestThread);
00420 }