Skip to content

Commit

Permalink
infinity and NaN asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
kizombaDev committed Dec 11, 2016
1 parent 9fb0d61 commit 71fe5ce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/org/wahlzeit/model/CartesianCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public CartesianCoordinate(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
assertClassInvariants();
}

public double getX() {
Expand All @@ -28,6 +29,13 @@ public double getZ() {

@Override
protected void assertClassInvariants() {
//nothing to do here
assertIsValidDoubleRange(getX());
assertIsValidDoubleRange(getY());
assertIsValidDoubleRange(getZ());
}

private void assertIsValidDoubleRange(double value) throws IllegalArgumentException {
if (Double.isInfinite(value) || Double.isNaN(value))
throw new IllegalArgumentException("Double value is not valid" + value);
}
}
45 changes: 45 additions & 0 deletions src/test/java/org/wahlzeit/model/CartesianCoordinateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,49 @@ public void propertiesTest() {
Assert.assertEquals(2, oneTwoThreeCoordinate.getY(), DOUBLE_TEST_DELTA);
Assert.assertEquals(3, oneTwoThreeCoordinate.getZ(), DOUBLE_TEST_DELTA);
}

@Test(expected = IllegalArgumentException.class)
public void positiveInfinityXTest() {
new CartesianCoordinate(Double.POSITIVE_INFINITY, 2, 3);
}

@Test(expected = IllegalArgumentException.class)
public void negativeInfinityXTest() {
new CartesianCoordinate(Double.NEGATIVE_INFINITY, 2, 3);
}

@Test(expected = IllegalArgumentException.class)
public void notANumberXTest() {
new CartesianCoordinate(Double.NaN, 2, 3);
}

@Test(expected = IllegalArgumentException.class)
public void positiveInfinityYTest() {
new CartesianCoordinate(1, Double.POSITIVE_INFINITY, 3);
}

@Test(expected = IllegalArgumentException.class)
public void negativeInfinityYTest() {
new CartesianCoordinate(1, Double.NEGATIVE_INFINITY, 3);
}

@Test(expected = IllegalArgumentException.class)
public void notANumberYTest() {
new CartesianCoordinate(1, Double.NaN, 3);
}

@Test(expected = IllegalArgumentException.class)
public void notANumberZTest() {
new CartesianCoordinate(1, 2, Double.NaN);
}

@Test(expected = IllegalArgumentException.class)
public void positiveInfinityZTest() {
new CartesianCoordinate(1, 2, Double.POSITIVE_INFINITY);
}

@Test(expected = IllegalArgumentException.class)
public void negativeInfinityZTest() {
new CartesianCoordinate(1, 2, Double.NEGATIVE_INFINITY);
}
}

0 comments on commit 71fe5ce

Please sign in to comment.