Skip to content

Commit 956f1eb

Browse files
committed
Write SO many tests
[no ci]
1 parent 15add32 commit 956f1eb

File tree

13 files changed

+608
-79
lines changed

13 files changed

+608
-79
lines changed

spatial-neoforge/src/test/java/dev/compactmods/spatial/test/TestUtils.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package dev.compactmods.spatial.test.junit;
2+
3+
import dev.compactmods.spatial.aabb.AABBAligner;
4+
import dev.compactmods.spatial.aabb.AABBHelper;
5+
import dev.compactmods.spatial.random.RandomSourceExtras;
6+
import dev.compactmods.spatial.test.util.MCAssertions;
7+
import dev.compactmods.spatial.test.util.TestUtils;
8+
import net.minecraft.core.BlockPos;
9+
import net.minecraft.core.Direction;
10+
import net.minecraft.util.RandomSource;
11+
import net.minecraft.world.phys.AABB;
12+
import net.minecraft.world.phys.Vec3;
13+
import org.joml.Vector3d;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class AABBAlignerTests {
18+
19+
@Test
20+
public void canFloorToY0() {
21+
// Source minY = 5
22+
AABB before = AABB.ofSize(new Vec3(0, 7.5, 0), 5, 5, 5);
23+
24+
// Align to Y-0
25+
final var after = AABBAligner.floor(before, 0);
26+
27+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
28+
Assertions.assertEquals(0, after.minY, "After y level should be zero. (was: %s)".formatted(after.minY));
29+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
30+
}
31+
32+
@Test
33+
public void canFloorToAnotherAABB() {
34+
// Source minY = 5
35+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
36+
37+
// Target minY = 1 (bounds are Y 1-11)
38+
AABB bounds = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 6), 10, 10, 10);
39+
40+
// Align to Y-0
41+
final var after = AABBAligner.floor(before, bounds);
42+
43+
Assertions.assertEquals(5, before.minY, "Before was modified in-place rather than immutably moved.");
44+
Assertions.assertEquals(1, after.minY, "After y level should be 1. (was: %s)".formatted(after.minY));
45+
46+
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
47+
}
48+
49+
@Test
50+
public void canNormalize() {
51+
final var random = RandomSource.create();
52+
53+
// Target boundaries, placed from {0,0,0} - {10,10,10}
54+
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10);
55+
56+
// Randomly placed boundaries of size {5,5,5}
57+
final var randomBounds = AABB.ofSize(RandomSourceExtras.randomVec3(random), 5, 5, 5);
58+
59+
final var aligned = AABBAligner.create(bounds, randomBounds)
60+
.normalize()
61+
.align();
62+
63+
final var EXPECTED_CENTER = new Vec3(-2.5, -2.5, -2.5);
64+
final var EXPECTED_CORNER = AABBHelper.minCorner(bounds);
65+
final var EXPECTED_SIZE = new Vector3d(5, 5, 5);
66+
67+
MCAssertions.assertVec3Equals(aligned.getCenter(), EXPECTED_CENTER);
68+
MCAssertions.assertVec3Equals(AABBHelper.minCorner(aligned), EXPECTED_CORNER);
69+
MCAssertions.assertVec3Equals(AABBHelper.sizeOf(aligned), EXPECTED_SIZE);
70+
}
71+
72+
@Test
73+
public void canAlignOnBlockPosition() {
74+
final var random = RandomSource.create();
75+
76+
AABB randomAABB = TestUtils.randomAABB(random);
77+
final var originalSize = AABBHelper.sizeOf(randomAABB);
78+
79+
// Center on 1, 2, 3
80+
final var aligned = AABBAligner.create(randomAABB)
81+
.center(new BlockPos(1, 2, 3))
82+
.align();
83+
84+
// Assert center position
85+
MCAssertions.assertVec3Equals(new Vec3(1.5, 2.5, 3.5), aligned.getCenter());
86+
87+
// Assert size did not change
88+
MCAssertions.assertVec3Equals(originalSize, AABBHelper.sizeOf(aligned));
89+
}
90+
}
Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package dev.compactmods.spatial.test.junit;
22

3+
import dev.compactmods.spatial.aabb.AABBAligner;
34
import dev.compactmods.spatial.aabb.AABBHelper;
4-
import dev.compactmods.spatial.test.TestUtils;
5-
import dev.compactmods.spatial.test.junit.util.MCAssertions;
5+
import dev.compactmods.spatial.random.RandomSourceExtras;
6+
import dev.compactmods.spatial.test.util.MCAssertions;
67
import net.minecraft.core.Direction;
78
import net.minecraft.util.RandomSource;
89
import net.minecraft.world.phys.AABB;
@@ -12,47 +13,16 @@
1213
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.api.TestFactory;
1415

