double array matcher

27 views
Skip to first unread message

Nathan Glenn

unread,
Jan 29, 2015, 6:20:17 AM1/29/15
to hamcre...@googlegroups.com

I wrote this matcher to check the contents of a double[]:

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 IsArray(matchers.toArray(new Matcher[matchers.size()]));
}

The method looks fine, but it always fails:

assertThat(new double[] { .1 }, isArrayCloseTo(new double[] { .1 })); //fails

I see elsewhere on the list that a matcher for double arrays has been provided, but I would like to know for future reference what is wrong with what I'm doing so that I won't repeat it again later.

Thanks for your time!

Nate

Reply all
Reply to author
Forward
0 new messages