TestDeferAndMessageQueue2.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2017 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <iostream>
  11. // back-end
  12. #include <boost/msm/back/state_machine.hpp>
  13. //front-end
  14. #include <boost/msm/front/state_machine_def.hpp>
  15. #include <boost/msm/front/functor_row.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. namespace msm = boost::msm;
  18. namespace mpl = boost::mpl;
  19. using namespace boost::msm::front;
  20. namespace
  21. {
  22. // events
  23. struct event1 {};
  24. struct event2 {};
  25. struct event3 {};
  26. struct eventd {};
  27. // front-end: define the FSM structure
  28. struct player_ : public msm::front::state_machine_def<player_>
  29. {
  30. player_()
  31. :expected_action_counter(0),expected_action2_counter(0)
  32. {}
  33. // The list of FSM states
  34. struct State11 : public msm::front::state<>
  35. {
  36. typedef mpl::vector<eventd> deferred_events;
  37. template <class Event,class FSM>
  38. void on_entry(Event const&,FSM& ) {++entry_counter;}
  39. template <class Event,class FSM>
  40. void on_exit(Event const&,FSM& ) {++exit_counter;}
  41. int entry_counter;
  42. int exit_counter;
  43. };
  44. struct State12 : public msm::front::state<>
  45. {
  46. template <class Event,class FSM>
  47. void on_entry(Event const&,FSM& ) {++entry_counter;}
  48. template <class Event,class FSM>
  49. void on_exit(Event const&,FSM& ) {++exit_counter;}
  50. int entry_counter;
  51. int exit_counter;
  52. };
  53. struct State13 : public msm::front::state<>
  54. {
  55. template <class Event,class FSM>
  56. void on_entry(Event const&,FSM& ) {++entry_counter;}
  57. template <class Event,class FSM>
  58. void on_exit(Event const&,FSM& ) {++exit_counter;}
  59. int entry_counter;
  60. int exit_counter;
  61. };
  62. struct enqueue_action
  63. {
  64. template <class EVT,class FSM,class SourceState,class TargetState>
  65. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  66. {
  67. fsm.template process_event(event2());
  68. }
  69. };
  70. struct expected_action
  71. {
  72. template <class EVT,class FSM,class SourceState,class TargetState>
  73. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  74. {
  75. ++fsm.expected_action_counter;
  76. }
  77. };
  78. struct expected_action2
  79. {
  80. template <class EVT,class FSM,class SourceState,class TargetState>
  81. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  82. {
  83. ++fsm.expected_action2_counter;
  84. }
  85. };
  86. // the initial state of the player SM. Must be defined
  87. typedef mpl::vector<State11> initial_state;
  88. // Transition table for player
  89. struct transition_table : mpl::vector<
  90. // Start Event Next Action Guard
  91. // +---------+-------------+---------+---------------------+----------------------+
  92. Row < State11 , event1 , State12 , enqueue_action >,
  93. Row < State12 , event2 , State13 , expected_action2 >,
  94. Row < State12 , eventd , State13 , expected_action >,
  95. Row < State13 , event2 , State11 >,
  96. Row < State13 , eventd , State11 >
  97. // +---------+-------------+---------+---------------------+----------------------+
  98. > {};
  99. // Replaces the default no-transition response.
  100. template <class FSM,class Event>
  101. void no_transition(Event const& , FSM&,int )
  102. {
  103. BOOST_FAIL("no_transition called!");
  104. }
  105. // init counters
  106. template <class Event,class FSM>
  107. void on_entry(Event const&,FSM& fsm)
  108. {
  109. fsm.template get_state<player_::State11&>().entry_counter=0;
  110. fsm.template get_state<player_::State11&>().exit_counter=0;
  111. fsm.template get_state<player_::State12&>().entry_counter=0;
  112. fsm.template get_state<player_::State12&>().exit_counter=0;
  113. fsm.template get_state<player_::State13&>().entry_counter=0;
  114. fsm.template get_state<player_::State13&>().exit_counter=0;
  115. }
  116. int expected_action_counter;
  117. int expected_action2_counter;
  118. };
  119. // Pick a back-end
  120. typedef msm::back::state_machine<player_> player;
  121. BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2 )
  122. {
  123. player p;
  124. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  125. p.start();
  126. p.process_event(eventd());
  127. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
  128. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 1,"State11 entry not called correctly");
  129. p.process_event(event1());
  130. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
  131. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().exit_counter == 1,"State11 exit not called correctly");
  132. BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().entry_counter == 1,"State12 entry not called correctly");
  133. BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().exit_counter == 1,"State12 exit not called correctly");
  134. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 2,"State11 entry not called correctly");
  135. BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().exit_counter == 1,"State13 exit not called correctly");
  136. BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().entry_counter == 1,"State13 entry not called correctly");
  137. BOOST_CHECK_MESSAGE(p.expected_action_counter == 1,"expected_action should have been called");
  138. BOOST_CHECK_MESSAGE(p.expected_action2_counter == 0,"expected_action2 should not have been called");
  139. }
  140. }