Step 2.5: Run the STNumber tests using both mantissa sizes

- Nothing really needed to be changed in the tests, but I added a couple
  of test cases for the min and max int64.
This commit is contained in:
Ed Hennis
2025-11-14 19:21:45 -05:00
parent 857eaffa55
commit c9ad49faf3
3 changed files with 60 additions and 24 deletions

View File

@@ -509,6 +509,20 @@ squelch(Number const& x, Number const& limit) noexcept
return x;
}
inline std::string
to_string(MantissaRange::mantissa_scale const& scale)
{
switch (scale)
{
case MantissaRange::small:
return "small";
case MantissaRange::large:
return "large";
default:
throw std::runtime_error("Bad scale");
}
}
class saveNumberRoundMode
{
Number::rounding_mode mode_;

View File

@@ -9,20 +9,6 @@
namespace ripple {
std::string
to_string(MantissaRange::mantissa_scale const& scale)
{
switch (scale)
{
case MantissaRange::small:
return "small";
case MantissaRange::large:
return "large";
default:
throw std::runtime_error("Bad scale");
}
}
class Number_test : public beast::unit_test::suite
{
public:

View File

@@ -29,10 +29,8 @@ struct STNumber_test : public beast::unit_test::suite
}
void
run() override
doRun()
{
static_assert(!std::is_convertible_v<STNumber*, Number*>);
{
STNumber const stnum{sfNumber};
BEAST_EXPECT(stnum.getSType() == STI_NUMBER);
@@ -127,6 +125,38 @@ struct STNumber_test : public beast::unit_test::suite
BEAST_EXPECT(
numberFromJson(sfNumber, "-0.000e6") == STNumber(sfNumber, 0));
{
NumberRoundModeGuard mg(Number::towards_zero);
// maxint64 9,223,372,036,854,775,807
auto const maxInt =
std::to_string(std::numeric_limits<std::int64_t>::max());
// minint64 -9,223,372,036,854,775,808
auto const minInt =
std::to_string(std::numeric_limits<std::int64_t>::min());
if (Number::getMantissaScale() == MantissaRange::small)
{
BEAST_EXPECT(
numberFromJson(sfNumber, maxInt) ==
STNumber(sfNumber, Number{9'223'372'036'854'775, 3}));
BEAST_EXPECT(
numberFromJson(sfNumber, minInt) ==
STNumber(sfNumber, Number{-9'223'372'036'854'775, 3}));
}
else
{
BEAST_EXPECT(
numberFromJson(sfNumber, maxInt) ==
STNumber(
sfNumber, Number{9'223'372'036'854'775'807, 0}));
/*
BEAST_EXPECT(
numberFromJson(sfNumber, minInt) ==
STNumber(
sfNumber, Number{-9'223'372'036'854'775'807, 0}));
*/
}
}
constexpr auto imin = std::numeric_limits<int>::min();
BEAST_EXPECT(
numberFromJson(sfNumber, imin) ==
@@ -279,15 +309,21 @@ struct STNumber_test : public beast::unit_test::suite
}
}
}
void
run() override
{
static_assert(!std::is_convertible_v<STNumber*, Number*>);
for (auto const scale : {MantissaRange::small, MantissaRange::large})
{
NumberMantissaScaleGuard sg(scale);
testcase << to_string(Number::getMantissaScale());
doRun();
}
}
};
BEAST_DEFINE_TESTSUITE(STNumber, protocol, ripple);
void
testCompile(std::ostream& out)
{
STNumber number{sfNumber, 42};
out << number;
}
} // namespace ripple