Changeset View
Changeset View
Standalone View
Standalone View
src/test/arith_uint256_tests.cpp
Show All 15 Lines | |||||
#include <iomanip> | #include <iomanip> | ||||
#include <limits> | #include <limits> | ||||
#include <sstream> | #include <sstream> | ||||
#include <string> | #include <string> | ||||
BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup) | ||||
/// Convert vector to arith_uint256, via uint256 blob | /// Convert vector to arith_uint256, via uint256 blob | ||||
inline arith_uint256 arith_uint256V(const std::vector<uint8_t> &vch) { | static inline arith_uint256 arith_uint256V(const std::vector<uint8_t> &vch) { | ||||
return UintToArith256(uint256(vch)); | return UintToArith256(uint256(vch)); | ||||
} | } | ||||
const uint8_t R1Array[] = | const uint8_t R1Array[] = | ||||
"\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" | "\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"; | "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; | ||||
const char R1ArrayHex[] = | const char R1ArrayHex[] = | ||||
"7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; | "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; | ||||
Show All 26 Lines | |||||
const uint8_t MaxArray[] = | const uint8_t 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" | ||||
"\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 = | const arith_uint256 MaxL = | ||||
arith_uint256V(std::vector<uint8_t>(MaxArray, MaxArray + 32)); | arith_uint256V(std::vector<uint8_t>(MaxArray, MaxArray + 32)); | ||||
const arith_uint256 HalfL = (OneL << 255); | const arith_uint256 HalfL = (OneL << 255); | ||||
std::string ArrayToString(const uint8_t A[], unsigned int width) { | static std::string ArrayToString(const uint8_t A[], unsigned int width) { | ||||
std::stringstream Stream; | std::stringstream Stream; | ||||
Stream << std::hex; | 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') | Stream << std::setw(2) << std::setfill('0') | ||||
<< (unsigned int)A[width - i - 1]; | << (unsigned int)A[width - i - 1]; | ||||
} | } | ||||
return Stream.str(); | return Stream.str(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(basics) { | ||||
tmpL = ~R1L; | tmpL = ~R1L; | ||||
BOOST_CHECK(tmpL == ~R1L); | BOOST_CHECK(tmpL == ~R1L); | ||||
tmpL = ~R2L; | tmpL = ~R2L; | ||||
BOOST_CHECK(tmpL == ~R2L); | BOOST_CHECK(tmpL == ~R2L); | ||||
tmpL = ~MaxL; | tmpL = ~MaxL; | ||||
BOOST_CHECK(tmpL == ~MaxL); | BOOST_CHECK(tmpL == ~MaxL); | ||||
} | } | ||||
void shiftArrayRight(uint8_t *to, const uint8_t *from, unsigned int arrayLength, | static void shiftArrayRight(uint8_t *to, const uint8_t *from, | ||||
unsigned int arrayLength, | |||||
unsigned int bitsToShift) { | unsigned int bitsToShift) { | ||||
for (unsigned int T = 0; T < arrayLength; ++T) { | for (unsigned int T = 0; T < arrayLength; ++T) { | ||||
unsigned int F = (T + bitsToShift / 8); | unsigned int F = (T + bitsToShift / 8); | ||||
if (F < arrayLength) { | if (F < arrayLength) { | ||||
to[T] = from[F] >> (bitsToShift % 8); | to[T] = from[F] >> (bitsToShift % 8); | ||||
} else { | } else { | ||||
to[T] = 0; | to[T] = 0; | ||||
} | } | ||||
if (F + 1 < arrayLength) { | if (F + 1 < arrayLength) { | ||||
to[T] |= from[(F + 1)] << (8 - bitsToShift % 8); | to[T] |= from[(F + 1)] << (8 - bitsToShift % 8); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void shiftArrayLeft(uint8_t *to, const uint8_t *from, unsigned int arrayLength, | static void shiftArrayLeft(uint8_t *to, const uint8_t *from, | ||||
unsigned int bitsToShift) { | unsigned int arrayLength, unsigned int bitsToShift) { | ||||
for (unsigned int T = 0; T < arrayLength; ++T) { | for (unsigned int T = 0; T < arrayLength; ++T) { | ||||
if (T >= bitsToShift / 8) { | if (T >= bitsToShift / 8) { | ||||
unsigned int F = T - bitsToShift / 8; | unsigned int F = T - bitsToShift / 8; | ||||
to[T] = from[F] << (bitsToShift % 8); | to[T] = from[F] << (bitsToShift % 8); | ||||
if (T >= bitsToShift / 8 + 1) { | if (T >= bitsToShift / 8 + 1) { | ||||
to[T] |= from[F - 1] >> (8 - bitsToShift % 8); | to[T] |= from[F - 1] >> (8 - bitsToShift % 8); | ||||
} | } | ||||
} else { | } else { | ||||
▲ Show 20 Lines • Show All 256 Lines • ▼ Show 20 Lines | BOOST_CHECK( | ||||
(R2L / D2L).ToString() == | (R2L / D2L).ToString() == | ||||
"000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); | "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); | ||||
BOOST_CHECK(R2L / OneL == R2L); | BOOST_CHECK(R2L / OneL == R2L); | ||||
BOOST_CHECK(R2L / MaxL == ZeroL); | BOOST_CHECK(R2L / MaxL == ZeroL); | ||||
BOOST_CHECK(MaxL / R2L == 1); | BOOST_CHECK(MaxL / R2L == 1); | ||||
BOOST_CHECK_THROW(R2L / ZeroL, uint_error); | BOOST_CHECK_THROW(R2L / ZeroL, uint_error); | ||||
} | } | ||||
bool almostEqual(double d1, double d2) { | static bool almostEqual(double d1, double d2) { | ||||
return fabs(d1 - d2) <= | return fabs(d1 - d2) <= | ||||
4 * fabs(d1) * std::numeric_limits<double>::epsilon(); | 4 * fabs(d1) * std::numeric_limits<double>::epsilon(); | ||||
} | } | ||||
// GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize | // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize | ||||
BOOST_AUTO_TEST_CASE(methods) { | BOOST_AUTO_TEST_CASE(methods) { | ||||
BOOST_CHECK(R1L.GetHex() == R1L.ToString()); | BOOST_CHECK(R1L.GetHex() == R1L.ToString()); | ||||
BOOST_CHECK(R2L.GetHex() == R2L.ToString()); | BOOST_CHECK(R2L.GetHex() == R2L.ToString()); | ||||
▲ Show 20 Lines • Show All 229 Lines • Show Last 20 Lines |