LCOV - code coverage report
Current view: top level - src/test - uint256_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 228 228
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 19 19
Branches: 50.4 % 1126 567

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-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 <streams.h>
       6                 :             : #include <test/util/setup_common.h>
       7                 :             : #include <uint256.h>
       8                 :             : #include <util/strencodings.h>
       9                 :             : #include <util/transaction_identifier.h>
      10                 :             : 
      11                 :             : #include <boost/test/unit_test.hpp>
      12                 :             : 
      13                 :             : #include <iomanip>
      14                 :             : #include <sstream>
      15                 :             : #include <string>
      16                 :             : #include <string_view>
      17                 :             : #include <vector>
      18                 :             : 
      19                 :             : BOOST_AUTO_TEST_SUITE(uint256_tests)
      20                 :             : 
      21                 :             : const unsigned char R1Array[] =
      22                 :             :     "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
      23                 :             :     "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
      24                 :             : const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
      25                 :             : const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
      26                 :             : const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
      27                 :             : 
      28                 :             : const unsigned char R2Array[] =
      29                 :             :     "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
      30                 :             :     "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
      31                 :             : const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
      32                 :             : const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
      33                 :             : 
      34                 :             : const unsigned char ZeroArray[] =
      35                 :             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      36                 :             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      37                 :             : const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
      38                 :             : const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
      39                 :             : 
      40                 :             : const unsigned char OneArray[] =
      41                 :             :     "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      42                 :             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      43                 :             : const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
      44                 :             : const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
      45                 :             : 
      46                 :             : const unsigned char MaxArray[] =
      47                 :             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
      48                 :             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
      49                 :             : const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
      50                 :             : const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
      51                 :             : 
      52                 :          12 : static std::string ArrayToString(const unsigned char A[], unsigned int width)
      53                 :             : {
      54                 :          12 :     std::stringstream Stream;
      55                 :          12 :     Stream << std::hex;
      56         [ +  + ]:         324 :     for (unsigned int i = 0; i < width; ++i)
      57                 :             :     {
      58   [ +  -  +  - ]:         312 :         Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
      59                 :             :     }
      60         [ +  - ]:          24 :     return Stream.str();
      61                 :          12 : }
      62                 :             : 
      63   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      64                 :             : {
      65                 :             :     // constructor uint256(vector<char>):
      66   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L.ToString(), ArrayToString(R1Array,32));
      67   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1S.ToString(), ArrayToString(R1Array,20));
      68   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R2L.ToString(), ArrayToString(R2Array,32));
      69   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R2S.ToString(), ArrayToString(R2Array,20));
      70   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroL.ToString(), ArrayToString(ZeroArray,32));
      71   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroS.ToString(), ArrayToString(ZeroArray,20));
      72   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(OneL.ToString(), ArrayToString(OneArray,32));
      73   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(OneS.ToString(), ArrayToString(OneArray,20));
      74   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxL.ToString(), ArrayToString(MaxArray,32));
      75   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxS.ToString(), ArrayToString(MaxArray,20));
      76   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray,32));
      77   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(OneS.ToString(), ArrayToString(ZeroArray,20));
      78                 :             : 
      79                 :             :     // == and !=
      80   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(R1L, R2L); BOOST_CHECK_NE(R1S, R2S);
      81   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(ZeroL, OneL); BOOST_CHECK_NE(ZeroS, OneS);
      82   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(OneL, ZeroL); BOOST_CHECK_NE(OneS, ZeroS);
      83   [ +  -  +  -  :           1 :     BOOST_CHECK_NE(MaxL, ZeroL); BOOST_CHECK_NE(MaxS, ZeroS);
             +  -  +  - ]
      84                 :             : 
      85                 :             :     // String Constructor and Copy Constructor
      86   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(R1L.ToString()).value(), R1L);
      87   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
      88   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), ZeroL);
      89   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(OneL.ToString()).value(), OneL);
      90   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(MaxL.ToString()).value(), MaxL);
      91   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromHex(R1ArrayHex).value(), R1L);
      92         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256(R1L), R1L);
      93         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256(ZeroL), ZeroL);
      94         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256(OneL), OneL);
      95                 :             : 
      96   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(R1S.ToString()).value(), R1S);
      97   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(R2S.ToString()).value(), R2S);
      98   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(ZeroS.ToString()).value(), ZeroS);
      99   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(OneS.ToString()).value(), OneS);
     100   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(MaxS.ToString()).value(), MaxS);
     101   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(uint160::FromHex(std::string_view{R1ArrayHex + 24, 40}).value(), R1S);
     102                 :             : 
     103         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint160(R1S), R1S);
     104         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint160(ZeroS), ZeroS);
     105         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint160(OneS), OneS);
     106                 :           1 : }
     107                 :             : 
     108   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     109                 :             : {
     110                 :           1 :     uint256 LastL;
     111         [ +  + ]:         257 :     for (int i = 255; i >= 0; --i) {
     112                 :         256 :         uint256 TmpL;
     113                 :         256 :         *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7));
     114         [ +  - ]:         256 :         BOOST_CHECK_LT(LastL, TmpL);
     115                 :         256 :         LastL = TmpL;
     116                 :             :     }
     117                 :             : 
     118         [ +  - ]:           1 :     BOOST_CHECK_LT(ZeroL, R1L);
     119         [ +  - ]:           1 :     BOOST_CHECK_LT(R2L, R1L);
     120         [ +  - ]:           1 :     BOOST_CHECK_LT(ZeroL, OneL);
     121         [ +  - ]:           1 :     BOOST_CHECK_LT(OneL, MaxL);
     122         [ +  - ]:           1 :     BOOST_CHECK_LT(R1L, MaxL);
     123         [ +  - ]:           1 :     BOOST_CHECK_LT(R2L, MaxL);
     124                 :             : 
     125                 :           1 :     uint160 LastS;
     126         [ +  + ]:         161 :     for (int i = 159; i >= 0; --i) {
     127                 :         160 :         uint160 TmpS;
     128                 :         160 :         *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
     129         [ +  - ]:         160 :         BOOST_CHECK_LT(LastS, TmpS);
     130                 :         160 :         LastS = TmpS;
     131                 :             :     }
     132         [ +  - ]:           1 :     BOOST_CHECK_LT(ZeroS, R1S);
     133         [ +  - ]:           1 :     BOOST_CHECK_LT(R2S, R1S);
     134         [ +  - ]:           1 :     BOOST_CHECK_LT(ZeroS, OneS);
     135         [ +  - ]:           1 :     BOOST_CHECK_LT(OneS, MaxS);
     136         [ +  - ]:           1 :     BOOST_CHECK_LT(R1S, MaxS);
     137         [ +  - ]:           1 :     BOOST_CHECK_LT(R2S, MaxS);
     138                 :             : 
     139                 :             :     // Non-arithmetic uint256s compare from the beginning of their inner arrays:
     140         [ +  - ]:           1 :     BOOST_CHECK_LT(R2L, R1L);
     141                 :             :     // Ensure first element comparisons give the same order as above:
     142         [ +  - ]:           1 :     BOOST_CHECK_LT(*R2L.begin(), *R1L.begin());
     143                 :             :     // Ensure last element comparisons give a different result (swapped params):
     144         [ +  - ]:           1 :     BOOST_CHECK_LT(*(R1L.end()-1), *(R2L.end()-1));
     145                 :             :     // Hex strings represent reverse-encoded bytes, with lexicographic ordering:
     146         [ +  - ]:           1 :     BOOST_CHECK_LT(uint256{"1000000000000000000000000000000000000000000000000000000000000000"},
     147                 :             :                    uint256{"0000000000000000000000000000000000000000000000000000000000000001"});
     148                 :           1 : }
     149                 :             : 
     150   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     151                 :             : {
     152   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
     153   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
     154   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(OneL.GetHex(), OneL.ToString());
     155   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
     156                 :           1 :     uint256 TmpL(R1L);
     157         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(TmpL, R1L);
     158                 :             :     // Verify previous values don't persist when setting to truncated string.
     159                 :           1 :     TmpL.SetHexDeprecated("21");
     160         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
     161   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
     162   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());
     163                 :             : 
     164         [ +  - ]:           2 :     TmpL = uint256::FromHex(R1L.ToString()).value();
     165   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array + uint256::size());
     166   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(TmpL.begin(), TmpL.end(), R1Array, R1Array + uint256::size());
     167   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(R2L.begin(), R2L.end(), R2Array, R2Array + uint256::size());
     168   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(ZeroL.begin(), ZeroL.end(), ZeroArray, ZeroArray + uint256::size());
     169   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(OneL.begin(), OneL.end(), OneArray, OneArray + uint256::size());
     170         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L.size(), sizeof(R1L));
     171         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(R1L), 32);
     172         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L.size(), 32);
     173         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(R2L.size(), 32);
     174         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroL.size(), 32);
     175         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxL.size(), 32);
     176         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L.begin() + 32, R1L.end());
     177         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(R2L.begin() + 32, R2L.end());
     178         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(OneL.begin() + 32, OneL.end());
     179         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxL.begin() + 32, MaxL.end());
     180         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(TmpL.begin() + 32, TmpL.end());
     181         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(R1L), 32);
     182         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(ZeroL), 32);
     183                 :             : 
     184                 :           1 :     DataStream ss{};
     185         [ +  - ]:           1 :     ss << R1L;
     186   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+32));
             +  -  +  - ]
     187         [ +  - ]:           1 :     ss >> TmpL;
     188   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1L, TmpL);
     189         [ -  + ]:           1 :     ss.clear();
     190         [ +  - ]:           1 :     ss << ZeroL;
     191   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(ZeroArray,ZeroArray+32));
             +  -  +  - ]
     192         [ +  - ]:           1 :     ss >> TmpL;
     193   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroL, TmpL);
     194         [ -  + ]:           1 :     ss.clear();
     195         [ +  - ]:           1 :     ss << MaxL;
     196   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(MaxArray,MaxArray+32));
             +  -  +  - ]
     197         [ +  - ]:           1 :     ss >> TmpL;
     198   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxL, TmpL);
     199         [ -  + ]:           1 :     ss.clear();
     200                 :             : 
     201   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(R1S.GetHex(), R1S.ToString());
             +  -  +  - ]
     202   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(R2S.GetHex(), R2S.ToString());
             +  -  +  - ]
     203   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(OneS.GetHex(), OneS.ToString());
             +  -  +  - ]
     204   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(MaxS.GetHex(), MaxS.ToString());
             +  -  +  - ]
     205                 :           1 :     uint160 TmpS(R1S);
     206   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(TmpS, R1S);
     207   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(R2S.ToString()).value(), R2S);
             +  -  +  - ]
     208   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(uint160::FromHex(ZeroS.ToString()).value(), uint160());
             +  -  +  - ]
     209                 :             : 
     210   [ +  -  +  - ]:           2 :     TmpS = uint160::FromHex(R1S.ToString()).value();
     211   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(R1S.begin(), R1S.end(), R1Array, R1Array + uint160::size());
             +  -  +  - ]
     212   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(TmpS.begin(), TmpS.end(), R1Array, R1Array + uint160::size());
             +  -  +  - ]
     213   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(R2S.begin(), R2S.end(), R2Array, R2Array + uint160::size());
             +  -  +  - ]
     214   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(ZeroS.begin(), ZeroS.end(), ZeroArray, ZeroArray + uint160::size());
             +  -  +  - ]
     215   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(OneS.begin(), OneS.end(), OneArray, OneArray + uint160::size());
             +  -  +  - ]
     216   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1S.size(), sizeof(R1S));
     217   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(R1S), 20);
     218   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1S.size(), 20);
     219   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R2S.size(), 20);
     220   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroS.size(), 20);
     221   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxS.size(), 20);
     222   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1S.begin() + 20, R1S.end());
     223   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R2S.begin() + 20, R2S.end());
     224   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(OneS.begin() + 20, OneS.end());
     225   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxS.begin() + 20, MaxS.end());
     226   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(TmpS.begin() + 20, TmpS.end());
     227   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(R1S), 20);
     228   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(ZeroS), 20);
     229                 :             : 
     230         [ +  - ]:           1 :     ss << R1S;
     231   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+20));
             +  -  +  - ]
     232         [ +  - ]:           1 :     ss >> TmpS;
     233   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(R1S, TmpS);
     234         [ -  + ]:           1 :     ss.clear();
     235         [ +  - ]:           1 :     ss << ZeroS;
     236   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(ZeroArray,ZeroArray+20));
             +  -  +  - ]
     237         [ +  - ]:           1 :     ss >> TmpS;
     238   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(ZeroS, TmpS);
     239         [ -  + ]:           1 :     ss.clear();
     240         [ +  - ]:           1 :     ss << MaxS;
     241   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(ss.str(), std::string(MaxArray,MaxArray+20));
             +  -  +  - ]
     242         [ +  - ]:           1 :     ss >> TmpS;
     243   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(MaxS, TmpS);
     244         [ -  + ]:           1 :     ss.clear();
     245                 :           1 : }
     246                 :             : 
     247                 :             : /**
     248                 :             :  * Implemented as a templated function so it can be reused by other classes that have a FromHex()
     249                 :             :  * method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex().
     250                 :             :  */
     251                 :             : template <typename T>
     252                 :           4 : void TestFromHex()
     253                 :             : {
     254                 :           4 :     constexpr unsigned int num_chars{T::size() * 2};
     255                 :             :     static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars
     256                 :           4 :     const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
     257         [ +  - ]:           4 :     const auto valid_input{valid_64char_input.substr(0, num_chars)};
     258                 :             :     {
     259                 :             :         // check that lower and upper case hex characters are accepted
     260         [ +  - ]:           4 :         auto valid_result{T::FromHex(valid_input)};
     261   [ +  -  +  -  :           8 :         BOOST_REQUIRE(valid_result);
                   +  - ]
     262   [ +  -  +  -  :           4 :         BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));
             +  -  +  - ]
     263                 :             :     }
     264                 :             :     {
     265                 :             :         // check that only strings of size num_chars are accepted
     266   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(""));
             +  -  +  - ]
     267   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex("0"));
             +  -  +  - ]
     268   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));
          +  -  +  -  +  
                      - ]
     269   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));
          +  -  +  -  +  
                      - ]
     270   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(valid_input + "0"));
          +  -  +  -  +  
                      - ]
     271                 :             :     }
     272                 :             :     {
     273                 :             :         // check that non-hex characters are not accepted
     274         [ +  - ]:           4 :         std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
     275         [ +  + ]:         296 :         for (auto c : invalid_chars) {
     276   [ +  -  +  -  :         584 :             BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));
          +  -  +  -  +  
                      - ]
     277                 :             :         }
     278                 :             :         // 0x prefixes are invalid
     279         [ +  - ]:           4 :         std::string invalid_prefix{"0x" + valid_input};
     280   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));
             +  -  +  - ]
     281   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(invalid_prefix));
                   +  - ]
     282                 :           4 :     }
     283                 :             :     {
     284                 :             :         // check that string_view length is respected
     285         [ +  - ]:           4 :         std::string chars_68{valid_64char_input + "0123"};
     286   [ +  -  +  -  :           8 :         BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));
          +  -  +  -  +  
                      - ]
     287   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short
             +  -  +  - ]
     288   [ +  -  +  -  :           8 :         BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long
                   +  - ]
     289                 :           4 :     }
     290                 :           4 : }
     291                 :             : 
     292   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(from_hex)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     293                 :             : {
     294                 :           1 :     TestFromHex<uint160>();
     295                 :           1 :     TestFromHex<uint256>();
     296                 :           1 :     TestFromHex<Txid>();
     297                 :           1 :     TestFromHex<Wtxid>();
     298                 :           1 : }
     299                 :             : 
     300   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(from_user_hex)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     301                 :             : {
     302         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex(""), uint256::ZERO);
     303         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("0x"), uint256::ZERO);
     304         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("0"), uint256::ZERO);
     305         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("00"), uint256::ZERO);
     306         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("1"), uint256::ONE);
     307         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("0x10"), uint256{0x10});
     308         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("10"), uint256{0x10});
     309         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("0xFf"), uint256{0xff});
     310         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(uint256::FromUserHex("Ff"), uint256{0xff});
     311                 :           1 :     const std::string valid_hex_64{"0x0123456789abcdef0123456789abcdef0123456789ABDCEF0123456789ABCDEF"};
     312   [ +  -  +  - ]:           1 :     BOOST_REQUIRE_EQUAL(valid_hex_64.size(), 2 + 64); // 0x prefix and 64 hex digits
     313   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(2)).value().ToString(), ToLower(valid_hex_64.substr(2)));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     314   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(uint256::FromUserHex(valid_hex_64.substr(0)).value().ToString(), ToLower(valid_hex_64.substr(2)));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     315                 :             : 
     316   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex("0x0 "));                       // no spaces at end,
             +  -  +  - ]
     317   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex(" 0x0"));                       // or beginning,
             +  -  +  - ]
     318   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex("0x 0"));                       // or middle,
             +  -  +  - ]
     319   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex(" "));                          // etc.
             +  -  +  - ]
     320   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex("0x0ga"));                      // invalid character
             +  -  +  - ]
     321   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex("x0"));                         // broken prefix
             +  -  +  - ]
     322   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex("0x0x00"));                     // two prefixes not allowed
             +  -  +  - ]
     323   [ +  -  +  -  :           3 :     BOOST_CHECK(!uint256::FromUserHex(valid_hex_64.substr(2) + "0")); // 1 hex digit too many
          +  -  +  -  +  
                      - ]
     324   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "a"));           // 1 hex digit too many
          +  -  +  -  +  
                      - ]
     325   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + " "));           // whitespace after max length
          +  -  +  -  +  
                      - ]
     326   [ +  -  +  -  :           2 :     BOOST_CHECK(!uint256::FromUserHex(valid_hex_64 + "z"));           // invalid character after max length
             +  -  +  - ]
     327                 :           1 : }
     328                 :             : 
     329   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE( check_ONE )
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     330                 :             : {
     331                 :           1 :     uint256 one = uint256{"0000000000000000000000000000000000000000000000000000000000000001"};
     332         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(one, uint256::ONE);
     333                 :           1 : }
     334                 :             : 
     335   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(FromHex_vs_uint256)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     336                 :             : {
     337                 :           1 :     auto runtime_uint{uint256::FromHex("4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b")};
     338                 :           1 :     constexpr uint256 consteval_uint{  "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};
     339         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(consteval_uint, runtime_uint);
     340                 :           1 : }
     341                 :             : 
     342                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1