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