|
13 | 13 | #include <cmath>
|
14 | 14 | #include <limits>
|
15 | 15 |
|
| 16 | +namespace |
| 17 | +{ |
| 18 | + |
| 19 | + using testing::TestWithParam; |
| 20 | + using testing::ValuesIn; |
| 21 | + |
| 22 | + template <typename T> |
| 23 | + struct TruncTestParams |
| 24 | + { |
| 25 | + T input{}; |
| 26 | + T expected{}; |
| 27 | + }; |
| 28 | + |
| 29 | + const std::vector<TruncTestParams<double>> kTruncDoubleTestsParams{ |
| 30 | + |
| 31 | + // Basic double values |
| 32 | + {1.0, std::trunc(1.0)}, |
| 33 | + {1.5, std::trunc(1.5)}, |
| 34 | + {1.9, std::trunc(1.9)}, |
| 35 | + {-1.0, std::trunc(-1.0)}, |
| 36 | + {-1.5, std::trunc(-1.5)}, |
| 37 | + {-1.9, std::trunc(-1.9)}, |
| 38 | + |
| 39 | + // Infinity values |
| 40 | + {std::numeric_limits<double>::infinity(), std::trunc(std::numeric_limits<double>::infinity())}, |
| 41 | + {-std::numeric_limits<double>::infinity(), std::trunc(-std::numeric_limits<double>::infinity())}, |
| 42 | + |
| 43 | + // Zero values |
| 44 | + {0.0, std::trunc(0.0)}, |
| 45 | + {-0.0, std::trunc(-0.0)}, |
| 46 | + |
| 47 | + // Fractional values |
| 48 | + {0.5, std::trunc(0.5)}, |
| 49 | + {-0.5, std::trunc(-0.5)}, |
| 50 | + {0.9, std::trunc(0.9)}, |
| 51 | + {-0.9, std::trunc(-0.9)}, |
| 52 | + |
| 53 | + // Very close to whole numbers |
| 54 | + {0.9999999999999999, std::trunc(0.9999999999999999)}, |
| 55 | + {-0.9999999999999999, std::trunc(-0.9999999999999999)}, |
| 56 | + {1.0000000000000001, std::trunc(1.0000000000000001)}, |
| 57 | + {-1.0000000000000001, std::trunc(-1.0000000000000001)}}; |
| 58 | + |
| 59 | + const std::vector<TruncTestParams<float>> kTruncFloatTestParams{ |
| 60 | + |
| 61 | + // Basic float values |
| 62 | + {1.0f, std::truncf(1.0f)}, |
| 63 | + {1.5f, std::truncf(1.5f)}, |
| 64 | + {1.9f, std::truncf(1.9f)}, |
| 65 | + {-1.0f, std::truncf(-1.0f)}, |
| 66 | + {-1.5f, std::truncf(-1.5f)}, |
| 67 | + {-1.9f, std::truncf(-1.9f)}, |
| 68 | + |
| 69 | + // Infinity values |
| 70 | + {std::numeric_limits<float>::infinity(), std::truncf(std::numeric_limits<float>::infinity())}, |
| 71 | + {-std::numeric_limits<float>::infinity(), std::truncf(-std::numeric_limits<float>::infinity())}, |
| 72 | + |
| 73 | + // Zero values |
| 74 | + {0.0f, std::truncf(0.0f)}, |
| 75 | + {-0.0f, std::truncf(-0.0f)}, |
| 76 | + |
| 77 | + // Fractional values |
| 78 | + {0.5f, std::truncf(0.5f)}, |
| 79 | + {-0.5f, std::truncf(-0.5f)}, |
| 80 | + {0.9f, std::truncf(0.9f)}, |
| 81 | + {-0.9f, std::truncf(-0.9f)}, |
| 82 | + |
| 83 | + // Very close to whole numbers |
| 84 | + {0.9999999999999999f, std::truncf(0.9999999999999999f)}, |
| 85 | + {-0.9999999999999999f, std::truncf(-0.9999999999999999f)}, |
| 86 | + |
| 87 | + {30.508474576271183309F, std::truncf(30.508474576271183309F)}, |
| 88 | + }; |
| 89 | + |
| 90 | + const std::vector<TruncTestParams<long double>> kTruncLongDoubleTestParams{ |
| 91 | + |
| 92 | + // Basic long double values |
| 93 | + {1.0L, std::truncl(1.0L)}, |
| 94 | + {1.5L, std::truncl(1.5L)}, |
| 95 | + {1.9L, std::truncl(1.9L)}, |
| 96 | + {-1.0L, std::truncl(-1.0L)}, |
| 97 | + {-1.5L, std::truncl(-1.5L)}, |
| 98 | + {-1.9L, std::truncl(-1.9L)}, |
| 99 | + |
| 100 | + // Infinity values |
| 101 | + {std::numeric_limits<long double>::infinity(), std::truncl(std::numeric_limits<long double>::infinity())}, |
| 102 | + {-std::numeric_limits<long double>::infinity(), std::truncl(-std::numeric_limits<long double>::infinity())}, |
| 103 | + |
| 104 | + // Fractional values |
| 105 | + {0.0L, std::truncl(0.0L)}, |
| 106 | + {-0.0L, std::truncl(-0.0L)}, |
| 107 | + {0.5L, std::truncl(0.5L)}, |
| 108 | + {-0.5L, std::truncl(-0.5L)}, |
| 109 | + {0.9L, std::truncl(0.9L)}, |
| 110 | + {-0.9L, std::truncl(-0.9L)}, |
| 111 | + |
| 112 | + // Very close to whole numbers |
| 113 | + {0.9999999999999999L, std::truncl(0.9999999999999999L)}, |
| 114 | + {-0.9999999999999999L, std::truncl(-0.9999999999999999L)}, |
| 115 | + }; |
| 116 | + |
| 117 | +} // namespace |
| 118 | + |
16 | 119 | // TODO: Find a way to test all of the different ways the function may behave internally to work based on the provided compiler.
|
17 | 120 |
|
18 |
| -TEST(CcmathNearestTests, Trunc) |
| 121 | +class CcmathTruncDoubleTests : public TestWithParam<TruncTestParams<double>> |
| 122 | +{ |
| 123 | +}; |
| 124 | +class CcmathTruncFloatTests : public TestWithParam<TruncTestParams<float>> |
| 125 | +{ |
| 126 | +}; |
| 127 | +class CcmathTruncLongDoubleTests : public TestWithParam<TruncTestParams<long double>> |
| 128 | +{ |
| 129 | +}; |
| 130 | + |
| 131 | +INSTANTIATE_TEST_SUITE_P(TruncDoubleTests, CcmathTruncDoubleTests, ValuesIn(kTruncDoubleTestsParams)); |
| 132 | +INSTANTIATE_TEST_SUITE_P(TruncFloatTests, CcmathTruncFloatTests, ValuesIn(kTruncFloatTestParams)); |
| 133 | +INSTANTIATE_TEST_SUITE_P(TruncLongDoubleTests, CcmathTruncLongDoubleTests, ValuesIn(kTruncLongDoubleTestParams)); |
| 134 | + |
| 135 | +TEST_P(CcmathTruncDoubleTests, Trunc) |
| 136 | +{ |
| 137 | + const auto param{GetParam()}; |
| 138 | + const auto actual{ccm::trunc(param.input)}; |
| 139 | + EXPECT_EQ(actual, param.expected) << "ccm::trunc(" << param.input << ") expected to equal " << param.expected << ". Instead got " << actual << "."; |
| 140 | +} |
| 141 | + |
| 142 | +TEST_P(CcmathTruncFloatTests, TruncF) |
| 143 | +{ |
| 144 | + const auto param{GetParam()}; |
| 145 | + const auto actual{ccm::truncf(param.input)}; |
| 146 | + EXPECT_EQ(actual, param.expected) << "ccm::truncf(" << param.input << ") expected to equal " << param.expected << ". Instead got " << actual << "."; |
| 147 | +} |
| 148 | + |
| 149 | +TEST_P(CcmathTruncLongDoubleTests, TruncL) |
| 150 | +{ |
| 151 | + const auto param{GetParam()}; |
| 152 | + const auto actual{ccm::truncl(param.input)}; |
| 153 | + EXPECT_EQ(actual, param.expected) << "ccm::truncl(" << param.input << ") expected to equal " << param.expected << ". Instead got " << actual << "."; |
| 154 | +} |
| 155 | + |
| 156 | +TEST(CcmathNearestTests, CcmTruncTestNanValues) |
| 157 | +{ |
| 158 | + // Check if ccm::trunc and std::trunc return NaN for positive NaN |
| 159 | + EXPECT_EQ(std::isnan(ccm::trunc(std::numeric_limits<double>::quiet_NaN())), std::isnan(std::trunc(std::numeric_limits<double>::quiet_NaN()))); |
| 160 | + |
| 161 | + // Check if ccm::trunc and std::trunc have the same sign bit for positive NaN |
| 162 | + EXPECT_EQ(std::signbit(ccm::trunc(std::numeric_limits<double>::quiet_NaN())), std::signbit(std::trunc(std::numeric_limits<double>::quiet_NaN()))); |
| 163 | + |
| 164 | + // Check if ccm::trunc and std::trunc return NaN for negative NaN |
| 165 | + EXPECT_EQ(std::isnan(ccm::trunc(-std::numeric_limits<double>::quiet_NaN())), std::isnan(std::trunc(-std::numeric_limits<double>::quiet_NaN()))); |
| 166 | + |
| 167 | + // Check if ccm::trunc and std::trunc have the same sign bit for negative NaN |
| 168 | + EXPECT_EQ(std::signbit(ccm::trunc(-std::numeric_limits<double>::quiet_NaN())), std::signbit(std::trunc(-std::numeric_limits<double>::quiet_NaN()))); |
| 169 | + |
| 170 | + // Google Test is apparently incapable of comparing NaNs or I do not know enough about gtest to find a solution. I've though personally validated that |
| 171 | + // ccm::signbit handles NaNs correctly |
| 172 | + // EXPECT_EQ(ccm::trunc(std::numeric_limits<double>::quiet_NaN()), std::trunc(std::numeric_limits<double>::quiet_NaN())); |
| 173 | + // EXPECT_EQ(ccm::trunc(-std::numeric_limits<double>::quiet_NaN()), std::trunc(-std::numeric_limits<double>::quiet_NaN())); |
| 174 | + |
| 175 | + EXPECT_TRUE(std::isnan(ccm::trunc(std::nan("")))); |
| 176 | + EXPECT_TRUE(std::isnan(ccm::trunc(-std::nan("")))); |
| 177 | +} |
| 178 | + |
| 179 | +TEST(CcmathNearestTests, CcmTruncCanBeEvaluatedAtCompileTime) |
| 180 | +{ |
| 181 | + constexpr auto trunc{ccm::trunc(1.0)}; |
| 182 | + static_assert(trunc == 1.0); |
| 183 | +} |
| 184 | + |
| 185 | +TEST(CcmathNearestTests, CcmTruncfCanBeEvaluatedAtCompileTime) |
| 186 | +{ |
| 187 | + constexpr auto truncf{ccm::truncf(1.0f)}; |
| 188 | + static_assert(truncf == 1.0f); |
| 189 | +} |
| 190 | + |
| 191 | +TEST(CcmathNearestTests, CcmTrunclCanBeEvaluatedAtCompileTime) |
19 | 192 | {
|
20 |
| - EXPECT_EQ(ccm::trunc(1.0), std::trunc(1.0)); |
21 |
| - EXPECT_EQ(ccm::trunc(1.5), std::trunc(1.5)); |
22 |
| - EXPECT_EQ(ccm::trunc(1.9), std::trunc(1.9)); |
23 |
| - EXPECT_EQ(ccm::trunc(-1.0), std::trunc(-1.0)); |
24 |
| - EXPECT_EQ(ccm::trunc(-1.5), std::trunc(-1.5)); |
25 |
| - EXPECT_EQ(ccm::trunc(-1.9), std::trunc(-1.9)); |
26 |
| - EXPECT_EQ(ccm::trunc(std::numeric_limits<double>::infinity()), std::trunc(std::numeric_limits<double>::infinity())); |
27 |
| - EXPECT_EQ(ccm::trunc(-std::numeric_limits<double>::infinity()), std::trunc(-std::numeric_limits<double>::infinity())); |
28 |
| - // Google Test is apparently incapable of comparing NaNs or I do not know enough about gtest to find a solution. I've though personally validated that ccm::signbit handles NaNs correctly |
29 |
| - //EXPECT_EQ(ccm::trunc(std::numeric_limits<double>::quiet_NaN()), std::trunc(std::numeric_limits<double>::quiet_NaN())); |
30 |
| - //EXPECT_EQ(ccm::trunc(-std::numeric_limits<double>::quiet_NaN()), std::trunc(-std::numeric_limits<double>::quiet_NaN())); |
31 |
| - |
32 |
| - // The standard when passed +NaN returns +NaN, and when passed -NaN returns -NaN |
33 |
| - bool isCcmPositiveNanPositive = (std::signbit(ccm::trunc(std::numeric_limits<double>::quiet_NaN())) == false && std::isnan(ccm::trunc(std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT |
34 |
| - bool isStdPositiveNanPositive = (std::signbit(std::trunc(std::numeric_limits<double>::quiet_NaN())) == false && std::isnan(std::trunc(std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT |
35 |
| - EXPECT_EQ(isCcmPositiveNanPositive, isStdPositiveNanPositive); |
36 |
| - |
37 |
| - |
38 |
| - bool isCcmNegativeNanNegative = (std::signbit(ccm::trunc(-std::numeric_limits<double>::quiet_NaN())) == true && std::isnan(ccm::trunc(-std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT |
39 |
| - bool isStdNegativeNanNegative = (std::signbit(std::trunc(-std::numeric_limits<double>::quiet_NaN())) == true && std::isnan(std::trunc(-std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT |
40 |
| - EXPECT_EQ(isCcmNegativeNanNegative, isStdNegativeNanNegative); |
41 |
| - |
42 |
| - |
43 |
| - EXPECT_TRUE(std::isnan(ccm::trunc(std::nan("")))); |
44 |
| - EXPECT_TRUE(std::isnan(ccm::trunc(-std::nan("")))); |
45 |
| - EXPECT_EQ(ccm::trunc(0.0), std::trunc(0.0)); |
46 |
| - EXPECT_EQ(ccm::trunc(-0.0), std::trunc(-0.0)); |
47 |
| - EXPECT_EQ(ccm::trunc(0.5), std::trunc(0.5)); |
48 |
| - EXPECT_EQ(ccm::trunc(-0.5), std::trunc(-0.5)); |
49 |
| - EXPECT_EQ(ccm::trunc(0.9), std::trunc(0.9)); |
50 |
| - EXPECT_EQ(ccm::trunc(-0.9), std::trunc(-0.9)); |
51 |
| - EXPECT_EQ(ccm::trunc(0.9999999999999999), std::trunc(0.9999999999999999)); |
52 |
| - EXPECT_EQ(ccm::trunc(-0.9999999999999999), std::trunc(-0.9999999999999999)); |
53 |
| - EXPECT_EQ(ccm::trunc(1.0000000000000001), std::trunc(1.0000000000000001)); |
54 |
| - EXPECT_EQ(ccm::trunc(-1.0000000000000001), std::trunc(-1.0000000000000001)); |
55 |
| - |
56 |
| - // Test with float using std::truncf and ccm::truncf |
57 |
| - EXPECT_EQ(ccm::truncf(1.0F), std::truncf(1.0F)); |
58 |
| - EXPECT_EQ(ccm::truncf(1.5F), std::truncf(1.5F)); |
59 |
| - EXPECT_EQ(ccm::truncf(1.9F), std::truncf(1.9F)); |
60 |
| - EXPECT_EQ(ccm::truncf(-1.0F), std::truncf(-1.0F)); |
61 |
| - EXPECT_EQ(ccm::truncf(-1.5F), std::truncf(-1.5F)); |
62 |
| - EXPECT_EQ(ccm::truncf(-1.9F), std::truncf(-1.9F)); |
63 |
| - EXPECT_EQ(ccm::truncf(std::numeric_limits<float>::infinity()), std::truncf(std::numeric_limits<float>::infinity())); |
64 |
| - EXPECT_EQ(ccm::truncf(-std::numeric_limits<float>::infinity()), std::truncf(-std::numeric_limits<float>::infinity())); |
65 |
| - EXPECT_TRUE(std::isnan(ccm::truncf(std::nanf("")))); |
66 |
| - EXPECT_TRUE(std::isnan(ccm::truncf(-std::nanf("")))); |
67 |
| - EXPECT_EQ(ccm::truncf(0.0F), std::truncf(0.0F)); |
68 |
| - EXPECT_EQ(ccm::truncf(-0.0F), std::truncf(-0.0F)); |
69 |
| - EXPECT_EQ(ccm::truncf(0.5F), std::truncf(0.5F)); |
70 |
| - EXPECT_EQ(ccm::truncf(-0.5F), std::truncf(-0.5F)); |
71 |
| - EXPECT_EQ(ccm::truncf(0.9F), std::truncf(0.9F)); |
72 |
| - EXPECT_EQ(ccm::truncf(-0.9F), std::truncf(-0.9F)); |
73 |
| - EXPECT_EQ(ccm::truncf(0.9999999999999999F), std::truncf(0.9999999999999999F)); |
74 |
| - EXPECT_EQ(ccm::truncf(-0.9999999999999999F), std::truncf(-0.9999999999999999F)); |
75 |
| - |
76 |
| - //EXPECT_FLOAT_EQ(ccm::fmod(30.508474576271183309f, 6.1016949152542370172F), std::fmod(30.508474576271183309f, 6.1016949152542370172F)); |
77 |
| - EXPECT_EQ(ccm::truncf(30.508474576271183309F), std::truncf(30.508474576271183309F)); |
78 |
| - |
79 |
| - |
80 |
| - // Test with long double using std::truncl and ccm::truncl |
81 |
| - EXPECT_EQ(ccm::truncl(1.0L), std::truncl(1.0L)); |
82 |
| - EXPECT_EQ(ccm::truncl(1.5L), std::truncl(1.5L)); |
83 |
| - EXPECT_EQ(ccm::truncl(1.9L), std::truncl(1.9L)); |
84 |
| - EXPECT_EQ(ccm::truncl(-1.0L), std::truncl(-1.0L)); |
85 |
| - EXPECT_EQ(ccm::truncl(-1.5L), std::truncl(-1.5L)); |
86 |
| - EXPECT_EQ(ccm::truncl(-1.9L), std::truncl(-1.9L)); |
87 |
| - EXPECT_EQ(ccm::truncl(std::numeric_limits<long double>::infinity()), std::truncl(std::numeric_limits<long double>::infinity())); |
88 |
| - EXPECT_EQ(ccm::truncl(-std::numeric_limits<long double>::infinity()), std::truncl(-std::numeric_limits<long double>::infinity())); |
89 |
| - EXPECT_TRUE(std::isnan(ccm::truncl(std::nanl("")))); |
90 |
| - EXPECT_TRUE(std::isnan(ccm::truncl(-std::nanl("")))); |
91 |
| - EXPECT_EQ(ccm::truncl(0.0L), std::truncl(0.0L)); |
92 |
| - EXPECT_EQ(ccm::truncl(-0.0L), std::truncl(-0.0L)); |
93 |
| - EXPECT_EQ(ccm::truncl(0.5L), std::truncl(0.5L)); |
94 |
| - EXPECT_EQ(ccm::truncl(-0.5L), std::truncl(-0.5L)); |
95 |
| - EXPECT_EQ(ccm::truncl(0.9L), std::truncl(0.9L)); |
96 |
| - EXPECT_EQ(ccm::truncl(-0.9L), std::truncl(-0.9L)); |
97 |
| - EXPECT_EQ(ccm::truncl(0.9999999999999999L), std::truncl(0.9999999999999999L)); |
98 |
| - EXPECT_EQ(ccm::truncl(-0.9999999999999999L), std::truncl(-0.9999999999999999L)); |
| 193 | + constexpr auto truncl{ccm::truncl(1.0L)}; |
| 194 | + static_assert(truncl == 1.0L); |
99 | 195 | }
|
0 commit comments