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 : { 23 : std::ifstream f(name.c_str()); 24 : return f.good(); 25 : } 26 : 27 : TEST_SUITE_BEGIN("io"); 28 : 29 4 : TEST_CASE("PGMHandler: Write PGM file") 30 : { 31 6 : GIVEN("A 2D DataContainer") 32 : { 33 4 : IndexVector_t numCoeff{{10, 10}}; 34 4 : VolumeDescriptor dd(numCoeff); 35 4 : 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 : } 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 4 : WHEN("Writing the DataContainer to the PGM format") 46 : { 47 4 : std::stringstream buf; 48 2 : PGM::write(dc, buf); 49 : 50 : // Get string from buffer 51 4 : 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 : }; 61 : 62 3 : THEN("The header is correct") 63 : { 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 : } 68 : 69 3 : THEN("The body is correct") 70 : { 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 : } 83 : } 84 : } 85 : } 86 : 87 5 : GIVEN("A 1D DataContainer") 88 : { 89 2 : IndexVector_t numCoeff1d{{10}}; 90 2 : VolumeDescriptor dd1d(numCoeff1d); 91 2 : DataContainer dc1d(dd1d); 92 2 : THEN("An exception is raised") 93 : { 94 4 : REQUIRE_THROWS_AS(PGM::write(dc1d, "test.pgm"), InvalidArgumentError); 95 : } 96 : } 97 : 98 5 : GIVEN("A 3D DataContainer") 99 : { 100 2 : IndexVector_t numCoeff3d{{10, 8, 17}}; 101 2 : VolumeDescriptor dd(numCoeff3d); 102 2 : DataContainer dc(dd); 103 2 : THEN("An exception is raised") 104 : { 105 4 : REQUIRE_THROWS_AS(PGM::write(dc, "test.pgm"), InvalidArgumentError); 106 : } 107 : } 108 4 : } 109 : 110 : TEST_SUITE_END();