[libc-commits] [libc] a875765 - [libc][obvious] Switch copysign[f|l] and fabs[f|l] to the new test matchers.

Siva Chandra via libc-commits libc-commits at lists.llvm.org
Wed Aug 12 00:21:14 PDT 2020


Author: Siva Chandra
Date: 2020-08-12T00:20:58-07:00
New Revision: a87576592c6326275e4a7aff6a86d5113b5a9766

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

LOG: [libc][obvious] Switch copysign[f|l] and fabs[f|l] to the new test matchers.

Added: 
    

Modified: 
    libc/test/src/math/copysign_test.cpp
    libc/test/src/math/copysignf_test.cpp
    libc/test/src/math/copysignl_test.cpp
    libc/test/src/math/fabs_test.cpp
    libc/test/src/math/fabsf_test.cpp
    libc/test/src/math/fabsl_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/src/math/copysign_test.cpp b/libc/test/src/math/copysign_test.cpp
index e0638467c5b9..676e8fed95d4 100644
--- a/libc/test/src/math/copysign_test.cpp
+++ b/libc/test/src/math/copysign_test.cpp
@@ -8,56 +8,42 @@
 
 #include "include/math.h"
 #include "src/math/copysign.h"
-#include "utils/FPUtil/BitPatterns.h"
-#include "utils/FPUtil/FloatOperations.h"
-#include "utils/FPUtil/FloatProperties.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/UnitTest/Test.h"
 
-using __llvm_libc::fputil::valueAsBits;
-using __llvm_libc::fputil::valueFromBits;
+using FPBits = __llvm_libc::fputil::FPBits<double>;
 
-using BitPatterns = __llvm_libc::fputil::BitPatterns<double>;
-using Properties = __llvm_libc::fputil::FloatProperties<double>;
+static const double zero = FPBits::zero();
+static const double negZero = FPBits::negZero();
+static const double nan = FPBits::buildNaN(1);
+static const double inf = FPBits::inf();
+static const double negInf = FPBits::negInf();
 
 TEST(CopySignTest, SpecialNumbers) {
-  EXPECT_EQ(BitPatterns::aNegativeQuietNaN,
-            valueAsBits(__llvm_libc::copysign(
-                valueFromBits(BitPatterns::aQuietNaN), -1.0)));
-  EXPECT_EQ(BitPatterns::aQuietNaN,
-            valueAsBits(__llvm_libc::copysign(
-                valueFromBits(BitPatterns::aNegativeQuietNaN), 1.0)));
-
-  EXPECT_EQ(BitPatterns::aNegativeSignallingNaN,
-            valueAsBits(__llvm_libc::copysign(
-                valueFromBits(BitPatterns::aSignallingNaN), -1.0)));
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(__llvm_libc::copysign(
-                valueFromBits(BitPatterns::aNegativeSignallingNaN), 1.0)));
-
-  EXPECT_EQ(BitPatterns::negInf, valueAsBits(__llvm_libc::copysign(
-                                     valueFromBits(BitPatterns::inf), -1.0)));
-  EXPECT_EQ(BitPatterns::inf, valueAsBits(__llvm_libc::copysign(
-                                  valueFromBits(BitPatterns::negInf), 1.0)));
-
-  EXPECT_EQ(BitPatterns::negZero, valueAsBits(__llvm_libc::copysign(
-                                      valueFromBits(BitPatterns::zero), -1.0)));
-  EXPECT_EQ(BitPatterns::zero, valueAsBits(__llvm_libc::copysign(
-                                   valueFromBits(BitPatterns::negZero), 1.0)));
+  EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, -1.0));
+  EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, 1.0));
+
+  EXPECT_FP_EQ(negInf, __llvm_libc::copysign(inf, -1.0));
+  EXPECT_FP_EQ(inf, __llvm_libc::copysign(negInf, 1.0));
+
+  EXPECT_FP_EQ(negZero, __llvm_libc::copysign(zero, -1.0));
+  EXPECT_FP_EQ(zero, __llvm_libc::copysign(negZero, 1.0));
 }
 
 TEST(CopySignTest, InDoubleRange) {
-  using BitsType = Properties::BitsType;
-  constexpr BitsType count = 1000000;
-  constexpr BitsType step = UINT64_MAX / count;
-  for (BitsType i = 0, v = 0; i <= count; ++i, v += step) {
-    double x = valueFromBits(v);
+  using UIntType = FPBits::UIntType;
+  constexpr UIntType count = 10000000;
+  constexpr UIntType step = UIntType(-1) / count;
+  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+    double x = FPBits(v);
     if (isnan(x) || isinf(x) || x == 0)
       continue;
 
     double res1 = __llvm_libc::copysign(x, -x);
-    ASSERT_TRUE(res1 == -x);
+    ASSERT_FP_EQ(res1, -x);
 
     double res2 = __llvm_libc::copysign(x, x);
-    ASSERT_TRUE(res2 == x);
+    ASSERT_FP_EQ(res2, x);
   }
 }

