rippled
Loading...
Searching...
No Matches
BookDirs_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5 Permission to use, copy, modify, and/or distribute this software for any
6 purpose with or without fee is hereby granted, provided that the above
7 copyright notice and this permission notice appear in all copies.
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16//==============================================================================
17
18#include <test/jtx.h>
19
20#include <xrpld/ledger/BookDirs.h>
21
22#include <xrpl/protocol/Feature.h>
23
24namespace ripple {
25namespace test {
26
28{
29 void
31 {
32 using namespace jtx;
33 Env env(*this, features);
34 auto gw = Account("gw");
35 auto USD = gw["USD"];
36 env.fund(XRP(1000000), "alice", "bob", "gw");
37 env.close();
38
39 {
40 Book book(xrpIssue(), USD.issue(), std::nullopt);
41 {
42 auto d = BookDirs(*env.current(), book);
43 BEAST_EXPECT(std::begin(d) == std::end(d));
44 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
45 }
46 {
47 auto d = BookDirs(*env.current(), reversed(book));
48 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
49 }
50 }
51
52 {
53 env(offer("alice", Account("alice")["USD"](50), XRP(10)));
54 auto d = BookDirs(
55 *env.current(),
56 Book(
57 Account("alice")["USD"].issue(), xrpIssue(), std::nullopt));
58 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
59 }
60
61 {
62 env(offer("alice", gw["CNY"](50), XRP(10)));
63 auto d = BookDirs(
64 *env.current(),
65 Book(gw["CNY"].issue(), xrpIssue(), std::nullopt));
66 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
67 }
68
69 {
70 env.trust(Account("bob")["CNY"](10), "alice");
71 env(pay("bob", "alice", Account("bob")["CNY"](10)));
72 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
73 auto d = BookDirs(
74 *env.current(),
75 Book(USD.issue(), Account("bob")["CNY"].issue(), std::nullopt));
76 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
77 }
78
79 {
80 auto AUD = gw["AUD"];
81 for (auto i = 1, j = 3; i <= 3; ++i, --j)
82 for (auto k = 0; k < 80; ++k)
83 env(offer("alice", AUD(i), XRP(j)));
84
85 auto d = BookDirs(
86 *env.current(), Book(AUD.issue(), xrpIssue(), std::nullopt));
87 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
88 auto i = 1, j = 3, k = 0;
89 for (auto const& e : d)
90 {
91 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
92 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
93 if (++k % 80 == 0)
94 {
95 ++i;
96 --j;
97 }
98 }
99 }
100 }
101
102 void
103 run() override
104 {
105 using namespace jtx;
106 auto const sa = testable_amendments();
107 test_bookdir(sa - featurePermissionedDEX);
108 test_bookdir(sa);
109 }
110};
111
112BEAST_DEFINE_TESTSUITE(BookDirs, ledger, ripple);
113
114} // namespace test
115} // namespace ripple
T begin(T... args)
A testsuite class.
Definition suite.h:55
Specifies an order book.
Definition Book.h:36
Immutable cryptographic account descriptor.
Definition Account.h:39
A transaction testing environment.
Definition Env.h:121
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:331
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:121
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:310
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:279
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:30
FeatureBitset testable_amendments()
Definition Env.h:74
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:29
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:115
Book reversed(Book const &book)
Definition Book.cpp:49
void test_bookdir(FeatureBitset features)
void run() override
Runs the suite.