1//===-- Utility class to test fmin[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_FMINTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_FMINTEST_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#include "utils/MPFRWrapper/MPFRUtils.h"
17
18#include "hdr/math_macros.h"
19
20namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
21
22template <typename T>
23class FMinTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
24
25 DECLARE_SPECIAL_CONSTANTS(T)
26
27public:
28 typedef T (*FMinFunc)(T, T);
29
30 void testNaN(FMinFunc func) {
31 EXPECT_FP_EQ(inf, func(aNaN, inf));
32 EXPECT_FP_EQ(neg_inf, func(neg_inf, aNaN));
33 EXPECT_FP_EQ(zero, func(aNaN, zero));
34 EXPECT_FP_EQ(neg_zero, func(neg_zero, aNaN));
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(aNaN, func(aNaN, aNaN));
38 }
39
40 void testInfArg(FMinFunc func) {
41 EXPECT_FP_EQ(neg_inf, func(neg_inf, inf));
42 EXPECT_FP_EQ(zero, func(inf, zero));
43 EXPECT_FP_EQ(neg_zero, func(neg_zero, inf));
44 EXPECT_FP_EQ(T(1.2345), func(inf, T(1.2345)));
45 EXPECT_FP_EQ(T(-1.2345), func(T(-1.2345), inf));
46 }
47
48 void testNegInfArg(FMinFunc func) {
49 EXPECT_FP_EQ(neg_inf, func(inf, neg_inf));
50 EXPECT_FP_EQ(neg_inf, func(neg_inf, zero));
51 EXPECT_FP_EQ(neg_inf, func(neg_zero, neg_inf));
52 EXPECT_FP_EQ(neg_inf, func(neg_inf, T(-1.2345)));
53 EXPECT_FP_EQ(neg_inf, func(T(1.2345), neg_inf));
54 }
55
56 void testBothZero(FMinFunc func) {
57 EXPECT_FP_EQ(zero, func(zero, zero));
58 EXPECT_FP_EQ(neg_zero, func(neg_zero, zero));
59 EXPECT_FP_EQ(neg_zero, func(zero, neg_zero));
60 EXPECT_FP_EQ(neg_zero, func(neg_zero, neg_zero));
61 }
62
63 void testRange(FMinFunc func) {
64 constexpr StorageType COUNT = 100'001;
65 constexpr StorageType STEP = STORAGE_MAX / COUNT;
66 for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
67 ++i, v += STEP, w -= STEP) {
68 T x = FPBits(v).get_val(), y = FPBits(w).get_val();
69 if (FPBits(v).is_nan() || FPBits(v).is_inf())
70 continue;
71 if (FPBits(w).is_nan() || FPBits(w).is_inf())
72 continue;
73 if ((x == 0) && (y == 0))
74 continue;
75
76 if (x > y) {
77 EXPECT_FP_EQ(y, func(x, y));
78 } else {
79 EXPECT_FP_EQ(x, func(x, y));
80 }
81 }
82 }
83};
84
85#define LIST_FMIN_TESTS(T, func) \
86 using LlvmLibcFMinTest = FMinTest<T>; \
87 TEST_F(LlvmLibcFMinTest, NaN) { testNaN(&func); } \
88 TEST_F(LlvmLibcFMinTest, InfArg) { testInfArg(&func); } \
89 TEST_F(LlvmLibcFMinTest, NegInfArg) { testNegInfArg(&func); } \
90 TEST_F(LlvmLibcFMinTest, BothZero) { testBothZero(&func); } \
91 TEST_F(LlvmLibcFMinTest, Range) { testRange(&func); }
92
93#endif // LLVM_LIBC_TEST_SRC_MATH_FMINTEST_H
94

source code of libc/test/src/math/FMinTest.h