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 31, 2017
1 parent c3ddd4a commit 11f716f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 8 deletions.
4 changes: 3 additions & 1 deletion include/internal/catch_approx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cmath>
Expand Down Expand Up @@ -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 <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
Expand Down Expand Up @@ -95,6 +96,7 @@ namespace Detail {
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
Approx& margin( T const& 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;
}

Expand Down
20 changes: 20 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,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
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 @@ -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

70 changes: 68 additions & 2 deletions projects/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,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,60 @@ PASSED:
with message:
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
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -4201,6 +4261,12 @@ PASSED:
with expansion:
Approx( 1.23 ) != 1.24

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

Message from section one
-------------------------------------------------------------------------------
Standard output from all sections is reported
Expand Down Expand Up @@ -7508,6 +7574,6 @@ MiscTests.cpp:<line number>:
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

4 changes: 3 additions & 1 deletion projects/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="#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}">
Expand Down Expand Up @@ -100,6 +100,8 @@ ExceptionTests.cpp:<line number>
</error>
</testcase>
<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 comparisons with different epsilons" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Approximate comparisons with floats" time="{duration}"/>
Expand Down
90 changes: 88 additions & 2 deletions projects/SelfTest/Baselines/xml.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,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 @@ -568,6 +576,76 @@
<TestCase name="Anonymous test case 1" filename="projects/<exe-name>/VariadicMacrosTests.cpp" >
<OverallResult success="true"/>
</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" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ApproxTests.cpp" >
<Original>
Expand Down Expand Up @@ -4795,6 +4873,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 output from all sections is reported" tags="[.][messages]" filename="projects/<exe-name>/MessageTests.cpp" >
Expand Down Expand Up @@ -8287,7 +8373,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="765" failures="100" expectedFailures="21"/>
<OverallResults successes="775" failures="100" expectedFailures="21"/>
</Group>
<OverallResults successes="765" failures="99" expectedFailures="21"/>
<OverallResults successes="775" failures="99" expectedFailures="21"/>
</Catch>

0 comments on commit 11f716f

Please sign in to comment.