cpp: Add basic types with default constructor

This commit is contained in:
Paweł Bylica 2019-07-20 00:08:10 +02:00
parent 83d2acffa2
commit e74f42e2ef
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 53 additions and 0 deletions

View File

@ -14,6 +14,28 @@
/// @ingroup cpp
namespace evmc
{
/// The big-endian 160-bit hash suitable for keeping an Ethereum address.
///
/// This type wraps C ::evmc_address to make sure objects of this type are always initialized.
struct address : evmc_address
{
/// Default and converting constructor.
///
/// Initializes bytes to zeros if not other @p init value provided.
constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
};
/// The fixed size array of 32 bytes for storing 256-bit EVM values.
///
/// This type wraps C ::evmc_bytes32 to make sure objects of this type are always initialized.
struct bytes32 : evmc_bytes32
{
/// Default and converting constructor.
///
/// Initializes bytes to zeros if not other @p init value provided.
constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
};
/// @copydoc evmc_result
///
/// This is a RAII wrapper for evmc_result and objects of this type

View File

@ -16,6 +16,37 @@
#include <cstring>
TEST(cpp, address)
{
evmc::address a;
EXPECT_EQ(std::count(std::begin(a.bytes), std::end(a.bytes), 0), sizeof(a));
auto other = evmc_address{};
other.bytes[19] = 0xfe;
a = other;
EXPECT_TRUE(std::equal(std::begin(a.bytes), std::end(a.bytes), std::begin(other.bytes)));
a.bytes[0] = 1;
other = a;
EXPECT_TRUE(std::equal(std::begin(a.bytes), std::end(a.bytes), std::begin(other.bytes)));
}
TEST(cpp, bytes32)
{
evmc::bytes32 b;
EXPECT_EQ(std::count(std::begin(b.bytes), std::end(b.bytes), 0), sizeof(b));
auto other = evmc_bytes32{};
other.bytes[31] = 0xfe;
b = other;
EXPECT_TRUE(std::equal(std::begin(b.bytes), std::end(b.bytes), std::begin(other.bytes)));
b.bytes[0] = 1;
other = b;
EXPECT_TRUE(std::equal(std::begin(b.bytes), std::end(b.bytes), std::begin(other.bytes)));
}
TEST(cpp, result)
{
static int release_called = 0;