Skip to content

Commit

Permalink
Support container kind field in json EOF validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jun 21, 2024
1 parent 734621d commit 98b39ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
24 changes: 19 additions & 5 deletions test/eoftest/eoftest_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,39 @@ struct EOFValidationTest
{
struct Expectation
{
evmc_revision rev;
bool result;
evmc_revision rev = EVMC_PRAGUE;
bool result = false;
};
std::string name;
evmc::bytes code;
ContainerKind kind = ContainerKind::runtime;
std::vector<Expectation> expectations;
};
std::unordered_map<std::string, Case> cases;
};

ContainerKind container_kind_from_string(std::string_view s)
{
if (s == "runtime")
return ContainerKind::runtime;
else if (s == "initcode")
return ContainerKind::initcode;
else if (s == "initcode_runtime")
return ContainerKind::initcode_runtime;
else
throw std::invalid_argument{"unknown container kind"};
}

void from_json(const json::json& j, EOFValidationTest::Case& o)
{
const auto op_code = evmc::from_hex(j.at("code").get<std::string>());
if (!op_code)
throw std::invalid_argument{"code is invalid hex string"};
o.code = *op_code;

if (const auto it_kind = j.find("kind"); it_kind != j.end())
o.kind = container_kind_from_string(it_kind->get<std::string>());

for (const auto& [rev, result] : j.at("results").items())
{
o.expectations.push_back({to_rev(rev), result.at("result").get<bool>()});
Expand Down Expand Up @@ -67,9 +83,7 @@ void run_eof_test(std::istream& input)
{
for (const auto& expectation : cases.expectations)
{
// TODO read requested container kind from the test
const auto result =
evmone::validate_eof(expectation.rev, ContainerKind::runtime, cases.code);
const auto result = evmone::validate_eof(expectation.rev, cases.kind, cases.code);
const bool b_result = (result == EOFValidationError::success);
EXPECT_EQ(b_result, expectation.result)
<< name << " " << expectation.rev << " " << hex(cases.code);
Expand Down
16 changes: 16 additions & 0 deletions test/unittests/eof_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ std::string_view get_tests_error_message(EOFValidationError err) noexcept
}
return "<unknown>";
}

std::string_view to_string(ContainerKind container_kind) noexcept
{
switch (container_kind)
{
case (ContainerKind::runtime):
return "runtime";
case (ContainerKind::initcode):
return "initcode";
case (ContainerKind::initcode_runtime):
return "initcode_runtime";
}
return "<unknown>";
}

} // namespace

void eof_validation::TearDown()
Expand Down Expand Up @@ -129,6 +144,7 @@ void eof_validation::export_eof_validation_test()

auto& jcase = jvectors[case_name];
jcase["code"] = hex0x(test_case.container);
jcase["kind"] = to_string(test_case.kind);

auto& jresults = jcase["results"][evmc::to_string(rev)];
if (test_case.error == EOFValidationError::success)
Expand Down

0 comments on commit 98b39ab

Please sign in to comment.