1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2014, Oracle and/or its affiliates.
5
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_TEST_MODULE
12 #define BOOST_TEST_MODULE test_num_interior_rings
13 #endif
14
15 #include <iostream>
16
17 #include <boost/test/included/unit_test.hpp>
18
19 #include <boost/variant/variant.hpp>
20
21 #include <boost/geometry/algorithms/num_interior_rings.hpp>
22
23 #include <boost/geometry/core/closure.hpp>
24 #include <boost/geometry/geometries/geometries.hpp>
25 #include <boost/geometry/io/wkt/wkt.hpp>
26 #include <boost/geometry/io/dsv/write.hpp>
27
28 namespace bg = boost::geometry;
29
30
31 typedef bg::model::point<double, 2, bg::cs::cartesian> point;
32 typedef bg::model::linestring<point> linestring;
33 typedef bg::model::segment<point> segment;
34 typedef bg::model::box<point> box;
35 typedef bg::model::ring<point> ring;
36 typedef bg::model::polygon<point> polygon;
37 typedef bg::model::multi_point<point> multi_point;
38 typedef bg::model::multi_linestring<linestring> multi_linestring;
39 typedef bg::model::multi_polygon<polygon> multi_polygon;
40
41
42 template <typename Geometry>
43 struct test_num_interior_rings
44 {
applytest_num_interior_rings45 static inline void apply(Geometry const& geometry,
46 std::size_t expected)
47 {
48 std::size_t detected = bg::num_interior_rings(geometry);
49 BOOST_CHECK_MESSAGE( detected == expected,
50 "Expected: " << expected
51 << " detected: " << detected
52 << " wkt: " << bg::wkt(geometry) );
53 }
54
applytest_num_interior_rings55 static inline void apply(std::string const& wkt,
56 std::size_t expected)
57 {
58 Geometry geometry;
59 bg::read_wkt(wkt, geometry);
60 apply(geometry, expected);
61 }
62 };
63
BOOST_AUTO_TEST_CASE(test_point)64 BOOST_AUTO_TEST_CASE( test_point )
65 {
66 test_num_interior_rings<point>::apply("POINT(0 0)", 0);
67 }
68
BOOST_AUTO_TEST_CASE(test_segment)69 BOOST_AUTO_TEST_CASE( test_segment )
70 {
71 test_num_interior_rings<segment>::apply("SEGMENT(0 0,1 1)", 0);
72 }
73
BOOST_AUTO_TEST_CASE(test_box)74 BOOST_AUTO_TEST_CASE( test_box )
75 {
76 test_num_interior_rings<box>::apply("BOX(0 0,1 1)", 0);
77 }
78
BOOST_AUTO_TEST_CASE(test_linestring)79 BOOST_AUTO_TEST_CASE( test_linestring )
80 {
81 test_num_interior_rings<linestring>::apply("LINESTRING(0 0,1 1)", 0);
82 }
83
BOOST_AUTO_TEST_CASE(test_multipoint)84 BOOST_AUTO_TEST_CASE( test_multipoint )
85 {
86 test_num_interior_rings<multi_point>::apply("MULTIPOINT(0 0,1 1)", 0);
87 }
88
BOOST_AUTO_TEST_CASE(test_multilinestring)89 BOOST_AUTO_TEST_CASE( test_multilinestring )
90 {
91 test_num_interior_rings
92 <
93 multi_linestring
94 >::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 0);
95 }
96
BOOST_AUTO_TEST_CASE(test_ring)97 BOOST_AUTO_TEST_CASE( test_ring )
98 {
99 typedef test_num_interior_rings<ring> tester;
100
101 tester::apply("POLYGON((0 0,1 0,0 1,0 0))", 0);
102 tester::apply("POLYGON((0 0,1 0,1 0,0 1,0 0))", 0);
103 }
104
BOOST_AUTO_TEST_CASE(test_polygon)105 BOOST_AUTO_TEST_CASE( test_polygon )
106 {
107 typedef test_num_interior_rings<polygon> tester;
108
109 tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 0);
110 tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 2,1 1))", 1);
111 tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5))", 2);
112 }
113
BOOST_AUTO_TEST_CASE(test_multipolygon)114 BOOST_AUTO_TEST_CASE( test_multipolygon )
115 {
116 typedef test_num_interior_rings<multi_polygon> tester;
117
118 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))", 0);
119 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 1);
120 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", 2);
121 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101)))", 2);
122 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101),(105 105,106 105,106 106,105 106,105 105)))", 4);
123 }
124
BOOST_AUTO_TEST_CASE(test_variant)125 BOOST_AUTO_TEST_CASE( test_variant )
126 {
127 typedef boost::variant
128 <
129 linestring, polygon, multi_polygon
130 > variant_geometry_type;
131
132 typedef test_num_interior_rings<variant_geometry_type> tester;
133
134 linestring ls;
135 bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
136
137 polygon poly;
138 bg::read_wkt("POLYGON((0 0,0 10,10 10,10 0,0 0),(1 1,9 1,9 9,1 9,1 1))", poly);
139
140 multi_polygon mpoly;
141 bg::read_wkt("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(1 1,2 1,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", mpoly);
142
143 variant_geometry_type variant_geometry;
144
145 variant_geometry = ls;
146 tester::apply(variant_geometry, 0);
147
148 variant_geometry = poly;
149 tester::apply(variant_geometry, 1);
150
151 variant_geometry = mpoly;
152 tester::apply(variant_geometry, 2);
153 }
154