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