Repository: bitcoin/bitcoin
File: src/test/uint256_tests.cpp (C++)

Code before:

    // Verify hex strings are little-endian
    BOOST_CHECK_LT(uint256S("2000000000000000000000000000000000000000000000000000000000000001"),
                   uint256S("1000000000000000000000000000000000000000000000000000000000000002"));
}

BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
{
    BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
    BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
    BOOST_CHECK_EQUAL(OneL.GetHex(), OneL.ToString());
    BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
    uint256 TmpL(R1L);
    BOOST_CHECK_EQUAL(TmpL, R1L);
    // Verify previous values don't persist when setting to truncated string.
    TmpL.SetHexDeprecated("21");
    BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
    BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
    BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());

    TmpL = uint256::FromHex(R1L.ToString()).value();
    BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array + R1L.size());
    BOOST_CHECK_EQUAL_COLLECTIONS(TmpL.begin(), TmpL.end(), R1Array, R1Array + TmpL.size());
    BOOST_CHECK_EQUAL_COLLECTIONS(R2L.begin(), R2L.end(), R2Array, R2Array + R2L.size());
    BOOST_CHECK_EQUAL_COLLECTIONS(ZeroL.begin(), ZeroL.end(), ZeroArray, ZeroArray + ZeroL.size());
    BOOST_CHECK_EQUAL_COLLECTIONS(OneL.begin(), OneL.end(), OneArray, OneArray + OneL.size());
    BOOST_CHECK_EQUAL(R1L.size(), sizeof(R1L));
    BOOST_CHECK_EQUAL(sizeof(R1L), 32);
    BOOST_CHECK_EQUAL(R1L.size(), 32);
    BOOST_CHECK_EQUAL(R2L.size(), 32);
    BOOST_CHECK_EQUAL(ZeroL.size(), 32);
    BOOST_CHECK_EQUAL(MaxL.size(), 32);
    BOOST_CHECK_EQUAL(R1L.begin() + 32, R1L.end());
    BOOST_CHECK_EQUAL(R2L.begin() + 32, R2L.end());
    BOOST_CHECK_EQUAL(OneL.begin() + 32, OneL.end());
    BOOST_CHECK_EQUAL(MaxL.begin() + 32, MaxL.end());
    BOOST_CHECK_EQUAL(TmpL.begin() + 32, TmpL.end());
    BOOST_CHECK_EQUAL(GetSerializeSize(R1L), 32);
    BOOST_CHECK_EQUAL(GetSerializeSize(ZeroL), 32);

    DataStream ss{};
    ss << R1L;
    BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+32));
    ss >> TmpL;
    BOOST_CHECK_EQUAL(R1L, TmpL);
    ss.clear();
    ss << ZeroL;
    BOOST_CHECK_EQUAL(ss.str(), std::string(ZeroArray,ZeroArray+32));
    ss >> TmpL;
    BOOST_CHECK_EQUAL(ZeroL, TmpL);
    ss.clear();

Review comment (suggestion):
👍 , if possible, we should do the `*_EQUAL` changes in the `arith_uint256_tests` as well (probably best in a different PR).

Code after:
    BOOST_CHECK_LT(R2L, R1L);
    // Ensure first element comparisons give the same order as above:
    BOOST_CHECK_LT(*R2L.begin(), *R1L.begin());
    // Ensure last element comparisons give a different result (swapped params):
    BOOST_CHECK_LT(*(R1L.end()-1), *(R2L.end()-1));
    // Hex strings represent reverse-encoded bytes, with lexicographic ordering:
    BOOST_CHECK_LT(uint256S("1000000000000000000000000000000000000000000000000000000000000000"),
                   uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
}

BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
{
    BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
    BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
    BOOST_CHECK_EQUAL(OneL.GetHex(), OneL.ToString());
    BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
    uint256 TmpL(R1L);
    BOOST_CHECK_EQUAL(TmpL, R1L);
    // Verify previous values don't persist when setting to truncated string.
    TmpL.SetHexDeprecated("21");
    BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
    BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
    BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());

    TmpL = uint256::FromHex(R1L.ToString()).value();
    BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array + uint256::size());
    BOOST_CHECK_EQUAL_COLLECTIONS(TmpL.begin(), TmpL.end(), R1Array, R1Array + uint256::size());
    BOOST_CHECK_EQUAL_COLLECTIONS(R2L.begin(), R2L.end(), R2Array, R2Array + uint256::size());
    BOOST_CHECK_EQUAL_COLLECTIONS(ZeroL.begin(), ZeroL.end(), ZeroArray, ZeroArray + uint256::size());
    BOOST_CHECK_EQUAL_COLLECTIONS(OneL.begin(), OneL.end(), OneArray, OneArray + uint256::size());
    BOOST_CHECK_EQUAL(R1L.size(), sizeof(R1L));
    BOOST_CHECK_EQUAL(sizeof(R1L), 32);
    BOOST_CHECK_EQUAL(R1L.size(), 32);
    BOOST_CHECK_EQUAL(R2L.size(), 32);
    BOOST_CHECK_EQUAL(ZeroL.size(), 32);
    BOOST_CHECK_EQUAL(MaxL.size(), 32);
    BOOST_CHECK_EQUAL(R1L.begin() + 32, R1L.end());
    BOOST_CHECK_EQUAL(R2L.begin() + 32, R2L.end());
    BOOST_CHECK_EQUAL(OneL.begin() + 32, OneL.end());
    BOOST_CHECK_EQUAL(MaxL.begin() + 32, MaxL.end());
    BOOST_CHECK_EQUAL(TmpL.begin() + 32, TmpL.end());
    BOOST_CHECK_EQUAL(GetSerializeSize(R1L), 32);
    BOOST_CHECK_EQUAL(GetSerializeSize(ZeroL), 32);

    DataStream ss{};
    ss << R1L;
    BOOST_CHECK_EQUAL(ss.str(), std::string(R1Array,R1Array+32));
    ss >> TmpL;
    BOOST_CHECK_EQUAL(R1L, TmpL);
    ss.clear();
    ss << ZeroL;