How to compare two lists of double in JUnit
I think the right solution here would be a Custom Matcher. Basically something like IsIterableContainingInOrder that only works on Doubles and supports an error margin.
If you are willing to convert from List<Double>
to double[]
, assertArrayEquals allows specifying a tolerance for error:
assertArrayEquals(new double[] {1, 2}, new double[] {1.01, 2.09}, 1E-1);
In Java 8, the conversion from list to array is relatively clean (see related question). For example:
double[] toArray(List<Double> list) {
return list.stream().mapToDouble(Number::doubleValue).toArray();
}
And the assert statement could then be as follows:
assertArrayEquals(toArray(refList), toArray(getValues()), 1E-9);
p.s.
toArray
can be made to work with any Number
type by just changing the signature to double[] toArray(List<? extends Number> list)
.