diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 663dabf0..8f75b5d7 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -57,7 +57,8 @@ namespace Detail { if (relativeOK) { return true; } - return std::fabs(lhs_v - rhs.m_value) < rhs.m_margin; + + return std::fabs(lhs_v - rhs.m_value) <= rhs.m_margin; } template ::value>::type> @@ -130,7 +131,7 @@ namespace Detail { if (relativeOK) { return true; } - return std::fabs(lhs - rhs.m_value) < rhs.m_margin; + return std::fabs(lhs - rhs.m_value) <= rhs.m_margin; } friend bool operator == ( Approx const& lhs, double rhs ) { diff --git a/projects/SelfTest/ApproxTests.cpp b/projects/SelfTest/ApproxTests.cpp index 14a8234e..65ba52fb 100644 --- a/projects/SelfTest/ApproxTests.cpp +++ b/projects/SelfTest/ApproxTests.cpp @@ -24,6 +24,8 @@ TEST_CASE REQUIRE( Approx( d ) == 1.23 ); REQUIRE( Approx( d ) != 1.22 ); REQUIRE( Approx( d ) != 1.24 ); + + REQUIRE( 0 == Approx(0) ); } /////////////////////////////////////////////////////////////////////////////// @@ -146,11 +148,22 @@ TEST_CASE( "Approximate PI", "[Approx][PI]" ) TEST_CASE( "Absolute margin", "[Approx]" ) { REQUIRE( 104.0 != Approx(100.0) ); REQUIRE( 104.0 == Approx(100.0).margin(5) ); + REQUIRE( 104.0 == Approx(100.0).margin(4) ); REQUIRE( 104.0 != Approx(100.0).margin(3) ); REQUIRE( 100.3 != Approx(100.0) ); REQUIRE( 100.3 == Approx(100.0).margin(0.5) ); } +TEST_CASE("Approx with exactly-representable margin", "[Approx]") { + CHECK( 0.25f == Approx(0.0f).margin(0.25f) ); + + CHECK( 0.0f == Approx(0.25f).margin(0.25f) ); + CHECK( 0.5f == Approx(0.25f).margin(0.25f) ); + + CHECK( 245.0f == Approx(245.25f).margin(0.25f) ); + CHECK( 245.5f == Approx(245.25f).margin(0.25f) ); +} + //////////////////////////////////////////////////////////////////////////////// #if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 910155ce..2bf57d56 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -956,6 +956,6 @@ with expansion: "first" == "second" =============================================================================== -test cases: 170 | 121 passed | 45 failed | 4 failed as expected -assertions: 973 | 864 passed | 88 failed | 21 failed as expected +test cases: 171 | 122 passed | 45 failed | 4 failed as expected +assertions: 980 | 871 passed | 88 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index c3b0499d..9c76fa4e 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -457,6 +457,12 @@ PASSED: with expansion: 104.0 == Approx( 100.0 ) +ApproxTests.cpp:: +PASSED: + REQUIRE( 104.0 == Approx(100.0).margin(4) ) +with expansion: + 104.0 == Approx( 100.0 ) + ApproxTests.cpp:: PASSED: REQUIRE( 104.0 != Approx(100.0).margin(3) ) @@ -552,6 +558,42 @@ with expansion: "this string contains 'abc' as a substring" ( contains: "not there" or contains: "string" ) +------------------------------------------------------------------------------- +Approx with exactly-representable margin +------------------------------------------------------------------------------- +ApproxTests.cpp: +............................................................................... + +ApproxTests.cpp:: +PASSED: + CHECK( 0.25f == Approx(0.0f).margin(0.25f) ) +with expansion: + 0.25f == Approx( 0.0 ) + +ApproxTests.cpp:: +PASSED: + CHECK( 0.0f == Approx(0.25f).margin(0.25f) ) +with expansion: + 0.0f == Approx( 0.25 ) + +ApproxTests.cpp:: +PASSED: + CHECK( 0.5f == Approx(0.25f).margin(0.25f) ) +with expansion: + 0.5f == Approx( 0.25 ) + +ApproxTests.cpp:: +PASSED: + CHECK( 245.0f == Approx(245.25f).margin(0.25f) ) +with expansion: + 245.0f == Approx( 245.25 ) + +ApproxTests.cpp:: +PASSED: + CHECK( 245.5f == Approx(245.25f).margin(0.25f) ) +with expansion: + 245.5f == Approx( 245.25 ) + ------------------------------------------------------------------------------- Approximate PI ------------------------------------------------------------------------------- @@ -6846,6 +6888,12 @@ PASSED: with expansion: Approx( 1.23 ) != 1.24 +ApproxTests.cpp:: +PASSED: + REQUIRE( 0 == Approx(0) ) +with expansion: + 0 == Approx( 0.0 ) + Write to std::cerr ------------------------------------------------------------------------------- Standard error is reported and redirected @@ -9576,6 +9624,6 @@ MiscTests.cpp:: PASSED: =============================================================================== -test cases: 170 | 119 passed | 47 failed | 4 failed as expected -assertions: 978 | 864 passed | 93 failed | 21 failed as expected +test cases: 171 | 120 passed | 47 failed | 4 failed as expected +assertions: 985 | 871 passed | 93 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 5fe36bae..e6fa4187 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,6 +1,6 @@ - + @@ -99,6 +99,7 @@ ExceptionTests.cpp: + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 6322993f..7a9684dd 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -462,6 +462,14 @@ 104.0 == Approx( 100.0 ) + + + 104.0 == Approx(100.0).margin(4) + + + 104.0 == Approx( 100.0 ) + + 104.0 != Approx(100.0).margin(3) @@ -565,6 +573,49 @@ + + + + 0.25f == Approx(0.0f).margin(0.25f) + + + 0.25f == Approx( 0.0 ) + + + + + 0.0f == Approx(0.25f).margin(0.25f) + + + 0.0f == Approx( 0.25 ) + + + + + 0.5f == Approx(0.25f).margin(0.25f) + + + 0.5f == Approx( 0.25 ) + + + + + 245.0f == Approx(245.25f).margin(0.25f) + + + 245.0f == Approx( 245.25 ) + + + + + 245.5f == Approx(245.25f).margin(0.25f) + + + 245.5f == Approx( 245.25 ) + + + + @@ -7325,6 +7376,14 @@ A string sent directly to stderr Approx( 1.23 ) != 1.24 + + + 0 == Approx(0) + + + 0 == Approx( 0.0 ) + + @@ -10179,7 +10238,7 @@ spanner - + - +