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

Code before:
    BOOST_CHECK_LT(ZeroL, OneL);
    BOOST_CHECK_LT(OneL, MaxL);
    BOOST_CHECK_LT(R1L, MaxL);
    BOOST_CHECK_LT(R2L, MaxL);

    uint160 LastS;
    for (int i = 159; i >= 0; --i) {
        uint160 TmpS;
        *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
        BOOST_CHECK_LT(LastS, TmpS);
        LastS = TmpS;
    }
    BOOST_CHECK_LT(ZeroS, R1S);
    BOOST_CHECK_LT(R2S, R1S);
    BOOST_CHECK_LT(ZeroS, OneS);
    BOOST_CHECK_LT(OneS, MaxS);
    BOOST_CHECK_LT(R1S, MaxS);
    BOOST_CHECK_LT(R2S, MaxS);

    // 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());

Review comment (suggestion):
In commit "doc + test: Correct uint256 hex string endianness" (07de45a882d43fd5567b2a6b7c8a2da3d6dbda3d)

I think if you wanted to make the test a stronger demonstration of the fact that last bytes in the hex strings determine the ordering, you would make the first bytes in the earlier hex string as high as possible to show that they don't effect the comparison. E.g.

```c++
    BOOST_CHECK_LT(uint256S("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00"),
                   uint256S("0000000000000000000000000000000000000000000000000000000000000001"));

```

Code after:
    BOOST_CHECK_LT(R1L, MaxL);
    BOOST_CHECK_LT(R2L, MaxL);

    uint160 LastS;
    for (int i = 159; i >= 0; --i) {
        uint160 TmpS;
        *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
        BOOST_CHECK_LT(LastS, TmpS);
        LastS = TmpS;
    }
    BOOST_CHECK_LT(ZeroS, R1S);
    BOOST_CHECK_LT(R2S, R1S);
    BOOST_CHECK_LT(ZeroS, OneS);
    BOOST_CHECK_LT(OneS, MaxS);
    BOOST_CHECK_LT(R1S, MaxS);
    BOOST_CHECK_LT(R2S, MaxS);

    // Non-arithmetic uint256s compare from the beginning of their inner arrays:
    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);