diff  --git a/libc/test/src/math/copysignf_test.cpp b/libc/test/src/math/copysignf_test.cpp
index cb2cf518bde2..a7c434635545 100644
--- a/libc/test/src/math/copysignf_test.cpp
+++ b/libc/test/src/math/copysignf_test.cpp
@@ -1,5 +1,4 @@
-//===-- Unittests for copysignf
-//--------------------------------------------===//
+//===-- Unittests for copysignf -------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,57 +8,42 @@
 
 #include "include/math.h"
 #include "src/math/copysignf.h"
-#include "utils/FPUtil/BitPatterns.h"
-#include "utils/FPUtil/FloatOperations.h"
-#include "utils/FPUtil/FloatProperties.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/UnitTest/Test.h"
 
-using __llvm_libc::fputil::valueAsBits;
-using __llvm_libc::fputil::valueFromBits;
+using FPBits = __llvm_libc::fputil::FPBits<float>;
 
-using BitPatterns = __llvm_libc::fputil::BitPatterns<float>;
-using Properties = __llvm_libc::fputil::FloatProperties<float>;
+static const float zero = FPBits::zero();
+static const float negZero = FPBits::negZero();
+static const float nan = FPBits::buildNaN(1);
+static const float inf = FPBits::inf();
+static const float negInf = FPBits::negInf();
 
-TEST(CopySignFTest, SpecialNumbers) {
-  EXPECT_EQ(BitPatterns::aNegativeQuietNaN,
-            valueAsBits(__llvm_libc::copysignf(
-                valueFromBits(BitPatterns::aQuietNaN), -1.0f)));
-  EXPECT_EQ(BitPatterns::aQuietNaN,
-            valueAsBits(__llvm_libc::copysignf(
-                valueFromBits(BitPatterns::aNegativeQuietNaN), 1.0f)));
+TEST(CopySinfTest, SpecialNumbers) {
+  EXPECT_FP_EQ(nan, __llvm_libc::copysignf(nan, -1.0));
+  EXPECT_FP_EQ(nan, __llvm_libc::copysignf(nan, 1.0));
 
-  EXPECT_EQ(BitPatterns::aNegativeSignallingNaN,
-            valueAsBits(__llvm_libc::copysignf(
-                valueFromBits(BitPatterns::aSignallingNaN), -1.0f)));
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(__llvm_libc::copysignf(
-                valueFromBits(BitPatterns::aNegativeSignallingNaN), 1.0f)));
+  EXPECT_FP_EQ(negInf, __llvm_libc::copysignf(inf, -1.0));
+  EXPECT_FP_EQ(inf, __llvm_libc::copysignf(negInf, 1.0));
 
-  EXPECT_EQ(BitPatterns::negInf, valueAsBits(__llvm_libc::copysignf(
-                                     valueFromBits(BitPatterns::inf), -1.0f)));
-  EXPECT_EQ(BitPatterns::inf, valueAsBits(__llvm_libc::copysignf(
-                                  valueFromBits(BitPatterns::negInf), 1.0f)));
-
-  EXPECT_EQ(BitPatterns::negZero,
-            valueAsBits(__llvm_libc::copysignf(valueFromBits(BitPatterns::zero),
-                                               -1.0f)));
-  EXPECT_EQ(BitPatterns::zero, valueAsBits(__llvm_libc::copysignf(
-                                   valueFromBits(BitPatterns::negZero), 1.0f)));
+  EXPECT_FP_EQ(negZero, __llvm_libc::copysignf(zero, -1.0));
+  EXPECT_FP_EQ(zero, __llvm_libc::copysignf(negZero, 1.0));
 }
 
