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#include <xrpld/ledger/BookDirs.h>
20#include <xrpl/protocol/Feature.h>
21
22namespace ripple {
23namespace test {
24
26{
27 void
29 {
30 using namespace jtx;
31 Env env(*this, features);
32 auto gw = Account("gw");
33 auto USD = gw["USD"];
34 env.fund(XRP(1000000), "alice", "bob", "gw");
35
36 {
37 Book book(xrpIssue(), USD.issue());
38 {
39 auto d = BookDirs(*env.current(), book);
40 BEAST_EXPECT(std::begin(d) == std::end(d));
41 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
42 }
43 {
44 auto d = BookDirs(*env.current(), reversed(book));
45 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
46 }
47 }
48
49 {
50 env(offer("alice", Account("alice")["USD"](50), XRP(10)));
51 auto d = BookDirs(
52 *env.current(),
53 Book(Account("alice")["USD"].issue(), xrpIssue()));
54 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
55 }
56
57 {
58 env(offer("alice", gw["CNY"](50), XRP(10)));
59 auto d =
60 BookDirs(*env.current(), Book(gw["CNY"].issue(), xrpIssue()));
61 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
62 }
63
64 {
65 env.trust(Account("bob")["CNY"](10), "alice");
66 env(pay("bob", "alice", Account("bob")["CNY"](10)));
67 env(offer("alice", USD(50), Account("bob")["CNY"](10)));
68 auto d = BookDirs(
69 *env.current(),
70 Book(USD.issue(), Account("bob")["CNY"].issue()));
71 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
72 }
73
74 {
75 auto AUD = gw["AUD"];
76 for (auto i = 1, j = 3; i <= 3; ++i, --j)
77 for (auto k = 0; k < 80; ++k)
78 env(offer("alice", AUD(i), XRP(j)));
79
80 auto d = BookDirs(*env.current(), Book(AUD.issue(), xrpIssue()));
81 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
82 auto i = 1, j = 3, k = 0;
83 for (auto const& e : d)
84 {
85 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
86 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
87 if (++k % 80 == 0)
88 {
89 ++i;
90 --j;
91 }
92 }
93 }
94 }
95
96 void
97 run() override
98 {
99 using namespace jtx;
100 auto const sa = supported_amendments();
101 test_bookdir(sa - featureFlowCross);
102 test_bookdir(sa);
103 }
104};
105
106BEAST_DEFINE_TESTSUITE(BookDirs, ledger, ripple);
107
108} // namespace test
109} // namespace ripple
T begin(T... args)
A testsuite class.
Definition: suite.h:55
Specifies an order book.
Definition: Book.h:35
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:118
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:326
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition: Env.cpp:262
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:231
T distance(T... args)
T end(T... args)
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition: pay.cpp:29
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition: offer.cpp:28
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:104
FeatureBitset supported_amendments()
Definition: Env.h:71
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:118
Book reversed(Book const &book)
Definition: Book.cpp:49
void test_bookdir(FeatureBitset features)
void run() override
Runs the suite.