detail_iterator_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Test for boost/detail/iterator.hpp
  3. //
  4. // Copyright 2014 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. #include <boost/detail/iterator.hpp>
  11. #include <boost/core/is_same.hpp>
  12. #include <boost/core/lightweight_test_trait.hpp>
  13. #include <cstddef>
  14. #include <list>
  15. /*
  16. namespace boost {
  17. namespace detail {
  18. template <class Iterator> struct iterator_traits: std::iterator_traits<Iterator>
  19. {
  20. };
  21. using std::distance;
  22. } // namespace detail
  23. } // namespace boost
  24. */
  25. // struct C {} doesn't wotk with libc++.
  26. typedef std::forward_iterator_tag C;
  27. struct T
  28. {
  29. };
  30. struct D
  31. {
  32. };
  33. struct P
  34. {
  35. };
  36. struct R
  37. {
  38. };
  39. template< class Category, class T, class Distance = std::ptrdiff_t, class Pointer = T*, class Reference = T& >
  40. struct iterator
  41. {
  42. typedef T value_type;
  43. typedef Distance difference_type;
  44. typedef Pointer pointer;
  45. typedef Reference reference;
  46. typedef Category iterator_category;
  47. };
  48. int main()
  49. {
  50. using boost::core::is_same;
  51. /*
  52. template<class Iterator> struct iterator_traits {
  53. typedef typename Iterator::difference_type difference_type;
  54. typedef typename Iterator::value_type value_type;
  55. typedef typename Iterator::pointer pointer;
  56. typedef typename Iterator::reference reference;
  57. typedef typename Iterator::iterator_category iterator_category;
  58. };
  59. */
  60. {
  61. typedef ::iterator<C,T,D,P,R> It;
  62. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,C>));
  63. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
  64. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,D>));
  65. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,P>));
  66. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,R>));
  67. }
  68. /*
  69. template<class T> struct iterator_traits<T*> {
  70. typedef ptrdiff_t difference_type;
  71. typedef T value_type;
  72. typedef T* pointer;
  73. typedef T& reference;
  74. typedef random_access_iterator_tag iterator_category;
  75. };
  76. */
  77. {
  78. typedef T* It;
  79. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,std::random_access_iterator_tag>));
  80. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
  81. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,std::ptrdiff_t>));
  82. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,T*>));
  83. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,T&>));
  84. }
  85. /*
  86. template<class T> struct iterator_traits<const T*> {
  87. typedef ptrdiff_t difference_type;
  88. typedef T value_type;
  89. typedef const T* pointer;
  90. typedef const T& reference;
  91. typedef random_access_iterator_tag iterator_category;
  92. };
  93. */
  94. {
  95. typedef T const* It;
  96. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::iterator_category,std::random_access_iterator_tag>));
  97. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::value_type,T>));
  98. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::difference_type,std::ptrdiff_t>));
  99. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::pointer,T const*>));
  100. BOOST_TEST_TRAIT_TRUE((is_same<boost::detail::iterator_traits<It>::reference,T const&>));
  101. }
  102. /*
  103. template<class InputIterator>
  104. typename iterator_traits<InputIterator>::difference_type
  105. distance( InputIterator first, InputIterator last );
  106. */
  107. {
  108. int const N = 5;
  109. T x[ N ] = {};
  110. BOOST_TEST_EQ( boost::detail::distance( x, x + N ), N );
  111. }
  112. {
  113. int const N = 5;
  114. T const x[ N ] = {};
  115. BOOST_TEST_EQ( boost::detail::distance( x, x + N ), N );
  116. }
  117. {
  118. int const N = 5;
  119. std::list<T> x( N );
  120. BOOST_TEST_EQ( boost::detail::distance( x.begin(), x.end() ), x.size() );
  121. }
  122. return boost::report_errors();
  123. }