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