Skip to content

Commit cd7e072

Browse files
committed
#79 Improve some classes coverage
* Add new equals tests * Add private constructor to MathPIE and make MathPIE final * Change broad phase tests category * Improve IShape#equals logic * Improve some unit tests * Add some other minor improvements
1 parent 6bf2a36 commit cd7e072

File tree

15 files changed

+131
-36
lines changed

15 files changed

+131
-36
lines changed

core/src/main/java/com/github/introfog/pie/core/math/MathPIE.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
package com.github.introfog.pie.core.math;
1717

1818
// TODO Change class to context or property class
19-
public class MathPIE {
19+
public final class MathPIE {
20+
private MathPIE() {
21+
// Empty constructor
22+
}
23+
2024
public static final float STATIC_BODY_DENSITY = 0f;
2125

2226
public static final int MAX_POLY_VERTEX_COUNT = 64;

core/src/main/java/com/github/introfog/pie/core/shape/Circle.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.github.introfog.pie.core.Body;
1919

20-
import java.util.Objects;
2120
import java.util.StringJoiner;
2221

2322
public class Circle extends IShape {
@@ -56,7 +55,7 @@ protected void computeMassAndInertia() {
5655
float mass = (float) Math.PI * radius * radius * body.density;
5756
body.invertedMass = (mass == 0f) ? 0f : 1f / mass;
5857

59-
float inertia = radius * radius / body.invertedMass;
58+
float inertia = radius * radius / (body.invertedMass == 0 ? 1 : body.invertedMass);
6059
body.invertedInertia = (inertia != 0.0f) ? 1.0f / inertia : 0.0f;
6160
}
6261
}

core/src/main/java/com/github/introfog/pie/core/shape/IShape.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,9 @@ public final void applyImpulse(Vector2f impulse, Vector2f contactVector) {
8181

8282
@Override
8383
public boolean equals(Object o) {
84-
if (this == o) {
85-
return true;
86-
}
87-
if (o == null || getClass() != o.getClass()) {
88-
return false;
89-
}
90-
IShape shape = (IShape) o;
91-
return shapeId == shape.shapeId;
84+
// In general, consider shapes the same only when they refer to the
85+
// same object in memory, this is provided by the shapeId variable
86+
return this == o;
9287
}
9388

9489
@Override

core/src/test/java/com/github/introfog/pie/core/BodyTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ public void equalsAndHashCodeToAnotherEqualBodyTest() {
6060
@Test
6161
public void equalsAndHashCodeToAnotherNotEqualBodyTest() {
6262
Body first = new Body(0.1f, 0.2f, 0.3f, 0.4f);
63-
Body second = new Body(0.2f, 0.2f, 0.3f, 1.8f);
63+
Body second = new Body(0.2f, 0.2f, 0.3f, 0.4f);
6464
PIETest.checkEqualsAndHashCodeMethods(first, second, false);
6565

66-
second = new Body(0.1f, 0.3f, 0.3f, 1.8f);
66+
second = new Body(0.1f, 0.3f, 0.3f, 0.4f);
6767
PIETest.checkEqualsAndHashCodeMethods(first, second, false);
6868

69-
second = new Body(0.1f, 0.2f, 0.4f, 1.8f);
69+
second = new Body(0.1f, 0.2f, 0.4f, 0.4f);
7070
PIETest.checkEqualsAndHashCodeMethods(first, second, false);
7171

72-
second = new Body(0.1f, 0.2f, 0.3f, 1.9f);
72+
second = new Body(0.1f, 0.2f, 0.3f, 0.5f);
7373
PIETest.checkEqualsAndHashCodeMethods(first, second, false);
7474
}
7575

@@ -80,6 +80,13 @@ public void equalsToNullTest() {
8080
Assert.assertFalse(body.equals(null));
8181
}
8282

83+
@Test
84+
public void equalsToAnotherClassTest() {
85+
Body body = new Body(0.1f, 0.2f, 0.3f, 0.4f);
86+
87+
Assert.assertFalse(body.equals(""));
88+
}
89+
8390
@Test
8491
public void toStringTest() {
8592
Body body = new Body(0.1634345f, 0.2f, 0.3f, 0.4f);

core/src/test/java/com/github/introfog/pie/core/collisions/broadphase/AbstractBroadPhaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import com.github.introfog.pie.core.shape.IShape;
2222
import com.github.introfog.pie.core.util.ShapePair;
2323
import com.github.introfog.pie.test.PIETest;
24-
import com.github.introfog.pie.test.annotations.AlgorithmicTest;
24+
import com.github.introfog.pie.test.annotations.UnitTest;
2525

2626
import java.util.ArrayList;
2727
import java.util.List;
2828

2929
import org.junit.Test;
3030
import org.junit.experimental.categories.Category;
3131

32-
@Category(AlgorithmicTest.class)
32+
@Category(UnitTest.class)
3333
public abstract class AbstractBroadPhaseTest extends PIETest {
3434
@Test
3535
public void addShapeMethodTest() {

core/src/test/java/com/github/introfog/pie/core/collisions/broadphase/BruteForceMethodTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package com.github.introfog.pie.core.collisions.broadphase;
1717

18-
import com.github.introfog.pie.test.annotations.AlgorithmicTest;
18+
import com.github.introfog.pie.test.annotations.UnitTest;
1919

2020
import org.junit.experimental.categories.Category;
2121

22-
@Category(AlgorithmicTest.class)
22+
@Category(UnitTest.class)
2323
public class BruteForceMethodTest extends AbstractBroadPhaseTest {
2424
@Override
2525
protected AbstractBroadPhase getBroadPhaseMethod() {

core/src/test/java/com/github/introfog/pie/core/collisions/broadphase/SpatialHashingMethodTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import com.github.introfog.pie.core.shape.IShape;
2222
import com.github.introfog.pie.core.shape.Polygon;
2323
import com.github.introfog.pie.core.util.ShapePair;
24-
import com.github.introfog.pie.test.annotations.AlgorithmicTest;
24+
import com.github.introfog.pie.test.annotations.UnitTest;
2525

2626
import java.util.ArrayList;
2727
import java.util.List;
2828

2929
import org.junit.Test;
3030
import org.junit.experimental.categories.Category;
3131

32-
@Category(AlgorithmicTest.class)
32+
@Category(UnitTest.class)
3333
public class SpatialHashingMethodTest extends AbstractBroadPhaseTest {
3434
@Override
3535
protected AbstractBroadPhase getBroadPhaseMethod() {

core/src/test/java/com/github/introfog/pie/core/collisions/broadphase/SweepAndPruneMethodTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
*/
1616
package com.github.introfog.pie.core.collisions.broadphase;
1717

18+
import com.github.introfog.pie.core.math.MathPIE;
19+
import com.github.introfog.pie.core.shape.Circle;
20+
import com.github.introfog.pie.core.shape.IShape;
21+
import com.github.introfog.pie.core.util.ShapePair;
22+
import com.github.introfog.pie.core.util.TestUtil;
1823
import com.github.introfog.pie.test.annotations.AlgorithmicTest;
1924

25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import org.junit.Test;
2028
import org.junit.experimental.categories.Category;
2129

2230
@Category(AlgorithmicTest.class)
@@ -25,4 +33,48 @@ public class SweepAndPruneMethodTest extends AbstractBroadPhaseTest {
2533
protected AbstractBroadPhase getBroadPhaseMethod() {
2634
return new SweepAndPruneMethod();
2735
}
36+
37+
@Test
38+
public void yAxisTest() {
39+
IShape c1 = new Circle(2f, 0, 0, MathPIE.STATIC_BODY_DENSITY, 0f);
40+
IShape c2 = new Circle(2f, 0, 2, MathPIE.STATIC_BODY_DENSITY, 0f);
41+
IShape c3 = new Circle(2f, 5, 10, MathPIE.STATIC_BODY_DENSITY, 0f);
42+
43+
List<IShape> shapes = new ArrayList<>();
44+
shapes.add(c1);
45+
shapes.add(c2);
46+
shapes.add(c3);
47+
AbstractBroadPhase broadPhaseMethod = getBroadPhaseMethod();
48+
broadPhaseMethod.setShapes(shapes);
49+
50+
List<ShapePair> cmpShapePairs = new ArrayList<>(1);
51+
52+
cmpShapePairs.add(new ShapePair(c1, c2));
53+
TestUtil.assertEqualsShapePairsList(cmpShapePairs, broadPhaseMethod.calculateAabbCollisions());
54+
// The test is designed so that after the first iteration, the SweepAndPrune method will work along the Y-axis
55+
TestUtil.assertEqualsShapePairsList(cmpShapePairs, broadPhaseMethod.calculateAabbCollisions());
56+
}
57+
58+
@Test
59+
public void intersectedByCurrentAxisButNotIntersectedByOtherAxisTest() {
60+
// The test reproduces the situation when shape intersect along the axis
61+
// that is current in the SAP method, but do not intersect along the other axis
62+
IShape c1 = new Circle(2f, 0, 0, MathPIE.STATIC_BODY_DENSITY, 0f);
63+
IShape c2 = new Circle(2f, 0, 5, MathPIE.STATIC_BODY_DENSITY, 0f);
64+
IShape c3 = new Circle(2f, 5, 5, MathPIE.STATIC_BODY_DENSITY, 0f);
65+
IShape c4 = new Circle(2f, 0, 10, MathPIE.STATIC_BODY_DENSITY, 0f);
66+
67+
List<IShape> shapes = new ArrayList<>();
68+
shapes.add(c1);
69+
shapes.add(c2);
70+
shapes.add(c3);
71+
shapes.add(c4);
72+
AbstractBroadPhase broadPhaseMethod = getBroadPhaseMethod();
73+
broadPhaseMethod.setShapes(shapes);
74+
75+
List<ShapePair> cmpShapePairs = new ArrayList<>(1);
76+
77+
TestUtil.assertEqualsShapePairsList(cmpShapePairs, broadPhaseMethod.calculateAabbCollisions());
78+
TestUtil.assertEqualsShapePairsList(cmpShapePairs, broadPhaseMethod.calculateAabbCollisions());
79+
}
2880
}

core/src/test/java/com/github/introfog/pie/core/collisions/broadphase/aabbtree/AABBTreeMethodTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import com.github.introfog.pie.core.math.MathPIE;
2121
import com.github.introfog.pie.core.math.Vector2f;
2222
import com.github.introfog.pie.core.shape.Circle;
23-
import com.github.introfog.pie.test.annotations.AlgorithmicTest;
23+
import com.github.introfog.pie.test.annotations.UnitTest;
2424

2525
import java.lang.reflect.Field;
2626

2727
import org.junit.Assert;
2828
import org.junit.Test;
2929
import org.junit.experimental.categories.Category;
3030

31-
@Category(AlgorithmicTest.class)
31+
@Category(UnitTest.class)
3232
public class AABBTreeMethodTest extends AbstractBroadPhaseTest {
3333
@Override
3434
protected AbstractBroadPhase getBroadPhaseMethod() {

core/src/test/java/com/github/introfog/pie/core/math/MathPIETest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
@Category(UnitTest.class)
2626
public class MathPIETest extends PIETest {
27+
@Test(expected = IllegalAccessException.class)
28+
public void constructorTest() throws IllegalAccessException, InstantiationException {
29+
MathPIE.class.newInstance();
30+
Assert.fail("Utility class constructor should be private");
31+
}
32+
2733
@Test
2834
public void fastFloorTest() {
2935
Assert.assertEquals(1.0, MathPIE.fastFloor(1.0000001f), PIETest.FLOAT_EPSILON_COMPARISON);

0 commit comments

Comments
 (0)