xref: /aosp_15_r20/external/llvm-libc/test/src/math/CopySignTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Utility class to test copysign[f|l] ---------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "test/UnitTest/FEnvSafeTest.h"
10 #include "test/UnitTest/FPMatcher.h"
11 #include "test/UnitTest/Test.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
13 
14 #include "hdr/math_macros.h"
15 
16 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17 
18 template <typename T>
19 class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
20 
21   DECLARE_SPECIAL_CONSTANTS(T)
22 
23 public:
24   typedef T (*CopySignFunc)(T, T);
25 
testSpecialNumbers(CopySignFunc func)26   void testSpecialNumbers(CopySignFunc func) {
27     EXPECT_FP_EQ(aNaN, func(aNaN, -1.0));
28     EXPECT_FP_EQ(aNaN, func(aNaN, 1.0));
29 
30     EXPECT_FP_EQ(neg_inf, func(inf, -1.0));
31     EXPECT_FP_EQ(inf, func(neg_inf, 1.0));
32 
33     EXPECT_FP_EQ(neg_zero, func(zero, -1.0));
34     EXPECT_FP_EQ(zero, func(neg_zero, 1.0));
35   }
36 
testRange(CopySignFunc func)37   void testRange(CopySignFunc func) {
38     constexpr StorageType COUNT = 100'000;
39     constexpr StorageType STEP = STORAGE_MAX / COUNT;
40     for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
41       T x = FPBits(v).get_val();
42       if (FPBits(v).is_nan() || FPBits(v).is_inf())
43         continue;
44 
45       double res1 = func(x, -x);
46       ASSERT_FP_EQ(res1, -x);
47 
48       double res2 = func(x, x);
49       ASSERT_FP_EQ(res2, x);
50     }
51   }
52 };
53 
54 #define LIST_COPYSIGN_TESTS(T, func)                                           \
55   using LlvmLibcCopySignTest = CopySignTest<T>;                                \
56   TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); }  \
57   TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }
58