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
29namespace boost {
30namespace test_tools {
31
32// ************************************************************************** //
33// ************** bitwise comparison manipulator ************** //
34// ************************************************************************** //
35
36//! Bitwise comparison manipulator
37struct bitwise {};
38
39//____________________________________________________________________________//
40
41inline int
42operator<<( unit_test::lazy_ostream const&, bitwise ) { return 0; }
43
44//____________________________________________________________________________//
45
46namespace tt_detail {
47
48/*!@brief Bitwise comparison of two operands
49 *
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.
52 */
53template<typename Lhs, typename Rhs, typename E>
54inline assertion_result
55bitwise_compare(Lhs const& lhs, Rhs const& rhs, E const& expr )
56{
57 assertion_result pr( true );
58
59 std::size_t left_bit_size = sizeof(Lhs)*CHAR_BIT;
60 std::size_t right_bit_size = sizeof(Rhs)*CHAR_BIT;
61
62 static Lhs const leftOne( 1 );
63 static Rhs const rightOne( 1 );
64
65 std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
66
67 for( std::size_t counter = 0; counter < total_bits; ++counter ) {
68 if( (lhs & ( leftOne << counter )) != (rhs & (rightOne << counter)) ) {
69 if( pr ) {
70 pr.message() << " [";
71 expr.report( pr.message().stream() );
72 pr.message() << "]. Bitwise comparison failed";
73 pr = false;
74 }
75 pr.message() << "\nMismatch at position " << counter;
76 }
77 }
78
79 if( left_bit_size != right_bit_size ) {
80 if( pr ) {
81 pr.message() << " [";
82 expr.report( pr.message().stream() );
83 pr.message() << "]. Bitwise comparison failed";
84 pr = false;
85 }
86 pr.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
87 }
88
89 return pr;
90}
91
92//____________________________________________________________________________//
93
94//! Returns an assertion_result using the bitwise comparison out of an expression
95//!
96//! This is used as a modifer of the normal operator<< on expressions to use the
97//! bitwise comparison.
98//!
99//! @note Available only for compilers supporting the @c auto declaration.
100template<typename T1, typename T2, typename T3, typename T4>
101inline assertion_result
102operator<<(assertion_evaluate_t<assertion::binary_expr<T1,T2,assertion::op::EQ<T3,T4> > > const& ae, bitwise )
103{
104 return bitwise_compare( ae.m_e.lhs().value(), ae.m_e.rhs(), ae.m_e );
105}
106
107//____________________________________________________________________________//
108
109inline check_type
110operator<<( assertion_type const& , bitwise )
111{
112 return CHECK_BUILT_ASSERTION;
113}
114
115//____________________________________________________________________________//
116
117} // namespace tt_detail
118} // namespace test_tools
119} // namespace boost
120
121#include <boost/test/detail/enable_warnings.hpp>
122
123#endif // BOOST_TEST_TOOLS_DETAIL_BITWISE_MANIP_HPP_012705GER
124

source code of boost/boost/test/tools/detail/bitwise_manip.hpp