Skip to content

Commit

Permalink
[libc][math] Fix RIntTest.h not expecting FP exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty committed Jun 5, 2024
1 parent ea477a6 commit d69f987
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions libc/test/src/math/RIntTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,43 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
void testSpecialNumbers(RIntFunc func) {
for (int mode : ROUNDING_MODES) {
LIBC_NAMESPACE::fputil::set_round(mode);
ASSERT_FP_EQ(inf, func(inf));
ASSERT_FP_EQ(neg_inf, func(neg_inf));
ASSERT_FP_EQ(nan, func(nan));
ASSERT_FP_EQ(zero, func(zero));
ASSERT_FP_EQ(neg_zero, func(neg_zero));
EXPECT_FP_EQ(inf, func(inf));
EXPECT_FP_EQ(neg_inf, func(neg_inf));
EXPECT_FP_EQ(nan, func(nan));
EXPECT_FP_EQ(zero, func(zero));
EXPECT_FP_EQ(neg_zero, func(neg_zero));
}
}

void testRoundNumbers(RIntFunc func) {
for (int mode : ROUNDING_MODES) {
LIBC_NAMESPACE::fputil::set_round(mode);
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode));
ASSERT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode));
ASSERT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode));
EXPECT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode));
EXPECT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode));
EXPECT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode));
EXPECT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode));
EXPECT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode));
EXPECT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode));
}
}

void testFractions(RIntFunc func) {
for (int mode : ROUNDING_MODES) {
LIBC_NAMESPACE::fputil::set_round(mode);
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mpfr_mode));
ASSERT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode));
ASSERT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode));
EXPECT_FP_EQ_WITH_EXCEPTION(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode),
FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(func(T(-0.5)),
mpfr::round(T(-0.5), mpfr_mode), FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(func(T(0.115)),
mpfr::round(T(0.115), mpfr_mode), FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(
func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode), FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(func(T(0.715)),
mpfr::round(T(0.715), mpfr_mode), FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(
func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode), FE_INEXACT);
}
}

Expand All @@ -110,7 +116,8 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
for (int mode : ROUNDING_MODES) {
LIBC_NAMESPACE::fputil::set_round(mode);
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
EXPECT_FP_EQ_WITH_EXCEPTION(func(x), mpfr::round(x, mpfr_mode),
FE_INEXACT);
}
}
}
Expand All @@ -131,7 +138,7 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
for (int mode : ROUNDING_MODES) {
LIBC_NAMESPACE::fputil::set_round(mode);
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
EXPECT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
}
}
}
Expand Down

0 comments on commit d69f987

Please sign in to comment.