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
11f716f28d6621d1e54cf13374f8b5dd1a7489f5
Author
Martin Hořeňovský ·
Parents
c3ddd4a7e286

Files · Download .tar.gz

Changes

 include/internal/catch_approx.h                    |  4 +-
 projects/SelfTest/ApproxTests.cpp                  | 20 +++++
 .../SelfTest/Baselines/console.std.approved.txt    |  4 +-
 .../SelfTest/Baselines/console.sw.approved.txt     | 70 ++++++++++++++++-
 projects/SelfTest/Baselines/junit.sw.approved.txt  |  4 +-
 projects/SelfTest/Baselines/xml.sw.approved.txt    | 90 +++++++++++++++++++++-
 6 files changed, 184 insertions(+), 8 deletions(-)

diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h
index a2af9c86..69428c8a 100644
--- a/include/internal/catch_approx.h
+++ b/include/internal/catch_approx.h
@@ -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>
@@ -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>
@@ -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;
         }
 
diff --git a/projects/SelfTest/ApproxTests.cpp b/projects/SelfTest/ApproxTests.cpp
index bb6abffe..208be8e4 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,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
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index cc23a7dd..d15e5ab8 100644
--- a/projects/SelfTest/Baselines/console.std.approved.txt
+++ b/projects/SelfTest/Baselines/console.std.approved.txt
@@ -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
 
diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt
index e32d6f72..f47b7329 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -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) )
@@ -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
 -------------------------------------------------------------------------------
@@ -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
@@ -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
 
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 508b9206..8c186f0c 100644
--- a/projects/SelfTest/Baselines/junit.sw.approved.txt
+++ b/projects/SelfTest/Baselines/junit.sw.approved.txt
@@ -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}">
@@ -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}"/>
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index 04c6f986..07eb6a52 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -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)
@@ -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>
@@ -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" >
@@ -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>