From 0484bdb3f61d309ff62da4df4be92cf139d19f6b Mon Sep 17 00:00:00 2001 From: Gilles Date: Wed, 11 May 2016 17:26:37 +0200 Subject: [PATCH] MATH-1335 Use new RNG API. Unit tests rewritten to avoid unnecessary usage of "RandomDataGenerator" (cf. MATH-1341). --- .../math4/util/ArithmeticUtilsTest.java | 18 ++++++------------ .../commons/math4/util/MathUtilsTest.java | 15 ++++++++------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java b/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java index fece22515..2de8ee1a6 100644 --- a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java @@ -22,8 +22,6 @@ import java.math.BigInteger; import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.random.RandomDataGenerator; -import org.apache.commons.math4.util.ArithmeticUtils; import org.junit.Assert; import org.junit.Test; @@ -125,17 +123,13 @@ public class ArithmeticUtilsTest { @Test public void testGcdConsistency() { int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131}; - ArrayList primes = new ArrayList(); - for (int i = 0; i < primeList.length; i++) { - primes.add(Integer.valueOf(primeList[i])); - } - RandomDataGenerator randomData = new RandomDataGenerator(); + for (int i = 0; i < 20; i++) { - Object[] sample = randomData.nextSample(primes, 4); - int p1 = ((Integer) sample[0]).intValue(); - int p2 = ((Integer) sample[1]).intValue(); - int p3 = ((Integer) sample[2]).intValue(); - int p4 = ((Integer) sample[3]).intValue(); + MathArrays.shuffle(primeList); + int p1 = primeList[0]; + int p2 = primeList[1]; + int p3 = primeList[2]; + int p4 = primeList[3]; int i1 = p1 * p2 * p3; int i2 = p1 * p2 * p4; int gcd = p1 * p2; diff --git a/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java b/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java index 8ce9a2d48..f83476fc5 100644 --- a/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java @@ -19,10 +19,8 @@ import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.NotFiniteNumberException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomDataGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.rng.RandomSource; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.math4.util.MathUtils; import org.junit.Assert; import org.junit.Test; @@ -97,23 +95,26 @@ public final class MathUtilsTest { public void testPermutedArrayHash() { double[] original = new double[10]; double[] permuted = new double[10]; - RandomDataGenerator random = new RandomDataGenerator(); + + final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_512_A, + 64925784252L); // Generate 10 distinct random values for (int i = 0; i < 10; i++) { final RealDistribution.Sampler u - = new UniformRealDistribution(i + 0.5, i + 0.75).createSampler(RandomSource.create(RandomSource.WELL_512_A, - 64925784252L)); + = new UniformRealDistribution(i + 0.5, i + 0.75).createSampler(random); original[i] = u.sample(); } // Generate a random permutation, making sure it is not the identity boolean isIdentity = true; do { - int[] permutation = random.nextPermutation(10, 10); + int[] permutation = MathArrays.natural(10); + MathArrays.shuffle(permutation, random); for (int i = 0; i < 10; i++) { if (i != permutation[i]) { isIdentity = false; + break; } permuted[i] = original[permutation[i]]; }