-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.cpp
195 lines (182 loc) · 7.87 KB
/
score.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <cstdlib>
#include <unistd.h>
using namespace std;
const string traceFolder = "Test/";
const string defaultStudentBasic = "./testcode";
const string defaultStanderBasic = "./Basic-Demo-64bit";
const int traceCount = 100;
const string traces[traceCount] = {
"trace00.txt", "trace01.txt", "trace02.txt", "trace03.txt", "trace04.txt", "trace05.txt", "trace06.txt",
"trace07.txt", "trace08.txt", "trace09.txt",
"trace10.txt", "trace11.txt", "trace12.txt", "trace13.txt", "trace14.txt", "trace15.txt", "trace16.txt",
"trace17.txt", "trace18.txt", "trace19.txt",
"trace20.txt", "trace21.txt", "trace22.txt", "trace23.txt", "trace24.txt", "trace25.txt", "trace26.txt",
"trace27.txt", "trace28.txt", "trace29.txt",
"trace30.txt", "trace31.txt", "trace32.txt", "trace33.txt", "trace34.txt", "trace35.txt", "trace36.txt",
"trace37.txt", "trace38.txt", "trace39.txt",
"trace40.txt", "trace41.txt", "trace42.txt", "trace43.txt", "trace44.txt", "trace45.txt", "trace46.txt",
"trace47.txt", "trace48.txt", "trace49.txt",
"trace50.txt", "trace51.txt", "trace52.txt", "trace53.txt", "trace54.txt", "trace55.txt", "trace56.txt",
"trace57.txt", "trace58.txt", "trace59.txt",
"trace60.txt", "trace61.txt", "trace62.txt", "trace63.txt", "trace64.txt", "trace65.txt", "trace66.txt",
"trace67.txt", "trace68.txt", "trace69.txt",
"trace70.txt", "trace71.txt", "trace72.txt", "trace73.txt", "trace74.txt", "trace75.txt", "trace76.txt",
"trace77.txt", "trace78.txt", "trace79.txt",
"trace80.txt", "trace81.txt", "trace82.txt", "trace83.txt", "trace84.txt", "trace85.txt", "trace86.txt",
"trace87.txt", "trace88.txt", "trace89.txt",
"trace90.txt", "trace91.txt", "trace92.txt", "trace93.txt", "trace94.txt", "trace95.txt", "trace96.txt",
"trace97.txt", "trace98.txt", "trace99.txt",
};
string studentBasic = "";
string standerBasic = "";
string traceFile = "";
int runTraces = traceCount, currentTrace = 0;
bool silent = false, firstFail = false, hideError = false, useColor = true;
int correct = 0, wrong = 0, total = 0;
void usage(const char *progname) {
cout
<< progname << " [-h] [-e <your_exec>] [-s <stander_exec>] [-t <trace_file>] [-f] [-m] [-q]" << endl
<< " -h Show this message and quit" << endl
<< " -e Specify your executable file, default value: " << defaultStudentBasic << endl
<< " -s Specify demo executable file, default value: " << defaultStanderBasic << endl
<< " -t Run specified trace file" << endl
<< " -f Stop at first failed test" << endl
<< " -m Hide error message" << endl
<< " -q Show final score only, cannot use with -t or -f, include -m" << endl;
exit(1);
}
string color(string ce) {
if (useColor) return ce;
return "";
}
void parseArguments(int argc, char **argv) {
int c;
opterr = 0;
while ((c = getopt(argc, argv, "e:s:t:fmqch")) != -1) {
switch (c) {
case 'e':
if (studentBasic.size()) usage(argv[0]);
studentBasic = optarg;
break;
case 's':
if (standerBasic.size()) usage(argv[0]);
standerBasic = optarg;
break;
case 't':
if (traceFile.size()) usage(argv[0]);
traceFile = optarg;
break;
case 'f':
if (firstFail) usage(argv[0]);
firstFail = true;
break;
case 'm':
if (hideError) usage(argv[0]);
hideError = true;
break;
case 'q':
if (silent) usage(argv[0]);
silent = true;
break;
case 'h':
usage(argv[0]);
break;
default:
usage(argv[0]);
break;
}
}
if (silent && (traceFile.size() || firstFail)) usage(argv[0]);
if (silent) hideError = true;
if (studentBasic.size() == 0) studentBasic = defaultStudentBasic;
if (standerBasic.size() == 0) standerBasic = defaultStanderBasic;
}
void clearTempFiles() {
int r = system("rm test_ans test_out -f");
(void) r;
}
int testTrace(const char *trace) {
clearTempFiles();
if (system((string() + "cat " + trace + " | timeout 1 " + standerBasic + " > test_ans 2> /dev/null").c_str()) !=
0)
return 1;
if (system((string() + "cat " + trace + " | timeout 1 " + studentBasic + " > test_out 2> /dev/null").c_str()) !=
0)
return 2;
if (system("diff test_ans test_out > /dev/null 2> /dev/null")) return 4;
if (system(
(string() + "cat " + trace + " | timeout 5 valgrind --error-exitcode=2 --leak-check=full " + studentBasic +
" > /dev/null 2> /dev/null").c_str()) != 0)
return 3;
clearTempFiles();
return 0;
}
void runTest(const string currentTrace) {
if (!silent) cout << "Trace \"" << currentTrace << "\" ... ";
cout.flush();
int error = testTrace(currentTrace.c_str());
total++;
if (!error) {
if (!silent) cout << color("\x1b[32;1m") << "Pass" << color("\x1b[0m") << endl;
correct++;
} else {
wrong++;
if (!silent) {
cout << color("\x1b[31;1m") << "Fail" << color("\x1b[0m") << endl;
if (!hideError) {
cout << "Trace file: " << endl << color("\x1b[35m");
cout.flush();
int r0 = system((string() + "cat " + currentTrace).c_str());
(void) r0;
cout << color("\x1b[0m") << endl;
if (error == 1)
cout << color("\x1b[31m") << "Error occurred while running demo program" << color("\x1b[0m")
<< endl;
if (error == 2)
cout << color("\x1b[31m") << "Error occurred while running your program" << color("\x1b[0m")
<< endl;
if (error == 3) cout << color("\x1b[31m") << "Memory leak" << color("\x1b[0m") << endl;
if (error == 4) {
cout << "Demo output: " << endl << color("\x1b[36m");
cout.flush();
int r1 = system("cat test_ans");
(void) r1;
cout << color("\x1b[0m") << endl;
cout << "Your output: " << endl << color("\x1b[33m");
cout.flush();
int r2 = system("cat test_out");
(void) r2;
cout << color("\x1b[0m") << endl;
}
}
}
clearTempFiles();
if (firstFail) throw exception();
}
}
void showScore() {
int score = correct / 5 * 5;
if (!silent)
cout << correct << " / " << total << " trace(s) passed." << endl;
if (total != traceCount) return;
cout << "Final Score: " << (score / 10) << "." << (score % 10) << endl;
}
int main(int argc, char **argv) {
parseArguments(argc, argv);
try {
cout << "Compiling code ..." << endl;
/**************************************************************
if you modify the structure of the files, you should modify the file paths here.
**************************************************************/
system("g++ -o testcode Basic/Basic.cpp Basic/evalstate.cpp Basic/exp.cpp Basic/parser.cpp Basic/program.cpp Basic/statement.cpp Basic/Utils/error.cpp Basic/Utils/error.hpp Basic/Utils/tokenScanner.cpp Basic/Utils/tokenScanner.hpp Basic/Utils/strlib.cpp");
if (traceFile.size()) runTest(traceFile);
else {
int i = 0;
for (; i < traceCount; i++) runTest(traceFolder + traces[i]);
}
} catch (...) {}
system("rm testcode -f");
showScore();
return 0;
}