-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc][NFC] Add template tests for a bunch of math functions.
Namely, template tests have been added for the following functions: ceil, copysign, fabs, fmax, fmin, floor, trunc, round.
- Loading branch information
Siva Chandra Reddy
committed
Apr 16, 2021
1 parent
82240e0
commit 80e166f
Showing
34 changed files
with
743 additions
and
1,364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//===-- Utility class to test ceil[f|l] -------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "utils/FPUtil/TestHelpers.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
#include "utils/UnitTest/Test.h" | ||
|
||
#include <math.h> | ||
|
||
namespace mpfr = __llvm_libc::testing::mpfr; | ||
|
||
template <typename T> class CeilTest : public __llvm_libc::testing::Test { | ||
|
||
DECLARE_SPECIAL_CONSTANTS(T) | ||
|
||
public: | ||
typedef T (*CeilFunc)(T); | ||
|
||
void testSpecialNumbers(CeilFunc func) { | ||
EXPECT_FP_EQ(zero, func(zero)); | ||
EXPECT_FP_EQ(negZero, func(negZero)); | ||
|
||
EXPECT_FP_EQ(inf, func(inf)); | ||
EXPECT_FP_EQ(negInf, func(negInf)); | ||
|
||
EXPECT_FP_EQ(aNaN, func(aNaN)); | ||
} | ||
|
||
void testRoundedNumbers(CeilFunc func) { | ||
EXPECT_FP_EQ(T(1.0), func(T(1.0))); | ||
EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); | ||
EXPECT_FP_EQ(T(10.0), func(T(10.0))); | ||
EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); | ||
EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); | ||
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); | ||
} | ||
|
||
void testFractions(CeilFunc func) { | ||
EXPECT_FP_EQ(T(1.0), func(T(0.5))); | ||
EXPECT_FP_EQ(T(-0.0), func(T(-0.5))); | ||
EXPECT_FP_EQ(T(1.0), func(T(0.115))); | ||
EXPECT_FP_EQ(T(-0.0), func(T(-0.115))); | ||
EXPECT_FP_EQ(T(1.0), func(T(0.715))); | ||
EXPECT_FP_EQ(T(-0.0), func(T(-0.715))); | ||
EXPECT_FP_EQ(T(2.0), func(T(1.3))); | ||
EXPECT_FP_EQ(T(-1.0), func(T(-1.3))); | ||
EXPECT_FP_EQ(T(2.0), func(T(1.5))); | ||
EXPECT_FP_EQ(T(-1.0), func(T(-1.5))); | ||
EXPECT_FP_EQ(T(2.0), func(T(1.75))); | ||
EXPECT_FP_EQ(T(-1.0), func(T(-1.75))); | ||
EXPECT_FP_EQ(T(11.0), func(T(10.32))); | ||
EXPECT_FP_EQ(T(-10.0), func(T(-10.32))); | ||
EXPECT_FP_EQ(T(11.0), func(T(10.65))); | ||
EXPECT_FP_EQ(T(-10.0), func(T(-10.65))); | ||
EXPECT_FP_EQ(T(1235.0), func(T(1234.38))); | ||
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38))); | ||
EXPECT_FP_EQ(T(1235.0), func(T(1234.96))); | ||
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96))); | ||
} | ||
|
||
void testRange(CeilFunc func) { | ||
constexpr UIntType count = 10000000; | ||
constexpr UIntType step = UIntType(-1) / count; | ||
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { | ||
T x = T(FPBits(v)); | ||
if (isnan(x) || isinf(x)) | ||
continue; | ||
|
||
ASSERT_MPFR_MATCH(mpfr::Operation::Ceil, x, func(x), 0.0); | ||
} | ||
} | ||
}; | ||
|
||
#define LIST_CEIL_TESTS(T, func) \ | ||
using LlvmLibcCeilTest = CeilTest<T>; \ | ||
TEST_F(LlvmLibcCeilTest, SpecialNumbers) { testSpecialNumbers(&func); } \ | ||
TEST_F(LlvmLibcCeilTest, RoundedNubmers) { testRoundedNumbers(&func); } \ | ||
TEST_F(LlvmLibcCeilTest, Fractions) { testFractions(&func); } \ | ||
TEST_F(LlvmLibcCeilTest, Range) { testRange(&func); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===-- Utility class to test copysign[f|l] ---------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "utils/FPUtil/TestHelpers.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
#include "utils/UnitTest/Test.h" | ||
|
||
#include <math.h> | ||
|
||
namespace mpfr = __llvm_libc::testing::mpfr; | ||
|
||
template <typename T> class CopySignTest : public __llvm_libc::testing::Test { | ||
|
||
DECLARE_SPECIAL_CONSTANTS(T) | ||
|
||
public: | ||
typedef T (*CopySignFunc)(T, T); | ||
|
||
void testSpecialNumbers(CopySignFunc func) { | ||
EXPECT_FP_EQ(aNaN, func(aNaN, -1.0)); | ||
EXPECT_FP_EQ(aNaN, func(aNaN, 1.0)); | ||
|
||
EXPECT_FP_EQ(negInf, func(inf, -1.0)); | ||
EXPECT_FP_EQ(inf, func(negInf, 1.0)); | ||
|
||
EXPECT_FP_EQ(negZero, func(zero, -1.0)); | ||
EXPECT_FP_EQ(zero, func(negZero, 1.0)); | ||
} | ||
|
||
void testRange(CopySignFunc func) { | ||
constexpr UIntType count = 10000000; | ||
constexpr UIntType step = UIntType(-1) / count; | ||
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { | ||
T x = T(FPBits(v)); | ||
if (isnan(x) || isinf(x)) | ||
continue; | ||
|
||
double res1 = func(x, -x); | ||
ASSERT_FP_EQ(res1, -x); | ||
|
||
double res2 = func(x, x); | ||
ASSERT_FP_EQ(res2, x); | ||
} | ||
} | ||
}; | ||
|
||
#define LIST_COPYSIGN_TESTS(T, func) \ | ||
using LlvmLibcCopySignTest = CopySignTest<T>; \ | ||
TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \ | ||
TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); } |
Oops, something went wrong.