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 2021/08/17 07:08:13 UTC
[commons-rng] 05/13: RNG-163: Update parametric tests to JUnit 5
ParameterizedTest
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 ad27ea7db0e20c2028cb38575e0b5a63e1f95cc4
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Aug 15 21:24:51 2021 +0100
RNG-163: Update parametric tests to JUnit 5 ParameterizedTest
---
.../rng/core/JumpableProvidersParametricTest.java | 121 +++++-------
.../rng/core/Providers32ParametricTest.java | 27 +--
.../rng/core/Providers64ParametricTest.java | 28 +--
.../rng/core/ProvidersCommonParametricTest.java | 212 ++++++++++-----------
.../org/apache/commons/rng/core/ProvidersList.java | 140 ++++++--------
.../ContinuousSamplerParametricTest.java | 31 +--
.../distribution/ContinuousSamplersList.java | 23 ++-
.../DiscreteSamplerParametricTest.java | 41 +---
.../distribution/DiscreteSamplersList.java | 29 ++-
.../rng/simple/ProvidersCommonParametricTest.java | 187 ++++++++++--------
.../apache/commons/rng/simple/ProvidersList.java | 39 +++-
.../internal/NativeSeedTypeParametricTest.java | 102 +++++-----
.../RandomSourceInternalParametricTest.java | 72 ++++---
13 files changed, 485 insertions(+), 567 deletions(-)
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/JumpableProvidersParametricTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/JumpableProvidersParametricTest.java
index 4793b42..9490712 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/JumpableProvidersParametricTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/JumpableProvidersParametricTest.java
@@ -18,10 +18,8 @@ package org.apache.commons.rng.core;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
@@ -36,7 +34,6 @@ import org.apache.commons.rng.core.source64.LongProvider;
/**
* Tests which all {@link JumpableUniformRandomProvider} generators must pass.
*/
-@RunWith(value = Parameterized.class)
public class JumpableProvidersParametricTest {
/** The size of the state for the IntProvider. */
private static final int INT_PROVIDER_STATE_SIZE;
@@ -48,81 +45,54 @@ public class JumpableProvidersParametricTest {
LONG_PROVIDER_STATE_SIZE = new State64Generator().getStateSize();
}
- /** RNG under test. */
- private final JumpableUniformRandomProvider generator;
-
- /**
- * Initializes generator instance.
- *
- * @param rng RNG to be tested.
- */
- public JumpableProvidersParametricTest(JumpableUniformRandomProvider rng) {
- generator = rng;
- }
-
/**
* Gets the list of Jumpable generators.
*
* @return the list
*/
- @Parameters(name = "{index}: data={0}")
- public static Iterable<JumpableUniformRandomProvider[]> getList() {
+ private static Iterable<JumpableUniformRandomProvider> getJumpableProviders() {
return ProvidersList.listJumpable();
}
/**
- * Gets the function using the {@link JumpableUniformRandomProvider#jump()} method.
- *
- * @return the jump function
- */
- private TestJumpFunction getJumpFunction() {
- return new TestJumpFunction() {
- @Override
- public UniformRandomProvider jump() {
- return generator.jump();
- }
- };
- }
-
- /**
* Gets the function using the {@link LongJumpableUniformRandomProvider#longJump()} method.
+ * If the RNG is not long jumpable then this will raise an exception to skip the test.
*
+ * @param generator RNG under test.
* @return the jump function
*/
- private TestJumpFunction getLongJumpFunction() {
+ private static TestJumpFunction getLongJumpFunction(JumpableUniformRandomProvider generator) {
Assumptions.assumeTrue(generator instanceof LongJumpableUniformRandomProvider, "No long jump function");
-
- final LongJumpableUniformRandomProvider rng = (LongJumpableUniformRandomProvider) generator;
- return new TestJumpFunction() {
- @Override
- public UniformRandomProvider jump() {
- return rng.longJump();
- }
- };
+ final LongJumpableUniformRandomProvider rng2 = (LongJumpableUniformRandomProvider) generator;
+ return rng2::jump;
}
/**
* Test that the random generator returned from the jump is a new instance of the same class.
*/
- @Test
- public void testJumpReturnsACopy() {
- assertJumpReturnsACopy(getJumpFunction());
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testJumpReturnsACopy(JumpableUniformRandomProvider generator) {
+ assertJumpReturnsACopy(generator::jump, generator);
}
/**
* Test that the random generator returned from the long jump is a new instance of the same class.
*/
- @Test
- public void testLongJumpReturnsACopy() {
- assertJumpReturnsACopy(getLongJumpFunction());
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testLongJumpReturnsACopy(JumpableUniformRandomProvider generator) {
+ assertJumpReturnsACopy(getLongJumpFunction(generator), generator);
}
/**
* Assert that the random generator returned from the jump function is a new instance of the same class.
*
* @param jumpFunction Jump function to test.
+ * @param generator RNG under test.
*/
- private void assertJumpReturnsACopy(TestJumpFunction jumpFunction) {
+ private static void assertJumpReturnsACopy(TestJumpFunction jumpFunction,
+ JumpableUniformRandomProvider generator) {
final UniformRandomProvider copy = jumpFunction.jump();
Assertions.assertNotSame(generator, copy, "The copy instance should be a different object");
Assertions.assertEquals(generator.getClass(), copy.getClass(), "The copy instance should be the same class");
@@ -132,18 +102,20 @@ public class JumpableProvidersParametricTest {
* Test that the random generator state of the copy instance returned from the jump
* matches the input state.
*/
- @Test
- public void testJumpCopyMatchesPreJumpState() {
- assertCopyMatchesPreJumpState(getJumpFunction());
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testJumpCopyMatchesPreJumpState(JumpableUniformRandomProvider generator) {
+ assertCopyMatchesPreJumpState(generator::jump, generator);
}
/**
* Test that the random generator state of the copy instance returned from the long jump
* matches the input state.
*/
- @Test
- public void testLongJumpCopyMatchesPreJumpState() {
- assertCopyMatchesPreJumpState(getLongJumpFunction());
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testLongJumpCopyMatchesPreJumpState(JumpableUniformRandomProvider generator) {
+ assertCopyMatchesPreJumpState(getLongJumpFunction(generator), generator);
}
/**
@@ -163,8 +135,10 @@ public class JumpableProvidersParametricTest {
* nextBoolean() and nextInt() functions.</p>
*
* @param jumpFunction Jump function to test.
+ * @param generator RNG under test.
*/
- private void assertCopyMatchesPreJumpState(TestJumpFunction jumpFunction) {
+ private static void assertCopyMatchesPreJumpState(TestJumpFunction jumpFunction,
+ JumpableUniformRandomProvider generator) {
Assumptions.assumeTrue(generator instanceof RestorableUniformRandomProvider, "Not a restorable RNG");
for (int repeats = 0; repeats < 2; repeats++) {
@@ -192,26 +166,20 @@ public class JumpableProvidersParametricTest {
* Test that a jump resets the state of the default implementation of a generator in
* {@link IntProvider} and {@link LongProvider}.
*/
- @Test
- public void testJumpResetsDefaultState() {
- if (generator instanceof IntProvider) {
- assertJumpResetsDefaultState(getJumpFunction(), INT_PROVIDER_STATE_SIZE);
- } else if (generator instanceof LongProvider) {
- assertJumpResetsDefaultState(getJumpFunction(), LONG_PROVIDER_STATE_SIZE);
- }
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testJumpResetsDefaultState(JumpableUniformRandomProvider generator) {
+ assertJumpResetsDefaultState(generator::jump, generator);
}
/**
* Test that a long jump resets the state of the default implementation of a generator in
* {@link IntProvider} and {@link LongProvider}.
*/
- @Test
- public void testLongJumpResetsDefaultState() {
- if (generator instanceof IntProvider) {
- assertJumpResetsDefaultState(getLongJumpFunction(), INT_PROVIDER_STATE_SIZE);
- } else if (generator instanceof LongProvider) {
- assertJumpResetsDefaultState(getLongJumpFunction(), LONG_PROVIDER_STATE_SIZE);
- }
+ @ParameterizedTest
+ @MethodSource("getJumpableProviders")
+ public void testLongJumpResetsDefaultState(JumpableUniformRandomProvider generator) {
+ assertJumpResetsDefaultState(getLongJumpFunction(generator), generator);
}
/**
@@ -222,9 +190,18 @@ public class JumpableProvidersParametricTest {
* {@link IntProvider} and {@link LongProvider} is reset.</p>
*
* @param jumpFunction Jump function to test.
- * @param stateSize State size.
+ * @param generator RNG under test.
*/
- private void assertJumpResetsDefaultState(TestJumpFunction jumpFunction, int stateSize) {
+ private static void assertJumpResetsDefaultState(TestJumpFunction jumpFunction,
+ JumpableUniformRandomProvider generator) {
+ int stateSize;
+ if (generator instanceof IntProvider) {
+ stateSize = INT_PROVIDER_STATE_SIZE;
+ } else if (generator instanceof LongProvider) {
+ stateSize = LONG_PROVIDER_STATE_SIZE;
+ } else {
+ throw new AssertionError("Unsupported RNG");
+ }
final byte[] expected = new byte[stateSize];
for (int repeats = 0; repeats < 2; repeats++) {
// Exercise the generator.
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers32ParametricTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers32ParametricTest.java
index 319400a..81d8897 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers32ParametricTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers32ParametricTest.java
@@ -16,37 +16,22 @@
*/
package org.apache.commons.rng.core;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.apache.commons.rng.RestorableUniformRandomProvider;
/**
* Tests which all 32-bits based generators must pass.
*/
-@RunWith(value = Parameterized.class)
public class Providers32ParametricTest {
- /** RNG under test. */
- private final RestorableUniformRandomProvider generator;
-
- /**
- * Initializes generator instance.
- *
- * @param rng RNG to be tested.
- */
- public Providers32ParametricTest(RestorableUniformRandomProvider rng) {
- generator = rng;
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<RestorableUniformRandomProvider[]> getList() {
+ private static Iterable<RestorableUniformRandomProvider> getList() {
return ProvidersList.list32();
}
- @Test
- public void testNextBytesChunks() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testNextBytesChunks(RestorableUniformRandomProvider generator) {
final int[] chunkSizes = {4, 8, 12, 16};
final int[] chunks = {1, 2, 3, 4, 5};
for (int chunkSize : chunkSizes) {
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers64ParametricTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers64ParametricTest.java
index 61a1d35..945c0fb 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers64ParametricTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers64ParametricTest.java
@@ -16,37 +16,21 @@
*/
package org.apache.commons.rng.core;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.apache.commons.rng.RestorableUniformRandomProvider;
/**
* Tests which all 64-bits based generators must pass.
*/
-@RunWith(value = Parameterized.class)
public class Providers64ParametricTest {
- /** RNG under test. */
- private final RestorableUniformRandomProvider generator;
-
- /**
- * Initializes generator instance.
- *
- * @param rng RNG to be tested.
- */
- public Providers64ParametricTest(RestorableUniformRandomProvider rng) {
- generator = rng;
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<RestorableUniformRandomProvider[]> getList() {
+ private static Iterable<RestorableUniformRandomProvider> getList() {
return ProvidersList.list64();
}
- @Test
- public void testNextBytesChunks() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testNextBytesChunks(RestorableUniformRandomProvider generator) {
final int[] chunkSizes = {8, 16, 24};
final int[] chunks = {1, 2, 3, 4, 5};
for (int chunkSize : chunkSizes) {
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 696b765..032f34d 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
@@ -22,10 +22,8 @@ import java.util.ArrayList;
import java.util.concurrent.Callable;
import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.RestorableUniformRandomProvider;
@@ -34,42 +32,30 @@ import org.apache.commons.rng.RandomProviderState;
/**
* Tests which all generators must pass.
*/
-@RunWith(value = Parameterized.class)
public class ProvidersCommonParametricTest {
- /** RNG under test. */
- private final RestorableUniformRandomProvider generator;
-
- /**
- * Initializes generator instance.
- *
- * @param rng RNG to be tested.
- */
- public ProvidersCommonParametricTest(RestorableUniformRandomProvider rng) {
- generator = rng;
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<RestorableUniformRandomProvider[]> getList() {
+ private static Iterable<RestorableUniformRandomProvider> getList() {
return ProvidersList.list();
}
-
// Precondition tests
- @Test
- public void testPreconditionNextInt() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testPreconditionNextInt(UniformRandomProvider generator) {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(0));
}
- @Test
- public void testPreconditionNextLong() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testPreconditionNextLong(UniformRandomProvider generator) {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(0));
}
- @Test
- public void testPreconditionNextBytes() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testPreconditionNextBytes(UniformRandomProvider generator) {
final int size = 10;
final int num = 1;
final byte[] buf = new byte[size];
@@ -80,11 +66,11 @@ public class ProvidersCommonParametricTest {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, -1));
}
-
// Uniformity tests
- @Test
- public void testUniformNextBytesFullBuffer() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextBytesFullBuffer(UniformRandomProvider generator) {
// Value chosen to exercise all the code lines in the
// "nextBytes" methods.
final int size = 23;
@@ -97,11 +83,12 @@ public class ProvidersCommonParametricTest {
}
};
- Assertions.assertTrue(isUniformNextBytes(buffer, 0, size, nextMethod));
+ Assertions.assertTrue(isUniformNextBytes(buffer, 0, size, nextMethod), generator::toString);
}
- @Test
- public void testUniformNextBytesPartialBuffer() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextBytesPartialBuffer(UniformRandomProvider generator) {
final int totalSize = 1234;
final int offset = 567;
final int size = 89;
@@ -116,52 +103,57 @@ public class ProvidersCommonParametricTest {
};
// Test should pass for the part of the buffer where values are put.
- Assertions.assertTrue(isUniformNextBytes(buffer, offset, offset + size, nextMethod));
+ 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));
}
- @Test
- public void testUniformNextIntegerInRange() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextIntegerInRange(UniformRandomProvider generator) {
// Statistical test uses 10 bins so tests are invalid below this level
- checkNextIntegerInRange(10, 1000);
- checkNextIntegerInRange(12, 1000);
- checkNextIntegerInRange(31, 1000);
- checkNextIntegerInRange(32, 1000);
- checkNextIntegerInRange(2016128993, 1000);
- checkNextIntegerInRange(1834691456, 1000);
- checkNextIntegerInRange(869657561, 1000);
- checkNextIntegerInRange(1570504788, 1000);
+ checkNextIntegerInRange(generator, 10, 1000);
+ checkNextIntegerInRange(generator, 12, 1000);
+ checkNextIntegerInRange(generator, 31, 1000);
+ checkNextIntegerInRange(generator, 32, 1000);
+ checkNextIntegerInRange(generator, 2016128993, 1000);
+ checkNextIntegerInRange(generator, 1834691456, 1000);
+ checkNextIntegerInRange(generator, 869657561, 1000);
+ checkNextIntegerInRange(generator, 1570504788, 1000);
}
- @Test
- public void testUniformNextLongInRange() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextLongInRange(UniformRandomProvider generator) {
// Statistical test uses 10 bins so tests are invalid below this level
- checkNextLongInRange(11, 1000);
- checkNextLongInRange(19, 1000);
- checkNextLongInRange(31, 1000);
- checkNextLongInRange(32, 1000);
+ checkNextLongInRange(generator, 11, 1000);
+ checkNextLongInRange(generator, 19, 1000);
+ checkNextLongInRange(generator, 31, 1000);
+ checkNextLongInRange(generator, 32, 1000);
final long q = Long.MAX_VALUE / 4;
- checkNextLongInRange(q, 1000);
- checkNextLongInRange(2 * q, 1000);
- checkNextLongInRange(3 * q, 1000);
+ checkNextLongInRange(generator, q, 1000);
+ checkNextLongInRange(generator, 2 * q, 1000);
+ checkNextLongInRange(generator, 3 * q, 1000);
}
- @Test
- public void testUniformNextFloat() {
- checkNextFloat(1000);
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextFloat(UniformRandomProvider generator) {
+ checkNextFloat(generator, 1000);
}
- @Test
- public void testUniformNextDouble() {
- checkNextDouble(1000);
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextDouble(UniformRandomProvider generator) {
+ checkNextDouble(generator, 1000);
}
- @Test
- public void testUniformNextIntRandomWalk() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextIntRandomWalk(UniformRandomProvider generator) {
final Callable<Boolean> nextMethod = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
@@ -169,11 +161,12 @@ public class ProvidersCommonParametricTest {
}
};
- checkRandomWalk(1000, nextMethod);
+ checkRandomWalk(generator, 1000, nextMethod);
}
- @Test
- public void testUniformNextLongRandomWalk() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextLongRandomWalk(UniformRandomProvider generator) {
final Callable<Boolean> nextMethod = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
@@ -181,11 +174,12 @@ public class ProvidersCommonParametricTest {
}
};
- checkRandomWalk(1000, nextMethod);
+ checkRandomWalk(generator, 1000, nextMethod);
}
- @Test
- public void testUniformNextBooleanRandomWalk() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testUniformNextBooleanRandomWalk(UniformRandomProvider generator) {
final Callable<Boolean> nextMethod = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
@@ -193,13 +187,14 @@ public class ProvidersCommonParametricTest {
}
};
- checkRandomWalk(1000, nextMethod);
+ checkRandomWalk(generator, 1000, nextMethod);
}
// State save and restore tests.
- @Test
- public void testStateSettable() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testStateSettable(RestorableUniformRandomProvider generator) {
// Should be fairly large in order to ensure that all the internal
// state is away from its initial settings.
final int n = 10000;
@@ -207,29 +202,31 @@ public class ProvidersCommonParametricTest {
// Save.
final RandomProviderState state = generator.saveState();
// Store some values.
- final List<Number> listOrig = makeList(n);
+ final List<Number> listOrig = makeList(n, generator);
// Discard a few more.
- final List<Number> listDiscard = makeList(n);
+ final List<Number> listDiscard = makeList(n, generator);
Assertions.assertNotEquals(0, listDiscard.size());
Assertions.assertNotEquals(listOrig, listDiscard);
// Reset.
generator.restoreState(state);
// Replay.
- final List<Number> listReplay = makeList(n);
+ final List<Number> listReplay = makeList(n, generator);
Assertions.assertNotSame(listOrig, listReplay);
// Check that the restored state is the same as the original.
Assertions.assertEquals(listOrig, listReplay);
}
- @Test
- public void testStateWrongSize() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testStateWrongSize(RestorableUniformRandomProvider generator) {
final RandomProviderState state = new DummyGenerator().saveState();
// Try to restore with an invalid state (wrong size).
Assertions.assertThrows(IllegalStateException.class, () -> generator.restoreState(state));
}
- @Test
- public void testRestoreForeignState() {
+ @ParameterizedTest
+ @MethodSource("getList")
+ public void testRestoreForeignState(RestorableUniformRandomProvider generator) {
Assertions.assertThrows(IllegalArgumentException.class, () -> generator.restoreState(new RandomProviderState() {}));
}
@@ -239,10 +236,11 @@ public class ProvidersCommonParametricTest {
* Populates a list with random numbers.
*
* @param n Loop counter.
+ * @param generator RNG under test.
* @return a list containing {@code 11 * n} random numbers.
*/
- private List<Number> makeList(int n) {
- final List<Number> list = new ArrayList<Number>();
+ private List<Number> makeList(int n, UniformRandomProvider generator) {
+ final List<Number> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
// Append 11 values.
@@ -328,11 +326,14 @@ public class ProvidersCommonParametricTest {
* approximately equal number of counts.
* The test uses the expectation from a fixed-step "random walk".
*
+ * @param generator Generator.
+ * @param sampleSize Number of random values generated.
* @param nextMethod Method that returns {@code true} if the generated
* values are to be placed in the first bin, {@code false} if it must
* go to the second bin.
*/
- private void checkRandomWalk(int sampleSize,
+ private void checkRandomWalk(UniformRandomProvider generator,
+ int sampleSize,
Callable<Boolean> nextMethod) {
int walk = 0;
@@ -359,41 +360,32 @@ public class ProvidersCommonParametricTest {
/**
* Tests uniformity of the distribution produced by {@code nextInt(int)}.
*
+ * @param generator Generator.
* @param max Upper bound.
* @param sampleSize Number of random values generated.
*/
- private void checkNextIntegerInRange(final int max,
- int sampleSize) {
- checkNextIntegerInRange(generator, max, sampleSize);
- }
-
- /**
- * Tests uniformity of the distribution produced by {@code nextInt(int)}.
- *
- * @param rng Generator.
- * @param max Upper bound.
- * @param sampleSize Number of random values generated.
- */
- private void checkNextIntegerInRange(final UniformRandomProvider rng,
+ private void checkNextIntegerInRange(final UniformRandomProvider generator,
final int max,
int sampleSize) {
final Callable<Integer> nextMethod = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
- return rng.nextInt(max);
+ return generator.nextInt(max);
}
};
- checkNextInRange(max, sampleSize, nextMethod);
+ checkNextInRange(generator, max, sampleSize, nextMethod);
}
/**
* Tests uniformity of the distribution produced by {@code nextLong(long)}.
*
+ * @param generator Generator.
* @param max Upper bound.
* @param sampleSize Number of random values generated.
*/
- private void checkNextLongInRange(final long max,
+ private void checkNextLongInRange(final UniformRandomProvider generator,
+ long max,
int sampleSize) {
final Callable<Long> nextMethod = new Callable<Long>() {
@Override
@@ -402,15 +394,17 @@ public class ProvidersCommonParametricTest {
}
};
- checkNextInRange(max, sampleSize, nextMethod);
+ checkNextInRange(generator, max, sampleSize, nextMethod);
}
/**
* Tests uniformity of the distribution produced by {@code nextFloat()}.
*
+ * @param generator Generator.
* @param sampleSize Number of random values generated.
*/
- private void checkNextFloat(int sampleSize) {
+ private void checkNextFloat(final UniformRandomProvider generator,
+ int sampleSize) {
final int max = 1234;
final Callable<Integer> nextMethod = new Callable<Integer>() {
@Override
@@ -419,15 +413,17 @@ public class ProvidersCommonParametricTest {
}
};
- checkNextInRange(max, sampleSize, nextMethod);
+ checkNextInRange(generator, max, sampleSize, nextMethod);
}
/**
* Tests uniformity of the distribution produced by {@code nextDouble()}.
*
+ * @param generator Generator.
* @param sampleSize Number of random values generated.
*/
- private void checkNextDouble(int sampleSize) {
+ private void checkNextDouble(final UniformRandomProvider generator,
+ int sampleSize) {
final int max = 578;
final Callable<Integer> nextMethod = new Callable<Integer>() {
@Override
@@ -436,7 +432,7 @@ public class ProvidersCommonParametricTest {
}
};
- checkNextInRange(max, sampleSize, nextMethod);
+ checkNextInRange(generator, max, sampleSize, nextMethod);
}
/**
@@ -447,11 +443,13 @@ public class ProvidersCommonParametricTest {
* Repeat tests are performed at the 1% level and the total number of failed
* tests is tested at the 0.5% significance level.
*
+ * @param generator Generator.
* @param max Upper bound.
* @param nextMethod method to call.
* @param sampleSize Number of random values generated.
*/
- private <T extends Number> void checkNextInRange(T max,
+ private <T extends Number> void checkNextInRange(final UniformRandomProvider generator,
+ T max,
int sampleSize,
Callable<T> nextMethod) {
final int numTests = 500;
@@ -532,27 +530,27 @@ public class ProvidersCommonParametricTest {
}
/**
- * @param rng Generator.
+ * @param generator Generator.
* @param chunkSize Size of the small buffer.
* @param numChunks Number of chunks that make the large buffer.
*/
- static void checkNextBytesChunks(RestorableUniformRandomProvider rng,
+ static void checkNextBytesChunks(RestorableUniformRandomProvider generator,
int chunkSize,
int numChunks) {
final byte[] b1 = new byte[chunkSize * numChunks];
final byte[] b2 = new byte[chunkSize];
- final RandomProviderState state = rng.saveState();
+ final RandomProviderState state = generator.saveState();
// Generate the chunks in a single call.
- rng.nextBytes(b1);
+ generator.nextBytes(b1);
// Reset to previous state.
- rng.restoreState(state);
+ generator.restoreState(state);
// Generate the chunks in consecutive calls.
for (int i = 0; i < numChunks; i++) {
- rng.nextBytes(b2);
+ generator.nextBytes(b2);
}
// Store last "chunkSize" bytes of b1 into b3.
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersList.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersList.java
index 620a5cc..6f18ab4 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersList.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersList.java
@@ -78,17 +78,13 @@ import org.apache.commons.rng.RestorableUniformRandomProvider;
*/
public final class ProvidersList {
/** List of all RNGs implemented in the library. */
- private static final List<RestorableUniformRandomProvider[]> LIST =
- new ArrayList<RestorableUniformRandomProvider[]>();
+ private static final List<RestorableUniformRandomProvider> LIST = new ArrayList<>();
/** List of 32-bits based RNGs. */
- private static final List<RestorableUniformRandomProvider[]> LIST32 =
- new ArrayList<RestorableUniformRandomProvider[]>();
+ private static final List<RestorableUniformRandomProvider> LIST32 = new ArrayList<>();
/** List of 64-bits based RNGs. */
- private static final List<RestorableUniformRandomProvider[]> LIST64 =
- new ArrayList<RestorableUniformRandomProvider[]>();
+ private static final List<RestorableUniformRandomProvider> LIST64 = new ArrayList<>();
/** List of {@link JumpableUniformRandomProvider} RNGs. */
- private static final List<JumpableUniformRandomProvider[]> LIST_JUMP =
- new ArrayList<JumpableUniformRandomProvider[]>();
+ private static final List<JumpableUniformRandomProvider> LIST_JUMP = new ArrayList<>();
static {
// External generator for creating a random seed.
@@ -96,57 +92,57 @@ public final class ProvidersList {
try {
// "int"-based RNGs.
- add(LIST32, new JDKRandom(g.nextLong()));
- add(LIST32, new MersenneTwister(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well512a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well1024a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well19937a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well19937c(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well44497a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new Well44497b(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new ISAACRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new MultiplyWithCarry256(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new KISSRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new XoRoShiRo64Star(new int[] {g.nextInt(), g.nextInt()}));
- add(LIST32, new XoRoShiRo64StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new XoShiRo128Plus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new XoShiRo128StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
- add(LIST32, new PcgXshRr32(new long[] {g.nextLong()}));
- add(LIST32, new PcgXshRr32(g.nextLong()));
- add(LIST32, new PcgXshRs32(new long[] {g.nextLong()}));
- add(LIST32, new PcgXshRs32(g.nextLong()));
- add(LIST32, new PcgMcgXshRr32(g.nextLong()));
- add(LIST32, new PcgMcgXshRs32(g.nextLong()));
+ LIST32.add(new JDKRandom(g.nextLong()));
+ LIST32.add(new MersenneTwister(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well512a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well1024a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well19937a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well19937c(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well44497a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new Well44497b(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new ISAACRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new MultiplyWithCarry256(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new KISSRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new XoRoShiRo64Star(new int[] {g.nextInt(), g.nextInt()}));
+ LIST32.add(new XoRoShiRo64StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new XoShiRo128Plus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new XoShiRo128StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new PcgXshRr32(new long[] {g.nextLong()}));
+ LIST32.add(new PcgXshRr32(g.nextLong()));
+ LIST32.add(new PcgXshRs32(new long[] {g.nextLong()}));
+ LIST32.add(new PcgXshRs32(g.nextLong()));
+ LIST32.add(new PcgMcgXshRr32(g.nextLong()));
+ LIST32.add(new PcgMcgXshRs32(g.nextLong()));
// Ensure a high complexity increment is used for the Weyl sequence
- add(LIST32, new MiddleSquareWeylSequence(new long[] {g.nextLong(), g.nextLong(), 0xb5ad4eceda1ce2a9L}));
- add(LIST32, new DotyHumphreySmallFastCounting32(new int[] {g.nextInt(), g.nextInt()}));
- add(LIST32, new JenkinsSmallFast32(g.nextInt()));
- add(LIST32, new XoShiRo128PlusPlus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
+ LIST32.add(new MiddleSquareWeylSequence(new long[] {g.nextLong(), g.nextLong(), 0xb5ad4eceda1ce2a9L}));
+ LIST32.add(new DotyHumphreySmallFastCounting32(new int[] {g.nextInt(), g.nextInt()}));
+ LIST32.add(new JenkinsSmallFast32(g.nextInt()));
+ LIST32.add(new XoShiRo128PlusPlus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
// ... add more here.
// "long"-based RNGs.
- add(LIST64, new SplitMix64(g.nextLong()));
- add(LIST64, new XorShift1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XorShift1024StarPhi(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new TwoCmres(g.nextInt()));
- add(LIST64, new TwoCmres(g.nextInt(), 5, 8));
- add(LIST64, new MersenneTwister64(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoRoShiRo128Plus(new long[] {g.nextLong(), g.nextLong()}));
- add(LIST64, new XoRoShiRo128StarStar(new long[] {g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo256Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo256StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo512Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo512StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new PcgRxsMXs64(new long[] {g.nextLong()}));
- add(LIST64, new PcgRxsMXs64(g.nextLong()));
- add(LIST64, new DotyHumphreySmallFastCounting64(new long[] {g.nextLong(), g.nextLong()}));
- add(LIST64, new JenkinsSmallFast64(g.nextLong()));
- add(LIST64, new XoRoShiRo128PlusPlus(new long[] {g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo256PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoShiRo512PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoRoShiRo1024PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoRoShiRo1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
- add(LIST64, new XoRoShiRo1024StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new SplitMix64(g.nextLong()));
+ LIST64.add(new XorShift1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XorShift1024StarPhi(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new TwoCmres(g.nextInt()));
+ LIST64.add(new TwoCmres(g.nextInt(), 5, 8));
+ LIST64.add(new MersenneTwister64(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoRoShiRo128Plus(new long[] {g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoRoShiRo128StarStar(new long[] {g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo256Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo256StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo512Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo512StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new PcgRxsMXs64(new long[] {g.nextLong()}));
+ LIST64.add(new PcgRxsMXs64(g.nextLong()));
+ LIST64.add(new DotyHumphreySmallFastCounting64(new long[] {g.nextLong(), g.nextLong()}));
+ LIST64.add(new JenkinsSmallFast64(g.nextLong()));
+ LIST64.add(new XoRoShiRo128PlusPlus(new long[] {g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo256PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoShiRo512PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoRoShiRo1024PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoRoShiRo1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
+ LIST64.add(new XoRoShiRo1024StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
// ... add more here.
// Do not modify the remaining statements.
@@ -154,11 +150,9 @@ public final class ProvidersList {
LIST.addAll(LIST32);
LIST.addAll(LIST64);
// Dynamically identify the Jumpable RNGs
- for (RestorableUniformRandomProvider[] rng : LIST) {
- if (rng[0] instanceof JumpableUniformRandomProvider) {
- add(LIST_JUMP, (JumpableUniformRandomProvider) rng[0]);
- }
- }
+ LIST.stream()
+ .filter(rng -> rng instanceof JumpableUniformRandomProvider)
+ .forEach(rng -> LIST_JUMP.add((JumpableUniformRandomProvider) rng));
} catch (Exception e) {
// CHECKSTYLE: stop Regexp
System.err.println("Unexpected exception while creating the list of generators: " + e);
@@ -174,30 +168,12 @@ public final class ProvidersList {
private ProvidersList() {}
/**
- * Helper to statisfy Junit requirement that each parameter set contains
- * the same number of objects.
- */
- private static void add(List<RestorableUniformRandomProvider[]> list,
- RestorableUniformRandomProvider rng) {
- list.add(new RestorableUniformRandomProvider[] {rng});
- }
-
- /**
- * Helper to statisfy Junit requirement that each parameter set contains
- * the same number of objects.
- */
- private static void add(List<JumpableUniformRandomProvider[]> list,
- JumpableUniformRandomProvider rng) {
- list.add(new JumpableUniformRandomProvider[] {rng});
- }
-
- /**
* Subclasses that are "parametric" tests can forward the call to
* the "@Parameters"-annotated method to this method.
*
* @return the list of all generators.
*/
- public static Iterable<RestorableUniformRandomProvider[]> list() {
+ public static Iterable<RestorableUniformRandomProvider> list() {
return Collections.unmodifiableList(LIST);
}
@@ -207,7 +183,7 @@ public final class ProvidersList {
*
* @return the list of 32-bits based generators.
*/
- public static Iterable<RestorableUniformRandomProvider[]> list32() {
+ public static Iterable<RestorableUniformRandomProvider> list32() {
return Collections.unmodifiableList(LIST32);
}
@@ -217,7 +193,7 @@ public final class ProvidersList {
*
* @return the list of 64-bits based generators.
*/
- public static Iterable<RestorableUniformRandomProvider[]> list64() {
+ public static Iterable<RestorableUniformRandomProvider> list64() {
return Collections.unmodifiableList(LIST64);
}
@@ -227,7 +203,7 @@ public final class ProvidersList {
*
* @return the list of {@link JumpableUniformRandomProvider} generators.
*/
- public static Iterable<JumpableUniformRandomProvider[]> listJumpable() {
+ public static Iterable<JumpableUniformRandomProvider> listJumpable() {
return Collections.unmodifiableList(LIST_JUMP);
}
}
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 4ad40e3..058e3b0 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
@@ -21,36 +21,21 @@ import java.util.List;
import java.util.ArrayList;
import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests for random deviates generators.
*/
-@RunWith(value = Parameterized.class)
public class ContinuousSamplerParametricTest {
- /** Sampler under test. */
- private final ContinuousSamplerTestData sampler;
-
- /**
- * Initializes the test instance.
- *
- * @param data sampler to be tested.
- */
- public ContinuousSamplerParametricTest(ContinuousSamplerTestData data) {
- sampler = data;
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<ContinuousSamplerTestData[]> getList() {
+ private static Iterable<ContinuousSamplerTestData> getSamplerTestData() {
return ContinuousSamplersList.list();
}
- @Test
- public void testSampling() {
- check(20000, sampler.getSampler(), sampler.getDeciles());
+ @ParameterizedTest
+ @MethodSource("getSamplerTestData")
+ public void testSampling(ContinuousSamplerTestData data) {
+ check(20000, data.getSampler(), data.getDeciles());
}
/**
@@ -85,7 +70,7 @@ public class ContinuousSamplerParametricTest {
final double chi2CriticalValue = 21.67;
// For storing chi2 larger than the critical value.
- final List<Double> failedStat = new ArrayList<Double>();
+ final List<Double> failedStat = new ArrayList<>();
try {
final int lastDecileIndex = numBins - 1;
for (int i = 0; i < numTests; i++) {
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
index e84776f..d145db3 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
@@ -28,8 +28,7 @@ import org.apache.commons.rng.simple.RandomSource;
*/
public final class ContinuousSamplersList {
/** List of all RNGs implemented in the library. */
- private static final List<ContinuousSamplerTestData[]> LIST =
- new ArrayList<ContinuousSamplerTestData[]>();
+ private static final List<ContinuousSamplerTestData> LIST = new ArrayList<>();
static {
try {
@@ -290,7 +289,7 @@ public final class ContinuousSamplersList {
* @param dist Distribution to which the samples are supposed to conform.
* @param rng Generator of uniformly distributed sequences.
*/
- private static void add(List<ContinuousSamplerTestData[]> list,
+ private static void add(List<ContinuousSamplerTestData> list,
final org.apache.commons.math3.distribution.RealDistribution dist,
UniformRandomProvider rng) {
final ContinuousSampler inverseMethodSampler =
@@ -305,8 +304,8 @@ public final class ContinuousSamplersList {
return dist.toString();
}
});
- list.add(new ContinuousSamplerTestData[] {new ContinuousSamplerTestData(inverseMethodSampler,
- getDeciles(dist))});
+ list.add(new ContinuousSamplerTestData(inverseMethodSampler,
+ getDeciles(dist)));
}
/**
@@ -314,11 +313,11 @@ public final class ContinuousSamplersList {
* @param dist Distribution to which the samples are supposed to conform.
* @param sampler Sampler.
*/
- private static void add(List<ContinuousSamplerTestData[]> list,
+ private static void add(List<ContinuousSamplerTestData> list,
final org.apache.commons.math3.distribution.RealDistribution dist,
final ContinuousSampler sampler) {
- list.add(new ContinuousSamplerTestData[] {new ContinuousSamplerTestData(sampler,
- getDeciles(dist))});
+ list.add(new ContinuousSamplerTestData(sampler,
+ getDeciles(dist)));
}
/**
@@ -326,11 +325,11 @@ public final class ContinuousSamplersList {
* @param deciles Deciles of the given distribution.
* @param sampler Sampler.
*/
- private static void add(List<ContinuousSamplerTestData[]> list,
+ private static void add(List<ContinuousSamplerTestData> list,
final double[] deciles,
final ContinuousSampler sampler) {
- list.add(new ContinuousSamplerTestData[] {new ContinuousSamplerTestData(sampler,
- deciles)});
+ list.add(new ContinuousSamplerTestData(sampler,
+ deciles));
}
/**
@@ -339,7 +338,7 @@ public final class ContinuousSamplersList {
*
* @return the list of all generators.
*/
- public static Iterable<ContinuousSamplerTestData[]> list() {
+ public static Iterable<ContinuousSamplerTestData> list() {
return Collections.unmodifiableList(LIST);
}
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplerParametricTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplerParametricTest.java
index 8ad84d1..f65c68c 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplerParametricTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplerParametricTest.java
@@ -21,49 +21,28 @@ import java.util.List;
import java.util.ArrayList;
import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.apache.commons.math3.stat.inference.ChiSquareTest;
/**
* Tests for random deviates generators.
*/
-@RunWith(value = Parameterized.class)
public class DiscreteSamplerParametricTest {
- /** Sampler under test. */
- private final DiscreteSamplerTestData sampler;
-
- /**
- * Initializes the test instance.
- *
- * @param data sampler to be tested.
- */
- public DiscreteSamplerParametricTest(DiscreteSamplerTestData data) {
- sampler = data;
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<DiscreteSamplerTestData[]> getList() {
+ private static Iterable<DiscreteSamplerTestData> getSamplerTestData() {
return DiscreteSamplersList.list();
}
- @Test
- public void testSampling() {
+ @ParameterizedTest
+ @MethodSource("getSamplerTestData")
+ public void testSampling(DiscreteSamplerTestData data) {
final int sampleSize = 10000;
-
- final double[] prob = sampler.getProbabilities();
- final int len = prob.length;
- final double[] expected = new double[len];
- for (int i = 0; i < len; i++) {
- expected[i] = prob[i] * sampleSize;
- }
+ // Probabilities are normalised by the chi-square test
check(sampleSize,
- sampler.getSampler(),
- sampler.getPoints(),
- expected);
+ data.getSampler(),
+ data.getPoints(),
+ data.getProbabilities());
}
/**
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplersList.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplersList.java
index 3e4b9f7..abb2445 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplersList.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/DiscreteSamplersList.java
@@ -30,8 +30,7 @@ import org.apache.commons.rng.simple.RandomSource;
*/
public final class DiscreteSamplersList {
/** List of all RNGs implemented in the library. */
- private static final List<DiscreteSamplerTestData[]> LIST =
- new ArrayList<DiscreteSamplerTestData[]>();
+ private static final List<DiscreteSamplerTestData> LIST = new ArrayList<>();
static {
try {
@@ -204,7 +203,7 @@ public final class DiscreteSamplersList {
* @param points Outcomes selection.
* @param rng Generator of uniformly distributed sequences.
*/
- private static void add(List<DiscreteSamplerTestData[]> list,
+ private static void add(List<DiscreteSamplerTestData> list,
final org.apache.commons.math3.distribution.IntegerDistribution dist,
int[] points,
UniformRandomProvider rng) {
@@ -220,9 +219,9 @@ public final class DiscreteSamplersList {
return dist.toString();
}
});
- list.add(new DiscreteSamplerTestData[] {new DiscreteSamplerTestData(inverseMethodSampler,
- points,
- getProbabilities(dist, points))});
+ list.add(new DiscreteSamplerTestData(inverseMethodSampler,
+ points,
+ getProbabilities(dist, points)));
}
/**
@@ -231,13 +230,13 @@ public final class DiscreteSamplersList {
* @param points Outcomes selection.
* @param sampler Sampler.
*/
- private static void add(List<DiscreteSamplerTestData[]> list,
+ private static void add(List<DiscreteSamplerTestData> list,
final org.apache.commons.math3.distribution.IntegerDistribution dist,
int[] points,
final DiscreteSampler sampler) {
- list.add(new DiscreteSamplerTestData[] {new DiscreteSamplerTestData(sampler,
- points,
- getProbabilities(dist, points))});
+ list.add(new DiscreteSamplerTestData(sampler,
+ points,
+ getProbabilities(dist, points)));
}
/**
@@ -245,12 +244,12 @@ public final class DiscreteSamplersList {
* @param probabilities Probability distribution to which the samples are supposed to conform.
* @param sampler Sampler.
*/
- private static void add(List<DiscreteSamplerTestData[]> list,
+ private static void add(List<DiscreteSamplerTestData> list,
final double[] probabilities,
final DiscreteSampler sampler) {
- list.add(new DiscreteSamplerTestData[] {new DiscreteSamplerTestData(sampler,
- MathArrays.natural(probabilities.length),
- probabilities)});
+ list.add(new DiscreteSamplerTestData(sampler,
+ MathArrays.natural(probabilities.length),
+ probabilities));
}
/**
@@ -259,7 +258,7 @@ public final class DiscreteSamplersList {
*
* @return the list of all generators.
*/
- public static Iterable<DiscreteSamplerTestData[]> list() {
+ public static Iterable<DiscreteSamplerTestData> list() {
return Collections.unmodifiableList(LIST);
}
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 dffa57c..ed56283 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
@@ -27,11 +27,9 @@ import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.JumpableUniformRandomProvider;
@@ -44,51 +42,35 @@ import org.apache.commons.rng.core.source64.SplitMix64;
/**
* Tests which all generators must pass.
*/
-@RunWith(value = Parameterized.class)
public class ProvidersCommonParametricTest {
- /** RNG under test. */
- private final UniformRandomProvider generator;
- /** RNG specifier. */
- private final RandomSource originalSource;
- /** Seed (constructor's first parameter). */
- private final Object originalSeed;
- /** Constructor's additional parameters. */
- private final Object[] originalArgs;
-
- /**
- * Initializes the test instance.
- *
- * @param data Random source (and seed arguments) to be tested.
- */
- public ProvidersCommonParametricTest(ProvidersList.Data data) {
- originalSource = data.getSource();
- originalSeed = data.getSeed();
- originalArgs = data.getArgs();
- generator = originalSource.create(originalSeed, originalArgs);
- }
-
- @Parameters(name = "{index}: data={0}")
- public static Iterable<ProvidersList.Data[]> getList() {
+ private static Iterable<ProvidersList.Data> getProvidersTestData() {
return ProvidersList.list();
}
// Seeding tests.
- @Test
- public void testUnsupportedSeedType() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testUnsupportedSeedType(ProvidersList.Data data) {
final byte seed = 123;
- Assertions.assertThrows(UnsupportedOperationException.class, () -> originalSource.create(seed, originalArgs));
+ Assertions.assertThrows(UnsupportedOperationException.class,
+ () -> data.getSource().create(seed, data.getArgs()));
}
/**
* Test the factory create method returns the same class as the instance create method.
*/
- @Test
- public void testFactoryCreateMethod() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testFactoryCreateMethod(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object originalSeed = data.getSeed();
+ final Object[] originalArgs = data.getArgs();
// Cannot test providers that require arguments
Assumptions.assumeTrue(originalArgs == null);
@SuppressWarnings("deprecation")
- final UniformRandomProvider rng = RandomSource.create(originalSource);
+ final UniformRandomProvider rng = RandomSource.create(data.getSource());
+ final UniformRandomProvider generator = originalSource.create(originalSeed, originalArgs);
Assertions.assertEquals(generator.getClass(), rng.getClass());
}
@@ -96,8 +78,13 @@ public class ProvidersCommonParametricTest {
* Test the factory create method returns the same class as the instance create method
* and produces the same output.
*/
- @Test
- public void testFactoryCreateMethodWithSeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testFactoryCreateMethodWithSeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object originalSeed = data.getSeed();
+ final Object[] originalArgs = data.getArgs();
+ final UniformRandomProvider generator = originalSource.create(originalSeed, originalArgs);
@SuppressWarnings("deprecation")
final UniformRandomProvider rng1 = RandomSource.create(originalSource, originalSeed, originalArgs);
Assertions.assertEquals(rng1.getClass(), generator.getClass());
@@ -112,8 +99,11 @@ public class ProvidersCommonParametricTest {
* Test the create method throws an {@link IllegalArgumentException} if passed the wrong
* arguments.
*/
- @Test
- public void testCreateMethodThrowsWithIncorrectArguments() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testCreateMethodThrowsWithIncorrectArguments(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
if (originalArgs == null) {
try {
// Try passing arguments to a provider that does not require them
@@ -135,8 +125,12 @@ public class ProvidersCommonParametricTest {
}
}
- @Test
- public void testAllSeedTypes() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testAllSeedTypes(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object originalSeed = data.getSeed();
+ final Object[] originalArgs = data.getArgs();
final Integer intSeed = -12131415;
final Long longSeed = -1213141516171819L;
final int[] intArraySeed = new int[] {0, 11, -22, 33, -44, 55, -66, 77, -88, 99};
@@ -169,8 +163,11 @@ public class ProvidersCommonParametricTest {
Assertions.assertEquals(5, nonNativeSeedCount);
}
- @Test
- public void testNullSeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testNullSeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
// Note: This is the only test that explicitly calls RandomSource.create() with no other arguments.
final UniformRandomProvider rng = originalArgs == null ?
originalSource.create() :
@@ -178,8 +175,11 @@ public class ProvidersCommonParametricTest {
checkNextIntegerInRange(rng, 10, 10000);
}
- @Test
- public void testEmptyIntArraySeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testEmptyIntArraySeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
final int[] empty = new int[0];
Assumptions.assumeTrue(originalSource.isNativeSeed(empty));
@@ -188,8 +188,11 @@ public class ProvidersCommonParametricTest {
checkNextIntegerInRange(rng, 10, 20000);
}
- @Test
- public void testEmptyLongArraySeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testEmptyLongArraySeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
final long[] empty = new long[0];
Assumptions.assumeTrue(originalSource.isNativeSeed(empty));
// The Middle-Square Weyl Sequence generator cannot self-seed
@@ -200,8 +203,11 @@ public class ProvidersCommonParametricTest {
checkNextIntegerInRange(rng, 10, 10000);
}
- @Test
- public void testZeroIntArraySeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testZeroIntArraySeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
// Exercise capacity to escape all "zero" state.
final int[] zero = new int[2000]; // Large enough to fill the entire state with zeroes.
final UniformRandomProvider rng = originalSource.create(zero, originalArgs);
@@ -210,8 +216,11 @@ public class ProvidersCommonParametricTest {
checkNextIntegerInRange(rng, 10, 10000);
}
- @Test
- public void testZeroLongArraySeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testZeroLongArraySeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
// Exercise capacity to escape all "zero" state.
final long[] zero = new long[2000]; // Large enough to fill the entire state with zeroes.
final UniformRandomProvider rng = originalSource.create(zero, originalArgs);
@@ -220,15 +229,21 @@ public class ProvidersCommonParametricTest {
checkNextIntegerInRange(rng, 10, 10000);
}
- @Test
- public void testRandomSourceCreateSeed() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testRandomSourceCreateSeed(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
final byte[] seed = originalSource.createSeed();
final UniformRandomProvider rng = originalSource.create(seed, originalArgs);
checkNextIntegerInRange(rng, 10, 10000);
}
- @Test
- public void testRandomSourceCreateSeedFromRNG() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testRandomSourceCreateSeedFromRNG(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
final byte[] seed = originalSource.createSeed(new SplitMix64(RandomSource.createLong()));
final UniformRandomProvider rng = originalSource.create(seed, originalArgs);
checkNextIntegerInRange(rng, 10, 10000);
@@ -236,8 +251,13 @@ public class ProvidersCommonParametricTest {
// State save and restore tests.
- @Test
- public void testUnrestorable() {
+ @SuppressWarnings("unused")
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testUnrestorable(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object originalSeed = data.getSeed();
+ final Object[] originalArgs = data.getArgs();
// Create two generators of the same type as the one being tested.
final UniformRandomProvider rng1 = originalSource.create(originalSeed, originalArgs);
final UniformRandomProvider rng2 = RandomSource.unrestorable(originalSource.create(originalSeed, originalArgs));
@@ -248,18 +268,18 @@ public class ProvidersCommonParametricTest {
// Cast must work.
final RestorableUniformRandomProvider restorable = (RestorableUniformRandomProvider) rng1;
// Cast must fail.
- try {
- final RestorableUniformRandomProvider dummy = (RestorableUniformRandomProvider) rng2;
- Assertions.fail("Cast should have failed");
- } catch (ClassCastException e) {
- // Expected.
- }
+ Assertions.assertThrows(ClassCastException.class, () -> {
+ RestorableUniformRandomProvider dummy = (RestorableUniformRandomProvider) rng2;
+ });
}
- @Test
- public void testSerializingState()
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testSerializingState(ProvidersList.Data data)
throws IOException,
ClassNotFoundException {
+ final UniformRandomProvider generator = data.getSource().create(data.getSeed(), data.getArgs());
+
// Large "n" is not necessary here as we only test the serialization.
final int n = 100;
@@ -274,10 +294,10 @@ public class ProvidersCommonParametricTest {
oos.writeObject(((RandomProviderDefaultState) stateOrig).getState());
// Store some values.
- final List<Number> listOrig = makeList(n);
+ final List<Number> listOrig = makeList(n, generator);
// Discard a few more.
- final List<Number> listDiscard = makeList(n);
+ final List<Number> listDiscard = makeList(n, generator);
Assertions.assertNotEquals(0, listDiscard.size());
Assertions.assertNotEquals(listOrig, listDiscard);
@@ -292,21 +312,26 @@ public class ProvidersCommonParametricTest {
restorable.restoreState(stateNew);
// Replay.
- final List<Number> listReplay = makeList(n);
+ final List<Number> listReplay = makeList(n, generator);
Assertions.assertNotSame(listOrig, listReplay);
// Check that the serialized data recreated the orginal state.
Assertions.assertEquals(listOrig, listReplay);
}
- @Test
- public void testUnrestorableToString() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testUnrestorableToString(ProvidersList.Data data) {
+ final UniformRandomProvider generator = data.getSource().create(data.getSeed(), data.getArgs());
Assertions.assertEquals(generator.toString(),
- RandomSource.unrestorable(generator).toString());
+ RandomSource.unrestorable(generator).toString());
}
- @Test
- public void testSupportedInterfaces() {
+ @ParameterizedTest
+ @MethodSource("getProvidersTestData")
+ public void testSupportedInterfaces(ProvidersList.Data data) {
+ final RandomSource originalSource = data.getSource();
+ final Object[] originalArgs = data.getArgs();
final UniformRandomProvider rng = originalSource.create(null, originalArgs);
Assertions.assertEquals(rng instanceof JumpableUniformRandomProvider,
originalSource.isJumpable(),
@@ -330,10 +355,11 @@ public class ProvidersCommonParametricTest {
* Populates a list with random numbers.
*
* @param n Loop counter.
+ * @param generator Random generator.
* @return a list containing {@code 11 * n} random numbers.
*/
- private List<Number> makeList(int n) {
- final List<Number> list = new ArrayList<Number>();
+ private static List<Number> makeList(int n, UniformRandomProvider generator) {
+ final List<Number> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
// Append 11 values.
@@ -359,6 +385,7 @@ public class ProvidersCommonParametricTest {
* @param rng Generator.
* @param max Upper bound.
* @param sampleSize Number of random values generated.
+ * @param generator Random generator.
*/
private void checkNextIntegerInRange(final UniformRandomProvider rng,
final int max,
@@ -370,7 +397,7 @@ public class ProvidersCommonParametricTest {
}
};
- checkNextInRange(max, sampleSize, nextMethod);
+ checkNextInRange(max, sampleSize, nextMethod, rng);
}
/**
@@ -384,10 +411,12 @@ public class ProvidersCommonParametricTest {
* @param max Upper bound.
* @param nextMethod method to call.
* @param sampleSize Number of random values generated.
+ * @param generator Random generator.
*/
- private <T extends Number> void checkNextInRange(T max,
- int sampleSize,
- Callable<T> nextMethod) {
+ private static <T extends Number> void checkNextInRange(T max,
+ int sampleSize,
+ Callable<T> nextMethod,
+ UniformRandomProvider generator) {
final int numTests = 500;
// Do not change (statistical test assumes that dof = 9).
@@ -461,7 +490,7 @@ public class ProvidersCommonParametricTest {
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)");
+ " (" + numFailures + " out of " + numTests + " tests failed)");
}
}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java
index 3fcf119..645ce6c 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java
@@ -32,11 +32,11 @@ import java.util.Collections;
*/
public final class ProvidersList {
/** List of all RNGs implemented in the library. */
- private static final List<Data[]> LIST = new ArrayList<Data[]>();
+ private static final List<Data> LIST = new ArrayList<>();
/** List of 32-bits based RNGs. */
- private static final List<Data[]> LIST32 = new ArrayList<Data[]>();
+ private static final List<Data> LIST32 = new ArrayList<>();
/** List of 64-bits based RNGs. */
- private static final List<Data[]> LIST64 = new ArrayList<Data[]>();
+ private static final List<Data> LIST64 = new ArrayList<>();
static {
try {
@@ -115,14 +115,14 @@ public final class ProvidersList {
* Helper to statisfy Junit requirement that each parameter set contains
* the same number of objects.
*/
- private static void add(List<Data[]> list,
+ private static void add(List<Data> list,
RandomSource source,
Object... data) {
final RandomSource rng = source;
final Object seed = data.length > 0 ? data[0] : null;
final Object[] args = data.length > 1 ? Arrays.copyOfRange(data, 1, data.length) : null;
- list.add(new Data[] {new Data(rng, seed, args)});
+ list.add(new Data(rng, seed, args));
}
/**
@@ -131,7 +131,7 @@ public final class ProvidersList {
*
* @return the list of all generators.
*/
- public static Iterable<Data[]> list() {
+ public static Iterable<Data> list() {
return Collections.unmodifiableList(LIST);
}
@@ -141,7 +141,7 @@ public final class ProvidersList {
*
* @return the list of 32-bits based generators.
*/
- public static Iterable<Data[]> list32() {
+ public static Iterable<Data> list32() {
return Collections.unmodifiableList(LIST32);
}
@@ -151,7 +151,7 @@ public final class ProvidersList {
*
* @return the list of 64-bits based generators.
*/
- public static Iterable<Data[]> list64() {
+ public static Iterable<Data> list64() {
return Collections.unmodifiableList(LIST64);
}
@@ -160,10 +160,18 @@ public final class ProvidersList {
* Better not to mix Junit assumptions of the usage of "Object[]".
*/
public static class Data {
+ /** RNG specifier. */
private final RandomSource source;
+ /** Seed (constructor's first parameter). */
private final Object seed;
+ /** Constructor's additional parameters. */
private final Object[] args;
+ /**
+ * @param source RNG specifier.
+ * @param seed Seed (constructor's first parameter).
+ * @param args Constructor's additional parameters.
+ */
public Data(RandomSource source,
Object seed,
Object[] args) {
@@ -172,14 +180,29 @@ public final class ProvidersList {
this.args = args;
}
+ /**
+ * Gets the RNG specifier.
+ *
+ * @return RNG specifier.
+ */
public RandomSource getSource() {
return source;
}
+ /**
+ * Gets the seed (constructor's first parameter).
+ *
+ * @return Seed
+ */
public Object getSeed() {
return seed;
}
+ /**
+ * Gets the constructor's additional parameters.
+ *
+ * @return Additional parameters.
+ */
public Object[] getArgs() {
return args == null ? null : Arrays.copyOf(args, args.length);
}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java
index 8469047..e9bb527 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java
@@ -17,20 +17,21 @@
package org.apache.commons.rng.simple.internal;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Set;
import java.util.function.Supplier;
+import java.util.stream.Collectors;
/**
* Tests for the {@link NativeSeedType} seed conversions. This test
* ensures that a seed can be created or converted from any supported input seed to each
* supported native seed type.
*/
-@RunWith(value = Parameterized.class)
public class NativeSeedTypeParametricTest {
/** This is a list of the class types that are supported native seeds. */
private static final Object[] SUPPORTED_NATIVE_TYPES = {
@@ -53,59 +54,39 @@ public class NativeSeedTypeParametricTest {
Double.valueOf(Math.PI),
};
- /** The native seed type enum instance. */
- private final NativeSeedType nativeSeedType;
- /** The class type of the native seed. */
- private final Class<?> type;
-
/**
- * Initializes the test instance.
- *
- * @param type The type of the native seed.
+ * Check that there are enum values for all supported types.
+ * This ensures the test is maintained to correspond to the enum.
*/
- public NativeSeedTypeParametricTest(Class<?> type) {
- this.type = type;
- nativeSeedType = findNativeSeedType(type);
- }
+ @Test
+ public void testNativeSeedTypeEnum() {
+ Set<Class<?>> supported = Arrays.stream(SUPPORTED_NATIVE_TYPES)
+ .map(o -> (Class<?>) o)
+ .collect(Collectors.toSet());
+ Assertions.assertEquals(SUPPORTED_NATIVE_TYPES.length, supported.size(),
+ "Class type of supported seeds should be unique");
- /**
- * Gets the supported native seed types.
- *
- * @return the types
- */
- @Parameters
- public static Object[] getTypes() {
- // Check that there are enum values for all supported types.
- // This ensures the test is maintained to correspond to the enum.
- Assertions.assertEquals(SUPPORTED_NATIVE_TYPES.length, NativeSeedType.values().length,
+ final NativeSeedType[] values = NativeSeedType.values();
+ Assertions.assertEquals(SUPPORTED_NATIVE_TYPES.length, values.length,
"Incorrect number of enum values for the supported native types");
- return SUPPORTED_NATIVE_TYPES;
- }
-
- /**
- * Creates the native seed type.
- *
- * @param type Class of the native seed.
- * @return the native seed type
- */
- private static NativeSeedType findNativeSeedType(Class<?> type) {
- for (final NativeSeedType nativeSeedType : NativeSeedType.values()) {
- if (type.equals(nativeSeedType.getType())) {
- return nativeSeedType;
- }
- }
- throw new AssertionError("No enum matching the type: " + type);
+ // Remove each
+ Arrays.stream(values).map(NativeSeedType::getType).forEach(supported::remove);
+ Assertions.assertEquals(0, supported.size());
}
/**
* Test the seed can be created as the correct type.
+ *
+ * @param nativeSeedType Native seed type.
*/
- @Test
- public void testCreateSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCreateSeed(NativeSeedType nativeSeedType) {
final int size = 3;
final Object seed = nativeSeedType.createSeed(size);
Assertions.assertNotNull(seed);
+ final Class<?> type = nativeSeedType.getType();
Assertions.assertEquals(type, seed.getClass(), "Seed was not the correct class");
if (type.isArray()) {
Assertions.assertEquals(size, Array.getLength(seed), "Seed was not created the correct length");
@@ -114,9 +95,12 @@ public class NativeSeedTypeParametricTest {
/**
* Test the seed can be created, converted to a byte[] and then back to the native type.
+ *
+ * @param nativeSeedType Native seed type.
*/
- @Test
- public void testConvertSeedToBytes() {
+ @ParameterizedTest
+ @EnumSource
+ public void testConvertSeedToBytes(NativeSeedType nativeSeedType) {
final int size = 3;
final Object seed = nativeSeedType.createSeed(size);
Assertions.assertNotNull(seed, "Null seed");
@@ -125,7 +109,7 @@ public class NativeSeedTypeParametricTest {
Assertions.assertNotNull(bytes, "Null byte[] seed");
final Object seed2 = nativeSeedType.convertSeed(bytes, size);
- if (type.isArray()) {
+ if (nativeSeedType.getType().isArray()) {
// This handles nested primitive arrays
Assertions.assertArrayEquals(new Object[] {seed}, new Object[] {seed2},
"byte[] seed was not converted back");
@@ -136,32 +120,34 @@ public class NativeSeedTypeParametricTest {
/**
* Test the seed can be converted to the correct type from any of the supported input types.
+ *
+ * @param nativeSeedType The native seed type enum instance.
*/
- @Test
- public void testConvertSupportedSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testConvertSupportedSeed(NativeSeedType nativeSeedType) {
// Size can be ignored during conversion and so it not asserted
final int size = 3;
for (final Object input : SUPPORTED_SEEDS) {
final Object seed = nativeSeedType.convertSeed(input, size);
final Supplier<String> msg = () -> input.getClass() + " input seed was not converted";
Assertions.assertNotNull(seed, msg);
- Assertions.assertEquals(type, seed.getClass(), msg);
+ Assertions.assertEquals(nativeSeedType.getType(), seed.getClass(), msg);
}
}
/**
* Test unsupported input seed types are rejected.
+ *
+ * @param nativeSeedType The native seed type enum instance.
*/
- @Test
- public void testCannotConvertUnsupportedSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCannotConvertUnsupportedSeed(NativeSeedType nativeSeedType) {
final int size = 3;
for (final Object input : UNSUPPORTED_SEEDS) {
- try {
- nativeSeedType.convertSeed(input, size);
- Assertions.fail(input.getClass() + " input seed was not rejected as unsupported");
- } catch (UnsupportedOperationException ex) {
- // This is expected
- }
+ Assertions.assertThrows(UnsupportedOperationException.class,
+ () -> nativeSeedType.convertSeed(input, size));
}
}
}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java
index 5948223..2e5ae23 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java
@@ -19,10 +19,8 @@ package org.apache.commons.rng.simple.internal;
import org.apache.commons.rng.core.source64.SplitMix64;
import org.apache.commons.rng.simple.internal.ProviderBuilder.RandomSourceInternal;
import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
import java.util.EnumMap;
import java.util.function.Supplier;
@@ -32,7 +30,6 @@ import java.util.function.Supplier;
* ensures that all random sources can create a seed or convert any supported seed to the
* correct type for the constructor.
*/
-@RunWith(value = Parameterized.class)
public class RandomSourceInternalParametricTest {
/** The supported seeds for conversion to a native seed type. */
private static final Object[] SUPPORTED_SEEDS = {
@@ -49,11 +46,11 @@ public class RandomSourceInternalParametricTest {
};
/** The expected byte size of the seed for each RandomSource. */
private static final EnumMap<RandomSourceInternal, Integer> EXPECTED_SEED_BYTES =
- new EnumMap<RandomSourceInternal, Integer>(RandomSourceInternal.class);
+ new EnumMap<>(RandomSourceInternal.class);
static {
- final int intBytes = 4;
- final int longBytes = 8;
+ final int intBytes = Integer.BYTES;
+ final int longBytes = Long.BYTES;
EXPECTED_SEED_BYTES.put(RandomSourceInternal.JDK, longBytes * 1);
EXPECTED_SEED_BYTES.put(RandomSourceInternal.WELL_512_A, intBytes * 16);
EXPECTED_SEED_BYTES.put(RandomSourceInternal.WELL_1024_A, intBytes * 32);
@@ -105,37 +102,25 @@ public class RandomSourceInternalParametricTest {
// Verify the seed byte size is reflected in the enum javadoc for RandomSource.
}
- /** Internal identifier for the random source. */
- private final RandomSourceInternal randomSourceInternal;
- /** The class type of the native seed. */
- private final Class<?> type;
-
/**
- * Initializes the test instance.
+ * Get the class type of the native seed for the random source.
*
* @param randomSourceInternal Internal identifier for the random source.
*/
- public RandomSourceInternalParametricTest(RandomSourceInternal randomSourceInternal) {
- this.randomSourceInternal = randomSourceInternal;
+ private static Class<?> getType(RandomSourceInternal randomSourceInternal) {
// The first constructor argument is always the seed type
- this.type = randomSourceInternal.getArgs()[0];
- }
-
- /**
- * Gets the supported native seed types.
- *
- * @return the types
- */
- @Parameters
- public static Object[] getTypes() {
- return RandomSourceInternal.values();
+ return randomSourceInternal.getArgs()[0];
}
/**
* Test the seed can be created as the correct type.
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
*/
- @Test
- public void testCreateSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCreateSeed(RandomSourceInternal randomSourceInternal) {
+ final Class<?> type = getType(randomSourceInternal);
final Object seed = randomSourceInternal.createSeed();
Assertions.assertNotNull(seed);
Assertions.assertEquals(type, seed.getClass(), "Seed was not the correct class");
@@ -144,9 +129,13 @@ public class RandomSourceInternalParametricTest {
/**
* Test the seed can be converted to the correct type from any of the supported input types.
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
*/
- @Test
- public void testConvertSupportedSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testConvertSupportedSeed(RandomSourceInternal randomSourceInternal) {
+ final Class<?> type = getType(randomSourceInternal);
for (final Object input : SUPPORTED_SEEDS) {
final Object seed = randomSourceInternal.convertSeed(input);
final Supplier<String> msg = () -> input.getClass() + " input seed was not converted";
@@ -158,9 +147,12 @@ public class RandomSourceInternalParametricTest {
/**
* Test unsupported input seed types are rejected.
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
*/
- @Test
- public void testCannotConvertUnsupportedSeed() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCannotConvertUnsupportedSeed(RandomSourceInternal randomSourceInternal) {
for (final Object input : UNSUPPORTED_SEEDS) {
try {
randomSourceInternal.convertSeed(input);
@@ -174,9 +166,12 @@ public class RandomSourceInternalParametricTest {
/**
* Test the seed byte size is reported as the size of a int/long primitive for Int/Long
* seed types and a multiple of it for int[]/long[] types.
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
*/
- @Test
- public void testCreateSeedBytesSizeIsPositiveAndMultipleOf4Or8() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCreateSeedBytesSizeIsPositiveAndMultipleOf4Or8(RandomSourceInternal randomSourceInternal) {
// This should be the full length seed
final byte[] seed = randomSourceInternal.createSeedBytes(new SplitMix64(12345L));
@@ -203,9 +198,12 @@ public class RandomSourceInternalParametricTest {
* for new generators. This test forms an additional cross-reference check that the
* seed size in RandomSourceInternal has been correctly set and the size should map to
* the array size in the RandomSource javadoc (if applicable).
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
*/
- @Test
- public void testCreateSeedBytes() {
+ @ParameterizedTest
+ @EnumSource
+ public void testCreateSeedBytes(RandomSourceInternal randomSourceInternal) {
// This should be the full length seed
final byte[] seed = randomSourceInternal.createSeedBytes(new SplitMix64(12345L));
final int size = seed.length;