-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.template
More file actions
146 lines (131 loc) · 4.54 KB
/
run.template
File metadata and controls
146 lines (131 loc) · 4.54 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
145
146
#include "%TaskFile%"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include <ctime>
#define IN_TEXT "jhelperin.txt"
#define OUT_TEXT "jhelperout.txt"
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class Modifier {
Code code;
public:
Modifier(Code pCode) : code(pCode) {}
friend std::ostream&
operator<<(std::ostream& os, const Modifier& mod) {
return os << "";
return os << "\033[" << mod.code << "m";
}
};
}
Color::Modifier red(Color::FG_RED);
Color::Modifier green(Color::FG_GREEN);
Color::Modifier yellow(Color::FG_YELLOW);
Color::Modifier blue(Color::FG_BLUE);
Color::Modifier def(Color::FG_DEFAULT);
namespace jhelper {
struct Test {
std::string input;
std::string output;
bool active;
bool has_output;
};
bool check(std::string expected, std::string actual) {
while (!expected.empty() && isspace(*--expected.end()))
expected.erase(--expected.end());
while (!actual.empty() && isspace(*--actual.end()))
actual.erase(--actual.end());
return expected == actual;
}
} // namespace jhelper
int main() {
std::vector<jhelper::Test> tests = {
%Tests%
};
bool allOK = true;
int testID = 0;
std::cout << std::fixed;
double maxTime = 0.0;
int test_cnt = (int) tests.size();
int test_passed = 0;
for (const jhelper::Test &test: tests) {
std::cout << blue << "Test #" << ++testID << def << std::endl;
if (!test.active) {
std::cout << yellow << "SKIPPED\n" << def;
std::cout << std::endl;
continue;
}
std::cout << blue << "Input: \n" << def << test.input << std::endl;
if (test.has_output) {
std::cout << blue << "Expected output: \n" << def << test.output << std::endl;
} else {
std::cout << blue << "Expected output: \n" << def << yellow << "N/A" << def << std::endl;
}
{
std::ofstream inw(IN_TEXT);
inw << test.input;
inw.close();
}
std::streambuf *cinbuf = cin.rdbuf();// save cin buffer
std::ifstream inw(IN_TEXT);
std::cin.rdbuf(inw.rdbuf()); //redirect std::cin to in.txt!
std::streambuf *coutbuf = std::cout.rdbuf(); //save cout buf
std::ofstream fout(OUT_TEXT); // open the output file
std::cout.rdbuf(fout.rdbuf()); //redirect std::cout to fout.txt
std::clock_t start = std::clock();
%ClassName% solver;
solver.solve();
std::clock_t finish = std::clock();
std::cin.rdbuf(cinbuf);//reset to standard input again
std::cout.rdbuf(coutbuf); //reset to standard output again
fout.close();
inw.close();
double currentTime = double(finish - start) / CLOCKS_PER_SEC;
maxTime = std::max(currentTime, maxTime);
std::stringstream output;
{
std::ifstream t(OUT_TEXT);
output << t.rdbuf();
t.close();
}
std::cout << blue << "Actual output: \n" << def << output.str() << std::endl;
if (test.has_output) {
bool result = jhelper::check(test.output, output.str());
allOK = allOK && result;
test_passed += result;
if (result) {
std::cout << blue << "Result: " << green << " OK " << def << std::endl;
} else {
std::cout << blue << "Result: " << red << "Wrong answer" << def << std::endl;
}
} else {
std::cout << red << " NO OUTPUT" << std::endl;
}
std::cout << blue << "Time: " << int(currentTime) << "." << (int(currentTime * 10) % 10)
<< (int(currentTime * 100) % 10) << (int(currentTime * 1000) % 10) << "s" << def << std::endl;
std::cout << std::endl;
}
std::cout << (test_passed == test_cnt ? green : red) << test_passed << " / " << test_cnt << " passed" << def
<< std::endl;
if (allOK) {
std::cout << green << "All OK ✅" << def << std::endl;
} else {
std::cout << red << "Some cases failed" << def << std::endl;
}
std::cout << blue << "Maximal time: " << int(maxTime) << "." << (int(maxTime * 10) % 10)
<< (int(maxTime * 100) % 10) << (int(maxTime * 1000) % 10) << "s" << def << std::endl;
return 0;
}