1 | //===-- Utility class to test roundeven[f|l] --------------------*- 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 | #ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDEVENTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDEVENTEST_H |
11 | |
12 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/Test.h" |
15 | |
16 | #include "hdr/math_macros.h" |
17 | |
18 | template <typename T> |
19 | class RoundEvenTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | |
21 | DECLARE_SPECIAL_CONSTANTS(T) |
22 | |
23 | public: |
24 | typedef T (*RoundEvenFunc)(T); |
25 | |
26 | void testSpecialNumbers(RoundEvenFunc func) { |
27 | EXPECT_FP_EQ(zero, func(zero)); |
28 | EXPECT_FP_EQ(neg_zero, func(neg_zero)); |
29 | |
30 | EXPECT_FP_EQ(inf, func(inf)); |
31 | EXPECT_FP_EQ(neg_inf, func(neg_inf)); |
32 | |
33 | EXPECT_FP_EQ(aNaN, func(aNaN)); |
34 | } |
35 | |
36 | void testRoundedNumbers(RoundEvenFunc func) { |
37 | EXPECT_FP_EQ(T(1.0), func(T(1.0))); |
38 | EXPECT_FP_EQ(T(-1.0), func(T(-1.0))); |
39 | EXPECT_FP_EQ(T(10.0), func(T(10.0))); |
40 | EXPECT_FP_EQ(T(-10.0), func(T(-10.0))); |
41 | EXPECT_FP_EQ(T(1234.0), func(T(1234.0))); |
42 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0))); |
43 | } |
44 | |
45 | void testFractions(RoundEvenFunc func) { |
46 | EXPECT_FP_EQ(T(0.0), func(T(0.5))); |
47 | EXPECT_FP_EQ(T(-0.0), func(T(-0.5))); |
48 | EXPECT_FP_EQ(T(0.0), func(T(0.115))); |
49 | EXPECT_FP_EQ(T(-0.0), func(T(-0.115))); |
50 | EXPECT_FP_EQ(T(1.0), func(T(0.715))); |
51 | EXPECT_FP_EQ(T(-1.0), func(T(-0.715))); |
52 | EXPECT_FP_EQ(T(2.0), func(T(1.5))); |
53 | EXPECT_FP_EQ(T(-2.0), func(T(-1.5))); |
54 | EXPECT_FP_EQ(T(2.0), func(T(1.75))); |
55 | EXPECT_FP_EQ(T(-2.0), func(T(-1.75))); |
56 | EXPECT_FP_EQ(T(10.0), func(T(10.50))); |
57 | EXPECT_FP_EQ(T(-10.0), func(T(-10.50))); |
58 | EXPECT_FP_EQ(T(11.0), func(T(10.65))); |
59 | EXPECT_FP_EQ(T(-11.0), func(T(-10.65))); |
60 | EXPECT_FP_EQ(T(1234.0), func(T(1234.50))); |
61 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.50))); |
62 | EXPECT_FP_EQ(T(1236.0), func(T(1235.50))); |
63 | EXPECT_FP_EQ(T(-1236.0), func(T(-1235.50))); |
64 | } |
65 | }; |
66 | |
67 | #define LIST_ROUNDEVEN_TESTS(T, func) \ |
68 | using LlvmLibcRoundEvenTest = RoundEvenTest<T>; \ |
69 | TEST_F(LlvmLibcRoundEvenTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
70 | TEST_F(LlvmLibcRoundEvenTest, RoundedNubmers) { testRoundedNumbers(&func); } \ |
71 | TEST_F(LlvmLibcRoundEvenTest, Fractions) { testFractions(&func); } |
72 | |
73 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDEVENTEST_H |
74 | |