12 changes: 5 additions & 7 deletions libc/test/src/math/sinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ TEST(LlvmLibcSinfTest, SpecialNumbers) {
errno = 0;

EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(aNaN));
EXPECT_EQ(errno, 0);
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ(0.0f, __llvm_libc::sinf(0.0f));
EXPECT_EQ(errno, 0);
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ(-0.0f, __llvm_libc::sinf(-0.0f));
EXPECT_EQ(errno, 0);
EXPECT_MATH_ERRNO(0);

errno = 0;
EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(inf));
EXPECT_EQ(errno, EDOM);
EXPECT_MATH_ERRNO(EDOM);

errno = 0;
EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(neg_inf));
EXPECT_EQ(errno, EDOM);
EXPECT_MATH_ERRNO(EDOM);
}

TEST(LlvmLibcSinfTest, InFloatRange) {
Expand Down
37 changes: 36 additions & 1 deletion libc/utils/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#ifndef LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
#define LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H

#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <math.h>

namespace __llvm_libc {
namespace fputil {
namespace testing {
Expand Down Expand Up @@ -97,4 +100,36 @@ FPMatcher<T, C> getMatcher(T expectedValue) {
__llvm_libc::fputil::testing::getMatcher<__llvm_libc::testing::Cond_NE>( \
expected))

#define EXPECT_MATH_ERRNO(expected) \
do { \
if (math_errhandling & MATH_ERRNO) { \
int actual = errno; \
errno = 0; \
EXPECT_EQ(actual, expected); \
} \
} while (0)

#define ASSERT_MATH_ERRNO(expected) \
do { \
if (math_errhandling & MATH_ERRNO) { \
int actual = errno; \
errno = 0; \
ASSERT_EQ(actual, expected); \
} \
} while (0)

#define EXPECT_FP_EXCEPTION(expected) \
do { \
if (math_errhandling & MATH_ERREXCEPT) { \
EXPECT_EQ(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT), expected); \
} \
} while (0)

#define ASSERT_FP_EXCEPTION(expected) \
do { \
if (math_errhandling & MATH_ERREXCEPT) { \
ASSERT_EQ(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT), expected); \
} \
} while (0)

#endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H