Changeset 285


Ignore:
Timestamp:
Apr 21, 2005, 11:47:05 PM (18 years ago)
Author:
Peter
Message:

major revision of style of tests

Location:
trunk/test
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Makefile.am

    r284 r285  
    33## $Id$
    44
    5 check_PROGRAMS =                   test_alignment test_averagerpair test_consensus_inputranker \
     5TESTS = test_alignment test_averagerpair test_consensus_inputranker \
    66  test_crossvalidation  \
    7   test_inputranker test_kernel test_matrix test_merge test_nni test_pca \
     7  test_inputranker test_kernel test_matrix test_merge test_nni test_pca \
    88  test_regression_linear test_regression_local test_regression_naive \
    99  test_rnd test_roc   \
    1010  test_statistics test_stl_utility test_svd test_svm test_vector
     11
     12check_PROGRAMS = $(TESTS)
    1113
    1214LDADD = @top_srcdir@/$(CPP_TOOLS_LIB_LOCATION)/$(CPP_TOOLS_LIB) \
     
    1618
    1719AM_LDFLAGS = -g
     20
     21
    1822
    1923test_alignment_SOURCES = test_alignment.cc
  • trunk/test/test_alignment.cc

    r261 r285  
    8282
    8383  if (ok) {
    84     std::cout << "test_alignment: SUCCESS\n";
    8584    return 0;
    8685  }
    87   std::cerr << "test_alignment: FAILED\n";
    8886  return -1;
    8987
  • trunk/test/test_matrix.cc

    r271 r285  
    2222  if (m2!=m)
    2323    ok=false;
    24   std::ofstream my_out("tmp_test_matrix.txt");
     24  std::ofstream my_out("data/tmp_test_matrix.txt");
    2525  my_out << m2;
    2626  my_out.close();
    2727
    28   std::ifstream is("tmp_test_matrix.txt");
     28  std::ifstream is("data/tmp_test_matrix.txt");
    2929  matrix m3(is);
    3030  is.close();
     
    3333
    3434  if (ok) {
    35     std::cout << "test_matrix: SUCCESS\n";
    3635    return 0;
    3736  }
    38   std::cerr << "test_matrix: FAILED\n";
    3937  return -1;
    4038
  • trunk/test/test_merge.cc

    r107 r285  
    11// $Id$
    22
    3 #include <iostream>
     3#include "Merge.h"
     4
    45#include <fstream>
    56#include <string>
    67
    7 #include "FileIO.h"
    8 #include "Merge.h"
    9 
    10 // class for command line options
    11 class Parameter {
    12 public:
    13   Parameter(const Parameter&);  // not implemented
    14   Parameter(const int argc,const char *argv[]);
    15 
    16   inline const std::string& data(void) const { return data_file_; }
    17   inline const std::string& error(void) const { return error_file_; }
    18   inline const std::string& annots(void) const { return annots_file_; }
    19 
    20 private:
    21   void analyse(void);
    22   void defaults(void);
    23 
    24   std::string data_file_;
    25   std::string error_file_;
    26   std::string annots_file_;
    27 
    28 };
    29 
    30 Parameter::Parameter(const int argc,const char *argv[])
    31 {
    32   using namespace std;
    33   // check if help requested
    34   for (int i=1; i<argc; i++)
    35     if (string(argv[i])==string("-help") || string(argv[i])==string("-h")) {
    36       cout << "Please write some help information\n";
    37       exit(0);      // always exit after printing help
    38     }
    39 
    40   defaults();
    41 
    42   for (int i=1; i<argc; i++) {
    43     bool ok=false;
    44     string myargv(argv[i]);
    45     if (myargv==string("-data"))
    46       if ((i+1)<argc) {
    47         data_file_=argv[++i];
    48         ok=true;
    49       }
    50     if (myargv==string("-error"))
    51       if ((i+1)<argc) {
    52         error_file_=argv[++i];
    53         ok=true;
    54       }
    55     if (myargv==string("-annots"))
    56       if ((i+1)<argc) {
    57         annots_file_=argv[++i];
    58         ok=true;
    59       }
    60 
    61     if (!ok)
    62       std::cerr << "# Parameter::Parameter     Invalid option: "
    63                 << argv[i] << '\n';
    64   }
    65 
    66   analyse();
    67 }
    68 
    69 void Parameter::analyse(void)
    70 {
    71   using namespace theplu::cpptools;
    72   bool ok=true;
    73   if (FileIO().access_rights(data(),"r")) {
    74     std::cerr << "Cannot access data file " << data() << std::endl;
    75     ok=false;
    76   }
    77   if (FileIO().access_rights(error(),"r")) {
    78     std::cerr << "Cannot access error file " << error() << std::endl;
    79     ok=false;
    80   }if (FileIO().access_rights(annots(),"r")) {
    81     std::cerr << "Cannot access annotation file " << annots() << std::endl;
    82     ok=false;
    83   }
    84   if (!ok)
    85     std::exit(-1);
    86 }
    87 
    88 void Parameter::defaults(void)
    89 {
    90   data_file_="data/merge_matrix.data";
    91   error_file_="data/merge_error.data";
    92   annots_file_="data/merge_annots.data";
    93 }
    948
    959int main(const int argc,const char* argv[])
    9610{
    97   Parameter option(argc,argv);
    98   std::ifstream data_stream(option.data().c_str());
    99   std::ifstream error_stream(option.error().c_str());
    100   std::ifstream annots_stream(option.annots().c_str());
     11  std::ifstream data_stream("data/merge_matrix.data");
     12  std::ifstream error_stream("data/merge_error.data");
     13  std::ifstream annots_stream("data/merge_annots.data");
    10114  theplu::cpptools::Merge merge(data_stream,error_stream,annots_stream);
    102   std::cout << "data matrix" << std::endl;
    103   std::cout << merge.data_matrix() << std::endl;
    104   std::cout << "error matrix" << std::endl;
    105   std::cout << merge.error_matrix() << std::endl;
    10615  merge.do_something();
    107   std::cout << "new data matrix" << std::endl;
    108   std::cout << merge.data_matrix() << std::endl;
    109   std::cout << "new error matrix" << std::endl;
    110   std::cout << merge.error_matrix() << std::endl;
    11116  return 0; // normal exit
    11217}
  • trunk/test/test_nni.cc

    r196 r285  
    5050      // hardcoded number below should be changed.
    5151      if (fabs(control(i,j))>5e-13) {
    52         std::cerr << "test_nni: kNNI FAILED, cannot reproduce test result\n";
     52        //std::cerr << "test_nni: kNNI FAILED, cannot reproduce test result\n";
    5353        exit(-1); // calculation result out of accepted error bounds
    5454      }
    5555      else
    5656        if (!((control(i,j)==control(i,j)))) {
    57           std::cerr << "test_nni: kNNI FAILED, nan (not a number) encountered in test\n";
     57          //std::cerr << "test_nni: kNNI FAILED, nan (not a number) encountered in test\n";
    5858          exit(-1); // calucaltion error detected
    5959        }
     
    7676      // hardcoded number below should be changed.
    7777      if (fabs(control(i,j))>5e-13) {
    78         std::cerr << "test_nni: WeNNI FAILED, cannot reproduce test result\n";
     78        //std::cerr << "test_nni: WeNNI FAILED, cannot reproduce test result\n";
    7979        exit(-1); // calculation result out of accepted error bounds
    8080      }
    8181      else
    8282        if (!((control(i,j)==control(i,j)))) {
    83           std::cerr << "test_nni: WeNNI FAILED, nan (not a number) encountered in test\n";
     83          //std::cerr << "test_nni: WeNNI FAILED, nan (not a number) encountered in test\n";
    8484          exit(-1); // calucaltion error detected
    8585        }
    8686
    87   std::cout << "test_nni: SUCCESS, reproducing test result\n";
     87  //std::cout << "test_nni: SUCCESS, reproducing test result\n";
    8888  exit(0);        // normal exit
    8989}
  • trunk/test/test_pca.cc

    r42 r285  
    11// $Id$
    22
    3 //Test program for SVD class.
     3#include "PCA.h"
    44
     5#include <cmath>
    56#include <fstream>
    67#include <string>
    78#include <vector>
    89
    9 #include "PCA.h"
    1010
    11 using namespace std;
    1211
    13 template<class T>
    14 void break_line( vector<T>& collector, char* p_buff, const char& delimiter );
    15 void try_peters_test_file( const string& );
    16 
     12using namespace theplu;
    1713int main()
    1814{
    19   theplu::cpptools::PCA* mypca = new theplu::cpptools::PCA;
    20   if( mypca->test() ) { delete mypca; return 0; }
    21   else { delete mypca; return -1; }
    22 //   try_peters_test_file( "testdatapca/nm_trn.dat" );
     15  gslapi::matrix A( 3, 4 );
     16  for( size_t i = 0; i < 3; ++i )
     17    for( size_t j = 0; j < 4; ++j )
     18      A(i,j)= sin( static_cast<double>(i+j+2+(i+1)*(j+1)) );
    2319
    24 //   return 0;
     20  cpptools::PCA pca(A);
     21
     22// Print only matrix that is row-centered as PCA ( process() )
     23// will do that before calculation
     24  pca.process_transposed_problem();
     25  return true;
    2526}
    2627
    27 
    28 // This function run PCA on testdata from peter.
    29 // The last column is excluded because looks suspicious
    30 // (only 1s and 2s e t c ). The function use get_line
    31 // which will limit the lengths of a line of the file
    32 // to MAXLINE (defined below).
    33 void try_peters_test_file( const string& filename )
    34 {
    35   const size_t MAXLINE = 100000;
    36   char* buff = new char[ MAXLINE ];
    37   ifstream fin( filename.c_str() );
    38   vector< vector<double> > data;
    39 
    40   while( fin.peek() != EOF )
    41     {
    42      fin.getline( buff, MAXLINE );
    43      vector<double> row;
    44      break_line( row, buff, ' ' );
    45      row.pop_back();
    46      data.push_back( row );
    47     }
    48   delete[] buff;
    49 
    50   const size_t cols = data.size();
    51   const size_t rows = data[0].size();
    52  
    53   theplu::gslapi::matrix A( cols, rows );
    54 
    55   // Copy data into matrix A
    56   for( size_t i = 0; i < cols; ++i )
    57     for( size_t j = 0; j < rows; ++j )
    58       A(i,j)= data[i][j];
    59 
    60   //  cout << A << endl;
    61 
    62   theplu::cpptools::PCA* mypca = new theplu::cpptools::PCA( A.transpose() );
    63   mypca->process();
    64 
    65 
    66   // The following functions print the eigenvalues with the explained
    67   // intensity.
    68   cout << "Eigenvalues \t Explained intensity\n";
    69   for( size_t i = 0; i < 64; ++i )
    70     {
    71      printf( "%6.6f \t %6.6f \n", mypca->get_eigenvalue( i ),
    72        mypca->get_explained_intensity( i ) );
    73      //  cout << mypca->get_eigenvector( i ) << endl;
    74     }
    75   delete mypca;
    76 }
    77 
    78 
    79 
    80 template<class T>
    81 void break_line( vector<T>& row, char* p_buff, const char& delimiter )
    82 {
    83   while( true )
    84     {
    85      string number = "";
    86      while( *p_buff++ != '\0' )
    87        {
    88   if( *p_buff == delimiter )
    89     {
    90      if( number.size() > 0 )
    91        {
    92         row.push_back( atof( number.c_str() ) );
    93         break;
    94        }
    95     }
    96   else
    97     {
    98      number += *p_buff;
    99     }
    100        }
    101 
    102      if( *p_buff == '\0' )
    103        {
    104   // Add last number
    105   if( number.size() > 0 )
    106     {
    107      row.push_back( atof( number.c_str() ) );
    108     }
    109   break;
    110        }
    111     }
    112 }
  • trunk/test/test_regression_local.cc

    r279 r285  
    66#include "RegressionLinear.h"
    77#include "RegressionLocal.h"
     8#include "RegressionNaive.h"
    89#include "vector.h"
    910
     
    1112// Standard includes
    1213////////////////////
     14#include <cmath>
    1315#include <fstream>
    1416#include <iostream>
    15 #include <cmath>
     17#include <vector>
    1618
    1719using namespace theplu;
     20
     21bool test(statistics::Regression& r, statistics::RegressionKernel& k)
     22{
     23  statistics::RegressionLocal rl(r,k);
     24  for (size_t i=0; i<1000; i++){
     25    double x = i;
     26    double y = 10.0;
     27    rl.add(x, y);
     28  }
     29
     30  std::ofstream myout("data/tmp.txt");
     31  rl.fit(myout,0.1,1);
     32
     33  std::vector<double> y = rl.y();
     34  for (size_t i=0; i<y.size(); i++)
     35    if (y[i]!=10.0){
     36      return false;
     37    }
     38  return true;
     39}
    1840
    1941int main()
     
    2244  bool ok=true;
    2345 
    24   statistics::RegressionLinear r;
    25   statistics::RegressionKernelBox k;
    26   statistics::RegressionLocal rl(r,k);
    27   for (size_t i=0; i<1000; i++){
    28     double x = static_cast<double>(i)/100;
    29     double y = sin(x);
    30     rl.add(x, y);
    31   }
     46  statistics::RegressionNaive rn;
     47  statistics::RegressionKernelBox kb;
     48  ok = (ok && test(rn,kb));
    3249
    33   std::string tmp = "tmp"; 
    34   std::ofstream myout(tmp.c_str());
    35   rl.fit(myout,0.1,1);
    36    
    37   if (ok) {
    38     std::cout << "test_regression_local: SUCCESS\n";
     50  statistics::RegressionLinear rl;
     51  ok = (ok && test(rl,kb));
     52
     53
     54  if (ok)
    3955    return 0;
    40   }
    41   std::cerr << "test_regression_local: FAILED\n";
    4256  return -1;
    4357}
  • trunk/test/test_regression_naive.cc

    r284 r285  
    3636  }
    3737
    38   if (ok) {
    39     std::cout << "test_regression_naive: SUCCESS\n";
     38  if (ok)
    4039    return 0;
    41   }
    42   std::cerr << "test_regression_naive: FAILED\n";
    4340  return -1;
    4441}
  • trunk/test/test_rnd.cc

    r42 r285  
    3232    antal[ my_rnd->get_rnd_discrete() ]++;
    3333
    34   for( size_t i = 0; i < 5; ++i )
    35     cout << i << ": " << antal[ i ] << endl;
    36 
    37 
    3834  delete my_rnd;
    3935
  • trunk/test/test_roc.cc

    r196 r285  
    1818using namespace std;
    1919
    20 int main()
     20int main(const int argc,const char* argv[])
     21
     22  bool print=false;
     23  if (argc>1 && argv[1]==string("-p"))
     24    print=true;
    2125
    22 
    2326  bool ok = true;
    2427  theplu::gslapi::vector value(31);
     
    3134  double area = roc.score(target, value);
    3235  if (area!=1.0){
    33     cerr << "test_roc a: area is " << area << " should be 1.0" << endl;
     36    if (print)
     37      cerr << "test_roc a: area is " << area << " should be 1.0" << endl;
    3438    ok = false;
    3539  }
    3640  area = roc.score(-target, value);
    3741  if (area!=1.0){
    38     cerr << "test_roc: area is " << area << " should be 1.0" << endl;
     42    if (print)
     43      cerr << "test_roc: area is " << area << " should be 1.0" << endl;
    3944    ok = false;
    4045  }
     
    4348  double p_matlab = 0.00000115;
    4449  if (p/p_matlab > 1.01 | p/p_matlab < 0.99){
    45     cerr << "get_p_approx: p-value not correct" << endl;
     50    if (print)
     51      cerr << "get_p_approx: p-value not correct" << endl;
    4652    ok = false;
    4753  }
     
    4955  p = roc.p_value();
    5056  if (p > pow(10, -8.0) | p < pow(10, -9.0)){
    51     cerr << "get_p_exact: p-value not correct" << endl;
     57    if (print)
     58      cerr << "get_p_exact: p-value not correct" << endl;
    5259    ok = false;
    5360  }
     
    7178    area = roc.score(target2,vec);
    7279    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
    73       cerr << "test_roc: area is " << area << " should be "
    74            << correct_area(i) << endl;
    75       return -1;
     80      if (print){
     81        cerr << "test_roc: area is " << area << " should be "
     82             << correct_area(i) << endl;
     83      }
     84      ok=false;
    7685    }
    7786  }
     
    8291    area = roc.score(target2, vec, weight);
    8392    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
    84       cerr << "test_roc: weighted area is " << area << " should be "
    85            << correct_area(i) << endl;
    86       return -1;
     93      if (print){
     94        cerr << "test_roc: weighted area is " << area << " should be "
     95             << correct_area(i) << endl;
     96      }
     97      ok=false;
    8798    }
    8899  }
  • trunk/test/test_stl_utility.cc

    r269 r285  
    4343    ok=false;
    4444  }
    45   if (ok) {
    46     std::cout << "test_stl_utility: SUCCESS\n";
     45  if (ok)
    4746    return 0;
    48   }
    49   std::cerr << "test_stl_utility: FAILED\n";
    5047  return -1;
    5148
  • trunk/test/test_svm.cc

    r223 r285  
    5959 
    6060  // testing on non solvable problem
    61   cout << "testing \n";
    6261  theplu::gslapi::matrix data(3,1);
    6362  data(0,0)=-1;
     
    6766  theplu::gslapi::vector target(3,1.0);
    6867  target(2)=-1;
    69   std::cout << "target:\n" << target << "\n";
    7068  theplu::cpptools::KernelFunction* kf =
    7169    new theplu::cpptools::PolynomialKernelFunction();
  • trunk/test/test_vector.cc

    r259 r285  
    5555    ok=false;
    5656
    57   if (ok) {
    58     std::cout << "test_vector: SUCCESS\n";
     57  if (ok)
    5958    return 0;
    60   }
    61   std::cerr << "test_vector: FAILED\n";
    6259  return -1;
    6360}
Note: See TracChangeset for help on using the changeset viewer.