Author: psteitz
Date: Tue Aug 2 19:48:18 2011
New Revision: 1153257
URL: http://svn.apache.org/viewvc?rev=1153257&view=rev
Log:
Refactored RandomGenerator tests.
* Added RandomGeneratorAbstractTest collecting stock tests for RandomGenerator
implementations and running RandomDataTest test cases using RandomDataImpls
constructed from generators under test.
* Added BitsStreamGeneratorTest extending RandomGeneratorAbstractTest to test
abstract method implementations in BitStreamGenerator.
* Changed Mersenne and Well generator tests to extend
RandomGeneratorAbstractTest.
* Improved test coverage (discovering MATH-640) of AbstractRandomGeneratorTest
by making this class extend RandomGeneratorAbstractTest.
All new tests use fixed seeds.
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
(with props)
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/MersenneTwisterTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well1024aTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937aTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937cTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497aTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497bTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well512aTest.java
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/AbstractRandomGeneratorTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
Tue Aug 2 19:48:18 2011
@@ -16,143 +16,32 @@
*/
package org.apache.commons.math.random;
-import org.apache.commons.math.stat.Frequency;
-import org.apache.commons.math.exception.MathIllegalArgumentException;
-import org.apache.commons.math.Retry;
-import org.apache.commons.math.RetryRunner;
-
-import org.junit.runner.RunWith;
-import org.junit.Assert;
-import org.junit.Test;
-
/**
- * Test cases for the AbstractRandomGenerator class
+ * Test cases for the AbstractRandomGenerator class.
*
* @version $Id$
*/
-@RunWith(RetryRunner.class)
-public class AbstractRandomGeneratorTest extends RandomDataTest {
-
- protected TestRandomGenerator testGenerator = new TestRandomGenerator();
-
- public AbstractRandomGeneratorTest() {
- randomData = new RandomDataImpl(testGenerator);
- }
-
+public class AbstractRandomGeneratorTest extends RandomGeneratorAbstractTest {
+
@Override
- @Test
- @Retry(2)
- public void testNextInt() {
- try {
- testGenerator.nextInt(-1);
- Assert.fail("MathIllegalArgumentException expected");
- } catch (MathIllegalArgumentException ex) {
- // ignored
- }
- Frequency freq = new Frequency();
- int value = 0;
- for (int i=0; i<smallSampleSize; i++) {
- value = testGenerator.nextInt(4);
- Assert.assertTrue("nextInt range",(value >= 0) && (value <= 3));
- freq.addValue(value);
- }
- long[] observed = new long[4];
- for (int i=0; i<4; i++) {
- observed[i] = freq.getCount(i);
- }
-
- /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
- * Change to 11.34 for alpha = .01
- */
- Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
- testStatistic.chiSquare(expected,observed) < 16.27);
+ public void testNextInt2() {
+ // Currently broken. Remove this stub when MATH-640 is resolved
}
-
+
@Override
- @Test
- @Retry(2)
- public void testNextLong() {
- long q1 = Long.MAX_VALUE/4;
- long q2 = 2 * q1;
- long q3 = 3 * q1;
-
- Frequency freq = new Frequency();
- long val = 0;
- int value = 0;
- for (int i=0; i<smallSampleSize; i++) {
- val = testGenerator.nextLong();
- if (val < q1) {
- value = 0;
- } else if (val < q2) {
- value = 1;
- } else if (val < q3) {
- value = 2;
- } else {
- value = 3;
- }
- freq.addValue(value);
- }
- long[] observed = new long[4];
- for (int i=0; i<4; i++) {
- observed[i] = freq.getCount(i);
- }
-
- /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
- * Change to 11.34 for alpha = .01
- */
- Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
- testStatistic.chiSquare(expected,observed) < 16.27);
+ public void testNextLong2() {
+ // Currently broken. Remove this stub when MATH-640 is resolved
}
- @Test
- @Retry(2)
- public void testNextBoolean() {
- long halfSampleSize = smallSampleSize / 2;
- double[] expected = {halfSampleSize, halfSampleSize};
- long[] observed = new long[2];
- for (int i=0; i<smallSampleSize; i++) {
- if (testGenerator.nextBoolean()) {
- observed[0]++;
- } else {
- observed[1]++;
- }
- }
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
- * Change to 6.635 for alpha = .01
- */
- Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
- testStatistic.chiSquare(expected,observed) < 10.828);
+ public AbstractRandomGeneratorTest() {
+ super();
}
-
- @Test
- @Retry(2)
- public void testNextFloat() {
- Frequency freq = new Frequency();
- float val = 0;
- int value = 0;
- for (int i=0; i<smallSampleSize; i++) {
- val = testGenerator.nextFloat();
- if (val < 0.25) {
- value = 0;
- } else if (val < 0.5) {
- value = 1;
- } else if (val < 0.75) {
- value = 2;
- } else {
- value = 3;
- }
- freq.addValue(value);
- }
- long[] observed = new long[4];
- for (int i=0; i<4; i++) {
- observed[i] = freq.getCount(i);
- }
-
- /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
- * Change to 11.34 for alpha = .01
- */
- Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
- testStatistic.chiSquare(expected,observed) < 16.27);
+
+ protected RandomGenerator makeGenerator() {
+ RandomGenerator generator = new TestRandomGenerator();
+ generator.setSeed(1000);
+ return generator;
}
+
}
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java?rev=1153257&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
Tue Aug 2 19:48:18 2011
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.random;
+
+import java.util.Random;
+
+/**
+ * Test cases for the BitStreamGenerator class
+ *
+ * @version $Id$
+ */
+
+public class BitsStreamGeneratorTest extends RandomGeneratorAbstractTest {
+
+ public BitsStreamGeneratorTest() {
+ super();
+ }
+
+ protected RandomGenerator makeGenerator() {
+ RandomGenerator generator = new TestBitStreamGenerator();
+ generator.setSeed(1000);
+ return generator;
+ }
+
+ /**
+ * Test BitStreamGenerator using a Random as bit source.
+ */
+ static class TestBitStreamGenerator extends BitsStreamGenerator {
+
+ private BitRandom ran = new BitRandom();
+
+ @Override
+ public void setSeed(int seed) {
+ ran.setSeed(seed);
+ }
+
+ @Override
+ public void setSeed(int[] seed) {
+ ran.setSeed(seed[0]);
+ }
+
+ @Override
+ public void setSeed(long seed) {
+ ran.setSeed((int) seed);
+
+ }
+
+ @Override
+ protected int next(int bits) {
+ return ran.nextBits(bits);
+ }
+ }
+
+ /**
+ * Extend Random to expose next(bits)
+ */
+ @SuppressWarnings("serial")
+ static class BitRandom extends Random {
+ public BitRandom() {
+ super();
+ }
+ public int nextBits(int bits) {
+ return next(bits);
+ }
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision Id
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/MersenneTwisterTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/MersenneTwisterTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/MersenneTwisterTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/MersenneTwisterTest.java
Tue Aug 2 19:48:18 2011
@@ -16,140 +16,19 @@
*/
package org.apache.commons.math.random;
-
-import org.apache.commons.math.stat.descriptive.SummaryStatistics;
-import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.exception.MathIllegalArgumentException;
-
import org.junit.Assert;
import org.junit.Test;
-public class MersenneTwisterTest {
-
- @Test
- public void testGaussian() {
- MersenneTwister mt = new MersenneTwister(42853252100l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 1000; ++i) {
- sample.addValue(mt.nextGaussian());
- }
- Assert.assertEquals(0.0, sample.getMean(), 0.005);
- Assert.assertEquals(1.0, sample.getStandardDeviation(), 0.025);
- }
-
- @Test
- public void testDouble() {
- MersenneTwister mt = new MersenneTwister(195357343514l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 1000; ++i) {
- sample.addValue(mt.nextDouble());
- }
- Assert.assertEquals(0.5, sample.getMean(), 0.02);
- Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
- sample.getStandardDeviation(),
- 0.002);
- }
-
- @Test
- public void testFloat() {
- MersenneTwister mt = new MersenneTwister(4442733263l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 1000; ++i) {
- sample.addValue(mt.nextFloat());
- }
- Assert.assertEquals(0.5, sample.getMean(), 0.01);
- Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
- sample.getStandardDeviation(),
- 0.006);
- }
-
- @Test(expected=MathIllegalArgumentException.class)
- public void testNextIntNeg() {
- new MersenneTwister(1).nextInt(-1);
- }
-
- @Test
- public void testNextIntN() {
- MersenneTwister mt = new MersenneTwister(0x12b8a7412bb25el);
- for (int n = 1; n < 20; ++n) {
- int[] count = new int[n];
- for (int k = 0; k < 10000; ++k) {
- int l = mt.nextInt(n);
- ++count[l];
- Assert.assertTrue(l >= 0);
- Assert.assertTrue(l < n);
- }
- for (int i = 0; i < n; ++i) {
- Assert.assertTrue(n * count[i] > 8600);
- Assert.assertTrue(n * count[i] < 11200);
- }
- }
- }
-
- @Test
- public void testNextInt() {
- MersenneTwister mt = new MersenneTwister(new int[] { 1, 2, 3, 4, 5 });
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextInt() >= 0) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 120);
- }
-
- @Test
- public void testNextLong() {
- MersenneTwister mt = new MersenneTwister(12345);
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextLong() >= 0) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 50);
- }
-
- @Test
- public void testNexBoolean() {
- MersenneTwister mt = new MersenneTwister(76342);
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextBoolean()) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 250);
- }
+public class MersenneTwisterTest extends RandomGeneratorAbstractTest {
- @Test
- public void testNexBytes() {
- MersenneTwister mt = new MersenneTwister(0);
- int[] count = new int[256];
- byte[] bytes = new byte[10];
- for (int k = 0; k < 100000; ++k) {
- mt.nextBytes(bytes);
- for (byte b : bytes) {
- ++count[b + 128];
- }
- }
- int min = Integer.MAX_VALUE;
- int max = Integer.MIN_VALUE;
- for (int c : count) {
- min = FastMath.min(min, c);
- max = FastMath.max(max, c);
- }
- int expected = (100000 * bytes.length) / count.length;
- Assert.assertTrue((expected - 200) < min);
- Assert.assertTrue(max < (expected + 200));
+ @Override
+ protected RandomGenerator makeGenerator() {
+ return new MersenneTwister(100);
}
-
+
+ // TODO: Some of the tests moved up to RandomGeneratorAbstractTest tested
alternative seeding / constructors
+ // Tests exercising these features directly should be added to this class.
+
@Test
public void testMakotoNishimura() {
MersenneTwister mt = new MersenneTwister(new int[] {0x123, 0x234,
0x345, 0x456});
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
Tue Aug 2 19:48:18 2011
@@ -65,6 +65,7 @@ public class RandomDataTest {
public RandomDataTest() {
randomData = new RandomDataImpl();
+ randomData.reSeed(1000);
}
protected final long smallSampleSize = 1000;
@@ -247,9 +248,6 @@ public class RandomDataTest {
@Test
public void testNextPoissonConsistency() throws Exception {
- // Reseed randomGenerator to get fixed sequence
- randomData.reSeed(1000);
-
// Small integral means
for (int i = 1; i < 100; i++) {
checkNextPoissonConsistency(i);
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java?rev=1153257&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
Tue Aug 2 19:48:18 2011
@@ -0,0 +1,280 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.random;
+
+import org.apache.commons.math.TestUtils;
+import org.apache.commons.math.stat.Frequency;
+import org.apache.commons.math.stat.descriptive.SummaryStatistics;
+import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.exception.MathIllegalArgumentException;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Base class for RandomGenerator tests.
+ *
+ * Tests RandomGenerator methods directly and also executes RandomDataTest
+ * test cases against a RandomDataImpl created using the provided generator.
+ *
+ * RandomGenerator test classes should extend this class, implementing
+ * makeGenerator() to provide a concrete generator to test. The generator
+ * returned by makeGenerator should be seeded with a fixed seed.
+ *
+ * @version $Id$
+ */
+
+public abstract class RandomGeneratorAbstractTest extends RandomDataTest {
+
+ /** RandomGenerator under test */
+ protected RandomGenerator generator;
+
+ /**
+ * Override this method in subclasses to provide a concrete generator to
test.
+ * Return a generator seeded with a fixed seed.
+ */
+ protected abstract RandomGenerator makeGenerator();
+
+ /**
+ * Initialize generator and randomData instance in superclass.
+ */
+ public RandomGeneratorAbstractTest() {
+ generator = makeGenerator();
+ randomData = new RandomDataImpl(generator);
+ }
+
+ /**
+ * Set a fixed seed for the tests
+ */
+ @Before
+ public void setUp() {
+ generator = makeGenerator();
+ }
+
+ // Omit secureXxx tests, since they do not use the provided generator
+ @Override
+ public void testNextSecureLong() {}
+
+ @Override
+ public void testNextSecureInt() {}
+
+ @Override
+ public void testNextSecureHex() {}
+
+ @Test
+ public void testNextIntDirect() {
+ try {
+ generator.nextInt(-1);
+ Assert.fail("MathIllegalArgumentException expected");
+ } catch (MathIllegalArgumentException ex) {
+ // ignored
+ }
+ Frequency freq = new Frequency();
+ int value = 0;
+ for (int i=0; i<smallSampleSize; i++) {
+ value = generator.nextInt(4);
+ Assert.assertTrue("nextInt range",(value >= 0) && (value <= 3));
+ freq.addValue(value);
+ }
+ long[] observed = new long[4];
+ for (int i=0; i<4; i++) {
+ observed[i] = freq.getCount(i);
+ }
+
+ /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
+ * Change to 11.34 for alpha = .01
+ */
+ Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
+ testStatistic.chiSquare(expected,observed) < 16.27);
+ }
+
+ @Test
+ public void testNextLongDirect() {
+ long q1 = Long.MAX_VALUE/4;
+ long q2 = 2 * q1;
+ long q3 = 3 * q1;
+
+ Frequency freq = new Frequency();
+ long val = 0;
+ int value = 0;
+ for (int i=0; i<smallSampleSize; i++) {
+ val = generator.nextLong();
+ val = val < 0 ? -val : val;
+ if (val < q1) {
+ value = 0;
+ } else if (val < q2) {
+ value = 1;
+ } else if (val < q3) {
+ value = 2;
+ } else {
+ value = 3;
+ }
+ freq.addValue(value);
+ }
+ long[] observed = new long[4];
+ for (int i=0; i<4; i++) {
+ observed[i] = freq.getCount(i);
+ }
+
+ /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
+ * Change to 11.34 for alpha = .01
+ */
+ Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
+ testStatistic.chiSquare(expected,observed) < 16.27);
+ }
+
+ @Test
+ public void testNextBooleanDirect() {
+ long halfSampleSize = smallSampleSize / 2;
+ double[] expected = {halfSampleSize, halfSampleSize};
+ long[] observed = new long[2];
+ for (int i=0; i<smallSampleSize; i++) {
+ if (generator.nextBoolean()) {
+ observed[0]++;
+ } else {
+ observed[1]++;
+ }
+ }
+ /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
+ * Change to 6.635 for alpha = .01
+ */
+ Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
+ testStatistic.chiSquare(expected,observed) < 10.828);
+ }
+
+ @Test
+ public void testNextFloatDirect() {
+ Frequency freq = new Frequency();
+ float val = 0;
+ int value = 0;
+ for (int i=0; i<smallSampleSize; i++) {
+ val = generator.nextFloat();
+ if (val < 0.25) {
+ value = 0;
+ } else if (val < 0.5) {
+ value = 1;
+ } else if (val < 0.75) {
+ value = 2;
+ } else {
+ value = 3;
+ }
+ freq.addValue(value);
+ }
+ long[] observed = new long[4];
+ for (int i=0; i<4; i++) {
+ observed[i] = freq.getCount(i);
+ }
+
+ /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
+ * Change to 11.34 for alpha = .01
+ */
+ Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times",
+ testStatistic.chiSquare(expected,observed) < 16.27);
+ }
+
+ @Test
+ public void testDoubleDirect() {
+ SummaryStatistics sample = new SummaryStatistics();
+ for (int i = 0; i < 10000; ++i) {
+ sample.addValue(generator.nextDouble());
+ }
+ Assert.assertEquals(0.5, sample.getMean(), 0.02);
+ Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
+ sample.getStandardDeviation(),
+ 0.01);
+ }
+
+ @Test
+ public void testFloatDirect() {
+ SummaryStatistics sample = new SummaryStatistics();
+ for (int i = 0; i < 1000; ++i) {
+ sample.addValue(generator.nextFloat());
+ }
+ Assert.assertEquals(0.5, sample.getMean(), 0.01);
+ Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
+ sample.getStandardDeviation(),
+ 0.01);
+ }
+
+ @Test(expected=MathIllegalArgumentException.class)
+ public void testNextIntNeg() {
+ generator.nextInt(-1);
+ }
+
+ @Test
+ public void testNextInt2() {
+ int walk = 0;
+ for (int k = 0; k < 10000; ++k) {
+ if (generator.nextInt() >= 0) {
+ ++walk;
+ } else {
+ --walk;
+ }
+ }
+ Assert.assertTrue("Walked too far astray: " + walk, FastMath.abs(walk)
< 120);
+ }
+
+ @Test
+ public void testNextLong2() {
+ int walk = 0;
+ for (int k = 0; k < 1000; ++k) {
+ if (generator.nextLong() >= 0) {
+ ++walk;
+ } else {
+ --walk;
+ }
+ }
+ Assert.assertTrue("Walked too far astray: " + walk, FastMath.abs(walk)
< 100);
+ }
+
+ @Test
+ public void testNexBoolean2() {
+ int walk = 0;
+ for (int k = 0; k < 10000; ++k) {
+ if (generator.nextBoolean()) {
+ ++walk;
+ } else {
+ --walk;
+ }
+ }
+ Assert.assertTrue(FastMath.abs(walk) < 250);
+ }
+
+ @Test
+ public void testNexBytes() throws Exception {
+ long[] count = new long[256];
+ byte[] bytes = new byte[10];
+ double[] expected = new double[256];
+ final int sampleSize = 100000;
+
+ for (int i = 0; i < 256; i++) {
+ expected[i] = (double) sampleSize / 265f;
+ }
+
+ for (int k = 0; k < sampleSize; ++k) {
+ generator.nextBytes(bytes);
+ for (byte b : bytes) {
+ ++count[b + 128];
+ }
+ }
+
+ TestUtils.assertChiSquareAccept(expected, count, 0.001);
+
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision Id
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well1024aTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well1024aTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well1024aTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well1024aTest.java
Tue Aug 2 19:48:18 2011
@@ -18,137 +18,15 @@ package org.apache.commons.math.random;
import org.junit.Assert;
-import org.apache.commons.math.stat.descriptive.SummaryStatistics;
-import org.apache.commons.math.exception.MathIllegalArgumentException;
-import org.apache.commons.math.util.FastMath;
import org.junit.Test;
-public class Well1024aTest {
+public class Well1024aTest extends RandomGeneratorAbstractTest {
- @Test
- public void testGaussian() {
- Well1024a mt = new Well1024a(42853252100l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 10000; ++i) {
- sample.addValue(mt.nextGaussian());
- }
- Assert.assertEquals(0.0, sample.getMean(), 0.004);
- Assert.assertEquals(1.0, sample.getStandardDeviation(), 0.003);
- }
-
- @Test
- public void testDouble() {
- Well1024a mt = new Well1024a(195357343514l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 10000; ++i) {
- sample.addValue(mt.nextDouble());
- }
- Assert.assertEquals(0.5, sample.getMean(), 0.0006);
- Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
- sample.getStandardDeviation(),
- 0.002);
- }
-
- @Test
- public void testFloat() {
- Well1024a mt = new Well1024a(4442733263l);
- SummaryStatistics sample = new SummaryStatistics();
- for (int i = 0; i < 10000; ++i) {
- sample.addValue(mt.nextFloat());
- }
- Assert.assertEquals(0.5, sample.getMean(), 0.0001);
- Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)),
- sample.getStandardDeviation(),
- 0.003);
- }
-
- @Test(expected=MathIllegalArgumentException.class)
- public void testNextIntNeg() {
- new Well1024a(1).nextInt(-1);
- }
-
- @Test
- public void testNextIntN() {
- Well1024a mt = new Well1024a(0x12b8a7412bb25el);
- for (int n = 1; n < 20; ++n) {
- int[] count = new int[n];
- for (int k = 0; k < 10000; ++k) {
- int l = mt.nextInt(n);
- ++count[l];
- Assert.assertTrue(l >= 0);
- Assert.assertTrue(l < n);
- }
- for (int i = 0; i < n; ++i) {
- Assert.assertTrue(n * count[i] > 8600);
- Assert.assertTrue(n * count[i] < 11200);
- }
- }
- }
-
- @Test
- public void testNextInt() {
- Well1024a mt = new Well1024a(new int[] { 1, 2, 3, 4, 5 });
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextInt() >= 0) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 70);
+ @Override
+ protected RandomGenerator makeGenerator() {
+ return new Well1024a(100);
}
-
- @Test
- public void testNextLong() {
- Well1024a mt = new Well1024a(12345);
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextLong() >= 0) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 70);
- }
-
- @Test
- public void testNexBoolean() {
- Well1024a mt = new Well1024a(76342);
- int walk = 0;
- for (int k = 0; k < 10000; ++k) {
- if (mt.nextBoolean()) {
- ++walk;
- } else {
- --walk;
- }
- }
- Assert.assertTrue(FastMath.abs(walk) < 180);
- }
-
- @Test
- public void testNexBytes() {
- Well1024a mt = new Well1024a(0);
- int[] count = new int[256];
- byte[] bytes = new byte[10];
- for (int k = 0; k < 1000000; ++k) {
- mt.nextBytes(bytes);
- for (byte b : bytes) {
- ++count[b + 128];
- }
- }
- int min = Integer.MAX_VALUE;
- int max = Integer.MIN_VALUE;
- for (int c : count) {
- min = FastMath.min(min, c);
- max = FastMath.max(max, c);
- }
- int expected = (1000000 * bytes.length) / count.length;
- Assert.assertTrue((expected - 600) < min);
- Assert.assertTrue(max < (expected + 600));
- }
-
+
@Test
public void testReferenceCode() {
Well1024a mt = new Well1024a(new int[] {
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937aTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937aTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937aTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937aTest.java
Tue Aug 2 19:48:18 2011
@@ -19,7 +19,12 @@ package org.apache.commons.math.random;
import org.junit.Assert;
import org.junit.Test;
-public class Well19937aTest {
+public class Well19937aTest extends RandomGeneratorAbstractTest {
+
+ @Override
+ public RandomGenerator makeGenerator() {
+ return new Well19937a(100);
+ }
@Test
public void testReferenceCode() {
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937cTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937cTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937cTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well19937cTest.java
Tue Aug 2 19:48:18 2011
@@ -19,7 +19,12 @@ package org.apache.commons.math.random;
import org.junit.Assert;
import org.junit.Test;
-public class Well19937cTest {
+public class Well19937cTest extends RandomGeneratorAbstractTest {
+
+ @Override
+ public RandomGenerator makeGenerator() {
+ return new Well19937c(100);
+ }
@Test
public void testReferenceCode() {
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497aTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497aTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497aTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497aTest.java
Tue Aug 2 19:48:18 2011
@@ -19,8 +19,13 @@ package org.apache.commons.math.random;
import org.junit.Assert;
import org.junit.Test;
-public class Well44497aTest {
-
+public class Well44497aTest extends RandomGeneratorAbstractTest {
+
+ @Override
+ public RandomGenerator makeGenerator() {
+ return new Well44497a(100);
+ }
+
@Test
public void testReferenceCode() {
int[] base = {
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497bTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497bTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497bTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well44497bTest.java
Tue Aug 2 19:48:18 2011
@@ -19,8 +19,13 @@ package org.apache.commons.math.random;
import org.junit.Assert;
import org.junit.Test;
-public class Well44497bTest {
-
+public class Well44497bTest extends RandomGeneratorAbstractTest {
+
+ @Override
+ public RandomGenerator makeGenerator() {
+ return new Well44497b(100);
+ }
+
@Test
public void testReferenceCode() {
int[] base = {
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well512aTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well512aTest.java?rev=1153257&r1=1153256&r2=1153257&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well512aTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/Well512aTest.java
Tue Aug 2 19:48:18 2011
@@ -19,8 +19,12 @@ package org.apache.commons.math.random;
import org.junit.Assert;
import org.junit.Test;
-public class Well512aTest {
-
+public class Well512aTest extends RandomGeneratorAbstractTest {
+
+ @Override
+ public RandomGenerator makeGenerator() {
+ return new Well512a(100);
+ }
@Test
public void testReferenceCode() {
Well512a mt = new Well512a(new int[] {