tolerance_manip.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. //! @brief Floating point comparison tolerance manipulators
  9. //!
  10. //! This file defines several manipulators for floating point comparison. These
  11. //! manipulators are intended to be used with BOOST_TEST.
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER
  14. #define BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER
  15. // Boost Test
  16. #include <boost/test/tools/detail/fwd.hpp>
  17. #include <boost/test/tools/detail/indirections.hpp>
  18. #include <boost/test/tools/fpc_tolerance.hpp>
  19. #include <boost/test/tools/floating_point_comparison.hpp>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. //____________________________________________________________________________//
  22. namespace boost {
  23. namespace test_tools {
  24. namespace tt_detail {
  25. // ************************************************************************** //
  26. // ************** fpc tolerance manipulator ************** //
  27. // ************************************************************************** //
  28. template<typename FPT>
  29. struct tolerance_manip {
  30. explicit tolerance_manip( FPT const & tol ) : m_value( tol ) {}
  31. FPT m_value;
  32. };
  33. //____________________________________________________________________________//
  34. struct tolerance_manip_delay {};
  35. template<typename FPT>
  36. inline tolerance_manip<FPT>
  37. operator%( FPT v, tolerance_manip_delay const& )
  38. {
  39. BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
  40. "tolerance should be specified using a floating points type" );
  41. return tolerance_manip<FPT>( FPT(v / 100) );
  42. }
  43. //____________________________________________________________________________//
  44. template<typename E, typename FPT>
  45. inline assertion_result
  46. operator<<(assertion_evaluate_t<E> const& ae, tolerance_manip<FPT> const& tol)
  47. {
  48. local_fpc_tolerance<FPT> lt( tol.m_value );
  49. return ae.m_e.evaluate();
  50. }
  51. //____________________________________________________________________________//
  52. template<typename FPT>
  53. inline int
  54. operator<<( unit_test::lazy_ostream const&, tolerance_manip<FPT> const& ) { return 0; }
  55. //____________________________________________________________________________//
  56. template<typename FPT>
  57. inline check_type
  58. operator<<( assertion_type const& /*at*/, tolerance_manip<FPT> const& ) { return CHECK_BUILT_ASSERTION; }
  59. //____________________________________________________________________________//
  60. } // namespace tt_detail
  61. /*! Tolerance manipulator
  62. *
  63. * These functions return a manipulator that can be used in conjunction with BOOST_TEST
  64. * in order to specify the tolerance with which floating point comparisons are made.
  65. */
  66. template<typename FPT>
  67. inline tt_detail::tolerance_manip<FPT>
  68. tolerance( FPT v )
  69. {
  70. BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
  71. "tolerance only for floating points" );
  72. return tt_detail::tolerance_manip<FPT>( v );
  73. }
  74. //____________________________________________________________________________//
  75. //! @overload tolerance( FPT v )
  76. template<typename FPT>
  77. inline tt_detail::tolerance_manip<FPT>
  78. tolerance( fpc::percent_tolerance_t<FPT> v )
  79. {
  80. BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
  81. "tolerance only for floating points" );
  82. return tt_detail::tolerance_manip<FPT>( static_cast<FPT>(v.m_value / 100) );
  83. }
  84. //____________________________________________________________________________//
  85. //! @overload tolerance( FPT v )
  86. inline tt_detail::tolerance_manip_delay
  87. tolerance()
  88. {
  89. return tt_detail::tolerance_manip_delay();
  90. }
  91. //____________________________________________________________________________//
  92. } // namespace test_tools
  93. } // namespace boost
  94. #include <boost/test/detail/enable_warnings.hpp>
  95. #endif // BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER