First batch of rotgen implementation: constructors, basic infrastructure

This commit is contained in:
Karen Kaspar 2025-05-16 16:01:17 +02:00 committed by Joel Falcou
parent 2554a83890
commit afd504d679
9 changed files with 428 additions and 10 deletions

175
test/basic/constructors.cpp Normal file
View file

@ -0,0 +1,175 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define TTS_MAIN
#include <rotgen/matrix.hpp>
#include "tts.hpp"
TTS_CASE("Default matrix dynamic constructor")
{
rotgen::matrix<double> matrix;
TTS_EQUAL(matrix.rows(), 0ULL);
TTS_EQUAL(matrix.cols(), 0ULL);
};
TTS_CASE("Default matrix static constructor")
{
rotgen::matrix<double, 4, 9> matrix;
TTS_EQUAL(matrix.rows(), 4ULL);
TTS_EQUAL(matrix.cols(), 9ULL);
};
TTS_CASE("Dynamic matrix constructor with row and columns")
{
rotgen::matrix<float> matrix(10, 5);
TTS_EQUAL(matrix.rows(), 10ULL);
TTS_EQUAL(matrix.cols(), 5ULL);
};
TTS_CASE("Static matrix constructor with row and columns")
{
rotgen::matrix<double, 6, 11> matrix(6, 11);
TTS_EQUAL(matrix.rows(), 6ULL);
TTS_EQUAL(matrix.cols(), 11ULL);
};
TTS_CASE("Copy constructor produces identical but independent matrix")
{
rotgen::matrix<double> a(3, 3);
for(std::size_t r=0;r<a.rows();r++)
for(std::size_t c=0;c<a.cols();c++)
a(r,c) = r + c;
rotgen::matrix<double> b = a;
TTS_EQUAL(b.rows(), a.rows());
TTS_EQUAL(b.cols(), a.cols());
for(std::size_t r=0;r<a.rows();r++)
for(std::size_t c=0;c<a.cols();c++)
TTS_EQUAL(b(r,c), a(r,c));
TTS_EQUAL(b(0, 0), 0.0);
TTS_EQUAL(b(1, 0), 1.0);
TTS_EQUAL(b(2, 1), 3.0);
a(0,0) = 42.0;
TTS_NOT_EQUAL(b(0,0), a(0,0));
};
TTS_CASE("Copy constructor on default matrix")
{
rotgen::matrix<double> a;
rotgen::matrix<double> b = a;
TTS_EQUAL(b.rows(), 0ULL);
TTS_EQUAL(b.cols(), 0ULL);
};
TTS_CASE("Copy constructor from const matrix")
{
const rotgen::matrix<double> a(2, 2);
auto b = a;
TTS_EQUAL(b.rows(), 2ULL);
TTS_EQUAL(b.cols(), 2ULL);
};
TTS_CASE("Copy constructor on static matrix")
{
rotgen::matrix<double, 2, 5> a;
rotgen::matrix<double, 2, 5> b = a;
TTS_EQUAL(b.rows(), 2ULL);
TTS_EQUAL(b.cols(), 5ULL);
};
/*
TTS_CASE("Copy constructor on static/dynamic matrix")
{
rotgen::matrix<double, 11, 4> a;
rotgen::matrix<double> b = a;
TTS_EQUAL(b.rows(), 11);
TTS_EQUAL(b.cols(), 4);
};
TTS_CASE("Copy constructor on dynamic/static matrix")
{
rotgen::matrix<double> a(5, 7);
rotgen::matrix<double, 5, 7> b = a;
TTS_EQUAL(b.rows(), 5);
TTS_EQUAL(b.cols(), 7);
};*/
TTS_CASE("Move constructor transfers contents")
{
rotgen::matrix<double> a(3, 3);
a(1,1) = 7;
auto ptr = a.data();
rotgen::matrix<double> b = std::move(a);
TTS_EQUAL(b(1,1), 7);
TTS_EQUAL(b.rows(), 3ULL);
TTS_EQUAL(b.cols(), 3ULL);
TTS_EXPECT(b.data() == ptr);
// TTS_EXPECT(a.data() == nullptr);
};
TTS_CASE("Move constructor from Rvalue")
{
rotgen::matrix<double> b = rotgen::matrix<double>(2, 2);
TTS_EQUAL(b.rows(), 2ULL);
TTS_EQUAL(b.cols(), 2ULL);
};
TTS_CASE("Constructor from Initializer list")
{
rotgen::matrix<double,1,1> b1 = {3.5};
TTS_EQUAL(b1.rows(), 1ULL);
TTS_EQUAL(b1.cols(), 1ULL);
TTS_EQUAL(b1(0) , 3.5);
rotgen::matrix<double,5,1> b9 = {0.01, 0.1, 1, 10, 100};
TTS_EQUAL(b9.rows(), 5ULL);
TTS_EQUAL(b9.cols(), 1ULL);
int i = 0.01;
for(std::size_t r=0;r<b9.rows();++r) {
TTS_EQUAL(b9(r,1), i);
i *= 10;
}
rotgen::matrix<double,1,3> b13 = {1.2,2.3,3.4};
TTS_EQUAL(b13.rows(), 1ULL);
TTS_EQUAL(b13.cols(), 3ULL);
TTS_EQUAL(b13(0) , 1.2);
TTS_EQUAL(b13(1) , 2.3);
TTS_EQUAL(b13(2) , 3.4);
};
TTS_CASE("Constructor from Initializer list of rows")
{
rotgen::matrix<double> b1 = {{3.5}};
TTS_EQUAL(b1.rows(), 1ULL);
TTS_EQUAL(b1.cols(), 1ULL);
TTS_EQUAL(b1(0,0) , 3.5);
rotgen::matrix<double> b23 = { {1.2,2.3,3.4}
, {10,200,3000}
};
TTS_EQUAL(b23.rows(), 2ULL);
TTS_EQUAL(b23.cols(), 3ULL);
TTS_EQUAL(b23(0,0) , 1.2);
TTS_EQUAL(b23(0,1) , 2.3);
TTS_EQUAL(b23(0,2) , 3.4);
TTS_EQUAL(b23(1,0) , 10);
TTS_EQUAL(b23(1,1) , 200);
TTS_EQUAL(b23(1,2) , 3000);
};

