9 #include <boost/test/unit_test.hpp>
17 namespace tt = boost::test_tools;
41 states::Pinging, states::Connected> {
45 const events::Connect& ) {
46 return states::Connecting{};
50 const events::Established& ) {
51 return states::Connected{};
55 const events::Ping& ) {
56 std::cout <<
"ping!" << std::endl;
57 setState(states::Pinging{});
62 const events::Pong& ) {
63 std::cout <<
"pong!" << std::endl;
64 return states::Connected{};
68 const events::Timeout& ) {
69 return states::Connecting{};
73 const events::Disconnect& ) {
74 return states::Disconnected{};
77 template <
typename State,
typename Event>
79 const Event& )
const {
83 template <
typename State,
typename...
Args>
84 void on_enter(
const State& ,
Args&&... ) {}
86 template <
typename State,
typename...
Args>
87 void on_exit(
const State& ,
Args&&... ) {}
89 template <
typename...
Args>
90 void on_process(
Args&&... ) {}
93 BOOST_AUTO_TEST_SUITE(Utilities)
98 sm.dispatch(events::Connect{});
99 BOOST_CHECK(sm.is(states::Connecting{}));
100 sm.dispatch(events::Established{});
101 BOOST_CHECK(sm.is(states::Connected{}));
102 sm.dispatch(events::Ping{});
103 sm.dispatch(events::Ping{});
104 sm.dispatch(events::Ping{});
105 sm.dispatch(events::Ping{});
106 BOOST_CHECK(sm.is(states::Connected{}));
107 sm.dispatch(events::Timeout{});
108 BOOST_CHECK(sm.is(states::Connecting{}));
109 sm.dispatch(events::Established{});
110 BOOST_CHECK(sm.is(states::Connected{}));
111 sm.dispatch(events::Disconnect{});
112 BOOST_CHECK(sm.is(states::Disconnected{}));
119 sm.dispatch(events::Disconnect{});
120 BOOST_CHECK(sm.terminated());
127 event_return on_event(
const states::Disconnected& ,
128 const events::Connect& ,
double f) {
129 std::cout <<
"f: " << f << std::endl;
130 return states::Connected{};
133 event_return on_event(
const states::Connected& ,
134 const events::Disconnect& ) {
135 std::cout <<
"disconnect!" << std::endl;
136 return states::Disconnected{};
139 template <
typename State,
typename Event,
typename...
Args>
140 event_return on_event(
const State& ,
const Event& ,
145 template <
typename...
Args>
146 void on_enter(
const Terminated& ,
Args&&... ) {
147 throw std::runtime_error(
"FSM terminated!");
150 template <
typename State,
typename...
Args>
151 void on_enter(
const State& ,
Args&&... ) {}
153 template <
typename State,
typename...
Args>
154 void on_exit(
const State& ,
Args&&... ) {}
155 template <
typename...
Args>
156 void on_process(
Args&&... ) {}
163 sm.dispatch(events::Connect{}, 42.);
164 BOOST_CHECK(sm.is(states::Connected{}));
165 sm.dispatch(events::Disconnect{});
166 BOOST_CHECK(sm.is(states::Disconnected{}));
167 sm.dispatch(events::Connect{}, -1.);
170 BOOST_REQUIRE_THROW(sm.dispatch(events::Disconnect{}, 9), std::runtime_error);
171 BOOST_CHECK(sm.terminated());
175 BOOST_REQUIRE_THROW(sm.dispatch(events::Connect{}), std::runtime_error);
177 BOOST_CHECK(sm.terminated());
180 sm.setState(states::Disconnected{});
181 BOOST_CHECK(sm.is(states::Disconnected{}));
182 sm.dispatch(events::Connect{}, -1.);
183 BOOST_CHECK(sm.is(states::Connected{}));
195 bool on_exit_called =
false;
196 bool on_enter_called =
false;
197 bool on_process_called =
false;
199 on_exit_called =
false;
200 on_enter_called =
false;
201 on_process_called =
false;
236 template <
typename State,
typename...
Args>
238 on_enter_called =
true;
241 template <
typename State,
typename...
Args>
243 on_exit_called =
true;
246 template <
typename...
Args>
248 on_process_called =
true;
254 BOOST_CHECK(sm.is(
S1{}));
257 BOOST_CHECK(sm.is(S2{}));
258 BOOST_CHECK(sm.on_exit_called);
259 BOOST_CHECK(sm.on_enter_called);
260 BOOST_CHECK(sm.on_process_called);
266 BOOST_CHECK(sm.is(S2{}));
268 BOOST_CHECK(sm.on_exit_called);
269 BOOST_CHECK(sm.on_enter_called);
270 BOOST_CHECK(sm.on_process_called);
275 BOOST_CHECK(sm.is(S2{}));
277 BOOST_CHECK(!sm.on_exit_called);
278 BOOST_CHECK(!sm.on_enter_called);
279 BOOST_CHECK(sm.on_process_called);
283 BOOST_CHECK(sm.is(S3{}));
285 BOOST_CHECK(sm.on_exit_called);
286 BOOST_CHECK(sm.on_enter_called);
287 BOOST_CHECK(sm.on_process_called);
291 BOOST_CHECK(sm.is(S1{}));
295 BOOST_CHECK(sm.terminated());
297 BOOST_CHECK(sm.on_exit_called);
298 BOOST_CHECK(sm.on_enter_called);
299 BOOST_CHECK(sm.on_process_called);
302 BOOST_AUTO_TEST_SUITE_END()