You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/10/19 13:27:31 UTC

[2/3] commons-rng git commit: Utility (for unit testing) that checks whether two generators produce the same sequence.

Utility (for unit testing) that checks whether two generators produce the same sequence.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/05efd8d5
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/05efd8d5
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/05efd8d5

Branch: refs/heads/master
Commit: 05efd8d51127b5840f264183baa828fc883234e1
Parents: 3688c54
Author: Gilles <er...@apache.org>
Authored: Wed Oct 19 14:19:49 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Wed Oct 19 14:19:49 2016 +0200

----------------------------------------------------------------------
 .../org/apache/commons/rng/RandomAssert.java    | 57 ++++++++++++++++++++
 1 file changed, 57 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/05efd8d5/src/test/java/org/apache/commons/rng/RandomAssert.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/rng/RandomAssert.java b/src/test/java/org/apache/commons/rng/RandomAssert.java
index 5b08ef4..58a34c4 100644
--- a/src/test/java/org/apache/commons/rng/RandomAssert.java
+++ b/src/test/java/org/apache/commons/rng/RandomAssert.java
@@ -17,6 +17,7 @@
 
 package org.apache.commons.rng;
 
+import java.util.Arrays;
 import org.junit.Assert;
 
 public class RandomAssert {
@@ -32,4 +33,60 @@ public class RandomAssert {
             Assert.assertEquals("Value at position " + i, expected[i], rng.nextLong());
         }
     }
+
+    /**
+     * Exercise all methods from the UniformRandomProvider interface, and
+     * ensure that the two generators produce the same sequence.
+     *
+     * @param rng1 RNG.
+     * @param rng2 RNG.
+     */
+    public static void assertProduceSameSequence(UniformRandomProvider rng1,
+                                                 UniformRandomProvider rng2) {
+        for (int i = 0; i < 54; i++) {
+            Assert.assertTrue(rng1.nextBoolean() == rng2.nextBoolean());
+        }
+        for (int i = 0; i < 23; i++) {
+            Assert.assertEquals(rng1.nextInt(), rng2.nextInt());
+        }
+        for (int i = 0; i < 4; i++) {
+            for (int j = 0; j < 5; j++) {
+                final int max = 107 * i + 374 * j + 11;
+                Assert.assertEquals(rng1.nextInt(max), rng2.nextInt(max));
+            }
+        }
+        for (int i = 0; i < 23; i++) {
+            Assert.assertEquals(rng1.nextLong(), rng2.nextLong());
+        }
+        for (int i = 0; i < 4; i++) {
+            for (int j = 0; j < 5; j++) {
+                final long max = (Long.MAX_VALUE << 2) + 107 * i + 374 * j + 11;
+                Assert.assertEquals(rng1.nextLong(max), rng2.nextLong(max));
+            }
+        }
+        for (int i = 0; i < 103; i++) {
+            Assert.assertEquals(rng1.nextFloat(), rng2.nextFloat(), 0);
+        }
+        for (int i = 0; i < 79; i++) {
+            Assert.assertEquals(rng1.nextDouble(), rng2.nextDouble(), 0);
+        }
+
+        final int size = 345;
+        final byte[] a1 = new byte[size];
+        final byte[] a2 = new byte[size];
+
+        for (int i = 0; i < 3; i++) {
+            rng1.nextBytes(a1);
+            rng2.nextBytes(a2);
+            Assert.assertTrue(Arrays.equals(a1, a2));
+        }
+
+        for (int i = 0; i < 5; i++) {
+            final int offset = 200 + i;
+            final int n = 23 + i;
+            rng1.nextBytes(a1, offset, n);
+            rng2.nextBytes(a2, offset, n);
+            Assert.assertTrue(Arrays.equals(a1, a2));
+        }
+    }
 }