Debian Codegraph @

unittest/gunit/gis_difference-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27 #include <memory> // unique_ptr
28 #include <ostream> // <<
29
30 #include "sql/dd/dd.h"
31 #include "sql/dd/impl/types/spatial_reference_system_impl.h"
32 #include "sql/dd/properties.h"
33 #include "sql/dd/types/spatial_reference_system.h"
34 #include "sql/gis/geometries.h"
35 #include "sql/gis/geometries_cs.h"
36 #include "sql/gis/relops.h"
37 #include "sql/gis/setops.h"
38 #include "template_utils.h" // down_cast
39 #include "unittest/gunit/gis_setops_testshapes.h"
40 #include "unittest/gunit/gis_test.h"
41 #include "unittest/gunit/gis_typeset.h"
42
43 namespace gis_differenceunittest {
44
45 template <typename Types>
46 struct DifferenceTest : Gis_test<Types> {
47 void test_valid_input(const gis::Geometry &g1, const gis::Geometry &g2,
48 gis::Geometry &expected_result) {
49 std::unique_ptr<gis::Geometry> result;
50 bool is_null = false;
51 bool error =
52 gis::difference(this->m_srs.get(), &g1, &g2, "unittest", &result);
53 EXPECT_FALSE(error);
54 EXPECT_FALSE(is_null);
55
56 // Verify geometry return type.
57 const std::string type_res(gis::type_to_name(result->type()));
58 const std::string type_exp(gis::type_to_name(expected_result.type()));
59 EXPECT_EQ(type_exp, type_res);
60
61 // Verify result is correct.
62 bool is_equals = false;
63 bool equals_error =
64 gis::equals(this->m_srs.get(), &expected_result, result.get(),
65 "unittest", &is_equals, &is_null);
66 EXPECT_FALSE(equals_error);
67 EXPECT_FALSE(is_null);
68 EXPECT_TRUE(is_equals);
69 }
70 };
71
72 // The purpose of these tests is to check that the result returned from
73 // gis::difference is correct. The tests test all combinations of geometries.
74 TYPED_TEST_SUITE(DifferenceTest, gis_typeset::Test_both);
75
76 // difference(... ,point, *, ...)
77
78 TYPED_TEST(DifferenceTest, PointPoint) {
79 typename TypeParam::Point pt1{0, 0};
80 typename TypeParam::Point pt2{0, 0.1};
81 typename TypeParam::Geometrycollection empty_gc{};
82
83 this->test_valid_input(pt1, pt1, empty_gc);
84 this->test_valid_input(pt1, pt2, pt1);
85 }
86
87 TYPED_TEST(DifferenceTest, PointMultipoint) {
88 typename TypeParam::Point pt1{0, 0};
89 typename TypeParam::Point pt2{0, 0.1};
90 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
91 typename TypeParam::Geometrycollection empty_gc{};
92
93 mpt.push_back(pt2);
94
95 this->test_valid_input(pt1, mpt, empty_gc);
96 this->test_valid_input(mpt, pt2, pt1);
97 }
98
99 TYPED_TEST(DifferenceTest, PointLinestring) {
100 typename TypeParam::Point pt1{0, 0};
101 typename TypeParam::Point pt2{0.1, 0.1};
102 typename TypeParam::Linestring ls = simple_ls<TypeParam>();
103 typename TypeParam::Geometrycollection empty_gc{};
104
105 this->test_valid_input(pt1, ls, empty_gc);
106 this->test_valid_input(pt2, ls, pt2);
107 this->test_valid_input(ls, pt1, ls);
108 }
109
110 TYPED_TEST(DifferenceTest, PointMultiLinestring) {
111 typename TypeParam::Point pt1{0, 0};
112 typename TypeParam::Point pt2{0.1, 0.1};
113 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
114 typename TypeParam::Geometrycollection empty_gc{};
115
116 this->test_valid_input(pt1, mls, empty_gc);
117 this->test_valid_input(pt2, mls, pt2);
118 this->test_valid_input(mls, pt1, mls[0]);
119 }
120
121 TYPED_TEST(DifferenceTest, PointPolygon) {
122 typename TypeParam::Point pt1{0, 0};
123 typename TypeParam::Point pt2{0, 0.2};
124 typename TypeParam::Polygon py = base_py<TypeParam>();
125 typename TypeParam::Geometrycollection empty_gc{};
126
127 this->test_valid_input(pt1, py, empty_gc);
128 this->test_valid_input(pt2, py, pt2);
129 this->test_valid_input(py, pt1, py);
130 }
131
132 TYPED_TEST(DifferenceTest, PointMultipolygon) {
133 typename TypeParam::Point pt1{0, 0};
134 typename TypeParam::Point pt2{0, 0.2};
135 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
136 typename TypeParam::Geometrycollection empty_gc{};
137
138 this->test_valid_input(pt1, mpy, empty_gc);
139 this->test_valid_input(pt2, mpy, pt2);
140 this->test_valid_input(mpy, pt1, mpy[0]);
141 }
142
143 // difference(..., multipoint, *, ...)
144
145 TYPED_TEST(DifferenceTest, MultipointMultipoint) {
146 typename TypeParam::Multipoint mpt1 = simple_mpt<TypeParam>();
147 typename TypeParam::Multipoint mpt2 = simple_mpt<TypeParam>();
148 typename TypeParam::Geometrycollection empty_gc{};
149 typename TypeParam::Point pt2{0.0, 0.1};
150
151 mpt1.push_back(pt2);
152 mpt2.push_back(typename TypeParam::Point{0.1, 0.1});
153
154 this->test_valid_input(mpt1, mpt1, empty_gc);
155 this->test_valid_input(mpt1, mpt2, pt2);
156 }
157
158 TYPED_TEST(DifferenceTest, MultipointLinestring) {
159 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
160 typename TypeParam::Linestring ls = simple_ls<TypeParam>();
161 typename TypeParam::Geometrycollection empty_gc{};
162 typename TypeParam::Point pt2{0.1, 0.1};
163
164 this->test_valid_input(mpt, ls, empty_gc);
165
166 mpt.push_back(pt2);
167
168 this->test_valid_input(mpt, ls, pt2);
169 this->test_valid_input(ls, mpt, ls);
170 }
171
172 TYPED_TEST(DifferenceTest, MultipointMultilinestring) {
173 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
174 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
175 typename TypeParam::Geometrycollection empty_gc{};
176 typename TypeParam::Point pt2{0.1, 0.1};
177
178 this->test_valid_input(mpt, mls, empty_gc);
179
180 mpt.push_back(pt2);
181
182 this->test_valid_input(mpt, mls, pt2);
183 this->test_valid_input(mls, mpt, mls[0]);
184 }
185
186 TYPED_TEST(DifferenceTest, MultipointPolygon) {
187 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
188 typename TypeParam::Polygon py = base_py<TypeParam>();
189 typename TypeParam::Geometrycollection empty_gc{};
190 typename TypeParam::Point pt2{0.0, 0.2};
191
192 this->test_valid_input(mpt, py, empty_gc);
193
194 mpt.push_back(pt2);
195
196 this->test_valid_input(mpt, py, pt2);
197 this->test_valid_input(py, mpt, py);
198 }
199
200 TYPED_TEST(DifferenceTest, MultipointMultipolygon) {
201 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
202 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
203 typename TypeParam::Geometrycollection empty_gc{};
204 typename TypeParam::Point pt2{0.0, 0.2};
205
206 this->test_valid_input(mpt, mpy, empty_gc);
207
208 mpt.push_back(pt2);
209
210 this->test_valid_input(mpt, mpy, pt2);
211 this->test_valid_input(mpy, mpt, mpy[0]);
212 }
213
214 // difference(..., linestring, *, ...)
215
216 TYPED_TEST(DifferenceTest, LinestringLinestring) {
217 typename TypeParam::Linestring ls1 = simple_ls<TypeParam>();
218 typename TypeParam::Linestring ls2 = offset_simple_ls<TypeParam>();
219 typename TypeParam::Geometrycollection empty_gc{};
220 typename TypeParam::Linestring expected_result{};
221
222 expected_result.push_back(typename TypeParam::Point(0.0, 0.0));
223 expected_result.push_back(typename TypeParam::Point(0.05, 0.0));
224
225 this->test_valid_input(ls1, ls1, empty_gc);
226 this->test_valid_input(ls1, ls2, expected_result);
227 }
228
229 TYPED_TEST(DifferenceTest, LinestringMultilinestring) {
230 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
231 typename TypeParam::Linestring ls2 = offset_simple_ls<TypeParam>();
232 typename TypeParam::Geometrycollection empty_gc{};
233 typename TypeParam::Linestring expected_result{};
234
235 expected_result.push_back(typename TypeParam::Point(0.0, 0.0));
236 expected_result.push_back(typename TypeParam::Point(0.05, 0.0));
237
238 this->test_valid_input(mls[0], mls, empty_gc);
239 this->test_valid_input(mls, ls2, expected_result);
240 }
241
242 TYPED_TEST(DifferenceTest, LinestringPolygon) {
243 typename TypeParam::Linestring ls1 = diagonal_ls<TypeParam>();
244 typename TypeParam::Linestring ls2 = ls_crossing_base_py<TypeParam>();
245 typename TypeParam::Polygon py = base_py<TypeParam>();
246 typename TypeParam::Geometrycollection empty_gc{};
247 typename TypeParam::Multilinestring expected_result =
248 ls_crossing_base_py_difference<TypeParam>();
249
250 this->test_valid_input(ls1, py, empty_gc);
251 this->test_valid_input(ls2, py, expected_result);
252 this->test_valid_input(py, ls1, py);
253 }
254
255 TYPED_TEST(DifferenceTest, LinestringMultiPolygon) {
256 typename TypeParam::Linestring ls1 = diagonal_ls<TypeParam>();
257 typename TypeParam::Linestring ls2 = ls_crossing_base_py<TypeParam>();
258 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
259 typename TypeParam::Geometrycollection empty_gc{};
260 typename TypeParam::Multilinestring expected_result =
261 ls_crossing_base_py_difference<TypeParam>();
262
263 this->test_valid_input(ls1, mpy, empty_gc);
264 this->test_valid_input(ls2, mpy, expected_result);
265 this->test_valid_input(mpy, ls1, mpy[0]);
266 }
267
268 // difference(..., multilinestring, *, ...)
269
270 TYPED_TEST(DifferenceTest, MultilinestringMultilinestring) {
271 typename TypeParam::Multilinestring mls1 = simple_mls<TypeParam>();
272 typename TypeParam::Multilinestring mls2{};
273 typename TypeParam::Geometrycollection empty_gc{};
274 typename TypeParam::Linestring expected_result{};
275
276 mls2.push_back(offset_simple_ls<TypeParam>());
277 expected_result.push_back(typename TypeParam::Point{0.0, 0.0});
278 expected_result.push_back(typename TypeParam::Point{0.05, 0.0});
279
280 this->test_valid_input(mls1, mls1, empty_gc);
281 this->test_valid_input(mls1, mls2, expected_result);
282 }
283
284 TYPED_TEST(DifferenceTest, MultilinestringPolygon) {
285 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
286 typename TypeParam::Polygon py = base_py<TypeParam>();
287 typename TypeParam::Geometrycollection empty_gc{};
288 typename TypeParam::Linestring expected_result =
289 ls_overlapping_base_py_difference<TypeParam>();
290
291 this->test_valid_input(mls, py, empty_gc);
292
293 mls.push_back(ls_overlapping_base_py<TypeParam>());
294
295 this->test_valid_input(mls, py, expected_result);
296 this->test_valid_input(py, mls, py);
297 }
298
299 TYPED_TEST(DifferenceTest, MultilinestringMultipolygon) {
300 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
301 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
302 typename TypeParam::Geometrycollection empty_gc{};
303 typename TypeParam::Linestring expected_result =
304 ls_overlapping_base_py_difference<TypeParam>();
305
306 this->test_valid_input(mls, mpy, empty_gc);
307
308 mls.push_back(ls_overlapping_base_py<TypeParam>());
309
310 this->test_valid_input(mls, mpy, expected_result);
311 this->test_valid_input(mpy, mls, mpy[0]);
312 }
313
314 // difference(..., polygon, *, ...)
315
316 TYPED_TEST(DifferenceTest, PolygonPolygon) {
317 typename TypeParam::Polygon py1 = base_py<TypeParam>();
318 typename TypeParam::Polygon py2 = overlapping_py<TypeParam>();
319 typename TypeParam::Geometrycollection empty_gc{};
320
321 this->test_valid_input(py1, py1, empty_gc);
322 this->test_valid_input(py1, py2, py1);
323 }
324
325 TYPED_TEST(DifferenceTest, PolygonMultipolygon) {
326 typename TypeParam::Polygon py1 = base_py<TypeParam>();
327 typename TypeParam::Polygon py2 = overlapping_py<TypeParam>();
328 typename TypeParam::Polygon py3 = disjoint_py<TypeParam>();
329 typename TypeParam::Multipolygon mpy{};
330 typename TypeParam::Geometrycollection empty_gc{};
331
332 mpy.push_back(py2);
333 mpy.push_back(py3);
334
335 this->test_valid_input(py2, mpy, empty_gc);
336 this->test_valid_input(py1, mpy, py1);
337 this->test_valid_input(mpy, py2, py3);
338 }
339
340 // difference(..., multipolygon, *, ...)
341
342 TYPED_TEST(DifferenceTest, MultipolygonMultipolygon) {
343 typename TypeParam::Polygon py1 = base_py<TypeParam>();
344 typename TypeParam::Polygon py2 = overlapping_py<TypeParam>();
345 typename TypeParam::Polygon py3 = disjoint_py<TypeParam>();
346 typename TypeParam::Multipolygon mpy1 = simple_mpy<TypeParam>();
347 typename TypeParam::Multipolygon mpy2{};
348 typename TypeParam::Geometrycollection empty_gc{};
349
350 mpy2.push_back(py2);
351 mpy2.push_back(py3);
352
353 this->test_valid_input(mpy1, mpy1, empty_gc);
354 this->test_valid_input(mpy1, mpy2, py1);
355 }
356
357 // difference(..., geometrycollection, *, ...)
358
359 TYPED_TEST(DifferenceTest, GeometrycollectionPoint) {
360 typename TypeParam::Geometrycollection gc{};
361 typename TypeParam::Geometrycollection empty_gc{};
362 typename TypeParam::Point pt1{0, 0};
363 typename TypeParam::Point pt2{0, 0.1};
364
365 gc.push_back(pt1);
366
367 this->test_valid_input(gc, pt1, empty_gc);
368 this->test_valid_input(pt2, gc, pt2);
369 }
370
371 TYPED_TEST(DifferenceTest, GeometrycollectionMultipoint) {
372 typename TypeParam::Geometrycollection gc{};
373 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
374 typename TypeParam::Geometrycollection empty_gc{};
375 typename TypeParam::Point pt2{0.1, 0.1};
376
377 gc.push_back(mpt[0]);
378 this->test_valid_input(mpt, gc, empty_gc);
379
380 gc.push_back(pt2);
381 this->test_valid_input(gc, mpt, pt2);
382 }
383
384 TYPED_TEST(DifferenceTest, GeometrycollectionLinestring) {
385 typename TypeParam::Geometrycollection gc{};
386 typename TypeParam::Linestring ls1 = simple_ls<TypeParam>();
387 typename TypeParam::Linestring ls2 = diagonal_ls<TypeParam>();
388 typename TypeParam::Geometrycollection empty_gc{};
389
390 gc.push_back(ls1);
391 this->test_valid_input(ls1, gc, empty_gc);
392
393 gc.push_back(ls2);
394 this->test_valid_input(gc, ls1, ls2);
395 }
396
397 TYPED_TEST(DifferenceTest, GeometrycollectionMultilinestring) {
398 typename TypeParam::Geometrycollection gc{};
399 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
400 typename TypeParam::Linestring ls2 = diagonal_ls<TypeParam>();
401 typename TypeParam::Geometrycollection empty_gc{};
402
403 gc.push_back(mls[0]);
404 this->test_valid_input(mls, gc, empty_gc);
405
406 gc.push_back(ls2);
407 this->test_valid_input(gc, mls, ls2);
408 }
409
410 TYPED_TEST(DifferenceTest, GeometrycollectionPolygon) {
411 typename TypeParam::Geometrycollection gc{};
412 typename TypeParam::Polygon py1 = base_py<TypeParam>();
413 typename TypeParam::Polygon py2 = disjoint_py<TypeParam>();
414 typename TypeParam::Geometrycollection empty_gc{};
415
416 gc.push_back(py1);
417 this->test_valid_input(py1, gc, empty_gc);
418
419 gc.push_back(py2);
420 this->test_valid_input(gc, py1, py2);
421 }
422
423 TYPED_TEST(DifferenceTest, GeometrycollectionMultipolygon) {
424 typename TypeParam::Geometrycollection gc{};
425 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
426 typename TypeParam::Polygon py2 = disjoint_py<TypeParam>();
427 typename TypeParam::Geometrycollection empty_gc{};
428
429 gc.push_back(mpy);
430 this->test_valid_input(mpy, gc, empty_gc);
431
432 gc.push_back(py2);
433 this->test_valid_input(gc, mpy, py2);
434 }
435
436 TYPED_TEST(DifferenceTest, GeometrycollectionGeometrycollection) {
437 typename TypeParam::Geometrycollection gc1{};
438 typename TypeParam::Geometrycollection gc2{};
439 this->test_valid_input(gc1, gc1, gc1);
440
441 typename TypeParam::Point pt{0, 0};
442 typename TypeParam::Linestring ls = ls_overlapping_base_py<TypeParam>();
443 typename TypeParam::Polygon py = overlapping_py<TypeParam>();
444 typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>();
445 typename TypeParam::Multilinestring mls = simple_mls<TypeParam>();
446 typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>();
447 gc1.push_back(pt);
448 gc1.push_back(ls);
449 gc1.push_back(py);
450 gc1.push_back(mpt);
451 gc1.push_back(mls);
452 gc1.push_back(mpy);
453 gc1.push_back(gc1);
454
455 gc2.push_back(py);
456 gc2.push_back(pt);
457
458 typename TypeParam::Geometrycollection expected_result{};
459 expected_result.push_back(mpy[0]);
460 expected_result.push_back(ls_overlapping_base_py_difference<TypeParam>());
461
462 this->test_valid_input(gc1, gc2, expected_result);
463 }
464
465 } // namespace gis_differenceunittest