This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 8bec3b0394d95da2e898087e7868f16f3ef3bf83 Author: jsorel <[email protected]> AuthorDate: Mon Nov 30 15:33:27 2026 +0100 test(Geometry): replace plain java test cases by junit 5 parameterized tests --- .../sis/geometries/operation/AsciiDocTestCase.java | 223 +++++++++++++ .../sis/geometries/operation/ContainsTest.adoc | 185 +++++++++++ .../sis/geometries/operation/ContainsTest.java | 100 ++---- .../sis/geometries/operation/CrossesTest.adoc | 151 +++++++++ .../sis/geometries/operation/CrossesTest.java | 96 ++---- .../sis/geometries/operation/DisjointTest.adoc | 149 +++++++++ .../sis/geometries/operation/DisjointTest.java | 76 ++--- .../apache/sis/geometries/operation/EqualTest.adoc | 105 ++++++ .../apache/sis/geometries/operation/EqualTest.java | 75 ++--- .../sis/geometries/operation/IntersectsTest.adoc | 360 +++++++++++++++++++++ .../sis/geometries/operation/IntersectsTest.java | 147 ++------- .../sis/geometries/operation/OverlapsTest.adoc | 151 +++++++++ .../sis/geometries/operation/OverlapsTest.java | 94 ++---- .../sis/geometries/operation/RelateTest.adoc | 146 +++++++++ .../sis/geometries/operation/RelateTest.java | 110 ++----- .../sis/geometries/operation/RelationShapes.adoc | 52 +++ .../sis/geometries/operation/TouchesTest.adoc | 177 ++++++++++ .../sis/geometries/operation/TouchesTest.java | 100 ++---- .../sis/geometries/operation/WithinTest.adoc | 176 ++++++++++ .../sis/geometries/operation/WithinTest.java | 92 ++---- 20 files changed, 2073 insertions(+), 692 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java new file mode 100644 index 0000000000..8db5b25d74 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AsciiDocTestCase.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.geometries.operation; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BiPredicate; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; +import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.adapter.WellKnownText3; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; +import org.junit.jupiter.api.function.Executable; + + +/** + * A test case read from an AsciiDoc file, with geometries in Well-Known Text 3. + * The file is laid out as below: + * + * <ul> + * <li>Each level-2 section is a test case, whose title is the test identifier: + * lowercase words separated by hyphens, unique in the file.</li> + * <li>The paragraph below the title describes the test case.</li> + * <li>Each line of the form {@code key:: `value`} gives an operand or the expected outcome. + * A value can be wrapped in {@code +} signs, which AsciiDoc renders verbatim.</li> + * <li>The text before the first level-2 section is documentation, and {@code //} lines are comments.</li> + * </ul> + * + * @param id the test identifier, which is the JUnit display name. + * @param description the description of the test case. + * @param values the operands and the expected outcome, by key. + * + * @author Johann Sorel (Geomatys) + */ +record AsciiDocTestCase(String id, String description, Map<String,String> values) { + /** + * Pattern of the title of a test case. + */ + private static final Pattern HEADER = Pattern.compile("== ([a-z0-9]+(?:-[a-z0-9]+)*)"); + + /** + * Pattern of a line giving an operand or the expected outcome. + */ + private static final Pattern ENTRY = Pattern.compile("([a-z]+):: `(.*)`"); + + /** + * Reads the test cases of an AsciiDoc file located next to the given class. + * + * @param caller the test class, used for locating the file. + * @param file the file name. + * @return the test cases, in the order of the file. + */ + static Stream<AsciiDocTestCase> read(final Class<?> caller, final String file) { + final List<AsciiDocTestCase> cases = new ArrayList<>(); + final Set<String> ids = new HashSet<>(); + String id = null; + final StringBuilder description = new StringBuilder(); + Map<String,String> values = new LinkedHashMap<>(); + final InputStream in = caller.getResourceAsStream(file); + if (in == null) { + fail("Test file not found: " + file); + } + try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith("//")) { + continue; + } + if (line.startsWith("== ")) { + final Matcher m = HEADER.matcher(line); + if (!m.matches() || !ids.add(m.group(1))) { + fail("Invalid or duplicated test identifier in " + file + ": " + line); + } + if (id != null) { + cases.add(create(id, description, values)); + } + id = m.group(1); + description.setLength(0); + values = new LinkedHashMap<>(); + } else if (id != null) { + final Matcher m = ENTRY.matcher(line); + if (m.matches()) { + String value = m.group(2); + if (value.length() >= 2 && value.startsWith("+") && value.endsWith("+")) { + value = value.substring(1, value.length() - 1); // AsciiDoc passthrough. + } + if (values.put(m.group(1), value) != null) { + fail("Duplicated " + m.group(1) + " in test " + id); + } + } else if (!line.isBlank()) { + if (!values.isEmpty()) { + fail("Unexpected text after the values of test " + id + ": " + line); + } + description.append(description.isEmpty() ? "" : " ").append(line.strip()); + } + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + if (id != null) { + cases.add(create(id, description, values)); + } + return cases.stream(); + } + + /** + * Creates a test case after checking that it has a description and an expected outcome. + */ + private static AsciiDocTestCase create(final String id, final CharSequence description, final Map<String,String> values) { + if (description.isEmpty()) { + fail("Test " + id + " has no description."); + } + if (values.containsKey("result") == values.containsKey("error")) { + fail("Test " + id + " shall have either a result or an error."); + } + return new AsciiDocTestCase(id, description.toString(), values); + } + + /** + * Returns a required value. + */ + String value(final String key) { + final String value = values.get(key); + if (value == null) { + fail("Test " + id + " has no " + key + '.'); + } + return value; + } + + /** + * Returns a required geometry, decoded from Well-Known Text 3. + */ + Geometry geometry(final String key) { + return new WellKnownText3().decode(value(key)); + } + + /** + * Returns the expected boolean result. + */ + boolean booleanResult() { + final String result = value("result"); + if (!result.equals("true") && !result.equals("false")) { + fail("Test " + id + " shall have a true or false result."); + } + return Boolean.parseBoolean(result); + } + + /** + * Returns whether this test case expects an error instead of a result. + */ + boolean expectsError() { + return values.containsKey("error"); + } + + /** + * Asserts that the given operation throws the expected error, an exception whose class + * or one of its parent classes has the simple name given by the {@code error} value. + */ + void assertError(final Executable operation) { + final String expected = value("error"); + final Exception error = assertThrows(Exception.class, operation, description); + for (Class<?> type = error.getClass(); type != null; type = type.getSuperclass()) { + if (type.getSimpleName().equals(expected)) { + return; + } + } + throw new AssertionError("Expected " + expected + " but got " + error, error); + } + + /** + * Verifies a relation between the {@code input} and {@code other} geometries: + * its result against the {@code result} value, or its failure against the {@code error} value. + * + * @param relation the relation to test, invoked with the input then the other geometry. + */ + void assertRelation(final BiPredicate<Geometry, Geometry> relation) { + final Geometry input = geometry("input"); + final Geometry other = geometry("other"); + if (expectsError()) { + assertError(() -> relation.test(input, other)); + } else { + assertEquals(booleanResult(), relation.test(input, other), description); + } + } + + /** + * Returns the identifier, which is the JUnit display name of the test case. + */ + @Override + public String toString() { + return id; + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.adoc new file mode 100644 index 0000000000..5d4ad16238 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.adoc @@ -0,0 +1,185 @@ += Contains +:description: Test cases of GeometryProcessor.contains(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` contains `other` when no position of `other` is in the exterior of `input`, +and at least one position of the interior of `other` is in the interior of `input`. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-empty + +The empty set is a superset of itself: two empty geometries contain each other. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +== empty-point + +The empty set has no position, so it does not contain a point. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +Every geometry contains the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +A point contains another point at the same position: the result depends on the positions, not on the instances. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `true` + +== point-other-position + +A point does not contain a point at another position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +== point-other-position-reversed + +Neither point contains the other, whatever the operand order. + +input:: `G_POINT [20 15]` +other:: `G_POINT [10 5]` +result:: `false` + +// ─── A surface and a position ────────────────────────────────────────────────── + +== square-point-inside + +The square contains a point strictly inside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POINT [5 5]` +result:: `true` + +== square-point-outside + +The square does not contain a point outside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POINT [20 20]` +result:: `false` + +== hole-point-in-hole + +The center of the square with hole is in the hole, so the polygon does not contain it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [4 4, 6 4, 6 6, 4 6, 4 4])` +other:: `G_POINT [5 5]` +result:: `false` + +// ─── A curve and a position ──────────────────────────────────────────────────── + +== line-point-interior + +A line contains a point interior to it. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POINT [5 0]` +result:: `true` + +== line-point-end + +An end point is on the boundary of the line, and none of its positions is interior to the line. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POINT [0 0]` +result:: `false` + +// ─── A surface and a curve ───────────────────────────────────────────────────── + +== square-line-inside + +The square contains a line strictly inside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING [2 5, 8 5]` +result:: `true` + +== square-line-on-edge + +A line lying on the edge of the square has no position interior to the square. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` + +== square-line-crossing + +A line passing through the square goes outside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING [5 -5, 5 15]` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-inner + +The square contains a smaller square strictly inside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `true` + +== square-overlap + +The square does not contain a square extending beyond it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `false` + +== square-same-position + +Two squares at the same position contain each other. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +// ─── Sets of geometries ──────────────────────────────────────────────────────── + +== square-points-inside + +A set of points is contained when all its points are. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_COLLECTION POINT [2 2, 5 5]` +result:: `true` + +== square-points-spread + +A set with one point outside the square is not contained. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_COLLECTION POINT [5 5, 20 20]` +result:: `false` + +== polygons-square + +A set of polygons contains a square equal to one of its polygons. + +input:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.java index f9467191e0..7781419d26 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ContainsTest.java @@ -16,102 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code contains} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code ContainsTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class ContainsTest extends AbstractD9IMTest { +public class ContainsTest { /** - * The inputs and expected result of a single test of {@code contains(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public ContainsTest() { } /** - * All test cases of {@code contains(Geometry, Geometry)}. + * Returns the test cases declared in {@code ContainsTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - // The empty set is a superset of itself only, and a subset of every geometry. - new TestCase(EMPTY_1, EMPTY_1, true, null), - new TestCase(EMPTY_1, EMPTY_2, true, null), - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, true, null), - // points - new TestCase(POINT_A, POINT_A, true, null), - new TestCase(POINT_A, POINT_A_BIS, true, null), - new TestCase(POINT_A, POINT_B, false, null), - new TestCase(POINT_B, POINT_A, false, null), - /* - * A surface and a position. Only the positions interior to the surface are contained: - * the center of a surface with a hole is in that hole and is therefore not. - * - * A position exactly on the boundary of a surface is left out of this table on purpose. - * That case is verified by `WithinTest`, which states the same thing the other way round - * without going through the point-in-polygon shortcut of `contains(Polygon, Point)`. - */ - new TestCase(SQUARE, POINT_CENTER, true, null), - new TestCase(SQUARE, POINT_OUTSIDE, false, null), - new TestCase(SQUARE_WITH_HOLE, POINT_CENTER, false, null), - /* - * A curve and a position. An end position of the curve is on its boundary, - * and a geometry does not contain the positions of its own boundary. - */ - new TestCase(LINE_BOTTOM, POINT_ON_EDGE, true, null), - new TestCase(LINE_BOTTOM, POINT_CORNER, false, null), - /* - * A surface and a curve. A curve lying on the boundary of a surface is not contained - * by it, no position of that curve being interior to the surface. - */ - new TestCase(SQUARE, LINE_INSIDE, true, null), - new TestCase(SQUARE, LINE_BOTTOM, false, null), - new TestCase(SQUARE, LINE_CROSSING, false, null), - /* - * Two surfaces. Two surfaces at the same position contain each other. - */ - new TestCase(SQUARE, SQUARE_INNER, true, null), - new TestCase(SQUARE, SQUARE_OVERLAP, false, null), - new TestCase(SQUARE, SQUARE_BIS, true, null), - /* - * Sets of geometries. A set is contained when all of its elements are. - */ - new TestCase(SQUARE, MULTI_POINT_INSIDE, true, null), - new TestCase(SQUARE, MULTI_POINT_SPREAD, false, null), - new TestCase(MULTI_POLYGON, SQUARE, true, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(ContainsTest.class, "ContainsTest.adoc"); + } /** - * Tests {@code contains(Geometry, Geometry)} on all declared test cases. + * Tests {@code contains(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testContains() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().contains(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testContains(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::contains); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.adoc new file mode 100644 index 0000000000..438899dae2 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.adoc @@ -0,0 +1,151 @@ += Crosses +:description: Test cases of GeometryProcessor.crosses(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` crosses `other` when their interiors share a position, neither geometry contains the other, +and what they share has a lower dimension than at least one of them. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set has no interior, so it crosses nothing. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +Nothing crosses the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +== empty-empty + +Two empty sets do not cross. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position do not cross: a single position never crosses anything. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `false` + +== point-other-position + +Two points at different positions share no position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +== point-inside-square + +A single position cannot be both inside and outside the square. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== point-on-line + +A point on a line does not cross it: a single position has nothing to put in the exterior. + +input:: `G_POINT [5 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` + +// ─── Two curves ──────────────────────────────────────────────────────────────── + +== line-crossing + +Two lines whose interiors meet at an isolated position cross. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 -5, 5 15]` +result:: `true` + +== line-shared-end + +Two lines sharing only an end point do not cross. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 0, 10 10]` +result:: `false` + +== line-collinear + +Two lines sharing a whole segment do not cross: what they share is a curve, not a position. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 0, 15 0]` +result:: `false` + +// ─── A curve and a surface ───────────────────────────────────────────────────── + +== line-through-square + +A line passing through the square crosses it. + +input:: `G_LINESTRING [5 -5, 5 15]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== line-inside-square + +A line staying inside the square does not cross it. + +input:: `G_LINESTRING [2 5, 8 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== line-on-edge + +A line lying on the edge of the square does not cross it. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-overlap + +Two surfaces never cross: what they share is a surface. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `false` + +// ─── Sets of positions ───────────────────────────────────────────────────────── + +== points-straddling + +A set of points with one point inside the square and one outside crosses it. + +input:: `G_COLLECTION POINT [5 5, 20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== points-inside + +A set of points entirely inside the square does not cross it. + +input:: `G_COLLECTION POINT [2 2, 5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.java index 2bacf35366..64b3b28152 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/CrossesTest.java @@ -16,98 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code crosses} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code CrossesTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class CrossesTest extends AbstractD9IMTest { +public class CrossesTest { /** - * The inputs and expected result of a single test of {@code crosses(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public CrossesTest() { } /** - * All test cases of {@code crosses(Geometry, Geometry)}. + * Returns the test cases declared in {@code CrossesTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * Crossing requires the two interiors to share a position while neither geometry - * contains the other. The empty set has no interior, therefore it crosses nothing. - */ - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_2, false, null), - // points - new TestCase(POINT_A, POINT_A_BIS, false, null), - new TestCase(POINT_A, POINT_B, false, null), - /* - * A single position never crosses anything: crossing requires a position shared with - * the interior of the other geometry and another one in its exterior, and a position - * has only one position to offer. - */ - new TestCase(POINT_CENTER, SQUARE, false, null), - new TestCase(POINT_ON_EDGE, LINE_BOTTOM, false, null), - /* - * Two curves cross when their interiors meet at isolated positions. Sharing only an end - * position is not crossing, and sharing a whole segment is not either: what they have in - * common is then a curve, not a position. - */ - new TestCase(LINE_BOTTOM, LINE_CROSSING, true, null), - new TestCase(LINE_BOTTOM, LINE_DIAGONAL, false, null), - new TestCase(LINE_BOTTOM, LINE_COLLINEAR, false, null), - /* - * A curve and a surface. The curve crosses when it passes through the surface, and does - * not when it stays inside it or on its boundary. - */ - new TestCase(LINE_CROSSING, SQUARE, true, null), - new TestCase(LINE_INSIDE, SQUARE, false, null), - new TestCase(LINE_BOTTOM, SQUARE, false, null), - /* - * Two surfaces never cross: what they share is a surface, never of a lower dimension. - */ - new TestCase(SQUARE, SQUARE_OVERLAP, false, null), - /* - * A set of positions straddling a surface crosses it, which a single position cannot do. - */ - new TestCase(MULTI_POINT_SPREAD, SQUARE, true, null), - new TestCase(MULTI_POINT_INSIDE, SQUARE, false, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(CrossesTest.class, "CrossesTest.adoc"); + } /** - * Tests {@code crosses(Geometry, Geometry)} on all declared test cases. + * Tests {@code crosses(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testCrosses() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().crosses(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testCrosses(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::crosses); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.adoc new file mode 100644 index 0000000000..95bf71362e --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.adoc @@ -0,0 +1,149 @@ += Disjoint +:description: Test cases of GeometryProcessor.disjoint(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` and `other` are disjoint when they have no position in common. +This is the negation of `intersects`. + +include::RelationShapes.adoc[] + +// ─── The empty set ─────────────────────────────────────────────────────────── + +== empty-point + +The empty set has no position in common with any geometry. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `true` + +== point-empty + +The empty set has no position in common with any geometry, whatever the operand order. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +== empty-empty + +Two empty sets have no position in common. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +// ─── Points ────────────────────────────────────────────────────────────────── + +== point-same-position + +Two distinct points at the same position are not disjoint: the result depends on the positions, not on the instances. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `false` + +== point-other-position + +Two points at different positions are disjoint. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `true` + +== point-other-position-reversed + +Disjunction is symmetric: swapping the two points gives the same result. + +input:: `G_POINT [20 15]` +other:: `G_POINT [10 5]` +result:: `true` + +// ─── Points against curves and surfaces ────────────────────────────────────── + +== point-outside-square + +A point outside the square has no position in common with it. + +input:: `G_POINT [20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== point-inside-square + +A point strictly inside the square is in its interior. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== point-in-hole + +A point strictly inside the hole of a polygon is outside that polygon. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [4 4, 6 4, 6 6, 4 6, 4 4])` +result:: `true` + +== point-off-line + +A point far from a line has no position in common with it. + +input:: `G_POINT [20 20]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `true` + +// ─── Curves ────────────────────────────────────────────────────────────────── + +== line-parallel + +Two parallel lines at a distance of 20 are disjoint. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 20, 10 20]` +result:: `true` + +== line-shared-end + +Two lines meeting only at an end point they have in common are not disjoint. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 0, 10 10]` +result:: `false` + +== line-far-square + +A line at a distance of 10 above the square is disjoint from it. + +input:: `G_LINESTRING [0 20, 10 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +// ─── Surfaces ──────────────────────────────────────────────────────────────── + +== square-disjoint + +Two squares far from each other are disjoint. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `true` + +== square-touching + +Two squares sharing a whole edge have the positions of that edge in common. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `false` + +== multipolygon-line-inside + +A line strictly inside one polygon of a collection is not disjoint from the collection. + +input:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +other:: `G_LINESTRING [2 5, 8 5]` +result:: `false` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.java index 5b1f0ad985..621fefdfb7 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DisjointTest.java @@ -16,78 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code disjoint} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code DisjointTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class DisjointTest extends AbstractD9IMTest { +public class DisjointTest { /** - * The inputs and expected result of a single test of {@code disjoint(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public DisjointTest() { } /** - * All test cases of {@code disjoint(Geometry, Geometry)}. + * Returns the test cases declared in {@code DisjointTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - // The empty set has no position in common with any geometry. - new TestCase(EMPTY_1, NON_EMPTY, true, null), - new TestCase(NON_EMPTY, EMPTY_1, true, null), - new TestCase(EMPTY_1, EMPTY_1, true, null), - new TestCase(EMPTY_1, EMPTY_2, true, null), - // points - new TestCase(POINT_A, POINT_A_BIS, false, null), - new TestCase(POINT_A, POINT_B, true, null), - new TestCase(POINT_B, POINT_A, true, null), - /* - * Disjunction is the negation of intersection, and is verified here on the same pairs. - */ - new TestCase(POINT_OUTSIDE, SQUARE, true, null), - new TestCase(POINT_CENTER, SQUARE, false, null), - new TestCase(POINT_CENTER, SQUARE_WITH_HOLE, true, null), - new TestCase(POINT_OUTSIDE, LINE_BOTTOM, true, null), - new TestCase(LINE_BOTTOM, LINE_FAR, true, null), - new TestCase(LINE_BOTTOM, LINE_DIAGONAL, false, null), - new TestCase(LINE_FAR, SQUARE, true, null), - new TestCase(SQUARE, SQUARE_DISJOINT, true, null), - new TestCase(SQUARE, SQUARE_TOUCHING, false, null), - new TestCase(MULTI_POLYGON, LINE_INSIDE, false, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(DisjointTest.class, "DisjointTest.adoc"); + } /** - * Tests {@code disjoint(Geometry, Geometry)} on all declared test cases. + * Tests {@code disjoint(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testDisjoint() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().disjoint(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testDisjoint(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::disjoint); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.adoc new file mode 100644 index 0000000000..9cdb941bd5 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.adoc @@ -0,0 +1,105 @@ += Equal +:description: Test cases of GeometryProcessor.equal(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` and `other` are equal when they are the same set of positions, +whatever the instances or the way they were built. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-empty + +Any two empty geometries are equal: the empty set is unique. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +== empty-point + +The empty set is not equal to a point. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +A point is not equal to the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position are equal. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `true` + +== point-other-position + +Two points at different positions are not equal. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +== point-center-edge + +The center of the square and a point on its edge are not equal. + +input:: `G_POINT [5 5]` +other:: `G_POINT [5 0]` +result:: `false` + +// ─── Curves and surfaces ─────────────────────────────────────────────────────── + +== square-same-position + +Two squares at the same position are equal. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== square-overlap + +Two squares at different positions are not equal. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `false` + +== square-inner + +A square is not equal to a smaller square inside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `false` + +== line-collinear + +Two collinear lines overlapping on half of their length are not equal. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 0, 15 0]` +result:: `false` + +== square-line-on-edge + +A surface is not equal to a line on its boundary: one is a surface, the other a curve. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.java index fc1e95e305..c867ff6478 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/EqualTest.java @@ -16,77 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code equal} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code EqualTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class EqualTest extends AbstractD9IMTest { +public class EqualTest { /** - * The inputs and expected result of a single test of {@code equal(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public EqualTest() { } /** - * All test cases of {@code equal(Geometry, Geometry)}. + * Returns the test cases declared in {@code EqualTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - // The empty set being unique, any two empty geometries are equal and nothing else is. - new TestCase(EMPTY_1, EMPTY_1, true, null), - new TestCase(EMPTY_1, EMPTY_2, true, null), - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - // points - new TestCase(POINT_A, POINT_A, true, null), - new TestCase(POINT_A, POINT_A_BIS, true, null), - new TestCase(POINT_A, POINT_B, false, null), - /* - * Equality compares the positions, not the identity nor the way the geometry was built. - */ - new TestCase(SQUARE, SQUARE_BIS, true, null), - new TestCase(SQUARE, SQUARE_OVERLAP, false, null), - new TestCase(SQUARE, SQUARE_INNER, false, null), - new TestCase(LINE_BOTTOM, LINE_COLLINEAR, false, null), - new TestCase(POINT_CENTER, POINT_ON_EDGE, false, null), - /* - * A surface and its own boundary are not equal: one is a surface, the other a curve. - */ - new TestCase(SQUARE, LINE_BOTTOM, false, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(EqualTest.class, "EqualTest.adoc"); + } /** - * Tests {@code equal(Geometry, Geometry)} on all declared test cases. + * Tests {@code equal(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testEqual() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().equal(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testEqual(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::equal); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.adoc new file mode 100644 index 0000000000..7f22a1dd30 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.adoc @@ -0,0 +1,360 @@ += Intersects +:description: Test cases of GeometryProcessor.intersects(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` intersects `other` when they have at least one position in common. +This is the negation of `disjoint`. + +// The ray against a plane of finite size is tested in IntersectsTest.java: +// Well-Known Text 3 cannot hold a bounded plane of Apache SIS. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set intersects nothing. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +Nothing intersects the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +== empty-empty + +Two empty sets have no position in common. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position intersect. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `true` + +== point-other-position + +Two points at different positions do not intersect. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +== point-other-position-reversed + +Intersection is symmetric: swapping the two points gives the same result. + +input:: `G_POINT [20 15]` +other:: `G_POINT [10 5]` +result:: `false` + +// ─── A position and a surface ────────────────────────────────────────────────── + +== point-inside-square + +A point inside the square intersects it. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== point-on-edge-square + +A point on the boundary of the square intersects it. + +input:: `G_POINT [5 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== point-outside-square + +A point outside the square does not intersect it. + +input:: `G_POINT [20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== point-in-hole + +The center of the square with hole is in the hole, so it does not intersect the polygon. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [4 4, 6 4, 6 6, 4 6, 4 4])` +result:: `false` + +// ─── A position and a curve ──────────────────────────────────────────────────── + +== point-on-line + +A point interior to the line intersects it. + +input:: `G_POINT [5 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `true` + +== point-line-end + +An end point of the line intersects it. + +input:: `G_POINT [0 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `true` + +== point-off-line + +A point far from the line does not intersect it. + +input:: `G_POINT [20 20]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` + +// ─── Two curves ──────────────────────────────────────────────────────────────── + +== line-crossing + +Two lines crossing each other intersect. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 -5, 5 15]` +result:: `true` + +== line-shared-end + +Two lines sharing only an end point intersect. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 0, 10 10]` +result:: `true` + +== line-parallel + +Two parallel lines at a distance of 20 do not intersect. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 20, 10 20]` +result:: `false` + +// ─── A curve and a surface ───────────────────────────────────────────────────── + +== line-through-square + +A line passing through the square intersects it. + +input:: `G_LINESTRING [5 -5, 5 15]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== line-on-edge + +A line lying on the edge of the square intersects it. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== line-far-square + +A line at a distance of 10 above the square does not intersect it. + +input:: `G_LINESTRING [0 20, 10 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-overlap + +Two overlapping squares intersect. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `true` + +== square-touching + +Two squares sharing only an edge intersect. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `true` + +== square-disjoint + +Two squares far from each other do not intersect. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `false` + +// ─── Sets of geometries ──────────────────────────────────────────────────────── + +== points-spread-square + +A set intersects a geometry as soon as one of its elements does. + +input:: `G_COLLECTION POINT [5 5, 20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== lines-square + +A set of lines intersects the square when one of its lines does. + +input:: `G_COLLECTION LINESTRING (G_LINESTRING [0 0, 10 0], G_LINESTRING [0 20, 10 20])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== polygons-square-inner + +A set of polygons intersects a square inside one of its polygons. + +input:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `true` + +== collection-square + +A heterogeneous collection intersects the square when one of its elements does. + +input:: `G_COLLECTION (G_POINT [5 5], G_LINESTRING [0 0, 10 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +// ─── Three-dimensional geometries ────────────────────────────────────────────── + +== ray-triangle-inside + +A vertical ray from below passes through the triangle. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 1]` +other:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +result:: `true` + +== triangle-ray-inside + +Intersection is symmetric: the triangle intersects the ray passing through it. + +input:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +other:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 1]` +result:: `true` + +== ray-triangle-outside + +A vertical ray from below passes outside the triangle. + +input:: `G_RAY <P:3 ; DIRECTION:3> [3 3 0] [0 0 1]` +other:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +result:: `false` + +== ray-triangle-after + +A ray starting above the triangle and going up never reaches it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 6] [0 0 1]` +other:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +result:: `false` + +== ray-plane-oblique + +An oblique ray going up reaches the plane z = 5. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 1 1]` +other:: `G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` +result:: `true` + +== ray-plane-away + +A ray going down never reaches the plane z = 5. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 -1]` +other:: `G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` +result:: `false` + +== ray-ring-through + +A horizontal ray in the plane of the 3D ring meets its edges. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 2 1] [1 0 0]` +other:: `G_LINESTRING <P:3, TEST:1> [0 0 1, 4 0 1, 4 4 1, 0 4 1, 0 0 1] [0, 10, 20, 40, 0]` +result:: `true` + +== ray-ring-above + +A horizontal ray above the plane of the 3D ring misses it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 2 2] [1 0 0]` +other:: `G_LINESTRING <P:3, TEST:1> [0 0 1, 4 0 1, 4 4 1, 0 4 1, 0 0 1] [0, 10, 20, 40, 0]` +result:: `false` + +== triangle-plane + +A triangle lying in the plane z = 5 intersects it. + +input:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +other:: `G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` +result:: `true` + +== plane-triangle + +Intersection is symmetric: the plane z = 5 intersects the triangle lying in it. + +input:: `G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` +other:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +result:: `true` + +== triangle-box + +The triangle at z = 5 is above the box. + +input:: `G_TRIANGLE <P:3, TEST:1> ([0 0 5, 4 0 5, 0 4 5, 0 0 5] [10, 20, 30, 10])` +other:: `G_BBOX <P:3> [0 0 0, 2 2 2]` +result:: `false` + +== box-triangle-through + +A large triangle at z = 1 passes through the box. + +input:: `G_BBOX <P:3> [0 0 0, 2 2 2]` +other:: `G_TRIANGLE <P:3> ([-10 -10 1, 10 -10 1, 0 10 1, -10 -10 1])` +result:: `true` + +== ray-box-through + +A horizontal ray through the middle of the box intersects it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 1 1] [1 0 0]` +other:: `G_BBOX <P:3> [0 0 0, 2 2 2]` +result:: `true` + +== ray-box-beside + +A horizontal ray passing beside the box misses it. + +input:: `G_RAY <P:3 ; DIRECTION:3> [-1 3 1] [1 0 0]` +other:: `G_BBOX <P:3> [0 0 0, 2 2 2]` +result:: `false` + +== ray-square-unsupported + +The intersection of a 3D ray with a 2D polygon is not supported. + +input:: `G_RAY <P:3 ; DIRECTION:3> [1 2 0] [0 0 1]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +error:: `UnsupportedOperationException` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java index 36840f628c..226ea800e4 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java @@ -16,151 +16,62 @@ */ package org.apache.sis.geometries.operation; -import java.util.Arrays; +import java.util.stream.Stream; import org.apache.sis.geometries.Geometry; -import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.Plane; -import org.apache.sis.geometries.surface.Triangle; import org.apache.sis.maths.Vector; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code intersects} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code IntersectsTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class IntersectsTest extends AbstractD9IMTest { +public class IntersectsTest { /** - * The inputs and expected result of a single test of {@code intersects(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public IntersectsTest() { } /** - * All test cases of {@code intersects(Geometry, Geometry)}. - */ - private static final TestCase[] ENTRIES = { - // Negation of `disjoint(Geometry, Geometry)`. - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_2, false, null), - //points - new TestCase(POINT_A, POINT_A_BIS, true, null), - new TestCase(POINT_A, POINT_B, false, null), - new TestCase(POINT_B, POINT_A, false, null), - /* - * A position and a surface. A position on the boundary of a surface meets it, and the - * center of a surface with a hole does not, that position being in the hole. - */ - new TestCase(POINT_CENTER, SQUARE, true, null), - new TestCase(POINT_ON_EDGE, SQUARE, true, null), - new TestCase(POINT_OUTSIDE, SQUARE, false, null), - new TestCase(POINT_CENTER, SQUARE_WITH_HOLE, false, null), - /* - * A position and a curve, the position being interior to the curve, an end of it, - * or on neither. - */ - new TestCase(POINT_ON_EDGE, LINE_BOTTOM, true, null), - new TestCase(POINT_CORNER, LINE_BOTTOM, true, null), - new TestCase(POINT_OUTSIDE, LINE_BOTTOM, false, null), - /* - * Two curves crossing each other, sharing only an end position, or sharing nothing. - */ - new TestCase(LINE_BOTTOM, LINE_CROSSING, true, null), - new TestCase(LINE_BOTTOM, LINE_DIAGONAL, true, null), - new TestCase(LINE_BOTTOM, LINE_FAR, false, null), - /* - * A curve and a surface, the curve passing through it, lying on its boundary, or - * staying away from it. - */ - new TestCase(LINE_CROSSING, SQUARE, true, null), - new TestCase(LINE_BOTTOM, SQUARE, true, null), - new TestCase(LINE_FAR, SQUARE, false, null), - /* - * Two surfaces overlapping, sharing only an edge, or sharing nothing. - */ - new TestCase(SQUARE, SQUARE_OVERLAP, true, null), - new TestCase(SQUARE, SQUARE_TOUCHING, true, null), - new TestCase(SQUARE, SQUARE_DISJOINT, false, null), - /* - * Sets of geometries. A set meets another geometry as soon as one of its elements does. - */ - new TestCase(MULTI_POINT_SPREAD, SQUARE, true, null), - new TestCase(MULTI_LINE, SQUARE, true, null), - new TestCase(MULTI_POLYGON, SQUARE_INNER, true, null), - new TestCase(COLLECTION, SQUARE, true, null), - /* - * A ray and a triangle, a hyperplane or a line string, in either order. The ray touches - * the triangle inside, outside, or starts after it. A plane of finite size is not supported. - */ - new TestCase(ray(1, 2, 0, 0, 0, 1), TRIANGLE_3D, true, null), - new TestCase(TRIANGLE_3D, ray(1, 2, 0, 0, 0, 1), true, null), - new TestCase(ray(3, 3, 0, 0, 0, 1), TRIANGLE_3D, false, null), - new TestCase(ray(1, 2, 6, 0, 0, 1), TRIANGLE_3D, false, null), - new TestCase(ray(1, 2, 0, 0, 1, 1), PLANE_Z5, true, null), - new TestCase(ray(1, 2, 0, 0, 0, -1), PLANE_Z5, false, null), - new TestCase(ray(-1, 2, 1, 1, 0, 0), RING_3D, true, null), - new TestCase(ray(-1, 2, 2, 1, 0, 0), RING_3D, false, null), - new TestCase(ray(1, 2, 0, 0, 0, 1), quad(), null, OperationException.class), - new TestCase(TRIANGLE_3D, PLANE_Z5, true, null), - new TestCase(TRIANGLE_3D, BOX_3D, false, null), - new TestCase(BOX_3D, triangle(-10, -10, 1, 10, -10, 1, 0, 10, 1), true, null), - new TestCase(PLANE_Z5, TRIANGLE_3D, true, null), - new TestCase(ray(-1, 1, 1, 1, 0, 0), BOX_3D, true, null), - new TestCase(ray(-1, 3, 1, 1, 0, 0), BOX_3D, false, null), - new TestCase(ray(1, 2, 0, 0, 0, 1), SQUARE, null, UnsupportedOperationException.class) - }; - - /** - * Returns the triangle of the given corners, in {@link #CRS_3D}. + * Returns the test cases declared in {@code IntersectsTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static Triangle triangle(final double... corners) { - final double[] closed = Arrays.copyOf(corners, 12); - System.arraycopy(corners, 0, closed, 9, 3); - return GeometryFactory.DEFAULT.createTriangle(GeometryFactory.DEFAULT.createLinearRing( - GeometryFactory.DEFAULT.createDataPoints(CRS_3D, closed))); + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(IntersectsTest.class, "IntersectsTest.adoc"); } /** - * Returns the square of size 2 × 2 centered on (0 0 5) in the plane <var>z</var> = 5. + * Tests {@code intersects(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - private static Plane quad() { - final Plane plane = new Plane(CRS_3D, Vector.of(0, 0, 5), Vector.of(0, 0, 1)); - plane.setSizeX(2); - plane.setSizeZ(2); - return plane; + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testIntersects(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::intersects); } /** - * Tests {@code intersects(Geometry, Geometry)} on all declared test cases. + * Tests {@code intersects(Geometry, Geometry)} between a ray and a plane of finite size, which is not supported. + * This case is not in {@code IntersectsTest.adoc} because Well-Known Text 3 cannot hold a bounded plane. */ @Test - public void testIntersects() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().intersects(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + public void testBoundedPlane() { + final Plane quad = new Plane(AbstractD9IMTest.CRS_3D, Vector.of(0.0, 0.0, 5.0), Vector.of(0.0, 0.0, 1.0)); + quad.setSizeX(2); + quad.setSizeZ(2); + final Geometry ray = AbstractD9IMTest.ray(1, 2, 0, 0, 0, 1); + assertThrows(OperationException.class, () -> new GeometryProcessor().intersects(ray, quad)); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.adoc new file mode 100644 index 0000000000..2c1cdc71ea --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.adoc @@ -0,0 +1,151 @@ += Overlaps +:description: Test cases of GeometryProcessor.overlaps(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` overlaps `other` when they have the same dimension, their interiors share a position, +and neither geometry contains the other. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set has no interior, so it overlaps nothing. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +Nothing overlaps the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +== empty-empty + +Two empty sets do not overlap. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position contain each other, so they do not overlap. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `false` + +== point-other-position + +Two points at different positions share no position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +// ─── Different dimensions ────────────────────────────────────────────────────── + +== point-square + +A position never overlaps a surface: overlapping requires the same dimension. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== line-square + +A curve never overlaps a surface. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── Two curves ──────────────────────────────────────────────────────────────── + +== line-collinear + +Two lines sharing a whole segment, neither containing the other, overlap. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 0, 15 0]` +result:: `true` + +== line-crossing + +Two lines sharing a single position do not overlap: what they share has a lower dimension. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 -5, 5 15]` +result:: `false` + +== line-shared-end + +Two lines sharing only an end point do not overlap. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 0, 10 10]` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-overlap + +Two squares sharing a quarter of their area overlap. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `true` + +== square-inner + +A square does not overlap a square it contains. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `false` + +== square-same-position + +Two squares at the same position contain each other, so they do not overlap. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== square-touching + +Two squares sharing only an edge have no interior position in common. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `false` + +== square-disjoint + +Two squares far from each other do not overlap. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `false` + +// ─── Sets of positions ───────────────────────────────────────────────────────── + +== points-sharing-one + +Two sets of points sharing one point, neither containing the other, overlap. + +input:: `G_COLLECTION POINT [2 2, 5 5]` +other:: `G_COLLECTION POINT [5 5, 20 20]` +result:: `true` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.java index c335a17f4f..da40c49fba 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/OverlapsTest.java @@ -16,96 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code overlaps} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code OverlapsTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class OverlapsTest extends AbstractD9IMTest { +public class OverlapsTest { /** - * The inputs and expected result of a single test of {@code overlaps(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public OverlapsTest() { } /** - * All test cases of {@code overlaps(Geometry, Geometry)}. + * Returns the test cases declared in {@code OverlapsTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * Overlapping requires the two interiors to share a position while neither geometry - * contains the other. The empty set has no interior, therefore it overlaps nothing. - */ - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_2, false, null), - /* - * Two points cannot overlap: either they are at the same position, and then each one - * contains the other, or they share no position at all. - */ - new TestCase(POINT_A, POINT_A_BIS, false, null), - new TestCase(POINT_A, POINT_B, false, null), - /* - * Overlapping requires the two geometries to have the same dimension, so a position - * never overlaps a surface and a curve never does either. - */ - new TestCase(POINT_CENTER, SQUARE, false, null), - new TestCase(LINE_BOTTOM, SQUARE, false, null), - /* - * Two curves overlap when they share a whole segment. Sharing a single position is not - * overlapping: what they have in common is then of a lower dimension than themselves. - */ - new TestCase(LINE_BOTTOM, LINE_COLLINEAR, true, null), - new TestCase(LINE_BOTTOM, LINE_CROSSING, false, null), - new TestCase(LINE_BOTTOM, LINE_DIAGONAL, false, null), - /* - * Two surfaces. Neither geometry may contain the other, so a surface overlaps neither - * a surface it contains nor a surface at its own position. - */ - new TestCase(SQUARE, SQUARE_OVERLAP, true, null), - new TestCase(SQUARE, SQUARE_INNER, false, null), - new TestCase(SQUARE, SQUARE_BIS, false, null), - new TestCase(SQUARE, SQUARE_TOUCHING, false, null), - new TestCase(SQUARE, SQUARE_DISJOINT, false, null), - /* - * Two sets of positions sharing one position, neither of them containing the other. - */ - new TestCase(MULTI_POINT_INSIDE, MULTI_POINT_SPREAD, true, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(OverlapsTest.class, "OverlapsTest.adoc"); + } /** - * Tests {@code overlaps(Geometry, Geometry)} on all declared test cases. + * Tests {@code overlaps(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testOverlaps() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().overlaps(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testOverlaps(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::overlaps); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.adoc new file mode 100644 index 0000000000..248d43d92c --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.adoc @@ -0,0 +1,146 @@ += Relate +:description: Test cases of GeometryProcessor.relate(Geometry, Geometry, DE9IM). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`relate` tells whether the DE-9IM matrix of `input` and `other` matches the `matrix` pattern. +The rows of the matrix are the interior, boundary and exterior of `input`, +and its columns those of `other`. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-empty + +The interior and boundary of the empty set meet nothing: two empty sets meet only by their exteriors. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +matrix:: `FFFFFFFF2` +result:: `true` + +== empty-point + +The exterior of the empty set meets the point and the exterior of the point. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +matrix:: `FFFFFF0F2` +result:: `true` + +== point-empty + +The point and its exterior meet the exterior of the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +matrix:: `FF0FFFFF2` +result:: `true` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points at the same position have their interiors in common and nothing else. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +matrix:: `0FFFFFFF2` +result:: `true` + +== point-other-position + +Two points at different positions each lie in the exterior of the other. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +matrix:: `FF0FFF0F2` +result:: `true` + +== point-other-position-equal-matrix + +Two points at different positions do not match the matrix of two equal points. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +matrix:: `0FFFFFFF2` +result:: `false` + +// ─── A position and a surface ────────────────────────────────────────────────── + +== point-inside-square + +The point is interior to the square, and its exterior meets the whole square. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `0FFFFF212` +result:: `true` + +== point-on-edge-square + +The point is on the boundary of the square, and its exterior meets the whole square. + +input:: `G_POINT [5 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `F0FFFF212` +result:: `true` + +== point-outside-square + +The point is in the exterior of the square, and its exterior meets the whole square. + +input:: `G_POINT [20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `FF0FFF212` +result:: `true` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-same-position + +Two squares at the same position: their interiors cover each other and their boundaries are the same curve. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `2FFF1FFF2` +result:: `true` + +== square-disjoint + +Two squares sharing nothing: each one lies in the exterior of the other. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +matrix:: `FF2FF1212` +result:: `true` + +== square-inner + +A square strictly inside another: its boundary meets the interior of the other, never its boundary. + +input:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `2FF1FF212` +result:: `true` + +== square-inner-within-pattern + +The wildcards of the pattern defining within accept any dimension. + +input:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `+T*F**F***+` +result:: `true` + +== square-overlap-within-pattern + +A square extending beyond the other does not match the pattern defining within. + +input:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +matrix:: `+T*F**F***+` +result:: `false` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.java index 47bd46a466..ca3ca192e8 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelateTest.java @@ -16,112 +16,46 @@ */ package org.apache.sis.geometries.operation; +import java.util.stream.Stream; import org.apache.sis.geometries.DE9IM; -import org.apache.sis.geometries.Geometry; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code relate} operation of {@link GeometryProcessor}. + * The test cases are declared in {@code RelateTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class RelateTest extends AbstractD9IMTest { +public class RelateTest { /** - * The inputs and expected result of a single test of {@code relate(Geometry, Geometry, DE9IM)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param matrix the dimensionally extended nine-intersection matrix. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - DE9IM matrix, - Boolean expected, - Class<? extends Exception> error) - { + public RelateTest() { } /** - * All test cases of {@code relate(Geometry, Geometry, DE9IM)}. + * Returns the test cases declared in {@code RelateTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * The interior and the boundary of the empty set meet nothing, while its exterior is the - * whole space. Two empty geometries therefore meet only by their exteriors. - */ - new TestCase(EMPTY_1, EMPTY_2, DE9IM.valueOf("FFFFFFFF2"), true, null), - /* - * Between the empty set and a point, the only non-empty intersections are those of the - * exterior of the empty set with the point, which is a position, and with the exterior - * of the point, which is the rest of the plane. The boundary of a point being empty, - * the boundary row and the boundary column stay empty in both cases. - */ - new TestCase(EMPTY_1, NON_EMPTY, DE9IM.valueOf("FFFFFF0F2"), true, null), - new TestCase(NON_EMPTY, EMPTY_1, DE9IM.valueOf("FF0FFFFF2"), true, null), - /* - * Two points at the same position have their interiors in common and nothing else. - * Two points at different positions have no position in common, each one lying in - * the exterior of the other. - */ - new TestCase(POINT_A, POINT_A_BIS, DE9IM.valueOf("0FFFFFFF2"), true, null), - new TestCase(POINT_A, POINT_B, DE9IM.valueOf("FF0FFF0F2"), true, null), - new TestCase(POINT_A, POINT_B, DE9IM.valueOf("0FFFFFFF2"), false, null), - /* - * A position and a surface. The three rows of the matrix describe the interior, the - * boundary and the exterior of the position, and its three columns those of the surface. - * The boundary of a position being empty, the second row is always empty. The exterior - * of a position meets the whole surface, hence the "212" of the last row in the three - * cases below. - * - * The position is interior to the surface, then on its boundary, then outside of it. - */ - new TestCase(POINT_CENTER, SQUARE, DE9IM.valueOf("0FFFFF212"), true, null), - new TestCase(POINT_ON_EDGE, SQUARE, DE9IM.valueOf("F0FFFF212"), true, null), - new TestCase(POINT_OUTSIDE, SQUARE, DE9IM.valueOf("FF0FFF212"), true, null), - /* - * Two surfaces at the same position: their interiors cover each other, their boundaries - * are the same curve, and neither meets the exterior of the other. - */ - new TestCase(SQUARE, SQUARE_BIS, DE9IM.valueOf("2FFF1FFF2"), true, null), - /* - * Two surfaces sharing nothing: each one lies entirely in the exterior of the other. - */ - new TestCase(SQUARE, SQUARE_DISJOINT, DE9IM.valueOf("FF2FF1212"), true, null), - /* - * A surface strictly inside another one: its boundary meets the interior of the other, - * never its boundary. - */ - new TestCase(SQUARE_INNER, SQUARE, DE9IM.valueOf("2FF1FF212"), true, null), - /* - * The same pair against the pattern which defines `within`, to verify that the wildcards - * of a pattern accept any dimension. - */ - new TestCase(SQUARE_INNER, SQUARE, DE9IM.valueOf("T*F**F***"), true, null), - new TestCase(SQUARE_OVERLAP, SQUARE, DE9IM.valueOf("T*F**F***"), false, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(RelateTest.class, "RelateTest.adoc"); + } /** - * Tests {@code relate(Geometry, Geometry, DE9IM)} on all declared test cases. + * Tests {@code relate(Geometry, Geometry, DE9IM)} on one test case. + * + * @param test the test case. */ - @Test - public void testRelate() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().relate(entry.input(), entry.other(), entry.matrix()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testRelate(final AsciiDocTestCase test) { + test.assertRelation((input, other) -> new GeometryProcessor().relate(input, other, + DE9IM.valueOf(test.value("matrix")))); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelationShapes.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelationShapes.adoc new file mode 100644 index 0000000000..8ce337c491 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/RelationShapes.adoc @@ -0,0 +1,52 @@ +// Shapes shared by the test cases of the spatial relationships, +// included by the AsciiDoc test files of this directory. + +The geometries have no coordinate reference system. +The two-dimensional shapes are laid out on the grid below, so that every expected result can be checked by hand. + +.... +y=30 ┌────────────┐ + │ square │ + │ disjoint │ +y=20 ├── line far ┤ ●────────────┘ +y=10 ┌────────────┬────────────┐ + │ square │ square │ + │ ● (5 5) │ touching │ +y=0 └────────────┴────────────┘ + x=0 x=10 x=20 x=30 + +line bottom: from (0 0) to (10 0), on the bottom edge of the square. +● (20 20): the bottom-left corner of the square disjoint. +.... + +[%header,cols="2,5,4"] +|=== +|Name |Well-Known Text 3 |Notes +|point A |`G_POINT [10 5]` |also the arbitrary non-empty geometry +|point B |`G_POINT [20 15]` | +|point center |`G_POINT [5 5]` |inside the square, in the hole of the square with hole +|point on edge |`G_POINT [5 0]` |on the bottom edge of the square, interior to line bottom +|point corner |`G_POINT [0 0]` |corner of the square, end point of line bottom +|point outside |`G_POINT [20 20]` |corner of the square disjoint +|line bottom |`G_LINESTRING [0 0, 10 0]` |on the bottom edge of the square +|line diagonal |`G_LINESTRING [0 0, 10 10]` |shares only an end point with line bottom +|line crossing |`G_LINESTRING [5 -5, 5 15]` |passes through the square +|line inside |`G_LINESTRING [2 5, 8 5]` |strictly inside the square +|line collinear |`G_LINESTRING [5 0, 15 0]` |overlaps half of line bottom +|line far |`G_LINESTRING [0 20, 10 20]` |at a distance of 10 above the square +|square |`G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` | +|square with hole |`G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0], [4 4, 6 4, 6 6, 4 6, 4 4])` |a hole of 2 × 2 at the center +|square inner |`G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` |strictly inside the square +|square overlap |`G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` |covers a quarter of the square +|square touching |`G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` |shares an edge with the square +|square disjoint |`G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` | +|points inside |`G_COLLECTION POINT [2 2, 5 5]` |both inside the square +|points spread |`G_COLLECTION POINT [5 5, 20 20]` |one inside the square, one outside +|lines |`G_COLLECTION LINESTRING (...)` |line bottom and line far +|polygons |`G_COLLECTION POLYGON (...)` |the square and the square disjoint +|collection |`G_COLLECTION (G_POINT [5 5], G_LINESTRING [0 0, 10 0])` |point center and line bottom +|triangle 3D |`G_TRIANGLE <P:3, TEST:1> (...)` |corners (0 0 5), (4 0 5), (0 4 5), with attribute values +|ring 3D |`G_LINESTRING <P:3, TEST:1> (...)` |closed square of side 4 in the plane z = 1 +|plane z5 |`G_HYPERPLANE <P:3 ; NORMAL:3> [0 0 5] [0 0 1]` |the plane z = 5 +|box 3D |`G_BBOX <P:3> [0 0 0, 2 2 2]` | +|=== diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.adoc new file mode 100644 index 0000000000..26c74d92d2 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.adoc @@ -0,0 +1,177 @@ += Touches +:description: Test cases of GeometryProcessor.touches(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` touches `other` when they share at least one position, and all their shared positions +are on the boundary of at least one of them: their interiors do not meet. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set has no boundary, so it touches nothing. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `false` + +== point-empty + +Nothing touches the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +== empty-empty + +Two empty sets do not touch. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +Two points cannot touch: the boundary of a point is empty, so a shared position is interior to both. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `false` + +== point-other-position + +Two points at different positions share no position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +// ─── A position and a surface ────────────────────────────────────────────────── + +== point-on-edge-square + +A point on the boundary of the square touches it. + +input:: `G_POINT [5 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== point-inside-square + +A point inside the square is in its interior, so it does not touch it. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== point-outside-square + +A point outside the square shares no position with it. + +input:: `G_POINT [20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── A position and a curve ──────────────────────────────────────────────────── + +== point-line-end + +An end point of the line touches it. + +input:: `G_POINT [0 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `true` + +== point-on-line + +A point interior to the line does not touch it. + +input:: `G_POINT [5 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` + +// ─── Two curves ──────────────────────────────────────────────────────────────── + +== line-shared-end + +Two lines sharing only an end point touch. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [0 0, 10 10]` +result:: `true` + +== line-crossing + +Two lines whose interiors meet at one position do not touch. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 -5, 5 15]` +result:: `false` + +== line-collinear + +Two lines whose interiors share a whole segment do not touch. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_LINESTRING [5 0, 15 0]` +result:: `false` + +// ─── A curve and a surface ───────────────────────────────────────────────────── + +== line-on-edge + +A line lying on the edge of the square touches it: none of its positions is interior to the square. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== line-inside-square + +A line inside the square does not touch it. + +input:: `G_LINESTRING [2 5, 8 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-touching + +Two squares sharing a whole edge but no interior position touch. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([10 0, 20 0, 20 10, 10 10, 10 0])` +result:: `true` + +== square-overlap + +Two overlapping squares share interior positions, so they do not touch. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +result:: `false` + +== square-inner + +A square does not touch a square strictly inside it. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +result:: `false` + +== square-disjoint + +Two squares far from each other do not touch. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20])` +result:: `false` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.java index 11282f238f..4ef534d2a9 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TouchesTest.java @@ -16,102 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code touches} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code TouchesTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class TouchesTest extends AbstractD9IMTest { +public class TouchesTest { /** - * The inputs and expected result of a single test of {@code touches(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public TouchesTest() { } /** - * All test cases of {@code touches(Geometry, Geometry)}. + * Returns the test cases declared in {@code TouchesTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - /* - * Touching requires a shared position which is in the boundary of at least one of the - * two geometries. The empty set has no boundary, therefore it touches nothing. - */ - new TestCase(EMPTY_1, NON_EMPTY, false, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_1, false, null), - new TestCase(EMPTY_1, EMPTY_2, false, null), - /* - * Two points cannot touch: the boundary of a point is empty, therefore the only - * position they may share is interior to both of them. - */ - new TestCase(POINT_A, POINT_A_BIS, false, null), - new TestCase(POINT_A, POINT_B, false, null), - /* - * A position and a surface. Touching requires a shared position which is interior to - * neither geometry, which for a position means that it lies on the boundary. - */ - new TestCase(POINT_ON_EDGE, SQUARE, true, null), - new TestCase(POINT_CENTER, SQUARE, false, null), - new TestCase(POINT_OUTSIDE, SQUARE, false, null), - /* - * A position and a curve, the position being an end of the curve or interior to it. - */ - new TestCase(POINT_CORNER, LINE_BOTTOM, true, null), - new TestCase(POINT_ON_EDGE, LINE_BOTTOM, false, null), - /* - * Two curves. They touch when they share only an end position, and do not when their - * interiors meet, whether they meet at one position or along a whole segment. - */ - new TestCase(LINE_BOTTOM, LINE_DIAGONAL, true, null), - new TestCase(LINE_BOTTOM, LINE_CROSSING, false, null), - new TestCase(LINE_BOTTOM, LINE_COLLINEAR, false, null), - /* - * A curve and a surface. A curve lying on the boundary of a surface touches it, - * no position of it being interior to the surface. - */ - new TestCase(LINE_BOTTOM, SQUARE, true, null), - new TestCase(LINE_INSIDE, SQUARE, false, null), - /* - * Two surfaces sharing a whole edge but no interior position. - */ - new TestCase(SQUARE, SQUARE_TOUCHING, true, null), - new TestCase(SQUARE, SQUARE_OVERLAP, false, null), - new TestCase(SQUARE, SQUARE_INNER, false, null), - new TestCase(SQUARE, SQUARE_DISJOINT, false, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(TouchesTest.class, "TouchesTest.adoc"); + } /** - * Tests {@code touches(Geometry, Geometry)} on all declared test cases. + * Tests {@code touches(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testTouches() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().touches(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testTouches(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::touches); } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.adoc b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.adoc new file mode 100644 index 0000000000..d0cd649fbc --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.adoc @@ -0,0 +1,176 @@ += Within +:description: Test cases of GeometryProcessor.within(Geometry, Geometry). + +Each level-2 section is one test. Its title is the test id, the paragraph below it +describes the case, and the labeled list gives the operands in Well-Known Text 3 +and the expected result or error. + +`input` is within `other` when `other` contains `input`: this is the converse of `contains`. + +include::RelationShapes.adoc[] + +// ─── The empty set ───────────────────────────────────────────────────────────── + +== empty-point + +The empty set is within every geometry. + +input:: `G_GEOMETRY EMPTY` +other:: `G_POINT [10 5]` +result:: `true` + +== empty-empty + +The empty set is within itself. + +input:: `G_GEOMETRY EMPTY` +other:: `G_GEOMETRY EMPTY` +result:: `true` + +== point-empty + +A point is not within the empty set. + +input:: `G_POINT [10 5]` +other:: `G_GEOMETRY EMPTY` +result:: `false` + +// ─── Points ──────────────────────────────────────────────────────────────────── + +== point-same-position + +A point is within another point at the same position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [10 5]` +result:: `true` + +== point-other-position + +A point is not within a point at another position. + +input:: `G_POINT [10 5]` +other:: `G_POINT [20 15]` +result:: `false` + +== point-other-position-reversed + +Neither point is within the other, whatever the operand order. + +input:: `G_POINT [20 15]` +other:: `G_POINT [10 5]` +result:: `false` + +// ─── A position and a surface ────────────────────────────────────────────────── + +== point-inside-square + +A point inside the square is within it. + +input:: `G_POINT [5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== point-on-edge-square + +A point on the boundary of the square is not within it: it shares no interior position. + +input:: `G_POINT [5 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== point-outside-square + +A point outside the square is not within it. + +input:: `G_POINT [20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── A position and a curve ──────────────────────────────────────────────────── + +== point-on-line + +A point interior to the line is within it. + +input:: `G_POINT [5 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `true` + +== point-line-end + +An end point of the line is on its boundary, so it is not within it. + +input:: `G_POINT [0 0]` +other:: `G_LINESTRING [0 0, 10 0]` +result:: `false` + +// ─── A curve and a surface ───────────────────────────────────────────────────── + +== line-inside-square + +A line strictly inside the square is within it. + +input:: `G_LINESTRING [2 5, 8 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== line-on-edge + +A line lying on the edge of the square is not within it. + +input:: `G_LINESTRING [0 0, 10 0]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +// ─── Two surfaces ────────────────────────────────────────────────────────────── + +== square-inner + +A square strictly inside the square is within it. + +input:: `G_POLYGON ([2 2, 4 2, 4 4, 2 4, 2 2])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== square-overlap + +A square extending beyond the square is not within it. + +input:: `G_POLYGON ([5 5, 15 5, 15 15, 5 15, 5 5])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== square-same-position + +Two squares at the same position are within each other. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +// ─── Sets of geometries ──────────────────────────────────────────────────────── + +== points-inside + +A set of points inside the square is within it. + +input:: `G_COLLECTION POINT [2 2, 5 5]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `true` + +== points-spread + +A set with one point outside the square is not within it. + +input:: `G_COLLECTION POINT [5 5, 20 20]` +other:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +result:: `false` + +== square-in-polygons + +The square is within a set of polygons holding an equal polygon. + +input:: `G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0])` +other:: `G_COLLECTION POLYGON (G_POLYGON ([0 0, 10 0, 10 10, 0 10, 0 0]), G_POLYGON ([20 20, 30 20, 30 30, 20 30, 20 20]))` +result:: `true` diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.java index 27caec4c0f..83b4d511b3 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/WithinTest.java @@ -16,94 +16,44 @@ */ package org.apache.sis.geometries.operation; -import org.apache.sis.geometries.Geometry; +import java.util.stream.Stream; // Test dependencies -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests the {@code within} operations of {@link GeometryProcessor}. + * The test cases are declared in {@code WithinTest.adoc}. * * @author Johann Sorel (Geomatys) */ -public class WithinTest extends AbstractD9IMTest { +public class WithinTest { /** - * The inputs and expected result of a single test of {@code within(Geometry, Geometry)}. - * - * @param input the geometry on which the operation is invoked. - * @param other the other operand. - * @param expected the expected result, or {@code null} if an exception is expected. - * @param error the type of the expected exception, or {@code null} if the operation should succeed. + * Creates a new test case. */ - private record TestCase(Geometry input, - Geometry other, - Boolean expected, - Class<? extends Exception> error) - { + public WithinTest() { } /** - * All test cases of {@code within(Geometry, Geometry)}. + * Returns the test cases declared in {@code WithinTest.adoc}. + * This method is public because JUnit cannot open the package of a named module. + * + * @return the test cases. */ - private static final TestCase[] ENTRIES = { - // The empty set is a subset of every geometry. Converse of `contains(Geometry, Geometry)`. - new TestCase(EMPTY_1, NON_EMPTY, true, null), - new TestCase(EMPTY_1, EMPTY_1, true, null), - new TestCase(EMPTY_1, EMPTY_2, true, null), - new TestCase(NON_EMPTY, EMPTY_1, false, null), - /* - * A point is a subset of another point only when the two are at the same position. - */ - new TestCase(POINT_A, POINT_A_BIS, true, null), - new TestCase(POINT_A, POINT_B, false, null), - new TestCase(POINT_B, POINT_A, false, null), - /* - * A position and a surface. A position on the boundary of a surface is not within it: - * being within requires a position interior to both geometries, and the interior of a - * position is the position itself. - */ - new TestCase(POINT_CENTER, SQUARE, true, null), - new TestCase(POINT_ON_EDGE, SQUARE, false, null), - new TestCase(POINT_OUTSIDE, SQUARE, false, null), - /* - * A position and a curve, the position being interior to the curve or one of its ends. - */ - new TestCase(POINT_ON_EDGE, LINE_BOTTOM, true, null), - new TestCase(POINT_CORNER, LINE_BOTTOM, false, null), - /* - * A curve and a surface. A curve lying on the boundary of a surface is not within it. - */ - new TestCase(LINE_INSIDE, SQUARE, true, null), - new TestCase(LINE_BOTTOM, SQUARE, false, null), - /* - * Two surfaces, and sets of geometries. - */ - new TestCase(SQUARE_INNER, SQUARE, true, null), - new TestCase(SQUARE_OVERLAP, SQUARE, false, null), - new TestCase(SQUARE, SQUARE_BIS, true, null), - new TestCase(MULTI_POINT_INSIDE, SQUARE, true, null), - new TestCase(MULTI_POINT_SPREAD, SQUARE, false, null), - new TestCase(SQUARE, MULTI_POLYGON, true, null) - }; + public static Stream<AsciiDocTestCase> cases() { + return AsciiDocTestCase.read(WithinTest.class, "WithinTest.adoc"); + } /** - * Tests {@code within(Geometry, Geometry)} on all declared test cases. + * Tests {@code within(Geometry, Geometry)} on one test case. + * + * @param test the test case. */ - @Test - public void testWithin() { - for (final TestCase entry : ENTRIES) { - try { - final boolean result = new GeometryProcessor().within(entry.input(), entry.other()); - assertNull(entry.error(), "An exception was expected."); - assertEquals(entry.expected(), result); - } catch (Exception ex) { - if (entry.error() == null || !entry.error().isInstance(ex)) { - throw new AssertionError("Unexpected exception for " + entry, ex); - } - } - } + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + public void testWithin(final AsciiDocTestCase test) { + test.assertRelation(new GeometryProcessor()::within); } }
