From: Mikko Rasa Date: Sun, 2 Jun 2019 13:22:24 +0000 (+0300) Subject: Copy Matrix test cases for DynamicMatrix X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=44a1fb77d9199ce76fec21ee8aaa6df38e141bf0;p=libs%2Fmath.git Copy Matrix test cases for DynamicMatrix --- diff --git a/tests/dynamicmatrix.cpp b/tests/dynamicmatrix.cpp new file mode 100644 index 0000000..769945d --- /dev/null +++ b/tests/dynamicmatrix.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; +using namespace Msp; + +class DynamicMatrixTests: public Test::RegisteredTest +{ +private: + typedef LinAl::DynamicMatrix Matrixd; + +public: + DynamicMatrixTests(); + + static const char *get_name() { return "DynamicMatrix"; } + +private: + void multiply(); + void invert(); +}; + + +DynamicMatrixTests::DynamicMatrixTests() +{ + add(&DynamicMatrixTests::multiply, "Multiplication"); + add(&DynamicMatrixTests::invert, "Inversion"); +} + +void DynamicMatrixTests::multiply() +{ + static double data[] = + { + 1, 2, 1, 2, 1, 2, + 3, 0, 3, 0, 0, 3, + 3, 6, 3, 3, 6, 3, 6, 3, 6, + 9, 3, 9, 6 + }; + + Matrixd a(3, 2, data); + Matrixd b(2, 3, data+6); + EXPECT_EQUAL(a*b, Matrixd(3, 3, data+12)); + EXPECT_EQUAL(b*a, Matrixd(2, 2, data+21)); +} + +template +bool is_identity(const LinAl::DynamicMatrix &m) +{ + static const T limit = numeric_limits::epsilon()*4; + for(unsigned i=0; ilimit) + return false; + return true; +} + +void DynamicMatrixTests::invert() +{ + static double data[] = + { + 1, 4, 2, 4, 8, 6, 2, 2, 4 + }; + + Matrixd m(3, 3, data); + Matrixd i = LinAl::invert(m); + EXPECT(is_identity(i*m)); +}