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