test_iterator_common.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef GEOMETRY_TEST_ITERATORS_TEST_ITERATOR_COMMON_HPP
  8. #define GEOMETRY_TEST_ITERATORS_TEST_ITERATOR_COMMON_HPP
  9. #include <cstddef>
  10. #include <iostream>
  11. #include <iterator>
  12. #include <string>
  13. #include <algorithm>
  14. #include <boost/test/included/unit_test.hpp>
  15. // helper functions for testing the concatenate and flatten iterators
  16. template <typename Iterator>
  17. inline std::ostream& print_container(std::ostream& os,
  18. Iterator begin, Iterator end,
  19. std::string const& header)
  20. {
  21. std::cout << header;
  22. for (Iterator it = begin; it != end; ++it)
  23. {
  24. os << " " << *it;
  25. }
  26. return os;
  27. }
  28. template <typename Iterator>
  29. inline std::ostream& print_nested_container(std::ostream& os,
  30. Iterator begin, Iterator end,
  31. std::string const& header)
  32. {
  33. typedef typename std::iterator_traits<Iterator>::value_type inner_container;
  34. typedef typename inner_container::const_iterator inner_iterator;
  35. std::cout << header;
  36. for (Iterator outer = begin; outer != end; ++outer)
  37. {
  38. os << " (";
  39. for (inner_iterator inner = outer->begin();
  40. inner != outer->end(); ++inner)
  41. {
  42. if ( inner != outer->begin() )
  43. {
  44. os << " ";
  45. }
  46. os << *inner;
  47. }
  48. os << ") ";
  49. }
  50. return os;
  51. }
  52. template <typename T>
  53. struct is_odd
  54. {
  55. inline bool operator()(T const& t) const
  56. {
  57. return t % 2 != 0;
  58. }
  59. };
  60. template <typename T>
  61. struct is_even
  62. {
  63. inline bool operator()(T const& t) const
  64. {
  65. return !is_odd<T>()(t);
  66. }
  67. };
  68. template <typename CombinedIterator, typename CombinedContainer>
  69. inline void test_size(CombinedIterator first, CombinedIterator beyond,
  70. CombinedContainer const& combined)
  71. {
  72. std::size_t size = static_cast<std::size_t>(std::distance(first, beyond));
  73. BOOST_CHECK( combined.size() == size );
  74. size = 0;
  75. for (CombinedIterator it = first; it != beyond; ++it)
  76. {
  77. ++size;
  78. }
  79. BOOST_CHECK( combined.size() == size );
  80. size = 0;
  81. for (CombinedIterator it = beyond; it != first; --it)
  82. {
  83. ++size;
  84. }
  85. BOOST_CHECK( combined.size() == size );
  86. }
  87. template <typename CombinedIterator, typename CombinedContainer>
  88. inline void test_equality(CombinedIterator first, CombinedIterator beyond,
  89. CombinedContainer const& combined)
  90. {
  91. typedef typename CombinedContainer::const_iterator iterator;
  92. iterator it = combined.begin();
  93. for (CombinedIterator cit = first; cit != beyond; ++cit, ++it)
  94. {
  95. BOOST_CHECK( *cit == *it );
  96. }
  97. if ( combined.begin() != combined.end() )
  98. {
  99. BOOST_CHECK( first != beyond );
  100. iterator it = combined.end();
  101. CombinedIterator cit = beyond;
  102. for (--cit, --it; cit != first; --cit, --it)
  103. {
  104. BOOST_CHECK( *cit == *it );
  105. }
  106. BOOST_CHECK( cit == first && it == combined.begin() );
  107. BOOST_CHECK( *cit == *it );
  108. }
  109. else
  110. {
  111. BOOST_CHECK( first == beyond );
  112. }
  113. }
  114. template <typename CombinedIterator, typename CombinedContainer>
  115. inline void test_using_remove_if(CombinedIterator first,
  116. CombinedIterator beyond,
  117. CombinedContainer& combined)
  118. {
  119. typedef typename std::iterator_traits
  120. <
  121. CombinedIterator
  122. >::value_type value_type;
  123. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  124. std::cout << std::endl;
  125. std::cout << "odd elements removed:" << std::endl;
  126. print_container(std::cout, first, beyond, "before:") << std::endl;
  127. #endif
  128. typename std::iterator_traits<CombinedIterator>::difference_type
  129. num_even = std::count_if(first, beyond, is_even<value_type>());
  130. CombinedIterator new_beyond =
  131. std::remove_if(first, beyond, is_odd<value_type>());
  132. std::size_t new_size = std::distance(first, new_beyond);
  133. for (CombinedIterator it = first; it != new_beyond; ++it)
  134. {
  135. BOOST_CHECK( !is_odd<value_type>()(*it) );
  136. }
  137. BOOST_CHECK( new_size == static_cast<std::size_t>(num_even) );
  138. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  139. print_container(std::cout, first, new_beyond, "after :") << std::endl;
  140. #endif
  141. combined.erase(std::remove_if(combined.begin(), combined.end(),
  142. is_odd<value_type>()),
  143. combined.end());
  144. test_equality(first, new_beyond, combined);
  145. }
  146. template <typename CombinedIterator, typename CombinedContainer>
  147. inline void test_using_reverse(CombinedIterator first,
  148. CombinedIterator beyond,
  149. CombinedContainer& combined)
  150. {
  151. std::reverse(first, beyond);
  152. std::reverse(combined.begin(), combined.end());
  153. test_equality(first, beyond, combined);
  154. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  155. print_container(std::cout, first, beyond, "reversed:") << std::endl;
  156. #endif
  157. std::reverse(first, beyond);
  158. std::reverse(combined.begin(), combined.end());
  159. test_equality(first, beyond, combined);
  160. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  161. print_container(std::cout, first, beyond, "re-reversed:") << std::endl;
  162. #endif
  163. }
  164. #endif // GEOMETRY_TEST_ITERATORS_TEST_ITERATOR_COMMON_HPP