TimeMeasureTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef TimeMeasure_cpp
18 #define TimeMeasure_cpp
19 
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/TextOutputter.h>
22 #include <cppunit/extensions/TestFactoryRegistry.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/TestAssert.h>
25 #include <coil/Time.h>
26 #include <coil/TimeMeasure.h>
27 #include <math.h>
32 namespace TimeMeasure
33 {
35  : public CppUnit::TestFixture
36  {
44 
45  private:
46 
47  public:
48 
53  {
54  }
55 
60  {
61  }
62 
66  virtual void setUp()
67  {
68  }
69 
73  virtual void tearDown()
74  {
75  }
76 
77  void test_count()
78  {
80 
81  CPPUNIT_ASSERT(tm.count() == 0);
82 
83  const unsigned long int count0(10);
84  for (unsigned int i(0); i < count0; ++i)
85  {
86  tm.tick();
87  tm.tack();
88  }
89  CPPUNIT_ASSERT(tm.count() == count0);
90 
91  tm.reset();
92 
93  CPPUNIT_ASSERT(tm.count() == 0);
94 
95  const unsigned long int count1(53);
96  for (unsigned int i(0); i < count1; ++i)
97  {
98  tm.tick();
99  tm.tack();
100  }
101  CPPUNIT_ASSERT(tm.count() == count1);
102  }
103 
104  void test_stat()
105  {
107  double maxi, mini, mean, stdev;
108  CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
109  tm.tick();
110  CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
111  tm.tack();
112  CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == true);
113  tm.reset();
114  CPPUNIT_ASSERT(tm.getStatistics(maxi, mini, mean, stdev) == false);
115  }
116 
117  void test_buflen()
118  {
119  {
120  coil::TimeMeasure tm0(1);
121  CPPUNIT_ASSERT(tm0.count() == 0);
122  tm0.tick();
123  tm0.tack();
124  CPPUNIT_ASSERT(tm0.count() == 1);
125  }
126 
127  {
128  const unsigned int count(1024);
129  coil::TimeMeasure tm1(count);
130 
131  for (unsigned int i(0); i < count; ++i)
132  {
133  CPPUNIT_ASSERT(tm1.count() == i);
134  tm1.tick();
135  tm1.tack();
136  }
137  for (unsigned int i(0); i < count; ++i)
138  {
139  tm1.tick();
140  tm1.tack();
141  CPPUNIT_ASSERT(tm1.count() == (count + 1));
142  }
143 
144  }
145  }
146 
147  /* test case */
148  void test_30ms()
149  {
150  const double wait(0.03); // [s]
152  for (int i(0); i < 10; ++i)
153  {
154  tm.tick();
155  coil::usleep((int)(wait * 1000000));
156  tm.tack();
157  }
158  double maxi, mini, mean, stdev;
159  tm.getStatistics(maxi, mini, mean, stdev);
160  /*
161  std::cout << "max interval : " << maxi << " [sec]" << std::endl;
162  std::cout << "min interval : " << mini << " [sec]" << std::endl;
163  std::cout << "mean interval: " << mean << " [sec]" << std::endl;
164  std::cout << "stddev : " << stdev<< " [sec]" << std::endl;
165  */
166  CPPUNIT_ASSERT(maxi < (wait + 0.030));
167  CPPUNIT_ASSERT(mini > (wait - 0.010));
168  CPPUNIT_ASSERT(fabs(mean - wait) < 0.03);
169  CPPUNIT_ASSERT(stdev < (wait / 5.0));
170  }
171  void test_1s()
172  {
173  const double wait(1.0); // [s]
175  for (int i(0); i < 1; ++i)
176  {
177  tm.tick();
178  coil::sleep(1);
179  tm.tack();
180  }
181  double maxi, mini, mean, stdev;
182  tm.getStatistics(maxi, mini, mean, stdev);
183  /*
184  std::cout << "max interval : " << maxi << " [sec]" << std::endl;
185  std::cout << "min interval : " << mini << " [sec]" << std::endl;
186  std::cout << "mean interval: " << mean << " [sec]" << std::endl;
187  std::cout << "stddev : " << stdev<< " [sec]" << std::endl;
188  */
189  CPPUNIT_ASSERT(maxi < (wait + 0.030));
190  CPPUNIT_ASSERT(mini > (wait - 0.010));
191  CPPUNIT_ASSERT(fabs(mean - wait) < 0.03);
192  CPPUNIT_ASSERT(stdev < (wait / 5.0));
193  }
194  };
195 }; // namespace TimeMeasure
196 
197 /*
198  * Register test suite
199  */
201 
202 #ifdef LOCAL_MAIN
203 int main(int argc, char* argv[])
204 {
205  CppUnit::TextUi::TestRunner runner;
206  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
207  CppUnit::Outputter* outputter =
208  new CppUnit::TextOutputter(&runner.result(), std::cout);
209  runner.setOutputter(outputter);
210  bool retcode = runner.run();
211  return !retcode;
212 }
213 #endif // MAIN
214 #endif // TimeMeasure_cpp
int main(int argc, char **argv)
bool getStatistics(double &max_interval, double &min_interval, double &mean_interval, double &stddev)
Get total statistics.
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
virtual void setUp()
Test initialization.
CPPUNIT_TEST_SUITE_REGISTRATION(TimeMeasure::TimeMeasureTests)
CPPUNIT_TEST_SUITE(TimeMeasureTests)
void tack()
Finish time measurement for time statistics.
Definition: TimeMeasure.cpp:72
virtual void tearDown()
Test finalization.
void reset()
Initialize for statistics related data.
TimeMeasure class.
Definition: TimeMeasure.h:49
void tick()
Begin time measurement for time statistics.
Definition: TimeMeasure.cpp:60
unsigned long int count() const
Get number of time measurement buffer.
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:56