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

Code before:
    num.SetCompact(0xff123456, &fNegative, &fOverflow);
    BOOST_CHECK_EQUAL(fNegative, false);
    BOOST_CHECK_EQUAL(fOverflow, true);
}


BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
{
    // ~R1L give a base_uint<256>
    BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
    BOOST_CHECK((~~R1L << 10) == (R1L << 10));
    BOOST_CHECK(!(~~R1L < R1L));
    BOOST_CHECK(~~R1L <= R1L);
    BOOST_CHECK(!(~~R1L > R1L));
    BOOST_CHECK(~~R1L >= R1L);
    BOOST_CHECK(!(R1L < ~~R1L));
    BOOST_CHECK(R1L <= ~~R1L);
    BOOST_CHECK(!(R1L > ~~R1L));
    BOOST_CHECK(R1L >= ~~R1L);

    BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
    BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
    BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
    unsigned char TmpArray[32];
    CHECKBITWISEOPERATOR(~R1,R2,|)
    CHECKBITWISEOPERATOR(~R1,R2,^)
    CHECKBITWISEOPERATOR(~R1,R2,&)
    CHECKBITWISEOPERATOR(R1,~R2,|)
    CHECKBITWISEOPERATOR(R1,~R2,^)
    CHECKBITWISEOPERATOR(R1,~R2,&)
}

BOOST_AUTO_TEST_SUITE_END()


Review comment (refactor):
The move of this test to this file and changes to use raw integer literals make total sense.

meganit: If you re-touch - Might just remove this empty line as the comment above applies to the preprocessor logic beneath.

Code after:
    BOOST_CHECK((~~R1L << 10) == (R1L << 10));
    BOOST_CHECK(!(~~R1L < R1L));
    BOOST_CHECK(~~R1L <= R1L);
    BOOST_CHECK(!(~~R1L > R1L));
    BOOST_CHECK(~~R1L >= R1L);
    BOOST_CHECK(!(R1L < ~~R1L));
    BOOST_CHECK(R1L <= ~~R1L);
    BOOST_CHECK(!(R1L > ~~R1L));
    BOOST_CHECK(R1L >= ~~R1L);

    BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
    BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
    BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
    unsigned char TmpArray[32];
    CHECKBITWISEOPERATOR(~R1,R2,|)
    CHECKBITWISEOPERATOR(~R1,R2,^)
    CHECKBITWISEOPERATOR(~R1,R2,&)
    CHECKBITWISEOPERATOR(R1,~R2,|)
    CHECKBITWISEOPERATOR(R1,~R2,^)
    CHECKBITWISEOPERATOR(R1,~R2,&)
}

BOOST_AUTO_TEST_CASE(conversion)
{
    for (const arith_uint256& arith : {ZeroL, OneL, R1L, R2L}) {
        const auto u256{uint256::FromHex(arith.GetHex()).value()};
        BOOST_CHECK_EQUAL(UintToArith256(ArithToUint256(arith)), arith);
        BOOST_CHECK_EQUAL(UintToArith256(u256), arith);
        BOOST_CHECK_EQUAL(u256, ArithToUint256(arith));
        BOOST_CHECK_EQUAL(ArithToUint256(arith).GetHex(), UintToArith256(u256).GetHex());
    }

    for (uint8_t num : {0, 1, 0xff}) {
        BOOST_CHECK_EQUAL(UintToArith256(uint256{num}), arith_uint256{num});
        BOOST_CHECK_EQUAL(uint256{num}, ArithToUint256(arith_uint256{num}));
        BOOST_CHECK_EQUAL(UintToArith256(uint256{num}), num);
    }
}

BOOST_AUTO_TEST_CASE(operator_with_self)
{
    /* Clang 16 and earlier detects v -= v and v /= v as self-assignments
       to 0 and 1 respectively.
       See: https://github.com/llvm/llvm-project/issues/42469
       and the fix in commit c5302325b2a62d77cf13dd16cd5c19141862fed0 .

       This makes some sense for arithmetic classes, but could be considered a bug
       elsewhere. Disable the warning here so that the code can be tested, but the
       warning should remain on as there will likely always be a better way to
       express this.
    */