Line data Source code
1 : /** 2 : * @file test_ioUtils.cpp 3 : * 4 : * @brief Tests for the ioUtils 5 : * 6 : * @author Tobias Lasser - initial code 7 : */ 8 : 9 : #include "doctest/doctest.h" 10 : #include "ioUtils.h" 11 : #include "elsaDefines.h" 12 : 13 : using namespace elsa; 14 : using namespace doctest; 15 : 16 : TEST_SUITE_BEGIN("io"); 17 : 18 3 : TEST_CASE("ioUtils: testing the StringUtils") 19 : { 20 4 : GIVEN("a string with whitespace at beginning/end") 21 : { 22 2 : std::string testString{" Test String "}; 23 : 24 2 : WHEN("trimming the string") 25 : { 26 1 : StringUtils::trim(testString); 27 : 28 2 : THEN("the string is trimmed") 29 : { 30 2 : std::string expectedTrimmedString("Test String"); 31 1 : REQUIRE_EQ(testString, expectedTrimmedString); 32 : } 33 : } 34 : } 35 : 36 5 : GIVEN("a mixed upper/lower case string") 37 : { 38 4 : std::string testString("aBcDeFGhIJKlM"); 39 : 40 3 : WHEN("transforming the string to lower case") 41 : { 42 1 : StringUtils::toLower(testString); 43 : 44 2 : THEN("the string is lower case") 45 : { 46 2 : std::string expectedLowerCaseString("abcdefghijklm"); 47 1 : REQUIRE_EQ(testString, expectedLowerCaseString); 48 : } 49 : } 50 : 51 3 : WHEN("transforming the string to upper case") 52 : { 53 1 : StringUtils::toUpper(testString); 54 : 55 2 : THEN("the string is upper case") 56 : { 57 2 : std::string expectedUpperCaseString("ABCDEFGHIJKLM"); 58 1 : REQUIRE_EQ(testString, expectedUpperCaseString); 59 : } 60 : } 61 : } 62 3 : } 63 : 64 5 : TEST_CASE("ioUtils: testing the DataUtils") 65 : { 66 6 : GIVEN("a data type") 67 : { 68 2 : WHEN("testing for its size") 69 : { 70 2 : THEN("the results is as expected") 71 : { 72 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::INT8), 1); 73 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::UINT8), 1); 74 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::INT16), 2); 75 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::UINT16), 2); 76 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::INT32), 4); 77 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::UINT32), 4); 78 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::FLOAT32), 4); 79 1 : REQUIRE_EQ(DataUtils::getSizeOfDataType(DataUtils::DataType::FLOAT64), 8); 80 : } 81 : } 82 : } 83 : 84 7 : GIVEN("strings containing numbers") 85 : { 86 4 : std::string testStringInt("12345"); 87 4 : std::string testStringFloat("12.456"); 88 : 89 3 : WHEN("parsing these strings") 90 : { 91 1 : auto intNumber = DataUtils::parse<index_t>(testStringInt); 92 1 : auto floatNumber = DataUtils::parse<real_t>(testStringFloat); 93 : 94 2 : THEN("the result is correct") 95 : { 96 1 : REQUIRE_EQ(intNumber, 12345); 97 1 : REQUIRE_EQ(floatNumber, Approx(12.456f)); 98 : } 99 : } 100 : 101 3 : WHEN("supplying garbage") 102 : { 103 2 : std::string testStringGarbage("b6c"); 104 : 105 2 : THEN("an exception is thrown") 106 : { 107 2 : REQUIRE_THROWS_AS(DataUtils::parse<index_t>(testStringGarbage), Error); 108 : } 109 : } 110 : } 111 : 112 7 : GIVEN("strings containing a vector of numbers") 113 : { 114 4 : std::string testStringInt("1 2 3"); 115 4 : std::string testStringFloat("1.2 3.4"); 116 : 117 3 : WHEN("parsing these strings") 118 : { 119 2 : auto intVector = DataUtils::parseVector<index_t>(testStringInt); 120 2 : auto floatVector = DataUtils::parseVector<real_t>(testStringFloat); 121 : 122 2 : THEN("the result is correct") 123 : { 124 1 : REQUIRE_EQ(intVector.size(), 3); 125 1 : REQUIRE_EQ(intVector[0], 1); 126 1 : REQUIRE_EQ(intVector[1], 2); 127 1 : REQUIRE_EQ(intVector[2], 3); 128 : 129 1 : REQUIRE_EQ(floatVector.size(), 2); 130 1 : REQUIRE_EQ(floatVector[0], Approx(1.2f)); 131 1 : REQUIRE_EQ(floatVector[1], Approx(3.4f)); 132 : } 133 : } 134 : 135 3 : WHEN("supplying garbage") 136 : { 137 2 : std::string testStringGarbage("1 3.4 abc"); 138 : 139 2 : THEN("an exception is thrown") 140 : { 141 2 : REQUIRE_THROWS_AS(DataUtils::parseVector<index_t>(testStringGarbage), Error); 142 : } 143 : } 144 : } 145 5 : } 146 : 147 : TEST_SUITE_END();