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 //! Bitwise comparison manipulator implementation
10 // ***************************************************************************
12 #ifndef BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
13 #define BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
16 #include <boost/test/tools/detail/fwd.hpp>
17 #include <boost/test/tools/detail/indirections.hpp>
19 #include <boost/test/tools/assertion_result.hpp>
20 #include <boost/test/tools/assertion.hpp>
23 #include <climits> // for CHAR_BIT
25 #include <boost/test/detail/suppress_warnings.hpp>
27 //____________________________________________________________________________//
30 namespace test_tools {
32 // ************************************************************************** //
33 // ************** bitwise comparison manipulator ************** //
34 // ************************************************************************** //
36 //! Bitwise comparison manipulator
39 //____________________________________________________________________________//
42 operator<<( unit_test::lazy_ostream const&, bitwise ) { return 0; }
44 //____________________________________________________________________________//
48 /*!@brief Bitwise comparison of two operands
50 * This class constructs an @ref assertion_result that contains precise bit comparison information.
51 * In particular the location of the mismatches (if any) are printed in the assertion result.
53 template<typename Lhs, typename Rhs, typename E>
54 inline assertion_result
55 bitwise_compare(Lhs const& lhs, Rhs const& rhs, E const& expr )
57 assertion_result pr( true );
59 std::size_t left_bit_size = sizeof(Lhs)*CHAR_BIT;
60 std::size_t right_bit_size = sizeof(Rhs)*CHAR_BIT;
62 static Lhs const leftOne( 1 );
63 static Rhs const rightOne( 1 );
65 std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
67 for( std::size_t counter = 0; counter < total_bits; ++counter ) {
68 if( (lhs & ( leftOne << counter )) != (rhs & (rightOne << counter)) ) {
71 expr.report( pr.message().stream() );
72 pr.message() << "]. Bitwise comparison failed";
75 pr.message() << "\nMismatch at position " << counter;
79 if( left_bit_size != right_bit_size ) {
82 expr.report( pr.message().stream() );
83 pr.message() << "]. Bitwise comparison failed";
86 pr.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
92 //____________________________________________________________________________//
94 //! Returns an assertion_result using the bitwise comparison out of an expression
96 //! This is used as a modifer of the normal operator<< on expressions to use the
97 //! bitwise comparison.
99 //! @note Available only for compilers supporting the @c auto declaration.
100 template<typename T1, typename T2, typename T3, typename T4>
101 inline assertion_result
102 operator<<(assertion_evaluate_t<assertion::binary_expr<T1,T2,assertion::op::EQ<T3,T4> > > const& ae, bitwise )
104 return bitwise_compare( ae.m_e.lhs().value(), ae.m_e.rhs(), ae.m_e );
107 //____________________________________________________________________________//
110 operator<<( assertion_type const& , bitwise )
112 return CHECK_BUILT_ASSERTION;
115 //____________________________________________________________________________//
117 } // namespace tt_detail
118 } // namespace test_tools
121 #include <boost/test/detail/enable_warnings.hpp>
123 #endif // BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER