Skip to content

Commit

Permalink
Cleanup for consistency with develop and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Jul 23, 2024
1 parent eca2d61 commit 47171d9
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 144 deletions.
4 changes: 2 additions & 2 deletions src/test/app/SetAuth_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct SetAuth_test : public beast::unit_test::suite
using namespace jtx;
Json::Value jv;
jv[jss::Account] = account.human();
jv[jss::LimitAmount] = STAmount(Issue{to_currency(currency), dest})
.getJson(JsonOptions::none);
jv[jss::LimitAmount] =
STAmount({to_currency(currency), dest}).getJson(JsonOptions::none);
jv[jss::TransactionType] = jss::TrustSet;
jv[jss::Flags] = tfSetfAuth;
return jv;
Expand Down
69 changes: 4 additions & 65 deletions src/test/jtx/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,64 +161,8 @@ operator!=(PrettyAmount const& lhs, PrettyAmount const& rhs)
return !operator==(lhs, rhs);
}

template <typename T>
static std::string
to_places(const T d, std::uint8_t places)
{
assert(places <= std::numeric_limits<T>::digits10);

std::ostringstream oss;
oss << std::setprecision(places) << std::fixed << d;

std::string out = oss.str();
out.erase(out.find_last_not_of('0') + 1, std::string::npos);
if (out.back() == '.')
out.pop_back();

return out;
}

inline std::ostream&
operator<<(std::ostream& os, PrettyAmount const& amount)
{
if (amount.value().isIssue())
{
auto const& stAmount = get<STAmount>(amount);
if (stAmount.native())
{
// measure in hundredths
auto const c = dropsPerXRP.drops() / 100;
auto const n = stAmount.mantissa();
if (n < c)
{
if (stAmount.negative())
os << "-" << n << " drops";
else
os << n << " drops";
return os;
}
auto const d = double(n) / dropsPerXRP.drops();
if (stAmount.negative())
os << "-";

os << to_places(d, 6) << " XRP";
}
else
{
os << stAmount.getText() << "/"
<< to_string(stAmount.issue().currency) << "(" << amount.name()
<< ")";
}
}
else
{
auto const& mptAmount = get<STMPTAmount>(amount);
os << mptAmount.getText() << "/"
<< to_string(mptAmount.issue().getMptID()) << "(" << amount.name()
<< ")";
}
return os;
}
std::ostream&
operator<<(std::ostream& os, PrettyAmount const& amount);

//------------------------------------------------------------------------------

Expand Down Expand Up @@ -422,11 +366,11 @@ operator<<(std::ostream& os, IOU const& iou);

//------------------------------------------------------------------------------

/** Converts to MPT Issue or STAmount.
/** Converts to MPT Issue or STMPTAmount.
Examples:
MPT Converts to the underlying Issue
MPT(10) Returns STAmount of 10 of
MPT(10) Returns STMPTAmount of 10 of
the underlying MPT
*/
class MPT
Expand Down Expand Up @@ -526,11 +470,6 @@ struct AnyAmount
}
};

inline PrettyAmount::operator AnyAmount() const
{
return AnyAmount{amount_};
}

