1 //////////////////////////////////////////////////////////////////////////////
2 // Copyright 2005-2006 Andreas Huber Doenni
3 // Distributed under the Boost Software License, Version 1.0. (See accompany-
4 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 //////////////////////////////////////////////////////////////////////////////
9 #include <boost/statechart/state_machine.hpp>
10 #include <boost/statechart/event.hpp>
11 #include <boost/statechart/simple_state.hpp>
12 #include <boost/statechart/transition.hpp>
14 #include <boost/mpl/list.hpp>
16 #include <boost/test/test_tools.hpp>
24 namespace sc
= boost::statechart
;
25 namespace mpl
= boost::mpl
;
29 struct EvToA
: sc::event
< EvToA
> {};
30 struct EvToB
: sc::event
< EvToB
> {};
31 struct EvToD
: sc::event
< EvToD
> {};
32 struct EvToE
: sc::event
< EvToE
> {};
35 struct StateIterationTest
: sc::state_machine
< StateIterationTest
, A
>
38 //////////////////////////////////////////////////////////////////////////
41 void AssertInState( const std::string
& stateNames
) const
43 stateNamesCache_
.clear();
45 for ( state_iterator currentState
= state_begin();
46 currentState
!= state_end(); ++currentState
)
48 const StateNamesMap::const_iterator found
=
49 stateNamesMap_
.find( currentState
->dynamic_type() );
50 BOOST_REQUIRE( found
!= stateNamesMap_
.end() );
51 stateNamesCache_
.insert( found
->second
);
54 std::string::const_iterator expectedName
= stateNames
.begin();
56 BOOST_REQUIRE( stateNames
.size() == stateNamesCache_
.size() );
58 for ( StateNamesCache::const_iterator actualName
=
59 stateNamesCache_
.begin();
60 actualName
!= stateNamesCache_
.end(); ++actualName
, ++expectedName
)
62 BOOST_REQUIRE( ( *actualName
)[ 0 ] == *expectedName
);
67 //////////////////////////////////////////////////////////////////////////
68 typedef std::map
< state_base_type::id_type
, std::string
> StateNamesMap
;
69 typedef std::set
< std::string
> StateNamesCache
;
71 StateNamesMap stateNamesMap_
;
72 mutable StateNamesCache stateNamesCache_
;
77 struct B
: sc::simple_state
< B
, StateIterationTest
, mpl::list
< C
, D
> >
79 typedef sc::transition
< EvToA
, A
> reactions
;
82 struct A
: sc::simple_state
< A
, StateIterationTest
>
84 typedef sc::transition
< EvToB
, B
> reactions
;
89 struct E
: sc::simple_state
< E
, B::orthogonal
< 1 >, mpl::list
< F
, G
> >
91 typedef sc::transition
< EvToD
, D
> reactions
;
94 struct F
: sc::simple_state
< F
, E::orthogonal
< 0 > > {};
95 struct G
: sc::simple_state
< G
, E::orthogonal
< 1 > > {};
97 struct C
: sc::simple_state
< C
, B::orthogonal
< 0 > > {};
98 struct D
: sc::simple_state
< D
, B::orthogonal
< 1 > >
100 typedef sc::transition
< EvToE
, E
> reactions
;
103 StateIterationTest::StateIterationTest()
105 // We're not using custom type information to make this test work even when
106 // BOOST_STATECHART_USE_NATIVE_RTTI is defined
107 stateNamesMap_
[ A::static_type() ] = "A";
108 stateNamesMap_
[ B::static_type() ] = "B";
109 stateNamesMap_
[ C::static_type() ] = "C";
110 stateNamesMap_
[ D::static_type() ] = "D";
111 stateNamesMap_
[ E::static_type() ] = "E";
112 stateNamesMap_
[ F::static_type() ] = "F";
113 stateNamesMap_
[ G::static_type() ] = "G";
117 int test_main( int, char* [] )
119 StateIterationTest machine
;
120 machine
.AssertInState( "" );
123 machine
.AssertInState( "A" );
125 machine
.process_event( EvToB() );
126 machine
.AssertInState( "CD" );
128 machine
.process_event( EvToA() );
129 machine
.AssertInState( "A" );
131 machine
.process_event( EvToB() );
132 machine
.AssertInState( "CD" );
134 machine
.process_event( EvToE() );
135 machine
.AssertInState( "CFG" );
137 machine
.process_event( EvToD() );
138 machine
.AssertInState( "CD" );
140 machine
.process_event( EvToE() );
141 machine
.AssertInState( "CFG" );
143 machine
.process_event( EvToA() );
144 machine
.AssertInState( "A" );
147 machine
.AssertInState( "" );