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
The following commit(s) were added to refs/heads/master by this push: new 99f91db Updated tests for XorShiRo generators for zero and non-zero seeds. 99f91db is described below commit 99f91db095d3c6471a601677dd935e6c6de31e13 Author: Alex Herbert <aherb...@apache.org> AuthorDate: Sat May 18 15:27:08 2019 +0100 Updated tests for XorShiRo generators for zero and non-zero seeds. --- .../org/apache/commons/rng/core/RandomAssert.java | 170 ++++++++++++++++++++- .../rng/core/source32/XoRoShiRo64StarStarTest.java | 23 ++- .../rng/core/source32/XoRoShiRo64StarTest.java | 23 ++- .../rng/core/source32/XoShiRo128PlusTest.java | 22 +-- .../rng/core/source32/XoShiRo128StarStarTest.java | 22 +-- .../rng/core/source64/XoRoShiRo128PlusTest.java | 22 +-- .../core/source64/XoRoShiRo128StarStarTest.java | 22 +-- .../rng/core/source64/XoShiRo256PlusTest.java | 22 +-- .../rng/core/source64/XoShiRo256StarStarTest.java | 22 +-- .../rng/core/source64/XoShiRo512PlusTest.java | 22 +-- .../rng/core/source64/XoShiRo512StarStarTest.java | 22 +-- .../rng/core/source64/XorShift1024StarPhiTest.java | 18 ++- .../rng/core/source64/XorShift1024StarTest.java | 18 ++- 13 files changed, 299 insertions(+), 129 deletions(-) diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomAssert.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomAssert.java index 37ec3bc..a0bb6da 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomAssert.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomAssert.java @@ -19,19 +19,187 @@ package org.apache.commons.rng.core; import org.junit.Assert; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + import org.apache.commons.rng.UniformRandomProvider; +/** + * Utility class for testing random generators. + */ public class RandomAssert { - + /** + * Assert that the random generator produces the expected output. + * + * @param expected Expected output. + * @param rng Random generator. + */ public static void assertEquals(int[] expected, UniformRandomProvider rng) { for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Value at position " + i, expected[i], rng.nextInt()); } } + /** + * Assert that the random generator produces the expected output. + * + * @param expected Expected output. + * @param rng Random generator. + */ public static void assertEquals(long[] expected, UniformRandomProvider rng) { for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Value at position " + i, expected[i], rng.nextLong()); } } + + /** + * Assert that the two random generators produce the same output for + * {@link UniformRandomProvider#nextInt()} over the given number of cycles. + * + * @param cycles Number of cycles. + * @param rng1 Random generator 1. + * @param rng2 Random generator 2. + */ + public static void assertNextIntEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { + for (int i = 0; i < cycles; i++) { + Assert.assertEquals("Value at position " + i, rng1.nextInt(), rng2.nextInt()); + } + } + + /** + * Assert that the two random generators produce the same output for + * {@link UniformRandomProvider#nextLong()} over the given number of cycles. + * + * @param cycles Number of cycles. + * @param rng1 Random generator 1. + * @param rng2 Random generator 2. + */ + public static void assertNextLongEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { + for (int i = 0; i < cycles; i++) { + Assert.assertEquals("Value at position " + i, rng1.nextLong(), rng2.nextLong()); + } + } + + /** + * Assert that the random generator produces zero output for + * {@link UniformRandomProvider#nextInt()} over the given number of cycles. + * This is used to test a poorly seeded generator cannot generate random output. + * + * @param rng Random generator. + * @param cycles Number of cycles. + */ + public static void assertNextIntZeroOutput(UniformRandomProvider rng, int cycles) { + for (int i = 0; i < cycles; i++) { + Assert.assertEquals("Expected the generator to output zeros", 0, rng.nextInt()); + } + } + + /** + * Assert that the random generator produces zero output for + * {@link UniformRandomProvider#nextLong()} over the given number of cycles. + * This is used to test a poorly seeded generator cannot generate random output. + * + * @param rng Random generator. + * @param cycles Number of cycles. + */ + public static void assertNextLongZeroOutput(UniformRandomProvider rng, int cycles) { + for (int i = 0; i < cycles; i++) { + Assert.assertEquals("Expected the generator to output zeros", 0, rng.nextLong()); + } + } + + /** + * Assert that following a set number of warm-up cycles the random generator produces + * at least one non-zero output for {@link UniformRandomProvider#nextLong()} over the + * given number of test cycles. This is used to test a poorly seeded generator can recover + * internal state to generate "random" output. + * + * @param rng Random generator. + * @param warmupCycles Number of warm-up cycles. + * @param testCycles Number of test cycles. + */ + public static void assertNextLongNonZeroOutput(UniformRandomProvider rng, + int warmupCycles, int testCycles) { + for (int i = 0; i < warmupCycles; i++) { + rng.nextLong(); + } + for (int i = 0; i < testCycles; i++) { + if (rng.nextLong() != 0L) { + return; + } + } + Assert.fail("No non-zero output after " + (warmupCycles + testCycles) + " cycles"); + } + + /** + * Assert that the random generator created using an {@code int[]} seed with a + * single bit set is functional. This is tested using the + * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using + * two times the seed size as the warm-up and test cycles. + * + * @param type Class of the generator. + * @param size Seed size. + */ + public static <T extends UniformRandomProvider> void + assertIntArrayConstructorWithSingleBitSeedIsFunctional(Class<T> type, int size) { + try { + // Find the int[] constructor + final Constructor<T> constructor = type.getConstructor(int[].class); + final int[] seed = new int[size]; + for (int i = 0; i < size; i++) { + seed[i] = 1; + for (int j = 0; j < 32; j++) { + final UniformRandomProvider rng = constructor.newInstance(seed); + RandomAssert.assertNextLongNonZeroOutput(rng, 2 * size, 2 * size); + // Eventually rolls-over to reset to zero + seed[i] <<= 1; + } + Assert.assertEquals("Seed element was not reset", 0, seed[i]); + } + } catch (IllegalAccessException ex) { + Assert.fail(ex.getMessage()); + } catch (NoSuchMethodException ex) { + Assert.fail(ex.getMessage()); + } catch (InstantiationException ex) { + Assert.fail(ex.getMessage()); + } catch (InvocationTargetException ex) { + Assert.fail(ex.getMessage()); + } + } + + /** + * Assert that the random generator created using a {@code long[]} seed with a + * single bit set is functional. This is tested using the + * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using two times the seed + * size as the warm-up and test cycles. + * + * @param type Class of the generator. + * @param size Seed size. + */ + public static <T extends UniformRandomProvider> void + assertLongArrayConstructorWithSingleBitSeedIsFunctional(Class<T> type, int size) { + try { + // Find the long[] constructor + final Constructor<T> constructor = type.getConstructor(long[].class); + final long[] seed = new long[size]; + for (int i = 0; i < size; i++) { + seed[i] = 1; + for (int j = 0; j < 64; j++) { + final UniformRandomProvider rng = constructor.newInstance(seed); + RandomAssert.assertNextLongNonZeroOutput(rng, 2 * size, 2 * size); + // Eventually rolls-over to reset to zero + seed[i] <<= 1; + } + Assert.assertEquals("Seed element was not reset", 0L, seed[i]); + } + } catch (IllegalAccessException ex) { + Assert.fail(ex.getMessage()); + } catch (NoSuchMethodException ex) { + Assert.fail(ex.getMessage()); + } catch (InstantiationException ex) { + Assert.fail(ex.getMessage()); + } catch (InvocationTargetException ex) { + Assert.fail(ex.getMessage()); + } + } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarStarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarStarTest.java index 930e910..594cce3 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarStarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarStarTest.java @@ -17,11 +17,12 @@ package org.apache.commons.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; -import org.apache.commons.rng.core.util.NumberFactory; -import org.junit.Assert; import org.junit.Test; public class XoRoShiRo64StarStarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 2; + @Test public void testReferenceCode() { /* @@ -49,13 +50,13 @@ public class XoRoShiRo64StarStarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 2; - final XoRoShiRo64StarStar rng = new XoRoShiRo64StarStar(new int[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0, rng.nextInt()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64StarStar(new int[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64StarStar.class, SEED_SIZE); } @Test @@ -71,8 +72,6 @@ public class XoRoShiRo64StarStarTest { }; final XoRoShiRo64StarStar rng1 = new XoRoShiRo64StarStar(seed); final XoRoShiRo64StarStar rng2 = new XoRoShiRo64StarStar(seed[0], seed[1]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextInt(), rng2.nextInt()); - } + RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarTest.java index 645427a..7a45bab 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarTest.java @@ -17,11 +17,12 @@ package org.apache.commons.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; -import org.apache.commons.rng.core.util.NumberFactory; -import org.junit.Assert; import org.junit.Test; public class XoRoShiRo64StarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 2; + @Test public void testReferenceCode() { /* @@ -49,13 +50,13 @@ public class XoRoShiRo64StarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 2; - final XoRoShiRo64Star rng = new XoRoShiRo64Star(new int[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0, rng.nextInt()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64Star(new int[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64Star.class, SEED_SIZE); } @Test @@ -71,8 +72,6 @@ public class XoRoShiRo64StarTest { }; final XoRoShiRo64Star rng1 = new XoRoShiRo64Star(seed); final XoRoShiRo64Star rng2 = new XoRoShiRo64Star(seed[0], seed[1]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextInt(), rng2.nextInt()); - } + RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusTest.java index a265e3b..e45ac98 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo128PlusTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 4; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoShiRo128PlusTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 4; - final XoShiRo128Plus rng = new XoShiRo128Plus(new int[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0, rng.nextInt()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo128Plus(new int[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo128Plus.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoShiRo128PlusTest { }; final XoShiRo128Plus rng1 = new XoShiRo128Plus(seed); final XoShiRo128Plus rng2 = new XoShiRo128Plus(seed[0], seed[1], seed[2], seed[3]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextInt(), rng2.nextInt()); - } + RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128StarStarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128StarStarTest.java index 307c23f..ea78563 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128StarStarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128StarStarTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo128StarStarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 4; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoShiRo128StarStarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 4; - final XoShiRo128StarStar rng = new XoShiRo128StarStar(new int[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0, rng.nextInt()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo128StarStar(new int[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo128StarStar.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoShiRo128StarStarTest { }; final XoShiRo128StarStar rng1 = new XoShiRo128StarStar(seed); final XoShiRo128StarStar rng2 = new XoShiRo128StarStar(seed[0], seed[1], seed[2], seed[3]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextInt(), rng2.nextInt()); - } + RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusTest.java index 2975567..b742800 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoRoShiRo128PlusTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 2; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoRoShiRo128PlusTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 2; - final XoRoShiRo128Plus rng = new XoRoShiRo128Plus(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoRoShiRo128Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo128Plus.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoRoShiRo128PlusTest { }; final XoRoShiRo128Plus rng1 = new XoRoShiRo128Plus(seed); final XoRoShiRo128Plus rng2 = new XoRoShiRo128Plus(seed[0], seed[1]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128StarStarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128StarStarTest.java index 7da6e97..ef9cd13 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128StarStarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128StarStarTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoRoShiRo128StarStarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 2; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoRoShiRo128StarStarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 2; - final XoRoShiRo128StarStar rng = new XoRoShiRo128StarStar(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoRoShiRo128StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo128StarStar.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoRoShiRo128StarStarTest { }; final XoRoShiRo128StarStar rng1 = new XoRoShiRo128StarStar(seed); final XoRoShiRo128StarStar rng2 = new XoRoShiRo128StarStar(seed[0], seed[1]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusTest.java index f455dc7..aafa163 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo256PlusTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 4; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoShiRo256PlusTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 4; - final XoShiRo256Plus rng = new XoShiRo256Plus(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo256Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo256Plus.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoShiRo256PlusTest { }; final XoShiRo256Plus rng1 = new XoShiRo256Plus(seed); final XoShiRo256Plus rng2 = new XoShiRo256Plus(seed[0], seed[1], seed[2], seed[3]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256StarStarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256StarStarTest.java index e154c13..db6fc72 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256StarStarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256StarStarTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo256StarStarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 4; + @Test public void testReferenceCode() { /* @@ -48,13 +50,13 @@ public class XoShiRo256StarStarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 4; - final XoShiRo256StarStar rng = new XoShiRo256StarStar(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo256StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo256StarStar.class, SEED_SIZE); } @Test @@ -70,8 +72,6 @@ public class XoShiRo256StarStarTest { }; final XoShiRo256StarStar rng1 = new XoShiRo256StarStar(seed); final XoShiRo256StarStar rng2 = new XoShiRo256StarStar(seed[0], seed[1], seed[2], seed[3]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusTest.java index 8ec5ffc..4a798d6 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo512PlusTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 8; + @Test public void testReferenceCode() { /* @@ -49,13 +51,13 @@ public class XoShiRo512PlusTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 8; - final XoShiRo512Plus rng = new XoShiRo512Plus(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo512Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo512Plus.class, SEED_SIZE); } @Test @@ -73,8 +75,6 @@ public class XoShiRo512PlusTest { final XoShiRo512Plus rng1 = new XoShiRo512Plus(seed); final XoShiRo512Plus rng2 = new XoShiRo512Plus(seed[0], seed[1], seed[2], seed[3], seed[4], seed[5], seed[6], seed[7]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512StarStarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512StarStarTest.java index 22c561d..9ec79ee 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512StarStarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512StarStarTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XoShiRo512StarStarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 8; + @Test public void testReferenceCode() { /* @@ -49,13 +51,13 @@ public class XoShiRo512StarStarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 8; - final XoShiRo512StarStar rng = new XoShiRo512StarStar(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XoShiRo512StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo512StarStar.class, SEED_SIZE); } @Test @@ -73,8 +75,6 @@ public class XoShiRo512StarStarTest { final XoShiRo512StarStar rng1 = new XoShiRo512StarStar(seed); final XoShiRo512StarStar rng2 = new XoShiRo512StarStar(seed[0], seed[1], seed[2], seed[3], seed[4], seed[5], seed[6], seed[7]); - for (int i = seed.length * 2; i-- != 0; ) { - Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); - } + RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarPhiTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarPhiTest.java index b88286d..988ece4 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarPhiTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarPhiTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XorShift1024StarPhiTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 16; + @Test public void testReferenceCode() { /* @@ -51,12 +53,12 @@ public class XorShift1024StarPhiTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 16; - final XorShift1024StarPhi rng = new XorShift1024StarPhi(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XorShift1024StarPhi(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XorShift1024StarPhi.class, SEED_SIZE); } } diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarTest.java index 67fd4bb..10a8e1d 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarTest.java @@ -17,10 +17,12 @@ package org.apache.commons.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; -import org.junit.Assert; import org.junit.Test; public class XorShift1024StarTest { + /** The size of the array seed. */ + private static final int SEED_SIZE = 16; + @Test public void testReferenceCode() { /* @@ -51,12 +53,12 @@ public class XorShift1024StarTest { } @Test - public void testConstructorWithZeroSeed() { - // This is allowed even though the generator is non-functional - final int size = 16; - final XorShift1024Star rng = new XorShift1024Star(new long[size]); - for (int i = size * 2; i-- != 0; ) { - Assert.assertEquals("Expected the generator to be broken", 0L, rng.nextLong()); - } + public void testConstructorWithZeroSeedIsNonFunctional() { + RandomAssert.assertNextIntZeroOutput(new XorShift1024Star(new long[SEED_SIZE]), 2 * SEED_SIZE); + } + + @Test + public void testConstructorWithSingleBitSeedIsFunctional() { + RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XorShift1024Star.class, SEED_SIZE); } }