Make Approx::margin inclusive

Fixes #952, related to #980
This commit is contained in:
Martin Hořeňovský 2017-10-30 15:25:48 +01:00
parent 06586b7180
commit a6cf19abff
6 changed files with 131 additions and 9 deletions

View File

@ -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>
@ -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 ) {

View File

@ -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)

View File

@ -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

View File

@ -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) )
@ -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
-------------------------------------------------------------------------------
@ -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
@ -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

View File

@ -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">
@ -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}"/>

View File

@ -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)
@ -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>
@ -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" >
@ -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>