[libc-commits] [libc] 18fd6df - [libc][math] Add unit tests for raising excepts in nextafter (#73556)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 27 21:50:21 PST 2023
Author: Nishant Mittal
Date: 2023-11-28T00:50:17-05:00
New Revision: 18fd6df885da35f6b6e65a373d70297ac8db303b
URL: https://github.com/llvm/llvm-project/commit/18fd6df885da35f6b6e65a373d70297ac8db303b
DIFF: https://github.com/llvm/llvm-project/commit/18fd6df885da35f6b6e65a373d70297ac8db303b.diff
LOG: [libc][math] Add unit tests for raising excepts in nextafter (#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`.
cc: @lntue
Added:
Modified:
libc/test/UnitTest/FPMatcher.h
libc/test/src/math/smoke/NextAfterTest.h
Removed:
################################################################################
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index 8c3610022c62a48..90bda0067d0476b 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -143,7 +143,8 @@ template <typename T> struct FPTest : public Test {
#define EXPECT_FP_EXCEPTION(expected) \
do { \
if (math_errhandling & MATH_ERREXCEPT) { \
- EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
+ EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
+ (expected), \
expected); \
} \
} while (0)
@@ -151,7 +152,8 @@ template <typename T> struct FPTest : public Test {
#define ASSERT_FP_EXCEPTION(expected) \
do { \
if (math_errhandling & MATH_ERREXCEPT) { \
- ASSERT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected, \
+ ASSERT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
+ (expected), \
expected); \
} \
} while (0)
@@ -162,7 +164,7 @@ template <typename T> struct FPTest : public Test {
EXPECT_FP_EQ(expected_val, actual_val); \
if (math_errhandling & MATH_ERREXCEPT) { \
EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
- expected_except, \
+ (expected_except), \
expected_except); \
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
} \
@@ -174,7 +176,7 @@ template <typename T> struct FPTest : public Test {
EXPECT_FP_IS_NAN(actual_val); \
if (math_errhandling & MATH_ERREXCEPT) { \
EXPECT_GE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & \
- expected_except, \
+ (expected_except), \
expected_except); \
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); \
} \
diff --git a/libc/test/src/math/smoke/NextAfterTest.h b/libc/test/src/math/smoke/NextAfterTest.h
index 4e450cf5bc1e9e7..1b082050a598c3e 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;
More information about the libc-commits
mailing list