-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.cpp
93 lines (84 loc) · 1.77 KB
/
suite.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
// suite.cpp
#include "Suite.h"
#include <iostream>
#include <stdexcept>
#include <cassert>
using namespace std;
class TestSuiteError : public logic_error {
public:
TestSuiteError(const string& s = "")
: logic_error(s) {}
};
void Suite::addTest(Test* t)
throw(TestSuiteError) {
// Make sure test has a stream:
if (t == 0)
throw TestSuiteError(
"Null test in Suite::addTest"); else if (osptr != 0 && t->getStream() == 0)
t->setStream(osptr);
tests.push_back(t);
t->reset();
}
void Suite::addSuite(const Suite& s)
throw(TestSuiteError) {
for (size_t i = 0; i < s.tests.size(); ++i)
addTest(s.tests[i]);
}
void Suite::free() {
// This is not a destructor because tests
// don't have to be on the heap.
for (size_t i = 0; i < tests.size(); ++i) {
delete tests[i];
tests[i] = 0;
}
}
void Suite::run() {
reset();
for (size_t i = 0; i < tests.size(); ++i) {
assert(tests[i]);
tests[i]->run();
}
}
long Suite::report() const {
if (osptr) {
long totFail = 0;
*osptr << "Suite \"" << name
<< "\"\n=======";
size_t i;
for (i = 0; i < name.size(); ++i)
*osptr << '=';
*osptr << "=\n";
for (i = 0; i < tests.size(); ++i) {
assert(tests[i]);
totFail += tests[i]->report();
}
*osptr << "=======";
for (i = 0; i < name.size(); ++i)
*osptr << '=';
*osptr << "=\n";
return totFail;
}
else
return getNumFailed();
}
long Suite::getNumPassed() const {
long totPass = 0;
for (size_t i = 0; i < tests.size(); ++i) {
assert(tests[i]);
totPass += tests[i]->getNumPassed();
}
return totPass;
}
long Suite::getNumFailed() const {
long totFail = 0;
for (size_t i = 0; i < tests.size(); ++i) {
assert(tests[i]); totFail += tests[i]->getNumFailed();
}
return totFail;
}
void Suite::reset() {
for (size_t i = 0; i < tests.size(); ++i) {
assert(tests[i]);
tests[i]->reset();
}
}