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 ripple {
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(
37 *env.current(),
38 Book(
39 Account("alice")["USD"].issue(), xrpIssue(), std::nullopt));
40 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
41 }
42
43 {
44 env(offer("alice", gw["CNY"](50), XRP(10)));
45 auto d = BookDirs(
46 *env.current(),
47 Book(gw["CNY"].issue(), xrpIssue(), std::nullopt));
48 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
49 }
50
51 {
52 env.trust(Account("bob")["CNY"](10), "alice");
53 env(pay("bob", "alice", Account("bob")["CNY"](10)));
54 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
55 auto d = BookDirs(
56 *env.current(),
57 Book(USD.issue(), Account("bob")["CNY"].issue(), std::nullopt));
58 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
59 }
60
61 {
62 auto AUD = gw["AUD"];
63 for (auto i = 1, j = 3; i <= 3; ++i, --j)
64 for (auto k = 0; k < 80; ++k)
65 env(offer("alice", AUD(i), XRP(j)));
66
67 auto d = BookDirs(
68 *env.current(), Book(AUD.issue(), xrpIssue(), std::nullopt));
69 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
70 auto i = 1, j = 3, k = 0;
71 for (auto const& e : d)
72 {
73 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
74 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
75 if (++k % 80 == 0)
76 {
77 ++i;
78 --j;
79 }
80 }
81 }
82 }
83
84 void
85 run() override
86 {
87 using namespace jtx;
88 auto const sa = testable_amendments();
89 test_bookdir(sa - featurePermissionedDEX);
90 test_bookdir(sa);
91 }
92};
93
94BEAST_DEFINE_TESTSUITE(BookDirs, ledger, ripple);
95
96} // namespace test
97} // namespace ripple
T begin(T... args)
A testsuite class.
Definition suite.h:52
Specifies an order book.
Definition Book.h:17
Immutable cryptographic account descriptor.
Definition Account.h:20
A transaction testing environment.
Definition Env.h:102
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:312
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:103
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:302
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:271
T distance(T... args)
T end(T... args)
T is_same_v
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:11
FeatureBitset testable_amendments()
Definition Env.h:55
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:10
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:96
Book reversed(Book const &book)
Definition Book.cpp:30
void test_bookdir(FeatureBitset features)
void run() override
Runs the suite.