11#include <boost/test/unit_test.hpp>
19BOOST_AUTO_TEST_SUITE(uint256_tests)
22 "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
23 "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
24const
char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
29 "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
30 "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
35 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
36 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
41 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
42 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
47 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
48 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
54 std::stringstream Stream;
56 for (
unsigned int i = 0; i < width; ++i)
58 Stream<<std::setw(2)<<std::setfill(
'0')<<(
unsigned int)A[width-i-1];
111 for (
int i = 255; i >= 0; --i) {
113 *(TmpL.
begin() + (i>>3)) |= 1<<(7-(i&7));
114 BOOST_CHECK_LT(LastL, TmpL);
126 for (
int i = 159; i >= 0; --i) {
128 *(TmpS.
begin() + (i>>3)) |= 1<<(7-(i&7));
129 BOOST_CHECK_LT(LastS, TmpS);
146 BOOST_CHECK_LT(
uint256{
"1000000000000000000000000000000000000000000000000000000000000000"},
147 uint256{
"0000000000000000000000000000000000000000000000000000000000000001"});
254 constexpr unsigned int num_chars{T::size() * 2};
255 static_assert(num_chars <= 64);
256 const std::string valid_64char_input{
"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
257 const auto valid_input{valid_64char_input.substr(0, num_chars)};
261 BOOST_REQUIRE(valid_result);
274 std::string invalid_chars{R
"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
275 for (
auto c : invalid_chars) {
279 std::string invalid_prefix{
"0x" + valid_input};
285 std::string chars_68{valid_64char_input +
"0123"};
294 TestFromHex<uint160>();
295 TestFromHex<uint256>();
297 TestFromHex<Wtxid>();
311 const std::string valid_hex_64{
"0x0123456789abcdef0123456789abcdef0123456789ABDCEF0123456789ABCDEF"};
312 BOOST_REQUIRE_EQUAL(valid_hex_64.size(), 2 + 64);
331 uint256 one =
uint256{
"0000000000000000000000000000000000000000000000000000000000000001"};
337 auto runtime_uint{
uint256::FromHex(
"4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b")};
338 constexpr uint256 consteval_uint{
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};
Double ended buffer combining vector and stream-like interfaces.
constexpr unsigned char * end()
static constexpr unsigned int size()
std::string ToString() const
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
constexpr unsigned char * begin()
std::string GetHex() const
static std::optional< uint160 > FromHex(std::string_view str)
static std::optional< uint256 > FromUserHex(std::string_view str)
static const uint256 ZERO
static std::optional< uint256 > FromHex(std::string_view str)
BOOST_AUTO_TEST_SUITE_END()
std::optional< uintN_t > FromHex(std::string_view str)
Writes the hex string (in reverse byte order) into a new uintN_t object and only returns a value iff ...
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
size_t GetSerializeSize(const T &t)
static void from_hex(unsigned char *data, int len, const char *hex)
const unsigned char ZeroArray[]
const unsigned char R1Array[]
BOOST_AUTO_TEST_CASE(basics)
const unsigned char OneArray[]
const unsigned char R2Array[]
static std::string ArrayToString(const unsigned char A[], unsigned int width)
const unsigned char MaxArray[]
void TestFromHex()
Implemented as a templated function so it can be reused by other classes that have a FromHex() method...
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.