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 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(*env.current(), Book(Account("alice")["USD"].issue(), xrpIssue(), std::nullopt));
37 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
38 }
39
40 {
41 env(offer("alice", gw["CNY"](50), XRP(10)));
42 auto d = BookDirs(*env.current(), Book(gw["CNY"].issue(), xrpIssue(), std::nullopt));
43 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
44 }
45
46 {
47 env.trust(Account("bob")["CNY"](10), "alice");
48 env(pay("bob", "alice", Account("bob")["CNY"](10)));
49 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
50 auto d = BookDirs(*env.current(), Book(USD.issue(), Account("bob")["CNY"].issue(), std::nullopt));
51 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
52 }
53
54 {
55 auto AUD = gw["AUD"];
56 for (auto i = 1, j = 3; i <= 3; ++i, --j)
57 for (auto k = 0; k < 80; ++k)
58 env(offer("alice", AUD(i), XRP(j)));
59
60 auto d = BookDirs(*env.current(), Book(AUD.issue(), xrpIssue(), std::nullopt));
61 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
62 auto i = 1, j = 3, k = 0;
63 for (auto const& e : d)
64 {
65 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
66 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
67 if (++k % 80 == 0)
68 {
69 ++i;
70 --j;
71 }
72 }
73 }
74 }
75
76 void
77 run() override
78 {
79 using namespace jtx;
80 auto const sa = testable_amendments();
81 test_bookdir(sa - featurePermissionedDEX);
82 test_bookdir(sa);
83 }
84};
85
86BEAST_DEFINE_TESTSUITE(BookDirs, ledger, xrpl);
87
88} // namespace test
89} // 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:119
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:98
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:261
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:284
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:319
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:90
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:11
FeatureBitset testable_amendments()
Definition Env.h:76
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.