Skip to content

Commit

Permalink
BUG: Fix NumberToStringGTest failures by adding missing #include <cmath>
Browse files Browse the repository at this point in the history
Addressed `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"

It appears that without `#include <cmath>`, the wrong `pow` overload may be called.

Also explicitly declared the local `power_of_ten` variables as `TValue` and
prevented a narrowing conversion from those `std::pow` calls, just to be sure.
  • Loading branch information
N-Dekker committed Nov 17, 2022
1 parent 55dad2b commit 2031457
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Modules/Core/Common/test/itkNumberToStringGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// First include the header file to be tested:
#include "itkNumberToString.h"
#include <gtest/gtest.h>
#include <cmath> // For std::pow.

namespace
{
Expand Down Expand Up @@ -81,7 +82,7 @@ Test_decimal_notation_supports_up_to_twentyone_digits()

for (int8_t exponent{ 20 }; exponent > 0; --exponent)
{
const auto power_of_ten = std::pow(TValue{ 10 }, static_cast<TValue>(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'));
Expand All @@ -90,7 +91,7 @@ Test_decimal_notation_supports_up_to_twentyone_digits()

for (int8_t exponent{ -6 }; exponent < 0; ++exponent)
{
const auto power_of_ten = std::pow(TValue{ 10 }, static_cast<TValue>(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');
Expand Down

0 comments on commit 2031457

Please sign in to comment.