xrpld
Loading...
Searching...
No Matches
Units_test.cpp
1#include <xrpl/beast/unit_test/suite.h>
2#include <xrpl/beast/utility/Zero.h>
3#include <xrpl/json/json_value.h>
4#include <xrpl/protocol/SystemParameters.h>
5#include <xrpl/protocol/Units.h>
6#include <xrpl/protocol/XRPAmount.h>
7
8#include <cstdint>
9#include <limits>
10#include <type_traits>
11
12namespace xrpl::test {
13
15{
16private:
17 void
19 {
20 using FeeLevel32 = FeeLevel<std::uint32_t>;
21
22 {
23 XRPAmount const x{100};
24 BEAST_EXPECT(x.drops() == 100);
25 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::dropTag>));
26 auto y = 4u * x;
27 BEAST_EXPECT(y.value() == 400);
28 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::dropTag>));
29
30 auto z = 4 * y;
31 BEAST_EXPECT(z.value() == 1600);
32 BEAST_EXPECT((std::is_same_v<decltype(z)::unit_type, unit::dropTag>));
33
34 FeeLevel32 const f{10};
35 FeeLevel32 const baseFee{100};
36
37 auto drops = mulDiv(baseFee, x, f);
38
39 BEAST_EXPECT(drops);
40 BEAST_EXPECT(drops.value() == 1000); // NOLINT(bugprone-unchecked-optional-access)
41 BEAST_EXPECT((std::is_same_v<
42 std::remove_reference_t<decltype(*drops)>::unit_type,
43 unit::dropTag>));
44
45 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
46 }
47 {
48 XRPAmount const x{100};
49 BEAST_EXPECT(x.value() == 100);
50 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::dropTag>));
51 auto y = 4u * x;
52 BEAST_EXPECT(y.value() == 400);
53 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::dropTag>));
54
55 FeeLevel64 const f{10};
56 FeeLevel64 const baseFee{100};
57
58 auto drops = mulDiv(baseFee, x, f);
59
60 BEAST_EXPECT(drops);
61 BEAST_EXPECT(drops.value() == 1000); // NOLINT(bugprone-unchecked-optional-access)
62 BEAST_EXPECT((std::is_same_v<
63 std::remove_reference_t<decltype(*drops)>::unit_type,
64 unit::dropTag>));
65 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
66 }
67 {
68 FeeLevel64 const x{1024};
69 BEAST_EXPECT(x.value() == 1024);
70 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::feelevelTag>));
71 std::uint64_t const m = 4;
72 auto y = m * x;
73 BEAST_EXPECT(y.value() == 4096);
74 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::feelevelTag>));
75
76 XRPAmount const basefee{10};
77 FeeLevel64 const referencefee{256};
78
79 auto drops = mulDiv(x, basefee, referencefee);
80
81 BEAST_EXPECT(drops);
82 BEAST_EXPECT(drops.value() == 40); // NOLINT(bugprone-unchecked-optional-access)
83 BEAST_EXPECT((std::is_same_v<
84 std::remove_reference_t<decltype(*drops)>::unit_type,
85 unit::dropTag>));
86 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
87 }
88 }
89
90 void
92 {
93 // Json value functionality
94 using FeeLevel32 = FeeLevel<std::uint32_t>;
95
96 {
97 FeeLevel32 const x{std::numeric_limits<std::uint32_t>::max()};
98 auto y = x.jsonClipped();
99 BEAST_EXPECT(y.type() == json::ValueType::UInt);
100 BEAST_EXPECT(y == json::Value{x.fee()});
101 }
102
103 {
104 FeeLevel32 const x{std::numeric_limits<std::uint32_t>::min()};
105 auto y = x.jsonClipped();
106 BEAST_EXPECT(y.type() == json::ValueType::UInt);
107 BEAST_EXPECT(y == json::Value{x.fee()});
108 }
109
110 {
112 auto y = x.jsonClipped();
113 BEAST_EXPECT(y.type() == json::ValueType::UInt);
115 }
116
117 {
119 auto y = x.jsonClipped();
120 BEAST_EXPECT(y.type() == json::ValueType::UInt);
121 BEAST_EXPECT(y == json::Value{0});
122 }
123
124 {
126 auto y = x.jsonClipped();
127 BEAST_EXPECT(y.type() == json::ValueType::Real);
128 BEAST_EXPECT(y == json::Value{std::numeric_limits<double>::max()});
129 }
130
131 {
133 auto y = x.jsonClipped();
134 BEAST_EXPECT(y.type() == json::ValueType::Real);
135 BEAST_EXPECT(y == json::Value{std::numeric_limits<double>::min()});
136 }
137
138 {
140 auto y = x.jsonClipped();
141 BEAST_EXPECT(y.type() == json::ValueType::Int);
143 }
144
145 {
147 auto y = x.jsonClipped();
148 BEAST_EXPECT(y.type() == json::ValueType::Int);
150 }
151 }
152
153 void
155 {
156 // Explicitly test every defined function for the ValueUnit class
157 // since some of them are templated, but not used anywhere else.
158 using FeeLevel32 = FeeLevel<std::uint32_t>;
159
160 {
161 auto make = [&](auto x) -> FeeLevel64 { return x; };
162 auto explicitmake = [&](auto x) -> FeeLevel64 { return FeeLevel64{x}; };
163
164 [[maybe_unused]]
165 FeeLevel64 const defaulted{};
166 FeeLevel64 test{0};
167 BEAST_EXPECT(test.fee() == 0);
168
169 test = explicitmake(beast::kZero);
170 BEAST_EXPECT(test.fee() == 0);
171
172 test = beast::kZero;
173 BEAST_EXPECT(test.fee() == 0);
174
175 test = explicitmake(100u);
176 BEAST_EXPECT(test.fee() == 100);
177
178 FeeLevel64 const targetSame{200u};
179 FeeLevel32 const targetOther{300u};
180 test = make(targetSame);
181 BEAST_EXPECT(test.fee() == 200);
182 BEAST_EXPECT(test == targetSame);
183 BEAST_EXPECT(test < FeeLevel64{1000});
184 BEAST_EXPECT(test > FeeLevel64{100});
185 test = make(targetOther);
186 BEAST_EXPECT(test.fee() == 300);
187 BEAST_EXPECT(test == targetOther);
188
189 test = std::uint64_t(200);
190 BEAST_EXPECT(test.fee() == 200);
191 test = std::uint32_t(300);
192 BEAST_EXPECT(test.fee() == 300);
193
194 test = targetSame;
195 BEAST_EXPECT(test.fee() == 200);
196 test = targetOther.fee();
197 BEAST_EXPECT(test.fee() == 300);
198 BEAST_EXPECT(test == targetOther);
199
200 test = targetSame * 2;
201 BEAST_EXPECT(test.fee() == 400);
202 test = 3 * targetSame;
203 BEAST_EXPECT(test.fee() == 600);
204 test = targetSame / 10;
205 BEAST_EXPECT(test.fee() == 20);
206
207 test += targetSame;
208 BEAST_EXPECT(test.fee() == 220);
209
210 test -= targetSame;
211 BEAST_EXPECT(test.fee() == 20);
212
213 test++;
214 BEAST_EXPECT(test.fee() == 21);
215 ++test;
216 BEAST_EXPECT(test.fee() == 22);
217 test--;
218 BEAST_EXPECT(test.fee() == 21);
219 --test;
220 BEAST_EXPECT(test.fee() == 20);
221
222 test *= 5;
223 BEAST_EXPECT(test.fee() == 100);
224 test /= 2;
225 BEAST_EXPECT(test.fee() == 50);
226 test %= 13;
227 BEAST_EXPECT(test.fee() == 11);
228
229 /*
230 // illegal with unsigned
231 test = -test;
232 BEAST_EXPECT(test.fee() == -11);
233 BEAST_EXPECT(test.signum() == -1);
234 BEAST_EXPECT(to_string(test) == "-11");
235 */
236
237 BEAST_EXPECT(test);
238 test = 0;
239 BEAST_EXPECT(!test);
240 BEAST_EXPECT(test.signum() == 0);
241 test = targetSame;
242 BEAST_EXPECT(test.signum() == 1);
243 BEAST_EXPECT(to_string(test) == "200");
244 }
245 {
246 auto make = [&](auto x) -> FeeLevelDouble { return x; };
247 auto explicitmake = [&](auto x) -> FeeLevelDouble { return FeeLevelDouble{x}; };
248
249 [[maybe_unused]]
250 FeeLevelDouble const defaulted{};
252 BEAST_EXPECT(test.fee() == 0);
253
254 test = explicitmake(beast::kZero);
255 BEAST_EXPECT(test.fee() == 0);
256
257 test = beast::kZero;
258 BEAST_EXPECT(test.fee() == 0);
259
260 test = explicitmake(100.0);
261 BEAST_EXPECT(test.fee() == 100);
262
263 FeeLevelDouble const targetSame{200.0};
264 FeeLevel64 const targetOther{300};
265 test = make(targetSame);
266 BEAST_EXPECT(test.fee() == 200);
267 BEAST_EXPECT(test == targetSame);
268 BEAST_EXPECT(test < FeeLevelDouble{1000.0});
269 BEAST_EXPECT(test > FeeLevelDouble{100.0});
270 test = targetOther.fee();
271 BEAST_EXPECT(test.fee() == 300);
272 BEAST_EXPECT(test == targetOther);
273
274 test = 200.0;
275 BEAST_EXPECT(test.fee() == 200);
276 test = std::uint64_t(300);
277 BEAST_EXPECT(test.fee() == 300);
278
279 test = targetSame;
280 BEAST_EXPECT(test.fee() == 200);
281
282 test = targetSame * 2;
283 BEAST_EXPECT(test.fee() == 400);
284 test = 3 * targetSame;
285 BEAST_EXPECT(test.fee() == 600);
286 test = targetSame / 10;
287 BEAST_EXPECT(test.fee() == 20);
288
289 test += targetSame;
290 BEAST_EXPECT(test.fee() == 220);
291
292 test -= targetSame;
293 BEAST_EXPECT(test.fee() == 20);
294
295 test++;
296 BEAST_EXPECT(test.fee() == 21);
297 ++test;
298 BEAST_EXPECT(test.fee() == 22);
299 test--;
300 BEAST_EXPECT(test.fee() == 21);
301 --test;
302 BEAST_EXPECT(test.fee() == 20);
303
304 test *= 5;
305 BEAST_EXPECT(test.fee() == 100);
306 test /= 2;
307 BEAST_EXPECT(test.fee() == 50);
308 /* illegal with floating
309 test %= 13;
310 BEAST_EXPECT(test.fee() == 11);
311 */
312
313 // legal with signed
314 test = -test;
315 BEAST_EXPECT(test.fee() == -50);
316 BEAST_EXPECT(test.signum() == -1);
317 BEAST_EXPECT(to_string(test) == "-50.000000");
318
319 BEAST_EXPECT(test);
320 test = 0;
321 BEAST_EXPECT(!test);
322 BEAST_EXPECT(test.signum() == 0);
323 test = targetSame;
324 BEAST_EXPECT(test.signum() == 1);
325 BEAST_EXPECT(to_string(test) == "200.000000");
326 }
327 }
328
329public:
330 void
331 run() override
332 {
333 BEAST_EXPECT(kInitialXrp.drops() == 100'000'000'000'000'000);
334 BEAST_EXPECT(kInitialXrp == XRPAmount{100'000'000'000'000'000});
335
336 testTypes();
337 testJson();
339 }
340};
341
343
344} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
Represents a JSON value.
Definition json_value.h:130
json::Value jsonClipped() const
Definition XRPAmount.h:199
constexpr value_type value() const
Returns the underlying value.
Definition XRPAmount.h:220
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:159
void run() override
Runs the suite.
constexpr value_type value() const
Returns the underlying value.
Definition Units.h:323
json::Value jsonClipped() const
Definition Units.h:295
constexpr value_type fee() const
Returns the number of drops.
Definition Units.h:278
T is_same_v
T max(T... args)
T min(T... args)
@ UInt
unsigned integer value
Definition json_value.h:21
@ Int
signed integer value
Definition json_value.h:20
@ Real
double value
Definition json_value.h:22
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:48
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:428
FeeLevel< double > FeeLevelDouble
Definition Units.h:429
unit::ValueUnit< unit::feelevelTag, T > FeeLevel
Definition Units.h:427
constexpr XRPAmount kInitialXrp
Configure the native currency.