iterator_tests.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <assert.h>
  22. # include <boost/type_traits.hpp>
  23. # include <boost/static_assert.hpp>
  24. # include <boost/concept_archetype.hpp> // for detail::dummy_constructor
  25. # include <boost/implicit_cast.hpp>
  26. # include <boost/type_traits/broken_compiler_spec.hpp>
  27. namespace boost {
  28. // use this for the value type
  29. struct dummyT {
  30. dummyT() { }
  31. dummyT(detail::dummy_constructor) { }
  32. dummyT(int x) : m_x(x) { }
  33. int foo() const { return m_x; }
  34. bool operator==(const dummyT& d) const { return m_x == d.m_x; }
  35. int m_x;
  36. };
  37. }
  38. BOOST_TT_BROKEN_COMPILER_SPEC(boost::dummyT)
  39. namespace boost {
  40. // Tests whether type Iterator satisfies the requirements for a
  41. // TrivialIterator.
  42. // Preconditions: i != j, *i == val
  43. template <class Iterator, class T>
  44. void trivial_iterator_test(const Iterator i, const Iterator j, T val)
  45. {
  46. Iterator k;
  47. assert(i == i);
  48. assert(j == j);
  49. assert(i != j);
  50. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  51. T v = *i;
  52. #else
  53. typename std::iterator_traits<Iterator>::value_type v = *i;
  54. #endif
  55. assert(v == val);
  56. #if 0
  57. // hmm, this will give a warning for transform_iterator... perhaps
  58. // this should be separated out into a stand-alone test since there
  59. // are several situations where it can't be used, like for
  60. // integer_range::iterator.
  61. assert(v == i->foo());
  62. #endif
  63. k = i;
  64. assert(k == k);
  65. assert(k == i);
  66. assert(k != j);
  67. assert(*k == val);
  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. assert(i == i1);
  82. assert(!(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. // assert(i != i2);
  91. // assert(!(i == i2));
  92. assert(*i1 == v1);
  93. assert(*i == v1);
  94. // we cannot test for equivalence of (void)++i & (void)i++
  95. // as i is only guaranteed to be single pass.
  96. assert(*i++ == v1);
  97. i1 = i;
  98. assert(i == i1);
  99. assert(!(i != i1));
  100. assert(*i1 == v2);
  101. assert(*i == v2);
  102. // i is dereferencable, so it must be incrementable.
  103. ++i;
  104. // how to test for operator-> ?
  105. }
  106. // how to test output iterator?
  107. template <bool is_pointer> struct lvalue_test
  108. {
  109. template <class Iterator> static void check(Iterator)
  110. {
  111. # ifndef BOOST_NO_STD_ITERATOR_TRAITS
  112. typedef typename std::iterator_traits<Iterator>::reference reference;
  113. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  114. # else
  115. typedef typename Iterator::reference reference;
  116. typedef typename Iterator::value_type value_type;
  117. # endif
  118. BOOST_STATIC_ASSERT(boost::is_reference<reference>::value);
  119. BOOST_STATIC_ASSERT((boost::is_same<reference,value_type&>::value
  120. || boost::is_same<reference,const value_type&>::value
  121. ));
  122. }
  123. };
  124. # ifdef BOOST_NO_STD_ITERATOR_TRAITS
  125. template <> struct lvalue_test<true> {
  126. template <class T> static void check(T) {}
  127. };
  128. #endif
  129. template <class Iterator, class T>
  130. void forward_iterator_test(Iterator i, T v1, T v2)
  131. {
  132. input_iterator_test(i, v1, v2);
  133. Iterator i1 = i, i2 = i;
  134. assert(i == i1++);
  135. assert(i != ++i2);
  136. trivial_iterator_test(i, i1, v1);
  137. trivial_iterator_test(i, i2, v1);
  138. ++i;
  139. assert(i == i1);
  140. assert(i == i2);
  141. ++i1;
  142. ++i2;
  143. trivial_iterator_test(i, i1, v2);
  144. trivial_iterator_test(i, i2, v2);
  145. // borland doesn't allow non-type template parameters
  146. # if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
  147. lvalue_test<(boost::is_pointer<Iterator>::value)>::check(i);
  148. #endif
  149. }
  150. // Preconditions: *i == v1, *++i == v2
  151. template <class Iterator, class T>
  152. void bidirectional_iterator_test(Iterator i, T v1, T v2)
  153. {
  154. forward_iterator_test(i, v1, v2);
  155. ++i;
  156. Iterator i1 = i, i2 = i;
  157. assert(i == i1--);
  158. assert(i != --i2);
  159. trivial_iterator_test(i, i1, v2);
  160. trivial_iterator_test(i, i2, v2);
  161. --i;
  162. assert(i == i1);
  163. assert(i == i2);
  164. ++i1;
  165. ++i2;
  166. trivial_iterator_test(i, i1, v1);
  167. trivial_iterator_test(i, i2, v1);
  168. }
  169. // mutable_bidirectional_iterator_test
  170. template <class U> struct undefined;
  171. // Preconditions: [i,i+N) is a valid range
  172. template <class Iterator, class TrueVals>
  173. void random_access_iterator_test(Iterator i, int N, TrueVals vals)
  174. {
  175. bidirectional_iterator_test(i, vals[0], vals[1]);
  176. const Iterator j = i;
  177. int c;
  178. typedef typename boost::detail::iterator_traits<Iterator>::value_type value_type;
  179. for (c = 0; c < N-1; ++c) {
  180. assert(i == j + c);
  181. assert(*i == vals[c]);
  182. assert(*i == boost::implicit_cast<value_type>(j[c]));
  183. assert(*i == *(j + c));
  184. assert(*i == *(c + j));
  185. ++i;
  186. assert(i > j);
  187. assert(i >= j);
  188. assert(j <= i);
  189. assert(j < i);
  190. }
  191. Iterator k = j + N - 1;
  192. for (c = 0; c < N-1; ++c) {
  193. assert(i == k - c);
  194. assert(*i == vals[N - 1 - c]);
  195. assert(*i == boost::implicit_cast<value_type>(j[N - 1 - c]));
  196. Iterator q = k - c;
  197. assert(*i == *q);
  198. assert(i > j);
  199. assert(i >= j);
  200. assert(j <= i);
  201. assert(j < i);
  202. --i;
  203. }
  204. }
  205. // Precondition: i != j
  206. template <class Iterator, class ConstIterator>
  207. void const_nonconst_iterator_test(Iterator i, ConstIterator j)
  208. {
  209. assert(i != j);
  210. assert(j != i);
  211. ConstIterator k(i);
  212. assert(k == i);
  213. assert(i == k);
  214. k = i;
  215. assert(k == i);
  216. assert(i == k);
  217. }
  218. } // namespace boost
  219. #endif // BOOST_ITERATOR_TESTS_HPP