new_iterator_tests.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef BOOST_NEW_ITERATOR_TESTS_HPP
  2. # define BOOST_NEW_ITERATOR_TESTS_HPP
  3. //
  4. // Copyright (c) David Abrahams 2001.
  5. // Copyright (c) Jeremy Siek 2001-2003.
  6. // Copyright (c) Thomas Witt 2002.
  7. //
  8. // Use, modification and distribution is subject to the
  9. // Boost Software License, Version 1.0.
  10. // (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // This is meant to be the beginnings of a comprehensive, generic
  14. // test suite for STL concepts such as iterators and containers.
  15. //
  16. // Revision History:
  17. // 28 Oct 2002 Started update for new iterator categories
  18. // (Jeremy Siek)
  19. // 28 Apr 2002 Fixed input iterator requirements.
  20. // For a == b a++ == b++ is no longer required.
  21. // See 24.1.1/3 for details.
  22. // (Thomas Witt)
  23. // 08 Feb 2001 Fixed bidirectional iterator test so that
  24. // --i is no longer a precondition.
  25. // (Jeremy Siek)
  26. // 04 Feb 2001 Added lvalue test, corrected preconditions
  27. // (David Abrahams)
  28. # include <iterator>
  29. # include <boost/type_traits.hpp>
  30. # include <boost/static_assert.hpp>
  31. # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
  32. # include <boost/detail/iterator.hpp>
  33. # include <boost/pending/iterator_tests.hpp>
  34. # include <boost/iterator/is_readable_iterator.hpp>
  35. # include <boost/iterator/is_lvalue_iterator.hpp>
  36. # include <boost/iterator/detail/config_def.hpp>
  37. # include <boost/detail/is_incrementable.hpp>
  38. # include <boost/detail/lightweight_test.hpp>
  39. namespace boost {
  40. // Do separate tests for *i++ so we can treat, e.g., smart pointers,
  41. // as readable and/or writable iterators.
  42. template <class Iterator, class T>
  43. void readable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
  44. {
  45. T v2(*i1++);
  46. BOOST_TEST(v == v2);
  47. }
  48. template <class Iterator, class T>
  49. void readable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
  50. {}
  51. template <class Iterator, class T>
  52. void writable_iterator_traversal_test(Iterator i1, T v, mpl::true_)
  53. {
  54. ++i1; // we just wrote into that position
  55. *i1++ = v;
  56. Iterator x(i1++);
  57. (void)x;
  58. }
  59. template <class Iterator, class T>
  60. void writable_iterator_traversal_test(const Iterator i1, T v, mpl::false_)
  61. {}
  62. // Preconditions: *i == v
  63. template <class Iterator, class T>
  64. void readable_iterator_test(const Iterator i1, T v)
  65. {
  66. Iterator i2(i1); // Copy Constructible
  67. typedef typename detail::iterator_traits<Iterator>::reference ref_t;
  68. ref_t r1 = *i1;
  69. ref_t r2 = *i2;
  70. T v1 = r1;
  71. T v2 = r2;
  72. BOOST_TEST(v1 == v);
  73. BOOST_TEST(v2 == v);
  74. # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
  75. readable_iterator_traversal_test(i1, v, detail::is_postfix_incrementable<Iterator>());
  76. // I think we don't really need this as it checks the same things as
  77. // the above code.
  78. BOOST_STATIC_ASSERT(is_readable_iterator<Iterator>::value);
  79. # endif
  80. }
  81. template <class Iterator, class T>
  82. void writable_iterator_test(Iterator i, T v, T v2)
  83. {
  84. Iterator i2(i); // Copy Constructible
  85. *i2 = v;
  86. # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
  87. writable_iterator_traversal_test(
  88. i, v2, mpl::and_<
  89. detail::is_incrementable<Iterator>
  90. , detail::is_postfix_incrementable<Iterator>
  91. >());
  92. # endif
  93. }
  94. template <class Iterator>
  95. void swappable_iterator_test(Iterator i, Iterator j)
  96. {
  97. Iterator i2(i), j2(j);
  98. typename detail::iterator_traits<Iterator>::value_type bi = *i, bj = *j;
  99. iter_swap(i2, j2);
  100. typename detail::iterator_traits<Iterator>::value_type ai = *i, aj = *j;
  101. BOOST_TEST(bi == aj && bj == ai);
  102. }
  103. template <class Iterator, class T>
  104. void constant_lvalue_iterator_test(Iterator i, T v1)
  105. {
  106. Iterator i2(i);
  107. typedef typename detail::iterator_traits<Iterator>::value_type value_type;
  108. typedef typename detail::iterator_traits<Iterator>::reference reference;
  109. BOOST_STATIC_ASSERT((is_same<const value_type&, reference>::value));
  110. const T& v2 = *i2;
  111. BOOST_TEST(v1 == v2);
  112. # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
  113. BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
  114. BOOST_STATIC_ASSERT(!is_non_const_lvalue_iterator<Iterator>::value);
  115. # endif
  116. }
  117. template <class Iterator, class T>
  118. void non_const_lvalue_iterator_test(Iterator i, T v1, T v2)
  119. {
  120. Iterator i2(i);
  121. typedef typename detail::iterator_traits<Iterator>::value_type value_type;
  122. typedef typename detail::iterator_traits<Iterator>::reference reference;
  123. BOOST_STATIC_ASSERT((is_same<value_type&, reference>::value));
  124. T& v3 = *i2;
  125. BOOST_TEST(v1 == v3);
  126. // A non-const lvalue iterator is not neccessarily writable, but we
  127. // are assuming the value_type is assignable here
  128. *i = v2;
  129. T& v4 = *i2;
  130. BOOST_TEST(v2 == v4);
  131. # ifndef BOOST_NO_LVALUE_RETURN_DETECTION
  132. BOOST_STATIC_ASSERT(is_lvalue_iterator<Iterator>::value);
  133. BOOST_STATIC_ASSERT(is_non_const_lvalue_iterator<Iterator>::value);
  134. # endif
  135. }
  136. template <class Iterator, class T>
  137. void forward_readable_iterator_test(Iterator i, Iterator j, T val1, T val2)
  138. {
  139. Iterator i2;
  140. Iterator i3(i);
  141. i2 = i;
  142. BOOST_TEST(i2 == i3);
  143. BOOST_TEST(i != j);
  144. BOOST_TEST(i2 != j);
  145. readable_iterator_test(i, val1);
  146. readable_iterator_test(i2, val1);
  147. readable_iterator_test(i3, val1);
  148. BOOST_TEST(i == i2++);
  149. BOOST_TEST(i != ++i3);
  150. readable_iterator_test(i2, val2);
  151. readable_iterator_test(i3, val2);
  152. readable_iterator_test(i, val1);
  153. }
  154. template <class Iterator, class T>
  155. void forward_swappable_iterator_test(Iterator i, Iterator j, T val1, T val2)
  156. {
  157. forward_readable_iterator_test(i, j, val1, val2);
  158. Iterator i2 = i;
  159. ++i2;
  160. swappable_iterator_test(i, i2);
  161. }
  162. // bidirectional
  163. // Preconditions: *i == v1, *++i == v2
  164. template <class Iterator, class T>
  165. void bidirectional_readable_iterator_test(Iterator i, T v1, T v2)
  166. {
  167. Iterator j(i);
  168. ++j;
  169. forward_readable_iterator_test(i, j, v1, v2);
  170. ++i;
  171. Iterator i1 = i, i2 = i;
  172. BOOST_TEST(i == i1--);
  173. BOOST_TEST(i != --i2);
  174. readable_iterator_test(i, v2);
  175. readable_iterator_test(i1, v1);
  176. readable_iterator_test(i2, v1);
  177. --i;
  178. BOOST_TEST(i == i1);
  179. BOOST_TEST(i == i2);
  180. ++i1;
  181. ++i2;
  182. readable_iterator_test(i, v1);
  183. readable_iterator_test(i1, v2);
  184. readable_iterator_test(i2, v2);
  185. }
  186. // random access
  187. // Preconditions: [i,i+N) is a valid range
  188. template <class Iterator, class TrueVals>
  189. void random_access_readable_iterator_test(Iterator i, int N, TrueVals vals)
  190. {
  191. bidirectional_readable_iterator_test(i, vals[0], vals[1]);
  192. const Iterator j = i;
  193. int c;
  194. for (c = 0; c < N-1; ++c)
  195. {
  196. BOOST_TEST(i == j + c);
  197. BOOST_TEST(*i == vals[c]);
  198. typename detail::iterator_traits<Iterator>::value_type x = j[c];
  199. BOOST_TEST(*i == x);
  200. BOOST_TEST(*i == *(j + c));
  201. BOOST_TEST(*i == *(c + j));
  202. ++i;
  203. BOOST_TEST(i > j);
  204. BOOST_TEST(i >= j);
  205. BOOST_TEST(j <= i);
  206. BOOST_TEST(j < i);
  207. }
  208. Iterator k = j + N - 1;
  209. for (c = 0; c < N-1; ++c)
  210. {
  211. BOOST_TEST(i == k - c);
  212. BOOST_TEST(*i == vals[N - 1 - c]);
  213. typename detail::iterator_traits<Iterator>::value_type x = j[N - 1 - c];
  214. BOOST_TEST(*i == x);
  215. Iterator q = k - c;
  216. BOOST_TEST(*i == *q);
  217. BOOST_TEST(i > j);
  218. BOOST_TEST(i >= j);
  219. BOOST_TEST(j <= i);
  220. BOOST_TEST(j < i);
  221. --i;
  222. }
  223. }
  224. } // namespace boost
  225. # include <boost/iterator/detail/config_undef.hpp>
  226. #endif // BOOST_NEW_ITERATOR_TESTS_HPP