//================================================================================================== /* ROTGEN - Runtime Overlay for Eigen Copyright : CODE RECKONS SPDX-License-Identifier: BSL-1.0 */ //================================================================================================== #include "unit/tests.hpp" #include #include template void test_map_operations(rotgen::Index rows, rotgen::Index cols, auto a_init_fn, auto b_init_fn, auto ops, auto self_ops) { MatrixType a_data(rows, cols); MatrixType b_data(rows, cols); MatrixType ref_data; for (rotgen::Index r = 0; r < rows; ++r) for (rotgen::Index c = 0; c < cols; ++c) { a_data(r, c) = a_init_fn(r, c); b_data(r, c) = b_init_fn(r, c); } ref_data = ops(a_data, b_data); rotgen::map a_map(a_data.data(), rows, cols); rotgen::map b_map(b_data.data(), rows, cols); TTS_EQUAL(ops(a_map, b_map), ref_data); self_ops(a_map, b_map); TTS_EQUAL(a_map, ref_data); TTS_EXPECT(verify_rotgen_reentrance(ops(a_map, b_map))); TTS_EXPECT(verify_rotgen_reentrance(self_ops(a_map, b_map))); } template void test_map_scalar_operations(rotgen::Index rows, rotgen::Index cols, auto fn, auto s, auto ops, auto self_ops) { MatrixType a_data(rows, cols); MatrixType ref_data; for (rotgen::Index r = 0; r < rows; ++r) for (rotgen::Index c = 0; c < cols; ++c) a_data(r, c) = fn(r, c); ref_data = ops(a_data, s); rotgen::map a_map(a_data.data(), rows, cols); TTS_EQUAL(ops(a_map, s), ref_data); self_ops(a_map, s); TTS_EQUAL(a_map, ref_data); TTS_EXPECT(verify_rotgen_reentrance(ops(a_map, s))); TTS_EXPECT(verify_rotgen_reentrance(self_ops(a_map, s))); } template void test_map_scalar_multiplications(rotgen::Index rows, rotgen::Index cols, auto fn, auto s) { MatrixType a_data(rows, cols); MatrixType ref_data; for (rotgen::Index r = 0; r < rows; ++r) for (rotgen::Index c = 0; c < cols; ++c) a_data(r, c) = fn(r, c); ref_data = a_data * s; rotgen::map a_map(a_data.data(), rows, cols); TTS_EQUAL(a_map * s, ref_data); TTS_EQUAL(s * a_map, ref_data); a_map *= s; TTS_EQUAL(a_map, ref_data); TTS_EXPECT(verify_rotgen_reentrance(a_map * s)); TTS_EXPECT(verify_rotgen_reentrance(s * a_map)); TTS_EXPECT(verify_rotgen_reentrance(a_map *= s)); } template void test_map_multiplication(rotgen::Index rows, rotgen::Index cols, auto a_init_fn, auto b_init_fn) { MatrixType a_data(rows, cols); MatrixType b_data(cols, rows); MatrixType ref_data; for (rotgen::Index r = 0; r < rows; ++r) for (rotgen::Index c = 0; c < cols; ++c) { a_data(r, c) = a_init_fn(r, c); b_data(c, r) = b_init_fn(c, r); } ref_data = a_data * b_data; rotgen::map a_map(a_data.data(), rows, cols); rotgen::map b_map(b_data.data(), cols, rows); TTS_EQUAL(a_map * b_map, ref_data); TTS_EXPECT(verify_rotgen_reentrance(a_map * b_map)); if (rows == cols) { a_map *= b_map; TTS_EQUAL(a_map, ref_data); TTS_EXPECT(verify_rotgen_reentrance(a_map *= b_map)); } } inline constexpr auto init_a = [](auto r, auto c) { return 9.9 * r * r * r - 6 * c - 12; }; inline constexpr auto init_b = [](auto r, auto c) { return 3.1 * r + 4.2 * c - 12.3; }; inline constexpr auto init_0 = [](auto, auto) { return 0; }; TTS_CASE_TPL("Check map addition", rotgen::tests::types)( tts::type>) { using mat_t = rotgen::matrix; auto op = [](auto a, auto b) { return a + b; }; auto s_op = [](auto& a, auto b) { return a += b; }; test_map_operations(1, 1, init_a, init_b, op, s_op); test_map_operations(3, 5, init_a, init_b, op, s_op); test_map_operations(5, 3, init_a, init_b, op, s_op); test_map_operations(5, 5, init_a, init_b, op, s_op); test_map_operations(5, 5, init_b, init_a, op, s_op); test_map_operations(10, 1, init_a, init_b, op, s_op); test_map_operations(1, 10, init_a, init_b, op, s_op); test_map_operations(5, 5, init_0, init_b, op, s_op); test_map_operations(5, 5, init_a, init_0, op, s_op); }; TTS_CASE_TPL("Check map subtraction", rotgen::tests::types)( tts::type>) { using mat_t = rotgen::matrix; auto op = [](auto a, auto b) { return a - b; }; auto s_op = [](auto& a, auto b) { return a -= b; }; test_map_operations(1, 1, init_a, init_b, op, s_op); test_map_operations(3, 5, init_a, init_b, op, s_op); test_map_operations(5, 3, init_a, init_b, op, s_op); test_map_operations(5, 5, init_a, init_b, op, s_op); test_map_operations(5, 5, init_b, init_a, op, s_op); test_map_operations(10, 1, init_a, init_b, op, s_op); test_map_operations(1, 10, init_a, init_b, op, s_op); test_map_operations(5, 5, init_0, init_b, op, s_op); test_map_operations(5, 5, init_a, init_0, op, s_op); }; TTS_CASE_TPL("Check map multiplications", rotgen::tests::types)( tts::type>) { using mat_t = rotgen::matrix; auto init_id = [](auto r, auto c) { return r == c ? 1 : 0; }; test_map_multiplication(1, 1, init_a, init_b); test_map_multiplication(3, 5, init_a, init_b); test_map_multiplication(5, 3, init_a, init_b); test_map_multiplication(5, 5, init_a, init_b); test_map_multiplication(5, 5, init_b, init_a); test_map_multiplication(5, 5, init_a, init_a); test_map_multiplication(5, 5, init_a, init_id); test_map_multiplication(5, 5, init_id, init_a); test_map_multiplication(10, 1, init_a, init_b); test_map_multiplication(1, 10, init_a, init_b); test_map_multiplication(5, 5, init_0, init_b); test_map_multiplication(5, 5, init_a, init_0); }; TTS_CASE_TPL("Check map multiplication with scalar", rotgen::tests::types)( tts::type>) { using mat_t = rotgen::matrix; test_map_scalar_multiplications(1, 1, init_a, T{3.5}); test_map_scalar_multiplications(3, 5, init_a, T{-2.5}); test_map_scalar_multiplications(5, 3, init_a, T{4.}); test_map_scalar_multiplications(5, 5, init_a, T{-5.}); test_map_scalar_multiplications(5, 5, init_a, T{1.}); test_map_scalar_multiplications(5, 5, init_a, T{6.}); test_map_scalar_multiplications(10, 1, init_a, T{10.}); test_map_scalar_multiplications(1, 10, init_a, T{-0.5}); }; TTS_CASE_TPL("Check map division with scalar", rotgen::tests::types)( tts::type>) { using mat_t = rotgen::matrix; auto op = [](auto a, auto b) { return a / b; }; auto s_op = [](auto& a, auto b) { return a /= b; }; test_map_scalar_operations(1, 1, init_a, T{3.5}, op, s_op); test_map_scalar_operations(3, 5, init_a, T{-2.5}, op, s_op); test_map_scalar_operations(5, 3, init_a, T{4.}, op, s_op); test_map_scalar_operations(5, 5, init_a, T{-5.}, op, s_op); test_map_scalar_operations(5, 5, init_a, T{1.}, op, s_op); test_map_scalar_operations(5, 5, init_a, T{6.}, op, s_op); test_map_scalar_operations(10, 1, init_a, T{10.}, op, s_op); test_map_scalar_operations(1, 10, init_a, T{-0.5}, op, s_op); };