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

Move Approx's validity checks out of line into cpp file

This avoids having to include <stdexcept> in the main include path
and speeds up the compilation if Approx is used with multiple
different types.
Commit
84fa76e9850c08e7d54533f1b9645f63c6b73001
Author
Martin Hořeňovský ·
Parents
fcd91c7d6bf8

Files · Download .tar.gz

Changes

 include/internal/catch_approx.cpp | 22 ++++++++++++++++++++++
 include/internal/catch_approx.h   | 26 +++++++++-----------------
 2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/include/internal/catch_approx.cpp b/include/internal/catch_approx.cpp
index efe245cb..13cc77bb 100644
--- a/include/internal/catch_approx.cpp
+++ b/include/internal/catch_approx.cpp
@@ -10,6 +10,7 @@
 
 #include <cmath>
 #include <limits>
+#include <stdexcept>
 
 namespace {
 
@@ -54,6 +55,27 @@ namespace Detail {
         return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value)));
     }
 
+    void Approx::setMargin(double margin) {
+        if (margin < 0) {
+            throw std::domain_error
+            ("Invalid Approx::margin: " +
+             Catch::Detail::stringify(margin) +
+             ", Approx::Margin has to be non-negative.");
+
+        }
+        m_margin = margin;
+    }
+
+    void Approx::setEpsilon(double epsilon) {
+        if (epsilon < 0 || epsilon > 1.0) {
+            throw std::domain_error
+            ("Invalid Approx::epsilon: " +
+             Catch::Detail::stringify(epsilon) +
+             ", Approx::epsilon has to be between 0 and 1");
+        }
+        m_epsilon = epsilon;
+    }
+
 } // end namespace Detail
 
 namespace literals {
diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h
index 287e8307..c0696307 100644
--- a/include/internal/catch_approx.h
+++ b/include/internal/catch_approx.h
@@ -11,7 +11,6 @@
 #include "catch_tostring.h"
 
 #include <type_traits>
-#include <stdexcept>
 
 namespace Catch {
 namespace Detail {
@@ -19,6 +18,12 @@ namespace Detail {
     class Approx {
     private:
         bool equalityComparisonImpl(double other) const;
+        // Validates the new margin (margin >= 0)
+        // out-of-line to avoid including stdexcept in the header
+        void setMargin(double margin);
+        // Validates the new epsilon (0 < epsilon < 1)
+        // out-of-line to avoid including stdexcept in the header
+        void setEpsilon(double epsilon);
 
     public:
         explicit Approx ( double value );
@@ -85,27 +90,14 @@ namespace Detail {
         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
         Approx& epsilon( T const& newEpsilon ) {
             double epsilonAsDouble = static_cast<double>(newEpsilon);
-            if( epsilonAsDouble < 0 || epsilonAsDouble > 1.0 ) {
-                throw std::domain_error
-                    (   "Invalid Approx::epsilon: " +
-                        Catch::Detail::stringify( epsilonAsDouble ) +
-                        ", Approx::epsilon has to be between 0 and 1" );
-            }
-            m_epsilon = epsilonAsDouble;
+            setEpsilon(epsilonAsDouble);
             return *this;
         }
 
         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
         Approx& margin( T const& newMargin ) {
             double marginAsDouble = static_cast<double>(newMargin);
-            if( marginAsDouble < 0 ) {
-                throw std::domain_error
-                    (   "Invalid Approx::margin: " +
-                         Catch::Detail::stringify( marginAsDouble ) +
-                         ", Approx::Margin has to be non-negative." );
-
-            }
-            m_margin = marginAsDouble;
+            setMargin(marginAsDouble);
             return *this;
         }
 
@@ -124,7 +116,7 @@ namespace Detail {
         double m_value;
     };
 } // end namespace Detail
-    
+
 namespace literals {
     Detail::Approx operator "" _a(long double val);
     Detail::Approx operator "" _a(unsigned long long val);