static wbBool wbSolution_correctQ(char *expectedOutputFile, wbSolution_t sol) { 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")) { wbBool res; 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 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 != NULL); res = wbImage_sameQ(solutionImage, expectedImage, _onUnsameImageFunction); } if (expectedImage != NULL) { 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")) { 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(wbArg_t arg, void * data, int rows, int columns) { int ii; char * type; wbBool res; json_t * msg; char * expectedOutputFile; char * outputFile; json_t * inputFileArray; 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 1 if (res) { _solution_correctQ = json_object(); msg = json_string("Solution is correct."); json_object_set(_solution_correctQ, "CorrectQ", json_true()); json_object_set(_solution_correctQ, "Message", msg); } else { msg = _solution_correctQ; _solution_correctQ = json_object(); json_object_set(_solution_correctQ, "CorrectQ", json_false()); json_object_set(_solution_correctQ, "Message", msg); } #else if (res) { _solution_correctQ = json_true(); } else { _solution_correctQ = json_false(); } #endif inputFileArray = json_array(); for (ii = 0; ii < wbArg_getInputCount(arg); ii++) { char * file = wbArg_getInputFile(arg, ii); if (file != NULL) { json_array_append(inputFileArray, json_string(file)); } } /* json_object_set(_solution_correctQ, "InputFiles", inputFileArray); json_object_set(_solution_correctQ, "ExpectedOutput", json_string(expectedOutputFile)); json_object_set(_solution_correctQ, "OutputFile", json_string(outputFile)); */ return res; }
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 != NULL); wbAssert(expectedOutputFile != NULL); wbAssert(outputFile != NULL); res = wbSolution(expectedOutputFile, outputFile, type, data, rows, columns, depth); if (WB_USE_JSON11) { json11::Json json; if (res) { json = json11::Json::object{{"correctq", true}, {"message", "The solution is correct"}}; } else { 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) { 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; }
static inline void wbExport_writeAsImage(wbExport_t exprt, wbImage_t img) { wbAssert(wbExport_getKind(exprt) == wbExportKind_ppm); wbPPM_export(wbExport_getFile(exprt), img); return; }
static void * wbImport(const char * file, int * resRows, int * resColumns, const char * type) { void * data, * res; wbImport_t imp; size_t sz; int columns = 0, rows = 0; wbImportKind_t kind; if (file == NULL) { fprintf(stderr, "Failed to import file.\n"); wbExit(); } kind = _parseImportExtension(file); wbAssert(kind != wbImportKind_unknown); imp = wbImport_open(file, kind); if (wbString_sameQ(type, "Real")) { data = wbImport_readAsReal(imp); sz = sizeof(wbReal_t); } else { data = wbImport_readAsInteger(imp); sz = sizeof(int); } if (kind == wbImportKind_csv || kind == wbImportKind_tsv) { rows = wbImportCSV_getRowCount(wbImport_getCSV(imp)); columns = wbImportCSV_getColumnCount(wbImport_getCSV(imp)); } else if (kind == wbImportKind_raw) { rows = wbImportRaw_getRowCount(wbImport_getRaw(imp)); columns = wbImportRaw_getColumnCount(wbImport_getRaw(imp)); } if (resRows != NULL) { *resRows = rows; } if (resColumns != NULL) { *resColumns = columns; } res = wbMalloc(sz * rows * columns); memcpy(res, data, sz * rows * columns); wbImport_close(imp); return res; }
wbFile_t wbFile_new(void) { int idx = wbFile_nextIndex(); wbFile_t file = wbNew(struct st_wbFile_t); wbAssert(idx >= 0); wbFile_setIndex(file, idx); wbFile_setFileName(file, NULL); wbFile_setMode(file, NULL); wbFile_setFileHandle(file, NULL); wbFile_setData(file, NULL); wbFile_handles[idx] = file; return file; }
void wbExport(const char *file, wbImage_t img) { wbExportKind_t kind; wbExport_t exprt; if (file == NULL) { return; } kind = _parseExportExtension(file); exprt = wbExport_open(file, kind); wbAssert(kind == wbExportKind_ppm); wbExport_writeAsImage(exprt, img); wbExport_close(exprt); }
static inline wbImportCSV_t wbImportCSV_findDimensions(wbImportCSV_t csv, int * resRows, int * resColumns) { int rows = 0, columns = -1; char * line; wbFile_t file; char seperator[2]; if (csv == NULL) { return NULL; } if (wbImportCSV_getSeperator(csv) == '\0') { seperator[0] = ','; } else { seperator[0] = wbImportCSV_getSeperator(csv); } seperator[1] = '\0'; file = wbImportCSV_getFile(csv); while ((line = wbFile_readLine(file)) != NULL) { int currColumn = 0; char * token = strtok(line, seperator); while (token != NULL) { token = strtok(NULL, seperator); currColumn++; } rows++; if (columns == -1) { columns = currColumn; } if (columns != currColumn) { wbLog(ERROR, "The csv file is not rectangular."); } wbAssert(columns == currColumn); } wbFile_rewind(file); *resRows = rows; *resColumns = columns; return csv; }
wbImage_t wbImport(const char * file) { wbImage_t img; wbImport_t imp; wbImportKind_t kind; if (file == NULL) { fprintf(stderr, "Failed to import file.\n"); wbExit(); } kind = _parseImportExtension(file); wbAssert(kind == wbImportKind_ppm); imp = wbImport_open(file, kind); img = wbImport_getImage(imp); wbImport_close(imp); return img; }
void wbFile_rewind(wbFile_t file) { if (file == NULL) { return; } if (wbFile_getData(file) == NULL) { FILE *handle; handle = wbFile_getFileHandle(file); wbAssert(handle != NULL); rewind(handle); } #ifndef LAZY_FILE_LOAD else { wbFile_setDataOffset(file, 0); } #endif return; }
void wbFile_delete(wbFile_t file) { if (file != NULL) { int idx = wbFile_getIndex(file); if (wbFile_getFileName(file) != NULL) { wbDelete(wbFile_getFileName(file)); } if (wbFile_getMode(file) != NULL) { wbDelete(wbFile_getMode(file)); } if (wbFile_getFileHandle(file) != NULL) { fclose(wbFile_getFileHandle(file)); } if (idx >= 0) { wbAssert(wbFile_handles[idx] == file); wbFile_handles[idx] = NULL; } if (wbFile_getData(file) != NULL) { wbDelete(wbFile_getData(file)); } wbDelete(file); } }
static wbBool wbSolution_correctQ(char * expectedOutputFile, wbSolution_t sol) { wbBool res; if (expectedOutputFile == NULL) { _solution_correctQ = json_string("Failed to determined the expected output file."); return wbFalse; } else if (!wbFile_existsQ(expectedOutputFile)) { string str = wbString("The file ", expectedOutputFile, " does not exist."); _solution_correctQ = json_string(str.c_str()); return wbFalse; } else if (wbString_sameQ(wbSolution_getType(sol), "image")) { wbImage_t solutionImage = NULL; wbImage_t expectedImage = wbImport(expectedOutputFile); if (expectedImage == NULL) { _solution_correctQ = json_string("Failed to open expected output file."); res = wbFalse; } else if (wbImage_getWidth(expectedImage) != wbSolution_getWidth(sol)) { _solution_correctQ = json_string("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 = json_string("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 = json_string("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 = json_string("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 = json_string("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 = json_string(str.c_str()); res = wbFalse; goto matrixCleanup; } } } res = wbTrue; } matrixCleanup: if (expectedData != NULL) { wbFree(expectedData); } return res; } else { wbAssert(wbFalse); return wbFalse; } }