-TEST(CopySignFTest, InDoubleRange) {
-  using BitsType = Properties::BitsType;
-  constexpr BitsType count = 1000000;
-  constexpr BitsType step = UINT32_MAX / count;
-  for (BitsType i = 0, v = 0; i <= count; ++i, v += step) {
-    float x = valueFromBits(v);
+TEST(CopySinfTest, InFloatRange) {
+  using UIntType = FPBits::UIntType;
+  constexpr UIntType count = 1000000;
+  constexpr UIntType step = UIntType(-1) / count;
+  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+    float x = FPBits(v);
     if (isnan(x) || isinf(x) || x == 0)
       continue;
 
     float res1 = __llvm_libc::copysignf(x, -x);
-    ASSERT_TRUE(res1 == -x);
+    ASSERT_FP_EQ(res1, -x);
 
     float res2 = __llvm_libc::copysignf(x, x);
-    ASSERT_TRUE(res2 == x);
+    ASSERT_FP_EQ(res2, x);
   }
 }

diff  --git a/libc/test/src/math/copysignl_test.cpp b/libc/test/src/math/copysignl_test.cpp
index 7ffb5fb1fb73..efe54e4eca6b 100644
--- a/libc/test/src/math/copysignl_test.cpp
+++ b/libc/test/src/math/copysignl_test.cpp
@@ -1,5 +1,4 @@
-//===-- Unittests for copysignl
-//--------------------------------------------===//
+//===-- Unittests for copysignl -------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,26 +9,29 @@
 #include "include/math.h"
 #include "src/math/copysignl.h"
 #include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/UnitTest/Test.h"
 
 using FPBits = __llvm_libc::fputil::FPBits<long double>;
 
-TEST(CopysignlTest, SpecialNumbers) {
-  EXPECT_TRUE(FPBits::negZero() ==
-              __llvm_libc::copysignl(FPBits::zero(), -1.0l));
-  EXPECT_TRUE(FPBits::zero() ==
-              __llvm_libc::copysignl(FPBits::negZero(), 1.0l));
+static const long double zero = FPBits::zero();
+static const long double negZero = FPBits::negZero();
+static const long double nan = FPBits::buildNaN(1);
+static const long double inf = FPBits::inf();
+static const long double negInf = FPBits::negInf();
 
-  EXPECT_TRUE(FPBits::negZero() ==
-              __llvm_libc::copysignl(FPBits::zero(), -1.0l));
-  EXPECT_TRUE(FPBits::zero() ==
-              __llvm_libc::copysignl(FPBits::negZero(), 1.0l));
+TEST(CopySinlTest, SpecialNumbers) {
+  EXPECT_FP_EQ(nan, __llvm_libc::copysignl(nan, -1.0));
+  EXPECT_FP_EQ(nan, __llvm_libc::copysignl(nan, 1.0));
 
-  EXPECT_TRUE(
-      FPBits(__llvm_libc::copysignl(FPBits::buildNaN(1), -1.0l)).isNaN());
+  EXPECT_FP_EQ(negInf, __llvm_libc::copysignl(inf, -1.0));
+  EXPECT_FP_EQ(inf, __llvm_libc::copysignl(negInf, 1.0));
+
+  EXPECT_FP_EQ(negZero, __llvm_libc::copysignl(zero, -1.0));
+  EXPECT_FP_EQ(zero, __llvm_libc::copysignl(negZero, 1.0));
 }
 
-TEST(CopysignlTest, InDoubleRange) {
+TEST(CopySinlTest, InLongDoubleRange) {
   using UIntType = FPBits::UIntType;
   constexpr UIntType count = 10000000;
   constexpr UIntType step = UIntType(-1) / count;
@@ -39,9 +41,9 @@ TEST(CopysignlTest, InDoubleRange) {
       continue;
 
     long double res1 = __llvm_libc::copysignl(x, -x);
-    ASSERT_TRUE(res1 == -x);
+    ASSERT_FP_EQ(res1, -x);
 
     long double res2 = __llvm_libc::copysignl(x, x);
-    ASSERT_TRUE(res2 == x);
+    ASSERT_FP_EQ(res2, x);
   }
 }

diff  --git a/libc/test/src/math/fabs_test.cpp b/libc/test/src/math/fabs_test.cpp
index a9ce9e764298..4c3f28f06d30 100644
--- a/libc/test/src/math/fabs_test.cpp
+++ b/libc/test/src/math/fabs_test.cpp
@@ -8,17 +8,18 @@
 
 #include "include/math.h"
 #include "src/math/fabs.h"
-#include "utils/FPUtil/BitPatterns.h"
-#include "utils/FPUtil/FloatOperations.h"
-#include "utils/FPUtil/FloatProperties.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/MPFRWrapper/MPFRUtils.h"
 #include "utils/UnitTest/Test.h"
 
-using __llvm_libc::fputil::valueAsBits;
-using __llvm_libc::fputil::valueFromBits;
+using FPBits = __llvm_libc::fputil::FPBits<double>;
 
-using BitPatterns = __llvm_libc::fputil::BitPatterns<double>;
-using Properties = __llvm_libc::fputil::FloatProperties<double>;
+static const double zero = FPBits::zero();
+static const double negZero = FPBits::negZero();
+static const double nan = FPBits::buildNaN(1);
+static const double inf = FPBits::inf();
+static const double negInf = FPBits::negInf();
 
 namespace mpfr = __llvm_libc::testing::mpfr;
 
@@ -27,36 +28,21 @@ static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::doublePrecision, 0,
                                            0};
 
 TEST(FabsTest, SpecialNumbers) {
-  EXPECT_EQ(
-      BitPatterns::aQuietNaN,
-      valueAsBits(__llvm_libc::fabs(valueFromBits(BitPatterns::aQuietNaN))));
-  EXPECT_EQ(BitPatterns::aQuietNaN, valueAsBits(__llvm_libc::fabs(valueFromBits(
-                                        BitPatterns::aNegativeQuietNaN))));
+  EXPECT_FP_EQ(nan, __llvm_libc::fabs(nan));
 
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(
-                __llvm_libc::fabs(valueFromBits(BitPatterns::aSignallingNaN))));
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(__llvm_libc::fabs(
-                valueFromBits(BitPatterns::aNegativeSignallingNaN))));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabs(inf));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabs(negInf));
 
