1//===-- Utility class to test fixed-point round -----------------*- 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
11#include "src/__support/fixed_point/fx_rep.h"
12
13template <typename T> class RoundTest : public LIBC_NAMESPACE::testing::Test {
14
15 using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
16 static constexpr T zero = FXRep::ZERO();
17 static constexpr T min = FXRep::MIN();
18 static constexpr T max = FXRep::MAX();
19 static constexpr T half = static_cast<T>(0.5);
20 static constexpr T neg_half = static_cast<T>(-0.5);
21 static constexpr T one =
22 (FXRep::INTEGRAL_LEN > 0) ? static_cast<T>(1) : FXRep::MAX();
23 static constexpr T neg_one = static_cast<T>(-1);
24 static constexpr T eps = FXRep::EPS();
25
26public:
27 typedef T (*RoundFunc)(T, int);
28
29 void testSpecialNumbers(RoundFunc func) {
30 EXPECT_EQ(zero, func(zero, FXRep::FRACTION_LEN - 5));
31 EXPECT_EQ(min, func(min, 0));
32 EXPECT_EQ(max, func(max, FXRep::FRACTION_LEN));
33
34 EXPECT_EQ(one, func(half, 0));
35 EXPECT_EQ(half, func(half, 1));
36 EXPECT_EQ(half, func(half, FXRep::FRACTION_LEN));
37 EXPECT_EQ(one, func(half + eps, 0));
38 EXPECT_EQ(half, func(half + eps, 1));
39 EXPECT_EQ(half, func(half + eps, 2));
40 EXPECT_EQ(zero, func(half - eps, 0));
41 EXPECT_EQ(half, func(half - eps, 1));
42 EXPECT_EQ(half, func(half - eps, 2));
43 EXPECT_EQ(eps, func(eps, FXRep::FRACTION_LEN + 10));
44 EXPECT_EQ(eps << 1, func(eps, FXRep::FRACTION_LEN - 1));
45 EXPECT_EQ(zero, func(eps, FXRep::FRACTION_LEN - 2));
46
47 if constexpr (FXRep::SIGN_LEN) {
48 EXPECT_EQ(zero, func(neg_half, 0));
49 EXPECT_EQ(neg_half, func(neg_half, 1));
50 EXPECT_EQ(neg_half, func(neg_half, 3));
51 EXPECT_EQ(zero, func(neg_half + eps, 0));
52 EXPECT_EQ(neg_half, func(neg_half + eps, 1));
53 EXPECT_EQ(neg_half, func(neg_half + eps, 2));
54 EXPECT_EQ(neg_one, func(neg_half - eps, 0));
55 EXPECT_EQ(neg_half, func(neg_half - eps, 1));
56 EXPECT_EQ(neg_half, func(neg_half - eps, 2));
57 EXPECT_EQ(-eps, func(-eps, FXRep::FRACTION_LEN + 10));
58 }
59 }
60};
61
62#define LIST_ROUND_TESTS(T, func) \
63 using LlvmLibcRoundTest = RoundTest<T>; \
64 TEST_F(LlvmLibcRoundTest, SpecialNumbers) { testSpecialNumbers(&func); } \
65 static_assert(true, "Require semicolon.")
66

source code of libc/test/src/stdfix/RoundTest.h