LCOV - code coverage report
Current view: top level - elsa/core/tests - test_elsaDefines.cpp (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 254 254 100.0 %
Date: 2022-08-25 03:05:39 Functions: 11 12 91.7 %

          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             : TEST_CASE("elsaDefines: Testing PI")
      22           3 : {
      23             : 
      24           3 :     THEN("Pi for real_t and pi_t are equal") { REQUIRE_EQ(pi<real_t>, pi_t); }
      25             : 
      26           3 :     THEN("pi_t is somewhat close to a representation for pi")
      27           3 :     {
      28           1 :         REQUIRE_EQ(pi_t, Approx(3.14159265358979323846).epsilon(1e-5));
      29           1 :     }
      30             : 
      31           3 :     THEN("Pi for double is close to given value for pi")
      32           3 :     {
      33           1 :         REQUIRE_EQ(pi<double>, 3.14159265358979323846);
      34           1 :     }
      35           3 : }
      36             : 
      37             : TEST_CASE("elsaDefines: Testing compile-time predicates")
      38           1 : {
      39           1 :     static_assert(std::is_same_v<float, GetFloatingPointType_t<complex<float>>>);
      40           1 :     static_assert(std::is_same_v<double, GetFloatingPointType_t<complex<double>>>);
      41           1 :     static_assert(std::is_same_v<double, GetFloatingPointType_t<double>>);
      42           1 :     static_assert(std::is_same_v<float, GetFloatingPointType_t<float>>);
      43           1 :     static_assert(!std::is_same_v<float, GetFloatingPointType_t<double>>);
      44             : 
      45           1 :     REQUIRE_UNARY(true);
      46           1 : }
      47             : 
      48             : TEST_CASE("elsaDefines: Printing default handler type")
      49           1 : {
      50             : #ifdef ELSA_CUDA_VECTOR
      51             :     REQUIRE_EQ(defaultHandlerType, DataHandlerType::GPU);
      52             : #else
      53           1 :     REQUIRE_EQ(defaultHandlerType, DataHandlerType::CPU);
      54           1 : #endif
      55           1 : }
      56             : 
      57             : TEST_CASE("TypeCasts: Casting to unsigned")
      58           2 : {
      59           2 :     WHEN("Value is in range of the destination type")
      60           2 :     {
      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           1 :         static_assert(std::is_same_v<decltype(unsigned1), std::make_unsigned_t<decltype(signed1)>>);
      72           1 :         static_assert(std::is_same_v<decltype(unsigned2), std::make_unsigned_t<decltype(signed2)>>);
      73           1 :         static_assert(std::is_same_v<decltype(unsigned3), std::make_unsigned_t<decltype(signed3)>>);
      74           1 :         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           1 :     }
      81             : 
      82           2 :     WHEN("Value is already unsigned")
      83           2 :     {
      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           1 :         static_assert(std::is_same_v<decltype(unsigned1), decltype(val1)>);
      95           1 :         static_assert(std::is_same_v<decltype(unsigned2), decltype(val2)>);
      96           1 :         static_assert(std::is_same_v<decltype(unsigned3), decltype(val3)>);
      97           1 :         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           1 :     }
     104           2 : }
     105             : 
     106             : TEST_CASE("TypeCasts: Casting to signed")
     107           2 : {
     108           2 :     WHEN("Value is in range of the destination type")
     109           2 :     {
     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           1 :         static_assert(std::is_same_v<decltype(signed1), std::make_signed_t<decltype(unsigned1)>>);
     121           1 :         static_assert(std::is_same_v<decltype(signed2), std::make_signed_t<decltype(unsigned2)>>);
     122           1 :         static_assert(std::is_same_v<decltype(signed3), std::make_signed_t<decltype(unsigned3)>>);
     123           1 :         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           1 :     }
     130             : 
     131           2 :     WHEN("Value is already signed")
     132           2 :     {
     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           1 :         static_assert(std::is_same_v<decltype(signed1), decltype(val1)>);
     144           1 :         static_assert(std::is_same_v<decltype(signed2), decltype(val2)>);
     145           1 :         static_assert(std::is_same_v<decltype(signed3), decltype(val3)>);
     146           1 :         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           1 :     }
     153           2 : }
     154             : 
     155             : TEST_CASE("TypeCasts: Testing is()")
     156           9 : {
     157           9 :     struct Base {
     158           9 :         virtual ~Base() = default;
     159           9 :     };
     160           9 :     struct Derived1 final : Base {
     161           9 :         ~Derived1() override = default;
     162           9 :     };
     163           9 :     struct Derived2 final : public Base {
     164           9 :         ~Derived2() override = default;
     165           9 :     };
     166             : 
     167           9 :     WHEN("Base pointer points to Derived1")
     168           9 :     {
     169           3 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     170             : 
     171           3 :         THEN("Casting to base is also fine")
     172           3 :         {
     173           1 :             REQUIRE_UNARY(is<Base>(ptr.get()));
     174           1 :             REQUIRE_UNARY(ptr);
     175           1 :         }
     176           3 :         THEN("Casting to Derived1 is fine")
     177           3 :         {
     178           1 :             REQUIRE_UNARY(is<Derived1>(ptr.get()));
     179           1 :             REQUIRE_UNARY(ptr);
     180           1 :         }
     181           3 :         THEN("Casting to Derived2 doesn't work")
     182           3 :         {
     183           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(ptr.get()));
     184           1 :             REQUIRE_UNARY(ptr);
     185           1 :         }
     186           3 :     }
     187             : 
     188           9 :     WHEN("Base reference points to Derived1")
     189           9 :     {
     190           3 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     191             : 
     192           3 :         THEN("Casting to base is also fine")
     193           3 :         {
     194           1 :             REQUIRE_UNARY(is<Base>(*ptr));
     195           1 :             REQUIRE_UNARY(ptr);
     196           1 :         }
     197           3 :         THEN("Casting to Derived1 is fine")
     198           3 :         {
     199           1 :             REQUIRE_UNARY(is<Derived1>(*ptr));
     200           1 :             REQUIRE_UNARY(ptr);
     201           1 :         }
     202           3 :         THEN("Casting to Derived2 doesn't work")
     203           3 :         {
     204           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(*ptr));
     205           1 :             REQUIRE_UNARY(ptr);
     206           1 :         }
     207           3 :     }
     208             : 
     209           9 :     WHEN("unique_ptr to Base points to Derived1")
     210           9 :     {
     211           3 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     212             : 
     213           3 :         THEN("Check is<Base> returns true")
     214           3 :         {
     215           1 :             REQUIRE_UNARY(is<Base>(ptr));
     216           1 :             REQUIRE_UNARY(ptr);
     217           1 :         }
     218           3 :         THEN("Check is<Derived1> returns true")
     219           3 :         {
     220           1 :             REQUIRE_UNARY(is<Derived1>(ptr));
     221           1 :             REQUIRE_UNARY(ptr);
     222           1 :         }
     223           3 :         THEN("Check is<Derived2> returns false")
     224           3 :         {
     225           1 :             REQUIRE_UNARY_FALSE(is<Derived2>(ptr));
     226           1 :             REQUIRE_UNARY(ptr);
     227           1 :         }
     228           3 :     }
     229           9 : }
     230             : 
     231             : TEST_CASE("TypeCasts: Testing downcast")
     232          12 : {
     233          12 :     struct Base {
     234          12 :         virtual ~Base() = default;
     235          12 :     };
     236          12 :     struct Derived1 final : Base {
     237          12 :         ~Derived1() override = default;
     238          12 :     };
     239          12 :     struct Derived2 final : public Base {
     240          12 :         ~Derived2() override = default;
     241          12 :     };
     242             : 
     243          12 :     WHEN("Base pointer points to Derived1")
     244          12 :     {
     245           4 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     246             : 
     247           4 :         THEN("Downcasting to Base works")
     248           4 :         {
     249           1 :             REQUIRE_UNARY(downcast<Base>(ptr.get()));
     250           1 :             REQUIRE_UNARY(ptr);
     251           1 :         }
     252           4 :         THEN("Downcasting to Derived1 works")
     253           4 :         {
     254           1 :             REQUIRE_UNARY(downcast<Derived1>(ptr.get()));
     255           1 :             REQUIRE_UNARY(ptr);
     256           1 :         }
     257           4 :         THEN("Safely downcasting to Derived1 works")
     258           4 :         {
     259           1 :             REQUIRE_UNARY(downcast_safe<Derived1>(ptr.get()));
     260           1 :             REQUIRE_UNARY(ptr);
     261           1 :         }
     262           4 :         THEN("Safely downcasting to Derived2 doesn't work")
     263           4 :         {
     264           1 :             REQUIRE_UNARY_FALSE(downcast_safe<Derived2>(ptr.get()));
     265           1 :             REQUIRE_UNARY(ptr);
     266           1 :         }
     267           4 :     }
     268             : 
     269          12 :     WHEN("Base reference points to Derived1")
     270          12 :     {
     271           4 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     272             : 
     273           4 :         THEN("Downcasting to Base works")
     274           4 :         {
     275           1 :             REQUIRE_NOTHROW(downcast<Base>(*ptr));
     276           1 :             REQUIRE_UNARY(ptr);
     277           1 :         }
     278           4 :         THEN("Downcasting to Derived1 works")
     279           4 :         {
     280           1 :             REQUIRE_NOTHROW(downcast<Derived1>(*ptr));
     281           1 :             REQUIRE_UNARY(ptr);
     282           1 :         }
     283           4 :         THEN("Safely downcasting to Derived1 works")
     284           4 :         {
     285           1 :             REQUIRE_NOTHROW(downcast_safe<Derived1>(*ptr));
     286           1 :             REQUIRE_UNARY(ptr);
     287           1 :         }
     288           4 :         THEN("Safely downcasting to Derived2 throws")
     289           4 :         {
     290           1 :             REQUIRE_THROWS(downcast_safe<Derived2>(*ptr));
     291           1 :             REQUIRE_UNARY(ptr);
     292           1 :         }
     293           4 :     }
     294             : 
     295          12 :     WHEN("unique_ptr to Base points to Derived1")
     296          12 :     {
     297           4 :         std::unique_ptr<Base> ptr = std::make_unique<Derived1>();
     298             : 
     299           4 :         THEN("Downcasting to base works")
     300           4 :         {
     301           1 :             REQUIRE_UNARY(downcast<Base>(std::move(ptr)));
     302           1 :             REQUIRE_UNARY_FALSE(ptr);
     303           1 :         }
     304           4 :         THEN("Downcasting to Derived1 works")
     305           4 :         {
     306           1 :             REQUIRE_UNARY(downcast<Derived1>(std::move(ptr)));
     307           1 :             REQUIRE_UNARY_FALSE(ptr);
     308           1 :         }
     309           4 :         THEN("Safely downcasting to Derived1 works")
     310           4 :         {
     311           1 :             REQUIRE_UNARY(downcast_safe<Derived1>(std::move(ptr)));
     312           1 :             REQUIRE_UNARY_FALSE(ptr);
     313           1 :         }
     314           4 :         THEN("Safely downcasting to Derived2 doesn't work")
     315           4 :         {
     316           1 :             REQUIRE_UNARY_FALSE(downcast_safe<Derived2>(std::move(ptr)));
     317           1 :             REQUIRE_UNARY(ptr);
     318           1 :         }
     319           4 :     }
     320          12 : }
     321             : 
     322             : TEST_SUITE_END();

Generated by: LCOV version 1.14