diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h index a2af9c86..69428c8a 100644 --- a/include/internal/catch_approx.h +++ b/include/internal/catch_approx.h @@ -8,6 +8,7 @@ #ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED #define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED +#include "catch_enforce.h" #include "catch_tostring.h" #include @@ -48,7 +49,7 @@ 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> @@ -95,6 +96,7 @@ namespace Detail { template ::value>::type> Approx& margin( T const& newMargin ) { m_margin = static_cast(newMargin); + CATCH_ENFORCE(m_margin >= 0, "Invalid Approx::margin: " << m_margin << ", Approx::Margin has to be non-negative."); return *this; } diff --git a/projects/SelfTest/ApproxTests.cpp b/projects/SelfTest/ApproxTests.cpp index bb6abffe..208be8e4 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,29 @@ 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) ); +} + +TEST_CASE("Approx setters validate their arguments", "[Approx]") { + REQUIRE_NOTHROW(Approx(0).margin(0)); + REQUIRE_NOTHROW(Approx(0).margin(1234656)); + + REQUIRE_THROWS_AS(Approx(0).margin(-2), std::domain_error); +} + //////////////////////////////////////////////////////////////////////////////// class StrongDoubleTypedef diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index cc23a7dd..d15e5ab8 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1003,6 +1003,6 @@ with expansion: "{?}" == "1" =============================================================================== -test cases: 180 | 129 passed | 47 failed | 4 failed as expected -assertions: 886 | 769 passed | 96 failed | 21 failed as expected +test cases: 182 | 131 passed | 47 failed | 4 failed as expected +assertions: 896 | 779 passed | 96 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index e32d6f72..f47b7329 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -490,6 +490,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,60 @@ PASSED: with message: anonymous test case +------------------------------------------------------------------------------- +Approx setters validate their arguments +------------------------------------------------------------------------------- +ApproxTests.cpp: +............................................................................... + +ApproxTests.cpp:: +PASSED: + REQUIRE_NOTHROW( Approx(0).margin(0) ) + +ApproxTests.cpp:: +PASSED: + REQUIRE_NOTHROW( Approx(0).margin(1234656) ) + +ApproxTests.cpp:: +PASSED: + REQUIRE_THROWS_AS( Approx(0).margin(-2), std::domain_error ) + +------------------------------------------------------------------------------- +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 ------------------------------------------------------------------------------- @@ -4201,6 +4261,12 @@ PASSED: with expansion: Approx( 1.23 ) != 1.24 +ApproxTests.cpp:: +PASSED: + REQUIRE( 0 == Approx(0) ) +with expansion: + 0 == Approx( 0.0 ) + Message from section one ------------------------------------------------------------------------------- Standard output from all sections is reported @@ -7508,6 +7574,6 @@ MiscTests.cpp:: PASSED: =============================================================================== -test cases: 180 | 127 passed | 49 failed | 4 failed as expected -assertions: 885 | 765 passed | 99 failed | 21 failed as expected +test cases: 182 | 129 passed | 49 failed | 4 failed as expected +assertions: 895 | 775 passed | 99 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 508b9206..8c186f0c 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -100,6 +100,8 @@ ExceptionTests.cpp: + + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 04c6f986..07eb6a52 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -495,6 +495,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) @@ -568,6 +576,76 @@ + + + + Approx(0).margin(0) + + + Approx(0).margin(0) + + + + + Approx(0).margin(1234656) + + + Approx(0).margin(1234656) + + + + + Approx(0).margin(-2), std::domain_error + + + Approx(0).margin(-2), std::domain_error + + + + + + + + 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 ) + + + + @@ -4795,6 +4873,14 @@ A string sent directly to stderr Approx( 1.23 ) != 1.24 + + + 0 == Approx(0) + + + 0 == Approx( 0.0 ) + + @@ -8287,7 +8373,7 @@ loose text artifact - + - +