1 | // Copyright 2010 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 | #ifndef BOOST_MSM_NONSTANDALONE_TEST |
18 | #define BOOST_TEST_MODULE MyTest |
19 | #endif |
20 | #include <boost/test/unit_test.hpp> |
21 | |
22 | namespace msm = boost::msm; |
23 | namespace mpl = boost::mpl; |
24 | using namespace boost::msm::front; |
25 | |
26 | namespace |
27 | { |
28 | // events |
29 | struct play {}; |
30 | struct end_pause {}; |
31 | struct stop {}; |
32 | struct pause {}; |
33 | struct open_close {}; |
34 | struct NextSong {}; |
35 | struct PreviousSong {}; |
36 | struct error_found {}; |
37 | struct end_error {}; |
38 | struct do_terminate {}; |
39 | |
40 | // Flags. Allow information about a property of the current state |
41 | struct PlayingPaused{}; |
42 | struct CDLoaded {}; |
43 | struct FirstSongPlaying {}; |
44 | |
45 | // A "complicated" event type that carries some data. |
46 | struct cd_detected |
47 | { |
48 | cd_detected(std::string name) |
49 | : name(name) |
50 | {} |
51 | |
52 | std::string name; |
53 | }; |
54 | |
55 | // front-end: define the FSM structure |
56 | struct player_ : public msm::front::state_machine_def<player_> |
57 | { |
58 | // we want deferred events and no state requires deferred events (only the fsm in the |
59 | // transition table), so the fsm does. |
60 | typedef int activate_deferred_events; |
61 | |
62 | unsigned int start_playback_counter; |
63 | unsigned int can_close_drawer_counter; |
64 | unsigned int report_error_counter; |
65 | unsigned int report_end_error_counter; |
66 | |
67 | player_(): |
68 | start_playback_counter(0), |
69 | can_close_drawer_counter(0), |
70 | report_error_counter(0), |
71 | report_end_error_counter(0) |
72 | {} |
73 | // The list of FSM states |
74 | struct Empty : public msm::front::state<> |
75 | { |
76 | template <class Event,class FSM> |
77 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
78 | template <class Event,class FSM> |
79 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
80 | int entry_counter; |
81 | int exit_counter; |
82 | }; |
83 | struct Open : public msm::front::state<> |
84 | { |
85 | typedef mpl::vector1<CDLoaded> flag_list; |
86 | |
87 | template <class Event,class FSM> |
88 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
89 | template <class Event,class FSM> |
90 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
91 | int entry_counter; |
92 | int exit_counter; |
93 | }; |
94 | |
95 | struct Stopped : public msm::front::state<> |
96 | { |
97 | typedef mpl::vector1<CDLoaded> flag_list; |
98 | |
99 | template <class Event,class FSM> |
100 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
101 | template <class Event,class FSM> |
102 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
103 | int entry_counter; |
104 | int exit_counter; |
105 | }; |
106 | |
107 | // the player state machine contains a state which is himself a state machine |
108 | // as you see, no need to declare it anywhere so Playing can be developed separately |
109 | // by another team in another module. For simplicity I just declare it inside player |
110 | struct Playing_ : public msm::front::state_machine_def<Playing_> |
111 | { |
112 | // when playing, the CD is loaded and we are in either pause or playing (duh) |
113 | typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list; |
114 | |
115 | template <class Event,class FSM> |
116 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
117 | template <class Event,class FSM> |
118 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
119 | int entry_counter; |
120 | int exit_counter; |
121 | unsigned int start_next_song_counter; |
122 | unsigned int start_prev_song_guard_counter; |
123 | |
124 | Playing_(): |
125 | start_next_song_counter(0), |
126 | start_prev_song_guard_counter(0) |
127 | {} |
128 | |
129 | // The list of FSM states |
130 | struct Song1 : public msm::front::state<> |
131 | { |
132 | typedef mpl::vector1<FirstSongPlaying> flag_list; |
133 | |
134 | template <class Event,class FSM> |
135 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
136 | template <class Event,class FSM> |
137 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
138 | int entry_counter; |
139 | int exit_counter; |
140 | }; |
141 | struct Song2 : public msm::front::state<> |
142 | { |
143 | template <class Event,class FSM> |
144 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
145 | template <class Event,class FSM> |
146 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
147 | int entry_counter; |
148 | int exit_counter; |
149 | }; |
150 | struct Song3 : public msm::front::state<> |
151 | { |
152 | template <class Event,class FSM> |
153 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
154 | template <class Event,class FSM> |
155 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
156 | int entry_counter; |
157 | int exit_counter; |
158 | }; |
159 | // the initial state. Must be defined |
160 | typedef Song1 initial_state; |
161 | // transition actions |
162 | void start_next_song(NextSong const&) {++start_next_song_counter; } |
163 | void start_prev_song(PreviousSong const&) { } |
164 | // guard conditions |
165 | bool start_prev_song_guard(PreviousSong const&) {++start_prev_song_guard_counter;return true; } |
166 | |
167 | typedef Playing_ pl; // makes transition table cleaner |
168 | // Transition table for Playing |
169 | struct transition_table : mpl::vector4< |
170 | // Start Event Next Action Guard |
171 | // +---------+-------------+---------+---------------------+----------------------+ |
172 | _row < Song1 , NextSong , Song2 >, |
173 | row < Song2 , PreviousSong, Song1 , &pl::start_prev_song,&pl::start_prev_song_guard>, |
174 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, |
175 | g_row < Song3 , PreviousSong, Song2 ,&pl::start_prev_song_guard> |
176 | // +---------+-------------+---------+---------------------+----------------------+ |
177 | > {}; |
178 | // Replaces the default no-transition response. |
179 | template <class FSM,class Event> |
180 | void no_transition(Event const&, FSM&,int) |
181 | { |
182 | BOOST_FAIL("no_transition called!" ); |
183 | } |
184 | }; |
185 | // back-end |
186 | typedef msm::back::state_machine<Playing_> Playing; |
187 | |
188 | // state not defining any entry or exit |
189 | struct Paused : public msm::front::state<> |
190 | { |
191 | typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list; |
192 | |
193 | template <class Event,class FSM> |
194 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
195 | template <class Event,class FSM> |
196 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
197 | int entry_counter; |
198 | int exit_counter; |
199 | }; |
200 | struct AllOk : public msm::front::state<> |
201 | { |
202 | template <class Event,class FSM> |
203 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
204 | template <class Event,class FSM> |
205 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
206 | int entry_counter; |
207 | int exit_counter; |
208 | }; |
209 | // this state is also made terminal so that all the events are blocked |
210 | struct ErrorMode : //public msm::front::terminate_state<> // ErrorMode terminates the state machine |
211 | public msm::front::interrupt_state<end_error> // ErroMode just interrupts. Will resume if |
212 | // the event end_error is generated |
213 | { |
214 | template <class Event,class FSM> |
215 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
216 | template <class Event,class FSM> |
217 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
218 | int entry_counter; |
219 | int exit_counter; |
220 | }; |
221 | struct ErrorTerminate : public msm::front::terminate_state<> // terminates the state machine |
222 | { |
223 | template <class Event,class FSM> |
224 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
225 | template <class Event,class FSM> |
226 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
227 | int entry_counter; |
228 | int exit_counter; |
229 | }; |
230 | // the initial state of the player SM. Must be defined |
231 | typedef mpl::vector<Empty,AllOk> initial_state; |
232 | |
233 | // transition actions |
234 | void start_playback(play const&) {++start_playback_counter; } |
235 | void open_drawer(open_close const&) { } |
236 | void store_cd_info(cd_detected const&) { } |
237 | void stop_playback(stop const&) { } |
238 | void pause_playback(pause const&) { } |
239 | void resume_playback(end_pause const&) { } |
240 | void stop_and_open(open_close const&) { } |
241 | void stopped_again(stop const&){} |
242 | void report_error(error_found const&) {++report_error_counter;} |
243 | void report_end_error(end_error const&) {++report_end_error_counter;} |
244 | |
245 | //guards |
246 | bool can_close_drawer(open_close const&) |
247 | { |
248 | ++can_close_drawer_counter; |
249 | return true; |
250 | } |
251 | typedef player_ p; // makes transition table cleaner |
252 | |
253 | // Transition table for player |
254 | struct transition_table : mpl::vector< |
255 | // Start Event Next Action Guard |
256 | // +---------+-------------+---------+---------------------+----------------------+ |
257 | a_row < Stopped , play , Playing , &p::start_playback >, |
258 | a_row < Stopped , open_close , Open , &p::open_drawer >, |
259 | _row < Stopped , stop , Stopped >, |
260 | // +---------+-------------+---------+---------------------+----------------------+ |
261 | g_row < Open , open_close , Empty , &p::can_close_drawer >, |
262 | Row < Open , play , none , Defer , none >, |
263 | // +---------+-------------+---------+---------------------+----------------------+ |
264 | a_row < Empty , open_close , Open , &p::open_drawer >, |
265 | a_row < Empty , cd_detected , Stopped , &p::store_cd_info >, |
266 | Row < Empty , play , none , Defer , none >, |
267 | // +---------+-------------+---------+---------------------+----------------------+ |
268 | a_row < Playing , stop , Stopped , &p::stop_playback >, |
269 | a_row < Playing , pause , Paused , &p::pause_playback >, |
270 | a_row < Playing , open_close , Open , &p::stop_and_open >, |
271 | // +---------+-------------+---------+---------------------+----------------------+ |
272 | a_row < Paused , end_pause , Playing , &p::resume_playback >, |
273 | a_row < Paused , stop , Stopped , &p::stop_playback >, |
274 | a_row < Paused , open_close , Open , &p::stop_and_open >, |
275 | // +---------+-------------+---------+---------------------+----------------------+ |
276 | a_row < AllOk , error_found ,ErrorMode, &p::report_error >, |
277 | a_row <ErrorMode, end_error ,AllOk , &p::report_end_error >, |
278 | _row < AllOk , do_terminate,ErrorTerminate > |
279 | // +---------+-------------+---------+---------------------+----------------------+ |
280 | > {}; |
281 | |
282 | // Replaces the default no-transition response. |
283 | template <class FSM,class Event> |
284 | void no_transition(Event const& , FSM&,int) |
285 | { |
286 | BOOST_FAIL("no_transition called!" ); |
287 | } |
288 | // init counters |
289 | template <class Event,class FSM> |
290 | void on_entry(Event const&,FSM& fsm) |
291 | { |
292 | fsm.template get_state<player_::Stopped&>().entry_counter=0; |
293 | fsm.template get_state<player_::Stopped&>().exit_counter=0; |
294 | fsm.template get_state<player_::Open&>().entry_counter=0; |
295 | fsm.template get_state<player_::Open&>().exit_counter=0; |
296 | fsm.template get_state<player_::Empty&>().entry_counter=0; |
297 | fsm.template get_state<player_::Empty&>().exit_counter=0; |
298 | fsm.template get_state<player_::Playing&>().entry_counter=0; |
299 | fsm.template get_state<player_::Playing&>().exit_counter=0; |
300 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0; |
301 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0; |
302 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0; |
303 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0; |
304 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0; |
305 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0; |
306 | fsm.template get_state<player_::Paused&>().entry_counter=0; |
307 | fsm.template get_state<player_::Paused&>().exit_counter=0; |
308 | fsm.template get_state<player_::AllOk&>().entry_counter=0; |
309 | fsm.template get_state<player_::AllOk&>().exit_counter=0; |
310 | fsm.template get_state<player_::ErrorMode&>().entry_counter=0; |
311 | fsm.template get_state<player_::ErrorMode&>().exit_counter=0; |
312 | fsm.template get_state<player_::ErrorTerminate&>().entry_counter=0; |
313 | fsm.template get_state<player_::ErrorTerminate&>().exit_counter=0; |
314 | } |
315 | }; |
316 | // Pick a back-end |
317 | typedef msm::back::state_machine<player_> player; |
318 | |
319 | //static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" }; |
320 | |
321 | BOOST_AUTO_TEST_CASE( my_test ) |
322 | { |
323 | player p; |
324 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM |
325 | p.start(); |
326 | // test deferred event |
327 | // deferred in Empty and Open, will be handled only after event cd_detected |
328 | p.process_event(evt: play()); |
329 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active" ); //Empty |
330 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 0,"Open exit not called correctly" ); |
331 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 0,"Playing entry not called correctly" ); |
332 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly" ); |
333 | //flags |
334 | BOOST_CHECK_MESSAGE(p.is_flag_active<CDLoaded>() == false,"CDLoaded should not be active" ); |
335 | |
336 | p.process_event(evt: open_close()); |
337 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active" ); //Open |
338 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly" ); |
339 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly" ); |
340 | |
341 | p.process_event(evt: open_close()); |
342 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active" ); //Empty |
343 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly" ); |
344 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly" ); |
345 | BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly" ); |
346 | |
347 | //deferred event should have been processed |
348 | p.process_event(evt: cd_detected("louie, louie" )); |
349 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
350 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly" ); |
351 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly" ); |
352 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly" ); |
353 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly" ); |
354 | BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly" ); |
355 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active" ); |
356 | BOOST_CHECK_MESSAGE( |
357 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1, |
358 | "Song1 entry not called correctly" ); |
359 | |
360 | //flags |
361 | BOOST_CHECK_MESSAGE(p.is_flag_active<PlayingPaused>() == true,"PlayingPaused should be active" ); |
362 | BOOST_CHECK_MESSAGE(p.is_flag_active<FirstSongPlaying>() == true,"FirstSongPlaying should be active" ); |
363 | |
364 | |
365 | p.process_event(evt: NextSong()); |
366 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
367 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active" ); |
368 | BOOST_CHECK_MESSAGE( |
369 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1, |
370 | "Song2 entry not called correctly" ); |
371 | BOOST_CHECK_MESSAGE( |
372 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1, |
373 | "Song1 exit not called correctly" ); |
374 | BOOST_CHECK_MESSAGE( |
375 | p.get_state<player_::Playing&>().start_next_song_counter == 0, |
376 | "submachine action not called correctly" ); |
377 | |
378 | p.process_event(evt: NextSong()); |
379 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
380 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active" ); |
381 | BOOST_CHECK_MESSAGE( |
382 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1, |
383 | "Song3 entry not called correctly" ); |
384 | BOOST_CHECK_MESSAGE( |
385 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1, |
386 | "Song2 exit not called correctly" ); |
387 | BOOST_CHECK_MESSAGE( |
388 | p.get_state<player_::Playing&>().start_next_song_counter == 1, |
389 | "submachine action not called correctly" ); |
390 | |
391 | p.process_event(evt: PreviousSong()); |
392 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
393 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active" ); |
394 | BOOST_CHECK_MESSAGE( |
395 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2, |
396 | "Song2 entry not called correctly" ); |
397 | BOOST_CHECK_MESSAGE( |
398 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1, |
399 | "Song3 exit not called correctly" ); |
400 | BOOST_CHECK_MESSAGE( |
401 | p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1, |
402 | "submachine guard not called correctly" ); |
403 | //flags |
404 | BOOST_CHECK_MESSAGE(p.is_flag_active<PlayingPaused>() == true,"PlayingPaused should be active" ); |
405 | BOOST_CHECK_MESSAGE(p.is_flag_active<FirstSongPlaying>() == false,"FirstSongPlaying should not be active" ); |
406 | |
407 | p.process_event(evt: pause()); |
408 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
409 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly" ); |
410 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly" ); |
411 | //flags |
412 | BOOST_CHECK_MESSAGE(p.is_flag_active<PlayingPaused>() == true,"PlayingPaused should be active" ); |
413 | |
414 | // go back to Playing |
415 | p.process_event(evt: end_pause()); |
416 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
417 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly" ); |
418 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly" ); |
419 | |
420 | p.process_event(evt: pause()); |
421 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
422 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
423 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly" ); |
424 | |
425 | p.process_event(evt: stop()); |
426 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
427 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly" ); |
428 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly" ); |
429 | //flags |
430 | BOOST_CHECK_MESSAGE(p.is_flag_active<PlayingPaused>() == false,"PlayingPaused should not be active" ); |
431 | BOOST_CHECK_MESSAGE(p.is_flag_active<CDLoaded>() == true,"CDLoaded should be active" ); |
432 | //BOOST_CHECK_MESSAGE(p.is_flag_active<CDLoaded,player::Flag_AND>() == false,"CDLoaded with AND should not be active"); |
433 | |
434 | p.process_event(evt: stop()); |
435 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
436 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly" ); |
437 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
438 | |
439 | //test interrupt |
440 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 5,"AllOk should be active" ); //AllOk |
441 | p.process_event(evt: error_found()); |
442 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 6,"ErrorMode should be active" ); //ErrorMode |
443 | BOOST_CHECK_MESSAGE(p.get_state<player_::AllOk&>().exit_counter == 1,"AllOk exit not called correctly" ); |
444 | BOOST_CHECK_MESSAGE(p.get_state<player_::ErrorMode&>().entry_counter == 1,"ErrorMode entry not called correctly" ); |
445 | |
446 | // try generating more events |
447 | p.process_event(evt: play()); |
448 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 6,"ErrorMode should be active" ); //ErrorMode |
449 | BOOST_CHECK_MESSAGE(p.get_state<player_::AllOk&>().exit_counter == 1,"AllOk exit not called correctly" ); |
450 | BOOST_CHECK_MESSAGE(p.get_state<player_::ErrorMode&>().entry_counter == 1,"ErrorMode entry not called correctly" ); |
451 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
452 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly" ); |
453 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
454 | |
455 | p.process_event(evt: end_error()); |
456 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 5,"AllOk should be active" ); //AllOk |
457 | BOOST_CHECK_MESSAGE(p.get_state<player_::ErrorMode&>().exit_counter == 1,"ErrorMode exit not called correctly" ); |
458 | BOOST_CHECK_MESSAGE(p.get_state<player_::AllOk&>().entry_counter == 2,"AllOk entry not called correctly" ); |
459 | |
460 | p.process_event(evt: play()); |
461 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
462 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 3,"Stopped exit not called correctly" ); |
463 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 3,"Playing entry not called correctly" ); |
464 | |
465 | //test terminate |
466 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 5,"AllOk should be active" ); //AllOk |
467 | p.process_event(evt: do_terminate()); |
468 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 7,"ErrorTerminate should be active" ); //ErrorTerminate |
469 | BOOST_CHECK_MESSAGE(p.get_state<player_::AllOk&>().exit_counter == 2,"AllOk exit not called correctly" ); |
470 | BOOST_CHECK_MESSAGE(p.get_state<player_::ErrorTerminate&>().entry_counter == 1,"ErrorTerminate entry not called correctly" ); |
471 | |
472 | // try generating more events |
473 | p.process_event(evt: stop()); |
474 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 7,"ErrorTerminate should be active" ); //ErrorTerminate |
475 | BOOST_CHECK_MESSAGE(p.get_state<player_::ErrorTerminate&>().exit_counter == 0,"ErrorTerminate exit not called correctly" ); |
476 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
477 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
478 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
479 | |
480 | p.process_event(evt: end_error()); |
481 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 7,"ErrorTerminate should be active" ); //ErrorTerminate |
482 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
483 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
484 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
485 | |
486 | p.process_event(evt: stop()); |
487 | BOOST_CHECK_MESSAGE(p.current_state()[1] == 7,"ErrorTerminate should be active" ); //ErrorTerminate |
488 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
489 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
490 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
491 | |
492 | } |
493 | } |
494 | |
495 | |