cpp: Add converting constructor from uint64_t to bytes32

This commit is contained in:
Paweł Bylica 2019-12-12 17:46:56 +01:00
parent f5d044d901
commit 677b7d97e5
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 61 additions and 0 deletions

View File

@ -39,6 +39,45 @@ struct bytes32 : evmc_bytes32
/// Initializes bytes to zeros if not other @p init value provided.
constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
/// Converting constructor from unsigned integer value.
///
/// This constructor assigns the @p v value to the last 8 bytes [24:31]
/// in big-endian order.
constexpr explicit bytes32(uint64_t v) noexcept
: evmc_bytes32{{0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
static_cast<uint8_t>(v >> 56),
static_cast<uint8_t>(v >> 48),
static_cast<uint8_t>(v >> 40),
static_cast<uint8_t>(v >> 32),
static_cast<uint8_t>(v >> 24),
static_cast<uint8_t>(v >> 16),
static_cast<uint8_t>(v >> 8),
static_cast<uint8_t>(v >> 0)}}
{}
/// Explicit operator converting to bool.
constexpr inline explicit operator bool() const noexcept;
};

View File

@ -330,6 +330,28 @@ TEST(cpp, literals)
EXPECT_EQ(h1, f1);
}
TEST(cpp, bytes32_from_uint)
{
using evmc::bytes32;
using evmc::operator""_bytes32;
static_assert(bytes32{0} == bytes32{}, "");
static_assert(bytes32{3}.bytes[31] == 3, "");
static_assert(bytes32{0xfe00000000000000}.bytes[24] == 0xfe, "");
EXPECT_EQ(bytes32{0}, bytes32{});
EXPECT_EQ(bytes32{0x01},
0x0000000000000000000000000000000000000000000000000000000000000001_bytes32);
EXPECT_EQ(bytes32{0xff},
0x00000000000000000000000000000000000000000000000000000000000000ff_bytes32);
EXPECT_EQ(bytes32{0x500},
0x0000000000000000000000000000000000000000000000000000000000000500_bytes32);
EXPECT_EQ(bytes32{0x8000000000000000},
0x0000000000000000000000000000000000000000000000008000000000000000_bytes32);
EXPECT_EQ(bytes32{0xc1c2c3c4c5c6c7c8},
0x000000000000000000000000000000000000000000000000c1c2c3c4c5c6c7c8_bytes32);
}
TEST(cpp, result)
{
static const uint8_t output = 0;