|
| 1 | +//===-- Utility class to test fxbits -----------------*- 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 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "test/UnitTest/Test.h" |
| 10 | +#include "src/__support/fixed_point/fx_rep.h" |
| 11 | + |
| 12 | +template <typename T, typename XType> class FxbitsTest : public LIBC_NAMESPACE::testing::Test { |
| 13 | + using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>; |
| 14 | + static constexpr T zero = FXRep::ZERO(); |
| 15 | + static constexpr T min = FXRep::MIN(); |
| 16 | + static constexpr T max = FXRep::MAX(); |
| 17 | + static constexpr T half = static_cast<T>(0.5); |
| 18 | + static constexpr T neg_half = static_cast<T>(-0.5); |
| 19 | + static constexpr T one = |
| 20 | + (FXRep::INTEGRAL_LEN > 0) ? static_cast<T>(1) : FXRep::MAX(); |
| 21 | + static constexpr T neg_one = static_cast<T>(-1); |
| 22 | + static constexpr T eps = FXRep::EPS(); |
| 23 | + |
| 24 | +public: |
| 25 | + typedef T (*FxbitsFunc)(XType); |
| 26 | + |
| 27 | + void testSpecialNumbers(FxbitsFunc func) { |
| 28 | + EXPECT_EQ(zero, func(0)); |
| 29 | + EXPECT_EQ(half, func((XType) (0b1 << (FXRep::FRACTION_LEN - 1)))); // 0.1000...b |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | + |
| 34 | +#define LIST_FXBITS_TEST(T, XType, func) \ |
| 35 | + using LlvmLibcFxbitsTest = FxbitsTest<T, XType>; \ |
| 36 | + TEST_F(LlvmLibcFxbitsTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
| 37 | + static_assert(true, "Require semicolon.") |
0 commit comments