mockit.external.hamcrest.number.IsCloseTo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for developer (unit/integration) testing.
It contains mocking APIs and other tools, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/* Copyright (c) 2000-2006 hamcrest.org
*/
package mockit.external.hamcrest.number;
import mockit.external.hamcrest.*;
/**
* Decides if the value of a number is equal to a value within some range of acceptable error.
*/
public final class IsCloseTo extends TypeSafeMatcher
{
private final double delta;
private final double value;
public IsCloseTo(double value, double error)
{
delta = error;
this.value = value;
}
@Override
public boolean matchesSafely(Number item)
{
return actualDelta(item) <= 0.0;
}
@Override
public void describeMismatchSafely(Number item, Description description)
{
description.appendValue(item).appendText(" differed by ").appendValue(actualDelta(item));
}
public void describeTo(Description description)
{
description.appendText("a numeric value within ").appendValue(delta).appendText(" of ")
.appendValue(value);
}
private double actualDelta(Number item)
{
return Math.abs(item.doubleValue() - value) - delta;
}
}