I'm writing a JUnit test for a Java variable that is ArrayList<Double>
. The complication is that my expected values are single precision whereas Java's Double
is double precision. So doing assertEquals(expected_array_list, actual_array_list)
does not pass due to precision errors.
My work-around is to iterate over all the elements and assertEquals
each pair of doubles, setting the epsilon
value to an appropriate value. Is there a method of setting the epsilon
for the entire ArrayList
, instead of having to decompose it into its elements?
JUnit 4.6 and above has a convenience method assertArrayEquals(double[] expected, double[] actual, double delta)
method (with an overloaded variant for a String
message, as per the norm). In Java 8, you could convert those List
s to primitive arrays pretty easily and just use it:
double delta = 0.05; // or any other reasonable value
double[] expectedArray =
expectedList.stream().mapToDouble(Double::doubleValue).toArray();
double[] actualArray =
actualList.stream().mapToDouble(Double::doubleValue).toArray();
assertArrayEqulas(expectedArray, actualArray, delta);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.