From 18405382700147dc3374d7b41dd4c36380839fe0 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Tue, 3 Jul 2018 19:14:18 +0200 Subject: [PATCH] Added unit_tests for new functions and methods --- tests/test_TinyMatrix.cpp | 94 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/tests/test_TinyMatrix.cpp b/tests/test_TinyMatrix.cpp index 25dc03c35..3d80a485e 100644 --- a/tests/test_TinyMatrix.cpp +++ b/tests/test_TinyMatrix.cpp @@ -53,6 +53,15 @@ TEST_CASE("TinyMatrix", "[algebra]") { 14, 16, 18)); } + SECTION("checking for scalar seft product") { + const int a = 2; + TinyMatrix<3, int> copy_A = A; + + REQUIRE((copy_A*=a) == TinyMatrix<3,int>(2, 4, 6, + 8, 10, 12, + 14, 16, 18)); + } + SECTION("checking for null matrix management") { TinyMatrix<3, int> Z = zero; REQUIRE(Z == TinyMatrix<3, int>(0,0,0, @@ -119,6 +128,50 @@ TEST_CASE("TinyMatrix", "[algebra]") { 42,14,-21)); } + SECTION("checking for minor calculation") { + TinyMatrix<3,int> A(1,2,3, + 4,5,6, + 7,8,9); + REQUIRE(getMinor(A,0,0)==TinyMatrix<2,int>(5,6, + 8,9)); + REQUIRE(getMinor(A,1,0)==TinyMatrix<2,int>(2,3, + 8,9)); + REQUIRE(getMinor(A,2,0)==TinyMatrix<2,int>(2,3, + 5,6)); + + REQUIRE(getMinor(A,0,1)==TinyMatrix<2,int>(4,6, + 7,9)); + REQUIRE(getMinor(A,1,1)==TinyMatrix<2,int>(1,3, + 7,9)); + REQUIRE(getMinor(A,2,1)==TinyMatrix<2,int>(1,3, + 4,6)); + + + REQUIRE(getMinor(A,0,2)==TinyMatrix<2,int>(4,5, + 7,8)); + REQUIRE(getMinor(A,1,2)==TinyMatrix<2,int>(1,2, + 7,8)); + REQUIRE(getMinor(A,2,2)==TinyMatrix<2,int>(1,2, + 4,5)); + } + + SECTION("checking for cofactors") { + TinyMatrix<3,int> A(1,2,3, + 4,5,6, + 7,8,9); + REQUIRE(cofactor(A,0,0)== (5*9-8*6)); + REQUIRE(cofactor(A,1,0)==-(2*9-8*3)); + REQUIRE(cofactor(A,2,0)== (2*6-5*3)); + + REQUIRE(cofactor(A,0,1)==-(4*9-7*6)); + REQUIRE(cofactor(A,1,1)== (1*9-7*3)); + REQUIRE(cofactor(A,2,1)==-(1*6-4*3)); + + REQUIRE(cofactor(A,0,2)== (4*8-5*7)); + REQUIRE(cofactor(A,1,2)==-(1*8-7*2)); + REQUIRE(cofactor(A,2,2)== (1*5-4*2)); + } + SECTION("checking for determinant calculations") { REQUIRE(det(TinyMatrix<1,int>(6))==6); REQUIRE(det(TinyMatrix<2,int>(3,1, @@ -130,16 +183,55 @@ TEST_CASE("TinyMatrix", "[algebra]") { 2,27,3,17.5))== Approx(6661.455).epsilon(1E-14)); } + SECTION("checking for inverse calculations") { + { + const TinyMatrix<1,double> A1(2); + REQUIRE(inverse(A1)(0,0) == Approx(0.5).epsilon(1E-14)); + } + + { + const TinyMatrix<2,double> A2(2,3, + 4,1); + const TinyMatrix<2,double> I = inverse(A2)*A2; + + REQUIRE(I(0,0) == Approx(1).epsilon(1E-14)); + REQUIRE(I(0,1) == Approx(0).epsilon(1E-14)); + REQUIRE(I(1,0) == Approx(0).epsilon(1E-14)); + REQUIRE(I(1,1) == Approx(1).epsilon(1E-14)); + } + + { + const TinyMatrix<3,double> A3(2,3,1, + 4,-1,5, + -2,3,4); + const TinyMatrix<3,double> I = inverse(A3)*A3; + + // Strangely Approx fails for some 0 ... + REQUIRE(I(0,0) == Approx(1).epsilon(1E-14)); + REQUIRE(std::abs(I(0,1))<1E-14); + REQUIRE(std::abs(I(0,2))<1E-14); + REQUIRE(std::abs(I(1,0))<1E-14); + REQUIRE(I(1,1) == Approx(1).epsilon(1E-14)); + REQUIRE(std::abs(I(1,2))<1E-14); + REQUIRE(std::abs(I(2,0))<1E-14); + REQUIRE(std::abs(I(2,1))<1E-14); + REQUIRE(I(2,2) == Approx(1).epsilon(1E-14)); + } + } + SECTION("checking for matrices output") { REQUIRE(Catch::Detail::stringify(A) == "[(1,2,3)(4,5,6)(7,8,9)]"); REQUIRE(Catch::Detail::stringify(TinyMatrix<1,int>(7)) == "7"); } #ifndef NDEBUG - SECTION("checking for bounds vioation") { + SECTION("checking for bounds violation") { REQUIRE_THROWS_AS(A(3,0), AssertError); REQUIRE_THROWS_AS(A(0,3), AssertError); + REQUIRE_THROWS_AS(getMinor(A,3,0), AssertError); + REQUIRE_THROWS_AS(getMinor(A,0,3), AssertError); + const TinyMatrix<3,int>& constA = A; REQUIRE_THROWS_AS(constA(3,0), AssertError); REQUIRE_THROWS_AS(constA(0,3), AssertError); -- GitLab