Skip to content

Commit

Permalink
Load 64 bit integers for dec and hex string. Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Mar 21, 2023
1 parent 2a22c9a commit 02f8a81
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
6 changes: 6 additions & 0 deletions test/statetest/statetest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct StateTransitionTest
template <typename T>
T from_json(const json::json& j) = delete;

template <>
uint64_t from_json<uint64_t>(const json::json& j);

template <>
int64_t from_json<int64_t>(const json::json& j);

template <>
state::BlockInfo from_json<state::BlockInfo>(const json::json& j);

Expand Down
14 changes: 12 additions & 2 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ uint8_t from_json<uint8_t>(const json::json& j)
template <>
int64_t from_json<int64_t>(const json::json& j)
{
return static_cast<int64_t>(std::stoll(j.get<std::string>(), nullptr, 16));
if (j.is_number_integer())
return j.get<int64_t>();
else if (j.is_string())
return std::stoll(j.get<std::string>(), nullptr, 0);
else
throw std::invalid_argument("from_json<int64_t>: must be string or number");
}

template <>
uint64_t from_json<uint64_t>(const json::json& j)
{
return static_cast<uint64_t>(std::stoull(j.get<std::string>(), nullptr, 16));
if (j.is_number_integer())
return j.get<uint64_t>();
else if (j.is_string())
return std::stoull(j.get<std::string>(), nullptr, 0);
else
throw std::invalid_argument("from_json<uint64_t>: must be string or number");
}

template <>
Expand Down
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_sources(
evmone_test.cpp
execution_state_test.cpp
instructions_test.cpp
json_loader_test.cpp
state_bloom_filter_test.cpp
state_mpt_hash_test.cpp
state_mpt_test.cpp
Expand Down
57 changes: 57 additions & 0 deletions test/unittests/json_loader_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <test/statetest/statetest.hpp>

using namespace evmone;

TEST(json_loader, uint64_t)
{
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json("0x00000005")), 5);
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json("5")), 5);
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json(7)), 7);

EXPECT_EQ(test::from_json<uint64_t>(json::basic_json("0xffffffffffffffff")),
std::numeric_limits<uint64_t>::max());
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json("18446744073709551615")),
std::numeric_limits<uint64_t>::max());
EXPECT_THROW(
test::from_json<uint64_t>(json::basic_json("0x10000000000000000")), std::out_of_range);
EXPECT_THROW(
test::from_json<uint64_t>(json::basic_json("18446744073709551616")), std::out_of_range);
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json(0xffffffffffffffff)),
std::numeric_limits<uint64_t>::max());

// TODO: Isn't it a little strange behaviour of std::stoull?
EXPECT_EQ(test::from_json<uint64_t>(json::basic_json("0x000000000000000k")), 0);
EXPECT_THROW(test::from_json<uint64_t>(json::basic_json("k")), std::invalid_argument);
}

TEST(json_loader, int64_t)
{
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("0x00000005")), 5);
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("-0x5")), -5);
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("-5")), -5);

EXPECT_EQ(test::from_json<int64_t>(json::basic_json(-7)), -7);
EXPECT_EQ(test::from_json<int64_t>(json::basic_json(0xffffffffffffffff)), -1);

EXPECT_EQ(test::from_json<int64_t>(json::basic_json("0x7fffffffffffffff")),
std::numeric_limits<int64_t>::max());
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("9223372036854775807")),
std::numeric_limits<int64_t>::max());
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("-9223372036854775808")),
std::numeric_limits<int64_t>::min());
EXPECT_THROW(
test::from_json<int64_t>(json::basic_json("0xffffffffffffffff")), std::out_of_range);
EXPECT_THROW(
test::from_json<int64_t>(json::basic_json("9223372036854775808")), std::out_of_range);
EXPECT_THROW(
test::from_json<int64_t>(json::basic_json("-9223372036854775809")), std::out_of_range);

// TODO: Isn't it a little strange behaviour of std::stoull?
EXPECT_EQ(test::from_json<int64_t>(json::basic_json("0x000000000000000k")), 0);
EXPECT_THROW(test::from_json<int64_t>(json::basic_json("k")), std::invalid_argument);
}
8 changes: 4 additions & 4 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST(statetest_loader, block_info)
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x1111111111111111111111111111111111111111_address);
EXPECT_EQ(
bi.prev_randao, 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32);
bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 0x0);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 0);
Expand Down Expand Up @@ -55,7 +55,7 @@ TEST(statetest_loader, block_info_hex)
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
EXPECT_EQ(
bi.prev_randao, 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32);
bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 100000000000000000);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 1000);
Expand Down Expand Up @@ -87,7 +87,7 @@ TEST(statetest_loader, block_info_dec)
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
EXPECT_EQ(
bi.prev_randao, 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32);
bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 100000000000000000);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 1000);
Expand All @@ -110,7 +110,7 @@ TEST(statetest_loader, block_info_0_random)
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x1111111111111111111111111111111111111111_address);
EXPECT_EQ(
bi.prev_randao, 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32);
bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 0x0);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 0);
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/statetest_loader_tx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TEST(statetest_loader, tx_create_legacy)
{
constexpr std::string_view input = R"({
"input": "b0b1",
"gas": "9091",
"gas": "0x9091",
"value": "0xe0e1",
"sender": "a0a1",
"to": "",
Expand Down Expand Up @@ -46,7 +46,7 @@ TEST(statetest_loader, tx_eip1559)
{
constexpr std::string_view input = R"({
"input": "b0b1",
"gas": "9091",
"gas": "0x9091",
"value": "0xe0e1",
"sender": "a0a1",
"to": "c0c1",
Expand Down

0 comments on commit 02f8a81

Please sign in to comment.