Skip to content

Commit 80e166f

Browse files
author
Siva Chandra Reddy
committed
[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.
1 parent 82240e0 commit 80e166f

34 files changed

+743
-1364
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ add_fp_unittest(
8282
libc_math_unittests
8383
SRCS
8484
fabs_test.cpp
85+
HDRS
86+
FAbsTest.h
8587
DEPENDS
8688
libc.include.math
8789
libc.src.math.fabs
@@ -95,6 +97,8 @@ add_fp_unittest(
9597
libc_math_unittests
9698
SRCS
9799
fabsf_test.cpp
100+
HDRS
101+
FAbsTest.h
98102
DEPENDS
99103
libc.include.math
100104
libc.src.math.fabsf
@@ -108,6 +112,8 @@ add_fp_unittest(
108112
libc_math_unittests
109113
SRCS
110114
fabsl_test.cpp
115+
HDRS
116+
FAbsTest.h
111117
DEPENDS
112118
libc.include.math
113119
libc.src.math.fabsl
@@ -121,6 +127,8 @@ add_fp_unittest(
121127
libc_math_unittests
122128
SRCS
123129
trunc_test.cpp
130+
HDRS
131+
TruncTest.h
124132
DEPENDS
125133
libc.include.math
126134
libc.src.math.trunc
@@ -134,6 +142,8 @@ add_fp_unittest(
134142
libc_math_unittests
135143
SRCS
136144
truncf_test.cpp
145+
HDRS
146+
TruncTest.h
137147
DEPENDS
138148
libc.include.math
139149
libc.src.math.truncf
@@ -147,6 +157,8 @@ add_fp_unittest(
147157
libc_math_unittests
148158
SRCS
149159
truncl_test.cpp
160+
HDRS
161+
TruncTest.h
150162
DEPENDS
151163
libc.include.math
152164
libc.src.math.truncl
@@ -160,6 +172,8 @@ add_fp_unittest(
160172
libc_math_unittests
161173
SRCS
162174
ceil_test.cpp
175+
HDRS
176+
CeilTest.h
163177
DEPENDS
164178
libc.include.math
165179
libc.src.math.ceil
@@ -173,6 +187,8 @@ add_fp_unittest(
173187
libc_math_unittests
174188
SRCS
175189
ceilf_test.cpp
190+
HDRS
191+
CeilTest.h
176192
DEPENDS
177193
libc.include.math
178194
libc.src.math.ceilf
@@ -186,6 +202,8 @@ add_fp_unittest(
186202
libc_math_unittests
187203
SRCS
188204
ceill_test.cpp
205+
HDRS
206+
CeilTest.h
189207
DEPENDS
190208
libc.include.math
191209
libc.src.math.ceill
@@ -199,6 +217,8 @@ add_fp_unittest(
199217
libc_math_unittests
200218
SRCS
201219
floor_test.cpp
220+
HDRS
221+
FloorTest.h
202222
DEPENDS
203223
libc.include.math
204224
libc.src.math.floor
@@ -212,6 +232,8 @@ add_fp_unittest(
212232
libc_math_unittests
213233
SRCS
214234
floorf_test.cpp
235+
HDRS
236+
FloorTest.h
215237
DEPENDS
216238
libc.include.math
217239
libc.src.math.floorf
@@ -225,6 +247,8 @@ add_fp_unittest(
225247
libc_math_unittests
226248
SRCS
227249
floorl_test.cpp
250+
HDRS
251+
FloorTest.h
228252
DEPENDS
229253
libc.include.math
230254
libc.src.math.floorl
@@ -238,6 +262,8 @@ add_fp_unittest(
238262
libc_math_unittests
239263
SRCS
240264
round_test.cpp
265+
HDRS
266+
RoundTest.h
241267
DEPENDS
242268
libc.include.math
243269
libc.src.math.round
@@ -251,6 +277,8 @@ add_fp_unittest(
251277
libc_math_unittests
252278
SRCS
253279
roundf_test.cpp
280+
HDRS
281+
RoundTest.h
254282
DEPENDS
255283
libc.include.math
256284
libc.src.math.roundf
@@ -264,6 +292,8 @@ add_fp_unittest(
264292
libc_math_unittests
265293
SRCS
266294
roundl_test.cpp
295+
HDRS
296+
RoundTest.h
267297
DEPENDS
268298
libc.include.math
269299
libc.src.math.roundl
@@ -559,6 +589,8 @@ add_fp_unittest(
559589
libc_math_unittests
560590
SRCS
561591
copysign_test.cpp
592+
HDRS
593+
CopySignTest.h
562594
DEPENDS
563595
libc.include.math
564596
libc.src.math.copysign
@@ -571,6 +603,8 @@ add_fp_unittest(
571603
libc_math_unittests
572604
SRCS
573605
copysignf_test.cpp
606+
HDRS
607+
CopySignTest.h
574608
DEPENDS
575609
libc.include.math
576610
libc.src.math.copysignf
@@ -583,6 +617,8 @@ add_fp_unittest(
583617
libc_math_unittests
584618
SRCS
585619
copysignl_test.cpp
620+
HDRS
621+
CopySignTest.h
586622
DEPENDS
587623
libc.include.math
588624
libc.src.math.copysignl
@@ -832,6 +868,8 @@ add_fp_unittest(
832868
libc_math_unittests
833869
SRCS
834870
fminf_test.cpp
871+
HDRS
872+
FMinTest.h
835873
DEPENDS
836874
libc.include.math
837875
libc.src.math.fminf
@@ -844,6 +882,8 @@ add_fp_unittest(
844882
libc_math_unittests
845883
SRCS
846884
fmin_test.cpp
885+
HDRS
886+
FMinTest.h
847887
DEPENDS
848888
libc.include.math
849889
libc.src.math.fmin
@@ -856,6 +896,8 @@ add_fp_unittest(
856896
libc_math_unittests
857897
SRCS
858898
fminl_test.cpp
899+
HDRS
900+
FMinTest.h
859901
DEPENDS
860902
libc.include.math
861903
libc.src.math.fminl
@@ -868,6 +910,8 @@ add_fp_unittest(
868910
libc_math_unittests
869911
SRCS
870912
fmaxf_test.cpp
913+
HDRS
914+
FMaxTest.h
871915
DEPENDS
872916
libc.include.math
873917
libc.src.math.fmaxf
@@ -880,6 +924,8 @@ add_fp_unittest(
880924
libc_math_unittests
881925
SRCS
882926
fmax_test.cpp
927+
HDRS
928+
FMaxTest.h
883929
DEPENDS
884930
libc.include.math
885931
libc.src.math.fmax
@@ -892,6 +938,8 @@ add_fp_unittest(
892938
libc_math_unittests
893939
SRCS
894940
fmaxl_test.cpp
941+
HDRS
942+
FMaxTest.h
895943
DEPENDS
896944
libc.include.math
897945
libc.src.math.fmaxl

libc/test/src/math/CeilTest.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===-- Utility class to test ceil[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 CeilTest : public __llvm_libc::testing::Test {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef T (*CeilFunc)(T);
23+
24+
void testSpecialNumbers(CeilFunc func) {
25+
EXPECT_FP_EQ(zero, func(zero));
26+
EXPECT_FP_EQ(negZero, func(negZero));
27+
28+
EXPECT_FP_EQ(inf, func(inf));
29+
EXPECT_FP_EQ(negInf, func(negInf));
30+
31+
EXPECT_FP_EQ(aNaN, func(aNaN));
32+
}
33+
34+
void testRoundedNumbers(CeilFunc func) {
35+
EXPECT_FP_EQ(T(1.0), func(T(1.0)));
36+
EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
37+
EXPECT_FP_EQ(T(10.0), func(T(10.0)));
38+
EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
39+
EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
40+
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
41+
}
42+
43+
void testFractions(CeilFunc func) {
44+
EXPECT_FP_EQ(T(1.0), func(T(0.5)));
45+
EXPECT_FP_EQ(T(-0.0), func(T(-0.5)));
46+
EXPECT_FP_EQ(T(1.0), func(T(0.115)));
47+
EXPECT_FP_EQ(T(-0.0), func(T(-0.115)));
48+
EXPECT_FP_EQ(T(1.0), func(T(0.715)));
49+
EXPECT_FP_EQ(T(-0.0), func(T(-0.715)));
50+
EXPECT_FP_EQ(T(2.0), func(T(1.3)));
51+
EXPECT_FP_EQ(T(-1.0), func(T(-1.3)));
52+
EXPECT_FP_EQ(T(2.0), func(T(1.5)));
53+
EXPECT_FP_EQ(T(-1.0), func(T(-1.5)));
54+
EXPECT_FP_EQ(T(2.0), func(T(1.75)));
55+
EXPECT_FP_EQ(T(-1.0), func(T(-1.75)));
56+
EXPECT_FP_EQ(T(11.0), func(T(10.32)));
57+
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
58+
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
59+
EXPECT_FP_EQ(T(-10.0), func(T(-10.65)));
60+
EXPECT_FP_EQ(T(1235.0), func(T(1234.38)));
61+
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
62+
EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
63+
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96)));
64+
}
65+
66+
void testRange(CeilFunc func) {
67+
constexpr UIntType count = 10000000;
68+
constexpr UIntType step = UIntType(-1) / count;
69+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
70+
T x = T(FPBits(v));
71+
if (isnan(x) || isinf(x))
72+
continue;
73+
74+
ASSERT_MPFR_MATCH(mpfr::Operation::Ceil, x, func(x), 0.0);
75+
}
76+
}
77+
};
78+
79+
#define LIST_CEIL_TESTS(T, func) \
80+
using LlvmLibcCeilTest = CeilTest<T>; \
81+
TEST_F(LlvmLibcCeilTest, SpecialNumbers) { testSpecialNumbers(&func); } \
82+
TEST_F(LlvmLibcCeilTest, RoundedNubmers) { testRoundedNumbers(&func); } \
83+
TEST_F(LlvmLibcCeilTest, Fractions) { testFractions(&func); } \
84+
TEST_F(LlvmLibcCeilTest, Range) { testRange(&func); }

libc/test/src/math/CopySignTest.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
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+
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); }

0 commit comments

Comments
 (0)