assertTrue(java.util.Arrays.equals(primitiveArray1, primitiveArray2));
JUnit4.3.* added the assertArrayEquals methods but surprisingly omitted a method to compare arrays of primitive doubles so, one had to fall back to the above mentioned way to compare them.
This important feature was added in JUnit4.6 so if you are using that or a later version, you can do the following:
assertArrayEquals(doubleArray1, doubleArray2, delta)where delta is the allowed tolerance.
The advantage with using the assertArrayEquals method over the previous method is that this method clearly indicates the first element which differed. For example, when the below basic test is run, it gives the error message:
arrays first differed at element [2]; expected:<2.3124> but was:<2.3224>
Here is a sample class file showing implementation of the above test:
import static org.junit.Assert.assertArrayEquals; import org.junit.Test; public class AAETest { @Test public void testAAEDouble() { double[] da1 = {1.002d, 1.234d, 2.3124d, 3.91293d}; double[] da2 = {1.003d, 1.2345d, 2.3224d, 3.91293d}; double tol = 0.001d; assertArrayEquals(da1, da2, tol); } }
No comments:
Post a Comment