Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:44:35

0001 #ifndef BOOST_NEW_ITERATOR_TESTS_HPP
0002 # define BOOST_NEW_ITERATOR_TESTS_HPP
0003 
0004 //
0005 // Copyright (c) David Abrahams 2001.
0006 // Copyright (c) Jeremy Siek 2001-2003.
0007 // Copyright (c) Thomas Witt 2002.
0008 //
0009 // Use, modification and distribution is subject to the
0010 // Boost Software License, Version 1.0.
0011 // (See accompanying file LICENSE_1_0.txt or copy at
0012 // http://www.boost.org/LICENSE_1_0.txt)
0013 //
0014 
0015 // This is meant to be the beginnings of a comprehensive, generic
0016 // test suite for STL concepts such as iterators and containers.
0017 //
0018 // Revision History:
0019 // 28 Oct 2002  Started update for new iterator categories
0020 //              (Jeremy Siek)
0021 // 28 Apr 2002  Fixed input iterator requirements.
0022 //              For a == b a++ == b++ is no longer required.
0023 //              See 24.1.1/3 for details.
0024 //              (Thomas Witt)
0025 // 08 Feb 2001  Fixed bidirectional iterator test so that
0026 //              --i is no longer a precondition.
0027 //              (Jeremy Siek)
0028 // 04 Feb 2001  Added lvalue test, corrected preconditions
0029 //              (David Abrahams)
0030 
0031 # include <iterator>
0032 # include <boost/static_assert.hpp>
0033 # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
0034 # include <boost/pending/iterator_tests.hpp>
0035 # include <boost/iterator/is_readable_iterator.hpp>
0036 # include <boost/iterator/is_lvalue_iterator.hpp>
0037 # include <boost/type_traits/is_same.hpp>
0038 # include <boost/mpl/bool.hpp>
0039 # include <boost/mpl/and.hpp>
0040 
0041 # include <boost/iterator/detail/config_def.hpp>
0042 # include <boost/detail/is_incrementable.hpp>
0043 # include <boost/core/lightweight_test.hpp>
0044 
0045 namespace boost {
0046 
0047 
0048 // Do separate tests for *i++ so we can treat, e.g., smart pointers,
0049 // as readable and/or writable iterators.
0050 template <class Iterator, class T>
0051 void readable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
0052 {
0053     T v2(*i1++);
0054     BOOST_TEST(v == v2);
0055 }
0056 
0057 template <class Iterator, class T>
0058 void readable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
0059 {}
0060 
0061 template <class Iterator, class T>
0062 void writable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
0063 {
0064     ++i1;  // we just wrote into that position
0065     *i1++ = v;
0066     Iterator x(i1++);
0067     (void)x;
0068 }
0069 
0070 template <class Iterator, class T>
0071 void writable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
0072 {}
0073 
0074 
0075 // Preconditions: *i == v
0076 template <class Iterator, class T>
0077 void readable_iterator_test(const Iterator i1, T v)
0078 {
0079   Iterator i2(i1); // Copy Constructible
0080   typedef typename std::iterator_traits<Iterator>::reference ref_t;
0081   ref_t r1 = *i1;
0082   ref_t r2 = *i2;
0083   T v1 = r1;
0084   T v2 = r2;
0085   BOOST_TEST(v1 == v);
0086   BOOST_TEST(v2 == v);
0087 
0088 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
0089   readable_iterator_traversal_test(i1, v, detail::is_postfix_incrementable<Iterator>());
0090 
0091   // I think we don't really need this as it checks the same things as
0092   // the above code.
0093   BOOST_STATIC_ASSERT(is_readable_iterator<Iterator>::value);
0094 # endif
0095 }
0096 
0097 template <class Iterator, class T>
0098 void writable_iterator_test(Iterator i, T v, T v2)
0099 {
0100   Iterator i2(i); // Copy Constructible
0101   *i2 = v;
0102 
0103 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
0104   writable_iterator_traversal_test(
0105       i, v2, mpl::and_<
0106           detail::is_incrementable<Iterator>
0107         , detail::is_postfix_incrementable<Iterator>
0108       >());
0109 # endif
0110 }
0111 
0112 template <class Iterator>
0113 void swappable_iterator_test(Iterator i, Iterator j)
0114 {
0115   Iterator i2(i), j2(j);
0116   typename std::iterator_traits<Iterator>::value_type bi = *i, bj = *j;
0117   iter_swap(i2, j2);
0118   typename std::iterator_traits<Iterator>::value_type ai = *i, aj = *j;
0119   BOOST_TEST(bi == aj && bj == ai);
0120 }
0121 
0122 template <class Iterator, class T>
0123 void constant_lvalue_iterator_test(Iterator i, T v1)
0124 {
0125   Iterator i2(i);
0126   typedef typename std::iterator_traits<Iterator>::value_type value_type;
0127   typedef typename std::iterator_traits<Iterator>::reference reference;
0128   BOOST_STATIC_ASSERT((is_same<const value_type&, reference>::value));
0129   const T& v2 = *i2;
0130   BOOST_TEST(v1 == v2);
0131 # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
0132   BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
0133   BOOST_STATIC_ASSERT(!is_non_const_lvalue_iterator<Iterator>::value);
0134 # endif
0135 }
0136 
0137 template <class Iterator, class T>
0138 void non_const_lvalue_iterator_test(Iterator i, T v1, T v2)
0139 {
0140   Iterator i2(i);
0141   typedef typename std::iterator_traits<Iterator>::value_type value_type;
0142   typedef typename std::iterator_traits<Iterator>::reference reference;
0143   BOOST_STATIC_ASSERT((is_same<value_type&, reference>::value));
0144   T& v3 = *i2;
0145   BOOST_TEST(v1 == v3);
0146 
0147   // A non-const lvalue iterator is not neccessarily writable, but we
0148   // are assuming the value_type is assignable here
0149   *i = v2;
0150 
0151   T& v4 = *i2;
0152   BOOST_TEST(v2 == v4);
0153 # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
0154   BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
0155   BOOST_STATIC_ASSERT(is_non_const_lvalue_iterator<Iterator>::value);
0156 # endif
0157 }
0158 
0159 template <class Iterator, class T>
0160 void forward_readable_iterator_test(Iterator i, Iterator j, T val1, T val2)
0161 {
0162   Iterator i2;
0163   Iterator i3(i);
0164   i2 = i;
0165   BOOST_TEST(i2 == i3);
0166   BOOST_TEST(i != j);
0167   BOOST_TEST(i2 != j);
0168   readable_iterator_test(i, val1);
0169   readable_iterator_test(i2, val1);
0170   readable_iterator_test(i3, val1);
0171 
0172   BOOST_TEST(i == i2++);
0173   BOOST_TEST(i != ++i3);
0174 
0175   readable_iterator_test(i2, val2);
0176   readable_iterator_test(i3, val2);
0177 
0178   readable_iterator_test(i, val1);
0179 }
0180 
0181 template <class Iterator, class T>
0182 void forward_swappable_iterator_test(Iterator i, Iterator j, T val1, T val2)
0183 {
0184   forward_readable_iterator_test(i, j, val1, val2);
0185   Iterator i2 = i;
0186   ++i2;
0187   swappable_iterator_test(i, i2);
0188 }
0189 
0190 // bidirectional
0191 // Preconditions: *i == v1, *++i == v2
0192 template <class Iterator, class T>
0193 void bidirectional_readable_iterator_test(Iterator i, T v1, T v2)
0194 {
0195   Iterator j(i);
0196   ++j;
0197   forward_readable_iterator_test(i, j, v1, v2);
0198   ++i;
0199 
0200   Iterator i1 = i, i2 = i;
0201 
0202   BOOST_TEST(i == i1--);
0203   BOOST_TEST(i != --i2);
0204 
0205   readable_iterator_test(i, v2);
0206   readable_iterator_test(i1, v1);
0207   readable_iterator_test(i2, v1);
0208 
0209   --i;
0210   BOOST_TEST(i == i1);
0211   BOOST_TEST(i == i2);
0212   ++i1;
0213   ++i2;
0214 
0215   readable_iterator_test(i, v1);
0216   readable_iterator_test(i1, v2);
0217   readable_iterator_test(i2, v2);
0218 }
0219 
0220 // random access
0221 // Preconditions: [i,i+N) is a valid range
0222 template <class Iterator, class TrueVals>
0223 void random_access_readable_iterator_test(Iterator i, int N, TrueVals vals)
0224 {
0225   bidirectional_readable_iterator_test(i, vals[0], vals[1]);
0226   const Iterator j = i;
0227   int c;
0228 
0229   for (c = 0; c < N-1; ++c)
0230   {
0231     BOOST_TEST(i == j + c);
0232     BOOST_TEST(*i == vals[c]);
0233     typename std::iterator_traits<Iterator>::value_type x = j[c];
0234     BOOST_TEST(*i == x);
0235     BOOST_TEST(*i == *(j + c));
0236     BOOST_TEST(*i == *(c + j));
0237     ++i;
0238     BOOST_TEST(i > j);
0239     BOOST_TEST(i >= j);
0240     BOOST_TEST(j <= i);
0241     BOOST_TEST(j < i);
0242   }
0243 
0244   Iterator k = j + N - 1;
0245   for (c = 0; c < N-1; ++c)
0246   {
0247     BOOST_TEST(i == k - c);
0248     BOOST_TEST(*i == vals[N - 1 - c]);
0249     typename std::iterator_traits<Iterator>::value_type x = j[N - 1 - c];
0250     BOOST_TEST(*i == x);
0251     Iterator q = k - c;
0252     BOOST_TEST(*i == *q);
0253     BOOST_TEST(i > j);
0254     BOOST_TEST(i >= j);
0255     BOOST_TEST(j <= i);
0256     BOOST_TEST(j < i);
0257     --i;
0258   }
0259 }
0260 
0261 } // namespace boost
0262 
0263 # include <boost/iterator/detail/config_undef.hpp>
0264 
0265 #endif // BOOST_NEW_ITERATOR_TESTS_HPP