Changeset 1877 for trunk/test/Suite.h


Ignore:
Timestamp:
Mar 21, 2009, 5:40:44 AM (14 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.