StateIterationTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //////////////////////////////////////////////////////////////////////////////
  6. #include <boost/statechart/state_machine.hpp>
  7. #include <boost/statechart/event.hpp>
  8. #include <boost/statechart/simple_state.hpp>
  9. #include <boost/statechart/transition.hpp>
  10. #include <boost/mpl/list.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <set>
  13. #include <map>
  14. #include <string>
  15. namespace sc = boost::statechart;
  16. namespace mpl = boost::mpl;
  17. struct EvToA : sc::event< EvToA > {};
  18. struct EvToB : sc::event< EvToB > {};
  19. struct EvToD : sc::event< EvToD > {};
  20. struct EvToE : sc::event< EvToE > {};
  21. struct A;
  22. struct StateIterationTest : sc::state_machine< StateIterationTest, A >
  23. {
  24. public:
  25. //////////////////////////////////////////////////////////////////////////
  26. StateIterationTest();
  27. void AssertInState( const std::string & stateNames ) const
  28. {
  29. stateNamesCache_.clear();
  30. for ( state_iterator currentState = state_begin();
  31. currentState != state_end(); ++currentState )
  32. {
  33. const StateNamesMap::const_iterator found =
  34. stateNamesMap_.find( currentState->dynamic_type() );
  35. BOOST_REQUIRE( found != stateNamesMap_.end() );
  36. stateNamesCache_.insert( found->second );
  37. }
  38. std::string::const_iterator expectedName = stateNames.begin();
  39. BOOST_REQUIRE( stateNames.size() == stateNamesCache_.size() );
  40. for ( StateNamesCache::const_iterator actualName =
  41. stateNamesCache_.begin();
  42. actualName != stateNamesCache_.end(); ++actualName, ++expectedName )
  43. {
  44. BOOST_REQUIRE( ( *actualName )[ 0 ] == *expectedName );
  45. }
  46. }
  47. private:
  48. //////////////////////////////////////////////////////////////////////////
  49. typedef std::map< state_base_type::id_type, std::string > StateNamesMap;
  50. typedef std::set< std::string > StateNamesCache;
  51. StateNamesMap stateNamesMap_;
  52. mutable StateNamesCache stateNamesCache_;
  53. };
  54. struct C;
  55. struct D;
  56. struct B : sc::simple_state< B, StateIterationTest, mpl::list< C, D > >
  57. {
  58. typedef sc::transition< EvToA, A > reactions;
  59. };
  60. struct A : sc::simple_state< A, StateIterationTest >
  61. {
  62. typedef sc::transition< EvToB, B > reactions;
  63. };
  64. struct F;
  65. struct G;
  66. struct E : sc::simple_state< E, B::orthogonal< 1 >, mpl::list< F, G > >
  67. {
  68. typedef sc::transition< EvToD, D > reactions;
  69. };
  70. struct F : sc::simple_state< F, E::orthogonal< 0 > > {};
  71. struct G : sc::simple_state< G, E::orthogonal< 1 > > {};
  72. struct C : sc::simple_state< C, B::orthogonal< 0 > > {};
  73. struct D : sc::simple_state< D, B::orthogonal< 1 > >
  74. {
  75. typedef sc::transition< EvToE, E > reactions;
  76. };
  77. StateIterationTest::StateIterationTest()
  78. {
  79. // We're not using custom type information to make this test work even when
  80. // BOOST_STATECHART_USE_NATIVE_RTTI is defined
  81. stateNamesMap_[ A::static_type() ] = "A";
  82. stateNamesMap_[ B::static_type() ] = "B";
  83. stateNamesMap_[ C::static_type() ] = "C";
  84. stateNamesMap_[ D::static_type() ] = "D";
  85. stateNamesMap_[ E::static_type() ] = "E";
  86. stateNamesMap_[ F::static_type() ] = "F";
  87. stateNamesMap_[ G::static_type() ] = "G";
  88. }
  89. int test_main( int, char* [] )
  90. {
  91. StateIterationTest machine;
  92. machine.AssertInState( "" );
  93. machine.initiate();
  94. machine.AssertInState( "A" );
  95. machine.process_event( EvToB() );
  96. machine.AssertInState( "CD" );
  97. machine.process_event( EvToA() );
  98. machine.AssertInState( "A" );
  99. machine.process_event( EvToB() );
  100. machine.AssertInState( "CD" );
  101. machine.process_event( EvToE() );
  102. machine.AssertInState( "CFG" );
  103. machine.process_event( EvToD() );
  104. machine.AssertInState( "CD" );
  105. machine.process_event( EvToE() );
  106. machine.AssertInState( "CFG" );
  107. machine.process_event( EvToA() );
  108. machine.AssertInState( "A" );
  109. machine.terminate();
  110. machine.AssertInState( "" );
  111. return 0;
  112. }