76 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/errno_macros.h"10#include "hdr/math_macros.h"11#include "hdr/stdint_proxy.h"12#include "src/__support/CPP/array.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/math/coshf.h"15#include "test/UnitTest/FPMatcher.h"16#include "test/UnitTest/Test.h"17#include "utils/MPFRWrapper/MPFRUtils.h"18 19using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;20 21namespace mpfr = LIBC_NAMESPACE::testing::mpfr;22 23TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {24 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));25 EXPECT_MATH_ERRNO(0);26 27 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(inf));28 EXPECT_MATH_ERRNO(0);29 30 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(neg_inf));31 EXPECT_MATH_ERRNO(0);32 33 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(0.0f));34 EXPECT_MATH_ERRNO(0);35 36 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(-0.0f));37 EXPECT_MATH_ERRNO(0);38}39 40TEST_F(LlvmLibcCoshfTest, Overflow) {41 EXPECT_FP_EQ_WITH_EXCEPTION(42 inf, LIBC_NAMESPACE::coshf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);43 EXPECT_MATH_ERRNO(ERANGE);44 45 EXPECT_FP_EQ_WITH_EXCEPTION(46 inf, LIBC_NAMESPACE::coshf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW);47 EXPECT_MATH_ERRNO(ERANGE);48 49 EXPECT_FP_EQ_WITH_EXCEPTION(50 inf, LIBC_NAMESPACE::coshf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW);51 EXPECT_MATH_ERRNO(ERANGE);52}53 54TEST_F(LlvmLibcCoshfTest, InFloatRange) {55 constexpr uint32_t COUNT = 100'000;56 constexpr uint32_t STEP = UINT32_MAX / COUNT;57 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {58 float x = FPBits(v).get_val();59 if (FPBits(v).is_nan() || FPBits(v).is_inf())60 continue;61 ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::coshf(x), 0.5);62 }63}64 65TEST_F(LlvmLibcCoshfTest, SmallValues) {66 float x = FPBits(0x17800000U).get_val();67 float result = LIBC_NAMESPACE::coshf(x);68 EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);69 EXPECT_FP_EQ(1.0f, result);70 71 x = FPBits(0x0040000U).get_val();72 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