Skip to content

Commit

Permalink
ENH: Mention the floating point type on a NumberToString expect failure
Browse files Browse the repository at this point in the history
Aims to ease the analysis of NumberToStringGTest test failures, like the
`NumberToString.DecimalNotationUpTo21Digits` failures reported by yurivict at
#3739 saying:

    /disk-samsung/freebsd-ports/science/InsightToolkit/work/ITK-5.2.1/Modules/Core/Common/test/itkNumberToStringGTest.cxx:87: Failure
    Expected equality of these values:
      numberToString(power_of_ten)
        Which is: "100000010000"
      '1' + std::string(exponent, '0')
        Which is: "100000000000"
  • Loading branch information
N-Dekker authored and dzenanz committed Nov 29, 2022
1 parent d5beb7a commit 43d3e84
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions Modules/Core/Common/test/itkNumberToStringGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
namespace
{

template <typename>
constexpr const char * floatingPointTypeName = "unspecified type";
template <>
constexpr const char * floatingPointTypeName<float> = "float";
template <>
constexpr const char * floatingPointTypeName<double> = "double";


template <typename TValue>
void
Test_all_digits()
Expand All @@ -43,10 +51,11 @@ Test_non_finite_special_floating_point_values()
{
using NumericLimitsType = std::numeric_limits<TValue>;
const itk::NumberToString<TValue> numberToString{};
const auto message = std::string("Floating point type: ") + floatingPointTypeName<TValue>;

EXPECT_EQ(numberToString(NumericLimitsType::quiet_NaN()), "NaN");
EXPECT_EQ(numberToString(NumericLimitsType::infinity()), "Infinity");
EXPECT_EQ(numberToString(-NumericLimitsType::infinity()), "-Infinity");
EXPECT_EQ(numberToString(NumericLimitsType::quiet_NaN()), "NaN") << message;
EXPECT_EQ(numberToString(NumericLimitsType::infinity()), "Infinity") << message;
EXPECT_EQ(numberToString(-NumericLimitsType::infinity()), "-Infinity") << message;
}


Expand All @@ -56,6 +65,7 @@ Test_round_trip_of_finite_numeric_limits()
{
using NumericLimitsType = std::numeric_limits<TValue>;
const itk::NumberToString<TValue> numberToString{};
const auto message = std::string("Floating point type: ") + floatingPointTypeName<TValue>;

for (const TValue expectedValue : { NumericLimitsType::lowest(),
NumericLimitsType::epsilon(),
Expand All @@ -65,11 +75,11 @@ Test_round_trip_of_finite_numeric_limits()
NumericLimitsType::max() })
{
std::istringstream inputStream{ numberToString(expectedValue) };
EXPECT_FALSE(inputStream.eof());
EXPECT_FALSE(inputStream.eof()) << message;
TValue actualValue;
inputStream >> actualValue;
EXPECT_TRUE(inputStream.eof());
EXPECT_EQ(actualValue, expectedValue);
EXPECT_TRUE(inputStream.eof()) << message;
EXPECT_EQ(actualValue, expectedValue) << message;
}
}

Expand All @@ -79,23 +89,24 @@ void
Test_decimal_notation_supports_up_to_twentyone_digits()
{
const itk::NumberToString<TValue> numberToString{};
const auto message = std::string("Floating point type: ") + floatingPointTypeName<TValue>;

for (int8_t exponent{ 20 }; exponent > 0; --exponent)
{
const TValue power_of_ten{ std::pow(TValue{ 10 }, static_cast<TValue>(exponent)) };

// Test +/- 10 ^ exponent
EXPECT_EQ(numberToString(power_of_ten), '1' + std::string(exponent, '0'));
EXPECT_EQ(numberToString(-power_of_ten), "-1" + std::string(exponent, '0'));
EXPECT_EQ(numberToString(power_of_ten), '1' + std::string(exponent, '0')) << message;
EXPECT_EQ(numberToString(-power_of_ten), "-1" + std::string(exponent, '0')) << message;
}

for (int8_t exponent{ -6 }; exponent < 0; ++exponent)
{
const TValue power_of_ten{ std::pow(TValue{ 10 }, static_cast<TValue>(exponent)) };

// Test +/- 10 ^ exponent
EXPECT_EQ(numberToString(power_of_ten), "0." + std::string(-1 - exponent, '0') + '1');
EXPECT_EQ(numberToString(-power_of_ten), "-0." + std::string(-1 - exponent, '0') + '1');
EXPECT_EQ(numberToString(power_of_ten), "0." + std::string(-1 - exponent, '0') + '1') << message;
EXPECT_EQ(numberToString(-power_of_ten), "-0." + std::string(-1 - exponent, '0') + '1') << message;
}
}

Expand Down

0 comments on commit 43d3e84

Please sign in to comment.