1 // (C) Copyright Gennadiy Rozental 2001.
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)
6 // See http://www.boost.org/libs/test for the library home page.
9 //!@brief Defines framework for automated assertion construction
10 // ***************************************************************************
12 #ifndef BOOST_TEST_TOOLS_ASSERTION_HPP_100911GER
13 #define BOOST_TEST_TOOLS_ASSERTION_HPP_100911GER
16 #include <boost/test/tools/assertion_result.hpp>
17 #include <boost/test/tools/detail/print_helper.hpp>
18 #include <boost/test/tools/detail/fwd.hpp>
21 #include <boost/type.hpp>
22 #include <boost/type_traits/decay.hpp>
23 #include <boost/mpl/assert.hpp>
24 #include <boost/utility/declval.hpp>
25 #include <boost/type_traits/remove_reference.hpp>
26 #include <boost/type_traits/remove_const.hpp>
29 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
33 #include <boost/test/detail/suppress_warnings.hpp>
35 //____________________________________________________________________________//
38 namespace test_tools {
41 // ************************************************************************** //
42 // ************** assertion::operators ************** //
43 // ************************************************************************** //
44 // precedence 4: ->*, .*
45 // precedence 5: *, /, %
47 // precedence 7: << , >>
48 // precedence 8: <, <=, > and >=
49 // precedence 9: == and !=
50 // precedence 10: bitwise AND
51 // precedence 11: bitwise XOR
52 // precedence 12: bitwise OR
53 // precedence 13: logical AND
55 // precedence 14: logical OR
57 // precedence 15: ternary conditional
59 // precedence 16: = and OP= operators
60 // precedence 17: throw operator
62 // precedence 18: comma
67 #define BOOST_TEST_FOR_EACH_COMP_OP(action) \
68 action( < , LT, >= ) \
70 action( > , GT, <= ) \
72 action( ==, EQ, != ) \
73 action( !=, NE, == ) \
76 //____________________________________________________________________________//
78 #ifndef BOOST_NO_CXX11_DECLTYPE
80 #define BOOST_TEST_FOR_EACH_CONST_OP(action)\
81 action(->*, MEMP, ->* ) \
83 action( * , MUL, * ) \
84 action( / , DIV, / ) \
85 action( % , MOD, % ) \
87 action( + , ADD, + ) \
88 action( - , SUB, - ) \
90 action( <<, LSH, << ) \
91 action( >>, RSH, >> ) \
93 BOOST_TEST_FOR_EACH_COMP_OP(action) \
95 action( & , BAND, & ) \
96 action( ^ , XOR, ^ ) \
97 action( | , BOR, | ) \
102 #define BOOST_TEST_FOR_EACH_CONST_OP(action)\
103 BOOST_TEST_FOR_EACH_COMP_OP(action) \
108 //____________________________________________________________________________//
110 #define BOOST_TEST_FOR_EACH_MUT_OP(action) \
111 action( = , SET , = ) \
112 action( +=, IADD, += ) \
113 action( -=, ISUB, -= ) \
114 action( *=, IMUL, *= ) \
115 action( /=, IDIV, /= ) \
116 action( %=, IMOD, %= ) \
117 action(<<=, ILSH, <<=) \
118 action(>>=, IRSH, >>=) \
119 action( &=, IAND, &= ) \
120 action( ^=, IXOR, ^= ) \
121 action( |=, IOR , |= ) \
124 //____________________________________________________________________________//
126 #ifndef BOOST_NO_CXX11_DECLTYPE
127 # define DEDUCE_RESULT_TYPE( oper ) \
128 decltype(boost::declval<Lhs>() oper boost::declval<Rhs>() ) optype; \
129 typedef typename boost::remove_reference<optype>::type \
132 # define DEDUCE_RESULT_TYPE( oper ) bool
135 #define DEFINE_CONST_OPER( oper, name, rev ) \
136 template<typename Lhs, typename Rhs, \
137 typename Enabler=void> \
139 typedef DEDUCE_RESULT_TYPE( oper ) result_type; \
142 eval( Lhs const& lhs, Rhs const& rhs ) \
144 return lhs oper rhs; \
147 template<typename PrevExprType> \
149 report( std::ostream& ostr, \
150 PrevExprType const& lhs, \
153 lhs.report( ostr ); \
155 << tt_detail::print_helper( rhs ); \
158 static char const* revert() \
159 { return " " #rev " "; } \
163 BOOST_TEST_FOR_EACH_CONST_OP( DEFINE_CONST_OPER )
165 #undef DEDUCE_RESULT_TYPE
166 #undef DEFINE_CONST_OPER
168 //____________________________________________________________________________//
172 // ************************************************************************** //
173 // ************** assertion::expression_base ************** //
174 // ************************************************************************** //
175 // Defines expression operators
177 template<typename Lhs, typename Rhs, typename OP> class binary_expr;
179 template<typename ExprType,typename ValType>
180 class expression_base {
183 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
185 struct RhsT : remove_const<typename remove_reference<T>::type> {};
187 #define ADD_OP_SUPPORT( oper, name, _ ) \
188 template<typename T> \
189 binary_expr<ExprType,T, \
190 op::name<ValType,typename RhsT<T>::type> > \
191 operator oper( T&& rhs ) \
193 return binary_expr<ExprType,T, \
194 op::name<ValType,typename RhsT<T>::type> > \
195 ( std::forward<ExprType>( \
196 *static_cast<ExprType*>(this) ), \
197 std::forward<T>(rhs) ); \
202 #define ADD_OP_SUPPORT( oper, name, _ ) \
203 template<typename T> \
204 binary_expr<ExprType,typename boost::decay<T const>::type, \
205 op::name<ValType,typename boost::decay<T const>::type> >\
206 operator oper( T const& rhs ) const \
208 typedef typename boost::decay<T const>::type Rhs; \
209 return binary_expr<ExprType,Rhs,op::name<ValType,Rhs> > \
210 ( *static_cast<ExprType const*>(this), \
216 BOOST_TEST_FOR_EACH_CONST_OP( ADD_OP_SUPPORT )
217 #undef ADD_OP_SUPPORT
219 #ifndef BOOST_NO_CXX11_AUTO_DECLARATIONS
220 // Disabled operators
223 operator ||( T const& /*rhs*/ )
225 BOOST_MPL_ASSERT_MSG(false, CANT_USE_LOGICAL_OPERATOR_OR_WITHIN_THIS_TESTING_TOOL, () );
227 return *static_cast<ExprType*>(this);
232 operator &&( T const& /*rhs*/ )
234 BOOST_MPL_ASSERT_MSG(false, CANT_USE_LOGICAL_OPERATOR_AND_WITHIN_THIS_TESTING_TOOL, () );
236 return *static_cast<ExprType*>(this);
241 BOOST_MPL_ASSERT_MSG(false, CANT_USE_TERNARY_OPERATOR_WITHIN_THIS_TESTING_TOOL, () );
248 // ************************************************************************** //
249 // ************** assertion::value_expr ************** //
250 // ************************************************************************** //
251 // simple value expression
254 class value_expr : public expression_base<value_expr<T>,typename remove_const<typename remove_reference<T>::type>::type> {
257 typedef T result_type;
260 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
261 value_expr( value_expr&& ve )
262 : m_value( std::forward<T>(ve.m_value) )
264 explicit value_expr( T&& val )
265 : m_value( std::forward<T>(val) )
268 explicit value_expr( T const& val )
273 // Specific expression interface
274 T const& value() const
278 void report( std::ostream& ostr ) const
280 ostr << tt_detail::print_helper( m_value );
283 // Mutating operators
284 #define ADD_OP_SUPPORT( OPER, ID, _ ) \
285 template<typename U> \
287 operator OPER( U const& rhs ) \
295 BOOST_TEST_FOR_EACH_MUT_OP( ADD_OP_SUPPORT )
296 #undef ADD_OP_SUPPORT
298 // expression interface
299 assertion_result evaluate( bool no_message = false ) const
301 assertion_result res( value() );
302 if( no_message || res )
305 format_message( res.message(), value() );
307 return tt_detail::format_assertion_result( "", res.message().str() );
312 static void format_message( wrap_stringstream& ostr, U const& v ) { ostr << "[(bool)" << v << " is false]"; }
313 static void format_message( wrap_stringstream& /*ostr*/, bool /*v*/ ) {}
314 static void format_message( wrap_stringstream& /*ostr*/, assertion_result const& /*v*/ ) {}
320 // ************************************************************************** //
321 // ************** assertion::binary_expr ************** //
322 // ************************************************************************** //
325 template<typename LExpr, typename Rhs, typename OP>
326 class binary_expr : public expression_base<binary_expr<LExpr,Rhs,OP>,typename OP::result_type> {
329 typedef typename OP::result_type result_type;
332 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
333 binary_expr( binary_expr&& be )
334 : m_lhs( std::forward<LExpr>(be.m_lhs) )
335 , m_rhs( std::forward<Rhs>(be.m_rhs) )
337 binary_expr( LExpr&& lhs, Rhs&& rhs )
338 : m_lhs( std::forward<LExpr>(lhs) )
339 , m_rhs( std::forward<Rhs>(rhs) )
342 binary_expr( LExpr const& lhs, Rhs const& rhs )
348 // Specific expression interface
349 result_type value() const
351 return OP::eval( m_lhs.value(), m_rhs );
353 void report( std::ostream& ostr ) const
355 return OP::report( ostr, m_lhs, m_rhs );
358 assertion_result evaluate( bool no_message = false ) const
360 assertion_result const expr_res( value() );
361 if( no_message || expr_res )
364 wrap_stringstream buff;
365 report( buff.stream() );
367 return tt_detail::format_assertion_result( buff.stream().str(), expr_res.message() );
370 // To support custom manipulators
371 LExpr const& lhs() const { return m_lhs; }
372 Rhs const& rhs() const { return m_rhs; }
379 // ************************************************************************** //
380 // ************** assertion::seed ************** //
381 // ************************************************************************** //
382 // seed added ot the input expression to form an assertion expression
386 // ->* is highest precedence left to right operator
389 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
390 operator->*( T&& v ) const
392 return value_expr<T>( std::forward<T>( v ) );
395 operator->*( T const& v ) const
397 return value_expr<T>( v );
402 #undef BOOST_TEST_FOR_EACH_CONST_OP
404 } // namespace assertion
405 } // namespace test_tools
408 #include <boost/test/detail/enable_warnings.hpp>
410 #endif // BOOST_TEST_TOOLS_ASSERTION_HPP_100911GER