Changeset 1877


Ignore:
Timestamp:
Mar 21, 2009, 5:40:44 AM (12 years ago)
Author:
Peter
Message:

moving tests for iterator features from iterator_test.cc to class test::Suite so they can be utilized in several places.

Location:
trunk/test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Suite.h

    r1797 r1877  
    66/*
    77  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
     8  Copyright (C) 2009 Peter Johansson
    89
    910  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    104105     */
    105106    int return_value(void) const;
     107
     108    template<typename TrivialIterator>
     109    void test_trivial_iterator(const TrivialIterator&);
     110   
     111    template<typename InputIterator>
     112    void test_input_iterator(InputIterator&);
     113   
     114    template<typename OutputIterator>
     115    void test_output_iterator(OutputIterator&);
     116 
     117    template<typename ForwardIterator>
     118    void test_forward_iterator(ForwardIterator);
     119   
     120    template<typename BidirectionalIterator>
     121    void test_bidirectional_iterator(BidirectionalIterator);
     122   
     123    template<typename RandomAccessIterator>
     124    void test_random_access_iterator(RandomAccessIterator);
    106125
    107126    /**
     
    195214  }
    196215
     216  template<typename TrivialIterator>
     217  void Suite::test_trivial_iterator(const TrivialIterator& iter)
     218  {
     219    err() << "  testing Trivial features" << std::endl;
     220    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
     221    add(tmp==*iter);
     222  }
     223 
     224  template<typename InputIterator>
     225  void Suite::test_input_iterator(InputIterator& iter)
     226  {
     227    test_trivial_iterator(iter);
     228    err() << "  testing Input features" << std::endl;
     229    // just to check compilation
     230    if (false) {
     231      ++iter;
     232      iter++;
     233    }
     234  }
     235 
     236  template<typename OutputIterator>
     237  void Suite::test_output_iterator(OutputIterator& iter)
     238  {
     239    test_trivial_iterator(iter);
     240    err() << "  testing Output features" << std::endl;
     241  }
     242 
     243  template<typename ForwardIterator>
     244  void Suite::test_forward_iterator(ForwardIterator iter)
     245  {
     246    test_output_iterator(iter);
     247    test_input_iterator(iter);
     248    err() << "  testing Forward features" << std::endl;
     249   
     250    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
     251    // testing multiple traversing is possible and does not change the data
     252    ForwardIterator iter1 = iter;
     253    ++iter1;
     254    add(iter!=iter1);
     255    ForwardIterator iter2 = iter;
     256    ++iter2;
     257    add(tmp==*iter);
     258  }
     259 
     260  template<typename BidirectionalIterator>
     261  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
     262  {
     263    test_forward_iterator(iter);
     264    bool ok_cached = ok();
     265    err() << "  testing Bidirectional features" << std::endl;
     266    const BidirectionalIterator i = iter;
     267    BidirectionalIterator tmp = iter--;
     268    add(tmp==i);
     269    ++iter;
     270    tmp = --iter;
     271    if (!add(tmp==iter))
     272      err() << "operator-- failed" << std::endl;
     273    add(++tmp==i);
     274    if (ok_cached && !ok())
     275      err() << "failed" << std::endl;
     276  }
     277
     278  template<typename RandomAccessIterator>
     279  void Suite::test_random_access_iterator(RandomAccessIterator iter)
     280  {
     281    test_bidirectional_iterator(iter);
     282    err() << "  testing RandomAccess features" << std::endl;
     283    bool ok_cached = ok();
     284    RandomAccessIterator iter2 = iter;
     285    iter2 += 1;
     286    iter2 -= 1;
     287    RandomAccessIterator& iter3 = (iter2 += 1);
     288    RandomAccessIterator& iter4 = (iter3 -= 1);
     289    if (!add(iter2 == iter4))
     290      err() << "operator-(int) failed" << std::endl;
     291    add(++iter2 == iter3);
     292   
     293    RandomAccessIterator iter5 = iter + 0;
     294    RandomAccessIterator iter6 = 0 + iter;
     295    add(iter6 == iter5);
     296   
     297    RandomAccessIterator iter7 = iter - 0;
     298    add(iter7 == iter);
     299    add(iter7 - iter == 0);
     300    add(! (iter7<iter));
     301   
     302    typename RandomAccessIterator::value_type tmp = iter[0];
     303    typename RandomAccessIterator::value_type tmp2 = *iter;
     304    tmp = tmp; // avoid compiler warning
     305    if (!add(tmp == tmp2))
     306      err() << "operator[] failed" << std::endl;
     307    if (!add(iter[0] == *iter))
     308      err() << "operator[] failed" << std::endl;
     309    if (ok_cached && !ok())
     310      err() << "failed" << std::endl;
     311  }
     312
    197313}}}
    198314
  • trunk/test/iterator_test.cc

    r1797 r1877  
    6060void test_boost_util(test::Suite&);
    6161
    62 template<typename TrivialIterator>
    63 void test_trivial_iterator(const TrivialIterator&, test::Suite&);
    64 
    65 template<typename InputIterator>
    66 void test_input_iterator(InputIterator&, test::Suite&);
    67 
    68 template<typename OutputIterator>
    69 void test_output_iterator(OutputIterator&, test::Suite&);
    70 
    71 template<typename ForwardIterator>
    72 void test_forward_iterator(ForwardIterator, test::Suite&);
    73 
    74 template<typename BidirectionalIterator>
    75 void test_bidirectional_iterator(BidirectionalIterator, test::Suite&);
    76 
    77 template<typename RandomAccessIterator>
    78 void test_random_access_iterator(RandomAccessIterator, test::Suite&);
    79 
    8062void test_stride_iterator(test::Suite& suite);
    8163void test_weighted_iterator(test::Suite& suite);
     
    11597  classifier::DataLookup1D lookup(vec);
    11698  utility::Vector::iterator begin=vec.begin();
    117   test_random_access_iterator(begin, suite);
     99  suite.test_random_access_iterator(begin);
    118100  // test iterator to const_iterator conversion
    119101  utility::Vector::const_iterator ci = vec.begin();
    120   test_random_access_iterator(ci, suite);
     102  suite.test_random_access_iterator(ci);
    121103  ci = begin;
    122104  if (begin!=ci)
     
    131113  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
    132114  classifier::DataLookup1D::const_iterator lend=lookup.end();
    133   test_random_access_iterator(lbegin, suite);
     115  suite.test_random_access_iterator(lbegin);
    134116 
    135117  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
     
    162144  utility::DataIterator<classifier::DataLookupWeighted1D::const_iterator>
    163145    data_iter(aw2.begin());
    164   test_random_access_iterator(data_iter, suite);
    165   test_random_access_iterator(weight_iterator(aw2.begin()), suite);
     146  suite.test_random_access_iterator(data_iter);
     147  suite.test_random_access_iterator(weight_iterator(aw2.begin()));
    166148  suite.add(*data_iter == 1.0);
    167149  suite.add(*data_iterator(aw2.begin()) == 1.0);
     
    295277}
    296278
    297 template<typename TrivialIterator>
    298 void test_trivial_iterator(const TrivialIterator& iter, test::Suite& suite)
    299 {
    300   suite.err() << "  testing Trivial features" << std::endl;
    301   typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
    302   suite.add(tmp==*iter);
    303 }
    304 
    305 template<typename InputIterator>
    306 void test_input_iterator(InputIterator& iter, test::Suite& suite)
    307 {
    308   test_trivial_iterator(iter, suite);
    309   suite.err() << "  testing Input features" << std::endl;
    310   // just to check compilation
    311   if (false) {
    312     ++iter;
    313     iter++;
    314   }
    315 }
    316 
    317 template<typename OutputIterator>
    318 void test_output_iterator(OutputIterator& iter, test::Suite& suite)
    319 {
    320   test_trivial_iterator(iter, suite);
    321   suite.err() << "  testing Output features" << std::endl;
    322 }
    323 
    324 template<typename ForwardIterator>
    325 void test_forward_iterator(ForwardIterator iter, test::Suite& suite)
    326 {
    327   test_output_iterator(iter, suite);
    328   test_input_iterator(iter, suite);
    329   suite.err() << "  testing Forward features" << std::endl;
    330 
    331   typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
    332   // testing multiple traversing is possible and does not change the data
    333   ForwardIterator iter1 = iter;
    334   ++iter1;
    335   suite.add(iter!=iter1);
    336   ForwardIterator iter2 = iter;
    337   ++iter2;
    338   suite.add(tmp==*iter);
    339 
    340 }
    341 
    342 template<typename BidirectionalIterator>
    343 void test_bidirectional_iterator(BidirectionalIterator iter, test::Suite& suite)
    344 {
    345   test_forward_iterator(iter, suite);
    346   bool ok_cached = suite.ok();
    347   suite.err() << "  testing Bidirectional features" << std::endl;
    348   const BidirectionalIterator i = iter;
    349   BidirectionalIterator tmp = iter--;
    350   suite.add(tmp==i);
    351   ++iter;
    352   tmp = --iter;
    353   if (!suite.add(tmp==iter))
    354     suite.err() << "operator-- failed" << std::endl;
    355   suite.add(++tmp==i);
    356   if (ok_cached && !suite.ok())
    357     suite.err() << "failed" << std::endl;
    358 }
    359 
    360 template<typename RandomAccessIterator>
    361 void test_random_access_iterator(RandomAccessIterator iter, test::Suite& suite)
    362 {
    363   test_bidirectional_iterator(iter, suite);
    364   suite.err() << "  testing RandomAccess features" << std::endl;
    365   bool ok_cached = suite.ok();
    366   RandomAccessIterator iter2 = iter;
    367   iter2 += 1;
    368   iter2 -= 1;
    369   RandomAccessIterator& iter3 = (iter2 += 1);
    370   RandomAccessIterator& iter4 = (iter3 -= 1);
    371   if (!suite.add(iter2 == iter4))
    372     suite.err() << "operator-(int) failed" << std::endl;
    373   suite.add(++iter2 == iter3);
    374  
    375   RandomAccessIterator iter5 = iter + 0;
    376   RandomAccessIterator iter6 = 0 + iter;
    377   suite.add(iter6 == iter5);
    378 
    379   RandomAccessIterator iter7 = iter - 0;
    380   suite.add(iter7 == iter);
    381   suite.add(iter7 - iter == 0);
    382   suite.add(! (iter7<iter));
    383 
    384   typename RandomAccessIterator::value_type tmp = iter[0];
    385   typename RandomAccessIterator::value_type tmp2 = *iter;
    386   tmp = tmp; // avoid compiler warning
    387   if (!suite.add(tmp == tmp2))
    388     suite.err() << "operator[] failed" << std::endl;
    389   if (!suite.add(iter[0] == *iter))
    390     suite.err() << "operator[] failed" << std::endl;
    391   if (ok_cached && !suite.ok())
    392     suite.err() << "failed" << std::endl;
    393 }
    394 
    395279void test_stride_iterator(test::Suite& suite)
    396280{
     
    425309  utility::DataWeight tmp = *iter;
    426310
    427   test_bidirectional_iterator(iter, suite);
    428   test_random_access_iterator(iter, suite);
     311  suite.test_bidirectional_iterator(iter);
     312  suite.test_random_access_iterator(iter);
    429313  std::vector<double> data(vec.size());
    430314  std::vector<double> weight(vec.size());
     
    471355{
    472356  classifier::MatrixLookup ml(10, 10);
    473   test_random_access_iterator(ml.begin(), suite);
     357  suite.test_random_access_iterator(ml.begin());
    474358  std::vector<double> vec(ml.rows()*ml.columns());
    475359  std::copy(ml.begin(), ml.end(), vec.begin());
Note: See TracChangeset for help on using the changeset viewer.