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

Add support for custom precision in floating point stringification

Closes #1612
Commit
53a83e855ec5c2a5659dd4ceacf15c00435eeaa6
Author
mlimber ·
Committer
Martin Hořeňovský
Parents
9c741fe96073

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.cpp                | 25 ++++++++++++++++++++--
 include/internal/catch_tostring.h                  |  9 ++++++++
 .../SelfTest/UsageTests/ToStringGeneral.tests.cpp  | 23 ++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/include/internal/catch_tostring.cpp b/include/internal/catch_tostring.cpp
index b857d3fb..33d442d6 100644
--- a/include/internal/catch_tostring.cpp
+++ b/include/internal/catch_tostring.cpp
@@ -234,11 +234,32 @@ std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
     return "nullptr";
 }
 
+int StringMaker<float>::m_precision = 5;
+   
 std::string StringMaker<float>::convert(float value) {
-    return fpToString(value, 5) + 'f';
+    return fpToString(value, m_precision) + 'f';
 }
+
+void StringMaker<float>::setPrecision(int precision) {
+    m_precision = precision;
+}
+
+int StringMaker<float>::getPrecision() {
+    return m_precision;
+}
+
+int StringMaker<double>::m_precision = 10;
+    
 std::string StringMaker<double>::convert(double value) {
-    return fpToString(value, 10);
+    return fpToString(value, m_precision);
+}
+
+void StringMaker<double>::setPrecision(int precision) {
+    m_precision = precision;
+}
+
+int StringMaker<double>::getPrecision() {
+    return m_precision;
 }
 
 std::string ratio_string<std::atto>::symbol() { return "a"; }
diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 52634a8c..89d0d633 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -261,10 +261,19 @@ namespace Catch {
     template<>
     struct StringMaker<float> {
         static std::string convert(float value);
+        static void setPrecision(int precision);
+        static int getPrecision();
+    private:
+        static int m_precision;
     };
+
     template<>
     struct StringMaker<double> {
         static std::string convert(double value);
+        static void setPrecision(int precision);
+        static int getPrecision();
+    private:
+        static int m_precision;
     };
 
     template <typename T>
diff --git a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
index 09ac3045..bbcc9a66 100644
--- a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
+++ b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
@@ -128,6 +128,29 @@ TEST_CASE("String views are stringified like other strings", "[toString][approva
 
 #endif
 
+//TEMPLATE_TEST_CASE("Floating-point precision can be set", "[toString][floatingPoint]", float, double)
+//{
+//    const auto oldPrecision = Catch::StringMaker<TestType>::getPrecision();
+//    const auto precision = GENERATE(-1, 0, 3, std::numeric_limits<TestType>::max_digits10);
+//    const auto expectedLength = unsigned(precision < 0 ? 3 : precision);
+//
+//    CAPTURE( precision );
+//
+//    if (precision >= 0)
+//    {
+//        Catch::StringMaker<TestType>::setPrecision(precision);
+//    }
+//
+//    // Expected to fail to demonstrate the problem
+//    const auto str = Catch::StringMaker<TestType>::convert(std::numeric_limits<TestType>::epsilon());
+//    CHECK(str.length() >= expectedLength);
+//
+//    if (precision >= 0)
+//    {
+//        Catch::StringMaker<TestType>::setPrecision(oldPrecision);
+//    }
+//}
+
 namespace {
 
 struct WhatException : std::exception {