This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathvmtester.cpp
More file actions
144 lines (123 loc) · 4.06 KB
/
Copy pathvmtester.cpp
File metadata and controls
144 lines (123 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2018 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.
#include "vmtester.hpp"
#include <evmc/evmc.hpp>
#include <evmc/loader.h>
#include <iostream>
evmc::VM evmc_vm_test::owned_vm;
class cli_parser
{
public:
const char* const application_name = nullptr;
const char* const application_version = nullptr;
std::vector<std::string> arguments_names;
std::vector<std::string> arguments;
cli_parser(const char* app_name,
const char* app_version,
std::vector<std::string> args_names) noexcept
: application_name{app_name},
application_version{app_version},
arguments_names{std::move(args_names)}
{
arguments.reserve(this->arguments_names.size());
}
/// Parses the command line arguments.
///
/// It recognize --help and --version options and output for these is sent
/// to the @p out output stream.
/// Errors are sent to the @p err output stream.
///
/// @return Negative value in case of error,
/// 0 in case --help or --version was provided and the program should terminate,
/// positive value in case the program should continue.
int parse(int argc, char* argv[], std::ostream& out, std::ostream& err)
{
bool help = false;
bool version = false;
size_t num_args = 0;
for (int i = 1; i < argc; ++i)
{
auto arg = std::string{argv[i]};
auto x = arg.find_first_not_of('-');
if (x == 0) // Argument.
{
++num_args;
if (num_args > arguments_names.size())
{
err << "Unexpected argument \"" << arg << "\"\n";
return -1;
}
arguments.emplace_back(std::move(arg));
continue;
}
else if (x <= 2)
{
arg.erase(0, x);
if (arg == "version")
{
version = true;
continue;
}
if (arg == "help")
{
help = true;
continue;
}
}
err << "Unknown option \"" << argv[i] << "\"\n";
return -1;
}
out << application_name << " " << application_version << "\n";
if (help)
{
out << "Usage: " << argv[0];
for (const auto& name : arguments_names)
out << " " << name;
out << "\n";
return 0;
}
if (version)
return 0;
if (num_args < arguments_names.size())
{
for (auto i = num_args; i < arguments_names.size(); ++i)
err << "The " << arguments_names[i] << " argument is required.\n";
err << "Run with --help for more information.\n";
return -1;
}
return 1;
}
};
int main(int argc, char* argv[])
{
try
{
testing::InitGoogleTest(&argc, argv);
auto cli = cli_parser{"EVMC VM Tester", PROJECT_VERSION, {"MODULE"}};
const auto error_code = cli.parse(argc, argv, std::cout, std::cerr);
if (error_code <= 0)
return error_code;
const auto& evmc_module = cli.arguments[0];
std::cout << "Testing " << evmc_module << "\n";
evmc_loader_error_code ec = EVMC_LOADER_UNSPECIFIED_ERROR;
auto vm = evmc::VM{evmc_load_and_configure(evmc_module.c_str(), &ec)};
if (ec != EVMC_LOADER_SUCCESS)
{
const auto error = evmc_last_error_msg();
if (error != nullptr)
std::cerr << error << "\n";
else
std::cerr << "Loading error " << ec << "\n";
return static_cast<int>(ec);
}
evmc_vm_test::set_vm(std::move(vm));
std::cout << std::endl;
return RUN_ALL_TESTS();
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << "\n";
return -2;
}
}