catch_approx.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #ifndef CATCH_APPROX_HPP_INCLUDED
  7. #define CATCH_APPROX_HPP_INCLUDED
  8. #include <catch2/catch_tostring.hpp>
  9. #include <type_traits>
  10. namespace Catch {
  11. class Approx {
  12. private:
  13. bool equalityComparisonImpl(double other) const;
  14. // Sets and validates the new margin (margin >= 0)
  15. void setMargin(double margin);
  16. // Sets and validates the new epsilon (0 < epsilon < 1)
  17. void setEpsilon(double epsilon);
  18. public:
  19. explicit Approx ( double value );
  20. static Approx custom();
  21. Approx operator-() const;
  22. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  23. Approx operator()( T const& value ) const {
  24. Approx approx( static_cast<double>(value) );
  25. approx.m_epsilon = m_epsilon;
  26. approx.m_margin = m_margin;
  27. approx.m_scale = m_scale;
  28. return approx;
  29. }
  30. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  31. explicit Approx( T const& value ): Approx(static_cast<double>(value))
  32. {}
  33. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  34. friend bool operator == ( const T& lhs, Approx const& rhs ) {
  35. auto lhs_v = static_cast<double>(lhs);
  36. return rhs.equalityComparisonImpl(lhs_v);
  37. }
  38. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  39. friend bool operator == ( Approx const& lhs, const T& rhs ) {
  40. return operator==( rhs, lhs );
  41. }
  42. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  43. friend bool operator != ( T const& lhs, Approx const& rhs ) {
  44. return !operator==( lhs, rhs );
  45. }
  46. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  47. friend bool operator != ( Approx const& lhs, T const& rhs ) {
  48. return !operator==( rhs, lhs );
  49. }
  50. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  51. friend bool operator <= ( T const& lhs, Approx const& rhs ) {
  52. return static_cast<double>(lhs) < rhs.m_value || lhs == rhs;
  53. }
  54. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  55. friend bool operator <= ( Approx const& lhs, T const& rhs ) {
  56. return lhs.m_value < static_cast<double>(rhs) || lhs == rhs;
  57. }
  58. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  59. friend bool operator >= ( T const& lhs, Approx const& rhs ) {
  60. return static_cast<double>(lhs) > rhs.m_value || lhs == rhs;
  61. }
  62. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  63. friend bool operator >= ( Approx const& lhs, T const& rhs ) {
  64. return lhs.m_value > static_cast<double>(rhs) || lhs == rhs;
  65. }
  66. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  67. Approx& epsilon( T const& newEpsilon ) {
  68. const auto epsilonAsDouble = static_cast<double>(newEpsilon);
  69. setEpsilon(epsilonAsDouble);
  70. return *this;
  71. }
  72. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  73. Approx& margin( T const& newMargin ) {
  74. const auto marginAsDouble = static_cast<double>(newMargin);
  75. setMargin(marginAsDouble);
  76. return *this;
  77. }
  78. template <typename T, typename = std::enable_if_t<std::is_constructible<double, T>::value>>
  79. Approx& scale( T const& newScale ) {
  80. m_scale = static_cast<double>(newScale);
  81. return *this;
  82. }
  83. std::string toString() const;
  84. private:
  85. double m_epsilon;
  86. double m_margin;
  87. double m_scale;
  88. double m_value;
  89. };
  90. namespace literals {
  91. Approx operator ""_a(long double val);
  92. Approx operator ""_a(unsigned long long val);
  93. } // end namespace literals
  94. template<>
  95. struct StringMaker<Catch::Approx> {
  96. static std::string convert(Catch::Approx const& value);
  97. };
  98. } // end namespace Catch
  99. #endif // CATCH_APPROX_HPP_INCLUDED