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 <xrpl/ledger/BookDirs.h>
21#include <xrpl/protocol/Feature.h>
22
23namespace ripple {
24namespace test {
25
27{
28 void
30 {
31 using namespace jtx;
32 Env env(*this, features);
33 auto gw = Account("gw");
34 auto USD = gw["USD"];
35 env.fund(XRP(1000000), "alice", "bob", "gw");
36 env.close();
37
38 {
39 Book book(xrpIssue(), USD.issue(), std::nullopt);
40 {
41 auto d = BookDirs(*env.current(), book);
42 BEAST_EXPECT(std::begin(d) == std::end(d));
43 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
44 }
45 {
46 auto d = BookDirs(*env.current(), reversed(book));
47 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
48 }
49 }
50
51 {
52 env(offer("alice", Account("alice")["USD"](50), XRP(10)));
53 auto d = BookDirs(
54 *env.current(),
55 Book(
56 Account("alice")["USD"].issue(), xrpIssue(), std::nullopt));
57 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
58 }
59
60 {
61 env(offer("alice", gw["CNY"](50), XRP(10)));
62 auto d = BookDirs(
63 *env.current(),
64 Book(gw["CNY"].issue(), xrpIssue(), std::nullopt));
65 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
66 }
67
68 {
69 env.trust(Account("bob")["CNY"](10), "alice");
70 env(pay("bob", "alice", Account("bob")["CNY"](10)));
71 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
72 auto d = BookDirs(
73 *env.current(),
74 Book(USD.issue(), Account("bob")["CNY"].issue(), std::nullopt));
75 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
76 }
77
78 {
79 auto AUD = gw["AUD"];
80 for (auto i = 1, j = 3; i <= 3; ++i, --j)
81 for (auto k = 0; k < 80; ++k)
82 env(offer("alice", AUD(i), XRP(j)));
83
84 auto d = BookDirs(
85 *env.current(), Book(AUD.issue(), xrpIssue(), std::nullopt));
86 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
87 auto i = 1, j = 3, k = 0;
88 for (auto const& e : d)
89 {
90 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
91 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
92 if (++k % 80 == 0)
93 {
94 ++i;
95 --j;
96 }
97 }
98 }
99 }
100
101 void
102 run() override
103 {
104 using namespace jtx;
105 auto const sa = testable_amendments();
106 test_bookdir(sa - featurePermissionedDEX);
107 test_bookdir(sa);
108 }
109};
110
111BEAST_DEFINE_TESTSUITE(BookDirs, ledger, ripple);
112
113} // namespace test
114} // 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:320
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:289
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:111
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.