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 c3ddd4a7e2
commit 11f716f28d
6 changed files with 184 additions and 8 deletions

View File

@ -8,6 +8,7 @@
#ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED #define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
#include "catch_enforce.h"
#include "catch_tostring.h" #include "catch_tostring.h"
#include <cmath> #include <cmath>
@ -48,7 +49,7 @@ namespace Detail {
if (relativeOK) { if (relativeOK) {
return true; 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> template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
@ -95,6 +96,7 @@ namespace Detail {
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
Approx& margin( T const& newMargin ) { Approx& margin( T const& newMargin ) {
m_margin = static_cast<double>(newMargin); m_margin = static_cast<double>(newMargin);
CATCH_ENFORCE(m_margin >= 0, "Invalid Approx::margin: " << m_margin << ", Approx::Margin has to be non-negative.");
return *this; return *this;
} }

View File

@ -24,6 +24,8 @@ TEST_CASE
REQUIRE( Approx( d ) == 1.23 ); REQUIRE( Approx( d ) == 1.23 );
REQUIRE( Approx( d ) != 1.22 ); REQUIRE( Approx( d ) != 1.22 );
REQUIRE( Approx( d ) != 1.24 ); REQUIRE( Approx( d ) != 1.24 );
REQUIRE( 0 == Approx(0) );
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -146,11 +148,29 @@ TEST_CASE( "Approximate PI", "[Approx][PI]" )
TEST_CASE( "Absolute margin", "[Approx]" ) { TEST_CASE( "Absolute margin", "[Approx]" ) {
REQUIRE( 104.0 != Approx(100.0) ); REQUIRE( 104.0 != Approx(100.0) );
REQUIRE( 104.0 == Approx(100.0).margin(5) ); 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( 104.0 != Approx(100.0).margin(3) );
REQUIRE( 100.3 != Approx(100.0) ); REQUIRE( 100.3 != Approx(100.0) );
REQUIRE( 100.3 == Approx(100.0).margin(0.5) ); 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 class StrongDoubleTypedef

View File

@ -1003,6 +1003,6 @@ with expansion:
"{?}" == "1" "{?}" == "1"
=============================================================================== ===============================================================================
test cases: 180 | 129 passed | 47 failed | 4 failed as expected test cases: 182 | 131 passed | 47 failed | 4 failed as expected
assertions: 886 | 769 passed | 96 failed | 21 failed as expected assertions: 896 | 779 passed | 96 failed | 21 failed as expected

View File

@ -490,6 +490,12 @@ PASSED:
with expansion: with expansion:
104.0 == Approx( 100.0 ) 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>: ApproxTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( 104.0 != Approx(100.0).margin(3) ) REQUIRE( 104.0 != Approx(100.0).margin(3) )
@ -552,6 +558,60 @@ PASSED:
with message: with message:
anonymous test case anonymous test case
-------------------------------------------------------------------------------
Approx setters validate their arguments
-------------------------------------------------------------------------------
ApproxTests.cpp:<line number>
...............................................................................
ApproxTests.cpp:<line number>:
PASSED:
REQUIRE_NOTHROW( Approx(0).margin(0) )
ApproxTests.cpp:<line number>:
PASSED:
REQUIRE_NOTHROW( Approx(0).margin(1234656) )
ApproxTests.cpp:<line number>:
PASSED:
REQUIRE_THROWS_AS( Approx(0).margin(-2), std::domain_error )
-------------------------------------------------------------------------------
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 Approximate PI
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -4201,6 +4261,12 @@ PASSED:
with expansion: with expansion:
Approx( 1.23 ) != 1.24 Approx( 1.23 ) != 1.24
ApproxTests.cpp:<line number>:
PASSED:
REQUIRE( 0 == Approx(0) )
with expansion:
0 == Approx( 0.0 )
Message from section one Message from section one
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Standard output from all sections is reported Standard output from all sections is reported
@ -7508,6 +7574,6 @@ MiscTests.cpp:<line number>:
PASSED: PASSED:
=============================================================================== ===============================================================================
test cases: 180 | 127 passed | 49 failed | 4 failed as expected test cases: 182 | 129 passed | 49 failed | 4 failed as expected
assertions: 885 | 765 passed | 99 failed | 21 failed as expected assertions: 895 | 775 passed | 99 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="15" failures="85" tests="886" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="15" failures="85" tests="896" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/> <testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#748 - captures with unexpected exceptions/outside assertions" time="{duration}"> <testcase classname="<exe-name>.global" name="#748 - captures with unexpected exceptions/outside assertions" time="{duration}">
@ -100,6 +100,8 @@ ExceptionTests.cpp:<line number>
</error> </error>
</testcase> </testcase>
<testcase classname="<exe-name>.global" name="Anonymous test case 1" time="{duration}"/> <testcase classname="<exe-name>.global" name="Anonymous test case 1" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approx setters validate their arguments" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approx with exactly-representable margin" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approximate PI" time="{duration}"/> <testcase classname="<exe-name>.global" name="Approximate PI" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approximate comparisons with different epsilons" time="{duration}"/> <testcase classname="<exe-name>.global" name="Approximate comparisons with different epsilons" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approximate comparisons with floats" time="{duration}"/> <testcase classname="<exe-name>.global" name="Approximate comparisons with floats" time="{duration}"/>

View File

@ -495,6 +495,14 @@
104.0 == Approx( 100.0 ) 104.0 == Approx( 100.0 )
</Expanded> </Expanded>
</Expression> </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" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original> <Original>
104.0 != Approx(100.0).margin(3) 104.0 != Approx(100.0).margin(3)
@ -568,6 +576,76 @@
<TestCase name="Anonymous test case 1" filename="projects/<exe-name>/VariadicMacrosTests.cpp" > <TestCase name="Anonymous test case 1" filename="projects/<exe-name>/VariadicMacrosTests.cpp" >
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Approx setters validate their arguments" tags="[Approx]" filename="projects/<exe-name>/ApproxTests.cpp" >
<Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
Approx(0).margin(0)
</Original>
<Expanded>
Approx(0).margin(0)
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
Approx(0).margin(1234656)
</Original>
<Expanded>
Approx(0).margin(1234656)
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE_THROWS_AS" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
Approx(0).margin(-2), std::domain_error
</Original>
<Expanded>
Approx(0).margin(-2), std::domain_error
</Expanded>
</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" > <TestCase name="Approximate PI" tags="[Approx][PI]" filename="projects/<exe-name>/ApproxTests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original> <Original>
@ -4795,6 +4873,14 @@ A string sent directly to stderr
Approx( 1.23 ) != 1.24 Approx( 1.23 ) != 1.24
</Expanded> </Expanded>
</Expression> </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"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Standard output from all sections is reported" tags="[.][messages]" filename="projects/<exe-name>/MessageTests.cpp" > <TestCase name="Standard output from all sections is reported" tags="[.][messages]" filename="projects/<exe-name>/MessageTests.cpp" >
@ -8287,7 +8373,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="765" failures="100" expectedFailures="21"/> <OverallResults successes="775" failures="100" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="765" failures="99" expectedFailures="21"/> <OverallResults successes="775" failures="99" expectedFailures="21"/>
</Catch> </Catch>