1 //===-- Utility class to test copysign[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
= __llvm_libc::testing::mpfr
;
17 template <typename T
> class CopySignTest
: public __llvm_libc::testing::Test
{
19 DECLARE_SPECIAL_CONSTANTS(T
)
22 typedef T (*CopySignFunc
)(T
, T
);
24 void testSpecialNumbers(CopySignFunc func
) {
25 EXPECT_FP_EQ(aNaN
, func(aNaN
, -1.0));
26 EXPECT_FP_EQ(aNaN
, func(aNaN
, 1.0));
28 EXPECT_FP_EQ(neg_inf
, func(inf
, -1.0));
29 EXPECT_FP_EQ(inf
, func(neg_inf
, 1.0));
31 EXPECT_FP_EQ(neg_zero
, func(zero
, -1.0));
32 EXPECT_FP_EQ(zero
, func(neg_zero
, 1.0));
35 void testRange(CopySignFunc func
) {
36 constexpr UIntType COUNT
= 10000000;
37 constexpr UIntType STEP
= UIntType(-1) / COUNT
;
38 for (UIntType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
40 if (isnan(x
) || isinf(x
))
43 double res1
= func(x
, -x
);
44 ASSERT_FP_EQ(res1
, -x
);
46 double res2
= func(x
, x
);
47 ASSERT_FP_EQ(res2
, x
);
52 #define LIST_COPYSIGN_TESTS(T, func) \
53 using LlvmLibcCopySignTest = CopySignTest<T>; \
54 TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \
55 TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }