concatenate_iterator.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_concatenate_iterator
  9. #endif
  10. #include <boost/test/included/unit_test.hpp>
  11. #include <cstddef>
  12. #include <iostream>
  13. #include <sstream>
  14. #include <string>
  15. #include <algorithm>
  16. #include <iterator>
  17. #include <vector>
  18. #include <list>
  19. #include <boost/assign/std/vector.hpp>
  20. #include <boost/assign/std/list.hpp>
  21. #include <boost/core/ignore_unused.hpp>
  22. #include "test_iterator_common.hpp"
  23. #include <boost/geometry/iterators/concatenate_iterator.hpp>
  24. using namespace boost::assign;
  25. struct test_concatenate_iterator
  26. {
  27. template
  28. <
  29. typename ConcatenateIterator,
  30. typename ConstConcatenateIterator,
  31. typename Container
  32. >
  33. static inline
  34. void test_using_max_element(ConcatenateIterator first,
  35. ConcatenateIterator beyond,
  36. ConstConcatenateIterator const_first,
  37. ConstConcatenateIterator const_beyond,
  38. Container const& c,
  39. std::size_t other_size,
  40. bool second_container)
  41. {
  42. typedef typename std::iterator_traits
  43. <
  44. ConcatenateIterator
  45. >::value_type value_type;
  46. if ( c.size() == 0 )
  47. {
  48. return;
  49. }
  50. ConcatenateIterator c_first = first;
  51. if ( second_container )
  52. {
  53. std::size_t counter(0);
  54. while ( counter != other_size )
  55. {
  56. ++counter;
  57. c_first++;
  58. }
  59. }
  60. ConcatenateIterator it_max = std::max_element(first, beyond);
  61. ConstConcatenateIterator const_it_max =
  62. std::max_element(const_first, const_beyond);
  63. BOOST_CHECK( it_max == const_it_max );
  64. BOOST_CHECK( *it_max == *const_it_max );
  65. value_type old_value = *c_first;
  66. value_type new_value = *it_max + 1;
  67. *c_first = *it_max + 1;
  68. BOOST_CHECK( *c.begin() == new_value );
  69. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  70. std::cout << std::endl;
  71. std::cout << "modified element of ";
  72. std::cout << (second_container ? "2nd" : "1st");
  73. std::cout << " container:" << std::endl;
  74. print_container(std::cout, c.begin(), c.end(),
  75. (second_container ? "second :" : "first :"))
  76. << std::endl;
  77. print_container(std::cout, first, beyond, "combined:") << std::endl;
  78. #endif
  79. *c_first = old_value;
  80. BOOST_CHECK( *c.begin() == old_value );
  81. }
  82. template <typename Container1, typename Container2>
  83. static inline void apply(Container1& c1, Container2& c2,
  84. std::string const& case_id,
  85. std::string const& containers_id)
  86. {
  87. boost::ignore_unused(case_id, containers_id);
  88. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  89. std::stringstream sstream;
  90. sstream << case_id << " [" << containers_id << "]";
  91. std::cout << "case id: " << sstream.str() << std::endl;
  92. #endif
  93. typedef typename Container1::const_iterator const_iterator1;
  94. typedef typename Container2::const_iterator const_iterator2;
  95. typedef typename Container1::iterator iterator1;
  96. typedef typename Container2::iterator iterator2;
  97. typedef boost::geometry::concatenate_iterator
  98. <
  99. const_iterator1, const_iterator2,
  100. typename Container1::value_type const
  101. > const_concat_iterator;
  102. typedef boost::geometry::concatenate_iterator
  103. <
  104. iterator1, iterator2, typename Container1::value_type
  105. > concat_iterator;
  106. typedef typename std::iterator_traits
  107. <
  108. concat_iterator
  109. >::value_type value_type;
  110. // test constructors/assignment operators
  111. concat_iterator begin(c1.begin(), c1.end(), c2.begin(), c2.begin());
  112. concat_iterator end(c1.end(), c2.begin(), c2.end());
  113. const_concat_iterator const_begin(begin);
  114. const_concat_iterator const_end(end);
  115. const_begin = begin;
  116. const_end = end;
  117. concat_iterator begin2(c1.end(), c1.end(), c2.begin(), c2.begin());
  118. const_concat_iterator const_begin2(c1.end(), c1.end(),
  119. c2.begin(), c2.begin());
  120. BOOST_CHECK( c1.empty() || *begin == *c1.begin() );
  121. BOOST_CHECK( c1.empty() || *const_begin == *c1.begin() );
  122. BOOST_CHECK( c2.empty() || *begin2 == *c2.begin() );
  123. BOOST_CHECK( c2.empty() || *const_begin2 == *c2.begin() );
  124. // test copying, dereferencing and element equality
  125. std::vector<value_type> combined;
  126. std::copy(c1.begin(), c1.end(), std::back_inserter(combined));
  127. std::copy(c2.begin(), c2.end(), std::back_inserter(combined));
  128. test_equality(begin, end, combined);
  129. combined.clear();
  130. std::copy(begin, end, std::back_inserter(combined));
  131. test_equality(begin, end, combined);
  132. test_equality(const_begin, const_end, combined);
  133. combined.clear();
  134. std::copy(const_begin, const_end, std::back_inserter(combined));
  135. test_equality(begin, end, combined);
  136. test_equality(const_begin, const_end, combined);
  137. // test sizes (and std::distance)
  138. test_size(begin, end, combined);
  139. test_size(const_begin, const_end, combined);
  140. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  141. print_container(std::cout, c1.begin(), c1.end(), "first :")
  142. << std::endl;
  143. print_container(std::cout, c2.begin(), c2.end(), "second :")
  144. << std::endl;
  145. print_container(std::cout, begin, end, "combined:")
  146. << std::endl;
  147. if ( begin != end )
  148. {
  149. std::cout << "min element: "
  150. << *std::min_element(begin, end)
  151. << std::endl;
  152. std::cout << "max element: "
  153. << *std::max_element(const_begin, const_end)
  154. << std::endl;
  155. }
  156. #endif
  157. // perform reversals (std::reverse)
  158. test_using_reverse(begin, end, combined);
  159. // test std::max_element, dereferencing and value assigment
  160. test_using_max_element(begin, end, const_begin, const_end,
  161. c1, c2.size(), false);
  162. test_using_max_element(begin, end, const_begin, const_end,
  163. c2, c1.size(), true);
  164. // test std::count_if / std::remove_if
  165. test_using_remove_if(begin, end, combined);
  166. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  167. std::cout << "====================" << std::endl << std::endl;
  168. #endif
  169. }
  170. };
  171. template <typename Container1, typename Container2>
  172. inline void test_concatenation_of_containers(Container1& c1, Container2& c2,
  173. std::string const& containers_id)
  174. {
  175. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  176. std::cout << std::endl << std::endl;
  177. std::cout << "************************************" << std::endl
  178. << " TESTING CONTAINERS COMBINATION: "
  179. << containers_id << std::endl
  180. << "************************************" << std::endl
  181. << std::endl;
  182. #endif
  183. c1.clear();
  184. c2.clear();
  185. test_concatenate_iterator::apply(c1, c2, "empty_both", containers_id);
  186. c2 += 10,11,12,13,14,15,16,17,18,19,20;
  187. test_concatenate_iterator::apply(c1, c2, "empty_first", containers_id);
  188. c2.clear();
  189. c1 += 0,1,2,3,4,5,6;
  190. test_concatenate_iterator::apply(c1, c2, "empty_second", containers_id);
  191. c1.clear();
  192. c2.clear();
  193. c1 += 0,1,2,3,4,5,6;
  194. c2 += 10,11,12,13,14,15,16,17,18,19,20;
  195. test_concatenate_iterator::apply(c1, c2, "non_empty", containers_id);
  196. }
  197. BOOST_AUTO_TEST_CASE( test_concatenate_iterator_all )
  198. {
  199. std::vector<int> v, vv;
  200. std::list<int> l, ll;
  201. test_concatenation_of_containers(v, vv, "VV");
  202. test_concatenation_of_containers(v, l, "VL");
  203. test_concatenation_of_containers(l, v, "LV");
  204. test_concatenation_of_containers(l, ll, "LL");
  205. }