LCOV - code coverage report
Current view: top level - src/test - amount_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 92 92
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 8 8
Branches: 50.0 % 348 174

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

Generated by: LCOV version 2.0-1