-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathtest-wkb.cpp
234 lines (191 loc) · 6.94 KB
/
test-wkb.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <catch.hpp>
#include "geom.hpp"
#include "wkb.hpp"
TEST_CASE("wkb: nullgeom", "[NoDB]")
{
geom::geometry_t const geom{};
REQUIRE(geom.is_null());
auto const wkb = geom_to_ewkb(geom);
REQUIRE(wkb.empty());
auto const result = ewkb_to_geom(wkb);
REQUIRE(result.is_null());
}
TEST_CASE("wkb: point", "[NoDB]")
{
geom::geometry_t const geom{geom::point_t{3.14, 2.17}, 42};
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_point());
REQUIRE(result.srid() == 42);
REQUIRE(result == geom);
}
TEST_CASE("wkb: linestring", "[NoDB]")
{
geom::geometry_t const geom{
geom::linestring_t{{1.2, 2.3}, {3.4, 4.5}, {5.6, 6.7}}, 43};
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_linestring());
REQUIRE(result.srid() == 43);
REQUIRE(result == geom);
}
TEST_CASE("wkb: polygon without inner ring", "[NoDB]")
{
geom::geometry_t const geom{
geom::polygon_t{geom::ring_t{
{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}}},
44};
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_polygon());
REQUIRE(result.srid() == 44);
REQUIRE(result == geom);
}
TEST_CASE("wkb: polygon with inner rings", "[NoDB]")
{
geom::geometry_t geom{
geom::polygon_t{geom::ring_t{
{0.0, 0.0}, {3.0, 0.0}, {3.0, 3.0}, {0.0, 3.0}, {0.0, 0.0}}},
45};
geom.get<geom::polygon_t>().add_inner_ring(geom::ring_t{
{1.0, 1.0}, {2.0, 1.0}, {2.0, 2.0}, {1.0, 2.0}, {1.0, 1.0}});
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_polygon());
REQUIRE(result.srid() == 45);
REQUIRE(result == geom);
}
TEST_CASE("wkb: point as multipoint", "[NoDB]")
{
geom::geometry_t const geom{geom::point_t{1.2, 2.3}, 47};
auto const result = ewkb_to_geom(geom_to_ewkb(geom, true));
REQUIRE(result.is_multipoint());
REQUIRE(result.srid() == 47);
auto const &rmp = result.get<geom::multipoint_t>();
REQUIRE(rmp.num_geometries() == 1);
REQUIRE(rmp[0] == geom.get<geom::point_t>());
}
TEST_CASE("wkb: multipoint", "[NoDB]")
{
geom::geometry_t geom{geom::multipoint_t{}, 46};
auto &mp = geom.get<geom::multipoint_t>();
mp.add_geometry(geom::point_t{{1.2, 2.3}});
mp.add_geometry(geom::point_t{{7.0, 7.0}});
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_multipoint());
REQUIRE(result.srid() == 46);
REQUIRE(result == geom);
}
TEST_CASE("wkb: linestring as multilinestring", "[NoDB]")
{
geom::geometry_t const geom{
geom::linestring_t{{1.2, 2.3}, {3.4, 4.5}, {5.6, 6.7}}, 43};
auto const result = ewkb_to_geom(geom_to_ewkb(geom, true));
REQUIRE(result.is_multilinestring());
REQUIRE(result.srid() == 43);
auto const &rml = result.get<geom::multilinestring_t>();
REQUIRE(rml.num_geometries() == 1);
REQUIRE(rml[0] == geom.get<geom::linestring_t>());
}
TEST_CASE("wkb: multilinestring", "[NoDB]")
{
geom::geometry_t geom{geom::multilinestring_t{}, 46};
auto &ml = geom.get<geom::multilinestring_t>();
ml.add_geometry(geom::linestring_t{{1.2, 2.3}, {3.4, 4.5}, {5.6, 6.7}});
ml.add_geometry(geom::linestring_t{{7.0, 7.0}, {8.0, 7.0}, {8.0, 8.0}});
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_multilinestring());
REQUIRE(result.srid() == 46);
REQUIRE(result == geom);
}
TEST_CASE("wkb: polygon as multipolygon", "[NoDB]")
{
geom::geometry_t const geom{
geom::polygon_t{geom::ring_t{
{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}}},
44};
auto const result = ewkb_to_geom(geom_to_ewkb(geom, true));
REQUIRE(result.is_multipolygon());
REQUIRE(result.srid() == 44);
auto const &rmp = result.get<geom::multipolygon_t>();
REQUIRE(rmp.num_geometries() == 1);
REQUIRE(rmp[0] == geom.get<geom::polygon_t>());
}
TEST_CASE("wkb: multipolygon", "[NoDB]")
{
geom::geometry_t geom{geom::multipolygon_t{}, 47};
auto &mp = geom.get<geom::multipolygon_t>();
{
auto &p0 = mp.add_geometry(geom::polygon_t{geom::ring_t{
{0.0, 0.0}, {3.0, 0.0}, {3.0, 3.0}, {0.0, 3.0}, {0.0, 0.0}}});
p0.add_inner_ring(geom::ring_t{
{1.0, 1.0}, {2.0, 1.0}, {2.0, 2.0}, {1.0, 2.0}, {1.0, 1.0}});
}
mp.add_geometry(geom::polygon_t{geom::ring_t{
{4.0, 4.0}, {5.0, 4.0}, {5.0, 5.0}, {4.0, 5.0}, {4.0, 4.0}}});
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_multipolygon());
REQUIRE(result.srid() == 47);
REQUIRE(result == geom);
}
TEST_CASE("wkb: geometrycollection", "[NoDB]")
{
geom::geometry_t geom1{geom::point_t{1.0, 2.0}};
geom::geometry_t geom2{geom::linestring_t{{1.2, 2.3}, {3.4, 4.5}}};
geom::geometry_t geom3{geom::multipolygon_t{}};
geom3.get<geom::multipolygon_t>().add_geometry(geom::polygon_t{geom::ring_t{
{4.0, 4.0}, {5.0, 4.0}, {5.0, 5.0}, {4.0, 5.0}, {4.0, 4.0}}});
geom::geometry_t geom{geom::collection_t{}, 49};
auto &c = geom.get<geom::collection_t>();
c.add_geometry(std::move(geom1));
c.add_geometry(std::move(geom2));
c.add_geometry(std::move(geom3));
auto const result = ewkb_to_geom(geom_to_ewkb(geom));
REQUIRE(result.is_collection());
REQUIRE(result.srid() == 49);
REQUIRE(result == geom);
}
TEST_CASE("wkb: invalid", "[NoDB]") { REQUIRE_THROWS(ewkb_to_geom("INVALID")); }
TEST_CASE("wkb hex decode of valid and invalid hex characters")
{
REQUIRE(decode_hex_char('0') == 0);
REQUIRE(decode_hex_char('9') == 9);
REQUIRE(decode_hex_char('a') == 0x0a);
REQUIRE(decode_hex_char('f') == 0x0f);
REQUIRE(decode_hex_char('A') == 0x0a);
REQUIRE(decode_hex_char('F') == 0x0f);
REQUIRE(decode_hex_char('#') == 0);
REQUIRE(decode_hex_char('@') == 0);
REQUIRE(decode_hex_char('g') == 0);
REQUIRE(decode_hex_char('G') == 0);
REQUIRE(decode_hex_char(0x7f) == 0);
}
TEST_CASE("wkb hex decode of valid hex string")
{
std::string const hex{"0001020F1099FF"};
std::string const data = {0x00,
0x01,
0x02,
0x0f,
0x10,
static_cast<char>(0x99),
static_cast<char>(0xff)};
auto const result = decode_hex(hex);
REQUIRE(result.size() == hex.size() / 2);
REQUIRE(result == data);
}
TEST_CASE("wkb hex decode of empty string is okay")
{
std::string const hex{};
REQUIRE(decode_hex(hex).empty());
}
TEST_CASE("wkb hex decode of string with odd number of characters fails")
{
REQUIRE_THROWS(decode_hex("a"));
REQUIRE_THROWS(decode_hex("abc"));
REQUIRE_THROWS(decode_hex("00000"));
}