1//===-- Utility class to test fminimum_num[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#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMNUMTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMNUMTEST_H
11
12#include "src/__support/FPUtil/FPBits.h"
13#include "test/UnitTest/FEnvSafeTest.h"
14#include "test/UnitTest/FPMatcher.h"
15#include "test/UnitTest/Test.h"
16
17template <typename T>
18class FMinimumNumTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
19
20 DECLARE_SPECIAL_CONSTANTS(T)
21
22public:
23 typedef T (*FMinimumNumFunc)(T, T);
24
25 void testNaN(FMinimumNumFunc func) {
26 EXPECT_FP_EQ(inf, func(aNaN, inf));
27 EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(sNaN, inf), FE_INVALID);
28 EXPECT_FP_EQ(neg_inf, func(neg_inf, aNaN));
29 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(neg_inf, sNaN), FE_INVALID);
30 EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(aNaN, aNaN)).uintval());
31 EXPECT_FP_EQ(0.0, func(aNaN, 0.0));
32 EXPECT_FP_EQ(-0.0, func(-0.0, aNaN));
33 EXPECT_FP_EQ_WITH_EXCEPTION(0.0, func(sNaN, 0.0), FE_INVALID);
34 EXPECT_FP_EQ_WITH_EXCEPTION(-0.0, func(-0.0, sNaN), FE_INVALID);
35 EXPECT_FP_EQ(T(-1.2345), func(aNaN, T(-1.2345)));
36 EXPECT_FP_EQ(T(1.2345), func(T(1.2345), aNaN));
37 EXPECT_FP_EQ_WITH_EXCEPTION(T(-1.2345), func(sNaN, T(-1.2345)), FE_INVALID);
38 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.2345), func(T(1.2345), sNaN), FE_INVALID);
39 EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(aNaN, sNaN), FE_INVALID);
40 EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, aNaN), FE_INVALID);
41 EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(aNaN, sNaN)).uintval());
42 EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(sNaN, aNaN)).uintval());
43 EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(sNaN, sNaN)).uintval());
44 }
45
46 void testInfArg(FMinimumNumFunc func) {
47 EXPECT_FP_EQ(neg_inf, func(neg_inf, inf));
48 EXPECT_FP_EQ(0.0, func(inf, 0.0));
49 EXPECT_FP_EQ(-0.0, func(-0.0, inf));
50 EXPECT_FP_EQ(T(1.2345), func(inf, T(1.2345)));
51 EXPECT_FP_EQ(T(-1.2345), func(T(-1.2345), inf));
52 }
53
54 void testNegInfArg(FMinimumNumFunc func) {
55 EXPECT_FP_EQ(neg_inf, func(inf, neg_inf));
56 EXPECT_FP_EQ(neg_inf, func(neg_inf, 0.0));
57 EXPECT_FP_EQ(neg_inf, func(-0.0, neg_inf));
58 EXPECT_FP_EQ(neg_inf, func(neg_inf, T(-1.2345)));
59 EXPECT_FP_EQ(neg_inf, func(T(1.2345), neg_inf));
60 }
61
62 void testBothZero(FMinimumNumFunc func) {
63 EXPECT_FP_EQ(0.0, func(0.0, 0.0));
64 EXPECT_FP_EQ(-0.0, func(-0.0, 0.0));
65 EXPECT_FP_EQ(-0.0, func(0.0, -0.0));
66 EXPECT_FP_EQ(-0.0, func(-0.0, -0.0));
67 }
68
69 void testRange(FMinimumNumFunc func) {
70 constexpr StorageType COUNT = 100'001;
71 constexpr StorageType STEP = STORAGE_MAX / COUNT;
72 for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
73 ++i, v += STEP, w -= STEP) {
74 FPBits xbits(v), ybits(w);
75 if (xbits.is_inf_or_nan())
76 continue;
77 if (ybits.is_inf_or_nan())
78 continue;
79 T x = xbits.get_val();
80 T y = ybits.get_val();
81 if ((x == 0) && (y == 0))
82 continue;
83
84 if (x > y) {
85 EXPECT_FP_EQ(y, func(x, y));
86 } else {
87 EXPECT_FP_EQ(x, func(x, y));
88 }
89 }
90 }
91};
92
93#define LIST_FMINIMUM_NUM_TESTS(T, func) \
94 using LlvmLibcFMinimumNumTest = FMinimumNumTest<T>; \
95 TEST_F(LlvmLibcFMinimumNumTest, NaN) { testNaN(&func); } \
96 TEST_F(LlvmLibcFMinimumNumTest, InfArg) { testInfArg(&func); } \
97 TEST_F(LlvmLibcFMinimumNumTest, NegInfArg) { testNegInfArg(&func); } \
98 TEST_F(LlvmLibcFMinimumNumTest, BothZero) { testBothZero(&func); } \
99 TEST_F(LlvmLibcFMinimumNumTest, Range) { testRange(&func); }
100
101#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMNUMTEST_H
102

source code of libc/test/src/math/smoke/FMinimumNumTest.h