167
test/basic/functions.cpp Normal file
View file

@ -0,0 +1,167 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define TTS_MAIN
#include <rotgen/matrix.hpp>
#include "tts.hpp"
TTS_CASE("Function size")
{
rotgen::matrix<double> empty_matrix;
rotgen::matrix<double> matrix(3,4);
rotgen::matrix<double> row_vector(9,1);
rotgen::matrix<double> column_vector(1,5);
TTS_EQUAL(empty_matrix.size(), 0ULL);
TTS_EQUAL(matrix.size(), 12ULL);
TTS_EQUAL(row_vector.size(), 9ULL);
TTS_EQUAL(column_vector.size(), 5ULL);
};
TTS_CASE("Resizing dynamic matrix")
{
rotgen::matrix<double> a(2, 3);
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
a(r,c) = 42 + 2*c + r;
a.resize(3, 2);
TTS_EQUAL(a.rows(), 3ULL);
TTS_EQUAL(a.cols(), 2ULL);
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
TTS_GREATER(a(r,c),0);
a.resize(2,2);
TTS_EQUAL(a.rows(), 2ULL);
TTS_EQUAL(a.cols(), 2ULL);
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
TTS_EQUAL(a(r,c),0);
};
TTS_CASE("Resizing static matrix")
{
rotgen::matrix<double, 3, 3> a;
TTS_EXPECT_NOT_COMPILES(a, { a.resize(4, 5); });
};
TTS_CASE("Dynamix matrix conservative resizing")
{
rotgen::matrix<double> a(2, 3);
int i = 1;
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
a(r, c) = i++;
a.conservativeResize(2, 3);
TTS_EQUAL(a.rows(), 2ULL);
TTS_EQUAL(a.cols(), 3ULL);
i = 1;
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
TTS_EQUAL(a(r,c),i++);
a.conservativeResize(3, 2);
TTS_EQUAL(a.rows(), 3ULL);
TTS_EQUAL(a.cols(), 2ULL);
int expected[3][2] = { {1, 2}, {4, 5}, {0, 0} };
for(std::size_t r = 0; r < 3; ++r)
for(std::size_t c = 0; c < 2; ++c)
TTS_EQUAL(a(r, c), expected[r][c]);
a.conservativeResize(4, 4);
TTS_EQUAL(a.rows(), 4ULL);
TTS_EQUAL(a.cols(), 4ULL);
TTS_EQUAL(a(0,0), 1);
TTS_EQUAL(a(3,3), 0);
a.conservativeResize(2, 2);
TTS_EQUAL(a.rows(), 2ULL);
TTS_EQUAL(a.cols(), 2ULL);
TTS_EQUAL(a(0,0), 1);
TTS_EQUAL(a(1,1), 5);
a.conservativeResize(1, 2);
TTS_EQUAL(a.rows(), 1ULL);
TTS_EQUAL(a.cols(), 2ULL);
TTS_EQUAL(a(0,0), 1);
TTS_EQUAL(a(0,1), 2);
a.conservativeResize(0, 0);
TTS_EQUAL(a.rows(), 0ULL);
TTS_EQUAL(a.cols(), 0ULL);
a.conservativeResize(3, 3);
TTS_EQUAL(a.rows(), 3ULL);
TTS_EQUAL(a.cols(), 3ULL);
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t c=0;c<a.cols();++c)
TTS_EQUAL(a(r,c),0);
};
TTS_CASE("Static matrix conservative resizing")
{
rotgen::matrix<double, 3, 3> a;
TTS_EXPECT_NOT_COMPILES(a, { a.conservativeResize(4, 5); });
};
TTS_CASE("Test coefficient accessors")
{
rotgen::matrix<double> a(3, 5);
for(std::size_t r=0;r<a.rows();++r)
for(std::size_t 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);
double& ref = a(2, 2);
ref = 17;
assert(a(2, 2) == 17);
};
TTS_CASE("Test one index coefficient accessors")
{
rotgen::matrix<double> a(2, 4);
int i = 1;
for(std::size_t c=0;c<a.cols();++c)
for(std::size_t r=0;r<a.rows();++r)
a(r, c) = i++;
i = 1;
for(std::size_t r=0;r<a.rows()*a.cols();++r)
TTS_EQUAL(a(r), i++);
double& ref = a(2);
ref = 999.1;
assert(a(2) == 999.1);
rotgen::matrix<double, 2, 3> b (2, 3);
i = 1;
for(std::size_t c=0;c<b.cols()*b.rows(); ++c)
a(c) = i++;
i = 1;
for(std::size_t c=0;c<b.cols();++c)
for(std::size_t r=0;r<b.rows();++r)
TTS_EQUAL(a(r, c), i++);
};

View file

@ -14,9 +14,9 @@ TTS_CASE("Check operator*")
rotgen::matrix<double> a(2,2);
rotgen::matrix<double> ref(2,2);
for(int r=0;r<a.rows();r++)
for(std::size_t r=0;r<a.rows();r++)
{
for(int c=0;c<a.cols();c++)
for(std::size_t c=0;c<a.cols();c++)
{
a(r,c) = (1+c) + 10*(1+r);
ref(r,c) = ((1+c) + 10*(1+r)) * 10.5;