Make test more lenient for custom clang version strings
[llvm-project.git] / libc / test / src / math / ILogbTest.h
blob5d805ef7a1e2262255bed7471e775f3cf1ac0eda
1 //===-- Utility class to test different flavors of ilogb --------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
12 #include "hdr/math_macros.h"
13 #include "src/__support/CPP/limits.h" // INT_MAX
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "src/__support/FPUtil/ManipulationFunctions.h"
16 #include "test/UnitTest/FEnvSafeTest.h"
17 #include "test/UnitTest/Test.h"
19 using LIBC_NAMESPACE::Sign;
21 class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
22 public:
23 template <typename T> struct ILogbFunc {
24 typedef int (*Func)(T);
27 template <typename T>
28 void test_special_numbers(typename ILogbFunc<T>::Func func) {
29 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
31 EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::POS).get_val()));
32 EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::NEG).get_val()));
33 EXPECT_EQ(FP_ILOGBNAN, func(FPBits::quiet_nan().get_val()));
34 EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::POS).get_val()));
35 EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::NEG).get_val()));
38 template <typename T>
39 void test_powers_of_two(typename ILogbFunc<T>::Func func) {
40 EXPECT_EQ(0, func(T(1.0)));
41 EXPECT_EQ(0, func(T(-1.0)));
43 EXPECT_EQ(1, func(T(2.0)));
44 EXPECT_EQ(1, func(T(-2.0)));
46 EXPECT_EQ(2, func(T(4.0)));
47 EXPECT_EQ(2, func(T(-4.0)));
49 EXPECT_EQ(3, func(T(8.0)));
50 EXPECT_EQ(3, func(-8.0));
52 EXPECT_EQ(4, func(16.0));
53 EXPECT_EQ(4, func(-16.0));
55 EXPECT_EQ(5, func(32.0));
56 EXPECT_EQ(5, func(-32.0));
59 template <typename T>
60 void test_some_integers(typename ILogbFunc<T>::Func func) {
61 EXPECT_EQ(1, func(T(3.0)));
62 EXPECT_EQ(1, func(T(-3.0)));
64 EXPECT_EQ(2, func(T(7.0)));
65 EXPECT_EQ(2, func(T(-7.0)));
67 EXPECT_EQ(3, func(T(10.0)));
68 EXPECT_EQ(3, func(T(-10.0)));
70 EXPECT_EQ(4, func(T(31.0)));
71 EXPECT_EQ(4, func(-31.0));
73 EXPECT_EQ(5, func(55.0));
74 EXPECT_EQ(5, func(-55.0));
77 template <typename T>
78 void test_subnormal_range(typename ILogbFunc<T>::Func func) {
79 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
80 using StorageType = typename FPBits::StorageType;
81 constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval();
82 constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval();
83 constexpr StorageType COUNT = 10'001;
84 constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
85 for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) {
86 T x = FPBits(v).get_val();
87 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0)
88 continue;
90 int exponent;
91 LIBC_NAMESPACE::fputil::frexp(x, exponent);
92 ASSERT_EQ(exponent, func(x) + 1);
96 template <typename T>
97 void test_normal_range(typename ILogbFunc<T>::Func func) {
98 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
99 using StorageType = typename FPBits::StorageType;
100 constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
101 constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
102 constexpr StorageType COUNT = 10'001;
103 constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
104 for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) {
105 T x = FPBits(v).get_val();
106 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0)
107 continue;
109 int exponent;
110 LIBC_NAMESPACE::fputil::frexp(x, exponent);
111 ASSERT_EQ(exponent, func(x) + 1);
116 #endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H