1 //===-- Utility class to test fabs[f|l] -------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H
12 #include "test/UnitTest/FEnvSafeTest.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
17 #include "hdr/math_macros.h"
19 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
22 class FAbsTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
24 DECLARE_SPECIAL_CONSTANTS(T
)
27 typedef T (*FabsFunc
)(T
);
29 void testSpecialNumbers(FabsFunc func
) {
30 EXPECT_FP_EQ(aNaN
, func(aNaN
));
32 EXPECT_FP_EQ(inf
, func(inf
));
33 EXPECT_FP_EQ(inf
, func(neg_inf
));
35 EXPECT_FP_EQ(zero
, func(zero
));
36 EXPECT_FP_EQ(zero
, func(neg_zero
));
39 void testRange(FabsFunc func
) {
40 constexpr StorageType COUNT
= 100'000;
41 constexpr StorageType STEP
= STORAGE_MAX
/ COUNT
;
42 for (StorageType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
43 T x
= FPBits(v
).get_val();
44 if (FPBits(v
).is_nan() || FPBits(v
).is_inf())
46 ASSERT_MPFR_MATCH(mpfr::Operation::Abs
, x
, func(x
), 0.0);
51 #define LIST_FABS_TESTS(T, func) \
52 using LlvmLibcFAbsTest = FAbsTest<T>; \
53 TEST_F(LlvmLibcFAbsTest, SpecialNumbers) { testSpecialNumbers(&func); } \
54 TEST_F(LlvmLibcFAbsTest, Range) { testRange(&func); }
56 #endif // LLVM_LIBC_TEST_SRC_MATH_FABSTEST_H