-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
81 lines (63 loc) · 1.6 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
#include <yaml-cpp/yaml.h>
#include "iostream"
#include "main.h"
#include "map"
using namespace std;
struct test_case {
string in;
string out;
};
vector<test_case> load_cases() {
vector<test_case> cases;
try {
YAML::Node yaml_cases = YAML::LoadFile("./test.yaml");
for (size_t i = 0; i < yaml_cases.size(); i++) {
YAML::Node c = yaml_cases[i];
cases.push_back({c["in"].as<string>(), c["out"].as<string>()});
}
} catch (YAML::Exception &e) {
cerr << "YAML load error: " << e.what() << endl;
exit(1);
}
return cases;
}
int main() {
auto cases = load_cases();
int failed = 0;
map<string, function<void(void)>> function_map = {
{"solve", solve},
};
for (auto it = function_map.begin(); it != function_map.end(); it++) {
auto func_name = it->first;
cout << "----- " << func_name << " -----" << endl;
for (size_t i = 0; i < cases.size(); i++) {
auto c = cases[i];
stringbuf in = stringbuf(c.in);
streambuf *cinbuf = cin.rdbuf();
cin.rdbuf(&in);
stringbuf out;
streambuf *coutbuf = cout.rdbuf();
cout.rdbuf(&out);
try {
it->second();
} catch (exit_exception &e) {
}
cin.rdbuf(cinbuf);
cout.rdbuf(coutbuf);
if (c.out != out.str()) {
cout << "--- CASE " << i << " ---" << endl;
cout << "[EXP] \n"
<< "\"" << c.out << "\""
<< "\n[ACT] \n"
<< "\"" << out.str() << "\""
<< "\n"
<< endl;
failed = 1;
}
}
if (!failed) {
cout << " PASS" << endl;
}
}
return 0;
}