|
| 1 | +package dev.compactmods.spatial.test.junit; |
| 2 | + |
| 3 | +import dev.compactmods.spatial.aabb.AABBHelper; |
| 4 | +import dev.compactmods.spatial.test.TestUtils; |
| 5 | +import dev.compactmods.spatial.test.junit.util.MCAssertions; |
| 6 | +import net.minecraft.core.Direction; |
| 7 | +import net.minecraft.util.RandomSource; |
| 8 | +import net.minecraft.world.phys.AABB; |
| 9 | +import net.minecraft.world.phys.Vec3; |
| 10 | +import org.junit.jupiter.api.Assertions; |
| 11 | +import org.junit.jupiter.api.DynamicTest; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.TestFactory; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +public class AABBHelperTests { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void canFloorToY0() { |
| 22 | + // Source minY = 5 |
| 23 | + AABB before = AABB.ofSize(new Vec3(0, 7.5, 0), 5, 5, 5); |
| 24 | + |
| 25 | + // Align to Y-0 |
| 26 | + final var after = AABBHelper.alignFloor(before, 0); |
| 27 | + |
| 28 | + Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); |
| 29 | + Assertions.assertEquals(0, after.minY, "After y level should be zero. (was: %s)".formatted(after.minY)); |
| 30 | + Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void canFloorToAnotherAABB() { |
| 35 | + // Source minY = 5 |
| 36 | + AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5); |
| 37 | + |
| 38 | + // Target minY = 1 (bounds are Y 1-11) |
| 39 | + AABB bounds = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 6), 10, 10, 10); |
| 40 | + |
| 41 | + // Align to Y-0 |
| 42 | + final var after = AABBHelper.alignFloor(before, bounds); |
| 43 | + |
| 44 | + Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); |
| 45 | + Assertions.assertEquals(1, after.minY, "After y level should be 1. (was: %s)".formatted(after.minY)); |
| 46 | + |
| 47 | + Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void normalizeToZero() { |
| 52 | + AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5); |
| 53 | + |
| 54 | + // Align to Y-0 |
| 55 | + final var after = AABBHelper.normalize(before); |
| 56 | + |
| 57 | + Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); |
| 58 | + |
| 59 | + Assertions.assertEquals(0, after.minX, "After x level was not zero (was: %s)".formatted(after.minX)); |
| 60 | + Assertions.assertEquals(0, after.minY, "After y level was not zero (was: %s)".formatted(after.minY)); |
| 61 | + Assertions.assertEquals(0, after.minZ, "After z level was not zero (was: %s)".formatted(after.minZ)); |
| 62 | + |
| 63 | + Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); |
| 64 | + } |
| 65 | + |
| 66 | + @TestFactory |
| 67 | + public static Stream<DynamicTest> normalizeBoundaryTests() { |
| 68 | + final var random = RandomSource.create(); |
| 69 | + |
| 70 | + return Stream.concat( |
| 71 | + TestUtils.randomVec3Stream(random).limit(10), |
| 72 | + |
| 73 | + // Ensure at least one negative and one positive bound are part of the test |
| 74 | + Stream.of( |
| 75 | + Vec3.ZERO.subtract(-3, -2, 5), |
| 76 | + Vec3.ZERO.add(2, 5, 1) |
| 77 | + ) |
| 78 | + ).map(randomOffset -> DynamicTest.dynamicTest( |
| 79 | + "normalize_boundaries_%s".formatted(randomOffset.toString()), |
| 80 | + () -> normalizeIntoBoundaries(randomOffset) |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + private static void normalizeIntoBoundaries(Vec3 randomOffset) { |
| 85 | + AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5); |
| 86 | + AABB bounds = AABB.ofSize(randomOffset, 5, 5, 5); |
| 87 | + |
| 88 | + final var after = AABBHelper.normalizeWithin(before, bounds); |
| 89 | + |
| 90 | + Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); |
| 91 | + |
| 92 | + MCAssertions.assertVec3Equals(AABBHelper.minCorner(after), AABBHelper.minCorner(bounds)); |
| 93 | + |
| 94 | + Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); |
| 95 | + } |
| 96 | +} |
0 commit comments