LCOV - code coverage report
Current view: top level - src/test - scriptnum_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 127 128 99.2 %
Date: 2020-09-26 01:30:44 Functions: 25 25 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2012-2020 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 <script/script.h>
       6             : #include <test/scriptnum10.h>
       7             : #include <test/util/setup_common.h>
       8             : 
       9             : #include <boost/test/unit_test.hpp>
      10             : #include <limits.h>
      11             : #include <stdint.h>
      12             : 
      13          89 : BOOST_FIXTURE_TEST_SUITE(scriptnum_tests, BasicTestingSetup)
      14             : 
      15             : /** A selection of numbers that do not trigger int64_t overflow
      16             :  *  when added/subtracted. */
      17             : static const int64_t values[] = { 0, 1, -2, 127, 128, -255, 256, (1LL << 15) - 1, -(1LL << 16), (1LL << 24) - 1, (1LL << 31), 1 - (1LL << 32), 1LL << 40 };
      18             : 
      19             : static const int64_t offsets[] = { 1, 0x79, 0x80, 0x81, 0xFF, 0x7FFF, 0x8000, 0xFFFF, 0x10000};
      20             : 
      21       13545 : static bool verify(const CScriptNum10& bignum, const CScriptNum& scriptnum)
      22             : {
      23       13545 :     return bignum.getvch() == scriptnum.getvch() && bignum.getint() == scriptnum.getint();
      24           0 : }
      25             : 
      26         351 : static void CheckCreateVch(const int64_t& num)
      27             : {
      28         351 :     CScriptNum10 bignum(num);
      29         351 :     CScriptNum scriptnum(num);
      30         351 :     BOOST_CHECK(verify(bignum, scriptnum));
      31             : 
      32         351 :     CScriptNum10 bignum2(bignum.getvch(), false);
      33         279 :     CScriptNum scriptnum2(scriptnum.getvch(), false);
      34         279 :     BOOST_CHECK(verify(bignum2, scriptnum2));
      35             : 
      36         279 :     CScriptNum10 bignum3(scriptnum2.getvch(), false);
      37         279 :     CScriptNum scriptnum3(bignum2.getvch(), false);
      38         279 :     BOOST_CHECK(verify(bignum3, scriptnum3));
      39         351 : }
      40             : 
      41         351 : static void CheckCreateInt(const int64_t& num)
      42             : {
      43         351 :     CScriptNum10 bignum(num);
      44         351 :     CScriptNum scriptnum(num);
      45         351 :     BOOST_CHECK(verify(bignum, scriptnum));
      46         351 :     BOOST_CHECK(verify(CScriptNum10(bignum.getint()), CScriptNum(scriptnum.getint())));
      47         351 :     BOOST_CHECK(verify(CScriptNum10(scriptnum.getint()), CScriptNum(bignum.getint())));
      48         351 :     BOOST_CHECK(verify(CScriptNum10(CScriptNum10(scriptnum.getint()).getint()), CScriptNum(CScriptNum(bignum.getint()).getint())));
      49         351 : }
      50             : 
      51             : 
      52        1404 : static void CheckAdd(const int64_t& num1, const int64_t& num2)
      53             : {
      54        1404 :     const CScriptNum10 bignum1(num1);
      55        1404 :     const CScriptNum10 bignum2(num2);
      56        1404 :     const CScriptNum scriptnum1(num1);
      57        1404 :     const CScriptNum scriptnum2(num2);
      58        1404 :     CScriptNum10 bignum3(num1);
      59        1404 :     CScriptNum10 bignum4(num1);
      60        1404 :     CScriptNum scriptnum3(num1);
      61        1404 :     CScriptNum scriptnum4(num1);
      62             : 
      63             :     // int64_t overflow is undefined.
      64        2808 :     bool invalid = (((num2 > 0) && (num1 > (std::numeric_limits<int64_t>::max() - num2))) ||
      65        1404 :                     ((num2 < 0) && (num1 < (std::numeric_limits<int64_t>::min() - num2))));
      66        1404 :     if (!invalid)
      67             :     {
      68        1404 :         BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + scriptnum2));
      69        1404 :         BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + num2));
      70        1404 :         BOOST_CHECK(verify(bignum1 + bignum2, scriptnum2 + num1));
      71             :     }
      72        1404 : }
      73             : 
      74        1404 : static void CheckNegate(const int64_t& num)
      75             : {
      76        1404 :     const CScriptNum10 bignum(num);
      77        1404 :     const CScriptNum scriptnum(num);
      78             : 
      79             :     // -INT64_MIN is undefined
      80        1404 :     if (num != std::numeric_limits<int64_t>::min())
      81        1404 :         BOOST_CHECK(verify(-bignum, -scriptnum));
      82        1404 : }
      83             : 
      84        1404 : static void CheckSubtract(const int64_t& num1, const int64_t& num2)
      85             : {
      86        1404 :     const CScriptNum10 bignum1(num1);
      87        1404 :     const CScriptNum10 bignum2(num2);
      88        1404 :     const CScriptNum scriptnum1(num1);
      89        1404 :     const CScriptNum scriptnum2(num2);
      90             : 
      91             :     // int64_t overflow is undefined.
      92        2808 :     bool invalid = ((num2 > 0 && num1 < std::numeric_limits<int64_t>::min() + num2) ||
      93        1404 :                     (num2 < 0 && num1 > std::numeric_limits<int64_t>::max() + num2));
      94        1404 :     if (!invalid)
      95             :     {
      96        1404 :         BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - scriptnum2));
      97        1404 :         BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - num2));
      98             :     }
      99             : 
     100        2808 :     invalid = ((num1 > 0 && num2 < std::numeric_limits<int64_t>::min() + num1) ||
     101        1404 :                (num1 < 0 && num2 > std::numeric_limits<int64_t>::max() + num1));
     102        1404 :     if (!invalid)
     103             :     {
     104        1404 :         BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - scriptnum1));
     105        1404 :         BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - num1));
     106             :     }
     107        1404 : }
     108             : 
     109        1404 : static void CheckCompare(const int64_t& num1, const int64_t& num2)
     110             : {
     111        1404 :     const CScriptNum10 bignum1(num1);
     112        1404 :     const CScriptNum10 bignum2(num2);
     113        1404 :     const CScriptNum scriptnum1(num1);
     114        1404 :     const CScriptNum scriptnum2(num2);
     115             : 
     116        1404 :     BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == scriptnum1));
     117        1404 :     BOOST_CHECK((bignum1 != bignum1) ==  (scriptnum1 != scriptnum1));
     118        1404 :     BOOST_CHECK((bignum1 < bignum1) ==  (scriptnum1 < scriptnum1));
     119        1404 :     BOOST_CHECK((bignum1 > bignum1) ==  (scriptnum1 > scriptnum1));
     120        1404 :     BOOST_CHECK((bignum1 >= bignum1) ==  (scriptnum1 >= scriptnum1));
     121        1404 :     BOOST_CHECK((bignum1 <= bignum1) ==  (scriptnum1 <= scriptnum1));
     122             : 
     123        1404 :     BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == num1));
     124        1404 :     BOOST_CHECK((bignum1 != bignum1) ==  (scriptnum1 != num1));
     125        1404 :     BOOST_CHECK((bignum1 < bignum1) ==  (scriptnum1 < num1));
     126        1404 :     BOOST_CHECK((bignum1 > bignum1) ==  (scriptnum1 > num1));
     127        1404 :     BOOST_CHECK((bignum1 >= bignum1) ==  (scriptnum1 >= num1));
     128        1404 :     BOOST_CHECK((bignum1 <= bignum1) ==  (scriptnum1 <= num1));
     129             : 
     130        1404 :     BOOST_CHECK((bignum1 == bignum2) ==  (scriptnum1 == scriptnum2));
     131        1404 :     BOOST_CHECK((bignum1 != bignum2) ==  (scriptnum1 != scriptnum2));
     132        1404 :     BOOST_CHECK((bignum1 < bignum2) ==  (scriptnum1 < scriptnum2));
     133        1404 :     BOOST_CHECK((bignum1 > bignum2) ==  (scriptnum1 > scriptnum2));
     134        1404 :     BOOST_CHECK((bignum1 >= bignum2) ==  (scriptnum1 >= scriptnum2));
     135        1404 :     BOOST_CHECK((bignum1 <= bignum2) ==  (scriptnum1 <= scriptnum2));
     136             : 
     137        1404 :     BOOST_CHECK((bignum1 == bignum2) ==  (scriptnum1 == num2));
     138        1404 :     BOOST_CHECK((bignum1 != bignum2) ==  (scriptnum1 != num2));
     139        1404 :     BOOST_CHECK((bignum1 < bignum2) ==  (scriptnum1 < num2));
     140        1404 :     BOOST_CHECK((bignum1 > bignum2) ==  (scriptnum1 > num2));
     141        1404 :     BOOST_CHECK((bignum1 >= bignum2) ==  (scriptnum1 >= num2));
     142        1404 :     BOOST_CHECK((bignum1 <= bignum2) ==  (scriptnum1 <= num2));
     143        1404 : }
     144             : 
     145         351 : static void RunCreate(const int64_t& num)
     146             : {
     147         351 :     CheckCreateInt(num);
     148         351 :     CScriptNum scriptnum(num);
     149         351 :     if (scriptnum.getvch().size() <= CScriptNum::nDefaultMaxNumSize)
     150         279 :         CheckCreateVch(num);
     151             :     else
     152             :     {
     153         144 :         BOOST_CHECK_THROW (CheckCreateVch(num), scriptnum10_error);
     154             :     }
     155         423 : }
     156             : 
     157        1404 : static void RunOperators(const int64_t& num1, const int64_t& num2)
     158             : {
     159        1404 :     CheckAdd(num1, num2);
     160        1404 :     CheckSubtract(num1, num2);
     161        1404 :     CheckNegate(num1);
     162        1404 :     CheckCompare(num1, num2);
     163        1404 : }
     164             : 
     165          95 : BOOST_AUTO_TEST_CASE(creation)
     166             : {
     167          14 :     for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
     168             :     {
     169         130 :         for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
     170             :         {
     171         117 :             RunCreate(values[i]);
     172         117 :             RunCreate(values[i] + offsets[j]);
     173         117 :             RunCreate(values[i] - offsets[j]);
     174             :         }
     175             :     }
     176           1 : }
     177             : 
     178          95 : BOOST_AUTO_TEST_CASE(operators)
     179             : {
     180          14 :     for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
     181             :     {
     182         130 :         for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
     183             :         {
     184         117 :             RunOperators(values[i], values[i]);
     185         117 :             RunOperators(values[i], -values[i]);
     186         117 :             RunOperators(values[i], values[j]);
     187         117 :             RunOperators(values[i], -values[j]);
     188         117 :             RunOperators(values[i] + values[j], values[j]);
     189         117 :             RunOperators(values[i] + values[j], -values[j]);
     190         117 :             RunOperators(values[i] - values[j], values[j]);
     191         117 :             RunOperators(values[i] - values[j], -values[j]);
     192         117 :             RunOperators(values[i] + values[j], values[i] + values[j]);
     193         117 :             RunOperators(values[i] + values[j], values[i] - values[j]);
     194         117 :             RunOperators(values[i] - values[j], values[i] + values[j]);
     195         117 :             RunOperators(values[i] - values[j], values[i] - values[j]);
     196             :         }
     197             :     }
     198           1 : }
     199             : 
     200          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15