-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtddFuncs.cpp
More file actions
executable file
·203 lines (159 loc) · 4.52 KB
/
tddFuncs.cpp
File metadata and controls
executable file
·203 lines (159 loc) · 4.52 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "tddFuncs.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <vector>
using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::ostringstream;
#include <json/json.h> // from jsoncpp library (https://github.com/open-source-parsers/jsoncpp)
#define FILENAME "results.json"
class GradescopeReporter {
private:
struct TestCase {
double score;
double max_score;
std::string name;
std::string output;
};
ifstream resultsIn;
ofstream resultsOut;
Json::Value root;
bool inGroup;
double groupMax;
const char* groupName;
std::vector<TestCase> testGroup;
public:
GradescopeReporter() : inGroup(false) {
resultsIn.open(FILENAME);
if (resultsIn.fail()) {
makeEmptyTestsArray();
} else {
resultsIn >> root; // read results.json into root
}
}
~GradescopeReporter() {
if (inGroup) {
std::cerr << "WARNING: MISSING END_GROUP for START_GROUP(" << groupMax << "," << groupName <<"); AUTOMATICALLY CALLED" << std::endl;
END_GROUP();
}
resultsOut.open(FILENAME);
resultsOut << root << endl;
resultsOut.close();
}
void makeEmptyTestsArray() {
Json::Value emptyArray;
emptyArray.append(Json::Value::null);
emptyArray.clear();
root["tests"] = emptyArray;
}
void addTestToJSON(int score,
int max_score,
std::string name,
std::string output) {
Json::Value newTest;
newTest["score"] = score;
newTest["max_score"] = max_score;
newTest["name"] = name;
newTest["output"] = output;
root["tests"].append(newTest);
}
void addTestToGroup(double score,
double max_score,
std::string name,
std::string output) {
TestCase tc = {score, max_score, name, output};
testGroup.push_back(tc);
}
void addTest(double score,
double max_score,
std::string name,
std::string output) {
if (inGroup) {
addTestToGroup(score, max_score, name, output);
} else {
addTestToJSON(score, max_score, name, output);
}
}
void startgroup(double points, const char* const name){
if (inGroup) {
std::cerr << "ERROR: NESTED GROUPS NOT ALLOWED" << std::endl;
exit(1);
}
inGroup = true;
groupMax = points;
groupName = name;
assert(testGroup.empty());
}
void endgroup(){
if (!inGroup) {
std::cerr << "ERROR: END_GROUP HAS NO MATCHING START_GROUP" << std::endl;
exit(1);
}
inGroup = false;
double totalGroupPossible = 0.0;
double totalGroupActual = 0.0;
std::string combinedMessage = "";
for (auto tc: testGroup) {
totalGroupActual += tc.score;
totalGroupPossible += tc.max_score;
combinedMessage += tc.output;
}
if(totalGroupPossible != 0.0){
double scaledActual = (totalGroupActual / totalGroupPossible ) * groupMax;
addTestToJSON(scaledActual, groupMax, groupName, combinedMessage);
}
testGroup.clear();
}
};
static GradescopeReporter gsr; // constructor executes before main, destructor after
void assertEquals(const char* expected,
const char* actual,
std::string message) {
/*std::ostringstream ea;
//ea << "Expected: " << expected << " Actual: " << actual;
if (strcmp(expected, actual)==0) {
ea << "PASSED: " << message << std::endl;
addTest(1,1,message, ea.str());
} else {
ea << " FAILED: " << message << std::endl
<< " " << "Expected: " << expected << " Actual: " << actual << std::endl;
addTest(0,1,message, ea.str());
}
std::cout << ea.str();*/
assertEquals(std::string(expected), std::string(actual), message);
}
void assertEquals(const char* expected,
std::string actual,
std::string message) {
assertEquals(std::string(expected), actual, message);
}
void addTest(double score,
double max_score,
std::string name,
std::string output){
gsr.addTest(score, max_score,name, output);
}
void assertTrue(bool expression,
std::string message) {
std::ostringstream ea;
if (expression) {
ea << "| PASSED: " << message << endl;
gsr.addTest(1,1,message,ea.str());
} else {
ea << "| FAILED: " << message << endl;
gsr.addTest(0,1,message,ea.str());
}
cout << ea.str();
}
void START_GROUP(double points, const char* const name){
gsr.startgroup(points, name);
}
void END_GROUP(){
gsr.endgroup();
}