Skip to content

Commit

Permalink
Improve BoundingBox test
Browse files Browse the repository at this point in the history
  • Loading branch information
nusco committed Nov 13, 2015
1 parent 157e2b0 commit 98b14c4
Showing 1 changed file with 63 additions and 67 deletions.
130 changes: 63 additions & 67 deletions src/test/java/org/nusco/narjillos/core/geometry/BoundingBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,9 @@

public class BoundingBoxTest {

private static class SegmentShapedThing implements SegmentShape {

private final double x1;
private final double y1;
private final double x2;
private final double y2;

public SegmentShapedThing(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

@Override
public Segment toSegment() {
return new Segment(Vector.cartesian(x1, y1), Vector.cartesian(x2 - x1, y2 - y1));
}
}

public final Set<SegmentShape> shapes = new LinkedHashSet<>();

@Test
public void definesTheBoundariesOfASegment() {
shapes.add(new SegmentShapedThing(10, 20, 30, 40));
BoundingBox boundingBox = new BoundingBox(shapes);
BoundingBox boundingBox = createBoundingBox(10, 20, 30, 40);

assertEquals(10, boundingBox.left, 0.0);
assertEquals(30, boundingBox.right, 0.0);
Expand All @@ -46,8 +23,7 @@ public void definesTheBoundariesOfASegment() {

@Test
public void worksWithNegativeBoundaries() {
shapes.add(new SegmentShapedThing(-10, -20, 30, 40));
BoundingBox boundingBox = new BoundingBox(shapes);
BoundingBox boundingBox = createBoundingBox(-10, -20, 30, 40);

assertEquals(-10, boundingBox.left, 0.0);
assertEquals(30, boundingBox.right, 0.0);
Expand All @@ -57,9 +33,7 @@ public void worksWithNegativeBoundaries() {

@Test
public void worksWithSegmentsInAnyOrientation() {
shapes.add(new SegmentShapedThing(30, 40, -10, -20));
BoundingBox boundingBox = new BoundingBox(shapes);

BoundingBox boundingBox = createBoundingBox(30, 40, -10, -20);

assertEquals(-10, boundingBox.left, 0.0);
assertEquals(30, boundingBox.right, 0.0);
Expand All @@ -69,7 +43,7 @@ public void worksWithSegmentsInAnyOrientation() {

@Test
public void isEmptyIfThereAreNoSegments() {
BoundingBox boundingBox = new BoundingBox(shapes);
BoundingBox boundingBox = new BoundingBox(new LinkedHashSet<SegmentShape>());

assertEquals(0, boundingBox.left, 0.0);
assertEquals(0, boundingBox.right, 0.0);
Expand All @@ -78,69 +52,91 @@ public void isEmptyIfThereAreNoSegments() {
}

@Test
public void checksOverlaps() {
Set<SegmentShape> shapes1 = new LinkedHashSet<>();
shapes1.add(new SegmentShapedThing(0, 0, 10, 10));
BoundingBox boundingBox1 = new BoundingBox(shapes1);
public void overlapsWithAnotherBoundingBox() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(0, 9, 10, 10);

Set<SegmentShape> shapes2 = new LinkedHashSet<>();
shapes2.add(new SegmentShapedThing(0, 11, 20, 20));
BoundingBox boundingBox2 = new BoundingBox(shapes2);
assertTrue(boundingBox1.overlaps(boundingBox2));
}

@Test
public void doesNotOverlapIfItIsToTheRightOfTheOtherBoundingBox() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(11, 0, 10, 10);

Set<SegmentShape> shapes3 = new LinkedHashSet<>();
shapes3.add(new SegmentShapedThing(-5, -5, 5, 5));
BoundingBox boundingBox3 = new BoundingBox(shapes3);
assertFalse(boundingBox1.overlaps(boundingBox2));
}

@Test
public void doesNotOverlapIfItIsToTheLeftOfTheOtherBoundingBox() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(-11, 0, 10, 10);

assertFalse(boundingBox1.overlaps(boundingBox2));
}

@Test
public void doesNotOverlapIfItIsBelowTheOtherBoundingBox() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(0, 11, 10, 10);

assertFalse(boundingBox1.overlaps(boundingBox2));
}

@Test
public void doesNotOverlapIfItIsAboveTheOtherBoundingBox() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(0, -11, 10, 10);

assertFalse(boundingBox1.overlaps(boundingBox2));
assertTrue(boundingBox1.overlaps(boundingBox3));
assertFalse(boundingBox2.overlaps(boundingBox3));
}

@Test
public void overlapsItself() {
shapes.add(new SegmentShapedThing(0, 0, 10, 10));
BoundingBox boundingBox = new BoundingBox(shapes);
BoundingBox boundingBox = createBoundingBox(0, 0, 10, 10);

assertTrue(boundingBox.overlaps(boundingBox));
assertFalse(boundingBox.overlaps(boundingBox));
}

@Test
public void doesNotOverlapIfItTouchesOnTheEdge() {
Set<SegmentShape> shapes1 = new LinkedHashSet<>();
shapes1.add(new SegmentShapedThing(0, 0, 10, 10));
BoundingBox boundingBox1 = new BoundingBox(shapes1);

Set<SegmentShape> shapes2 = new LinkedHashSet<>();
shapes2.add(new SegmentShapedThing(0, 10, 20, 20));
BoundingBox boundingBox2 = new BoundingBox(shapes2);
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(0, 10, 20, 20);

assertFalse(boundingBox1.overlaps(boundingBox2));
}

@Test
public void neverOverlapsIfBothBoxesHaveAreaZero() {
Set<SegmentShape> shapes = new LinkedHashSet<>();
shapes.add(new SegmentShapedThing(10, 10, 10, 10));
BoundingBox boundingBox = new BoundingBox(shapes);
BoundingBox boundingBox = createBoundingBox(10, 10, 0, 0);

assertFalse(boundingBox.overlaps(boundingBox));
}

@Test
public void canStillOverlapsIfOnlyOneBoxHasAreaZero() {
Set<SegmentShape> shapes1 = new LinkedHashSet<>();
shapes1.add(new SegmentShapedThing(0, 0, 0, 0));
BoundingBox boundingBox1 = new BoundingBox(shapes1);
public void canStillOverlapIfTheFirstBoxHasAreaZero() {
BoundingBox boundingBox1 = createBoundingBox(10, 10, 0, 0);
BoundingBox boundingBox2 = createBoundingBox(-10, -10, 20, 20);

Set<SegmentShape> shapes2 = new LinkedHashSet<>();
shapes2.add(new SegmentShapedThing(-10, -10, 10, 10));
BoundingBox boundingBox2 = new BoundingBox(shapes2);
Set<SegmentShape> shapes3 = new LinkedHashSet<>();
shapes3.add(new SegmentShapedThing(1, 1, 5, 5));
BoundingBox boundingBox3 = new BoundingBox(shapes3);
assertTrue(boundingBox1.overlaps(boundingBox2));
}

@Test
public void canStillOverlapIfTheSecondBoxHasAreaZero() {
BoundingBox boundingBox1 = createBoundingBox(0, 0, 10, 10);
BoundingBox boundingBox2 = createBoundingBox(5, 5, 0, 0);

assertTrue(boundingBox1.overlaps(boundingBox2));
assertFalse(boundingBox1.overlaps(boundingBox3));
}

private BoundingBox createBoundingBox(int x, int y, int width, int height) {
Set<SegmentShape> shapes = new LinkedHashSet<>();
shapes.add(new SegmentShape() {
@Override
public Segment toSegment() {
return new Segment(Vector.cartesian(x, y), Vector.cartesian(width, height));
}
});
return new BoundingBox(shapes);
}
}

0 comments on commit 98b14c4

Please sign in to comment.