LCOV - code coverage report
Current view: top level - elsa/io/tests - test_ioUtils.cpp (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 107 107 100.0 %
Date: 2022-08-25 03:05:39 Functions: 2 2 100.0 %

          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             : TEST_CASE("ioUtils: testing the StringUtils")
      19           3 : {
      20           3 :     GIVEN("a string with whitespace at beginning/end")
      21           3 :     {
      22           1 :         std::string testString{"   Test String   "};
      23             : 
      24           1 :         WHEN("trimming the string")
      25           1 :         {
      26           1 :             StringUtils::trim(testString);
      27             : 
      28           1 :             THEN("the string is trimmed")
      29           1 :             {
      30           1 :                 std::string expectedTrimmedString("Test String");
      31           1 :                 REQUIRE_EQ(testString, expectedTrimmedString);
      32           1 :             }
      33           1 :         }
      34           1 :     }
      35             : 
      36           3 :     GIVEN("a mixed upper/lower case string")
      37           3 :     {
      38           2 :         std::string testString("aBcDeFGhIJKlM");
      39             : 
      40           2 :         WHEN("transforming the string to lower case")
      41           2 :         {
      42           1 :             StringUtils::toLower(testString);
      43             : 
      44           1 :             THEN("the string is lower case")
      45           1 :             {
      46           1 :                 std::string expectedLowerCaseString("abcdefghijklm");
      47           1 :                 REQUIRE_EQ(testString, expectedLowerCaseString);
      48           1 :             }
      49           1 :         }
      50             : 
      51           2 :         WHEN("transforming the string to upper case")
      52           2 :         {
      53           1 :             StringUtils::toUpper(testString);
      54             : 
      55           1 :             THEN("the string is upper case")
      56           1 :             {
      57           1 :                 std::string expectedUpperCaseString("ABCDEFGHIJKLM");
      58           1 :                 REQUIRE_EQ(testString, expectedUpperCaseString);
      59           1 :             }
      60           1 :         }
      61           2 :     }
      62           3 : }
      63             : 
      64             : TEST_CASE("ioUtils: testing the DataUtils")
      65           5 : {
      66           5 :     GIVEN("a data type")
      67           5 :     {
      68           1 :         WHEN("testing for its size")
      69           1 :         {
      70           1 :             THEN("the results is as expected")
      71           1 :             {
      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           1 :             }
      81           1 :         }
      82           1 :     }
      83             : 
      84           5 :     GIVEN("strings containing numbers")
      85           5 :     {
      86           2 :         std::string testStringInt("12345");
      87           2 :         std::string testStringFloat("12.456");
      88             : 
      89           2 :         WHEN("parsing these strings")
      90           2 :         {
      91           1 :             auto intNumber = DataUtils::parse<index_t>(testStringInt);
      92           1 :             auto floatNumber = DataUtils::parse<real_t>(testStringFloat);
      93             : 
      94           1 :             THEN("the result is correct")
      95           1 :             {
      96           1 :                 REQUIRE_EQ(intNumber, 12345);
      97           1 :                 REQUIRE_EQ(floatNumber, Approx(12.456f));
      98           1 :             }
      99           1 :         }
     100             : 
     101           2 :         WHEN("supplying garbage")
     102           2 :         {
     103           1 :             std::string testStringGarbage("b6c");
     104             : 
     105           1 :             THEN("an exception is thrown")
     106           1 :             {
     107           1 :                 REQUIRE_THROWS_AS(DataUtils::parse<index_t>(testStringGarbage), Error);
     108           1 :             }
     109           1 :         }
     110           2 :     }
     111             : 
     112           5 :     GIVEN("strings containing a vector of numbers")
     113           5 :     {
     114           2 :         std::string testStringInt("1 2 3");
     115           2 :         std::string testStringFloat("1.2 3.4");
     116             : 
     117           2 :         WHEN("parsing these strings")
     118           2 :         {
     119           1 :             auto intVector = DataUtils::parseVector<index_t>(testStringInt);
     120           1 :             auto floatVector = DataUtils::parseVector<real_t>(testStringFloat);
     121             : 
     122           1 :             THEN("the result is correct")
     123           1 :             {
     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           1 :             }
     133           1 :         }
     134             : 
     135           2 :         WHEN("supplying garbage")
     136           2 :         {
     137           1 :             std::string testStringGarbage("1 3.4 abc");
     138             : 
     139           1 :             THEN("an exception is thrown")
     140           1 :             {
     141           1 :                 REQUIRE_THROWS_AS(DataUtils::parseVector<index_t>(testStringGarbage), Error);
     142           1 :             }
     143           1 :         }
     144           2 :     }
     145           5 : }
     146             : 
     147             : TEST_SUITE_END();

Generated by: LCOV version 1.14