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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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