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 "utils/FPUtil/TestHelpers.h" 10 #include "utils/MPFRWrapper/MPFRUtils.h" 11 #include "utils/UnitTest/Test.h" 12 13 #include <math.h> 14 15 namespace mpfr = __llvm_libc::testing::mpfr; 16 17 template <typename T> class CopySignTest : public __llvm_libc::testing::Test { 18 19 DECLARE_SPECIAL_CONSTANTS(T) 20 21 public: 22 typedef T (*CopySignFunc)(T, T); 23 testSpecialNumbers(CopySignFunc func)24 void testSpecialNumbers(CopySignFunc func) { 25 EXPECT_FP_EQ(aNaN, func(aNaN, -1.0)); 26 EXPECT_FP_EQ(aNaN, func(aNaN, 1.0)); 27 28 EXPECT_FP_EQ(negInf, func(inf, -1.0)); 29 EXPECT_FP_EQ(inf, func(negInf, 1.0)); 30 31 EXPECT_FP_EQ(negZero, func(zero, -1.0)); 32 EXPECT_FP_EQ(zero, func(negZero, 1.0)); 33 } 34 testRange(CopySignFunc func)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) { 39 T x = T(FPBits(v)); 40 if (isnan(x) || isinf(x)) 41 continue; 42 43 double res1 = func(x, -x); 44 ASSERT_FP_EQ(res1, -x); 45 46 double res2 = func(x, x); 47 ASSERT_FP_EQ(res2, x); 48 } 49 } 50 }; 51 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); } 56