All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.external.hamcrest.number.IsCloseTo Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.7
Show newest version
/*  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;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy