Skip to content

Commit

Permalink
Make Approx::margin inclusive
Browse files Browse the repository at this point in the history
Fixes #952, related to #980
  • Loading branch information
horenmar committed Oct 30, 2017
1 parent 06586b7 commit a6cf19a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 9 deletions.
5 changes: 3 additions & 2 deletions include/internal/catch_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
Expand Down Expand Up @@ -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 ) {
Expand Down
13 changes: 13 additions & 0 deletions projects/SelfTest/ApproxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions projects/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

52 changes: 50 additions & 2 deletions projects/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ PASSED:
with expansion:
104.0 == Approx( 100.0 )

ApproxTests.cpp:<line number>:
PASSED:
REQUIRE( 104.0 == Approx(100.0).margin(4) )
with expansion:
104.0 == Approx( 100.0 )

ApproxTests.cpp:<line number>:
PASSED:
REQUIRE( 104.0 != Approx(100.0).margin(3) )
Expand Down Expand Up @@ -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:<line number>
...............................................................................

ApproxTests.cpp:<line number>:
PASSED:
CHECK( 0.25f == Approx(0.0f).margin(0.25f) )
with expansion:
0.25f == Approx( 0.0 )

ApproxTests.cpp:<line number>:
PASSED:
CHECK( 0.0f == Approx(0.25f).margin(0.25f) )
with expansion:
0.0f == Approx( 0.25 )

ApproxTests.cpp:<line number>:
PASSED:
CHECK( 0.5f == Approx(0.25f).margin(0.25f) )
with expansion:
0.5f == Approx( 0.25 )

ApproxTests.cpp:<line number>:
PASSED:
CHECK( 245.0f == Approx(245.25f).margin(0.25f) )
with expansion:
245.0f == Approx( 245.25 )

ApproxTests.cpp:<line number>:
PASSED:
CHECK( 245.5f == Approx(245.25f).margin(0.25f) )
with expansion:
245.5f == Approx( 245.25 )

-------------------------------------------------------------------------------
Approximate PI
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -6846,6 +6888,12 @@ PASSED:
with expansion:
Approx( 1.23 ) != 1.24

ApproxTests.cpp:<line number>:
PASSED:
REQUIRE( 0 == Approx(0) )
with expansion:
0 == Approx( 0.0 )

Write to std::cerr
-------------------------------------------------------------------------------
Standard error is reported and redirected
Expand Down Expand Up @@ -9576,6 +9624,6 @@ MiscTests.cpp:<line number>:
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

3 changes: 2 additions & 1 deletion projects/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesspanner>
<testsuite name="<exe-name>" errors="13" failures="81" tests="979" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="13" failures="81" tests="986" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="#748 - captures with unexpected exceptions" name="outside assertions" time="{duration}">
<error type="TEST_CASE">
Expand Down Expand Up @@ -99,6 +99,7 @@ ExceptionTests.cpp:<line number>
</testcase>
<testcase classname="global" name="Anonymous test case 1" time="{duration}"/>
<testcase classname="global" name="AnyOf matcher" time="{duration}"/>
<testcase classname="global" name="Approx with exactly-representable margin" time="{duration}"/>
<testcase classname="global" name="Approximate PI" time="{duration}"/>
<testcase classname="global" name="Approximate comparisons with different epsilons" time="{duration}"/>
<testcase classname="global" name="Approximate comparisons with floats" time="{duration}"/>
Expand Down
63 changes: 61 additions & 2 deletions projects/SelfTest/Baselines/xml.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@
104.0 == Approx( 100.0 )
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
104.0 == Approx(100.0).margin(4)
</Original>
<Expanded>
104.0 == Approx( 100.0 )
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
104.0 != Approx(100.0).margin(3)
Expand Down Expand Up @@ -565,6 +573,49 @@
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Approx with exactly-representable margin" tags="[Approx]" filename="projects/<exe-name>/ApproxTests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
0.25f == Approx(0.0f).margin(0.25f)
</Original>
<Expanded>
0.25f == Approx( 0.0 )
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
0.0f == Approx(0.25f).margin(0.25f)
</Original>
<Expanded>
0.0f == Approx( 0.25 )
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
0.5f == Approx(0.25f).margin(0.25f)
</Original>
<Expanded>
0.5f == Approx( 0.25 )
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
245.0f == Approx(245.25f).margin(0.25f)
</Original>
<Expanded>
245.0f == Approx( 245.25 )
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
245.5f == Approx(245.25f).margin(0.25f)
</Original>
<Expanded>
245.5f == Approx( 245.25 )
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Approximate PI" tags="[Approx][PI]" filename="projects/<exe-name>/ApproxTests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
Expand Down Expand Up @@ -7325,6 +7376,14 @@ A string sent directly to stderr
Approx( 1.23 ) != 1.24
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
0 == Approx(0)
</Original>
<Expanded>
0 == Approx( 0.0 )
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Standard error is reported and redirected" tags="[.][hide][messages]" filename="projects/<exe-name>/MessageTests.cpp" >
Expand Down Expand Up @@ -10179,7 +10238,7 @@ spanner <OverallResult success="true"/>
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="864" failures="94" expectedFailures="21"/>
<OverallResults successes="871" failures="94" expectedFailures="21"/>
</Group>
<OverallResults successes="864" failures="93" expectedFailures="21"/>
<OverallResults successes="871" failures="93" expectedFailures="21"/>
</Catch>

0 comments on commit a6cf19a

Please sign in to comment.