Skip to content
Snippets Groups Projects
Commit d6938642 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Added unit tests for TinyMatrix

parent 475b73d7
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PASTIS_BINARY_DIR}) ...@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PASTIS_BINARY_DIR})
add_executable (unit_tests add_executable (unit_tests
test_main.cpp test_main.cpp
test_RevisionInfo.cpp test_RevisionInfo.cpp
test_TinyMatrix.cpp
test_TinyVector.cpp) test_TinyVector.cpp)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
......
#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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment