closeable_view.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <sstream>
  10. #include <string>
  11. #include <geometry_test_common.hpp>
  12. #include <boost/geometry/views/closeable_view.hpp>
  13. #include <boost/geometry/io/wkt/read.hpp>
  14. #include <boost/geometry/io/dsv/write.hpp>
  15. #include <boost/geometry/geometries/geometries.hpp>
  16. #include <boost/geometry/geometries/point_xy.hpp>
  17. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  18. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  19. // The closeable view should also work on normal std:: containers
  20. void test_non_geometry()
  21. {
  22. typedef bg::closeable_view
  23. <
  24. std::vector<int> const, bg::open
  25. >::type view_type;
  26. std::vector<int> v;
  27. v.push_back(1);
  28. v.push_back(2);
  29. v.push_back(3);
  30. view_type view(v);
  31. typedef boost::range_iterator<view_type const>::type iterator;
  32. iterator it = boost::begin(view);
  33. iterator end = boost::end(view);
  34. std::ostringstream out;
  35. for ( ; it != end; ++it)
  36. {
  37. out << *it;
  38. }
  39. BOOST_CHECK_EQUAL(out.str(), "1231");
  40. // Check operators =, ++, --, +=, -=
  41. it = boost::begin(view);
  42. BOOST_CHECK_EQUAL(*it, 1);
  43. it += 2;
  44. BOOST_CHECK_EQUAL(*it, 3);
  45. it -= 2;
  46. BOOST_CHECK_EQUAL(*it, 1);
  47. it++;
  48. BOOST_CHECK_EQUAL(*it, 2);
  49. it--;
  50. BOOST_CHECK_EQUAL(*it, 1);
  51. // Also check them in the last regions
  52. it = boost::begin(view) + 3;
  53. BOOST_CHECK_EQUAL(*it, 1);
  54. it--;
  55. BOOST_CHECK_EQUAL(*it, 3);
  56. it++;
  57. BOOST_CHECK_EQUAL(*it, 1);
  58. it -= 2;
  59. BOOST_CHECK_EQUAL(*it, 2);
  60. it += 2;
  61. BOOST_CHECK_EQUAL(*it, 1);
  62. BOOST_CHECK_EQUAL(boost::size(view), 4u);
  63. }
  64. template <bg::closure_selector Closure, typename Range>
  65. void test_optionally_closing(Range const& range, std::string const& expected)
  66. {
  67. typedef typename bg::closeable_view<Range const, Closure>::type view_type;
  68. typedef typename boost::range_iterator<view_type const>::type iterator;
  69. view_type view(range);
  70. bool first = true;
  71. std::ostringstream out;
  72. iterator end = boost::end(view);
  73. for (iterator it = boost::begin(view); it != end; ++it, first = false)
  74. {
  75. out << (first ? "" : " ") << bg::dsv(*it);
  76. }
  77. BOOST_CHECK_EQUAL(out.str(), expected);
  78. }
  79. template <typename Geometry>
  80. void test_geometry(std::string const& wkt,
  81. std::string const& expected_false,
  82. std::string const& expected_true)
  83. {
  84. Geometry geo;
  85. bg::read_wkt(wkt, geo);
  86. test_optionally_closing<bg::closed>(geo, expected_false);
  87. test_optionally_closing<bg::open>(geo, expected_true);
  88. }
  89. template <typename P>
  90. void test_all()
  91. {
  92. test_geometry<bg::model::ring<P> >(
  93. "POLYGON((1 1,1 4,4 4,4 1))",
  94. "(1, 1) (1, 4) (4, 4) (4, 1)",
  95. "(1, 1) (1, 4) (4, 4) (4, 1) (1, 1)");
  96. }
  97. int test_main(int, char* [])
  98. {
  99. test_non_geometry();
  100. test_all<bg::model::d2::point_xy<double> >();
  101. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  102. test_all<boost::tuple<double, double> >();
  103. return 0;
  104. }