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 #include "test/UnitTest/FPMatcher.h"
10 #include "test/UnitTest/Test.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
15 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
17 template <typename T
> class FAbsTest
: public LIBC_NAMESPACE::testing::Test
{
19 DECLARE_SPECIAL_CONSTANTS(T
)
22 typedef T (*FabsFunc
)(T
);
24 void testSpecialNumbers(FabsFunc func
) {
25 EXPECT_FP_EQ(aNaN
, func(aNaN
));
27 EXPECT_FP_EQ(inf
, func(inf
));
28 EXPECT_FP_EQ(inf
, func(neg_inf
));
30 EXPECT_FP_EQ(zero
, func(zero
));
31 EXPECT_FP_EQ(zero
, func(neg_zero
));
34 void testRange(FabsFunc func
) {
35 constexpr UIntType COUNT
= 100'000;
36 constexpr UIntType STEP
= UIntType(-1) / COUNT
;
37 for (UIntType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
39 if (isnan(x
) || isinf(x
))
41 ASSERT_MPFR_MATCH(mpfr::Operation::Abs
, x
, func(x
), 0.0);
46 #define LIST_FABS_TESTS(T, func) \
47 using LlvmLibcFAbsTest = FAbsTest<T>; \
48 TEST_F(LlvmLibcFAbsTest, SpecialNumbers) { testSpecialNumbers(&func); } \
49 TEST_F(LlvmLibcFAbsTest, Range) { testRange(&func); }