1 | //===-- Utility class to test trunc[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_TRUNCTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_TRUNCTEST_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 TruncTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | |
21 | DECLARE_SPECIAL_CONSTANTS(T) |
22 | |
23 | public: |
24 | typedef T (*TruncFunc)(T); |
25 | |
26 | void testSpecialNumbers(TruncFunc 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(TruncFunc 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(TruncFunc 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(0.0), func(T(0.715))); |
51 | EXPECT_FP_EQ(T(-0.0), func(T(-0.715))); |
52 | EXPECT_FP_EQ(T(1.0), func(T(1.3))); |
53 | EXPECT_FP_EQ(T(-1.0), func(T(-1.3))); |
54 | EXPECT_FP_EQ(T(1.0), func(T(1.5))); |
55 | EXPECT_FP_EQ(T(-1.0), func(T(-1.5))); |
56 | EXPECT_FP_EQ(T(1.0), func(T(1.75))); |
57 | EXPECT_FP_EQ(T(-1.0), func(T(-1.75))); |
58 | EXPECT_FP_EQ(T(10.0), func(T(10.32))); |
59 | EXPECT_FP_EQ(T(-10.0), func(T(-10.32))); |
60 | EXPECT_FP_EQ(T(10.0), func(T(10.65))); |
61 | EXPECT_FP_EQ(T(-10.0), func(T(-10.65))); |
62 | EXPECT_FP_EQ(T(1234.0), func(T(1234.38))); |
63 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38))); |
64 | EXPECT_FP_EQ(T(1234.0), func(T(1234.96))); |
65 | EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96))); |
66 | } |
67 | }; |
68 | |
69 | #define LIST_TRUNC_TESTS(T, func) \ |
70 | using LlvmLibcTruncTest = TruncTest<T>; \ |
71 | TEST_F(LlvmLibcTruncTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
72 | TEST_F(LlvmLibcTruncTest, RoundedNubmers) { testRoundedNumbers(&func); } \ |
73 | TEST_F(LlvmLibcTruncTest, Fractions) { testFractions(&func); } |
74 | |
75 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_TRUNCTEST_H |
76 | |