Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:50:00

0001 // Copyright David Abrahams and Jeremy Siek 2003.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef BOOST_ITERATOR_TESTS_HPP
0006 # define BOOST_ITERATOR_TESTS_HPP
0007 
0008 // This is meant to be the beginnings of a comprehensive, generic
0009 // test suite for STL concepts such as iterators and containers.
0010 //
0011 // Revision History:
0012 // 28 Apr 2002  Fixed input iterator requirements.
0013 //              For a == b a++ == b++ is no longer required.
0014 //              See 24.1.1/3 for details.
0015 //              (Thomas Witt)
0016 // 08 Feb 2001  Fixed bidirectional iterator test so that
0017 //              --i is no longer a precondition.
0018 //              (Jeremy Siek)
0019 // 04 Feb 2001  Added lvalue test, corrected preconditions
0020 //              (David Abrahams)
0021 
0022 # include <iterator>
0023 # include <boost/static_assert.hpp>
0024 # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
0025 # include <boost/core/ignore_unused.hpp>
0026 # include <boost/core/lightweight_test.hpp>
0027 # include <boost/type_traits/is_same.hpp>
0028 # include <boost/type_traits/is_pointer.hpp>
0029 # include <boost/type_traits/is_reference.hpp>
0030 
0031 namespace boost {
0032 
0033   // use this for the value type
0034 struct dummyT {
0035   dummyT() { }
0036   dummyT(detail::dummy_constructor) { }
0037   dummyT(int x) : m_x(x) { }
0038   int foo() const { return m_x; }
0039   bool operator==(const dummyT& d) const { return m_x == d.m_x; }
0040   int m_x;
0041 };
0042 
0043 }
0044 
0045 namespace boost {
0046 namespace iterators {
0047 
0048 // Tests whether type Iterator satisfies the requirements for a
0049 // TrivialIterator.
0050 // Preconditions: i != j, *i == val
0051 template <class Iterator, class T>
0052 void trivial_iterator_test(const Iterator i, const Iterator j, T val)
0053 {
0054   Iterator k;
0055   BOOST_TEST(i == i);
0056   BOOST_TEST(j == j);
0057   BOOST_TEST(i != j);
0058 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
0059   T v = *i;
0060 #else
0061   typename std::iterator_traits<Iterator>::value_type v = *i;
0062 #endif
0063   BOOST_TEST(v == val);
0064   boost::ignore_unused(v);
0065 #if 0
0066   // hmm, this will give a warning for transform_iterator...  perhaps
0067   // this should be separated out into a stand-alone test since there
0068   // are several situations where it can't be used, like for
0069   // integer_range::iterator.
0070   BOOST_TEST(v == i->foo());
0071 #endif
0072   k = i;
0073   BOOST_TEST(k == k);
0074   BOOST_TEST(k == i);
0075   BOOST_TEST(k != j);
0076   BOOST_TEST(*k == val);
0077   boost::ignore_unused(k);
0078 }
0079 
0080 
0081 // Preconditions: i != j
0082 template <class Iterator, class T>
0083 void mutable_trivial_iterator_test(const Iterator i, const Iterator j, T val)
0084 {
0085   *i = val;
0086   trivial_iterator_test(i, j, val);
0087 }
0088 
0089 
0090 // Preconditions: *i == v1, *++i == v2
0091 template <class Iterator, class T>
0092 void input_iterator_test(Iterator i, T v1, T v2)
0093 {
0094   Iterator i1(i);
0095 
0096   BOOST_TEST(i == i1);
0097   BOOST_TEST(!(i != i1));
0098 
0099   // I can see no generic way to create an input iterator
0100   // that is in the domain of== of i and != i.
0101   // The following works for istream_iterator but is not
0102   // guaranteed to work for arbitrary input iterators.
0103   //
0104   //   Iterator i2;
0105   //
0106   //   BOOST_TEST(i != i2);
0107   //   BOOST_TEST(!(i == i2));
0108 
0109   BOOST_TEST(*i1 == v1);
0110   BOOST_TEST(*i  == v1);
0111 
0112   // we cannot test for equivalence of (void)++i & (void)i++
0113   // as i is only guaranteed to be single pass.
0114   BOOST_TEST(*i++ == v1);
0115   boost::ignore_unused(i1);
0116 
0117   i1 = i;
0118 
0119   BOOST_TEST(i == i1);
0120   BOOST_TEST(!(i != i1));
0121 
0122   BOOST_TEST(*i1 == v2);
0123   BOOST_TEST(*i  == v2);
0124   boost::ignore_unused(i1);
0125 
0126   // i is dereferencable, so it must be incrementable.
0127   ++i;
0128 
0129   // how to test for operator-> ?
0130 }
0131 
0132 // how to test output iterator?
0133 
0134 
0135 template <bool is_pointer> struct lvalue_test
0136 {
0137     template <class Iterator> static void check(Iterator)
0138     {
0139 # ifndef BOOST_NO_STD_ITERATOR_TRAITS
0140         typedef typename std::iterator_traits<Iterator>::reference reference;
0141         typedef typename std::iterator_traits<Iterator>::value_type value_type;
0142 # else
0143         typedef typename Iterator::reference reference;
0144         typedef typename Iterator::value_type value_type;
0145 # endif
0146         BOOST_STATIC_ASSERT(boost::is_reference<reference>::value);
0147         BOOST_STATIC_ASSERT((boost::is_same<reference,value_type&>::value
0148                              || boost::is_same<reference,const value_type&>::value
0149             ));
0150     }
0151 };
0152 
0153 # ifdef BOOST_NO_STD_ITERATOR_TRAITS
0154 template <> struct lvalue_test<true> {
0155     template <class T> static void check(T) {}
0156 };
0157 #endif
0158 
0159 template <class Iterator, class T>
0160 void forward_iterator_test(Iterator i, T v1, T v2)
0161 {
0162   input_iterator_test(i, v1, v2);
0163 
0164   Iterator i1 = i, i2 = i;
0165 
0166   BOOST_TEST(i == i1++);
0167   BOOST_TEST(i != ++i2);
0168 
0169   trivial_iterator_test(i, i1, v1);
0170   trivial_iterator_test(i, i2, v1);
0171 
0172   ++i;
0173   BOOST_TEST(i == i1);
0174   BOOST_TEST(i == i2);
0175   ++i1;
0176   ++i2;
0177 
0178   trivial_iterator_test(i, i1, v2);
0179   trivial_iterator_test(i, i2, v2);
0180 
0181  // borland doesn't allow non-type template parameters
0182 # if !defined(BOOST_BORLANDC) || (BOOST_BORLANDC > 0x551)
0183   lvalue_test<(boost::is_pointer<Iterator>::value)>::check(i);
0184 #endif
0185 }
0186 
0187 // Preconditions: *i == v1, *++i == v2
0188 template <class Iterator, class T>
0189 void bidirectional_iterator_test(Iterator i, T v1, T v2)
0190 {
0191   forward_iterator_test(i, v1, v2);
0192   ++i;
0193 
0194   Iterator i1 = i, i2 = i;
0195 
0196   BOOST_TEST(i == i1--);
0197   BOOST_TEST(i != --i2);
0198 
0199   trivial_iterator_test(i, i1, v2);
0200   trivial_iterator_test(i, i2, v2);
0201 
0202   --i;
0203   BOOST_TEST(i == i1);
0204   BOOST_TEST(i == i2);
0205   ++i1;
0206   ++i2;
0207 
0208   trivial_iterator_test(i, i1, v1);
0209   trivial_iterator_test(i, i2, v1);
0210 }
0211 
0212 // mutable_bidirectional_iterator_test
0213 
0214 template <class U> struct undefined;
0215 
0216 // Preconditions: [i,i+N) is a valid range
0217 template <class Iterator, class TrueVals>
0218 void random_access_iterator_test(Iterator i, int N, TrueVals vals)
0219 {
0220   bidirectional_iterator_test(i, vals[0], vals[1]);
0221   const Iterator j = i;
0222   int c;
0223 
0224   typedef typename std::iterator_traits<Iterator>::value_type value_type;
0225   struct local
0226   {
0227     static value_type to_value_type(value_type v) { return v; }
0228   };
0229 
0230   for (c = 0; c < N-1; ++c) {
0231     BOOST_TEST(i == j + c);
0232     BOOST_TEST(*i == vals[c]);
0233     BOOST_TEST(*i == local::to_value_type(j[c]));
0234     BOOST_TEST(*i == *(j + c));
0235     BOOST_TEST(*i == *(c + j));
0236     ++i;
0237     BOOST_TEST(i > j);
0238     BOOST_TEST(i >= j);
0239     BOOST_TEST(j <= i);
0240     BOOST_TEST(j < i);
0241   }
0242 
0243   Iterator k = j + N - 1;
0244   for (c = 0; c < N-1; ++c) {
0245     BOOST_TEST(i == k - c);
0246     BOOST_TEST(*i == vals[N - 1 - c]);
0247     BOOST_TEST(*i == local::to_value_type(j[N - 1 - c]));
0248     Iterator q = k - c;
0249     boost::ignore_unused(q);
0250     BOOST_TEST(*i == *q);
0251     BOOST_TEST(i > j);
0252     BOOST_TEST(i >= j);
0253     BOOST_TEST(j <= i);
0254     BOOST_TEST(j < i);
0255     --i;
0256   }
0257 }
0258 
0259 // Precondition: i != j
0260 template <class Iterator, class ConstIterator>
0261 void const_nonconst_iterator_test(Iterator i, ConstIterator j)
0262 {
0263   BOOST_TEST(i != j);
0264   BOOST_TEST(j != i);
0265 
0266   ConstIterator k(i);
0267   BOOST_TEST(k == i);
0268   BOOST_TEST(i == k);
0269 
0270   k = i;
0271   BOOST_TEST(k == i);
0272   BOOST_TEST(i == k);
0273   boost::ignore_unused(k);
0274 }
0275 
0276 } // namespace iterators
0277 
0278 using iterators::undefined;
0279 using iterators::trivial_iterator_test;
0280 using iterators::mutable_trivial_iterator_test;
0281 using iterators::input_iterator_test;
0282 using iterators::lvalue_test;
0283 using iterators::forward_iterator_test;
0284 using iterators::bidirectional_iterator_test;
0285 using iterators::random_access_iterator_test;
0286 using iterators::const_nonconst_iterator_test;
0287 
0288 } // namespace boost
0289 
0290 #endif // BOOST_ITERATOR_TESTS_HPP