-  EXPECT_EQ(BitPatterns::inf,
-            valueAsBits(__llvm_libc::fabs(valueFromBits(BitPatterns::inf))));
-  EXPECT_EQ(BitPatterns::inf,
-            valueAsBits(__llvm_libc::fabs(valueFromBits(BitPatterns::negInf))));
-
-  EXPECT_EQ(BitPatterns::zero,
-            valueAsBits(__llvm_libc::fabs(valueFromBits(BitPatterns::zero))));
-  EXPECT_EQ(BitPatterns::zero, valueAsBits(__llvm_libc::fabs(
-                                   valueFromBits(BitPatterns::negZero))));
+  EXPECT_FP_EQ(zero, __llvm_libc::fabs(zero));
+  EXPECT_FP_EQ(zero, __llvm_libc::fabs(negZero));
 }
 
 TEST(FabsTest, InDoubleRange) {
-  using BitsType = Properties::BitsType;
-  constexpr BitsType count = 1000000;
-  constexpr BitsType step = UINT64_MAX / count;
-  for (BitsType i = 0, v = 0; i <= count; ++i, v += step) {
-    double x = valueFromBits(v);
+  using UIntType = FPBits::UIntType;
+  constexpr UIntType count = 10000000;
+  constexpr UIntType step = UIntType(-1) / count;
+  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+    double x = FPBits(v);
     if (isnan(x) || isinf(x))
       continue;
     ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabs(x), tolerance);

diff  --git a/libc/test/src/math/fabsf_test.cpp b/libc/test/src/math/fabsf_test.cpp
index 4231a251bf13..83425d7d2f8f 100644
--- a/libc/test/src/math/fabsf_test.cpp
+++ b/libc/test/src/math/fabsf_test.cpp
@@ -8,17 +8,18 @@
 
 #include "include/math.h"
 #include "src/math/fabsf.h"
-#include "utils/FPUtil/BitPatterns.h"
-#include "utils/FPUtil/FloatOperations.h"
-#include "utils/FPUtil/FloatProperties.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/MPFRWrapper/MPFRUtils.h"
 #include "utils/UnitTest/Test.h"
 
-using __llvm_libc::fputil::valueAsBits;
-using __llvm_libc::fputil::valueFromBits;
+using FPBits = __llvm_libc::fputil::FPBits<float>;
 
-using BitPatterns = __llvm_libc::fputil::BitPatterns<float>;
-using Properties = __llvm_libc::fputil::FloatProperties<float>;
+static const float zero = FPBits::zero();
+static const float negZero = FPBits::negZero();
+static const float nan = FPBits::buildNaN(1);
+static const float inf = FPBits::inf();
+static const float negInf = FPBits::negInf();
 
 namespace mpfr = __llvm_libc::testing::mpfr;
 
@@ -26,39 +27,22 @@ namespace mpfr = __llvm_libc::testing::mpfr;
 static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0,
                                            0};
 
