test: remove test-only arith_uint256S

Tests that are solely testing constructing from a hex string
are dropped, others are modified to use a uint256 constructor
or the arith_uint256 uint64_t constructor.

Since an arith_uint256 can not be constructed from a string
directly, we need to ensure that test coverage on
UintToArith256(uint256::FromHex()) is not reduced.
uint256::FromHex() already has good test coverage, but
the test coverage on UintToArith256() and ArithToUint256()
is increased in this commit by upgrading the `conversion`
test case.

Moreover, since `uint256.h` does not have any dependencies
on `arith_uint256.h`, the conversion tests are moved to
`arith_uint256_tests.cpp` so the dependency can be cleaned
up entirely in a future commit.
This commit is contained in:
stickies-v 2024-09-02 14:19:27 +01:00
parent f51b237723
commit adc00ad728
No known key found for this signature in database
GPG key ID: 5CB1CE6E5E66A757
2 changed files with 33 additions and 42 deletions

View file

@ -23,9 +23,6 @@ static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch
{ {
return UintToArith256(uint256(vch)); return UintToArith256(uint256(vch));
} }
// Takes a number written in hex (with most significant digits first).
static inline arith_uint256 arith_uint256S(std::string_view str) { return UintToArith256(uint256S(str)); }
const unsigned char R1Array[] = const unsigned char R1Array[] =
"\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
"\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
@ -39,8 +36,6 @@ const unsigned char R2Array[] =
"\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32)); const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
const unsigned char ZeroArray[] = const unsigned char ZeroArray[] =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
@ -97,27 +92,25 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
} }
BOOST_CHECK(ZeroL == (OneL << 256)); BOOST_CHECK(ZeroL == (OneL << 256));
// String Constructor and Copy Constructor // Construct from hex string
BOOST_CHECK(arith_uint256S("0x" + R1L.ToString()) == R1L); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1L.ToString()).value()), R1L);
BOOST_CHECK(arith_uint256S("0x" + R2L.ToString()) == R2L); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R2L.ToString()).value()), R2L);
BOOST_CHECK(arith_uint256S("0x" + ZeroL.ToString()) == ZeroL); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(ZeroL.ToString()).value()), ZeroL);
BOOST_CHECK(arith_uint256S("0x" + OneL.ToString()) == OneL); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(OneL.ToString()).value()), OneL);
BOOST_CHECK(arith_uint256S("0x" + MaxL.ToString()) == MaxL); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(MaxL.ToString()).value()), MaxL);
BOOST_CHECK(arith_uint256S(R1L.ToString()) == R1L); BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1ArrayHex).value()), R1L);
BOOST_CHECK(arith_uint256S(" 0x" + R1L.ToString() + " ") == R1L);
BOOST_CHECK(arith_uint256S("") == ZeroL); // Copy constructor
BOOST_CHECK(arith_uint256S("1") == OneL);
BOOST_CHECK(R1L == arith_uint256S(R1ArrayHex));
BOOST_CHECK(arith_uint256(R1L) == R1L); BOOST_CHECK(arith_uint256(R1L) == R1L);
BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L); BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
BOOST_CHECK(arith_uint256(ZeroL) == ZeroL); BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
BOOST_CHECK(arith_uint256(OneL) == OneL); BOOST_CHECK(arith_uint256(OneL) == OneL);
// uint64_t constructor // uint64_t constructor
BOOST_CHECK((R1L & arith_uint256S("0xffffffffffffffff")) == arith_uint256(R1LLow64)); BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
BOOST_CHECK(ZeroL == arith_uint256(0)); BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});
BOOST_CHECK(OneL == arith_uint256(1)); BOOST_CHECK_EQUAL(OneL, arith_uint256{1});
BOOST_CHECK(arith_uint256S("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL)); BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});
// Assignment (from base_uint) // Assignment (from base_uint)
arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
@ -284,15 +277,12 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
BOOST_CHECK_LT(ZeroL, BOOST_CHECK_LT(ZeroL,
OneL); OneL);
// Verify hex number representation has the most significant digits first.
BOOST_CHECK_LT(arith_uint256S("0000000000000000000000000000000000000000000000000000000000000001"),
arith_uint256S("1000000000000000000000000000000000000000000000000000000000000000"));
} }
BOOST_AUTO_TEST_CASE( plusMinus ) BOOST_AUTO_TEST_CASE( plusMinus )
{ {
arith_uint256 TmpL = 0; arith_uint256 TmpL = 0;
BOOST_CHECK(R1L + R2L == arith_uint256S(R1LplusR2L)); BOOST_CHECK_EQUAL(R1L + R2L, UintToArith256(uint256{"549fb09fea236a1ea3e31d4d58f1b1369288d204211ca751527cfc175767850c"}));
TmpL += R1L; TmpL += R1L;
BOOST_CHECK(TmpL == R1L); BOOST_CHECK(TmpL == R1L);
TmpL += R2L; TmpL += R2L;
@ -356,8 +346,8 @@ BOOST_AUTO_TEST_CASE( multiply )
BOOST_AUTO_TEST_CASE( divide ) BOOST_AUTO_TEST_CASE( divide )
{ {
arith_uint256 D1L{arith_uint256S("AD7133AC1977FA2B7")}; arith_uint256 D1L{UintToArith256(uint256{"00000000000000000000000000000000000000000000000ad7133ac1977fa2b7"})};
arith_uint256 D2L{arith_uint256S("ECD751716")}; arith_uint256 D2L{UintToArith256(uint256{"0000000000000000000000000000000000000000000000000000000ecd751716"})};
BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a"); BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a"); BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
BOOST_CHECK(R1L / OneL == R1L); BOOST_CHECK(R1L / OneL == R1L);
@ -571,4 +561,21 @@ BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% cover
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_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View file

@ -264,22 +264,6 @@ BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() s
ss.clear(); ss.clear();
} }
BOOST_AUTO_TEST_CASE( conversion )
{
BOOST_CHECK_EQUAL(ArithToUint256(UintToArith256(ZeroL)), ZeroL);
BOOST_CHECK_EQUAL(ArithToUint256(UintToArith256(OneL)), OneL);
BOOST_CHECK_EQUAL(ArithToUint256(UintToArith256(R1L)), R1L);
BOOST_CHECK_EQUAL(ArithToUint256(UintToArith256(R2L)), R2L);
BOOST_CHECK_EQUAL(UintToArith256(ZeroL), 0);
BOOST_CHECK_EQUAL(UintToArith256(OneL), 1);
BOOST_CHECK_EQUAL(ArithToUint256(0), ZeroL);
BOOST_CHECK_EQUAL(ArithToUint256(1), OneL);
BOOST_CHECK_EQUAL(arith_uint256(UintToArith256(uint256S(R1L.GetHex()))), UintToArith256(R1L));
BOOST_CHECK_EQUAL(arith_uint256(UintToArith256(uint256S(R2L.GetHex()))), UintToArith256(R2L));
BOOST_CHECK_EQUAL(R1L.GetHex(), UintToArith256(R1L).GetHex());
BOOST_CHECK_EQUAL(R2L.GetHex(), UintToArith256(R2L).GetHex());
}
BOOST_AUTO_TEST_CASE( operator_with_self ) BOOST_AUTO_TEST_CASE( operator_with_self )
{ {