TestDeferAndMessageQueue3.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // in this test, we inverse the order deferred queue / event queue
  31. typedef int event_queue_before_deferred_queue;
  32. player_()
  33. :expected_action_counter(0),expected_action2_counter(0)
  34. {}
  35. // The list of FSM states
  36. struct State11 : public msm::front::state<>
  37. {
  38. typedef mpl::vector<eventd> deferred_events;
  39. template <class Event,class FSM>
  40. void on_entry(Event const&,FSM& ) {++entry_counter;}
  41. template <class Event,class FSM>
  42. void on_exit(Event const&,FSM& ) {++exit_counter;}
  43. int entry_counter;
  44. int exit_counter;
  45. };
  46. struct State12 : public msm::front::state<>
  47. {
  48. template <class Event,class FSM>
  49. void on_entry(Event const&,FSM& ) {++entry_counter;}
  50. template <class Event,class FSM>
  51. void on_exit(Event const&,FSM& ) {++exit_counter;}
  52. int entry_counter;
  53. int exit_counter;
  54. };
  55. struct State13 : public msm::front::state<>
  56. {
  57. template <class Event,class FSM>
  58. void on_entry(Event const&,FSM& ) {++entry_counter;}
  59. template <class Event,class FSM>
  60. void on_exit(Event const&,FSM& ) {++exit_counter;}
  61. int entry_counter;
  62. int exit_counter;
  63. };
  64. struct enqueue_action
  65. {
  66. template <class EVT,class FSM,class SourceState,class TargetState>
  67. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  68. {
  69. fsm.template process_event(event2());
  70. }
  71. };
  72. struct expected_action
  73. {
  74. template <class EVT,class FSM,class SourceState,class TargetState>
  75. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  76. {
  77. ++fsm.expected_action_counter;
  78. }
  79. };
  80. struct expected_action2
  81. {
  82. template <class EVT,class FSM,class SourceState,class TargetState>
  83. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  84. {
  85. ++fsm.expected_action2_counter;
  86. }
  87. };
  88. // the initial state of the player SM. Must be defined
  89. typedef mpl::vector<State11> initial_state;
  90. // Transition table for player
  91. struct transition_table : mpl::vector<
  92. // Start Event Next Action Guard
  93. // +---------+-------------+---------+---------------------+----------------------+
  94. Row < State11 , event1 , State12 , enqueue_action >,
  95. Row < State12 , event2 , State13 , expected_action2 >,
  96. Row < State12 , eventd , State13 , expected_action >,
  97. Row < State13 , event2 , State11 >,
  98. Row < State13 , eventd , State11 >
  99. // +---------+-------------+---------+---------------------+----------------------+
  100. > {};
  101. // Replaces the default no-transition response.
  102. template <class FSM,class Event>
  103. void no_transition(Event const& , FSM&,int )
  104. {
  105. BOOST_FAIL("no_transition called!");
  106. }
  107. // init counters
  108. template <class Event,class FSM>
  109. void on_entry(Event const&,FSM& fsm)
  110. {
  111. fsm.template get_state<player_::State11&>().entry_counter=0;
  112. fsm.template get_state<player_::State11&>().exit_counter=0;
  113. fsm.template get_state<player_::State12&>().entry_counter=0;
  114. fsm.template get_state<player_::State12&>().exit_counter=0;
  115. fsm.template get_state<player_::State13&>().entry_counter=0;
  116. fsm.template get_state<player_::State13&>().exit_counter=0;
  117. }
  118. int expected_action_counter;
  119. int expected_action2_counter;
  120. };
  121. // Pick a back-end
  122. typedef msm::back::state_machine<player_> player;
  123. BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2 )
  124. {
  125. player p;
  126. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  127. p.start();
  128. p.process_event(eventd());
  129. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
  130. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 1,"State11 entry not called correctly");
  131. p.process_event(event1());
  132. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
  133. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().exit_counter == 1,"State11 exit not called correctly");
  134. BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().entry_counter == 1,"State12 entry not called correctly");
  135. BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().exit_counter == 1,"State12 exit not called correctly");
  136. BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 2,"State11 entry not called correctly");
  137. BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().exit_counter == 1,"State13 exit not called correctly");
  138. BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().entry_counter == 1,"State13 entry not called correctly");
  139. BOOST_CHECK_MESSAGE(p.expected_action_counter == 0,"expected_action should have been called");
  140. BOOST_CHECK_MESSAGE(p.expected_action2_counter == 1,"expected_action2 should not have been called");
  141. }
  142. }