• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //! @file
9 //! Bitwise comparison manipulator implementation
10 // ***************************************************************************
11 
12 #ifndef BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
13 #define BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
14 
15 // Boost Test
16 #include <boost/test/tools/detail/fwd.hpp>
17 #include <boost/test/tools/detail/indirections.hpp>
18 
19 #include <boost/test/tools/assertion_result.hpp>
20 #include <boost/test/tools/assertion.hpp>
21 
22 // STL
23 #include <climits>          // for CHAR_BIT
24 
25 #include <boost/test/detail/suppress_warnings.hpp>
26 
27 //____________________________________________________________________________//
28 
29 namespace boost {
30 namespace test_tools {
31 
32 // ************************************************************************** //
33 // **************        bitwise comparison manipulator        ************** //
34 // ************************************************************************** //
35 
36 //! Bitwise comparison manipulator
37 //! This is a terminal for the expression
38 struct bitwise {};
39 
40 //____________________________________________________________________________//
41 
42 inline unit_test::lazy_ostream &
operator <<(unit_test::lazy_ostream & o,bitwise)43 operator<<( unit_test::lazy_ostream &o, bitwise )   { return o; }
44 
45 // needed for the lazy evaluation in lazy_ostream as bitwise is a terminal
46 inline std::ostream&
operator <<(std::ostream & o,bitwise)47 operator<<( std::ostream& o, bitwise )              { return o; }
48 
49 
50 //____________________________________________________________________________//
51 
52 namespace tt_detail {
53 
54 /*!@brief Bitwise comparison of two operands
55  *
56  * This class constructs an @ref assertion_result that contains precise bit comparison information.
57  * In particular the location of the mismatches (if any) are printed in the assertion result.
58  */
59 template<typename Lhs, typename Rhs, typename E>
60 inline assertion_result
bitwise_compare(Lhs const & lhs,Rhs const & rhs,E const & expr)61 bitwise_compare(Lhs const& lhs, Rhs const& rhs, E const& expr )
62 {
63     assertion_result    pr( true );
64 
65     std::size_t left_bit_size  = sizeof(Lhs)*CHAR_BIT;
66     std::size_t right_bit_size = sizeof(Rhs)*CHAR_BIT;
67 
68     static Lhs const leftOne( 1 );
69     static Rhs const rightOne( 1 );
70 
71     std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
72 
73     for( std::size_t counter = 0; counter < total_bits; ++counter ) {
74         if( (lhs & ( leftOne << counter )) != (rhs & (rightOne << counter)) ) {
75             if( pr ) {
76                 pr.message() << " [";
77                 expr.report( pr.message().stream() );
78                 pr.message() << "]. Bitwise comparison failed";
79                 pr = false;
80             }
81             pr.message() << "\nMismatch at position " << counter;
82         }
83     }
84 
85     if( left_bit_size != right_bit_size ) {
86         if( pr ) {
87             pr.message() << " [";
88             expr.report( pr.message().stream() );
89             pr.message() << "]. Bitwise comparison failed";
90             pr = false;
91         }
92         pr.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
93     }
94 
95     return pr;
96 }
97 
98 //____________________________________________________________________________//
99 
100 //! Returns an assertion_result using the bitwise comparison out of an expression
101 //!
102 //! This is used as a modifer of the normal operator<< on expressions to use the
103 //! bitwise comparison.
104 //!
105 //! @note Available only for compilers supporting the @c auto declaration.
106 template<typename T1, typename T2, typename T3, typename T4>
107 inline assertion_result
operator <<(assertion_evaluate_t<assertion::binary_expr<T1,T2,assertion::op::EQ<T3,T4>>> const & ae,bitwise)108 operator<<(assertion_evaluate_t<assertion::binary_expr<T1,T2,assertion::op::EQ<T3,T4> > > const& ae, bitwise )
109 {
110     return bitwise_compare( ae.m_e.lhs().value(), ae.m_e.rhs(), ae.m_e );
111 }
112 
113 //____________________________________________________________________________//
114 
115 inline assertion_type
operator <<(assertion_type const &,bitwise)116 operator<<( assertion_type const& , bitwise )
117 {
118     return assertion_type(CHECK_BUILT_ASSERTION);
119 }
120 
121 //____________________________________________________________________________//
122 
123 } // namespace tt_detail
124 } // namespace test_tools
125 } // namespace boost
126 
127 #include <boost/test/detail/enable_warnings.hpp>
128 
129 #endif // BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
130