Ignore:
Timestamp:
Mar 11, 2007, 4:40:13 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Fixes #193. Last test for the new functionality added to matrix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix_test.cc

    r789 r793  
    119119  }
    120120
     121  // Test of const view implementation (make sure no zero pointer is
     122  // used). Here we use the bad style of not making the view const!
     123  // If test fails with a null pointer exception it is an uncatchable
     124  // core dump!
     125  {
     126    *error << "\tconst view implementation" << std::endl;
     127    // Change the next statement to
     128    // const utility::matrix m(10,10,3.0);
     129    // when ticket:202 is fixes
     130    utility::matrix m(10,10,3.0);
     131    utility::matrix mview(m,3,3,3,3);
     132    // const utility::vector vview(vv,0,5,1); // this is the proper line
     133    utility::matrix m2(3,3,2.0);
     134    m2.mul(mview); // should work even without const since const arg passing
     135    m2.div(mview); // should work even without const since const arg passing
     136  }
     137
     138  // checking that copy constructor creates an independent object when
     139  // a non-view matrix is copied
     140  {
     141    *error << "\tcopy constructor" << std::endl;
     142    utility::matrix m2(m5);
     143    ok &= (m2.rows()==m5.rows());
     144    ok &= (m2.columns()==m5.columns());
     145    ok &= (m2==m5);
     146    ok &= (&m2 != &m5);
     147    ok &= !m2.isview();
     148  }
     149
     150  // checking that copy constructor creates an independent object when
     151  // a view matrix is copied
     152  {
     153    *error << "\tcopy contructor on view" << std::endl;
     154    // Change the next statement to
     155    // const utility::matrix m(10,10,3.0);
     156    // when ticket:202 is fixes
     157    utility::matrix m(10,10,3.0);
     158    utility::matrix mview(m,3,3,3,3);
     159    utility::matrix m3(mview);
     160    ok &= (mview.rows()==m3.rows());
     161    ok &= (mview.columns()==m3.columns());
     162    ok &= (m3 == mview);
     163    ok &= (&m3 != &mview);
     164    ok &= !m3.isview();
     165  }
     166
     167  // checking that assignment operator throws an exception if matrices
     168  // differ in size
     169  {
     170    *error << "\tassignment operator" << std::endl;
     171    // GSL will catch the error in this test there for the GSL error
     172    // handling must be disabled until after the exception is
     173    // catched. The GSL error handler is reinstated after the
     174    // try-catch construct.
     175    gsl_error_handler_t* err_handler=gsl_set_error_handler_off();
     176    bool exception_happens=false;
     177    try {
     178      utility::matrix m(m5.rows()+1,3,0.0);
     179      m=m5;
     180    } catch (utility::GSL_error& err) {
     181      exception_happens=true;
     182    }
     183    if (!exception_happens) {
     184      *error << "Matrix assignment operator did not throw expected exception"
     185             << std::endl;
     186      ok=false;
     187    }
     188    gsl_set_error_handler(err_handler);
     189  }
     190
     191  // checking that assignment operator changes the underlying object when
     192  // a view is changed.
     193  {
     194    *error << "\tassignment operator on view" << std::endl;
     195    bool this_ok(true);
     196    utility::matrix mat_view(m5,3,3,3,3);
     197    utility::matrix m2(3,3,12.0);
     198    mat_view=m2;
     199    for (u_int i=0; i<mat_view.rows(); ++i)
     200      for (u_int j=0; j<mat_view.columns(); ++j)
     201        if (m5(i+3,j+3)!=mat_view(i,j))
     202          this_ok=false;
     203    if (!this_ok) {
     204      *error << "FAIL: assignemnt operator on view" << std::endl;
     205      ok=false;
     206    }
     207  }
     208
     209  // checking clone functionality
     210  {
     211    *error << "\tclone functionality" << std::endl;
     212    bool this_ok=true;
     213    *error << "\t\tcloning normal matrix" << std::endl;
     214    utility::matrix mat2;
     215    mat2.clone(m5);
     216    if (mat2.rows()!=m5.rows() || mat2.columns()!=m5.columns())
     217      this_ok=false;
     218    else
     219      for (u_int i=0; i<m5.rows(); ++i)
     220        for (u_int j=0; j<m5.columns(); ++j)
     221          if (mat2(i,j)!=m5(i,j))
     222            this_ok=false;
     223    if (mat2.gsl_matrix_p()==m5.gsl_matrix_p())
     224      this_ok=false;
     225    *error << "\t\tcloning matrix view (sub matrix)" << std::endl;
     226    utility::matrix* mat_view=new utility::matrix(m5,3,3,3,3);
     227    utility::matrix mat_view2;
     228    mat_view2.clone(*mat_view);
     229    if (!mat_view2.isview())
     230      this_ok=false;
     231    if ( (mat_view->rows()!=mat_view2.rows()) ||
     232         (mat_view->columns()!=mat_view2.columns()) )
     233      this_ok=false;
     234    else
     235      for (u_int i=0; i<mat_view2.rows(); ++i)
     236        for (u_int j=0; j<mat_view2.columns(); ++j)
     237          if ((*mat_view)(i,j)!=mat_view2(i,j))
     238            this_ok=false;
     239    *error << "\t\tcloned matrix view independence" << std::endl;
     240    delete mat_view;
     241    for (u_int i=0; i<mat_view2.rows(); ++i)
     242      for (u_int j=0; j<mat_view2.columns(); ++j)
     243        if (m5(i+3,j+3)!=mat_view2(i,j))
     244          this_ok=false;
     245    if (!this_ok) {
     246      *error << "FAIL: clone test" << std::endl;
     247      ok=false;
     248    }
     249  }
     250
    121251  *error << "\tsub-(row)vector" << std::endl;
    122252  // Checking that the row view works, i.e. mutation to the view are
    123253  // reflected in the viewed object.
     254  m5_sum2=0;
     255  for (size_t i=0; i<m5.rows(); ++i)
     256    for (size_t j=0; j<m5.columns(); ++j)
     257      m5_sum2+=m5(i,j);
    124258  utility::vector v5subrow(m5,3);
    125259  double v5subrow_sum=0;
     
    140274  // Checking that the column view works, i.e. mutation to the view
    141275  // are reflected in the viewed object.
     276  m5_sum3=0;
     277  for (size_t i=0; i<m5.rows(); ++i)
     278    for (size_t j=0; j<m5.columns(); ++j)
     279      m5_sum3+=m5(i,j);
    142280  utility::vector v5subcolumn(m5,0,false);
    143281  double v5subcolumn_sum=0;
     
    154292    *error << "error sub-vector test 2" << std::endl;
    155293  }
     294
    156295  // Checking that the column view above mutates the values in the row
    157296  // view.
Note: See TracChangeset for help on using the changeset viewer.