[support] Fix PrintNumber Test on AIX
authorPaul Kirth <paulkirth@google.com>
Thu, 23 Mar 2023 17:13:35 +0000 (17:13 +0000)
committerPaul Kirth <paulkirth@google.com>
Thu, 23 Mar 2023 17:49:03 +0000 (17:49 +0000)
When fixing the test earlier, we missed the JSON case for NaN and INF,
so handle those the same as for non-JSON, by creating the string
dynamically.

Reviewed By: abhina.sreeskantharajan

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

llvm/unittests/Support/ScopedPrinterTest.cpp

index f62d310..9ebcb0b 100644 (file)
@@ -510,7 +510,16 @@ FirstSecondThirdByteMask [ (0x333)
 }
 
 TEST_F(ScopedPrinterTest, PrintNumber) {
-  auto PrintFunc = [](ScopedPrinter &W) {
+  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 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");
+
+  auto PrintFunc = [&](ScopedPrinter &W) {
     uint64_t Unsigned64Max = std::numeric_limits<uint64_t>::max();
     uint64_t Unsigned64Min = std::numeric_limits<uint64_t>::min();
     W.printNumber("uint64_t-max", Unsigned64Max);
@@ -556,10 +565,6 @@ TEST_F(ScopedPrinterTest, PrintNumber) {
 
     W.printNumber("label", "value", 0);
 
-    float MaxFloat = std::numeric_limits<float>::max();
-    float MinFloat = std::numeric_limits<float>::min();
-    float InfFloat = std::numeric_limits<float>::infinity();
-    float NaNFloat = std::nanf("1");
     W.printNumber("float-max", MaxFloat);
     W.printNumber("float-min", MinFloat);
     W.printNumber("float-inf", InfFloat);
@@ -567,11 +572,7 @@ TEST_F(ScopedPrinterTest, PrintNumber) {
     W.printNumber("float-42.0", 42.0f);
     W.printNumber("float-42.5625", 42.5625f);
 
-    double MaxDouble = std::numeric_limits<double>::max();
-    double MinDouble = std::numeric_limits<double>::min();
-    double InfDouble = std::numeric_limits<double>::infinity();
-    double NaNDouble = std::nan("1");
-    W.printNumber("double-max", MaxDouble);
+        W.printNumber("double-max", MaxDouble);
     W.printNumber("double-min", MinDouble);
     W.printNumber("double-inf", InfDouble);
     W.printNumber("double-nan", NaNDouble);
@@ -583,29 +584,30 @@ TEST_F(ScopedPrinterTest, PrintNumber) {
   // 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.
+  // formatting, since it uses exponents, which will be consistent. However,
+  // NaN and INF may be printed differently, (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", std::numeric_limits<float>::max()).snprint(Buf, sizeof(Buf));
+  format("%5.1f", MaxFloat).snprint(Buf, sizeof(Buf));
   std::string MaxFloatStr(Buf);
 
-  format("%5.1f", std::numeric_limits<double>::max()).snprint(Buf, sizeof(Buf));
+  format("%5.1f", MaxDouble).snprint(Buf, sizeof(Buf));
   std::string MaxDoubleStr(Buf);
 
-  format("%5.1f", std::numeric_limits<double>::infinity())
-      .snprint(Buf, sizeof(Buf));
+  format("%5.1f", InfFloat).snprint(Buf, sizeof(Buf));
   std::string InfFloatStr(Buf);
 
-  std::to_string(std::numeric_limits<float>::infinity());
+  format("%5.1f", InfDouble).snprint(Buf, sizeof(Buf));
   std::string InfDoubleStr(Buf);
 
-  format("%5.1f", std::nanf("1")).snprint(Buf, sizeof(Buf));
+  format("%5.1f", NaNFloat).snprint(Buf, sizeof(Buf));
   std::string NaNFloatStr(Buf);
 
-  format("%5.1f", std::nan("1")).snprint(Buf, sizeof(Buf));
+  format("%5.1f", NaNDouble).snprint(Buf, sizeof(Buf));
   std::string NaNDoubleStr(Buf);
 
   std::string ExpectedOut = Twine(
@@ -643,7 +645,7 @@ double-42.5625:  42.6
 )")
                                 .str();
 
-  const char *JSONExpectedOut = R"({
+  std::string JSONExpectedOut = Twine(R"({
   "uint64_t-max": 18446744073709551615,
   "uint64_t-min": 0,
   "uint32_t-max": 4294967295,
@@ -667,17 +669,17 @@ double-42.5625:  42.6
   },
   "float-max": 3.4028234663852886e+38,
   "float-min": 1.1754943508222875e-38,
-  "float-inf": inf,
-  "float-nan": nan,
+  "float-inf": )" + std::to_string(InfFloat) + R"(,
+  "float-nan": )" + std::to_string(NaNFloat) + R"(,
   "float-42.0": 42,
   "float-42.5625": 42.5625,
   "double-max": 1.7976931348623157e+308,
   "double-min": 2.2250738585072014e-308,
-  "double-inf": inf,
-  "double-nan": nan,
+  "double-inf": )" + std::to_string(InfDouble) + R"(,
+  "double-nan": )" + std::to_string(NaNDouble) + R"(,
   "double-42.0": 42,
   "double-42.5625": 42.5625
-})";
+})").str();
   verifyAll(ExpectedOut, JSONExpectedOut, PrintFunc);
 }