[libc-commits] [libc] [libc][math] Add unit tests for raising excepts in nextafter (PR #73556)

Nishant Mittal via libc-commits libc-commits at lists.llvm.org
Mon Nov 27 10:59:26 PST 2023


https://github.com/nishantwrp created https://github.com/llvm/llvm-project/pull/73556

Follow up to https://github.com/llvm/llvm-project/pull/72763#discussion_r1398277962. 

### Summary
- Add unit tests for raising excepts in `nextafter`. 
- Fixed a bug in testing code for `nexttoward`.  



>From 65f0647f4fd51d8992d0f55f255b71cdf6db80cb Mon Sep 17 00:00:00 2001
From: Nishant Mittal <nishantwrp at google.com>
Date: Mon, 27 Nov 2023 18:56:51 +0000
Subject: [PATCH] [libc][math] Add unit tests for raising excepts in nextafter

---
 libc/test/src/math/smoke/NextAfterTest.h  | 39 +++++++++++++++--------
 libc/test/src/math/smoke/NextTowardTest.h |  4 +--
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/libc/test/src/math/smoke/NextAfterTest.h b/libc/test/src/math/smoke/NextAfterTest.h
index 4e450cf5bc1e9e7..69eb9446a9ffa16 100644
--- a/libc/test/src/math/smoke/NextAfterTest.h
+++ b/libc/test/src/math/smoke/NextAfterTest.h
@@ -17,6 +17,17 @@
 #include "test/UnitTest/Test.h"
 #include <math.h>
 
+#define ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, expected_exception)      \
+  ASSERT_FP_EQ(result, expected);                                              \
+  ASSERT_FP_EXCEPTION(expected_exception);                                     \
+  LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
+
+#define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected)                          \
+  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_UNDERFLOW))
+
+#define ASSERT_FP_EQ_WITH_OVERFLOW(result, expected)                           \
+  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_OVERFLOW))
+
 template <typename T>
 class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
@@ -53,23 +64,23 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
     T result = func(x, T(1));
     UIntType expected_bits = 1;
     T expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     result = func(x, T(-1));
     expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     x = neg_zero;
     result = func(x, 1);
     expected_bits = 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     result = func(x, -1);
     expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     // 'from' is max subnormal value.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_subnormal);
@@ -80,7 +91,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
     result = func(x, 0);
     expected_bits = max_subnormal - 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     x = -x;
 
@@ -93,30 +104,30 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
     expected_bits =
         (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal - 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     // 'from' is min subnormal value.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_subnormal);
     result = func(x, 1);
     expected_bits = min_subnormal + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
-    ASSERT_FP_EQ(func(x, 0), 0);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), 0);
 
     x = -x;
     result = func(x, -1);
     expected_bits =
         (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_subnormal + 1;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
-    ASSERT_FP_EQ(func(x, 0), T(-0.0));
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), T(-0.0));
 
     // 'from' is min normal.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
     result = func(x, 0);
     expected_bits = max_subnormal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     result = func(x, inf);
     expected_bits = min_normal + 1;
@@ -127,7 +138,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
     result = func(x, 0);
     expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal;
     expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
-    ASSERT_FP_EQ(result, expected);
+    ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
 
     result = func(x, -inf);
     expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal + 1;
@@ -137,10 +148,10 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
     // 'from' is max normal and 'to' is infinity.
     x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_normal);
     result = func(x, inf);
-    ASSERT_FP_EQ(result, inf);
+    ASSERT_FP_EQ_WITH_OVERFLOW(result, inf);
 
     result = func(-x, -inf);
-    ASSERT_FP_EQ(result, -inf);
+    ASSERT_FP_EQ_WITH_OVERFLOW(result, -inf);
 
     // 'from' is infinity.
     x = inf;
diff --git a/libc/test/src/math/smoke/NextTowardTest.h b/libc/test/src/math/smoke/NextTowardTest.h
index 4d27592a11422bf..fc37a9d1fd3d95d 100644
--- a/libc/test/src/math/smoke/NextTowardTest.h
+++ b/libc/test/src/math/smoke/NextTowardTest.h
@@ -24,10 +24,10 @@
   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
 
 #define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected)                          \
-  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_UNDERFLOW)
+  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_UNDERFLOW))
 
 #define ASSERT_FP_EQ_WITH_OVERFLOW(result, expected)                           \
-  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_OVERFLOW)
+  ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_OVERFLOW))
 
 template <typename T>
 class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::Test {



More information about the libc-commits mailing list