sl@0: // (C) Copyright Gennadiy Rozental 2005. sl@0: // Distributed under the Boost Software License, Version 1.0. sl@0: // (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // See http://www.boost.org/libs/test for the library home page. sl@0: // sl@0: // File : $RCSfile: interaction_based.hpp,v $ sl@0: // sl@0: // Version : $Revision: 1.3 $ sl@0: // sl@0: // Description : Facilities to perform interaction-based testing sl@0: // *************************************************************************** sl@0: sl@0: #ifndef BOOST_TEST_INTERACTION_BASED_HPP_112105GER sl@0: #define BOOST_TEST_INTERACTION_BASED_HPP_112105GER sl@0: sl@0: // Boost.Test sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #include sl@0: sl@0: // Boost sl@0: #include sl@0: sl@0: //____________________________________________________________________________// sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_EPOINT ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_EPOINT( description ) \ sl@0: ::boost::itest::manager::instance().exception_point( BOOST_TEST_L(__FILE__), __LINE__, description ) sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_DPOINT ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_DPOINT() \ sl@0: ::boost::itest::manager::instance().decision_point( BOOST_TEST_L(__FILE__), __LINE__ ) sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_SCOPE ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_SCOPE( scope_name ) \ sl@0: ::boost::itest::scope_guard itest_scope_guard ## __LINE__( BOOST_TEST_L(__FILE__), __LINE__, BOOST_STRINGIZE(scope_name) ) sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_NEW ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_NEW( type_name ) \ sl@0: new ( ::boost::itest::location( BOOST_TEST_L(__FILE__), __LINE__ ) ) type_name sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_DATA_FLOW ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_DATA_FLOW( v ) \ sl@0: ::boost::itest::manager::instance().generic_data_flow( v ) sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_RETURN ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_RETURN( type, default_value ) \ sl@0: ::boost::itest::manager::instance().generic_return( default_value ) sl@0: /**/ sl@0: sl@0: // ************************************************************************** // sl@0: // ************** BOOST_ITEST_MOCK_FUNC ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #define BOOST_ITEST_MOCK_FUNC( function_name ) \ sl@0: BOOST_ITEST_SCOPE( function_name ); \ sl@0: BOOST_ITEST_EPOINT( 0 ); \ sl@0: return ::boost::itest::mock_object<>::prototype(); \ sl@0: /**/ sl@0: sl@0: namespace boost { sl@0: sl@0: namespace itest { // interaction-based testing sl@0: sl@0: using unit_test::const_string; sl@0: sl@0: // ************************************************************************** // sl@0: // ************** manager ************** // sl@0: // ************************************************************************** // sl@0: sl@0: class BOOST_TEST_DECL manager { sl@0: public: sl@0: // instance access sl@0: static manager& instance() { return *instance_ptr(); } sl@0: sl@0: // Mock objects interface hooks sl@0: virtual void exception_point( const_string /*file*/, sl@0: std::size_t /*line_num*/, sl@0: const_string /*descr*/ ){} sl@0: virtual bool decision_point( const_string /*file*/, sl@0: std::size_t /*line_num*/ ) { return true; } sl@0: virtual unsigned enter_scope( const_string /*file*/, sl@0: std::size_t /*line_num*/, sl@0: const_string /*scope_name*/){ return 0; } sl@0: virtual void leave_scope( unsigned ) {} sl@0: virtual void allocated( const_string /*file*/, sl@0: std::size_t /*line_num*/, sl@0: void* /*p*/, std::size_t /*s*/ ) {} sl@0: virtual void freed( void* /*p*/ ) {} sl@0: virtual void data_flow( const_string d ) {} sl@0: virtual std::string return_value( const_string default_value ) { return ""; } sl@0: sl@0: template sl@0: void generic_data_flow( T const& t ) sl@0: { sl@0: wrap_stringstream ws; sl@0: sl@0: data_flow( (ws << t).str() ); sl@0: } sl@0: template sl@0: T generic_return( DefaultValueType const& dv ) sl@0: { sl@0: wrap_stringstream ws; sl@0: sl@0: std::string const& res = return_value( (ws << dv).str() ); sl@0: sl@0: if( res.empty() ) sl@0: return dv; sl@0: sl@0: return lexical_cast( res ); sl@0: } sl@0: sl@0: protected: sl@0: manager(); sl@0: #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) sl@0: public: sl@0: #endif sl@0: BOOST_TEST_PROTECTED_VIRTUAL ~manager(); sl@0: sl@0: private: sl@0: struct dummy_constr{}; sl@0: explicit manager( dummy_constr* ) {} sl@0: sl@0: static manager* instance_ptr( bool reset = false, manager* ptr = 0 ); sl@0: }; // manager sl@0: sl@0: // ************************************************************************** // sl@0: // ************** scope_guard ************** // sl@0: // ************************************************************************** // sl@0: sl@0: class scope_guard { sl@0: public: sl@0: // Constructor sl@0: scope_guard( const_string file, std::size_t line_num, const_string scope_name ) sl@0: { sl@0: m_scope_index = manager::instance().enter_scope( file, line_num, scope_name ); sl@0: } sl@0: ~scope_guard() sl@0: { sl@0: manager::instance().leave_scope( m_scope_index ); sl@0: } sl@0: sl@0: unsigned m_scope_index; sl@0: }; sl@0: sl@0: // ************************************************************************** // sl@0: // ************** location ************** // sl@0: // ************************************************************************** // sl@0: sl@0: struct location { sl@0: location( const_string file, std::size_t line ) sl@0: : m_file_name( file ) sl@0: , m_line_num( line ) sl@0: {} sl@0: sl@0: const_string m_file_name; sl@0: std::size_t m_line_num; sl@0: }; sl@0: sl@0: } // namespace itest sl@0: sl@0: } // namespace boost sl@0: sl@0: // ************************************************************************** // sl@0: // ************** operator new overload ************** // sl@0: // ************************************************************************** // sl@0: sl@0: #if !defined(BOOST_ITEST_NO_NEW_OVERLOADS) sl@0: sl@0: // STL sl@0: #include sl@0: sl@0: # ifdef BOOST_NO_STDC_NAMESPACE sl@0: namespace std { using ::malloc; using ::free; } sl@0: # endif sl@0: sl@0: inline void* sl@0: operator new( std::size_t s, ::boost::itest::location const& l ) sl@0: { sl@0: void* res = std::malloc(s ? s : 1); sl@0: sl@0: if( res ) sl@0: ::boost::itest::manager::instance().allocated( l.m_file_name, l.m_line_num, res, s ); sl@0: else sl@0: throw std::bad_alloc(); sl@0: sl@0: return res; sl@0: } sl@0: sl@0: //____________________________________________________________________________// sl@0: sl@0: inline void* sl@0: operator new[]( std::size_t s, ::boost::itest::location const& l ) sl@0: { sl@0: void* res = std::malloc(s ? s : 1); sl@0: sl@0: if( res ) sl@0: ::boost::itest::manager::instance().allocated( l.m_file_name, l.m_line_num, res, s ); sl@0: else sl@0: throw std::bad_alloc(); sl@0: sl@0: return res; sl@0: } sl@0: sl@0: //____________________________________________________________________________// sl@0: sl@0: inline void sl@0: operator delete( void* p, ::boost::itest::location const& ) sl@0: { sl@0: ::boost::itest::manager::instance().freed( p ); sl@0: sl@0: std::free( p ); sl@0: } sl@0: sl@0: //____________________________________________________________________________// sl@0: sl@0: inline void sl@0: operator delete[]( void* p, ::boost::itest::location const& ) sl@0: { sl@0: ::boost::itest::manager::instance().freed( p ); sl@0: sl@0: std::free( p ); sl@0: } sl@0: sl@0: //____________________________________________________________________________// sl@0: sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: // *************************************************************************** sl@0: // Revision History : sl@0: // sl@0: // $Log: interaction_based.hpp,v $ sl@0: // Revision 1.3 2006/01/28 08:52:35 rogeeff sl@0: // operator new overloads made inline to: sl@0: // 1. prevent issues with export them from DLL sl@0: // 2. release link issue fixed sl@0: // sl@0: // Revision 1.2 2006/01/15 11:14:38 rogeeff sl@0: // simpl_mock -> mock_object<>::prototype() sl@0: // operator new need to be rethinked sl@0: // sl@0: // Revision 1.1 2005/12/14 05:09:21 rogeeff sl@0: // interraction based testing is introdused sl@0: // sl@0: // *************************************************************************** sl@0: sl@0: #endif // BOOST_TEST_INTERACTION_BASED_HPP_112105GER