Test new RandomDataGenerator class rather than deprecated RandomImpl.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1454846 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2013-03-10 13:02:04 +00:00
parent 0d057fc6dc
commit 26f3166f11
2 changed files with 25 additions and 21 deletions

View File

@ -51,16 +51,15 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
/** /**
* Test cases for the RandomData class. * Test cases for the RandomDataGenerator class.
* *
* @version $Id$ * @version $Id$
* 2009) $
*/ */
@RunWith(RetryRunner.class) @RunWith(RetryRunner.class)
public class RandomDataTest { public class RandomDataGeneratorTest {
public RandomDataTest() { public RandomDataGeneratorTest() {
randomData = new RandomDataImpl(); randomData = new RandomDataGenerator();
randomData.reSeed(1000); randomData.reSeed(1000);
} }
@ -69,7 +68,7 @@ public class RandomDataTest {
protected final int largeSampleSize = 10000; protected final int largeSampleSize = 10000;
private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" }; "a", "b", "c", "d", "e", "f" };
protected RandomDataImpl randomData = null; protected RandomDataGenerator randomData = null;
protected final ChiSquareTest testStatistic = new ChiSquareTest(); protected final ChiSquareTest testStatistic = new ChiSquareTest();
@Test @Test
@ -828,16 +827,16 @@ public class RandomDataTest {
*/ */
// test reseeding without first using the generators // test reseeding without first using the generators
RandomDataImpl rd = new RandomDataImpl(); RandomDataGenerator rd = new RandomDataGenerator();
rd.reSeed(100); rd.reSeed(100);
rd.nextLong(1, 2); rd.nextLong(1, 2);
RandomDataImpl rd2 = new RandomDataImpl(); RandomDataGenerator rd2 = new RandomDataGenerator();
rd2.reSeedSecure(2000); rd2.reSeedSecure(2000);
rd2.nextSecureLong(1, 2); rd2.nextSecureLong(1, 2);
rd = new RandomDataImpl(); rd = new RandomDataGenerator();
rd.reSeed(); rd.reSeed();
rd.nextLong(1, 2); rd.nextLong(1, 2);
rd2 = new RandomDataImpl(); rd2 = new RandomDataGenerator();
rd2.reSeedSecure(); rd2.reSeedSecure();
rd2.nextSecureLong(1, 2); rd2.nextSecureLong(1, 2);
} }
@ -992,21 +991,23 @@ public class RandomDataTest {
@Test @Test
public void testNextInversionDeviate() { public void testNextInversionDeviate() {
// Set the seed for the default random generator // Set the seed for the default random generator
randomData.reSeed(100); RandomGenerator rg = new Well19937c(100);
RandomDataGenerator rdg = new RandomDataGenerator(rg);
double[] quantiles = new double[10]; double[] quantiles = new double[10];
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
quantiles[i] = randomData.nextUniform(0, 1); quantiles[i] = rdg.nextUniform(0, 1);
} }
// Reseed again so the inversion generator gets the same sequence // Reseed again so the inversion generator gets the same sequence
randomData.reSeed(100); rg.setSeed(100);
BetaDistribution betaDistribution = new BetaDistribution(2, 4); BetaDistribution betaDistribution = new BetaDistribution(rg, 2, 4,
BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
/* /*
* Generate a sequence of deviates using inversion - the distribution function * Generate a sequence of deviates using inversion - the distribution function
* evaluated at the random value from the distribution should match the uniform * evaluated at the random value from the distribution should match the uniform
* random value used to generate it, which is stored in the quantiles[] array. * random value used to generate it, which is stored in the quantiles[] array.
*/ */
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
double value = randomData.nextInversionDeviate(betaDistribution); double value = betaDistribution.sample();
Assert.assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9); Assert.assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9);
} }
} }

View File

@ -41,7 +41,7 @@ import org.junit.Test;
* @version $Id$ * @version $Id$
*/ */
public abstract class RandomGeneratorAbstractTest extends RandomDataTest { public abstract class RandomGeneratorAbstractTest extends RandomDataGeneratorTest {
/** RandomGenerator under test */ /** RandomGenerator under test */
protected RandomGenerator generator; protected RandomGenerator generator;
@ -57,7 +57,7 @@ public abstract class RandomGeneratorAbstractTest extends RandomDataTest {
*/ */
public RandomGeneratorAbstractTest() { public RandomGeneratorAbstractTest() {
generator = makeGenerator(); generator = makeGenerator();
randomData = new RandomDataImpl(generator); randomData = new RandomDataGenerator(generator);
} }
/** /**
@ -161,16 +161,19 @@ public abstract class RandomGeneratorAbstractTest extends RandomDataTest {
} }
} }
@Override // TODO is this supposed to be an override? @Test
@Test(expected=MathIllegalArgumentException.class) public void testNextIntIAE2() {
public void testNextIntIAE() {
try { try {
generator.nextInt(-1); generator.nextInt(-1);
Assert.fail("MathIllegalArgumentException expected"); Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) { } catch (MathIllegalArgumentException ex) {
// ignored // ignored
} }
generator.nextInt(0); try {
generator.nextInt(0);
} catch (MathIllegalArgumentException ex) {
// ignored
}
} }
@Test @Test