Bitcoin Core  22.99.0
P2P Digital Currency
amount_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <amount.h>
6 #include <policy/feerate.h>
7 
8 #include <limits>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 BOOST_AUTO_TEST_SUITE(amount_tests)
13 
14 BOOST_AUTO_TEST_CASE(MoneyRangeTest)
15 {
21 }
22 
24 {
25  CFeeRate feeRate, altFeeRate;
26 
27  feeRate = CFeeRate(0);
28  // Must always return 0
29  BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
30  BOOST_CHECK_EQUAL(feeRate.GetFee(1e5), CAmount(0));
31 
32  feeRate = CFeeRate(1000);
33  // Must always just return the arg
34  BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
35  BOOST_CHECK_EQUAL(feeRate.GetFee(1), CAmount(1));
36  BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(121));
37  BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(999));
38  BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(1e3));
39  BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(9e3));
40 
41  feeRate = CFeeRate(-1000);
42  // Must always just return -1 * arg
43  BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
44  BOOST_CHECK_EQUAL(feeRate.GetFee(1), CAmount(-1));
45  BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(-121));
46  BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(-999));
47  BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(-1e3));
48  BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(-9e3));
49 
50  feeRate = CFeeRate(123);
51  // Truncates the result, if not integer
52  BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
53  BOOST_CHECK_EQUAL(feeRate.GetFee(8), CAmount(1)); // Special case: returns 1 instead of 0
54  BOOST_CHECK_EQUAL(feeRate.GetFee(9), CAmount(1));
55  BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(14));
56  BOOST_CHECK_EQUAL(feeRate.GetFee(122), CAmount(15));
57  BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(122));
58  BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(123));
59  BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(1107));
60 
61  feeRate = CFeeRate(-123);
62  // Truncates the result, if not integer
63  BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
64  BOOST_CHECK_EQUAL(feeRate.GetFee(8), CAmount(-1)); // Special case: returns -1 instead of 0
65  BOOST_CHECK_EQUAL(feeRate.GetFee(9), CAmount(-1));
66 
67  // check alternate constructor
68  feeRate = CFeeRate(1000);
69  altFeeRate = CFeeRate(feeRate);
70  BOOST_CHECK_EQUAL(feeRate.GetFee(100), altFeeRate.GetFee(100));
71 
72  // Check full constructor
73  BOOST_CHECK(CFeeRate(CAmount(-1), 0) == CFeeRate(0));
74  BOOST_CHECK(CFeeRate(CAmount(0), 0) == CFeeRate(0));
75  BOOST_CHECK(CFeeRate(CAmount(1), 0) == CFeeRate(0));
76  // default value
77  BOOST_CHECK(CFeeRate(CAmount(-1), 1000) == CFeeRate(-1));
78  BOOST_CHECK(CFeeRate(CAmount(0), 1000) == CFeeRate(0));
79  BOOST_CHECK(CFeeRate(CAmount(1), 1000) == CFeeRate(1));
80  // lost precision (can only resolve satoshis per kB)
81  BOOST_CHECK(CFeeRate(CAmount(1), 1001) == CFeeRate(0));
82  BOOST_CHECK(CFeeRate(CAmount(2), 1001) == CFeeRate(1));
83  // some more integer checks
84  BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32));
85  BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34));
86  // Maximum size in bytes, should not crash
87  CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).GetFeePerK();
88 }
89 
90 BOOST_AUTO_TEST_CASE(BinaryOperatorTest)
91 {
92  CFeeRate a, b;
93  a = CFeeRate(1);
94  b = CFeeRate(2);
95  BOOST_CHECK(a < b);
96  BOOST_CHECK(b > a);
97  BOOST_CHECK(a == a);
98  BOOST_CHECK(a <= b);
99  BOOST_CHECK(a <= a);
100  BOOST_CHECK(b >= a);
101  BOOST_CHECK(b >= b);
102  // a should be 0.00000002 BTC/kvB now
103  a += a;
104  BOOST_CHECK(a == b);
105 }
106 
107 BOOST_AUTO_TEST_CASE(ToStringTest)
108 {
109  CFeeRate feeRate;
110  feeRate = CFeeRate(1);
111  BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kvB");
112  BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 BTC/kvB");
113  BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB");
114 }
115 
FeeEstimateMode::BTC_KVB
@ BTC_KVB
Use BTC/kvB fee rate unit.
feerate.h
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CFeeRate
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
FeeEstimateMode::SAT_VB
@ SAT_VB
Use sat/vB fee rate unit.
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CFeeRate::ToString
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:35
MAX_MONEY
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(MoneyRangeTest)
Definition: amount_tests.cpp:14
CFeeRate::GetFee
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:21
CFeeRate::GetFeePerK
CAmount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:56
amount.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18