Changeset 1477 for trunk/test


Ignore:
Timestamp:
Sep 4, 2008, 9:15:47 PM (13 years ago)
Author:
Peter
Message:

adding some tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1437 r1477  
    4040#include <algorithm>
    4141#include <fstream>
     42#include <iterator>
    4243#include <string>
    4344#include <vector>
     
    4647
    4748void old_main(test::Suite&);
     49
     50template<typename Iterator>
     51void is_weighted(Iterator, test::Suite&);
     52
     53bool is_weighted(utility::unweighted_iterator_tag x) { return false; }
     54bool is_weighted(utility::weighted_iterator_tag x) { return true; }
     55
    4856void test_boost_util(test::Suite&);
     57
     58template<typename TrivialIterator>
     59void test_trivial_iterator(const TrivialIterator&, test::Suite&);
     60
     61template<typename InputIterator>
     62void test_input_iterator(InputIterator&, test::Suite&);
     63
     64template<typename OutputIterator>
     65void test_output_iterator(OutputIterator&, test::Suite&);
     66
     67template<typename ForwardIterator>
     68void test_forward_iterator(ForwardIterator, test::Suite&);
     69
     70template<typename BidirectionalIterator>
     71void test_bidirectional_iterator(BidirectionalIterator, test::Suite&);
     72
     73template<typename RandomAccessIterator>
     74void test_random_access_iterator(RandomAccessIterator, test::Suite&);
    4975
    5076int main(int argc, char* argv[])
     
    5884}
    5985 
     86template<typename Iterator>
     87void is_weighted(Iterator i, test::Suite& suite)
     88{
     89  suite.err() << "testing that iterator is unweighted... ";
     90  typename utility::weighted_iterator_traits<Iterator>::type tag;
     91  if (is_weighted(tag))
     92    suite.err() << "ok.\n";
     93  else {
     94    suite.err() << "failed.\n";
     95    suite.add(false);
     96  }
     97   
     98}
     99
    60100
    61101void old_main(test::Suite& suite)
    62102{
    63   suite.err() << "testing utility::vector::iterator" << std::endl;
     103  suite.err() << "testing utility::Vector::iterator" << std::endl;
    64104  utility::Vector vec(12);
    65105  classifier::DataLookup1D lookup(vec);
    66106  utility::Vector::iterator begin=vec.begin();
     107  test_random_access_iterator(begin, suite);
    67108  // test iterator to const_iterator conversion
    68109  utility::Vector::const_iterator ci = vec.begin();
     110  test_random_access_iterator(ci, suite);
    69111  ci = begin;
    70112  if (begin!=ci)
     
    77119  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
    78120  classifier::DataLookup1D::const_iterator lend=lookup.end();
     121  test_random_access_iterator(lbegin, suite);
    79122 
    80123  suite.err() << "copy from DataLookup1D to Vector" << std::endl;
     
    238281    suite.err() << "test_bool_util failed" << std::endl;
    239282}
     283
     284template<typename TrivialIterator>
     285void test_trivial_iterator(const TrivialIterator& iter, test::Suite& suite)
     286{
     287  suite.err() << "  testing Trivial features" << std::endl;
     288  typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
     289  suite.add(tmp==*iter);
     290}
     291
     292template<typename InputIterator>
     293void test_input_iterator(InputIterator& iter, test::Suite& suite)
     294{
     295  test_trivial_iterator(iter, suite);
     296  suite.err() << "  testing Input features" << std::endl;
     297  // just to check compilation
     298  if (false) {
     299    ++iter;
     300    iter++;
     301  }
     302}
     303
     304template<typename OutputIterator>
     305void test_output_iterator(OutputIterator& iter, test::Suite& suite)
     306{
     307  test_trivial_iterator(iter, suite);
     308  suite.err() << "  testing Output features" << std::endl;
     309}
     310
     311template<typename ForwardIterator>
     312void test_forward_iterator(ForwardIterator iter, test::Suite& suite)
     313{
     314  test_output_iterator(iter, suite);
     315  test_input_iterator(iter, suite);
     316  suite.err() << "  testing Forward features" << std::endl;
     317
     318  typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
     319  // testing multiple traversing is possible and does not change the data
     320  ForwardIterator iter1 = iter;
     321  ++iter1;
     322  suite.add(iter!=iter1);
     323  ForwardIterator iter2 = iter;
     324  ++iter2;
     325  suite.add(tmp==*iter);
     326
     327}
     328
     329template<typename BidirectionalIterator>
     330void test_bidirectional_iterator(BidirectionalIterator iter, test::Suite& suite)
     331{
     332  test_forward_iterator(iter, suite);
     333  bool ok_cached = suite.ok();
     334  suite.err() << "  testing Bidirectional features" << std::endl;
     335  const BidirectionalIterator i = iter;
     336  BidirectionalIterator tmp = iter--;
     337  suite.add(tmp==i);
     338  ++iter;
     339  tmp = --iter;
     340  if (!suite.add(tmp==iter))
     341    suite.err() << "operator-- failed" << std::endl;
     342  suite.add(++tmp==i);
     343  if (ok_cached && !suite.ok())
     344    suite.err() << "failed" << std::endl;
     345}
     346
     347template<typename RandomAccessIterator>
     348void test_random_access_iterator(RandomAccessIterator iter, test::Suite& suite)
     349{
     350  test_bidirectional_iterator(iter, suite);
     351  suite.err() << "  testing RandomAccess features" << std::endl;
     352  bool ok_cached = suite.ok();
     353  RandomAccessIterator iter2 = iter;
     354  iter2 += 1;
     355  iter2 -= 1;
     356  RandomAccessIterator& iter3 = (iter2 += 1);
     357  RandomAccessIterator& iter4 = (iter3 -= 1);
     358  if (!suite.add(iter2 == iter4))
     359    suite.err() << "operator-(int) failed" << std::endl;
     360  suite.add(++iter2 == iter3);
     361 
     362  RandomAccessIterator iter5 = iter + 0;
     363  RandomAccessIterator iter6 = 0 + iter;
     364  suite.add(iter6 == iter5);
     365
     366  RandomAccessIterator iter7 = iter - 0;
     367  suite.add(iter7 == iter);
     368  suite.add(iter7 - iter == 0);
     369  suite.add(! (iter7<iter));
     370
     371  if (!suite.add(iter[0] == *iter))
     372    suite.err() << "operator[] failed" << std::endl;
     373  if (ok_cached && !suite.ok())
     374    suite.err() << "failed" << std::endl;
     375 
     376
     377}
Note: See TracChangeset for help on using the changeset viewer.