xrpld
Loading...
Searching...
No Matches
tests/libxrpl/protocol/STAmount.cpp
1#include <xrpl/protocol/STAmount.h>
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/protocol/AccountID.h>
5#include <xrpl/protocol/Indexes.h>
6#include <xrpl/protocol/Issue.h>
7#include <xrpl/protocol/MPTIssue.h>
8#include <xrpl/protocol/Protocol.h>
9#include <xrpl/protocol/Quality.h>
10#include <xrpl/protocol/UintTypes.h>
11#include <xrpl/protocol/XRPAmount.h>
12
13#include <gtest/gtest.h>
14
15#include <cstdint>
16#include <stdexcept>
17
18using namespace xrpl;
19
20namespace {
21
22// A 25% transfer fee.
23constexpr std::uint32_t kTransferRate125 = 1'250'000'000;
24
25AccountID const kUsdIssuer{1};
26
27} // namespace
28
29TEST(STAmount, mul_ratio_iou)
30{
31 Issue const usd{toCurrency("USD"), kUsdIssuer};
32
33 EXPECT_EQ(
34 mulRatio(STAmount{usd, 100}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Downward),
35 STAmount(usd, 80));
36 EXPECT_EQ(
37 mulRatio(STAmount{usd, 80}, kTransferRate125, QUALITY_ONE, Number::RoundingMode::Upward),
38 STAmount(usd, 100));
39
40 // 1 / 1.7 does not fit in a 16-digit mantissa, so the rounding direction
41 // decides the last digit.
42 STAmount const one{usd, 1};
43 EXPECT_LT(
44 mulRatio(one, QUALITY_ONE, 1'700'000'000, Number::RoundingMode::Downward),
45 mulRatio(one, QUALITY_ONE, 1'700'000'000, Number::RoundingMode::Upward));
46
47 STAmount const full{usd, std::uint64_t{9'999'999'999'999'999}, -5};
48 EXPECT_EQ(mulRatio(full, QUALITY_ONE, QUALITY_ONE, Number::RoundingMode::Downward), full);
49}
50
51TEST(STAmount, mul_ratio_mpt)
52{
53 MPTIssue const mpt{makeMptID(1, kUsdIssuer)};
54
55 EXPECT_EQ(
56 mulRatio(STAmount{mpt, 31}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Downward),
57 STAmount(mpt, 24));
58 EXPECT_EQ(
59 mulRatio(STAmount{mpt, 31}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Upward),
60 STAmount(mpt, 25));
61 EXPECT_EQ(
62 mulRatio(STAmount{mpt, 1}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Downward),
63 STAmount(mpt));
64 EXPECT_EQ(
65 mulRatio(STAmount{mpt, 1}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Upward),
66 STAmount(mpt, 1));
67
68 STAmount const max{mpt, kMaxMpTokenAmount};
69 EXPECT_EQ(mulRatio(max, QUALITY_ONE, QUALITY_ONE, Number::RoundingMode::Downward), max);
70 EXPECT_NO_THROW(
71 (void)mulRatio(max, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Downward));
72 EXPECT_THROW(
73 (void)mulRatio(max, kTransferRate125, QUALITY_ONE, Number::RoundingMode::Upward),
75}
76
77TEST(STAmount, mul_ratio_xrp)
78{
79 EXPECT_EQ(
81 STAmount{XRPAmount{31}}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Downward),
82 STAmount{XRPAmount{24}});
83 EXPECT_EQ(
85 STAmount{XRPAmount{31}}, QUALITY_ONE, kTransferRate125, Number::RoundingMode::Upward),
86 STAmount{XRPAmount{25}});
87}
A currency issued by an account.
Definition Issue.h:18
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TEST(FileUtilitiesTest, get_file_contents)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:297