-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
23: Breaks up nearest tests to not be singular test
Tests modified: - floor_test.cpp - trunc_test.cpp Both adopts parameterized tests. These help reduce the expect calls and code duplication, also making it easier to add additional test cases in the future. NaN tests are still inside of the original TEST function. Since the tests seemed to be directly comparing the function under test with the standard library equivalent, adjusted the `std::signbit` and `std::isnan` comparisons to directly compare equality in the NaN tests.
- Loading branch information
Showing
2 changed files
with
230 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,82 @@ | ||
/* | ||
* Copyright (c) 2024-Present Ian Pike | ||
* Copyright (c) 2024-Present ccmath contributors | ||
* | ||
* This library is provided under the MIT License. | ||
* See LICENSE for more information. | ||
*/ | ||
* Copyright (c) 2024-Present Ian Pike | ||
* Copyright (c) 2024-Present ccmath contributors | ||
* | ||
* This library is provided under the MIT License. | ||
* See LICENSE for more information. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "ccmath/ccmath.hpp" | ||
#include <cmath> | ||
#include <limits> | ||
#include "ccmath/ccmath.hpp" | ||
|
||
TEST(CcmathNearestTests, Floor) | ||
namespace | ||
{ | ||
EXPECT_EQ(ccm::floor(1.0), std::floor(1.0)); | ||
EXPECT_EQ(ccm::floor(1.0000000000000000000000000000000000000000000001), std::floor(1.0000000000000000000000000000000000000000000001)); | ||
EXPECT_EQ(ccm::floor(1.5), std::floor(1.5)); | ||
EXPECT_EQ(ccm::floor(1.9), std::floor(1.9)); | ||
EXPECT_EQ(ccm::floor(-1.0), std::floor(-1.0)); | ||
EXPECT_EQ(ccm::floor(-1.5), std::floor(-1.5)); | ||
EXPECT_EQ(ccm::floor(-1.9), std::floor(-1.9)); | ||
EXPECT_EQ(ccm::floor(std::numeric_limits<double>::infinity()), std::floor(std::numeric_limits<double>::infinity())); | ||
EXPECT_EQ(ccm::floor(-std::numeric_limits<double>::infinity()), std::floor(-std::numeric_limits<double>::infinity())); | ||
|
||
bool isCcmPositiveNanPositive = (std::signbit(ccm::floor(std::numeric_limits<double>::quiet_NaN())) == false && std::isnan(ccm::floor(std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT | ||
bool isStdPositiveNanPositive = (std::signbit(std::floor(std::numeric_limits<double>::quiet_NaN())) == false && std::isnan(std::floor(std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT | ||
EXPECT_EQ(isCcmPositiveNanPositive, isStdPositiveNanPositive); | ||
|
||
bool isCcmNegativeNanNegative = (std::signbit(ccm::floor(-std::numeric_limits<double>::quiet_NaN())) == true && std::isnan(ccm::floor(-std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT | ||
bool isStdNegativeNanNegative = (std::signbit(std::floor(-std::numeric_limits<double>::quiet_NaN())) == true && std::isnan(std::floor(-std::numeric_limits<double>::quiet_NaN())) == true); // NOLINT | ||
EXPECT_EQ(isCcmNegativeNanNegative, isStdNegativeNanNegative); | ||
|
||
bool testThatCcmFloorThatNanReturnsNan = (std::isnan(ccm::floor(std::numeric_limits<double>::quiet_NaN()))); | ||
bool testThatCcmFloorThatNegativeNanReturnsNegativeNan = (std::isnan(ccm::floor(-std::numeric_limits<double>::quiet_NaN()))); | ||
EXPECT_EQ(testThatCcmFloorThatNanReturnsNan, testThatCcmFloorThatNegativeNanReturnsNegativeNan); | ||
|
||
EXPECT_EQ(ccm::floor(0.0), std::floor(0.0)); | ||
EXPECT_EQ(ccm::floor(-0.0), std::floor(-0.0)); | ||
EXPECT_EQ(ccm::floor(0.5), std::floor(0.5)); | ||
EXPECT_EQ(ccm::floor(-0.5), std::floor(-0.5)); | ||
EXPECT_EQ(ccm::floor(0.9), std::floor(0.9)); | ||
EXPECT_EQ(ccm::floor(-0.9), std::floor(-0.9)); | ||
EXPECT_EQ(ccm::floor(0.9999999999999999), std::floor(0.9999999999999999)); | ||
EXPECT_EQ(ccm::floor(-0.9999999999999999), std::floor(-0.9999999999999999)); | ||
EXPECT_EQ(ccm::floor(1.0000000000000001), std::floor(1.0000000000000001)); | ||
EXPECT_EQ(ccm::floor(-1.0000000000000001), std::floor(-1.0000000000000001)); | ||
|
||
using testing::TestWithParam; | ||
using testing::ValuesIn; | ||
|
||
struct FloorTestParams | ||
{ | ||
double input{}; | ||
double expected{}; | ||
}; | ||
|
||
const std::vector<FloorTestParams> kFloorTestsParams{ | ||
// Basic double values | ||
{1.0, std::floor(1.0)}, | ||
{1.5, std::floor(1.5)}, | ||
{1.9, std::floor(1.9)}, | ||
{-1.0, std::floor(-1.0)}, | ||
{-1.5, std::floor(-1.5)}, | ||
{-1.9, std::floor(-1.9)}, | ||
|
||
// Zero values | ||
{0.0, std::floor(0.0)}, | ||
{-0.0, std::floor(-0.0)}, | ||
|
||
// Fractional values | ||
{0.5, std::floor(0.5)}, | ||
{-0.5, std::floor(-0.5)}, | ||
{0.9, std::floor(0.9)}, | ||
{-0.9, std::floor(-0.9)}, | ||
|
||
// Very close to whole numbers | ||
{0.9999999999999999, std::floor(0.9999999999999999)}, | ||
{-0.9999999999999999, std::floor(-0.9999999999999999)}, | ||
{1.0000000000000001, std::floor(1.0000000000000001)}, | ||
{-1.0000000000000001, std::floor(-1.0000000000000001)}, | ||
{1.0000000000000000000000000000000000000000000001, std::floor(1.0000000000000000000000000000000000000000000001)}, | ||
}; | ||
|
||
} // namespace | ||
|
||
class CcmathFloorTests : public TestWithParam<FloorTestParams> | ||
{ | ||
}; | ||
|
||
INSTANTIATE_TEST_SUITE_P(FloorTests, CcmathFloorTests, ValuesIn(kFloorTestsParams)); | ||
|
||
TEST_P(CcmathFloorTests, Floor) | ||
{ | ||
const auto param{GetParam()}; | ||
const auto actual{ccm::floor(param.input)}; | ||
EXPECT_EQ(actual, param.expected) << "ccm::floor(" << param.input << ") expected to equal " << param.expected << ". Instead got " << actual << "."; | ||
} | ||
|
||
TEST(CcmathNearestTests, CcmFloorTestNanValues) | ||
{ | ||
// Check if ccm::floor and std::floor return NaN for positive NaN | ||
EXPECT_EQ(std::isnan(ccm::floor(std::numeric_limits<double>::quiet_NaN())), std::isnan(std::floor(std::numeric_limits<double>::quiet_NaN()))); | ||
|
||
// Check if ccm::floor and std::floor have the same sign bit for positive NaN | ||
EXPECT_EQ(std::signbit(ccm::floor(std::numeric_limits<double>::quiet_NaN())), std::signbit(std::floor(std::numeric_limits<double>::quiet_NaN()))); | ||
|
||
// Check if ccm::floor and std::floor return NaN for negative NaN | ||
EXPECT_EQ(std::isnan(ccm::floor(-std::numeric_limits<double>::quiet_NaN())), std::isnan(std::floor(-std::numeric_limits<double>::quiet_NaN()))); | ||
|
||
// Check if ccm::floor and std::floor have the same sign bit for negative NaN | ||
EXPECT_EQ(std::signbit(ccm::floor(-std::numeric_limits<double>::quiet_NaN())), std::signbit(std::floor(-std::numeric_limits<double>::quiet_NaN()))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters