From 1f8663aad2fd4c30d1aa628c6b52d28b1faf4cf0 Mon Sep 17 00:00:00 2001 From: Joel FALCOU Date: Wed, 3 Sep 2025 12:38:04 +0200 Subject: [PATCH] Add UT for unary cwise --- test/unit/block/cwise.cpp | 42 +++++++++++++++++ test/unit/common/cwise.hpp | 93 ++++++++++++++++++++++++++++++++++++++ test/unit/map/cwise.cpp | 41 +++++++++++++++++ test/unit/matrix/cwise.cpp | 37 +++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 test/unit/block/cwise.cpp create mode 100644 test/unit/common/cwise.hpp create mode 100644 test/unit/map/cwise.cpp create mode 100644 test/unit/matrix/cwise.cpp diff --git a/test/unit/block/cwise.cpp b/test/unit/block/cwise.cpp new file mode 100644 index 0000000..d3892a6 --- /dev/null +++ b/test/unit/block/cwise.cpp @@ -0,0 +1,42 @@ +//================================================================================================== +/* + ROTGEN - Runtime Overlay for Eigen + Copyright : CODE RECKONS + SPDX-License-Identifier: BSL-1.0 +*/ +//================================================================================================== +#include "unit/tests.hpp" +#include "unit/common/cwise.hpp" +#include + +TTS_CASE_TPL("Test dynamic block cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_block_references(); + for (const auto& [matrix_desc, i0, j0, ni, nj] : cases) + { + auto[rows,cols,fn] = matrix_desc; + rotgen::matrix m(rows, cols); + rotgen::tests::prepare(rows,cols,fn,m); + auto input = rotgen::extract(m, i0, j0, ni, nj); + rotgen::tests::check_cwise_functions(input); + } +}; + +TTS_CASE_TPL("Test static block cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_static_block_references(); + + auto process = [](D const& d) + { + auto[desc,i0,j0] = d; + auto[rows,cols,fn] = desc; + rotgen::matrix m(rows, cols); + rotgen::tests::prepare(rows,cols,fn,m); + auto input = rotgen::extract(m, i0, j0); + rotgen::tests::check_cwise_functions(input); + }; + + std::apply([&](auto const&... d) { (process(d),...);}, cases); +}; diff --git a/test/unit/common/cwise.hpp b/test/unit/common/cwise.hpp new file mode 100644 index 0000000..712de11 --- /dev/null +++ b/test/unit/common/cwise.hpp @@ -0,0 +1,93 @@ +//================================================================================================== +/* + ROTGEN - Runtime Overlay for Eigen + Copyright : CODE RECKONS + SPDX-License-Identifier: BSL-1.0 +*/ +//================================================================================================== +#include +#include +#include +#include + +namespace rotgen::tests +{ + template + void check_cwise_functions(const T& input) + { + using EigenMatrix = Eigen::Matrix; + using mat_t = matrix; + + TTS_WHEN("Unary Cwise operations") + { + EigenMatrix e_ref(input.rows(), input.cols()); + prepare([&](auto r, auto c) { return input(r,c); }, e_ref); + + mat_t ref(input.rows(), input.cols()); + + TTS_AND_THEN(".cwiseAbs") + { + e_ref = e_ref.cwiseAbs(); + prepare([&](auto r, auto c) { return e_ref(r,c); }, ref); + TTS_EQUAL(input.cwiseAbs(), ref); + } + + TTS_AND_THEN(".cwiseAbs2") + { + e_ref = e_ref.cwiseAbs2(); + prepare([&](auto r, auto c) { return e_ref(r,c); }, ref); + TTS_EQUAL(input.cwiseAbs2(), ref); + } + + TTS_AND_THEN(".cwiseInverse") + { + e_ref = e_ref.cwiseInverse(); + prepare([&](auto r, auto c) { return e_ref(r,c); }, ref); + TTS_EQUAL(input.cwiseInverse(), ref); + } + + TTS_AND_THEN(".cwiseSqrt") + { + e_ref = e_ref.cwiseAbs().cwiseSqrt(); + auto mat = input.cwiseAbs(); + auto proper_input = mat.cwiseSqrt(); + + for(rotgen::Index r=0;r + +TTS_CASE_TPL("Test dynamic map cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_matrix_references(); + for (const auto& [rows, cols, fn] : cases) + { + rotgen::matrix base(rows, cols); + rotgen::tests::prepare(rows,cols,fn,base); + + rotgen::map> input(base.data(),rows,cols); + rotgen::tests::check_cwise_functions(input); + } +}; + +TTS_CASE_TPL("Test static map cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_static_matrix_references(); + + auto process = [](D const& desc) + { + rotgen::matrix base(D::rows,D::cols); + rotgen::tests::prepare(base.rows(),base.cols(),desc.init_fn,base); + + rotgen::map> input(base.data()); + rotgen::tests::check_cwise_functions(input); + }; + + std::apply([&](auto const&... d) { (process(d),...);}, cases); +}; \ No newline at end of file diff --git a/test/unit/matrix/cwise.cpp b/test/unit/matrix/cwise.cpp new file mode 100644 index 0000000..680a3d1 --- /dev/null +++ b/test/unit/matrix/cwise.cpp @@ -0,0 +1,37 @@ +//================================================================================================== +/* + ROTGEN - Runtime Overlay for Eigen + Copyright : CODE RECKONS + SPDX-License-Identifier: BSL-1.0 +*/ +//================================================================================================== +#include "unit/tests.hpp" +#include "unit/common/cwise.hpp" +#include + +TTS_CASE_TPL("Test dynamic matrix cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_matrix_references(); + for (const auto& [rows, cols, fn] : cases) + { + rotgen::matrix input(rows, cols); + rotgen::tests::prepare(rows,cols,fn,input); + rotgen::tests::check_cwise_functions(input); + } +}; + +TTS_CASE_TPL("Test static matrix cwise operations", rotgen::tests::types) +( tts::type< tts::types> ) +{ + auto const cases = rotgen::tests::generate_static_matrix_references(); + + auto process = [](D const& desc) + { + rotgen::matrix input; + rotgen::tests::prepare(input.rows(),input.cols(),desc.init_fn,input); + rotgen::tests::check_cwise_functions(input); + }; + + std::apply([&](auto const&... d) { (process(d),...);}, cases); +}; \ No newline at end of file