mirror of https://github.com/status-im/evmc.git
cpp: Add converting constructor from uint64_t to bytes32
This commit is contained in:
parent
f5d044d901
commit
677b7d97e5
|
@ -39,6 +39,45 @@ struct bytes32 : evmc_bytes32
|
||||||
/// Initializes bytes to zeros if not other @p init value provided.
|
/// Initializes bytes to zeros if not other @p init value provided.
|
||||||
constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
|
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.
|
/// Explicit operator converting to bool.
|
||||||
constexpr inline explicit operator bool() const noexcept;
|
constexpr inline explicit operator bool() const noexcept;
|
||||||
};
|
};
|
||||||
|
|
|
@ -330,6 +330,28 @@ TEST(cpp, literals)
|
||||||
EXPECT_EQ(h1, f1);
|
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)
|
TEST(cpp, result)
|
||||||
{
|
{
|
||||||
static const uint8_t output = 0;
|
static const uint8_t output = 0;
|
||||||
|
|
Loading…
Reference in New Issue