Skip to content

Commit

Permalink
[libc][NFC] Add template tests for a bunch of math functions.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 34 changed files with 743 additions and 1,364 deletions.
48 changes: 48 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fabs_test.cpp
HDRS
FAbsTest.h
DEPENDS
libc.include.math
libc.src.math.fabs
Expand All @@ -95,6 +97,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fabsf_test.cpp
HDRS
FAbsTest.h
DEPENDS
libc.include.math
libc.src.math.fabsf
Expand All @@ -108,6 +112,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fabsl_test.cpp
HDRS
FAbsTest.h
DEPENDS
libc.include.math
libc.src.math.fabsl
Expand All @@ -121,6 +127,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
trunc_test.cpp
HDRS
TruncTest.h
DEPENDS
libc.include.math
libc.src.math.trunc
Expand All @@ -134,6 +142,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
truncf_test.cpp
HDRS
TruncTest.h
DEPENDS
libc.include.math
libc.src.math.truncf
Expand All @@ -147,6 +157,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
truncl_test.cpp
HDRS
TruncTest.h
DEPENDS
libc.include.math
libc.src.math.truncl
Expand All @@ -160,6 +172,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
ceil_test.cpp
HDRS
CeilTest.h
DEPENDS
libc.include.math
libc.src.math.ceil
Expand All @@ -173,6 +187,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
ceilf_test.cpp
HDRS
CeilTest.h
DEPENDS
libc.include.math
libc.src.math.ceilf
Expand All @@ -186,6 +202,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
ceill_test.cpp
HDRS
CeilTest.h
DEPENDS
libc.include.math
libc.src.math.ceill
Expand All @@ -199,6 +217,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
floor_test.cpp
HDRS
FloorTest.h
DEPENDS
libc.include.math
libc.src.math.floor
Expand All @@ -212,6 +232,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
floorf_test.cpp
HDRS
FloorTest.h
DEPENDS
libc.include.math
libc.src.math.floorf
Expand All @@ -225,6 +247,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
floorl_test.cpp
HDRS
FloorTest.h
DEPENDS
libc.include.math
libc.src.math.floorl
Expand All @@ -238,6 +262,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
round_test.cpp
HDRS
RoundTest.h
DEPENDS
libc.include.math
libc.src.math.round
Expand All @@ -251,6 +277,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
roundf_test.cpp
HDRS
RoundTest.h
DEPENDS
libc.include.math
libc.src.math.roundf
Expand All @@ -264,6 +292,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
roundl_test.cpp
HDRS
RoundTest.h
DEPENDS
libc.include.math
libc.src.math.roundl
Expand Down Expand Up @@ -559,6 +589,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
copysign_test.cpp
HDRS
CopySignTest.h
DEPENDS
libc.include.math
libc.src.math.copysign
Expand All @@ -571,6 +603,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
copysignf_test.cpp
HDRS
CopySignTest.h
DEPENDS
libc.include.math
libc.src.math.copysignf
Expand All @@ -583,6 +617,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
copysignl_test.cpp
HDRS
CopySignTest.h
DEPENDS
libc.include.math
libc.src.math.copysignl
Expand Down Expand Up @@ -832,6 +868,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fminf_test.cpp
HDRS
FMinTest.h
DEPENDS
libc.include.math
libc.src.math.fminf
Expand All @@ -844,6 +882,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fmin_test.cpp
HDRS
FMinTest.h
DEPENDS
libc.include.math
libc.src.math.fmin
Expand All @@ -856,6 +896,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fminl_test.cpp
HDRS
FMinTest.h
DEPENDS
libc.include.math
libc.src.math.fminl
Expand All @@ -868,6 +910,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fmaxf_test.cpp
HDRS
FMaxTest.h
DEPENDS
libc.include.math
libc.src.math.fmaxf
Expand All @@ -880,6 +924,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fmax_test.cpp
HDRS
FMaxTest.h
DEPENDS
libc.include.math
libc.src.math.fmax
Expand All @@ -892,6 +938,8 @@ add_fp_unittest(
libc_math_unittests
SRCS
fmaxl_test.cpp
HDRS
FMaxTest.h
DEPENDS
libc.include.math
libc.src.math.fmaxl
Expand Down
84 changes: 84 additions & 0 deletions libc/test/src/math/CeilTest.h
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); }
55 changes: 55 additions & 0 deletions libc/test/src/math/CopySignTest.h
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); }
Loading

0 comments on commit 80e166f

Please sign in to comment.