interaction_based.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 49312 $
  10. //
  11. // Description : Facilities to perform interaction-based testing
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_INTERACTION_BASED_HPP_112105GER
  14. #define BOOST_TEST_INTERACTION_BASED_HPP_112105GER
  15. // Boost.Test
  16. #include <boost/test/detail/config.hpp>
  17. #include <boost/test/detail/global_typedef.hpp>
  18. #include <boost/test/utils/wrap_stringstream.hpp>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. // Boost
  21. #include <boost/lexical_cast.hpp>
  22. //____________________________________________________________________________//
  23. // ************************************************************************** //
  24. // ************** BOOST_ITEST_EPOINT ************** //
  25. // ************************************************************************** //
  26. #define BOOST_ITEST_EPOINT( description ) \
  27. ::boost::itest::manager::instance().exception_point( BOOST_TEST_L(__FILE__), __LINE__, description )
  28. /**/
  29. // ************************************************************************** //
  30. // ************** BOOST_ITEST_DPOINT ************** //
  31. // ************************************************************************** //
  32. #define BOOST_ITEST_DPOINT() \
  33. ::boost::itest::manager::instance().decision_point( BOOST_TEST_L(__FILE__), __LINE__ )
  34. /**/
  35. // ************************************************************************** //
  36. // ************** BOOST_ITEST_SCOPE ************** //
  37. // ************************************************************************** //
  38. #define BOOST_ITEST_SCOPE( scope_name ) \
  39. ::boost::itest::scope_guard itest_scope_guard ## __LINE__( BOOST_TEST_L(__FILE__), __LINE__, BOOST_STRINGIZE(scope_name) )
  40. /**/
  41. // ************************************************************************** //
  42. // ************** BOOST_ITEST_NEW ************** //
  43. // ************************************************************************** //
  44. #define BOOST_ITEST_NEW( type_name ) \
  45. new ( ::boost::itest::location( BOOST_TEST_L(__FILE__), __LINE__ ) ) type_name
  46. /**/
  47. // ************************************************************************** //
  48. // ************** BOOST_ITEST_DATA_FLOW ************** //
  49. // ************************************************************************** //
  50. #define BOOST_ITEST_DATA_FLOW( v ) \
  51. ::boost::itest::manager::instance().generic_data_flow( v )
  52. /**/
  53. // ************************************************************************** //
  54. // ************** BOOST_ITEST_RETURN ************** //
  55. // ************************************************************************** //
  56. #define BOOST_ITEST_RETURN( type, default_value ) \
  57. ::boost::itest::manager::instance().generic_return<type>( default_value )
  58. /**/
  59. // ************************************************************************** //
  60. // ************** BOOST_ITEST_MOCK_FUNC ************** //
  61. // ************************************************************************** //
  62. #define BOOST_ITEST_MOCK_FUNC( function_name ) \
  63. BOOST_ITEST_SCOPE( function_name ); \
  64. BOOST_ITEST_EPOINT( 0 ); \
  65. return ::boost::itest::mock_object<>::prototype(); \
  66. /**/
  67. namespace boost {
  68. namespace itest { // interaction-based testing
  69. using unit_test::const_string;
  70. // ************************************************************************** //
  71. // ************** manager ************** //
  72. // ************************************************************************** //
  73. class BOOST_TEST_DECL manager {
  74. public:
  75. // instance access
  76. static manager& instance() { return *instance_ptr(); }
  77. // Mock objects interface hooks
  78. virtual void exception_point( const_string /*file*/,
  79. std::size_t /*line_num*/,
  80. const_string /*descr*/ ){}
  81. virtual bool decision_point( const_string /*file*/,
  82. std::size_t /*line_num*/ ) { return true; }
  83. virtual unsigned enter_scope( const_string /*file*/,
  84. std::size_t /*line_num*/,
  85. const_string /*scope_name*/){ return 0; }
  86. virtual void leave_scope( unsigned ) {}
  87. virtual void allocated( const_string /*file*/,
  88. std::size_t /*line_num*/,
  89. void* /*p*/, std::size_t /*s*/ ) {}
  90. virtual void freed( void* /*p*/ ) {}
  91. virtual void data_flow( const_string /*d*/ ) {}
  92. virtual std::string return_value( const_string /*default_value */ ) { return ""; }
  93. template<typename T>
  94. void generic_data_flow( T const& t )
  95. {
  96. wrap_stringstream ws;
  97. data_flow( (ws << t).str() );
  98. }
  99. template<typename T, typename DefaultValueType>
  100. T generic_return( DefaultValueType const& dv )
  101. {
  102. wrap_stringstream ws;
  103. std::string const& res = return_value( (ws << dv).str() );
  104. if( res.empty() )
  105. return dv;
  106. return lexical_cast<T>( res );
  107. }
  108. protected:
  109. manager();
  110. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  111. public:
  112. #endif
  113. BOOST_TEST_PROTECTED_VIRTUAL ~manager();
  114. private:
  115. struct dummy_constr{};
  116. explicit manager( dummy_constr* ) {}
  117. static manager* instance_ptr( bool reset = false, manager* ptr = 0 );
  118. }; // manager
  119. // ************************************************************************** //
  120. // ************** scope_guard ************** //
  121. // ************************************************************************** //
  122. class scope_guard {
  123. public:
  124. // Constructor
  125. scope_guard( const_string file, std::size_t line_num, const_string scope_name )
  126. {
  127. m_scope_index = manager::instance().enter_scope( file, line_num, scope_name );
  128. }
  129. ~scope_guard()
  130. {
  131. manager::instance().leave_scope( m_scope_index );
  132. }
  133. unsigned m_scope_index;
  134. };
  135. // ************************************************************************** //
  136. // ************** location ************** //
  137. // ************************************************************************** //
  138. struct location {
  139. location( const_string file, std::size_t line )
  140. : m_file_name( file )
  141. , m_line_num( line )
  142. {}
  143. const_string m_file_name;
  144. std::size_t m_line_num;
  145. };
  146. } // namespace itest
  147. } // namespace boost
  148. // ************************************************************************** //
  149. // ************** operator new overload ************** //
  150. // ************************************************************************** //
  151. #if !defined(BOOST_ITEST_NO_NEW_OVERLOADS)
  152. // STL
  153. #include <cstdlib>
  154. # ifdef BOOST_NO_STDC_NAMESPACE
  155. namespace std { using ::malloc; using ::free; }
  156. # endif
  157. # ifdef _CRTDBG_MAP_ALLOC
  158. namespace std { using ::_malloc_dbg; using ::_free_dbg; }
  159. # endif
  160. inline void*
  161. operator new( std::size_t s, ::boost::itest::location const& l )
  162. {
  163. void* res = std::malloc(s ? s : 1);
  164. if( res )
  165. ::boost::itest::manager::instance().allocated( l.m_file_name, l.m_line_num, res, s );
  166. else
  167. throw std::bad_alloc();
  168. return res;
  169. }
  170. //____________________________________________________________________________//
  171. inline void*
  172. operator new[]( std::size_t s, ::boost::itest::location const& l )
  173. {
  174. void* res = std::malloc(s ? s : 1);
  175. if( res )
  176. ::boost::itest::manager::instance().allocated( l.m_file_name, l.m_line_num, res, s );
  177. else
  178. throw std::bad_alloc();
  179. return res;
  180. }
  181. //____________________________________________________________________________//
  182. inline void
  183. operator delete( void* p, ::boost::itest::location const& )
  184. {
  185. ::boost::itest::manager::instance().freed( p );
  186. std::free( p );
  187. }
  188. //____________________________________________________________________________//
  189. inline void
  190. operator delete[]( void* p, ::boost::itest::location const& )
  191. {
  192. ::boost::itest::manager::instance().freed( p );
  193. std::free( p );
  194. }
  195. //____________________________________________________________________________//
  196. #endif
  197. #include <boost/test/detail/enable_warnings.hpp>
  198. #endif // BOOST_TEST_INTERACTION_BASED_HPP_112105GER