iterator_tests.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <type_traits>
  22. # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
  23. # include <boost/core/ignore_unused.hpp>
  24. # include <boost/core/lightweight_test.hpp>
  25. namespace boost {
  26. // use this for the value type
  27. struct dummyT {
  28. dummyT() { }
  29. dummyT(detail::dummy_constructor) { }
  30. dummyT(int x) : m_x(x) { }
  31. int foo() const { return m_x; }
  32. bool operator==(const dummyT& d) const { return m_x == d.m_x; }
  33. int m_x;
  34. };
  35. }
  36. namespace boost {
  37. namespace iterators {
  38. // Tests whether type Iterator satisfies the requirements for a
  39. // TrivialIterator.
  40. // Preconditions: i != j, *i == val
  41. template <class Iterator, class T>
  42. void trivial_iterator_test(const Iterator i, const Iterator j, T val)
  43. {
  44. Iterator k;
  45. BOOST_TEST(i == i);
  46. BOOST_TEST(j == j);
  47. BOOST_TEST(i != j);
  48. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  49. T v = *i;
  50. #else
  51. typename std::iterator_traits<Iterator>::value_type v = *i;
  52. #endif
  53. BOOST_TEST(v == val);
  54. boost::ignore_unused(v);
  55. #if 0
  56. // hmm, this will give a warning for transform_iterator... perhaps
  57. // this should be separated out into a stand-alone test since there
  58. // are several situations where it can't be used, like for
  59. // integer_range::iterator.
  60. BOOST_TEST(v == i->foo());
  61. #endif
  62. k = i;
  63. BOOST_TEST(k == k);
  64. BOOST_TEST(k == i);
  65. BOOST_TEST(k != j);
  66. BOOST_TEST(*k == val);
  67. boost::ignore_unused(k);
  68. }
  69. // Preconditions: i != j
  70. template <class Iterator, class T>
  71. void mutable_trivial_iterator_test(const Iterator i, const Iterator j, T val)
  72. {
  73. *i = val;
  74. trivial_iterator_test(i, j, val);
  75. }
  76. // Preconditions: *i == v1, *++i == v2
  77. template <class Iterator, class T>
  78. void input_iterator_test(Iterator i, T v1, T v2)
  79. {
  80. Iterator i1(i);
  81. BOOST_TEST(i == i1);
  82. BOOST_TEST(!(i != i1));
  83. // I can see no generic way to create an input iterator
  84. // that is in the domain of== of i and != i.
  85. // The following works for istream_iterator but is not
  86. // guaranteed to work for arbitrary input iterators.
  87. //
  88. // Iterator i2;
  89. //
  90. // BOOST_TEST(i != i2);
  91. // BOOST_TEST(!(i == i2));
  92. BOOST_TEST(*i1 == v1);
  93. BOOST_TEST(*i == v1);
  94. // we cannot test for equivalence of (void)++i & (void)i++
  95. // as i is only guaranteed to be single pass.
  96. BOOST_TEST(*i++ == v1);
  97. boost::ignore_unused(i1);
  98. i1 = i;
  99. BOOST_TEST(i == i1);
  100. BOOST_TEST(!(i != i1));
  101. BOOST_TEST(*i1 == v2);
  102. BOOST_TEST(*i == v2);
  103. boost::ignore_unused(i1);
  104. // i is dereferencable, so it must be incrementable.
  105. ++i;
  106. // how to test for operator-> ?
  107. }
  108. // how to test output iterator?
  109. template <bool is_pointer> struct lvalue_test
  110. {
  111. template <class Iterator> static void check(Iterator)
  112. {
  113. # ifndef BOOST_NO_STD_ITERATOR_TRAITS
  114. typedef typename std::iterator_traits<Iterator>::reference reference;
  115. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  116. # else
  117. typedef typename Iterator::reference reference;
  118. typedef typename Iterator::value_type value_type;
  119. # endif
  120. static_assert(std::is_reference<reference>::value, "reference must be a reference type.");
  121. static_assert(
  122. std::is_same<reference, value_type&>::value || std::is_same<reference, const value_type&>::value,
  123. "reference must either be a reference to value_type or constant reference to value_type."
  124. );
  125. }
  126. };
  127. # ifdef BOOST_NO_STD_ITERATOR_TRAITS
  128. template <> struct lvalue_test<true> {
  129. template <class T> static void check(T) {}
  130. };
  131. #endif
  132. template <class Iterator, class T>
  133. void forward_iterator_test(Iterator i, T v1, T v2)
  134. {
  135. input_iterator_test(i, v1, v2);
  136. Iterator i1 = i, i2 = i;
  137. BOOST_TEST(i == i1++);
  138. BOOST_TEST(i != ++i2);
  139. trivial_iterator_test(i, i1, v1);
  140. trivial_iterator_test(i, i2, v1);
  141. ++i;
  142. BOOST_TEST(i == i1);
  143. BOOST_TEST(i == i2);
  144. ++i1;
  145. ++i2;
  146. trivial_iterator_test(i, i1, v2);
  147. trivial_iterator_test(i, i2, v2);
  148. // borland doesn't allow non-type template parameters
  149. # if !defined(BOOST_BORLANDC) || (BOOST_BORLANDC > 0x551)
  150. lvalue_test<std::is_pointer<Iterator>::value>::check(i);
  151. #endif
  152. }
  153. // Preconditions: *i == v1, *++i == v2
  154. template <class Iterator, class T>
  155. void bidirectional_iterator_test(Iterator i, T v1, T v2)
  156. {
  157. forward_iterator_test(i, v1, v2);
  158. ++i;
  159. Iterator i1 = i, i2 = i;
  160. BOOST_TEST(i == i1--);
  161. BOOST_TEST(i != --i2);
  162. trivial_iterator_test(i, i1, v2);
  163. trivial_iterator_test(i, i2, v2);
  164. --i;
  165. BOOST_TEST(i == i1);
  166. BOOST_TEST(i == i2);
  167. ++i1;
  168. ++i2;
  169. trivial_iterator_test(i, i1, v1);
  170. trivial_iterator_test(i, i2, v1);
  171. }
  172. // mutable_bidirectional_iterator_test
  173. template <class U> struct undefined;
  174. // Preconditions: [i,i+N) is a valid range
  175. template <class Iterator, class TrueVals>
  176. void random_access_iterator_test(Iterator i, int N, TrueVals vals)
  177. {
  178. bidirectional_iterator_test(i, vals[0], vals[1]);
  179. const Iterator j = i;
  180. int c;
  181. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  182. struct local
  183. {
  184. static value_type to_value_type(value_type v) { return v; }
  185. };
  186. for (c = 0; c < N-1; ++c) {
  187. BOOST_TEST(i == j + c);
  188. BOOST_TEST(*i == vals[c]);
  189. BOOST_TEST(*i == local::to_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 == local::to_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