lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master d46e3440 07/11: Modernize duff_fmt()


From: Greg Chicares
Subject: [lmi-commits] [lmi] master d46e3440 07/11: Modernize duff_fmt()
Date: Fri, 29 Apr 2022 11:59:33 -0400 (EDT)

branch: master
commit d46e3440629a2588efa0444018b0320da6e1c199
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Modernize duff_fmt()
    
    When this code was originally written in 2002, C99 could not be assumed;
    now it can. Accordingly, added tests for formatting infinities and NaN.
    The astute reviewer will notice the surreptitious correction of a defect
    that those tests revealed.
---
 duff_fmt.hpp      |  8 ++------
 duff_fmt_test.cpp | 33 ++++++++++++---------------------
 objects.make      |  1 +
 3 files changed, 15 insertions(+), 27 deletions(-)

diff --git a/duff_fmt.hpp b/duff_fmt.hpp
index 3606a9ee..83f3c17f 100644
--- a/duff_fmt.hpp
+++ b/duff_fmt.hpp
@@ -1,4 +1,4 @@
-// Format NNNN.NNN --> "N,NNN.NN".
+// Format doubles with thousands separators.
 //
 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Gregory W. 
Chicares.
 //
@@ -19,10 +19,6 @@
 // email: <gchicares@sbcglobal.net>
 // snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
 
-// Ideally we would use imbue an appropriate locale and use some facet
-// like money_put, but most of our compilers's standard libraries don't
-// support that, so we use this US-specific workaround.
-
 #ifndef duff_fmt_hpp
 #define duff_fmt_hpp
 
@@ -61,7 +57,7 @@ inline std::string duff_fmt(double value, int decimals)
     if(nullptr == r)
         {
         // Infinities and NaNs need no commas.
-        return out_buf;
+        return p;
         }
 
     switch((r - p) % 3)
diff --git a/duff_fmt_test.cpp b/duff_fmt_test.cpp
index 07fe737b..f53dd379 100644
--- a/duff_fmt_test.cpp
+++ b/duff_fmt_test.cpp
@@ -1,4 +1,4 @@
-// Format NNNN.NNN --> "N,NNN.NN": unit test.
+// Format doubles with thousands separators: unit test.
 //
 // Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Gregory W. Chicares.
 //
@@ -23,14 +23,11 @@
 
 #include "duff_fmt.hpp"
 
+#include "miscellany.hpp"               // begins_with()
 #include "test_tools.hpp"
 
 #include <limits>
 
-#if defined __BORLANDC__
-#   include <float.h>                   // nonstandard _control87()
-#endif // defined __BORLANDC__
-
 int test_main(int, char*[])
 {
     // Format positive numbers, with two decimals.
@@ -201,27 +198,21 @@ int test_main(int, char*[])
         ||              "2,000.00" == duff_fmt(      1999.995     , 2)
         );
 
-    // Infinities and NaNs. Apparently the C89/90 standard referenced
-    // by C++98 does not specify a unique string representation
-    // 
http://groups.google.com/groups?as_umsgid=5cj5ae%24f4e%241%40shade.twinsun.com
-    // so we test only that the formatting routine executes.
-    // C99 does specify the result [7.19.6.1/8], but that doesn't
-    // affect the standard C++ language as this is written in 2002.
-
-#if defined __BORLANDC__
-    // By default, the borland compiler traps infinity and NaNs,
-    // and signals a hardware exception; but we want to test them,
-    // so we mask them from the application.
-    _control87(0x00ff,  0x00ff);
-#endif // defined __BORLANDC__
+    // Infinities and NaNs.
 
     double volatile d = 0.0;
-    duff_fmt( 1.0 / d, 2);
-    duff_fmt(-1.0 / d, 2);
+    std::string pos_inf = duff_fmt( 1.0 / d, 2);
+    std::string neg_inf = duff_fmt(-1.0 / d, 2);
+    LMI_TEST( "inf" == pos_inf ||  "infinity" == pos_inf);
+    LMI_TEST("-inf" == neg_inf || "-infinity" == neg_inf);
 
     if(std::numeric_limits<double>::has_quiet_NaN)
         {
-        duff_fmt(std::numeric_limits<double>::quiet_NaN(), 2);
+        constexpr double quiet_NaN = std::numeric_limits<double>::quiet_NaN();
+        std::string qnan = duff_fmt(quiet_NaN, 2);
+        // Test only "nan", disregarding any 'n-char-sequence' payload.
+        // The sign of quiet_NaN() seems to be unspecified.
+        LMI_TEST(begins_with(qnan, "nan") || begins_with(qnan, "-nan"));
         }
 
     return 0;
diff --git a/objects.make b/objects.make
index 0cfe266f..8a31fb0d 100644
--- a/objects.make
+++ b/objects.make
@@ -579,6 +579,7 @@ dbo_rules_test$(EXEEXT): \
 duff_fmt_test$(EXEEXT): \
   $(common_test_objects) \
   duff_fmt_test.o \
+  miscellany.o \
 
 et_vector_test$(EXEEXT): \
   $(common_test_objects) \



reply via email to

[Prev in Thread] Current Thread [Next in Thread]