File indexing completed on 2025-01-30 09:50:00
0001
0002
0003
0004
0005 #ifndef BOOST_ITERATOR_TESTS_HPP
0006 # define BOOST_ITERATOR_TESTS_HPP
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
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
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
0049
0050
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
0067
0068
0069
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
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
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
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 BOOST_TEST(*i1 == v1);
0110 BOOST_TEST(*i == v1);
0111
0112
0113
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
0127 ++i;
0128
0129
0130 }
0131
0132
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
0182 # if !defined(BOOST_BORLANDC) || (BOOST_BORLANDC > 0x551)
0183 lvalue_test<(boost::is_pointer<Iterator>::value)>::check(i);
0184 #endif
0185 }
0186
0187
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
0213
0214 template <class U> struct undefined;
0215
0216
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
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 }
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 }
0289
0290 #endif