rotgen/test/basic/static_functions.cpp
Karen Kaspar ab8336bd5b [ADVANCED INITIALISATION] implemented and tested the static methods
Co-authored-by: kallore <kkaspar@codereckons.com>

See merge request oss/rotgen!4
2025-05-20 20:56:39 +02:00

96 lines
3.4 KiB
C++

//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define TTS_MAIN
#include <rotgen/matrix.hpp>
#include "tts.hpp"
template <typename MatrixType>
void test_zero(const MatrixType& matrix, std::size_t rows, std::size_t cols)
{
for(std::size_t r=0;r<rows;++r)
for(std::size_t c=0;c<cols;++c)
TTS_EQUAL(matrix(r, c), 0);
}
template <typename MatrixType>
void test_constant(const MatrixType& matrix, std::size_t rows, std::size_t cols, double constant)
{
for(std::size_t r=0;r<rows;++r)
for(std::size_t c=0;c<cols;++c)
TTS_EQUAL(matrix(r, c), constant);
constexpr double epsilon = 1e-10;
TTS_RELATIVE_EQUAL(rows * cols * constant, matrix.sum(), epsilon);
}
template <typename MatrixType>
void test_random(const MatrixType& matrix, std::size_t rows, std::size_t cols)
{
for(std::size_t r=0;r<rows;++r)
for(std::size_t c=0;c<cols;++c)
{
TTS_GREATER_EQUAL(matrix(r, c), -1.0);
TTS_LESS_EQUAL(matrix(r, c), 1.0);
}
}
template <typename MatrixType>
void test_identity(const MatrixType& matrix, std::size_t rows, std::size_t cols)
{
for(std::size_t r=0;r<rows;++r)
for(std::size_t c=0;c<cols;++c)
TTS_EQUAL(matrix(r, c), r==c ? 1 : 0);
std::size_t minimum = std::min(rows, cols);
TTS_EQUAL(matrix.trace(), minimum);
}
TTS_CASE("Test zero")
{
test_zero(rotgen::matrix<double, 3, 4>::Zero(), 3, 4);
test_zero(rotgen::matrix<double, 1, 1>::Zero(), 1, 1);
test_zero(rotgen::matrix<double, 10, 10>::Zero(), 10, 10);
test_zero(rotgen::matrix<double>::Zero(3, 4), 3, 4);
test_zero(rotgen::matrix<double, 7, 5>::Zero(7, 5), 7, 5);
test_zero(rotgen::matrix<double, 9>::Zero(9, 3), 9, 3);
test_zero(rotgen::matrix<double, -1, 3>::Zero(2, 3), 2, 3);
};
TTS_CASE("Test constant")
{
test_constant(rotgen::matrix<double, 3, 8>::Constant(5.12), 3, 8, 5.12);
test_constant(rotgen::matrix<double, 1, 1>::Constant(2.2), 1, 1, 2.2);
test_constant(rotgen::matrix<double, 11, 12>::Constant(13), 11, 12, 13);
test_constant(rotgen::matrix<double>::Constant(2, 7, 5.6), 2, 7, 5.6);
test_constant(rotgen::matrix<double, 2, 2>::Constant(2, 2, 2.0), 2, 2, 2.0);
test_constant(rotgen::matrix<double, 9>::Constant(9, 3, 1.1), 9, 3, 1.1);
test_constant(rotgen::matrix<double, -1, 9>::Constant(5, 9, 42), 5, 9, 42);
};
TTS_CASE("Test random")
{
test_random(rotgen::matrix<double, 2, 3>::Random(), 2, 3);
test_random(rotgen::matrix<double, 1, 1>::Random(), 1, 1);
test_random(rotgen::matrix<double, 11, 17>::Random(), 11, 17);
test_random(rotgen::matrix<double>::Random(7, 3), 7, 3);
test_random(rotgen::matrix<double, 2, 2>::Random(2, 2), 2, 2);
test_random(rotgen::matrix<double, 4, -1>::Random(4, 3), 4, 3);
test_random(rotgen::matrix<double, -1, 5>::Random(5, 5), 5, 5);
};
TTS_CASE("Test identity")
{
test_identity(rotgen::matrix<double, 4, 5>::Identity(), 4, 5);
test_identity(rotgen::matrix<double, 1, 1>::Identity(), 1, 1);
test_identity(rotgen::matrix<double, 21, 3>::Identity(), 21, 3);
test_identity(rotgen::matrix<double>::Identity(2, 7), 2, 7);
test_identity(rotgen::matrix<double, 2, 2>::Identity(2, 2), 2, 2);
test_identity(rotgen::matrix<double, 3>::Identity(3, 3), 3, 3);
test_identity(rotgen::matrix<double, -1, 11>::Identity(5, 11), 5, 11);
};