iterator_tests.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright David Abrahams and Jeremy Siek 2003.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_ITERATOR_TESTS_HPP
  6. # define BOOST_ITERATOR_TESTS_HPP
  7. // This is meant to be the beginnings of a comprehensive, generic
  8. // test suite for STL concepts such as iterators and containers.
  9. //
  10. // Revision History:
  11. // 28 Apr 2002 Fixed input iterator requirements.
  12. // For a == b a++ == b++ is no longer required.
  13. // See 24.1.1/3 for details.
  14. // (Thomas Witt)
  15. // 08 Feb 2001 Fixed bidirectional iterator test so that
  16. // --i is no longer a precondition.
  17. // (Jeremy Siek)
  18. // 04 Feb 2001 Added lvalue test, corrected preconditions
  19. // (David Abrahams)
  20. # include <iterator>
  21. # include <boost/static_assert.hpp>
  22. # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
  23. # include <boost/implicit_cast.hpp>
  24. # include <boost/core/ignore_unused.hpp>
  25. # include <boost/core/lightweight_test.hpp>
  26. # include <boost/type_traits/is_same.hpp>
  27. # include <boost/type_traits/is_pointer.hpp>
  28. # include <boost/type_traits/is_reference.hpp>
  29. namespace boost {
  30. // use this for the value type
  31. struct dummyT {
  32. dummyT() { }
  33. dummyT(detail::dummy_constructor) { }
  34. dummyT(int x) : m_x(x) { }
  35. int foo() const { return m_x; }
  36. bool operator==(const dummyT& d) const { return m_x == d.m_x; }
  37. int m_x;
  38. };
  39. }
  40. namespace boost {
  41. namespace iterators {
  42. // Tests whether type Iterator satisfies the requirements for a
  43. // TrivialIterator.
  44. // Preconditions: i != j, *i == val
  45. template <class Iterator, class T>
  46. void trivial_iterator_test(const Iterator i, const Iterator j, T val)
  47. {
  48. Iterator k;
  49. BOOST_TEST(i == i);
  50. BOOST_TEST(j == j);
  51. BOOST_TEST(i != j);
  52. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  53. T v = *i;
  54. #else
  55. typename std::iterator_traits<Iterator>::value_type v = *i;
  56. #endif
  57. BOOST_TEST(v == val);
  58. boost::ignore_unused(v);
  59. #if 0
  60. // hmm, this will give a warning for transform_iterator... perhaps
  61. // this should be separated out into a stand-alone test since there
  62. // are several situations where it can't be used, like for
  63. // integer_range::iterator.
  64. BOOST_TEST(v == i->foo());
  65. #endif
  66. k = i;
  67. BOOST_TEST(k == k);
  68. BOOST_TEST(k == i);
  69. BOOST_TEST(k != j);
  70. BOOST_TEST(*k == val);
  71. boost::ignore_unused(k);
  72. }
  73. // Preconditions: i != j
  74. template <class Iterator, class T>
  75. void mutable_trivial_iterator_test(const Iterator i, const Iterator j, T val)
  76. {
  77. *i = val;
  78. trivial_iterator_test(i, j, val);
  79. }
  80. // Preconditions: *i == v1, *++i == v2
  81. template <class Iterator, class T>
  82. void input_iterator_test(Iterator i, T v1, T v2)
  83. {
  84. Iterator i1(i);
  85. BOOST_TEST(i == i1);
  86. BOOST_TEST(!(i != i1));
  87. // I can see no generic way to create an input iterator
  88. // that is in the domain of== of i and != i.
  89. // The following works for istream_iterator but is not
  90. // guaranteed to work for arbitrary input iterators.
  91. //
  92. // Iterator i2;
  93. //
  94. // BOOST_TEST(i != i2);
  95. // BOOST_TEST(!(i == i2));
  96. BOOST_TEST(*i1 == v1);
  97. BOOST_TEST(*i == v1);
  98. // we cannot test for equivalence of (void)++i & (void)i++
  99. // as i is only guaranteed to be single pass.
  100. BOOST_TEST(*i++ == v1);
  101. boost::ignore_unused(i1);
  102. i1 = i;
  103. BOOST_TEST(i == i1);
  104. BOOST_TEST(!(i != i1));
  105. BOOST_TEST(*i1 == v2);
  106. BOOST_TEST(*i == v2);
  107. boost::ignore_unused(i1);
  108. // i is dereferencable, so it must be incrementable.
  109. ++i;
  110. // how to test for operator-> ?
  111. }
  112. // how to test output iterator?
  113. template <bool is_pointer> struct lvalue_test
  114. {
  115. template <class Iterator> static void check(Iterator)
  116. {
  117. # ifndef BOOST_NO_STD_ITERATOR_TRAITS
  118. typedef typename std::iterator_traits<Iterator>::reference reference;
  119. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  120. # else
  121. typedef typename Iterator::reference reference;
  122. typedef typename Iterator::value_type value_type;
  123. # endif
  124. BOOST_STATIC_ASSERT(boost::is_reference<reference>::value);
  125. BOOST_STATIC_ASSERT((boost::is_same<reference,value_type&>::value
  126. || boost::is_same<reference,const value_type&>::value
  127. ));
  128. }
  129. };
  130. # ifdef BOOST_NO_STD_ITERATOR_TRAITS
  131. template <> struct lvalue_test<true> {
  132. template <class T> static void check(T) {}
  133. };
  134. #endif
  135. template <class Iterator, class T>
  136. void forward_iterator_test(Iterator i, T v1, T v2)
  137. {
  138. input_iterator_test(i, v1, v2);
  139. Iterator i1 = i, i2 = i;
  140. BOOST_TEST(i == i1++);
  141. BOOST_TEST(i != ++i2);
  142. trivial_iterator_test(i, i1, v1);
  143. trivial_iterator_test(i, i2, v1);
  144. ++i;
  145. BOOST_TEST(i == i1);
  146. BOOST_TEST(i == i2);
  147. ++i1;
  148. ++i2;
  149. trivial_iterator_test(i, i1, v2);
  150. trivial_iterator_test(i, i2, v2);
  151. // borland doesn't allow non-type template parameters
  152. # if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
  153. lvalue_test<(boost::is_pointer<Iterator>::value)>::check(i);
  154. #endif
  155. }
  156. // Preconditions: *i == v1, *++i == v2
  157. template <class Iterator, class T>
  158. void bidirectional_iterator_test(Iterator i, T v1, T v2)
  159. {
  160. forward_iterator_test(i, v1, v2);
  161. ++i;
  162. Iterator i1 = i, i2 = i;
  163. BOOST_TEST(i == i1--);
  164. BOOST_TEST(i != --i2);
  165. trivial_iterator_test(i, i1, v2);
  166. trivial_iterator_test(i, i2, v2);
  167. --i;
  168. BOOST_TEST(i == i1);
  169. BOOST_TEST(i == i2);
  170. ++i1;
  171. ++i2;
  172. trivial_iterator_test(i, i1, v1);
  173. trivial_iterator_test(i, i2, v1);
  174. }
  175. // mutable_bidirectional_iterator_test
  176. template <class U> struct undefined;
  177. // Preconditions: [i,i+N) is a valid range
  178. template <class Iterator, class TrueVals>
  179. void random_access_iterator_test(Iterator i, int N, TrueVals vals)
  180. {
  181. bidirectional_iterator_test(i, vals[0], vals[1]);
  182. const Iterator j = i;
  183. int c;
  184. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  185. boost::ignore_unused<value_type>();
  186. for (c = 0; c < N-1; ++c) {
  187. BOOST_TEST(i == j + c);
  188. BOOST_TEST(*i == vals[c]);
  189. BOOST_TEST(*i == boost::implicit_cast<value_type>(j[c]));
  190. BOOST_TEST(*i == *(j + c));
  191. BOOST_TEST(*i == *(c + j));
  192. ++i;
  193. BOOST_TEST(i > j);
  194. BOOST_TEST(i >= j);
  195. BOOST_TEST(j <= i);
  196. BOOST_TEST(j < i);
  197. }
  198. Iterator k = j + N - 1;
  199. for (c = 0; c < N-1; ++c) {
  200. BOOST_TEST(i == k - c);
  201. BOOST_TEST(*i == vals[N - 1 - c]);
  202. BOOST_TEST(*i == boost::implicit_cast<value_type>(j[N - 1 - c]));
  203. Iterator q = k - c;
  204. boost::ignore_unused(q);
  205. BOOST_TEST(*i == *q);
  206. BOOST_TEST(i > j);
  207. BOOST_TEST(i >= j);
  208. BOOST_TEST(j <= i);
  209. BOOST_TEST(j < i);
  210. --i;
  211. }
  212. }
  213. // Precondition: i != j
  214. template <class Iterator, class ConstIterator>
  215. void const_nonconst_iterator_test(Iterator i, ConstIterator j)
  216. {
  217. BOOST_TEST(i != j);
  218. BOOST_TEST(j != i);
  219. ConstIterator k(i);
  220. BOOST_TEST(k == i);
  221. BOOST_TEST(i == k);
  222. k = i;
  223. BOOST_TEST(k == i);
  224. BOOST_TEST(i == k);
  225. boost::ignore_unused(k);
  226. }
  227. } // namespace iterators
  228. using iterators::undefined;
  229. using iterators::trivial_iterator_test;
  230. using iterators::mutable_trivial_iterator_test;
  231. using iterators::input_iterator_test;
  232. using iterators::lvalue_test;
  233. using iterators::forward_iterator_test;
  234. using iterators::bidirectional_iterator_test;
  235. using iterators::random_access_iterator_test;
  236. using iterators::const_nonconst_iterator_test;
  237. } // namespace boost
  238. #endif // BOOST_ITERATOR_TESTS_HPP