LCOV - code coverage report
Current view: top level - operators/tests - test_Scaling.cpp (source / functions) Hit Total Coverage
Test: test_coverage.info.cleaned Lines: 64 64 100.0 %
Date: 2022-02-28 03:37:41 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file test_Scaling.cpp
       3             :  *
       4             :  * @brief Tests for Scaling operator class
       5             :  *
       6             :  * @author Matthias Wieczorek - initial code
       7             :  * @author David Frank - rewrite
       8             :  * @author Tobias Lasser - minor extensions
       9             :  */
      10             : 
      11             : #include "doctest/doctest.h"
      12             : #include "Scaling.h"
      13             : #include "VolumeDescriptor.h"
      14             : #include "testHelpers.h"
      15             : 
      16             : using namespace elsa;
      17             : using namespace doctest;
      18             : 
      19             : TEST_SUITE_BEGIN("core");
      20             : 
      21          22 : TEST_CASE_TEMPLATE("Scaling: Testing construction", data_t, float, double)
      22             : {
      23          20 :     GIVEN("a descriptor")
      24             :     {
      25          20 :         IndexVector_t numCoeff(2);
      26          10 :         numCoeff << 11, 17;
      27          20 :         VolumeDescriptor dd(numCoeff);
      28             : 
      29          14 :         WHEN("instantiating an isotropic scaling operator")
      30             :         {
      31           4 :             real_t scaleFactor = 3.5f;
      32           8 :             Scaling<data_t> scalingOp(dd, scaleFactor);
      33             : 
      34           6 :             THEN("the descriptors are as expected")
      35             :             {
      36           2 :                 REQUIRE_EQ(scalingOp.getDomainDescriptor(), dd);
      37           2 :                 REQUIRE_EQ(scalingOp.getRangeDescriptor(), dd);
      38             :             }
      39             : 
      40           6 :             THEN("the scaling is isotropic and correct")
      41             :             {
      42           2 :                 REQUIRE_UNARY(scalingOp.isIsotropic());
      43           2 :                 REQUIRE_EQ(scalingOp.getScaleFactor(), scaleFactor);
      44           4 :                 REQUIRE_THROWS_AS(scalingOp.getScaleFactors(), LogicError);
      45             :             }
      46             :         }
      47             : 
      48          14 :         WHEN("instantiating an anisotropic scaling operator")
      49             :         {
      50           8 :             DataContainer<data_t> dc(dd);
      51           4 :             dc = 3.5f;
      52           8 :             Scaling<data_t> scalingOp(dd, dc);
      53             : 
      54           6 :             THEN("the descriptors  are as expected")
      55             :             {
      56           2 :                 REQUIRE_EQ(scalingOp.getDomainDescriptor(), dd);
      57           2 :                 REQUIRE_EQ(scalingOp.getRangeDescriptor(), dd);
      58             :             }
      59             : 
      60           6 :             THEN("the scaling is anisotropic")
      61             :             {
      62           2 :                 REQUIRE_UNARY_FALSE(scalingOp.isIsotropic());
      63           2 :                 REQUIRE_EQ(scalingOp.getScaleFactors(), dc);
      64           4 :                 REQUIRE_THROWS_AS(scalingOp.getScaleFactor(), LogicError);
      65             :             }
      66             :         }
      67             : 
      68          12 :         WHEN("cloning a scaling operator")
      69             :         {
      70           4 :             Scaling<data_t> scalingOp(dd, 3.5f);
      71           4 :             auto scalingOpClone = scalingOp.clone();
      72             : 
      73           4 :             THEN("everything matches")
      74             :             {
      75           2 :                 REQUIRE_NE(scalingOpClone.get(), &scalingOp);
      76           2 :                 REQUIRE_EQ(*scalingOpClone, scalingOp);
      77             :             }
      78             :         }
      79             :     }
      80          10 : }
      81             : 
      82          16 : TEST_CASE_TEMPLATE("Scaling: Testing apply to data", data_t, float, double)
      83             : {
      84           8 :     GIVEN("some data")
      85             :     {
      86           8 :         IndexVector_t numCoeff(2);
      87           4 :         numCoeff << 34, 13;
      88           8 :         VolumeDescriptor dd(numCoeff);
      89           8 :         DataContainer<data_t> input(dd);
      90           4 :         data_t inputScalar{2.5};
      91           4 :         input = inputScalar;
      92             : 
      93           6 :         WHEN("applying isotropic scaling")
      94             :         {
      95           2 :             real_t scaleFactor{3.7f};
      96           4 :             Scaling<data_t> scalingOp(dd, scaleFactor);
      97             : 
      98           4 :             THEN("apply and applyAdjoint yield the correct results")
      99             :             {
     100           4 :                 auto resultApply = scalingOp.apply(input);
     101         886 :                 for (index_t i = 0; i < resultApply.getSize(); ++i)
     102         884 :                     REQUIRE_UNARY(checkApproxEq(resultApply[i], inputScalar * scaleFactor));
     103             : 
     104           4 :                 auto resultApplyAdjoint = scalingOp.applyAdjoint(input);
     105         886 :                 for (index_t i = 0; i < resultApply.getSize(); ++i)
     106         884 :                     REQUIRE_UNARY(checkApproxEq(resultApplyAdjoint[i], inputScalar * scaleFactor));
     107             :             }
     108             :         }
     109             : 
     110           6 :         WHEN("applying anisotropic scaling")
     111             :         {
     112           4 :             Vector_t<data_t> randomData(dd.getNumberOfCoefficients());
     113           2 :             randomData.setRandom();
     114           4 :             DataContainer<data_t> scaleFactors(dd, randomData);
     115             : 
     116           4 :             Scaling<data_t> scalingOp(dd, scaleFactors);
     117             : 
     118           4 :             THEN("apply and applyAdjoint yield the correct results")
     119             :             {
     120           4 :                 auto resultApply = scalingOp.apply(input);
     121         886 :                 for (index_t i = 0; i < resultApply.getSize(); ++i)
     122         884 :                     REQUIRE_UNARY(checkApproxEq(resultApply[i], inputScalar * scaleFactors[i]));
     123             : 
     124           4 :                 auto resultApplyAdjoint = scalingOp.applyAdjoint(input);
     125         886 :                 for (index_t i = 0; i < resultApply.getSize(); ++i)
     126         884 :                     REQUIRE_UNARY(
     127             :                         checkApproxEq(resultApplyAdjoint[i], inputScalar * scaleFactors[i]));
     128             :             }
     129             :         }
     130             :     }
     131           4 : }
     132             : TEST_SUITE_END();

Generated by: LCOV version 1.15