Changeset 419


Ignore:
Timestamp:
Dec 2, 2005, 12:52:49 AM (16 years ago)
Author:
Jari Häkkinen
Message:

Added more matrix tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/better_matrix_class/test/matrix_test.cc

    r417 r419  
    66#include <fstream>
    77#include <iostream>
     8
     9class matrixwrapper
     10{
     11public:
     12  matrixwrapper(size_t i,size_t j,double value=7)
     13    : m_(i,j,value) {}
     14
     15  inline theplu::gslapi::vector
     16  row(const size_t& i) { return theplu::gslapi::vector(m_,i); }
     17
     18  inline const theplu::gslapi::matrix& matrix(void) const { return m_; }
     19
     20private:
     21  theplu::gslapi::matrix m_;
     22};
     23
    824
    925int main(const int argc,const char* argv[])
     
    1834      std::cout << "matrix_test -v : for printing extra information\n";
    1935  }
     36
    2037  *error << "Testing matrix class" << std::endl;
    2138  bool ok = true;
     
    2845
    2946  *error << "\toutput operator and istream constructor" << std::endl;
     47  // Checking that the matrix output operator writes a file that the
     48  // input operator can read.
    3049  std::ofstream my_out("data/tmp_test_matrix.txt");
    3150  my_out << m2;
     
    4766
    4867  *error << "\tsub-matrix" << std::endl;
     68  // Checking that sub-matrices work, i.e. mutation to the view are
     69  // reflected in the viewed object.
    4970  is.open("data/knni_matrix.data");
     71  // The stream input is a proper matrix file, with some stray empty
     72  // lines and other whitespaces. The file is not expected to break
     73  // things.
    5074  gslapi::matrix m5(is);
    5175  is.close();
     
    7296
    7397  *error << "\tsub-(row)vector" << std::endl;
     98  // Checking that the row view works, i.e. mutation to the view are
     99  // reflected in the viewed object.
    74100  gslapi::vector v5subrow(m5,3);
    75101  double v5subrow_sum=0;
     
    88114
    89115  *error << "\tsub-(column)vector" << std::endl;
     116  // Checking that the column view works, i.e. mutation to the view
     117  // are reflected in the viewed object.
    90118  gslapi::vector v5subcolumn(m5,0,false);
    91119  double v5subcolumn_sum=0;
     
    102130    *error << "error sub-vector test 2" << std::endl;
    103131  }
     132  // Checking that the column view above mutates the values in the row
     133  // view.
    104134  double v5subrow_sum2=0;
    105135  for (size_t i=0; i<v5subrow.size(); ++i)
     
    111141
    112142  *error << "\tsub-vector and vector copying" << std::endl;
     143  // Checking that a view is not inherited through the copy
     144  // contructor.
    113145  gslapi::vector v6(v5subrow);
    114146  v6.set_all(2);
     
    120152    *error << "error sub-vector test 4" << std::endl;
    121153  }
     154  // Checking that values in a vector is copied into a viewed matrix.
    122155  v5subrow.set(v6);
    123156  double m5_sum5=0;
     
    129162    *error << "error sub-vector test 5" << std::endl;
    130163  }
     164  // Checking that vector::operator= indeed makes a view to become a
     165  // "normal" vector.
    131166  v5subrow=gslapi::vector(23,22);
    132167  double m5_sum6=0;
     
    139174  }
    140175
     176  // Checking that the memberfunction matrixwrapper::row() returns a
     177  // view
     178  *error << "\tthat class member returns a view" << std::endl;
     179  matrixwrapper mw(5,2);
     180  gslapi::vector mwrow=mw.row(2);
     181  if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) {
     182    ok=false;
     183    *error << "error sub-vector test 7" << std::endl;
     184  }
     185
     186  *error << "\tsub-matrix of a sub-matrix" << std::endl;
     187  // Checking that a sub-matrix of a sub-matrix can be created.
     188  gslapi::matrix sub(m5,3,3,3,3);
     189  gslapi::matrix subsub(sub,2,1,1,2);
     190  subsub(0,0)=23221121;
     191  if (&sub(2,1)!=&subsub(0,0) || subsub(0,0)!=m5(5,4) || &subsub(0,0)!=&m5(5,4)){
     192    ok=false;
     193    *error << "error sub-matrix test 1" << std::endl;
     194  }
     195
    141196  if (error!=&std::cerr)
    142197    delete error;
Note: See TracChangeset for help on using the changeset viewer.