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//===----------------------------------------------------------------------===//
8
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"
17
18#include <errno.h>
19#include <stdint.h>
20
21using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
22
23namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
24
25TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
26 LIBC_NAMESPACE::libc_errno = 0;
27
28 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));
29 EXPECT_MATH_ERRNO(0);
30
31 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(inf));
32 EXPECT_MATH_ERRNO(0);
33
34 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(neg_inf));
35 EXPECT_MATH_ERRNO(0);
36
37 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(0.0f));
38 EXPECT_MATH_ERRNO(0);
39
40 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(-0.0f));
41 EXPECT_MATH_ERRNO(0);
42}
43
44TEST_F(LlvmLibcCoshfTest, Overflow) {
45 LIBC_NAMESPACE::libc_errno = 0;
46 EXPECT_FP_EQ_WITH_EXCEPTION(
47 inf, LIBC_NAMESPACE::coshf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
48 EXPECT_MATH_ERRNO(ERANGE);
49
50 EXPECT_FP_EQ_WITH_EXCEPTION(
51 inf, LIBC_NAMESPACE::coshf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW);
52 EXPECT_MATH_ERRNO(ERANGE);
53
54 EXPECT_FP_EQ_WITH_EXCEPTION(
55 inf, LIBC_NAMESPACE::coshf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW);
56 EXPECT_MATH_ERRNO(ERANGE);
57}
58
59TEST_F(LlvmLibcCoshfTest, InFloatRange) {
60 constexpr uint32_t COUNT = 100'000;
61 constexpr uint32_t STEP = UINT32_MAX / COUNT;
62 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
63 float x = FPBits(v).get_val();
64 if (isnan(x: x) || isinf(x: x))
65 continue;
66 ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::coshf(x), 0.5);
67 }
68}
69
70TEST_F(LlvmLibcCoshfTest, SmallValues) {
71 float x = FPBits(0x17800000U).get_val();
72 float result = LIBC_NAMESPACE::coshf(x);
73 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
74 EXPECT_FP_EQ(1.0f, result);
75
76 x = FPBits(0x0040000U).get_val();
77 result = LIBC_NAMESPACE::coshf(x);
78 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
79 EXPECT_FP_EQ(1.0f, result);
80}
81

source code of libc/test/src/math/coshf_test.cpp