xrpld
Loading...
Searching...
No Matches
tests/libxrpl/basics/MPTAmount.cpp
1#include <xrpl/protocol/MPTAmount.h>
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/Rules.h>
7
8#include <gtest/gtest.h>
9
10#include <limits>
11#include <stdexcept>
12#include <unordered_set>
13
14namespace xrpl {
15
16namespace {
17
20
22makeRules(bool withFix)
23{
24 // Rules keeps a reference to its presets, so they must outlive it.
25 static std::unordered_set<uint256, beast::Uhash<>> const kWithFix{fixCleanup3_5_0};
26 static std::unordered_set<uint256, beast::Uhash<>> const kWithoutFix;
27 return Rules{withFix ? kWithFix : kWithoutFix};
28}
29
30void
31expectBoundariesDoNotThrow()
32{
33 MPTAmount const max(kMaxMpt);
34 MPTAmount const min(kMinMpt);
35
36 EXPECT_EQ(MPTAmount(kMaxMpt - 1) + MPTAmount(1), max);
37 EXPECT_EQ(MPTAmount(kMinMpt + 1) - MPTAmount(1), min);
38 EXPECT_EQ(MPTAmount(kMinMpt + 1) + MPTAmount(-1), min);
39 EXPECT_EQ(MPTAmount(kMaxMpt - 1) - MPTAmount(-1), max);
40 EXPECT_EQ(max + min, MPTAmount(-1));
41 EXPECT_EQ(max - max, MPTAmount(0));
42 EXPECT_EQ(min - min, MPTAmount(0));
43 EXPECT_EQ(MPTAmount(kMaxMpt / 2) + MPTAmount(kMaxMpt / 2), MPTAmount(kMaxMpt - 1));
44}
45
46void
47expectOverflowsThrow()
48{
49 // += and -= leave the lhs unchanged after the throw
50 {
51 MPTAmount a(kMaxMpt);
52 EXPECT_THROW(a += MPTAmount(1), std::overflow_error);
53 EXPECT_EQ(a, MPTAmount(kMaxMpt));
54
55 MPTAmount b(kMinMpt);
56 EXPECT_THROW(b += MPTAmount(-1), std::overflow_error);
57 EXPECT_EQ(b, MPTAmount(kMinMpt));
58
59 MPTAmount c(kMinMpt);
60 EXPECT_THROW(c -= MPTAmount(1), std::overflow_error);
61 EXPECT_EQ(c, MPTAmount(kMinMpt));
62
63 MPTAmount d(kMaxMpt);
64 EXPECT_THROW(d -= MPTAmount(-1), std::overflow_error);
65 EXPECT_EQ(d, MPTAmount(kMaxMpt));
66 }
67
68 // Binary + and - are built from the compound operators
69 {
70 // Two amounts that are each a legal MPT balance
71 MPTAmount const half((kMaxMpt / 2) + 1);
72 EXPECT_THROW((void)(half + half), std::overflow_error);
73
74 MPTAmount const max(kMaxMpt);
75 MPTAmount const negMax = MPTAmount(0) - max;
76 EXPECT_THROW((void)(negMax - max), std::overflow_error);
77 }
78}
79
80} // namespace
81
82TEST(MPTAmountTest, overflow_throws_with_fix)
83{
84 CurrentTransactionRulesGuard const rg(makeRules(true));
85 expectOverflowsThrow();
86 expectBoundariesDoNotThrow();
87}
88
89// With no rules set (RPC, pathfinding) the operators throw as if the
90// amendment were enabled.
91TEST(MPTAmountTest, overflow_throws_without_rules)
92{
93 ASSERT_FALSE(getCurrentTransactionRules());
94 expectOverflowsThrow();
95 expectBoundariesDoNotThrow();
96}
97
98// Without the amendment an overflow keeps the legacy signed arithmetic, which
99// is undefined behavior, so only results that do not overflow are checked.
100TEST(MPTAmountTest, boundaries_without_fix)
101{
102 CurrentTransactionRulesGuard const rg(makeRules(false));
103 expectBoundariesDoNotThrow();
104}
105
106} // namespace xrpl
RAII class to set and restore the current transaction rules.
Definition Rules.h:113
Rules controlling protocol behavior.
Definition Rules.h:40
T max(T... args)
T min(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)
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:30