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