-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcpjudge.cpp
More file actions
89 lines (65 loc) · 2.05 KB
/
cpjudge.cpp
File metadata and controls
89 lines (65 loc) · 2.05 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
#include "color.h"
using namespace std;
string readFile(const string &filename) {
ifstream file(filename);
stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
bool compareOutput(const string &file1, const string &file2) {
string out1 = readFile(file1);
string out2 = readFile(file2);
return out1 == out2;
}
void runTest(int testNum, int &passed) {
string inputFile = "tests/input" + to_string(testNum) + ".txt";
string expectedFile = "tests/output" + to_string(testNum) + ".txt";
string outputFile = "temp_output.txt";
string runCmd = "./solution < " + inputFile + " > " + outputFile;
system(runCmd.c_str());
if (compareOutput(outputFile, expectedFile)) {
cout << "Test " << testNum << " "
<< GREEN << "PASSED" << RESET << endl;
passed++;
}
else {
cout << "Test " << testNum << " "
<< RED << "FAILED" << RESET << endl;
}
}
int main() {
enableColors();
cout << YELLOW << "===== CodeJudge =====" << RESET << endl;
string sourceFile = "solution.cpp";
cout << "Compiling solution..." << endl;
string compileCmd = "g++ " + sourceFile + " -o solution";
if (system(compileCmd.c_str()) != 0) {
cout << RED << "Compilation Failed" << RESET << endl;
return 1;
}
cout << GREEN << "Compilation Successful" << RESET << endl;
int totalTests = 3;
int passed = 0;
cout << "\nRunning Tests...\n" << endl;
for (int i = 1; i <= totalTests; i++) {
runTest(i, passed);
}
cout << "\n========================\n";
if (passed == totalTests) {
cout << GREEN << "All Tests Passed "
<< "(" << passed << "/" << totalTests << ")"
<< RESET << endl;
}
else {
cout << RED << "Tests Passed "
<< "(" << passed << "/" << totalTests << ")"
<< RESET << endl;
}
cout << "========================\n";
return 0;
}