| 1 | /* Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 2 | This program is free software; you can redistribute it and/or modify |
| 3 | it under the terms of the GNU General Public License, version 2.0, |
| 4 | as published by the Free Software Foundation. |
| 5 | |
| 6 | This program is designed to work with certain software (including |
| 7 | but not limited to OpenSSL) that is licensed under separate terms, |
| 8 | as designated in a particular file or component or in included license |
| 9 | documentation. The authors of MySQL hereby grant you an additional |
| 10 | permission to link the program and your derivative works with the |
| 11 | separately licensed software that they have either included with |
| 12 | the program or referenced in the documentation. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License, version 2.0, for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "sql/gis/geometries.h" // gis::Geometry, gis::Coordinate_system |
| 26 | #include "sql/gis/is_simple.h" |
| 27 | |
| 28 | #include "unittest/gunit/gis_project_on_pole.h" |
| 29 | #include "unittest/gunit/gis_test.h" |
| 30 | #include "unittest/gunit/gis_test_geom.h" |
| 31 | #include "unittest/gunit/gis_typeset.h" |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | template <typename T_typeset> |
| 36 | struct Is_simple : Gis_test<T_typeset> { |
| 37 | bool is_simple(gis::Geometry const &g) { |
| 38 | assert(g.coordinate_system() == T_typeset::coordinate_system()); |
| 39 | |
| 40 | bool result; |
| 41 | { |
| 42 | bool result_null; |
| 43 | gis::is_simple(this->m_srs.get(), &g, "testcase", &result, &result_null); |
| 44 | assert(!result_null); |
| 45 | } |
| 46 | |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | // Implictly convert to geographic coordinates, projecting `g` onto the |
| 51 | // north pole if given cartesian coordinates while testing geographic. |
| 52 | template <typename T> |
| 53 | bool implicit_geo_is_simple(T const &g) { |
| 54 | if ((T_typeset::coordinate_system() == |
| 55 | gis::Coordinate_system::kGeographic) && |
| 56 | (g.coordinate_system() == gis::Coordinate_system::kCartesian)) |
| 57 | return is_simple(gis_project_on_pole(g)); |
| 58 | else |
| 59 | return is_simple(g); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | TYPED_TEST_SUITE(Is_simple, gis_typeset::Test_both); |
| 64 | using Is_simple_car = Is_simple<gis_typeset::Cartesian>; |
| 65 | using Is_simple_geo = Is_simple<gis_typeset::Geographic>; |
| 66 | |
| 67 | ///// Tests common to Cartesian and Geographic |
| 68 | |
| 69 | /// Point |
| 70 | TYPED_TEST(Is_simple, point_origin) { |
| 71 | EXPECT_TRUE(this->implicit_geo_is_simple(test_geom::point_origin())); |
| 72 | } |
| 73 | |
| 74 | /// Linestring |
| 75 | TYPED_TEST(Is_simple, linestring_line) { |
| 76 | EXPECT_TRUE(this->implicit_geo_is_simple(test_geom::linestring_line())); |
| 77 | } |
| 78 | TYPED_TEST(Is_simple, linestring_right_angle) { |
| 79 | EXPECT_TRUE( |
| 80 | this->implicit_geo_is_simple(test_geom::linestring_right_angle())); |
| 81 | } |
| 82 | TYPED_TEST(Is_simple, linestring_triangle) { |
| 83 | EXPECT_TRUE(this->implicit_geo_is_simple(test_geom::linestring_triangle())); |
| 84 | } |
| 85 | TYPED_TEST(Is_simple, linestring_selfintersecting) { |
| 86 | EXPECT_FALSE( |
| 87 | this->implicit_geo_is_simple(test_geom::linestring_selfintersecting())); |
| 88 | } |
| 89 | TYPED_TEST(Is_simple, linestring_selfintersecting_closed) { |
| 90 | EXPECT_FALSE(this->implicit_geo_is_simple( |
| 91 | test_geom::linestring_selfintersecting_closed())); |
| 92 | } |
| 93 | |
| 94 | /// Polygon, Multipolygon |
| 95 | // All valid polygons are simple; considering testing not worth it. |
| 96 | |
| 97 | ///// Cartesian only tests |
| 98 | |
| 99 | // This test was failing which put trough implicit_geo_is_simple. Most likely a |
| 100 | // numeric error. |
| 101 | TEST_F(Is_simple_car, linestring_selftangential_point_edge) { |
| 102 | EXPECT_FALSE(is_simple(test_geom::linestring_selftangential())); |
| 103 | } |
| 104 | |
| 105 | // TODO: Overload project_on_north_pole for geometrycollection and make these |
| 106 | // tests common for cartesian and geographic. |
| 107 | |
| 108 | TEST_F(Is_simple_car, geometrycollection_empty) { |
| 109 | EXPECT_TRUE(is_simple(test_geom::geometrycollection_empty())); |
| 110 | } |
| 111 | TEST_F(Is_simple_car, geometrycollection_crossintersecting) { |
| 112 | EXPECT_FALSE(is_simple(test_geom::geometrycollection_crossintersecting())); |
| 113 | } |
| 114 | TEST_F(Is_simple_car, geometrycollection_subintersecting) { |
| 115 | EXPECT_FALSE(is_simple(test_geom::geometrycollection_subintersecting())); |
| 116 | } |
| 117 | |
| 118 | ///// Geographic only tests |
| 119 | |
| 120 | // SQL/MM is ambiguous on whether pole-aliasing is simple, but Adam Wulkiewicz, |
| 121 | // Boost::geometry developer, agrees that we choose to consider it not simple. |
| 122 | TEST_F(Is_simple_geo, linestring_pole_alias) { |
| 123 | EXPECT_FALSE(is_simple(test_geom::linestring_pole_alias())); |
| 124 | } |
| 125 | |
| 126 | // Regression test for Bug#26495068 |
| 127 | // Certain geometries crossing the anti-meridian give an incorrect result. |
| 128 | TEST_F(Is_simple_geo, bug26495068) { |
| 129 | EXPECT_FALSE(is_simple(test_geom::bug26495068())); |
| 130 | } |
| 131 | |
| 132 | } // namespace |