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

          Line data    Source code
       1             : /**
       2             :  * @file test_HardThresholding.cpp
       3             :  *
       4             :  * @brief Tests for the HardThresholding class
       5             :  *
       6             :  * @author Andi Braimllari
       7             :  */
       8             : 
       9             : #include "Error.h"
      10             : #include "HardThresholding.h"
      11             : #include "VolumeDescriptor.h"
      12             : 
      13             : #include "doctest/doctest.h"
      14             : #include <testHelpers.h>
      15             : 
      16             : using namespace elsa;
      17             : using namespace doctest;
      18             : 
      19             : TEST_SUITE_BEGIN("proximity_operators");
      20             : 
      21             : TEST_CASE_TEMPLATE("HardThresholding: Testing construction", data_t, float, double)
      22           4 : {
      23           4 :     GIVEN("a DataDescriptor")
      24           4 :     {
      25           4 :         IndexVector_t numCoeff(3);
      26           4 :         numCoeff << 8, 4, 52;
      27           4 :         VolumeDescriptor volDescr(numCoeff);
      28             : 
      29           4 :         WHEN("instantiating a HardThresholding operator")
      30           4 :         {
      31           2 :             HardThresholding<data_t> hThrOp(volDescr);
      32             : 
      33           2 :             THEN("the DataDescriptors are equal")
      34           2 :             {
      35           2 :                 REQUIRE_EQ(hThrOp.getRangeDescriptor(), volDescr);
      36           2 :             }
      37           2 :         }
      38             : 
      39           4 :         WHEN("cloning a HardThresholding operator")
      40           4 :         {
      41           2 :             HardThresholding<data_t> hThrOp(volDescr);
      42           2 :             auto hThrOpClone = hThrOp.clone();
      43             : 
      44           2 :             THEN("cloned HardThresholding operator equals original HardThresholding operator")
      45           2 :             {
      46           2 :                 REQUIRE_NE(hThrOpClone.get(), &hThrOp);
      47           2 :                 REQUIRE_EQ(*hThrOpClone, hThrOp);
      48           2 :             }
      49           2 :         }
      50           4 :     }
      51           4 : }
      52             : 
      53             : TEST_CASE_TEMPLATE("HardThresholding: Testing in 1D", data_t, float, double)
      54           2 : {
      55           2 :     GIVEN("a DataDescriptor")
      56           2 :     {
      57           2 :         IndexVector_t numCoeff(1);
      58           2 :         numCoeff << 8;
      59           2 :         VolumeDescriptor volDescr(numCoeff);
      60             : 
      61           2 :         WHEN("Using HardThresholding operator in 1D")
      62           2 :         {
      63           2 :             HardThresholding<data_t> hThrOp(volDescr);
      64             : 
      65           2 :             THEN("Values under threshold=4 are 0 and values above remain the same")
      66           2 :             {
      67           2 :                 Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
      68           2 :                 data << -2, 3, 4, -7, 7, 8, 8, 3;
      69           2 :                 DataContainer<data_t> dC(volDescr, data);
      70             : 
      71           2 :                 Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
      72           2 :                 expectedRes << 0, 0, 0, -7, 7, 8, 8, 0;
      73           2 :                 DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
      74             : 
      75           2 :                 REQUIRE_UNARY(isApprox(dCRes, hThrOp.apply(dC, geometry::Threshold<data_t>{4})));
      76           2 :             }
      77           2 :         }
      78           2 :     }
      79           2 : }
      80             : 
      81             : TEST_CASE_TEMPLATE("HardThresholding: Testing in 3D", data_t, float, double)
      82           2 : {
      83           2 :     GIVEN("a DataDescriptor")
      84           2 :     {
      85           2 :         IndexVector_t numCoeff(3);
      86           2 :         numCoeff << 3, 2, 3;
      87           2 :         VolumeDescriptor volumeDescriptor(numCoeff);
      88             : 
      89           2 :         WHEN("Using HardThresholding operator in 3D")
      90           2 :         {
      91           2 :             HardThresholding<data_t> hThrOp(volumeDescriptor);
      92             : 
      93           2 :             THEN("Values under threshold=5 are 0 and values above remain the same")
      94           2 :             {
      95           2 :                 Vector_t<data_t> data(volumeDescriptor.getNumberOfCoefficients());
      96           2 :                 data << 2, 1, 6, 6, 1, 4, 2, -9, 7, 7, 7, 3, 1, 2, 8, 9, -4, 5;
      97           2 :                 DataContainer<data_t> dC(volumeDescriptor, data);
      98             : 
      99           2 :                 Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
     100           2 :                 expectedRes << 0, 0, 6, 6, 0, 0, 0, -9, 7, 7, 7, 0, 0, 0, 8, 9, 0, 0;
     101           2 :                 DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
     102             : 
     103           2 :                 REQUIRE_UNARY(isApprox(dCRes, hThrOp.apply(dC, geometry::Threshold<data_t>{5})));
     104           2 :             }
     105           2 :         }
     106           2 :     }
     107           2 : }
     108             : 
     109             : TEST_CASE_TEMPLATE("HardThresholding: Testing general behaviour", data_t, float, double)
     110           8 : {
     111           8 :     GIVEN("a DataDescriptor")
     112           8 :     {
     113           8 :         IndexVector_t numCoeff(1);
     114           8 :         numCoeff << 8;
     115           8 :         VolumeDescriptor volDescr(numCoeff);
     116             : 
     117           8 :         WHEN("Using HardThresholding operator")
     118           8 :         {
     119           8 :             HardThresholding<data_t> hThrOp(volDescr);
     120             : 
     121           8 :             THEN("The zero vector is returned when the zero vector is given")
     122           8 :             {
     123           2 :                 Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
     124           2 :                 data << 0, 0, 0, 0, 0, 0, 0, 0;
     125           2 :                 DataContainer<data_t> dataContainer(volDescr, data);
     126             : 
     127           2 :                 Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
     128           2 :                 expectedRes << 0, 0, 0, 0, 0, 0, 0, 0;
     129           2 :                 DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
     130             : 
     131           2 :                 REQUIRE_UNARY(
     132           2 :                     isApprox(dCRes, hThrOp.apply(dataContainer, geometry::Threshold<data_t>{4})));
     133           2 :             }
     134             : 
     135           8 :             THEN("HardThresholding operator throws exception for t = 0")
     136           8 :             {
     137           2 :                 Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
     138           2 :                 data << 0, 0, 0, 0, 0, 0, 0, 0;
     139           2 :                 DataContainer<data_t> dC(volDescr, data);
     140             : 
     141             :                 // actually the geometry::Threshold throws this
     142           2 :                 REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{0}),
     143           2 :                                   InvalidArgumentError);
     144           2 :             }
     145             : 
     146           8 :             THEN("HardThresholding operator throws exception for t < 0")
     147           8 :             {
     148           2 :                 Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
     149           2 :                 data << 0, 0, 0, 0, 0, 0, 0, 0;
     150           2 :                 DataContainer<data_t> dC(volDescr, data);
     151             : 
     152             :                 // actually the geometry::Threshold throws this
     153           2 :                 REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{-1}),
     154           2 :                                   InvalidArgumentError);
     155           2 :             }
     156             : 
     157           8 :             THEN("HardThresholding operator throws exception for differently sized v and prox")
     158           8 :             {
     159           2 :                 Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
     160           2 :                 data << 0, 0, 0, 0, 0, 0, 0, 0;
     161           2 :                 DataContainer<data_t> dC(volDescr, data);
     162             : 
     163           2 :                 IndexVector_t numCoeff1(1);
     164           2 :                 numCoeff1 << 9;
     165           2 :                 VolumeDescriptor volDescr1(numCoeff1);
     166           2 :                 Vector_t<data_t> data1(volDescr1.getNumberOfCoefficients());
     167           2 :                 data1 << 0, 0, 0, 0, 0, 0, 0, 0, 0;
     168           2 :                 DataContainer<data_t> dC1(volDescr1, data1);
     169             : 
     170           2 :                 REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{1}, dC1),
     171           2 :                                   LogicError);
     172           2 :             }
     173           8 :         }
     174           8 :     }
     175           8 : }
     176             : 
     177             : TEST_SUITE_END();

Generated by: LCOV version 1.14