xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
basics
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
14
namespace
xrpl::test
{
15
16
TEST
(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
85
TEST
(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
{
105
FeeLevel64
const
x{
std::numeric_limits<std::uint64_t>::max
()};
106
auto
const
y = x.
jsonClipped
();
107
EXPECT_EQ(y.type(),
json::ValueType::UInt
);
108
EXPECT_EQ(y,
json::Value
{
std::numeric_limits<std::uint32_t>::max
()});
109
}
110
111
{
112
FeeLevel64
const
x{
std::numeric_limits<std::uint64_t>::min
()};
113
auto
const
y = x.
jsonClipped
();
114
EXPECT_EQ(y.type(),
json::ValueType::UInt
);
115
EXPECT_EQ(y,
json::Value
{0});
116
}
117
118
{
119
FeeLevelDouble
const
x{
std::numeric_limits<double>::max
()};
120
auto
const
y = x.
jsonClipped
();
121
EXPECT_EQ(y.type(),
json::ValueType::Real
);
122
EXPECT_EQ(y,
json::Value
{
std::numeric_limits<double>::max
()});
123
}
124
125
{
126
FeeLevelDouble
const
x{
std::numeric_limits<double>::min
()};
127
auto
const
y = x.
jsonClipped
();
128
EXPECT_EQ(y.type(),
json::ValueType::Real
);
129
EXPECT_EQ(y,
json::Value
{
std::numeric_limits<double>::min
()});
130
}
131
132
{
133
XRPAmount
const
x{
std::numeric_limits<std::int64_t>::max
()};
134
auto
const
y = x.
jsonClipped
();
135
EXPECT_EQ(y.type(),
json::ValueType::Int
);
136
EXPECT_EQ(y,
json::Value
{
std::numeric_limits<std::int32_t>::max
()});
137
}
138
139
{
140
XRPAmount
const
x{
std::numeric_limits<std::int64_t>::min
()};
141
auto
const
y = x.
jsonClipped
();
142
EXPECT_EQ(y.type(),
json::ValueType::Int
);
143
EXPECT_EQ(y,
json::Value
{
std::numeric_limits<std::int32_t>::min
()});
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.
151
struct
FeeLevel64Test
:
public
::testing::Test
152
{
153
using
FeeLevel32
=
FeeLevel<std::uint32_t>
;
154
155
[[nodiscard]]
static
FeeLevel64
156
make
(
auto
x)
157
{
158
return
x;
159
}
160
161
[[nodiscard]]
static
FeeLevel64
162
explicitMake
(
auto
x)
163
{
164
return
FeeLevel64
{x};
165
}
166
167
FeeLevel64
const
targetSame
{200u};
168
FeeLevel32
const
targetOther
{300u};
169
};
170
171
TEST_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
180
TEST_F
(
FeeLevel64Test
, construction_from_zero)
181
{
182
FeeLevel64
test
{0};
183
184
test
= explicitMake(
beast::kZero
);
185
EXPECT_EQ(
test
.fee(), 0);
186
187
test
=
beast::kZero
;
188
EXPECT_EQ(
test
.fee(), 0);
189
}
190
191
TEST_F
(
FeeLevel64Test
, construction_from_an_unsigned_literal)
192
{
193
FeeLevel64
const
test
= explicitMake(100u);
194
EXPECT_EQ(
test
.fee(), 100);
195
}
196
197
TEST_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
207
TEST_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
215
TEST_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
226
TEST_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
238
TEST_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
252
TEST_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
263
TEST_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
277
TEST_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
292
TEST_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
306
struct
FeeLevelDoubleTest
:
public
::testing::Test
307
{
308
[[nodiscard]]
static
FeeLevelDouble
309
make
(
auto
x)
310
{
311
return
x;
312
}
313
314
[[nodiscard]]
static
FeeLevelDouble
315
explicitMake
(
auto
x)
316
{
317
return
FeeLevelDouble
{x};
318
}
319
320
FeeLevelDouble
const
targetSame
{200.0};
321
FeeLevel64
const
targetOther
{300};
322
};
323
324
TEST_F
(
FeeLevelDoubleTest
, construction)
325
{
326
[[maybe_unused]]
327
FeeLevelDouble
const
defaulted{};
328
329
FeeLevelDouble
const
test
{0};
330
EXPECT_EQ(
test
.fee(), 0);
331
}
332
333
TEST_F
(
FeeLevelDoubleTest
, construction_from_zero)
334
{
335
FeeLevelDouble
test
{0};
336
337
test
= explicitMake(
beast::kZero
);
338
EXPECT_EQ(
test
.fee(), 0);
339
340
test
=
beast::kZero
;
341
EXPECT_EQ(
test
.fee(), 0);
342
}
343
344
TEST_F
(
FeeLevelDoubleTest
, construction_from_a_floating_literal)
345
{
346
FeeLevelDouble
const
test
= explicitMake(100.0);
347
EXPECT_EQ(
test
.fee(), 100);
348
}
349
350
TEST_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
360
TEST_F
(
FeeLevelDoubleTest
, assignment_from_an_integral_unit)
361
{
362
FeeLevelDouble
test
{0};
363
364
test
= targetOther.fee();
365
EXPECT_EQ(
test
.fee(), 300);
366
EXPECT_EQ(
test
, targetOther);
367
}
368
369
TEST_F
(
FeeLevelDoubleTest
, assignment_from_raw_numbers)
370
{
371
FeeLevelDouble
test
{0};
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
380
TEST_F
(
FeeLevelDoubleTest
, assignment_from_units)
381
{
382
FeeLevelDouble
test
{0};
383
384
test
= targetSame;
385
EXPECT_EQ(
test
.fee(), 200);
386
}
387
388
TEST_F
(
FeeLevelDoubleTest
, multiplication_and_division)
389
{
390
FeeLevelDouble
test
{0};
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
402
TEST_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
413
TEST_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
427
TEST_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
439
TEST_F
(
FeeLevelDoubleTest
, negation)
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
450
TEST_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
464
TEST
(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
json::Value
Represents a JSON value.
Definition
json_value.h:121
xrpl::XRPAmount::jsonClipped
json::Value jsonClipped() const
Definition
XRPAmount.h:203
xrpl::XRPAmount::value
constexpr value_type value() const
Returns the underlying value.
Definition
XRPAmount.h:225
xrpl::XRPAmount::drops
constexpr value_type drops() const
Returns the number of drops.
Definition
XRPAmount.h:163
xrpl::unit::ValueUnit::value
constexpr value_type value() const
Returns the underlying value.
Definition
Units.h:338
xrpl::unit::ValueUnit::jsonClipped
json::Value jsonClipped() const
Definition
Units.h:309
cstdint
std::uint64_t
std::is_same_v
T is_same_v
limits
std::numeric_limits::max
T max(T... args)
std::numeric_limits::min
T min(T... args)
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
json
JSON (JavaScript Object Notation).
Definition
json_errors.h:5
json::ValueType::UInt
@ UInt
unsigned integer value
Definition
json_value.h:28
json::ValueType::Int
@ Int
signed integer value
Definition
json_value.h:27
json::ValueType::Real
@ Real
double value
Definition
json_value.h:29
xrpl::test::jtx::drops
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
Definition
src/test/jtx/amount.h:361
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::TEST
TEST(UnitsTest, types)
Definition
Units.cpp:16
xrpl::test::TEST_F
TEST_F(BaseUintDeathTest, from_raw_size_mismatch)
Definition
base_uint.cpp:73
xrpl::test::XRPAmount
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition
TxTest.h:53
xrpl::mulDiv
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
Definition
libxrpl/basics/mulDiv.cpp:11
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:658
xrpl::FeeLevel64
FeeLevel< std::uint64_t > FeeLevel64
Definition
Units.h:443
xrpl::FeeLevelDouble
FeeLevel< double > FeeLevelDouble
Definition
Units.h:444
xrpl::FeeLevel
unit::ValueUnit< unit::feelevelTag, T > FeeLevel
Definition
Units.h:442
xrpl::kInitialXrp
constexpr XRPAmount kInitialXrp
Configure the native currency.
Definition
SystemParameters.h:31
std::remove_reference_t
xrpl::test::FeeLevel64Test
Definition
Units.cpp:152
xrpl::test::FeeLevel64Test::targetSame
FeeLevel64 const targetSame
Definition
Units.cpp:167
xrpl::test::FeeLevel64Test::FeeLevel32
FeeLevel< std::uint32_t > FeeLevel32
Definition
Units.cpp:153
xrpl::test::FeeLevel64Test::make
static FeeLevel64 make(auto x)
Definition
Units.cpp:156
xrpl::test::FeeLevel64Test::targetOther
FeeLevel32 const targetOther
Definition
Units.cpp:168
xrpl::test::FeeLevel64Test::explicitMake
static FeeLevel64 explicitMake(auto x)
Definition
Units.cpp:162
xrpl::test::FeeLevelDoubleTest
Definition
Units.cpp:307
xrpl::test::FeeLevelDoubleTest::targetOther
FeeLevel64 const targetOther
Definition
Units.cpp:321
xrpl::test::FeeLevelDoubleTest::make
static FeeLevelDouble make(auto x)
Definition
Units.cpp:309
xrpl::test::FeeLevelDoubleTest::explicitMake
static FeeLevelDouble explicitMake(auto x)
Definition
Units.cpp:315
xrpl::test::FeeLevelDoubleTest::targetSame
FeeLevelDouble const targetSame
Definition
Units.cpp:320
type_traits
Generated by
1.17.0