Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / amount_tests.cpp
blob952cf901f02bd3f5c3dfc9e7cf7f9acc7d25f9ba
1 // Copyright (c) 2016 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.
5 #include "amount.h"
6 #include "policy/feerate.h"
7 #include "test/test_bitcoin.h"
9 #include <boost/test/unit_test.hpp>
11 BOOST_FIXTURE_TEST_SUITE(amount_tests, BasicTestingSetup)
13 BOOST_AUTO_TEST_CASE(MoneyRangeTest)
15 BOOST_CHECK_EQUAL(MoneyRange(CAmount(-1)), false);
16 BOOST_CHECK_EQUAL(MoneyRange(MAX_MONEY + CAmount(1)), false);
17 BOOST_CHECK_EQUAL(MoneyRange(CAmount(1)), true);
20 BOOST_AUTO_TEST_CASE(GetFeeTest)
22 CFeeRate feeRate, altFeeRate;
24 feeRate = CFeeRate(0);
25 // Must always return 0
26 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0);
27 BOOST_CHECK_EQUAL(feeRate.GetFee(1e5), 0);
29 feeRate = CFeeRate(1000);
30 // Must always just return the arg
31 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0);
32 BOOST_CHECK_EQUAL(feeRate.GetFee(1), 1);
33 BOOST_CHECK_EQUAL(feeRate.GetFee(121), 121);
34 BOOST_CHECK_EQUAL(feeRate.GetFee(999), 999);
35 BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), 1e3);
36 BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), 9e3);
38 feeRate = CFeeRate(-1000);
39 // Must always just return -1 * arg
40 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0);
41 BOOST_CHECK_EQUAL(feeRate.GetFee(1), -1);
42 BOOST_CHECK_EQUAL(feeRate.GetFee(121), -121);
43 BOOST_CHECK_EQUAL(feeRate.GetFee(999), -999);
44 BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), -1e3);
45 BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), -9e3);
47 feeRate = CFeeRate(123);
48 // Truncates the result, if not integer
49 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0);
50 BOOST_CHECK_EQUAL(feeRate.GetFee(8), 1); // Special case: returns 1 instead of 0
51 BOOST_CHECK_EQUAL(feeRate.GetFee(9), 1);
52 BOOST_CHECK_EQUAL(feeRate.GetFee(121), 14);
53 BOOST_CHECK_EQUAL(feeRate.GetFee(122), 15);
54 BOOST_CHECK_EQUAL(feeRate.GetFee(999), 122);
55 BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), 123);
56 BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), 1107);
58 feeRate = CFeeRate(-123);
59 // Truncates the result, if not integer
60 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0);
61 BOOST_CHECK_EQUAL(feeRate.GetFee(8), -1); // Special case: returns -1 instead of 0
62 BOOST_CHECK_EQUAL(feeRate.GetFee(9), -1);
64 // check alternate constructor
65 feeRate = CFeeRate(1000);
66 altFeeRate = CFeeRate(feeRate);
67 BOOST_CHECK_EQUAL(feeRate.GetFee(100), altFeeRate.GetFee(100));
69 // Check full constructor
70 // default value
71 BOOST_CHECK(CFeeRate(CAmount(-1), 1000) == CFeeRate(-1));
72 BOOST_CHECK(CFeeRate(CAmount(0), 1000) == CFeeRate(0));
73 BOOST_CHECK(CFeeRate(CAmount(1), 1000) == CFeeRate(1));
74 // lost precision (can only resolve satoshis per kB)
75 BOOST_CHECK(CFeeRate(CAmount(1), 1001) == CFeeRate(0));
76 BOOST_CHECK(CFeeRate(CAmount(2), 1001) == CFeeRate(1));
77 // some more integer checks
78 BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32));
79 BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34));
80 // Maximum size in bytes, should not crash
81 CFeeRate(MAX_MONEY, std::numeric_limits<size_t>::max() >> 1).GetFeePerK();
84 BOOST_AUTO_TEST_CASE(BinaryOperatorTest)
86 CFeeRate a, b;
87 a = CFeeRate(1);
88 b = CFeeRate(2);
89 BOOST_CHECK(a < b);
90 BOOST_CHECK(b > a);
91 BOOST_CHECK(a == a);
92 BOOST_CHECK(a <= b);
93 BOOST_CHECK(a <= a);
94 BOOST_CHECK(b >= a);
95 BOOST_CHECK(b >= b);
96 // a should be 0.00000002 BTC/kB now
97 a += a;
98 BOOST_CHECK(a == b);
101 BOOST_AUTO_TEST_CASE(ToStringTest)
103 CFeeRate feeRate;
104 feeRate = CFeeRate(1);
105 BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kB");
108 BOOST_AUTO_TEST_SUITE_END()