This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit ff9b2a8d1796dae55348d9eb2676900f31a517f7 Author: Alex Herbert <[email protected]> AuthorDate: Thu Oct 21 17:43:17 2021 +0100 Report relative and absolute error in assertion messages --- .../distribution/BaseDistributionTest.java | 6 ++-- .../commons/statistics/distribution/TestUtils.java | 40 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDistributionTest.java index fcdbee3..915a0e9 100644 --- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDistributionTest.java +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDistributionTest.java @@ -69,9 +69,6 @@ import org.junit.jupiter.params.provider.MethodSource; */ @TestInstance(Lifecycle.PER_CLASS) abstract class BaseDistributionTest<T, D extends DistributionTestData> { - /** The test data. Protected to allow use in sub-classes. */ - protected final List<D> data = new ArrayList<>(); - /** * The smallest value (epsilon) for the relative error of a {@code double}. * Set the relative error to an integer factor of this to test very @@ -87,6 +84,9 @@ abstract class BaseDistributionTest<T, D extends DistributionTestData> { */ static final double RELATIVE_EPS = Math.ulp(1.0); + /** The test data. Protected to allow use in sub-classes. */ + protected final List<D> data = new ArrayList<>(); + /** * Setup the test using data loaded from resource files. * Resource files are assumed to be named sequentially from 1: diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TestUtils.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TestUtils.java index 7cb9460..564022b 100644 --- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TestUtils.java +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TestUtils.java @@ -28,6 +28,10 @@ import org.junit.jupiter.api.Assertions; */ final class TestUtils { /** + * The relative error threshold below which absolute error is reported in ULP. + */ + private static final double ULP_THRESHOLD = 100 * Math.ulp(1.0); + /** * The prefix for the formatted expected value. * * <p>This should be followed by the expected value then '>'. @@ -41,6 +45,30 @@ final class TestUtils { * <p>This should be followed by the actual value then '>'. */ private static final String ACTUAL_FORMAT = ">, actual: <"; + /** + * The prefix for the formatted relative error value. + * + * <p>It is assumed this will be following the actual value. + * + * <p>This should be followed by the relative error value then '>'. + */ + private static final String RELATIVE_ERROR_FORMAT = ">, rel.error: <"; + /** + * The prefix for the formatted absolute error value. + * + * <p>It is assumed this will be following the relative value. + * + * <p>This should be followed by the absolute error value then '>'. + */ + private static final String ABSOLUTE_ERROR_FORMAT = ">, abs.error: <"; + /** + * The prefix for the formatted ULP error value. + * + * <p>It is assumed this will be following the relative value. + * + * <p>This should be followed by the ULP error value then '>'. + */ + private static final String ULP_ERROR_FORMAT = ">, ulp error: <"; /** * Collection of static methods used in math unit tests. @@ -135,8 +163,18 @@ final class TestUtils { * @return the formatted values */ private static String formatValues(double expected, double actual, DoubleTolerance tolerance) { + // Add error + final double diff = Math.abs(expected - actual); + final double rel = diff / Math.max(Math.abs(expected), Math.abs(actual)); final StringBuilder msg = new StringBuilder(EXPECTED_FORMAT).append(expected).append(ACTUAL_FORMAT) - .append(actual).append('>'); + .append(actual).append(RELATIVE_ERROR_FORMAT).append(rel); + if (rel < ULP_THRESHOLD) { + final long ulp = Math.abs(Double.doubleToRawLongBits(expected) - Double.doubleToRawLongBits(actual)); + msg.append(ULP_ERROR_FORMAT).append(ulp); + } else { + msg.append(ABSOLUTE_ERROR_FORMAT).append(diff); + } + msg.append('>'); appendTolerance(msg, tolerance); return msg.toString(); }
