Ignore:
Timestamp:
Mar 5, 2006, 10:30:03 AM (16 years ago)
Author:
Peter
Message:

forked tests and fixed bugs

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix_lookup_test.cc

    r536 r537  
    22
    33#include <c++_tools/gslapi/matrix.h>
    4 #include <c++_tools/classifier/DataLookup1D.h>
    5 #include <c++_tools/classifier/KernelLookup.h>
    6 #include <c++_tools/classifier/Kernel_SEV.h>
    74#include <c++_tools/classifier/MatrixLookup.h>
    8 #include <c++_tools/classifier/PolynomialKernelFunction.h>
    9 
    105
    116#include <fstream>
     
    1611
    1712gslapi::matrix matrix(size_t n);
    18 
    19 bool test_data_lookup1D(std::ostream* error);
    20 bool test_kernel_lookup(std::ostream* error);
    21 bool test_matrix_lookup(std::ostream* error);
    2213
    2314int main(const int argc,const char* argv[])
     
    3324      std::cout << "lookup_test -v : for printing extra information\n";
    3425  }
     26  bool ok = true;
    3527
    36   *error << "Testing Lookup Classes" << std::endl;
    37   bool ok = test_data_lookup1D(error);
    38 
    39   ok = ok && test_matrix_lookup(error);
    40  
    41   ok = ok && test_kernel_lookup(error);
    42 
    43   if (ok)
    44     *error << "Ok." << std::endl;
    45 
    46   if (error!=&std::cerr)
    47     delete error;
    48 
    49   return (ok ? 0 : -1);
    50 }
    51 
    52 gslapi::matrix matrix(size_t n)
    53 {
    54   gslapi::matrix res(n,n);
    55   for (size_t i=0;i<n;i++)
    56     for (size_t j=0;j<n;j++)
    57       res(i,j)=10*i+j;
    58   return res;
    59 }
    60 
    61 bool test_data_lookup1D(std::ostream* error)
    62 {
    63   bool ok =true;
    64   *error << "Testing DataLookup1D" << std::endl;
    65   gslapi::matrix gsl_m1(matrix(5));
    66   std::vector<size_t> index_odd;
    67   index_odd.push_back(1);
    68   index_odd.push_back(3);
    69   std::vector<size_t> index_even;
    70   index_even.push_back(2);
    71   index_even.push_back(0);
    72   index_even.push_back(4);
    73   classifier::MatrixLookup m1(gsl_m1,index_odd,index_even);
    74   *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
    75          << "                           const size_t, const bool)...";
    76   classifier::DataLookup1D v1(m1,1);
    77   if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) ||
    78       v1(1)!=m1(1,1) ) {
    79     ok =false;
    80     *error << "\nERROR" << std::endl;
    81     *error << "size: " << v1.size() << " expected " << m1.columns() << "\n"
    82            << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n"
    83            << "v1(1): " << v1(1) << " expected " << m1(1,1)
    84            << std::endl;
    85   }
    86   else
    87     *error << "Ok" << std::endl;
    88 
    89   *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
    90          << "                           const size_t, const bool)...";
    91   classifier::DataLookup1D v2(m1,1,false);
    92   if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) {
    93     ok =false;
    94     *error << "\nERROR\n"
    95            << "size: " << v2.size() << " expected " << m1.rows() << "\n"
    96            << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n"
    97            << "v2(1): " << v2(1) << " expected " << m1(1,1)
    98            << std::endl;
    99   }
    100   else
    101     *error << "Ok" << std::endl;
    102 
    103   return ok;
    104 }
    105 
    106 bool test_kernel_lookup(std::ostream* error)
    107 {
    108   bool ok =true;
    109   *error << "\nTesting KernelLookup" << std::endl;
    110   gslapi::matrix data_core(1,5);
    111   for (size_t i=0; i<data_core.columns(); i++)
    112     data_core(0,i)=i;
    113   classifier::MatrixLookup data(data_core);
    114   classifier::PolynomialKernelFunction kf;
    115   classifier::Kernel_SEV kernel(data,kf);
    116   *error << "KernelLookup::KernelLookup(const Kernel&)...";
    117   classifier::KernelLookup k1(kernel);
    118   if (k1.rows()!=kernel.rows() || k1.columns()!=kernel.columns()) {
    119     ok =false;
    120     *error <<   "ERROR:" << std::endl;
    121     *error << "Dimensions do not agree." << std::endl;
    122   }
    123   for (size_t i=0; i<k1.rows(); i++)
    124     for (size_t j=0; j<k1.columns(); j++)
    125       if (k1(i,j)!=kernel(i,j)) {
    126         ok =false;
    127         *error << "ERROR:\n"
    128                << "KernelLookup::KernelLookup(const Kernel& data)"
    129                << std::endl;
    130         *error << "k(" << i << "," << j << ") is " << k1(i,j)
    131                << "expected " << kernel(i,j) << std::endl;
    132       }
    133  
    134   std::vector<size_t> index_odd;
    135   index_odd.push_back(1);
    136   index_odd.push_back(3);
    137   std::vector<size_t> index_even;
    138   index_even.push_back(2);
    139   index_even.push_back(0);
    140   index_even.push_back(5);
    141  
    142 
    143 
    144   return ok;
    145 }
    146 
    147 bool test_matrix_lookup(std::ostream* error)
    148 {
    149   bool ok =true;
    15028  *error << "\nTesting MatrixLookup" << std::endl;
    15129  *error << "MatrixLookup::MatrixLookup(const gslapi::matrix& data)...";
     
    234112    *error << "Ok" << std::endl;
    235113
     114  *error << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
     115         << "                           const double)...";
     116  classifier::MatrixLookup m7(103,112,12);
     117  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
     118    ok =false;
     119    *error << "ERROR:" << std::endl;
     120  }
     121  else
     122    *error << "Ok" << std::endl;
     123
    236124
    237125  *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
     
    257145  delete ValData;
    258146
     147  if (ok)
     148    *error << "Test Ok." << std::endl;
     149  if (error!=&std::cerr)
     150    delete error;
     151  return (ok ? 0 : -1);
     152}
    259153
    260   return ok;
     154gslapi::matrix matrix(size_t n)
     155{
     156  gslapi::matrix res(n,n);
     157  for (size_t i=0;i<n;i++)
     158    for (size_t j=0;j<n;j++)
     159      res(i,j)=10*i+j;
     160  return res;
    261161}
     162
     163
Note: See TracChangeset for help on using the changeset viewer.