Branch data Line data Source code
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 : :
12 : 0 : BOOST_AUTO_TEST_SUITE(amount_tests)
13 : :
14 : 0 : BOOST_AUTO_TEST_CASE(MoneyRangeTest)
15 : : {
16 : 0 : BOOST_CHECK_EQUAL(MoneyRange(CAmount(-1)), false);
17 : 0 : BOOST_CHECK_EQUAL(MoneyRange(CAmount(0)), true);
18 : 0 : BOOST_CHECK_EQUAL(MoneyRange(CAmount(1)), true);
19 : 0 : BOOST_CHECK_EQUAL(MoneyRange(MAX_MONEY), true);
20 : 0 : BOOST_CHECK_EQUAL(MoneyRange(MAX_MONEY + CAmount(1)), false);
21 : 0 : }
22 : :
23 : 0 : BOOST_AUTO_TEST_CASE(GetFeeTest)
24 : : {
25 : 0 : CFeeRate feeRate, altFeeRate;
26 : :
27 : 0 : feeRate = CFeeRate(0);
28 : : // Must always return 0
29 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
30 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1e5), CAmount(0));
31 : :
32 : 0 : feeRate = CFeeRate(1000);
33 : : // Must always just return the arg
34 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
35 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1), CAmount(1));
36 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(121));
37 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(999));
38 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(1e3));
39 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(9e3));
40 : :
41 : 0 : feeRate = CFeeRate(-1000);
42 : : // Must always just return -1 * arg
43 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
44 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1), CAmount(-1));
45 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(-121));
46 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(-999));
47 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(-1e3));
48 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(-9e3));
49 : :
50 : 0 : feeRate = CFeeRate(123);
51 : : // Rounds up the result, if not integer
52 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
53 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(8), CAmount(1)); // Special case: returns 1 instead of 0
54 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(9), CAmount(2));
55 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(121), CAmount(15));
56 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(122), CAmount(16));
57 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(999), CAmount(123));
58 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), CAmount(123));
59 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), CAmount(1107));
60 : :
61 : 0 : feeRate = CFeeRate(-123);
62 : : // Truncates the result, if not integer
63 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(0), CAmount(0));
64 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(8), CAmount(-1)); // Special case: returns -1 instead of 0
65 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(9), CAmount(-1));
66 : :
67 : : // check alternate constructor
68 : 0 : feeRate = CFeeRate(1000);
69 : 0 : altFeeRate = CFeeRate(feeRate);
70 : 0 : BOOST_CHECK_EQUAL(feeRate.GetFee(100), altFeeRate.GetFee(100));
71 : :
72 : : // Check full constructor
73 : 0 : BOOST_CHECK(CFeeRate(CAmount(-1), 0) == CFeeRate(0));
74 : 0 : BOOST_CHECK(CFeeRate(CAmount(0), 0) == CFeeRate(0));
75 : 0 : BOOST_CHECK(CFeeRate(CAmount(1), 0) == CFeeRate(0));
76 : : // default value
77 : 0 : BOOST_CHECK(CFeeRate(CAmount(-1), 1000) == CFeeRate(-1));
78 : 0 : BOOST_CHECK(CFeeRate(CAmount(0), 1000) == CFeeRate(0));
79 : 0 : BOOST_CHECK(CFeeRate(CAmount(1), 1000) == CFeeRate(1));
80 : : // lost precision (can only resolve satoshis per kB)
81 : 0 : BOOST_CHECK(CFeeRate(CAmount(1), 1001) == CFeeRate(0));
82 : 0 : BOOST_CHECK(CFeeRate(CAmount(2), 1001) == CFeeRate(1));
83 : : // some more integer checks
84 : 0 : BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32));
85 : 0 : BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34));
86 : : // Maximum size in bytes, should not crash
87 : 0 : CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).GetFeePerK();
88 : 0 : }
89 : :
90 : 0 : BOOST_AUTO_TEST_CASE(BinaryOperatorTest)
91 : : {
92 : 0 : CFeeRate a, b;
93 : 0 : a = CFeeRate(1);
94 : 0 : b = CFeeRate(2);
95 : 0 : BOOST_CHECK(a < b);
96 : 0 : BOOST_CHECK(b > a);
97 : 0 : BOOST_CHECK(a == a);
98 : 0 : BOOST_CHECK(a <= b);
99 : 0 : BOOST_CHECK(a <= a);
100 : 0 : BOOST_CHECK(b >= a);
101 : 0 : BOOST_CHECK(b >= b);
102 : : // a should be 0.00000002 BTC/kvB now
103 : 0 : a += a;
104 : 0 : BOOST_CHECK(a == b);
105 : 0 : }
106 : :
107 : 0 : BOOST_AUTO_TEST_CASE(ToStringTest)
108 : : {
109 : 0 : CFeeRate feeRate;
110 : 0 : feeRate = CFeeRate(1);
111 : 0 : BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kvB");
112 : 0 : BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 BTC/kvB");
113 : 0 : BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB");
114 : 0 : }
115 : :
116 : 0 : BOOST_AUTO_TEST_SUITE_END()
|