reversible_view.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/reversible_view.hpp>
  13. #include <boost/geometry/io/wkt/wkt.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. template <bg::iterate_direction Direction, typename Range>
  20. void test_forward_or_reverse(Range const& range, std::string const& expected)
  21. {
  22. typedef typename bg::reversible_view
  23. <
  24. Range const,
  25. Direction
  26. >::type view_type;
  27. view_type view(range);
  28. bool first = true;
  29. std::ostringstream out;
  30. for (typename boost::range_iterator<view_type const>::type
  31. it = boost::begin(view);
  32. it != boost::end(view);
  33. ++it, first = false)
  34. {
  35. out << (first ? "" : " ") << bg::dsv(*it);
  36. }
  37. BOOST_CHECK_EQUAL(out.str(), expected);
  38. }
  39. template <typename Geometry>
  40. void test_geometry(std::string const& wkt,
  41. std::string const& expected_forward, std::string const& expected_reverse)
  42. {
  43. Geometry geo;
  44. bg::read_wkt(wkt, geo);
  45. test_forward_or_reverse<bg::iterate_forward>(geo, expected_forward);
  46. test_forward_or_reverse<bg::iterate_reverse>(geo, expected_reverse);
  47. }
  48. template <typename P>
  49. void test_all()
  50. {
  51. test_geometry<bg::model::linestring<P> >(
  52. "linestring(1 1,2 2,3 3)",
  53. "(1, 1) (2, 2) (3, 3)",
  54. "(3, 3) (2, 2) (1, 1)");
  55. }
  56. int test_main(int, char* [])
  57. {
  58. test_all<bg::model::d2::point_xy<double> >();
  59. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  60. test_all<boost::tuple<double, double> >();
  61. return 0;
  62. }