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:48 UTC

[commons-rng] 02/04: Correct isUniformNextByte 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 498610f60964b9b6bc63ef7721bf86120cc7d778
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Mar 30 12:26:24 2022 +0100

    Correct isUniformNextByte test
    
    Chi-square critical value should be for 255 degrees of freedom.
    
    This lowers the critical value from 311.56 to 310.46. The test is now
    harder to pass.
---
 .../rng/core/ProvidersCommonParametricTest.java    | 26 +++++++++++-----------
 1 file changed, 13 insertions(+), 13 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 4d3d3a0..4c7b54c 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
@@ -122,9 +122,13 @@ class ProvidersCommonParametricTest {
         // Test should pass for the part of the buffer where values are put.
         Assertions.assertTrue(isUniformNextBytes(buffer, offset, offset + size, nextMethod), generator::toString);
 
-        // Test must fail for the parts of the buffer where no values are put.
-        Assertions.assertFalse(isUniformNextBytes(buffer, 0, offset, nextMethod));
-        Assertions.assertFalse(isUniformNextBytes(buffer, offset + size, buffer.length, nextMethod));
+        // The parts of the buffer where no values are put should be zero.
+        for (int i = 0; i < offset; i++) {
+            Assertions.assertEquals(0, buffer[i], generator::toString);
+        }
+        for (int i = offset + size; i < totalSize; i++) {
+            Assertions.assertEquals(0, buffer[i], generator::toString);
+        }
     }
 
     @ParameterizedTest
@@ -299,27 +303,23 @@ class ProvidersCommonParametricTest {
 
         // Number of possible values (do not change).
         final int byteRange = 256;
-        // Chi-square critical value with 256 degrees of freedom
+        // Chi-square critical value with 255 degrees of freedom
         // and 1% significance level.
-        final double chi2CriticalValue = 311.560343;
-        // To transform a byte value into its bin index.
-        final int byteRangeOffset = 128;
+        final double chi2CriticalValue = 310.45738821990585;
 
         // Bins.
         final long[] observed = new long[byteRange];
         final double[] expected = new double[byteRange];
 
-        for (int i = 0; i < byteRange; i++) {
-            expected[i] = sampleSize * (last - first) / (double) byteRange;
-        }
+        Arrays.fill(expected, sampleSize * (last - first) / (double) byteRange);
 
         try {
             for (int k = 0; k < sampleSize; k++) {
                 nextMethod.run();
 
                 for (int i = first; i < last; i++) {
-                    final byte b = buffer[i];
-                    ++observed[b + byteRangeOffset];
+                    // Convert byte to an index in [0, 255]
+                    ++observed[buffer[i] & 0xff];
                 }
             }
         } catch (Exception e) {
@@ -335,7 +335,7 @@ class ProvidersCommonParametricTest {
         }
 
         // Statistics check.
-        return chi2 < chi2CriticalValue;
+        return chi2 <= chi2CriticalValue;
     }
 
     /**