xrpld
Loading...
Searching...
No Matches
Units.cpp
1#include <xrpl/protocol/Units.h>
2
3#include <xrpl/beast/utility/Zero.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/protocol/SystemParameters.h>
6#include <xrpl/protocol/XRPAmount.h>
7
8#include <gtest/gtest.h>
9
10#include <cstdint>
11#include <limits>
12#include <type_traits>
13
14namespace xrpl::test {
15
16TEST(UnitsTest, types)
17{
18 using FeeLevel32 = FeeLevel<std::uint32_t>;
19
20 {
21 XRPAmount const x{100};
22 EXPECT_EQ(x.drops(), 100);
23 EXPECT_TRUE((std::is_same_v<decltype(x)::UnitType, unit::dropTag>));
24 auto const y = 4u * x;
25 EXPECT_EQ(y.value(), 400);
26 EXPECT_TRUE((std::is_same_v<decltype(y)::UnitType, unit::dropTag>));
27
28 auto const z = 4 * y;
29 EXPECT_EQ(z.value(), 1600);
30 EXPECT_TRUE((std::is_same_v<decltype(z)::UnitType, unit::dropTag>));
31
32 FeeLevel32 const f{10};
33 FeeLevel32 const baseFee{100};
34
35 auto drops = mulDiv(baseFee, x, f);
36
37 EXPECT_TRUE(drops);
38 EXPECT_EQ(drops.value(), 1000); // NOLINT(bugprone-unchecked-optional-access)
39 EXPECT_TRUE(
40 (std::is_same_v<std::remove_reference_t<decltype(*drops)>::UnitType, unit::dropTag>));
41
42 EXPECT_TRUE((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
43 }
44 {
45 XRPAmount const x{100};
46 EXPECT_EQ(x.value(), 100);
47 EXPECT_TRUE((std::is_same_v<decltype(x)::UnitType, unit::dropTag>));
48 auto const y = 4u * x;
49 EXPECT_EQ(y.value(), 400);
50 EXPECT_TRUE((std::is_same_v<decltype(y)::UnitType, unit::dropTag>));
51
52 FeeLevel64 const f{10};
53 FeeLevel64 const baseFee{100};
54
55 auto drops = mulDiv(baseFee, x, f);
56
57 EXPECT_TRUE(drops);
58 EXPECT_EQ(drops.value(), 1000); // NOLINT(bugprone-unchecked-optional-access)
59 EXPECT_TRUE(
60 (std::is_same_v<std::remove_reference_t<decltype(*drops)>::UnitType, unit::dropTag>));
61 EXPECT_TRUE((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
62 }
63 {
64 FeeLevel64 const x{1024};
65 EXPECT_EQ(x.value(), 1024);
66 EXPECT_TRUE((std::is_same_v<decltype(x)::UnitType, unit::feelevelTag>));
67 std::uint64_t const m = 4;
68 auto const y = m * x;
69 EXPECT_EQ(y.value(), 4096);
70 EXPECT_TRUE((std::is_same_v<decltype(y)::UnitType, unit::feelevelTag>));
71
72 XRPAmount const basefee{10};
73 FeeLevel64 const referencefee{256};
74
75 auto drops = mulDiv(x, basefee, referencefee);
76
77 EXPECT_TRUE(drops);
78 EXPECT_EQ(drops.value(), 40); // NOLINT(bugprone-unchecked-optional-access)
79 EXPECT_TRUE(
80 (std::is_same_v<std::remove_reference_t<decltype(*drops)>::UnitType, unit::dropTag>));
81 EXPECT_TRUE((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
82 }
83}
84
85TEST(UnitsTest, json)
86{
87 // Json value functionality
88 using FeeLevel32 = FeeLevel<std::uint32_t>;
89
90 {
91 FeeLevel32 const x{std::numeric_limits<std::uint32_t>::max()};
92 auto const y = x.jsonClipped();
93 EXPECT_EQ(y.type(), json::ValueType::UInt);
94 EXPECT_EQ(y, json::Value{x.fee()});
95 }
96
97 {
98 FeeLevel32 const x{std::numeric_limits<std::uint32_t>::min()};
99 auto const y = x.jsonClipped();
100 EXPECT_EQ(y.type(), json::ValueType::UInt);
101 EXPECT_EQ(y, json::Value{x.fee()});
102 }
103
104 {
106 auto const y = x.jsonClipped();
107 EXPECT_EQ(y.type(), json::ValueType::UInt);
109 }
110
111 {
113 auto const y = x.jsonClipped();
114 EXPECT_EQ(y.type(), json::ValueType::UInt);
115 EXPECT_EQ(y, json::Value{0});
116 }
117
118 {
120 auto const y = x.jsonClipped();
121 EXPECT_EQ(y.type(), json::ValueType::Real);
123 }
124
125 {
127 auto const y = x.jsonClipped();
128 EXPECT_EQ(y.type(), json::ValueType::Real);
130 }
131
132 {
134 auto const y = x.jsonClipped();
135 EXPECT_EQ(y.type(), json::ValueType::Int);
137 }
138
139 {
141 auto const y = x.jsonClipped();
142 EXPECT_EQ(y.type(), json::ValueType::Int);
144 }
145}
146
147// The ValueUnit operators are templated and mostly unused elsewhere, so each is
148// exercised explicitly. FeeLevel64 is the unsigned-integral instantiation;
149// FeeLevelDouble below repeats the set for the floating-point one, where the
150// modulo and unary-minus cases differ.
151struct FeeLevel64Test : public ::testing::Test
152{
154
155 [[nodiscard]] static FeeLevel64
156 make(auto x)
157 {
158 return x;
159 }
160
161 [[nodiscard]] static FeeLevel64
163 {
164 return FeeLevel64{x};
165 }
166
169};
170
171TEST_F(FeeLevel64Test, construction)
172{
173 [[maybe_unused]]
174 FeeLevel64 const defaulted{};
175
176 FeeLevel64 const test{0};
177 EXPECT_EQ(test.fee(), 0);
178}
179
180TEST_F(FeeLevel64Test, construction_from_zero)
181{
182 FeeLevel64 test{0};
183
184 test = explicitMake(beast::kZero);
185 EXPECT_EQ(test.fee(), 0);
186
188 EXPECT_EQ(test.fee(), 0);
189}
190
191TEST_F(FeeLevel64Test, construction_from_an_unsigned_literal)
192{
193 FeeLevel64 const test = explicitMake(100u);
194 EXPECT_EQ(test.fee(), 100);
195}
196
197TEST_F(FeeLevel64Test, conversion_from_the_same_unit)
198{
199 FeeLevel64 const test = make(targetSame);
200
201 EXPECT_EQ(test.fee(), 200);
202 EXPECT_EQ(test, targetSame);
203 EXPECT_TRUE(test < FeeLevel64{1000});
204 EXPECT_TRUE(test > FeeLevel64{100});
205}
206
207TEST_F(FeeLevel64Test, conversion_from_a_narrower_unit)
208{
209 FeeLevel64 const test = make(targetOther);
210
211 EXPECT_EQ(test.fee(), 300);
212 EXPECT_EQ(test, targetOther);
213}
214
215TEST_F(FeeLevel64Test, assignment_from_raw_integers)
216{
217 FeeLevel64 test{0};
218
219 test = std::uint64_t(200);
220 EXPECT_EQ(test.fee(), 200);
221
222 test = std::uint32_t(300);
223 EXPECT_EQ(test.fee(), 300);
224}
225
226TEST_F(FeeLevel64Test, assignment_from_units)
227{
228 FeeLevel64 test{0};
229
230 test = targetSame;
231 EXPECT_EQ(test.fee(), 200);
232
233 test = targetOther.fee();
234 EXPECT_EQ(test.fee(), 300);
235 EXPECT_EQ(test, targetOther);
236}
237
238TEST_F(FeeLevel64Test, multiplication_and_division)
239{
240 FeeLevel64 test{0};
241
242 test = targetSame * 2;
243 EXPECT_EQ(test.fee(), 400);
244
245 test = 3 * targetSame;
246 EXPECT_EQ(test.fee(), 600);
247
248 test = targetSame / 10;
249 EXPECT_EQ(test.fee(), 20);
250}
251
252TEST_F(FeeLevel64Test, compound_addition_and_subtraction)
253{
254 FeeLevel64 test{20u};
255
256 test += targetSame;
257 EXPECT_EQ(test.fee(), 220);
258
259 test -= targetSame;
260 EXPECT_EQ(test.fee(), 20);
261}
262
263TEST_F(FeeLevel64Test, increment_and_decrement)
264{
265 FeeLevel64 test{20u};
266
267 test++;
268 EXPECT_EQ(test.fee(), 21);
269 ++test;
270 EXPECT_EQ(test.fee(), 22);
271 test--;
272 EXPECT_EQ(test.fee(), 21);
273 --test;
274 EXPECT_EQ(test.fee(), 20);
275}
276
277TEST_F(FeeLevel64Test, compound_multiplication_division_and_modulo)
278{
279 FeeLevel64 test{20u};
280
281 test *= 5;
282 EXPECT_EQ(test.fee(), 100);
283
284 test /= 2;
285 EXPECT_EQ(test.fee(), 50);
286
287 // Modulo is only available on the integral instantiation.
288 test %= 13;
289 EXPECT_EQ(test.fee(), 11);
290}
291
292TEST_F(FeeLevel64Test, truthiness_signum_and_to_string)
293{
294 FeeLevel64 test{11u};
295 EXPECT_TRUE(test);
296
297 test = 0;
298 EXPECT_FALSE(test);
299 EXPECT_EQ(test.signum(), 0);
300
301 test = targetSame;
302 EXPECT_EQ(test.signum(), 1);
303 EXPECT_EQ(to_string(test), "200");
304}
305
306struct FeeLevelDoubleTest : public ::testing::Test
307{
308 [[nodiscard]] static FeeLevelDouble
309 make(auto x)
310 {
311 return x;
312 }
313
314 [[nodiscard]] static FeeLevelDouble
316 {
317 return FeeLevelDouble{x};
318 }
319
322};
323
325{
326 [[maybe_unused]]
327 FeeLevelDouble const defaulted{};
328
329 FeeLevelDouble const test{0};
330 EXPECT_EQ(test.fee(), 0);
331}
332
333TEST_F(FeeLevelDoubleTest, construction_from_zero)
334{
336
337 test = explicitMake(beast::kZero);
338 EXPECT_EQ(test.fee(), 0);
339
341 EXPECT_EQ(test.fee(), 0);
342}
343
344TEST_F(FeeLevelDoubleTest, construction_from_a_floating_literal)
345{
346 FeeLevelDouble const test = explicitMake(100.0);
347 EXPECT_EQ(test.fee(), 100);
348}
349
350TEST_F(FeeLevelDoubleTest, conversion_from_the_same_unit)
351{
352 FeeLevelDouble const test = make(targetSame);
353
354 EXPECT_EQ(test.fee(), 200);
355 EXPECT_EQ(test, targetSame);
356 EXPECT_TRUE(test < FeeLevelDouble{1000.0});
357 EXPECT_TRUE(test > FeeLevelDouble{100.0});
358}
359
360TEST_F(FeeLevelDoubleTest, assignment_from_an_integral_unit)
361{
363
364 test = targetOther.fee();
365 EXPECT_EQ(test.fee(), 300);
366 EXPECT_EQ(test, targetOther);
367}
368
369TEST_F(FeeLevelDoubleTest, assignment_from_raw_numbers)
370{
372
373 test = 200.0;
374 EXPECT_EQ(test.fee(), 200);
375
376 test = std::uint64_t(300);
377 EXPECT_EQ(test.fee(), 300);
378}
379
380TEST_F(FeeLevelDoubleTest, assignment_from_units)
381{
383
384 test = targetSame;
385 EXPECT_EQ(test.fee(), 200);
386}
387
388TEST_F(FeeLevelDoubleTest, multiplication_and_division)
389{
391
392 test = targetSame * 2;
393 EXPECT_EQ(test.fee(), 400);
394
395 test = 3 * targetSame;
396 EXPECT_EQ(test.fee(), 600);
397
398 test = targetSame / 10;
399 EXPECT_EQ(test.fee(), 20);
400}
401
402TEST_F(FeeLevelDoubleTest, compound_addition_and_subtraction)
403{
404 FeeLevelDouble test{20.0};
405
406 test += targetSame;
407 EXPECT_EQ(test.fee(), 220);
408
409 test -= targetSame;
410 EXPECT_EQ(test.fee(), 20);
411}
412
413TEST_F(FeeLevelDoubleTest, increment_and_decrement)
414{
415 FeeLevelDouble test{20.0};
416
417 test++;
418 EXPECT_EQ(test.fee(), 21);
419 ++test;
420 EXPECT_EQ(test.fee(), 22);
421 test--;
422 EXPECT_EQ(test.fee(), 21);
423 --test;
424 EXPECT_EQ(test.fee(), 20);
425}
426
427TEST_F(FeeLevelDoubleTest, compound_multiplication_and_division)
428{
429 // No %=: modulo is not defined for the floating-point instantiation.
430 FeeLevelDouble test{20.0};
431
432 test *= 5;
433 EXPECT_EQ(test.fee(), 100);
434
435 test /= 2;
436 EXPECT_EQ(test.fee(), 50);
437}
438
440{
441 // Only legal on the signed instantiation.
442 FeeLevelDouble test{50.0};
443
444 test = -test;
445 EXPECT_EQ(test.fee(), -50);
446 EXPECT_EQ(test.signum(), -1);
447 EXPECT_EQ(to_string(test), "-50.000000");
448}
449
450TEST_F(FeeLevelDoubleTest, truthiness_signum_and_to_string)
451{
452 FeeLevelDouble test{-50.0};
453 EXPECT_TRUE(test);
454
455 test = 0;
456 EXPECT_FALSE(test);
457 EXPECT_EQ(test.signum(), 0);
458
459 test = targetSame;
460 EXPECT_EQ(test.signum(), 1);
461 EXPECT_EQ(to_string(test), "200.000000");
462}
463
464TEST(UnitsTest, initial_xrp)
465{
466 EXPECT_EQ(kInitialXrp.drops(), 100'000'000'000'000'000);
467 EXPECT_EQ(kInitialXrp, XRPAmount{100'000'000'000'000'000});
468}
469
470} // namespace xrpl::test
Represents a JSON value.
Definition json_value.h:121
json::Value jsonClipped() const
Definition XRPAmount.h:203
constexpr value_type value() const
Returns the underlying value.
Definition XRPAmount.h:225
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:163
constexpr value_type value() const
Returns the underlying value.
Definition Units.h:338
json::Value jsonClipped() const
Definition Units.h:309
T is_same_v
T max(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
JSON (JavaScript Object Notation).
Definition json_errors.h:5
@ UInt
unsigned integer value
Definition json_value.h:28
@ Int
signed integer value
Definition json_value.h:27
@ Real
double value
Definition json_value.h:29
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
TEST(UnitsTest, types)
Definition Units.cpp:16
TEST_F(BaseUintDeathTest, from_raw_size_mismatch)
Definition base_uint.cpp:73
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
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:658
FeeLevel< std::uint64_t > FeeLevel64
Definition Units.h:443
FeeLevel< double > FeeLevelDouble
Definition Units.h:444
unit::ValueUnit< unit::feelevelTag, T > FeeLevel
Definition Units.h:442
constexpr XRPAmount kInitialXrp
Configure the native currency.
FeeLevel64 const targetSame
Definition Units.cpp:167
FeeLevel< std::uint32_t > FeeLevel32
Definition Units.cpp:153
static FeeLevel64 make(auto x)
Definition Units.cpp:156
FeeLevel32 const targetOther
Definition Units.cpp:168
static FeeLevel64 explicitMake(auto x)
Definition Units.cpp:162
FeeLevel64 const targetOther
Definition Units.cpp:321
static FeeLevelDouble make(auto x)
Definition Units.cpp:309
static FeeLevelDouble explicitMake(auto x)
Definition Units.cpp:315
FeeLevelDouble const targetSame
Definition Units.cpp:320