| 1 | /* |
| 2 | Copyright (c) 2017, 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 | #include <gtest/gtest.h> |
| 26 | #include "sql/dd/dd.h" |
| 27 | #include "sql/dd/impl/types/spatial_reference_system_impl.h" |
| 28 | #include "sql/dd/types/spatial_reference_system.h" |
| 29 | #include "sql/gis/geometries.h" |
| 30 | #include "sql/gis/geometries_cs.h" |
| 31 | #include "sql/gis/is_valid.h" |
| 32 | |
| 33 | #include "unittest/gunit/gis_testshapes.h" |
| 34 | namespace isvalid_unittest { |
| 35 | std::unique_ptr<dd::Spatial_reference_system_impl> get_srs( |
| 36 | gis::Coordinate_system coordinate_system) { |
| 37 | switch (coordinate_system) { |
| 38 | case gis::Coordinate_system::kCartesian: { |
| 39 | // Use SRID 0. |
| 40 | return std::unique_ptr<dd::Spatial_reference_system_impl>(); |
| 41 | } |
| 42 | case gis::Coordinate_system::kGeographic: { |
| 43 | // EPSG 4326, but with long-lat axes (E-N). |
| 44 | std::unique_ptr<dd::Spatial_reference_system_impl> m_srs( |
| 45 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 46 | dd::create_object<dd::Spatial_reference_system>())); |
| 47 | m_srs->set_id(4326); |
| 48 | m_srs->set_name("WGS 84"); |
| 49 | m_srs->set_created(0UL); |
| 50 | m_srs->set_last_altered(0UL); |
| 51 | m_srs->set_organization("EPSG"); |
| 52 | m_srs->set_organization_coordsys_id(4326); |
| 53 | m_srs->set_definition( |
| 54 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 55 | "1984\",SPHEROID[\"WGS " |
| 56 | "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 57 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 58 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 59 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 60 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 61 | m_srs->set_description(""); |
| 62 | m_srs->parse_definition(); |
| 63 | return m_srs; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | throw std::exception(); /* purecov: dead code */ |
| 68 | } |
| 69 | |
| 70 | template <typename Types> |
| 71 | class IsvalidTest : public ::testing::Test { |
| 72 | public: |
| 73 | std::unique_ptr<const dd::Spatial_reference_system_impl> m_srs; |
| 74 | IsvalidTest() { m_srs = get_srs(Types::coordinate_system()); } |
| 75 | }; |
| 76 | |
| 77 | typedef ::testing::Types<Cartesian_types, Geographic_types> Types; |
| 78 | |
| 79 | TYPED_TEST_SUITE(IsvalidTest, Types); |
| 80 | |
| 81 | TYPED_TEST(IsvalidTest, Point) { |
| 82 | typename TypeParam::Point pt0{0, 0}; |
| 83 | bool result; |
| 84 | gis::is_valid(this->m_srs.get(), &pt0, "unittest", &result); |
| 85 | EXPECT_TRUE(result); |
| 86 | |
| 87 | typename TypeParam::Point pt1; |
| 88 | gis::is_valid(this->m_srs.get(), &pt1, "unittest", &result); |
| 89 | EXPECT_TRUE(result); |
| 90 | } |
| 91 | |
| 92 | TYPED_TEST(IsvalidTest, Linestring) { |
| 93 | typename TypeParam::Linestring ls{}; |
| 94 | EXPECT_TRUE(ls.is_empty()); |
| 95 | bool result; |
| 96 | gis::is_valid(this->m_srs.get(), &ls, "unittest", &result); |
| 97 | EXPECT_TRUE(result); |
| 98 | ls.push_back(typename TypeParam::Point(0.0, 0.0)); |
| 99 | gis::is_valid(this->m_srs.get(), &ls, "unittest", &result); |
| 100 | EXPECT_FALSE(result); // This should be a invalid linestring, but mysql |
| 101 | // should not generate it. |
| 102 | ls.push_back(typename TypeParam::Point(1.0, 1.0)); |
| 103 | gis::is_valid(this->m_srs.get(), &ls, "unittest", &result); |
| 104 | EXPECT_TRUE(result); |
| 105 | } |
| 106 | TYPED_TEST(IsvalidTest, Polygon) { |
| 107 | typename TypeParam::Linearring lr{}; |
| 108 | typename TypeParam::Polygon py{}; |
| 109 | bool result; |
| 110 | gis::is_valid(this->m_srs.get(), &py, "unittest", &result); |
| 111 | EXPECT_TRUE(result); |
| 112 | lr.push_back(typename TypeParam::Point(0.0, 0.0)); |
| 113 | py.push_back(lr); |
| 114 | gis::is_valid(this->m_srs.get(), &py, "unittest", &result); |
| 115 | EXPECT_FALSE(result); |
| 116 | auto py1 = selfTouchingPolygon<TypeParam>(); |
| 117 | gis::is_valid(this->m_srs.get(), &py1, "unittest", &result); |
| 118 | EXPECT_FALSE(result); |
| 119 | auto py2 = polygonWithTouchingHole<TypeParam>(); |
| 120 | gis::is_valid(this->m_srs.get(), &py2, "unittest", &result); |
| 121 | EXPECT_TRUE(result); |
| 122 | auto py3 = polygonSelfTouchEdgeVertice<TypeParam>(); |
| 123 | gis::is_valid(this->m_srs.get(), &py3, "unittest", &result); |
| 124 | EXPECT_FALSE(result); |
| 125 | auto py4 = polygonDisconnectedLimit<TypeParam>(); |
| 126 | gis::is_valid(this->m_srs.get(), &py4, "unittest", &result); |
| 127 | EXPECT_FALSE(result); |
| 128 | auto py5 = polygon_with_touching_hole_vertice_vertice<TypeParam>(); |
| 129 | gis::is_valid(this->m_srs.get(), &py5, "unittest", &result); |
| 130 | EXPECT_TRUE(result); |
| 131 | auto py6 = polygon_hourglass<TypeParam>(); |
| 132 | gis::is_valid(this->m_srs.get(), &py6, "unittest", &result); |
| 133 | EXPECT_FALSE(result); |
| 134 | auto py7 = polygon_empty_hole<TypeParam>(); |
| 135 | gis::is_valid(this->m_srs.get(), &py7, "unittest", &result); |
| 136 | EXPECT_FALSE(result); |
| 137 | auto py8 = polygon_reverse_touching_hole<TypeParam>(); |
| 138 | gis::is_valid(this->m_srs.get(), &py8, "unittest", &result); |
| 139 | EXPECT_FALSE(result); |
| 140 | auto py9 = polygon_reverse<TypeParam>(); |
| 141 | gis::is_valid(this->m_srs.get(), &py9, "unittest", &result); |
| 142 | EXPECT_FALSE(result); |
| 143 | auto py10 = polygon_open<TypeParam>(); |
| 144 | gis::is_valid(this->m_srs.get(), &py10, "unittest", &result); |
| 145 | EXPECT_FALSE(result); |
| 146 | auto py11 = polygon_inner_partially_outside<TypeParam>(); |
| 147 | gis::is_valid(this->m_srs.get(), &py11, "unittest", &result); |
| 148 | EXPECT_FALSE(result); |
| 149 | auto py12 = polygon_inner_wholly_outside<TypeParam>(); |
| 150 | gis::is_valid(this->m_srs.get(), &py12, "unittest", &result); |
| 151 | EXPECT_FALSE(result); |
| 152 | auto py13 = polygon_inner_intersecting<TypeParam>(); |
| 153 | gis::is_valid(this->m_srs.get(), &py13, "unittest", &result); |
| 154 | EXPECT_FALSE(result); |
| 155 | auto py14 = polygon_2_inner<TypeParam>(); |
| 156 | gis::is_valid(this->m_srs.get(), &py14, "unittest", &result); |
| 157 | EXPECT_TRUE(result); |
| 158 | auto py15 = polygon_2_inner_edge_to_edge0<TypeParam>(); |
| 159 | gis::is_valid(this->m_srs.get(), &py15, "unittest", &result); |
| 160 | EXPECT_FALSE(result); |
| 161 | auto py16 = polygon_2_inner_edge_to_edge1<TypeParam>(); |
| 162 | gis::is_valid(this->m_srs.get(), &py16, "unittest", &result); |
| 163 | EXPECT_FALSE(result); |
| 164 | auto py17 = polygon_2_inner_edge_to_vertice<TypeParam>(); |
| 165 | gis::is_valid(this->m_srs.get(), &py17, "unittest", &result); |
| 166 | EXPECT_TRUE(result); |
| 167 | auto py18 = polygon_2_inner_vertice_to_vertice<TypeParam>(); |
| 168 | gis::is_valid(this->m_srs.get(), &py18, "unittest", &result); |
| 169 | EXPECT_TRUE(result); |
| 170 | } |
| 171 | TYPED_TEST(IsvalidTest, Multipoint) { |
| 172 | typename TypeParam::Multipoint mpt{}; |
| 173 | bool result; |
| 174 | gis::is_valid(this->m_srs.get(), &mpt, "unittest", &result); |
| 175 | EXPECT_TRUE(result); |
| 176 | } |
| 177 | TYPED_TEST(IsvalidTest, Multilinestring) { |
| 178 | typename TypeParam::Multilinestring mls{}; |
| 179 | bool result; |
| 180 | gis::is_valid(this->m_srs.get(), &mls, "unittest", &result); |
| 181 | EXPECT_TRUE(result); |
| 182 | } |
| 183 | TYPED_TEST(IsvalidTest, Multipolygon) { |
| 184 | typename TypeParam::Multipolygon mpy{}; |
| 185 | bool result; |
| 186 | gis::is_valid(this->m_srs.get(), &mpy, "unittest", &result); |
| 187 | EXPECT_TRUE(result); |
| 188 | } |
| 189 | TYPED_TEST(IsvalidTest, Geometrycollection) { |
| 190 | typename TypeParam::Geometrycollection gc{}; |
| 191 | bool result; |
| 192 | gis::is_valid(this->m_srs.get(), &gc, "unittest", &result); |
| 193 | EXPECT_TRUE(result); |
| 194 | } |
| 195 | |
| 196 | TEST(IsvalidTest, bug26476445_1) { |
| 197 | auto mpy1 = multipolygon_1_bug26476445<Geographic_types>(); |
| 198 | bool result; |
| 199 | auto m_srs1 = get_srs(mpy1.coordinate_system()); |
| 200 | gis::is_valid(m_srs1.get(), &mpy1, "unittest", &result); |
| 201 | EXPECT_TRUE(result); |
| 202 | } |
| 203 | |
| 204 | TEST(IsvalidTest, bug26476445_2) { |
| 205 | auto mpy2 = multipolygon_2_bug26476445<Geographic_types>(); |
| 206 | bool result; |
| 207 | auto m_srs2 = get_srs(mpy2.coordinate_system()); |
| 208 | gis::is_valid(m_srs2.get(), &mpy2, "unittest", &result); |
| 209 | EXPECT_TRUE(result); |
| 210 | } |
| 211 | } // namespace isvalid_unittest |