File: GEOSGeom_createCollectionTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (206 lines) | stat: -rw-r--r-- 5,057 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Test Suite for C-API GEOSGeom_createCollection

#include <tut/tut.hpp>
// geos
#include <geos_c.h>
// std
#include <array>
#include <cstdio>
#include <cstdlib>
#include <vector>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capigeosgeom_createcollection_data : public capitest::utility
{
    GEOSGeometry ** m_geoms;
    unsigned int m_ngeoms;

    test_capigeosgeom_createcollection_data()
        : m_geoms(nullptr)
        , m_ngeoms(0)
    {}

    ~test_capigeosgeom_createcollection_data()
    {
        if (m_geoms) GEOSFree(m_geoms);
        m_geoms = nullptr;
    }
};

#define geom_size 3

typedef test_group<test_capigeosgeom_createcollection_data> group;
typedef group::object object;

group test_capigeosgeom_createcollection_group("capi::GEOSGeom_createCollection");

//
// Test Cases
//

// Create collection from constant length C-array
template<>
template<>
void object::test<1>()
{
    GEOSGeometry* geoms[geom_size];
    geoms[0] = GEOSGeom_createEmptyPoint();
    geoms[1] = GEOSGeom_createEmptyPoint();
    geoms[2] = GEOSGeom_createEmptyPoint();

    // takes ownership of individual geometries
    geom1_ = GEOSGeom_createCollection(GEOS_MULTIPOINT, geoms, geom_size);
    ensure_equals(GEOSGetNumGeometries(geom1_), geom_size);
}

// Create collection from constant length std::array
template<>
template<>
void object::test<2>()
{
    std::array<GEOSGeometry*, geom_size> geoms = {{
        GEOSGeom_createEmptyLineString(),
        GEOSGeom_createEmptyLineString(),
        GEOSGeom_createEmptyLineString()
    }};

    // takes ownership of individual geometries
    geom1_ = GEOSGeom_createCollection(
        GEOS_MULTILINESTRING,
        geoms.data(),
        static_cast<unsigned int>(geoms.size()));

    ensure_equals(GEOSGetNumGeometries(geom1_), geom_size);
}

// Create collection from dynamic length std::vector of geometries
template<>
template<>
void object::test<3>()
{
    std::vector<GEOSGeometry*> geoms;
    geoms.push_back(GEOSGeom_createEmptyPolygon());
    geoms.push_back(GEOSGeom_createEmptyPolygon());
    geoms.push_back(GEOSGeom_createEmptyPolygon());
    geoms.push_back(GEOSGeom_createEmptyPolygon());
    geoms.push_back(GEOSGeom_createEmptyPolygon());

    // takes ownership of individual geometries
    geom1_ = GEOSGeom_createCollection(
        GEOS_MULTIPOLYGON,
        geoms.data(),
        static_cast<unsigned int>(geoms.size()));

    ensure_equals(static_cast<size_t>(GEOSGetNumGeometries(geom1_)), geoms.size());
}

// Error on invalid collection type, ownership is still transferred
template<>
template<>
void object::test<4>()
{
    std::vector<GEOSGeometry*> geoms;
    geoms.push_back(GEOSGeom_createEmptyPolygon());
    // takes ownership of individual geometries
    geom1_ = GEOSGeom_createCollection(
        12345,
        geoms.data(),
        static_cast<unsigned int>(geoms.size()));
    ensure(geom1_ == nullptr);

    geom1_ = GEOSGeom_createEmptyCollection(12345);
    ensure(geom1_ == nullptr);
}

// Release empty collection
template<>
template<>
void object::test<5>()
{
    geom1_ = fromWKT("MULTIPOLYGON EMPTY");

    m_geoms = GEOSGeom_releaseCollection(geom1_, &m_ngeoms);
    ensure(m_geoms == nullptr);
    ensure(m_ngeoms == 0);
}


// Release generic collection
template<>
template<>
void object::test<6>
()
{
    geom1_ = fromWKT("GEOMETRYCOLLECTION(POINT(0 0), POINT(1 1))");

    m_geoms = GEOSGeom_releaseCollection(geom1_, &m_ngeoms);
    ensure(m_geoms != nullptr);
    ensure(m_ngeoms == 2);

    for (size_t i = 0 ; i < m_ngeoms; i++) {
        ensure(GEOSGeomTypeId(m_geoms[i]) == GEOS_POINT);
        GEOSGeom_destroy(m_geoms[i]);
    }
}

// Release typed collection
template<>
template<>
void object::test<7>
()
{
    geom1_ = fromWKT("MULTIPOINT((0 0), (1 1))");

    m_geoms = GEOSGeom_releaseCollection(geom1_, &m_ngeoms);
    ensure(m_geoms != nullptr);
    ensure(m_ngeoms == 2);

    for (size_t i = 0 ; i < m_ngeoms; i++) {
        ensure(GEOSGeomTypeId(m_geoms[i]) == GEOS_POINT);
        GEOSGeom_destroy(m_geoms[i]);
    }
}

// Create MultiCurve
template<>
template<>
void object::test<8>
()
{
    GEOSGeometry* geoms[2];
    geoms[0] = fromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
    geoms[1] = fromWKT("LINESTRING (2 0, 3 3)");

    result_ = GEOSGeom_createCollection(GEOS_MULTICURVE, geoms, 2);
    expected_ = fromWKT("MULTICURVE (CIRCULARSTRING (0 0, 1 1, 2 0), (2 0, 3 3))");

    ensure_geometry_equals_identical(result_, expected_);
}

// Create MultiSurface
template<>
template<>
void object::test<9>
()
{
    GEOSGeometry* geoms[2];
    geoms[0] = fromWKT("POLYGON ((0 0, 1 0, 1 1, 0 0))");
    geoms[1] = fromWKT("CURVEPOLYGON (CIRCULARSTRING (10 10, 20 10, 15 15, 10 10))");

    result_ = GEOSGeom_createCollection(GEOS_MULTISURFACE, geoms, 2);
    expected_ = fromWKT("MULTISURFACE (((0 0, 1 0, 1 1, 0 0)), CURVEPOLYGON (CIRCULARSTRING (10 10, 20 10, 15 15, 10 10)))");

    ensure_geometry_equals_identical(result_, expected_);
}


} // namespace tut