-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwbSolution.cpp
191 lines (157 loc) · 6.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
#include <wb.h>
char * solutionJSON = NULL;
static string _solution_correctQ("");
static void _onUnsameImageFunction(string str) {
_solution_correctQ = str;
}
static wbBool wbSolution_correctQ(char * expectedOutputFile, wbSolution_t sol) {
wbBool res;
if (expectedOutputFile == NULL) {
_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")) {
wbImage_t solutionImage = NULL;
wbImage_t expectedImage = wbImport(expectedOutputFile);
if (expectedImage == NULL) {
_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 {
solutionImage = (wbImage_t) wbSolution_getData(sol);
wbAssert(solutionImage != NULL);
res = wbImage_sameQ(solutionImage, expectedImage, _onUnsameImageFunction);
}
if (expectedImage != NULL) {
wbImage_delete(expectedImage);
}
return res;
} else if (wbString_sameQ(wbSolution_getType(sol), "vector") ||
wbString_sameQ(wbSolution_getType(sol), "matrix")) {
wbReal_t * expectedData;
int expectedRows, expectedColumns;
expectedData = (wbReal_t *) wbImport(expectedOutputFile, &expectedRows, &expectedColumns);
if (expectedData == NULL) {
_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;
wbReal_t * solutionData;
solutionData = (wbReal_t *) wbSolution_getData(sol);
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 != NULL) {
wbFree(expectedData);
}
return res;
} else {
wbAssert(wbFalse);
return wbFalse;
}
}
wbBool wbSolution(char * expectedOutputFile,
char * outputFile, char * type0,
void * data, int rows, int columns) {
char * type;
wbBool res;
wbSolution_t sol;
if (expectedOutputFile == NULL ||
//outputFile == NULL ||
data == NULL ||
type0 == NULL) {
wbLog(ERROR, "Failed to grade solution");
return wbFalse;
}
type = wbString_toLower(type0);
if (_solution_correctQ != "") {
_solution_correctQ = "";
}
wbSolution_setOutputFile(sol, outputFile);
wbSolution_setType(sol, type);
wbSolution_setData(sol, data);
wbSolution_setRows(sol, rows);
wbSolution_setColumns(sol, columns);
res = wbSolution_correctQ(expectedOutputFile, sol);
if (wbString_sameQ(type, "image")) {
wbImage_t img = wbImage_new(rows, columns);
memcpy(wbImage_getData(img), data, rows*columns*wbImage_channels*sizeof(wbReal_t));
wbExport(outputFile, img);
wbImage_delete(img);
} else if (wbString_sameQ(type, "vector") ||
wbString_sameQ(type, "matrix")) {
wbExport(outputFile, (wbReal_t *) data, rows, columns);
}
wbFree(type);
return res;
}
wbBool wbSolution(wbArg_t arg, void * data, int rows, int columns) {
char * type;
wbBool res;
char * expectedOutputFile;
char * outputFile;
stringstream ss;
expectedOutputFile = wbArg_getExpectedOutputFile(arg);
outputFile = wbArg_getOutputFile(arg);
type = wbArg_getType(arg);
wbAssert(type != NULL);
wbAssert(expectedOutputFile != NULL);
wbAssert(outputFile != NULL);
res = wbSolution(expectedOutputFile, outputFile, type, data, rows, columns);
if (res) {
ss << "{\n";
ss << wbString_quote("correctq") << ": true,\n";
ss << wbString_quote("message") << ": " << wbString_quote("Solution is correct.") << "\n";
ss << "}";
} else {
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) {
return wbSolution(arg, data, rows, 1);
}
wbBool wbSolution(wbArg_t arg, wbImage_t img) {
return wbSolution(arg, img, wbImage_getHeight(img), wbImage_getWidth(img));
}