Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Make Approx::margin inclusive

Fixes #952, related to #980
Commit
a6cf19abff33a790695d571b35c022e7c496095f
Author
Martin Hořeňovský ·
Parents
06586b71808d

Files · Download .tar.gz

Changes

 include/internal/catch_approx.hpp                  |  5 +-
 projects/SelfTest/ApproxTests.cpp                  | 13 +++++
 .../SelfTest/Baselines/console.std.approved.txt    |  4 +-
 .../SelfTest/Baselines/console.sw.approved.txt     | 52 +++++++++++++++++-
 projects/SelfTest/Baselines/junit.sw.approved.txt  |  3 +-
 projects/SelfTest/Baselines/xml.sw.approved.txt    | 63 +++++++++++++++++++++-
 6 files changed, 131 insertions(+), 9 deletions(-)

diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp
index 663dabf0..8f75b5d7 100644
--- a/include/internal/catch_approx.hpp
+++ b/include/internal/catch_approx.hpp
@@ -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 ) {
diff --git a/projects/SelfTest/ApproxTests.cpp b/projects/SelfTest/ApproxTests.cpp
index 14a8234e..65ba52fb 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,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)
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index 910155ce..2bf57d56 100644
--- a/projects/SelfTest/Baselines/console.std.approved.txt
+++ b/projects/SelfTest/Baselines/console.std.approved.txt
@@ -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
 
diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt
index c3b0499d..9c76fa4e 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -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
 
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 5fe36bae..e6fa4187 100644
--- a/projects/SelfTest/Baselines/junit.sw.approved.txt
+++ b/projects/SelfTest/Baselines/junit.sw.approved.txt
@@ -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}"/>
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index 6322993f..7a9684dd 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -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>