InvalidResultCopyTest.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <libs/statechart/test/ThrowingBoostAssert.hpp>
  7. #include <boost/statechart/state_machine.hpp>
  8. #include <boost/statechart/event.hpp>
  9. #include <boost/statechart/simple_state.hpp>
  10. #include <boost/statechart/custom_reaction.hpp>
  11. #include <boost/statechart/result.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <stdexcept> // std::logic_error
  14. namespace sc = boost::statechart;
  15. struct E : sc::event< E > {};
  16. struct A;
  17. struct InvalidResultCopyTest :
  18. sc::state_machine< InvalidResultCopyTest, A > {};
  19. struct A : sc::simple_state< A, InvalidResultCopyTest >
  20. {
  21. typedef sc::custom_reaction< E > reactions;
  22. sc::result react( const E & )
  23. {
  24. sc::result r( discard_event() );
  25. sc::result rCopy1( r );
  26. // Ensure the copy is consumed so that we're not accidentally tripping
  27. // the assert in the sc::result dtor
  28. sc::detail::result_utility::get_result( rCopy1 );
  29. // We must not make more than one copy of a result value
  30. sc::result rCopy2( r );
  31. return rCopy2;
  32. }
  33. };
  34. int test_main( int, char* [] )
  35. {
  36. InvalidResultCopyTest machine;
  37. machine.initiate();
  38. #ifdef NDEBUG
  39. BOOST_REQUIRE_NO_THROW( machine.process_event( E() ) );
  40. #else
  41. BOOST_REQUIRE_THROW( machine.process_event( E() ), std::logic_error );
  42. #endif
  43. return 0;
  44. }