-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.h
76 lines (63 loc) · 1.41 KB
/
testing.h
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
//just create cases.txt in the project and add test cases in such format
//
//input
//....
//output
//....
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <sstream>
using namespace std;
class Task
{
public:
virtual void solve() = 0;
};
void test(Task &t, string testCasesFile = "cases.txt")
{
ifstream cases(testCasesFile);
if (!cases.is_open()) throw;
bool firstLine = true;
int testCase = 1;
while (!cases.eof())
{
string inputLine;
stringstream input(""), outputE(""), outputA("");
if (firstLine)
{
getline(cases, inputLine);
assert(inputLine == "input");
firstLine = false;
}
while (getline(cases, inputLine) && inputLine != "output")
{
input.write(inputLine.c_str(), inputLine.size());
input.write("\n", 1);
}
while (getline(cases, inputLine) && inputLine != "input")
{
outputE.write(inputLine.c_str(), inputLine.size());
outputE.write("\n", 1);
if (cases.eof()) break;
}
auto cin_old = cin.rdbuf(input.rdbuf());
auto cout_old = cout.rdbuf(outputA.rdbuf());
t.solve();
cin.rdbuf(cin_old);
cout.rdbuf(cout_old);
cout << "Test case " << testCase << ":\t\t";
outputA.write("\n", 1);
if (outputA.str() == outputE.str())
cout << "Accepted\n\n";
else
{
cout << "Failed\n";
cout << "Expected:\t" << outputE.str() << "\n";
cout << "Actuall:\t" << outputA.str() << "\n\n";
}
++testCase;
}
}