range.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2014-2015 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <geometry_test_common.hpp>
  9. #include <iterator>
  10. #include <vector>
  11. #include <boost/range/iterator_range.hpp>
  12. #include <boost/geometry/util/range.hpp>
  13. namespace bgt {
  14. template <bool MutableIterator>
  15. struct beginner
  16. {
  17. template <typename Range>
  18. typename boost::range_iterator<Range>::type
  19. operator()(Range & rng)
  20. {
  21. return boost::begin(rng);
  22. }
  23. };
  24. template <>
  25. struct beginner<false>
  26. {
  27. template <typename Range>
  28. typename boost::range_iterator<Range const>::type
  29. operator()(Range & rng)
  30. {
  31. return boost::const_begin(rng);
  32. }
  33. };
  34. template <bool MutableIterator>
  35. struct ender
  36. {
  37. template <typename Range>
  38. typename boost::range_iterator<Range>::type
  39. operator()(Range & rng)
  40. {
  41. return boost::end(rng);
  42. }
  43. };
  44. template <>
  45. struct ender<false>
  46. {
  47. template <typename Range>
  48. typename boost::range_iterator<Range const>::type
  49. operator()(Range & rng)
  50. {
  51. return boost::const_end(rng);
  52. }
  53. };
  54. struct NonMovable
  55. {
  56. NonMovable(int ii = 0) : i(ii) {}
  57. NonMovable(NonMovable const& ii) : i(ii.i) {}
  58. NonMovable & operator=(NonMovable const& ii) { i = ii.i; return *this; }
  59. bool operator==(NonMovable const& ii) const { return i == ii.i; }
  60. int i;
  61. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  62. private:
  63. NonMovable(NonMovable && ii);
  64. NonMovable & operator=(NonMovable && ii);
  65. #endif
  66. };
  67. struct CopyableAndMovable
  68. {
  69. CopyableAndMovable(int ii = 0) : i(ii) {}
  70. CopyableAndMovable(CopyableAndMovable const& ii) : i(ii.i) {}
  71. CopyableAndMovable & operator=(CopyableAndMovable const& ii) { i = ii.i; return *this; }
  72. bool operator==(CopyableAndMovable const& ii) const { return i == ii.i; }
  73. int i;
  74. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  75. CopyableAndMovable(CopyableAndMovable && ii) : i(std::move(ii.i)) {}
  76. CopyableAndMovable & operator=(CopyableAndMovable && ii) { i = std::move(ii.i); return *this; }
  77. #endif
  78. };
  79. } // namespace bgt
  80. namespace bgr = bg::range;
  81. template <typename T, bool MutableIterator>
  82. void test_all()
  83. {
  84. bgt::beginner<MutableIterator> begin;
  85. bgt::ender<MutableIterator> end;
  86. std::vector<T> v;
  87. for (int i = 0 ; i < 20 ; ++i)
  88. {
  89. bgr::push_back(v, i);
  90. }
  91. for (int i = 0 ; i < 20 ; ++i)
  92. {
  93. BOOST_CHECK(bgr::at(v, i) == i);
  94. }
  95. {
  96. std::vector<T> w;
  97. std::copy(v.begin(), v.end(), bgr::back_inserter(w));
  98. BOOST_CHECK(v.size() == w.size() && std::equal(v.begin(), v.end(), w.begin()));
  99. }
  100. BOOST_CHECK(bgr::front(v) == 0);
  101. BOOST_CHECK(bgr::back(v) == 19);
  102. BOOST_CHECK(boost::size(v) == 20); // [0,19]
  103. bgr::resize(v, 15);
  104. BOOST_CHECK(boost::size(v) == 15); // [0,14]
  105. BOOST_CHECK(bgr::back(v) == 14);
  106. bgr::pop_back(v);
  107. BOOST_CHECK(boost::size(v) == 14); // [0,13]
  108. BOOST_CHECK(bgr::back(v) == 13);
  109. typename std::vector<T>::iterator
  110. it = bgr::erase(v, end(v) - 1);
  111. BOOST_CHECK(boost::size(v) == 13); // [0,12]
  112. BOOST_CHECK(bgr::back(v) == 12);
  113. BOOST_CHECK(it == end(v));
  114. it = bgr::erase(v, end(v) - 3, end(v));
  115. BOOST_CHECK(boost::size(v) == 10); // [0,9]
  116. BOOST_CHECK(bgr::back(v) == 9);
  117. BOOST_CHECK(it == end(v));
  118. it = bgr::erase(v, begin(v) + 2);
  119. BOOST_CHECK(boost::size(v) == 9); // {0,1,3..9}
  120. BOOST_CHECK(bgr::at(v, 1) == 1);
  121. BOOST_CHECK(bgr::at(v, 2) == 3);
  122. BOOST_CHECK(bgr::back(v) == 9);
  123. BOOST_CHECK(it == bgr::pos(v, 2));
  124. it = bgr::erase(v, begin(v) + 2, begin(v) + 2);
  125. BOOST_CHECK(boost::size(v) == 9); // {0,1,3..9}
  126. BOOST_CHECK(bgr::at(v, 1) == 1);
  127. BOOST_CHECK(bgr::at(v, 2) == 3);
  128. BOOST_CHECK(bgr::back(v) == 9);
  129. BOOST_CHECK(it == bgr::pos(v, 2));
  130. it = bgr::erase(v, begin(v) + 2, begin(v) + 5);
  131. BOOST_CHECK(boost::size(v) == 6); // {0,1,6..9}
  132. BOOST_CHECK(bgr::at(v, 1) == 1);
  133. BOOST_CHECK(bgr::at(v, 2) == 6);
  134. BOOST_CHECK(bgr::back(v) == 9);
  135. BOOST_CHECK(it == bgr::pos(v, 2));
  136. it = bgr::erase(v, begin(v));
  137. BOOST_CHECK(boost::size(v) == 5); // {1,6..9}
  138. BOOST_CHECK(bgr::at(v, 0) == 1);
  139. BOOST_CHECK(bgr::at(v, 1) == 6);
  140. BOOST_CHECK(bgr::back(v) == 9);
  141. BOOST_CHECK(it == bgr::pos(v, 0));
  142. it = bgr::erase(v, begin(v), begin(v) + 3);
  143. BOOST_CHECK(boost::size(v) == 2); // {8,9}
  144. BOOST_CHECK(bgr::at(v, 0) == 8);
  145. BOOST_CHECK(bgr::at(v, 1) == 9);
  146. BOOST_CHECK(bgr::back(v) == 9);
  147. BOOST_CHECK(it == bgr::pos(v, 0));
  148. it = bgr::erase(v, begin(v), end(v));
  149. BOOST_CHECK(boost::size(v) == 0);
  150. BOOST_CHECK(it == end(v));
  151. }
  152. void test_detail()
  153. {
  154. int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  155. bgr::detail::copy_or_move(arr + 1, arr + 10, arr);
  156. BOOST_CHECK(arr[0] == 1);
  157. std::vector<int> v(10, 0);
  158. bgr::detail::copy_or_move(v.begin() + 1, v.begin() + 10, v.begin());
  159. BOOST_CHECK(boost::size(v) == 10);
  160. bgr::erase(v, v.begin() + 1);
  161. BOOST_CHECK(boost::size(v) == 9);
  162. bgt::NonMovable * arr2[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  163. bgt::NonMovable foo;
  164. arr2[1] = &foo;
  165. bgr::detail::copy_or_move(arr2 + 1, arr2 + 10, arr2);
  166. BOOST_CHECK(arr2[0] == &foo);
  167. // Storing pointers in a std::vector is not possible in MinGW C++98
  168. #if __cplusplus >= 201103L
  169. std::vector<bgt::NonMovable*> v2(10, (bgt::NonMovable*)NULL);
  170. bgr::detail::copy_or_move(v2.begin() + 1, v2.begin() + 10, v2.begin());
  171. BOOST_CHECK(boost::size(v2) == 10);
  172. bgr::erase(v2, v2.begin() + 1);
  173. BOOST_CHECK(boost::size(v2) == 9);
  174. #endif
  175. }
  176. template <class Iterator>
  177. void test_pointers()
  178. {
  179. int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  180. boost::iterator_range<Iterator> r1(arr, arr + 10);
  181. std::pair<Iterator, Iterator> r2(arr, arr + 10);
  182. BOOST_CHECK(bgr::front(r1) == 0);
  183. BOOST_CHECK(bgr::front(r2) == 0);
  184. BOOST_CHECK(bgr::back(r1) == 9);
  185. BOOST_CHECK(bgr::back(r2) == 9);
  186. BOOST_CHECK(bgr::at(r1, 5) == 5);
  187. BOOST_CHECK(bgr::at(r2, 5) == 5);
  188. }
  189. int test_main(int, char* [])
  190. {
  191. test_all<int, true>();
  192. test_all<int, false>();
  193. // Storing non-movable elements in a std::vector is not possible in some implementations of STD lib
  194. #ifdef BOOST_GEOMETRY_TEST_NONMOVABLE_ELEMENTS
  195. test_all<bgt::NonMovable, true>();
  196. test_all<bgt::NonMovable, false>();
  197. #endif
  198. test_all<bgt::CopyableAndMovable, true>();
  199. test_all<bgt::CopyableAndMovable, false>();
  200. test_detail();
  201. test_pointers<int*>();
  202. test_pointers<int const*>();
  203. return 0;
  204. }