boost_range.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <geometry_test_common.hpp>
  8. #include <boost/geometry/geometry.hpp>
  9. #include <boost/geometry/geometries/geometries.hpp>
  10. #include <boost/geometry/geometries/point_xy.hpp>
  11. #include <boost/geometry/geometries/adapted/boost_range/adjacent_filtered.hpp>
  12. #include <boost/geometry/geometries/adapted/boost_range/filtered.hpp>
  13. #include <boost/geometry/geometries/adapted/boost_range/reversed.hpp>
  14. #include <boost/geometry/geometries/adapted/boost_range/strided.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_range/sliced.hpp>
  16. #include <boost/geometry/geometries/adapted/boost_range/uniqued.hpp>
  17. #include <boost/geometry/io/wkt/wkt.hpp>
  18. #include <sstream>
  19. #ifdef BOOST_GEOMETRY_TEST_QUARANTINED
  20. struct not_two
  21. {
  22. template <typename P>
  23. bool operator()(P const& p) const
  24. {
  25. return boost::geometry::get<0>(p) != 2.0;
  26. }
  27. };
  28. struct sum_not_five
  29. {
  30. template <typename P>
  31. bool operator()(P const& p1, P const& p2) const
  32. {
  33. return boost::geometry::get<0>(p1) + boost::geometry::get<0>(p2) != 5.0;
  34. }
  35. };
  36. #endif
  37. template <typename P>
  38. void test_range_adaptor()
  39. {
  40. bg::model::linestring<P> ls;
  41. bg::read_wkt("LINESTRING(1 1,2 2,3 3,4 4)", ls);
  42. {
  43. std::ostringstream out;
  44. out << bg::wkt(ls);
  45. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(1 1,2 2,3 3,4 4)");
  46. }
  47. {
  48. std::ostringstream out;
  49. out << bg::wkt(ls | boost::adaptors::reversed);
  50. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(4 4,3 3,2 2,1 1)");
  51. }
  52. {
  53. std::ostringstream out;
  54. out << bg::wkt(ls | boost::adaptors::strided(2));
  55. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(1 1,3 3)");
  56. }
  57. {
  58. std::ostringstream out;
  59. out << bg::wkt(ls | boost::adaptors::sliced(1,3));
  60. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(2 2,3 3)");
  61. }
  62. #ifdef BOOST_GEOMETRY_TEST_QUARANTINED
  63. // range filter adaptor does not support boost::size()
  64. // This makes it in practice not applicable, boost::geometry calls boost::size
  65. // in most if not all algorithms
  66. {
  67. std::ostringstream out;
  68. out << bg::wkt(ls | boost::adaptors::filtered(not_two()));
  69. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(1 1,3 3,4 4)");
  70. }
  71. {
  72. std::ostringstream out;
  73. out << bg::wkt(ls | boost::adaptors::adjacent_filtered(sum_not_five()));
  74. BOOST_CHECK_EQUAL(out.str(), "LINESTRING(1 1,3 3,4 4)");
  75. }
  76. {
  77. bg::model::linestring<P> ls2;
  78. bg::read_wkt("LINESTRING(1 1,1 1,2 2,3 3,3 3,4 4)", ls2);
  79. std::ostringstream out;
  80. // uniqued needs == operator, equals
  81. //out << bg::wkt(ls | boost::adaptors::uniqued);
  82. //BOOST_CHECK_EQUAL(out.str(), "LINESTRING(1 1,2 2,3 3,4 4)");
  83. }
  84. #endif
  85. }
  86. template <typename P>
  87. void test_all()
  88. {
  89. test_range_adaptor<P>();
  90. }
  91. int test_main(int, char* [])
  92. {
  93. test_all<bg::model::d2::point_xy<double> >();
  94. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  95. return 0;
  96. }