|
| 1 | +//===-- Utility class to test fxdivi functions ------------------*- 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 "src/__support/CPP/type_traits.h" |
| 10 | +#include "src/__support/fixed_point/fx_bits.h" |
| 11 | +#include "src/__support/fixed_point/fx_rep.h" |
| 12 | +#include "test/UnitTest/Test.h" |
| 13 | + |
| 14 | +template <typename XType> XType get_epsilon() { |
| 15 | + // TODO: raise error |
| 16 | + return 0; |
| 17 | +} |
| 18 | +template <> fract get_epsilon() { return FRACT_EPSILON; } |
| 19 | +template <> unsigned fract get_epsilon() { return UFRACT_EPSILON; } |
| 20 | +template <> long fract get_epsilon() { return LFRACT_EPSILON; } |
| 21 | + |
| 22 | +template <typename XType> |
| 23 | +class DivITest : public LIBC_NAMESPACE::testing::Test { |
| 24 | + using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<XType>; |
| 25 | + using FXBits = LIBC_NAMESPACE::fixed_point::FXBits<XType>; |
| 26 | + |
| 27 | +public: |
| 28 | + typedef XType (*DivIFunc)(int, int); |
| 29 | + |
| 30 | + void testBasic(DivIFunc func) { |
| 31 | + XType epsilon = get_epsilon<XType>(); |
| 32 | + EXPECT_LT((func(2, 3) - 0.666666r), epsilon); |
| 33 | + EXPECT_LT((func(3, 4) - 0.75r), epsilon); |
| 34 | + EXPECT_LT((func(1043, 2764) - 0.37735r), epsilon); |
| 35 | + EXPECT_LT((func(60000, 720293) - 0.083299r), epsilon); |
| 36 | + |
| 37 | + EXPECT_EQ(func(128, 256), 0.5r); |
| 38 | + EXPECT_EQ(func(1, 2), 0.5r); |
| 39 | + EXPECT_EQ(func(1, 4), 0.25r); |
| 40 | + EXPECT_EQ(func(1, 8), 0.125r); |
| 41 | + EXPECT_EQ(func(1, 16), 0.0625r); |
| 42 | + |
| 43 | + EXPECT_EQ(func(-1, 2), -0.5r); |
| 44 | + EXPECT_EQ(func(1, -4), -0.25r); |
| 45 | + EXPECT_EQ(func(-1, 8), -0.125r); |
| 46 | + EXPECT_EQ(func(1, -16), -0.0625r); |
| 47 | + } |
| 48 | + |
| 49 | + void testSpecial(DivIFunc func) { |
| 50 | + EXPECT_EQ(func(0,10), 0.r); |
| 51 | + EXPECT_DEATH([func] { func(10, 0); }, WITH_SIGNAL(-1)); |
| 52 | + EXPECT_EQ(func(-32768,32768), FRACT_MIN); |
| 53 | + EXPECT_EQ(func(32767,32768), FRACT_MAX); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +#define LIST_DIVI_TESTS(Name, XType, func) \ |
| 58 | + using LlvmLibc##Name##diviTest = DivITest<XType>; \ |
| 59 | + TEST_F(LlvmLibc##Name##diviTest, Basic) { testBasic(&func); } \ |
| 60 | + TEST_F(LlvmLibc##Name##diviTest, Special) { testSpecial(&func); } \ |
| 61 | + static_assert(true, "Require semicolon.") |
0 commit comments