-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
608 additions
and
79 deletions.
There are no files selected for viewing
13 changes: 0 additions & 13 deletions
13
spatial-neoforge/src/test/java/dev/compactmods/spatial/test/TestUtils.java
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
spatial-neoforge/src/test/java/dev/compactmods/spatial/test/junit/AABBAlignerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package dev.compactmods.spatial.test.junit; | ||
|
||
import dev.compactmods.spatial.aabb.AABBAligner; | ||
import dev.compactmods.spatial.aabb.AABBHelper; | ||
import dev.compactmods.spatial.random.RandomSourceExtras; | ||
import dev.compactmods.spatial.test.util.MCAssertions; | ||
import dev.compactmods.spatial.test.util.TestUtils; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.joml.Vector3d; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AABBAlignerTests { | ||
|
||
@Test | ||
public void canFloorToY0() { | ||
// Source minY = 5 | ||
AABB before = AABB.ofSize(new Vec3(0, 7.5, 0), 5, 5, 5); | ||
|
||
// Align to Y-0 | ||
final var after = AABBAligner.floor(before, 0); | ||
|
||
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); | ||
Assertions.assertEquals(0, after.minY, "After y level should be zero. (was: %s)".formatted(after.minY)); | ||
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); | ||
} | ||
|
||
@Test | ||
public void canFloorToAnotherAABB() { | ||
// Source minY = 5 | ||
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5); | ||
|
||
// Target minY = 1 (bounds are Y 1-11) | ||
AABB bounds = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 6), 10, 10, 10); | ||
|
||
// Align to Y-0 | ||
final var after = AABBAligner.floor(before, bounds); | ||
|
||
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved."); | ||
Assertions.assertEquals(1, after.minY, "After y level should be 1. (was: %s)".formatted(after.minY)); | ||
|
||
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same."); | ||
} | ||
|
||
@Test | ||
public void canNormalize() { | ||
final var random = RandomSource.create(); | ||
|
||
// Target boundaries, placed from {0,0,0} - {10,10,10} | ||
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10); | ||
|
||
// Randomly placed boundaries of size {5,5,5} | ||
final var randomBounds = AABB.ofSize(RandomSourceExtras.randomVec3(random), 5, 5, 5); | ||
|
||
final var aligned = AABBAligner.create(bounds, randomBounds) | ||
.normalize() | ||
.align(); | ||
|
||
final var EXPECTED_CENTER = new Vec3(-2.5, -2.5, -2.5); | ||
final var EXPECTED_CORNER = AABBHelper.minCorner(bounds); | ||
final var EXPECTED_SIZE = new Vector3d(5, 5, 5); | ||
|
||
MCAssertions.assertVec3Equals(aligned.getCenter(), EXPECTED_CENTER); | ||
MCAssertions.assertVec3Equals(AABBHelper.minCorner(aligned), EXPECTED_CORNER); | ||
MCAssertions.assertVec3Equals(AABBHelper.sizeOf(aligned), EXPECTED_SIZE); | ||
} | ||
|
||
@Test | ||
public void canAlignOnBlockPosition() { | ||
final var random = RandomSource.create(); | ||
|
||
AABB randomAABB = TestUtils.randomAABB(random); | ||
final var originalSize = AABBHelper.sizeOf(randomAABB); | ||
|
||
// Center on 1, 2, 3 | ||
final var aligned = AABBAligner.create(randomAABB) | ||
.center(new BlockPos(1, 2, 3)) | ||
.align(); | ||
|
||
// Assert center position | ||
MCAssertions.assertVec3Equals(new Vec3(1.5, 2.5, 3.5), aligned.getCenter()); | ||
|
||
// Assert size did not change | ||
MCAssertions.assertVec3Equals(originalSize, AABBHelper.sizeOf(aligned)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
spatial-neoforge/src/test/java/dev/compactmods/spatial/test/junit/DirectionalMathTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package dev.compactmods.spatial.test.junit; | ||
|
||
import dev.compactmods.spatial.aabb.AABBHelper; | ||
import dev.compactmods.spatial.direction.DirectionalMath; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.joml.Vector3dc; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DirectionalMathTests { | ||
|
||
@Test | ||
public void canAlignUp() { | ||
AABB boundaries = AABBHelper.zeroOriginSized(10); | ||
|
||
Vec3 centerCenter = boundaries.getCenter(); | ||
Vector3dc topCenter = DirectionalMath.directionalEdge(Direction.UP, boundaries); | ||
|
||
Assertions.assertEquals(centerCenter.x, topCenter.x()); | ||
Assertions.assertEquals(boundaries.maxY, topCenter.y()); | ||
Assertions.assertEquals(centerCenter.z, topCenter.z()); | ||
} | ||
|
||
@Test | ||
public void canAlignDown() { | ||
AABB boundaries = AABBHelper.zeroOriginSized(10); | ||
|
||
Vec3 centerCenter = boundaries.getCenter(); | ||
Vector3dc bottomCenter = DirectionalMath.directionalEdge(Direction.DOWN, boundaries); | ||
|
||
Assertions.assertEquals(centerCenter.x, bottomCenter.x()); | ||
Assertions.assertEquals(boundaries.minY, bottomCenter.y()); | ||
Assertions.assertEquals(centerCenter.z, bottomCenter.z()); | ||
} | ||
|
||
@Test | ||
public void canAlignNorth() { | ||
AABB boundaries = AABBHelper.zeroOriginSized(10); | ||
|
||
Vec3 centerCenter = boundaries.getCenter(); | ||
Vector3dc northCenter = DirectionalMath.directionalEdge(Direction.NORTH, boundaries); | ||
|
||
Assertions.assertEquals(0, northCenter.x()); | ||
Assertions.assertEquals(centerCenter.y, northCenter.y()); | ||
Assertions.assertEquals(-5, northCenter.z()); | ||
} | ||
|
||
@Test | ||
public void canAlignWest() { | ||
AABB boundaries = AABBHelper.zeroOriginSized(10); | ||
|
||
Vec3 centerCenter = boundaries.getCenter(); | ||
Vector3dc westCenter = DirectionalMath.directionalEdge(Direction.WEST, boundaries); | ||
|
||
Assertions.assertEquals(-5, westCenter.x()); | ||
Assertions.assertEquals(centerCenter.y, westCenter.y()); | ||
Assertions.assertEquals(0, westCenter.z()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...al-neoforge/src/test/java/dev/compactmods/spatial/test/junit/RandomSourceExtrasTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dev.compactmods.spatial.test.junit; | ||
|
||
import dev.compactmods.spatial.random.RandomSourceExtras; | ||
import dev.compactmods.spatial.test.util.MCAssertions; | ||
import dev.compactmods.spatial.vector.VectorUtils; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RandomSourceExtrasTests { | ||
|
||
@Test | ||
public void canGenerateRandomDoubles() { | ||
RandomSource random = RandomSource.create(); | ||
|
||
Assertions.assertDoesNotThrow(() -> { | ||
RandomSourceExtras.randomDouble(random); | ||
}); | ||
|
||
double zeroAndOne = RandomSourceExtras.randomDouble(random, 0, 1); | ||
double negative = RandomSourceExtras.randomDouble(random, -10, -1); | ||
double positive = RandomSourceExtras.randomDouble(random, 1, 10); | ||
|
||
Assertions.assertTrue(zeroAndOne > 0); | ||
Assertions.assertTrue(negative < 0); | ||
Assertions.assertTrue(positive > 0); | ||
} | ||
|
||
@Test | ||
public void canGenerateRandomVecWithinBounds() { | ||
RandomSource random = RandomSource.create(); | ||
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10); | ||
|
||
for(int i = 0; i < 25; i++) { | ||
var randomV3 = RandomSourceExtras.randomVec3(random, bounds); | ||
|
||
Assertions.assertTrue(bounds.contains(randomV3), "Random generated outside of boundaries: " + randomV3.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void canStreamRandomVectorsInBoundaries() { | ||
RandomSource random = RandomSource.create(); | ||
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10); | ||
|
||
RandomSourceExtras.randomVec3Stream(random, bounds) | ||
.limit(10) | ||
.forEach(pos -> Assertions.assertTrue(bounds.contains(pos))); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
spatial-neoforge/src/test/java/dev/compactmods/spatial/test/junit/VectorUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.compactmods.spatial.test.junit; | ||
|
||
import dev.compactmods.spatial.test.util.MCAssertions; | ||
import dev.compactmods.spatial.vector.VectorUtils; | ||
import net.minecraft.core.Vec3i; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.joml.Vector3d; | ||
import org.joml.Vector3dc; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class VectorUtilsTests { | ||
|
||
@Test | ||
public void testConvertVec3i() { | ||
Vec3i pos = new Vec3i(1, 2, 3); | ||
Vector3d converted = VectorUtils.convert3d(pos); | ||
|
||
MCAssertions.assertVec3Equals(new Vector3d(1, 2, 3), converted); | ||
} | ||
|
||
@Test | ||
public void testConvertVector3dc() { | ||
Vector3dc vec = new Vector3d(2.5, 2.5, 2.5); | ||
Vec3 converted = VectorUtils.convert3d(vec); | ||
|
||
MCAssertions.assertVec3Equals(new Vec3(2.5, 2.5, 2.5), converted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
spatial-neoforge/src/test/java/dev/compactmods/spatial/test/util/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.compactmods.spatial.test.util; | ||
|
||
import dev.compactmods.spatial.random.RandomSourceExtras; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
public class TestUtils { | ||
|
||
public static AABB randomAABB(RandomSource random) { | ||
final var position = RandomSourceExtras.randomVec3(random, AABB.ofSize(Vec3.ZERO, 50, 50, 50)); | ||
final var size = RandomSourceExtras.randomVec3(random); | ||
|
||
return AABB.ofSize(position, size.x, size.y, size.z); | ||
} | ||
} |
Oops, something went wrong.