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

Code before:
    BOOST_CHECK_EQUAL(fNegative, false);
    BOOST_CHECK_EQUAL(fOverflow, false);

    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 (suggestion):
so these are basically roundtrip tests now, right?
These are very valuable (could even be a fuzz test now), but could we add back some of the hardcoded ones to make sure they're not just self-referentially correct (i.e. circular reasoning):
```C++
    BOOST_CHECK_EQUAL(ArithToUint256(ZeroL), uint256::ZERO);
    BOOST_CHECK_EQUAL(ArithToUint256(OneL), uint256::ONE);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::ZERO), ZeroL);
    BOOST_CHECK_EQUAL(UintToArith256(uint256::ONE), OneL);
```

Code after:
{
    // ~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_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