diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h index 8c3610022c62a4..90bda0067d0476 100644 --- a/libc/test/UnitTest/FPMatcher.h +++ b/libc/test/UnitTest/FPMatcher.h @@ -143,7 +143,8 @@ template 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 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 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 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 4e450cf5bc1e9e..1b082050a598c3 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 +#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 class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test { using FPBits = LIBC_NAMESPACE::fputil::FPBits; @@ -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(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(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(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(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(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(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(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(min_subnormal); result = func(x, 1); expected_bits = min_subnormal + 1; expected = LIBC_NAMESPACE::cpp::bit_cast(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(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(min_normal); result = func(x, 0); expected_bits = max_subnormal; expected = LIBC_NAMESPACE::cpp::bit_cast(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(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(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;