Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:40

0001 //  (C) Copyright Gennadiy Rozental 2001.
0002 //  Distributed under the Boost Software License, Version 1.0.
0003 //  (See accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 //  See http://www.boost.org/libs/test for the library home page.
0007 //
0008 //! @file
0009 //! Bitwise comparison manipulator implementation
0010 // ***************************************************************************
0011 
0012 #ifndef BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
0013 #define BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
0014 
0015 // Boost Test
0016 #include <boost/test/tools/detail/fwd.hpp>
0017 #include <boost/test/tools/detail/indirections.hpp>
0018 
0019 #include <boost/test/tools/assertion_result.hpp>
0020 #include <boost/test/tools/assertion.hpp>
0021 
0022 // STL
0023 #include <climits>          // for CHAR_BIT
0024 
0025 #include <boost/test/detail/suppress_warnings.hpp>
0026 
0027 //____________________________________________________________________________//
0028 
0029 namespace boost {
0030 namespace test_tools {
0031 
0032 // ************************************************************************** //
0033 // **************        bitwise comparison manipulator        ************** //
0034 // ************************************************************************** //
0035 
0036 //! Bitwise comparison manipulator
0037 //! This is a terminal for the expression
0038 struct bitwise {};
0039 
0040 //____________________________________________________________________________//
0041 
0042 inline unit_test::lazy_ostream &
0043 operator<<( unit_test::lazy_ostream &o, bitwise )   { return o; }
0044 
0045 // needed for the lazy evaluation in lazy_ostream as bitwise is a terminal
0046 inline std::ostream& 
0047 operator<<( std::ostream& o, bitwise )              { return o; }
0048 
0049 
0050 //____________________________________________________________________________//
0051 
0052 namespace tt_detail {
0053 
0054 /*!@brief Bitwise comparison of two operands
0055  *
0056  * This class constructs an @ref assertion_result that contains precise bit comparison information.
0057  * In particular the location of the mismatches (if any) are printed in the assertion result. 
0058  */
0059 template<typename Lhs, typename Rhs, typename E>
0060 inline assertion_result
0061 bitwise_compare(Lhs const& lhs, Rhs const& rhs, E const& expr )
0062 {
0063     assertion_result    pr( true );
0064 
0065     std::size_t left_bit_size  = sizeof(Lhs)*CHAR_BIT;
0066     std::size_t right_bit_size = sizeof(Rhs)*CHAR_BIT;
0067 
0068     static Lhs const leftOne( 1 );
0069     static Rhs const rightOne( 1 );
0070 
0071     std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
0072 
0073     for( std::size_t counter = 0; counter < total_bits; ++counter ) {
0074         if( (lhs & ( leftOne << counter )) != (rhs & (rightOne << counter)) ) {
0075             if( pr ) {
0076                 pr.message() << " [";
0077                 expr.report( pr.message().stream() );
0078                 pr.message() << "]. Bitwise comparison failed";
0079                 pr = false;
0080             }
0081             pr.message() << "\nMismatch at position " << counter;
0082         }
0083     }
0084 
0085     if( left_bit_size != right_bit_size ) {
0086         if( pr ) {
0087             pr.message() << " [";
0088             expr.report( pr.message().stream() );
0089             pr.message() << "]. Bitwise comparison failed";
0090             pr = false;
0091         }
0092         pr.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
0093     }
0094 
0095     return pr;
0096 }
0097 
0098 //____________________________________________________________________________//
0099 
0100 //! Returns an assertion_result using the bitwise comparison out of an expression
0101 //!
0102 //! This is used as a modifer of the normal operator<< on expressions to use the
0103 //! bitwise comparison. 
0104 //!
0105 //! @note Available only for compilers supporting the @c auto declaration. 
0106 template<typename T1, typename T2, typename T3, typename T4>
0107 inline assertion_result
0108 operator<<(assertion_evaluate_t<assertion::binary_expr<T1,T2,assertion::op::EQ<T3,T4> > > const& ae, bitwise )
0109 {
0110     return bitwise_compare( ae.m_e.lhs().value(), ae.m_e.rhs(), ae.m_e );
0111 }
0112 
0113 //____________________________________________________________________________//
0114 
0115 inline assertion_type
0116 operator<<( assertion_type const& , bitwise )
0117 {
0118     return assertion_type(CHECK_BUILT_ASSERTION);
0119 }
0120 
0121 //____________________________________________________________________________//
0122 
0123 } // namespace tt_detail
0124 } // namespace test_tools
0125 } // namespace boost
0126 
0127 #include <boost/test/detail/enable_warnings.hpp>
0128 
0129 #endif // BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER