xrpld
Loading...
Searching...
No Matches
Issue_test.cpp
1#include <xrpl/basics/UnorderedContainers.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/beast/unit_test/suite.h>
4#include <xrpl/protocol/AccountID.h>
5#include <xrpl/protocol/Book.h>
6#include <xrpl/protocol/Issue.h>
7#include <xrpl/protocol/UintTypes.h>
8
9#include <functional>
10#include <map>
11#include <optional>
12#include <set>
13#include <utility>
14
15#if BEAST_MSVC
16#define STL_SET_HAS_EMPLACE 1
17#else
18#define STL_SET_HAS_EMPLACE 0
19#endif
20
21namespace xrpl {
22
24{
25public:
26 using Domain = uint256;
27
28 // Comparison, hash tests for uint60 (via base_uint)
29 template <typename Unsigned>
30 void
32 {
33 Unsigned const u1(1);
34 Unsigned const u2(2);
35 Unsigned const u3(3);
36
37 BEAST_EXPECT(u1 != u2);
38 BEAST_EXPECT(u1 < u2);
39 BEAST_EXPECT(u1 <= u2);
40 BEAST_EXPECT(u2 <= u2);
41 BEAST_EXPECT(u2 == u2);
42 BEAST_EXPECT(u2 >= u2);
43 BEAST_EXPECT(u3 >= u2);
44 BEAST_EXPECT(u3 > u2);
45
46 std::hash<Unsigned> const hash;
47
48 BEAST_EXPECT(hash(u1) == hash(u1));
49 BEAST_EXPECT(hash(u2) == hash(u2));
50 BEAST_EXPECT(hash(u3) == hash(u3));
51 BEAST_EXPECT(hash(u1) != hash(u2));
52 BEAST_EXPECT(hash(u1) != hash(u3));
53 BEAST_EXPECT(hash(u2) != hash(u3));
54 }
55
56 //--------------------------------------------------------------------------
57
58 // Comparison, hash tests for Issue
59 template <class Issue>
60 void
62 {
63 Currency const c1(1);
64 AccountID const i1(1);
65 Currency const c2(2);
66 AccountID const i2(2);
67 Currency const c3(3);
68 AccountID const i3(3);
69
70 BEAST_EXPECT(Issue(c1, i1) != Issue(c2, i1));
71 BEAST_EXPECT(Issue(c1, i1) < Issue(c2, i1));
72 BEAST_EXPECT(Issue(c1, i1) <= Issue(c2, i1));
73 BEAST_EXPECT(Issue(c2, i1) <= Issue(c2, i1));
74 BEAST_EXPECT(Issue(c2, i1) == Issue(c2, i1));
75 BEAST_EXPECT(Issue(c2, i1) >= Issue(c2, i1));
76 BEAST_EXPECT(Issue(c3, i1) >= Issue(c2, i1));
77 BEAST_EXPECT(Issue(c3, i1) > Issue(c2, i1));
78 BEAST_EXPECT(Issue(c1, i1) != Issue(c1, i2));
79 BEAST_EXPECT(Issue(c1, i1) < Issue(c1, i2));
80 BEAST_EXPECT(Issue(c1, i1) <= Issue(c1, i2));
81 BEAST_EXPECT(Issue(c1, i2) <= Issue(c1, i2));
82 BEAST_EXPECT(Issue(c1, i2) == Issue(c1, i2));
83 BEAST_EXPECT(Issue(c1, i2) >= Issue(c1, i2));
84 BEAST_EXPECT(Issue(c1, i3) >= Issue(c1, i2));
85 BEAST_EXPECT(Issue(c1, i3) > Issue(c1, i2));
86
87 std::hash<Issue> const hash;
88
89 BEAST_EXPECT(hash(Issue(c1, i1)) == hash(Issue(c1, i1)));
90 BEAST_EXPECT(hash(Issue(c1, i2)) == hash(Issue(c1, i2)));
91 BEAST_EXPECT(hash(Issue(c1, i3)) == hash(Issue(c1, i3)));
92 BEAST_EXPECT(hash(Issue(c2, i1)) == hash(Issue(c2, i1)));
93 BEAST_EXPECT(hash(Issue(c2, i2)) == hash(Issue(c2, i2)));
94 BEAST_EXPECT(hash(Issue(c2, i3)) == hash(Issue(c2, i3)));
95 BEAST_EXPECT(hash(Issue(c3, i1)) == hash(Issue(c3, i1)));
96 BEAST_EXPECT(hash(Issue(c3, i2)) == hash(Issue(c3, i2)));
97 BEAST_EXPECT(hash(Issue(c3, i3)) == hash(Issue(c3, i3)));
98 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c1, i2)));
99 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c1, i3)));
100 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c2, i1)));
101 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c2, i2)));
102 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c2, i3)));
103 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c3, i1)));
104 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c3, i2)));
105 BEAST_EXPECT(hash(Issue(c1, i1)) != hash(Issue(c3, i3)));
106 }
107
108 template <class Set>
109 void
111 {
112 Currency const c1(1);
113 AccountID const i1(1);
114 Currency const c2(2);
115 AccountID const i2(2);
116 Issue const a1(c1, i1);
117 Issue const a2(c2, i2);
118
119 {
120 Set c;
121
122 c.insert(a1);
123 if (!BEAST_EXPECT(c.size() == 1))
124 return;
125 c.insert(a2);
126 if (!BEAST_EXPECT(c.size() == 2))
127 return;
128
129 if (!BEAST_EXPECT(c.erase(Issue(c1, i2)) == 0))
130 return;
131 if (!BEAST_EXPECT(c.erase(Issue(c1, i1)) == 1))
132 return;
133 if (!BEAST_EXPECT(c.erase(Issue(c2, i2)) == 1))
134 return;
135 if (!BEAST_EXPECT(c.empty()))
136 return;
137 }
138
139 {
140 Set c;
141
142 c.insert(a1);
143 if (!BEAST_EXPECT(c.size() == 1))
144 return;
145 c.insert(a2);
146 if (!BEAST_EXPECT(c.size() == 2))
147 return;
148
149 if (!BEAST_EXPECT(c.erase(Issue(c1, i2)) == 0))
150 return;
151 if (!BEAST_EXPECT(c.erase(Issue(c1, i1)) == 1))
152 return;
153 if (!BEAST_EXPECT(c.erase(Issue(c2, i2)) == 1))
154 return;
155 if (!BEAST_EXPECT(c.empty()))
156 return;
157
158#if STL_SET_HAS_EMPLACE
159 c.emplace(c1, i1);
160 if (!BEAST_EXPECT(c.size() == 1))
161 return;
162 c.emplace(c2, i2);
163 if (!BEAST_EXPECT(c.size() == 2))
164 return;
165#endif
166 }
167 }
168
169 template <class Map>
170 void
172 {
173 Currency const c1(1);
174 AccountID const i1(1);
175 Currency const c2(2);
176 AccountID const i2(2);
177 Issue const a1(c1, i1);
178 Issue const a2(c2, i2);
179
180 {
181 Map c;
182
183 c.insert(std::make_pair(a1, 1));
184 if (!BEAST_EXPECT(c.size() == 1))
185 return;
186 c.insert(std::make_pair(a2, 2));
187 if (!BEAST_EXPECT(c.size() == 2))
188 return;
189
190 if (!BEAST_EXPECT(c.erase(Issue(c1, i2)) == 0))
191 return;
192 if (!BEAST_EXPECT(c.erase(Issue(c1, i1)) == 1))
193 return;
194 if (!BEAST_EXPECT(c.erase(Issue(c2, i2)) == 1))
195 return;
196 if (!BEAST_EXPECT(c.empty()))
197 return;
198 }
199
200 {
201 Map c;
202
203 c.insert(std::make_pair(a1, 1));
204 if (!BEAST_EXPECT(c.size() == 1))
205 return;
206 c.insert(std::make_pair(a2, 2));
207 if (!BEAST_EXPECT(c.size() == 2))
208 return;
209
210 if (!BEAST_EXPECT(c.erase(Issue(c1, i2)) == 0))
211 return;
212 if (!BEAST_EXPECT(c.erase(Issue(c1, i1)) == 1))
213 return;
214 if (!BEAST_EXPECT(c.erase(Issue(c2, i2)) == 1))
215 return;
216 if (!BEAST_EXPECT(c.empty()))
217 return;
218 }
219 }
220
221 template <class Set>
222 void
224 {
225 Currency const c1(1);
226 AccountID const i1(1);
227 Currency const c2(2);
228 AccountID const i2(2);
229 Issue const a1(c1, i1);
230 Issue const a2(c2, i2);
231 uint256 const domain1{1};
232 uint256 const domain2{2};
233
234 Set c;
235
236 c.insert(std::make_pair(a1, domain1));
237 if (!BEAST_EXPECT(c.size() == 1))
238 return;
239 c.insert(std::make_pair(a2, domain1));
240 if (!BEAST_EXPECT(c.size() == 2))
241 return;
242 c.insert(std::make_pair(a2, domain2));
243 if (!BEAST_EXPECT(c.size() == 3))
244 return;
245
246 if (!BEAST_EXPECT(c.erase(std::make_pair(Issue(c1, i2), domain1)) == 0))
247 return;
248 if (!BEAST_EXPECT(c.erase(std::make_pair(a1, domain1)) == 1))
249 return;
250 if (!BEAST_EXPECT(c.erase(std::make_pair(a2, domain1)) == 1))
251 return;
252 if (!BEAST_EXPECT(c.erase(std::make_pair(a2, domain2)) == 1))
253 return;
254 if (!BEAST_EXPECT(c.empty()))
255 return;
256 }
257
258 template <class Map>
259 void
261 {
262 Currency const c1(1);
263 AccountID const i1(1);
264 Currency const c2(2);
265 AccountID const i2(2);
266 Issue const a1(c1, i1);
267 Issue const a2(c2, i2);
268 uint256 const domain1{1};
269 uint256 const domain2{2};
270
271 Map c;
272
273 c.insert(std::make_pair(std::make_pair(a1, domain1), 1));
274 if (!BEAST_EXPECT(c.size() == 1))
275 return;
276 c.insert(std::make_pair(std::make_pair(a2, domain1), 2));
277 if (!BEAST_EXPECT(c.size() == 2))
278 return;
279 c.insert(std::make_pair(std::make_pair(a2, domain2), 2));
280 if (!BEAST_EXPECT(c.size() == 3))
281 return;
282
283 if (!BEAST_EXPECT(c.erase(std::make_pair(Issue(c1, i2), domain1)) == 0))
284 return;
285 if (!BEAST_EXPECT(c.erase(std::make_pair(a1, domain1)) == 1))
286 return;
287 if (!BEAST_EXPECT(c.erase(std::make_pair(a2, domain1)) == 1))
288 return;
289 if (!BEAST_EXPECT(c.erase(std::make_pair(a2, domain2)) == 1))
290 return;
291 if (!BEAST_EXPECT(c.empty()))
292 return;
293 }
294
295 void
297 {
298 testcase("std::set <std::pair<Issue, Domain>>");
300
301 testcase("std::set <std::pair<Issue, Domain>>");
303
304 testcase("hash_set <std::pair<Issue, Domain>>");
306
307 testcase("hash_set <std::pair<Issue, Domain>>");
309 }
310
311 void
313 {
314 testcase("std::map <std::pair<Issue, Domain>, int>");
316
317 testcase("std::map <std::pair<Issue, Domain>, int>");
319
320#if XRPL_ASSETS_ENABLE_STD_HASH
321 testcase("hash_map <std::pair<Issue, Domain>, int>");
323
324 testcase("hash_map <std::pair<Issue, Domain>, int>");
326
327 testcase("hardened_hash_map <std::pair<Issue, Domain>, int>");
329
330 testcase("hardened_hash_map <std::pair<Issue, Domain>, int>");
332#endif
333 }
334
335 void
337 {
338 testcase("std::set <Issue>");
340
341 testcase("std::set <Issue>");
343
344#if XRPL_ASSETS_ENABLE_STD_HASH
345 testcase("std::unordered_set <Issue>");
347
348 testcase("std::unordered_set <Issue>");
350#endif
351
352 testcase("hash_set <Issue>");
354
355 testcase("hash_set <Issue>");
357 }
358
359 void
361 {
362 testcase("std::map <Issue, int>");
364
365 testcase("std::map <Issue, int>");
367
368#if XRPL_ASSETS_ENABLE_STD_HASH
369 testcase("std::unordered_map <Issue, int>");
371
372 testcase("std::unordered_map <Issue, int>");
374
375 testcase("hash_map <Issue, int>");
377
378 testcase("hash_map <Issue, int>");
380
381#endif
382 }
383
384 //--------------------------------------------------------------------------
385
386 // Comparison, hash tests for Book
387 template <class Book>
388 void
390 {
391 Currency const c1(1);
392 AccountID const i1(1);
393 Currency const c2(2);
394 AccountID const i2(2);
395 Currency const c3(3);
396 AccountID const i3(3);
397
398 Issue const a1(c1, i1);
399 Issue const a2(c1, i2);
400 Issue const a3(c2, i2);
401 Issue const a4(c3, i2);
402 uint256 const domain1{1};
403 uint256 const domain2{2};
404
405 // Books without domains
406 BEAST_EXPECT(Book(a1, a2, std::nullopt) != Book(a2, a3, std::nullopt));
407 BEAST_EXPECT(Book(a1, a2, std::nullopt) < Book(a2, a3, std::nullopt));
408 BEAST_EXPECT(Book(a1, a2, std::nullopt) <= Book(a2, a3, std::nullopt));
409 BEAST_EXPECT(Book(a2, a3, std::nullopt) <= Book(a2, a3, std::nullopt));
410 BEAST_EXPECT(Book(a2, a3, std::nullopt) == Book(a2, a3, std::nullopt));
411 BEAST_EXPECT(Book(a2, a3, std::nullopt) >= Book(a2, a3, std::nullopt));
412 BEAST_EXPECT(Book(a3, a4, std::nullopt) >= Book(a2, a3, std::nullopt));
413 BEAST_EXPECT(Book(a3, a4, std::nullopt) > Book(a2, a3, std::nullopt));
414
415 // test domain books
416 {
417 // Books with different domains
418 BEAST_EXPECT(Book(a2, a3, domain1) != Book(a2, a3, domain2));
419 BEAST_EXPECT(Book(a2, a3, domain1) < Book(a2, a3, domain2));
420 BEAST_EXPECT(Book(a2, a3, domain2) > Book(a2, a3, domain1));
421
422 // One Book has a domain, the other does not
423 BEAST_EXPECT(Book(a2, a3, domain1) != Book(a2, a3, std::nullopt));
424 BEAST_EXPECT(Book(a2, a3, std::nullopt) < Book(a2, a3, domain1));
425 BEAST_EXPECT(Book(a2, a3, domain1) > Book(a2, a3, std::nullopt));
426
427 // Both Books have the same domain
428 BEAST_EXPECT(Book(a2, a3, domain1) == Book(a2, a3, domain1));
429 BEAST_EXPECT(Book(a2, a3, domain2) == Book(a2, a3, domain2));
430 BEAST_EXPECT(Book(a2, a3, std::nullopt) == Book(a2, a3, std::nullopt));
431
432 // Both Books have no domain
433 BEAST_EXPECT(Book(a2, a3, std::nullopt) == Book(a2, a3, std::nullopt));
434
435 // Testing comparisons with >= and <=
436
437 // When comparing books with domain1 vs domain2
438 BEAST_EXPECT(Book(a2, a3, domain1) <= Book(a2, a3, domain2));
439 BEAST_EXPECT(Book(a2, a3, domain2) >= Book(a2, a3, domain1));
440 BEAST_EXPECT(Book(a2, a3, domain1) >= Book(a2, a3, domain1));
441 BEAST_EXPECT(Book(a2, a3, domain2) <= Book(a2, a3, domain2));
442
443 // One Book has domain1 and the other has no domain
444 BEAST_EXPECT(Book(a2, a3, domain1) > Book(a2, a3, std::nullopt));
445 BEAST_EXPECT(Book(a2, a3, std::nullopt) < Book(a2, a3, domain1));
446
447 // One Book has domain2 and the other has no domain
448 BEAST_EXPECT(Book(a2, a3, domain2) > Book(a2, a3, std::nullopt));
449 BEAST_EXPECT(Book(a2, a3, std::nullopt) < Book(a2, a3, domain2));
450
451 // Comparing two Books with no domains
452 BEAST_EXPECT(Book(a2, a3, std::nullopt) <= Book(a2, a3, std::nullopt));
453 BEAST_EXPECT(Book(a2, a3, std::nullopt) >= Book(a2, a3, std::nullopt));
454
455 // Test case where domain1 is less than domain2
456 BEAST_EXPECT(Book(a2, a3, domain1) <= Book(a2, a3, domain2));
457 BEAST_EXPECT(Book(a2, a3, domain2) >= Book(a2, a3, domain1));
458
459 // Test case where domain2 is equal to domain1
460 BEAST_EXPECT(Book(a2, a3, domain1) >= Book(a2, a3, domain1));
461 BEAST_EXPECT(Book(a2, a3, domain1) <= Book(a2, a3, domain1));
462
463 // More test cases involving a4 (with domain2)
464
465 // Comparing Book with domain2 (a4) to a Book with domain1
466 BEAST_EXPECT(Book(a2, a3, domain1) < Book(a3, a4, domain2));
467 BEAST_EXPECT(Book(a3, a4, domain2) > Book(a2, a3, domain1));
468
469 // Comparing Book with domain2 (a4) to a Book with no domain
470 BEAST_EXPECT(Book(a3, a4, domain2) > Book(a2, a3, std::nullopt));
471 BEAST_EXPECT(Book(a2, a3, std::nullopt) < Book(a3, a4, domain2));
472
473 // Comparing Book with domain2 (a4) to a Book with the same domain
474 BEAST_EXPECT(Book(a3, a4, domain2) == Book(a3, a4, domain2));
475
476 // Comparing Book with domain2 (a4) to a Book with domain1
477 BEAST_EXPECT(Book(a2, a3, domain1) < Book(a3, a4, domain2));
478 BEAST_EXPECT(Book(a3, a4, domain2) > Book(a2, a3, domain1));
479 }
480
481 std::hash<Book> const hash;
482
483 // log << std::hex << hash (Book (a1, a2));
484 // log << std::hex << hash (Book (a1, a2));
485 //
486 // log << std::hex << hash (Book (a1, a3));
487 // log << std::hex << hash (Book (a1, a3));
488 //
489 // log << std::hex << hash (Book (a1, a4));
490 // log << std::hex << hash (Book (a1, a4));
491 //
492 // log << std::hex << hash (Book (a2, a3));
493 // log << std::hex << hash (Book (a2, a3));
494 //
495 // log << std::hex << hash (Book (a2, a4));
496 // log << std::hex << hash (Book (a2, a4));
497 //
498 // log << std::hex << hash (Book (a3, a4));
499 // log << std::hex << hash (Book (a3, a4));
500
501 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) == hash(Book(a1, a2, std::nullopt)));
502 BEAST_EXPECT(hash(Book(a1, a3, std::nullopt)) == hash(Book(a1, a3, std::nullopt)));
503 BEAST_EXPECT(hash(Book(a1, a4, std::nullopt)) == hash(Book(a1, a4, std::nullopt)));
504 BEAST_EXPECT(hash(Book(a2, a3, std::nullopt)) == hash(Book(a2, a3, std::nullopt)));
505 BEAST_EXPECT(hash(Book(a2, a4, std::nullopt)) == hash(Book(a2, a4, std::nullopt)));
506 BEAST_EXPECT(hash(Book(a3, a4, std::nullopt)) == hash(Book(a3, a4, std::nullopt)));
507
508 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a1, a3, std::nullopt)));
509 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a1, a4, std::nullopt)));
510 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a2, a3, std::nullopt)));
511 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a2, a4, std::nullopt)));
512 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a3, a4, std::nullopt)));
513
514 // Books with domain
515 BEAST_EXPECT(hash(Book(a1, a2, domain1)) == hash(Book(a1, a2, domain1)));
516 BEAST_EXPECT(hash(Book(a1, a3, domain1)) == hash(Book(a1, a3, domain1)));
517 BEAST_EXPECT(hash(Book(a1, a4, domain1)) == hash(Book(a1, a4, domain1)));
518 BEAST_EXPECT(hash(Book(a2, a3, domain1)) == hash(Book(a2, a3, domain1)));
519 BEAST_EXPECT(hash(Book(a2, a4, domain1)) == hash(Book(a2, a4, domain1)));
520 BEAST_EXPECT(hash(Book(a3, a4, domain1)) == hash(Book(a3, a4, domain1)));
521 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) == hash(Book(a1, a2, std::nullopt)));
522
523 // Comparing Books with domain1 vs no domain
524 BEAST_EXPECT(hash(Book(a1, a2, std::nullopt)) != hash(Book(a1, a2, domain1)));
525 BEAST_EXPECT(hash(Book(a1, a3, std::nullopt)) != hash(Book(a1, a3, domain1)));
526 BEAST_EXPECT(hash(Book(a1, a4, std::nullopt)) != hash(Book(a1, a4, domain1)));
527 BEAST_EXPECT(hash(Book(a2, a3, std::nullopt)) != hash(Book(a2, a3, domain1)));
528 BEAST_EXPECT(hash(Book(a2, a4, std::nullopt)) != hash(Book(a2, a4, domain1)));
529 BEAST_EXPECT(hash(Book(a3, a4, std::nullopt)) != hash(Book(a3, a4, domain1)));
530
531 // Books with domain1 but different Issues
532 BEAST_EXPECT(hash(Book(a1, a2, domain1)) != hash(Book(a1, a3, domain1)));
533 BEAST_EXPECT(hash(Book(a1, a2, domain1)) != hash(Book(a1, a4, domain1)));
534 BEAST_EXPECT(hash(Book(a2, a3, domain1)) != hash(Book(a2, a4, domain1)));
535 BEAST_EXPECT(hash(Book(a1, a2, domain1)) != hash(Book(a2, a3, domain1)));
536 BEAST_EXPECT(hash(Book(a2, a4, domain1)) != hash(Book(a3, a4, domain1)));
537 BEAST_EXPECT(hash(Book(a3, a4, domain1)) != hash(Book(a1, a4, domain1)));
538
539 // Books with domain1 and domain2
540 BEAST_EXPECT(hash(Book(a1, a2, domain1)) != hash(Book(a1, a2, domain2)));
541 BEAST_EXPECT(hash(Book(a1, a3, domain1)) != hash(Book(a1, a3, domain2)));
542 BEAST_EXPECT(hash(Book(a1, a4, domain1)) != hash(Book(a1, a4, domain2)));
543 BEAST_EXPECT(hash(Book(a2, a3, domain1)) != hash(Book(a2, a3, domain2)));
544 BEAST_EXPECT(hash(Book(a2, a4, domain1)) != hash(Book(a2, a4, domain2)));
545 BEAST_EXPECT(hash(Book(a3, a4, domain1)) != hash(Book(a3, a4, domain2)));
546 }
547
548 //--------------------------------------------------------------------------
549
550 template <class Set>
551 void
553 {
554 Currency const c1(1);
555 AccountID const i1(1);
556 Currency const c2(2);
557 AccountID const i2(2);
558 Issue const a1(c1, i1);
559 Issue const a2(c2, i2);
560 Book const b1(a1, a2, std::nullopt);
561 Book const b2(a2, a1, std::nullopt);
562
563 uint256 const domain1{1};
564 uint256 const domain2{2};
565
566 Book const b1D1(a1, a2, domain1);
567 Book const b2D1(a2, a1, domain1);
568 Book const b1D2(a1, a2, domain2);
569 Book const b2D2(a2, a1, domain2);
570
571 {
572 Set c;
573
574 c.insert(b1);
575 if (!BEAST_EXPECT(c.size() == 1))
576 return;
577 c.insert(b2);
578 if (!BEAST_EXPECT(c.size() == 2))
579 return;
580
581 if (!BEAST_EXPECT(c.erase(Book(a1, a1, std::nullopt)) == 0))
582 return;
583 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
584 return;
585 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
586 return;
587 if (!BEAST_EXPECT(c.empty()))
588 return;
589 }
590
591 {
592 Set c;
593
594 c.insert(b1);
595 if (!BEAST_EXPECT(c.size() == 1))
596 return;
597 c.insert(b2);
598 if (!BEAST_EXPECT(c.size() == 2))
599 return;
600
601 if (!BEAST_EXPECT(c.erase(Book(a1, a1, std::nullopt)) == 0))
602 return;
603 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
604 return;
605 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
606 return;
607 if (!BEAST_EXPECT(c.empty()))
608 return;
609
610#if STL_SET_HAS_EMPLACE
611 c.emplace(a1, a2);
612 if (!BEAST_EXPECT(c.size() == 1))
613 return;
614 c.emplace(a2, a1);
615 if (!BEAST_EXPECT(c.size() == 2))
616 return;
617#endif
618 }
619
620 {
621 Set c;
622
623 c.insert(b1D1);
624 if (!BEAST_EXPECT(c.size() == 1))
625 return;
626 c.insert(b2D1);
627 if (!BEAST_EXPECT(c.size() == 2))
628 return;
629 c.insert(b1D2);
630 if (!BEAST_EXPECT(c.size() == 3))
631 return;
632 c.insert(b2D2);
633 if (!BEAST_EXPECT(c.size() == 4))
634 return;
635
636 // Try removing non-existent elements
637 if (!BEAST_EXPECT(c.erase(Book(a2, a2, domain1)) == 0))
638 return;
639
640 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain1)) == 1))
641 return;
642 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain1)) == 1))
643 return;
644 if (!BEAST_EXPECT(c.size() == 2))
645 return;
646
647 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain2)) == 1))
648 return;
649 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain2)) == 1))
650 return;
651 if (!BEAST_EXPECT(c.empty()))
652 return;
653 }
654
655 {
656 Set c;
657
658 c.insert(b1);
659 c.insert(b2);
660 c.insert(b1D1);
661 c.insert(b2D1);
662 if (!BEAST_EXPECT(c.size() == 4))
663 return;
664
665 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
666 return;
667 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
668 return;
669 if (!BEAST_EXPECT(c.size() == 2))
670 return;
671
672 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain1)) == 1))
673 return;
674 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain1)) == 1))
675 return;
676 if (!BEAST_EXPECT(c.empty()))
677 return;
678 }
679 }
680
681 template <class Map>
682 void
684 {
685 Currency const c1(1);
686 AccountID const i1(1);
687 Currency const c2(2);
688 AccountID const i2(2);
689 Issue const a1(c1, i1);
690 Issue const a2(c2, i2);
691 Book const b1(a1, a2, std::nullopt);
692 Book const b2(a2, a1, std::nullopt);
693
694 uint256 const domain1{1};
695 uint256 const domain2{2};
696
697 Book const b1D1(a1, a2, domain1);
698 Book const b2D1(a2, a1, domain1);
699 Book const b1D2(a1, a2, domain2);
700 Book const b2D2(a2, a1, domain2);
701
702 // typename Map::value_type value_type;
703 // std::pair <Book const, int> value_type;
704
705 {
706 Map c;
707
708 // c.insert (value_type (b1, 1));
709 c.insert(std::make_pair(b1, 1));
710 if (!BEAST_EXPECT(c.size() == 1))
711 return;
712 // c.insert (value_type (b2, 2));
713 c.insert(std::make_pair(b2, 1));
714 if (!BEAST_EXPECT(c.size() == 2))
715 return;
716
717 if (!BEAST_EXPECT(c.erase(Book(a1, a1, std::nullopt)) == 0))
718 return;
719 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
720 return;
721 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
722 return;
723 if (!BEAST_EXPECT(c.empty()))
724 return;
725 }
726
727 {
728 Map c;
729
730 // c.insert (value_type (b1, 1));
731 c.insert(std::make_pair(b1, 1));
732 if (!BEAST_EXPECT(c.size() == 1))
733 return;
734 // c.insert (value_type (b2, 2));
735 c.insert(std::make_pair(b2, 1));
736 if (!BEAST_EXPECT(c.size() == 2))
737 return;
738
739 if (!BEAST_EXPECT(c.erase(Book(a1, a1, std::nullopt)) == 0))
740 return;
741 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
742 return;
743 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
744 return;
745 if (!BEAST_EXPECT(c.empty()))
746 return;
747 }
748
749 {
750 Map c;
751
752 c.insert(std::make_pair(b1D1, 10));
753 if (!BEAST_EXPECT(c.size() == 1))
754 return;
755 c.insert(std::make_pair(b2D1, 20));
756 if (!BEAST_EXPECT(c.size() == 2))
757 return;
758 c.insert(std::make_pair(b1D2, 30));
759 if (!BEAST_EXPECT(c.size() == 3))
760 return;
761 c.insert(std::make_pair(b2D2, 40));
762 if (!BEAST_EXPECT(c.size() == 4))
763 return;
764
765 // Try removing non-existent elements
766 if (!BEAST_EXPECT(c.erase(Book(a2, a2, domain1)) == 0))
767 return;
768
769 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain1)) == 1))
770 return;
771 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain1)) == 1))
772 return;
773 if (!BEAST_EXPECT(c.size() == 2))
774 return;
775
776 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain2)) == 1))
777 return;
778 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain2)) == 1))
779 return;
780 if (!BEAST_EXPECT(c.empty()))
781 return;
782 }
783
784 {
785 Map c;
786
787 c.insert(std::make_pair(b1, 1));
788 c.insert(std::make_pair(b2, 2));
789 c.insert(std::make_pair(b1D1, 3));
790 c.insert(std::make_pair(b2D1, 4));
791 if (!BEAST_EXPECT(c.size() == 4))
792 return;
793
794 // Try removing non-existent elements
795 if (!BEAST_EXPECT(c.erase(Book(a1, a1, domain1)) == 0))
796 return;
797 if (!BEAST_EXPECT(c.erase(Book(a2, a2, domain2)) == 0))
798 return;
799
800 if (!BEAST_EXPECT(c.erase(Book(a1, a2, std::nullopt)) == 1))
801 return;
802 if (!BEAST_EXPECT(c.erase(Book(a2, a1, std::nullopt)) == 1))
803 return;
804 if (!BEAST_EXPECT(c.size() == 2))
805 return;
806
807 if (!BEAST_EXPECT(c.erase(Book(a1, a2, domain1)) == 1))
808 return;
809 if (!BEAST_EXPECT(c.erase(Book(a2, a1, domain1)) == 1))
810 return;
811 if (!BEAST_EXPECT(c.empty()))
812 return;
813 }
814 }
815
816 void
818 {
819 testcase("std::set <Book>");
821
822 testcase("std::set <Book>");
824
825#if XRPL_ASSETS_ENABLE_STD_HASH
826 testcase("std::unordered_set <Book>");
828
829 testcase("std::unordered_set <Book>");
831#endif
832
833 testcase("hash_set <Book>");
835
836 testcase("hash_set <Book>");
838 }
839
840 void
842 {
843 testcase("std::map <Book, int>");
845
846 testcase("std::map <Book, int>");
848
849#if XRPL_ASSETS_ENABLE_STD_HASH
850 testcase("std::unordered_map <Book, int>");
852
853 testcase("std::unordered_map <Book, int>");
855
856 testcase("hash_map <Book, int>");
858
859 testcase("hash_map <Book, int>");
861#endif
862 }
863
864 //--------------------------------------------------------------------------
865
866 void
867 run() override
868 {
869 testcase("Currency");
871
872 testcase("AccountID");
874
875 // ---
876
877 testcase("Issue");
879
880 testcase("Issue");
882
885
886 // ---
887
888 testcase("Book");
890
891 testcase("Book");
893
894 testBookSets();
895 testBookMaps();
896
897 // ---
900 }
901};
902
904
905} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
Specifies an order book.
Definition Book.h:16
void run() override
Runs the suite.
void testIssueDomainMaps()
void testIssueDomainMap()
void testIssueDomainSet()
void testIssueDomainSets()
A currency issued by an account.
Definition Issue.h:13
T make_pair(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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
BaseUInt< 256 > uint256
Definition base_uint.h:562
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)