Changeset 1240


Ignore:
Timestamp:
Mar 16, 2008, 6:21:39 AM (13 years ago)
Author:
Peter
Message:

working on #223

Location:
trunk/test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix_lookup_test.cc

    r1170 r1240  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/utility/Matrix.h"
    2527#include "yat/classifier/MatrixLookup.h"
     
    3335utility::Matrix matrix(size_t n);
    3436
    35 int main(const int argc,const char* argv[])
     37int main(int argc, char* argv[])
    3638{
    3739  using namespace theplu::yat::classifier;
    3840
    39   std::ostream* error;
    40   if (argc>1 && argv[1]==std::string("-v"))
    41     error = &std::cerr;
    42   else {
    43     error = new std::ofstream("/dev/null");
    44     if (argc>1)
    45       std::cout << "lookup_test -v : for printing extra information\n";
    46   }
    47   bool ok = true;
     41  test::Suite suite(argc, argv);
    4842
    49   *error << "\nTesting MatrixLookup" << std::endl;
    50   *error << "MatrixLookup::MatrixLookup(const utility::Matrix& data)...";
     43  suite.err() << "\nTesting MatrixLookup" << std::endl;
     44  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix& data)...";
    5145  utility::Matrix gsl_m1(matrix(2));
    5246  classifier::MatrixLookup m1(gsl_m1);
     
    5448      m1(0,0)!=gsl_m1(0,0) || m1(0,1)!=gsl_m1(0,1) ||
    5549      m1(1,0)!=gsl_m1(1,0) || m1(1,1)!=gsl_m1(1,1) ) {
    56     ok =false;
    57     *error <<   "ERROR:" << std::endl;
     50    suite.add(false);
     51    suite.err() <<  "ERROR:" << std::endl;
    5852  }
    5953  else
    60     *error << "Ok" << std::endl;
     54    suite.err() << "Ok" << std::endl;
    6155
    6256 
    63   *error << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"
     57  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"
    6458         << "                           const std::vector<size_t>&,\n"
    6559         << "                           const std::vector<size_t>&)...";
     
    7771      m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) ||
    7872      m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) {
    79     ok =false;
    80     *error <<   "ERROR:" << std::endl;
    81     *error << "rows: " << m2.rows() << " expected 2\n"
     73    suite.add(false);
     74    suite.err() <<  "ERROR:" << std::endl;
     75    suite.err() << "rows: " << m2.rows() << " expected 2\n"
    8276           << "columns: " << m2.columns() << " expected 2\n"
    8377           << "(0,0): " << m2(0,0) << " expected " << gsl_m2(1,2) << "\n"
     
    8781  }
    8882  else
    89     *error << "Ok" << std::endl;
     83    suite.err() << "Ok" << std::endl;
    9084
    91   *error << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"
     85  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n"
    9286         << "                           const std::vector<size_t>&,\n"
    9387         << "                           const bool)...";
     
    9690  if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) ||
    9791      m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) {
    98     ok =false;
    99     *error <<   "ERROR:" << std::endl;
    100     *error << "m3.rows(): " << m3.rows() << " expected 1" << std::endl;
    101     *error << "m3.columns(): " << m3.columns() << " expected "
     92    suite.add(false);
     93    suite.err() <<  "ERROR:" << std::endl;
     94    suite.err() << "m3.rows(): " << m3.rows() << " expected 1" << std::endl;
     95    suite.err() << "m3.columns(): " << m3.columns() << " expected "
    10296           << gsl_m2.columns() << std::endl;
    10397  }
    10498  else
    105     *error << "Ok" << std::endl;
     99    suite.err() << "Ok" << std::endl;
    106100
    107   *error << "MatrixLookup::MatrixLookup(const MatrixLookup&)...";
     101  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&)...";
    108102  classifier::MatrixLookup m4(m2);
    109103  if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) ||
    110104      m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) {
    111     ok =false;
    112     *error <<   "ERROR:" << std::endl;
     105    suite.add(false);
     106    suite.err() <<  "ERROR:" << std::endl;
    113107  }
    114108  else
    115     *error << "Ok" << std::endl;
     109    suite.err() << "Ok" << std::endl;
    116110
    117   *error << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n"
     111  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n"
    118112         << "                           const std::vector<size_t>&,\n"
    119113         << "                           const std::vector<size_t>&)...";
    120114  classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one));
    121115  if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) {
    122     ok =false;
    123     *error <<   "ERROR:" << std::endl;
    124     *error << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)
     116    suite.add(false);
     117    suite.err() <<  "ERROR:" << std::endl;
     118    suite.err() << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)
    125119           << std::endl;
    126120  }
    127121  else
    128     *error << "Ok" << std::endl;
     122    suite.err() << "Ok" << std::endl;
    129123 
    130   *error << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n"
     124  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n"
    131125         << "                           const std::vector<size_t>&,\n"
    132126         << "                           const bool)...";
     
    134128  if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) ||
    135129      m6(0,1)!=m2(1,1)) {
    136     ok =false;
    137     *error << "ERROR:" << std::endl;
     130    suite.add(false);
     131    suite.err() << "ERROR:" << std::endl;
    138132  }
    139133  else
    140     *error << "Ok" << std::endl;
     134    suite.err() << "Ok" << std::endl;
    141135
    142   *error << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
     136  suite.err() << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
    143137         << "                           const double)...";
    144138  classifier::MatrixLookup m7(103,112,12);
    145139  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
    146     ok =false;
    147     *error << "ERROR:" << std::endl;
     140    suite.add(false);
     141    suite.err() << "ERROR:" << std::endl;
    148142  }
    149143  else
    150     *error << "Ok" << std::endl;
     144    suite.err() << "Ok" << std::endl;
    151145
    152146
    153   *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
     147  suite.err() << "MatrixLookup::training_data(const std::vector<size_t>)...";
    154148  const classifier::MatrixLookup* TrnData =
    155149    new MatrixLookup(m2, utility::Index(one), false);
    156150  if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){
    157     ok =false;
    158     *error << "ERROR:" << std::endl;
     151    suite.add(false);
     152    suite.err() << "ERROR:" << std::endl;
    159153  }
    160154  else
    161     *error << "Ok" << std::endl;
     155    suite.err() << "Ok" << std::endl;
    162156  delete TrnData;
    163157
    164   *error << "MatrixLookup::validation_data(const std::vector<size_t>,\n"
     158  suite.err() << "MatrixLookup::validation_data(const std::vector<size_t>,\n"
    165159         << "                              const std::vector<size_t>)...";
    166160  std::vector<size_t> val(23,2);
     
    168162    new MatrixLookup(m2, utility::Index(val), false);
    169163  if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){
    170     ok =false;
    171     *error << "ERROR:" << std::endl;
     164    suite.add(false);
     165    suite.err() << "ERROR:" << std::endl;
    172166  }
    173167  else
    174     *error << "Ok" << std::endl;
     168    suite.err() << "Ok" << std::endl;
    175169  delete ValData;
    176170
    177   if (ok)
    178     *error << "Test Ok." << std::endl;
    179   if (error!=&std::cerr)
    180     delete error;
    181   return (ok ? 0 : -1);
     171  return suite.return_value();
    182172}
    183173
  • trunk/test/matrix_test.cc

    r1151 r1240  
    2424*/
    2525
     26#include "Suite.h"
     27
    2628#include "yat/utility/Matrix.h"
    2729
     
    4648
    4749
    48 int main(const int argc,const char* argv[])
     50int main(int argc, char* argv[])
    4951{
    5052  using namespace theplu::yat;
    51   std::ostream* error;
    52   if (argc>1 && argv[1]==std::string("-v"))
    53     error = &std::cerr;
    54   else {
    55     error = new std::ofstream("/dev/null");
    56     if (argc>1)
    57       std::cout << "matrix_test -v : for printing extra information\n";
    58   }
    59 
    60   *error << "Testing matrix class" << std::endl;
    61   bool ok = true;
     53  test::Suite suite(argc, argv);
     54
     55  suite.err() << "Testing matrix class" << std::endl;
    6256  utility::Matrix unit3x3(3,3);
    6357  for (size_t i=0; i<unit3x3.rows(); ++i)
    6458    unit3x3(i,i)=1;
    6559
    66   *error << "\tcopy constructor and operator!=" << std::endl;
     60  suite.err() << "\tcopy constructor and operator!=" << std::endl;
    6761  utility::Matrix m(3,3,9);
    6862  utility::Matrix m2(m);
    6963  if (m2!=m)
    70     ok=false;
    71 
    72   *error << "\toutput operator and istream constructor" << std::endl;
     64    suite.add(false);
     65
     66  suite.err() << "\toutput operator and istream constructor" << std::endl;
    7367  // Checking that the matrix output operator writes a file that the
    7468  // input operator can read.
     
    8074  is.close();
    8175  if (m3!=m2)
    82     ok=false;
     76    suite.add(false);
    8377  std::remove("data/tmp_test_matrix.txt");
    8478
    85   *error << "\toperator*=(double)" << std::endl;
     79  suite.err() << "\toperator*=(double)" << std::endl;
    8680  utility::Matrix m4(3,3,1);
    8781  m4 *= 9;
    8882  if (m4!=m) {
    89     ok=false;
    90     *error << "error operator*=(double)" << std::endl;
     83    suite.add(false);
     84    suite.err() << "error operator*=(double)" << std::endl;
    9185  }
    9286
     
    10599  // a non-view matrix is copied
    106100  {
    107     *error << "\tcopy constructor" << std::endl;
     101    suite.err() << "\tcopy constructor" << std::endl;
    108102    utility::Matrix m2(m5);
    109     ok &= (m2.rows()==m5.rows());
    110     ok &= (m2.columns()==m5.columns());
    111     ok &= (m2==m5);
    112     ok &= (&m2 != &m5);
    113   }
    114 
    115   *error << "\tsub-(row)vector" << std::endl;
     103    suite.add(m2.rows()==m5.rows());
     104    suite.add(m2.columns()==m5.columns());
     105    suite.add(m2==m5);
     106    suite.add(&m2 != &m5);
     107  }
     108
     109  suite.err() << "\tsub-(row)vector" << std::endl;
    116110  // Checking that the row view works, i.e. mutation to the view are
    117111  // reflected in the viewed object.
     
    131125      m5_sum3+=m5(i,j);
    132126  if (m5_sum3-m5_sum2+v5subrow_sum>1e-13) {
    133     ok=false;
    134     *error << "error sub-vector test 1" << std::endl;
    135   }
    136 
    137   *error << "\tsub-(column)vector" << std::endl;
     127    suite.add(false);
     128    suite.err() << "error sub-vector test 1" << std::endl;
     129  }
     130
     131  suite.err() << "\tsub-(column)vector" << std::endl;
    138132  // Checking that the column view works, i.e. mutation to the view
    139133  // are reflected in the viewed object.
     
    153147      m5_sum4+=m5(i,j);
    154148  if (m5_sum4-m5_sum3-v5subcolumn.size()+v5subcolumn_sum>1e-13) {
    155     ok=false;
    156     *error << "error sub-vector test 2" << std::endl;
     149    suite.add(false);
     150    suite.err() << "error sub-vector test 2" << std::endl;
    157151  }
    158152
     
    163157    v5subrow_sum2+=v5subrow(i);
    164158  if (v5subrow_sum2-v5subcolumn(3)>1e-13) {
    165     ok=false;
    166     *error << "error sub-vector test 3" << std::endl;
    167   }
    168 
    169   *error << "\tsub-vector and vector copying" << std::endl;
     159    suite.add(false);
     160    suite.err() << "error sub-vector test 3" << std::endl;
     161  }
     162
     163  suite.err() << "\tsub-vector and vector copying" << std::endl;
    170164  // Checking that a view is not inherited through the copy
    171165  // contructor.
     
    176170    v5subrow_sum3+=v5subrow(i);
    177171  if (v5subrow_sum3-v5subrow_sum2>1e-13) {
    178     ok=false;
    179     *error << "error sub-vector test 4" << std::endl;
     172    suite.add(false);
     173    suite.err() << "error sub-vector test 4" << std::endl;
    180174  }
    181175  // Checking that values in a vector is copied into a viewed matrix.
     
    186180      m5_sum5+=m5(i,j);
    187181  if (m5_sum5-m5_sum4-v5subrow.size()*2+v5subrow_sum3>1e-13) {
    188     ok=false;
    189     *error << "error sub-vector test 5" << std::endl;
     182    suite.add(false);
     183    suite.err() << "error sub-vector test 5" << std::endl;
    190184  }
    191185
    192186  // Checking that the memberfunction matrixwrapper::row() returns a
    193187  // view
    194   *error << "\tthat class member returns a view" << std::endl;
     188  suite.err() << "\tthat class member returns a view" << std::endl;
    195189  matrixwrapper mw(5,2);
    196190  utility::VectorView mwrow=mw.row(2);
    197191  if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) {
    198     ok=false;
    199     *error << "error sub-vector test 7" << std::endl;
    200   }
    201 
    202   *error << "\tmatrix::nan()" << std::endl;
     192    suite.add(false);
     193    suite.err() << "error sub-vector test 7" << std::endl;
     194  }
     195
     196  suite.err() << "\tmatrix::nan()" << std::endl;
    203197  is.open("data/sorlie_centroid_data.txt");
    204198  utility::Matrix* m_nan = new utility::Matrix(is,'\t');
     
    207201  is.close();
    208202  if (m_weight(0,0)!=1){
    209     *error << "error in matrix::nan(): element(0,0) is " << m_weight(0,0)
     203    suite.err() << "error in matrix::nan(): element(0,0) is " << m_weight(0,0)
    210204           << " expected 1." << std::endl;
    211     ok=false;
     205    suite.add(false);
    212206  }
    213207  if (m_weight(3,2)!=0){
    214     *error << "error in matrix::nan(): element(3,2) is " << m_weight(3,2)
     208    suite.err() << "error in matrix::nan(): element(3,2) is " << m_weight(3,2)
    215209           << " expected 0." << std::endl;
    216     ok=false;
     210    suite.add(false);
    217211  }
    218212  delete m_nan;
    219213
    220   *error << "\toperator*=(matrix&)" << std::endl;
     214  suite.err() << "\toperator*=(matrix&)" << std::endl;
    221215  utility::Matrix m6(unit3x3);
    222216  m6 *= m;
    223217  if (m6!=m) {
    224     ok=false;
    225     *error << "error operator*=(matrix) 1" << std::endl;
     218    suite.add(false);
     219    suite.err() << "error operator*=(matrix) 1" << std::endl;
    226220  }
    227221  m6 *= unit3x3;
    228222  if (m6!=m) {
    229     ok=false;
    230     *error << "error operator*=(matrix) 2" << std::endl;
     223    suite.add(false);
     224    suite.err() << "error operator*=(matrix) 2" << std::endl;
    231225  }
    232226  m6*= utility::Matrix(3,4,1.0);
     
    237231  m6*= unit3x3;
    238232
    239   if (error!=&std::cerr)
    240     delete error;
    241 
    242   return (ok ? 0 : -1);
     233  suite.return_value();
    243234}
Note: See TracChangeset for help on using the changeset viewer.