-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestCase.hpp
More file actions
105 lines (99 loc) · 3.49 KB
/
TestCase.hpp
File metadata and controls
105 lines (99 loc) · 3.49 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
#pragma once
/**
*header file of class TestCase
*Authors Alexey Titov and Shir Bentabou
*Version 1.0
**/
//libraries
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <functional>
using namespace std;
class TestCase{
private:
string test;
ostream &out;
int successes;
int failures;
int test_count;
public:
//Constructor for TestCase class
TestCase(string test_name, ostream &os);
//Function that compares between two objects.
template <typename T1, typename T2>
TestCase& check_equal(T1 a, T2 b){
this->test_count++;
if (a==b){
this->successes++;
return *this;
}
this->failures++;
ostringstream stream1;
stream1 << a;
ostringstream stream2;
stream2 << b;
string ans = this->test+": Failure in test #"+to_string(this->test_count)+": "+stream1.str()+
" should equal "+stream2.str()+"!";
out<<ans<<endl;
return *this;
}
//Function that checks if two objects are different.
template <typename T>
TestCase& check_different(T a, T b){
test_count++;
if ((T)a != (T)b){
successes++;
return *this;
}
failures++;
ostringstream stream1;
stream1 << a;
ostringstream stream2;
stream2 << b;
string ans = test+": Failure in test #"+to_string(this->test_count)+": "+stream1.str()+
" shouldn't equal "+stream2.str()+"!";
out<<ans<<endl;
return *this;
}
//Function that compares the ostream compared to a given string.
template <typename T>
TestCase& check_output(T a, string s){
test_count++;
ostringstream stream;
stream << a;
string str = stream.str();
if (str.compare(s)==0){
successes++;
return *this;
}
failures++;
string ans = test+": Failure in test #"+to_string(test_count)+": string value should be " + s +
" but is "+ str;
out<<ans<<endl;
return *this;
}
//Function that receives a function, and two variables, and compares the between the answer received from the function
//and the answer already known.
template <typename Func,typename Rec, typename Ans>
TestCase& check_function(Func function_name, Rec received, Ans answered){
test_count++;
Ans a = function_name(received);
if (a==answered){
successes++;
return *this;
}
failures++;
ostringstream stream1;
stream1 << a; //what the function returns
ostringstream stream2;
stream2 << answered; //the real answer we are supposed to received
string str = test+": Failure in test #"+to_string(test_count)+": Function should return "+stream2.str()+
" but returned "+stream1.str()+"!";
out<<str<<endl;
return *this;
}
//Function that prints num. of successes and num. of failures in testcase.
TestCase& print();
};