From c7a3ed982ec7c6af31d1db1ec2cf9738db94d636 Mon Sep 17 00:00:00 2001 From: Algent Date: Sat, 5 Sep 2026 10:32:46 +0200 Subject: [PATCH] test: characterize box occlusion inputs --- .../multipart/NormalOcclusionBoxTest.java | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/test/java/codechicken/multipart/NormalOcclusionBoxTest.java diff --git a/src/test/java/codechicken/multipart/NormalOcclusionBoxTest.java b/src/test/java/codechicken/multipart/NormalOcclusionBoxTest.java new file mode 100644 index 0000000..52d12a1 --- /dev/null +++ b/src/test/java/codechicken/multipart/NormalOcclusionBoxTest.java @@ -0,0 +1,154 @@ +package codechicken.multipart; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.function.BiFunction; + +import org.junit.jupiter.api.Test; + +import codechicken.lib.vec.Cuboid6; +import scala.collection.JavaConversions; +import scala.collection.Traversable; + +class NormalOcclusionBoxTest { + + private static final List, Iterable, Boolean>> LEGACY = Arrays.asList( + (left, right) -> NormalOcclusionTest.apply(scala(left), scala(right)), + (left, right) -> NormalOcclusionTest$.MODULE$.apply(scala(left), scala(right))); + + @Test + void legacyEntriesSnapshotBothInputsBeforeOrderedShortCircuiting() { + LEGACY.forEach(NormalOcclusionBoxTest::assertSnapshotAndOrder); + } + + @Test + void legacyEntriesPropagateInputFailuresBeforeTestingGeometry() { + LEGACY.forEach(NormalOcclusionBoxTest::assertInputFailures); + } + + @Test + void legacyEntriesKeepLazyNullBoxesAndOriginalGeometryFailures() { + LEGACY.forEach(NormalOcclusionBoxTest::assertNullsAndGeometryFailures); + } + + private static void assertSnapshotAndOrder(BiFunction, Iterable, Boolean> query) { + List calls = new ArrayList<>(); + List left = new ArrayList<>(); + List right = new ArrayList<>(); + Cuboid6 first = new Cuboid6(0, 0, 0, 1, 1, 1) { + + @Override + public boolean intersects(Cuboid6 other) { + calls.add("first:" + other.min.x); + left.clear(); + right.clear(); + return false; + } + }; + Cuboid6 last = new Cuboid6(0, 0, 0, 1, 1, 1) { + + @Override + public boolean intersects(Cuboid6 other) { + calls.add("last:" + other.min.x); + return true; + } + }; + left.addAll(Arrays.asList(first, first, last)); + right.addAll(Arrays.asList(new Cuboid6(1, 0, 0, 2, 1, 1), new Cuboid6(2, 0, 0, 3, 1, 1))); + assertFalse(query.apply(observed("left", left, calls), observed("right", right, calls))); + assertEquals( + Arrays.asList("left", "right", "first:1.0", "first:2.0", "first:1.0", "first:2.0", "last:1.0"), + calls); + assertTrue(left.isEmpty()); + assertTrue(right.isEmpty()); + } + + private static void assertInputFailures(BiFunction, Iterable, Boolean> query) { + List calls = new ArrayList<>(); + IllegalStateException failure = new IllegalStateException("input failed after first box"); + Cuboid6 full = new Cuboid6(0, 0, 0, 1, 1, 1); + Iterable failing = () -> new Iterator() { + + private boolean first = true; + + @Override + public boolean hasNext() { + return true; + } + + @Override + public Cuboid6 next() { + if (!first) { + throw failure; + } + first = false; + return full; + } + }; + assertSame( + failure, + assertThrows( + IllegalStateException.class, + () -> query.apply(failing, observed("right", Collections.singletonList(full), calls)))); + assertTrue(calls.isEmpty(), "A left collection failure must not start the right input"); + assertSame( + failure, + assertThrows(IllegalStateException.class, () -> query.apply(Collections.singletonList(full), failing)), + "Collection finishes before the first overlapping pair"); + assertSame( + failure, + assertThrows(IllegalStateException.class, () -> query.apply(Collections.emptyList(), failing)), + "Even an empty left side consumes the right input"); + } + + private static void assertNullsAndGeometryFailures( + BiFunction, Iterable, Boolean> query) { + List empty = Collections.emptyList(); + List nullBox = Collections.singletonList(null); + List full = Collections.singletonList(new Cuboid6(0, 0, 0, 1, 1, 1)); + assertThrows(NullPointerException.class, () -> query.apply(null, empty)); + assertThrows(NullPointerException.class, () -> query.apply(empty, null)); + assertTrue(query.apply(empty, nullBox)); + assertTrue(query.apply(nullBox, empty)); + assertThrows(NullPointerException.class, () -> query.apply(nullBox, full)); + assertThrows(NullPointerException.class, () -> query.apply(full, nullBox)); + assertFalse(query.apply(full, Arrays.asList(full.get(0), null))); + IllegalArgumentException failure = new IllegalArgumentException("intersects failed"); + Cuboid6 broken = new Cuboid6(0, 0, 0, 1, 1, 1) { + + @Override + public boolean intersects(Cuboid6 other) { + throw failure; + } + }; + assertSame( + failure, + assertThrows( + IllegalArgumentException.class, + () -> query.apply(Collections.singletonList(broken), full))); + } + + private static Iterable observed(String name, List boxes, List calls) { + return () -> { + assertFalse(calls.contains(name), "Input iterator must be requested only once"); + calls.add(name); + return boxes.iterator(); + }; + } + + private static Traversable scala(Iterable boxes) { + if (boxes == null) { + return null; + } + return JavaConversions.iterableAsScalaIterable(boxes); + } +}