StateMachineTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
21 /*
22  * $Log: StateMachineTests.cpp,v $
23  * Revision 1.3 2008/04/08 02:21:39 arafune
24  * Refactored test cases.
25  *
26  * Revision 1.2 2008/04/04 14:29:12 arafune
27  * *** empty log message ***
28  *
29  * Revision 1.1 2007/12/20 07:50:18 arafune
30  * *** empty log message ***
31  *
32  * Revision 1.2 2007/01/12 14:56:19 n-ando
33  * The getState() function is now getState().
34  *
35  * Revision 1.1 2006/11/27 08:26:03 n-ando
36  * TestSuites are devided into each directory.
37  *
38  * Revision 1.2 2006/11/02 12:27:09 kurihara
39  *
40  * StateMachineTest is modified by kurihara.
41  *
42  * Revision 1.1 2006/10/26 08:56:56 n-ando
43  * The first commitment.
44  */
45 
46 #ifndef StateMachine_cpp
47 #define StateMachine_cpp
48 
49 #include <cppunit/ui/text/TestRunner.h>
50 #include <cppunit/TextOutputter.h>
51 #include <cppunit/extensions/TestFactoryRegistry.h>
52 #include <cppunit/extensions/HelperMacros.h>
53 #include <cppunit/TestAssert.h>
54 
55 #include <coil/TimeValue.h>
56 #include <rtm/StateMachine.h>
57 
63 {
65  {
66  public:
67  static const int STATE_1 = 0;
68  static const int STATE_2 = 1;
69  static const int STATE_3 = 2;
70  static const int STATE_4 = 3;
71  static const int STATE_5 = 4;
72  static const int STATE_6 = 5;
73  static const int SIZEOF_STATE = 6;
74 
75  static const int ENTRY = 0;
76  static const int PREDO = 1;
77  static const int DO = 2;
78  static const int POSTDO = 3;
79  static const int EXIT = 4;
80  static const int SIZEOF_ACTION = 5;
81 
82  StateMachineContext1() : m_fsm(SIZEOF_STATE)
83  {
84  m_fsm.setListener(this);
85 
86  m_fsm.setEntryAction(STATE_1, &StateMachineContext1::onEntry_STATE_1);
87  m_fsm.setPreDoAction(STATE_1, &StateMachineContext1::onPreDo_STATE_1);
88  m_fsm.setDoAction(STATE_1, &StateMachineContext1::onDo_STATE_1);
89  m_fsm.setPostDoAction(STATE_1, &StateMachineContext1::onPostDo_STATE_1);
90  m_fsm.setExitAction(STATE_1, &StateMachineContext1::onExit_STATE_1);
91 
92  m_fsm.setEntryAction(STATE_2, &StateMachineContext1::onEntry_STATE_2);
93  m_fsm.setPreDoAction(STATE_2, &StateMachineContext1::onPreDo_STATE_2);
94  m_fsm.setDoAction(STATE_2, &StateMachineContext1::onDo_STATE_2);
95  m_fsm.setPostDoAction(STATE_2, &StateMachineContext1::onPostDo_STATE_2);
96  m_fsm.setExitAction(STATE_2, &StateMachineContext1::onExit_STATE_2);
97 
98  m_fsm.setEntryAction(STATE_3, &StateMachineContext1::onEntry_STATE_3);
99  m_fsm.setPreDoAction(STATE_3, &StateMachineContext1::onPreDo_STATE_3);
100  m_fsm.setDoAction(STATE_3, &StateMachineContext1::onDo_STATE_3);
101  m_fsm.setPostDoAction(STATE_3, &StateMachineContext1::onPostDo_STATE_3);
102  m_fsm.setExitAction(STATE_3, &StateMachineContext1::onExit_STATE_3);
103 
104  m_fsm.setEntryAction(STATE_4, &StateMachineContext1::onEntry_STATE_4);
105  m_fsm.setPreDoAction(STATE_4, &StateMachineContext1::onPreDo_STATE_4);
106  m_fsm.setDoAction(STATE_4, &StateMachineContext1::onDo_STATE_4);
107  m_fsm.setPostDoAction(STATE_4, &StateMachineContext1::onPostDo_STATE_4);
108  m_fsm.setExitAction(STATE_4, &StateMachineContext1::onExit_STATE_4);
109 
110  m_fsm.setEntryAction(STATE_5, &StateMachineContext1::onEntry_STATE_5);
111  m_fsm.setPreDoAction(STATE_5, &StateMachineContext1::onPreDo_STATE_5);
112  m_fsm.setDoAction(STATE_5, &StateMachineContext1::onDo_STATE_5);
113  m_fsm.setPostDoAction(STATE_5, &StateMachineContext1::onPostDo_STATE_5);
114  m_fsm.setExitAction(STATE_5, &StateMachineContext1::onExit_STATE_5);
115 
116  m_fsm.setEntryAction(STATE_6, &StateMachineContext1::onEntry_STATE_6);
117  m_fsm.setPreDoAction(STATE_6, &StateMachineContext1::onPreDo_STATE_6);
118  m_fsm.setDoAction(STATE_6, &StateMachineContext1::onDo_STATE_6);
119  m_fsm.setPostDoAction(STATE_6, &StateMachineContext1::onPostDo_STATE_6);
120  m_fsm.setExitAction(STATE_6, &StateMachineContext1::onExit_STATE_6);
121 
122  RTC_Utils::StateHolder<int> initialStates;
123  initialStates.prev = STATE_1;
124  initialStates.curr = STATE_1;
125  initialStates.next = STATE_1;
126  m_fsm.setStartState(initialStates);
127  }
128 
129  void work()
130  {
131  for (int i = 0; i < 50; ++i)
132  {
133  m_fsm.worker();
134  }
135  }
136 
138  {
139  logCallback(STATE_1, ENTRY);
140  }
142  {
143  logCallback(STATE_1, PREDO);
144  m_fsm.goTo(STATE_2);
145  }
147  {
148  logCallback(STATE_1, DO);
149  }
151  {
152  logCallback(STATE_1, POSTDO);
153  }
155  {
156  logCallback(STATE_1, EXIT);
157  }
158 
160  {
161  logCallback(STATE_2, ENTRY);
162  }
164  {
165  logCallback(STATE_2, PREDO);
166  }
167 
169  {
170  logCallback(STATE_2, DO);
171  m_fsm.goTo(STATE_3);
172  }
174  {
175  logCallback(STATE_2, POSTDO);
176  }
178  {
179  logCallback(STATE_2, EXIT);
180  }
181 
183  {
184  logCallback(STATE_3, ENTRY);
185  }
187  {
188  logCallback(STATE_3, PREDO);
189  }
191  {
192  logCallback(STATE_3, DO);
193  }
195  {
196  logCallback(STATE_3, POSTDO);
197  m_fsm.goTo(STATE_4);
198  }
200  {
201  logCallback(STATE_3, EXIT);
202  }
203 
205  {
206  logCallback(STATE_4, ENTRY);
207  m_fsm.goTo(STATE_5);
208  }
210  {
211  logCallback(STATE_4, PREDO);
212  }
214  {
215  logCallback(STATE_4, DO);
216  }
218  {
219  logCallback(STATE_4, POSTDO);
220  }
222  {
223  logCallback(STATE_4, EXIT);
224  }
225 
227  {
228  logCallback(STATE_5, ENTRY);
229  }
231  {
232  logCallback(STATE_5, PREDO);
233  }
235  {
236  logCallback(STATE_5, DO);
237  }
239  {
240  logCallback(STATE_5, POSTDO);
241  }
243  {
244  logCallback(STATE_5, EXIT);
245  m_fsm.goTo(STATE_6);
246  }
247 
249  {
250  logCallback(STATE_6, ENTRY);
251  }
253  {
254  logCallback(STATE_6, PREDO);
255  }
257  {
258  logCallback(STATE_6, DO);
259  }
261  {
262  logCallback(STATE_6, POSTDO);
263  }
265  {
266  logCallback(STATE_6, EXIT);
267  }
268 
269  public:
271  {
272  int state;
273  int action;
274  };
275 
276  const std::vector<StateAndAction>& getCallbackLog() const
277  {
278  return m_callbackLog;
279  }
280 
281  private:
283  std::vector<StateAndAction> m_callbackLog;
284 
285  private:
286  void logCallback(int state, int action)
287  {
288  StateAndAction sa;
289  sa.state = state;
290  sa.action = action;
291  m_callbackLog.push_back(sa);
292  }
293  };
294 
296  : public CppUnit::TestFixture
297  {
298  CPPUNIT_TEST_SUITE(StateMachineTests);
299  CPPUNIT_TEST(test_transition_story1);
300  CPPUNIT_TEST_SUITE_END();
301 
302  private:
303 
304  public:
309  {
310  }
311 
316  {
317  }
318 
322  virtual void setUp()
323  {
324  }
325 
329  virtual void tearDown()
330  {
331  }
332 
345  {
346  typedef StateMachineContext1 SMC;
347 
348  SMC::StateAndAction expected[] =
349  {
350  { SMC::STATE_1, SMC::PREDO },
351  { SMC::STATE_1, SMC::EXIT },
352  { SMC::STATE_2, SMC::ENTRY },
353  { SMC::STATE_2, SMC::PREDO },
354  { SMC::STATE_2, SMC::DO },
355  { SMC::STATE_2, SMC::EXIT },
356  { SMC::STATE_3, SMC::ENTRY },
357  { SMC::STATE_3, SMC::PREDO },
358  { SMC::STATE_3, SMC::DO },
359  { SMC::STATE_3, SMC::POSTDO },
360  { SMC::STATE_3, SMC::EXIT },
361  { SMC::STATE_4, SMC::ENTRY },
362  { SMC::STATE_4, SMC::EXIT },
363  { SMC::STATE_5, SMC::ENTRY },
364  { SMC::STATE_5, SMC::PREDO },
365  { SMC::STATE_5, SMC::DO },
366  { SMC::STATE_5, SMC::POSTDO },
367  { SMC::STATE_5, SMC::PREDO }
368  };
369 
370  SMC context;
371  context.work();
372 
373  const std::vector<SMC::StateAndAction> log = context.getCallbackLog();
374  for (int i = 0; i < sizeof(expected) / sizeof(SMC::StateAndAction); ++i)
375  {
376  CPPUNIT_ASSERT_EQUAL(expected[i].state, log[i].state);
377  CPPUNIT_ASSERT_EQUAL(expected[i].action, log[i].action);
378  }
379  }
380 
381  };
382 }; // namespace StateMachine
383 
384 /*
385  * Register test suite
386  */
388 
389 #ifdef LOCAL_MAIN
390 int main(int argc, char* argv[])
391 {
392 
393  FORMAT format = TEXT_OUT;
394  int target = 0;
395  std::string xsl;
396  std::string ns;
397  std::string fname;
398  std::ofstream ofs;
399 
400  int i(1);
401  while (i < argc)
402  {
403  std::string arg(argv[i]);
404  std::string next_arg;
405  if (i + 1 < argc) next_arg = argv[i + 1];
406  else next_arg = "";
407 
408  if (arg == "--text") { format = TEXT_OUT; break; }
409  if (arg == "--xml")
410  {
411  if (next_arg == "")
412  {
413  fname = argv[0];
414  fname += ".xml";
415  }
416  else
417  {
418  fname = next_arg;
419  }
420  format = XML_OUT;
421  ofs.open(fname.c_str());
422  }
423  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
424  if ( arg == "--cerr" ) { target = 1; break; }
425  if ( arg == "--xsl" )
426  {
427  if (next_arg == "") xsl = "default.xsl";
428  else xsl = next_arg;
429  }
430  if ( arg == "--namespace" )
431  {
432  if (next_arg == "")
433  {
434  std::cerr << "no namespace specified" << std::endl;
435  exit(1);
436  }
437  else
438  {
439  xsl = next_arg;
440  }
441  }
442  ++i;
443  }
444  CppUnit::TextUi::TestRunner runner;
445  if ( ns.empty() )
446  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
447  else
448  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
449  CppUnit::Outputter* outputter = 0;
450  std::ostream* stream = target ? &std::cerr : &std::cout;
451  switch ( format )
452  {
453  case TEXT_OUT :
454  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
455  break;
456  case XML_OUT :
457  std::cout << "XML_OUT" << std::endl;
458  outputter = new CppUnit::XmlOutputter(&runner.result(),
459  ofs, "shift_jis");
460  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
461  break;
462  case COMPILER_OUT :
463  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
464  break;
465  }
466  runner.setOutputter(outputter);
467  runner.run();
468  return 0; // runner.run() ? 0 : 1;
469 }
470 #endif // MAIN
471 #endif // StateMachine_cpp
void onPreDo_STATE_1(const RTC_Utils::StateHolder< int > &states)
int main(int argc, char **argv)
void onPostDo_STATE_1(const RTC_Utils::StateHolder< int > &states)
void onDo_STATE_4(const RTC_Utils::StateHolder< int > &states)
virtual void setUp()
Test initialization.
void onExit_STATE_1(const RTC_Utils::StateHolder< int > &states)
void onDo_STATE_6(const RTC_Utils::StateHolder< int > &states)
void onPostDo_STATE_3(const RTC_Utils::StateHolder< int > &states)
void onEntry_STATE_6(const RTC_Utils::StateHolder< int > &states)
void onPostDo_STATE_6(const RTC_Utils::StateHolder< int > &states)
void onPreDo_STATE_3(const RTC_Utils::StateHolder< int > &states)
void onDo_STATE_2(const RTC_Utils::StateHolder< int > &states)
void onPreDo_STATE_6(const RTC_Utils::StateHolder< int > &states)
void onEntry_STATE_5(const RTC_Utils::StateHolder< int > &states)
void onDo_STATE_5(const RTC_Utils::StateHolder< int > &states)
void onExit_STATE_5(const RTC_Utils::StateHolder< int > &states)
void onPostDo_STATE_5(const RTC_Utils::StateHolder< int > &states)
void onDo_STATE_3(const RTC_Utils::StateHolder< int > &states)
void onExit_STATE_2(const RTC_Utils::StateHolder< int > &states)
State machine class.
Definition: StateMachine.h:263
const std::vector< StateAndAction > & getCallbackLog() const
void onExit_STATE_6(const RTC_Utils::StateHolder< int > &states)
void test_transition_story1()
StateMachineによる状態遷移のテスト
virtual void tearDown()
Test finalization.
void onDo_STATE_1(const RTC_Utils::StateHolder< int > &states)
void onExit_STATE_4(const RTC_Utils::StateHolder< int > &states)
void onExit_STATE_3(const RTC_Utils::StateHolder< int > &states)
void onPostDo_STATE_2(const RTC_Utils::StateHolder< int > &states)
std::vector< StateAndAction > m_callbackLog
void onPreDo_STATE_4(const RTC_Utils::StateHolder< int > &states)
RTC_Utils::StateMachine< int, StateMachineContext1 > m_fsm
void onEntry_STATE_1(const RTC_Utils::StateHolder< int > &states)
void onEntry_STATE_2(const RTC_Utils::StateHolder< int > &states)
State machine template class.
void onPreDo_STATE_5(const RTC_Utils::StateHolder< int > &states)
void onEntry_STATE_4(const RTC_Utils::StateHolder< int > &states)
void onPostDo_STATE_4(const RTC_Utils::StateHolder< int > &states)
void onPreDo_STATE_2(const RTC_Utils::StateHolder< int > &states)
CPPUNIT_TEST_SUITE_REGISTRATION(StateMachineTests::StateMachineTests)
void onEntry_STATE_3(const RTC_Utils::StateHolder< int > &states)


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