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

Console reporter now uses fixed decimal formatting

3 decimal places, output in seconds.
Commit
061a18303635e4101309ae874a09f0f4bf4477ad
Author
Martin Hořeňovský ·
Parents
70ac6dbb9fbd

Files · Download .tar.gz

Changes

 include/reporters/catch_reporter_console.hpp | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/include/reporters/catch_reporter_console.hpp b/include/reporters/catch_reporter_console.hpp
index 31adf8d4..6a3bd646 100644
--- a/include/reporters/catch_reporter_console.hpp
+++ b/include/reporters/catch_reporter_console.hpp
@@ -13,8 +13,31 @@
 #include "../internal/catch_reporter_registrars.hpp"
 #include "../internal/catch_console_colour.hpp"
 
+#include <cfloat>
+#include <cstdio>
+
 namespace Catch {
 
+    namespace {
+        // Because formatting using c++ streams is stateful, drop down to C is required
+        // Alternatively we could use stringstream, but its performance is... not good.
+        std::string getFormattedDuration( double duration ) {
+            // Max exponent + 1 is required to represent the whole part
+            // + 1 for decimal point
+            // + 3 for the 3 decimal places
+            // + 1 for null terminator
+            const size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1;
+            char buffer[maxDoubleSize];
+#ifdef _MSC_VER
+            sprintf_s(buffer, "%.3f", duration);
+#else
+            sprintf(buffer, "%.3f", duration);
+#endif
+            return std::string(buffer);
+        }
+    }
+
+
     struct ConsoleReporter : StreamingReporterBase {
         ConsoleReporter( ReporterConfig const& _config )
         :   StreamingReporterBase( _config ),
@@ -68,7 +91,7 @@ namespace Catch {
                 stream << " '" << _sectionStats.sectionInfo.name << "'\n" << std::endl;
             }
             if( m_config->showDurations() == ShowDurations::Always ) {
-                stream << _sectionStats.durationInSeconds << " s: " << _sectionStats.sectionInfo.name << std::endl;
+                stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl;
             }
             if( m_headerPrinted ) {
                 m_headerPrinted = false;