TimerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Timer_cpp
00018 #define Timer_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 <../../include/coil/Timer.h>
00033 #include <coil/Timer.h>
00034 #include <coil/TimeValue.h>
00035 
00036 #define JUDGEMAX 12
00037 #define JUDGEMIN 8
00038 
00042 namespace Timer
00043 {
00044   class TimerTests
00045    : public CppUnit::TestFixture
00046   {
00047     CPPUNIT_TEST_SUITE(TimerTests);
00048 
00049     CPPUNIT_TEST(test_registerListener);
00050     CPPUNIT_TEST(test_activate_multi_timers_continuously);
00051     CPPUNIT_TEST(test_activate_multi_timers_concurrently);
00052  
00053 
00054     CPPUNIT_TEST_SUITE_END();
00055   
00056   private:
00057     class Listener : public ListenerBase
00058     {
00059     public:
00060       Listener(const char* name = "", bool printMsg = false)
00061         : _name(name), _printMsg(printMsg), _count(0)
00062       {
00063       }
00064 
00065       virtual void invoke()
00066       {
00067         _count++;
00068 
00069         if (_printMsg) {
00070           std::cout
00071             << std::endl
00072             << _name << ":invoked. (count = " << _count << ")"
00073             << std::endl;
00074         }
00075       }
00076 
00077       const char* _name;
00078       bool _printMsg;
00079       int _count;
00080     };
00081    
00082   public:
00083   
00087     TimerTests()
00088     {
00089     }
00090     
00094     ~TimerTests()
00095     {
00096     }
00097   
00101     virtual void setUp()
00102     {
00103     }
00104     
00108     virtual void tearDown()
00109     { 
00110     }
00111   
00112     /* test case */
00113     void test_case0()
00114     {
00115     }
00122     void test_registerListener()
00123     {
00124       time_t tmstart, tmend;
00125       char cstr[256];
00126       coil::TimeValue timerInterval(0, 100000); // 0.1 [sec]
00127       coil::Timer timer(timerInterval);
00128 
00129       Listener listener;
00130       coil::TimeValue listenerInterval(0, 1000000);
00131       timer.registerListener(&listener, listenerInterval);
00132 
00133       timer.start();
00134 //      sleep(10);
00135       time(&tmstart);
00136       for(;;)
00137       {
00138         time(&tmend);
00139                 if(difftime(tmend,tmstart)>=10)
00140                 {
00141           break;
00142                 }
00143           }
00144           timer.stop();
00145       sprintf(cstr, "count:%d", listener._count );
00146       // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E      
00147       CPPUNIT_ASSERT_MESSAGE(cstr ,(JUDGEMIN <= listener._count) && (listener._count <= JUDGEMAX));
00148     }
00149 
00156     void test_activate_multi_timers_continuously()
00157     {
00158       time_t tmstart, tmend;
00159       char cstr[256];
00160           //The first timer is started.
00161           coil::TimeValue timerInterval1(0, 100000); // 0.1 [sec]
00162       coil::Timer timer1(timerInterval1);
00163 
00164       Listener listener1("listener-1");
00165       coil::TimeValue listenerInterval1(0, 1000000);
00166       timer1.registerListener(&listener1, listenerInterval1);
00167 
00168       timer1.start();
00169 //      sleep(10);
00170       time(&tmstart);
00171       for(;;)
00172       {
00173         time(&tmend);
00174                 if(difftime(tmend,tmstart)>=10)
00175                 {
00176           break;
00177                 }
00178           }
00179       timer1.stop();
00180 
00181       //The second timer is started. 
00182           coil::TimeValue timerInterval2(0, 100000); // 0.1 [sec]
00183       coil::Timer timer2(timerInterval2);
00184 
00185       Listener listener2("listener-2");
00186       coil::TimeValue listenerInterval2(0, 1000000);
00187       timer2.registerListener(&listener2, listenerInterval2);
00188 
00189       timer2.start();
00190 //      sleep(10);
00191       time(&tmstart);
00192       for(;;)
00193       {
00194         time(&tmend);
00195                 if(difftime(tmend,tmstart)>=10)
00196                 {
00197           break;
00198                 }
00199           }
00200       timer2.stop();
00201 
00202       // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E
00203       sprintf(cstr,"count:%d", listener1._count);
00204       CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener1._count) && (listener1._count <= JUDGEMAX));
00205       sprintf(cstr,"count:%d", listener2._count);
00206       CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener2._count) && (listener2._count <= JUDGEMAX));
00207     }
00208 
00215     void test_activate_multi_timers_concurrently()
00216     {
00217       time_t tmstart, tmend;
00218       char cstr[256];
00219           //The first timer is started. 
00220           coil::TimeValue timerInterval1(0, 100000); // 0.1 [sec]
00221       coil::Timer timer1(timerInterval1);
00222 
00223       Listener listener1("listener-1");
00224       coil::TimeValue listenerInterval1(0, 1000000);
00225       timer1.registerListener(&listener1, listenerInterval1);
00226 
00227       //The second timer is started. 
00228           coil::TimeValue timerInterval2(0, 100000); // 0.1 [sec]
00229       coil::Timer timer2(timerInterval2);
00230 
00231       Listener listener2("listener-2");
00232       coil::TimeValue listenerInterval2(0, 1000000);
00233       timer2.registerListener(&listener2, listenerInterval2);
00234 
00235       //Two timers be started are stopped simultaneously. 
00236           timer1.start();
00237       timer2.start();
00238 //      sleep(10);
00239       time(&tmstart);
00240       for(;;)
00241       {
00242         time(&tmend);
00243                 if(difftime(tmend,tmstart)>=10)
00244                 {
00245           break;
00246                 }
00247           }
00248       timer1.stop();
00249       timer2.stop();
00250 
00251       // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E
00252       sprintf(cstr,"count:%d", listener1._count);
00253       CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener1._count) && (listener1._count <= JUDGEMAX));
00254       sprintf(cstr,"count:%d", listener2._count);
00255       CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener2._count) && (listener2._count <= JUDGEMAX));
00256     }
00257 
00258   };
00259 }; // namespace Timer
00260 
00261 /*
00262  * Register test suite
00263  */
00264 CPPUNIT_TEST_SUITE_REGISTRATION(Timer::TimerTests);
00265 
00266 #ifdef LOCAL_MAIN
00267 int main(int argc, char* argv[])
00268 {
00269 #if 0
00270         FORMAT format = TEXT_OUT;
00271   int target = 0;
00272   std::string xsl;
00273   std::string ns;
00274   std::string fname;
00275   std::ofstream ofs;
00276 
00277   int i(1);
00278   while (i < argc)
00279     {
00280       std::string arg(argv[i]);
00281       std::string next_arg;
00282       if (i + 1 < argc) next_arg = argv[i + 1];
00283       else              next_arg = "";
00284 
00285       if (arg == "--text") { format = TEXT_OUT; break; }
00286       if (arg == "--xml")
00287         {
00288           if (next_arg == "")
00289             {
00290               fname = argv[0];
00291               fname += ".xml";
00292             }
00293           else
00294             {
00295               fname = next_arg;
00296             }
00297           format = XML_OUT;
00298           ofs.open(fname.c_str());
00299         }
00300       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00301   if ( arg == "--cerr"      ) { target = 1; break; }
00302       if ( arg == "--xsl"       )
00303         {
00304           if (next_arg == "") xsl = "default.xsl";
00305           else                xsl = next_arg;
00306         }
00307   if ( arg == "--namespace" )
00308         {
00309           if (next_arg == "")
00310             {
00311               std::cerr << "no namespace specified" << std::endl;
00312               exit(1);
00313             }
00314           else
00315             {
00316               xsl = next_arg;
00317             }
00318     }
00319       ++i;
00320     }
00321   CppUnit::TextUi::TestRunner runner;
00322   if ( ns.empty() )
00323     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00324   else
00325     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00326   CppUnit::Outputter* outputter = 0;
00327   std::ostream* stream = target ? &std::cerr : &std::cout;
00328   switch ( format )
00329     {
00330     case TEXT_OUT :
00331       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00332       break;
00333     case XML_OUT :
00334   std::cout << "XML_OUT" << std::endl;
00335       outputter = new CppUnit::XmlOutputter(&runner.result(),
00336                                             ofs, "shift_jis");
00337       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00338       break;
00339     case COMPILER_OUT :
00340       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00341       break;
00342     }
00343   runner.setOutputter(outputter);
00344   runner.run();
00345   return 0; // runner.run() ? 0 : 1;
00346 #endif
00347     CppUnit::TextUi::TestRunner runner;
00348     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00349     CppUnit::Outputter* outputter = 
00350       new CppUnit::TextOutputter(&runner.result(), std::cout);
00351     runner.setOutputter(outputter);
00352     bool retcode = runner.run();
00353     return !retcode;
00354 }
00355 #endif // MAIN
00356 #endif // Timer_cpp


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