1//===-- Unittests for cbrtf -----------------------------------------------===//
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/FPUtil/FPBits.h"
11#include "src/math/cbrtf.h"
12#include "test/UnitTest/FPMatcher.h"
13#include "test/UnitTest/Test.h"
14#include "utils/MPFRWrapper/MPFRUtils.h"
15
16using LlvmLibcCbrtfTest = LIBC_NAMESPACE::testing::FPTest<float>;
17
18namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
19
20TEST_F(LlvmLibcCbrtfTest, InFloatRange) {
21 constexpr uint32_t COUNT = 100'000;
22 const uint32_t STEP = FPBits(inf).uintval() / COUNT;
23 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
24 float x = FPBits(v).get_val();
25 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, x,
26 LIBC_NAMESPACE::cbrtf(x), 0.5);
27 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cbrt, -x,
28 LIBC_NAMESPACE::cbrtf(-x), 0.5);
29 }
30}
31
32TEST_F(LlvmLibcCbrtfTest, SpecialValues) {
33 constexpr float INPUTS[] = {
34 0x1.60451p2f, 0x1.31304cp1f, 0x1.d17cp2f, 0x1.bp-143f, 0x1.338cp2f,
35 };
36 for (float v : INPUTS) {
37 float x = FPBits(v).get_val();
38 mpfr::ForceRoundingMode r(mpfr::RoundingMode::Upward);
39 EXPECT_MPFR_MATCH(mpfr::Operation::Cbrt, x, LIBC_NAMESPACE::cbrtf(x), 0.5,
40 mpfr::RoundingMode::Upward);
41 }
42}
43

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