Line data Source code
1 : // Copyright (c) 2014 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 "chainparams.h"
6 : #include "main.h"
7 :
8 : #include "test/test_bitcoin.h"
9 :
10 : #include <boost/signals2/signal.hpp>
11 : #include <boost/test/unit_test.hpp>
12 :
13 1 : BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup)
14 :
15 3 : static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
16 : {
17 3 : int maxHalvings = 64;
18 3 : CAmount nInitialSubsidy = 50 * COIN;
19 :
20 3 : CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
21 15 : BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
22 192 : for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
23 192 : int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval;
24 192 : CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
25 1536 : BOOST_CHECK(nSubsidy <= nInitialSubsidy);
26 960 : BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
27 192 : nPreviousSubsidy = nSubsidy;
28 : }
29 15 : BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0);
30 3 : }
31 :
32 : static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
33 : {
34 2 : Consensus::Params consensusParams;
35 2 : consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
36 2 : TestBlockSubsidyHalvings(consensusParams);
37 : }
38 :
39 6 : BOOST_AUTO_TEST_CASE(block_subsidy_test)
40 : {
41 2 : TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main
42 : TestBlockSubsidyHalvings(150); // As in regtest
43 : TestBlockSubsidyHalvings(1000); // Just another interval
44 1 : }
45 :
46 6 : BOOST_AUTO_TEST_CASE(subsidy_limit_test)
47 : {
48 2 : const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus();
49 1 : CAmount nSum = 0;
50 14001 : for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
51 14000 : CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
52 112000 : BOOST_CHECK(nSubsidy <= 50 * COIN);
53 14000 : nSum += nSubsidy * 1000;
54 126000 : BOOST_CHECK(MoneyRange(nSum));
55 : }
56 5 : BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL);
57 1 : }
58 :
59 2 : bool ReturnFalse() { return false; }
60 1 : bool ReturnTrue() { return true; }
61 :
62 6 : BOOST_AUTO_TEST_CASE(test_combiner_all)
63 : {
64 1 : boost::signals2::signal<bool (), CombinerAll> Test;
65 8 : BOOST_CHECK(Test());
66 2 : Test.connect(&ReturnFalse);
67 8 : BOOST_CHECK(!Test());
68 2 : Test.connect(&ReturnTrue);
69 7 : BOOST_CHECK(!Test());
70 1 : Test.disconnect(&ReturnFalse);
71 8 : BOOST_CHECK(Test());
72 1 : Test.disconnect(&ReturnTrue);
73 8 : BOOST_CHECK(Test());
74 1 : }
75 :
76 3 : BOOST_AUTO_TEST_SUITE_END()
|