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 <test/jtx.h>
19 #include <ripple/ledger/BookDirs.h>
20 #include <ripple/protocol/Feature.h>
21 
22 namespace ripple {
23 namespace test {
24 
25 struct BookDirs_test : public beast::unit_test::suite
26 {
27  void test_bookdir(FeatureBitset features)
28  {
29  using namespace jtx;
30  Env env(*this, features);
31  auto gw = Account("gw");
32  auto USD = gw["USD"];
33  env.fund(XRP(1000000), "alice", "bob", "gw");
34 
35  {
36  Book book(xrpIssue(), USD.issue());
37  {
38  auto d = BookDirs(*env.current(), book);
39  BEAST_EXPECT(std::begin(d) == std::end(d));
40  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
41  }
42  {
43  auto d = BookDirs(*env.current(), reversed(book));
44  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
45  }
46  }
47 
48  {
49  env(offer("alice", Account("alice")["USD"](50), XRP(10)));
50  auto d = BookDirs(*env.current(),
51  Book(Account("alice")["USD"].issue(), xrpIssue()));
52  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
53  }
54 
55  {
56  env(offer("alice", gw["CNY"](50), XRP(10)));
57  auto d = BookDirs(*env.current(),
58  Book(gw["CNY"].issue(), xrpIssue()));
59  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
60  }
61 
62  {
63  env.trust(Account("bob")["CNY"](10), "alice");
64  env(pay("bob", "alice", Account("bob")["CNY"](10)));
65  env(offer("alice", USD(50), Account("bob")["CNY"](10)));
66  auto d = BookDirs(*env.current(),
67  Book(USD.issue(), Account("bob")["CNY"].issue()));
68  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
69  }
70 
71  {
72  auto AUD = gw["AUD"];
73  for (auto i = 1, j = 3; i <= 3; ++i, --j)
74  for (auto k = 0; k < 80; ++k)
75  env(offer("alice", AUD(i), XRP(j)));
76 
77  auto d = BookDirs(*env.current(),
78  Book(AUD.issue(), xrpIssue()));
79  BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
80  auto i = 1, j = 3, k = 0;
81  for (auto const& e : d)
82  {
83  BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == AUD(i));
84  BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
85  if (++k % 80 == 0)
86  {
87  ++i;
88  --j;
89  }
90  }
91  }
92  }
93 
94  void run() override
95  {
96  using namespace jtx;
97  auto const sa = supported_amendments();
99  test_bookdir(sa);
100  }
101 };
102 
104 
105 } // test
106 } // ripple
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:109
ripple::test::BookDirs_test::run
void run() override
Definition: BookDirs_test.cpp:94
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
ripple::sfTakerPays
const SF_Amount sfTakerPays(access, STI_AMOUNT, 4, "TakerPays")
Definition: SField.h:426
std::distance
T distance(T... args)
ripple::test::jtx::offer
Json::Value offer(Account const &account, STAmount const &in, STAmount const &out, std::uint32_t flags)
Create an offer.
Definition: offer.cpp:28
ripple::test::jtx::Env::trust
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition: Env.cpp:245
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::test::BookDirs_test::test_bookdir
void test_bookdir(FeatureBitset features)
Definition: BookDirs_test.cpp:27
ripple::test::jtx::supported_amendments
FeatureBitset supported_amendments()
Definition: Env.h:71
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:214
ripple::FeatureBitset
Definition: Feature.h:153
ripple::sfTakerGets
const SF_Amount sfTakerGets(access, STI_AMOUNT, 5, "TakerGets")
Definition: SField.h:427
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:97
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::Book
Specifies an order book.
Definition: Book.h:32
std::end
T end(T... args)
ripple::featureFlowCross
const uint256 featureFlowCross
Definition: Feature.cpp:161
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:296
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117