1 #ifndef BOOST_NEW_ITERATOR_TESTS_HPP
2 # define BOOST_NEW_ITERATOR_TESTS_HPP
5 // Copyright (c) David Abrahams 2001.
6 // Copyright (c) Jeremy Siek 2001-2003.
7 // Copyright (c) Thomas Witt 2002.
9 // Use, modification and distribution is subject to the
10 // Boost Software License, Version 1.0.
11 // (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
15 // This is meant to be the beginnings of a comprehensive, generic
16 // test suite for STL concepts such as iterators and containers.
19 // 28 Oct 2002 Started update for new iterator categories
21 // 28 Apr 2002 Fixed input iterator requirements.
22 // For a == b a++ == b++ is no longer required.
23 // See 24.1.1/3 for details.
25 // 08 Feb 2001 Fixed bidirectional iterator test so that
26 // --i is no longer a precondition.
28 // 04 Feb 2001 Added lvalue test, corrected preconditions
32 # include <boost/type_traits.hpp>
33 # include <boost/static_assert.hpp>
34 # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
35 # include <boost/detail/iterator.hpp>
36 # include <boost/pending/iterator_tests.hpp>
37 # include <boost/iterator/is_readable_iterator.hpp>
38 # include <boost/iterator/is_lvalue_iterator.hpp>
40 # include <boost/iterator/detail/config_def.hpp>
41 # include <boost/detail/is_incrementable.hpp>
42 # include <boost/detail/lightweight_test.hpp>
47 // Do separate tests for *i++ so we can treat, e.g., smart pointers,
48 // as readable and/or writable iterators.
49 template <class Iterator, class T>
50 void readable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
56 template <class Iterator, class T>
57 void readable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
60 template <class Iterator, class T>
61 void writable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
63 ++i1; // we just wrote into that position
69 template <class Iterator, class T>
70 void writable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
74 // Preconditions: *i == v
75 template <class Iterator, class T>
76 void readable_iterator_test(const Iterator i1, T v)
78 Iterator i2(i1); // Copy Constructible
79 typedef typename detail::iterator_traits<Iterator>::reference ref_t;
87 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
88 readable_iterator_traversal_test(i1, v, detail::is_postfix_incrementable<Iterator>());
90 // I think we don't really need this as it checks the same things as
92 BOOST_STATIC_ASSERT(is_readable_iterator<Iterator>::value);
96 template <class Iterator, class T>
97 void writable_iterator_test(Iterator i, T v, T v2)
99 Iterator i2(i); // Copy Constructible
102 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
103 writable_iterator_traversal_test(
105 detail::is_incrementable<Iterator>
106 , detail::is_postfix_incrementable<Iterator>
111 template <class Iterator>
112 void swappable_iterator_test(Iterator i, Iterator j)
114 Iterator i2(i), j2(j);
115 typename detail::iterator_traits<Iterator>::value_type bi = *i, bj = *j;
117 typename detail::iterator_traits<Iterator>::value_type ai = *i, aj = *j;
118 BOOST_TEST(bi == aj && bj == ai);
121 template <class Iterator, class T>
122 void constant_lvalue_iterator_test(Iterator i, T v1)
125 typedef typename detail::iterator_traits<Iterator>::value_type value_type;
126 typedef typename detail::iterator_traits<Iterator>::reference reference;
127 BOOST_STATIC_ASSERT((is_same<const value_type&, reference>::value));
129 BOOST_TEST(v1 == v2);
130 # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
131 BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
132 BOOST_STATIC_ASSERT(!is_non_const_lvalue_iterator<Iterator>::value);
136 template <class Iterator, class T>
137 void non_const_lvalue_iterator_test(Iterator i, T v1, T v2)
140 typedef typename detail::iterator_traits<Iterator>::value_type value_type;
141 typedef typename detail::iterator_traits<Iterator>::reference reference;
142 BOOST_STATIC_ASSERT((is_same<value_type&, reference>::value));
144 BOOST_TEST(v1 == v3);
146 // A non-const lvalue iterator is not neccessarily writable, but we
147 // are assuming the value_type is assignable here
151 BOOST_TEST(v2 == v4);
152 # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
153 BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
154 BOOST_STATIC_ASSERT(is_non_const_lvalue_iterator<Iterator>::value);
158 template <class Iterator, class T>
159 void forward_readable_iterator_test(Iterator i, Iterator j, T val1, T val2)
164 BOOST_TEST(i2 == i3);
167 readable_iterator_test(i, val1);
168 readable_iterator_test(i2, val1);
169 readable_iterator_test(i3, val1);
171 BOOST_TEST(i == i2++);
172 BOOST_TEST(i != ++i3);
174 readable_iterator_test(i2, val2);
175 readable_iterator_test(i3, val2);
177 readable_iterator_test(i, val1);
180 template <class Iterator, class T>
181 void forward_swappable_iterator_test(Iterator i, Iterator j, T val1, T val2)
183 forward_readable_iterator_test(i, j, val1, val2);
186 swappable_iterator_test(i, i2);
190 // Preconditions: *i == v1, *++i == v2
191 template <class Iterator, class T>
192 void bidirectional_readable_iterator_test(Iterator i, T v1, T v2)
196 forward_readable_iterator_test(i, j, v1, v2);
199 Iterator i1 = i, i2 = i;
201 BOOST_TEST(i == i1--);
202 BOOST_TEST(i != --i2);
204 readable_iterator_test(i, v2);
205 readable_iterator_test(i1, v1);
206 readable_iterator_test(i2, v1);
214 readable_iterator_test(i, v1);
215 readable_iterator_test(i1, v2);
216 readable_iterator_test(i2, v2);
220 // Preconditions: [i,i+N) is a valid range
221 template <class Iterator, class TrueVals>
222 void random_access_readable_iterator_test(Iterator i, int N, TrueVals vals)
224 bidirectional_readable_iterator_test(i, vals[0], vals[1]);
225 const Iterator j = i;
228 for (c = 0; c < N-1; ++c)
230 BOOST_TEST(i == j + c);
231 BOOST_TEST(*i == vals[c]);
232 typename detail::iterator_traits<Iterator>::value_type x = j[c];
234 BOOST_TEST(*i == *(j + c));
235 BOOST_TEST(*i == *(c + j));
243 Iterator k = j + N - 1;
244 for (c = 0; c < N-1; ++c)
246 BOOST_TEST(i == k - c);
247 BOOST_TEST(*i == vals[N - 1 - c]);
248 typename detail::iterator_traits<Iterator>::value_type x = j[N - 1 - c];
251 BOOST_TEST(*i == *q);
262 # include <boost/iterator/detail/config_undef.hpp>
264 #endif // BOOST_NEW_ITERATOR_TESTS_HPP