TimeMeasureTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef TimeMeasure_cpp
00018 #define TimeMeasure_cpp
00019 
00020 #include <cppunit/ui/text/TestRunner.h>
00021 #include <cppunit/TextOutputter.h>
00022 #include <cppunit/extensions/TestFactoryRegistry.h>
00023 #include <cppunit/extensions/HelperMacros.h>
00024 #include <cppunit/TestAssert.h>
00025 #include <coil/Time.h>
00026 #include <coil/TimeMeasure.h>
00027 #include <math.h>
00032 namespace TimeMeasure
00033 {
00034   class TimeMeasureTests
00035    : public CppUnit::TestFixture
00036   {
00037     CPPUNIT_TEST_SUITE(TimeMeasureTests);
00038     CPPUNIT_TEST(test_count);
00039     CPPUNIT_TEST(test_stat);
00040     CPPUNIT_TEST(test_buflen);
00041     CPPUNIT_TEST(test_30ms);
00042     CPPUNIT_TEST(test_1s);
00043     CPPUNIT_TEST_SUITE_END();
00044   
00045   private:
00046   
00047   public:
00048   
00052     TimeMeasureTests()
00053     {
00054     }
00055     
00059     ~TimeMeasureTests()
00060     {
00061     }
00062   
00066     virtual void setUp()
00067     {
00068     }
00069     
00073     virtual void tearDown()
00074     { 
00075     }
00076   
00077     void test_count()
00078     {
00079       coil::TimeMeasure tm;
00080 
00081       CPPUNIT_ASSERT(tm.count() == 0);
00082 
00083       const unsigned long int count0(10);
00084       for (unsigned int i(0); i < count0; ++i)
00085         {
00086           tm.tick();
00087           tm.tack();
00088         }
00089       CPPUNIT_ASSERT(tm.count() ==  count0);
00090 
00091       tm.reset();
00092 
00093       CPPUNIT_ASSERT(tm.count() == 0);
00094 
00095       const unsigned long int count1(53);
00096       for (unsigned int i(0); i < count1; ++i)
00097         {
00098           tm.tick();
00099           tm.tack();
00100         }
00101       CPPUNIT_ASSERT(tm.count() == count1);
00102     }
00103 
00104     void test_stat()
00105     {
00106       coil::TimeMeasure tm;
00107       double maxi, mini, mean, stdev;
00108       CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
00109       tm.tick();
00110       CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
00111       tm.tack();
00112       CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == true);
00113       tm.reset();
00114       CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
00115     }
00116 
00117     void test_buflen()
00118     {
00119       {
00120         coil::TimeMeasure tm0(1);
00121         CPPUNIT_ASSERT(tm0.count() == 0);
00122         tm0.tick();
00123         tm0.tack();
00124         CPPUNIT_ASSERT(tm0.count() == 1);
00125       }
00126 
00127       {
00128         const unsigned int count(1024);
00129         coil::TimeMeasure tm1(count);
00130 
00131         for (unsigned int i(0); i < count; ++i)
00132           {
00133             CPPUNIT_ASSERT(tm1.count() == i);
00134             tm1.tick();
00135             tm1.tack();
00136           }
00137         for (unsigned int i(0); i < count; ++i)
00138           {
00139             tm1.tick();
00140             tm1.tack();
00141             CPPUNIT_ASSERT(tm1.count() == (count + 1));
00142           }
00143 
00144       }
00145     }
00146 
00147     /* test case */
00148     void test_30ms()
00149     {
00150       const double wait(0.03); // [s]
00151       coil::TimeMeasure tm;
00152       for (int i(0); i < 10; ++i)
00153         {
00154           tm.tick();
00155           coil::usleep((int)(wait * 1000000));
00156           tm.tack();
00157         }
00158       double maxi, mini, mean, stdev;
00159       tm.getStatistics(maxi, mini, mean, stdev);
00160       /*
00161       std::cout << "max interval : " << maxi << " [sec]" << std::endl;
00162       std::cout << "min interval : " << mini << " [sec]" << std::endl;
00163       std::cout << "mean interval: " << mean << " [sec]" << std::endl;
00164       std::cout << "stddev       : " << stdev<< " [sec]" << std::endl;
00165       */
00166       CPPUNIT_ASSERT(maxi < (wait + 0.030));
00167       CPPUNIT_ASSERT(mini > (wait - 0.010));
00168       CPPUNIT_ASSERT(fabs(mean - wait) < 0.03);
00169       CPPUNIT_ASSERT(stdev < (wait / 5.0));
00170     }
00171     void test_1s()
00172     {
00173       const double wait(1.0); // [s]
00174       coil::TimeMeasure tm;
00175       for (int i(0); i < 1; ++i)
00176         {
00177           tm.tick();
00178           coil::sleep(1);
00179           tm.tack();
00180         }
00181       double maxi, mini, mean, stdev;
00182       tm.getStatistics(maxi, mini, mean, stdev);
00183       /*
00184       std::cout << "max interval : " << maxi << " [sec]" << std::endl;
00185       std::cout << "min interval : " << mini << " [sec]" << std::endl;
00186       std::cout << "mean interval: " << mean << " [sec]" << std::endl;
00187       std::cout << "stddev       : " << stdev<< " [sec]" << std::endl;
00188       */
00189       CPPUNIT_ASSERT(maxi < (wait + 0.030));
00190       CPPUNIT_ASSERT(mini > (wait - 0.010));
00191       CPPUNIT_ASSERT(fabs(mean - wait) < 0.03);
00192       CPPUNIT_ASSERT(stdev < (wait / 5.0));
00193     }
00194   };
00195 }; // namespace TimeMeasure
00196 
00197 /*
00198  * Register test suite
00199  */
00200 CPPUNIT_TEST_SUITE_REGISTRATION(TimeMeasure::TimeMeasureTests);
00201 
00202 #ifdef LOCAL_MAIN
00203 int main(int argc, char* argv[])
00204 {
00205     CppUnit::TextUi::TestRunner runner;
00206     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00207     CppUnit::Outputter* outputter = 
00208       new CppUnit::TextOutputter(&runner.result(), std::cout);
00209     runner.setOutputter(outputter);
00210     bool retcode = runner.run();
00211     return !retcode;
00212 }
00213 #endif // MAIN
00214 #endif // TimeMeasure_cpp


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