TaskTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Task_cpp
00018 #define Task_cpp
00019 
00020 #include <iostream>
00021 #include <iomanip>
00022 #include <string>
00023 #include <stdio.h>
00024 #include <time.h>
00025 #include <cppunit/ui/text/TestRunner.h>
00026 #include <cppunit/TextOutputter.h>
00027 #include <cppunit/extensions/TestFactoryRegistry.h>
00028 #include <cppunit/extensions/HelperMacros.h>
00029 #include <cppunit/TestAssert.h>
00030 
00031 //#include <../../include/coil/Task.h>
00032 #include <coil/Task.h>
00033 #include <coil/Time.h>
00034 
00039 namespace Task
00040 {
00041   class TaskTests
00042    : public CppUnit::TestFixture , 
00043      public coil::Task
00044   {
00045     CPPUNIT_TEST_SUITE(TaskTests);
00046 //    CPPUNIT_TEST(test_case0);
00047     CPPUNIT_TEST(test_open_close);
00048     CPPUNIT_TEST(test_activate);
00049     CPPUNIT_TEST(test_activate2);
00050     CPPUNIT_TEST(test_wait);
00051     CPPUNIT_TEST(test_suspend);
00052     CPPUNIT_TEST(test_resume);
00053     CPPUNIT_TEST_SUITE_END();
00054   
00055   private:
00056     bool m_statflag;
00057     short m_tasknumber;
00058     short m_threadcmd;
00059     short m_threadcnt[256];
00060   public:
00061     coil::Task * testtk;  
00065     TaskTests()
00066     {
00067         short ic;
00068         for (ic=0; ic<256; ic++)
00069         {
00070             m_threadcnt[ic] = 0;
00071         }
00072         m_statflag = false;
00073         m_tasknumber =0;
00074         m_threadcmd = 0;
00075     }
00076     
00080     ~TaskTests()
00081     {
00082     }
00083   
00087     virtual void setUp()
00088     {
00089     }
00090     
00094     virtual void tearDown()
00095     { 
00096     }
00097   
00098     /*
00099     ---------------------------------------------------------------------------
00100     ---------------------------------------------------------------------------
00101     */
00102     int svc(void)
00103     {
00104         short ic;
00105         m_statflag = true;
00106         switch(m_threadcmd)
00107         {
00108             case 0:
00109                 std::cout<<"/"<<std::endl;
00110                 m_tasknumber ++;
00111                 for(;;)
00112                 {  
00113                   if(m_statflag != true)
00114                   {
00115                     break;
00116                   }
00117                   m_threadcnt[m_tasknumber-1]++;
00118                 }
00119                 break;
00120             case 1:
00121                 std::cout<<"/"<<std::endl;
00122                 for(ic=0;ic<10;ic++){
00123                   ;;
00124                 }
00125                 break;
00126             default:
00127                 break;
00128         }
00129         return 0;
00130     }
00131     /* test case */
00132     void test_case0()
00133     {
00134     }
00135     /*
00136     ---------------------------------------------------------------------------
00137     This function tests the Task::open function and the Task::close function.
00138     Check that the open function and the close function return 0.
00139     ---------------------------------------------------------------------------
00140     */
00141     void test_open_close()
00142     {
00143         int iret;
00144         iret = 1;
00145         iret = open(0);
00146         CPPUNIT_ASSERT_MESSAGE("open", (iret == 0) );
00147 
00148         iret = 1;
00149         iret = close(0);
00150         CPPUNIT_ASSERT_MESSAGE("close", (iret == 0) );
00151 
00152     }
00153     /*
00154     ---------------------------------------------------------------------------
00155     This function tests the Task::activate function.
00156     Check that the thread makes only one even if the activate function is
00157     called two or more times.
00158     ---------------------------------------------------------------------------
00159     */
00160     void test_activate()
00161     {
00162         
00163         time_t tmstart, tmend;
00164                 char cstr[256];
00165         short ic;
00166         if ( m_statflag == true )
00167         {
00168             m_statflag = false;
00169         }
00170         m_threadcmd = 0;
00171         m_tasknumber = 0;
00172         //Start 10 threads. & Check that only 1 thread start.
00173         for (ic=0; ic<10; ic++)
00174         {
00175             //Start a thread. 
00176             activate();
00177             time(&tmstart);
00178             for(;;)
00179             {
00180               time(&tmend);
00181               if(difftime(tmend,tmstart)>=1.0)
00182               {
00183                 break;
00184               }
00185             }
00186             sprintf(cstr, "counter:%d ", m_tasknumber);
00187             //Check that a thread start.
00188             CPPUNIT_ASSERT_MESSAGE(cstr , (m_tasknumber == 1) );
00189         }
00190         m_statflag = false;
00191         wait();
00192 
00193     }
00199     void test_activate2()
00200     {
00201         
00202         time_t tmstart, tmend;
00203                 char cstr[256];
00204         short ic;
00205         if ( m_statflag == true )
00206         {
00207             m_statflag = false;
00208         }
00209         m_threadcmd = 0;
00210         m_tasknumber = 0;
00211         //Start 10 threads. & Check that 10 thread start.
00212         for (ic=0; ic<10; ic++)
00213         {
00214             //Start a thread. 
00215             activate();
00216             time(&tmstart);
00217             for(;;)
00218             {
00219               time(&tmend);
00220               if(difftime(tmend,tmstart)>=1.0)
00221               {
00222                 break;
00223               }
00224             }
00225             sprintf(cstr, "m_tasknumber:%d (ic+1):%d", m_tasknumber,ic+1);
00226             //Check that a thread start.
00227             CPPUNIT_ASSERT_MESSAGE(cstr , (m_tasknumber == ic+1) );
00228             m_statflag = false;
00229             wait();
00230         }
00231 
00232     }
00233     /*
00234     ---------------------------------------------------------------------------
00235     ---------------------------------------------------------------------------
00236     */
00237     void test_wait()
00238     {
00239         wait(); //If Segmentation fault is not caused, it is OK.
00240         m_threadcmd = 1;        
00241         activate();
00242         wait();
00243     }
00244     /*
00245     ---------------------------------------------------------------------------
00246     This function tests the Task::suspend function.
00247     Check that the suspend function returns 0.
00248     ---------------------------------------------------------------------------
00249     */
00250     void test_suspend()
00251     {
00252         int iret;
00253         iret = 1;
00254         iret = suspend();
00255         CPPUNIT_ASSERT_MESSAGE("suspend", (iret == 0) );
00256     }
00257     /*
00258     ---------------------------------------------------------------------------
00259     This function tests the Task::resume function.
00260     Check that the resume function returns 0.
00261     ---------------------------------------------------------------------------
00262     */
00263     void test_resume()
00264     {
00265         int iret;
00266         iret = 1;
00267         iret = resume();
00268         CPPUNIT_ASSERT_MESSAGE("resume", (iret == 0) );
00269     }
00270   };
00271 }; // namespace Task
00272 
00273 /*
00274  * Register test suite
00275  */
00276 CPPUNIT_TEST_SUITE_REGISTRATION(Task::TaskTests);
00277 
00278 #ifdef LOCAL_MAIN
00279 int main(int argc, char* argv[])
00280 {
00281     CppUnit::TextUi::TestRunner runner;
00282     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00283     CppUnit::Outputter* outputter = 
00284       new CppUnit::TextOutputter(&runner.result(), std::cout);
00285     runner.setOutputter(outputter);
00286     bool retcode = runner.run();
00287     return !retcode;
00288 }
00289 #endif // MAIN
00290 #endif // Task_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39