[llvm] f331e13 - [support] Revise ScopedPrinter formatting tests for floats

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 7 10:09:18 PDT 2023


Author: Paul Kirth
Date: 2023-04-07T17:09:08Z
New Revision: f331e13d53c58703073f189195f99def5e3ff6c1

URL: https://github.com/llvm/llvm-project/commit/f331e13d53c58703073f189195f99def5e3ff6c1
DIFF: https://github.com/llvm/llvm-project/commit/f331e13d53c58703073f189195f99def5e3ff6c1.diff

LOG: [support] Revise ScopedPrinter formatting tests for floats

Previously there were several attempts to make the format checks for NaN
and Inf work across platforms, like AIX and Solaris, that print these
values slightly differently. This resulted in a number of forward fixes,
until we finally disabled the tests for NaN and Inf. This change should
make the test robust across different platforms, and reduce the overall
amount of code by delegating to helper functions that use the same
format strings as the implementations used by PrintNumber().

This additionally reverts commit 5a9bad171be5dfdf9430a0f6cbff14d29ca54181
and fa56e362af475e0758cfb41c42f78db50da7235c.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D146851

Added: 
    

Modified: 
    llvm/unittests/Support/ScopedPrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/ScopedPrinterTest.cpp b/llvm/unittests/Support/ScopedPrinterTest.cpp
index 1b2d03bedaaa6..895e04c2c024a 100644
--- a/llvm/unittests/Support/ScopedPrinterTest.cpp
+++ b/llvm/unittests/Support/ScopedPrinterTest.cpp
@@ -509,15 +509,37 @@ FirstSecondThirdByteMask [ (0x333)
   verifyAll(ExpectedOut, JSONExpectedOut, PrintFunc);
 }
 
+// Format floats using the same format string as PrintNumber, so we can check
+// the output on all platforms.
+template <typename T,
+          std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
+std::string formatFloatString(T Val) {
+  std::string Ret;
+  raw_string_ostream OS(Ret);
+  OS << format("%5.1f", Val);
+  return Ret;
+}
+
+// Format floats using the same format string used in JSON, so we can check the
+// output on all platforms.
+template <typename T,
+          std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
+std::string formatJsonFloatString(T Val) {
+  std::string Ret;
+  raw_string_ostream OS(Ret);
+  OS << format("%.*g", std::numeric_limits<double>::max_digits10, Val);
+  return Ret;
+}
+
 TEST_F(ScopedPrinterTest, PrintNumber) {
   constexpr float MaxFloat = std::numeric_limits<float>::max();
   constexpr float MinFloat = std::numeric_limits<float>::min();
-  // constexpr float InfFloat = std::numeric_limits<float>::infinity();
-  // const float NaNFloat = std::nanf("1");
+  constexpr float InfFloat = std::numeric_limits<float>::infinity();
+  const float NaNFloat = std::nanf("1");
   constexpr double MaxDouble = std::numeric_limits<double>::max();
   constexpr double MinDouble = std::numeric_limits<double>::min();
-  // constexpr double InfDouble = std::numeric_limits<double>::infinity();
-  // const double NaNDouble = std::nan("1");
+  constexpr double InfDouble = std::numeric_limits<double>::infinity();
+  const double NaNDouble = std::nan("1");
 
   auto PrintFunc = [&](ScopedPrinter &W) {
     uint64_t Unsigned64Max = std::numeric_limits<uint64_t>::max();
@@ -567,43 +589,19 @@ TEST_F(ScopedPrinterTest, PrintNumber) {
 
     W.printNumber("float-max", MaxFloat);
     W.printNumber("float-min", MinFloat);
+    W.printNumber("float-inf", InfFloat);
+    W.printNumber("float-nan", NaNFloat);
     W.printNumber("float-42.0", 42.0f);
     W.printNumber("float-42.5625", 42.5625f);
 
     W.printNumber("double-max", MaxDouble);
     W.printNumber("double-min", MinDouble);
+    W.printNumber("double-inf", InfDouble);
+    W.printNumber("double-nan", NaNDouble);
     W.printNumber("double-42.0", 42.0);
     W.printNumber("double-42.5625", 42.5625);
-
-    // FIXME: temporarily disable checking the for Inf and NaN until we have a
-    // cross platform solution can handle this case
-    // W.printNumber("float-inf", InfFloat);
-    // W.printNumber("float-nan", NaNFloat);
-    // W.printNumber("double-inf", InfDouble);
-    // W.printNumber("double-nan", NaNDouble);
   };
 
-  // Make sure when we check floating point representation we avoid
-  // implementation defined behavior. So format the max float/double, instead of
-  // hard coding it in the tests. Note: we can't just use std::to_string(),
-  // since we format the float in PrintNumber(). This isn't required for JSON
-  // formatting, since it uses exponents, which will be consistent. However,
-  // NaN and INF may be printed 
diff erently, (like AIX), so we still need to 
-  // handle those cases for JSON checking.
-
-  // Allocate a buffer large enough to represent large floating point values
-  // and construct the string representation for them there.
-  char Buf[512];
-
-  format("%5.1f", MaxFloat).snprint(Buf, sizeof(Buf));
-  std::string MaxFloatStr(Buf);
-
-  format("%5.1f", MaxDouble).snprint(Buf, sizeof(Buf));
-  std::string MaxDoubleStr(Buf);
-
-  // FIXME: temporarily disable checking the for Inf and NaN until we have a
-  // cross platform solution can handle this case
-
   std::string ExpectedOut = Twine(
                                 R"(uint64_t-max: 18446744073709551615
 uint64_t-min: 0
@@ -623,13 +621,22 @@ int8_t-max: 127
 int8_t-min: -128
 apsint: 9999999999999999999999
 label: value (0)
-float-max: )" + MaxFloatStr + R"(
+float-max: )" + formatFloatString(MaxFloat) +
+                                R"(
 float-min:   0.0
+float-inf: )" + formatFloatString(InfFloat) +
+                                R"(
+float-nan: )" + formatFloatString(NaNFloat) +
+                                R"(
 float-42.0:  42.0
 float-42.5625:  42.6
-double-max: )" + MaxDoubleStr +
+double-max: )" + formatFloatString(MaxDouble) +
                                 R"(
 double-min:   0.0
+double-inf: )" + formatFloatString(InfDouble) +
+                                R"(
+double-nan: )" + formatFloatString(NaNDouble) +
+                                R"(
 double-42.0:  42.0
 double-42.5625:  42.6
 )")
@@ -659,10 +666,18 @@ double-42.5625:  42.6
   },
   "float-max": 3.4028234663852886e+38,
   "float-min": 1.1754943508222875e-38,
+  "float-inf": )" + formatJsonFloatString(InfFloat) +
+                                      R"(,
+  "float-nan": )" + formatJsonFloatString(NaNFloat) +
+                                      R"(,
   "float-42.0": 42,
   "float-42.5625": 42.5625,
   "double-max": 1.7976931348623157e+308,
   "double-min": 2.2250738585072014e-308,
+  "double-inf": )" + formatJsonFloatString(InfDouble) +
+                                      R"(,
+  "double-nan": )" + formatJsonFloatString(NaNDouble) +
+                                      R"(,
   "double-42.0": 42,
   "double-42.5625": 42.5625
 })")


        


More information about the llvm-commits mailing list