133 lines
4 KiB
C++
133 lines
4 KiB
C++
//==================================================================================================
|
|
/*
|
|
ROTGEN - Runtime Overlay for Eigen
|
|
Copyright : CODE RECKONS
|
|
SPDX-License-Identifier: BSL-1.0
|
|
*/
|
|
//==================================================================================================
|
|
#include "unit/tests.hpp"
|
|
#include <rotgen/rotgen.hpp>
|
|
|
|
// Helper: fill matrix from raw data
|
|
// NB: This function must not be turned into a lambda, otherwise
|
|
// `test-ubuntu-gcc-release` will not pass and the CI will fail.
|
|
// This is likely due to a compilation bug from GCC 13.3.0.
|
|
void fill(auto &m, int r, int c, auto data[]) {
|
|
for (int k = 0; k < r * c; ++k)
|
|
m.data()[k] = data[k];
|
|
}
|
|
|
|
TTS_CASE_TPL("Function size", rotgen::tests::types)
|
|
<typename T, typename O>(tts::type<tts::types<T, O>>)
|
|
{
|
|
T data[] = {1,2,3,4,5,6,7,8,9,10,11,12};
|
|
|
|
// 1x12 dynamic block at (0,0)
|
|
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> dm(1,12);
|
|
fill(dm,1,12,data);
|
|
auto b1 = rotgen::block<decltype(dm), rotgen::Dynamic, rotgen::Dynamic>(dm, 0,0,1,12);
|
|
TTS_EQUAL(b1.rows(), rotgen::Index{1});
|
|
TTS_EQUAL(b1.cols(), rotgen::Index{12});
|
|
|
|
// 1x5 dynamic block at (0,2)
|
|
auto b2 = rotgen::block<decltype(dm), rotgen::Dynamic, rotgen::Dynamic>(dm, 0,2,1,5);
|
|
TTS_EQUAL(b2.rows(), rotgen::Index{1});
|
|
TTS_EQUAL(b2.cols(), rotgen::Index{5});
|
|
|
|
// 3x2 dynamic block at (1,4) in 4x6
|
|
rotgen::matrix<T, rotgen::Dynamic, rotgen::Dynamic, O::value> dm2(4,6);
|
|
fill(dm2,4,6,data);
|
|
auto b3 = rotgen::block<decltype(dm2), rotgen::Dynamic, rotgen::Dynamic>(dm2, 1,4,3,2);
|
|
TTS_EQUAL(b3.rows(), rotgen::Index{3});
|
|
TTS_EQUAL(b3.cols(), rotgen::Index{2});
|
|
TTS_EQUAL(b3.size(), rotgen::Index{6});
|
|
|
|
// 3x4 static block
|
|
rotgen::matrix<T,3,4,O::value> sm;
|
|
fill(sm,3,4,data);
|
|
auto b4 = rotgen::block<decltype(sm), 3, 4>(sm, 0,0);
|
|
TTS_EQUAL(b4.rows(), rotgen::Index{3});
|
|
TTS_EQUAL(b4.cols(), rotgen::Index{4});
|
|
TTS_EQUAL(b4.size(), rotgen::Index{12});
|
|
|
|
// 6x2 static block
|
|
rotgen::matrix<T,6,2,O::value> sm2;
|
|
fill(sm2,6,2,data);
|
|
auto b5 = rotgen::block<decltype(sm2), 6, 2>(sm2, 0,0);
|
|
TTS_EQUAL(b5.rows(), rotgen::Index{6});
|
|
TTS_EQUAL(b5.cols(), rotgen::Index{2});
|
|
TTS_EQUAL(b5.size(), rotgen::Index{12});
|
|
};
|
|
|
|
TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
|
|
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
|
{
|
|
using base = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
|
|
|
T data[] = {1,2,3,4,5,6,7,8,9,10,11,12};
|
|
base mat(4,3);
|
|
for(int k=0;k<12;++k) mat.data()[k] = data[k];
|
|
|
|
auto b = rotgen::block<base,rotgen::Dynamic,rotgen::Dynamic>(mat, 0, 0, 4, 3);
|
|
for(rotgen::Index i=0;i<4;i++)
|
|
{
|
|
for(rotgen::Index j=0;j<3;j++)
|
|
{
|
|
if constexpr(O::value) TTS_EQUAL(b(i,j), data[j+3*i]);
|
|
else TTS_EQUAL(b(i,j), data[i+4*j]);
|
|
}
|
|
}
|
|
|
|
b(1, 1) = 42;
|
|
TTS_EQUAL(b(1,1), 42);
|
|
|
|
T& ref = b(2, 2);
|
|
ref = 17;
|
|
TTS_EQUAL(b(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 vs = [&]()
|
|
{
|
|
if constexpr(O::value == rotgen::ColMajor)
|
|
{
|
|
using base = rotgen::matrix<T,1,rotgen::Dynamic>;
|
|
base m(12);
|
|
for(int k=0;k<12;++k) m(k) = k+1;
|
|
return std::tuple{m,rotgen::block<base,1,rotgen::Dynamic>(m, 0, 0, 1, 12)};
|
|
}
|
|
else
|
|
{
|
|
using base = rotgen::matrix<T,rotgen::Dynamic,1>;
|
|
base m(12);
|
|
for(int k=0;k<12;++k) m(k) = k+1;
|
|
return std::tuple{m,rotgen::block<base,rotgen::Dynamic,1>(m, 0, 0, 12, 1)};
|
|
}
|
|
}();
|
|
|
|
auto mat = get<0>(vs);
|
|
auto b = get<1>(vs);
|
|
TTS_EXPECT(b.IsVectorAtCompileTime);
|
|
|
|
for(rotgen::Index i=0;i<b.size();i++)
|
|
TTS_EQUAL(b(i), mat(i)) << "Index: " << i << "\n";
|
|
|
|
for(rotgen::Index i=0;i<b.size();i++)
|
|
TTS_EQUAL(b[i], mat(i)) << "Index: " << i << "\n";
|
|
|
|
b(1) = 42;
|
|
TTS_EQUAL(mat(1), 42);
|
|
|
|
T& ref = b(2);
|
|
ref = 17;
|
|
TTS_EQUAL(mat(2), 17);
|
|
|
|
b[1] = 77;
|
|
TTS_EQUAL(mat(1), 77);
|
|
|
|
T& bref = b[3];
|
|
bref = 331;
|
|
TTS_EQUAL(mat(3), 331);
|
|
};
|