Skip to content
Browse files

Merge pull request #416 from jcortez/master

Fixed issue #219 (rebased)
  • Loading branch information...
2 parents 2e460f8 + 0c38750 commit 688854ba468c53884b74952f4daf938a99c44e8c David Saff committed May 25, 2012
View
4 acknowledgements.txt
@@ -108,6 +108,10 @@
Daniel Rothmaler (drothmaler@github):
#299: random temp file/folder creation
#300: ErrorCollector.checkThat overload
+
+2011 Jun 25
+ Juan Cortez (jcortez@github): Fixed issue #219 where floats were being
+ printed and represented as doubles in error messages.
2011 Jul 06
Stefan Birkner: Fixed wrong documentation of ClassRule (github#254).
View
49 src/main/java/org/junit/Assert.java
@@ -536,7 +536,7 @@ private static void internalArrayEquals(String message, Object expecteds,
}
/**
- * Asserts that two doubles or floats are equal to within a positive delta.
+ * Asserts that two doubles are equal to within a positive delta.
* If they are not, an {@link AssertionError} is thrown with the given
* message. If the expected value is infinity then the delta value is
* ignored. NaNs are considered equal:
@@ -560,6 +560,31 @@ static public void assertEquals(String message, double expected,
failNotEquals(message, new Double(expected), new Double(actual));
}
+ /**
+ * Asserts that two floats are equal to within a positive delta.
+ * If they are not, an {@link AssertionError} is thrown with the given
+ * message. If the expected value is infinity then the delta value is
+ * ignored. NaNs are considered equal:
+ * <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
+ *
+ * @param message
+ * the identifying message for the {@link AssertionError} (<code>null</code>
+ * okay)
+ * @param expected
+ * expected value
+ * @param actual
+ * the value to check against <code>expected</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+ static public void assertEquals(String message, float expected,
+ float actual, float delta) {
+ if (Float.compare(expected, actual) == 0)
+ return;
+ if (!(Math.abs(expected - actual) <= delta))
+ failNotEquals(message, new Float(expected), new Float(actual));
static private boolean doubleIsDifferent(double d1, double d2, double delta) {
if (Double.compare(d1, d2) == 0)
return false;
@@ -620,7 +645,7 @@ static public void assertEquals(String message, double expected,
}
/**
- * Asserts that two doubles or floats are equal to within a positive delta.
+ * Asserts that two doubles are equal to within a positive delta.
* If they are not, an {@link AssertionError} is thrown. If the expected
* value is infinity then the delta value is ignored.NaNs are considered
* equal: <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
@@ -637,6 +662,26 @@ static public void assertEquals(String message, double expected,
static public void assertEquals(double expected, double actual, double delta) {
assertEquals(null, expected, actual, delta);
}
+
+ /**
+ * Asserts that two floats are equal to within a positive delta.
+ * If they are not, an {@link AssertionError} is thrown. If the expected
+ * value is infinity then the delta value is ignored. NaNs are considered
+ * equal: <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
+ *
+ * @param expected
+ * expected value
+ * @param actual
+ * the value to check against <code>expected</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+
+ static public void assertEquals(float expected, float actual, float delta) {
+ assertEquals(null, expected, actual, delta);
+ }
/**
* Asserts that an object isn't null. If it is an {@link AssertionError} is
View
10 src/main/java/org/junit/internal/InexactComparisonCriteria.java
@@ -3,17 +3,21 @@
import org.junit.Assert;
public class InexactComparisonCriteria extends ComparisonCriteria {
- public double fDelta;
+ public Object fDelta;
public InexactComparisonCriteria(double delta) {
fDelta= delta;
}
+
+ public InexactComparisonCriteria(float delta){
+ fDelta = delta;
+ }
@Override
protected void assertElementsEqual(Object expected, Object actual) {
if (expected instanceof Double)
- Assert.assertEquals((Double)expected, (Double)actual, fDelta);
+ Assert.assertEquals((Double)expected, (Double)actual, (Double)fDelta);
else
- Assert.assertEquals((Float)expected, (Float)actual, fDelta);
+ Assert.assertEquals((Float)expected, (Float)actual, (Float)fDelta);
}
}
View
9 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -299,6 +299,15 @@ public void IntegerDoesNotEqualLong() {
assertEquals(1.0, 1.1);
}
+ @Test
+ public void floatsNotDoublesInArrays()
+ {
+ float delta = 4.444f;
+ float[] f1 = new float[] {1.111f};
+ float[] f2 = new float[] {5.555f};
+ Assert.assertArrayEquals(f1, f2, delta);
+ }
+
@Test(expected= AssertionError.class) public void bigDecimalsNotEqual() {
assertEquals(new BigDecimal("123.4"), new BigDecimal("123.0"));
}

0 comments on commit 688854b

Please sign in to comment.
Something went wrong with that request. Please try again.