Bitcoin Core 28.99.0
P2P Digital Currency
amount_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-2021 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 <consensus/amount.h>
6#include <policy/feerate.h>
7
8#include <limits>
9
10#include <boost/test/unit_test.hpp>
11
12BOOST_AUTO_TEST_SUITE(amount_tests)
13
14BOOST_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 // Rounds up 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(2));
55 BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(15));
56 BOOST_CHECK_EQUAL(feeRate.GetFee(122), CAmount(16));
57 BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(123));
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));
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 // check multiplication operator
90 // check multiplying by zero
91 feeRate = CFeeRate(1000);
92 BOOST_CHECK(0 * feeRate == CFeeRate(0));
93 BOOST_CHECK(feeRate * 0 == CFeeRate(0));
94 // check multiplying by a positive integer
95 BOOST_CHECK(3 * feeRate == CFeeRate(3000));
96 BOOST_CHECK(feeRate * 3 == CFeeRate(3000));
97 // check multiplying by a negative integer
98 BOOST_CHECK(-3 * feeRate == CFeeRate(-3000));
99 BOOST_CHECK(feeRate * -3 == CFeeRate(-3000));
100 // check commutativity
101 BOOST_CHECK(2 * feeRate == feeRate * 2);
102 // check with large numbers
103 int largeNumber = 1000000;
104 BOOST_CHECK(largeNumber * feeRate == feeRate * largeNumber);
105 // check boundary values
106 int maxInt = std::numeric_limits<int>::max();
107 feeRate = CFeeRate(maxInt);
108 BOOST_CHECK(feeRate * 2 == CFeeRate(static_cast<int64_t>(maxInt) * 2));
109 BOOST_CHECK(2 * feeRate == CFeeRate(static_cast<int64_t>(maxInt) * 2));
110 // check with zero fee rate
111 feeRate = CFeeRate(0);
112 BOOST_CHECK(feeRate * 5 == CFeeRate(0));
113 BOOST_CHECK(5 * feeRate == CFeeRate(0));
114}
115
116BOOST_AUTO_TEST_CASE(BinaryOperatorTest)
117{
118 CFeeRate a, b;
119 a = CFeeRate(1);
120 b = CFeeRate(2);
121 BOOST_CHECK(a < b);
122 BOOST_CHECK(b > a);
123 BOOST_CHECK(a == a);
124 BOOST_CHECK(a <= b);
125 BOOST_CHECK(a <= a);
126 BOOST_CHECK(b >= a);
127 BOOST_CHECK(b >= b);
128 // a should be 0.00000002 BTC/kvB now
129 a += a;
130 BOOST_CHECK(a == b);
131}
132
134{
135 CFeeRate feeRate;
136 feeRate = CFeeRate(1);
137 BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kvB");
138 BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 BTC/kvB");
139 BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB");
140}
141
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
BOOST_AUTO_TEST_CASE(MoneyRangeTest)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:39
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:23
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:63
BOOST_AUTO_TEST_SUITE_END()
@ BTC_KVB
Use BTC/kvB fee rate unit.
@ SAT_VB
Use sat/vB fee rate unit.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17