rippled
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 <ripple/ledger/BookDirs.h>
19 #include <ripple/protocol/Feature.h>
20 #include <test/jtx.h>
21 
22 namespace ripple {
23 namespace test {
24 
25 struct BookDirs_test : public beast::unit_test::suite
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();
102  test_bookdir(sa);
103  }
104 };
105 
107 
108 } // namespace test
109 } // namespace ripple
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::test::BookDirs_test::run
void run() override
Definition: BookDirs_test.cpp:97
std::distance
T distance(T... args)
ripple::test::jtx::Env::trust
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition: Env.cpp:259
ripple::sfTakerPays
const SF_AMOUNT sfTakerPays
ripple::test::BookDirs_test
Definition: BookDirs_test.cpp:25
ripple::BookDirs
Definition: BookDirs.h:27
ripple::reversed
Book reversed(Book const &book)
Definition: Book.cpp:45
ripple::sfTakerGets
const SF_AMOUNT sfTakerGets
ripple::test::BookDirs_test::test_bookdir
void test_bookdir(FeatureBitset features)
Definition: BookDirs_test.cpp:28
ripple::test::jtx::supported_amendments
FeatureBitset supported_amendments()
Definition: Env.h:70
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::pay
Json::Value pay(Account const &account, Account const &to, AnyAmount amount)
Create a payment.
Definition: pay.cpp:29
std::begin
T begin(T... args)
ripple::test::jtx::Env::fund
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:228
ripple::FeatureBitset
Definition: Feature.h:113
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:95
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::Book
Specifies an order book.
Definition: Book.h:33
std::end
T end(T... args)
ripple::featureFlowCross
const uint256 featureFlowCross
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:300
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)