rippled
CashDiff_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/beast/unit_test.h>
21 #include <ripple/ledger/CashDiff.h>
22 #include <ripple/protocol/STAmount.h>
23 #include <type_traits>
24 
25 namespace ripple {
26 namespace test {
27 
28 class CashDiff_test : public beast::unit_test::suite
29 {
30  static_assert(!std::is_default_constructible<CashDiff>{}, "");
31  static_assert(!std::is_copy_constructible<CashDiff>{}, "");
32  static_assert(!std::is_copy_assignable<CashDiff>{}, "");
34  static_assert(!std::is_move_assignable<CashDiff>{}, "");
35 
36  // Exercise diffIsDust (STAmount, STAmount)
37  void
39  {
40  testcase("diffIsDust (STAmount, STAmount)");
41 
42  Issue const usd(Currency(0x5553440000000000), AccountID(0x4985601));
43  Issue const usf(Currency(0x5553460000000000), AccountID(0x4985601));
44 
45  // Positive and negative are never dust.
46  expect(!diffIsDust(STAmount{usd, 1}, STAmount{usd, -1}));
47 
48  // Different issues are never dust.
49  expect(!diffIsDust(STAmount{usd, 1}, STAmount{usf, 1}));
50 
51  // Native and non-native are never dust.
52  expect(!diffIsDust(STAmount{usd, 1}, STAmount{1}));
53 
54  // Equal values are always dust.
55  expect(diffIsDust(STAmount{0}, STAmount{0}));
56  {
57  // Test IOU.
58  std::uint64_t oldProbe = 0;
59  std::uint64_t newProbe = 10;
60  std::uint8_t e10 = 1;
61  do
62  {
63  STAmount large(usd, newProbe + 1);
64  STAmount small(usd, newProbe);
65 
66  expect(diffIsDust(large, small, e10));
67  expect(diffIsDust(large, small, e10 + 1) == (e10 > 13));
68 
69  oldProbe = newProbe;
70  newProbe = oldProbe * 10;
71  e10 += 1;
72  } while (newProbe > oldProbe &&
74  }
75  {
76  // Test XRP.
77  // A delta of 2 or less is always dust.
78  expect(diffIsDust(STAmount{2}, STAmount{0}));
79 
80  std::uint64_t oldProbe = 0;
81  std::uint64_t newProbe = 10;
82  std::uint8_t e10 = 0;
83  do
84  {
85  // Differences of 2 of fewer drops are always treated as dust,
86  // so use a delta of 3.
87  STAmount large(newProbe + 3);
88  STAmount small(newProbe);
89 
90  expect(diffIsDust(large, small, e10));
91  expect(diffIsDust(large, small, e10 + 1) == (e10 >= 20));
92 
93  oldProbe = newProbe;
94  newProbe = oldProbe * 10;
95  e10 += 1;
96  } while (newProbe > oldProbe &&
98  }
99  }
100 
101 public:
102  void
103  run() override
104  {
105  testDust();
106  }
107 };
108 
110 
111 } // namespace test
112 } // namespace ripple
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
ripple::diffIsDust
bool diffIsDust(STAmount const &v1, STAmount const &v2, std::uint8_t e10=6)
Definition: CashDiff.cpp:758
ripple::CashDiff
Definition: CashDiff.h:60
std::is_default_constructible
std::is_nothrow_move_constructible
ripple::test::CashDiff_test
Definition: CashDiff_test.cpp:28
ripple::test::CashDiff_test::testDust
void testDust()
Definition: CashDiff_test.cpp:38
ripple::base_uint< 160, detail::AccountIDTag >
ripple::test::CashDiff_test::run
void run() override
Definition: CashDiff_test.cpp:103
ripple::STAmount
Definition: STAmount.h:42
std::uint64_t
std::is_copy_constructible
std::is_move_assignable
ripple::Currency
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::is_copy_assignable
std::numeric_limits
type_traits