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
= LIBC_NAMESPACE::testing::mpfr
;
18 class CopySignTest
: public LIBC_NAMESPACE::testing::Test
{
20 DECLARE_SPECIAL_CONSTANTS(T
)
23 typedef T (*CopySignFunc
)(T
, T
);
25 void testSpecialNumbers(CopySignFunc func
) {
26 EXPECT_FP_EQ(aNaN
, func(aNaN
, -1.0));
27 EXPECT_FP_EQ(aNaN
, func(aNaN
, 1.0));
29 EXPECT_FP_EQ(neg_inf
, func(inf
, -1.0));
30 EXPECT_FP_EQ(inf
, func(neg_inf
, 1.0));
32 EXPECT_FP_EQ(neg_zero
, func(zero
, -1.0));
33 EXPECT_FP_EQ(zero
, func(neg_zero
, 1.0));
36 void testRange(CopySignFunc func
) {
37 constexpr UIntType COUNT
= 100'000;
38 constexpr UIntType STEP
= UIntType(-1) / COUNT
;
39 for (UIntType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
41 if (isnan(x
) || isinf(x
))
44 double res1
= func(x
, -x
);
45 ASSERT_FP_EQ(res1
, -x
);
47 double res2
= func(x
, x
);
48 ASSERT_FP_EQ(res2
, x
);
53 #define LIST_COPYSIGN_TESTS(T, func) \
54 using LlvmLibcCopySignTest = CopySignTest<T>; \
55 TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \
56 TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }