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#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_COPYSIGNTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_COPYSIGNTEST_H
11
12#include "test/UnitTest/FEnvSafeTest.h"
13#include "test/UnitTest/FPMatcher.h"
14#include "test/UnitTest/Test.h"
15
16#include "hdr/math_macros.h"
17
18template <typename T>
19class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
20
21 DECLARE_SPECIAL_CONSTANTS(T)
22
23public:
24 typedef T (*CopySignFunc)(T, T);
25
26 void testSpecialNumbers(CopySignFunc func) {
27 EXPECT_FP_EQ(aNaN, func(aNaN, T(-1.0)));
28 EXPECT_FP_EQ(aNaN, func(aNaN, T(1.0)));
29
30 EXPECT_FP_EQ(neg_inf, func(inf, T(-1.0)));
31 EXPECT_FP_EQ(inf, func(neg_inf, T(1.0)));
32
33 EXPECT_FP_EQ(neg_zero, func(zero, T(-1.0)));
34 EXPECT_FP_EQ(zero, func(neg_zero, T(1.0)));
35 }
36
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 FPBits x_bits = FPBits(v);
42 T x = T(v);
43 if (x_bits.is_nan() || x_bits.is_inf())
44 continue;
45
46 T res1 = func(x, -x);
47 ASSERT_FP_EQ(res1, -x);
48
49 T res2 = func(x, x);
50 ASSERT_FP_EQ(res2, x);
51 }
52 }
53};
54
55#define LIST_COPYSIGN_TESTS(T, func) \
56 using LlvmLibcCopySignTest = CopySignTest<T>; \
57 TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \
58 TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }
59
60#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_COPYSIGNTEST_H
61

source code of libc/test/src/math/smoke/CopySignTest.h