diff --git a/test/mlpp_matrix_tests.cpp b/test/mlpp_matrix_tests.cpp index f7d64a6..7330e7a 100644 --- a/test/mlpp_matrix_tests.cpp +++ b/test/mlpp_matrix_tests.cpp @@ -1,11 +1,37 @@ #include "mlpp_matrix_tests.h" +#include "core/log/logger.h" + //TODO remove #include #include "../mlpp/lin_alg/mlpp_matrix.h" +void MLPPMatrixTests::run_tests() { + PLOG_MSG("RUNNIG MLPPMatrixTests!"); + + PLOG_MSG("test_mlpp_matrix()"); + test_mlpp_matrix(); + + PLOG_MSG("test_add_row()"); + test_add_row(); + PLOG_MSG("test_add_row_pool_vector()"); + test_add_row_pool_vector(); + PLOG_MSG("test_add_row_mlpp_vector()"); + test_add_row_mlpp_vector(); + PLOG_MSG("test_add_rows_mlpp_matrix()"); + test_add_rows_mlpp_matrix(); + + PLOG_MSG("test_remove_row()"); + test_remove_row(); + PLOG_MSG("test_remove_row_unordered()"); + test_remove_row_unordered(); + + PLOG_MSG("test_mlpp_matrix_mul()"); + test_mlpp_matrix_mul(); +} + void MLPPMatrixTests::test_mlpp_matrix() { std::vector> A = { { 1, 0, 0, 0 }, @@ -29,6 +55,295 @@ void MLPPMatrixTests::test_mlpp_matrix() { is_approx_equals_mat(rmat, rmat2, "re-set_from_std_vectors test."); } +void MLPPMatrixTests::test_add_row() { + std::vector> A = { + { 1, 2, 3, 4 }, + }; + + std::vector> B = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + std::vector> C = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + Vector rv; + rv.push_back(1); + rv.push_back(2); + rv.push_back(3); + rv.push_back(4); + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + + rmat->add_row(rv); + is_approx_equals_mat(rmata, rmat, "rmat->add_row(rv);"); + + rmat->add_row(rv); + is_approx_equals_mat(rmatb, rmat, "rmat->add_row(rv);"); + + rmat->add_row(rv); + is_approx_equals_mat(rmatc, rmat, "rmat->add_row(rv);"); +} +void MLPPMatrixTests::test_add_row_pool_vector() { + std::vector> A = { + { 1, 2, 3, 4 }, + }; + + std::vector> B = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + std::vector> C = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + PoolVector rv; + rv.push_back(1); + rv.push_back(2); + rv.push_back(3); + rv.push_back(4); + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + + rmat->add_row_pool_vector(rv); + is_approx_equals_mat(rmata, rmat, "rmat->add_row_pool_vector(rv);"); + + rmat->add_row_pool_vector(rv); + is_approx_equals_mat(rmatb, rmat, "rmat->add_row_pool_vector(rv);"); + + rmat->add_row_pool_vector(rv); + is_approx_equals_mat(rmatc, rmat, "rmat->add_row_pool_vector(rv);"); +} +void MLPPMatrixTests::test_add_row_mlpp_vector() { + std::vector> A = { + { 1, 2, 3, 4 }, + }; + + std::vector> B = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + std::vector> C = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + Ref rv; + rv.instance(); + rv->push_back(1); + rv->push_back(2); + rv->push_back(3); + rv->push_back(4); + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + + rmat->add_row_mlpp_vector(rv); + is_approx_equals_mat(rmata, rmat, "rmat->add_row_mlpp_vector(rv);"); + + rmat->add_row_mlpp_vector(rv); + is_approx_equals_mat(rmatb, rmat, "rmat->add_row_mlpp_vector(rv);"); + + rmat->add_row_mlpp_vector(rv); + is_approx_equals_mat(rmatc, rmat, "rmat->add_row_mlpp_vector(rv);"); +} +void MLPPMatrixTests::test_add_rows_mlpp_matrix() { + std::vector> A = { + { 1, 2, 3, 4 }, + }; + + std::vector> B = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + std::vector> C = { + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + { 1, 2, 3, 4 }, + }; + + std::vector r = { 1, 2, 3, 4 }; + + PoolVector rvp; + rvp.push_back(1); + rvp.push_back(2); + rvp.push_back(3); + rvp.push_back(4); + + Ref rv; + rv.instance(); + rv->add_row_pool_vector(rvp); + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + + rmat->add_rows_mlpp_matrix(rv); + is_approx_equals_mat(rmata, rmat, "rmat->add_rows_mlpp_matrix(rv);"); + + rmat->add_rows_mlpp_matrix(rv); + is_approx_equals_mat(rmatb, rmat, "rmat->add_rows_mlpp_matrix(rv);"); + + rmat->add_rows_mlpp_matrix(rv); + is_approx_equals_mat(rmatc, rmat, "rmat->add_rows_mlpp_matrix(rv);"); +} + +void MLPPMatrixTests::test_remove_row() { + std::vector> A = { + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 9, 10, 11, 12 }, + }; + + std::vector> B = { + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + }; + + std::vector> C = { + { 1, 2, 3, 4 }, + }; + + std::vector> D = { + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 13, 14, 15, 16 }, + { 9, 10, 11, 12 }, + }; + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + rmat->set_from_std_vectors(D); + + rmat->remove_row(2); + is_approx_equals_mat(rmat, rmata, "rmat->remove_row(2);"); + + rmat->remove_row(2); + is_approx_equals_mat(rmat, rmatb, "rmat->remove_row(2);"); + + rmat->remove_row(1); + is_approx_equals_mat(rmat, rmatc, "rmat->remove_row(1);"); +} +void MLPPMatrixTests::test_remove_row_unordered() { + std::vector> A = { + { 1, 2, 3, 4 }, + { 13, 14, 15, 16 }, + { 9, 10, 11, 12 }, + }; + + std::vector> B = { + { 9, 10, 11, 12 }, + { 13, 14, 15, 16 }, + }; + + std::vector> C = { + { 9, 10, 11, 12 }, + }; + + std::vector> D = { + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 9, 10, 11, 12 }, + { 13, 14, 15, 16 }, + }; + + Ref rmata; + rmata.instance(); + rmata->set_from_std_vectors(A); + + Ref rmatb; + rmatb.instance(); + rmatb->set_from_std_vectors(B); + + Ref rmatc; + rmatc.instance(); + rmatc->set_from_std_vectors(C); + + Ref rmat; + rmat.instance(); + rmat->set_from_std_vectors(D); + + rmat->remove_row_unordered(1); + is_approx_equals_mat(rmat, rmata, "rmat->remove_row_unordered(1);"); + + rmat->remove_row_unordered(0); + is_approx_equals_mat(rmat, rmatb, "rmat->remove_row(0);"); + + rmat->remove_row_unordered(1); + is_approx_equals_mat(rmat, rmatc, "rmat->remove_row_unordered(1);"); +} + void MLPPMatrixTests::test_mlpp_matrix_mul() { std::vector> A = { { 1, 2 }, @@ -80,6 +395,8 @@ MLPPMatrixTests::~MLPPMatrixTests() { } void MLPPMatrixTests::_bind_methods() { + ClassDB::bind_method(D_METHOD("run_tests"), &MLPPMatrixTests::run_tests); + ClassDB::bind_method(D_METHOD("test_mlpp_matrix"), &MLPPMatrixTests::test_mlpp_matrix); ClassDB::bind_method(D_METHOD("test_mlpp_matrix_mul"), &MLPPMatrixTests::test_mlpp_matrix_mul); } diff --git a/test/mlpp_matrix_tests.h b/test/mlpp_matrix_tests.h index 478ece2..b2cfd22 100644 --- a/test/mlpp_matrix_tests.h +++ b/test/mlpp_matrix_tests.h @@ -21,7 +21,19 @@ class MLPPMatrixTests : public MLPPTests { GDCLASS(MLPPMatrixTests, MLPPTests); public: + void run_tests(); + void test_mlpp_matrix(); + + void test_add_row(); + void test_add_row_pool_vector(); + void test_add_row_mlpp_vector(); + void test_add_rows_mlpp_matrix(); + + void test_remove_row(); + void test_remove_row_unordered(); + + void test_mlpp_matrix_mul(); MLPPMatrixTests();