forked from ivogeorg/ucd-csci2312-pa4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorContext.cpp
121 lines (99 loc) · 2.65 KB
/
ErrorContext.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Created by Ivo Georgiev on 11/22/15.
//
//
// Created by Ivo Georgiev on 11/13/15.
//
#include "ErrorContext.h"
/**
* Acknowledgement: Donnie Pinkston, CALTECH
*/
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <set>
#include "ErrorContext.h"
using std::string;
using std::ios;
using std::endl;
namespace Testing {
ErrorContext::ErrorContext(ostream &os) :
os(os),
passed(0),
total(0),
lastline(0),
skip(false) {
os << "line: ";
os.width(65);
os.setf(ios::left, ios::adjustfield);
os << "description" << " result" << endl;
os.width(78);
os.fill('~');
os << "~" << endl;
os.fill(' ');
os.setf(ios::right, ios::adjustfield);
}
void ErrorContext::desc(const char *msg, int line) {
if (lastline != 0 || (*msg == '-' && skip)) {
os << endl;
}
os.width(4);
os << line << ": ";
os.width(65);
os.setf(ios::left, ios::adjustfield);
os << msg << " ";
os.setf(ios::right, ios::adjustfield);
os.flush();
lastline = line;
skip = true;
}
void ErrorContext::desc(string msg, int line) {
if ((lastline != 0) || ((msg[0] == '-') && skip)) {
os << endl;
}
os.width(4);
os << line << ": ";
os.width(65);
os.setf(ios::left, ios::adjustfield);
os << msg << " ";
os.setf(ios::right, ios::adjustfield);
os.flush();
lastline = line;
skip = true;
}
#define DESC(x) desc(x, __LINE__) // ugly hack
void ErrorContext::result(bool good) {
if (good) {
os << "ok";
passed++;
}
else {
os << "ERROR";
badlines.insert(lastline);
}
os << endl;
total++;
lastline = 0;
}
ErrorContext::~ErrorContext() {
os << endl << "Passed " << passed << "/" << total << " tests." << endl
<< endl;
if (badlines.size() > 0) {
os << "For more information, please consult:" << endl;
for (set<int>::const_iterator it = badlines.begin();
it != badlines.end(); it++) {
os << " " << __FILE__ << ", line " << *it << endl;
}
os << endl;
if (badlines.size() > 2) {
os << "We recommend that you "
<< "fix the topmost failure before going on."
<< endl << endl;
}
}
}
bool ErrorContext::ok() const {
return passed == total;
}
}