Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:02:51

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_MATCHERS_FLOATING_POINT_HPP_INCLUDED
0009 #define CATCH_MATCHERS_FLOATING_POINT_HPP_INCLUDED
0010 
0011 #include <catch2/matchers/catch_matchers.hpp>
0012 
0013 namespace Catch {
0014 namespace Matchers {
0015 
0016     namespace Detail {
0017         enum class FloatingPointKind : uint8_t;
0018     }
0019 
0020     class  WithinAbsMatcher final : public MatcherBase<double> {
0021     public:
0022         WithinAbsMatcher(double target, double margin);
0023         bool match(double const& matchee) const override;
0024         std::string describe() const override;
0025     private:
0026         double m_target;
0027         double m_margin;
0028     };
0029 
0030     //! Creates a matcher that accepts numbers within certain range of target
0031     WithinAbsMatcher WithinAbs( double target, double margin );
0032 
0033 
0034 
0035     class WithinUlpsMatcher final : public MatcherBase<double> {
0036     public:
0037         WithinUlpsMatcher( double target,
0038                            uint64_t ulps,
0039                            Detail::FloatingPointKind baseType );
0040         bool match(double const& matchee) const override;
0041         std::string describe() const override;
0042     private:
0043         double m_target;
0044         uint64_t m_ulps;
0045         Detail::FloatingPointKind m_type;
0046     };
0047 
0048     //! Creates a matcher that accepts doubles within certain ULP range of target
0049     WithinUlpsMatcher WithinULP(double target, uint64_t maxUlpDiff);
0050     //! Creates a matcher that accepts floats within certain ULP range of target
0051     WithinUlpsMatcher WithinULP(float target, uint64_t maxUlpDiff);
0052 
0053 
0054 
0055     // Given IEEE-754 format for floats and doubles, we can assume
0056     // that float -> double promotion is lossless. Given this, we can
0057     // assume that if we do the standard relative comparison of
0058     // |lhs - rhs| <= epsilon * max(fabs(lhs), fabs(rhs)), then we get
0059     // the same result if we do this for floats, as if we do this for
0060     // doubles that were promoted from floats.
0061     class WithinRelMatcher final : public MatcherBase<double> {
0062     public:
0063         WithinRelMatcher( double target, double epsilon );
0064         bool match(double const& matchee) const override;
0065         std::string describe() const override;
0066     private:
0067         double m_target;
0068         double m_epsilon;
0069     };
0070 
0071     //! Creates a matcher that accepts doubles within certain relative range of target
0072     WithinRelMatcher WithinRel(double target, double eps);
0073     //! Creates a matcher that accepts doubles within 100*DBL_EPS relative range of target
0074     WithinRelMatcher WithinRel(double target);
0075     //! Creates a matcher that accepts doubles within certain relative range of target
0076     WithinRelMatcher WithinRel(float target, float eps);
0077     //! Creates a matcher that accepts floats within 100*FLT_EPS relative range of target
0078     WithinRelMatcher WithinRel(float target);
0079 
0080 
0081 
0082     class IsNaNMatcher final : public MatcherBase<double> {
0083     public:
0084         IsNaNMatcher() = default;
0085         bool match( double const& matchee ) const override;
0086         std::string describe() const override;
0087     };
0088 
0089     IsNaNMatcher IsNaN();
0090 
0091 } // namespace Matchers
0092 } // namespace Catch
0093 
0094 #endif // CATCH_MATCHERS_FLOATING_POINT_HPP_INCLUDED