-namespace mpfr = __llvm_libc::testing::mpfr;
 TEST(FabsfTest, SpecialNumbers) {
-  EXPECT_EQ(
-      BitPatterns::aQuietNaN,
-      valueAsBits(__llvm_libc::fabsf(valueFromBits(BitPatterns::aQuietNaN))));
-  EXPECT_EQ(BitPatterns::aQuietNaN,
-            valueAsBits(__llvm_libc::fabsf(
-                valueFromBits(BitPatterns::aNegativeQuietNaN))));
-
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(__llvm_libc::fabsf(
-                valueFromBits(BitPatterns::aSignallingNaN))));
-  EXPECT_EQ(BitPatterns::aSignallingNaN,
-            valueAsBits(__llvm_libc::fabsf(
-                valueFromBits(BitPatterns::aNegativeSignallingNaN))));
+  EXPECT_FP_EQ(nan, __llvm_libc::fabsf(nan));
 
-  EXPECT_EQ(BitPatterns::inf,
-            valueAsBits(__llvm_libc::fabsf(valueFromBits(BitPatterns::inf))));
-  EXPECT_EQ(BitPatterns::inf, valueAsBits(__llvm_libc::fabsf(
-                                  valueFromBits(BitPatterns::negInf))));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabsf(inf));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabsf(negInf));
 
-  EXPECT_EQ(BitPatterns::zero,
-            valueAsBits(__llvm_libc::fabsf(valueFromBits(BitPatterns::zero))));
-  EXPECT_EQ(BitPatterns::zero, valueAsBits(__llvm_libc::fabsf(
-                                   valueFromBits(BitPatterns::negZero))));
+  EXPECT_FP_EQ(zero, __llvm_libc::fabsf(zero));
+  EXPECT_FP_EQ(zero, __llvm_libc::fabsf(negZero));
 }
 
 TEST(FabsfTest, InFloatRange) {
-  using BitsType = Properties::BitsType;
-  constexpr BitsType count = 1000000;
-  constexpr BitsType step = UINT32_MAX / count;
-  for (BitsType i = 0, v = 0; i <= count; ++i, v += step) {
-    double x = valueFromBits(v);
+  using UIntType = FPBits::UIntType;
+  constexpr UIntType count = 1000000;
+  constexpr UIntType step = UIntType(-1) / count;
+  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+    float x = FPBits(v);
     if (isnan(x) || isinf(x))
       continue;
     ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabsf(x),

diff  --git a/libc/test/src/math/fabsl_test.cpp b/libc/test/src/math/fabsl_test.cpp
index 4a645682446a..b2ceb0c6205c 100644
--- a/libc/test/src/math/fabsl_test.cpp
+++ b/libc/test/src/math/fabsl_test.cpp
@@ -9,11 +9,18 @@
 #include "include/math.h"
 #include "src/math/fabsl.h"
 #include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
 #include "utils/MPFRWrapper/MPFRUtils.h"
 #include "utils/UnitTest/Test.h"
 
 using FPBits = __llvm_libc::fputil::FPBits<long double>;
 
+static const long double zero = FPBits::zero();
+static const long double negZero = FPBits::negZero();
+static const long double nan = FPBits::buildNaN(1);
+static const long double inf = FPBits::inf();
+static const long double negInf = FPBits::negInf();
+
 namespace mpfr = __llvm_libc::testing::mpfr;
 
 // Zero tolerance; As in, exact match with MPFR result.
@@ -21,15 +28,13 @@ static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0,
                                            0};
 
 TEST(FabslTest, SpecialNumbers) {
-  EXPECT_TRUE(FPBits::zero() == __llvm_libc::fabsl(FPBits::zero()));
-  EXPECT_TRUE(FPBits::zero() == __llvm_libc::fabsl(FPBits::negZero()));
+  EXPECT_FP_EQ(nan, __llvm_libc::fabsl(nan));
 
-  EXPECT_TRUE(FPBits::inf() == __llvm_libc::fabsl(FPBits::inf()));
-  EXPECT_TRUE(FPBits::inf() == __llvm_libc::fabsl(FPBits::negInf()));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabsl(inf));
+  EXPECT_FP_EQ(inf, __llvm_libc::fabsl(negInf));
 
-  long double nan = FPBits::buildNaN(1);
-  ASSERT_TRUE(isnan(nan) != 0);
-  ASSERT_TRUE(isnan(__llvm_libc::fabsl(nan)) != 0);
+  EXPECT_FP_EQ(zero, __llvm_libc::fabsl(zero));
+  EXPECT_FP_EQ(zero, __llvm_libc::fabsl(negZero));
 }
 
 TEST(FabslTest, InLongDoubleRange) {


        


More information about the libc-commits mailing list