diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/Checkpoints_tests.cpp | 4 | ||||
-rw-r--r-- | src/test/arith_uint256_tests.cpp | 566 | ||||
-rw-r--r-- | src/test/bloom_tests.cpp | 58 | ||||
-rw-r--r-- | src/test/pmt_tests.cpp | 18 | ||||
-rw-r--r-- | src/test/script_P2SH_tests.cpp | 2 | ||||
-rw-r--r-- | src/test/sighash_tests.cpp | 5 | ||||
-rw-r--r-- | src/test/sigopcount_tests.cpp | 2 | ||||
-rw-r--r-- | src/test/skiplist_tests.cpp | 12 | ||||
-rw-r--r-- | src/test/transaction_tests.cpp | 4 | ||||
-rw-r--r-- | src/test/uint256_tests.cpp | 745 |
10 files changed, 710 insertions, 706 deletions
diff --git a/src/test/Checkpoints_tests.cpp b/src/test/Checkpoints_tests.cpp index 4ea6a91d54..a9b6cd44a8 100644 --- a/src/test/Checkpoints_tests.cpp +++ b/src/test/Checkpoints_tests.cpp @@ -18,8 +18,8 @@ BOOST_AUTO_TEST_SUITE(Checkpoints_tests) BOOST_AUTO_TEST_CASE(sanity) { - uint256 p11111 = uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"); - uint256 p134444 = uint256("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"); + uint256 p11111 = uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"); + uint256 p134444 = uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"); BOOST_CHECK(Checkpoints::CheckBlock(11111, p11111)); BOOST_CHECK(Checkpoints::CheckBlock(134444, p134444)); diff --git a/src/test/arith_uint256_tests.cpp b/src/test/arith_uint256_tests.cpp new file mode 100644 index 0000000000..565b02ae64 --- /dev/null +++ b/src/test/arith_uint256_tests.cpp @@ -0,0 +1,566 @@ +// Copyright (c) 2011-2013 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <boost/test/unit_test.hpp> +#include <stdint.h> +#include <sstream> +#include <iomanip> +#include <limits> +#include <cmath> +#include "uint256.h" +#include "arith_uint256.h" +#include <string> +#include "version.h" + +BOOST_AUTO_TEST_SUITE(arith_uint256_tests) + +/// Convert vector to arith_uint256, via uint256 blob +inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch) +{ + return UintToArith256(uint256(vch)); +} + +const unsigned char R1Array[] = + "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" + "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; +const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; +const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 +const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32)); +const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; + +const unsigned char R2Array[] = + "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" + "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; +const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32)); + +const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; + +const unsigned char ZeroArray[] = + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32)); + +const unsigned char OneArray[] = + "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32)); + +const unsigned char MaxArray[] = + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; +const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32)); + +const arith_uint256 HalfL = (OneL << 255); +std::string ArrayToString(const unsigned char A[], unsigned int width) +{ + std::stringstream Stream; + Stream << std::hex; + for (unsigned int i = 0; i < width; ++i) + { + Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1]; + } + return Stream.str(); +} + +BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality +{ + BOOST_CHECK(1 == 0+1); + // constructor arith_uint256(vector<char>): + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); + + // == and != + BOOST_CHECK(R1L != R2L); + BOOST_CHECK(ZeroL != OneL); + BOOST_CHECK(OneL != ZeroL); + BOOST_CHECK(MaxL != ZeroL); + BOOST_CHECK(~MaxL == ZeroL); + BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); + + uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; + for (unsigned int i = 0; i < 256; ++i) + { + BOOST_CHECK(ZeroL != (OneL << i)); + BOOST_CHECK((OneL << i) != ZeroL); + BOOST_CHECK(R1L != (R1L ^ (OneL << i))); + BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 )); + } + BOOST_CHECK(ZeroL == (OneL << 256)); + + // String Constructor and Copy Constructor + BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L); + BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L); + BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL); + BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL); + BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L); + BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(arith_uint256("") == ZeroL); + BOOST_CHECK(R1L == arith_uint256(R1ArrayHex)); + BOOST_CHECK(arith_uint256(R1L) == R1L); + BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L); + BOOST_CHECK(arith_uint256(ZeroL) == ZeroL); + BOOST_CHECK(arith_uint256(OneL) == OneL); + + // uint64_t constructor + BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64)); + BOOST_CHECK(ZeroL == arith_uint256(0)); + BOOST_CHECK(OneL == arith_uint256(1)); + BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL)); + + // Assignment (from base_uint) + arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); + tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL); + tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L); + tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L); + tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL); +} + +void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) +{ + for (unsigned int T=0; T < arrayLength; ++T) + { + unsigned int F = (T+bitsToShift/8); + if (F < arrayLength) + to[T] = from[F] >> (bitsToShift%8); + else + to[T] = 0; + if (F + 1 < arrayLength) + to[T] |= from[(F+1)] << (8-bitsToShift%8); + } +} + +void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) +{ + for (unsigned int T=0; T < arrayLength; ++T) + { + if (T >= bitsToShift/8) + { + unsigned int F = T-bitsToShift/8; + to[T] = from[F] << (bitsToShift%8); + if (T >= bitsToShift/8+1) + to[T] |= from[F-1] >> (8-bitsToShift%8); + } + else { + to[T] = 0; + } + } +} + +BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" + unsigned char TmpArray[32]; + arith_uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) + { + shiftArrayLeft(TmpArray, OneArray, 32, i); + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i)); + TmpL = OneL; TmpL <<= i; + BOOST_CHECK(TmpL == (OneL << i)); + BOOST_CHECK((HalfL >> (255-i)) == (OneL << i)); + TmpL = HalfL; TmpL >>= (255-i); + BOOST_CHECK(TmpL == (OneL << i)); + + shiftArrayLeft(TmpArray, R1Array, 32, i); + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i)); + TmpL = R1L; TmpL <<= i; + BOOST_CHECK(TmpL == (R1L << i)); + + shiftArrayRight(TmpArray, R1Array, 32, i); + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i)); + TmpL = R1L; TmpL >>= i; + BOOST_CHECK(TmpL == (R1L >> i)); + + shiftArrayLeft(TmpArray, MaxArray, 32, i); + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i)); + TmpL = MaxL; TmpL <<= i; + BOOST_CHECK(TmpL == (MaxL << i)); + + shiftArrayRight(TmpArray, MaxArray, 32, i); + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i)); + TmpL = MaxL; TmpL >>= i; + BOOST_CHECK(TmpL == (MaxL >> i)); + } + arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL); + arith_uint256 c2L = c1L << 128; + for (unsigned int i = 0; i < 128; ++i) { + BOOST_CHECK((c1L << i) == (c2L >> (128-i))); + } + for (unsigned int i = 128; i < 256; ++i) { + BOOST_CHECK((c1L << i) == (c2L << (i-128))); + } +} + +BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - +{ + BOOST_CHECK(!ZeroL); + BOOST_CHECK(!(!OneL)); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(!(!(OneL<<i))); + BOOST_CHECK(!(!R1L)); + BOOST_CHECK(!(!MaxL)); + + BOOST_CHECK(~ZeroL == MaxL); + + unsigned char TmpArray[32]; + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; } + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L)); + + BOOST_CHECK(-ZeroL == ZeroL); + BOOST_CHECK(-R1L == (~R1L)+1); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(-(OneL<<i) == (MaxL << i)); +} + + +// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each +// element of Aarray and Barray, and then converting the result into a arith_uint256. +#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \ + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ + BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); + +#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \ + TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); + +BOOST_AUTO_TEST_CASE( bitwiseOperators ) +{ + unsigned char TmpArray[32]; + + CHECKBITWISEOPERATOR(R1,R2,|) + CHECKBITWISEOPERATOR(R1,R2,^) + CHECKBITWISEOPERATOR(R1,R2,&) + CHECKBITWISEOPERATOR(R1,Zero,|) + CHECKBITWISEOPERATOR(R1,Zero,^) + CHECKBITWISEOPERATOR(R1,Zero,&) + CHECKBITWISEOPERATOR(R1,Max,|) + CHECKBITWISEOPERATOR(R1,Max,^) + CHECKBITWISEOPERATOR(R1,Max,&) + CHECKBITWISEOPERATOR(Zero,R1,|) + CHECKBITWISEOPERATOR(Zero,R1,^) + CHECKBITWISEOPERATOR(Zero,R1,&) + CHECKBITWISEOPERATOR(Max,R1,|) + CHECKBITWISEOPERATOR(Max,R1,^) + CHECKBITWISEOPERATOR(Max,R1,&) + + arith_uint256 TmpL; + CHECKASSIGNMENTOPERATOR(R1,R2,|) + CHECKASSIGNMENTOPERATOR(R1,R2,^) + CHECKASSIGNMENTOPERATOR(R1,R2,&) + CHECKASSIGNMENTOPERATOR(R1,Zero,|) + CHECKASSIGNMENTOPERATOR(R1,Zero,^) + CHECKASSIGNMENTOPERATOR(R1,Zero,&) + CHECKASSIGNMENTOPERATOR(R1,Max,|) + CHECKASSIGNMENTOPERATOR(R1,Max,^) + CHECKASSIGNMENTOPERATOR(R1,Max,&) + CHECKASSIGNMENTOPERATOR(Zero,R1,|) + CHECKASSIGNMENTOPERATOR(Zero,R1,^) + CHECKASSIGNMENTOPERATOR(Zero,R1,&) + CHECKASSIGNMENTOPERATOR(Max,R1,|) + CHECKASSIGNMENTOPERATOR(Max,R1,^) + CHECKASSIGNMENTOPERATOR(Max,R1,&) + + uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; + TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64))); + TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L); + TmpL ^= 0; BOOST_CHECK(TmpL == R1L); + TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64))); +} + +BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > +{ + arith_uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) { + TmpL= OneL<< i; + BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL); + BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); + TmpL |= R1L; + BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L)); + BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL)); + BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL)); + } +} + +BOOST_AUTO_TEST_CASE( plusMinus ) +{ + arith_uint256 TmpL = 0; + BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L)); + TmpL += R1L; + BOOST_CHECK(TmpL == R1L); + TmpL += R2L; + BOOST_CHECK(TmpL == R1L + R2L); + BOOST_CHECK(OneL+MaxL == ZeroL); + BOOST_CHECK(MaxL+OneL == ZeroL); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) ); + BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += OneL; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += 1; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); + BOOST_CHECK( TmpL++ == (MaxL>>i) ); + BOOST_CHECK( TmpL == (HalfL >> (i-1))); + } + BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); + TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); + TmpL = R1L; + BOOST_CHECK(++TmpL == R1L+1); + + BOOST_CHECK(R1L -(-R2L) == R1L+R2L); + BOOST_CHECK(R1L -(-OneL) == R1L+OneL); + BOOST_CHECK(R1L - OneL == R1L+(-OneL)); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1))); + BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(TmpL-- == (HalfL >> (i-1))); + BOOST_CHECK(TmpL == (MaxL >> i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(--TmpL == (MaxL >> i)); + } + TmpL = R1L; + BOOST_CHECK(--TmpL == R1L-1); +} + +BOOST_AUTO_TEST_CASE( multiply ) +{ + BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10"); + BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40"); + BOOST_CHECK((R1L * ZeroL) == ZeroL); + BOOST_CHECK((R1L * OneL) == R1L); + BOOST_CHECK((R1L * MaxL) == -R1L); + BOOST_CHECK((R2L * R1L) == (R1L * R2L)); + BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100"); + BOOST_CHECK((R2L * ZeroL) == ZeroL); + BOOST_CHECK((R2L * OneL) == R2L); + BOOST_CHECK((R2L * MaxL) == -R2L); + + BOOST_CHECK(MaxL * MaxL == OneL); + + BOOST_CHECK((R1L * 0) == 0); + BOOST_CHECK((R1L * 1) == R1L); + BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4"); + BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070"); +} + +BOOST_AUTO_TEST_CASE( divide ) +{ + arith_uint256 D1L("AD7133AC1977FA2B7"); + arith_uint256 D2L("ECD751716"); + BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a"); + BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a"); + BOOST_CHECK(R1L / OneL == R1L); + BOOST_CHECK(R1L / MaxL == ZeroL); + BOOST_CHECK(MaxL / R1L == 2); + BOOST_CHECK_THROW(R1L / ZeroL, uint_error); + BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5"); + BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); + BOOST_CHECK(R2L / OneL == R2L); + BOOST_CHECK(R2L / MaxL == ZeroL); + BOOST_CHECK(MaxL / R2L == 1); + BOOST_CHECK_THROW(R2L / ZeroL, uint_error); +} + + +bool almostEqual(double d1, double d2) +{ + return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon(); +} + +BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize +{ + BOOST_CHECK(R1L.GetHex() == R1L.ToString()); + BOOST_CHECK(R2L.GetHex() == R2L.ToString()); + BOOST_CHECK(OneL.GetHex() == OneL.ToString()); + BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); + arith_uint256 TmpL(R1L); + BOOST_CHECK(TmpL == R1L); + TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); + TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0); + TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL); + + TmpL.SetHex(R1L.ToString()); + BOOST_CHECK(R1L.size() == 32); + BOOST_CHECK(R2L.size() == 32); + BOOST_CHECK(ZeroL.size() == 32); + BOOST_CHECK(MaxL.size() == 32); + BOOST_CHECK(R1L.GetLow64() == R1LLow64); + BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL); + BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL); + + for (unsigned int i = 0; i < 255; ++i) + { + BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); + } + BOOST_CHECK(ZeroL.getdouble() == 0.0); + for (int i = 256; i > 53; --i) + BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); + uint64_t R1L64part = (R1L>>192).GetLow64(); + for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly + { + BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); + } +} + +BOOST_AUTO_TEST_CASE(bignum_SetCompact) +{ + arith_uint256 num; + bool fNegative; + bool fOverflow; + num.SetCompact(0, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x00123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x01003456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x02000056, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x03000000, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x04000000, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x00923456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x01803456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x02800056, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x03800000, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x04800000, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x01123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + // Make sure that we don't generate compacts with the 0x00800000 bit set + num = 0x80; + BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U); + + num.SetCompact(0x01fedcba, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e"); + BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U); + BOOST_CHECK_EQUAL(fNegative, true); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x02123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x03123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x04123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x04923456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); + BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U); + BOOST_CHECK_EQUAL(fNegative, true); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x05009234, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0x20123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, false); + + num.SetCompact(0xff123456, &fNegative, &fOverflow); + BOOST_CHECK_EQUAL(fNegative, false); + BOOST_CHECK_EQUAL(fOverflow, true); +} + + +BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage +{ + // ~R1L give a base_uint<256> + BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); + BOOST_CHECK((~~R1L << 10) == (R1L << 10)); + BOOST_CHECK(!(~~R1L < R1L)); + BOOST_CHECK(~~R1L <= R1L); + BOOST_CHECK(!(~~R1L > R1L)); + BOOST_CHECK(~~R1L >= R1L); + BOOST_CHECK(!(R1L < ~~R1L)); + BOOST_CHECK(R1L <= ~~R1L); + BOOST_CHECK(!(R1L > ~~R1L)); + BOOST_CHECK(R1L >= ~~R1L); + + BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); + BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); + BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); + unsigned char TmpArray[32]; + CHECKBITWISEOPERATOR(~R1,R2,|) + CHECKBITWISEOPERATOR(~R1,R2,^) + CHECKBITWISEOPERATOR(~R1,R2,&) + CHECKBITWISEOPERATOR(R1,~R2,|) + CHECKBITWISEOPERATOR(R1,~R2,^) + CHECKBITWISEOPERATOR(R1,~R2,&) +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index c32098efa0..f9cbcb0d9e 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(bloom_match) spendStream >> spendingTx; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(uint256("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")); + filter.insert(uint256S("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); @@ -151,11 +151,11 @@ BOOST_AUTO_TEST_CASE(bloom_match) BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); + filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - COutPoint prevOutPoint(uint256("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); + COutPoint prevOutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); { vector<unsigned char> data(32 + sizeof(unsigned int)); memcpy(&data[0], prevOutPoint.hash.begin(), 32); @@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE(bloom_match) BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(uint256("00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436")); + filter.insert(uint256S("00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436")); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); @@ -173,11 +173,11 @@ BOOST_AUTO_TEST_CASE(bloom_match) BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1)); + filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); + filter.insert(COutPoint(uint256S("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); } @@ -191,7 +191,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_1) CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction - filter.insert(uint256("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); + filter.insert(uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); @@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_1) BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); pair<unsigned int, uint256> pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 8); vector<uint256> vMatched; @@ -209,7 +209,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_1) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); // Also match the 8th transaction - filter.insert(uint256("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); + filter.insert(uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); @@ -217,7 +217,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_1) BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 7); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched) == block.hashMerkleRoot); @@ -236,7 +236,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_2) CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the first transaction - filter.insert(uint256("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + filter.insert(uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); @@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_2) BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); pair<unsigned int, uint256> pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); vector<uint256> vMatched; @@ -265,13 +265,13 @@ BOOST_AUTO_TEST_CASE(merkle_block_2) BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); - BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); + BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df21bea5f4e27e2")); + BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df21bea5f4e27e2")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 2); - BOOST_CHECK(merkleBlock.vMatchedTxn[3].second == uint256("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); + BOOST_CHECK(merkleBlock.vMatchedTxn[3].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[3].first == 3); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched) == block.hashMerkleRoot); @@ -290,7 +290,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_NONE); // Match the first transaction - filter.insert(uint256("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + filter.insert(uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); @@ -298,7 +298,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); pair<unsigned int, uint256> pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); vector<uint256> vMatched; @@ -319,10 +319,10 @@ BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); - BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); + BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); + BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 3); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched) == block.hashMerkleRoot); @@ -341,14 +341,14 @@ BOOST_AUTO_TEST_CASE(merkle_block_3_and_serialize) CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the only transaction - filter.insert(uint256("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); + filter.insert(uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); vector<uint256> vMatched; @@ -379,7 +379,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4) CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction - filter.insert(uint256("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); + filter.insert(uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); @@ -387,7 +387,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4) BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); pair<unsigned int, uint256> pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 6); vector<uint256> vMatched; @@ -397,13 +397,13 @@ BOOST_AUTO_TEST_CASE(merkle_block_4) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); // Also match the 4th transaction - filter.insert(uint256("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); + filter.insert(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 3); BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); @@ -432,9 +432,9 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only) BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We should match the generation outpoint - BOOST_CHECK(filter.contains(COutPoint(uint256("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); + BOOST_CHECK(filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); // ... but not the 4th transaction's output (its not pay-2-pubkey) - BOOST_CHECK(!filter.contains(COutPoint(uint256("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); } BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) @@ -455,8 +455,8 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We shouldn't match any outpoints (UPDATE_NONE) - BOOST_CHECK(!filter.contains(COutPoint(uint256("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); - BOOST_CHECK(!filter.contains(COutPoint(uint256("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp index 56e0541664..372cf3b306 100644 --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -6,6 +6,7 @@ #include "serialize.h" #include "streams.h" #include "uint256.h" +#include "arith_uint256.h" #include "version.h" #include <vector> @@ -22,8 +23,7 @@ public: void Damage() { unsigned int n = rand() % vHash.size(); int bit = rand() % 256; - uint256 &hash = vHash[n]; - hash ^= ((uint256)1 << bit); + *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); } }; @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1) // calculate actual merkle root and height uint256 merkleRoot1 = block.BuildMerkleTree(); - std::vector<uint256> vTxid(nTx, 0); + std::vector<uint256> vTxid(nTx, uint256()); for (unsigned int j=0; j<nTx; j++) vTxid[j] = block.vtx[j].GetHash(); int nHeight = 1, nTx_ = nTx; @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1) // check that it has the same merkle root as the original, and a valid one BOOST_CHECK(merkleRoot1 == merkleRoot2); - BOOST_CHECK(merkleRoot2 != 0); + BOOST_CHECK(!merkleRoot2.IsNull()); // check that it contains the matched transactions (in the same order!) BOOST_CHECK(vMatchTxid1 == vMatchTxid2); @@ -107,12 +107,18 @@ BOOST_AUTO_TEST_CASE(pmt_test1) BOOST_AUTO_TEST_CASE(pmt_malleability) { - std::vector<uint256> vTxid = boost::assign::list_of(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(9)(10); + std::vector<uint256> vTxid = boost::assign::list_of + (ArithToUint256(1))(ArithToUint256(2)) + (ArithToUint256(3))(ArithToUint256(4)) + (ArithToUint256(5))(ArithToUint256(6)) + (ArithToUint256(7))(ArithToUint256(8)) + (ArithToUint256(9))(ArithToUint256(10)) + (ArithToUint256(9))(ArithToUint256(10)); std::vector<bool> vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false); CPartialMerkleTree tree(vTxid, vMatch); std::vector<uint256> vTxid2; - BOOST_CHECK(tree.ExtractMatches(vTxid) == 0); + BOOST_CHECK(tree.ExtractMatches(vTxid).IsNull()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp index 63553556ec..77e70022df 100644 --- a/src/test/script_P2SH_tests.cpp +++ b/src/test/script_P2SH_tests.cpp @@ -210,7 +210,7 @@ BOOST_AUTO_TEST_CASE(set) BOOST_AUTO_TEST_CASE(is) { // Test CScript::IsPayToScriptHash() - uint160 dummy(0); + uint160 dummy; CScript p2sh; p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(p2sh.IsPayToScriptHash()); diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index a9d2f9e8ca..662f765a0b 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -24,10 +24,11 @@ extern Array read_json(const std::string& jsondata); // Old script.cpp SignatureHash function uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType) { + static const uint256 one(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); if (nIn >= txTo.vin.size()) { printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn); - return 1; + return one; } CMutableTransaction txTmp(txTo); @@ -58,7 +59,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un if (nOut >= txTmp.vout.size()) { printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut); - return 1; + return one; } txTmp.vout.resize(nOut+1); for (unsigned int i = 0; i < nOut; i++) diff --git a/src/test/sigopcount_tests.cpp b/src/test/sigopcount_tests.cpp index 3326601492..3c8264d89d 100644 --- a/src/test/sigopcount_tests.cpp +++ b/src/test/sigopcount_tests.cpp @@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount) BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U); BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U); - uint160 dummy(0); + uint160 dummy; s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U); s1 << OP_IF << OP_CHECKSIG << OP_ENDIF; diff --git a/src/test/skiplist_tests.cpp b/src/test/skiplist_tests.cpp index 646c7e6ade..c75e21a2ad 100644 --- a/src/test/skiplist_tests.cpp +++ b/src/test/skiplist_tests.cpp @@ -49,12 +49,12 @@ BOOST_AUTO_TEST_CASE(getlocator_test) std::vector<uint256> vHashMain(100000); std::vector<CBlockIndex> vBlocksMain(100000); for (unsigned int i=0; i<vBlocksMain.size(); i++) { - vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances. + vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances. vBlocksMain[i].nHeight = i; vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL; vBlocksMain[i].phashBlock = &vHashMain[i]; vBlocksMain[i].BuildSkip(); - BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight); + BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight); BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1); } @@ -62,12 +62,12 @@ BOOST_AUTO_TEST_CASE(getlocator_test) std::vector<uint256> vHashSide(50000); std::vector<CBlockIndex> vBlocksSide(50000); for (unsigned int i=0; i<vBlocksSide.size(); i++) { - vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height. + vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height. vBlocksSide[i].nHeight = i + 50000; vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999]; vBlocksSide[i].phashBlock = &vHashSide[i]; vBlocksSide[i].BuildSkip(); - BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight); + BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight); BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1); } @@ -87,13 +87,13 @@ BOOST_AUTO_TEST_CASE(getlocator_test) // Entries 1 through 11 (inclusive) go back one step each. for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) { - BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i); + BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i); } // The further ones (excluding the last one) go back with exponential steps. unsigned int dist = 2; for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) { - BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist); + BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist); dist *= 2; } } diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 2594f89ac3..efa2af917e 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) break; } - mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str()); + mapprevOutScriptPubKeys[COutPoint(uint256S(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str()); } if (!fValid) { @@ -194,7 +194,7 @@ BOOST_AUTO_TEST_CASE(tx_invalid) break; } - mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str()); + mapprevOutScriptPubKeys[COutPoint(uint256S(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str()); } if (!fValid) { diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index d05b28956c..5b33846ba9 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -1,6 +1,9 @@ // Copyright (c) 2011-2013 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "arith_uint256.h" +#include "uint256.h" +#include "version.h" #include <boost/test/unit_test.hpp> #include <stdint.h> @@ -8,61 +11,66 @@ #include <iomanip> #include <limits> #include <cmath> -#include "uint256.h" #include <string> -#include "version.h" +#include <stdio.h> BOOST_AUTO_TEST_SUITE(uint256_tests) - -const unsigned char R1Array[] = + +const unsigned char R1Array[] = "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; -const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 -const double R1Sdouble = 0.7096329412477836074; const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32)); const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20)); -const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; -const unsigned char R2Array[] = +const unsigned char R2Array[] = "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32)); const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20)); -const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; - -const unsigned char ZeroArray[] = +const unsigned char ZeroArray[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32)); const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20)); - -const unsigned char OneArray[] = + +const unsigned char OneArray[] = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32)); const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20)); -const unsigned char MaxArray[] = +const unsigned char MaxArray[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32)); const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20)); -const uint256 HalfL = (OneL << 255); -const uint160 HalfS = (OneS << 159); std::string ArrayToString(const unsigned char A[], unsigned int width) { std::stringstream Stream; Stream << std::hex; - for (unsigned int i = 0; i < width; ++i) + for (unsigned int i = 0; i < width; ++i) { Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1]; - } + } return Stream.str(); } +inline uint160 uint160S(const char *str) +{ + uint160 rv; + rv.SetHex(str); + return rv; +} +inline uint160 uint160S(const std::string& str) +{ + uint160 rv; + rv.SetHex(str); + return rv; +} + BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality { BOOST_CHECK(1 == 0+1); @@ -85,477 +93,66 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); - BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS); - BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); - BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S); - - uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; - for (unsigned int i = 0; i < 256; ++i) - { - BOOST_CHECK(ZeroL != (OneL << i)); - BOOST_CHECK((OneL << i) != ZeroL); - BOOST_CHECK(R1L != (R1L ^ (OneL << i))); - BOOST_CHECK(((uint256(Tmp64) ^ (OneL << i) ) != Tmp64 )); - } - BOOST_CHECK(ZeroL == (OneL << 256)); - - for (unsigned int i = 0; i < 160; ++i) - { - BOOST_CHECK(ZeroS != (OneS << i)); - BOOST_CHECK((OneS << i) != ZeroS); - BOOST_CHECK(R1S != (R1S ^ (OneS << i))); - BOOST_CHECK(((uint160(Tmp64) ^ (OneS << i) ) != Tmp64 )); - } - BOOST_CHECK(ZeroS == (OneS << 256)); // String Constructor and Copy Constructor - BOOST_CHECK(uint256("0x"+R1L.ToString()) == R1L); - BOOST_CHECK(uint256("0x"+R2L.ToString()) == R2L); - BOOST_CHECK(uint256("0x"+ZeroL.ToString()) == ZeroL); - BOOST_CHECK(uint256("0x"+OneL.ToString()) == OneL); - BOOST_CHECK(uint256("0x"+MaxL.ToString()) == MaxL); - BOOST_CHECK(uint256(R1L.ToString()) == R1L); - BOOST_CHECK(uint256(" 0x"+R1L.ToString()+" ") == R1L); - BOOST_CHECK(uint256("") == ZeroL); - BOOST_CHECK(R1L == uint256(R1ArrayHex)); + BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L); + BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L); + BOOST_CHECK(uint256S("0x"+ZeroL.ToString()) == ZeroL); + BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL); + BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(uint256S(R1L.ToString()) == R1L); + BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(uint256S("") == ZeroL); + BOOST_CHECK(R1L == uint256S(R1ArrayHex)); BOOST_CHECK(uint256(R1L) == R1L); - BOOST_CHECK((uint256(R1L^R2L)^R2L) == R1L); BOOST_CHECK(uint256(ZeroL) == ZeroL); BOOST_CHECK(uint256(OneL) == OneL); - BOOST_CHECK(uint160("0x"+R1S.ToString()) == R1S); - BOOST_CHECK(uint160("0x"+R2S.ToString()) == R2S); - BOOST_CHECK(uint160("0x"+ZeroS.ToString()) == ZeroS); - BOOST_CHECK(uint160("0x"+OneS.ToString()) == OneS); - BOOST_CHECK(uint160("0x"+MaxS.ToString()) == MaxS); - BOOST_CHECK(uint160(R1S.ToString()) == R1S); - BOOST_CHECK(uint160(" 0x"+R1S.ToString()+" ") == R1S); - BOOST_CHECK(uint160("") == ZeroS); - BOOST_CHECK(R1S == uint160(R1ArrayHex)); + BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S); + BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S); + BOOST_CHECK(uint160S("0x"+ZeroS.ToString()) == ZeroS); + BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS); + BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS); + BOOST_CHECK(uint160S(R1S.ToString()) == R1S); + BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S); + BOOST_CHECK(uint160S("") == ZeroS); + BOOST_CHECK(R1S == uint160S(R1ArrayHex)); BOOST_CHECK(uint160(R1S) == R1S); - BOOST_CHECK((uint160(R1S^R2S)^R2S) == R1S); BOOST_CHECK(uint160(ZeroS) == ZeroS); BOOST_CHECK(uint160(OneS) == OneS); - - // uint64_t constructor - BOOST_CHECK( (R1L & uint256("0xffffffffffffffff")) == uint256(R1LLow64)); - BOOST_CHECK(ZeroL == uint256(0)); - BOOST_CHECK(OneL == uint256(1)); - BOOST_CHECK(uint256("0xffffffffffffffff") = uint256(0xffffffffffffffffULL)); - BOOST_CHECK( (R1S & uint160("0xffffffffffffffff")) == uint160(R1LLow64)); - BOOST_CHECK(ZeroS == uint160(0)); - BOOST_CHECK(OneS == uint160(1)); - BOOST_CHECK(uint160("0xffffffffffffffff") = uint160(0xffffffffffffffffULL)); - - // Assignment (from base_uint) - uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); - tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL); - tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L); - tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L); - tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL); - uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS); - tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS); - tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S); - tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S); - tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS); - - // Wrong length must throw exception. - BOOST_CHECK_THROW(uint256(std::vector<unsigned char>(OneArray,OneArray+31)), uint_error); - BOOST_CHECK_THROW(uint256(std::vector<unsigned char>(OneArray,OneArray+20)), uint_error); - BOOST_CHECK_THROW(uint160(std::vector<unsigned char>(OneArray,OneArray+32)), uint_error); - BOOST_CHECK_THROW(uint160(std::vector<unsigned char>(OneArray,OneArray+19)), uint_error); -} - -void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) -{ - for (unsigned int T=0; T < arrayLength; ++T) - { - unsigned int F = (T+bitsToShift/8); - if (F < arrayLength) - to[T] = from[F] >> (bitsToShift%8); - else - to[T] = 0; - if (F + 1 < arrayLength) - to[T] |= from[(F+1)] << (8-bitsToShift%8); - } -} - -void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) -{ - for (unsigned int T=0; T < arrayLength; ++T) - { - if (T >= bitsToShift/8) - { - unsigned int F = T-bitsToShift/8; - to[T] = from[F] << (bitsToShift%8); - if (T >= bitsToShift/8+1) - to[T] |= from[F-1] >> (8-bitsToShift%8); - } - else { - to[T] = 0; - } - } -} - -BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" - unsigned char TmpArray[32]; - uint256 TmpL; - for (unsigned int i = 0; i < 256; ++i) - { - shiftArrayLeft(TmpArray, OneArray, 32, i); - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i)); - TmpL = OneL; TmpL <<= i; - BOOST_CHECK(TmpL == (OneL << i)); - BOOST_CHECK((HalfL >> (255-i)) == (OneL << i)); - TmpL = HalfL; TmpL >>= (255-i); - BOOST_CHECK(TmpL == (OneL << i)); - - shiftArrayLeft(TmpArray, R1Array, 32, i); - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i)); - TmpL = R1L; TmpL <<= i; - BOOST_CHECK(TmpL == (R1L << i)); - - shiftArrayRight(TmpArray, R1Array, 32, i); - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i)); - TmpL = R1L; TmpL >>= i; - BOOST_CHECK(TmpL == (R1L >> i)); - - shiftArrayLeft(TmpArray, MaxArray, 32, i); - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i)); - TmpL = MaxL; TmpL <<= i; - BOOST_CHECK(TmpL == (MaxL << i)); - - shiftArrayRight(TmpArray, MaxArray, 32, i); - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i)); - TmpL = MaxL; TmpL >>= i; - BOOST_CHECK(TmpL == (MaxL >> i)); - } - uint256 c1L = uint256(0x0123456789abcdefULL); - uint256 c2L = c1L << 128; - for (unsigned int i = 0; i < 128; ++i) { - BOOST_CHECK((c1L << i) == (c2L >> (128-i))); - } - for (unsigned int i = 128; i < 256; ++i) { - BOOST_CHECK((c1L << i) == (c2L << (i-128))); - } - - uint160 TmpS; - for (unsigned int i = 0; i < 160; ++i) - { - shiftArrayLeft(TmpArray, OneArray, 20, i); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i)); - TmpS = OneS; TmpS <<= i; - BOOST_CHECK(TmpS == (OneS << i)); - BOOST_CHECK((HalfS >> (159-i)) == (OneS << i)); - TmpS = HalfS; TmpS >>= (159-i); - BOOST_CHECK(TmpS == (OneS << i)); - - shiftArrayLeft(TmpArray, R1Array, 20, i); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i)); - TmpS = R1S; TmpS <<= i; - BOOST_CHECK(TmpS == (R1S << i)); - - shiftArrayRight(TmpArray, R1Array, 20, i); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i)); - TmpS = R1S; TmpS >>= i; - BOOST_CHECK(TmpS == (R1S >> i)); - - shiftArrayLeft(TmpArray, MaxArray, 20, i); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i)); - TmpS = MaxS; TmpS <<= i; - BOOST_CHECK(TmpS == (MaxS << i)); - - shiftArrayRight(TmpArray, MaxArray, 20, i); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i)); - TmpS = MaxS; TmpS >>= i; - BOOST_CHECK(TmpS == (MaxS >> i)); - } - uint160 c1S = uint160(0x0123456789abcdefULL); - uint160 c2S = c1S << 80; - for (unsigned int i = 0; i < 80; ++i) { - BOOST_CHECK((c1S << i) == (c2S >> (80-i))); - } - for (unsigned int i = 80; i < 160; ++i) { - BOOST_CHECK((c1S << i) == (c2S << (i-80))); - } -} - -BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - -{ - BOOST_CHECK(!ZeroL); BOOST_CHECK(!ZeroS); - BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS)); - for (unsigned int i = 0; i < 256; ++i) - BOOST_CHECK(!(!(OneL<<i))); - for (unsigned int i = 0; i < 160; ++i) - BOOST_CHECK(!(!(OneS<<i))); - BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S)); - BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S)); - BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS)); - - BOOST_CHECK(~ZeroL == MaxL); BOOST_CHECK(~ZeroS == MaxS); - - unsigned char TmpArray[32]; - for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; } - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L)); - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S)); - - BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS); - BOOST_CHECK(-R1L == (~R1L)+1); - BOOST_CHECK(-R1S == (~R1S)+1); - for (unsigned int i = 0; i < 256; ++i) - BOOST_CHECK(-(OneL<<i) == (MaxL << i)); - for (unsigned int i = 0; i < 160; ++i) - BOOST_CHECK(-(OneS<<i) == (MaxS << i)); -} - - -// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each -// element of Aarray and Barray, and then converting the result into a uint256. -#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \ - for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ - BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \ - for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ - BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S)); - -#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \ - TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \ - TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S)); - -BOOST_AUTO_TEST_CASE( bitwiseOperators ) -{ - unsigned char TmpArray[32]; - - CHECKBITWISEOPERATOR(R1,R2,|) - CHECKBITWISEOPERATOR(R1,R2,^) - CHECKBITWISEOPERATOR(R1,R2,&) - CHECKBITWISEOPERATOR(R1,Zero,|) - CHECKBITWISEOPERATOR(R1,Zero,^) - CHECKBITWISEOPERATOR(R1,Zero,&) - CHECKBITWISEOPERATOR(R1,Max,|) - CHECKBITWISEOPERATOR(R1,Max,^) - CHECKBITWISEOPERATOR(R1,Max,&) - CHECKBITWISEOPERATOR(Zero,R1,|) - CHECKBITWISEOPERATOR(Zero,R1,^) - CHECKBITWISEOPERATOR(Zero,R1,&) - CHECKBITWISEOPERATOR(Max,R1,|) - CHECKBITWISEOPERATOR(Max,R1,^) - CHECKBITWISEOPERATOR(Max,R1,&) - - uint256 TmpL; - uint160 TmpS; - CHECKASSIGNMENTOPERATOR(R1,R2,|) - CHECKASSIGNMENTOPERATOR(R1,R2,^) - CHECKASSIGNMENTOPERATOR(R1,R2,&) - CHECKASSIGNMENTOPERATOR(R1,Zero,|) - CHECKASSIGNMENTOPERATOR(R1,Zero,^) - CHECKASSIGNMENTOPERATOR(R1,Zero,&) - CHECKASSIGNMENTOPERATOR(R1,Max,|) - CHECKASSIGNMENTOPERATOR(R1,Max,^) - CHECKASSIGNMENTOPERATOR(R1,Max,&) - CHECKASSIGNMENTOPERATOR(Zero,R1,|) - CHECKASSIGNMENTOPERATOR(Zero,R1,^) - CHECKASSIGNMENTOPERATOR(Zero,R1,&) - CHECKASSIGNMENTOPERATOR(Max,R1,|) - CHECKASSIGNMENTOPERATOR(Max,R1,^) - CHECKASSIGNMENTOPERATOR(Max,R1,&) - - uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; - TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | uint256(Tmp64))); - TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | uint160(Tmp64))); - TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L); - TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S); - TmpL ^= 0; BOOST_CHECK(TmpL == R1L); - TmpS ^= 0; BOOST_CHECK(TmpS == R1S); - TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ uint256(Tmp64))); - TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ uint160(Tmp64))); } BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > { - uint256 TmpL; - for (unsigned int i = 0; i < 256; ++i) { - TmpL= OneL<< i; - BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL); - BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); - TmpL |= R1L; - BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L)); - BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL)); - BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL)); - } - uint160 TmpS; - for (unsigned int i = 0; i < 160; ++i) { - TmpS= OneS<< i; - BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS); - BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS); - TmpS |= R1S; - BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S)); - BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS)); - BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS)); + uint256 LastL; + for (int i = 255; i >= 0; --i) { + uint256 TmpL; + *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7)); + BOOST_CHECK( LastL < TmpL ); + LastL = TmpL; } -} -BOOST_AUTO_TEST_CASE( plusMinus ) -{ - uint256 TmpL = 0; - BOOST_CHECK(R1L+R2L == uint256(R1LplusR2L)); - TmpL += R1L; - BOOST_CHECK(TmpL == R1L); - TmpL += R2L; - BOOST_CHECK(TmpL == R1L + R2L); - BOOST_CHECK(OneL+MaxL == ZeroL); - BOOST_CHECK(MaxL+OneL == ZeroL); - for (unsigned int i = 1; i < 256; ++i) { - BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) ); - BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); TmpL += OneL; - BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); TmpL += 1; - BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); - BOOST_CHECK( TmpL++ == (MaxL>>i) ); - BOOST_CHECK( TmpL == (HalfL >> (i-1))); + BOOST_CHECK( ZeroL < R1L ); + BOOST_CHECK( R2L < R1L ); + BOOST_CHECK( ZeroL < OneL ); + BOOST_CHECK( OneL < MaxL ); + BOOST_CHECK( R1L < MaxL ); + BOOST_CHECK( R2L < MaxL ); + + uint160 LastS; + for (int i = 159; i >= 0; --i) { + uint160 TmpS; + *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7)); + BOOST_CHECK( LastS < TmpS ); + LastS = TmpS; } - BOOST_CHECK(uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); - TmpL = uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL; - BOOST_CHECK(TmpL == uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); - TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); - TmpL = R1L; - BOOST_CHECK(++TmpL == R1L+1); - - BOOST_CHECK(R1L -(-R2L) == R1L+R2L); - BOOST_CHECK(R1L -(-OneL) == R1L+OneL); - BOOST_CHECK(R1L - OneL == R1L+(-OneL)); - for (unsigned int i = 1; i < 256; ++i) { - BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1))); - BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i)); - TmpL = (HalfL >> (i-1)); - BOOST_CHECK(TmpL-- == (HalfL >> (i-1))); - BOOST_CHECK(TmpL == (MaxL >> i)); - TmpL = (HalfL >> (i-1)); - BOOST_CHECK(--TmpL == (MaxL >> i)); - } - TmpL = R1L; - BOOST_CHECK(--TmpL == R1L-1); - - // 160-bit; copy-pasted - uint160 TmpS = 0; - BOOST_CHECK(R1S+R2S == uint160(R1LplusR2L)); - TmpS += R1S; - BOOST_CHECK(TmpS == R1S); - TmpS += R2S; - BOOST_CHECK(TmpS == R1S + R2S); - BOOST_CHECK(OneS+MaxS == ZeroS); - BOOST_CHECK(MaxS+OneS == ZeroS); - for (unsigned int i = 1; i < 160; ++i) { - BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) ); - BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) ); - TmpS = (MaxS>>i); TmpS += OneS; - BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); - TmpS = (MaxS>>i); TmpS += 1; - BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); - TmpS = (MaxS>>i); - BOOST_CHECK( TmpS++ == (MaxS>>i) ); - BOOST_CHECK( TmpS == (HalfS >> (i-1))); - } - BOOST_CHECK(uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); - TmpS = uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL; - BOOST_CHECK(TmpS == uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); - TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL); - TmpS = R1S; - BOOST_CHECK(++TmpS == R1S+1); - - BOOST_CHECK(R1S -(-R2S) == R1S+R2S); - BOOST_CHECK(R1S -(-OneS) == R1S+OneS); - BOOST_CHECK(R1S - OneS == R1S+(-OneS)); - for (unsigned int i = 1; i < 160; ++i) { - BOOST_CHECK((MaxS>>i) - (-OneS) == (HalfS >> (i-1))); - BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i)); - TmpS = (HalfS >> (i-1)); - BOOST_CHECK(TmpS-- == (HalfS >> (i-1))); - BOOST_CHECK(TmpS == (MaxS >> i)); - TmpS = (HalfS >> (i-1)); - BOOST_CHECK(--TmpS == (MaxS >> i)); - } - TmpS = R1S; - BOOST_CHECK(--TmpS == R1S-1); - -} - -BOOST_AUTO_TEST_CASE( multiply ) -{ - BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10"); - BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40"); - BOOST_CHECK((R1L * ZeroL) == ZeroL); - BOOST_CHECK((R1L * OneL) == R1L); - BOOST_CHECK((R1L * MaxL) == -R1L); - BOOST_CHECK((R2L * R1L) == (R1L * R2L)); - BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100"); - BOOST_CHECK((R2L * ZeroL) == ZeroL); - BOOST_CHECK((R2L * OneL) == R2L); - BOOST_CHECK((R2L * MaxL) == -R2L); - - BOOST_CHECK((R1S * R1S).ToString() == "a7761bf30d5237e9873f9bff3642a732c4d84f10"); - BOOST_CHECK((R1S * R2S).ToString() == "ba51c008df851987d9dd323f0e5de07760529c40"); - BOOST_CHECK((R1S * ZeroS) == ZeroS); - BOOST_CHECK((R1S * OneS) == R1S); - BOOST_CHECK((R1S * MaxS) == -R1S); - BOOST_CHECK((R2S * R1S) == (R1S * R2S)); - BOOST_CHECK((R2S * R2S).ToString() == "c28bb2b45a1d85ab7996ccd3e102a650f74ff100"); - BOOST_CHECK((R2S * ZeroS) == ZeroS); - BOOST_CHECK((R2S * OneS) == R2S); - BOOST_CHECK((R2S * MaxS) == -R2S); - - BOOST_CHECK(MaxL * MaxL == OneL); - BOOST_CHECK(MaxS * MaxS == OneS); - - BOOST_CHECK((R1L * 0) == 0); - BOOST_CHECK((R1L * 1) == R1L); - BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4"); - BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070"); - BOOST_CHECK((R1S * 0) == 0); - BOOST_CHECK((R1S * 1) == R1S); - BOOST_CHECK((R1S * 7).ToString() == "f7a987f3c3bf758d927f202d7e795faeff084244"); - BOOST_CHECK((R2S * 0xFFFFFFFFUL).ToString() == "1c6f6c930353e17f7d6127213bb18d2883e2cd90"); -} - -BOOST_AUTO_TEST_CASE( divide ) -{ - uint256 D1L("AD7133AC1977FA2B7"); - uint256 D2L("ECD751716"); - BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a"); - BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a"); - BOOST_CHECK(R1L / OneL == R1L); - BOOST_CHECK(R1L / MaxL == ZeroL); - BOOST_CHECK(MaxL / R1L == 2); - BOOST_CHECK_THROW(R1L / ZeroL, uint_error); - BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5"); - BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); - BOOST_CHECK(R2L / OneL == R2L); - BOOST_CHECK(R2L / MaxL == ZeroL); - BOOST_CHECK(MaxL / R2L == 1); - BOOST_CHECK_THROW(R2L / ZeroL, uint_error); - - uint160 D1S("D3C5EDCDEA54EB92679F0A4B4"); - uint160 D2S("13037"); - BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02"); - BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c"); - BOOST_CHECK(R1S / OneS == R1S); - BOOST_CHECK(R1S / MaxS == ZeroS); - BOOST_CHECK(MaxS / R1S == 1); - BOOST_CHECK_THROW(R1S / ZeroS, uint_error); - BOOST_CHECK((R2S / D1S).ToString() == "0000000000000000000000000c5608e781182047"); - BOOST_CHECK((R2S / D2S).ToString() == "00008966751b7187c3c67c1fda5cea7db2c1c069"); - BOOST_CHECK(R2S / OneS == R2S); - BOOST_CHECK(R2S / MaxS == ZeroS); - BOOST_CHECK(MaxS / R2S == 1); - BOOST_CHECK_THROW(R2S / ZeroS, uint_error); -} - - -bool almostEqual(double d1, double d2) -{ - return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon(); + BOOST_CHECK( ZeroS < R1S ); + BOOST_CHECK( R2S < R1S ); + BOOST_CHECK( ZeroS < OneS ); + BOOST_CHECK( OneS < MaxS ); + BOOST_CHECK( R1S < MaxS ); + BOOST_CHECK( R2S < MaxS ); } BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize @@ -567,8 +164,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G uint256 TmpL(R1L); BOOST_CHECK(TmpL == R1L); TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); - TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0); - TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL); + TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == uint256()); TmpL.SetHex(R1L.ToString()); BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0); @@ -576,6 +172,8 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0); BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0); BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0); + BOOST_CHECK(R1L.size() == sizeof(R1L)); + BOOST_CHECK(sizeof(R1L) == 32); BOOST_CHECK(R1L.size() == 32); BOOST_CHECK(R2L.size() == 32); BOOST_CHECK(ZeroL.size() == 32); @@ -585,9 +183,6 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(OneL.begin() + 32 == OneL.end()); BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); - BOOST_CHECK(R1L.GetLow64() == R1LLow64); - BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL); - BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL); BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); @@ -615,8 +210,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G uint160 TmpS(R1S); BOOST_CHECK(TmpS == R1S); TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); - TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0); - TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS); + TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == uint160()); TmpS.SetHex(R1S.ToString()); BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0); @@ -624,6 +218,8 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0); BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0); BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0); + BOOST_CHECK(R1S.size() == sizeof(R1S)); + BOOST_CHECK(sizeof(R1S) == 20); BOOST_CHECK(R1S.size() == 20); BOOST_CHECK(R2S.size() == 20); BOOST_CHECK(ZeroS.size() == 20); @@ -633,9 +229,6 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(OneS.begin() + 20 == OneS.end()); BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); - BOOST_CHECK(R1S.GetLow64() == R1LLow64); - BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL); - BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL); BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); @@ -654,184 +247,22 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G TmpS.Unserialize(ss,0,PROTOCOL_VERSION); BOOST_CHECK(MaxS == TmpS); ss.str(""); - - for (unsigned int i = 0; i < 255; ++i) - { - BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); - if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i)); - } - BOOST_CHECK(ZeroL.getdouble() == 0.0); - BOOST_CHECK(ZeroS.getdouble() == 0.0); - for (int i = 256; i > 53; --i) - BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); - for (int i = 160; i > 53; --i) - BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i))); - uint64_t R1L64part = (R1L>>192).GetLow64(); - uint64_t R1S64part = (R1S>>96).GetLow64(); - for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly - { - BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); - BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i))); - } } -BOOST_AUTO_TEST_CASE(bignum_SetCompact) +BOOST_AUTO_TEST_CASE( conversion ) { - uint256 num; - bool fNegative; - bool fOverflow; - num.SetCompact(0, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x00123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x01003456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x02000056, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x03000000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x04000000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x00923456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x01803456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x02800056, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x03800000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x04800000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x01123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - // Make sure that we don't generate compacts with the 0x00800000 bit set - num = 0x80; - BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U); - - num.SetCompact(0x01fedcba, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e"); - BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U); - BOOST_CHECK_EQUAL(fNegative, true); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x02123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x03123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x04123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x04923456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); - BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U); - BOOST_CHECK_EQUAL(fNegative, true); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x05009234, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0x20123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000"); - BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, false); - - num.SetCompact(0xff123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(fNegative, false); - BOOST_CHECK_EQUAL(fOverflow, true); -} - - -BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage -{ - // ~R1L give a base_uint<256> - BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10)); - BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10)); - BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S)); - BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S); - BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S)); - BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S); - BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S)); - BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S); - BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S)); - BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S); - - BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); - BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S); - BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); - BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S); - BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); - BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S); - unsigned char TmpArray[32]; - CHECKBITWISEOPERATOR(~R1,R2,|) - CHECKBITWISEOPERATOR(~R1,R2,^) - CHECKBITWISEOPERATOR(~R1,R2,&) - CHECKBITWISEOPERATOR(R1,~R2,|) - CHECKBITWISEOPERATOR(R1,~R2,^) - CHECKBITWISEOPERATOR(R1,~R2,&) + BOOST_CHECK(ArithToUint256(UintToArith256(ZeroL)) == ZeroL); + BOOST_CHECK(ArithToUint256(UintToArith256(OneL)) == OneL); + BOOST_CHECK(ArithToUint256(UintToArith256(R1L)) == R1L); + BOOST_CHECK(ArithToUint256(UintToArith256(R2L)) == R2L); + BOOST_CHECK(UintToArith256(ZeroL) == 0); + BOOST_CHECK(UintToArith256(OneL) == 1); + BOOST_CHECK(ArithToUint256(0) == ZeroL); + BOOST_CHECK(ArithToUint256(1) == OneL); + BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L)); + BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L)); + BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex()); + BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex()); } BOOST_AUTO_TEST_SUITE_END() - |