Made "BitsStreamGenerator" class "Serializable" to allow cloning of
subclasses. Added cloning test for all "RealDistribution" classes.
Thanks to Dennis Hendriks.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1428822 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2013-01-04 12:28:44 +00:00
parent 42673df5b7
commit 185e3033ef
2 changed files with 53 additions and 5 deletions

View File

@ -16,21 +16,26 @@
*/
package org.apache.commons.math3.random;
import java.io.Serializable;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.util.FastMath;
/** Base class for random number generators that generates bits streams.
*
* @version $Id$
* @since 2.0
*/
public abstract class BitsStreamGenerator implements RandomGenerator {
public abstract class BitsStreamGenerator
implements RandomGenerator,
Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = 20130104L;
/** Next gaussian. */
private double nextGaussian;
/** Creates a new random number generator.
/**
* Creates a new random number generator.
*/
public BitsStreamGenerator() {
nextGaussian = Double.NaN;

View File

@ -19,6 +19,11 @@ package org.apache.commons.math3.distribution;
import java.util.ArrayList;
import java.util.Collections;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.apache.commons.math3.TestUtils;
import org.apache.commons.math3.util.FastMath;
@ -371,6 +376,24 @@ public abstract class RealDistributionAbstractTest {
}
@Test
public void testDistributionClone()
throws IOException,
ClassNotFoundException {
// Construct a distribution and initialize its internal random
// generator, using a fixed seed for deterministic results.
distribution.reseedRandomGenerator(123);
distribution.sample();
// Clone the distribution.
final RealDistribution cloned = deepClone();
// Make sure they still produce the same samples.
final double s1 = distribution.sample();
final double s2 = cloned.sample();
Assert.assertEquals(s1, s2, 0d);
}
//------------------ Getters / Setters for test instance data -----------
/**
* @return Returns the cumulativeTestPoints.
@ -464,4 +487,24 @@ public abstract class RealDistributionAbstractTest {
this.tolerance = tolerance;
}
/**
* Serialization and deserialization loop of the {@link #distribution}.
*/
private RealDistribution deepClone()
throws IOException,
ClassNotFoundException {
// Serialize to byte array.
final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
final ObjectOutputStream oOut = new ObjectOutputStream(bOut);
oOut.writeObject(distribution);
final byte[] data = bOut.toByteArray();
// Deserialize from byte array.
final ByteArrayInputStream bIn = new ByteArrayInputStream(data);
final ObjectInputStream oIn = new ObjectInputStream(bIn);
final Object clone = oIn.readObject();
oIn.close();
return (RealDistribution) clone;
}
}