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

          Line data    Source code
       1             : // Copyright (c) 2011-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 <arith_uint256.h>
       6             : #include <test/util/setup_common.h>
       7             : #include <uint256.h>
       8             : 
       9             : #include <boost/test/unit_test.hpp>
      10             : 
      11             : #include <cmath>
      12             : #include <iomanip>
      13             : #include <limits>
      14             : #include <sstream>
      15             : #include <stdint.h>
      16             : #include <string>
      17             : 
      18          89 : BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup)
      19             : 
      20             : /// Convert vector to arith_uint256, via uint256 blob
      21        1747 : static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
      22             : {
      23        1747 :     return UintToArith256(uint256(vch));
      24             : }
      25             : 
      26             : const unsigned char R1Array[] =
      27             :     "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
      28             :     "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
      29             : const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
      30             : const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
      31          89 : const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32));
      32             : const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
      33             : 
      34             : const unsigned char R2Array[] =
      35             :     "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
      36             :     "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
      37          89 : const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
      38             : 
      39             : const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
      40             : 
      41             : const unsigned char ZeroArray[] =
      42             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      43             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      44          89 : const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
      45             : 
      46             : const unsigned char OneArray[] =
      47             :     "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      48             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      49          89 : const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));
      50             : 
      51             : const unsigned char MaxArray[] =
      52             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
      53             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
      54          89 : const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));
      55             : 
      56          89 : const arith_uint256 HalfL = (OneL << 255);
      57           6 : static std::string ArrayToString(const unsigned char A[], unsigned int width)
      58             : {
      59           6 :     std::stringstream Stream;
      60           6 :     Stream << std::hex;
      61         198 :     for (unsigned int i = 0; i < width; ++i)
      62             :     {
      63         192 :         Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
      64             :     }
      65           6 :     return Stream.str();
      66           6 : }
      67             : 
      68          95 : BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
      69             : {
      70           1 :     BOOST_CHECK(1 == 0+1);
      71             :     // constructor arith_uint256(vector<char>):
      72           1 :     BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
      73           1 :     BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
      74           1 :     BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
      75           1 :     BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
      76           1 :     BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
      77           1 :     BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
      78             : 
      79             :     // == and !=
      80           1 :     BOOST_CHECK(R1L != R2L);
      81           1 :     BOOST_CHECK(ZeroL != OneL);
      82           1 :     BOOST_CHECK(OneL != ZeroL);
      83           1 :     BOOST_CHECK(MaxL != ZeroL);
      84           1 :     BOOST_CHECK(~MaxL == ZeroL);
      85           1 :     BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
      86             : 
      87             :     uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
      88         257 :     for (unsigned int i = 0; i < 256; ++i)
      89             :     {
      90         256 :         BOOST_CHECK(ZeroL != (OneL << i));
      91         256 :         BOOST_CHECK((OneL << i) != ZeroL);
      92         256 :         BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
      93         256 :         BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
      94             :     }
      95           1 :     BOOST_CHECK(ZeroL == (OneL << 256));
      96             : 
      97             :     // String Constructor and Copy Constructor
      98           1 :     BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L);
      99           1 :     BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L);
     100           1 :     BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL);
     101           1 :     BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL);
     102           1 :     BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL);
     103           1 :     BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L);
     104           1 :     BOOST_CHECK(arith_uint256("   0x"+R1L.ToString()+"   ") == R1L);
     105           1 :     BOOST_CHECK(arith_uint256("") == ZeroL);
     106           1 :     BOOST_CHECK(R1L == arith_uint256(R1ArrayHex));
     107           1 :     BOOST_CHECK(arith_uint256(R1L) == R1L);
     108           1 :     BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
     109           1 :     BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
     110           1 :     BOOST_CHECK(arith_uint256(OneL) == OneL);
     111             : 
     112             :     // uint64_t constructor
     113           1 :     BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
     114           1 :     BOOST_CHECK(ZeroL == arith_uint256(0));
     115           1 :     BOOST_CHECK(OneL == arith_uint256(1));
     116           1 :     BOOST_CHECK(arith_uint256("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL));
     117             : 
     118             :     // Assignment (from base_uint)
     119           1 :     arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
     120           1 :     tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
     121           1 :     tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
     122           1 :     tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
     123           1 :     tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
     124           1 : }
     125             : 
     126         512 : static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
     127             : {
     128       16896 :     for (unsigned int T=0; T < arrayLength; ++T)
     129             :     {
     130       16384 :         unsigned int F = (T+bitsToShift/8);
     131       16384 :         if (F < arrayLength)
     132        8448 :             to[T]  = from[F] >> (bitsToShift%8);
     133             :         else
     134        7936 :             to[T] = 0;
     135       16384 :         if (F + 1 < arrayLength)
     136        7936 :             to[T] |= from[(F+1)] << (8-bitsToShift%8);
     137             :     }
     138         512 : }
     139             : 
     140         768 : static void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
     141             : {
     142       25344 :     for (unsigned int T=0; T < arrayLength; ++T)
     143             :     {
     144       24576 :         if (T >= bitsToShift/8)
     145             :         {
     146       12672 :             unsigned int F = T-bitsToShift/8;
     147       12672 :             to[T]  = from[F] << (bitsToShift%8);
     148       12672 :             if (T >= bitsToShift/8+1)
     149       11904 :                 to[T] |= from[F-1] >> (8-bitsToShift%8);
     150       12672 :         }
     151             :         else {
     152       11904 :             to[T] = 0;
     153             :         }
     154             :     }
     155         768 : }
     156             : 
     157          95 : BOOST_AUTO_TEST_CASE( shifts ) { // "<<"  ">>"  "<<="  ">>="
     158           1 :     unsigned char TmpArray[32];
     159           1 :     arith_uint256 TmpL;
     160         257 :     for (unsigned int i = 0; i < 256; ++i)
     161             :     {
     162         256 :         shiftArrayLeft(TmpArray, OneArray, 32, i);
     163         256 :         BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
     164         256 :         TmpL = OneL; TmpL <<= i;
     165         256 :         BOOST_CHECK(TmpL == (OneL << i));
     166         256 :         BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
     167         256 :         TmpL = HalfL; TmpL >>= (255-i);
     168         256 :         BOOST_CHECK(TmpL == (OneL << i));
     169             : 
     170         256 :         shiftArrayLeft(TmpArray, R1Array, 32, i);
     171         256 :         BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
     172         256 :         TmpL = R1L; TmpL <<= i;
     173         256 :         BOOST_CHECK(TmpL == (R1L << i));
     174             : 
     175         256 :         shiftArrayRight(TmpArray, R1Array, 32, i);
     176         256 :         BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
     177         256 :         TmpL = R1L; TmpL >>= i;
     178         256 :         BOOST_CHECK(TmpL == (R1L >> i));
     179             : 
     180         256 :         shiftArrayLeft(TmpArray, MaxArray, 32, i);
     181         256 :         BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
     182         256 :         TmpL = MaxL; TmpL <<= i;
     183         256 :         BOOST_CHECK(TmpL == (MaxL << i));
     184             : 
     185         256 :         shiftArrayRight(TmpArray, MaxArray, 32, i);
     186         256 :         BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
     187         256 :         TmpL = MaxL; TmpL >>= i;
     188         256 :         BOOST_CHECK(TmpL == (MaxL >> i));
     189             :     }
     190           1 :     arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
     191           1 :     arith_uint256 c2L = c1L << 128;
     192         129 :     for (unsigned int i = 0; i < 128; ++i) {
     193         128 :         BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
     194             :     }
     195         129 :     for (unsigned int i = 128; i < 256; ++i) {
     196         128 :         BOOST_CHECK((c1L << i) == (c2L << (i-128)));
     197             :     }
     198           1 : }
     199             : 
     200          95 : BOOST_AUTO_TEST_CASE( unaryOperators ) // !    ~    -
     201             : {
     202           1 :     BOOST_CHECK(~ZeroL == MaxL);
     203             : 
     204           1 :     unsigned char TmpArray[32];
     205          33 :     for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
     206           1 :     BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
     207             : 
     208           1 :     BOOST_CHECK(-ZeroL == ZeroL);
     209           1 :     BOOST_CHECK(-R1L == (~R1L)+1);
     210         257 :     for (unsigned int i = 0; i < 256; ++i)
     211         256 :         BOOST_CHECK(-(OneL<<i) == (MaxL << i));
     212           1 : }
     213             : 
     214             : 
     215             : // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
     216             : // element of Aarray and Barray, and then converting the result into an arith_uint256.
     217             : #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_)                              \
     218             :     for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
     219             :     BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
     220             : 
     221             : #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_)                           \
     222             :     TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
     223             : 
     224          95 : BOOST_AUTO_TEST_CASE( bitwiseOperators )
     225             : {
     226           1 :     unsigned char TmpArray[32];
     227             : 
     228          33 :     CHECKBITWISEOPERATOR(R1,R2,|)
     229          33 :     CHECKBITWISEOPERATOR(R1,R2,^)
     230          33 :     CHECKBITWISEOPERATOR(R1,R2,&)
     231          33 :     CHECKBITWISEOPERATOR(R1,Zero,|)
     232          33 :     CHECKBITWISEOPERATOR(R1,Zero,^)
     233          33 :     CHECKBITWISEOPERATOR(R1,Zero,&)
     234          33 :     CHECKBITWISEOPERATOR(R1,Max,|)
     235          33 :     CHECKBITWISEOPERATOR(R1,Max,^)
     236          33 :     CHECKBITWISEOPERATOR(R1,Max,&)
     237          33 :     CHECKBITWISEOPERATOR(Zero,R1,|)
     238          33 :     CHECKBITWISEOPERATOR(Zero,R1,^)
     239          33 :     CHECKBITWISEOPERATOR(Zero,R1,&)
     240          33 :     CHECKBITWISEOPERATOR(Max,R1,|)
     241          33 :     CHECKBITWISEOPERATOR(Max,R1,^)
     242          33 :     CHECKBITWISEOPERATOR(Max,R1,&)
     243             : 
     244           1 :     arith_uint256 TmpL;
     245           1 :     CHECKASSIGNMENTOPERATOR(R1,R2,|)
     246           1 :     CHECKASSIGNMENTOPERATOR(R1,R2,^)
     247           1 :     CHECKASSIGNMENTOPERATOR(R1,R2,&)
     248           1 :     CHECKASSIGNMENTOPERATOR(R1,Zero,|)
     249           1 :     CHECKASSIGNMENTOPERATOR(R1,Zero,^)
     250           1 :     CHECKASSIGNMENTOPERATOR(R1,Zero,&)
     251           1 :     CHECKASSIGNMENTOPERATOR(R1,Max,|)
     252           1 :     CHECKASSIGNMENTOPERATOR(R1,Max,^)
     253           1 :     CHECKASSIGNMENTOPERATOR(R1,Max,&)
     254           1 :     CHECKASSIGNMENTOPERATOR(Zero,R1,|)
     255           1 :     CHECKASSIGNMENTOPERATOR(Zero,R1,^)
     256           1 :     CHECKASSIGNMENTOPERATOR(Zero,R1,&)
     257           1 :     CHECKASSIGNMENTOPERATOR(Max,R1,|)
     258           1 :     CHECKASSIGNMENTOPERATOR(Max,R1,^)
     259           1 :     CHECKASSIGNMENTOPERATOR(Max,R1,&)
     260             : 
     261             :     uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
     262           1 :     TmpL = R1L; TmpL |= Tmp64;  BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
     263           1 :     TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
     264           1 :     TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
     265           1 :     TmpL ^= Tmp64;  BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
     266           1 : }
     267             : 
     268          95 : BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
     269             : {
     270           1 :     arith_uint256 TmpL;
     271         257 :     for (unsigned int i = 0; i < 256; ++i) {
     272         256 :         TmpL= OneL<< i;
     273         256 :         BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
     274         256 :         BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
     275         256 :         TmpL |= R1L;
     276         256 :         BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
     277         256 :         BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
     278         256 :         BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
     279             :     }
     280           1 : }
     281             : 
     282          95 : BOOST_AUTO_TEST_CASE( plusMinus )
     283             : {
     284           1 :     arith_uint256 TmpL = 0;
     285           1 :     BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L));
     286           1 :     TmpL += R1L;
     287           1 :     BOOST_CHECK(TmpL == R1L);
     288           1 :     TmpL += R2L;
     289           1 :     BOOST_CHECK(TmpL == R1L + R2L);
     290           1 :     BOOST_CHECK(OneL+MaxL == ZeroL);
     291           1 :     BOOST_CHECK(MaxL+OneL == ZeroL);
     292         256 :     for (unsigned int i = 1; i < 256; ++i) {
     293         255 :         BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
     294         255 :         BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
     295         255 :         TmpL = (MaxL>>i); TmpL += OneL;
     296         255 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
     297         255 :         TmpL = (MaxL>>i); TmpL += 1;
     298         255 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
     299         255 :         TmpL = (MaxL>>i);
     300         255 :         BOOST_CHECK( TmpL++ == (MaxL>>i) );
     301         255 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)));
     302             :     }
     303           1 :     BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
     304           1 :     TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
     305           1 :     BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
     306           1 :     TmpL -= 0xee8d836fce66fbULL;  BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
     307           1 :     TmpL = R1L;
     308           1 :     BOOST_CHECK(++TmpL == R1L+1);
     309             : 
     310           1 :     BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
     311           1 :     BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
     312           1 :     BOOST_CHECK(R1L - OneL == R1L+(-OneL));
     313         256 :     for (unsigned int i = 1; i < 256; ++i) {
     314         255 :         BOOST_CHECK((MaxL>>i) - (-OneL)  == (HalfL >> (i-1)));
     315         255 :         BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
     316         255 :         TmpL = (HalfL >> (i-1));
     317         255 :         BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
     318         255 :         BOOST_CHECK(TmpL == (MaxL >> i));
     319         255 :         TmpL = (HalfL >> (i-1));
     320         255 :         BOOST_CHECK(--TmpL == (MaxL >> i));
     321             :     }
     322           1 :     TmpL = R1L;
     323           1 :     BOOST_CHECK(--TmpL == R1L-1);
     324           1 : }
     325             : 
     326          95 : BOOST_AUTO_TEST_CASE( multiply )
     327             : {
     328           1 :     BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
     329           1 :     BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
     330           1 :     BOOST_CHECK((R1L * ZeroL) == ZeroL);
     331           1 :     BOOST_CHECK((R1L * OneL) == R1L);
     332           1 :     BOOST_CHECK((R1L * MaxL) == -R1L);
     333           1 :     BOOST_CHECK((R2L * R1L) == (R1L * R2L));
     334           1 :     BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
     335           1 :     BOOST_CHECK((R2L * ZeroL) == ZeroL);
     336           1 :     BOOST_CHECK((R2L * OneL) == R2L);
     337           1 :     BOOST_CHECK((R2L * MaxL) == -R2L);
     338             : 
     339           1 :     BOOST_CHECK(MaxL * MaxL == OneL);
     340             : 
     341           1 :     BOOST_CHECK((R1L * 0) == 0);
     342           1 :     BOOST_CHECK((R1L * 1) == R1L);
     343           1 :     BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
     344           1 :     BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
     345           1 : }
     346             : 
     347          95 : BOOST_AUTO_TEST_CASE( divide )
     348             : {
     349           1 :     arith_uint256 D1L("AD7133AC1977FA2B7");
     350           1 :     arith_uint256 D2L("ECD751716");
     351           1 :     BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
     352           1 :     BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
     353           1 :     BOOST_CHECK(R1L / OneL == R1L);
     354           1 :     BOOST_CHECK(R1L / MaxL == ZeroL);
     355           1 :     BOOST_CHECK(MaxL / R1L == 2);
     356           1 :     BOOST_CHECK_THROW(R1L / ZeroL, uint_error);
     357           1 :     BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
     358           1 :     BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
     359           1 :     BOOST_CHECK(R2L / OneL == R2L);
     360           1 :     BOOST_CHECK(R2L / MaxL == ZeroL);
     361           1 :     BOOST_CHECK(MaxL / R2L == 1);
     362           1 :     BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
     363           3 : }
     364             : 
     365             : 
     366         203 : static bool almostEqual(double d1, double d2)
     367             : {
     368         203 :     return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
     369             : }
     370             : 
     371          95 : BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize
     372             : {
     373           1 :     BOOST_CHECK(R1L.GetHex() == R1L.ToString());
     374           1 :     BOOST_CHECK(R2L.GetHex() == R2L.ToString());
     375           1 :     BOOST_CHECK(OneL.GetHex() == OneL.ToString());
     376           1 :     BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
     377           1 :     arith_uint256 TmpL(R1L);
     378           1 :     BOOST_CHECK(TmpL == R1L);
     379           1 :     TmpL.SetHex(R2L.ToString());   BOOST_CHECK(TmpL == R2L);
     380           1 :     TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
     381           1 :     TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL);
     382             : 
     383           1 :     TmpL.SetHex(R1L.ToString());
     384           1 :     BOOST_CHECK(R1L.size() == 32);
     385           1 :     BOOST_CHECK(R2L.size() == 32);
     386           1 :     BOOST_CHECK(ZeroL.size() == 32);
     387           1 :     BOOST_CHECK(MaxL.size() == 32);
     388           1 :     BOOST_CHECK(R1L.GetLow64()  == R1LLow64);
     389           1 :     BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
     390           1 :     BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
     391             : 
     392         256 :     for (unsigned int i = 0; i < 255; ++i)
     393             :     {
     394         255 :         BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
     395             :     }
     396           1 :     BOOST_CHECK(ZeroL.getdouble() == 0.0);
     397         204 :     for (int i = 256; i > 53; --i)
     398         203 :         BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
     399           1 :     uint64_t R1L64part = (R1L>>192).GetLow64();
     400          54 :     for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
     401             :     {
     402          53 :         BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
     403             :     }
     404           1 : }
     405             : 
     406          95 : BOOST_AUTO_TEST_CASE(bignum_SetCompact)
     407             : {
     408           1 :     arith_uint256 num;
     409           1 :     bool fNegative;
     410           1 :     bool fOverflow;
     411           1 :     num.SetCompact(0, &fNegative, &fOverflow);
     412           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     413           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     414           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     415           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     416             : 
     417           1 :     num.SetCompact(0x00123456, &fNegative, &fOverflow);
     418           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     419           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     420           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     421           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     422             : 
     423           1 :     num.SetCompact(0x01003456, &fNegative, &fOverflow);
     424           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     425           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     426           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     427           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     428             : 
     429           1 :     num.SetCompact(0x02000056, &fNegative, &fOverflow);
     430           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     431           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     432           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     433           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     434             : 
     435           1 :     num.SetCompact(0x03000000, &fNegative, &fOverflow);
     436           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     437           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     438           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     439           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     440             : 
     441           1 :     num.SetCompact(0x04000000, &fNegative, &fOverflow);
     442           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     443           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     444           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     445           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     446             : 
     447           1 :     num.SetCompact(0x00923456, &fNegative, &fOverflow);
     448           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     449           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     450           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     451           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     452             : 
     453           1 :     num.SetCompact(0x01803456, &fNegative, &fOverflow);
     454           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     455           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     456           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     457           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     458             : 
     459           1 :     num.SetCompact(0x02800056, &fNegative, &fOverflow);
     460           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     461           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     462           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     463           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     464             : 
     465           1 :     num.SetCompact(0x03800000, &fNegative, &fOverflow);
     466           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     467           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     468           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     469           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     470             : 
     471           1 :     num.SetCompact(0x04800000, &fNegative, &fOverflow);
     472           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     473           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     474           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     475           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     476             : 
     477           1 :     num.SetCompact(0x01123456, &fNegative, &fOverflow);
     478           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
     479           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
     480           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     481           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     482             : 
     483             :     // Make sure that we don't generate compacts with the 0x00800000 bit set
     484           1 :     num = 0x80;
     485           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
     486             : 
     487           1 :     num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
     488           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
     489           1 :     BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
     490           1 :     BOOST_CHECK_EQUAL(fNegative, true);
     491           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     492             : 
     493           1 :     num.SetCompact(0x02123456, &fNegative, &fOverflow);
     494           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
     495           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
     496           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     497           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     498             : 
     499           1 :     num.SetCompact(0x03123456, &fNegative, &fOverflow);
     500           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
     501           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
     502           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     503           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     504             : 
     505           1 :     num.SetCompact(0x04123456, &fNegative, &fOverflow);
     506           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
     507           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
     508           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     509           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     510             : 
     511           1 :     num.SetCompact(0x04923456, &fNegative, &fOverflow);
     512           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
     513           1 :     BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
     514           1 :     BOOST_CHECK_EQUAL(fNegative, true);
     515           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     516             : 
     517           1 :     num.SetCompact(0x05009234, &fNegative, &fOverflow);
     518           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
     519           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
     520           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     521           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     522             : 
     523           1 :     num.SetCompact(0x20123456, &fNegative, &fOverflow);
     524           1 :     BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
     525           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
     526           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     527           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     528             : 
     529           1 :     num.SetCompact(0xff123456, &fNegative, &fOverflow);
     530           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     531           1 :     BOOST_CHECK_EQUAL(fOverflow, true);
     532           1 : }
     533             : 
     534             : 
     535          95 : BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
     536             : {
     537             :     // ~R1L give a base_uint<256>
     538           1 :     BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
     539           1 :     BOOST_CHECK((~~R1L << 10) == (R1L << 10));
     540           1 :     BOOST_CHECK(!(~~R1L < R1L));
     541           1 :     BOOST_CHECK(~~R1L <= R1L);
     542           1 :     BOOST_CHECK(!(~~R1L > R1L));
     543           1 :     BOOST_CHECK(~~R1L >= R1L);
     544           1 :     BOOST_CHECK(!(R1L < ~~R1L));
     545           1 :     BOOST_CHECK(R1L <= ~~R1L);
     546           1 :     BOOST_CHECK(!(R1L > ~~R1L));
     547           1 :     BOOST_CHECK(R1L >= ~~R1L);
     548             : 
     549           1 :     BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
     550           1 :     BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
     551           1 :     BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
     552           1 :     unsigned char TmpArray[32];
     553          33 :     CHECKBITWISEOPERATOR(~R1,R2,|)
     554          33 :     CHECKBITWISEOPERATOR(~R1,R2,^)
     555          33 :     CHECKBITWISEOPERATOR(~R1,R2,&)
     556          33 :     CHECKBITWISEOPERATOR(R1,~R2,|)
     557          33 :     CHECKBITWISEOPERATOR(R1,~R2,^)
     558          33 :     CHECKBITWISEOPERATOR(R1,~R2,&)
     559           1 : }
     560             : 
     561          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15