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
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
17#include <boost/test/unit_test.hpp>
18
19namespace msm = boost::msm;
20namespace mpl = boost::mpl;
21using namespace boost::msm::front;
22
23namespace
24{
25 // events
26 struct event1 {};
27 struct event2 {};
28 struct event3 {};
29 struct eventd {};
30
31
32 // front-end: define the FSM structure
33 struct player_ : public msm::front::state_machine_def<player_>
34 {
35 // in this test, we inverse the order deferred queue / event queue
36 typedef int event_queue_before_deferred_queue;
37
38 player_()
39 :expected_action_counter(0),expected_action2_counter(0)
40 {}
41 // The list of FSM states
42 struct State11 : public msm::front::state<>
43 {
44 typedef boost::fusion::vector<eventd> deferred_events;
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 State12 : 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 State13 : public msm::front::state<>
63 {
64 template <class Event,class FSM>
65 void on_entry(Event const&,FSM& ) {++entry_counter;}
66 template <class Event,class FSM>
67 void on_exit(Event const&,FSM& ) {++exit_counter;}
68 int entry_counter;
69 int exit_counter;
70 };
71 struct enqueue_action
72 {
73 template <class EVT,class FSM,class SourceState,class TargetState>
74 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
75 {
76 fsm.template process_event(event2());
77 }
78 };
79 struct expected_action
80 {
81 template <class EVT,class FSM,class SourceState,class TargetState>
82 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
83 {
84 ++fsm.expected_action_counter;
85 }
86 };
87 struct expected_action2
88 {
89 template <class EVT,class FSM,class SourceState,class TargetState>
90 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
91 {
92 ++fsm.expected_action2_counter;
93 }
94 };
95 // the initial state of the player SM. Must be defined
96 typedef mpl::vector<State11> initial_state;
97
98
99 // Transition table for player
100 struct transition_table : mpl::vector<
101 // Start Event Next Action Guard
102 // +---------+-------------+---------+---------------------+----------------------+
103 Row < State11 , event1 , State12 , enqueue_action >,
104 Row < State12 , event2 , State13 , expected_action2 >,
105 Row < State12 , eventd , State13 , expected_action >,
106 Row < State13 , event2 , State11 >,
107 Row < State13 , eventd , State11 >
108 // +---------+-------------+---------+---------------------+----------------------+
109 > {};
110
111 // Replaces the default no-transition response.
112 template <class FSM,class Event>
113 void no_transition(Event const& , FSM&,int )
114 {
115 BOOST_FAIL("no_transition called!");
116 }
117 // init counters
118 template <class Event,class FSM>
119 void on_entry(Event const&,FSM& fsm)
120 {
121 fsm.template get_state<player_::State11&>().entry_counter=0;
122 fsm.template get_state<player_::State11&>().exit_counter=0;
123 fsm.template get_state<player_::State12&>().entry_counter=0;
124 fsm.template get_state<player_::State12&>().exit_counter=0;
125 fsm.template get_state<player_::State13&>().entry_counter=0;
126 fsm.template get_state<player_::State13&>().exit_counter=0;
127 }
128 int expected_action_counter;
129 int expected_action2_counter;
130 };
131 // Pick a back-end
132 typedef msm::back::state_machine<player_> player;
133
134 BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2 )
135 {
136 player p;
137 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
138 p.start();
139
140 p.process_event(evt: eventd());
141 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
142 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 1,"State11 entry not called correctly");
143
144 p.process_event(evt: event1());
145 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active");
146 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().exit_counter == 1,"State11 exit not called correctly");
147 BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().entry_counter == 1,"State12 entry not called correctly");
148 BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().exit_counter == 1,"State12 exit not called correctly");
149 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 2,"State11 entry not called correctly");
150 BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().exit_counter == 1,"State13 exit not called correctly");
151 BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().entry_counter == 1,"State13 entry not called correctly");
152 BOOST_CHECK_MESSAGE(p.expected_action_counter == 0,"expected_action should have been called");
153 BOOST_CHECK_MESSAGE(p.expected_action2_counter == 1,"expected_action2 should not have been called");
154 }
155}
156
157
158

source code of boost/libs/msm/test/TestDeferAndMessageQueue3.cpp