From d693864284dc82eaa12cc6e911225c59c8e06490 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Mon, 11 Jun 2018 15:57:46 +0200 Subject: [PATCH] Added unit tests for TinyMatrix --- tests/CMakeLists.txt | 1 + tests/test_TinyMatrix.cpp | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 tests/test_TinyMatrix.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 21ba01a73..de3c3ebeb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PASTIS_BINARY_DIR}) add_executable (unit_tests test_main.cpp test_RevisionInfo.cpp + test_TinyMatrix.cpp test_TinyVector.cpp) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) diff --git a/tests/test_TinyMatrix.cpp b/tests/test_TinyMatrix.cpp new file mode 100644 index 000000000..cac95d6c4 --- /dev/null +++ b/tests/test_TinyMatrix.cpp @@ -0,0 +1,110 @@ +#include <catch.hpp> + +#include <TinyMatrix.hpp> +#include <PastisAssert.hpp> + +TEST_CASE("TinyMatrix", "[algebra]") { + TinyMatrix<3, int> A(1,2,3, + 4,5,6, + 7,8,9); + REQUIRE(((A(0,0)==1) and (A(0,1)==2) and (A(0,2)==3) and + (A(1,0)==4) and (A(1,1)==5) and (A(1,2)==6) and + (A(2,0)==7) and (A(2,1)==8) and (A(2,2)==9))); + { + const TinyMatrix<3, int> copy_A = A; + REQUIRE(((copy_A(0,0)==1) and (copy_A(0,1)==2) and (copy_A(0,2)==3) and + (copy_A(1,0)==4) and (copy_A(1,1)==5) and (copy_A(1,2)==6) and + (copy_A(2,0)==7) and (copy_A(2,1)==8) and (copy_A(2,2)==9))); + REQUIRE(copy_A==A); + REQUIRE_FALSE(copy_A!=A); + + TinyMatrix<3, int> affected_A; affected_A = A; + REQUIRE(affected_A==A); + REQUIRE_FALSE(affected_A!=A); + } + + { + const int a = 2; + const TinyMatrix<3, int> aA = a*A; + + REQUIRE(aA == TinyMatrix<3,int>(2, 4, 6, + 8, 10, 12, + 14, 16, 18)); + } + TinyMatrix<3, int> Z = zero; + REQUIRE(Z == TinyMatrix<3, int>(0,0,0, + 0,0,0, + 0,0,0)); + { + TinyMatrix<3, int> affected_Z; affected_Z = zero; + REQUIRE(affected_Z == Z); + } + TinyMatrix<3, int> I = identity; + REQUIRE(I == TinyMatrix<3,int>(1,0,0, + 0,1,0, + 0,0,1)); + + { + TinyMatrix<3, int> affected_I; affected_I=identity; + REQUIRE(affected_I==I); + } + + TinyMatrix<3,int> B(6,5,3, + 8,34,6, + 35,6,7); + REQUIRE(A!=B); + REQUIRE_FALSE(A==B); + + REQUIRE(A+B == TinyMatrix<3,int>(7,7,6, + 12,39,12, + 42,14,16)); + { + TinyMatrix<3, int> ApB = A; + ApB += B; + REQUIRE(ApB==A+B); + } + + REQUIRE(A-B == TinyMatrix<3,int>(-5, -3, 0, + -4, -29, 0, + -28, 2, 2)); + { + TinyMatrix<3, int> AmB = A; + AmB -= B; + REQUIRE(AmB==A-B); + } + + REQUIRE(A*B == TinyMatrix<3,int>(127,91,36, + 274,226,84, + 421,361,132)); + + REQUIRE(A*TinyVector<3,int>(2,-3,5) == TinyVector<3,int>(11,23,35)); + + TinyVector<3,int> u(1,3,7); + TinyVector<3,int> v(6,2,-3); + + REQUIRE(tensorProduct(u,v)==TinyMatrix<3,int>(6,2,-3, + 18,6,-9, + 42,14,-21)); + + REQUIRE(det(TinyMatrix<1,int>(6))==6); + REQUIRE(det(TinyMatrix<2,int>(3,1, + -3,6))==21); + REQUIRE(det(B)==-1444); + + REQUIRE(det(TinyMatrix<4,double> (1,2.3,7,-6.2, + 3,4,9,1, + 4.1,5,2,-3, + 2,27,3,17.5))== Approx(6661.455).epsilon(1E-14)); + + 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 + REQUIRE_THROWS_AS(A(3,0), AssertError); + REQUIRE_THROWS_AS(A(0,3), AssertError); + + const TinyMatrix<3,int>& constA = A; + REQUIRE_THROWS_AS(constA(3,0), AssertError); + REQUIRE_THROWS_AS(constA(0,3), AssertError); +#endif // NDEBUG +} -- GitLab