Skip to content

Commit

Permalink
1.7 adds assertWithinPercent
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick McEvoy committed Dec 5, 2019
1 parent 1d7364b commit e9feef1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>junit</groupId>
<artifactId>junit-more</artifactId>
<packaging>jar</packaging>
<version>1.6</version>
<version>1.7</version>

<name>JUnitMore</name>
<url>https://github.com/pamcevoy/junit-more</url>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/ext/junit/more/NumberAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,63 @@ public static void assertBetween(long expectedMin, long actual, long expectedMax
assertGreaterThan(expectedMin, actual);
assertLessThan(expectedMax, actual);
}

/**
* Assert that the actual value is within the given percent of the expected value.
* @param expected the expected value
* @param percent the percent above and below expected
* @param actual the actual value
*/
public static void assertWithinPercent(long expected, double percent, long actual) {
double pct = Math.max(0.0, Math.min(Math.abs(percent), 1.0));
long expectedMin = (long)(expected * (1.0 - pct));
long expectedMax = (long)(expected * (1.0 + pct));
assertBetween(expectedMin, actual, expectedMax);
}

/**
* Assert that the actual value is greater than the expected minimum.
* @param expectedMin the expected minimum
* @param actual the actual value
*/
public static void assertGreaterThan(double expectedMin, double actual) {
if (actual < expectedMin) {
Assert.fail("Actual '" + actual + "' less than expected minimum '" + expectedMin + "'.");
}
}

/**
* Assert that the actual value is less than the expected maximum.
* @param expectedMax the expected minimum
* @param actual the actual value
*/
public static void assertLessThan(double expectedMax, double actual) {
if (actual > expectedMax) {
Assert.fail("Actual '" + actual + "' greater than expected maximum '" + expectedMax + "'.");
}
}

/**
* Assert that the value is between these two values (inclusive).
* @param expectedMin the expected min
* @param actual the actual value
* @param expectedMax the expected max
*/
public static void assertBetween(double expectedMin, double actual, double expectedMax) {
assertGreaterThan(expectedMin, actual);
assertLessThan(expectedMax, actual);
}

/**
* Assert that the actual value is within the given percent of the expected value.
* @param expected the expected value
* @param percent the percent above and below expected
* @param actual the actual value
*/
public static void assertWithinPercent(double expected, double percent, double actual) {
double pct = Math.max(0.0, Math.min(Math.abs(percent), 1.0));
double expectedMin = (expected * (1.0 - pct));
double expectedMax = (expected * (1.0 + pct));
assertBetween(expectedMin, actual, expectedMax);
}
}

0 comments on commit e9feef1

Please sign in to comment.