xrpld
Loading...
Searching...
No Matches
Quality_test.cpp
1#include <xrpl/beast/unit_test/suite.h>
2#include <xrpl/beast/utility/Zero.h>
3#include <xrpl/protocol/AccountID.h>
4#include <xrpl/protocol/Issue.h>
5#include <xrpl/protocol/Quality.h>
6#include <xrpl/protocol/STAmount.h>
7#include <xrpl/protocol/UintTypes.h>
8
9#include <cstdint>
10#include <type_traits>
11
12namespace xrpl {
13
15{
16public:
17 // Create a raw, non-integral amount from mantissa and exponent
18 STAmount static raw(std::uint64_t mantissa, int exponent)
19 {
20 return STAmount(Issue{Currency(3), AccountID(3)}, mantissa, exponent);
21 }
22
23 template <class Integer>
24 static STAmount
26 {
27 static_assert(std::is_integral_v<Integer>, "");
28 return STAmount(integer, false);
29 }
30
31 template <class Integer>
32 static STAmount
34 {
35 static_assert(std::is_integral_v<Integer>, "");
36 if (integer < 0)
37 return STAmount(-integer, true);
38 return STAmount(integer, false);
39 }
40
41 template <class In, class Out>
42 static Amounts
43 amounts(In in, Out out)
44 {
45 return Amounts(amount(in), amount(out));
46 }
47
48 template <class In1, class Out1, class Int, class In2, class Out2>
49 void
50 ceilIn(Quality const& q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
51 {
52 auto expectResult(amounts(inExpected, outExpected));
53 auto actualResult(q.ceilIn(amounts(in, out), amount(limit)));
54
55 BEAST_EXPECT(actualResult == expectResult);
56 }
57
58 template <class In1, class Out1, class Int, class In2, class Out2>
59 void
60 ceilOut(Quality const& q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
61 {
62 auto const expectResult(amounts(inExpected, outExpected));
63 auto const actualResult(q.ceilOut(amounts(in, out), amount(limit)));
64
65 BEAST_EXPECT(actualResult == expectResult);
66 }
67
68 void
70 {
71 testcase("ceil_in");
72
73 {
74 // 1 in, 1 out:
75 Quality const q(Amounts(amount(1), amount(1)));
76
77 ceilIn(
78 q,
79 1,
80 1, // 1 in, 1 out
81 1, // limit: 1
82 1,
83 1); // 1 in, 1 out
84
85 ceilIn(
86 q,
87 10,
88 10, // 10 in, 10 out
89 5, // limit: 5
90 5,
91 5); // 5 in, 5 out
92
93 ceilIn(
94 q,
95 5,
96 5, // 5 in, 5 out
97 10, // limit: 10
98 5,
99 5); // 5 in, 5 out
100 }
101
102 {
103 // 1 in, 2 out:
104 Quality const q(Amounts(amount(1), amount(2)));
105
106 ceilIn(
107 q,
108 40,
109 80, // 40 in, 80 out
110 40, // limit: 40
111 40,
112 80); // 40 in, 20 out
113
114 ceilIn(
115 q,
116 40,
117 80, // 40 in, 80 out
118 20, // limit: 20
119 20,
120 40); // 20 in, 40 out
121
122 ceilIn(
123 q,
124 40,
125 80, // 40 in, 80 out
126 60, // limit: 60
127 40,
128 80); // 40 in, 80 out
129 }
130
131 {
132 // 2 in, 1 out:
133 Quality const q(Amounts(amount(2), amount(1)));
134
135 ceilIn(
136 q,
137 40,
138 20, // 40 in, 20 out
139 20, // limit: 20
140 20,
141 10); // 20 in, 10 out
142
143 ceilIn(
144 q,
145 40,
146 20, // 40 in, 20 out
147 40, // limit: 40
148 40,
149 20); // 40 in, 20 out
150
151 ceilIn(
152 q,
153 40,
154 20, // 40 in, 20 out
155 50, // limit: 40
156 40,
157 20); // 40 in, 20 out
158 }
159 }
160
161 void
163 {
164 testcase("ceil_out");
165
166 {
167 // 1 in, 1 out:
168 Quality const q(Amounts(amount(1), amount(1)));
169
170 ceilOut(
171 q,
172 1,
173 1, // 1 in, 1 out
174 1, // limit 1
175 1,
176 1); // 1 in, 1 out
177
178 ceilOut(
179 q,
180 10,
181 10, // 10 in, 10 out
182 5, // limit 5
183 5,
184 5); // 5 in, 5 out
185
186 ceilOut(
187 q,
188 10,
189 10, // 10 in, 10 out
190 20, // limit 20
191 10,
192 10); // 10 in, 10 out
193 }
194
195 {
196 // 1 in, 2 out:
197 Quality const q(Amounts(amount(1), amount(2)));
198
199 ceilOut(
200 q,
201 40,
202 80, // 40 in, 80 out
203 40, // limit 40
204 20,
205 40); // 20 in, 40 out
206
207 ceilOut(
208 q,
209 40,
210 80, // 40 in, 80 out
211 80, // limit 80
212 40,
213 80); // 40 in, 80 out
214
215 ceilOut(
216 q,
217 40,
218 80, // 40 in, 80 out
219 100, // limit 100
220 40,
221 80); // 40 in, 80 out
222 }
223
224 {
225 // 2 in, 1 out:
226 Quality const q(Amounts(amount(2), amount(1)));
227
228 ceilOut(
229 q,
230 40,
231 20, // 40 in, 20 out
232 20, // limit 20
233 40,
234 20); // 40 in, 20 out
235
236 ceilOut(
237 q,
238 40,
239 20, // 40 in, 20 out
240 40, // limit 40
241 40,
242 20); // 40 in, 20 out
243
244 ceilOut(
245 q,
246 40,
247 20, // 40 in, 20 out
248 10, // limit 10
249 20,
250 10); // 20 in, 10 out
251 }
252 }
253
254 void
256 {
257 testcase("raw");
258
259 {
260 Quality const q(0x5d048191fb9130daull); // 126836389.7680090
261 Amounts const value(
262 amount(349469768), // 349.469768 XRP
263 raw(2755280000000000ull, -15)); // 2.75528
264 STAmount const limit(raw(4131113916555555, -16)); // .4131113916555555
265 Amounts const result(q.ceilOut(value, limit));
266 BEAST_EXPECT(result.in != beast::kZero);
267 }
268 }
269
270 void
272 {
273 testcase("round");
274
275 Quality const q(0x59148191fb913522ull); // 57719.63525051682
276 BEAST_EXPECT(q.round(3).rate().getText() == "57800");
277 BEAST_EXPECT(q.round(4).rate().getText() == "57720");
278 BEAST_EXPECT(q.round(5).rate().getText() == "57720");
279 BEAST_EXPECT(q.round(6).rate().getText() == "57719.7");
280 BEAST_EXPECT(q.round(7).rate().getText() == "57719.64");
281 BEAST_EXPECT(q.round(8).rate().getText() == "57719.636");
282 BEAST_EXPECT(q.round(9).rate().getText() == "57719.6353");
283 BEAST_EXPECT(q.round(10).rate().getText() == "57719.63526");
284 BEAST_EXPECT(q.round(11).rate().getText() == "57719.635251");
285 BEAST_EXPECT(q.round(12).rate().getText() == "57719.6352506");
286 BEAST_EXPECT(q.round(13).rate().getText() == "57719.63525052");
287 BEAST_EXPECT(q.round(14).rate().getText() == "57719.635250517");
288 BEAST_EXPECT(q.round(15).rate().getText() == "57719.6352505169");
289 BEAST_EXPECT(q.round(16).rate().getText() == "57719.63525051682");
290 }
291
292 void
294 {
295 testcase("comparisons");
296
297 STAmount const amount1(noIssue(), 231);
298 STAmount const amount2(noIssue(), 462);
299 STAmount const amount3(noIssue(), 924);
300
301 Quality const q11(Amounts(amount1, amount1));
302 Quality const q12(Amounts(amount1, amount2));
303 Quality const q13(Amounts(amount1, amount3));
304 Quality const q21(Amounts(amount2, amount1));
305 Quality const q31(Amounts(amount3, amount1));
306
307 BEAST_EXPECT(q11 == q11);
308 BEAST_EXPECT(q11 < q12);
309 BEAST_EXPECT(q12 < q13);
310 BEAST_EXPECT(q31 < q21);
311 BEAST_EXPECT(q21 < q11);
312 BEAST_EXPECT(q11 >= q11);
313 BEAST_EXPECT(q12 >= q11);
314 BEAST_EXPECT(q13 >= q12);
315 BEAST_EXPECT(q21 >= q31);
316 BEAST_EXPECT(q11 >= q21);
317 BEAST_EXPECT(q12 > q11);
318 BEAST_EXPECT(q13 > q12);
319 BEAST_EXPECT(q21 > q31);
320 BEAST_EXPECT(q11 > q21);
321 BEAST_EXPECT(q11 <= q11);
322 BEAST_EXPECT(q11 <= q12);
323 BEAST_EXPECT(q12 <= q13);
324 BEAST_EXPECT(q31 <= q21);
325 BEAST_EXPECT(q21 <= q11);
326 BEAST_EXPECT(q31 != q21);
327 }
328
329 void
331 {
332 testcase("composition");
333
334 STAmount const amount1(noIssue(), 231);
335 STAmount const amount2(noIssue(), 462);
336 STAmount const amount3(noIssue(), 924);
337
338 Quality const q11(Amounts(amount1, amount1));
339 Quality const q12(Amounts(amount1, amount2));
340 Quality const q13(Amounts(amount1, amount3));
341 Quality const q21(Amounts(amount2, amount1));
342 Quality const q31(Amounts(amount3, amount1));
343
344 BEAST_EXPECT(composedQuality(q12, q21) == q11);
345
346 Quality const q1331(composedQuality(q13, q31));
347 Quality const q3113(composedQuality(q31, q13));
348
349 BEAST_EXPECT(q1331 == q3113);
350 BEAST_EXPECT(q1331 == q11);
351 }
352
353 void
355 {
356 testcase("operations");
357
358 Quality const q11(Amounts(STAmount(noIssue(), 731), STAmount(noIssue(), 731)));
359
360 Quality qa(q11);
361 Quality qb(q11);
362
363 BEAST_EXPECT(qa == qb);
364 BEAST_EXPECT(++qa != q11);
365 BEAST_EXPECT(qa != qb);
366 BEAST_EXPECT(--qb != q11);
367 BEAST_EXPECT(qa != qb);
368 BEAST_EXPECT(qb < qa);
369 BEAST_EXPECT(qb++ < qa);
370 BEAST_EXPECT(qb++ < qa);
371 BEAST_EXPECT(qb++ == qa);
372 BEAST_EXPECT(qa < qb);
373 }
374 void
375 run() override
376 {
380 testCeilIn();
381 testCeilOut();
382 testRaw();
383 testRound();
384 }
385};
386
388
389} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
A currency issued by an account.
Definition Issue.h:13
static STAmount raw(std::uint64_t mantissa, int exponent)
static STAmount amount(Integer integer, std::enable_if_t< std::is_signed_v< Integer > > *=0)
void ceilOut(Quality const &q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
void run() override
Runs the suite.
void ceilIn(Quality const &q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
static Amounts amounts(In in, Out out)
static STAmount amount(Integer integer, std::enable_if_t<!std::is_signed_v< Integer > > *=0)
Represents the logical ratio of output currency to input currency.
Definition Quality.h:91
STAmount rate() const
Returns the quality as STAmount.
Definition Quality.h:149
Quality round(int tickSize) const
Returns the quality rounded up to the specified number of decimal digits.
Definition Quality.cpp:134
Amounts ceilOut(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with out capped.
Definition Quality.cpp:102
Amounts ceilIn(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with in capped.
Definition Quality.cpp:73
std::string getText() const override
Definition STAmount.cpp:646
T is_integral_v
T is_signed_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:64
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:105
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
Quality composedQuality(Quality const &lhs, Quality const &rhs)
Calculate the quality of a two-hop path given the two hops.
Definition Quality.cpp:114