LCOV - code coverage report
Current view: top level - core/tests - test_elsaDefines.cpp (source / functions) Hit Total Coverage
Test: test_coverage.info.cleaned Lines: 152 153 99.3 %
Date: 2022-02-28 03:37:41 Functions: 13 17 76.5 %

          Line data    Source code
       1             : /**
       2             :  * @file test_elsaDefines.cpp
       3             :  *
       4             :  * @brief Tests for common elsa defines
       5             :  *
       6             :  * @author David Frank - initial version
       7             :  */
       8             : 
       9             : #include "doctest/doctest.h"
      10             : #include <iostream>
      11             : #include "elsaDefines.h"
      12             : #include "TypeCasts.hpp"
      13             : 
      14             : #include <type_traits>
      15             : 
      16             : using namespace elsa;
      17             : using namespace doctest;
      18             : 
      19             : TEST_SUITE_BEGIN("core");
      20             : 
      21           3 : TEST_CASE("elsaDefines: Testing PI")
      22             : {
      23             : 
      24           4 :     THEN("Pi for real_t and pi_t are equal") { REQUIRE_EQ(pi<real_t>, pi_t); }
      25             : 
      26           4 :     THEN("pi_t is somewhat close to a representation for pi")
      27             :     {
      28           1 :         REQUIRE_EQ(pi_t, Approx(3.14159265358979323846).epsilon(1e-5));
      29             :     }
      30             : 
      31           4 :     THEN("Pi for double is close to given value for pi")
      32             :     {
      33           1 :         REQUIRE_EQ(pi<double>, 3.14159265358979323846);
      34             :     }
      35           3 : }
      36             : 
      37           1 : TEST_CASE("elsaDefines: Testing compile-time predicates")
      38             : {
      39             :     static_assert(std::is_same_v<float, GetFloatingPointType_t<complex<float>>>);
      40             :     static_assert(std::is_same_v<double, GetFloatingPointType_t<complex<double>>>);
      41             :     static_assert(std::is_same_v<double, GetFloatingPointType_t<double>>);
      42             :     static_assert(std::is_same_v<float, GetFloatingPointType_t<float>>);
      43             :     static_assert(!std::is_same_v<float, GetFloatingPointType_t<double>>);
      44             : 
      45           1 :     REQUIRE_UNARY(true);
      46           1 : }
      47             : 
      48           1 : TEST_CASE("elsaDefines: Printing default handler type")
      49             : {
      50             : #ifdef ELSA_CUDA_VECTOR
      51             :     REQUIRE_EQ(defaultHandlerType, DataHandlerType::GPU);
      52             : #else
      53           1 :     REQUIRE_EQ(defaultHandlerType, DataHandlerType::CPU);
      54             : #endif
      55           1 : }
      56             : 
      57           2 : TEST_CASE("TypeCasts: Casting to unsigned")
      58             : {
      59           3 :     WHEN("Value is in range of the destination type")
      60             :     {
      61           1 :         std::int8_t signed1{100};
      62           1 :         std::int16_t signed2{100};
      63           1 :         std::int32_t signed3{100};
      64           1 :         std::int64_t signed4{100};
      65             : 
      66           1 :         auto unsigned1 = asUnsigned(signed1);
      67           1 :         auto unsigned2 = asUnsigned(signed2);
      68           1 :         auto unsigned3 = asUnsigned(signed3);
      69           1 :         auto unsigned4 = asUnsigned(signed4);
      70             : 
      71             :         static_assert(std::is_same_v<decltype(unsigned1), std::make_unsigned_t<decltype(signed1)>>);
      72             :         static_assert(std::is_same_v<decltype(unsigned2), std::make_unsigned_t<decltype(signed2)>>);
      73             :         static_assert(std::is_same_v<decltype(unsigned3), std::make_unsigned_t<decltype(signed3)>>);
      74             :         static_assert(std::is_same_v<decltype(unsigned4), std::make_unsigned_t<decltype(signed4)>>);
      75             : 
      76           1 :         REQUIRE_EQ(signed1, unsigned1);
      77           1 :         REQUIRE_EQ(signed2, unsigned2);
      78           1 :         REQUIRE_EQ(signed3, unsigned3);
      79           1 :         REQUIRE_EQ(signed4, unsigned4);
      80             :     }
      81             : 
      82           3 :     WHEN("Value is already unsigned")
      83             :     {
      84           1 :         std::uint8_t val1{100};
      85           1 :         std::uint16_t val2{100};
      86           1 :         std::uint32_t val3{100};
      87           1 :         std::uint64_t val4{100};
      88             : 
      89           1 :         auto unsigned1 = asUnsigned(val1);
      90           1 :         auto unsigned2 = asUnsigned(val2);
      91           1 :         auto unsigned3 = asUnsigned(val3);
      92           1 :         auto unsigned4 = asUnsigned(val4);
      93             : 
      94             :         static_assert(std::is_same_v<decltype(unsigned1), decltype(val1)>);
      95             :         static_assert(std::is_same_v<decltype(unsigned2), decltype(val2)>);
      96             :         static_assert(std::is_same_v<decltype(unsigned3), decltype(val3)>);
      97             :         static_assert(std::is_same_v<decltype(unsigned4), decltype(val4)>);
      98             : 
      99           1 :         REQUIRE_EQ(val1, unsigned1);
     100           1 :         REQUIRE_EQ(val2, unsigned2);
     101           1 :         REQUIRE_EQ(val3, unsigned3);
     102           1 :         REQUIRE_EQ(val4, unsigned4);
     103             :     }
     104           2 : }
     105             : 
     106           2 : TEST_CASE("TypeCasts: Casting to signed")
     107             : {
     108           3 :     WHEN("Value is in range of the destination type")
     109             :     {
     110           1 :         std::uint8_t unsigned1{100};
     111           1 :         std::uint16_t unsigned2{100};
     112           1 :         std::uint32_t unsigned3{100};
     113           1 :         std::uint64_t unsigned4{100};
     114             : 
     115           1 :         auto signed1 = asSigned(unsigned1);
     116           1 :         auto signed2 = asSigned(unsigned2);
     117           1 :         auto signed3 = asSigned(unsigned3);
     118           1 :         auto signed4 = asSigned(unsigned4);
     119             : 
     120             :         static_assert(std::is_same_v<decltype(signed1), std::make_signed_t<decltype(unsigned1)>>);
     121             :         static_assert(std::is_same_v<decltype(signed2), std::make_signed_t<decltype(unsigned2)>>);
     122             :         static_assert(std::is_same_v<decltype(signed3), std::make_signed_t<decltype(unsigned3)>>);
     123             :         static_assert(std::is_same_v<decltype(signed4), std::make_signed_t<decltype(unsigned4)>>);
     124             : 
     125           1 :         REQUIRE_EQ(signed1, unsigned1);
     126           1 :         REQUIRE_EQ(signed2, unsigned2);
     127           1 :         REQUIRE_EQ(signed3, unsigned3);
     128           1 :         REQUIRE_EQ(signed4, unsigned4);
     129             :     }
     130             : 
     131           3 :     WHEN("Value is already signed")
     132             :     {
     133           1 :         std::int8_t val1{100};
     134           1 :         std::int16_t val2{100};
     135           1 :         std::int32_t val3{100};
     136           1 :         std::int64_t val4{100};
     137             : 
     138           1 :         auto signed1 = asSigned(val1);
     139           1 :         auto signed2 = asSigned(val2);
     140           1 :         auto signed3 = asSigned(val3);
     141           1 :         auto signed4 = asSigned(val4);
     142             : 
     143             :         static_assert(std::is_same_v<decltype(signed1), decltype(val1)>);
     144             :         static_assert(std::is_same_v<decltype(signed2), decltype(val2)>);
     145             :         static_assert(std::is_same_v<decltype(signed3), decltype(val3)>);
     146             :         static_assert(std::is_same_v<decltype(signed4), decltype(val4)>);
     147             : 
     148           1 :         REQUIRE_EQ(val1, signed1);
     149           1 :         REQUIRE_EQ(val2, signed2);
     150           1 :         REQUIRE_EQ(val3, signed3);
     151           1 :         REQUIRE_EQ(val4, signed4);
     152             :     }
     153           2 : }
     154             : 
     155           9 : TEST_CASE("TypeCasts: Testing is()")
     156             : {
     157             :     struct Base {
     158           9 :         virtual ~Base() = default;
     159             :     };
     160             :     struct Derived1 final : Base {
     161          18 :         ~Derived1() override = default;
     162             :     };
     163             :     struct Derived2 final : public Base {
     164             :         ~Derived2() override = default;
     165             :     };
     166             : 
     167          12 :     WHEN("Base pointer points to Derived1")
     168             :     {
     169           6 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     170             : 
     171           4 :         THEN("Casting to base is also fine")
     172             :         {
     173           1 :             REQUIRE_UNARY(is<Base>(ptr.get()));
     174           1 :             REQUIRE_UNARY(ptr);
     175             :         }
     176           4 :         THEN("Casting to Derived1 is fine")
     177             :         {
     178           1 :             REQUIRE_UNARY(is<Derived1>(ptr.get()));
     179           1 :             REQUIRE_UNARY(ptr);
     180             :         }
     181           4 :         THEN("Casting to Derived2 doesn't work")
     182             :         {
     183           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(ptr.get()));
     184           1 :             REQUIRE_UNARY(ptr);
     185             :         }
     186             :     }
     187             : 
     188          12 :     WHEN("Base reference points to Derived1")
     189             :     {
     190           6 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     191             : 
     192           4 :         THEN("Casting to base is also fine")
     193             :         {
     194           1 :             REQUIRE_UNARY(is<Base>(*ptr));
     195           1 :             REQUIRE_UNARY(ptr);
     196             :         }
     197           4 :         THEN("Casting to Derived1 is fine")
     198             :         {
     199           1 :             REQUIRE_UNARY(is<Derived1>(*ptr));
     200           1 :             REQUIRE_UNARY(ptr);
     201             :         }
     202           4 :         THEN("Casting to Derived2 doesn't work")
     203             :         {
     204           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(*ptr));
     205           1 :             REQUIRE_UNARY(ptr);
     206             :         }
     207             :     }
     208             : 
     209          12 :     WHEN("unique_ptr to Base points to Derived1")
     210             :     {
     211           6 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     212             : 
     213           4 :         THEN("Check is<Base> returns true")
     214             :         {
     215           1 :             REQUIRE_UNARY(is<Base>(ptr));
     216           1 :             REQUIRE_UNARY(ptr);
     217             :         }
     218           4 :         THEN("Check is<Derived1> returns true")
     219             :         {
     220           1 :             REQUIRE_UNARY(is<Derived1>(ptr));
     221           1 :             REQUIRE_UNARY(ptr);
     222             :         }
     223           4 :         THEN("Check is<Derived2> returns false")
     224             :         {
     225           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(ptr));
     226           1 :             REQUIRE_UNARY(ptr);
     227             :         }
     228             :     }
     229           9 : }
     230             : 
     231          12 : TEST_CASE("TypeCasts: Testing downcast")
     232             : {
     233             :     struct Base {
     234          12 :         virtual ~Base() = default;
     235             :     };
     236             :     struct Derived1 final : Base {
     237          24 :         ~Derived1() override = default;
     238             :     };
     239             :     struct Derived2 final : public Base {
     240           0 :         ~Derived2() override = default;
     241             :     };
     242             : 
     243          16 :     WHEN("Base pointer points to Derived1")
     244             :     {
     245           8 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     246             : 
     247           5 :         THEN("Downcasting to Base works")
     248             :         {
     249           1 :             REQUIRE_UNARY(downcast<Base>(ptr.get()));
     250           1 :             REQUIRE_UNARY(ptr);
     251             :         }
     252           5 :         THEN("Downcasting to Derived1 works")
     253             :         {
     254           1 :             REQUIRE_UNARY(downcast<Derived1>(ptr.get()));
     255           1 :             REQUIRE_UNARY(ptr);
     256             :         }
     257           5 :         THEN("Safely downcasting to Derived1 works")
     258             :         {
     259           1 :             REQUIRE_UNARY(downcast_safe<Derived1>(ptr.get()));
     260           1 :             REQUIRE_UNARY(ptr);
     261             :         }
     262           5 :         THEN("Safely downcasting to Derived2 doesn't work")
     263             :         {
     264           1 :             REQUIRE_UNARY_FALSE(downcast_safe<Derived2>(ptr.get()));
     265           1 :             REQUIRE_UNARY(ptr);
     266             :         }
     267             :     }
     268             : 
     269          16 :     WHEN("Base reference points to Derived1")
     270             :     {
     271           8 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     272             : 
     273           5 :         THEN("Downcasting to Base works")
     274             :         {
     275           1 :             REQUIRE_NOTHROW(downcast<Base>(*ptr));
     276           1 :             REQUIRE_UNARY(ptr);
     277             :         }
     278           5 :         THEN("Downcasting to Derived1 works")
     279             :         {
     280           1 :             REQUIRE_NOTHROW(downcast<Derived1>(*ptr));
     281           1 :             REQUIRE_UNARY(ptr);
     282             :         }
     283           5 :         THEN("Safely downcasting to Derived1 works")
     284             :         {
     285           1 :             REQUIRE_NOTHROW(downcast_safe<Derived1>(*ptr));
     286           1 :             REQUIRE_UNARY(ptr);
     287             :         }
     288           5 :         THEN("Safely downcasting to Derived2 throws")
     289             :         {
     290           2 :             REQUIRE_THROWS(downcast_safe<Derived2>(*ptr));
     291           1 :             REQUIRE_UNARY(ptr);
     292             :         }
     293             :     }
     294             : 
     295          16 :     WHEN("unique_ptr to Base points to Derived1")
     296             :     {
     297           8 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     298             : 
     299           5 :         THEN("Downcasting to base works")
     300             :         {
     301           1 :             REQUIRE_UNARY(downcast<Base>(std::move(ptr)));
     302           1 :             REQUIRE_UNARY_FALSE(ptr);
     303             :         }
     304           5 :         THEN("Downcasting to Derived1 works")
     305             :         {
     306           1 :             REQUIRE_UNARY(downcast<Derived1>(std::move(ptr)));
     307           1 :             REQUIRE_UNARY_FALSE(ptr);
     308             :         }
     309           5 :         THEN("Safely downcasting to Derived1 works")
     310             :         {
     311           1 :             REQUIRE_UNARY(downcast_safe<Derived1>(std::move(ptr)));
     312           1 :             REQUIRE_UNARY_FALSE(ptr);
     313             :         }
     314           5 :         THEN("Safely downcasting to Derived2 doesn't work")
     315             :         {
     316           1 :             REQUIRE_UNARY_FALSE(downcast_safe<Derived2>(std::move(ptr)));
     317           1 :             REQUIRE_UNARY(ptr);
     318             :         }
     319             :     }
     320          12 : }
     321             : 
     322             : TEST_SUITE_END();

Generated by: LCOV version 1.15