bitwise_manip.hpp 4.0 KB

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