peter.murray.rust
peter.murray.rust

Reputation: 38063

why does junit4 not have Assert.assertArrayEquals() for double[]s?

There appear to be Assert.assertArrayEquals() methods in Junit4 for all primitives other than double, e.g.

Assert.assertArrayEquals(int[] expected, int[] actual)

and

Assert.assertArrayEquals(char[] expected, char[] actual)

but not

Assert.assertArrayEquals(double[] expected, double[] actual, double eps)

or

Assert.assertArrayEquals(double[] expected, double[] actual, double[] eps)

(the latter to account for variable ranges of doubles). Is there a fundamental reason why I should not write such a function?

Upvotes: 6

Views: 3676

Answers (4)

Daniel
Daniel

Reputation: 2298

just use:

 AssertTrue(**message**, Arrays.equals(**expected**,**result**)

You might need this to round your result values to test against expected:

 public double roundTo2Decimals(double val) {
    DecimalFormat df2 = new DecimalFormat("###.####");
    return Double.valueOf(df2.format(val));
}

See javdoc for more info

Upvotes: 0

SingleShot
SingleShot

Reputation: 19131

According to the JUnit bug database, they are "working on it". Based on other answers, it sounds like the bug database is not completely in sync with reality.

Upvotes: 1

jarnbjo
jarnbjo

Reputation: 34323

The method seem to have been added in JUnit 4.6, but is for some reason missing in 4.5 and previous versions. I wouldn't expect any problems upgrading to a newer JUnit version.

Upvotes: 7

Yishai
Yishai

Reputation: 91911

It does have such a method (in 4.7), although it is not documented on the online javadoc here. It was certainly an oversight in the javadoc/version, but it is there now.

Upvotes: 6

Related Questions