简体   繁体   English

为什么junit4没有用于double [] s的Assert.assertArrayEquals()?

[英]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, eg Junit4中似乎存在Assert.assertArrayEquals()方法,用于除double之外的所有基元,例如

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? 有没有一个根本原因我不应该写这样的功能?

The method seem to have been added in JUnit 4.6, but is for some reason missing in 4.5 and previous versions. 该方法似乎已在JUnit 4.6中添加,但由于某些原因在4.5及以前的版本中缺失。 I wouldn't expect any problems upgrading to a newer JUnit version. 我不希望升级到更新的JUnit版本有任何问题。

It does have such a method (in 4.7), although it is not documented on the online javadoc here . 确实有这样的方法(4.7),虽然它没有在网上的Javadoc文档在这里 It was certainly an oversight in the javadoc/version, but it is there now. 这肯定是javadoc /版本的疏忽,但它现在就在那里。

According to the JUnit bug database, they are " working on it ". 根据JUnit错误数据库,他们正在“ 努力 ”。 Based on other answers, it sounds like the bug database is not completely in sync with reality. 根据其他答案,听起来像错误数据库并不完全与现实同步。

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 有关更多信息,请参阅javdoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM