Line data Source code
1 : /** 2 : * @file test_PGMHandler.cpp 3 : * 4 : * @brief Tests for the PGMHandler class 5 : * 6 : * @author David Frank - initial code 7 : */ 8 : 9 : #include "doctest/doctest.h" 10 : #include "Error.h" 11 : #include "PGMHandler.h" 12 : 13 : #include "VolumeDescriptor.h" 14 : 15 : #include <sstream> 16 : #include <string_view> 17 : 18 : using namespace elsa; 19 : using namespace doctest; 20 : 21 : inline bool file_exists(std::string name) 22 0 : { 23 0 : std::ifstream f(name.c_str()); 24 0 : return f.good(); 25 0 : } 26 : 27 : TEST_SUITE_BEGIN("io"); 28 : 29 : TEST_CASE("PGMHandler: Write PGM file") 30 4 : { 31 4 : GIVEN("A 2D DataContainer") 32 4 : { 33 2 : IndexVector_t numCoeff{{10, 10}}; 34 2 : VolumeDescriptor dd(numCoeff); 35 2 : DataContainer dc(dd); 36 : 37 202 : for (int i = 0; i < dc.getSize(); ++i) { 38 200 : dc[i] = static_cast<real_t>(i) + 10; 39 200 : } 40 : 41 : // Also compute the max value and the scale factor 42 2 : const auto maxVal = dc.maxElement(); 43 2 : const auto minVal = dc.minElement(); 44 : 45 2 : WHEN("Writing the DataContainer to the PGM format") 46 2 : { 47 2 : std::stringstream buf; 48 2 : PGM::write(dc, buf); 49 : 50 : // Get string from buffer 51 2 : auto str = buf.str(); 52 : 53 : // Lambda to get the next token from `str` until `delimiter` 54 : // remove the part from `str` and return it 55 106 : auto nextToken = [](std::string& str, std::string_view delimiter) { 56 106 : auto pos = str.find(delimiter); 57 106 : auto token = str.substr(0, pos); 58 106 : str.erase(0, pos + delimiter.length()); 59 106 : return token; 60 106 : }; 61 : 62 2 : THEN("The header is correct") 63 2 : { 64 1 : CHECK_EQ(nextToken(str, "\n"), "P2"); 65 1 : CHECK_EQ(nextToken(str, "\n"), "10 10"); 66 1 : CHECK_EQ(nextToken(str, "\n"), "255"); 67 1 : } 68 : 69 2 : THEN("The body is correct") 70 2 : { 71 : // Pop the header again 72 1 : nextToken(str, "\n"); 73 1 : nextToken(str, "\n"); 74 1 : nextToken(str, "\n"); 75 : 76 1 : int counter = 0; 77 101 : while (!str.empty()) { 78 100 : const auto normalized = (dc[counter] - minVal) / (maxVal - minVal); 79 100 : auto val = static_cast<int>(normalized * 255.f); 80 100 : REQUIRE_EQ(std::to_string(val), nextToken(str, " ")); 81 100 : counter++; 82 100 : } 83 1 : } 84 2 : } 85 2 : } 86 : 87 4 : GIVEN("A 1D DataContainer") 88 4 : { 89 1 : IndexVector_t numCoeff1d{{10}}; 90 1 : VolumeDescriptor dd1d(numCoeff1d); 91 1 : DataContainer dc1d(dd1d); 92 1 : THEN("An exception is raised") 93 1 : { 94 1 : REQUIRE_THROWS_AS(PGM::write(dc1d, "test.pgm"), InvalidArgumentError); 95 1 : } 96 1 : } 97 : 98 4 : GIVEN("A 3D DataContainer") 99 4 : { 100 1 : IndexVector_t numCoeff3d{{10, 8, 17}}; 101 1 : VolumeDescriptor dd(numCoeff3d); 102 1 : DataContainer dc(dd); 103 1 : THEN("An exception is raised") 104 1 : { 105 1 : REQUIRE_THROWS_AS(PGM::write(dc, "test.pgm"), InvalidArgumentError); 106 1 : } 107 1 : } 108 4 : } 109 : 110 : TEST_SUITE_END();