Merge pull request #182 from ethereum/is_zero

helpers: Add is_zero() helper
This commit is contained in:
Paweł Bylica 2019-01-18 13:52:12 +01:00 committed by GitHub
commit f27aa78a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/
/**
@ -41,6 +41,18 @@ inline bool operator==(const evmc_bytes32& a, const evmc_bytes32& b)
return std::memcmp(a.bytes, b.bytes, sizeof(a.bytes)) == 0;
}
/// Check if the address is zero (all bytes are zeros).
inline bool is_zero(const evmc_address& address) noexcept
{
return address == evmc_address{};
}
/// Check if the hash is zero (all bytes are zeros).
inline bool is_zero(const evmc_bytes32& x) noexcept
{
return x == evmc_bytes32{};
}
/// FNV1a hash function with 64-bit result.
inline uint64_t fnv1a_64(const uint8_t* ptr, size_t len)
{

View File

@ -1,6 +1,6 @@
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2018 The EVMC Authors.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
// Copyright 2019 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.
#include <evmc/helpers.hpp>
@ -27,3 +27,16 @@ TEST(helpers, maps)
unordered_storage.emplace(*storage.begin());
EXPECT_EQ(unordered_storage.size(), 1);
}
TEST(helpers, is_zero)
{
auto a = evmc_address{};
EXPECT_TRUE(is_zero(a));
a.bytes[0] = 1;
EXPECT_FALSE(is_zero(a));
auto b = evmc_bytes32{};
EXPECT_TRUE(is_zero(b));
b.bytes[0] = 1;
EXPECT_FALSE(is_zero(b));
}