rotgen/test/unit/matrix/basic_api.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

151 lines
4.6 KiB
C++

//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#include <rotgen/rotgen.hpp>
#include "unit/tests.hpp"
TTS_CASE_TPL("Function size", rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> empty_matrix;
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> matrix(3, 4);
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> row_vector(9,
1);
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> column_vector(
1, 5);
TTS_EQUAL(empty_matrix.size(), rotgen::Index{0});
TTS_EQUAL(matrix.size(), rotgen::Index{12});
TTS_EQUAL(row_vector.size(), rotgen::Index{9});
TTS_EQUAL(column_vector.size(), rotgen::Index{5});
};
TTS_CASE_TPL("Resizing dynamic matrix",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> a(2, 3);
for (rotgen::Index r = 0; r < a.rows(); ++r)
for (rotgen::Index c = 0; c < a.cols(); ++c) a(r, c) = 42 + 2 * c + r;
rotgen::resize(a, 3, 2);
TTS_EQUAL(a.rows(), rotgen::Index(3));
TTS_EQUAL(a.cols(), rotgen::Index(2));
for (rotgen::Index r = 0; r < a.rows(); ++r)
for (rotgen::Index c = 0; c < a.cols(); ++c) TTS_GREATER(a(r, c), 0);
rotgen::resize(a, 2, 2);
TTS_EQUAL(a.rows(), rotgen::Index(2));
TTS_EQUAL(a.cols(), rotgen::Index(2));
};
TTS_CASE_TPL("Dynamix matrix conservative resizing",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> a(2, 3);
int i = 1;
for (rotgen::Index r = 0; r < a.rows(); ++r)
for (rotgen::Index c = 0; c < a.cols(); ++c) a(r, c) = i++;
rotgen::conservativeResize(a, 2, 3);
TTS_EQUAL(a.rows(), rotgen::Index(2));
TTS_EQUAL(a.cols(), rotgen::Index(3));
i = 1;
for (rotgen::Index r = 0; r < a.rows(); ++r)
for (rotgen::Index c = 0; c < a.cols(); ++c) TTS_EQUAL(a(r, c), i++);
rotgen::conservativeResize(a, 3, 2);
TTS_EQUAL(a.rows(), rotgen::Index(3));
TTS_EQUAL(a.cols(), rotgen::Index(2));
int expected[3][2] = {{1, 2}, {4, 5}};
for (rotgen::Index r = 0; r < 2; ++r)
for (rotgen::Index c = 0; c < 2; ++c) TTS_EQUAL(a(r, c), expected[r][c]);
rotgen::conservativeResize(a, 4, 4);
TTS_EQUAL(a.rows(), rotgen::Index(4));
TTS_EQUAL(a.cols(), rotgen::Index(4));
TTS_EQUAL(a(0, 0), 1);
TTS_EQUAL(a(3, 3), 0);
rotgen::conservativeResize(a, 2, 2);
TTS_EQUAL(a.rows(), rotgen::Index(2));
TTS_EQUAL(a.cols(), rotgen::Index(2));
TTS_EQUAL(a(0, 0), 1);
TTS_EQUAL(a(1, 1), 5);
rotgen::conservativeResize(a, 1, 2);
TTS_EQUAL(a.rows(), rotgen::Index(1));
TTS_EQUAL(a.cols(), rotgen::Index(2));
TTS_EQUAL(a(0, 0), 1);
TTS_EQUAL(a(0, 1), 2);
rotgen::conservativeResize(a, 0, 0);
TTS_EQUAL(a.rows(), rotgen::Index(0));
TTS_EQUAL(a.cols(), rotgen::Index(0));
rotgen::conservativeResize(a, 3, 3);
TTS_EQUAL(a.rows(), rotgen::Index(3));
TTS_EQUAL(a.cols(), rotgen::Index(3));
};
TTS_CASE_TPL("Test coefficient accessors",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> a(3, 5);
for (rotgen::Index r = 0; r < a.rows(); ++r)
for (rotgen::Index c = 0; c < a.cols(); ++c) a(r, c) = r + 2 * c + 3;
TTS_EQUAL(a(0, 0), 3);
TTS_EQUAL(a(1, 1), 6);
TTS_EQUAL(a(1, 0), 4);
TTS_EQUAL(a(2, 2), 9);
TTS_EQUAL(a(2, 4), 13);
a(1, 1) = 42;
TTS_EQUAL(a(1, 1), 42);
T& ref = a(2, 2);
ref = 17;
TTS_EQUAL(a(2, 2), 17);
};
TTS_CASE_TPL("Test one index coefficient accessors",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
auto a = [&]() {
if constexpr (O::value == rotgen::ColMajor)
return rotgen::matrix<T, 1, rotgen::Dynamic>(1, 8);
else return rotgen::matrix<T, rotgen::Dynamic, 1>(8, 1);
}();
TTS_EXPECT(a.IsVectorAtCompileTime);
for (rotgen::Index s = 0; s < a.size(); ++s) a(s) = s + 1;
int i = 1;
for (rotgen::Index s = 0; s < a.size(); ++s) TTS_EQUAL(a(s), i++) << a;
i = 1;
for (rotgen::Index s = 0; s < a.size(); ++s) TTS_EQUAL(a[s], i++) << a;
T& ref = a(2);
ref = 999.5;
TTS_EQUAL(a(2), 999.5);
T& bref = a[3];
bref = 42.5;
TTS_EQUAL(a[3], 42.5);
};