Skip to content

Commit 78b4a89

Browse files
committed
Implement assertNotEquals() for float parameters.
Fix inconsistencies between assertEquals() and assertNotEquals() for double parameters.
1 parent 92a3f73 commit 78b4a89

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

src/main/java/org/junit/Assert.java

100644100755
+62-16
Original file line numberDiff line numberDiff line change
@@ -210,41 +210,57 @@ static public void assertNotEquals(long first, long second) {
210210
}
211211

212212
/**
213-
* Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
213+
* Asserts that two doubles are <b>not</b> equal to within a positive delta.
214214
* If they are, an {@link AssertionError} is thrown with the given
215215
* message. If the expected value is infinity then the delta value is
216216
* ignored. NaNs are considered equal:
217217
* <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
218218
*
219219
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
220220
* okay)
221-
* @param first first value to check
222-
* @param second the value to check against <code>first</code>
221+
* @param expected expected value
222+
* @param actual the value to check against <code>expected</code>
223223
* @param delta the maximum delta between <code>expected</code> and
224224
* <code>actual</code> for which both numbers are still
225225
* considered equal.
226226
*/
227-
static public void assertNotEquals(String message, double first,
228-
double second, double delta) {
229-
if (!doubleIsDifferent(first, second, delta)) {
230-
failEquals(message, new Double(first));
227+
static public void assertNotEquals(String message, double expected,
228+
double actual, double delta) {
229+
if (!doubleIsDifferent(expected, actual, delta)) {
230+
failEquals(message, new Double(expected));
231231
}
232232
}
233233

234234
/**
235-
* Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
235+
* Asserts that two doubles are <b>not</b> equal to within a positive delta.
236236
* If they are, an {@link AssertionError} is thrown. If the expected
237237
* value is infinity then the delta value is ignored.NaNs are considered
238238
* equal: <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
239239
*
240-
* @param first first value to check
241-
* @param second the value to check against <code>first</code>
240+
* @param expected expected value
241+
* @param actual the value to check against <code>expected</code>
242+
* @param delta the maximum delta between <code>expected</code> and
243+
* <code>actual</code> for which both numbers are still
244+
* considered equal.
245+
*/
246+
static public void assertNotEquals(double expected, double actual, double delta) {
247+
assertNotEquals(null, expected, actual, delta);
248+
}
249+
250+
/**
251+
* Asserts that two floats are <b>not</b> equal to within a positive delta.
252+
* If they are, an {@link AssertionError} is thrown. If the expected
253+
* value is infinity then the delta value is ignored.NaNs are considered
254+
* equal: <code>assertNotEquals(Float.NaN, Float.NaN, *)</code> fails
255+
*
256+
* @param expected expected value
257+
* @param actual the value to check against <code>expected</code>
242258
* @param delta the maximum delta between <code>expected</code> and
243259
* <code>actual</code> for which both numbers are still
244260
* considered equal.
245261
*/
246-
static public void assertNotEquals(double first, double second, double delta) {
247-
assertNotEquals(null, first, second, delta);
262+
static public void assertNotEquals(float expected, float actual, float delta) {
263+
assertNotEquals(null, expected, actual, delta);
248264
}
249265

250266
/**
@@ -512,14 +528,33 @@ static public void assertEquals(String message, double expected,
512528
*/
513529
static public void assertEquals(String message, float expected,
514530
float actual, float delta) {
515-
if (Float.compare(expected, actual) == 0) {
516-
return;
517-
}
518-
if (!(Math.abs(expected - actual) <= delta)) {
531+
if (floatIsDifferent(expected, actual, delta)) {
519532
failNotEquals(message, new Float(expected), new Float(actual));
520533
}
521534
}
522535

536+
/**
537+
* Asserts that two floats are <b>not</b> equal to within a positive delta.
538+
* If they are, an {@link AssertionError} is thrown with the given
539+
* message. If the expected value is infinity then the delta value is
540+
* ignored. NaNs are considered equal:
541+
* <code>assertNotEquals(Float.NaN, Float.NaN, *)</code> fails
542+
*
543+
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
544+
* okay)
545+
* @param expected expected value
546+
* @param actual the value to check against <code>expected</code>
547+
* @param delta the maximum delta between <code>expected</code> and
548+
* <code>actual</code> for which both numbers are still
549+
* considered equal.
550+
*/
551+
static public void assertNotEquals(String message, float expected,
552+
float actual, float delta) {
553+
if (!floatIsDifferent(expected, actual, delta)) {
554+
failEquals(message, new Float(expected));
555+
}
556+
}
557+
523558
static private boolean doubleIsDifferent(double d1, double d2, double delta) {
524559
if (Double.compare(d1, d2) == 0) {
525560
return false;
@@ -531,6 +566,17 @@ static private boolean doubleIsDifferent(double d1, double d2, double delta) {
531566
return true;
532567
}
533568

569+
static private boolean floatIsDifferent(float f1, float f2, float delta) {
570+
if (Float.compare(f1, f2) == 0) {
571+
return false;
572+
}
573+
if ((Math.abs(f1 - f2) <= delta)) {
574+
return false;
575+
}
576+
577+
return true;
578+
}
579+
534580
/**
535581
* Asserts that two longs are equal. If they are not, an
536582
* {@link AssertionError} is thrown.

src/test/java/org/junit/tests/assertion/AssertionTest.java

100644100755
+12
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,27 @@ public void assertNotEqualsWorksWithPrimitiveTypes() {
619619
assertNotEquals("The values should be different", 1L, 2L);
620620
assertNotEquals(1.0, 2.0, 0);
621621
assertNotEquals("The values should be different", 1.0, 2.0, 0);
622+
assertNotEquals(1.0f, 2.0f, 0f);
623+
assertNotEquals("The values should be different", 1.0f, 2.0f, 0f);
622624
}
623625

624626
@Test(expected = AssertionError.class)
625627
public void assertNotEqualsConsidersDeltaCorrectly() {
626628
assertNotEquals(1.0, 0.9, 0.1);
627629
}
628630

631+
@Test(expected = AssertionError.class)
632+
public void assertNotEqualsConsidersFloatDeltaCorrectly() {
633+
assertNotEquals(1.0f, 0.75f, 0.25f);
634+
}
635+
629636
@Test(expected = AssertionError.class)
630637
public void assertNotEqualsIgnoresDeltaOnNaN() {
631638
assertNotEquals(Double.NaN, Double.NaN, 1);
632639
}
640+
641+
@Test(expected = AssertionError.class)
642+
public void assertNotEqualsIgnoresFloatDeltaOnNaN() {
643+
assertNotEquals(Float.NaN, Float.NaN, 1f);
644+
}
633645
}

0 commit comments

Comments
 (0)