15-
import java.util.Collection;
1616
import java.util.stream.Stream;
1717

1818
public class AABBHelperTests {
1919

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-
5020
@Test
5121
public void normalizeToZero() {
5222
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
5323

5424
// Align to Y-0
55-
final var after = AABBHelper.normalize(before);
25+
final var after = AABBAligner.normalize(before);
5626

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

@@ -64,11 +34,11 @@ public void normalizeToZero() {
6434
}
6535

6636
@TestFactory
67-
public static Stream<DynamicTest> normalizeBoundaryTests() {
37+
public Stream<DynamicTest> normalizeBoundaryTests() {
6838
final var random = RandomSource.create();
6939

7040
return Stream.concat(
71-
TestUtils.randomVec3Stream(random).limit(10),
41+
RandomSourceExtras.randomVec3Stream(random).limit(10),
7242

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

88-
final var after = AABBHelper.normalizeWithin(before, bounds);
58+
final var after = AABBAligner.create(bounds, before)
59+
.normalize()
60+
.align();
8961

9062
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-
63+
MCAssertions.assertVec3Equals(AABBHelper.minCorner(bounds), AABBHelper.minCorner(after));
9464
Assertions.assertEquals(5, after.getYsize(), "AABB size was modified; should have remained the same.");
9565
}
9666
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dev.compactmods.spatial.test.junit;
2+
3+
import dev.compactmods.spatial.aabb.AABBHelper;
4+
import dev.compactmods.spatial.direction.DirectionalMath;
5+
import net.minecraft.core.Direction;
6+
import net.minecraft.world.phys.AABB;
7+
import net.minecraft.world.phys.Vec3;
8+
import org.joml.Vector3dc;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class DirectionalMathTests {
13+
14+
@Test
15+
public void canAlignUp() {
16+
AABB boundaries = AABBHelper.zeroOriginSized(10);
17+
18+
Vec3 centerCenter = boundaries.getCenter();
19+
Vector3dc topCenter = DirectionalMath.directionalEdge(Direction.UP, boundaries);
20+
21+
Assertions.assertEquals(centerCenter.x, topCenter.x());
22+
Assertions.assertEquals(boundaries.maxY, topCenter.y());
23+
Assertions.assertEquals(centerCenter.z, topCenter.z());
24+
}
25+
26+
@Test
27+
public void canAlignDown() {
28+
AABB boundaries = AABBHelper.zeroOriginSized(10);
29+
30+
Vec3 centerCenter = boundaries.getCenter();
31+
Vector3dc bottomCenter = DirectionalMath.directionalEdge(Direction.DOWN, boundaries);
32+
33+
Assertions.assertEquals(centerCenter.x, bottomCenter.x());
34+
Assertions.assertEquals(boundaries.minY, bottomCenter.y());
35+
Assertions.assertEquals(centerCenter.z, bottomCenter.z());
36+
}
37+
38+
@Test
39+
public void canAlignNorth() {
40+
AABB boundaries = AABBHelper.zeroOriginSized(10);
41+
42+
Vec3 centerCenter = boundaries.getCenter();
43+
Vector3dc northCenter = DirectionalMath.directionalEdge(Direction.NORTH, boundaries);
44+
45+
Assertions.assertEquals(0, northCenter.x());
46+
Assertions.assertEquals(centerCenter.y, northCenter.y());
47+
Assertions.assertEquals(-5, northCenter.z());
48+
}
49+
50+
@Test
51+
public void canAlignWest() {
52+
AABB boundaries = AABBHelper.zeroOriginSized(10);
53+
54+
Vec3 centerCenter = boundaries.getCenter();
55+
Vector3dc westCenter = DirectionalMath.directionalEdge(Direction.WEST, boundaries);
56+
57+
Assertions.assertEquals(-5, westCenter.x());
58+
Assertions.assertEquals(centerCenter.y, westCenter.y());
59+
Assertions.assertEquals(0, westCenter.z());
60+
}
61+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.compactmods.spatial.test.junit;
2+
3+
import dev.compactmods.spatial.random.RandomSourceExtras;
4+
import dev.compactmods.spatial.test.util.MCAssertions;
5+
import dev.compactmods.spatial.vector.VectorUtils;
6+
import net.minecraft.util.RandomSource;
7+
import net.minecraft.world.phys.AABB;
8+
import net.minecraft.world.phys.Vec3;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class RandomSourceExtrasTests {
13+
14+
@Test
15+
public void canGenerateRandomDoubles() {
16+
RandomSource random = RandomSource.create();
17+
18+
Assertions.assertDoesNotThrow(() -> {
19+
RandomSourceExtras.randomDouble(random);
20+
});
21+
22+
double zeroAndOne = RandomSourceExtras.randomDouble(random, 0, 1);
23+
double negative = RandomSourceExtras.randomDouble(random, -10, -1);
24+
double positive = RandomSourceExtras.randomDouble(random, 1, 10);
25+
26+
Assertions.assertTrue(zeroAndOne > 0);
27+
Assertions.assertTrue(negative < 0);
28+
Assertions.assertTrue(positive > 0);
29+
}
30+
31+
@Test
32+
public void canGenerateRandomVecWithinBounds() {
33+
RandomSource random = RandomSource.create();
34+
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10);
35+
36+
for(int i = 0; i < 25; i++) {
37+
var randomV3 = RandomSourceExtras.randomVec3(random, bounds);
38+
39+
Assertions.assertTrue(bounds.contains(randomV3), "Random generated outside of boundaries: " + randomV3.toString());
40+
}
41+
}
42+
43+
@Test
44+
public void canStreamRandomVectorsInBoundaries() {
45+
RandomSource random = RandomSource.create();
46+
final var bounds = AABB.ofSize(Vec3.ZERO, 10, 10, 10);
47+
48+
RandomSourceExtras.randomVec3Stream(random, bounds)
49+
.limit(10)
50+
.forEach(pos -> Assertions.assertTrue(bounds.contains(pos)));
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.compactmods.spatial.test.junit;
2+
3+
import dev.compactmods.spatial.test.util.MCAssertions;
4+
import dev.compactmods.spatial.vector.VectorUtils;
5+
import net.minecraft.core.Vec3i;
6+
import net.minecraft.world.phys.Vec3;
7+
import org.joml.Vector3d;
8+
import org.joml.Vector3dc;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class VectorUtilsTests {
12+
13+
@Test
14+
public void testConvertVec3i() {
15+
Vec3i pos = new Vec3i(1, 2, 3);
16+
Vector3d converted = VectorUtils.convert3d(pos);
17+
18+
MCAssertions.assertVec3Equals(new Vector3d(1, 2, 3), converted);
19+
}
20+
21+
@Test
22+
public void testConvertVector3dc() {
23+
Vector3dc vec = new Vector3d(2.5, 2.5, 2.5);
24+
Vec3 converted = VectorUtils.convert3d(vec);
25+
26+
MCAssertions.assertVec3Equals(new Vec3(2.5, 2.5, 2.5), converted);
27+
}
28+
}

spatial-neoforge/src/test/java/dev/compactmods/spatial/test/junit/util/MCAssertions.java renamed to spatial-neoforge/src/test/java/dev/compactmods/spatial/test/util/MCAssertions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package dev.compactmods.spatial.test.junit.util;
1+
package dev.compactmods.spatial.test.util;
22

33
import com.google.common.math.DoubleMath;
44
import net.minecraft.world.phys.Vec3;
5+
import org.joml.Vector3dc;
56
import org.junit.jupiter.api.Assertions;
67

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

@@ -15,4 +16,9 @@ public static void assertVec3Equals(Vec3 actual, Vec3 expected) {
1516
if(!DoubleMath.fuzzyEquals(actual.z, expected.z, 0.001))
1617
Assertions.fail("Z did not match expected value (was: %s; expected: %s)".formatted(actual.z, expected.z));
1718
}
19+
20+
public static void assertVec3Equals(Vector3dc expected, Vector3dc actual) {
21+
if(!expected.equals(actual, 0.001))
22+
Assertions.fail("Actual did not match expected value (was: %s; expected: %s)".formatted(actual, expected));
23+
}
1824
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.compactmods.spatial.test.util;
2+
3+
import dev.compactmods.spatial.random.RandomSourceExtras;
4+
import net.minecraft.util.RandomSource;
5+
import net.minecraft.world.phys.AABB;
6+
import net.minecraft.world.phys.Vec3;
7+
8+
public class TestUtils {
9+
10+
public static AABB randomAABB(RandomSource random) {
11+
final var position = RandomSourceExtras.randomVec3(random, AABB.ofSize(Vec3.ZERO, 50, 50, 50));
12+
final var size = RandomSourceExtras.randomVec3(random);
13+
14+
return AABB.ofSize(position, size.x, size.y, size.z);
15+
}
16+
}

0 commit comments

Comments
 (0)