1#include <xrpl/protocol/XRPAmount.h>
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Rules.h>
8#include <xrpl/protocol/SystemParameters.h>
10#include <gtest/gtest.h>
19TEST(XRPAmountTest, sig_num)
21 for (
auto const i : {-1, 0, 1})
27 EXPECT_TRUE(x.
signum() < 0);
31 EXPECT_TRUE(x.
signum() > 0);
40TEST(XRPAmountTest, beast_zero)
44 for (
auto const i : {-1, 0, 1})
48 EXPECT_TRUE((i == 0) == (x == kZero));
49 EXPECT_TRUE((i != 0) == (x != kZero));
50 EXPECT_TRUE((i < 0) == (x < kZero));
51 EXPECT_TRUE((i > 0) == (x > kZero));
52 EXPECT_TRUE((i <= 0) == (x <= kZero));
53 EXPECT_TRUE((i >= 0) == (x >= kZero));
55 EXPECT_TRUE((0 == i) == (kZero == x));
56 EXPECT_TRUE((0 != i) == (kZero != x));
57 EXPECT_TRUE((0 < i) == (kZero < x));
58 EXPECT_TRUE((0 > i) == (kZero > x));
59 EXPECT_TRUE((0 <= i) == (kZero <= x));
60 EXPECT_TRUE((0 >= i) == (kZero >= x));
64TEST(XRPAmountTest, comparisons)
66 for (
auto const i : {-1, 0, 1})
70 for (
auto const j : {-1, 0, 1})
74 EXPECT_EQ((i == j), (x == y));
75 EXPECT_EQ((i != j), (x != y));
76 EXPECT_EQ((i < j), (x < y));
77 EXPECT_EQ((i > j), (x > y));
78 EXPECT_EQ((i <= j), (x <= y));
79 EXPECT_EQ((i >= j), (x >= y));
84TEST(XRPAmountTest, add_sub)
86 for (
auto const i : {-1, 0, 1})
90 for (
auto const j : {-1, 0, 1})
97 EXPECT_EQ((x + y), (y + x));
108 EXPECT_EQ(
test.decimalXRP(), 0.000001);
111 EXPECT_EQ(
test.decimalXRP(), -0.000001);
114 EXPECT_EQ(
test.decimalXRP(), 100);
117 EXPECT_EQ(
test.decimalXRP(), -100);
129 EXPECT_EQ(
test.drops(), 0);
132 EXPECT_EQ(
test.drops(), 0);
135 EXPECT_EQ(
test.drops(), 0);
138 EXPECT_EQ(
test.drops(), 100);
141 EXPECT_EQ(
test.drops(), 100);
144 test = make(targetSame);
145 EXPECT_EQ(
test.drops(), 200);
146 EXPECT_EQ(
test, targetSame);
151 EXPECT_EQ(
test.drops(), 200);
153 EXPECT_EQ(
test.drops(), 300);
156 EXPECT_EQ(
test.drops(), 200);
158 EXPECT_TRUE(testOther);
159 EXPECT_EQ(*testOther, 200);
162 EXPECT_FALSE(testOther);
165 EXPECT_FALSE(testOther);
167 test = targetSame * 2;
168 EXPECT_EQ(
test.drops(), 400);
169 test = 3 * targetSame;
170 EXPECT_EQ(
test.drops(), 600);
172 EXPECT_EQ(
test.drops(), 20);
175 EXPECT_EQ(
test.drops(), 220);
178 EXPECT_EQ(
test.drops(), 20);
181 EXPECT_EQ(
test.drops(), 100);
183 EXPECT_EQ(
test.drops(), 50);
185 EXPECT_EQ(
test.drops(), 11);
189 EXPECT_EQ(
test.drops(), -11);
190 EXPECT_EQ(
test.signum(), -1);
196 EXPECT_EQ(
test.signum(), 0);
198 EXPECT_EQ(
test.signum(), 1);
215 EXPECT_EQ(big,
mulRatio(big, kMaxUInt32, kMaxUInt32,
true));
217 EXPECT_EQ(big,
mulRatio(big, kMaxUInt32, kMaxUInt32,
false));
225 EXPECT_EQ(
mulRatio(big, 3, 4,
false).value(), (big.
value() / 4) * 3);
226 EXPECT_EQ(
mulRatio(big, 3, 4,
true).value(), (big.
value() / 4) * 3);
230 EXPECT_EQ(big.
value() % 4, 0);
231 EXPECT_GT(big.
value(), kMaxXrp / 3);
232 EXPECT_LE(big.
value() / 4, kMaxXrp / 3);
239 EXPECT_EQ(big,
mulRatio(big, kMaxUInt32, kMaxUInt32,
true));
241 EXPECT_EQ(big,
mulRatio(big, kMaxUInt32, kMaxUInt32,
false));
248 EXPECT_EQ(
mulRatio(big, 3, 4,
false).value(), (big.
value() / 4) * 3);
249 EXPECT_EQ(
mulRatio(big, 3, 4,
true).value(), (big.
value() / 4) * 3);
251 EXPECT_EQ(big.
value() % 4, 0);
252 EXPECT_LT(big.
value(), kMinXrp / 3);
253 EXPECT_GE(big.
value() / 4, kMinXrp / 3);
260 EXPECT_EQ(tiny,
mulRatio(tiny, 1, kMaxUInt32,
true));
272 EXPECT_EQ(tinyNeg,
mulRatio(tinyNeg, kMaxUInt32 - 1, kMaxUInt32,
false));
279 auto const roundedUp =
mulRatio(one, kMaxUInt32 - 1, kMaxUInt32,
true);
280 auto const roundedDown =
mulRatio(one, kMaxUInt32 - 1, kMaxUInt32,
false);
281 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
288 auto const roundedUp =
mulRatio(big, kMaxUInt32 - 1, kMaxUInt32,
true);
289 auto const roundedDown =
mulRatio(big, kMaxUInt32 - 1, kMaxUInt32,
false);
290 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
297 auto const roundedUp =
mulRatio(negOne, kMaxUInt32 - 1, kMaxUInt32,
true);
298 auto const roundedDown =
mulRatio(negOne, kMaxUInt32 - 1, kMaxUInt32,
false);
299 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
306 EXPECT_ANY_THROW({
mulRatio(one, 1, 0,
true); });
313 EXPECT_ANY_THROW({
mulRatio(big, 2, 1,
true); });
319 XRPAmount const bigNegative(kMinXrp + 10);
321 EXPECT_EQ(
mulRatio(bigNegative, 2, 1,
true), kMinXrp);
330makeRules(
bool withFix)
334 static std::unordered_set<uint256, beast::Uhash<>>
const kWithoutFix;
335 return Rules{withFix ? kWithFix : kWithoutFix};
339expectBoundariesDoNotThrow()
357 EXPECT_EQ(min * 1, min);
360 EXPECT_EQ(max * -1,
XRPAmount(-kMaxDrops));
376expectOverflowsThrow()
381 EXPECT_THROW(a +=
XRPAmount(1), std::overflow_error);
385 EXPECT_THROW(b +=
XRPAmount(-1), std::overflow_error);
389 EXPECT_THROW(c -=
XRPAmount(1), std::overflow_error);
393 EXPECT_THROW(d -=
XRPAmount(-1), std::overflow_error);
419 EXPECT_THROW((
void)(max +
XRPAmount(1)), std::overflow_error);
420 EXPECT_THROW((
void)((
XRPAmount(0) - max) - max), std::overflow_error);
426 EXPECT_THROW(a *= 2, std::overflow_error);
430 EXPECT_THROW(b *= -1, std::overflow_error);
433 XRPAmount const half((kMaxDrops / 2) + 1);
434 EXPECT_THROW((
void)(half * 2), std::overflow_error);
435 EXPECT_THROW((
void)(2 * half), std::overflow_error);
436 EXPECT_THROW((
void)(
XRPAmount(kMinDrops) * -1), std::overflow_error);
437 EXPECT_THROW((
void)(-1 *
XRPAmount(kMinDrops)), std::overflow_error);
439 EXPECT_THROW((
void)(
XRPAmount(kMaxDrops) * -2), std::overflow_error);
440 EXPECT_THROW((
void)(
XRPAmount(kMinDrops) * 2), std::overflow_error);
446TEST(XRPAmountTest, overflow_throws_with_fix)
449 expectOverflowsThrow();
450 expectBoundariesDoNotThrow();
455TEST(XRPAmountTest, overflow_throws_without_rules)
458 expectOverflowsThrow();
459 expectBoundariesDoNotThrow();
464TEST(XRPAmountTest, boundaries_without_fix)
467 expectBoundariesDoNotThrow();
RAII class to set and restore the current transaction rules.
Rules controlling protocol behavior.
static constexpr auto kMaxXrp
static constexpr auto kMinXrp
static constexpr auto kMaxUInt32
constexpr value_type value() const
Returns the underlying value.
constexpr int signum() const noexcept
Return the sign of the amount.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TEST(FileUtilitiesTest, get_file_contents)
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
std::optional< Rules > const & getCurrentTransactionRules()
TEST_F(HardenedHashTest, user_types)
std::string to_string(BaseUInt< Bits, Tag > const &a)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
constexpr XRPAmount kInitialXrp
Configure the native currency.