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_geometries
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_geometries.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_geometries
44 {
applytest_num_geometries45 static inline void apply(Geometry const& geometry,
46 std::size_t expected)
47 {
48 std::size_t detected = bg::num_geometries(geometry);
49 BOOST_CHECK_MESSAGE( detected == expected,
50 "Expected: " << expected
51 << " detected: " << detected
52 << " wkt: " << bg::wkt(geometry) );
53 }
54
applytest_num_geometries55 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_geometries<point>::apply("POINT(0 0)", 1);
67 }
68
BOOST_AUTO_TEST_CASE(test_segment)69 BOOST_AUTO_TEST_CASE( test_segment )
70 {
71 test_num_geometries<segment>::apply("SEGMENT(0 0,1 1)", 1);
72 }
73
BOOST_AUTO_TEST_CASE(test_box)74 BOOST_AUTO_TEST_CASE( test_box )
75 {
76 test_num_geometries<box>::apply("BOX(0 0,1 1)", 1);
77 }
78
BOOST_AUTO_TEST_CASE(test_linestring)79 BOOST_AUTO_TEST_CASE( test_linestring )
80 {
81 test_num_geometries<linestring>::apply("LINESTRING(0 0,1 1,2 2)", 1);
82 }
83
BOOST_AUTO_TEST_CASE(test_multipoint)84 BOOST_AUTO_TEST_CASE( test_multipoint )
85 {
86 typedef test_num_geometries<multi_point> tester;
87
88 tester::apply("MULTIPOINT()", 0);
89 tester::apply("MULTIPOINT(0 0)", 1);
90 tester::apply("MULTIPOINT(0 0,0 0)", 2);
91 tester::apply("MULTIPOINT(0 0,0 0,1 1)", 3);
92 }
93
BOOST_AUTO_TEST_CASE(test_multilinestring)94 BOOST_AUTO_TEST_CASE( test_multilinestring )
95 {
96 typedef test_num_geometries<multi_linestring> tester;
97
98 tester::apply("MULTILINESTRING()", 0);
99 tester::apply("MULTILINESTRING((0 0,1 0))", 1);
100 tester::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 2);
101 tester::apply("MULTILINESTRING((),(),(0 0,1 0))", 3);
102 }
103
BOOST_AUTO_TEST_CASE(test_ring)104 BOOST_AUTO_TEST_CASE( test_ring )
105 {
106 test_num_geometries<ring>::apply("POLYGON((0 0,1 0,0 1,0 0))", 1);
107 }
108
BOOST_AUTO_TEST_CASE(test_polygon)109 BOOST_AUTO_TEST_CASE( test_polygon )
110 {
111 typedef test_num_geometries<polygon> tester;
112
113 tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 1);
114 tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 1))", 1);
115 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))", 1);
116 }
117
BOOST_AUTO_TEST_CASE(test_multipolygon)118 BOOST_AUTO_TEST_CASE( test_multipolygon )
119 {
120 typedef test_num_geometries<multi_polygon> tester;
121
122 tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 1);
123 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)))", 1);
124 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);
125 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)))", 2);
126 }
127
BOOST_AUTO_TEST_CASE(test_variant)128 BOOST_AUTO_TEST_CASE( test_variant )
129 {
130 typedef boost::variant
131 <
132 linestring, multi_linestring
133 > variant_geometry_type;
134
135 typedef test_num_geometries<variant_geometry_type> tester;
136
137 linestring ls;
138 bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
139
140 multi_linestring mls;
141 bg::read_wkt("MULTILINESTRING((0 0,1 1,2 2),(3 3,4 4),(5 5,6 6,7 7,8 8))",
142 mls);
143
144 variant_geometry_type variant_geometry;
145
146 variant_geometry = ls;
147 tester::apply(variant_geometry, 1);
148
149 variant_geometry = mls;
150 tester::apply(variant_geometry, 3);
151 }
152