Include rounding mode in XRPAmount to STAmount conversion.

This commit is contained in:
Howard Hinnant
2022-10-19 16:00:41 -04:00
committed by Elliot Lee
parent 6fcd654bee
commit e354497f63
6 changed files with 1028 additions and 653 deletions

View File

@@ -26,24 +26,6 @@
namespace ripple {
class saveNumberRoundMode
{
Number::rounding_mode mode_;
public:
~saveNumberRoundMode()
{
Number::setround(mode_);
}
explicit saveNumberRoundMode(Number::rounding_mode mode) noexcept
: mode_{mode}
{
}
saveNumberRoundMode(saveNumberRoundMode const&) = delete;
saveNumberRoundMode&
operator=(saveNumberRoundMode const&) = delete;
};
class Number_test : public beast::unit_test::suite
{
public:
@@ -580,6 +562,29 @@ public:
BEAST_EXPECT(x == y);
}
void
test_toSTAmount()
{
NumberSO stNumberSO{true};
Issue const issue;
Number const n{7'518'783'80596, -5};
saveNumberRoundMode const save{Number::setround(Number::to_nearest)};
auto res2 = STAmount{issue, n.mantissa(), n.exponent()};
BEAST_EXPECT(res2 == STAmount{7518784});
Number::setround(Number::towards_zero);
res2 = STAmount{issue, n.mantissa(), n.exponent()};
BEAST_EXPECT(res2 == STAmount{7518783});
Number::setround(Number::downward);
res2 = STAmount{issue, n.mantissa(), n.exponent()};
BEAST_EXPECT(res2 == STAmount{7518783});
Number::setround(Number::upward);
res2 = STAmount{issue, n.mantissa(), n.exponent()};
BEAST_EXPECT(res2 == STAmount{7518784});
}
void
run() override
{
@@ -599,6 +604,7 @@ public:
test_relationals();
test_stream();
test_inc_dec();
test_toSTAmount();
}
};