• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Unittests for copysign --------------------------------------------===//
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 "src/math/copysign.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/UnitTest/Test.h"
13 #include <math.h>
14 
15 using FPBits = __llvm_libc::fputil::FPBits<double>;
16 
17 DECLARE_SPECIAL_CONSTANTS(double)
18 
TEST(CopySignTest,SpecialNumbers)19 TEST(CopySignTest, SpecialNumbers) {
20   EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, -1.0));
21   EXPECT_FP_EQ(nan, __llvm_libc::copysign(nan, 1.0));
22 
23   EXPECT_FP_EQ(negInf, __llvm_libc::copysign(inf, -1.0));
24   EXPECT_FP_EQ(inf, __llvm_libc::copysign(negInf, 1.0));
25 
26   EXPECT_FP_EQ(negZero, __llvm_libc::copysign(zero, -1.0));
27   EXPECT_FP_EQ(zero, __llvm_libc::copysign(negZero, 1.0));
28 }
29 
TEST(CopySignTest,InDoubleRange)30 TEST(CopySignTest, InDoubleRange) {
31   using UIntType = FPBits::UIntType;
32   constexpr UIntType count = 10000000;
33   constexpr UIntType step = UIntType(-1) / count;
34   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
35     double x = FPBits(v);
36     if (isnan(x) || isinf(x) || x == 0)
37       continue;
38 
39     double res1 = __llvm_libc::copysign(x, -x);
40     ASSERT_FP_EQ(res1, -x);
41 
42     double res2 = __llvm_libc::copysign(x, x);
43     ASSERT_FP_EQ(res2, x);
44   }
45 }
46