TimeTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Time_cpp
00018 #define Time_cpp
00019 
00020 #include <iostream>
00021 #include <iomanip>
00022 #include <string>
00023 #include <sstream>
00024 #include <stdio.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 <coil/Time.h>
00033 #include <coil/Task.h>
00034 #include <coil/TimeValue.h>
00035 
00036 
00041 namespace Time
00042 {
00043   class TimeTests
00044    : public CppUnit::TestFixture ,
00045      public coil::Task
00046   {
00047     CPPUNIT_TEST_SUITE(TimeTests);
00048     CPPUNIT_TEST(test_sleep);
00049 //    CPPUNIT_TEST(test_sleep1);
00050 //    CPPUNIT_TEST(test_sleep2);
00051 //    CPPUNIT_TEST(test_sleep3);
00052     CPPUNIT_TEST(test_usleep);
00053 //    CPPUNIT_TEST(test_usleep1);
00054     CPPUNIT_TEST(test_gettimeofday);
00055     CPPUNIT_TEST_SUITE_END();
00056   
00057   private:
00058   public:
00059     class TestTimeTask
00060        : public coil::Task 
00061     {
00062     public:
00063       short m_count_time;
00064       short m_svccmd;
00065       short m_mainstop;
00066       short m_svcstop;
00067       int svc(void)
00068       {
00069         coil::TimeValue tv(0,1000000);
00070         unsigned int usec;
00071         unsigned int utime;
00072         short ic;
00073         switch(m_svccmd)
00074         {
00075           case 0:
00076             utime = 1;                   //1sec          
00077                     for (ic=0; ic<10; ic++)
00078             {
00079                           coil::sleep(utime);        //unsigned int sleep(unsigned int seconds)
00080               m_count_time++;
00081 //              std::cout<<m_count_time<<std::endl;
00082               if (m_svcstop==1)
00083               {
00084                 m_svcstop = 0;
00085                 break;
00086               }
00087             }
00088             m_mainstop = 1;
00089             break;
00090           case 1:
00091 //            tv = 1.0;                         //1s
00092                     for (ic=0; ic<10; ic++)
00093             {
00094                           coil::sleep(tv);                //int sleep(TimeValue& interval)
00095                           m_count_time++;
00096 //              std::cout<<m_count_time<<std::endl;
00097               if (m_svcstop==1)
00098               {
00099                 m_svcstop = 0;
00100                 break;
00101               }
00102             }
00103             m_mainstop = 1;
00104             break;
00105           case 2:
00106             usec = 1000000;                      //1s
00107                     for (ic=0; ic<10; ic++)
00108             {
00109                           coil::usleep(usec);                //int usleep(useconds_t usec)
00110               m_count_time++;
00111 //              std::cout<<m_count_time<<std::endl;
00112               if (m_svcstop==1)
00113               {
00114                 m_svcstop = 0;
00115                 break;
00116               }
00117             }
00118             m_mainstop = 1;
00119             break;
00120           case 3:
00121             tv = 0.1;                         //100ms
00122                     for (ic=0; ic<100; ic++)
00123             {
00124                           coil::sleep(tv);                //int sleep(TimeValue& interval)
00125                           m_count_time++;
00126 //              std::cout<<m_count_time<<std::endl;
00127               if (m_svcstop==1)
00128               {
00129                 m_svcstop = 0;
00130                 break;
00131               }
00132             }
00133             m_mainstop = 1;
00134             break;
00135           default:
00136            break;
00137         }
00138         return 0;
00139       }
00140     };  
00144     TimeTests()
00145     {
00146     }
00147     
00151     ~TimeTests()
00152     {
00153     }
00154   
00158     virtual void setUp()
00159     {
00160     }
00161     
00165     int svc(void)
00166     {
00167       return 0;
00168     }
00169     virtual void tearDown()
00170     { 
00171     }
00172   
00173     /* test case */
00174     /*
00175     ---------------------------------------------------------------------------
00176     This function tests coil::sleep. 
00177      1.unsigned int sleep(unsigned int seconds)
00178      2.int sleep(TimeValue& interval)
00179     Check that it measures for ten seconds by the time function and coil::sleep(1) 
00180     ends ten times between those. 
00181     ---------------------------------------------------------------------------
00182     */
00183     void test_sleep()
00184     {
00185 
00186       time_t tmstart, tmend;
00187       short ic, icnt;
00188       char cstr[256];
00189       TimeTests::TestTimeTask *tta;
00190 
00191       tta = new TimeTests::TestTimeTask;
00192 
00193       tta->m_mainstop = 0;
00194       tta->m_svcstop = 0;
00195       tta->m_svccmd = 0;
00196       tta->m_count_time = 0;
00197       tta->activate();
00198 
00199       for(ic=0; ic<10; ic++)
00200       {
00201         ::time(&tmstart);
00202         for(;;)
00203         {
00204           ::time(&tmend);
00205                   if(::difftime(tmend,tmstart)>=1.0)
00206                   {
00207             break;
00208                   }
00209             }
00210         if(tta->m_mainstop == 1)
00211         {
00212           break;
00213         }
00214       }
00215       tta->m_svcstop = 1;
00216       tta->wait();
00217       icnt = tta->m_count_time;
00218       delete tta;
00219       sprintf(cstr,"main:%d svc:%d", ic, icnt);
00220       //カウンタをチェック
00221       CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
00222 
00223       tta = new TimeTests::TestTimeTask;
00224       tta->m_svcstop = 0;
00225       tta->m_mainstop = 0;
00226       tta->m_svccmd = 1;
00227       tta->m_count_time = 0;
00228       tta->activate();
00229       for(ic=0; ic<10; ic++)
00230       {
00231         ::time(&tmstart);
00232         for(;;)
00233         {
00234           ::time(&tmend);
00235                   if(::difftime(tmend,tmstart)>=1.0)
00236                   {
00237             break;
00238                   }
00239             }
00240         if(tta->m_mainstop == 1)
00241         {
00242           break;
00243         }
00244       }
00245       tta->m_svcstop = 1;
00246       tta->wait();
00247       icnt = tta->m_count_time;
00248       delete tta;
00249       sprintf(cstr,"main:%d svc:%d", ic, icnt);
00250       //カウンタをチェック
00251       CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
00252     }
00253     /*
00254     ---------------------------------------------------------------------------
00255     This function was made for the debugging of win32. 
00256     Time is measured by using the clock function. 
00257     ---------------------------------------------------------------------------
00258     */
00259     void test_sleep1()
00260     {
00261           clock_t start, end;
00262       coil::TimeValue tv;
00263       TimeTests::TestTimeTask *tta;
00264       double dbcnt;
00265       char cstr[256];
00266 
00267       tta = new TimeTests::TestTimeTask;
00268 
00269       tta->m_mainstop = 0;
00270       tta->m_svcstop = 0;
00271       tta->m_svccmd = 0;
00272       tta->m_count_time = 0;
00273       tta->activate();
00274       start = ::clock();
00275       tta->wait();
00276       end = ::clock();
00277       delete tta;
00278       dbcnt = double(end - start) / CLOCKS_PER_SEC;
00279       sprintf(cstr,"time:%f", dbcnt);
00280       std::cout<<cstr<<std::endl;
00281       CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
00282 
00283       tta = new TimeTests::TestTimeTask;
00284 
00285       tta->m_svcstop = 0;
00286       tta->m_mainstop = 0;
00287       tta->m_svccmd = 1;
00288       tta->m_count_time = 0;
00289       tta->activate();
00290       start = ::clock();
00291       tta->wait();
00292       end = ::clock();
00293       delete tta;
00294       dbcnt = double(end - start) / CLOCKS_PER_SEC;
00295       sprintf(cstr,"time:%f", dbcnt);
00296       std::cout<<cstr<<std::endl;
00297       CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
00298     }
00299     /*
00300     ---------------------------------------------------------------------------
00301     This function was made for the debugging of win32. 
00302     Time is measured by using the clock function. 
00303     ---------------------------------------------------------------------------
00304     */
00305     void test_sleep2()
00306     {
00307           clock_t start, end;
00308       coil::TimeValue tv;
00309       TimeTests::TestTimeTask *tta;
00310       double dbcnt;
00311       char cstr[256];
00312 
00313       tta = new TimeTests::TestTimeTask;
00314 
00315       tta->m_mainstop = 0;
00316       tta->m_svcstop = 0;
00317       tta->m_svccmd = 3;
00318       tta->m_count_time = 0;
00319       tta->activate();
00320       start = ::clock();
00321       tta->wait();
00322       end = ::clock();
00323       delete tta;
00324       dbcnt = double(end - start) / CLOCKS_PER_SEC;
00325       sprintf(cstr,"time:%f", dbcnt);
00326       std::cout<<cstr<<std::endl;
00327       CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
00328 
00329     }
00330     /*
00331     ---------------------------------------------------------------------------
00332     Measurement at time of sleep(TimeValue& interval)
00333     ---------------------------------------------------------------------------
00334     */
00335     void test_sleep3()
00336     {
00337       
00338           clock_t start, end;
00339       coil::TimeValue tv;
00340       double dbcnt;
00341       double dbcntmax,dbcntmin,dbcntave;
00342       short ic,icc;
00343 
00344       //0.0
00345       tv = 0;
00346       for(icc=0;icc<=10;icc++)
00347       {
00348         dbcntave = 0.0;
00349         dbcntmax = 0.0;
00350         dbcntmin = 10.0;
00351         for(ic=0;ic<100;ic++)
00352         {
00353           start = ::clock();
00354           coil::sleep(tv);
00355           end = ::clock();
00356           dbcnt = double(end - start) / CLOCKS_PER_SEC;
00357           dbcntave = dbcntave + dbcnt;
00358           if(dbcnt>dbcntmax)
00359           {
00360             dbcntmax = dbcnt;
00361           }
00362           if(dbcnt<dbcntmin)
00363           {
00364             dbcntmin = dbcnt;
00365           }
00366         }
00367         std::cout<<"sleep :"<<tv<<std::endl;
00368         std::cout<<"sleep time ave :"<<dbcntave/100<<std::endl;
00369         std::cout<<"sleep time max :"<<dbcntmax<<std::endl;
00370         std::cout<<"sleep time min :"<<dbcntmin<<std::endl;
00371         tv = tv + 0.01;
00372       }
00373     }
00374     /*
00375     ---------------------------------------------------------------------------
00376     This function tests coil::usleep. 
00377     Check that it measures for ten seconds by the time function and coil::usleep(1000000)
00378     ends ten times between those. 
00379     ---------------------------------------------------------------------------
00380     */
00381     void test_usleep()
00382     {
00383 
00384       time_t tmstart, tmend;
00385       coil::TimeValue tv;
00386       short ic,icnt;
00387       char cstr[256];
00388       TimeTests::TestTimeTask *tta;
00389 
00390       tta = new TimeTests::TestTimeTask;
00391       
00392       tta->m_svccmd = 2;
00393       tta->m_count_time = 0;
00394       tta->m_svcstop = 0;
00395       tta->m_mainstop = 0;
00396       tta->activate();
00397       for(ic=0; ic<10; ic++)
00398       {
00399         ::time(&tmstart);
00400         for(;;)
00401         {
00402           ::time(&tmend);
00403                   if(::difftime(tmend,tmstart)>=1.0)
00404                   {
00405             break;
00406                   }
00407             }
00408         if(tta->m_mainstop == 1)
00409         {
00410           break;
00411         }
00412       }
00413       tta->m_svcstop = 1;
00414       tta->wait();
00415       icnt = tta->m_count_time;
00416       delete tta;
00417       sprintf(cstr,"main:%d svc:%d", ic, icnt);
00418       //カウンタをチェック
00419       CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
00420     }
00421     /*
00422     ---------------------------------------------------------------------------
00423     This function was made for the debugging of win32. 
00424     Time is measured by using the clock function. 
00425     ---------------------------------------------------------------------------
00426     */
00427     void test_usleep1()
00428     {
00429 
00430           clock_t start, end;
00431       double dbcnt;
00432       char cstr[256];
00433 
00434       TimeTests::TestTimeTask *tta;
00435 
00436       tta = new TimeTests::TestTimeTask;
00437       
00438       tta->m_svccmd = 2;
00439       tta->m_count_time = 0;
00440       tta->m_svcstop = 0;
00441       tta->m_mainstop = 0;
00442       tta->activate();
00443       start = ::clock();
00444       tta->wait();
00445       end = ::clock();
00446       delete tta;
00447       dbcnt = double(end - start) / CLOCKS_PER_SEC;
00448       sprintf(cstr,"time:%f", dbcnt);
00449       std::cout<<cstr<<std::endl;
00450       CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
00451     }
00452     /*
00453     ---------------------------------------------------------------------------
00454     This function tests coil::gettimeofday().
00455     ---------------------------------------------------------------------------
00456     */
00457     void test_gettimeofday()
00458     {
00459       long l1,l2;
00460           time_t tmstart, tmend;
00461           clock_t start, end;
00462       coil::TimeValue tv;
00463       char cstr[256];
00464       //check sec
00465 
00466       //get time
00467       tv = coil::gettimeofday();
00468       l1 = tv.sec();
00469       // 10sec wait
00470           ::time(&tmstart);
00471       for(;;)
00472       {
00473         ::time(&tmend);
00474                 if(::difftime(tmend,tmstart)>=10)
00475                 {
00476           break;
00477                 }
00478           }
00479       //get time
00480       tv = coil::gettimeofday();
00481       l2 = tv.sec();
00482       //check
00483           sprintf(cstr ,"sec%ld ",l2-l1);
00484       std::cout<<cstr<<std::endl;
00485       CPPUNIT_ASSERT_MESSAGE( cstr, (l2- l1)==10 );
00486 
00487           //check usec
00488       //get time
00489           tv = coil::gettimeofday();
00490       l1 = tv.usec();
00491       //100msec wait
00492       start = ::clock();
00493       for(;;)
00494       {
00495         end = ::clock();
00496         if((double)(end-start)/CLOCKS_PER_SEC>0.1)
00497             {
00498           break;
00499         }
00500       }
00501       //get time
00502       tv = coil::gettimeofday();
00503       l2 = tv.usec();
00504       //check usec
00505           sprintf(cstr ,"usec%ld ",l2-l1);
00506       std::cout<<cstr<<std::endl;
00507       CPPUNIT_ASSERT_MESSAGE( cstr, ((l2 - l1)>=80000) & ((l2 - l1)<=120000) );
00508 
00509     }
00510   };
00511 }; // namespace Time
00512 
00513 /*
00514  * Register test suite
00515  */
00516 CPPUNIT_TEST_SUITE_REGISTRATION(Time::TimeTests);
00517 
00518 #ifdef LOCAL_MAIN
00519 int main(int argc, char* argv[])
00520 {
00521   FORMAT format = TEXT_OUT;
00522   int target = 0;
00523   std::string xsl;
00524   std::string ns;
00525   std::string fname;
00526   std::ofstream ofs;
00527 
00528   int i(1);
00529   while (i < argc)
00530     {
00531       std::string arg(argv[i]);
00532       std::string next_arg;
00533       if (i + 1 < argc) next_arg = argv[i + 1];
00534       else              next_arg = "";
00535 
00536       if (arg == "--text") { format = TEXT_OUT; break; }
00537       if (arg == "--xml")
00538         {
00539           if (next_arg == "")
00540             {
00541               fname = argv[0];
00542               fname += ".xml";
00543             }
00544           else
00545             {
00546               fname = next_arg;
00547             }
00548           format = XML_OUT;
00549           ofs.open(fname.c_str());
00550         }
00551       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00552   if ( arg == "--cerr"      ) { target = 1; break; }
00553       if ( arg == "--xsl"       )
00554         {
00555           if (next_arg == "") xsl = "default.xsl";
00556           else                xsl = next_arg;
00557         }
00558   if ( arg == "--namespace" )
00559         {
00560           if (next_arg == "")
00561             {
00562               std::cerr << "no namespace specified" << std::endl;
00563               exit(1);
00564             }
00565           else
00566             {
00567               xsl = next_arg;
00568             }
00569     }
00570       ++i;
00571     }
00572   CppUnit::TextUi::TestRunner runner;
00573   if ( ns.empty() )
00574     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00575   else
00576     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00577   CppUnit::Outputter* outputter = 0;
00578   std::ostream* stream = target ? &std::cerr : &std::cout;
00579   switch ( format )
00580     {
00581     case TEXT_OUT :
00582       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00583       break;
00584     case XML_OUT :
00585   std::cout << "XML_OUT" << std::endl;
00586       outputter = new CppUnit::XmlOutputter(&runner.result(),
00587                                             ofs, "shift_jis");
00588       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00589       break;
00590     case COMPILER_OUT :
00591       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00592       break;
00593     }
00594   runner.setOutputter(outputter);
00595   runner.run();
00596   return 0; // runner.run() ? 0 : 1;
00597 #if 0
00598     CppUnit::TextUi::TestRunner runner;
00599     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00600     CppUnit::Outputter* outputter = 
00601       new CppUnit::TextOutputter(&runner.result(), std::cout);
00602     runner.setOutputter(outputter);
00603     bool retcode = runner.run();
00604     return !retcode;
00605 #endif
00606 }
00607 #endif // MAIN
00608 #endif // Time_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17