aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstickies-v <stickies-v@protonmail.com>2024-07-30 12:29:34 +0100
committerstickies-v <stickies-v@protonmail.com>2024-09-06 17:36:18 +0200
commit62cc4656e2bb38f80485a13d75b42add09a6b731 (patch)
tree54aeea77ea1853d71495ad0ed9e8dcc666e78f3a /src
parentadc00ad728dd54084671398f8fa5c12be92d2bab (diff)
test: remove test-only uint256S
uint256S was previously deprecated for being unsafe. All non-test usage has already been removed in earlier commits. 1. Tests now use uint256::FromHex() or other constructors wherever possible without further modification. 2. Tests that can't use uint256::FromHex() because they use input with non-hex digit characters are a) modified by dropping the non-hex digit characters if that provides useful test coverage. b) dropped if the test without non-hex digit characters does not provide useful test coverage, e.g. because it is now duplicated. Additionally, use BOOST_CHECK_EQUAL where relevant on touched lines to make error messages more readable.
Diffstat (limited to 'src')
-rw-r--r--src/test/fuzz/hex.cpp1
-rw-r--r--src/test/fuzz/integer.cpp2
-rw-r--r--src/test/uint256_tests.cpp56
-rw-r--r--src/uint256.h10
4 files changed, 12 insertions, 57 deletions
diff --git a/src/test/fuzz/hex.cpp b/src/test/fuzz/hex.cpp
index 964e30cc7e..dcf91fd383 100644
--- a/src/test/fuzz/hex.cpp
+++ b/src/test/fuzz/hex.cpp
@@ -37,7 +37,6 @@ FUZZ_TARGET(hex)
if (const auto result{uint256::FromUserHex(random_hex_string)}) {
assert(uint256::FromHex(result->ToString()));
}
- (void)uint256S(random_hex_string);
try {
(void)HexToPubKey(random_hex_string);
} catch (const UniValue&) {
diff --git a/src/test/fuzz/integer.cpp b/src/test/fuzz/integer.cpp
index 02c6796d11..e27b2065d5 100644
--- a/src/test/fuzz/integer.cpp
+++ b/src/test/fuzz/integer.cpp
@@ -140,7 +140,7 @@ FUZZ_TARGET(integer, .init = initialize_integer)
const arith_uint256 au256 = UintToArith256(u256);
assert(ArithToUint256(au256) == u256);
- assert(uint256S(au256.GetHex()) == u256);
+ assert(uint256::FromHex(au256.GetHex()).value() == u256);
(void)au256.bits();
(void)au256.GetCompact(/* fNegative= */ false);
(void)au256.GetCompact(/* fNegative= */ true);
diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp
index c6cacb7997..2a516731e8 100644
--- a/src/test/uint256_tests.cpp
+++ b/src/test/uint256_tests.cpp
@@ -92,18 +92,12 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
BOOST_CHECK_NE(MaxL, ZeroL); BOOST_CHECK_NE(MaxS, ZeroS);
// String Constructor and Copy Constructor
- BOOST_CHECK_EQUAL(uint256S("0x"+R1L.ToString()), R1L);
- BOOST_CHECK_EQUAL(uint256S("0x"+R2L.ToString()), R2L);
- BOOST_CHECK_EQUAL(uint256S("0x"+ZeroL.ToString()), ZeroL);
- BOOST_CHECK_EQUAL(uint256S("0x"+OneL.ToString()), OneL);
- BOOST_CHECK_EQUAL(uint256S("0x"+MaxL.ToString()), MaxL);
- BOOST_CHECK_EQUAL(uint256S(R1L.ToString()), R1L);
- BOOST_CHECK_EQUAL(uint256S(" 0x"+R1L.ToString()+" "), R1L);
- BOOST_CHECK_EQUAL(uint256S(" 0x"+R1L.ToString()+"-trash;%^& "), R1L);
- BOOST_CHECK_EQUAL(uint256S("\t \n \n \f\n\r\t\v\t 0x"+R1L.ToString()+" \t \n \n \f\n\r\t\v\t "), R1L);
- BOOST_CHECK_EQUAL(uint256S(""), ZeroL);
- BOOST_CHECK_EQUAL(uint256S("1"), OneL);
- BOOST_CHECK_EQUAL(R1L, uint256S(R1ArrayHex));
+ BOOST_CHECK_EQUAL(uint256::FromHex(R1L.ToString()).value(), R1L);
+ BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
+ BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), ZeroL);
+ BOOST_CHECK_EQUAL(uint256::FromHex(OneL.ToString()).value(), OneL);
+ BOOST_CHECK_EQUAL(uint256::FromHex(MaxL.ToString()).value(), MaxL);
+ BOOST_CHECK_EQUAL(uint256::FromHex(R1ArrayHex).value(), R1L);
BOOST_CHECK_EQUAL(uint256(R1L), R1L);
BOOST_CHECK_EQUAL(uint256(ZeroL), ZeroL);
BOOST_CHECK_EQUAL(uint256(OneL), OneL);
@@ -282,48 +276,20 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
#endif
- arith_uint256 v = UintToArith256(uint256S("02"));
+ arith_uint256 v{2};
v *= v;
- BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("04")));
+ BOOST_CHECK_EQUAL(v, arith_uint256{4});
v /= v;
- BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("01")));
+ BOOST_CHECK_EQUAL(v, arith_uint256{1});
v += v;
- BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("02")));
+ BOOST_CHECK_EQUAL(v, arith_uint256{2});
v -= v;
- BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("0")));
+ BOOST_CHECK_EQUAL(v, arith_uint256{0});
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
}
-BOOST_AUTO_TEST_CASE(parse)
-{
- {
- std::string s_12{"0000000000000000000000000000000000000000000000000000000000000012"};
- BOOST_CHECK_EQUAL(uint256S("12\0").GetHex(), s_12);
- BOOST_CHECK_EQUAL(uint256S(std::string_view{"12\0", 3}).GetHex(), s_12);
- BOOST_CHECK_EQUAL(uint256S("0x12").GetHex(), s_12);
- BOOST_CHECK_EQUAL(uint256S(" 0x12").GetHex(), s_12);
- BOOST_CHECK_EQUAL(uint256S(" 12").GetHex(), s_12);
- }
- {
- std::string s_1{uint256::ONE.GetHex()};
- BOOST_CHECK_EQUAL(uint256S("1\0").GetHex(), s_1);
- BOOST_CHECK_EQUAL(uint256S(std::string_view{"1\0", 2}).GetHex(), s_1);
- BOOST_CHECK_EQUAL(uint256S("0x1").GetHex(), s_1);
- BOOST_CHECK_EQUAL(uint256S(" 0x1").GetHex(), s_1);
- BOOST_CHECK_EQUAL(uint256S(" 1").GetHex(), s_1);
- }
- {
- std::string s_0{uint256::ZERO.GetHex()};
- BOOST_CHECK_EQUAL(uint256S("\0").GetHex(), s_0);
- BOOST_CHECK_EQUAL(uint256S(std::string_view{"\0", 1}).GetHex(), s_0);
- BOOST_CHECK_EQUAL(uint256S("0x").GetHex(), s_0);
- BOOST_CHECK_EQUAL(uint256S(" 0x").GetHex(), s_0);
- BOOST_CHECK_EQUAL(uint256S(" ").GetHex(), s_0);
- }
-}
-
/**
* Implemented as a templated function so it can be reused by other classes that have a FromHex()
* method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex().
diff --git a/src/uint256.h b/src/uint256.h
index c255f49ad6..8223787041 100644
--- a/src/uint256.h
+++ b/src/uint256.h
@@ -199,14 +199,4 @@ public:
static const uint256 ONE;
};
-/* uint256 from std::string_view, containing byte-reversed hex encoding.
- * DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
- */
-inline uint256 uint256S(std::string_view str)
-{
- uint256 rv;
- rv.SetHexDeprecated(str);
- return rv;
-}
-
#endif // BITCOIN_UINT256_H