This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathwbSolution.cpp
287 lines (256 loc) · 9.67 KB
/
wbSolution.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "wb.h"
char *solutionJSON = nullptr;
static string _solution_correctQ("");
static void _onUnsameImageFunction(string str) {
_solution_correctQ = str;
}
template <typename T>
static wbBool wbSolution_listCorrectQ(const char *expectedOutputFile,
wbSolution_t sol, const char *type) {
wbBool res;
T *expectedData;
int expectedRows, expectedColumns;
expectedData = (T *)wbImport(expectedOutputFile, &expectedRows,
&expectedColumns, type);
if (expectedData == nullptr) {
_solution_correctQ = "Failed to open expected output file.";
res = wbFalse;
} else if (expectedRows != wbSolution_getRows(sol)) {
wbLog(TRACE, "Number of rows in the solution is ",
wbSolution_getRows(sol), ". Expected number of rows is ",
expectedRows, ".");
_solution_correctQ =
"The number of rows in the solution did not match "
"that of the expected results.";
res = wbFalse;
} else if (expectedColumns != wbSolution_getColumns(sol)) {
wbLog(TRACE, "Number of columns in the solution is ",
wbSolution_getColumns(sol), ". Expected number of columns is ",
expectedColumns, ".");
_solution_correctQ = "The number of columns in the solution did not "
"match that of the expected results.";
res = wbFalse;
} else {
int ii, jj, idx;
T *solutionData;
solutionData = (T *)wbSolution_getData(sol);
if (wbSolution_getType(sol) == "integral_vector/sorted") {
wbSort(solutionData, expectedRows * expectedColumns);
}
for (ii = 0; ii < expectedRows; ii++) {
for (jj = 0; jj < expectedColumns; jj++) {
idx = ii * expectedColumns + jj;
if (wbUnequalQ(expectedData[idx], solutionData[idx])) {
string str;
if (expectedColumns == 1) {
str = wbString(
"The solution did not match the expected results at row ",
ii, ". Expecting ", expectedData[idx], " but got ",
solutionData[idx], ".");
} else {
str = wbString("The solution did not match the expected "
"results at column ",
jj, " and row ", ii, ". Expecting ",
expectedData[idx], " but got ",
solutionData[idx], ".");
}
_solution_correctQ = str;
res = wbFalse;
goto matrixCleanup;
}
}
}
res = wbTrue;
matrixCleanup:
if (expectedData != nullptr) {
wbFree(expectedData);
}
}
return res;
}
static wbBool wbSolution_correctQ(char *expectedOutputFile,
wbSolution_t sol) {
if (expectedOutputFile == nullptr) {
_solution_correctQ = "Failed to determined the expected output file.";
return wbFalse;
} else if (!wbFile_existsQ(expectedOutputFile)) {
_solution_correctQ =
wbString("The file ", expectedOutputFile, " does not exist.");
return wbFalse;
} else if (wbString_sameQ(wbSolution_getType(sol), "image")) {
wbBool res;
wbImage_t solutionImage = nullptr;
wbImage_t expectedImage = wbImport(expectedOutputFile);
if (expectedImage == nullptr) {
_solution_correctQ = "Failed to open expected output file.";
res = wbFalse;
} else if (wbImage_getWidth(expectedImage) !=
wbSolution_getWidth(sol)) {
_solution_correctQ =
"The image width of the expected image does not "
"match that of the solution.";
res = wbFalse;
} else if (wbImage_getHeight(expectedImage) !=
wbSolution_getHeight(sol)) {
_solution_correctQ =
"The image height of the expected image does not "
"match that of the solution.";
res = wbFalse;
} else if (wbImage_getChannels(expectedImage) !=
wbSolution_getChannels(sol)) {
_solution_correctQ =
"The image channels of the expected image does not "
"match that of the solution.";
res = wbFalse;
} else {
solutionImage = (wbImage_t)wbSolution_getData(sol);
wbAssert(solutionImage != nullptr);
res = wbImage_sameQ(solutionImage, expectedImage,
_onUnsameImageFunction);
}
if (expectedImage != nullptr) {
wbImage_delete(expectedImage);
}
return res;
} else if (wbString_sameQ(wbSolution_getType(sol), "histogram")) {
return wbSolution_listCorrectQ<unsigned char>(expectedOutputFile, sol,
"Integer");
} else if (wbString_sameQ(wbSolution_getType(sol), "integral_vector/sorted") ||
wbString_sameQ(wbSolution_getType(sol), "integral_vector")) {
return wbSolution_listCorrectQ<int>(expectedOutputFile, sol,
"Integer");
} else if (wbString_sameQ(wbSolution_getType(sol), "vector") ||
wbString_sameQ(wbSolution_getType(sol), "matrix")) {
return wbSolution_listCorrectQ<wbReal_t>(expectedOutputFile, sol,
"Real");
} else {
wbAssert(wbFalse);
return wbFalse;
}
}
wbBool wbSolution(char *expectedOutputFile, char *outputFile, char *type0,
void *data, int rows, int columns, int depth) {
char *type;
wbBool res;
wbSolution_t sol;
if (expectedOutputFile == nullptr || data == nullptr || type0 == nullptr) {
wbLog(ERROR, "Failed to grade solution");
return wbFalse;
}
type = wbString_toLower(type0);
if (_solution_correctQ != "") {
_solution_correctQ = "";
}
wbSolution_setOutputFile(sol, outputFile);
wbSolution_setId(sol, uuid());
wbSolution_setSessionId(sol, sessionId());
wbSolution_setType(sol, type);
wbSolution_setData(sol, data);
wbSolution_setRows(sol, rows);
wbSolution_setColumns(sol, columns);
wbSolution_setDepth(sol, depth);
res = wbSolution_correctQ(expectedOutputFile, sol);
if (outputFile != nullptr) {
if (wbString_sameQ(type, "image")) {
wbImage_t inputImage = (wbImage_t)data;
wbImage_t img = wbImage_new(wbImage_getWidth(inputImage),
wbImage_getHeight(inputImage),
wbImage_getChannels(inputImage));
memcpy(wbImage_getData(img), wbImage_getData(inputImage),
rows * columns * depth * sizeof(wbReal_t));
wbExport(outputFile, img);
wbImage_delete(img);
} else if (wbString_sameQ(type, "integral_vector/sort")) {
wbSort((int *)data, rows*columns);
wbExport(outputFile, (int *)data, rows, columns);
} else if (wbString_sameQ(type, "integral_vector")) {
wbExport(outputFile, (int *)data, rows, columns);
} else if (wbString_sameQ(type, "vector") ||
wbString_sameQ(type, "matrix")) {
wbExport(outputFile, (wbReal_t *)data, rows, columns);
} else if (wbString_sameQ(type, "histogram")) {
wbExport(outputFile, (unsigned char *)data, rows, columns);
} else if (wbString_sameQ(type, "text")) {
wbExport_text(outputFile, (unsigned char *)data, rows * columns);
}
}
wbFree(type);
return res;
}
wbBool wbSolution(char *expectedOutputFile, char *outputFile, char *type0,
void *data, int rows, int columns) {
return wbSolution(expectedOutputFile, outputFile, type0, data, rows,
columns, 1);
}
wbBool wbSolution(wbArg_t arg, void *data, int rows, int columns,
int depth) {
char *type;
wbBool res;
char *expectedOutputFile;
char *outputFile;
stringstream ss;
expectedOutputFile = wbArg_getExpectedOutputFile(arg);
outputFile = wbArg_getOutputFile(arg);
type = wbArg_getType(arg);
wbAssert(type != nullptr);
wbAssert(expectedOutputFile != nullptr);
wbAssert(outputFile != nullptr);
res = wbSolution(expectedOutputFile, outputFile, type, data, rows,
columns, depth);
if (WB_USE_JSON11) {
json11::Json json;
if (res) {
#ifndef JSON_OUTPUT
printf("The solution is correct\n");
#endif
json = json11::Json::object{{"correctq", true},
{"message", "The solution is correct"}};
} else {
#ifndef JSON_OUTPUT
printf("The solution is NOT correct\n");
#endif
json = json11::Json::object{{"correctq", false},
{"message", _solution_correctQ}};
}
#ifdef wbLogger_printOnLog
if (wbLogger_printOnLog) {
json11::Json e =
json11::Json::object{{"type", "solution"}, {"data", json}};
std::cout << e.dump() << std::endl;
}
#endif /* wbLogger_printOnLog */
solutionJSON = wbString_duplicate(json.string_value());
} else {
if (res) {
#ifndef JSON_OUTPUT
printf("Solution is correct\n");
#endif
ss << "{\n";
ss << wbString_quote("correctq") << ": true,\n";
ss << wbString_quote("message") << ": "
<< wbString_quote("Solution is correct.") << "\n";
ss << "}";
} else {
#ifndef JSON_OUTPUT
printf("Solution is NOT correct\n");
#endif
ss << "{\n";
ss << wbString_quote("correctq") << ": false,\n";
ss << wbString_quote("message") << ": "
<< wbString_quote(_solution_correctQ) << "\n";
ss << "}";
}
solutionJSON = wbString_duplicate(ss.str());
}
return res;
}
wbBool wbSolution(wbArg_t arg, void *data, int rows, int columns) {
return wbSolution(arg, data, rows, columns, 1);
}
EXTERN_C wbBool wbSolution(wbArg_t arg, void *data, int rows) {
return wbSolution(arg, data, rows, 1);
}
wbBool wbSolution(wbArg_t arg, wbImage_t img) {
return wbSolution(arg, img, wbImage_getHeight(img),
wbImage_getWidth(img), wbImage_getChannels(img));
}