rippled
Loading...
Searching...
No Matches
BookDirs_test.cpp
1#include <test/jtx.h>
2
3#include <xrpl/ledger/BookDirs.h>
4#include <xrpl/protocol/Feature.h>
5
6namespace xrpl {
7namespace test {
8
10{
11 void
13 {
14 using namespace jtx;
15 Env env(*this, features);
16 auto gw = Account("gw");
17 auto USD = gw["USD"];
18 env.fund(XRP(1000000), "alice", "bob", "gw");
19 env.close();
20
21 {
22 Book const book(xrpIssue(), USD.issue(), std::nullopt);
23 {
24 auto d = BookDirs(*env.current(), book);
25 BEAST_EXPECT(std::begin(d) == std::end(d));
26 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
27 }
28 {
29 auto d = BookDirs(*env.current(), reversed(book));
30 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
31 }
32 }
33
34 {
35 env(offer("alice", Account("alice")["USD"](50), XRP(10)));
36 auto d = BookDirs(
37 *env.current(), Book(Account("alice")["USD"].issue(), xrpIssue(), std::nullopt));
38 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
39 }
40
41 {
42 env(offer("alice", gw["CNY"](50), XRP(10)));
43 auto d = BookDirs(*env.current(), Book(gw["CNY"].issue(), xrpIssue(), std::nullopt));
44 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
45 }
46
47 {
48 env.trust(Account("bob")["CNY"](10), "alice");
49 env(pay("bob", "alice", Account("bob")["CNY"](10)));
50 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
51 auto d = BookDirs(
52 *env.current(), Book(USD.issue(), Account("bob")["CNY"].issue(), std::nullopt));
53 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
54 }
55
56 {
57 auto AUD = gw["AUD"];
58 for (auto i = 1, j = 3; i <= 3; ++i, --j)
59 {
60 for (auto k = 0; k < 80; ++k)
61 env(offer("alice", AUD(i), XRP(j)));
62 }
63
64 auto d = BookDirs(*env.current(), Book(AUD.issue(), xrpIssue(), std::nullopt));
65 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
66 auto i = 1, j = 3, k = 0;
67 for (auto const& e : d)
68 {
69 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
70 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
71 if (++k % 80 == 0)
72 {
73 ++i;
74 --j;
75 }
76 }
77 }
78 }
79
80 void
81 run() override
82 {
83 using namespace jtx;
84 auto const sa = testable_amendments();
85 test_bookdir(sa - featurePermissionedDEX);
86 test_bookdir(sa);
87 }
88};
89
90BEAST_DEFINE_TESTSUITE(BookDirs, ledger, xrpl);
91
92} // namespace test
93} // namespace xrpl
T begin(T... args)
A testsuite class.
Definition suite.h:51
Specifies an order book.
Definition Book.h:16
Immutable cryptographic account descriptor.
Definition Account.h:19
A transaction testing environment.
Definition Env.h:122
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:100
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:270
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:301
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:329
T distance(T... args)
T end(T... args)
T is_same_v
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:95
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:11
FeatureBitset testable_amendments()
Definition Env.h:78
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:10
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
Book reversed(Book const &book)
Definition Book.cpp:29
void test_bookdir(FeatureBitset features)
void run() override
Runs the suite.