aboutsummaryrefslogtreecommitdiff
path: root/src/test/arith_uint256_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/arith_uint256_tests.cpp')
-rw-r--r--src/test/arith_uint256_tests.cpp89
1 files changed, 63 insertions, 26 deletions
diff --git a/src/test/arith_uint256_tests.cpp b/src/test/arith_uint256_tests.cpp
index f178499299..37a39adb9c 100644
--- a/src/test/arith_uint256_tests.cpp
+++ b/src/test/arith_uint256_tests.cpp
@@ -23,9 +23,6 @@ static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& 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[] =
"\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";
@@ -39,8 +36,6 @@ const unsigned char R2Array[] =
"\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 char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
-
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";
@@ -97,27 +92,25 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
}
BOOST_CHECK(ZeroL == (OneL << 256));
- // String Constructor and Copy Constructor
- BOOST_CHECK(arith_uint256S("0x" + R1L.ToString()) == R1L);
- BOOST_CHECK(arith_uint256S("0x" + R2L.ToString()) == R2L);
- BOOST_CHECK(arith_uint256S("0x" + ZeroL.ToString()) == ZeroL);
- BOOST_CHECK(arith_uint256S("0x" + OneL.ToString()) == OneL);
- BOOST_CHECK(arith_uint256S("0x" + MaxL.ToString()) == MaxL);
- BOOST_CHECK(arith_uint256S(R1L.ToString()) == R1L);
- BOOST_CHECK(arith_uint256S(" 0x" + R1L.ToString() + " ") == R1L);
- BOOST_CHECK(arith_uint256S("") == ZeroL);
- BOOST_CHECK(arith_uint256S("1") == OneL);
- BOOST_CHECK(R1L == arith_uint256S(R1ArrayHex));
+ // Construct from hex string
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1L.ToString()).value()), R1L);
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R2L.ToString()).value()), R2L);
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(ZeroL.ToString()).value()), ZeroL);
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(OneL.ToString()).value()), OneL);
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(MaxL.ToString()).value()), MaxL);
+ BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1ArrayHex).value()), R1L);
+
+ // Copy constructor
BOOST_CHECK(arith_uint256(R1L) == R1L);
BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
BOOST_CHECK(arith_uint256(OneL) == OneL);
// uint64_t constructor
- BOOST_CHECK((R1L & arith_uint256S("0xffffffffffffffff")) == arith_uint256(R1LLow64));
- BOOST_CHECK(ZeroL == arith_uint256(0));
- BOOST_CHECK(OneL == arith_uint256(1));
- BOOST_CHECK(arith_uint256S("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL));
+ BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
+ BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});
+ BOOST_CHECK_EQUAL(OneL, arith_uint256{1});
+ BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});
// Assignment (from base_uint)
arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
@@ -284,15 +277,12 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
BOOST_CHECK_LT(ZeroL,
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 )
{
arith_uint256 TmpL = 0;
- BOOST_CHECK(R1L + R2L == arith_uint256S(R1LplusR2L));
+ BOOST_CHECK_EQUAL(R1L + R2L, UintToArith256(uint256{"549fb09fea236a1ea3e31d4d58f1b1369288d204211ca751527cfc175767850c"}));
TmpL += R1L;
BOOST_CHECK(TmpL == R1L);
TmpL += R2L;
@@ -356,8 +346,8 @@ BOOST_AUTO_TEST_CASE( multiply )
BOOST_AUTO_TEST_CASE( divide )
{
- arith_uint256 D1L{arith_uint256S("AD7133AC1977FA2B7")};
- arith_uint256 D2L{arith_uint256S("ECD751716")};
+ arith_uint256 D1L{UintToArith256(uint256{"00000000000000000000000000000000000000000000000ad7133ac1977fa2b7"})};
+ arith_uint256 D2L{UintToArith256(uint256{"0000000000000000000000000000000000000000000000000000000ecd751716"})};
BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
BOOST_CHECK(R1L / OneL == R1L);
@@ -571,4 +561,51 @@ BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% cover
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.
+ */
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wself-assign-overloaded"
+#endif
+ arith_uint256 v{2};
+ v *= v;
+ BOOST_CHECK_EQUAL(v, arith_uint256{4});
+ v /= v;
+ BOOST_CHECK_EQUAL(v, arith_uint256{1});
+ v += v;
+ BOOST_CHECK_EQUAL(v, arith_uint256{2});
+ v -= v;
+ BOOST_CHECK_EQUAL(v, arith_uint256{0});
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+}
+
BOOST_AUTO_TEST_SUITE_END()