-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (71 loc) · 2.78 KB
/
main.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
/**
* A main program for testing the "TestCase" tester.
*/
#include <iostream>
#include <sstream>
#include <string>
#include "TestCase.hpp"
using namespace std;
/**
* Below we define some functions with deliberate bugs,
* to demonstrate that our tester can find them.
*/
int sqr(int x) {
return x*x*x; // a deliberate bug (it should be: x*x)
}
int round(double x) {
return int(x); // a deliberate bug (it should be: int(x+0.5)).
}
/**
* Below we define a whole new struct with deliberate bugs,
* to demonstrate that our tester can find bugs even in new classes.
*/
struct MyStruct {
int num;
MyStruct(int num): num(num) {}
bool operator==(const MyStruct& other) {
return false; // a deliberate bug
}
bool operator!=(const MyStruct& other) {
return num!=other.num; // no bug
}
int myNum() const {
return num+2; // a deliberate bug
}
};
int getNum(const MyStruct& s) {
return s.num+1; // a deliberate bug
}
ostream& operator<< (ostream& out, const MyStruct& tc) {
return (out << "MyStrct"<<"("<<tc.num<<")"); // a deliberate typo (forgot "u").
}
int main() {
TestCase("Test int operators", cerr)
.check_equal(5,5) // check operator ==. Here there is no bug.
.check_different(5,6) // check operator !=. Here there is no bug.
.check_function(sqr, 1, 1) // check a function int->int. Here there is no bug.
.check_function(sqr, 5, 25) // check a function int->int. Here there is a bug.
.check_function(round, 5.3, 5) // check a function double->int. Here there is no bug.
.check_function(round, 5.8, 6) // check a function double->int. Here there is a bug.
.check_output(5, "5") // check output operator <<
.print();
TestCase("Test MyStruct operators", cerr)
.check_equal(MyStruct(5), MyStruct(5)) // Here there is a bug.
.check_different(MyStruct(5), MyStruct(6)) // Here there is no bug.
.check_output(MyStruct(5), "MyStruct(5)") // Here there is a bug.
.check_function(getNum, MyStruct(5), 5) // Here there is a bug.
.check_function([](const MyStruct& s){return s.myNum();}, MyStruct(5), 5) // Here there is a bug.
.print();
}
/* Expected output:
Test int operators: Failure in test #4: Function should return 25 but returned 125!
Test int operators: Failure in test #6: Function should return 6 but returned 5!
Test int operators: 2 failed, 5 passed, 7 total.
---
Test MyStruct operators: Failure in test #1: MyStrct(5) should equal MyStrct(5)!
Test MyStruct operators: Failure in test #3: string value should be MyStruct(5) but is MyStrct(5)
Test MyStruct operators: Failure in test #4: Function should return 5 but returned 6!
Test MyStruct operators: Failure in test #5: Function should return 5 but returned 7!
Test MyStruct operators: 4 failed, 1 passed, 5 total.
---
*/