Permalink
Browse files

Fixes issue #219 where Assert.assertArrayEquals(float[],float[],float…

…) displays an error message with doubles instead of floats
  • Loading branch information...
jcortez committed Jun 25, 2011
1 parent ed2b51b commit 0333e7efdc9ecf81df4212f82c3a59c6ff0fab19
@@ -417,7 +417,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:
@@ -442,6 +442,37 @@ static public void assertEquals(String message, double expected,
if (!(Math.abs(expected - actual) <= delta))
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));
+ }
+ }
/**
* Asserts that two longs are equal. If they are not, an
@@ -494,7 +525,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
@@ -511,6 +542,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
@@ -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);
}
}

0 comments on commit 0333e7e

Please sign in to comment.