diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -111,36 +111,47 @@ BOOST_CHECK(uint160(OneS) == OneS); } +static void CheckComparison(const uint256 &a, const uint256 &b) { + BOOST_CHECK(a < b); + BOOST_CHECK(b > a); +} + +static void CheckComparison(const uint160 &a, const uint160 &b) { + BOOST_CHECK(a < b); + BOOST_CHECK(b > a); +} + // <= >= < > BOOST_AUTO_TEST_CASE(comparison) { uint256 LastL; for (int i = 255; i >= 0; --i) { uint256 TmpL; *(TmpL.begin() + (i >> 3)) |= 1 << (7 - (i & 7)); - BOOST_CHECK(LastL < TmpL); + CheckComparison(LastL, TmpL); LastL = TmpL; } - BOOST_CHECK(ZeroL < R1L); - BOOST_CHECK(R2L < R1L); - BOOST_CHECK(ZeroL < OneL); - BOOST_CHECK(OneL < MaxL); - BOOST_CHECK(R1L < MaxL); - BOOST_CHECK(R2L < MaxL); + CheckComparison(ZeroL, R1L); + CheckComparison(R2L, R1L); + CheckComparison(ZeroL, OneL); + CheckComparison(OneL, MaxL); + CheckComparison(R1L, MaxL); + CheckComparison(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); + CheckComparison(LastS, TmpS); LastS = TmpS; } - BOOST_CHECK(ZeroS < R1S); - BOOST_CHECK(R2S < R1S); - BOOST_CHECK(ZeroS < OneS); - BOOST_CHECK(OneS < MaxS); - BOOST_CHECK(R1S < MaxS); - BOOST_CHECK(R2S < MaxS); + + CheckComparison(ZeroS, R1S); + CheckComparison(R2S, R1S); + CheckComparison(ZeroS, OneS); + CheckComparison(OneS, MaxS); + CheckComparison(R1S, MaxS); + CheckComparison(R2S, MaxS); } // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, diff --git a/src/uint256.h b/src/uint256.h --- a/src/uint256.h +++ b/src/uint256.h @@ -27,8 +27,11 @@ explicit base_blob(const std::vector &vch); bool IsNull() const { - for (int i = 0; i < WIDTH; i++) - if (data[i] != 0) return false; + for (int i = 0; i < WIDTH; i++) { + if (data[i] != 0) { + return false; + } + } return true; } @@ -47,6 +50,9 @@ friend inline bool operator<(const base_blob &a, const base_blob &b) { return a.Compare(b) < 0; } + friend inline bool operator>(const base_blob &a, const base_blob &b) { + return a.Compare(b) > 0; + } std::string GetHex() const; void SetHex(const char *psz); @@ -65,10 +71,10 @@ uint64_t GetUint64(int pos) const { const uint8_t *ptr = data + pos * 8; - return ((uint64_t)ptr[0]) | ((uint64_t)ptr[1]) << 8 | - ((uint64_t)ptr[2]) << 16 | ((uint64_t)ptr[3]) << 24 | - ((uint64_t)ptr[4]) << 32 | ((uint64_t)ptr[5]) << 40 | - ((uint64_t)ptr[6]) << 48 | ((uint64_t)ptr[7]) << 56; + return uint64_t(ptr[0]) | (uint64_t(ptr[1]) << 8) | + (uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) | + (uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) | + (uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56); } template void Serialize(Stream &s) const {