GuardTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Guard_cpp
00018 #define Guard_cpp
00019 
00020 #include <iostream>
00021 #include <iomanip>
00022 #include <string>
00023 #include <stdio.h>
00024 //#include <unistd.h>
00025 #include <time.h>
00026 #include <cppunit/ui/text/TestRunner.h>
00027 #include <cppunit/TextOutputter.h>
00028 #include <cppunit/extensions/TestFactoryRegistry.h>
00029 #include <cppunit/extensions/HelperMacros.h>
00030 #include <cppunit/TestAssert.h>
00031 
00032 //#include <../../include/coil/Guard.h>
00033 #include <coil/Guard.h>
00034 #include <coil/Task.h>
00035 
00040 namespace Guard
00041 {
00042   class GuardTests
00043    : public CppUnit::TestFixture ,
00044      public coil::Task 
00045   {
00046     CPPUNIT_TEST_SUITE(GuardTests);
00047     CPPUNIT_TEST(test_case0);
00048     CPPUNIT_TEST_SUITE_END();
00049   
00050   private:
00051 
00052   public:
00053     coil::Mutex mtx;
00054 //    long sharecount;
00055     class TestGuardTask
00056        : public coil::Task 
00057     {
00058     public:
00059       static long ShareCount;
00060       static coil::Mutex mtx;
00061       int svc(void)
00062       {
00063         clock_t start,end;
00064             long lc;
00065         long lbuf;
00066         coil::Guard<coil::Mutex> guard(mtx);
00067         for(lc=0; lc<100; lc++)
00068         {
00069           lbuf = ShareCount;
00070           start = clock();
00071           for(;;)
00072           {
00073             end = clock();
00074             if((end-start)>1)
00075                         {
00076               break;
00077             }
00078           }
00079                   lbuf++;
00080           ShareCount = lbuf;
00081         }
00082         return 0;
00083       }
00084 
00085       long getShareCount()
00086       {
00087           return ShareCount;
00088       }
00089       void setShareCount(long lc)
00090       {
00091           coil::Guard<coil::Mutex> guard(mtx);
00092           ShareCount = lc;
00093       }
00094     };
00095 
00099     GuardTests()
00100     {
00101 //        sharecount = 0;
00102     }
00103     
00107     ~GuardTests()
00108     {
00109     }
00110   
00114     virtual void setUp()
00115     {
00116     }
00117     
00121     virtual void tearDown()
00122     { 
00123     }
00124   
00125     /* test case */
00126     int svc(void)
00127     {
00128       return 0;
00129     }
00130     /*
00131     ---------------------------------------------------------------------------
00132     This function tests coil::Guard.
00133     Check exclusive control,when two threads access static variable.
00134     ---------------------------------------------------------------------------
00135     */
00136     void test_case0()
00137     {
00138       GuardTests::TestGuardTask tka;
00139       GuardTests::TestGuardTask tkb;
00140       tka.setShareCount(0);
00141       long lc;
00142       char cstr[256];
00143 
00144       tka.activate();
00145       tkb.activate();        
00146       tka.wait();
00147       tkb.wait();
00148       lc = tka.getShareCount(); 
00149       sprintf(cstr, "sharecount:%ld", lc); 
00150       CPPUNIT_ASSERT_MESSAGE(cstr, (lc== 200) );
00151 
00152     }
00153   };
00154   long GuardTests::TestGuardTask::ShareCount = 0L;
00155   coil::Mutex GuardTests::TestGuardTask::mtx ;
00156 
00157 }; // namespace Guard
00158 
00159 /*
00160  * Register test suite
00161  */
00162 CPPUNIT_TEST_SUITE_REGISTRATION(Guard::GuardTests);
00163 
00164 #ifdef LOCAL_MAIN
00165 int main(int argc, char* argv[])
00166 {
00167     CppUnit::TextUi::TestRunner runner;
00168     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00169     CppUnit::Outputter* outputter = 
00170       new CppUnit::TextOutputter(&runner.result(), std::cout);
00171     runner.setOutputter(outputter);
00172     bool retcode = runner.run();
00173     return !retcode;
00174 }
00175 #endif // MAIN
00176 #endif // Guard_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:04