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, >=, GE ) \
69 action( <=, LE, > , GT ) \
70 action( > , GT, <=, LE ) \
71 action( >=, GE, < , LT ) \
72 action( ==, EQ, !=, NE ) \
73 action( !=, NE, ==, EQ ) \
76 //____________________________________________________________________________//
78 #ifndef BOOST_NO_CXX11_DECLTYPE
80 #define BOOST_TEST_FOR_EACH_CONST_OP(action)\
81 action(->*, MEMP, ->*, MEMP ) \
83 action( * , MUL , * , MUL ) \
84 action( / , DIV , / , DIV ) \
85 action( % , MOD , % , MOD ) \
87 action( + , ADD , + , ADD ) \
88 action( - , SUB , - , SUB ) \
90 action( <<, LSH , << , LSH ) \
91 action( >>, RSH , >> , RSH ) \
93 BOOST_TEST_FOR_EACH_COMP_OP(action) \
95 action( & , BAND, & , BAND ) \
96 action( ^ , XOR , ^ , XOR ) \
97 action( | , BOR , | , 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 , = , SET ) \
112 action( +=, IADD, += , IADD ) \
113 action( -=, ISUB, -= , ISUB ) \
114 action( *=, IMUL, *= , IMUL ) \
115 action( /=, IDIV, /= , IDIV ) \
116 action( %=, IMOD, %= , IMOD ) \
117 action(<<=, ILSH, <<=, ILSH ) \
118 action(>>=, IRSH, >>=, IRSH ) \
119 action( &=, IAND, &= , IAND ) \
120 action( ^=, IXOR, ^= , IXOR ) \
121 action( |=, IOR , |= , 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_FWD_DECL( oper, name, rev, name_inverse ) \
136 template<typename Lhs, typename Rhs, \
137 typename Enabler=void> \
141 BOOST_TEST_FOR_EACH_CONST_OP( DEFINE_CONST_OPER_FWD_DECL )
143 #define DEFINE_CONST_OPER( oper, name, rev, name_inverse ) \
144 template<typename Lhs, typename Rhs, \
147 typedef DEDUCE_RESULT_TYPE( oper ) result_type; \
148 typedef name_inverse<Lhs, Rhs> inverse; \
151 eval( Lhs const& lhs, Rhs const& rhs ) \
153 return lhs oper rhs; \
156 template<typename PrevExprType> \
158 report( std::ostream& ostr, \
159 PrevExprType const& lhs, \
162 lhs.report( ostr ); \
164 << tt_detail::print_helper( rhs ); \
167 static char const* forward() \
168 { return " " #oper " "; } \
169 static char const* revert() \
170 { return " " #rev " "; } \
174 BOOST_TEST_FOR_EACH_CONST_OP( DEFINE_CONST_OPER )
176 #undef DEDUCE_RESULT_TYPE
177 #undef DEFINE_CONST_OPER
179 //____________________________________________________________________________//
183 // ************************************************************************** //
184 // ************** assertion::expression_base ************** //
185 // ************************************************************************** //
186 // Defines expression operators
188 template<typename Lhs, typename Rhs, typename OP> class binary_expr;
190 template<typename ExprType,typename ValType>
191 class expression_base {
194 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
196 struct RhsT : remove_const<typename remove_reference<T>::type> {};
198 #define ADD_OP_SUPPORT( oper, name, _, _i ) \
199 template<typename T> \
200 binary_expr<ExprType,T, \
201 op::name<ValType,typename RhsT<T>::type> > \
202 operator oper( T&& rhs ) \
204 return binary_expr<ExprType,T, \
205 op::name<ValType,typename RhsT<T>::type> > \
206 ( std::forward<ExprType>( \
207 *static_cast<ExprType*>(this) ), \
208 std::forward<T>(rhs) ); \
213 #define ADD_OP_SUPPORT( oper, name, _, _i ) \
214 template<typename T> \
215 binary_expr<ExprType,typename boost::decay<T const>::type, \
216 op::name<ValType,typename boost::decay<T const>::type> >\
217 operator oper( T const& rhs ) const \
219 typedef typename boost::decay<T const>::type Rhs; \
220 return binary_expr<ExprType,Rhs,op::name<ValType,Rhs> > \
221 ( *static_cast<ExprType const*>(this), \
227 BOOST_TEST_FOR_EACH_CONST_OP( ADD_OP_SUPPORT )
228 #undef ADD_OP_SUPPORT
230 #ifndef BOOST_NO_CXX11_AUTO_DECLARATIONS
231 // Disabled operators
234 operator ||( T const& /*rhs*/ )
236 BOOST_MPL_ASSERT_MSG(false, CANT_USE_LOGICAL_OPERATOR_OR_WITHIN_THIS_TESTING_TOOL, () );
238 return *static_cast<ExprType*>(this);
243 operator &&( T const& /*rhs*/ )
245 BOOST_MPL_ASSERT_MSG(false, CANT_USE_LOGICAL_OPERATOR_AND_WITHIN_THIS_TESTING_TOOL, () );
247 return *static_cast<ExprType*>(this);
252 BOOST_MPL_ASSERT_MSG(false, CANT_USE_TERNARY_OPERATOR_WITHIN_THIS_TESTING_TOOL, () );
259 // ************************************************************************** //
260 // ************** assertion::value_expr ************** //
261 // ************************************************************************** //
262 // simple value expression
265 class value_expr : public expression_base<value_expr<T>,typename remove_const<typename remove_reference<T>::type>::type> {
268 typedef T result_type;
271 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
272 value_expr( value_expr&& ve )
273 : m_value( std::forward<T>(ve.m_value) )
275 explicit value_expr( T&& val )
276 : m_value( std::forward<T>(val) )
279 explicit value_expr( T const& val )
284 // Specific expression interface
285 T const& value() const
289 void report( std::ostream& ostr ) const
291 ostr << tt_detail::print_helper( value() );
294 // Mutating operators
295 #define ADD_OP_SUPPORT( OPER, ID, _, _i)\
296 template<typename U> \
298 operator OPER( U const& rhs ) \
306 BOOST_TEST_FOR_EACH_MUT_OP( ADD_OP_SUPPORT )
307 #undef ADD_OP_SUPPORT
309 // expression interface
310 assertion_result evaluate( bool no_message = false ) const
312 assertion_result res( value() );
313 if( no_message || res )
316 format_message( res.message(), value() );
318 return tt_detail::format_assertion_result( "", res.message().str() );
323 static void format_message( wrap_stringstream& ostr, U const& v )
325 ostr << "['" << tt_detail::print_helper(v) << "' evaluates to false]";
327 static void format_message( wrap_stringstream& /*ostr*/, bool /*v*/ ) {}
328 static void format_message( wrap_stringstream& /*ostr*/, assertion_result const& /*v*/ ) {}
334 // ************************************************************************** //
335 // ************** assertion::binary_expr ************** //
336 // ************************************************************************** //
339 template<typename LExpr, typename Rhs, typename OP>
340 class binary_expr : public expression_base<binary_expr<LExpr,Rhs,OP>,typename OP::result_type> {
343 typedef typename OP::result_type result_type;
346 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
347 binary_expr( binary_expr&& be )
348 : m_lhs( std::forward<LExpr>(be.m_lhs) )
349 , m_rhs( std::forward<Rhs>(be.m_rhs) )
351 binary_expr( LExpr&& lhs, Rhs&& rhs )
352 : m_lhs( std::forward<LExpr>(lhs) )
353 , m_rhs( std::forward<Rhs>(rhs) )
356 binary_expr( LExpr const& lhs, Rhs const& rhs )
362 // Specific expression interface
363 result_type value() const
365 return OP::eval( m_lhs.value(), m_rhs );
367 void report( std::ostream& ostr ) const
369 return OP::report( ostr, m_lhs, m_rhs );
372 assertion_result evaluate( bool no_message = false ) const
374 assertion_result const expr_res( value() );
375 if( no_message || expr_res )
378 wrap_stringstream buff;
379 report( buff.stream() );
381 return tt_detail::format_assertion_result( buff.stream().str(), expr_res.message() );
384 // To support custom manipulators
385 LExpr const& lhs() const { return m_lhs; }
386 Rhs const& rhs() const { return m_rhs; }
393 // ************************************************************************** //
394 // ************** assertion::seed ************** //
395 // ************************************************************************** //
396 // seed added ot the input expression to form an assertion expression
400 // ->* is highest precedence left to right operator
403 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
404 operator->*( T&& v ) const
406 return value_expr<T>( std::forward<T>( v ) );
409 operator->*( T const& v ) const
411 return value_expr<T>( v );
416 #undef BOOST_TEST_FOR_EACH_CONST_OP
418 } // namespace assertion
419 } // namespace test_tools
422 #include <boost/test/detail/enable_warnings.hpp>
424 #endif // BOOST_TEST_TOOLS_ASSERTION_HPP_100911GER