]> git.tdb.fi Git - libs/math.git/commitdiff
Copy Matrix test cases for DynamicMatrix
authorMikko Rasa <tdb@tdb.fi>
Sun, 2 Jun 2019 13:22:24 +0000 (16:22 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 2 Jun 2019 16:13:09 +0000 (19:13 +0300)
tests/dynamicmatrix.cpp [new file with mode: 0644]

diff --git a/tests/dynamicmatrix.cpp b/tests/dynamicmatrix.cpp
new file mode 100644 (file)
index 0000000..769945d
--- /dev/null
@@ -0,0 +1,67 @@
+#include <limits>
+#include <msp/linal/dynamicmatrix.h>
+#include <msp/test/test.h>
+
+using namespace std;
+using namespace Msp;
+
+class DynamicMatrixTests: public Test::RegisteredTest<DynamicMatrixTests>
+{
+private:
+       typedef LinAl::DynamicMatrix<double> 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<typename T>
+bool is_identity(const LinAl::DynamicMatrix<T> &m)
+{
+       static const T limit = numeric_limits<T>::epsilon()*4;
+       for(unsigned i=0; i<m.rows(); ++i)
+               for(unsigned j=0; j<m.columns(); ++j)
+                       if(abs(T(i==j)-m(i, j))>limit)
+                               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));
+}