Changeset 789 for trunk/test


Ignore:
Timestamp:
Mar 10, 2007, 9:07:13 PM (16 years ago)
Author:
Jari Häkkinen
Message:

Addresses #193. vector now works as outlined here. Added some
functionality. Added a clone function that facilitates resizing of
vectors. clone is needed since assignement operator functionality is
changed.

Location:
trunk/test
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix_test.cc

    r774 r789  
    186186    *error << "error sub-vector test 5" << std::endl;
    187187  }
    188   // Checking that vector::operator= indeed makes a view to become a
    189   // "normal" vector.
    190   v5subrow=utility::vector(23,22);
    191   double m5_sum6=0;
    192   for (size_t i=0; i<m5.rows(); ++i)
    193     for (size_t j=0; j<m5.columns(); ++j)
    194       m5_sum6+=m5(i,j);
    195   if (m5_sum6-m5_sum5>1e-13) {
    196     ok=false;
    197     *error << "error sub-vector test 6" << std::endl;
    198   }
    199188
    200189  // Checking that the memberfunction matrixwrapper::row() returns a
  • trunk/test/regression_test.cc

    r775 r789  
    502502  rl.fit(10, 100);
    503503
    504   utility::vector y = rl.y_predicted();
     504  utility::vector y(rl.y_predicted());
    505505  for (size_t i=0; i<y.size(); i++)
    506506    if (y(i)!=10.0){
  • trunk/test/vector_test.cc

    r786 r789  
    4343int main(const int argc,const char* argv[])
    4444
    45   bool print = (argc>1 && argv[1]==std::string("-p"));
     45  std::ostream* message;
     46  if (argc>1 && argv[1]==std::string("-v"))
     47    message = &std::cerr;
     48  else {
     49    message = new std::ofstream("/dev/null");
     50    if (argc>1)
     51      std::cout << argv[0] << " -v : for printing extra "  << "information\n";
     52  }
     53  *message << "testing vector" << std::endl;
    4654  bool ok = true;
    4755
     
    5159
    5260  // checking that shuffle works
     61  *message << "shuffle" << std::endl;
    5362  double sum_before = utility::sum(vec);
    5463  shuffle(vec);
    5564  double sum_after = utility::sum(vec);
    56   if (sum_after != sum_before)
    57     ok = false;
     65  ok &= (sum_after==sum_before);
    5866
    5967  // checking that view works
     68  *message << "view" << std::endl;
    6069  sum_before=0;
    6170  for (unsigned int i=0; i<vec.size(); i+=2)
     
    6372  utility::vector vec_view(vec,0,6,2);
    6473  sum_after=utility::sum(vec_view);
    65   if (sum_after != sum_before)
    66     ok = false;
     74  ok &= (sum_after==sum_before);
    6775  vec[0]=0;
    6876  vec_view[0]=24;
    69   if (vec[0]!=vec_view[0])
    70     ok=false;
    71 
    72   // checking that copy constructor creates an independent object
    73   utility::vector vec2(vec);
    74   if (vec.size()!=vec2.size())
    75     ok=false;
    76   if (&vec2 == &vec)
    77     ok=false;
    78   if (vec2.isview())
    79     ok=false;
     77  ok &= (vec[0]==vec_view[0]);
     78
     79  // Test of const view implementation (make sure no zero pointer is
     80  // used). Here we use the bad style of not making the view const!
     81  {
     82    *message << "const view implementation" << std::endl;
     83    const utility::vector vv(10,3.0);
     84    utility::vector vview(vv,0,5,1);
     85    // const utility::vector vview(vv,0,5,1); // this is the proper line
     86    utility::vector vv2(5,2.0);
     87    vv2.mul(vview); // should work even without const since const arg passing
     88    vv2.div(vview); // should work even without const since const arg passing
     89    ok &= (vview*vview == 3.0*3.0*vview.size());
     90  }
    8091
    8192  // checking that copy constructor creates an independent object when
    82   // copying a view
    83   vec2=vec_view;
    84   if (vec_view.size()!=vec2.size())
    85     ok=false;
    86   if ((&vec2 == &vec_view) || (&vec2 == &vec))
    87     ok=false;
    88   if (vec2.isview())
    89     ok=false;
    90   vec2[0]=0;
    91   vec_view[0]=24;
    92   if (vec2[0]==vec_view[0])
    93     ok=false;
     93  // a non-view vector is copied
     94  {
     95    *message << "copy constructor" << std::endl;
     96    utility::vector vec2(vec);
     97    ok &= (vec.size()==vec2.size());
     98    ok &= (vec2==vec);
     99    ok &= (&vec2 != &vec);
     100    ok &= !vec2.isview();
     101  }
     102
     103  // checking that copy constructor creates an independent object when
     104  // a view vector is copied
     105  {
     106    *message << "copy contructor on view" << std::endl;
     107    utility::vector vec3(vec_view);
     108    ok &= (vec_view.size()==vec3.size());
     109    ok &= (vec3 == vec_view);
     110    ok &= (&vec3 != &vec_view);
     111    ok &= !vec3.isview();
     112  }
     113
     114  // checking that assignment operator throws an exception if vectors
     115  // differ in size
     116  {
     117    *message << "assignment operator" << std::endl;
     118    bool exception_happens=false;
     119    try {
     120      utility::vector v(vec_view.size()+1,0.0);
     121      v=vec_view;
     122    } catch (utility::GSL_error& err) {
     123      exception_happens=true;
     124    }
     125    if (!exception_happens) {
     126      *message << "Vector assignment operator did not throw expected exception"
     127               << std::endl;
     128      ok=false;
     129    }
     130  }
     131
     132  // checking that assignment operator changes the underlying object when
     133  // a view is changed.
     134  {
     135    *message << "assignment operator on view" << std::endl;
     136    vec[3]=vec[4]=vec[5]=13;
     137    utility::vector vec_view(vec,3,3,1);
     138    utility::vector vec2(3,123.0);
     139    vec_view=vec2;
     140    if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2])
     141      ok=false;
     142  }
     143
     144  // checking clone functionality
     145  {
     146    *message << "clone functionality" << std::endl;
     147    bool this_ok=true;
     148    *message << "\tcloning normal vector" << std::endl;
     149    utility::vector vec2(3,123.0);
     150    vec2.clone(vec);
     151    if (vec.size()!=vec2.size())
     152      this_ok=false;
     153    else
     154      for (unsigned int i=0; i<vec.size(); ++i)
     155        if (vec(i)!=vec2(i))
     156          this_ok=false;
     157    if (vec.gsl_vector_p()==vec2.gsl_vector_p())
     158      this_ok=false;
     159    *message << "\tcloning vector view" << std::endl;
     160    utility::vector* vec_view=new utility::vector(vec,3,3,1);
     161    utility::vector vec_view2;
     162    vec_view2.clone(*vec_view);
     163    if (!vec_view2.isview())
     164      this_ok=false;
     165    if (vec_view->size()!=vec_view2.size())
     166      this_ok=false;
     167    else
     168      for (u_int i=0; i<vec_view2.size(); ++i)
     169        if ((*vec_view)(i)!=vec_view2(i))
     170          this_ok=false;
     171    *message << "\tcloned vector view independence" << std::endl;
     172    delete vec_view;
     173    if (vec[3]!=vec_view2[0] || vec[4]!=vec_view2[1] || vec[5]!=vec_view2[2])
     174      this_ok=false;
     175
     176    if (!this_ok) {
     177      *message << "FAIL: clone test" << std::endl;
     178      ok=false;
     179    }
     180  }
    94181
    95182  // checking that reading vectors from properly formatted files works
    96183  try {
     184    *message << "stream" << std::endl;
    97185    std::string data1("data/vector1.data");
    98186    std::string data2("data/vector2.data");
     
    107195    std::ifstream data_stream3(data3.c_str());
    108196    std::ifstream data_stream4(data4.c_str());
    109     vec=utility::vector(data_stream1);
    110     if (vec.size()!=9)
    111       ok=false;
    112     vec=utility::vector(data_stream2);
    113     if (vec.size()!=9)
    114       ok=false;
    115     vec=utility::vector(data_stream3);
    116     if (vec.size()!=12)
    117       ok=false;
    118     vec=utility::vector(data_stream4);
    119     if (vec.size()!=12)
    120       ok=false;
    121   } catch (utility::IO_error& err) {
    122     if (print)
    123       std::cerr << err.what() << std::endl;
     197    utility::vector vec1(data_stream1);
     198    ok &= (vec1.size()==9);
     199    vec1=utility::vector(data_stream2);
     200    ok &= (vec1.size()==9);
     201    utility::vector vec2(data_stream3);
     202    ok &= (vec2.size()==12);
     203    vec2=utility::vector(data_stream4);
     204    ok &= (vec2.size()==12);
     205  } catch (utility::IO_error& err) {
     206    *message << err.what() << std::endl;
    124207    ok=false;
    125208  }
     
    127210  // Checking that vector stream input operator can read whatever
    128211  // vector stream output operator produces.
    129   std::stringstream s;
    130   s << vec;
    131   vec2=utility::vector(s);
    132   if (!(vec==vec2))
    133     ok=false;
     212  {
     213    *message << "checking that output stream is valid as an input stream"
     214             << std::endl;
     215    std::stringstream s;
     216    s << vec;
     217    utility::vector vec2(s);
     218    ok &= (vec==vec2);
     219  }
    134220
    135221  // Checking that badly formatted files are not accepted, or at least
    136222  // throws an exception for the programmer to catch.
     223  *message << "checking that bad stream are rejected" << std::endl;
    137224  bool this_ok=false;
    138225  // Checking that unexpected characters in a stream gives error.
     
    141228    check_file_access(data5);
    142229    std::ifstream data_stream5(data5.c_str());
    143     vec=utility::vector(data_stream5); // this will give an exception
    144   } catch (utility::IO_error& err) {
    145     if (print)
    146       std::cerr << err.what() << std::endl;
     230    utility::vector dummy(data_stream5); // this will give an exception
     231  } catch (utility::IO_error& err) {
     232    *message << err.what() << std::endl;
    147233    this_ok=true; // good, exceoption thrown, test passed
    148234  }
     
    151237    check_file_access(data);
    152238    std::ifstream data_stream(data.c_str());
    153     vec=utility::vector(data_stream); // this will give an exception
    154   } catch (utility::IO_error& err) {
    155     if (print)
    156       std::cerr << err.what() << std::endl;
     239    utility::vector dummy(data_stream); // this will give an exception
     240  } catch (utility::IO_error& err) {
     241    *message << err.what() << std::endl;
    157242    this_ok=true; // good, exceoption thrown, test passed
    158243  }
    159   if (!this_ok)
    160     ok=false;
     244  ok &= this_ok;
     245
    161246  this_ok=false;
    162247  try {
     
    166251    vec=utility::vector(data_stream); // this will give an exception
    167252  } catch (utility::IO_error& err) {
    168     if (print)
    169       std::cerr << err.what() << std::endl;
     253    *message << err.what() << std::endl;
    170254    this_ok=true; // good, exceoption thrown, test passed
    171255  }
    172 
    173   if (!this_ok)
    174     ok=false;
    175 
    176   // test of const view implementation (make sure no zero pointer used.
    177   const utility::vector vv(10,3.0);
    178   const utility::vector vview(vv,0,5,1);
    179   utility::vector vv2(5,2.0);
    180   vv2.mul(vview); // should work without an abort due to zero pointer in mul
    181   vv2.div(vview); // should work without an abort due to zero pointer in mul
    182 
    183   if (vview*vview!= 3.0*3.0*vview.size())
    184     ok = false;
    185 
    186   if (print && !ok)
    187     std::cout << "vector_test failed" << std::endl;
    188 
    189  
     256  ok &= this_ok;
     257
     258  if (!ok)
     259    *message << "vector test failed" << std::endl;
    190260
    191261  return (ok ? 0 : -1);
Note: See TracChangeset for help on using the changeset viewer.