[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / math / coshf_test.cpp
blob0d1c322b8e62229379b83009386ba261ce253a73
1 //===-- Unittests for coshf -----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "hdr/math_macros.h"
10 #include "src/__support/CPP/array.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/errno/libc_errno.h"
13 #include "src/math/coshf.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
18 #include <stdint.h>
20 using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
22 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
24 TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
25 LIBC_NAMESPACE::libc_errno = 0;
27 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));
28 EXPECT_MATH_ERRNO(0);
30 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(inf));
31 EXPECT_MATH_ERRNO(0);
33 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(neg_inf));
34 EXPECT_MATH_ERRNO(0);
36 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(0.0f));
37 EXPECT_MATH_ERRNO(0);
39 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(-0.0f));
40 EXPECT_MATH_ERRNO(0);
43 TEST_F(LlvmLibcCoshfTest, Overflow) {
44 LIBC_NAMESPACE::libc_errno = 0;
45 EXPECT_FP_EQ_WITH_EXCEPTION(
46 inf, LIBC_NAMESPACE::coshf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
47 EXPECT_MATH_ERRNO(ERANGE);
49 EXPECT_FP_EQ_WITH_EXCEPTION(
50 inf, LIBC_NAMESPACE::coshf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW);
51 EXPECT_MATH_ERRNO(ERANGE);
53 EXPECT_FP_EQ_WITH_EXCEPTION(
54 inf, LIBC_NAMESPACE::coshf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW);
55 EXPECT_MATH_ERRNO(ERANGE);
58 TEST_F(LlvmLibcCoshfTest, InFloatRange) {
59 constexpr uint32_t COUNT = 100'000;
60 constexpr uint32_t STEP = UINT32_MAX / COUNT;
61 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
62 float x = FPBits(v).get_val();
63 if (FPBits(v).is_nan() || FPBits(v).is_inf())
64 continue;
65 ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::coshf(x), 0.5);
69 TEST_F(LlvmLibcCoshfTest, SmallValues) {
70 float x = FPBits(0x17800000U).get_val();
71 float result = LIBC_NAMESPACE::coshf(x);
72 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
73 EXPECT_FP_EQ(1.0f, result);
75 x = FPBits(0x0040000U).get_val();
76 result = LIBC_NAMESPACE::coshf(x);
77 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
78 EXPECT_FP_EQ(1.0f, result);