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,14 +27,19 @@ 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; } void SetNull() { memset(data, 0, sizeof(data)); } inline int Compare(const base_blob &other) const { + // This doesn't quite work as you'd expect because the comparison use + // the wrong endianess. return memcmp(data, other.data, sizeof(data)); } @@ -47,11 +52,14 @@ 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); void SetHex(const std::string &str); - std::string ToString() const; + std::string ToString() const { return GetHex(); } uint8_t *begin() { return &data[0]; } @@ -65,10 +73,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 { diff --git a/src/uint256.cpp b/src/uint256.cpp --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -18,8 +18,9 @@ template std::string base_blob::GetHex() const { char psz[sizeof(data) * 2 + 1]; - for (unsigned int i = 0; i < sizeof(data); i++) + for (size_t i = 0; i < sizeof(data); i++) { sprintf(psz + i * 2, "%02x", data[sizeof(data) - i - 1]); + } return std::string(psz, psz + sizeof(data) * 2); } @@ -27,16 +28,21 @@ memset(data, 0, sizeof(data)); // skip leading spaces - while (isspace(*psz)) + while (isspace(*psz)) { psz++; + } // skip 0x - if (psz[0] == '0' && tolower(psz[1]) == 'x') psz += 2; + if (psz[0] == '0' && tolower(psz[1]) == 'x') { + psz += 2; + } // hex string to uint const char *pbegin = psz; - while (::HexDigit(*psz) != -1) + while (::HexDigit(*psz) != -1) { psz++; + } + psz--; uint8_t *p1 = (uint8_t *)data; uint8_t *pend = p1 + WIDTH; @@ -54,10 +60,6 @@ SetHex(str.c_str()); } -template std::string base_blob::ToString() const { - return (GetHex()); -} - // Explicit instantiations for base_blob<160> template base_blob<160>::base_blob(const std::vector &); template std::string base_blob<160>::GetHex() const;