Changeset 1248


Ignore:
Timestamp:
Mar 20, 2008, 12:07:30 AM (13 years ago)
Author:
Peter
Message:

fixes tests - ticket:223

Location:
trunk/test
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/fileutil_test.cc

    r1207 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/utility/FileUtil.h"
    2527#include "yat/utility/Exception.h"
     
    2931#include <string>
    3032
    31 int main(const int argc,const char* argv[])
    32 
     33int main(int argc,char* argv[])
    3334
    3435  using namespace theplu::yat;
    35 
    36   std::ostream* error;
    37   if (argc>1 && argv[1]==std::string("-v"))
    38     error = &std::cerr;
    39   else {
    40     error = new std::ofstream("/dev/null");
    41     if (argc>1)
    42       std::cout << "fileio_test -v : for printing extra information\n";
    43   }
    44   *error << "testing FileUtil ... " << std::endl;
    45 
    46   bool ok = true;
     36  test::Suite suite(argc, argv);
     37  suite.err() << "testing FileUtil ... " << std::endl;
    4738
    4839  // checking permissions for current directory
    4940  try {
    50     *error << "FileUtil Test 1" << std::endl;
     41    suite.err() << "FileUtil Test 1" << std::endl;
    5142    bool testval=true;
    5243    utility::FileUtil file(".");
    5344    if (testval=file.exists())
    54       *error << "\tfile exists: " << file.path() << std::endl;
    55     ok&=testval;
     45      suite.err() << "\tfile exists: " << file.path() << std::endl;
     46    suite.add(testval);
    5647    if (!(testval=file.permissions("rwx")))
    57       *error << "\trwx permissions on " << file.path() << std::endl;
    58     ok&=!testval;
     48      suite.err() << "\trwx permissions on " << file.path() << std::endl;
     49    suite.add(!testval);
    5950    if (!(testval=file.permissions("rx")))
    60       *error << "\trx permissions on " << file.path() << std::endl;
    61     ok&=!testval;
     51      suite.err() << "\trx permissions on " << file.path() << std::endl;
     52    suite.add(!testval);
    6253    if (!(testval=file.permissions("d")))
    63       *error << "\td permissions on " << file.path() << std::endl;
    64     ok&=!testval;
     54      suite.err() << "\td permissions on " << file.path() << std::endl;
     55    suite.add(!testval);
    6556  }
    6657  catch (utility::IO_error e) {
    67     ok=false;
    68     *error << e.what() << std::endl;
     58    suite.add(false);
     59    suite.err() << e.what() << std::endl;
    6960  }
    7061
    7162  // checking permissions on non-existent file with ./ in path
    7263  try {
    73     *error << "FileUtil Test 2" << std::endl;
     64    suite.err() << "FileUtil Test 2" << std::endl;
    7465    bool testval=true;
    7566    // filename below should be unique
    7667    utility::FileUtil file("./fileio_test.sdf34DSF");
    77     *error << "\tpath: " << std::endl;
     68    suite.err() << "\tpath: " << std::endl;
    7869    if (!(testval=file.exists()))
    79       *error << "\tfile does not exist: " << file.path() << std::endl;
    80     ok&=!testval;
     70      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
     71    suite.add(!testval);
    8172    if (!(testval=file.permissions("r")))
    82       *error << "\tr permission on " << file.path() << std::endl;
    83     ok&=!testval;
     73      suite.err() << "\tr permission on " << file.path() << std::endl;
     74    suite.add(!testval);
    8475    if (!(testval=file.permissions("rwx")))
    85       *error << "\trwx permissions on " << file.path() << std::endl;
    86     ok&=!testval;
     76      suite.err() << "\trwx permissions on " << file.path() << std::endl;
     77    suite.add(!testval);
    8778    if (!(testval=file.permissions("w")))
    88       *error << "\tw permission on " << file.path() << std::endl;
    89     ok&=!testval; // 'w' on non-existent file ok if directory writeable
     79      suite.err() << "\tw permission on " << file.path() << std::endl;
     80    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
    9081    if (!(testval=file.permissions("d")))
    91       *error << "\td permission on " << file.path() << std::endl;
    92     ok&=!testval;
     82      suite.err() << "\td permission on " << file.path() << std::endl;
     83    suite.add(!testval);
    9384  }
    9485  catch (utility::IO_error e) {
    95     ok=false;
    96     *error << e.what() << std::endl;
     86    suite.add(false);
     87    suite.err() << e.what() << std::endl;
    9788  }
    9889
    9990  // checking permissions on non-existent file without ./ in path
    10091  try {
    101     *error << "FileUtil Test 3" << std::endl;
     92    suite.err() << "FileUtil Test 3" << std::endl;
    10293    bool testval=true;
    10394    // filename below should be unique
    10495    utility::FileUtil file("fileio_test.sdf34DSF");
    10596    if (!(testval=file.exists()))
    106       *error << "\tfile does not exist: " << file.path() << std::endl;
    107     ok&=!testval;
     97      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
     98    suite.add(!testval);
    10899    if (!(testval=file.permissions("r")))
    109       *error << "\tr permission on " << file.path() << std::endl;
    110     ok&=!testval;
     100      suite.err() << "\tr permission on " << file.path() << std::endl;
     101    suite.add(!testval);
    111102    if (!(testval=file.permissions("rwx")))
    112       *error << "\trwx permissions on " << file.path() << std::endl;
    113     ok&=!testval;
     103      suite.err() << "\trwx permissions on " << file.path() << std::endl;
     104    suite.add(!testval);
    114105    if (!(testval=file.permissions("w")))
    115       *error << "\tw permission on " << file.path() << std::endl;
    116     ok&=!testval; // 'w' on non-existent file ok if directory writeable
     106      suite.err() << "\tw permission on " << file.path() << std::endl;
     107    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
    117108    if (!(testval=file.permissions("d")))
    118       *error << "\td permission on " << file.path() << std::endl;
    119     ok&=!testval;
     109      suite.err() << "\td permission on " << file.path() << std::endl;
     110    suite.add(!testval);
    120111  }
    121112  catch (utility::IO_error e) {
    122     ok=false;
    123     *error << e.what() << std::endl;
     113    suite.add(false);
     114    suite.err() << e.what() << std::endl;
    124115  }
    125116
     
    127118  // directories in path
    128119  try {
    129     *error << "FileUtil Test 4" << std::endl;
     120    suite.err() << "FileUtil Test 4" << std::endl;
    130121    bool testval=true;
    131122     // intermediate dir must be unique!
    132123    utility::FileUtil file("./fileio_test.sdf34DSF/file");
    133124    if (!(testval=file.exists()))
    134       *error << "\tfile does not exist: " << file.path() << std::endl;
    135     ok&=!testval;
     125      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
     126    suite.add(!testval);
    136127    if ((testval=file.permissions("r")))
    137       *error << "\tr permission failed on " << file.path() << std::endl;
    138     ok&=testval;
     128      suite.err() << "\tr permission failed on " << file.path() << std::endl;
     129    suite.add(testval);
    139130    if (testval=file.permissions("rwx"))
    140       *error << "\trwx permissions failed on " << file.path() << std::endl;
    141     ok&=testval;
     131      suite.err() << "\trwx permissions failed on " << file.path() << std::endl;
     132    suite.add(testval);
    142133    if (testval=file.permissions("w"))
    143       *error << "\tw permission failed on " << file.path() << std::endl;
    144     ok&=testval;
     134      suite.err() << "\tw permission failed on " << file.path() << std::endl;
     135    suite.add(testval);
    145136    if (testval=file.permissions("d"))
    146       *error << "\td permission failed on " << file.path() << std::endl;
    147     ok&=testval;
     137      suite.err() << "\td permission failed on " << file.path() << std::endl;
     138    suite.add(testval);
    148139  }
    149140  catch (utility::IO_error e) {
    150     ok=false;
    151     *error << e.what() << std::endl;
     141    suite.add(false);
     142    suite.err() << e.what() << std::endl;
    152143  }
    153144
     
    155146  // has been checked
    156147  try {
    157     *error << "FileUtil Test 5" << std::endl;
     148    suite.err() << "FileUtil Test 5" << std::endl;
    158149    bool testval=true;
    159150     // intermediate dir must be unique!
    160151    utility::FileUtil file("./fileio_test.sdf34DSF/file");
    161152    if (!(testval=file.exists()))
    162       *error << "\tfile does not exist: " << file.path() << std::endl;
    163     ok&=!testval;
     153      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
     154    suite.add(!testval);
    164155    utility::FileUtil file2("fileutil_test.cc");
    165156    if (!(testval=!file2.exists()))
    166       *error << "\tfile does exist: " << file2.path() << std::endl;
    167     ok&=!testval;
     157      suite.err() << "\tfile does exist: " << file2.path() << std::endl;
     158    suite.add(!testval);
    168159  }
    169160  catch (utility::IO_error e) {
    170     ok=false;
    171     *error << e.what() << std::endl;
     161    suite.add(false);
     162    suite.err() << e.what() << std::endl;
    172163  }
    173164
    174165  try {
    175     *error << "FileUtil Test 6" << std::endl;
     166    suite.err() << "FileUtil Test 6" << std::endl;
    176167    utility::FileUtil file("fileutil_test.cc");
    177168    file.permissions("rxwa"); // should throw
    178     ok = false;
     169    suite.add(false);
    179170  }
    180171  catch (std::invalid_argument& e) {
    181     *error << "Expected exception thrown with what: " << e.what() << std::endl;
     172    suite.err() << "Expected exception thrown with what: " << e.what() << std::endl;
    182173  }
    183174
    184  
    185  
    186 
    187   *error << (ok ? "OK" : "Failed") << std::endl;
    188   if (error!=&std::cerr)
    189     delete error;
    190 
    191   return (ok ? 0 : -1);
     175  return suite.return_value();
    192176}
  • trunk/test/score_test.cc

    r1121 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/classifier/Target.h"
    2527#include "yat/statistics/FoldChange.h"
     
    4143using namespace theplu::yat;
    4244
    43 int main(const int argc,const char* argv[])
     45int main(int argc, char* argv[])
    4446
    45   std::ostream* error;
    46   if (argc>1 && argv[1]==std::string("-v"))
    47     error = &std::cerr;
    48   else {
    49     error = new std::ofstream("/dev/null");
    50     if (argc>1)
    51       std::cout << "score_test -v : for printing extra information\n";
    52   }
    53   *error << "testing score classes" << std::endl;
    54   bool ok = true;
     47  test::Suite suite(argc, argv);
    5548
    56   *error << "testing AUC" << std::endl;
     49  suite.err() << "testing score classes" << std::endl;
     50
     51  suite.err() << "testing AUC" << std::endl;
    5752  utility::Vector value(31);
    5853  std::vector<std::string> label(31,"negative");
     
    6459  statistics::AUC auc(false);
    6560  double area = auc.score(target, value);
    66   if (area!=0.0){
    67     *error << "test_auc: area is " << area << " should be 0.0"
     61  if (!(suite.equal(1-area,1))){
     62    suite.err() << "test_auc: area is " << area << " should be 0.0"
    6863           << std::endl;
    69     ok = false;
     64    suite.add(false);
    7065  }
    7166  target.set_binary(0,false);
    7267  target.set_binary(1,true);
    7368  area = auc.score(target, value);
    74   if (area!=1.0){
    75     *error << "test_auc: area is " << area << " should be 1.0"
     69  if (!suite.equal(area,1.0)){
     70    suite.err() << "test_auc: area is " << area << " should be 1.0"
    7671           << std::endl;
    77     ok = false;
     72    suite.add(false);
    7873  }
    7974 
     
    9186  correct_area(2)=0.0;
    9287
    93   const double tol = 0.001;
    9488  for (size_t i=0; i<data.rows(); i++){
    9589    utility::VectorView vec(data,i);
    9690    if (vec.size()!=target2.size()){
    97       *error << "vec.size() is " << vec.size() << " and target2.size() is "
     91      suite.err() << "vec.size() is " << vec.size() << " and target2.size() is "
    9892             << target2.size() << ". Should be equal." << std::endl;
    99       ok=false;
     93      suite.add(false);
    10094    }
    10195    area = auc.score(target2,vec);
    102     if (area<correct_area(i)-tol || area>correct_area(i)+tol){
    103       *error << "test_roc: area is " << area << " should be "
     96    if (!suite.equal(area, correct_area(i)) ){
     97      suite.err() << "test_roc: area is " << area << " should be "
    10498             << correct_area(i) << std::endl;
    105       ok=false;
     99      suite.add(false);
    106100    }
    107101  }
     
    111105    utility::VectorView vec = data.row_view(i);
    112106    area = auc.score(target2, vec, weight);
    113     if (area<correct_area(i)-tol || area>correct_area(i)+tol){
    114       *error << "test_roc: weighted area is " << area << " should be "
     107    if (!suite.equal(area,correct_area(i)) ){
     108      suite.err() << "test_roc: weighted area is " << area << " should be "
    115109             << correct_area(i) << std::endl;
    116       ok=false;
     110      suite.add(false);
    117111    }
    118112  }
    119113
    120   *error << "testing FoldChange" << std::endl;
     114  suite.err() << "testing FoldChange" << std::endl;
    121115  statistics::FoldChange fold_change(true);
    122116
    123   *error << "testing tScore" << std::endl;
     117  suite.err() << "testing tScore" << std::endl;
    124118  statistics::tScore t_score(true);
    125119
    126   *error << "testing Pearson" << std::endl;
     120  suite.err() << "testing Pearson" << std::endl;
    127121  statistics::Pearson pearson(true);
    128122
    129   *error << "testing WilcoxonFoldChange" << std::endl;
     123  suite.err() << "testing WilcoxonFoldChange" << std::endl;
    130124  statistics::WilcoxonFoldChange wfc(true);
    131125
    132   *error << "testing SAMScore" << std::endl;
     126  suite.err() << "testing SAMScore" << std::endl;
    133127  statistics::SAMScore sam(1.0,true);
    134128
    135   *error << "testing SNRScore" << std::endl;
     129  suite.err() << "testing SNRScore" << std::endl;
    136130  statistics::SNRScore snr(true);
    137131
    138132
    139   if (ok)
    140     return 0;
    141   return -1;
     133  return suite.return_value();
    142134}
  • trunk/test/smart_ptr_test.cc

    r1121 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/utility/SmartPtr.h"
    2527#include "yat/utility/Matrix.h"
     
    2830#include <iostream>
    2931
    30 int main(const int argc,const char* argv[])
    31 
     32int main(int argc, char* argv[])
    3233
    3334  using namespace theplu::yat;
    3435
    35   std::ostream* error;
    36   if (argc>1 && argv[1]==std::string("-v"))
    37     error = &std::cerr;
    38   else {
    39     error = new std::ofstream("/dev/null");
    40     if (argc>1)
    41       std::cout << "smart_ptr_test -v : for printing extra information\n";
    42   }
    43   *error << "testing smart_ptr" << std::endl;
     36  test::Suite suite(argc, argv);
    4437
    45   bool ok = true;
     38  suite.err() << "testing smart_ptr" << std::endl;
    4639
    4740  utility::SmartPtr<utility::Matrix> m(new utility::Matrix(10,10));
     
    5144  }
    5245  else
    53     ok = false;
     46    suite.add(false);
    5447
    5548  m = m;
     
    5750
    5851  if (m->columns()!=10)
    59     ok = false;
     52    suite.add(false);
    6053
    61   if(ok)
    62     *error << "OK" << std::endl;
    63   else
    64     *error << "Failed" << std::endl;
    65   if (error!=&std::cerr)
    66     delete error;
    67 
    68 
    69   if (ok)
    70     return 0;
    71   return -1;
    72 
     54  return suite.return_value();
    7355}
  • trunk/test/statistics_test.cc

    r1210 r1248  
    2424*/
    2525
     26#include "Suite.h"
     27
    2628#include "yat/statistics/utility.h"
    2729#include "yat/utility/Vector.h"
     
    3234#include <cmath>
    3335
    34 int main()
     36int main(int argc, char* argv[])
    3537
    3638  using namespace theplu::yat;
     39  test::Suite suite(argc, argv);
     40
    3741  utility::Vector gsl_vec(10);
    3842  std::vector<double> data;
     
    4549  double m_gsl=statistics::median(gsl_vec.begin(), gsl_vec.end());
    4650  if (m!=4.5 || m!=m_gsl)
    47     return -1;
     51    suite.add(false);
    4852  statistics::percentile(data.begin(), data.end(), 100);
    4953  data.resize(1);
    5054  statistics::median(data.begin(), data.end());
    5155
    52   double tolerance=1e-10;
    5356  double skewness_gsl=statistics::skewness(gsl_vec);
    54   if (std::abs(skewness_gsl)>tolerance)
    55     return -1;
     57  if (!suite.equal(1-skewness_gsl, 1.0) )
     58    suite.add(false);
    5659  double kurtosis_gsl=statistics::kurtosis(gsl_vec);
    57   if (std::abs(kurtosis_gsl+1.5616363636363637113)>tolerance)
    58     return -1
     60  if (!(std::abs(kurtosis_gsl+1.5616363636363637113)<1e-10) )
     61    suite.add(false);
    5962  return 0;
    6063}
  • trunk/test/subset_generator_test.cc

    r1173 r1248  
    2222  02111-1307, USA.
    2323*/
     24
     25#include "Suite.h"
    2426
    2527#include "yat/classifier/BootstrapSampler.h"
     
    4143using namespace theplu::yat;
    4244
    43 bool class_count_test(const std::vector<size_t>&, std::ostream*);
    44 bool sample_count_test(const std::vector<size_t>&, std::ostream*);
    45 bool test_nested(std::ostream* error);
    46 bool test_cv(std::ostream*);
    47 bool test_creation(std::ostream* error);
    48 bool test_bootstrap(std::ostream* error);
    49 
    50 
    51 int main(const int argc,const char* argv[])
     45bool class_count_test(const std::vector<size_t>&, test::Suite&);
     46bool sample_count_test(const std::vector<size_t>&, test::Suite&);
     47bool test_nested(test::Suite&);
     48bool test_cv(test::Suite&);
     49bool test_creation(test::Suite&);
     50bool test_bootstrap(test::Suite&);
     51
     52
     53int main(int argc, char* argv[])
    5254
    53   std::ostream* error;
    54   if (argc>1 && argv[1]==std::string("-v"))
    55     error = &std::cerr;
    56   else {
    57     error = new std::ofstream("/dev/null");
    58     if (argc>1)
    59       std::cout << "subset_generator -v : for printing extra information\n";
    60   }
    61   *error << "testing subset_generator" << std::endl;
    62   bool ok = true;
    63 
    64   ok = ok && test_creation(error);
    65   ok = ok && test_nested(error);
    66   ok = ok && test_cv(error);
    67 
    68   if (ok)
    69     return 0;
    70   return -1;
    71 }
    72 
    73 
    74 bool test_creation(std::ostream* error)
     55  test::Suite suite(argc, argv);
     56  suite.err() << "testing subset_generator" << std::endl;
     57
     58  test_creation(suite);
     59  test_nested(suite);
     60  test_cv(suite);
     61
     62  return suite.return_value();
     63}
     64
     65
     66bool test_creation(test::Suite& suite)
    7567{
    7668  bool ok=true;
    7769  std::ifstream is("data/nm_target_bin.txt");
    78   *error << "loading target " << std::endl;
     70  suite.err() << "loading target " << std::endl;
    7971  classifier::Target target(is);
    8072  is.close();
    81   *error << "number of targets: " << target.size() << std::endl;
    82   *error << "number of classes: " << target.nof_classes() << std::endl;
     73  suite.err() << "number of targets: " << target.size() << std::endl;
     74  suite.err() << "number of classes: " << target.nof_classes() << std::endl;
    8375  is.open("data/nm_data_centralized.txt");
    84   *error << "loading data " << std::endl;
     76  suite.err() << "loading data " << std::endl;
    8577  utility::Matrix m(is);
    8678  is.close();
    8779  classifier::MatrixLookup data(m);
    88   *error << "number of samples: " << data.columns() << std::endl;
    89   *error << "number of features: " << data.rows() << std::endl;
     80  suite.err() << "number of samples: " << data.columns() << std::endl;
     81  suite.err() << "number of features: " << data.rows() << std::endl;
    9082  assert(data.columns()==target.size());
    9183
    92   *error << "building kernel" << std::endl;
     84  suite.err() << "building kernel" << std::endl;
    9385  classifier::PolynomialKernelFunction kf(1);
    9486  classifier::Kernel_SEV kernel_core(data,kf);
    9587  classifier::KernelLookup kernel(kernel_core);
    96   *error << "building Sampler" << std::endl;
     88  suite.err() << "building Sampler" << std::endl;
    9789  classifier::CrossValidationSampler sampler(target, 30, 3);
    9890
    9991  statistics::AUC score;
    10092  classifier::FeatureSelectorIR fs(score, 96, 0);
    101   *error << "building SubsetGenerator" << std::endl;
     93  suite.err() << "building SubsetGenerator" << std::endl;
    10294  classifier::SubsetGenerator<classifier::MatrixLookup>
    10395    subset_data(sampler, data, fs);
     
    10799}
    108100
    109 bool test_nested(std::ostream* error)
     101bool test_nested(test::Suite& suite)
    110102{
    111103  bool ok=true;
     
    114106  //
    115107
    116   *error << "\ntesting two nested crossplitters" << std::endl;
     108  suite.err() << "\ntesting two nested crossplitters" << std::endl;
    117109  std::vector<std::string> label(9);
    118110  label[0]=label[1]=label[2]="0";
     
    156148      if(test_target(i)!=target(test_index[i])) {
    157149        ok=false;
    158         *error << "ERROR: incorrect mapping of test indices" << std:: endl;
     150        suite.err() << "ERROR: incorrect mapping of test indices" << std:: endl;
    159151      }       
    160152    }
     
    180172          || test_index.size()+v_index.size()+t_index.size() !=  target.size()){
    181173        ok = false;
    182         *error << "ERROR: size of training samples, validation samples "
     174        suite.err() << "ERROR: size of training samples, validation samples "
    183175               << "and test samples in is invalid."
    184176               << std::endl;
     
    187179          v_index.size()!=3){
    188180        ok = false;
    189         *error << "ERROR: size of training, validation, and test samples"
     181        suite.err() << "ERROR: size of training, validation, and test samples"
    190182               << " is invalid."
    191183               << " Expected sizes to be 3" << std::endl;
     
    211203      }
    212204 
    213       ok = ok && sample_count_test(tv_sample_count,error);     
    214 
    215     }
    216     ok = ok && sample_count_test(v_sample_count,error);
    217     ok = ok && sample_count_test(t_sample_count,error);
     205      ok = ok && sample_count_test(tv_sample_count,suite);     
     206
     207    }
     208    ok = ok && sample_count_test(v_sample_count,suite);
     209    ok = ok && sample_count_test(t_sample_count,suite);
    218210   
    219     ok = ok && class_count_test(t_class_count,error);
    220     ok = ok && class_count_test(t_class_count2,error);
    221     ok = ok && class_count_test(v_class_count,error);
    222 
    223 
    224   }
    225   ok = ok && sample_count_test(test_sample_count,error);
    226   ok = ok && class_count_test(test_class_count,error);
     211    ok = ok && class_count_test(t_class_count,suite);
     212    ok = ok && class_count_test(t_class_count2,suite);
     213    ok = ok && class_count_test(v_class_count,suite);
     214
     215
     216  }
     217  ok = ok && sample_count_test(test_sample_count,suite);
     218  ok = ok && class_count_test(test_class_count,suite);
    227219 
    228220  if(test_value1[0]!=135 || test_value1[1]!=36 || test_value1[2]!=45 ||
    229221     test_value1[3]!=54) {
    230222    ok=false;
    231     *error << "ERROR: incorrect sums of test values in row 1"
     223    suite.err() << "ERROR: incorrect sums of test values in row 1"
    232224           << " found: " << test_value1[0] << ", "  << test_value1[1]
    233225           << ", "  << test_value1[2] << " and "  << test_value1[3]
     
    239231     test_value2[3]!=84) {
    240232    ok=false;
    241     *error << "ERROR: incorrect sums of test values in row 2"
     233    suite.err() << "ERROR: incorrect sums of test values in row 2"
    242234           << " found: " << test_value2[0] << ", "  << test_value2[1]
    243235           << ", "  << test_value2[2] << " and "  << test_value2[3]
     
    247239  if(t_value[0]!=270 || t_value[1]!=72 || t_value[2]!=90 || t_value[3]!=108)  {
    248240    ok=false;
    249     *error << "ERROR: incorrect sums of training values in row 1"
     241    suite.err() << "ERROR: incorrect sums of training values in row 1"
    250242           << " found: " << t_value[0] << ", "  << t_value[1]
    251243           << ", "  << t_value[2] << " and "  << t_value[3]
     
    255247  if(v_value[0]!=270 || v_value[1]!=72 || v_value[2]!=90 || v_value[3]!=108)  {
    256248    ok=false;
    257     *error << "ERROR: incorrect sums of validation values in row 1"
     249    suite.err() << "ERROR: incorrect sums of validation values in row 1"
    258250           << " found: " << v_value[0] << ", "  << v_value[1]
    259251           << ", "  << v_value[2] << " and "  << v_value[3]
     
    264256
    265257bool class_count_test(const std::vector<size_t>& class_count,
    266                       std::ostream* error
     258                      test::Suite& suite
    267259{
    268260  bool ok=true;
     
    270262    if (class_count[i]==0){
    271263      ok = false;
    272       *error << "ERROR: class " << i << " was not in set."
     264      suite.err() << "ERROR: class " << i << " was not in set."
    273265             << " Expected at least one sample from each class."
    274266             << std::endl;
     
    278270
    279271bool sample_count_test(const std::vector<size_t>& sample_count,
    280                        std::ostream* error
     272                       test::Suite& suite
    281273{
    282274  bool ok=true;
     
    284276    if (sample_count[i]!=1){
    285277      ok = false;
    286       *error << "ERROR: sample " << i << " was in a group " << sample_count[i]
     278      suite.err() << "ERROR: sample " << i << " was in a group " << sample_count[i]
    287279             << " times." << " Expected to be 1 time" << std::endl;
    288280    }
     
    292284
    293285
    294 bool test_bootstrap(std::ostream* error)
     286bool test_bootstrap(test::Suite& suite)
    295287{
    296288  bool ok=true;
     
    309301
    310302
    311 bool test_cv(std::ostream* error)
     303bool test_cv(test::Suite& suite)
    312304{
    313305  bool ok=true;
     
    330322        target.size()){
    331323      ok = false;
    332       *error << "ERROR: size of training samples plus "
     324      suite.err() << "ERROR: size of training samples plus "
    333325             << "size of validation samples is invalid." << std::endl;
    334326    }
    335327    if (cv.validation_index(j).size()!=3 && cv.validation_index(j).size()!=4){
    336328      ok = false;
    337       *error << "ERROR: size of validation samples is invalid."
     329      suite.err() << "ERROR: size of validation samples is invalid."
    338330             << "expected size to be 3 or 4" << std::endl;
    339331    }
     
    345337      class_count[target(cv.training_index(j)[i])]++;
    346338    }
    347     ok = ok && class_count_test(class_count,error);
    348   }
    349   ok = ok && sample_count_test(sample_count,error);
    350  
    351   return ok;
    352 }
     339    ok = ok && class_count_test(class_count,suite);
     340  }
     341  ok = ok && sample_count_test(sample_count,suite);
     342 
     343  return ok;
     344}
  • trunk/test/svd_test.cc

    r1151 r1248  
    2525*/
    2626
     27#include "Suite.h"
     28
    2729#include "yat/random/random.h"
    2830#include "yat/utility/Matrix.h"
     
    3234using namespace theplu::yat;
    3335
    34 double this_norm(const utility::Matrix& A)
     36void do_test(size_t m, size_t n, utility::SVD::SVDalgorithm algo,
     37             test::Suite& suite)
    3538{
    36   double sum=0.0;
    37   for (size_t i=0; i<A.rows(); ++i)
    38     for (size_t j=0; j<A.columns(); ++j)
    39       sum += A(i,j)*A(i,j);
    40   return sum;
    41 }
    42 
    43 
    44 
    45 bool test(size_t m, size_t n, utility::SVD::SVDalgorithm algo)
    46 {
    47   double MAXTOL=1e-13; // accepted error
    48 
    4939  // initialise a random test-matrix
    5040  theplu::yat::random::ContinuousUniform rnd;
     41
     42  // set seed to make test deterministic
     43  theplu::yat::random::RNG::instance()->seed(20);
     44
    5145  utility::Matrix A(m,n);
    5246  for (size_t i=0; i<m; ++i)
     
    6660  Areconstruct*=S;
    6761  Areconstruct*=Vtranspose;
    68   Areconstruct-=A;  // Expect null matrix
    69   double error = this_norm(Areconstruct);
    70   bool testerror=false;
    71   if (error>MAXTOL) {
    72     std::cerr << "test_svd: FAILED, algorithm " << algo
    73               << " recontruction error ("
    74               << error << ") > tolerance (" << MAXTOL << "), matrix dimension ("
    75               << m << ',' << n << ')' << std::endl;
    76     testerror=true;
     62  if (!suite.equal_range(A.begin(), A.end(), Areconstruct.begin(), 1000)) {
     63    suite.err() << "test_svd: FAILED, algorithm " << algo << std::endl;
     64    suite.err() << "reconstruction error" << std::endl;
     65    suite.add(false);
    7766  }
    7867
    7968  Vtranspose*=svd.V();  // Expect unity matrix
    80   error=this_norm(Vtranspose)-n;
    81   if (error>MAXTOL) {
    82     std::cerr << "test_svd: FAILED, algorithm " << algo
    83               << " V orthogonality error ("
    84               << error << ") > tolerance (" << MAXTOL << ')' << std::endl;
    85     testerror=true;
     69  Vtranspose += 1.0;
     70  utility::Matrix eye(Vtranspose.rows(), Vtranspose.columns(), 1.0);
     71  for (size_t i=0; i<eye.rows() && i<eye.columns(); ++i)
     72    eye(i,i)=2.0;
     73  if (!suite.equal_range(eye.begin(), eye.end(), Vtranspose.begin(), 100) ) {
     74    suite.err() << "test_svd: FAILED, algorithm " << algo
     75                << " V orthogonality error " << std::endl;
     76    suite.add(false);
    8677  }
    8778
     
    8980  Utranspose.transpose();
    9081  Utranspose*=svd.U();  // Expect unity matrix
    91   error=this_norm(Utranspose)-n;
    92   if (error>MAXTOL) {
    93     std::cerr << "test_svd: FAILED, algorithm " << algo
    94               << " U orthogonality error ("
    95               << error << ") > tolerance (" << MAXTOL << ')' << std::endl;
    96     testerror=true;
     82  Utranspose += 1.0;
     83  eye = utility::Matrix(Utranspose.rows(), Utranspose.columns(), 1.0);
     84  for (size_t i=0; i<eye.rows() && i<eye.columns(); ++i)
     85    eye(i,i)=2.0;
     86  if (!suite.equal_range(eye.begin(), eye.end(), Utranspose.begin(), 100) ) {
     87    suite.err() << "test_svd: FAILED, algorithm " << algo
     88                << " U orthogonality error " << std::endl;
     89    suite.add(false);
    9790  }
    98   return testerror;
    9991}
    10092
    10193
    10294
    103 int main(const int argc,const char* argv[])
     95int main(int argc, char* argv[])
    10496{
    105   bool testfail=false;
     97  theplu::yat::test::Suite suite(argc, argv);
    10698
    10799  // The GSL Jacobi, Golub-Reinsch, and modified Golub-Reinsch
    108100  // implementations supports rows>=columns matrix dimensions only
    109   testfail|=test(12,12,utility::SVD::GolubReinsch);
    110   testfail|=test(12,4,utility::SVD::GolubReinsch);
    111   testfail|=test(12,12,utility::SVD::ModifiedGolubReinsch);
    112   testfail|=test(12,4,utility::SVD::ModifiedGolubReinsch);
    113   testfail|=test(12,12,utility::SVD::Jacobi);
    114   testfail|=test(12,4,utility::SVD::Jacobi);
     101  do_test(12,12,utility::SVD::GolubReinsch, suite);
     102  do_test(12,4,utility::SVD::GolubReinsch, suite);
     103  do_test(12,12,utility::SVD::ModifiedGolubReinsch, suite);
     104  do_test(12,4,utility::SVD::ModifiedGolubReinsch, suite);
     105  do_test(12,12,utility::SVD::Jacobi, suite);
     106  do_test(12,4,utility::SVD::Jacobi, suite);
    115107
    116   return (testfail ? -1 : 0);
     108  return suite.return_value();
    117109}
  • trunk/test/svm_test.cc

    r1210 r1248  
    2424*/
    2525
     26#include "Suite.h"
     27
    2628#include "yat/classifier/SVM.h"
    2729#include "yat/classifier/Kernel.h"
     
    4345using namespace theplu::yat;
    4446
    45 int main(const int argc,const char* argv[])
     47int main( int argc, char* argv[])
    4648
    47 
    48   std::ostream* error;
    49   if (argc>1 && argv[1]==std::string("-v"))
    50     error = &std::cerr;
    51   else {
    52     error = new std::ofstream("/dev/null");
    53     if (argc>1)
    54       std::cout << "svm_test -v : for printing extra information\n";
    55   }
    56   *error << "testing svm" << std::endl;
    57   bool ok = true;
     49  test::Suite suite(argc, argv);
     50  suite.err() << "testing svm" << std::endl;
    5851
    5952  utility::Matrix data2_core(2,3);
     
    7770  for (size_t i=0; i<3; i++){
    7871    for (size_t j=0; j<3; j++)
    79       *error << kernel2(i,j) << " ";
    80     *error << std::endl;
     72      suite.err() << kernel2(i,j) << " ";
     73    suite.err() << std::endl;
    8174  }
    8275  classifier::KernelLookup kv2(kernel2);
    83   *error << "testing with linear kernel" << std::endl;
     76  suite.err() << "testing with linear kernel" << std::endl;
    8477  assert(kv2.rows()==target2.size());
    8578  classifier::SVM classifier2;
    86   *error << "training...";
     79  suite.err() << "training...";
    8780  classifier2.train(kv2, target2);
    88   *error << " done!" << std::endl;
     81  suite.err() << " done!" << std::endl;
    8982
    9083  double tmp=0;
     
    9689
    9790  if (tmp){
    98     *error << "ERROR: found " << tmp << " expected zero" << std::endl;
     91    suite.err() << "ERROR: found " << tmp << " expected zero" << std::endl;
    9992    return -1;
    10093  }
     
    10396  if (std::abs(classifier2.alpha()(1)-2)>tol ||
    10497      std::abs(classifier2.alpha()(2)-2)>tol){
    105     *error << "wrong alpha" << std::endl;
    106     *error << "alpha: " << classifier2.alpha() <<  std::endl;
    107     *error << "expected: 4 2 2" <<  std::endl;
     98    suite.err() << "wrong alpha" << std::endl;
     99    suite.err() << "alpha: " << classifier2.alpha() <<  std::endl;
     100    suite.err() << "expected: 4 2 2" <<  std::endl;
    108101
    109102    return -1;
     
    139132  theplu::yat::utility::Vector diff_alpha(alpha);
    140133  diff_alpha-=alpha_matlab;
    141   if (diff_alpha*diff_alpha> 1e-10 ){
    142     *error << "Difference to matlab alphas too large\n";
    143     ok=false;
     134  if (!(diff_alpha*diff_alpha<1e-10) ){
     135    suite.err() << "Difference to matlab alphas too large\n";
     136    suite.add(false);
    144137  }
    145138
     
    157150  double slack_bound=2e-7;
    158151  if (slack > slack_bound || std::isnan(slack)){
    159     *error << "Slack too large. Is the bias correct?\n";
    160     *error << "slack: " << slack << std::endl;
    161     *error << "expected less than " << slack_bound << std::endl;
    162     ok = false;
     152    suite.err() << "Slack too large. Is the bias correct?\n";
     153    suite.err() << "slack: " << slack << std::endl;
     154    suite.err() << "expected less than " << slack_bound << std::endl;
     155    suite.add(false);
    163156  }
    164157 
     
    166159  delete kf2;
    167160
    168   if (error!=&std::cerr)
    169     delete error;
    170 
    171   if(ok)
    172     return 0;
    173   return -1;
    174  
     161  return suite.return_value();
    175162}
  • trunk/test/target_test.cc

    r1134 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/classifier/Target.h"
    2527#include "yat/utility/Index.h"
     
    3234using namespace theplu::yat;
    3335
    34 int main(const int argc,const char* argv[])
     36int main( int argc, char* argv[])
    3537
    36   std::ostream* error;
    37   if (argc>1 && argv[1]==std::string("-v"))
    38     error = &std::cerr;
    39   else {
    40     error = new std::ofstream("/dev/null");
    41     if (argc>1)
    42       std::cout << "target_test -v : for printing extra information\n";
    43   }
    44   *error << "testing target" << std::endl;
    45   bool ok = true;
     38  test::Suite suite(argc, argv);
     39  suite.err() << "testing target" << std::endl;
    4640
    4741  // This test assumes classes are introduced in order of appearence
     
    6054  for (size_t i=0; i<facit.size(); i++){
    6155    if (target3(i)!=facit[i]){
    62       ok=false;
    63       *error << "target(" << i << ") is " << target3(0) << " expected "
     56      suite.add(false);
     57      suite.err() << "target(" << i << ") is " << target3(0) << " expected "
    6458             << facit[i] << std::endl;
    6559    }
     
    7064  classifier::Target sub_target(target3,utility::Index(sub));
    7165  if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){
    72     ok = false;
    73     *error << "error in constructor Target(const Target&, std::vector<size_t>&)"
     66    suite.add(false);
     67    suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)"
    7468           << "\nclasses are not preserved" << std::endl;
    7569  }
    7670  if (target3.binary(sub[0])!=sub_target.binary(0) ||
    7771      target3.binary(sub[1])!=sub_target.binary(1)){
    78     ok = false;
    79     *error << "error in constructor Target(const Target&, std::vector<size_t>&)"
     72    suite.add(false);
     73    suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)"
    8074           << "\nbinary classes are not preserved" << std::endl;
    8175  }
     
    8882  classifier::Target target(label);
    8983  if (target.nof_classes()!=3){
    90     ok=false;
    91     *error << "Expected number of classes to be 3" << std::endl;
     84    suite.add(false);
     85    suite.err() << "Expected number of classes to be 3" << std::endl;
    9286  }
    9387  if (target(0)!=0){
    94     ok=false;
    95     *error << "Error: target(0)!=0" << std::endl;
     88    suite.add(false);
     89    suite.err() << "Error: target(0)!=0" << std::endl;
    9690  }
    9791  if (target(20)!=2){
    98     ok=false;
    99     *error << "Error: target(20)!=2" << std::endl;
     92    suite.add(false);
     93    suite.err() << "Error: target(20)!=2" << std::endl;
    10094  }
    10195  if (!target.binary(0)){
    102     ok=false;
    103     *error << "Error: target.binary(0) not true" << std::endl;
     96    suite.add(false);
     97    suite.err() << "Error: target.binary(0) not true" << std::endl;
    10498  }   
    10599  if (target.binary(20)){
    106     ok=false;
    107     *error << "Error: target.binary(20) not false" << std::endl;
     100    suite.add(false);
     101    suite.err() << "Error: target.binary(20) not false" << std::endl;
    108102  }   
    109103  target.set_binary(2,true);
    110104  if (!target.binary(20)){
    111     ok=false;
    112     *error << "Error: target.binary(20) not true" << std::endl;
     105    suite.add(false);
     106    suite.err() << "Error: target.binary(20) not true" << std::endl;
    113107  }   
    114108  std::ifstream is("data/rank_target.txt");
     
    116110  is.close();
    117111  if (target2.size()!=6){
    118     ok=false;
    119     *error << "ERROR: size of target is " << target2.size() << " expected 6."
    120           << std::endl;
     112    suite.add(false);
     113    suite.err() << "ERROR: size of target is " << target2.size()
     114                << " expected 6." << std::endl;
    121115  }   
    122116
    123   if (ok)
    124     return 0;
    125   return -1;
     117  suite.return_value();
    126118}
  • trunk/test/ttest_test.cc

    r1145 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/classifier/Target.h"
    2527#include "yat/statistics/tTest.h"
     
    3436using namespace theplu::yat;
    3537
    36 int main(const int argc,const char* argv[])
     38int main(int argc, char* argv[])
    3739
    38   std::ostream* error;
    39   if (argc>1 && argv[1]==std::string("-v"))
    40     error = &std::cerr;
    41   else {
    42     error = new std::ofstream("/dev/null");
    43     if (argc>1)
    44       std::cout << "ttest_test -v : for printing extra information\n";
    45   }
    46   bool ok = true;
     40  test::Suite suite(argc, argv);
    4741
    48   *error << "testing ttest" << std::endl;
     42  suite.err() << "testing ttest" << std::endl;
    4943  utility::Vector value(31);
    5044  std::vector<std::string> label(31,"positive");
     
    6054  double p = test.p_value_one_sided();
    6155  double p2 = test.p_value();
    62   if (p2 != 2*p) {
    63     ok = false;
    64     *error << "Two-sided P-value should equal 2 * one-sided P-value.\n";
     56  if (!suite.equal(p2, 2*p) ){
     57    suite.add(false);
     58    suite.err() << "Two-sided P-value should equal 2 * one-sided P-value.\n";
    6559  }
    6660
    67   if (ok)
    68     return 0;
    69   return -1;
     61  return suite.return_value();
    7062}
  • trunk/test/utility_test.cc

    r1195 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/utility/utility.h"
    2527
     
    2830#include <string>
    2931
    30 int main(const int argc,const char* argv[])
    31 
     32int main(int argc, char* argv[])
    3233
    3334  using namespace theplu::yat;
    34 
    35   std::ostream* error;
    36   if (argc>1 && argv[1]==std::string("-v"))
    37     error = &std::cerr;
    38   else {
    39     error = new std::ofstream("/dev/null");
    40     if (argc>1)
    41       std::cout << "utility_test -v : for printing extra information\n";
    42   }
    43   *error << "testing utility ... " << std::endl;
    44 
    45   bool ok = true;
     35  test::Suite suite(argc, argv);
     36  suite.err() << "testing utility ... " << std::endl;
    4637
    4738  // test float/double
    4839  std::string s("1.2");
    4940  if (!utility::is_double(s)){
    50     ok=false;
     41    suite.add(false);
    5142  }
    5243  else if (!utility::is_float(s)) {
    53     ok=false;
     44    suite.add(false);
    5445  }
    5546  else if (utility::is_int(s)) {
    56     ok=false;
     47    suite.add(false);
    5748  }
    5849  else if (utility::is_nan(s)) {
    59     ok=false;
     50    suite.add(false);
    6051  }
    6152
     
    6354  s="23";
    6455  if (!utility::is_double(s)){
    65     ok=false;
     56    suite.add(false);
    6657  }
    6758  else if (!utility::is_float(s)) {
    68     ok=false;
     59    suite.add(false);
    6960  }
    7061  else if (!utility::is_int(s)) {
    71     ok=false;
     62    suite.add(false);
    7263  }
    7364  else if (utility::is_nan(s)) {
    74     ok=false;
     65    suite.add(false);
    7566  }
    7667
     
    7869  s=" nAn  ";
    7970  if (!utility::is_double(s)){
    80     ok=false;
     71    suite.add(false);
    8172  }
    8273  else if (!utility::is_float(s)) {
    83     ok=false;
     74    suite.add(false);
    8475  }
    8576  else if (!utility::is_int(s)) {
    86     ok=false;
     77    suite.add(false);
    8778  }
    8879  else if (!utility::is_nan(s)) {
    89     ok=false;
     80    suite.add(false);
    9081  }
    9182 
     
    9384  s=" 23 23   ";
    9485  if (utility::is_double(s)){
    95     ok=false;
     86    suite.add(false);
    9687  }
    9788  else if (utility::is_float(s)) {
    98     ok=false;
     89    suite.add(false);
    9990  }
    10091  else if (utility::is_int(s)) {
    101     ok=false;
     92    suite.add(false);
    10293  }
    10394  else if (utility::is_nan(s)) {
    104     ok=false;
     95    suite.add(false);
    10596  }
    10697
    10798  if (utility::convert<double>("1.23")!=1.23)
    108     ok = false;
     99    suite.add(false);
    109100  utility::convert<double>("-inf");
    110101  utility::convert<double>("inf");
    111102  utility::convert<double>("NaN");
    112103
    113   if(ok)
    114     *error << "OK" << std::endl;
    115   else
    116     *error << "Failed" << std::endl;
    117   if (error!=&std::cerr)
    118     delete error;
    119 
    120 
    121   if (ok)
    122     return 0;
    123   return -1;
    124 
     104  return suite.return_value();
    125105}
  • trunk/test/vector_test.cc

    r1151 r1248  
    2424  02111-1307, USA.
    2525*/
     26
     27#include "Suite.h"
    2628
    2729#include "yat/utility/Exception.h"
     
    4648}
    4749
    48 int main(const int argc,const char* argv[])
     50int main(int argc,char* argv[])
    4951
    50   std::ostream* message;
    51   if (argc>1 && argv[1]==std::string("-v"))
    52     message = &std::cerr;
    53   else {
    54     message = new std::ofstream("/dev/null");
    55     if (argc>1)
    56       std::cout << "vector_test -v : for printing extra "  << "information\n";
    57   }
    58   *message << "testing vector" << std::endl;
    59   bool ok = true;
     52  test::Suite suite(argc, argv);
     53  suite.err() << "testing vector" << std::endl;
    6054
    6155  utility::Vector vec(12);
     
    6458
    6559  // checking that shuffle works
    66   *message << "shuffle" << std::endl;
     60  suite.err() << "shuffle" << std::endl;
    6761  double sum_before = utility::sum(vec);
    6862  shuffle(vec);
    6963  double sum_after = utility::sum(vec);
    7064  if (sum_after!=sum_before){
    71     *message << "shuffle failed" << std::endl;
    72     ok = false;
     65    suite.err() << "shuffle failed" << std::endl;
     66    suite.add(false);
    7367  }
    7468
    7569  // checking that view works
    76   *message << "view" << std::endl;
     70  suite.err() << "view" << std::endl;
    7771  sum_before=0;
    7872  for (size_t i=0; i<vec.size(); i+=2)
     
    8074  utility::VectorView vec_view(vec,0,6,2);
    8175  sum_after=utility::sum(vec_view);
    82   ok &= (sum_after==sum_before);
     76  suite.add(suite.equal(sum_after, sum_before));
    8377  vec(0)=0;
    8478  vec_view(0)=24;
    85   ok &= (vec(0)==vec_view(0));
     79  suite.add(suite.equal(vec(0), vec_view(0)));
    8680
    8781  // Test of const view implementation (make sure no zero pointer is
     
    9084  // core dump!
    9185  {
    92     *message << "const view implementation" << std::endl;
     86    suite.err() << "const view implementation" << std::endl;
    9387    const utility::Vector vv(10,3.0);
    9488    utility::VectorConstView vview(vv,0,5,1);
     
    9791    vv2.mul(vview); // should work even without const since const arg passing
    9892    vv2.div(vview); // should work even without const since const arg passing
    99     ok &= (vview*vview == 3.0*3.0*vview.size());
     93    suite.add(suite.equal(vview*vview, 3.0*3.0*vview.size()));
    10094  }
    10195
     
    10397  // a non-view vector is copied
    10498  {
    105     *message << "copy constructor" << std::endl;
     99    suite.err() << "copy constructor" << std::endl;
    106100    utility::Vector vec2(vec);
    107     ok &= (vec.size()==vec2.size());
    108     ok &= (vec2==vec);
    109     ok &= (&vec2 != &vec);
    110     ok &= !vec2.isview();
     101    suite.add(vec.size()==vec2.size());
     102    suite.add(suite.equal_range(vec2.begin(), vec2.end(), vec.begin()));
     103    suite.add(&vec2 != &vec);
     104    suite.add(!vec2.isview());
    111105  }
    112106
     
    114108  // a view vector is copied
    115109  {
    116     *message << "copy contructor on view" << std::endl;
     110    suite.err() << "copy contructor on view" << std::endl;
    117111    utility::Vector vec3(vec_view);
    118     ok &= (vec_view.size()==vec3.size());
    119     ok &= (vec3 == vec_view);
    120     ok &= !vec3.isview();
     112    suite.add(vec_view.size()==vec3.size());
     113    suite.add(suite.equal_range(vec3.begin(), vec3.end(), vec_view.begin()));
     114    suite.add(!vec3.isview());
    121115  }
    122116
     
    124118  // differ in size
    125119  {
    126     *message << "assignment operator" << std::endl;
     120    suite.err() << "assignment operator" << std::endl;
    127121    // GSL will catch the error in this test there for the GSL error
    128122    // handling must be disabled until after the exception is
     
    138132    }
    139133    if (!exception_happens) {
    140       *message << "Vector assignment operator did not throw expected exception"
     134      suite.err() << "Vector assignment operator did not throw expected exception"
    141135               << std::endl;
    142       ok=false;
     136      suite.add(false);
    143137    }
    144138    gsl_set_error_handler(err_handler);
     
    148142  // a view is changed.
    149143  {
    150     *message << "assignment operator on view" << std::endl;
     144    suite.err() << "assignment operator on view" << std::endl;
    151145    vec(3)=vec(4)=vec(5)=13;
    152146    utility::VectorView vec_view(vec,3,3,1);
     
    154148    vec_view=vec2;
    155149    if (vec(3)!=vec_view(0) || vec(4)!=vec_view(1) || vec(5)!=vec_view(2)){
    156       *message << " failed\n";
    157       ok=false;
     150      suite.err() << " failed\n";
     151      suite.add(false);
    158152    }
    159153  }
     
    161155  // checking clone functionality
    162156  {
    163     *message << "clone functionality" << std::endl;
     157    suite.err() << "clone functionality" << std::endl;
    164158    bool this_ok=true;
    165     *message << "\tcloning normal vector" << std::endl;
     159    suite.err() << "\tcloning normal vector" << std::endl;
    166160    utility::Vector vec2(3,123.0);
    167161    vec2 = vec;
     
    174168    if (vec.gsl_vector_p()==vec2.gsl_vector_p())
    175169      this_ok=false;
    176     *message << "\tcloning vector view" << std::endl;
     170    suite.err() << "\tcloning vector view" << std::endl;
    177171    utility::VectorView* vec_view=new utility::VectorView(vec,3,3,1);
    178172    utility::VectorView vec_view2(*vec_view);
     
    185179        if ((*vec_view)(i)!=vec_view2(i))
    186180          this_ok=false;
    187     *message << "\tcloned vector view independence" << std::endl;
     181    suite.err() << "\tcloned vector view independence" << std::endl;
    188182    delete vec_view;
    189183    if (vec(3)!=vec_view2(0) || vec(4)!=vec_view2(1) || vec(5)!=vec_view2(2))
     
    191185
    192186    if (!this_ok) {
    193       *message << "FAIL: clone test" << std::endl;
    194       ok=false;
     187      suite.err() << "FAIL: clone test" << std::endl;
     188      suite.add(false);
    195189    }
    196190  }
     
    198192  // checking that reading vectors from properly formatted files works
    199193  try {
    200     *message << "stream" << std::endl;
     194    suite.err() << "stream" << std::endl;
    201195    std::string data1("data/vector1.data");
    202196    std::string data2("data/vector2.data");
     
    212206    std::ifstream data_stream4(data4.c_str());
    213207    utility::Vector vec1(data_stream1);
    214     ok &= (vec1.size()==9);
     208    suite.add(vec1.size()==9);
    215209    vec1=utility::Vector(data_stream2);
    216     ok &= (vec1.size()==9);
     210    suite.add(vec1.size()==9);
    217211    utility::Vector vec2(data_stream3);
    218     ok &= (vec2.size()==12);
     212    suite.add(vec2.size()==12);
    219213    vec2=utility::Vector(data_stream4);
    220     ok &= (vec2.size()==12);
     214    suite.add(vec2.size()==12);
    221215  } catch (utility::IO_error& err) {
    222     *message << err.what() << std::endl;
    223     ok=false;
     216    suite.err() << err.what() << std::endl;
     217    suite.add(false);
    224218  }
    225219
     
    227221  // vector stream output operator produces.
    228222  {
    229     *message << "checking that output stream is valid as an input stream"
     223    suite.err() << "checking that output stream is valid as an input stream"
    230224             << std::endl;
    231225    std::stringstream s;
    232226    s << vec;
    233227    utility::Vector vec2(s);
    234     ok &= (vec==vec2);
     228    suite.add(suite.equal_range(vec.begin(), vec.end(), vec2.begin()));
    235229  }
    236230
    237231  // Checking that badly formatted files are not accepted, or at least
    238232  // throws an exception for the programmer to catch.
    239   *message << "checking that bad stream are rejected" << std::endl;
     233  suite.err() << "checking that bad stream are rejected" << std::endl;
    240234  bool this_ok=false;
    241235  // Checking that unexpected characters in a stream gives error.
     
    246240    utility::Vector dummy(data_stream5); // this will give an exception
    247241  } catch (utility::IO_error& err) {
    248     *message << err.what() << std::endl;
     242    suite.err() << err.what() << std::endl;
    249243    this_ok=true; // good, exception thrown, test passed
    250244  }
     
    255249    utility::Vector dummy(data_stream); // this will give an exception
    256250  } catch (utility::IO_error& err) {
    257     *message << err.what() << std::endl;
     251    suite.err() << err.what() << std::endl;
    258252    this_ok=true; // good, exceoption thrown, test passed
    259253  }
    260   ok &= this_ok;
     254  suite.add(this_ok);
    261255
    262256  this_ok=false;
     
    267261    vec=utility::Vector(data_stream); // this will give an exception
    268262  } catch (utility::IO_error& err) {
    269     *message << err.what() << std::endl;
     263    suite.err() << err.what() << std::endl;
    270264    this_ok=true; // good, exceoption thrown, test passed
    271265  }
    272   ok &= this_ok;
     266  suite.add(this_ok);
    273267
    274268
     
    283277    utility::sort_index(dummy,vec3);
    284278    if(dummy.size()!=vec3.size()) {
    285       ok=false;
    286       *message << "Vector of sorted indices has incorrect size" << std::endl;
     279      suite.add(false);
     280      suite.err() << "Vector of sorted indices has incorrect size" << std::endl;
    287281    }
    288282    if(dummy[0]!=11 || dummy[dummy.size()-1]!=8) {
    289       ok=false;
    290       *message << "Vector of sorted indices incorrect" << std::endl;
     283      suite.add(false);
     284      suite.err() << "Vector of sorted indices incorrect" << std::endl;
    291285    }
    292286    size_t k=3;
     
    294288    utility::sort_smallest_index(dummy,k,vec3);
    295289    if(dummy.size()!=k) {
    296       ok=false;
    297       *message << "Vector of sorted smallest indices has incorrect size" << std::endl;
     290      suite.add(false);
     291      suite.err() << "Vector of sorted smallest indices has incorrect size" << std::endl;
    298292    }
    299293    if(dummy[0]!=11 || dummy[dummy.size()-1]!=9) {
    300       ok=false;
    301       *message << "Vector of sorted smallest indices incorrect" << std::endl;
     294      suite.add(false);
     295      suite.err() << "Vector of sorted smallest indices incorrect" << std::endl;
    302296    }
    303297
     
    305299    utility::sort_largest_index(dummy,k,vec3);
    306300    if(dummy.size()!=k) {
    307       ok=false;
    308       *message << "Vector of sorted largest indices has incorrect size" << std::endl;
     301      suite.add(false);
     302      suite.err() << "Vector of sorted largest indices has incorrect size" << std::endl;
    309303    }
    310304    if(dummy[0]!=8 || dummy[dummy.size()-1]!=5) {
    311       ok=false;
    312       *message << "Vector of sorted largest indices incorrect" << std::endl;
     305      suite.add(false);
     306      suite.err() << "Vector of sorted largest indices incorrect" << std::endl;
    313307    }
    314308  }
     
    319313    vec = vv;
    320314    if (vec.size()!=10){
    321       *message << "indirect self-assignment failed - size should be 10\n";
    322       ok = false;
     315      suite.err() << "indirect self-assignment failed - size should be 10\n";
     316      suite.add(false);
    323317    }
    324318    if (vec(0)!=2.3){
    325       *message << "indirect self-assignment failed - vec(0) should be 2.3\n";
    326       ok = false;
     319      suite.err() << "indirect self-assignment failed - vec(0) should be 2.3\n";
     320      suite.add(false);
    327321    }
    328322    utility::VectorView vv2(vec, 0, 5);
    329323    vec = vv2;
    330324    if (vec.size()!=5){
    331       *message << "indirect self-assignment failed - size should be 5\n";
    332       ok = false;
     325      suite.err() << "indirect self-assignment failed - size should be 5\n";
     326      suite.add(false);
    333327    }
    334328    if (vec(0)!=2.3){
    335       *message << "indirect self-assignment failed - vec(0) should be 2.3\n";
    336       ok = false;
     329      suite.err() << "indirect self-assignment failed - vec(0) should be 2.3\n";
     330      suite.add(false);
    337331    }
    338332  }
     
    345339  }
    346340
    347   if (!ok)
    348     *message << "vector test failed" << std::endl;
    349 
    350   if (message!=&std::cerr)
    351     delete message;
    352 
    353   return (ok ? 0 : -1);
     341  return suite.return_value();
    354342}
  • trunk/test/view_test.cc

    r1137 r1248  
    2222*/
    2323
     24#include "Suite.h"
     25
    2426#include "yat/utility/Matrix.h"
    2527#include "yat/utility/VectorBase.h"
     
    3234#include <string>
    3335
    34 int main(const int argc,const char* argv[])
     36int main(int argc, char* argv[])
    3537
    3638  using namespace theplu::yat::utility;
    37   std::ostream* message;
    38   if (argc>1 && argv[1]==std::string("-v"))
    39     message = &std::cerr;
    40   else {
    41     message = new std::ofstream("/dev/null");
    42     if (argc>1)
    43       std::cout << "vector_test -v : for printing extra "  << "information\n";
    44   }
    45   *message << "testing vector" << std::endl;
    46   bool ok = true;
     39  theplu::yat::test::Suite suite(argc, argv);
     40  suite.err() << "testing vector" << std::endl;
    4741
    4842  Matrix data1;
     
    5953  VectorView bajs(data2, 0, false);
    6054  if (a(0)!=data1(0,0)){
    61     *message << "Matrix::column_const_view failed\n";
    62     ok =false;
     55    suite.err() << "Matrix::column_const_view failed\n";
     56    suite.add(false);
    6357  }
    6458  if (b(0)!=data2(0,0)){
    65     *message << "Matrix::column_view failed\n";
    66     ok =false;
     59    suite.err() << "Matrix::column_view failed\n";
     60    suite.add(false);
    6761  }
    6862
    6963  data1.column_const_view(0);
    7064  if (b.size()!=3){
    71     ok =false;
    72     *message << "ERROR: expected size to be 3 - found " << b.size() << std::endl;
     65    suite.add(false);
     66    suite.err() << "ERROR: expected size to be 3 - found " << b.size() << std::endl;
    7367  }
    7468  if ( !(b(0)==1 && b(1)==(1) && b(2)==1) )
    75     ok = false;
     69    suite.add(false);
    7670
    7771  if (a.begin()!=data1.begin_column(0)){
    78     ok=false;
    79     *message << "VectorConstView::begin should equal "
     72    suite.add(false);
     73    suite.err() << "VectorConstView::begin should equal "
    8074             << "Matrix::begin_column(size_t)\n";
    8175  }
    8276  if (a.end()!=data1.end_column(0)) {
    83     ok=false;
    84     *message << "VectorConstView::end should equal "
     77    suite.add(false);
     78    suite.err() << "VectorConstView::end should equal "
    8579             << "Matrix::end_column(size_t)\n";
    8680  }
    8781  if (b.begin()!=data2.begin_column(0)){
    88     ok=false;
    89     *message << "VectorView::begin "
     82    suite.add(false);
     83    suite.err() << "VectorView::begin "
    9084             << b.begin().base() << " should equal\n"
    9185             << "Matrix::begin_column(size_t) "
     
    9387  }
    9488  if (b.end()!=data2.end_column(0)){
    95     ok=false;
    96     *message << "VectorView::end should equal "
     89    suite.add(false);
     90    suite.err() << "VectorView::end should equal "
    9791             << "Matrix::end_column(size_t)\n";
    9892  }
     
    10094  VectorView d=data2.row_view(0);
    10195  if (c.begin()!=data1.begin_row(0)) {
    102     ok=false;
    103     *message << "VectorConstView::begin should equal "
     96    suite.add(false);
     97    suite.err() << "VectorConstView::begin should equal "
    10498             << "Matrix::begin_row(size_t)\n";
    10599  }
    106100  if (c.end()!=data1.end_row(0)){
    107     ok=false;
    108     *message << "VectorConstView::end should equal "
     101    suite.add(false);
     102    suite.err() << "VectorConstView::end should equal "
    109103             << "Matrix::end_row(size_t)\n";
    110104  }
    111105  if (d.begin()!=data2.begin_row(0)){
    112     ok=false;
    113     *message << "VectorView::begin should equal "
     106    suite.add(false);
     107    suite.err() << "VectorView::begin should equal "
    114108             << "Matrix::begin_row(size_t)\n";
    115109  }
    116110  if (d.end()!=data2.end_row(0)){
    117     ok=false;
    118     *message << "VectorView::end should equal "
     111    suite.add(false);
     112    suite.err() << "VectorView::end should equal "
    119113             << "Matrix::end_row(size_t)\n";
    120114  }
     
    129123    delete vv;
    130124    if (vcv.size()!=10)
    131       ok = false;
     125      suite.add(false);
    132126    if (vcv(0)!=1.2)
    133       ok = false;
     127      suite.add(false);
    134128  }
    135129 
    136130
    137   if (ok)
    138     *message << "test is ok" << std::endl;
    139   else
    140     *message << "view test failed" << std::endl;
    141 
    142   if (message!=&std::cerr)
    143     delete message;
    144 
    145   return (ok ? 0 : -1);
     131  return suite.return_value();
    146132}
    147133
Note: See TracChangeset for help on using the changeset viewer.