closing_iterator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <algorithm>
  12. #include <iterator>
  13. #include <sstream>
  14. #include <string>
  15. #include <geometry_test_common.hpp>
  16. #include <boost/geometry/iterators/closing_iterator.hpp>
  17. #include <boost/geometry/core/coordinate_type.hpp>
  18. #include <boost/geometry/io/wkt/read.hpp>
  19. #include <boost/geometry/geometries/geometries.hpp>
  20. #include <boost/geometry/geometries/point_xy.hpp>
  21. #include <boost/range/adaptor/transformed.hpp>
  22. // The closing iterator should also work on normal std:: containers
  23. void test_empty_non_geometry()
  24. {
  25. std::vector<int> v;
  26. typedef bg::closing_iterator
  27. <
  28. std::vector<int> const
  29. > closing_iterator;
  30. closing_iterator it(v);
  31. closing_iterator end(v, true);
  32. std::ostringstream out;
  33. for (; it != end; ++it)
  34. {
  35. out << *it;
  36. }
  37. BOOST_CHECK_EQUAL(out.str(), "");
  38. }
  39. void test_non_geometry()
  40. {
  41. std::vector<int> v;
  42. v.push_back(1);
  43. v.push_back(2);
  44. v.push_back(3);
  45. typedef bg::closing_iterator
  46. <
  47. std::vector<int> const
  48. > closing_iterator;
  49. closing_iterator it(v);
  50. closing_iterator end(v, true);
  51. std::ostringstream out;
  52. for (; it != end; ++it)
  53. {
  54. out << *it;
  55. }
  56. BOOST_CHECK_EQUAL(out.str(), "1231");
  57. }
  58. void test_transformed_non_geometry()
  59. {
  60. std::vector<int> v;
  61. v.push_back(-1);
  62. v.push_back(-2);
  63. v.push_back(-3);
  64. typedef boost::transformed_range
  65. <
  66. std::negate<int>,
  67. std::vector<int>
  68. > transformed_range;
  69. typedef bg::closing_iterator
  70. <
  71. transformed_range const
  72. > closing_iterator;
  73. transformed_range v2 = v | boost::adaptors::transformed(std::negate<int>());
  74. closing_iterator it(v2);
  75. closing_iterator end(v2, true);
  76. std::ostringstream out;
  77. for (; it != end; ++it)
  78. {
  79. out << *it;
  80. }
  81. BOOST_CHECK_EQUAL(out.str(), "1231");
  82. }
  83. template <typename Geometry>
  84. void test_geometry(std::string const& wkt)
  85. {
  86. Geometry geometry;
  87. bg::read_wkt(wkt, geometry);
  88. typedef bg::closing_iterator<Geometry const> closing_iterator;
  89. // 1. Test normal behaviour
  90. {
  91. closing_iterator it(geometry);
  92. closing_iterator end(geometry, true);
  93. std::ostringstream out;
  94. for (; it != end; ++it)
  95. {
  96. out << " " << bg::get<0>(*it) << bg::get<1>(*it);
  97. }
  98. BOOST_CHECK_EQUAL(out.str(), " 11 14 44 41 11");
  99. // Check compilations, it is (since Oct 2010) random access
  100. it--;
  101. --it;
  102. it += 2;
  103. it -= 2;
  104. }
  105. // 2: check copy behaviour
  106. {
  107. typedef typename boost::range_iterator<Geometry const>::type normal_iterator;
  108. Geometry copy;
  109. std::copy<closing_iterator>(
  110. closing_iterator(geometry),
  111. closing_iterator(geometry, true),
  112. std::back_inserter(copy));
  113. std::ostringstream out;
  114. for (normal_iterator cit = boost::begin(copy); cit != boost::end(copy); ++cit)
  115. {
  116. out << " " << bg::get<0>(*cit) << bg::get<1>(*cit);
  117. }
  118. BOOST_CHECK_EQUAL(out.str(), " 11 14 44 41 11");
  119. }
  120. }
  121. template <typename P>
  122. void test_all()
  123. {
  124. test_empty_non_geometry();
  125. test_non_geometry();
  126. test_transformed_non_geometry();
  127. test_geometry<bg::model::ring<P> >("POLYGON((1 1,1 4,4 4,4 1))");
  128. }
  129. int test_main(int, char* [])
  130. {
  131. test_all<bg::model::d2::point_xy<double> >();
  132. return 0;
  133. }