You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/03/30 11:41:47 UTC

[commons-rng] 01/04: Consistent error messages for nextInRange test

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-rng.git

commit 587a39b33c80832016e672e196ed4b3e248b22e3
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Mar 30 12:06:45 2022 +0100

    Consistent error messages for nextInRange test
    
    Port modifications from sampling ContinuousSamplerParametricTest to the
    ProvidersCommonParametricTest in core and simple modules.
    
    Updated the precision of the chi-square critical value.
    
    Add failed chi-square values to the message.
    
    Format failed chi-square values to 3 decimal places.
---
 .../rng/core/ProvidersCommonParametricTest.java    | 23 +++++++++++++++-------
 .../ContinuousSamplerParametricTest.java           |  6 ++++--
 .../rng/simple/ProvidersCommonParametricTest.java  | 22 +++++++++++++++------
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersCommonParametricTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersCommonParametricTest.java
index 6935b90..4d3d3a0 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersCommonParametricTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersCommonParametricTest.java
@@ -20,7 +20,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.concurrent.Callable;
-
+import java.util.stream.Collectors;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
@@ -498,21 +498,25 @@ class ProvidersCommonParametricTest {
         final int[] observed = new int[numBins];
         // Chi-square critical value with 9 degrees of freedom
         // and 1% significance level.
-        final double chi2CriticalValue = 21.67;
+        final double chi2CriticalValue = 21.665994333461924;
 
+        // For storing chi2 larger than the critical value.
+        final List<Double> failedStat = new ArrayList<>();
         try {
+            final int lastDecileIndex = numBins - 1;
             for (int i = 0; i < numTests; i++) {
                 Arrays.fill(observed, 0);
-                for (int j = 0; j < sampleSize; j++) {
+                SAMPLE: for (int j = 0; j < sampleSize; j++) {
                     final long value = nextMethod.call().longValue();
                     Assertions.assertTrue(value >= 0 && value < n, "Range");
 
-                    for (int k = 0; k < numBins; k++) {
+                    for (int k = 0; k < lastDecileIndex; k++) {
                         if (value < binUpperBounds[k]) {
                             ++observed[k];
-                            break;
+                            continue SAMPLE;
                         }
                     }
+                    ++observed[lastDecileIndex];
                 }
 
                 // Compute chi-square.
@@ -524,6 +528,7 @@ class ProvidersCommonParametricTest {
 
                 // Statistics check.
                 if (chi2 > chi2CriticalValue) {
+                    failedStat.add(chi2);
                     ++numFailures;
                 }
             }
@@ -541,8 +546,12 @@ class ProvidersCommonParametricTest {
         // 12    0.00190
 
         if (numFailures > 11) { // Test will fail with 0.5% probability
-            Assertions.fail(generator + ": Too many failures for n = " + n +
-                            " (" + numFailures + " out of " + numTests + " tests failed)");
+            Assertions.fail(String.format(
+                "%s: Too many failures for n = %d, sample size = %d " +
+                "(%d out of %d tests failed, chi2 > %.3f=%s)",
+                generator, n, sampleSize, numFailures, numTests, chi2CriticalValue,
+                failedStat.stream().map(d -> String.format("%.3f", d))
+                          .collect(Collectors.joining(", ", "[", "]"))));
         }
     }
 
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplerParametricTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplerParametricTest.java
index baa528f..751210f 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplerParametricTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplerParametricTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.rng.sampling.distribution;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 import java.util.ArrayList;
 
 import org.junit.jupiter.api.Assertions;
@@ -114,9 +115,10 @@ class ContinuousSamplerParametricTest {
         if (numFailures > 3) { // Test will fail with 0.16% probability
             Assertions.fail(String.format(
                     "%s: Too many failures for sample size = %d " +
-                    " (%d out of %d tests failed, chi2 > %.3f=%s)",
+                    "(%d out of %d tests failed, chi2 > %.3f=%s)",
                     sampler, sampleSize, numFailures, numTests, chi2CriticalValue,
-                    Arrays.toString(failedStat.toArray(new Double[0]))));
+                    failedStat.stream().map(d -> String.format("%.3f", d))
+                              .collect(Collectors.joining(", ", "[", "]"))));
         }
     }
 }
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
index c4d95cc..55c6070 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.concurrent.Callable;
+import java.util.stream.Collectors;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
 import java.io.ObjectInputStream;
@@ -440,21 +441,25 @@ class ProvidersCommonParametricTest {
         final int[] observed = new int[numBins];
         // Chi-square critical value with 9 degrees of freedom
         // and 1% significance level.
-        final double chi2CriticalValue = 21.67;
+        final double chi2CriticalValue = 21.665994333461924;
 
+        // For storing chi2 larger than the critical value.
+        final List<Double> failedStat = new ArrayList<>();
         try {
+            final int lastDecileIndex = numBins - 1;
             for (int i = 0; i < numTests; i++) {
                 Arrays.fill(observed, 0);
-                for (int j = 0; j < sampleSize; j++) {
+                SAMPLE: for (int j = 0; j < sampleSize; j++) {
                     final long value = nextMethod.call().longValue();
                     Assertions.assertTrue(value >= 0 && value < n, "Range");
 
-                    for (int k = 0; k < numBins; k++) {
+                    for (int k = 0; k < lastDecileIndex; k++) {
                         if (value < binUpperBounds[k]) {
                             ++observed[k];
-                            break;
+                            continue SAMPLE;
                         }
                     }
+                    ++observed[lastDecileIndex];
                 }
 
                 // Compute chi-square.
@@ -466,6 +471,7 @@ class ProvidersCommonParametricTest {
 
                 // Statistics check.
                 if (chi2 > chi2CriticalValue) {
+                    failedStat.add(chi2);
                     ++numFailures;
                 }
             }
@@ -483,8 +489,12 @@ class ProvidersCommonParametricTest {
         // 12    0.00190
 
         if (numFailures > 11) { // Test will fail with 0.5% probability
-            Assertions.fail(generator + ": Too many failures for n = " + n +
-                            " (" + numFailures + " out of " + numTests + " tests failed)");
+            Assertions.fail(String.format(
+                    "%s: Too many failures for n = %d, sample size = %d " +
+                    "(%d out of %d tests failed, chi2 > %.3f=%s)",
+                    generator, n, sampleSize, numFailures, numTests, chi2CriticalValue,
+                    failedStat.stream().map(d -> String.format("%.3f", d))
+                              .collect(Collectors.joining(", ", "[", "]"))));
         }
     }