inline AnyAmount
any_t::operator()(STAmount const& sta) const
{
Expand Down
64 changes: 64 additions & 0 deletions src/test/jtx/impl/amount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,70 @@ operator<<(std::ostream&& os,
}
#endif

PrettyAmount::operator AnyAmount() const
{
return {amount_};
}

template <typename T>
static std::string
to_places(const T d, std::uint8_t places)
{
assert(places <= std::numeric_limits<T>::digits10);

std::ostringstream oss;
oss << std::setprecision(places) << std::fixed << d;

std::string out = oss.str();
out.erase(out.find_last_not_of('0') + 1, std::string::npos);
if (out.back() == '.')
out.pop_back();

return out;
}

std::ostream&
operator<<(std::ostream& os, PrettyAmount const& amount)
{
if (amount.value().isIssue())
{
auto const& stAmount = get<STAmount>(amount);
if (stAmount.native())
{
// measure in hundredths
auto const c = dropsPerXRP.drops() / 100;
auto const n = stAmount.mantissa();
if (n < c)
{
if (stAmount.negative())
os << "-" << n << " drops";
else
os << n << " drops";
return os;
}
auto const d = double(n) / dropsPerXRP.drops();
if (stAmount.negative())
os << "-";

os << to_places(d, 6) << " XRP";
}
else
{
os << stAmount.getText() << "/"
<< to_string(stAmount.issue().currency) << "(" << amount.name()
<< ")";
}
}
else
{
auto const& mptAmount = get<STMPTAmount>(amount);
os << mptAmount.getText() << "/"
<< to_string(mptAmount.issue().getMptID()) << "(" << amount.name()
<< ")";
}
return os;
}

//------------------------------------------------------------------------------

XRP_t const XRP{};
Expand Down
27 changes: 3 additions & 24 deletions src/test/jtx/impl/offer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ namespace ripple {
namespace test {
namespace jtx {

template <ValidAmountType T>
static Json::Value
Json::Value
offer(
Account const& account,
T const& takerPays,
STAmount const& takerGets,
STEitherAmount const& takerPays,
STEitherAmount const& takerGets,
std::uint32_t flags)
{
Json::Value jv;
Expand All @@ -42,26 +41,6 @@ offer(
return jv;
}

Json::Value
offer(
Account const& account,
STAmount const& takerPays,
STAmount const& takerGets,
std::uint32_t flags)
{
return offer<STAmount>(account, takerPays, takerGets, flags);
}

Json::Value
offer(
Account const& account,
STMPTAmount const& takerPays,
STAmount const& takerGets,
std::uint32_t flags)
{
return offer<STMPTAmount>(account, takerPays, takerGets, flags);
}

Json::Value
offer_cancel(Account const& account, std::uint32_t offerSeq)
{
Expand Down
23 changes: 2 additions & 21 deletions src/test/jtx/impl/trust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ trust(
return jv;
}

template <ValidAmountType T>
Json::Value
claw_amount(
claw(
Account const& account,
T const& amount,
STEitherAmount const& amount,
std::optional<Account> const& mptHolder)
{
Json::Value jv;
Expand All @@ -81,24 +80,6 @@ claw_amount(
return jv;
}

Json::Value
claw(
Account const& account,
STAmount const& amount,
std::optional<Account> const& mptHolder)
{
return claw_amount(account, amount, mptHolder);
}

Json::Value
claw(
Account const& account,
STMPTAmount const& amount,
std::optional<Account> const& mptHolder)
{
return claw_amount(account, amount, mptHolder);
}

} // namespace jtx
} // namespace test
} // namespace ripple
11 changes: 2 additions & 9 deletions src/test/jtx/offer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ namespace jtx {
Json::Value
offer(
Account const& account,
STAmount const& takerPays,
STAmount const& takerGets,
std::uint32_t flags = 0);

Json::Value
offer(
Account const& account,
STMPTAmount const& takerPays,
STAmount const& takerGets,
STEitherAmount const& takerPays,
STEitherAmount const& takerGets,
std::uint32_t flags = 0);

/** Cancel an offer. */
Expand Down
7 changes: 1 addition & 6 deletions src/test/jtx/trust.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ trust(
Json::Value
claw(
Account const& account,
STAmount const& amount,
std::optional<Account> const& mptHolder = std::nullopt);
Json::Value
claw(
Account const& account,
STMPTAmount const& amount,
STEitherAmount const& amount,
std::optional<Account> const& mptHolder = std::nullopt);

} // namespace jtx
Expand Down
6 changes: 2 additions & 4 deletions src/test/ledger/BookDirs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ struct BookDirs_test : public beast::unit_test::suite
auto i = 1, j = 3, k = 0;
for (auto const& e : d)
{
BEAST_EXPECT(
get<STAmount>(e->getFieldAmount(sfTakerPays)) == AUD(i));
BEAST_EXPECT(
get<STAmount>(e->getFieldAmount(sfTakerGets)) == XRP(j));
BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
if (++k % 80 == 0)
{
++i;
Expand Down
8 changes: 2 additions & 6 deletions src/test/ledger/Directory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,8 @@ struct Directory_test : public beast::unit_test::suite
for (auto const& offer : book)
{
count++;
BEAST_EXPECT(
get<STAmount>(offer->getFieldAmount(sfTakerPays)) ==
USD(count));
BEAST_EXPECT(
get<STAmount>(offer->getFieldAmount(sfTakerGets)) ==
XRP(count));
BEAST_EXPECT(offer->getFieldAmount(sfTakerPays) == USD(count));
BEAST_EXPECT(offer->getFieldAmount(sfTakerGets) == XRP(count));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/protocol/Quality_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Quality_test : public beast::unit_test::suite
// Create a raw, non-integral amount from mantissa and exponent
STAmount static raw(std::uint64_t mantissa, int exponent)
{
return STAmount(Issue{Currency(3), AccountID(3)}, mantissa, exponent);
return STAmount({Currency(3), AccountID(3)}, mantissa, exponent);
}

template <class Integer>
Expand Down
3 changes: 1 addition & 2 deletions src/test/protocol/STAmount_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,7 @@ class STAmount_test : public beast::unit_test::suite

try
{
auto const t =
static_cast<STAmount>(amountFromString(usd, "136500"));
auto const t = amountFromString(usd, "136500");
fail(to_string(t.xrp()));
}
catch (std::logic_error const&)
Expand Down
2 changes: 1 addition & 1 deletion src/test/protocol/STObject_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ class STObject_test : public beast::unit_test::suite
st[sfAmount] = STEitherAmount{};
st[sfAccount] = AccountID{};
st[sfDigest] = uint256{};
[&](STAmount) {}(get<STAmount>(st[sfAmount]));
[&](STEitherAmount) {}(st[sfAmount]);
[&](AccountID) {}(st[sfAccount]);
[&](uint256) {}(st[sfDigest]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/xrpld/app/misc/FeeVoteImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ FeeVoteImpl::doVoting(
}

// choose our positions
// TODO: Use structured binding once LLVM 16 is the minimum supported
// version. See also: https://github.com/llvm/llvm-project/issues/48582
// https://github.com/llvm/llvm-project/commit/127bf44385424891eb04cff8e52d3f157fc2cb7c
// TODO: Use structured binding once LLVM issue
// https://github.com/llvm/llvm-project/issues/48582
// is fixed.
auto const baseFee = baseFeeVote.getVotes();
auto const baseReserve = baseReserveVote.getVotes();
auto const incReserve = incReserveVote.getVotes();
Expand Down

0 comments on commit 47171d9

Please sign in to comment.