/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * <p/> * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.lang.Double> closeTo(double operand, double error) { return org.hamcrest.number.IsCloseTo.closeTo(operand, error); }
/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<Double> closeTo(double operand, double error) { return new IsCloseTo(operand, error); } }
@Override public boolean matchesSafely(Double item) { return actualDelta(item) <= 0.0; }
/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.lang.Double> closeTo(double operand, double error) { return org.hamcrest.number.IsCloseTo.closeTo(operand, error); }
/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * <p/> * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ @Factory public static Matcher<Double> closeTo(double operand, double error) { return new IsCloseTo(operand, error); } }
@Override public boolean matchesSafely(Double item) { return actualDelta(item) <= 0.0; }
@Override protected Matcher<?> createMatcher() { final double irrelevant = 0.1; return closeTo(irrelevant, irrelevant); }
/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<Double> closeTo(double operand, double error) { return new IsCloseTo(operand, error); } }
@Override public void describeMismatchSafely(Double item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)); }
@Test public void test_compute_withDailyHabit() { int check = 1; double freq = 1.0; assertThat(compute(freq, 0, check), closeTo(0.051922, E)); assertThat(compute(freq, 0.5, check), closeTo(0.525961, E)); assertThat(compute(freq, 0.75, check), closeTo(0.762981, E)); check = 0; assertThat(compute(freq, 0, check), closeTo(0, E)); assertThat(compute(freq, 0.5, check), closeTo(0.474039, E)); assertThat(compute(freq, 0.75, check), closeTo(0.711058, E)); }
/** * Creates a matcher of {@link Double}s that matches when an examined double is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. * For example: * <pre>assertThat(1.03, is(closeTo(1.0, 0.03)))</pre> * * @param operand * the expected value of matching doubles * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<Double> closeTo(double operand, double error) { return new IsCloseTo(operand, error); } }
@Override public void describeMismatchSafely(Double item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)) .appendText(" more than delta ") .appendValue(delta); }
@Test public void test_compute_withNonDailyHabit() { int check = 1; double freq = 1 / 3.0; assertThat(compute(freq, 0, check), closeTo(0.017616, E)); assertThat(compute(freq, 0.5, check), closeTo(0.508808, E)); assertThat(compute(freq, 0.75, check), closeTo(0.754404, E)); check = 0; assertThat(compute(freq, 0, check), closeTo(0.0, E)); assertThat(compute(freq, 0.5, check), closeTo(0.491192, E)); assertThat(compute(freq, 0.75, check), closeTo(0.736788, E)); }
@Factory public static Matcher<Double> closeTo(double operand, double error) { return new IsCloseTo(operand, error); }
@Override public boolean matchesSafely(Double item) { return actualDelta(item) <= 0.0; }
@Test public void test_groupBy() { Habit habit = fixtures.createLongHabit(); List<Score> list = habit.getScores().groupBy(DateUtils.TruncateField.MONTH); assertThat(list.size(), equalTo(5)); assertThat(list.get(0).getValue(), closeTo(0.653659, E)); assertThat(list.get(1).getValue(), closeTo(0.622715, E)); assertThat(list.get(2).getValue(), closeTo(0.520997, E)); }
@SuppressWarnings({"rawtypes", "unchecked"}) public static Matcher<double[]> isArrayCloseTo(double[] expected) { final double DELTA = 1e-10; List<Matcher<Double>> matchers = new ArrayList<>(); for (double d : expected) matchers.add(new IsCloseTo(d, DELTA)); return new CustomTypeSafeMatcher<double[]>("array that is close to" + Arrays.toString(expected)) { @Override protected boolean matchesSafely(double[] actual) { return new IsArray<Double>(matchers.toArray(new Matcher[matchers.size()])) .matchesSafely(Arrays.stream(actual).boxed().toArray(Double[]::new)); } }; }
@Override public void describeMismatchSafely(Double item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)) .appendText(" more than delta ") .appendValue(delta); }
@Test public void test_getTodayValue() { toggleRepetitions(0, 20); double actual = habit.getScores().getTodayValue(); assertThat(actual, closeTo(0.655747, E)); }
@Override public boolean matchesSafely(Double item) { return actualDelta(item) <= 0.0; }