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

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