Commit e9e9a882 authored by Jens Petit's avatar Jens Petit Committed by Tobias Lasser

Fix undefined behavior in tests

parent 3c8ba22f
Pipeline #227562 passed with stages
in 33 minutes and 59 seconds
...@@ -199,7 +199,9 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Assigning to DataHandlerMap", "", (DataHan ...@@ -199,7 +199,9 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Assigning to DataHandlerMap", "", (DataHan
THEN("a deep copy is performed") THEN("a deep copy is performed")
{ {
const TestType dh2{size}; Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec{size};
randVec.setRandom();
const TestType dh2{randVec};
const auto dh2Map = dh2.getBlock(0, size); const auto dh2Map = dh2.getBlock(0, size);
const auto& dh2MapRef = const auto& dh2MapRef =
static_cast<const typename MapToHandler<TestType>::map&>(*dh2Map); static_cast<const typename MapToHandler<TestType>::map&>(*dh2Map);
...@@ -667,8 +669,7 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the reduction operations of DataHa ...@@ -667,8 +669,7 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the reduction operations of DataHa
WHEN("putting in some random data") WHEN("putting in some random data")
{ {
Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec(size * 2); auto randVec = generateRandomMatrix<data_t>(size * 2);
randVec.setRandom();
TestType realDh(randVec); TestType realDh(randVec);
auto dhPtr = realDh.getBlock(size / 3, size); auto dhPtr = realDh.getBlock(size / 3, size);
auto& dh = *dhPtr; auto& dh = *dhPtr;
...@@ -683,8 +684,7 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the reduction operations of DataHa ...@@ -683,8 +684,7 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the reduction operations of DataHa
REQUIRE(checkSameNumbers(dh.squaredL2Norm(), REQUIRE(checkSameNumbers(dh.squaredL2Norm(),
randVec.middleRows(size / 3, size).squaredNorm())); randVec.middleRows(size / 3, size).squaredNorm()));
Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec2(size); auto randVec2 = generateRandomMatrix<data_t>(size);
randVec2.setRandom();
TestType realDh2(randVec2); TestType realDh2(randVec2);
auto dh2Ptr = realDh2.getBlock(0, size); auto dh2Ptr = realDh2.getBlock(0, size);
auto& dh2 = *dh2Ptr; auto& dh2 = *dh2Ptr;
...@@ -727,9 +727,9 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the element-wise operations of Dat ...@@ -727,9 +727,9 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the element-wise operations of Dat
WHEN("putting in some random data") WHEN("putting in some random data")
{ {
Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec(size); auto randVec = generateRandomMatrix<data_t>(size);
randVec.setRandom();
TestType realDh(randVec); TestType realDh(randVec);
auto dhPtr = realDh.getBlock(0, size); auto dhPtr = realDh.getBlock(0, size);
auto& dh = static_cast<typename MapToHandler<TestType>::map&>(*dhPtr); auto& dh = static_cast<typename MapToHandler<TestType>::map&>(*dhPtr);
...@@ -746,8 +746,8 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the element-wise operations of Dat ...@@ -746,8 +746,8 @@ TEMPLATE_PRODUCT_TEST_CASE("Scenario: Testing the element-wise operations of Dat
auto oldDhPtr = realOldDh.getBlock(0, size); auto oldDhPtr = realOldDh.getBlock(0, size);
auto& oldDh = static_cast<typename MapToHandler<TestType>::map&>(*oldDhPtr); auto& oldDh = static_cast<typename MapToHandler<TestType>::map&>(*oldDhPtr);
Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec2(size); auto randVec2 = generateRandomMatrix<data_t>(size);
randVec2.setRandom();
TestType dhCPU(randVec2); TestType dhCPU(randVec2);
auto dh2Ptr = dhCPU.getBlock(0, size); auto dh2Ptr = dhCPU.getBlock(0, size);
auto& dh2 = *dh2Ptr; auto& dh2 = *dh2Ptr;
......
...@@ -418,6 +418,7 @@ TEMPLATE_TEST_CASE("Scenario: Cloning DataHandler", "", DataHandlerCPU<float>) ...@@ -418,6 +418,7 @@ TEMPLATE_TEST_CASE("Scenario: Cloning DataHandler", "", DataHandlerCPU<float>)
{ {
index_t size = 728; index_t size = 728;
TestType dh(size); TestType dh(size);
dh = 1.0f;
WHEN("cloning") WHEN("cloning")
{ {
...@@ -432,7 +433,7 @@ TEMPLATE_TEST_CASE("Scenario: Cloning DataHandler", "", DataHandlerCPU<float>) ...@@ -432,7 +433,7 @@ TEMPLATE_TEST_CASE("Scenario: Cloning DataHandler", "", DataHandlerCPU<float>)
REQUIRE(dhClone->getSize() == dh.getSize()); REQUIRE(dhClone->getSize() == dh.getSize());
dh[0] = 1; dh[0] = 2.f;
REQUIRE(dh != *dhClone); REQUIRE(dh != *dhClone);
} }
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <type_traits> #include <type_traits>
#include <complex> #include <complex>
#include <random>
#include "elsaDefines.h" #include "elsaDefines.h"
/** /**
...@@ -33,3 +34,38 @@ bool checkSameNumbers(T left, T right, int epsilonFactor = 1) ...@@ -33,3 +34,38 @@ bool checkSameNumbers(T left, T right, int epsilonFactor = 1)
return Approx(left).epsilon(eps) == right; return Approx(left).epsilon(eps) == right;
} }
} }
/**
* \brief Generates a random Eigen matrix for different data_t types with integer values limited to
* a certain range
*
* \param[in] size the number of elements in the vector like matrix
*
* \tparam data_t the numerical type to use
*
* The integer range is chosen to be small, to allow multiplication with the values without running
* into overflow issues.
*/
template <typename data_t>
auto generateRandomMatrix(elsa::index_t size)
{
Eigen::Matrix<data_t, Eigen::Dynamic, 1> randVec(size);
if constexpr (std::is_integral_v<data_t>) {
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(-100, 100);
for (elsa::index_t i = 0; i < size; ++i) {
data_t num = distr(eng);
// remove zeros as this leads to errors when dividing
if (num == 0)
num = 1;
randVec[i] = num;
}
} else {
randVec.setRandom();
}
return randVec;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment