reverse_iterator_test.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright Thomas Witt 2003, Jeremy Siek 2004.
  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. #include <boost/iterator/reverse_iterator.hpp>
  6. #include <boost/iterator/new_iterator_tests.hpp>
  7. #include <boost/concept_check.hpp>
  8. #include <boost/concept_archetype.hpp>
  9. #include <boost/iterator/iterator_concepts.hpp>
  10. #include <boost/iterator/iterator_archetypes.hpp>
  11. #include <boost/cstdlib.hpp>
  12. #include <algorithm>
  13. #include <deque>
  14. #include <iostream>
  15. using boost::dummyT;
  16. // Test reverse iterator
  17. int main()
  18. {
  19. dummyT array[] = { dummyT(0), dummyT(1), dummyT(2),
  20. dummyT(3), dummyT(4), dummyT(5) };
  21. const int N = sizeof(array)/sizeof(dummyT);
  22. // Concept checks
  23. // Adapting old-style iterators
  24. {
  25. typedef boost::reverse_iterator<boost::bidirectional_iterator_archetype<dummyT> > Iter;
  26. boost::function_requires< boost::BidirectionalIteratorConcept<Iter> >();
  27. boost::function_requires< boost_concepts::ReadableIteratorConcept<Iter> >();
  28. boost::function_requires< boost_concepts::LvalueIteratorConcept<Iter> >();
  29. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  30. }
  31. {
  32. typedef boost::reverse_iterator<boost::mutable_bidirectional_iterator_archetype<dummyT> > Iter;
  33. boost::function_requires< boost::Mutable_BidirectionalIteratorConcept<Iter> >();
  34. boost::function_requires< boost_concepts::WritableIteratorConcept<Iter> >();
  35. boost::function_requires< boost_concepts::LvalueIteratorConcept<Iter> >();
  36. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  37. }
  38. // Adapting new-style iterators
  39. {
  40. typedef boost::iterator_archetype<
  41. const dummyT
  42. , boost::iterator_archetypes::readable_iterator_t
  43. , boost::bidirectional_traversal_tag
  44. > iter;
  45. typedef boost::reverse_iterator<iter> Iter;
  46. boost::function_requires< boost::InputIteratorConcept<Iter> >();
  47. boost::function_requires< boost_concepts::ReadableIteratorConcept<Iter> >();
  48. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  49. }
  50. #if 0
  51. // It does not seem feasible to make this work. Need to change docs to
  52. // require at lease Readable for the base iterator. -Jeremy
  53. {
  54. typedef boost::iterator_archetype<
  55. dummyT
  56. , boost::iterator_archetypes::writable_iterator_t
  57. , boost::bidirectional_traversal_tag
  58. > iter;
  59. typedef boost::reverse_iterator<iter> Iter;
  60. boost::function_requires< boost_concepts::WritableIteratorConcept<Iter, dummyT> >();
  61. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  62. }
  63. #endif
  64. #if !BOOST_WORKAROUND(BOOST_MSVC, == 1200) // Causes Internal Error in linker.
  65. {
  66. typedef boost::iterator_archetype<
  67. dummyT
  68. , boost::iterator_archetypes::readable_writable_iterator_t
  69. , boost::bidirectional_traversal_tag
  70. > iter;
  71. typedef boost::reverse_iterator<iter> Iter;
  72. boost::function_requires< boost::InputIteratorConcept<Iter> >();
  73. boost::function_requires< boost_concepts::ReadableIteratorConcept<Iter> >();
  74. boost::function_requires< boost_concepts::WritableIteratorConcept<Iter> >();
  75. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  76. }
  77. {
  78. typedef boost::iterator_archetype<
  79. const dummyT
  80. , boost::iterator_archetypes::readable_lvalue_iterator_t
  81. , boost::bidirectional_traversal_tag
  82. > iter;
  83. typedef boost::reverse_iterator<iter> Iter;
  84. boost::function_requires< boost::BidirectionalIteratorConcept<Iter> >();
  85. boost::function_requires< boost_concepts::ReadableIteratorConcept<Iter> >();
  86. boost::function_requires< boost_concepts::LvalueIteratorConcept<Iter> >();
  87. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  88. }
  89. {
  90. typedef boost::iterator_archetype<
  91. dummyT
  92. , boost::iterator_archetypes::writable_lvalue_iterator_t
  93. , boost::bidirectional_traversal_tag
  94. > iter;
  95. typedef boost::reverse_iterator<iter> Iter;
  96. boost::function_requires< boost::BidirectionalIteratorConcept<Iter> >();
  97. boost::function_requires< boost_concepts::WritableIteratorConcept<Iter> >();
  98. boost::function_requires< boost_concepts::LvalueIteratorConcept<Iter> >();
  99. boost::function_requires< boost_concepts::BidirectionalTraversalConcept<Iter> >();
  100. }
  101. #endif
  102. // Test reverse_iterator
  103. {
  104. dummyT reversed[N];
  105. std::copy(array, array + N, reversed);
  106. std::reverse(reversed, reversed + N);
  107. typedef boost::reverse_iterator<dummyT*> reverse_iterator;
  108. reverse_iterator i(reversed + N);
  109. boost::random_access_iterator_test(i, N, array);
  110. boost::random_access_iterator_test(boost::make_reverse_iterator(reversed + N), N, array);
  111. typedef boost::reverse_iterator<const dummyT*> const_reverse_iterator;
  112. const_reverse_iterator j(reversed + N);
  113. boost::random_access_iterator_test(j, N, array);
  114. const dummyT* const_reversed = reversed;
  115. boost::random_access_iterator_test(boost::make_reverse_iterator(const_reversed + N), N, array);
  116. boost::const_nonconst_iterator_test(i, ++j);
  117. }
  118. // Test reverse_iterator again, with traits fully deducible on all platforms
  119. {
  120. std::deque<dummyT> reversed_container;
  121. std::reverse_copy(array, array + N, std::back_inserter(reversed_container));
  122. const std::deque<dummyT>::iterator reversed = reversed_container.begin();
  123. typedef boost::reverse_iterator<
  124. std::deque<dummyT>::iterator> reverse_iterator;
  125. typedef boost::reverse_iterator<
  126. std::deque<dummyT>::const_iterator> const_reverse_iterator;
  127. // MSVC/STLport gives an INTERNAL COMPILER ERROR when any computation
  128. // (e.g. "reversed + N") is used in the constructor below.
  129. const std::deque<dummyT>::iterator finish = reversed_container.end();
  130. reverse_iterator i(finish);
  131. boost::random_access_iterator_test(i, N, array);
  132. boost::random_access_iterator_test(boost::make_reverse_iterator(reversed + N), N, array);
  133. const_reverse_iterator j = reverse_iterator(finish);
  134. boost::random_access_iterator_test(j, N, array);
  135. const std::deque<dummyT>::const_iterator const_reversed = reversed;
  136. boost::random_access_iterator_test(boost::make_reverse_iterator(const_reversed + N), N, array);
  137. // Many compilers' builtin deque iterators don't interoperate well, though
  138. // STLport fixes that problem.
  139. #if defined(__SGI_STL_PORT) \
  140. || !BOOST_WORKAROUND(__GNUC__, <= 2) \
  141. && !(BOOST_WORKAROUND(__GNUC__, == 3) && BOOST_WORKAROUND(__GNUC_MINOR__, <= 1)) \
  142. && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) \
  143. && !BOOST_WORKAROUND(__LIBCOMO_VERSION__, BOOST_TESTED_AT(29)) \
  144. && !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, <= 1)
  145. boost::const_nonconst_iterator_test(i, ++j);
  146. #endif
  147. }
  148. return boost::report_errors();
  149. }