rotgen/test/unit/block/generators.cpp
Joel Falcou c400650f1a Implements rotgen::quaternion
Closes #19

Co-authored-by: Jules Pénuchot <jules@penuchot.com>
2025-11-09 19:07:20 +01:00

279 lines
9.2 KiB
C++

//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#include <rotgen/rotgen.hpp>
#include "rotgen/functions/functions.hpp"
#include "unit/common/references.hpp"
#include "unit/tests.hpp"
void test_value(auto const& matrix,
auto value,
rotgen::Index i0,
rotgen::Index j0,
rotgen::Index rows,
rotgen::Index cols)
{
for (rotgen::Index r = 0; r < rows; ++r)
for (rotgen::Index c = 0; c < cols; ++c)
TTS_EQUAL(matrix(i0 + r, j0 + c), value);
}
void test_identity(auto const& matrix,
rotgen::Index i0,
rotgen::Index j0,
rotgen::Index rows,
rotgen::Index cols)
{
for (rotgen::Index r = 0; r < rows; ++r)
for (rotgen::Index c = 0; c < cols; ++c)
TTS_EQUAL(matrix(i0 + r, j0 + c), r == c ? 1 : 0);
}
void test_random(auto const& matrix,
rotgen::Index i0,
rotgen::Index j0,
rotgen::Index rows,
rotgen::Index cols)
{
for (rotgen::Index r = 0; r < rows; ++r)
for (rotgen::Index c = 0; c < cols; ++c)
{
TTS_GREATER_EQUAL(matrix(i0 + r, j0 + c), -1.0);
TTS_LESS_EQUAL(matrix(i0 + r, j0 + c), 1.0);
}
}
TTS_CASE_TPL("Test dynamic block::setZero",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_block_references<T, O>();
for (auto const& [matrix_desc, i0, j0, ni, nj] : cases)
{
auto [rows, cols, fn] = matrix_desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract(m, i0, j0, ni, nj);
rotgen::setZero(input);
test_value(m, T{0}, i0, j0, ni, nj);
using input_type = decltype(rotgen::extract(m, i0, j0, ni, nj));
auto values = input_type::Zero(ni, nj);
test_value(values, T{0}, 0, 0, ni, nj);
}
};
TTS_CASE_TPL("Test static block::setZero",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_static_block_references<T, O>();
auto process = []<typename D>(D const& d) {
auto [desc, i0, j0] = d;
auto [rows, cols, fn] = desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract<D::ni, D::nj>(m, i0, j0);
rotgen::setZero(input);
test_value(m, T{0}, i0, j0, D::ni, D::nj);
using input_type = decltype(rotgen::extract<D::ni, D::nj>(m, i0, j0));
auto values = input_type::Zero();
test_value(values, T{0}, 0, 0, D::ni, D::nj);
};
std::apply([&](auto const&... d) { (process(d), ...); }, cases);
};
TTS_CASE_TPL("Test dynamic block::setOnes",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_block_references<T, O>();
for (auto const& [matrix_desc, i0, j0, ni, nj] : cases)
{
auto [rows, cols, fn] = matrix_desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract(m, i0, j0, ni, nj);
rotgen::setOnes(input);
test_value(m, T{1}, i0, j0, ni, nj);
using input_type = decltype(rotgen::extract(m, i0, j0, ni, nj));
auto values = input_type::Ones(ni, nj);
test_value(values, T{1}, 0, 0, ni, nj);
}
};
TTS_CASE_TPL("Test static block::setOnes",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_static_block_references<T, O>();
auto process = []<typename D>(D const& d) {
auto [desc, i0, j0] = d;
auto [rows, cols, fn] = desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract<D::ni, D::nj>(m, i0, j0);
rotgen::setOnes(input);
test_value(m, T{1}, i0, j0, D::ni, D::nj);
using input_type = decltype(rotgen::extract<D::ni, D::nj>(m, i0, j0));
auto values = input_type::Ones();
test_value(values, T{1}, 0, 0, D::ni, D::nj);
};
std::apply([&](auto const&... d) { (process(d), ...); }, cases);
};
TTS_CASE_TPL("Test dynamic block::setConstant",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_block_references<T, O>();
for (auto const& [matrix_desc, i0, j0, ni, nj] : cases)
{
auto [rows, cols, fn] = matrix_desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract(m, i0, j0, ni, nj);
rotgen::setConstant(input, T{13.37f});
test_value(m, T{13.37f}, i0, j0, ni, nj);
using input_type = decltype(rotgen::extract(m, i0, j0, ni, nj));
auto values = input_type::Constant(ni, nj, T{13.37f});
test_value(values, T{13.37f}, 0, 0, ni, nj);
}
};
TTS_CASE_TPL("Test static block::setConstant",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_static_block_references<T, O>();
auto process = []<typename D>(D const& d) {
auto [desc, i0, j0] = d;
auto [rows, cols, fn] = desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract<D::ni, D::nj>(m, i0, j0);
rotgen::setConstant(input, T{13.37f});
test_value(m, T{13.37f}, i0, j0, D::ni, D::nj);
using input_type = decltype(rotgen::extract<D::ni, D::nj>(m, i0, j0));
auto values = input_type::Constant(T{13.37f});
test_value(values, T{13.37f}, 0, 0, D::ni, D::nj);
};
std::apply([&](auto const&... d) { (process(d), ...); }, cases);
};
TTS_CASE_TPL("Test dynamic block::setIdentity",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_block_references<T, O>();
for (auto const& [matrix_desc, i0, j0, ni, nj] : cases)
{
auto [rows, cols, fn] = matrix_desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract(m, i0, j0, ni, nj);
rotgen::setIdentity(input);
test_identity(m, i0, j0, ni, nj);
using input_type = decltype(rotgen::extract(m, i0, j0, ni, nj));
auto values = input_type::Identity(ni, nj);
test_identity(values, 0, 0, ni, nj);
}
};
TTS_CASE_TPL("Test static block::setIdentity",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_static_block_references<T, O>();
auto process = []<typename D>(D const& d) {
auto [desc, i0, j0] = d;
auto [rows, cols, fn] = desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract<D::ni, D::nj>(m, i0, j0);
rotgen::setIdentity(input);
test_identity(m, i0, j0, D::ni, D::nj);
using input_type = decltype(rotgen::extract<D::ni, D::nj>(m, i0, j0));
auto values = input_type::Identity();
test_identity(values, 0, 0, D::ni, D::nj);
};
std::apply([&](auto const&... d) { (process(d), ...); }, cases);
};
TTS_CASE_TPL("Test dynamic block::setRandom",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_block_references<T, O>();
for (auto const& [matrix_desc, i0, j0, ni, nj] : cases)
{
auto [rows, cols, fn] = matrix_desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract(m, i0, j0, ni, nj);
rotgen::setRandom(input);
test_random(m, i0, j0, ni, nj);
using input_type = decltype(rotgen::extract(m, i0, j0, ni, nj));
auto values = input_type::Random(ni, nj);
test_random(values, 0, 0, ni, nj);
}
};
TTS_CASE_TPL("Test static block::setRandom",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto const cases = rotgen::tests::generate_static_block_references<T, O>();
auto process = []<typename D>(D const& d) {
auto [desc, i0, j0] = d;
auto [rows, cols, fn] = desc;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> m(rows, cols);
rotgen::tests::prepare(rows, cols, fn, m);
auto input = rotgen::extract<D::ni, D::nj>(m, i0, j0);
rotgen::setRandom(input);
test_random(m, i0, j0, D::ni, D::nj);
using input_type = decltype(rotgen::extract<D::ni, D::nj>(m, i0, j0));
auto values = input_type::Random();
test_random(values, 0, 0, D::ni, D::nj);
};
std::apply([&](auto const&... d) { (process(d), ...); }, cases);
};