-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
173 lines (139 loc) · 4.31 KB
/
test.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <vector>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <utility>
#include <map>
#include <string>
using std::string, std::cout, std::cerr, std::endl;
const string FILE_EXTENSION = ".wis";
inline bool is_file_exists(const std::string &name) {
std::ifstream f(name.c_str());
return f.good();
}
void execute_command(bool silent_mode, const string &command)
{
if (!silent_mode) cout << "[CMD] " << command << endl;
int exit_code = system(command.c_str());
if (exit_code != 0)
{
cerr << "[ERROR] Executing command crashed with " << std::to_string(exit_code) << " exit code" << endl;
exit(exit_code);
}
}
string shift_vector(std::vector<string> &vec)
{
if (!vec.empty()) {
string result = vec[0];
vec.erase(vec.begin());
return result;
}
cerr << "[ERROR] Can't shift empty vector" << endl;
exit(1);
}
int test_file(const string &file_path) {
string file_name = file_path.substr(0, file_path.find_last_of('.'));
string output_path = file_name + ".output";
if (!is_file_exists(output_path))
{
cout << "[INFO] Skipping test for file: " << file_path << ". No recorded output found" << endl;
return 1;
}
string command = "./wis -quiet " + file_path + " && ./" + file_name;
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
cerr << "ERROR: Command execution failed: " << command << endl;
return -1;
}
string output;
char buffer[128];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) output += buffer;
pclose(pipe);
std::ifstream outputFile(output_path);
string expected_output((std::istreambuf_iterator<char>(outputFile)), std::istreambuf_iterator<char>());
if (output == expected_output) {
cout << "[INFO] Test passed for file: " << file_path << endl;
return 0;
} else {
cerr << "[ERROR] Test failed for file: " << file_path << endl;
cerr << " Expected output:\n" << expected_output << "\n Actual output:\n" << output << endl;
return -1;
}
}
void run_tests(std::vector<string> args, std::vector<string> paths)
{
int i = 0;
while (i < args.size())
{
string arg = shift_vector(args);
if (arg == "-f")
{
if (args.empty())
{
cerr << "[ERROR] path to directory not found after `-f` flag" << endl;
exit(1);
}
paths.push_back(args[0]);
}
}
int failed = 0;
int passed = 0;
int skipped = 0;
for (const auto &path : paths)
{
for (const auto &entry : std::filesystem::directory_iterator(path)) {
if (entry.path().extension() == FILE_EXTENSION) {
int result = test_file(entry.path().string());
if (result == -1) failed++;
else if (result == 1) skipped++;
else passed++;
}
}
}
cout << endl << "Testing report:" << endl;
cout << " Tests passed: " << passed << ", Tests skipped: " << skipped << ", Tests failed: " << failed << endl;
for (auto const &path: paths)
{
execute_command(true, "rm " + path + "/*.asm");
execute_command(true, "rm " + path + "/*.o");
}
if (failed > 0) exit(1);
}
void record_test_output(string const &file_path)
{
string file_name = file_path.substr(0, file_path.length() - FILE_EXTENSION.length());
execute_command(false, "./wis " + file_path);
execute_command(false, "./" + file_name + " > " + file_name + ".output");
}
int main(int argc, char* argv[])
{
std::vector<string> args(argv, argv + argc);
std::vector<string> paths = {"./tests/"};
string program = shift_vector(args);
if (args.empty())
{
cerr << "[ERROR] no subcommand provided" << endl;
exit(1);
}
string subcommand = shift_vector(args);
if (subcommand == "run")
{
run_tests(args, paths);
}
else if (subcommand == "record")
{
if (args.empty())
{
cerr << "[ERROR] not enough arguments for `record` subcommand" << endl;
exit(1);
}
record_test_output(shift_vector(args));
}
else
{
cerr << "[ERROR] unknown subcommand provided" << endl;
exit(1);
}
return 0;
}