TimeTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Time_cpp
18 #define Time_cpp
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <string>
23 #include <sstream>
24 #include <stdio.h>
25 #include <time.h>
26 #include <cppunit/ui/text/TestRunner.h>
27 #include <cppunit/TextOutputter.h>
28 #include <cppunit/extensions/TestFactoryRegistry.h>
29 #include <cppunit/extensions/HelperMacros.h>
30 #include <cppunit/TestAssert.h>
31 
32 #include <coil/Time.h>
33 #include <coil/Task.h>
34 #include <coil/TimeValue.h>
35 
36 
41 namespace Time
42 {
43  class TimeTests
44  : public CppUnit::TestFixture ,
45  public coil::Task
46  {
49 // CPPUNIT_TEST(test_sleep1);
50 // CPPUNIT_TEST(test_sleep2);
51 // CPPUNIT_TEST(test_sleep3);
53 // CPPUNIT_TEST(test_usleep1);
56 
57  private:
58  public:
60  : public coil::Task
61  {
62  public:
63  short m_count_time;
64  short m_svccmd;
65  short m_mainstop;
66  short m_svcstop;
67  int svc(void)
68  {
69  coil::TimeValue tv(0,1000000);
70  unsigned int usec;
71  unsigned int utime;
72  short ic;
73  switch(m_svccmd)
74  {
75  case 0:
76  utime = 1; //1sec
77  for (ic=0; ic<10; ic++)
78  {
79  coil::sleep(utime); //unsigned int sleep(unsigned int seconds)
80  m_count_time++;
81 // std::cout<<m_count_time<<std::endl;
82  if (m_svcstop==1)
83  {
84  m_svcstop = 0;
85  break;
86  }
87  }
88  m_mainstop = 1;
89  break;
90  case 1:
91 // tv = 1.0; //1s
92  for (ic=0; ic<10; ic++)
93  {
94  coil::sleep(tv); //int sleep(TimeValue& interval)
95  m_count_time++;
96 // std::cout<<m_count_time<<std::endl;
97  if (m_svcstop==1)
98  {
99  m_svcstop = 0;
100  break;
101  }
102  }
103  m_mainstop = 1;
104  break;
105  case 2:
106  usec = 1000000; //1s
107  for (ic=0; ic<10; ic++)
108  {
109  coil::usleep(usec); //int usleep(useconds_t usec)
110  m_count_time++;
111 // std::cout<<m_count_time<<std::endl;
112  if (m_svcstop==1)
113  {
114  m_svcstop = 0;
115  break;
116  }
117  }
118  m_mainstop = 1;
119  break;
120  case 3:
121  tv = 0.1; //100ms
122  for (ic=0; ic<100; ic++)
123  {
124  coil::sleep(tv); //int sleep(TimeValue& interval)
125  m_count_time++;
126 // std::cout<<m_count_time<<std::endl;
127  if (m_svcstop==1)
128  {
129  m_svcstop = 0;
130  break;
131  }
132  }
133  m_mainstop = 1;
134  break;
135  default:
136  break;
137  }
138  return 0;
139  }
140  };
145  {
146  }
147 
152  {
153  }
154 
158  virtual void setUp()
159  {
160  }
161 
165  int svc(void)
166  {
167  return 0;
168  }
169  virtual void tearDown()
170  {
171  }
172 
173  /* test case */
174  /*
175  ---------------------------------------------------------------------------
176  This function tests coil::sleep.
177  1.unsigned int sleep(unsigned int seconds)
178  2.int sleep(TimeValue& interval)
179  Check that it measures for ten seconds by the time function and coil::sleep(1)
180  ends ten times between those.
181  ---------------------------------------------------------------------------
182  */
183  void test_sleep()
184  {
185 
186  time_t tmstart, tmend;
187  short ic, icnt;
188  char cstr[256];
190 
191  tta = new TimeTests::TestTimeTask;
192 
193  tta->m_mainstop = 0;
194  tta->m_svcstop = 0;
195  tta->m_svccmd = 0;
196  tta->m_count_time = 0;
197  tta->activate();
198 
199  for(ic=0; ic<10; ic++)
200  {
201  ::time(&tmstart);
202  for(;;)
203  {
204  ::time(&tmend);
205  if(::difftime(tmend,tmstart)>=1.0)
206  {
207  break;
208  }
209  }
210  if(tta->m_mainstop == 1)
211  {
212  break;
213  }
214  }
215  tta->m_svcstop = 1;
216  tta->wait();
217  icnt = tta->m_count_time;
218  delete tta;
219  sprintf(cstr,"main:%d svc:%d", ic, icnt);
220  //カウンタをチェック
221  CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
222 
223  tta = new TimeTests::TestTimeTask;
224  tta->m_svcstop = 0;
225  tta->m_mainstop = 0;
226  tta->m_svccmd = 1;
227  tta->m_count_time = 0;
228  tta->activate();
229  for(ic=0; ic<10; ic++)
230  {
231  ::time(&tmstart);
232  for(;;)
233  {
234  ::time(&tmend);
235  if(::difftime(tmend,tmstart)>=1.0)
236  {
237  break;
238  }
239  }
240  if(tta->m_mainstop == 1)
241  {
242  break;
243  }
244  }
245  tta->m_svcstop = 1;
246  tta->wait();
247  icnt = tta->m_count_time;
248  delete tta;
249  sprintf(cstr,"main:%d svc:%d", ic, icnt);
250  //カウンタをチェック
251  CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
252  }
253  /*
254  ---------------------------------------------------------------------------
255  This function was made for the debugging of win32.
256  Time is measured by using the clock function.
257  ---------------------------------------------------------------------------
258  */
259  void test_sleep1()
260  {
261  clock_t start, end;
262  coil::TimeValue tv;
264  double dbcnt;
265  char cstr[256];
266 
267  tta = new TimeTests::TestTimeTask;
268 
269  tta->m_mainstop = 0;
270  tta->m_svcstop = 0;
271  tta->m_svccmd = 0;
272  tta->m_count_time = 0;
273  tta->activate();
274  start = ::clock();
275  tta->wait();
276  end = ::clock();
277  delete tta;
278  dbcnt = double(end - start) / CLOCKS_PER_SEC;
279  sprintf(cstr,"time:%f", dbcnt);
280  std::cout<<cstr<<std::endl;
281  CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
282 
283  tta = new TimeTests::TestTimeTask;
284 
285  tta->m_svcstop = 0;
286  tta->m_mainstop = 0;
287  tta->m_svccmd = 1;
288  tta->m_count_time = 0;
289  tta->activate();
290  start = ::clock();
291  tta->wait();
292  end = ::clock();
293  delete tta;
294  dbcnt = double(end - start) / CLOCKS_PER_SEC;
295  sprintf(cstr,"time:%f", dbcnt);
296  std::cout<<cstr<<std::endl;
297  CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
298  }
299  /*
300  ---------------------------------------------------------------------------
301  This function was made for the debugging of win32.
302  Time is measured by using the clock function.
303  ---------------------------------------------------------------------------
304  */
305  void test_sleep2()
306  {
307  clock_t start, end;
308  coil::TimeValue tv;
310  double dbcnt;
311  char cstr[256];
312 
313  tta = new TimeTests::TestTimeTask;
314 
315  tta->m_mainstop = 0;
316  tta->m_svcstop = 0;
317  tta->m_svccmd = 3;
318  tta->m_count_time = 0;
319  tta->activate();
320  start = ::clock();
321  tta->wait();
322  end = ::clock();
323  delete tta;
324  dbcnt = double(end - start) / CLOCKS_PER_SEC;
325  sprintf(cstr,"time:%f", dbcnt);
326  std::cout<<cstr<<std::endl;
327  CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
328 
329  }
330  /*
331  ---------------------------------------------------------------------------
332  Measurement at time of sleep(TimeValue& interval)
333  ---------------------------------------------------------------------------
334  */
335  void test_sleep3()
336  {
337 
338  clock_t start, end;
339  coil::TimeValue tv;
340  double dbcnt;
341  double dbcntmax,dbcntmin,dbcntave;
342  short ic,icc;
343 
344  //0.0
345  tv = 0;
346  for(icc=0;icc<=10;icc++)
347  {
348  dbcntave = 0.0;
349  dbcntmax = 0.0;
350  dbcntmin = 10.0;
351  for(ic=0;ic<100;ic++)
352  {
353  start = ::clock();
354  coil::sleep(tv);
355  end = ::clock();
356  dbcnt = double(end - start) / CLOCKS_PER_SEC;
357  dbcntave = dbcntave + dbcnt;
358  if(dbcnt>dbcntmax)
359  {
360  dbcntmax = dbcnt;
361  }
362  if(dbcnt<dbcntmin)
363  {
364  dbcntmin = dbcnt;
365  }
366  }
367  std::cout<<"sleep :"<<tv<<std::endl;
368  std::cout<<"sleep time ave :"<<dbcntave/100<<std::endl;
369  std::cout<<"sleep time max :"<<dbcntmax<<std::endl;
370  std::cout<<"sleep time min :"<<dbcntmin<<std::endl;
371  tv = tv + 0.01;
372  }
373  }
374  /*
375  ---------------------------------------------------------------------------
376  This function tests coil::usleep.
377  Check that it measures for ten seconds by the time function and coil::usleep(1000000)
378  ends ten times between those.
379  ---------------------------------------------------------------------------
380  */
381  void test_usleep()
382  {
383 
384  time_t tmstart, tmend;
385  coil::TimeValue tv;
386  short ic,icnt;
387  char cstr[256];
389 
390  tta = new TimeTests::TestTimeTask;
391 
392  tta->m_svccmd = 2;
393  tta->m_count_time = 0;
394  tta->m_svcstop = 0;
395  tta->m_mainstop = 0;
396  tta->activate();
397  for(ic=0; ic<10; ic++)
398  {
399  ::time(&tmstart);
400  for(;;)
401  {
402  ::time(&tmend);
403  if(::difftime(tmend,tmstart)>=1.0)
404  {
405  break;
406  }
407  }
408  if(tta->m_mainstop == 1)
409  {
410  break;
411  }
412  }
413  tta->m_svcstop = 1;
414  tta->wait();
415  icnt = tta->m_count_time;
416  delete tta;
417  sprintf(cstr,"main:%d svc:%d", ic, icnt);
418  //カウンタをチェック
419  CPPUNIT_ASSERT_MESSAGE( cstr, (ic == icnt) );
420  }
421  /*
422  ---------------------------------------------------------------------------
423  This function was made for the debugging of win32.
424  Time is measured by using the clock function.
425  ---------------------------------------------------------------------------
426  */
428  {
429 
430  clock_t start, end;
431  double dbcnt;
432  char cstr[256];
433 
435 
436  tta = new TimeTests::TestTimeTask;
437 
438  tta->m_svccmd = 2;
439  tta->m_count_time = 0;
440  tta->m_svcstop = 0;
441  tta->m_mainstop = 0;
442  tta->activate();
443  start = ::clock();
444  tta->wait();
445  end = ::clock();
446  delete tta;
447  dbcnt = double(end - start) / CLOCKS_PER_SEC;
448  sprintf(cstr,"time:%f", dbcnt);
449  std::cout<<cstr<<std::endl;
450  CPPUNIT_ASSERT_MESSAGE( cstr, (dbcnt>9.7)&(dbcnt<10.3) );
451  }
452  /*
453  ---------------------------------------------------------------------------
454  This function tests coil::gettimeofday().
455  ---------------------------------------------------------------------------
456  */
458  {
459  long l1,l2;
460  time_t tmstart, tmend;
461  clock_t start, end;
462  coil::TimeValue tv;
463  char cstr[256];
464  //check sec
465 
466  //get time
467  tv = coil::gettimeofday();
468  l1 = tv.sec();
469  // 10sec wait
470  ::time(&tmstart);
471  for(;;)
472  {
473  ::time(&tmend);
474  if(::difftime(tmend,tmstart)>=10)
475  {
476  break;
477  }
478  }
479  //get time
480  tv = coil::gettimeofday();
481  l2 = tv.sec();
482  //check
483  sprintf(cstr ,"sec%ld ",l2-l1);
484  std::cout<<cstr<<std::endl;
485  CPPUNIT_ASSERT_MESSAGE( cstr, (l2- l1)==10 );
486 
487  //check usec
488  //get time
489  tv = coil::gettimeofday();
490  l1 = tv.usec();
491  //100msec wait
492  start = ::clock();
493  for(;;)
494  {
495  end = ::clock();
496  if((double)(end-start)/CLOCKS_PER_SEC>0.1)
497  {
498  break;
499  }
500  }
501  //get time
502  tv = coil::gettimeofday();
503  l2 = tv.usec();
504  //check usec
505  sprintf(cstr ,"usec%ld ",l2-l1);
506  std::cout<<cstr<<std::endl;
507  CPPUNIT_ASSERT_MESSAGE( cstr, ((l2 - l1)>=80000) & ((l2 - l1)<=120000) );
508 
509  }
510  };
511 }; // namespace Time
512 
513 /*
514  * Register test suite
515  */
517 
518 #ifdef LOCAL_MAIN
519 int main(int argc, char* argv[])
520 {
521  FORMAT format = TEXT_OUT;
522  int target = 0;
523  std::string xsl;
524  std::string ns;
525  std::string fname;
526  std::ofstream ofs;
527 
528  int i(1);
529  while (i < argc)
530  {
531  std::string arg(argv[i]);
532  std::string next_arg;
533  if (i + 1 < argc) next_arg = argv[i + 1];
534  else next_arg = "";
535 
536  if (arg == "--text") { format = TEXT_OUT; break; }
537  if (arg == "--xml")
538  {
539  if (next_arg == "")
540  {
541  fname = argv[0];
542  fname += ".xml";
543  }
544  else
545  {
546  fname = next_arg;
547  }
548  format = XML_OUT;
549  ofs.open(fname.c_str());
550  }
551  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
552  if ( arg == "--cerr" ) { target = 1; break; }
553  if ( arg == "--xsl" )
554  {
555  if (next_arg == "") xsl = "default.xsl";
556  else xsl = next_arg;
557  }
558  if ( arg == "--namespace" )
559  {
560  if (next_arg == "")
561  {
562  std::cerr << "no namespace specified" << std::endl;
563  exit(1);
564  }
565  else
566  {
567  xsl = next_arg;
568  }
569  }
570  ++i;
571  }
572  CppUnit::TextUi::TestRunner runner;
573  if ( ns.empty() )
574  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
575  else
576  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
577  CppUnit::Outputter* outputter = 0;
578  std::ostream* stream = target ? &std::cerr : &std::cout;
579  switch ( format )
580  {
581  case TEXT_OUT :
582  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
583  break;
584  case XML_OUT :
585  std::cout << "XML_OUT" << std::endl;
586  outputter = new CppUnit::XmlOutputter(&runner.result(),
587  ofs, "shift_jis");
588  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
589  break;
590  case COMPILER_OUT :
591  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
592  break;
593  }
594  runner.setOutputter(outputter);
595  runner.run();
596  return 0; // runner.run() ? 0 : 1;
597 #if 0
598  CppUnit::TextUi::TestRunner runner;
599  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
600  CppUnit::Outputter* outputter =
601  new CppUnit::TextOutputter(&runner.result(), std::cout);
602  runner.setOutputter(outputter);
603  bool retcode = runner.run();
604  return !retcode;
605 #endif
606 }
607 #endif // MAIN
608 #endif // Time_cpp
CPPUNIT_TEST(test_sleep)
int main(int argc, char **argv)
int svc(void)
Test finalization.
Definition: TimeTests.cpp:165
TimeTests()
Constructor.
Definition: TimeTests.cpp:144
CPPUNIT_TEST_SUITE(TimeTests)
~TimeTests()
Destructor.
Definition: TimeTests.cpp:151
void test_sleep2()
Definition: TimeTests.cpp:305
void test_sleep1()
Definition: TimeTests.cpp:259
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
void test_usleep()
Definition: TimeTests.cpp:381
void test_gettimeofday()
Definition: TimeTests.cpp:457
TimeValue class.
Definition: TimeValue.h:40
virtual int wait(void)
Waiting for the thread terminate.
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get the time and timezone.
Definition: ace/coil/Time.h:57
virtual void setUp()
Test initialization.
Definition: TimeTests.cpp:158
int svc(void)
Execute thread.
Definition: TimeTests.cpp:67
void test_sleep3()
Definition: TimeTests.cpp:335
void test_usleep1()
Definition: TimeTests.cpp:427
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
Task class.
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
CPPUNIT_TEST_SUITE_REGISTRATION(Time::TimeTests)
virtual void tearDown()
Definition: TimeTests.cpp:169
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
virtual void activate()
Create a thread.


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