LCOV - code coverage report
Current view: top level - source/tests - TestMatrixOperator.cpp (source / functions) Hit Total Coverage
Test: LibForBES Unit Tests Lines: 72 72 100.0 %
Date: 2016-04-18 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * File:   TestMatrixOperator.cpp
       3             :  * Author: Pantelis Sopasakis
       4             :  *
       5             :  * Created on Jul 24, 2015, 8:39:54 PM
       6             :  */
       7             : 
       8             : #include "TestMatrixOperator.h"
       9             : 
      10             : 
      11           1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestMatrixOperator);
      12             : 
      13           4 : TestMatrixOperator::TestMatrixOperator() {
      14           4 : }
      15             : 
      16           8 : TestMatrixOperator::~TestMatrixOperator() {
      17           8 : }
      18             : 
      19           4 : void TestMatrixOperator::setUp() {
      20           4 :     srand(time(NULL));
      21           4 : }
      22             : 
      23           4 : void TestMatrixOperator::tearDown() {
      24           4 : }
      25             : 
      26           1 : void TestMatrixOperator::testCall2() {
      27           1 :     size_t n = 10;
      28           1 :     size_t m = 3;
      29           1 :     Matrix A = MatrixFactory::MakeRandomMatrix(n, m, -2.0, 4.0);
      30           2 :     Matrix x = MatrixFactory::MakeRandomMatrix(m, 1, 0.0, 1.0);
      31           2 :     Matrix y = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0);
      32             : 
      33           2 :     Matrix y_copy(y);
      34           1 :     LinearOperator * op = new MatrixOperator(A);
      35             : 
      36           1 :     double alpha = M_PI;
      37           1 :     double gamma = M_SQRT2;
      38             : 
      39           1 :     int status = op->call(y, alpha, x, gamma); // y = gamma * y + alpha * A * x
      40           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
      41             : 
      42           1 :     y_copy *= gamma;
      43           2 :     Matrix z = A * x;
      44           1 :     z *= alpha;
      45           1 :     y_copy += z;
      46             : 
      47           1 :     _ASSERT_EQ(y_copy, y);
      48             : 
      49           2 :     delete op;
      50           1 : }
      51             : 
      52           1 : void TestMatrixOperator::testCall() {
      53           1 :     size_t n = 10;
      54           1 :     size_t m = 3;
      55             : 
      56           1 :     Matrix M = MatrixFactory::MakeRandomMatrix(m, n, 0.0, 10.0, Matrix::MATRIX_DENSE);
      57           2 :     Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 10.0, Matrix::MATRIX_DENSE);
      58           1 :     LinearOperator *T = new MatrixOperator(M);
      59           2 :     Matrix y;
      60           1 :     _ASSERT_OK(y = T->call(x));
      61             : 
      62           2 :     Matrix z = M*x;
      63           1 :     _ASSERT_EQ(z, y);
      64           2 :     delete T;
      65             : 
      66           1 : }
      67             : 
      68           1 : void TestMatrixOperator::testCallId() {
      69           1 :     size_t n = 10;
      70           1 :     Matrix Id = MatrixFactory::MakeIdentity(n, 1.0);
      71           2 :     Matrix x = MatrixFactory::MakeRandomMatrix(n, 1.0, 2.0, 2.0);
      72           2 :     MatrixOperator IdOp(Id);
      73           2 :     Matrix y = IdOp.call(x);
      74           2 :     _ASSERT_EQ(x, y);
      75           1 : }
      76             : 
      77           1 : void TestMatrixOperator::testCallAdjoint() {
      78           1 :     size_t n = 10;
      79             : 
      80           1 :     Matrix M = MatrixFactory::MakeRandomMatrix(n, n, 0.0, 10.0, Matrix::MATRIX_SYMMETRIC);
      81           2 :     Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 10.0, Matrix::MATRIX_DENSE);
      82           1 :     MatrixOperator *T = new MatrixOperator(M);
      83           2 :     Matrix y;
      84           2 :     Matrix z;
      85             : 
      86           1 :     _ASSERT(T->isSelfAdjoint());
      87           1 :     y = T->call(x);
      88           1 :     z = T->callAdjoint(x);
      89             : 
      90           1 :     _ASSERT_EQ(z, y);
      91             : 
      92             : 
      93           1 :     M = MatrixFactory::MakeRandomMatrix(n, n, 0.0, 10.0, Matrix::MATRIX_DENSE);
      94           1 :     _ASSERT_OK(T->setMatrix(M));
      95           1 :     _ASSERT_NOT(T->isSelfAdjoint());
      96             : 
      97           1 :     M = MatrixFactory::MakeRandomMatrix(n, n, 0.0, 10.0, Matrix::MATRIX_DENSE);
      98           1 :     _ASSERT_NOT(M.isSymmetric());
      99           1 :     _ASSERT_OK(T->setMatrix(M));
     100           1 :     _ASSERT_NOT(T->isSelfAdjoint());
     101           1 :     _ASSERT_OK(y = T->call(x));
     102           1 :     _ASSERT_OK(z = T->callAdjoint(x));
     103           1 :     _ASSERT_NOT(y == z);
     104             : 
     105           2 :     delete T;
     106           4 : }
     107             : 

Generated by: LCOV version 1.10