]> git.tdb.fi Git - libs/math.git/blobdiff - tests/matrix.cpp
Some basic test cases
[libs/math.git] / tests / matrix.cpp
diff --git a/tests/matrix.cpp b/tests/matrix.cpp
new file mode 100644 (file)
index 0000000..10e3ffd
--- /dev/null
@@ -0,0 +1,62 @@
+#include <msp/linal/matrix.h>
+#include <msp/linal/squarematrix.h>
+#include <msp/test/test.h>
+
+using namespace Msp;
+
+class MatrixTests: public Test::RegisteredTest<MatrixTests>
+{
+public:
+       MatrixTests();
+
+       static const char *get_name() { return "Matrix"; }
+
+private:
+       void multiply();
+       void invert();
+};
+
+
+MatrixTests::MatrixTests()
+{
+       add(&MatrixTests::multiply, "Multiplication");
+       add(&MatrixTests::invert,   "Inversion");
+}
+
+void MatrixTests::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
+       };
+
+       LinAl::Matrix<double, 3, 2> a(data);
+       LinAl::Matrix<double, 2, 3> b(data+6);
+       EXPECT(a*b == (LinAl::Matrix<double, 3, 3>(data+12)));
+       EXPECT(b*a == (LinAl::Matrix<double, 2, 2>(data+21)));
+}
+
+template<typename T, unsigned N>
+bool is_identity(const LinAl::Matrix<T, N, N> &m)
+{
+       for(unsigned i=0; i<N; ++i)
+               for(unsigned j=0; j<N; ++j)
+                       if(abs(T(i==j)-m(i, j))>1e-10)
+                               return false;
+       return true;
+}
+
+void MatrixTests::invert()
+{
+       static double data[] =
+       {
+               1, 4, 2, 4, 8, 6, 2, 2, 4
+       };
+
+       LinAl::SquareMatrix<double, 3> m(data);
+       LinAl::SquareMatrix<double, 3> i = LinAl::invert(m);
+       EXPECT(is_identity(i*m));
+}