TimerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Timer_cpp
18 #define Timer_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 <../../include/coil/Timer.h>
33 #include <coil/Timer.h>
34 #include <coil/TimeValue.h>
35 
36 #define JUDGEMAX 12
37 #define JUDGEMIN 8
38 
42 namespace Timer
43 {
44  class TimerTests
45  : public CppUnit::TestFixture
46  {
48 
52 
53 
55 
56  private:
57  class Listener : public ListenerBase
58  {
59  public:
60  Listener(const char* name = "", bool printMsg = false)
61  : _name(name), _printMsg(printMsg), _count(0)
62  {
63  }
64 
65  virtual void invoke()
66  {
67  _count++;
68 
69  if (_printMsg) {
70  std::cout
71  << std::endl
72  << _name << ":invoked. (count = " << _count << ")"
73  << std::endl;
74  }
75  }
76 
77  const char* _name;
78  bool _printMsg;
79  int _count;
80  };
81 
82  public:
83 
88  {
89  }
90 
95  {
96  }
97 
101  virtual void setUp()
102  {
103  }
104 
108  virtual void tearDown()
109  {
110  }
111 
112  /* test case */
113  void test_case0()
114  {
115  }
123  {
124  time_t tmstart, tmend;
125  char cstr[256];
126  coil::TimeValue timerInterval(0, 100000); // 0.1 [sec]
127  coil::Timer timer(timerInterval);
128 
129  Listener listener;
130  coil::TimeValue listenerInterval(0, 1000000);
131  timer.registerListener(&listener, listenerInterval);
132 
133  timer.start();
134 // sleep(10);
135  time(&tmstart);
136  for(;;)
137  {
138  time(&tmend);
139  if(difftime(tmend,tmstart)>=10)
140  {
141  break;
142  }
143  }
144  timer.stop();
145  sprintf(cstr, "count:%d", listener._count );
146  // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E
147  CPPUNIT_ASSERT_MESSAGE(cstr ,(JUDGEMIN <= listener._count) && (listener._count <= JUDGEMAX));
148  }
149 
157  {
158  time_t tmstart, tmend;
159  char cstr[256];
160  //The first timer is started.
161  coil::TimeValue timerInterval1(0, 100000); // 0.1 [sec]
162  coil::Timer timer1(timerInterval1);
163 
164  Listener listener1("listener-1");
165  coil::TimeValue listenerInterval1(0, 1000000);
166  timer1.registerListener(&listener1, listenerInterval1);
167 
168  timer1.start();
169 // sleep(10);
170  time(&tmstart);
171  for(;;)
172  {
173  time(&tmend);
174  if(difftime(tmend,tmstart)>=10)
175  {
176  break;
177  }
178  }
179  timer1.stop();
180 
181  //The second timer is started.
182  coil::TimeValue timerInterval2(0, 100000); // 0.1 [sec]
183  coil::Timer timer2(timerInterval2);
184 
185  Listener listener2("listener-2");
186  coil::TimeValue listenerInterval2(0, 1000000);
187  timer2.registerListener(&listener2, listenerInterval2);
188 
189  timer2.start();
190 // sleep(10);
191  time(&tmstart);
192  for(;;)
193  {
194  time(&tmend);
195  if(difftime(tmend,tmstart)>=10)
196  {
197  break;
198  }
199  }
200  timer2.stop();
201 
202  // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E
203  sprintf(cstr,"count:%d", listener1._count);
204  CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener1._count) && (listener1._count <= JUDGEMAX));
205  sprintf(cstr,"count:%d", listener2._count);
206  CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener2._count) && (listener2._count <= JUDGEMAX));
207  }
208 
216  {
217  time_t tmstart, tmend;
218  char cstr[256];
219  //The first timer is started.
220  coil::TimeValue timerInterval1(0, 100000); // 0.1 [sec]
221  coil::Timer timer1(timerInterval1);
222 
223  Listener listener1("listener-1");
224  coil::TimeValue listenerInterval1(0, 1000000);
225  timer1.registerListener(&listener1, listenerInterval1);
226 
227  //The second timer is started.
228  coil::TimeValue timerInterval2(0, 100000); // 0.1 [sec]
229  coil::Timer timer2(timerInterval2);
230 
231  Listener listener2("listener-2");
232  coil::TimeValue listenerInterval2(0, 1000000);
233  timer2.registerListener(&listener2, listenerInterval2);
234 
235  //Two timers be started are stopped simultaneously.
236  timer1.start();
237  timer2.start();
238 // sleep(10);
239  time(&tmstart);
240  for(;;)
241  {
242  time(&tmend);
243  if(difftime(tmend,tmstart)>=10)
244  {
245  break;
246  }
247  }
248  timer1.stop();
249  timer2.stop();
250 
251  // 1秒に1回の呼出なので、10回カウントさE討いE呂此精度を考慮して、9〜11回の範囲であE海箸魍稜Г垢E
252  sprintf(cstr,"count:%d", listener1._count);
253  CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener1._count) && (listener1._count <= JUDGEMAX));
254  sprintf(cstr,"count:%d", listener2._count);
255  CPPUNIT_ASSERT_MESSAGE(cstr, (JUDGEMIN <= listener2._count) && (listener2._count <= JUDGEMAX));
256  }
257 
258  };
259 }; // namespace Timer
260 
261 /*
262  * Register test suite
263  */
265 
266 #ifdef LOCAL_MAIN
267 int main(int argc, char* argv[])
268 {
269 #if 0
270  FORMAT format = TEXT_OUT;
271  int target = 0;
272  std::string xsl;
273  std::string ns;
274  std::string fname;
275  std::ofstream ofs;
276 
277  int i(1);
278  while (i < argc)
279  {
280  std::string arg(argv[i]);
281  std::string next_arg;
282  if (i + 1 < argc) next_arg = argv[i + 1];
283  else next_arg = "";
284 
285  if (arg == "--text") { format = TEXT_OUT; break; }
286  if (arg == "--xml")
287  {
288  if (next_arg == "")
289  {
290  fname = argv[0];
291  fname += ".xml";
292  }
293  else
294  {
295  fname = next_arg;
296  }
297  format = XML_OUT;
298  ofs.open(fname.c_str());
299  }
300  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
301  if ( arg == "--cerr" ) { target = 1; break; }
302  if ( arg == "--xsl" )
303  {
304  if (next_arg == "") xsl = "default.xsl";
305  else xsl = next_arg;
306  }
307  if ( arg == "--namespace" )
308  {
309  if (next_arg == "")
310  {
311  std::cerr << "no namespace specified" << std::endl;
312  exit(1);
313  }
314  else
315  {
316  xsl = next_arg;
317  }
318  }
319  ++i;
320  }
321  CppUnit::TextUi::TestRunner runner;
322  if ( ns.empty() )
323  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
324  else
325  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
326  CppUnit::Outputter* outputter = 0;
327  std::ostream* stream = target ? &std::cerr : &std::cout;
328  switch ( format )
329  {
330  case TEXT_OUT :
331  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
332  break;
333  case XML_OUT :
334  std::cout << "XML_OUT" << std::endl;
335  outputter = new CppUnit::XmlOutputter(&runner.result(),
336  ofs, "shift_jis");
337  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
338  break;
339  case COMPILER_OUT :
340  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
341  break;
342  }
343  runner.setOutputter(outputter);
344  runner.run();
345  return 0; // runner.run() ? 0 : 1;
346 #endif
347  CppUnit::TextUi::TestRunner runner;
348  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
349  CppUnit::Outputter* outputter =
350  new CppUnit::TextOutputter(&runner.result(), std::cout);
351  runner.setOutputter(outputter);
352  bool retcode = runner.run();
353  return !retcode;
354 }
355 #endif // MAIN
356 #endif // Timer_cpp
CPPUNIT_TEST_SUITE(TimerTests)
int main(int argc, char **argv)
virtual void invoke()
Callback.
Definition: TimerTests.cpp:65
void test_activate_multi_timers_continuously()
複数のタイマーを時間的に直列に動笹正せE謄好
Definition: TimerTests.cpp:156
#define JUDGEMAX
Definition: TimerTests.cpp:36
ListenerId registerListener(ListenerBase *listener, TimeValue tm)
Register listener.
Definition: Timer.cpp:141
~TimerTests()
Destructor.
Definition: TimerTests.cpp:94
void test_registerListener()
registerListener()メソッドのテスト
Definition: TimerTests.cpp:122
TimerTests()
Constructor.
Definition: TimerTests.cpp:87
void test_activate_multi_timers_concurrently()
複数のタイマーを時間的に並列に動笹正せE謄好
Definition: TimerTests.cpp:215
TimeValue class.
Definition: TimeValue.h:40
void stop()
Stop Timer task.
Definition: Timer.cpp:107
ListenerBase class.
Definition: Listener.h:43
CPPUNIT_TEST(test_registerListener)
Timer class.
Definition: Timer.h:53
void start()
Start Timer task.
Definition: Timer.cpp:90
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
#define JUDGEMIN
Definition: TimerTests.cpp:37
virtual void tearDown()
Test finalization.
Definition: TimerTests.cpp:108
Listener(const char *name="", bool printMsg=false)
Definition: TimerTests.cpp:60
virtual void setUp()
Test initialization.
Definition: TimerTests.cpp:101
CPPUNIT_TEST_SUITE_REGISTRATION(Timer::TimerTests)


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