Skip to content

Commit

Permalink
Write SO many tests
Browse files Browse the repository at this point in the history
[no ci]
  • Loading branch information
robotgryphon committed Nov 17, 2024
1 parent 15add32 commit 956f1eb
Show file tree
Hide file tree
Showing 13 changed files with 608 additions and 79 deletions.

This file was deleted.

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));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dev.compactmods.spatial.test.junit;

import dev.compactmods.spatial.aabb.AABBAligner;
import dev.compactmods.spatial.aabb.AABBHelper;
import dev.compactmods.spatial.test.TestUtils;
import dev.compactmods.spatial.test.junit.util.MCAssertions;
import dev.compactmods.spatial.random.RandomSourceExtras;
import dev.compactmods.spatial.test.util.MCAssertions;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.phys.AABB;
Expand All @@ -12,47 +13,16 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import java.util.Collection;
import java.util.stream.Stream;

public class AABBHelperTests {

@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 = AABBHelper.alignFloor(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 = AABBHelper.alignFloor(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 normalizeToZero() {
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);

// Align to Y-0
final var after = AABBHelper.normalize(before);
final var after = AABBAligner.normalize(before);

Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");

Expand All @@ -64,11 +34,11 @@ public void normalizeToZero() {
}

@TestFactory
public static Stream<DynamicTest> normalizeBoundaryTests() {
public Stream<DynamicTest> normalizeBoundaryTests() {
final var random = RandomSource.create();

return Stream.concat(
TestUtils.randomVec3Stream(random).limit(10),
RandomSourceExtras.randomVec3Stream(random).limit(10),

// Ensure at least one negative and one positive bound are part of the test
Stream.of(
Expand All @@ -85,12 +55,12 @@ private static void normalizeIntoBoundaries(Vec3 randomOffset) {
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
AABB bounds = AABB.ofSize(randomOffset, 5, 5, 5);

final var after = AABBHelper.normalizeWithin(before, bounds);
final var after = AABBAligner.create(bounds, before)
.normalize()
.align();

Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");

MCAssertions.assertVec3Equals(AABBHelper.minCorner(after), AABBHelper.minCorner(bounds));

MCAssertions.assertVec3Equals(AABBHelper.minCorner(bounds), AABBHelper.minCorner(after));
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
}
}
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());
}
}
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)));
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package dev.compactmods.spatial.test.junit.util;
package dev.compactmods.spatial.test.util;

import com.google.common.math.DoubleMath;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3dc;
import org.junit.jupiter.api.Assertions;

public class MCAssertions {
public static void assertVec3Equals(Vec3 actual, Vec3 expected) {
public static void assertVec3Equals(Vec3 expected, Vec3 actual) {
if(!DoubleMath.fuzzyEquals(actual.x, expected.x, 0.001))
Assertions.fail("X did not match expected value (was: %s; expected: %s)".formatted(actual.x, expected.x));

Expand All @@ -15,4 +16,9 @@ public static void assertVec3Equals(Vec3 actual, Vec3 expected) {
if(!DoubleMath.fuzzyEquals(actual.z, expected.z, 0.001))
Assertions.fail("Z did not match expected value (was: %s; expected: %s)".formatted(actual.z, expected.z));
}

public static void assertVec3Equals(Vector3dc expected, Vector3dc actual) {
if(!expected.equals(actual, 0.001))
Assertions.fail("Actual did not match expected value (was: %s; expected: %s)".formatted(actual, expected));
}
}
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);
}
}
Loading

0 comments on commit 956f1eb

Please sign in to comment.