/** * Asserts that the actual value is not close to the expected one by less than the given offset. * * @param info contains information about the assertion. * @param actual the actual value. * @param expected the value to compare actual too. * @param offset the given positive offset. */ public void assertIsNotCloseTo(final AssertionInfo info, final NUMBER actual, final NUMBER expected, final Offset<NUMBER> offset) { assertNotNull(info, actual); checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); NUMBER diff = absDiff(actual, expected); // with strict offset and actual == other => too close ! if (offset.strict && isGreaterThanOrEqualTo(diff, offset.value)) return; // with non strict offset and actual == other => too close ! if (!offset.strict && !areEqual(actual, expected)) { if (isGreaterThan(diff, offset.value)) return; } throw failures.failure(info, shouldNotBeEqual(actual, expected, offset, diff)); }
/** * Asserts that the actual value is not close to the expected one by less than the given offset. * * @param info contains information about the assertion. * @param actual the actual value. * @param expected the value to compare actual too. * @param offset the given positive offset. */ public void assertIsNotCloseTo(final AssertionInfo info, final NUMBER actual, final NUMBER expected, final Offset<NUMBER> offset) { assertNotNull(info, actual); checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); NUMBER diff = absDiff(actual, expected); // with strict offset and actual == other => too close ! if (offset.strict && isGreaterThanOrEqualTo(diff, offset.value)) return; // with non strict offset and actual == other => too close ! if (!offset.strict && !areEqual(actual, expected)) { if (isGreaterThan(diff, offset.value)) return; } throw failures.failure(info, shouldNotBeEqual(actual, expected, offset, diff)); }
/** * Asserts that the actual value is close to the expected one by less than the given offset. * <p> * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific * comparison strategy. * * @param info contains information about the assertion. * @param actual the actual value. * @param expected the value to compare actual too. * @param offset the given positive offset. */ public void assertIsCloseTo(final AssertionInfo info, final NUMBER actual, final NUMBER expected, final Offset<NUMBER> offset) { assertNotNull(info, actual); checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); if (areEqual(actual, expected)) return; // handles correctly NaN comparison if (!offset.strict && isGreaterThan(absDiff(actual, expected), offset.value)) throw failures.failure(info, shouldBeEqual(actual, expected, offset, absDiff(actual, expected))); if (offset.strict && isGreaterThanOrEqualTo(absDiff(actual, expected), offset.value)) throw failures.failure(info, shouldBeEqual(actual, expected, offset, absDiff(actual, expected))); }
/** * Asserts that the actual value is close to the expected one by less than the given offset. * <p> * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific * comparison strategy. * * @param info contains information about the assertion. * @param actual the actual value. * @param expected the value to compare actual too. * @param offset the given positive offset. */ public void assertIsCloseTo(final AssertionInfo info, final NUMBER actual, final NUMBER expected, final Offset<NUMBER> offset) { assertNotNull(info, actual); checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); if (areEqual(actual, expected)) return; // handles correctly NaN comparison if (!offset.strict && isGreaterThan(absDiff(actual, expected), offset.value)) throw failures.failure(info, shouldBeEqual(actual, expected, offset, absDiff(actual, expected))); if (offset.strict && isGreaterThanOrEqualTo(absDiff(actual, expected), offset.value)) throw failures.failure(info, shouldBeEqual(actual, expected, offset, absDiff(actual, expected))); }
/** * Verifies that two floats are equal within a positive offset.<br> * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific comparison * strategy. * @param info contains information about the assertion. * @param actual the actual value. * @param expected the expected value. * @param offset the given positive offset. * @throws NullPointerException if the given offset is {@code null}. * @throws AssertionError if the actual value is not equal to the expected one. */ // can't be defined in RealNumbers because Offset parameter must inherits from Number // while RealNumber parameter must inherits from Comparable (sadly Number is not Comparable) public void assertEqual(AssertionInfo info, Float actual, Float expected, Offset<Float> offset) { assertNotNull(info, actual); checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); // doesn't use areEqual method relying on comparisonStrategy attribute if (Objects.areEqual(actual, expected)) return; if (isEqualTo(actual, expected, offset)) return; throw failures.failure(info, shouldBeEqual(actual, expected, offset, abs(expected - actual))); }
/** * Verifies that two floats are equal within a positive offset.<br> * It does not rely on the custom comparisonStrategy (if one is set) because using an offset is already a specific comparison * strategy. * @param info contains information about the assertion. * @param actual the actual value. * @param expected the expected value. * @param offset the given positive offset. * @throws NullPointerException if the given offset is {@code null}. * @throws AssertionError if the actual value is {@code null}. * @throws AssertionError if the actual value is not equal to the expected one. */ // can't be defined in RealNumbers because Offset parameter must inherits from Number // while RealNumber parameter must inherits from Comparable (sadly Number is not Comparable) public void assertEqual(AssertionInfo info, Double actual, Double expected, Offset<Double> offset) { checkOffsetIsNotNull(offset); checkNumberIsNotNull(expected); assertNotNull(info, actual); // doesn't use areEqual method relying on comparisonStrategy attribute if (Objects.areEqual(actual, expected)) return; if (isEqualTo(actual, expected, offset)) return; throw failures.failure(info, shouldBeEqual(actual, expected, offset, abs(expected - actual))); }