fpc_tolerance.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 : $RCSfile$
  8. //
  9. // Version : $Revision: 74248 $
  10. //
  11. // Description : FPC tools tolerance holder
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_TOOLS_FPC_TOLERANCE_HPP_121612GER
  14. #define BOOST_TEST_TOOLS_FPC_TOLERANCE_HPP_121612GER
  15. // Boost Test
  16. #include <boost/test/tree/decorator.hpp>
  17. #include <boost/test/tools/floating_point_comparison.hpp>
  18. #include <boost/test/detail/suppress_warnings.hpp>
  19. //____________________________________________________________________________//
  20. namespace boost {
  21. namespace test_tools {
  22. namespace fpc = math::fpc;
  23. // ************************************************************************** //
  24. // ************** floating point comparison tolerance ************** //
  25. // ************************************************************************** //
  26. template<typename FPT>
  27. inline FPT&
  28. fpc_tolerance()
  29. {
  30. static FPT s_value = 0;
  31. return s_value;
  32. }
  33. //____________________________________________________________________________//
  34. template<typename FPT>
  35. struct local_fpc_tolerance {
  36. local_fpc_tolerance( FPT fraction_tolerance ) : m_old_tolerance( fpc_tolerance<FPT>() )
  37. {
  38. fpc_tolerance<FPT>() = fraction_tolerance;
  39. }
  40. ~local_fpc_tolerance()
  41. {
  42. if( m_old_tolerance != (FPT)-1 )
  43. fpc_tolerance<FPT>() = m_old_tolerance;
  44. }
  45. private:
  46. // Data members
  47. FPT m_old_tolerance;
  48. };
  49. //____________________________________________________________________________//
  50. } // namespace test_tools
  51. // ************************************************************************** //
  52. // ************** decorator::tolerance ************** //
  53. // ************************************************************************** //
  54. namespace unit_test {
  55. namespace decorator {
  56. template<typename FPT>
  57. inline fixture_t
  58. tolerance( FPT v )
  59. {
  60. return fixture_t( test_unit_fixture_ptr(
  61. new unit_test::class_based_fixture<test_tools::local_fpc_tolerance<FPT>,FPT>( v ) ) );
  62. }
  63. //____________________________________________________________________________//
  64. template<typename FPT>
  65. inline fixture_t
  66. tolerance( test_tools::fpc::percent_tolerance_t<FPT> v )
  67. {
  68. return fixture_t( test_unit_fixture_ptr(
  69. new unit_test::class_based_fixture<test_tools::local_fpc_tolerance<FPT>,FPT>( boost::math::fpc::fpc_detail::fraction_tolerance<FPT>( v ) ) ) );
  70. }
  71. //____________________________________________________________________________//
  72. } // namespace decorator
  73. using decorator::tolerance;
  74. } // namespace unit_test
  75. } // namespace boost
  76. #include <boost/test/detail/enable_warnings.hpp>
  77. #endif // BOOST_TEST_TOOLS_FPC_TOLERANCE_HPP_121612GER